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 ServerActiveObject::registerType(getType(), create);
150 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
151 const std::string &data)
153 std::istringstream is(data, std::ios::binary);
158 // check if version is supported
161 std::string inventorystring = deSerializeString(is);
162 dstream<<"ItemSAO::create(): Creating item \""
163 <<inventorystring<<"\""<<std::endl;
164 return new ItemSAO(env, id, pos, inventorystring);
167 void ItemSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
168 bool send_recommended)
172 const float interval = 0.2;
173 if(m_move_interval.step(dtime, interval)==false)
177 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
178 collisionMoveResult moveresult;
180 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
181 // Maximum movement without glitches
182 f32 pos_max_d = BS*0.25;
184 if(m_speed_f.getLength()*dtime > pos_max_d)
185 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
186 v3f pos_f = getBasePosition();
187 v3f pos_f_old = pos_f;
188 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
189 box, dtime, pos_f, m_speed_f);
191 if(send_recommended == false)
194 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
196 setBasePosition(pos_f);
197 m_last_sent_position = pos_f;
199 std::ostringstream os(std::ios::binary);
201 // command (0 = update position)
205 writeS32((u8*)buf, m_base_position.X*1000);
207 writeS32((u8*)buf, m_base_position.Y*1000);
209 writeS32((u8*)buf, m_base_position.Z*1000);
211 // create message and add to list
212 ActiveObjectMessage aom(getId(), false, os.str());
213 messages.push_back(aom);
217 std::string ItemSAO::getClientInitializationData()
219 std::ostringstream os(std::ios::binary);
225 writeS32((u8*)buf, m_base_position.X*1000);
227 writeS32((u8*)buf, m_base_position.Y*1000);
229 writeS32((u8*)buf, m_base_position.Z*1000);
232 os<<serializeString(m_inventorystring);
236 std::string ItemSAO::getStaticData()
238 dstream<<__FUNCTION_NAME<<std::endl;
239 std::ostringstream os(std::ios::binary);
245 os<<serializeString(m_inventorystring);
249 InventoryItem * ItemSAO::createInventoryItem()
252 std::istringstream is(m_inventorystring, std::ios_base::binary);
253 InventoryItem *item = InventoryItem::deSerialize(is);
254 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
255 <<m_inventorystring<<"\" -> item="<<item
259 catch(SerializationError &e)
261 dstream<<__FUNCTION_NAME<<": serialization error: "
262 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
273 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
275 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
276 ServerActiveObject(env, id, pos),
280 ServerActiveObject::registerType(getType(), create);
282 m_oldpos = v3f(0,0,0);
283 m_last_sent_position = v3f(0,0,0);
288 m_touching_ground = false;
291 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
292 const std::string &data)
294 std::istringstream is(data, std::ios::binary);
299 // check if version is supported
302 return new RatSAO(env, id, pos);
305 void RatSAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
306 bool send_recommended)
310 if(m_is_active == false)
312 if(m_inactive_interval.step(dtime, 0.5)==false)
329 m_speed_f.Y -= dtime*9.81*BS;
332 Move around if some player is close
334 bool player_is_close = false;
335 // Check connected players
336 core::list<Player*> players = m_env->getPlayers(true);
337 core::list<Player*>::Iterator i;
338 for(i = players.begin();
339 i != players.end(); i++)
342 v3f playerpos = player->getPosition();
343 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
345 player_is_close = true;
350 m_is_active = player_is_close;
352 if(player_is_close == false)
360 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
362 m_speed_f.X = speed * dir.X;
363 m_speed_f.Z = speed * dir.Z;
365 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
372 m_speed_f.Y = 5.0*BS;
380 m_counter2 += (float)(myrand()%100)/100*3.0;
381 m_yaw += ((float)(myrand()%200)-100)/100*180;
382 m_yaw = wrapDegrees(m_yaw);
387 m_oldpos = m_base_position;
390 Move it, with collision detection
393 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
394 collisionMoveResult moveresult;
395 // Maximum movement without glitches
396 f32 pos_max_d = BS*0.25;
398 if(m_speed_f.getLength()*dtime > pos_max_d)
399 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
400 v3f pos_f = getBasePosition();
401 v3f pos_f_old = pos_f;
402 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
403 box, dtime, pos_f, m_speed_f);
404 m_touching_ground = moveresult.touching_ground;
406 setBasePosition(pos_f);
408 if(send_recommended == false)
411 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
413 m_last_sent_position = pos_f;
415 std::ostringstream os(std::ios::binary);
416 // command (0 = update position)
419 writeV3F1000(os, m_base_position);
421 writeF1000(os, m_yaw);
422 // create message and add to list
423 ActiveObjectMessage aom(getId(), false, os.str());
424 messages.push_back(aom);
428 std::string RatSAO::getClientInitializationData()
430 std::ostringstream os(std::ios::binary);
434 writeV3F1000(os, m_base_position);
438 std::string RatSAO::getStaticData()
440 //dstream<<__FUNCTION_NAME<<std::endl;
441 std::ostringstream os(std::ios::binary);
447 InventoryItem* RatSAO::createPickedUpItem()
449 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
450 InventoryItem *item = InventoryItem::deSerialize(is);
459 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
461 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
462 ServerActiveObject(env, id, pos),
466 ServerActiveObject::registerType(getType(), create);
468 m_oldpos = v3f(0,0,0);
469 m_last_sent_position = v3f(0,0,0);
474 m_touching_ground = false;
478 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
479 const std::string &data)
481 std::istringstream is(data, std::ios::binary);
483 u8 version = readU8(is);
486 // check if version is supported
489 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
494 void Oerkki1SAO::step(float dtime, Queue<ActiveObjectMessage> &messages,
495 bool send_recommended)
499 if(m_is_active == false)
501 if(m_inactive_interval.step(dtime, 0.5)==false)
518 m_speed_f.Y -= dtime*9.81*BS;
521 Move around if some player is close
523 bool player_is_close = false;
525 // Check connected players
526 core::list<Player*> players = m_env->getPlayers(true);
527 core::list<Player*>::Iterator i;
528 for(i = players.begin();
529 i != players.end(); i++)
532 v3f playerpos = player->getPosition();
533 if(m_base_position.getDistanceFrom(playerpos) < BS*15.0)
535 player_is_close = true;
536 near_player_pos = playerpos;
541 m_is_active = player_is_close;
543 if(player_is_close == false)
552 v3f ndir = near_player_pos - m_base_position;
554 ndir /= ndir.getLength();
555 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
556 if(nyaw < m_yaw - 180)
558 else if(nyaw > m_yaw + 180)
560 m_yaw = 0.95*m_yaw + 0.05*nyaw;
561 m_yaw = wrapDegrees(m_yaw);
563 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
565 m_speed_f.X = speed * dir.X;
566 m_speed_f.Z = speed * dir.Z;
568 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
576 m_speed_f.Y = 5.0*BS;
584 m_counter2 += (float)(myrand()%100)/100*3.0;
585 //m_yaw += ((float)(myrand()%200)-100)/100*180;
586 m_yaw += ((float)(myrand()%200)-100)/100*90;
587 m_yaw = wrapDegrees(m_yaw);
592 m_oldpos = m_base_position;
595 Move it, with collision detection
598 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
599 collisionMoveResult moveresult;
600 // Maximum movement without glitches
601 f32 pos_max_d = BS*0.25;
603 if(m_speed_f.getLength()*dtime > pos_max_d)
604 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
605 v3f pos_f = getBasePosition();
606 v3f pos_f_old = pos_f;
607 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
608 box, dtime, pos_f, m_speed_f);
609 m_touching_ground = moveresult.touching_ground;
611 setBasePosition(pos_f);
613 if(send_recommended == false)
616 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
618 m_last_sent_position = pos_f;
620 std::ostringstream os(std::ios::binary);
621 // command (0 = update position)
624 writeV3F1000(os, m_base_position);
626 writeF1000(os, m_yaw);
627 // create message and add to list
628 ActiveObjectMessage aom(getId(), false, os.str());
629 messages.push_back(aom);
633 std::string Oerkki1SAO::getClientInitializationData()
635 std::ostringstream os(std::ios::binary);
639 writeV3F1000(os, m_base_position);
643 std::string Oerkki1SAO::getStaticData()
645 //dstream<<__FUNCTION_NAME<<std::endl;
646 std::ostringstream os(std::ios::binary);
654 u16 Oerkki1SAO::punch(const std::string &toolname)