3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "serverobject.h"
22 #include "environment.h"
23 #include "inventory.h"
25 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
27 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
31 m_static_exists(false),
32 m_static_block(1337,1337,1337),
38 ServerActiveObject::~ServerActiveObject()
42 ServerActiveObject* ServerActiveObject::create(u8 type,
43 ServerEnvironment *env, u16 id, v3f pos,
44 const std::string &data)
46 // Find factory function
47 core::map<u16, Factory>::Node *n;
48 n = m_types.find(type);
51 // If factory is not found, just return.
52 dstream<<"WARNING: ServerActiveObject: No factory for type="
57 Factory f = n->getValue();
58 ServerActiveObject *object = (*f)(env, id, pos, data);
62 void ServerActiveObject::registerType(u16 type, Factory f)
64 core::map<u16, Factory>::Node *n;
65 n = m_types.find(type);
68 m_types.insert(type, f);
77 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
79 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
80 ServerActiveObject(env, id, pos),
84 ServerActiveObject::registerType(getType(), create);
87 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
88 const std::string &data)
90 return new TestSAO(env, id, pos);
93 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
102 m_base_position.Y += dtime * BS * 2;
103 if(m_base_position.Y > 8*BS)
104 m_base_position.Y = 2*BS;
110 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
114 data += itos(0); // 0 = position
116 data += itos(m_base_position.X);
118 data += itos(m_base_position.Y);
120 data += itos(m_base_position.Z);
122 ActiveObjectMessage aom(getId(), false, data);
123 messages.push_back(aom);
133 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
135 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
136 const std::string inventorystring):
137 ServerActiveObject(env, id, pos),
138 m_inventorystring(inventorystring)
140 dstream<<"Server: ItemSAO created with inventorystring=\""
141 <<m_inventorystring<<"\""<<std::endl;
142 ServerActiveObject::registerType(getType(), create);
145 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
146 const std::string &data)
148 std::istringstream is(data, std::ios::binary);
150 is.read(buf, 1); // read version
151 std::string inventorystring = deSerializeString(is);
152 dstream<<"ItemSAO::create(): Creating item \""
153 <<inventorystring<<"\""<<std::endl;
154 return new ItemSAO(env, id, pos, inventorystring);
157 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages)
161 std::string ItemSAO::getClientInitializationData()
163 dstream<<__FUNCTION_NAME<<std::endl;
165 data += itos(m_base_position.X);
167 data += itos(m_base_position.Y);
169 data += itos(m_base_position.Z);
171 data += m_inventorystring;
175 std::string ItemSAO::getStaticData()
177 dstream<<__FUNCTION_NAME<<std::endl;
178 std::ostringstream os(std::ios::binary);
180 buf[0] = 0; //version
182 os<<serializeString(m_inventorystring);
186 InventoryItem * ItemSAO::createInventoryItem()
189 std::istringstream is(m_inventorystring, std::ios_base::binary);
190 InventoryItem *item = InventoryItem::deSerialize(is);
191 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
192 <<m_inventorystring<<"\" -> item="<<item
196 catch(SerializationError &e)
198 dstream<<__FUNCTION_NAME<<": serialization error: "
199 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;