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 "content_sao.h"
21 #include "collision.h"
22 #include "environment.h"
24 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
31 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
33 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
34 ServerActiveObject(env, id, pos),
38 ServerActiveObject::registerType(getType(), create);
41 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
42 const std::string &data)
44 return new TestSAO(env, id, pos);
47 void TestSAO::step(float dtime, bool send_recommended)
56 m_base_position.Y += dtime * BS * 2;
57 if(m_base_position.Y > 8*BS)
58 m_base_position.Y = 2*BS;
60 if(send_recommended == false)
67 //dstream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
71 data += itos(0); // 0 = position
73 data += itos(m_base_position.X);
75 data += itos(m_base_position.Y);
77 data += itos(m_base_position.Z);
79 ActiveObjectMessage aom(getId(), false, data);
80 m_messages_out.push_back(aom);
90 ItemSAO proto_ItemSAO(NULL, 0, v3f(0,0,0), "");
92 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
93 const std::string inventorystring):
94 ServerActiveObject(env, id, pos),
95 m_inventorystring(inventorystring),
97 m_last_sent_position(0,0,0)
99 ServerActiveObject::registerType(getType(), create);
102 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, v3f pos,
103 const std::string &data)
105 std::istringstream is(data, std::ios::binary);
110 // check if version is supported
113 std::string inventorystring = deSerializeString(is);
114 dstream<<"ItemSAO::create(): Creating item \""
115 <<inventorystring<<"\""<<std::endl;
116 return new ItemSAO(env, id, pos, inventorystring);
119 void ItemSAO::step(float dtime, bool send_recommended)
123 const float interval = 0.2;
124 if(m_move_interval.step(dtime, interval)==false)
128 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
129 collisionMoveResult moveresult;
131 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
132 // Maximum movement without glitches
133 f32 pos_max_d = BS*0.25;
135 if(m_speed_f.getLength()*dtime > pos_max_d)
136 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
137 v3f pos_f = getBasePosition();
138 v3f pos_f_old = pos_f;
139 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
140 box, dtime, pos_f, m_speed_f);
142 if(send_recommended == false)
145 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
147 setBasePosition(pos_f);
148 m_last_sent_position = pos_f;
150 std::ostringstream os(std::ios::binary);
152 // command (0 = update position)
156 writeS32((u8*)buf, m_base_position.X*1000);
158 writeS32((u8*)buf, m_base_position.Y*1000);
160 writeS32((u8*)buf, m_base_position.Z*1000);
162 // create message and add to list
163 ActiveObjectMessage aom(getId(), false, os.str());
164 m_messages_out.push_back(aom);
168 std::string ItemSAO::getClientInitializationData()
170 std::ostringstream os(std::ios::binary);
176 writeS32((u8*)buf, m_base_position.X*1000);
178 writeS32((u8*)buf, m_base_position.Y*1000);
180 writeS32((u8*)buf, m_base_position.Z*1000);
183 os<<serializeString(m_inventorystring);
187 std::string ItemSAO::getStaticData()
189 dstream<<__FUNCTION_NAME<<std::endl;
190 std::ostringstream os(std::ios::binary);
196 os<<serializeString(m_inventorystring);
200 InventoryItem * ItemSAO::createInventoryItem()
203 std::istringstream is(m_inventorystring, std::ios_base::binary);
204 InventoryItem *item = InventoryItem::deSerialize(is);
205 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
206 <<m_inventorystring<<"\" -> item="<<item
210 catch(SerializationError &e)
212 dstream<<__FUNCTION_NAME<<": serialization error: "
213 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
218 void ItemSAO::rightClick(Player *player)
220 dstream<<__FUNCTION_NAME<<std::endl;
221 InventoryItem *item = createInventoryItem();
225 bool to_be_deleted = item->use(m_env, player);
230 // Reflect changes to the item here
231 m_inventorystring = item->getItemString();
241 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
243 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
244 ServerActiveObject(env, id, pos),
248 ServerActiveObject::registerType(getType(), create);
250 m_oldpos = v3f(0,0,0);
251 m_last_sent_position = v3f(0,0,0);
252 m_yaw = myrand_range(0,PI*2);
256 m_touching_ground = false;
259 ServerActiveObject* RatSAO::create(ServerEnvironment *env, u16 id, v3f pos,
260 const std::string &data)
262 std::istringstream is(data, std::ios::binary);
267 // check if version is supported
270 return new RatSAO(env, id, pos);
273 void RatSAO::step(float dtime, bool send_recommended)
277 if(m_is_active == false)
279 if(m_inactive_interval.step(dtime, 0.5)==false)
296 m_speed_f.Y -= dtime*9.81*BS;
299 Move around if some player is close
301 bool player_is_close = false;
302 // Check connected players
303 core::list<Player*> players = m_env->getPlayers(true);
304 core::list<Player*>::Iterator i;
305 for(i = players.begin();
306 i != players.end(); i++)
309 v3f playerpos = player->getPosition();
310 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
312 player_is_close = true;
317 m_is_active = player_is_close;
319 if(player_is_close == false)
327 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
329 m_speed_f.X = speed * dir.X;
330 m_speed_f.Z = speed * dir.Z;
332 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
339 m_speed_f.Y = 5.0*BS;
347 m_counter2 += (float)(myrand()%100)/100*3.0;
348 m_yaw += ((float)(myrand()%200)-100)/100*180;
349 m_yaw = wrapDegrees(m_yaw);
354 m_oldpos = m_base_position;
357 Move it, with collision detection
360 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
361 collisionMoveResult moveresult;
362 // Maximum movement without glitches
363 f32 pos_max_d = BS*0.25;
365 if(m_speed_f.getLength()*dtime > pos_max_d)
366 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
367 v3f pos_f = getBasePosition();
368 v3f pos_f_old = pos_f;
369 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
370 box, dtime, pos_f, m_speed_f);
371 m_touching_ground = moveresult.touching_ground;
373 setBasePosition(pos_f);
375 if(send_recommended == false)
378 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
380 m_last_sent_position = pos_f;
382 std::ostringstream os(std::ios::binary);
383 // command (0 = update position)
386 writeV3F1000(os, m_base_position);
388 writeF1000(os, m_yaw);
389 // create message and add to list
390 ActiveObjectMessage aom(getId(), false, os.str());
391 m_messages_out.push_back(aom);
395 std::string RatSAO::getClientInitializationData()
397 std::ostringstream os(std::ios::binary);
401 writeV3F1000(os, m_base_position);
405 std::string RatSAO::getStaticData()
407 //dstream<<__FUNCTION_NAME<<std::endl;
408 std::ostringstream os(std::ios::binary);
414 InventoryItem* RatSAO::createPickedUpItem()
416 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
417 InventoryItem *item = InventoryItem::deSerialize(is);
425 // Y is copied, X and Z change is limited
426 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
428 v3f d_wanted = target_speed - speed;
430 f32 dl_wanted = d_wanted.getLength();
432 if(dl > max_increase)
435 v3f d = d_wanted.normalize() * dl;
439 speed.Y = target_speed.Y;
443 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
445 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
446 ServerActiveObject(env, id, pos),
450 ServerActiveObject::registerType(getType(), create);
452 m_oldpos = v3f(0,0,0);
453 m_last_sent_position = v3f(0,0,0);
458 m_touching_ground = false;
460 m_after_jump_timer = 0;
463 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
464 const std::string &data)
466 std::istringstream is(data, std::ios::binary);
468 u8 version = readU8(is);
471 // check if version is supported
474 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
479 void Oerkki1SAO::step(float dtime, bool send_recommended)
483 if(m_is_active == false)
485 if(m_inactive_interval.step(dtime, 0.5)==false)
501 m_after_jump_timer -= dtime;
503 v3f old_speed = m_speed_f;
506 m_speed_f.Y -= dtime*9.81*BS;
509 Move around if some player is close
511 bool player_is_close = false;
512 bool player_is_too_close = false;
514 // Check connected players
515 core::list<Player*> players = m_env->getPlayers(true);
516 core::list<Player*>::Iterator i;
517 for(i = players.begin();
518 i != players.end(); i++)
521 v3f playerpos = player->getPosition();
522 f32 dist = m_base_position.getDistanceFrom(playerpos);
525 player_is_too_close = true;
526 near_player_pos = playerpos;
529 else if(dist < BS*15.0)
531 player_is_close = true;
532 near_player_pos = playerpos;
536 m_is_active = player_is_close;
538 v3f target_speed = m_speed_f;
542 target_speed = v3f(0,0,0);
548 v3f ndir = near_player_pos - m_base_position;
552 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
553 if(nyaw < m_yaw - 180)
555 else if(nyaw > m_yaw + 180)
557 m_yaw = 0.95*m_yaw + 0.05*nyaw;
558 m_yaw = wrapDegrees(m_yaw);
562 if((m_touching_ground || m_after_jump_timer > 0.0)
563 && !player_is_too_close)
565 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
566 target_speed.X = speed * dir.X;
567 target_speed.Z = speed * dir.Z;
570 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
578 target_speed.Y = 5.0*BS;
579 m_after_jump_timer = 1.0;
587 m_counter2 += (float)(myrand()%100)/100*3.0;
588 //m_yaw += ((float)(myrand()%200)-100)/100*180;
589 m_yaw += ((float)(myrand()%200)-100)/100*90;
590 m_yaw = wrapDegrees(m_yaw);
595 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
596 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
598 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
600 m_oldpos = m_base_position;
603 Move it, with collision detection
606 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
607 collisionMoveResult moveresult;
608 // Maximum movement without glitches
609 f32 pos_max_d = BS*0.25;
611 if(m_speed_f.getLength()*dtime > pos_max_d)
612 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
613 v3f pos_f = getBasePosition();
614 v3f pos_f_old = pos_f;
615 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
616 box, dtime, pos_f, m_speed_f);
617 m_touching_ground = moveresult.touching_ground;
619 // Do collision damage
620 float tolerance = BS*12;
621 float factor = BS*0.5;
622 v3f speed_diff = old_speed - m_speed_f;
623 // Increase effect in X and Z
626 float vel = speed_diff.getLength();
629 f32 damage_f = (vel - tolerance)/BS*factor;
630 u16 damage = (u16)(damage_f+0.5);
634 setBasePosition(pos_f);
636 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
639 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
641 m_last_sent_position = pos_f;
643 std::ostringstream os(std::ios::binary);
644 // command (0 = update position)
647 writeV3F1000(os, m_base_position);
649 writeF1000(os, m_yaw);
650 // create message and add to list
651 ActiveObjectMessage aom(getId(), false, os.str());
652 m_messages_out.push_back(aom);
656 std::string Oerkki1SAO::getClientInitializationData()
658 std::ostringstream os(std::ios::binary);
662 writeV3F1000(os, m_base_position);
666 std::string Oerkki1SAO::getStaticData()
668 //dstream<<__FUNCTION_NAME<<std::endl;
669 std::ostringstream os(std::ios::binary);
677 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
679 m_speed_f += dir*12*BS;
686 void Oerkki1SAO::doDamage(u16 d)
688 dstream<<"oerkki damage: "<<d<<std::endl;
702 std::ostringstream os(std::ios::binary);
703 // command (1 = damage)
707 // create message and add to list
708 ActiveObjectMessage aom(getId(), false, os.str());
709 m_messages_out.push_back(aom);
718 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
720 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
721 ServerActiveObject(env, id, pos),
725 ServerActiveObject::registerType(getType(), create);
727 m_oldpos = v3f(0,0,0);
728 m_last_sent_position = v3f(0,0,0);
733 m_touching_ground = false;
736 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
737 const std::string &data)
739 std::istringstream is(data, std::ios::binary);
744 // check if version is supported
747 return new FireflySAO(env, id, pos);
750 void FireflySAO::step(float dtime, bool send_recommended)
754 if(m_is_active == false)
756 if(m_inactive_interval.step(dtime, 0.5)==false)
764 // Apply (less) gravity
765 m_speed_f.Y -= dtime*3*BS;
768 Move around if some player is close
770 bool player_is_close = false;
771 // Check connected players
772 core::list<Player*> players = m_env->getPlayers(true);
773 core::list<Player*>::Iterator i;
774 for(i = players.begin();
775 i != players.end(); i++)
778 v3f playerpos = player->getPosition();
779 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
781 player_is_close = true;
786 m_is_active = player_is_close;
788 if(player_is_close == false)
796 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
798 m_speed_f.X = speed * dir.X;
799 m_speed_f.Z = speed * dir.Z;
801 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
808 m_speed_f.Y = 5.0*BS;
816 m_counter2 += (float)(myrand()%100)/100*3.0;
817 m_yaw += ((float)(myrand()%200)-100)/100*180;
818 m_yaw = wrapDegrees(m_yaw);
823 m_oldpos = m_base_position;
826 Move it, with collision detection
829 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
830 collisionMoveResult moveresult;
831 // Maximum movement without glitches
832 f32 pos_max_d = BS*0.25;
834 if(m_speed_f.getLength()*dtime > pos_max_d)
835 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
836 v3f pos_f = getBasePosition();
837 v3f pos_f_old = pos_f;
838 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
839 box, dtime, pos_f, m_speed_f);
840 m_touching_ground = moveresult.touching_ground;
842 setBasePosition(pos_f);
844 if(send_recommended == false)
847 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
849 m_last_sent_position = pos_f;
851 std::ostringstream os(std::ios::binary);
852 // command (0 = update position)
855 writeV3F1000(os, m_base_position);
857 writeF1000(os, m_yaw);
858 // create message and add to list
859 ActiveObjectMessage aom(getId(), false, os.str());
860 m_messages_out.push_back(aom);
864 std::string FireflySAO::getClientInitializationData()
866 std::ostringstream os(std::ios::binary);
870 writeV3F1000(os, m_base_position);
874 std::string FireflySAO::getStaticData()
876 //dstream<<__FUNCTION_NAME<<std::endl;
877 std::ostringstream os(std::ios::binary);
883 InventoryItem* FireflySAO::createPickedUpItem()
885 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
886 InventoryItem *item = InventoryItem::deSerialize(is);