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_pending_deactivation(false),
33 m_static_exists(false),
34 m_static_block(1337,1337,1337),
40 ServerActiveObject::~ServerActiveObject()
44 ServerActiveObject* ServerActiveObject::create(u8 type,
45 ServerEnvironment *env, u16 id, v3f pos,
46 const std::string &data)
48 // Find factory function
49 core::map<u16, Factory>::Node *n;
50 n = m_types.find(type);
53 // If factory is not found, just return.
54 dstream<<"WARNING: ServerActiveObject: No factory for type="
59 Factory f = n->getValue();
60 ServerActiveObject *object = (*f)(env, id, pos, data);
64 void ServerActiveObject::registerType(u16 type, Factory f)
66 core::map<u16, Factory>::Node *n;
67 n = m_types.find(type);
70 m_types.insert(type, f);
79 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
81 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
82 ServerActiveObject(env, id, pos),
86 ServerActiveObject::registerType(getType(), create);
89 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
90 const std::string &data)
92 return new TestSAO(env, id, pos);
95 void TestSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
96 bool send_recommended)
105 m_base_position.Y += dtime * BS * 2;
106 if(m_base_position.Y > 8*BS)
107 m_base_position.Y = 2*BS;
109 if(send_recommended == false)
116 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
120 data += itos(0); // 0 = position
122 data += itos(m_base_position.X);
124 data += itos(m_base_position.Y);
126 data += itos(m_base_position.Z);
128 ActiveObjectMessage aom(getId(), false, data);
129 messages.push_back(aom);
139 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
141 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
142 const std::string inventorystring):
143 ServerActiveObject(env, id, pos),
144 m_inventorystring(inventorystring),
146 m_last_sent_position(0,0,0)
148 ServerActiveObject::registerType(getType(), create);
151 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
152 const std::string &data)
154 std::istringstream is(data, std::ios::binary);
159 // check if version is supported
162 std::string inventorystring = deSerializeString(is);
163 dstream<<"ItemSAO::create(): Creating item \""
164 <<inventorystring<<"\""<<std::endl;
165 return new ItemSAO(env, id, pos, inventorystring);
168 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
169 bool send_recommended)
173 const float interval = 0.2;
174 if(m_move_interval.step(dtime, interval)==false)
178 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
179 collisionMoveResult moveresult;
181 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
182 // Maximum movement without glitches
183 f32 pos_max_d = BS*0.25;
185 if(m_speed_f.getLength()*dtime > pos_max_d)
186 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
187 v3f pos_f = getBasePosition();
188 v3f pos_f_old = pos_f;
189 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
190 box, dtime, pos_f, m_speed_f);
192 if(send_recommended == false)
195 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
197 setBasePosition(pos_f);
198 m_last_sent_position = pos_f;
200 std::ostringstream os(std::ios::binary);
202 // command (0 = update position)
206 writeS32((u8*)buf, m_base_position.X*1000);
208 writeS32((u8*)buf, m_base_position.Y*1000);
210 writeS32((u8*)buf, m_base_position.Z*1000);
212 // create message and add to list
213 ActiveObjectMessage aom(getId(), false, os.str());
214 messages.push_back(aom);
218 std::string ItemSAO::getClientInitializationData()
220 std::ostringstream os(std::ios::binary);
226 writeS32((u8*)buf, m_base_position.X*1000);
228 writeS32((u8*)buf, m_base_position.Y*1000);
230 writeS32((u8*)buf, m_base_position.Z*1000);
233 os<<serializeString(m_inventorystring);
237 std::string ItemSAO::getStaticData()
239 dstream<<__FUNCTION_NAME<<std::endl;
240 std::ostringstream os(std::ios::binary);
246 os<<serializeString(m_inventorystring);
250 InventoryItem * ItemSAO::createInventoryItem()
253 std::istringstream is(m_inventorystring, std::ios_base::binary);
254 InventoryItem *item = InventoryItem::deSerialize(is);
255 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
256 <<m_inventorystring<<"\" -> item="<<item
260 catch(SerializationError &e)
262 dstream<<__FUNCTION_NAME<<": serialization error: "
263 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
274 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
276 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
277 ServerActiveObject(env, id, pos),
281 ServerActiveObject::registerType(getType(), create);
283 m_oldpos = v3f(0,0,0);
284 m_last_sent_position = v3f(0,0,0);
289 m_touching_ground = false;
292 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
293 const std::string &data)
295 std::istringstream is(data, std::ios::binary);
300 // check if version is supported
303 return new RatSAO(env, id, pos);
306 void RatSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
307 bool send_recommended)
311 if(m_is_active == false)
313 if(m_inactive_interval.step(dtime, 0.5)==false)
330 m_speed_f.Y -= dtime*9.81*BS;
333 Move around if some player is close
335 bool player_is_close = false;
336 // Check connected players
337 core::list<Player*> players = m_env->getPlayers(true);
338 core::list<Player*>::Iterator i;
339 for(i = players.begin();
340 i != players.end(); i++)
343 v3f playerpos = player->getPosition();
344 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
346 player_is_close = true;
351 m_is_active = player_is_close;
353 if(player_is_close == false)
361 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
363 m_speed_f.X = speed * dir.X;
364 m_speed_f.Z = speed * dir.Z;
366 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
373 m_speed_f.Y = 5.0*BS;
381 m_counter2 += (float)(myrand()%100)/100*3.0;
382 m_yaw += ((float)(myrand()%200)-100)/100*180;
383 m_yaw = wrapDegrees(m_yaw);
388 m_oldpos = m_base_position;
391 Move it, with collision detection
394 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
395 collisionMoveResult moveresult;
396 // Maximum movement without glitches
397 f32 pos_max_d = BS*0.25;
399 if(m_speed_f.getLength()*dtime > pos_max_d)
400 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
401 v3f pos_f = getBasePosition();
402 v3f pos_f_old = pos_f;
403 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
404 box, dtime, pos_f, m_speed_f);
405 m_touching_ground = moveresult.touching_ground;
407 setBasePosition(pos_f);
409 if(send_recommended == false)
412 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
414 m_last_sent_position = pos_f;
416 std::ostringstream os(std::ios::binary);
417 // command (0 = update position)
420 writeV3F1000(os, m_base_position);
422 writeF1000(os, m_yaw);
423 // create message and add to list
424 ActiveObjectMessage aom(getId(), false, os.str());
425 messages.push_back(aom);
429 std::string RatSAO::getClientInitializationData()
431 std::ostringstream os(std::ios::binary);
435 writeV3F1000(os, m_base_position);
439 std::string RatSAO::getStaticData()
441 //dstream<<__FUNCTION_NAME<<std::endl;
442 std::ostringstream os(std::ios::binary);
448 InventoryItem* RatSAO::createPickedUpItem()
450 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
451 InventoryItem *item = InventoryItem::deSerialize(is);
460 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
462 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
463 ServerActiveObject(env, id, pos),
467 ServerActiveObject::registerType(getType(), create);
469 m_oldpos = v3f(0,0,0);
470 m_last_sent_position = v3f(0,0,0);
475 m_touching_ground = false;
479 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
480 const std::string &data)
482 std::istringstream is(data, std::ios::binary);
484 u8 version = readU8(is);
487 // check if version is supported
490 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
495 void Oerkki1SAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
496 bool send_recommended)
500 if(m_is_active == false)
502 if(m_inactive_interval.step(dtime, 0.5)==false)
519 m_speed_f.Y -= dtime*9.81*BS;
522 Move around if some player is close
524 bool player_is_close = false;
526 // Check connected players
527 core::list<Player*> players = m_env->getPlayers(true);
528 core::list<Player*>::Iterator i;
529 for(i = players.begin();
530 i != players.end(); i++)
533 v3f playerpos = player->getPosition();
534 if(m_base_position.getDistanceFrom(playerpos) < BS*15.0)
536 player_is_close = true;
537 near_player_pos = playerpos;
542 m_is_active = player_is_close;
544 if(player_is_close == false)
553 v3f ndir = near_player_pos - m_base_position;
555 ndir /= ndir.getLength();
556 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
557 if(nyaw < m_yaw - 180)
559 else if(nyaw > m_yaw + 180)
561 m_yaw = 0.95*m_yaw + 0.05*nyaw;
562 m_yaw = wrapDegrees(m_yaw);
564 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
566 m_speed_f.X = speed * dir.X;
567 m_speed_f.Z = speed * dir.Z;
569 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
577 m_speed_f.Y = 5.0*BS;
585 m_counter2 += (float)(myrand()%100)/100*3.0;
586 //m_yaw += ((float)(myrand()%200)-100)/100*180;
587 m_yaw += ((float)(myrand()%200)-100)/100*90;
588 m_yaw = wrapDegrees(m_yaw);
593 m_oldpos = m_base_position;
596 Move it, with collision detection
599 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
600 collisionMoveResult moveresult;
601 // Maximum movement without glitches
602 f32 pos_max_d = BS*0.25;
604 if(m_speed_f.getLength()*dtime > pos_max_d)
605 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
606 v3f pos_f = getBasePosition();
607 v3f pos_f_old = pos_f;
608 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
609 box, dtime, pos_f, m_speed_f);
610 m_touching_ground = moveresult.touching_ground;
612 setBasePosition(pos_f);
614 if(send_recommended == false)
617 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
619 m_last_sent_position = pos_f;
621 std::ostringstream os(std::ios::binary);
622 // command (0 = update position)
625 writeV3F1000(os, m_base_position);
627 writeF1000(os, m_yaw);
628 // create message and add to list
629 ActiveObjectMessage aom(getId(), false, os.str());
630 messages.push_back(aom);
634 std::string Oerkki1SAO::getClientInitializationData()
636 std::ostringstream os(std::ios::binary);
640 writeV3F1000(os, m_base_position);
644 std::string Oerkki1SAO::getStaticData()
646 //dstream<<__FUNCTION_NAME<<std::endl;
647 std::ostringstream os(std::ios::binary);
655 u16 Oerkki1SAO::punch(const std::string &toolname)