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 #include "main.h" // For g_profiler
26 #include "serialization.h" // For compressZlib
28 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
30 /* Some helper functions */
32 // Y is copied, X and Z change is limited
33 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
35 v3f d_wanted = target_speed - speed;
37 f32 dl_wanted = d_wanted.getLength();
42 v3f d = d_wanted.normalize() * dl;
46 speed.Y = target_speed.Y;
54 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
56 TestSAO::TestSAO(ServerEnvironment *env, v3f pos):
57 ServerActiveObject(env, pos),
61 ServerActiveObject::registerType(getType(), create);
64 ServerActiveObject* TestSAO::create(ServerEnvironment *env, v3f pos,
65 const std::string &data)
67 return new TestSAO(env, pos);
70 void TestSAO::step(float dtime, bool send_recommended)
79 m_base_position.Y += dtime * BS * 2;
80 if(m_base_position.Y > 8*BS)
81 m_base_position.Y = 2*BS;
83 if(send_recommended == false)
93 data += itos(0); // 0 = position
95 data += itos(m_base_position.X);
97 data += itos(m_base_position.Y);
99 data += itos(m_base_position.Z);
101 ActiveObjectMessage aom(getId(), false, data);
102 m_messages_out.push_back(aom);
112 ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), "");
114 ItemSAO::ItemSAO(ServerEnvironment *env, v3f pos,
115 const std::string inventorystring):
116 ServerActiveObject(env, pos),
117 m_inventorystring(inventorystring),
119 m_last_sent_position(0,0,0)
121 ServerActiveObject::registerType(getType(), create);
124 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, v3f pos,
125 const std::string &data)
127 std::istringstream is(data, std::ios::binary);
132 // check if version is supported
135 std::string inventorystring = deSerializeString(is);
136 infostream<<"ItemSAO::create(): Creating item \""
137 <<inventorystring<<"\""<<std::endl;
138 return new ItemSAO(env, pos, inventorystring);
141 void ItemSAO::step(float dtime, bool send_recommended)
143 ScopeProfiler sp2(g_profiler, "ItemSAO::step avg", SPT_AVG);
147 const float interval = 0.2;
148 if(m_move_interval.step(dtime, interval)==false)
152 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
153 collisionMoveResult moveresult;
155 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
156 // Maximum movement without glitches
157 f32 pos_max_d = BS*0.25;
159 if(m_speed_f.getLength()*dtime > pos_max_d)
160 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
161 v3f pos_f = getBasePosition();
162 v3f pos_f_old = pos_f;
163 IGameDef *gamedef = m_env->getGameDef();
164 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
165 pos_max_d, box, dtime, pos_f, m_speed_f);
167 if(send_recommended == false)
170 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
172 setBasePosition(pos_f);
173 m_last_sent_position = pos_f;
175 std::ostringstream os(std::ios::binary);
177 // command (0 = update position)
181 writeS32((u8*)buf, m_base_position.X*1000);
183 writeS32((u8*)buf, m_base_position.Y*1000);
185 writeS32((u8*)buf, m_base_position.Z*1000);
187 // create message and add to list
188 ActiveObjectMessage aom(getId(), false, os.str());
189 m_messages_out.push_back(aom);
193 std::string ItemSAO::getClientInitializationData()
195 std::ostringstream os(std::ios::binary);
201 writeS32((u8*)buf, m_base_position.X*1000);
203 writeS32((u8*)buf, m_base_position.Y*1000);
205 writeS32((u8*)buf, m_base_position.Z*1000);
208 os<<serializeString(m_inventorystring);
212 std::string ItemSAO::getStaticData()
214 infostream<<__FUNCTION_NAME<<std::endl;
215 std::ostringstream os(std::ios::binary);
221 os<<serializeString(m_inventorystring);
225 InventoryItem * ItemSAO::createInventoryItem()
228 std::istringstream is(m_inventorystring, std::ios_base::binary);
229 IGameDef *gamedef = m_env->getGameDef();
230 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
231 infostream<<__FUNCTION_NAME<<": m_inventorystring=\""
232 <<m_inventorystring<<"\" -> item="<<item
236 catch(SerializationError &e)
238 infostream<<__FUNCTION_NAME<<": serialization error: "
239 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
244 void ItemSAO::punch(ServerActiveObject *puncher)
246 InventoryItem *item = createInventoryItem();
247 bool fits = puncher->addToInventory(item);
254 void ItemSAO::rightClick(ServerActiveObject *clicker)
256 infostream<<__FUNCTION_NAME<<std::endl;
257 InventoryItem *item = createInventoryItem();
261 bool to_be_deleted = item->use(m_env, clicker);
266 // Reflect changes to the item here
267 m_inventorystring = item->getItemString();
269 delete item; // Delete temporary item
277 RatSAO proto_RatSAO(NULL, v3f(0,0,0));
279 RatSAO::RatSAO(ServerEnvironment *env, v3f pos):
280 ServerActiveObject(env, pos),
284 ServerActiveObject::registerType(getType(), create);
286 m_oldpos = v3f(0,0,0);
287 m_last_sent_position = v3f(0,0,0);
288 m_yaw = myrand_range(0,PI*2);
292 m_touching_ground = false;
295 ServerActiveObject* RatSAO::create(ServerEnvironment *env, v3f pos,
296 const std::string &data)
298 std::istringstream is(data, std::ios::binary);
303 // check if version is supported
306 return new RatSAO(env, pos);
309 void RatSAO::step(float dtime, bool send_recommended)
311 ScopeProfiler sp2(g_profiler, "RatSAO::step avg", SPT_AVG);
315 if(m_is_active == false)
317 if(m_inactive_interval.step(dtime, 0.5)==false)
334 m_speed_f.Y -= dtime*9.81*BS;
337 Move around if some player is close
339 bool player_is_close = false;
340 // Check connected players
341 core::list<Player*> players = m_env->getPlayers(true);
342 core::list<Player*>::Iterator i;
343 for(i = players.begin();
344 i != players.end(); i++)
347 v3f playerpos = player->getPosition();
348 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
350 player_is_close = true;
355 m_is_active = player_is_close;
357 if(player_is_close == false)
365 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
367 m_speed_f.X = speed * dir.X;
368 m_speed_f.Z = speed * dir.Z;
370 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
377 m_speed_f.Y = 5.0*BS;
385 m_counter2 += (float)(myrand()%100)/100*3.0;
386 m_yaw += ((float)(myrand()%200)-100)/100*180;
387 m_yaw = wrapDegrees(m_yaw);
392 m_oldpos = m_base_position;
395 Move it, with collision detection
398 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
399 collisionMoveResult moveresult;
400 // Maximum movement without glitches
401 f32 pos_max_d = BS*0.25;
403 if(m_speed_f.getLength()*dtime > pos_max_d)
404 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
405 v3f pos_f = getBasePosition();
406 v3f pos_f_old = pos_f;
407 IGameDef *gamedef = m_env->getGameDef();
408 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
409 pos_max_d, box, dtime, pos_f, m_speed_f);
410 m_touching_ground = moveresult.touching_ground;
412 setBasePosition(pos_f);
414 if(send_recommended == false)
417 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
419 m_last_sent_position = pos_f;
421 std::ostringstream os(std::ios::binary);
422 // command (0 = update position)
425 writeV3F1000(os, m_base_position);
427 writeF1000(os, m_yaw);
428 // create message and add to list
429 ActiveObjectMessage aom(getId(), false, os.str());
430 m_messages_out.push_back(aom);
434 std::string RatSAO::getClientInitializationData()
436 std::ostringstream os(std::ios::binary);
440 writeV3F1000(os, m_base_position);
444 std::string RatSAO::getStaticData()
446 //infostream<<__FUNCTION_NAME<<std::endl;
447 std::ostringstream os(std::ios::binary);
453 void RatSAO::punch(ServerActiveObject *puncher)
455 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
456 IGameDef *gamedef = m_env->getGameDef();
457 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
458 bool fits = puncher->addToInventory(item);
470 Oerkki1SAO proto_Oerkki1SAO(NULL, v3f(0,0,0));
472 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, v3f pos):
473 ServerActiveObject(env, pos),
477 ServerActiveObject::registerType(getType(), create);
479 m_oldpos = v3f(0,0,0);
480 m_last_sent_position = v3f(0,0,0);
485 m_touching_ground = false;
487 m_after_jump_timer = 0;
490 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, v3f pos,
491 const std::string &data)
493 std::istringstream is(data, std::ios::binary);
495 u8 version = readU8(is);
498 // check if version is supported
501 Oerkki1SAO *o = new Oerkki1SAO(env, pos);
506 void Oerkki1SAO::step(float dtime, bool send_recommended)
508 ScopeProfiler sp2(g_profiler, "Oerkki1SAO::step avg", SPT_AVG);
512 if(m_is_active == false)
514 if(m_inactive_interval.step(dtime, 0.5)==false)
530 m_after_jump_timer -= dtime;
532 v3f old_speed = m_speed_f;
535 m_speed_f.Y -= dtime*9.81*BS;
538 Move around if some player is close
540 bool player_is_close = false;
541 bool player_is_too_close = false;
543 // Check connected players
544 core::list<Player*> players = m_env->getPlayers(true);
545 core::list<Player*>::Iterator i;
546 for(i = players.begin();
547 i != players.end(); i++)
550 v3f playerpos = player->getPosition();
551 f32 dist = m_base_position.getDistanceFrom(playerpos);
556 player_is_too_close = true;
557 near_player_pos = playerpos;
559 else if(dist < BS*15.0 && !player_is_too_close)
561 player_is_close = true;
562 near_player_pos = playerpos;
566 m_is_active = player_is_close;
568 v3f target_speed = m_speed_f;
572 target_speed = v3f(0,0,0);
578 v3f ndir = near_player_pos - m_base_position;
582 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
583 if(nyaw < m_yaw - 180)
585 else if(nyaw > m_yaw + 180)
587 m_yaw = 0.95*m_yaw + 0.05*nyaw;
588 m_yaw = wrapDegrees(m_yaw);
592 if((m_touching_ground || m_after_jump_timer > 0.0)
593 && !player_is_too_close)
595 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
596 target_speed.X = speed * dir.X;
597 target_speed.Z = speed * dir.Z;
600 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
608 target_speed.Y = 5.0*BS;
609 m_after_jump_timer = 1.0;
617 m_counter2 += (float)(myrand()%100)/100*3.0;
618 //m_yaw += ((float)(myrand()%200)-100)/100*180;
619 m_yaw += ((float)(myrand()%200)-100)/100*90;
620 m_yaw = wrapDegrees(m_yaw);
625 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
626 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
628 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
630 m_oldpos = m_base_position;
633 Move it, with collision detection
636 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
637 collisionMoveResult moveresult;
638 // Maximum movement without glitches
639 f32 pos_max_d = BS*0.25;
641 if(m_speed_f.getLength()*dtime > pos_max_d)
642 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
643 v3f pos_f = getBasePosition();
644 v3f pos_f_old = pos_f;
645 IGameDef *gamedef = m_env->getGameDef();
646 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
647 pos_max_d, box, dtime, pos_f, m_speed_f);
648 m_touching_ground = moveresult.touching_ground;
650 // Do collision damage
651 float tolerance = BS*30;
652 float factor = BS*0.5;
653 v3f speed_diff = old_speed - m_speed_f;
654 // Increase effect in X and Z
657 float vel = speed_diff.getLength();
660 f32 damage_f = (vel - tolerance)/BS*factor;
661 u16 damage = (u16)(damage_f+0.5);
665 setBasePosition(pos_f);
667 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
670 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
672 m_last_sent_position = pos_f;
674 std::ostringstream os(std::ios::binary);
675 // command (0 = update position)
678 writeV3F1000(os, m_base_position);
680 writeF1000(os, m_yaw);
681 // create message and add to list
682 ActiveObjectMessage aom(getId(), false, os.str());
683 m_messages_out.push_back(aom);
687 std::string Oerkki1SAO::getClientInitializationData()
689 std::ostringstream os(std::ios::binary);
693 writeV3F1000(os, m_base_position);
697 std::string Oerkki1SAO::getStaticData()
699 //infostream<<__FUNCTION_NAME<<std::endl;
700 std::ostringstream os(std::ios::binary);
708 void Oerkki1SAO::punch(ServerActiveObject *puncher)
710 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
712 std::string toolname = "";
713 InventoryItem *item = puncher->getWieldedItem();
714 if(item && (std::string)item->getName() == "ToolItem"){
715 ToolItem *titem = (ToolItem*)item;
716 toolname = titem->getToolName();
719 m_speed_f += dir*12*BS;
722 /* See tool names in inventory.h */
723 if(toolname == "WSword")
725 if(toolname == "STSword")
727 if(toolname == "SteelSword")
729 if(toolname == "STAxe")
731 if(toolname == "SteelAxe")
733 if(toolname == "SteelPick")
737 puncher->damageWieldedItem(65536/100);
740 void Oerkki1SAO::doDamage(u16 d)
742 infostream<<"oerkki damage: "<<d<<std::endl;
756 std::ostringstream os(std::ios::binary);
757 // command (1 = damage)
761 // create message and add to list
762 ActiveObjectMessage aom(getId(), false, os.str());
763 m_messages_out.push_back(aom);
772 FireflySAO proto_FireflySAO(NULL, v3f(0,0,0));
774 FireflySAO::FireflySAO(ServerEnvironment *env, v3f pos):
775 ServerActiveObject(env, pos),
779 ServerActiveObject::registerType(getType(), create);
781 m_oldpos = v3f(0,0,0);
782 m_last_sent_position = v3f(0,0,0);
787 m_touching_ground = false;
790 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, v3f pos,
791 const std::string &data)
793 std::istringstream is(data, std::ios::binary);
798 // check if version is supported
801 return new FireflySAO(env, pos);
804 void FireflySAO::step(float dtime, bool send_recommended)
806 ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG);
810 if(m_is_active == false)
812 if(m_inactive_interval.step(dtime, 0.5)==false)
820 // Apply (less) gravity
821 m_speed_f.Y -= dtime*3*BS;
824 Move around if some player is close
826 bool player_is_close = false;
827 // Check connected players
828 core::list<Player*> players = m_env->getPlayers(true);
829 core::list<Player*>::Iterator i;
830 for(i = players.begin();
831 i != players.end(); i++)
834 v3f playerpos = player->getPosition();
835 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
837 player_is_close = true;
842 m_is_active = player_is_close;
844 if(player_is_close == false)
852 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
854 m_speed_f.X = speed * dir.X;
855 m_speed_f.Z = speed * dir.Z;
857 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
864 m_speed_f.Y = 5.0*BS;
872 m_counter2 += (float)(myrand()%100)/100*3.0;
873 m_yaw += ((float)(myrand()%200)-100)/100*180;
874 m_yaw = wrapDegrees(m_yaw);
879 m_oldpos = m_base_position;
882 Move it, with collision detection
885 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
886 collisionMoveResult moveresult;
887 // Maximum movement without glitches
888 f32 pos_max_d = BS*0.25;
890 if(m_speed_f.getLength()*dtime > pos_max_d)
891 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
892 v3f pos_f = getBasePosition();
893 v3f pos_f_old = pos_f;
894 IGameDef *gamedef = m_env->getGameDef();
895 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
896 pos_max_d, box, dtime, pos_f, m_speed_f);
897 m_touching_ground = moveresult.touching_ground;
899 setBasePosition(pos_f);
901 if(send_recommended == false)
904 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
906 m_last_sent_position = pos_f;
908 std::ostringstream os(std::ios::binary);
909 // command (0 = update position)
912 writeV3F1000(os, m_base_position);
914 writeF1000(os, m_yaw);
915 // create message and add to list
916 ActiveObjectMessage aom(getId(), false, os.str());
917 m_messages_out.push_back(aom);
921 std::string FireflySAO::getClientInitializationData()
923 std::ostringstream os(std::ios::binary);
927 writeV3F1000(os, m_base_position);
931 std::string FireflySAO::getStaticData()
933 //infostream<<__FUNCTION_NAME<<std::endl;
934 std::ostringstream os(std::ios::binary);
940 InventoryItem* FireflySAO::createPickedUpItem()
942 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
943 IGameDef *gamedef = m_env->getGameDef();
944 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
953 MobV2SAO proto_MobV2SAO(NULL, v3f(0,0,0), NULL);
955 MobV2SAO::MobV2SAO(ServerEnvironment *env, v3f pos,
956 Settings *init_properties):
957 ServerActiveObject(env, pos),
958 m_move_type("ground_nodes"),
960 m_last_sent_position(0,0,0),
966 m_touching_ground(false),
968 m_walk_around(false),
969 m_walk_around_timer(0),
970 m_next_pos_exists(false),
971 m_shoot_reload_timer(0),
975 m_disturb_timer(100000),
976 m_random_disturb_timer(0),
979 ServerActiveObject::registerType(getType(), create);
981 m_properties = new Settings();
983 m_properties->update(*init_properties);
985 m_properties->setV3F("pos", pos);
987 setPropertyDefaults();
991 MobV2SAO::~MobV2SAO()
996 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, v3f pos,
997 const std::string &data)
999 std::istringstream is(data, std::ios::binary);
1000 Settings properties;
1001 properties.parseConfigLines(is, "MobArgsEnd");
1002 MobV2SAO *o = new MobV2SAO(env, pos, &properties);
1006 std::string MobV2SAO::getStaticData()
1010 std::ostringstream os(std::ios::binary);
1011 m_properties->writeLines(os);
1015 std::string MobV2SAO::getClientInitializationData()
1017 //infostream<<__FUNCTION_NAME<<std::endl;
1021 std::ostringstream os(std::ios::binary);
1026 Settings client_properties;
1028 /*client_properties.set("version", "0");
1029 client_properties.updateValue(*m_properties, "pos");
1030 client_properties.updateValue(*m_properties, "yaw");
1031 client_properties.updateValue(*m_properties, "hp");*/
1033 // Just send everything for simplicity
1034 client_properties.update(*m_properties);
1036 std::ostringstream os2(std::ios::binary);
1037 client_properties.writeLines(os2);
1038 compressZlib(os2.str(), os);
1043 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
1045 for(int dx=0; dx<size.X; dx++)
1046 for(int dy=0; dy<size.Y; dy++)
1047 for(int dz=0; dz<size.Z; dz++){
1048 v3s16 dp(dx, dy, dz);
1050 MapNode n = map->getNodeNoEx(p);
1051 if(n.getContent() != CONTENT_AIR)
1057 bool checkWalkablePosition(Map *map, v3s16 p0)
1059 v3s16 p = p0 + v3s16(0,-1,0);
1060 MapNode n = map->getNodeNoEx(p);
1061 if(n.getContent() != CONTENT_AIR)
1066 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1068 if(!checkFreePosition(map, p0, size))
1070 if(!checkWalkablePosition(map, p0))
1075 static void get_random_u32_array(u32 a[], u32 len)
1078 for(i=0; i<len; i++)
1082 u32 k = myrand() % n;
1090 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1092 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1094 core::map<v3s16, MapBlock*> modified_blocks;
1096 for(int dx=0; dx<size.X; dx++)
1097 for(int dy=0; dy<size.Y; dy++)
1098 for(int dz=0; dz<size.Z; dz++){
1099 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1101 MapNode n = map->getNodeNoEx(p);
1102 if(n.getContent() == CONTENT_IGNORE)
1104 //map->removeNodeWithEvent(p);
1105 map->removeNodeAndUpdate(p, modified_blocks);
1108 // Send a MEET_OTHER event
1110 event.type = MEET_OTHER;
1111 for(core::map<v3s16, MapBlock*>::Iterator
1112 i = modified_blocks.getIterator();
1113 i.atEnd() == false; i++)
1115 v3s16 p = i.getNode()->getKey();
1116 event.modified_blocks.insert(p, true);
1118 map->dispatchEvent(&event);
1121 void MobV2SAO::step(float dtime, bool send_recommended)
1123 ScopeProfiler sp2(g_profiler, "MobV2SAO::step avg", SPT_AVG);
1126 Map *map = &m_env->getMap();
1130 if(m_die_age >= 0.0 && m_age >= m_die_age){
1135 m_random_disturb_timer += dtime;
1136 if(m_random_disturb_timer >= 5.0)
1138 m_random_disturb_timer = 0;
1139 // Check connected players
1140 core::list<Player*> players = m_env->getPlayers(true);
1141 core::list<Player*>::Iterator i;
1142 for(i = players.begin();
1143 i != players.end(); i++)
1145 Player *player = *i;
1146 v3f playerpos = player->getPosition();
1147 f32 dist = m_base_position.getDistanceFrom(playerpos);
1150 if(myrand_range(0,3) == 0){
1151 actionstream<<"Mob id="<<m_id<<" at "
1152 <<PP(m_base_position/BS)
1153 <<" got randomly disturbed by "
1154 <<player->getName()<<std::endl;
1155 m_disturbing_player = player->getName();
1156 m_disturb_timer = 0;
1163 Player *disturbing_player =
1164 m_env->getPlayer(m_disturbing_player.c_str());
1165 v3f disturbing_player_off = v3f(0,1,0);
1166 v3f disturbing_player_norm = v3f(0,1,0);
1167 float disturbing_player_distance = 1000000;
1168 float disturbing_player_dir = 0;
1169 if(disturbing_player){
1170 disturbing_player_off =
1171 disturbing_player->getPosition() - m_base_position;
1172 disturbing_player_distance = disturbing_player_off.getLength();
1173 disturbing_player_norm = disturbing_player_off;
1174 disturbing_player_norm.normalize();
1175 disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z,
1176 disturbing_player_norm.X);
1179 m_disturb_timer += dtime;
1183 m_shooting_timer -= dtime;
1184 if(m_shooting_timer <= 0.0 && m_shooting){
1187 std::string shoot_type = m_properties->get("shoot_type");
1188 v3f shoot_pos(0,0,0);
1189 shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1190 if(shoot_type == "fireball"){
1191 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1194 v3f speed = dir * BS * 10.0;
1195 v3f pos = m_base_position + shoot_pos;
1196 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1197 <<" shooting fireball from "<<PP(pos)
1198 <<" at speed "<<PP(speed)<<std::endl;
1199 Settings properties;
1200 properties.set("looks", "fireball");
1201 properties.setV3F("speed", speed);
1202 properties.setFloat("die_age", 5.0);
1203 properties.set("move_type", "constant_speed");
1204 properties.setFloat("hp", 1000);
1205 properties.set("lock_full_brightness", "true");
1206 properties.set("player_hit_damage", "9");
1207 properties.set("player_hit_distance", "2");
1208 properties.set("player_hit_interval", "1");
1209 ServerActiveObject *obj = new MobV2SAO(m_env,
1211 //m_env->addActiveObjectAsStatic(obj);
1212 m_env->addActiveObject(obj);
1214 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1215 <<": Unknown shoot_type="<<shoot_type
1220 m_shoot_reload_timer += dtime;
1222 float reload_time = 15.0;
1223 if(m_disturb_timer <= 15.0)
1226 bool shoot_without_player = false;
1227 if(m_properties->getBool("mindless_rage"))
1228 shoot_without_player = true;
1230 if(!m_shooting && m_shoot_reload_timer >= reload_time &&
1231 !m_next_pos_exists &&
1232 (m_disturb_timer <= 60.0 || shoot_without_player))
1235 if(m_disturb_timer < 60.0 && disturbing_player &&
1236 disturbing_player_distance < 16*BS &&
1237 fabs(disturbing_player_norm.Y) < 0.8){
1238 m_yaw = disturbing_player_dir;
1240 m_shoot_y += disturbing_player_norm.Y;
1242 m_shoot_y = 0.01 * myrand_range(-30,10);
1244 m_shoot_reload_timer = 0.0;
1246 m_shooting_timer = 1.5;
1248 std::ostringstream os(std::ios::binary);
1249 // command (2 = shooting)
1252 writeF1000(os, m_shooting_timer + 0.1);
1255 // create message and add to list
1256 ActiveObjectMessage aom(getId(), false, os.str());
1257 m_messages_out.push_back(aom);
1262 if(m_move_type == "ground_nodes")
1265 m_walk_around_timer -= dtime;
1266 if(m_walk_around_timer <= 0.0){
1267 m_walk_around = !m_walk_around;
1269 m_walk_around_timer = 0.1*myrand_range(10,50);
1271 m_walk_around_timer = 0.1*myrand_range(30,70);
1276 if(m_next_pos_exists){
1277 v3f pos_f = m_base_position;
1278 v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1280 v3f v = next_pos_f - pos_f;
1281 m_yaw = atan2(v.Z, v.X) / PI * 180;
1283 v3f diff = next_pos_f - pos_f;
1286 float speed = BS * 0.5;
1289 dir *= dtime * speed;
1290 bool arrived = false;
1291 if(dir.getLength() > diff.getLength()){
1296 m_base_position = pos_f;
1298 if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1299 m_next_pos_exists = false;
1303 v3s16 pos_i = floatToInt(m_base_position, BS);
1304 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1305 v3s16 pos_size_off(0,0,0);
1306 if(m_size.X >= 2.5){
1307 pos_size_off.X = -1;
1308 pos_size_off.Y = -1;
1311 if(!m_next_pos_exists){
1312 /* Check whether to drop down */
1313 if(checkFreePosition(map,
1314 pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1315 m_next_pos_i = pos_i + v3s16(0,-1,0);
1316 m_next_pos_exists = true;
1325 if(!m_next_pos_exists){
1326 /* Find some position where to go next */
1329 for(int dx=-1; dx<=1; dx++)
1330 for(int dy=-1; dy<=1; dy++)
1331 for(int dz=-1; dz<=1; dz++){
1332 if(dx == 0 && dy == 0)
1334 if(dx != 0 && dz != 0 && dy != 0)
1336 dps[num_dps++] = v3s16(dx,dy,dz);
1339 get_random_u32_array(order, num_dps);
1340 for(int i=0; i<num_dps; i++){
1341 v3s16 p = dps[order[i]] + pos_i;
1342 bool is_free = checkFreeAndWalkablePosition(map,
1343 p + pos_size_off, size_blocks);
1347 m_next_pos_exists = true;
1353 else if(m_move_type == "constant_speed")
1355 m_base_position += m_speed * dtime;
1357 v3s16 pos_i = floatToInt(m_base_position, BS);
1358 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1359 v3s16 pos_size_off(0,0,0);
1360 if(m_size.X >= 2.5){
1361 pos_size_off.X = -1;
1362 pos_size_off.Y = -1;
1364 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1366 explodeSquare(map, pos_i, v3s16(3,3,3));
1373 errorstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1374 <<m_move_type<<"\""<<std::endl;
1377 if(send_recommended == false)
1380 if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1386 void MobV2SAO::punch(ServerActiveObject *puncher)
1388 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
1390 std::string toolname = "";
1391 InventoryItem *item = puncher->getWieldedItem();
1392 if(item && (std::string)item->getName() == "ToolItem"){
1393 ToolItem *titem = (ToolItem*)item;
1394 toolname = titem->getToolName();
1397 // A quick hack; SAO description is player name for player
1398 std::string playername = puncher->getDescription();
1400 Map *map = &m_env->getMap();
1402 actionstream<<playername<<" punches mob id="<<m_id
1403 <<" with a \""<<toolname<<"\" at "
1404 <<PP(m_base_position/BS)<<std::endl;
1406 m_disturb_timer = 0;
1407 m_disturbing_player = playername;
1408 m_next_pos_exists = false; // Cancel moving immediately
1410 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1411 v3f new_base_position = m_base_position + dir * BS;
1413 v3s16 pos_i = floatToInt(new_base_position, BS);
1414 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1415 v3s16 pos_size_off(0,0,0);
1416 if(m_size.X >= 2.5){
1417 pos_size_off.X = -1;
1418 pos_size_off.Y = -1;
1420 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1422 m_base_position = new_base_position;
1427 /* See tool names in inventory.h */
1428 if(toolname == "WSword")
1430 if(toolname == "STSword")
1432 if(toolname == "SteelSword")
1434 if(toolname == "STAxe")
1436 if(toolname == "SteelAxe")
1438 if(toolname == "SteelPick")
1442 puncher->damageWieldedItem(65536/100);
1445 bool MobV2SAO::isPeaceful()
1447 return m_properties->getBool("is_peaceful");
1450 void MobV2SAO::sendPosition()
1452 m_last_sent_position = m_base_position;
1454 std::ostringstream os(std::ios::binary);
1455 // command (0 = update position)
1458 writeV3F1000(os, m_base_position);
1460 writeF1000(os, m_yaw);
1461 // create message and add to list
1462 ActiveObjectMessage aom(getId(), false, os.str());
1463 m_messages_out.push_back(aom);
1466 void MobV2SAO::setPropertyDefaults()
1468 m_properties->setDefault("is_peaceful", "false");
1469 m_properties->setDefault("move_type", "ground_nodes");
1470 m_properties->setDefault("speed", "(0,0,0)");
1471 m_properties->setDefault("age", "0");
1472 m_properties->setDefault("yaw", "0");
1473 m_properties->setDefault("pos", "(0,0,0)");
1474 m_properties->setDefault("hp", "0");
1475 m_properties->setDefault("die_age", "-1");
1476 m_properties->setDefault("size", "(1,2)");
1477 m_properties->setDefault("shoot_type", "fireball");
1478 m_properties->setDefault("shoot_y", "0");
1479 m_properties->setDefault("mindless_rage", "false");
1481 void MobV2SAO::readProperties()
1483 m_move_type = m_properties->get("move_type");
1484 m_speed = m_properties->getV3F("speed");
1485 m_age = m_properties->getFloat("age");
1486 m_yaw = m_properties->getFloat("yaw");
1487 m_base_position = m_properties->getV3F("pos");
1488 m_hp = m_properties->getS32("hp");
1489 m_die_age = m_properties->getFloat("die_age");
1490 m_size = m_properties->getV2F("size");
1492 void MobV2SAO::updateProperties()
1494 m_properties->set("move_type", m_move_type);
1495 m_properties->setV3F("speed", m_speed);
1496 m_properties->setFloat("age", m_age);
1497 m_properties->setFloat("yaw", m_yaw);
1498 m_properties->setV3F("pos", m_base_position);
1499 m_properties->setS32("hp", m_hp);
1500 m_properties->setFloat("die_age", m_die_age);
1501 m_properties->setV2F("size", m_size);
1503 m_properties->setS32("version", 0);
1506 void MobV2SAO::doDamage(u16 d)
1508 infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1516 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1517 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1524 std::ostringstream os(std::ios::binary);
1525 // command (1 = damage)
1529 // create message and add to list
1530 ActiveObjectMessage aom(getId(), false, os.str());
1531 m_messages_out.push_back(aom);
1540 #include "scriptapi.h"
1541 #include "luaentity_common.h"
1544 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
1546 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
1547 const std::string &name, const std::string &state):
1548 ServerActiveObject(env, pos),
1550 m_init_state(state),
1551 m_registered(false),
1552 m_prop(new LuaEntityProperties),
1554 m_acceleration(0,0,0),
1557 m_last_sent_position(0,0,0),
1558 m_last_sent_velocity(0,0,0),
1559 m_last_sent_position_timer(0),
1560 m_last_sent_move_precision(0)
1562 // Only register type if no environment supplied
1564 ServerActiveObject::registerType(getType(), create);
1569 LuaEntitySAO::~LuaEntitySAO()
1572 lua_State *L = m_env->getLua();
1573 scriptapi_luaentity_rm(L, m_id);
1578 void LuaEntitySAO::addedToEnvironment()
1580 ServerActiveObject::addedToEnvironment();
1582 // Create entity from name and state
1583 lua_State *L = m_env->getLua();
1584 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str(),
1585 m_init_state.c_str());
1589 scriptapi_luaentity_get_properties(L, m_id, m_prop);
1593 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
1594 const std::string &data)
1596 std::istringstream is(data, std::ios::binary);
1598 u8 version = readU8(is);
1599 // check if version is supported
1603 std::string name = deSerializeString(is);
1605 std::string state = deSerializeLongString(is);
1607 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
1608 <<state<<"\")"<<std::endl;
1609 return new LuaEntitySAO(env, pos, name, state);
1612 void LuaEntitySAO::step(float dtime, bool send_recommended)
1614 m_last_sent_position_timer += dtime;
1616 if(m_prop->physical){
1617 core::aabbox3d<f32> box = m_prop->collisionbox;
1620 collisionMoveResult moveresult;
1621 f32 pos_max_d = BS*0.25; // Distance per iteration
1622 v3f p_pos = getBasePosition();
1623 v3f p_velocity = m_velocity;
1624 IGameDef *gamedef = m_env->getGameDef();
1625 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
1626 pos_max_d, box, dtime, p_pos, p_velocity);
1628 setBasePosition(p_pos);
1629 m_velocity = p_velocity;
1631 m_velocity += dtime * m_acceleration;
1633 m_base_position += dtime * m_velocity + 0.5 * dtime
1634 * dtime * m_acceleration;
1635 m_velocity += dtime * m_acceleration;
1639 lua_State *L = m_env->getLua();
1640 scriptapi_luaentity_step(L, m_id, dtime);
1643 if(send_recommended == false)
1646 // TODO: force send when acceleration changes enough?
1647 float minchange = 0.2*BS;
1648 if(m_last_sent_position_timer > 1.0){
1649 minchange = 0.01*BS;
1650 } else if(m_last_sent_position_timer > 0.2){
1651 minchange = 0.05*BS;
1653 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
1654 move_d += m_last_sent_move_precision;
1655 float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
1656 if(move_d > minchange || vel_d > minchange ||
1657 fabs(m_yaw - m_last_sent_yaw) > 1.0){
1658 sendPosition(true, false);
1662 std::string LuaEntitySAO::getClientInitializationData()
1664 std::ostringstream os(std::ios::binary);
1668 writeV3F1000(os, m_base_position);
1670 writeF1000(os, m_yaw);
1672 std::ostringstream prop_os(std::ios::binary);
1673 m_prop->serialize(prop_os);
1674 os<<serializeLongString(prop_os.str());
1679 std::string LuaEntitySAO::getStaticData()
1681 infostream<<__FUNCTION_NAME<<std::endl;
1682 std::ostringstream os(std::ios::binary);
1686 os<<serializeString(m_init_name);
1689 lua_State *L = m_env->getLua();
1690 std::string state = scriptapi_luaentity_get_staticdata(L, m_id);
1691 os<<serializeLongString(state);
1693 os<<serializeLongString(m_init_state);
1698 InventoryItem* LuaEntitySAO::createPickedUpItem()
1700 // TODO: Ask item from scriptapi
1701 std::istringstream is("CraftItem testobject1 1", std::ios_base::binary);
1702 IGameDef *gamedef = m_env->getGameDef();
1703 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
1707 void LuaEntitySAO::punch(ServerActiveObject *puncher)
1711 lua_State *L = m_env->getLua();
1712 scriptapi_luaentity_punch(L, m_id, puncher);
1715 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
1719 lua_State *L = m_env->getLua();
1720 scriptapi_luaentity_rightclick(L, m_id, clicker);
1723 void LuaEntitySAO::setPos(v3f pos)
1725 m_base_position = pos;
1726 sendPosition(false, true);
1729 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
1731 m_base_position = pos;
1733 sendPosition(true, true);
1736 float LuaEntitySAO::getMinimumSavedMovement()
1741 void LuaEntitySAO::setVelocity(v3f velocity)
1743 m_velocity = velocity;
1746 void LuaEntitySAO::setAcceleration(v3f acceleration)
1748 m_acceleration = acceleration;
1751 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
1753 m_last_sent_move_precision = m_base_position.getDistanceFrom(
1754 m_last_sent_position);
1755 m_last_sent_position_timer = 0;
1756 m_last_sent_yaw = m_yaw;
1757 m_last_sent_position = m_base_position;
1758 m_last_sent_velocity = m_velocity;
1759 //m_last_sent_acceleration = m_acceleration;
1761 float update_interval = m_env->getSendRecommendedInterval();
1763 std::ostringstream os(std::ios::binary);
1764 // command (0 = update position)
1768 writeU8(os, do_interpolate);
1770 writeV3F1000(os, m_base_position);
1772 writeV3F1000(os, m_velocity);
1774 writeV3F1000(os, m_acceleration);
1776 writeF1000(os, m_yaw);
1777 // is_end_position (for interpolation)
1778 writeU8(os, is_movement_end);
1779 // update_interval (for interpolation)
1780 writeF1000(os, update_interval);
1782 // create message and add to list
1783 ActiveObjectMessage aom(getId(), false, os.str());
1784 m_messages_out.push_back(aom);