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"
24 #include "collision.h"
26 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
28 ServerActiveObject::ServerActiveObject(ServerEnvironment *env, u16 id, v3f pos):
32 m_static_exists(false),
33 m_static_block(1337,1337,1337),
39 ServerActiveObject::~ServerActiveObject()
43 ServerActiveObject* ServerActiveObject::create(u8 type,
44 ServerEnvironment *env, u16 id, v3f pos,
45 const std::string &data)
47 // Find factory function
48 core::map<u16, Factory>::Node *n;
49 n = m_types.find(type);
52 // If factory is not found, just return.
53 dstream<<"WARNING: ServerActiveObject: No factory for type="
58 Factory f = n->getValue();
59 ServerActiveObject *object = (*f)(env, id, pos, data);
63 void ServerActiveObject::registerType(u16 type, Factory f)
65 core::map<u16, Factory>::Node *n;
66 n = m_types.find(type);
69 m_types.insert(type, f);
78 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
80 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
81 ServerActiveObject(env, id, pos),
85 ServerActiveObject::registerType(getType(), create);
88 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
89 const std::string &data)
91 return new TestSAO(env, id, pos);
94 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
95 bool send_recommended)
104 m_base_position.Y += dtime * BS * 2;
105 if(m_base_position.Y > 8*BS)
106 m_base_position.Y = 2*BS;
108 if(send_recommended == false)
115 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
119 data += itos(0); // 0 = position
121 data += itos(m_base_position.X);
123 data += itos(m_base_position.Y);
125 data += itos(m_base_position.Z);
127 ActiveObjectMessage aom(getId(), false, data);
128 messages.push_back(aom);
138 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
140 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
141 const std::string inventorystring):
142 ServerActiveObject(env, id, pos),
143 m_inventorystring(inventorystring),
145 m_last_sent_position(0,0,0)
147 dstream<<"Server: ItemSAO created with inventorystring=\""
148 <<m_inventorystring<<"\""<<std::endl;
149 ServerActiveObject::registerType(getType(), create);
152 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
153 const std::string &data)
155 std::istringstream is(data, std::ios::binary);
160 // check if version is supported
163 std::string inventorystring = deSerializeString(is);
164 dstream<<"ItemSAO::create(): Creating item \""
165 <<inventorystring<<"\""<<std::endl;
166 return new ItemSAO(env, id, pos, inventorystring);
169 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
170 bool send_recommended)
172 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
173 collisionMoveResult moveresult;
175 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
176 // Maximum movement without glitches
177 f32 pos_max_d = BS*0.25;
179 if(m_speed_f.getLength()*dtime > pos_max_d)
180 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
181 v3f pos_f = getBasePosition();
182 v3f pos_f_old = pos_f;
183 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
184 box, dtime, pos_f, m_speed_f);
186 if(send_recommended == false)
189 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
191 setBasePosition(pos_f);
192 m_last_sent_position = pos_f;
194 std::ostringstream os(std::ios::binary);
196 // command (0 = update position)
200 writeS32((u8*)buf, m_base_position.X*1000);
202 writeS32((u8*)buf, m_base_position.Y*1000);
204 writeS32((u8*)buf, m_base_position.Z*1000);
206 // create message and add to list
207 ActiveObjectMessage aom(getId(), false, os.str());
208 messages.push_back(aom);
212 std::string ItemSAO::getClientInitializationData()
214 std::ostringstream os(std::ios::binary);
220 writeS32((u8*)buf, m_base_position.X*1000);
222 writeS32((u8*)buf, m_base_position.Y*1000);
224 writeS32((u8*)buf, m_base_position.Z*1000);
227 os<<serializeString(m_inventorystring);
231 std::string ItemSAO::getStaticData()
233 dstream<<__FUNCTION_NAME<<std::endl;
234 std::ostringstream os(std::ios::binary);
240 os<<serializeString(m_inventorystring);
244 InventoryItem * ItemSAO::createInventoryItem()
247 std::istringstream is(m_inventorystring, std::ios_base::binary);
248 InventoryItem *item = InventoryItem::deSerialize(is);
249 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
250 <<m_inventorystring<<"\" -> item="<<item
254 catch(SerializationError &e)
256 dstream<<__FUNCTION_NAME<<": serialization error: "
257 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
268 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
270 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
271 ServerActiveObject(env, id, pos),
274 dstream<<"Server: RatSAO created"<<std::endl;
275 ServerActiveObject::registerType(getType(), create);
277 m_oldpos = v3f(0,0,0);
278 m_last_sent_position = v3f(0,0,0);
283 m_touching_ground = false;
286 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
287 const std::string &data)
289 std::istringstream is(data, std::ios::binary);
294 // check if version is supported
297 return new RatSAO(env, id, pos);
300 void RatSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
301 bool send_recommended)
316 m_speed_f.Y -= dtime*9.81*BS;
319 Move around if some player is close
321 bool player_is_close = false;
322 // Check connected players
323 core::list<Player*> players = m_env->getPlayers(true);
324 core::list<Player*>::Iterator i;
325 for(i = players.begin();
326 i != players.end(); i++)
329 v3f playerpos = player->getPosition();
330 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
332 player_is_close = true;
337 if(player_is_close == false)
345 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
347 m_speed_f.X = speed * dir.X;
348 m_speed_f.Z = speed * dir.Z;
350 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
357 m_speed_f.Y = 5.0*BS;
365 m_counter2 += (float)(myrand()%100)/100*3.0;
366 m_yaw += ((float)(myrand()%200)-100)/100*180;
367 m_yaw = wrapDegrees(m_yaw);
372 m_oldpos = m_base_position;
375 Move it, with collision detection
378 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
379 collisionMoveResult moveresult;
380 // Maximum movement without glitches
381 f32 pos_max_d = BS*0.25;
383 if(m_speed_f.getLength()*dtime > pos_max_d)
384 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
385 v3f pos_f = getBasePosition();
386 v3f pos_f_old = pos_f;
387 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
388 box, dtime, pos_f, m_speed_f);
389 m_touching_ground = moveresult.touching_ground;
391 setBasePosition(pos_f);
393 if(send_recommended == false)
396 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
398 m_last_sent_position = pos_f;
400 std::ostringstream os(std::ios::binary);
401 // command (0 = update position)
404 writeV3F1000(os, m_base_position);
406 writeF1000(os, m_yaw);
407 // create message and add to list
408 ActiveObjectMessage aom(getId(), false, os.str());
409 messages.push_back(aom);
413 std::string RatSAO::getClientInitializationData()
415 std::ostringstream os(std::ios::binary);
419 writeV3F1000(os, m_base_position);
423 std::string RatSAO::getStaticData()
425 dstream<<__FUNCTION_NAME<<std::endl;
426 std::ostringstream os(std::ios::binary);
432 InventoryItem* RatSAO::createPickedUpItem()
434 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
435 InventoryItem *item = InventoryItem::deSerialize(is);