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);
259 RatSAO proto_RatSAO(NULL, v3f(0,0,0));
261 RatSAO::RatSAO(ServerEnvironment *env, v3f pos):
262 ServerActiveObject(env, pos),
266 ServerActiveObject::registerType(getType(), create);
268 m_oldpos = v3f(0,0,0);
269 m_last_sent_position = v3f(0,0,0);
270 m_yaw = myrand_range(0,PI*2);
274 m_touching_ground = false;
277 ServerActiveObject* RatSAO::create(ServerEnvironment *env, v3f pos,
278 const std::string &data)
280 std::istringstream is(data, std::ios::binary);
285 // check if version is supported
288 return new RatSAO(env, pos);
291 void RatSAO::step(float dtime, bool send_recommended)
293 ScopeProfiler sp2(g_profiler, "RatSAO::step avg", SPT_AVG);
297 if(m_is_active == false)
299 if(m_inactive_interval.step(dtime, 0.5)==false)
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 m_is_active = player_is_close;
339 if(player_is_close == false)
347 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
349 m_speed_f.X = speed * dir.X;
350 m_speed_f.Z = speed * dir.Z;
352 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
359 m_speed_f.Y = 5.0*BS;
367 m_counter2 += (float)(myrand()%100)/100*3.0;
368 m_yaw += ((float)(myrand()%200)-100)/100*180;
369 m_yaw = wrapDegrees(m_yaw);
374 m_oldpos = m_base_position;
377 Move it, with collision detection
380 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
381 collisionMoveResult moveresult;
382 // Maximum movement without glitches
383 f32 pos_max_d = BS*0.25;
385 if(m_speed_f.getLength()*dtime > pos_max_d)
386 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
387 v3f pos_f = getBasePosition();
388 v3f pos_f_old = pos_f;
389 IGameDef *gamedef = m_env->getGameDef();
390 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
391 pos_max_d, box, dtime, pos_f, m_speed_f);
392 m_touching_ground = moveresult.touching_ground;
394 setBasePosition(pos_f);
396 if(send_recommended == false)
399 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
401 m_last_sent_position = pos_f;
403 std::ostringstream os(std::ios::binary);
404 // command (0 = update position)
407 writeV3F1000(os, m_base_position);
409 writeF1000(os, m_yaw);
410 // create message and add to list
411 ActiveObjectMessage aom(getId(), false, os.str());
412 m_messages_out.push_back(aom);
416 std::string RatSAO::getClientInitializationData()
418 std::ostringstream os(std::ios::binary);
422 writeV3F1000(os, m_base_position);
426 std::string RatSAO::getStaticData()
428 //infostream<<__FUNCTION_NAME<<std::endl;
429 std::ostringstream os(std::ios::binary);
435 void RatSAO::punch(ServerActiveObject *puncher)
437 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
438 IGameDef *gamedef = m_env->getGameDef();
439 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
440 bool fits = puncher->addToInventory(item);
452 Oerkki1SAO proto_Oerkki1SAO(NULL, v3f(0,0,0));
454 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, v3f pos):
455 ServerActiveObject(env, pos),
459 ServerActiveObject::registerType(getType(), create);
461 m_oldpos = v3f(0,0,0);
462 m_last_sent_position = v3f(0,0,0);
467 m_touching_ground = false;
469 m_after_jump_timer = 0;
472 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, v3f pos,
473 const std::string &data)
475 std::istringstream is(data, std::ios::binary);
477 u8 version = readU8(is);
480 // check if version is supported
483 Oerkki1SAO *o = new Oerkki1SAO(env, pos);
488 void Oerkki1SAO::step(float dtime, bool send_recommended)
490 ScopeProfiler sp2(g_profiler, "Oerkki1SAO::step avg", SPT_AVG);
494 if(m_is_active == false)
496 if(m_inactive_interval.step(dtime, 0.5)==false)
512 m_after_jump_timer -= dtime;
514 v3f old_speed = m_speed_f;
517 m_speed_f.Y -= dtime*9.81*BS;
520 Move around if some player is close
522 bool player_is_close = false;
523 bool player_is_too_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 f32 dist = m_base_position.getDistanceFrom(playerpos);
538 player_is_too_close = true;
539 near_player_pos = playerpos;
541 else if(dist < BS*15.0 && !player_is_too_close)
543 player_is_close = true;
544 near_player_pos = playerpos;
548 m_is_active = player_is_close;
550 v3f target_speed = m_speed_f;
554 target_speed = v3f(0,0,0);
560 v3f ndir = near_player_pos - m_base_position;
564 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
565 if(nyaw < m_yaw - 180)
567 else if(nyaw > m_yaw + 180)
569 m_yaw = 0.95*m_yaw + 0.05*nyaw;
570 m_yaw = wrapDegrees(m_yaw);
574 if((m_touching_ground || m_after_jump_timer > 0.0)
575 && !player_is_too_close)
577 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
578 target_speed.X = speed * dir.X;
579 target_speed.Z = speed * dir.Z;
582 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
590 target_speed.Y = 5.0*BS;
591 m_after_jump_timer = 1.0;
599 m_counter2 += (float)(myrand()%100)/100*3.0;
600 //m_yaw += ((float)(myrand()%200)-100)/100*180;
601 m_yaw += ((float)(myrand()%200)-100)/100*90;
602 m_yaw = wrapDegrees(m_yaw);
607 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
608 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
610 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
612 m_oldpos = m_base_position;
615 Move it, with collision detection
618 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
619 collisionMoveResult moveresult;
620 // Maximum movement without glitches
621 f32 pos_max_d = BS*0.25;
623 if(m_speed_f.getLength()*dtime > pos_max_d)
624 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
625 v3f pos_f = getBasePosition();
626 v3f pos_f_old = pos_f;
627 IGameDef *gamedef = m_env->getGameDef();
628 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
629 pos_max_d, box, dtime, pos_f, m_speed_f);
630 m_touching_ground = moveresult.touching_ground;
632 // Do collision damage
633 float tolerance = BS*30;
634 float factor = BS*0.5;
635 v3f speed_diff = old_speed - m_speed_f;
636 // Increase effect in X and Z
639 float vel = speed_diff.getLength();
642 f32 damage_f = (vel - tolerance)/BS*factor;
643 u16 damage = (u16)(damage_f+0.5);
647 setBasePosition(pos_f);
649 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
652 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
654 m_last_sent_position = pos_f;
656 std::ostringstream os(std::ios::binary);
657 // command (0 = update position)
660 writeV3F1000(os, m_base_position);
662 writeF1000(os, m_yaw);
663 // create message and add to list
664 ActiveObjectMessage aom(getId(), false, os.str());
665 m_messages_out.push_back(aom);
669 std::string Oerkki1SAO::getClientInitializationData()
671 std::ostringstream os(std::ios::binary);
675 writeV3F1000(os, m_base_position);
679 std::string Oerkki1SAO::getStaticData()
681 //infostream<<__FUNCTION_NAME<<std::endl;
682 std::ostringstream os(std::ios::binary);
690 void Oerkki1SAO::punch(ServerActiveObject *puncher)
692 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
694 std::string toolname = "";
695 InventoryItem *item = puncher->getWieldedItem();
696 if(item && (std::string)item->getName() == "ToolItem"){
697 ToolItem *titem = (ToolItem*)item;
698 toolname = titem->getToolName();
701 m_speed_f += dir*12*BS;
704 /* See tool names in inventory.h */
705 if(toolname == "WSword")
707 if(toolname == "STSword")
709 if(toolname == "SteelSword")
711 if(toolname == "STAxe")
713 if(toolname == "SteelAxe")
715 if(toolname == "SteelPick")
719 puncher->damageWieldedItem(65536/100);
722 void Oerkki1SAO::doDamage(u16 d)
724 infostream<<"oerkki damage: "<<d<<std::endl;
738 std::ostringstream os(std::ios::binary);
739 // command (1 = damage)
743 // create message and add to list
744 ActiveObjectMessage aom(getId(), false, os.str());
745 m_messages_out.push_back(aom);
754 FireflySAO proto_FireflySAO(NULL, v3f(0,0,0));
756 FireflySAO::FireflySAO(ServerEnvironment *env, v3f pos):
757 ServerActiveObject(env, pos),
761 ServerActiveObject::registerType(getType(), create);
763 m_oldpos = v3f(0,0,0);
764 m_last_sent_position = v3f(0,0,0);
769 m_touching_ground = false;
772 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, v3f pos,
773 const std::string &data)
775 std::istringstream is(data, std::ios::binary);
780 // check if version is supported
783 return new FireflySAO(env, pos);
786 void FireflySAO::step(float dtime, bool send_recommended)
788 ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG);
792 if(m_is_active == false)
794 if(m_inactive_interval.step(dtime, 0.5)==false)
802 // Apply (less) gravity
803 m_speed_f.Y -= dtime*3*BS;
806 Move around if some player is close
808 bool player_is_close = false;
809 // Check connected players
810 core::list<Player*> players = m_env->getPlayers(true);
811 core::list<Player*>::Iterator i;
812 for(i = players.begin();
813 i != players.end(); i++)
816 v3f playerpos = player->getPosition();
817 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
819 player_is_close = true;
824 m_is_active = player_is_close;
826 if(player_is_close == false)
834 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
836 m_speed_f.X = speed * dir.X;
837 m_speed_f.Z = speed * dir.Z;
839 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
846 m_speed_f.Y = 5.0*BS;
854 m_counter2 += (float)(myrand()%100)/100*3.0;
855 m_yaw += ((float)(myrand()%200)-100)/100*180;
856 m_yaw = wrapDegrees(m_yaw);
861 m_oldpos = m_base_position;
864 Move it, with collision detection
867 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
868 collisionMoveResult moveresult;
869 // Maximum movement without glitches
870 f32 pos_max_d = BS*0.25;
872 if(m_speed_f.getLength()*dtime > pos_max_d)
873 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
874 v3f pos_f = getBasePosition();
875 v3f pos_f_old = pos_f;
876 IGameDef *gamedef = m_env->getGameDef();
877 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
878 pos_max_d, box, dtime, pos_f, m_speed_f);
879 m_touching_ground = moveresult.touching_ground;
881 setBasePosition(pos_f);
883 if(send_recommended == false)
886 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
888 m_last_sent_position = pos_f;
890 std::ostringstream os(std::ios::binary);
891 // command (0 = update position)
894 writeV3F1000(os, m_base_position);
896 writeF1000(os, m_yaw);
897 // create message and add to list
898 ActiveObjectMessage aom(getId(), false, os.str());
899 m_messages_out.push_back(aom);
903 std::string FireflySAO::getClientInitializationData()
905 std::ostringstream os(std::ios::binary);
909 writeV3F1000(os, m_base_position);
913 std::string FireflySAO::getStaticData()
915 //infostream<<__FUNCTION_NAME<<std::endl;
916 std::ostringstream os(std::ios::binary);
922 InventoryItem* FireflySAO::createPickedUpItem()
924 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
925 IGameDef *gamedef = m_env->getGameDef();
926 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
935 MobV2SAO proto_MobV2SAO(NULL, v3f(0,0,0), NULL);
937 MobV2SAO::MobV2SAO(ServerEnvironment *env, v3f pos,
938 Settings *init_properties):
939 ServerActiveObject(env, pos),
940 m_move_type("ground_nodes"),
942 m_last_sent_position(0,0,0),
948 m_touching_ground(false),
950 m_walk_around(false),
951 m_walk_around_timer(0),
952 m_next_pos_exists(false),
953 m_shoot_reload_timer(0),
957 m_disturb_timer(100000),
958 m_random_disturb_timer(0),
961 ServerActiveObject::registerType(getType(), create);
963 m_properties = new Settings();
965 m_properties->update(*init_properties);
967 m_properties->setV3F("pos", pos);
969 setPropertyDefaults();
973 MobV2SAO::~MobV2SAO()
978 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, v3f pos,
979 const std::string &data)
981 std::istringstream is(data, std::ios::binary);
983 properties.parseConfigLines(is, "MobArgsEnd");
984 MobV2SAO *o = new MobV2SAO(env, pos, &properties);
988 std::string MobV2SAO::getStaticData()
992 std::ostringstream os(std::ios::binary);
993 m_properties->writeLines(os);
997 std::string MobV2SAO::getClientInitializationData()
999 //infostream<<__FUNCTION_NAME<<std::endl;
1003 std::ostringstream os(std::ios::binary);
1008 Settings client_properties;
1010 /*client_properties.set("version", "0");
1011 client_properties.updateValue(*m_properties, "pos");
1012 client_properties.updateValue(*m_properties, "yaw");
1013 client_properties.updateValue(*m_properties, "hp");*/
1015 // Just send everything for simplicity
1016 client_properties.update(*m_properties);
1018 std::ostringstream os2(std::ios::binary);
1019 client_properties.writeLines(os2);
1020 compressZlib(os2.str(), os);
1025 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
1027 for(int dx=0; dx<size.X; dx++)
1028 for(int dy=0; dy<size.Y; dy++)
1029 for(int dz=0; dz<size.Z; dz++){
1030 v3s16 dp(dx, dy, dz);
1032 MapNode n = map->getNodeNoEx(p);
1033 if(n.getContent() != CONTENT_AIR)
1039 bool checkWalkablePosition(Map *map, v3s16 p0)
1041 v3s16 p = p0 + v3s16(0,-1,0);
1042 MapNode n = map->getNodeNoEx(p);
1043 if(n.getContent() != CONTENT_AIR)
1048 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1050 if(!checkFreePosition(map, p0, size))
1052 if(!checkWalkablePosition(map, p0))
1057 static void get_random_u32_array(u32 a[], u32 len)
1060 for(i=0; i<len; i++)
1064 u32 k = myrand() % n;
1072 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1074 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1076 core::map<v3s16, MapBlock*> modified_blocks;
1078 for(int dx=0; dx<size.X; dx++)
1079 for(int dy=0; dy<size.Y; dy++)
1080 for(int dz=0; dz<size.Z; dz++){
1081 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1083 MapNode n = map->getNodeNoEx(p);
1084 if(n.getContent() == CONTENT_IGNORE)
1086 //map->removeNodeWithEvent(p);
1087 map->removeNodeAndUpdate(p, modified_blocks);
1090 // Send a MEET_OTHER event
1092 event.type = MEET_OTHER;
1093 for(core::map<v3s16, MapBlock*>::Iterator
1094 i = modified_blocks.getIterator();
1095 i.atEnd() == false; i++)
1097 v3s16 p = i.getNode()->getKey();
1098 event.modified_blocks.insert(p, true);
1100 map->dispatchEvent(&event);
1103 void MobV2SAO::step(float dtime, bool send_recommended)
1105 ScopeProfiler sp2(g_profiler, "MobV2SAO::step avg", SPT_AVG);
1108 Map *map = &m_env->getMap();
1112 if(m_die_age >= 0.0 && m_age >= m_die_age){
1117 m_random_disturb_timer += dtime;
1118 if(m_random_disturb_timer >= 5.0)
1120 m_random_disturb_timer = 0;
1121 // Check connected players
1122 core::list<Player*> players = m_env->getPlayers(true);
1123 core::list<Player*>::Iterator i;
1124 for(i = players.begin();
1125 i != players.end(); i++)
1127 Player *player = *i;
1128 v3f playerpos = player->getPosition();
1129 f32 dist = m_base_position.getDistanceFrom(playerpos);
1132 if(myrand_range(0,3) == 0){
1133 actionstream<<"Mob id="<<m_id<<" at "
1134 <<PP(m_base_position/BS)
1135 <<" got randomly disturbed by "
1136 <<player->getName()<<std::endl;
1137 m_disturbing_player = player->getName();
1138 m_disturb_timer = 0;
1145 Player *disturbing_player =
1146 m_env->getPlayer(m_disturbing_player.c_str());
1147 v3f disturbing_player_off = v3f(0,1,0);
1148 v3f disturbing_player_norm = v3f(0,1,0);
1149 float disturbing_player_distance = 1000000;
1150 float disturbing_player_dir = 0;
1151 if(disturbing_player){
1152 disturbing_player_off =
1153 disturbing_player->getPosition() - m_base_position;
1154 disturbing_player_distance = disturbing_player_off.getLength();
1155 disturbing_player_norm = disturbing_player_off;
1156 disturbing_player_norm.normalize();
1157 disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z,
1158 disturbing_player_norm.X);
1161 m_disturb_timer += dtime;
1165 m_shooting_timer -= dtime;
1166 if(m_shooting_timer <= 0.0 && m_shooting){
1169 std::string shoot_type = m_properties->get("shoot_type");
1170 v3f shoot_pos(0,0,0);
1171 shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1172 if(shoot_type == "fireball"){
1173 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1176 v3f speed = dir * BS * 10.0;
1177 v3f pos = m_base_position + shoot_pos;
1178 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1179 <<" shooting fireball from "<<PP(pos)
1180 <<" at speed "<<PP(speed)<<std::endl;
1181 Settings properties;
1182 properties.set("looks", "fireball");
1183 properties.setV3F("speed", speed);
1184 properties.setFloat("die_age", 5.0);
1185 properties.set("move_type", "constant_speed");
1186 properties.setFloat("hp", 1000);
1187 properties.set("lock_full_brightness", "true");
1188 properties.set("player_hit_damage", "9");
1189 properties.set("player_hit_distance", "2");
1190 properties.set("player_hit_interval", "1");
1191 ServerActiveObject *obj = new MobV2SAO(m_env,
1193 //m_env->addActiveObjectAsStatic(obj);
1194 m_env->addActiveObject(obj);
1196 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1197 <<": Unknown shoot_type="<<shoot_type
1202 m_shoot_reload_timer += dtime;
1204 float reload_time = 15.0;
1205 if(m_disturb_timer <= 15.0)
1208 bool shoot_without_player = false;
1209 if(m_properties->getBool("mindless_rage"))
1210 shoot_without_player = true;
1212 if(!m_shooting && m_shoot_reload_timer >= reload_time &&
1213 !m_next_pos_exists &&
1214 (m_disturb_timer <= 60.0 || shoot_without_player))
1217 if(m_disturb_timer < 60.0 && disturbing_player &&
1218 disturbing_player_distance < 16*BS &&
1219 fabs(disturbing_player_norm.Y) < 0.8){
1220 m_yaw = disturbing_player_dir;
1222 m_shoot_y += disturbing_player_norm.Y;
1224 m_shoot_y = 0.01 * myrand_range(-30,10);
1226 m_shoot_reload_timer = 0.0;
1228 m_shooting_timer = 1.5;
1230 std::ostringstream os(std::ios::binary);
1231 // command (2 = shooting)
1234 writeF1000(os, m_shooting_timer + 0.1);
1237 // create message and add to list
1238 ActiveObjectMessage aom(getId(), false, os.str());
1239 m_messages_out.push_back(aom);
1244 if(m_move_type == "ground_nodes")
1247 m_walk_around_timer -= dtime;
1248 if(m_walk_around_timer <= 0.0){
1249 m_walk_around = !m_walk_around;
1251 m_walk_around_timer = 0.1*myrand_range(10,50);
1253 m_walk_around_timer = 0.1*myrand_range(30,70);
1258 if(m_next_pos_exists){
1259 v3f pos_f = m_base_position;
1260 v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1262 v3f v = next_pos_f - pos_f;
1263 m_yaw = atan2(v.Z, v.X) / PI * 180;
1265 v3f diff = next_pos_f - pos_f;
1268 float speed = BS * 0.5;
1271 dir *= dtime * speed;
1272 bool arrived = false;
1273 if(dir.getLength() > diff.getLength()){
1278 m_base_position = pos_f;
1280 if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1281 m_next_pos_exists = false;
1285 v3s16 pos_i = floatToInt(m_base_position, BS);
1286 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1287 v3s16 pos_size_off(0,0,0);
1288 if(m_size.X >= 2.5){
1289 pos_size_off.X = -1;
1290 pos_size_off.Y = -1;
1293 if(!m_next_pos_exists){
1294 /* Check whether to drop down */
1295 if(checkFreePosition(map,
1296 pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1297 m_next_pos_i = pos_i + v3s16(0,-1,0);
1298 m_next_pos_exists = true;
1307 if(!m_next_pos_exists){
1308 /* Find some position where to go next */
1311 for(int dx=-1; dx<=1; dx++)
1312 for(int dy=-1; dy<=1; dy++)
1313 for(int dz=-1; dz<=1; dz++){
1314 if(dx == 0 && dy == 0)
1316 if(dx != 0 && dz != 0 && dy != 0)
1318 dps[num_dps++] = v3s16(dx,dy,dz);
1321 get_random_u32_array(order, num_dps);
1322 for(int i=0; i<num_dps; i++){
1323 v3s16 p = dps[order[i]] + pos_i;
1324 bool is_free = checkFreeAndWalkablePosition(map,
1325 p + pos_size_off, size_blocks);
1329 m_next_pos_exists = true;
1335 else if(m_move_type == "constant_speed")
1337 m_base_position += m_speed * dtime;
1339 v3s16 pos_i = floatToInt(m_base_position, BS);
1340 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1341 v3s16 pos_size_off(0,0,0);
1342 if(m_size.X >= 2.5){
1343 pos_size_off.X = -1;
1344 pos_size_off.Y = -1;
1346 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1348 explodeSquare(map, pos_i, v3s16(3,3,3));
1355 errorstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1356 <<m_move_type<<"\""<<std::endl;
1359 if(send_recommended == false)
1362 if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1368 void MobV2SAO::punch(ServerActiveObject *puncher)
1370 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
1372 std::string toolname = "";
1373 InventoryItem *item = puncher->getWieldedItem();
1374 if(item && (std::string)item->getName() == "ToolItem"){
1375 ToolItem *titem = (ToolItem*)item;
1376 toolname = titem->getToolName();
1379 // A quick hack; SAO description is player name for player
1380 std::string playername = puncher->getDescription();
1382 Map *map = &m_env->getMap();
1384 actionstream<<playername<<" punches mob id="<<m_id
1385 <<" with a \""<<toolname<<"\" at "
1386 <<PP(m_base_position/BS)<<std::endl;
1388 m_disturb_timer = 0;
1389 m_disturbing_player = playername;
1390 m_next_pos_exists = false; // Cancel moving immediately
1392 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1393 v3f new_base_position = m_base_position + dir * BS;
1395 v3s16 pos_i = floatToInt(new_base_position, BS);
1396 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1397 v3s16 pos_size_off(0,0,0);
1398 if(m_size.X >= 2.5){
1399 pos_size_off.X = -1;
1400 pos_size_off.Y = -1;
1402 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1404 m_base_position = new_base_position;
1409 /* See tool names in inventory.h */
1410 if(toolname == "WSword")
1412 if(toolname == "STSword")
1414 if(toolname == "SteelSword")
1416 if(toolname == "STAxe")
1418 if(toolname == "SteelAxe")
1420 if(toolname == "SteelPick")
1424 puncher->damageWieldedItem(65536/100);
1427 bool MobV2SAO::isPeaceful()
1429 return m_properties->getBool("is_peaceful");
1432 void MobV2SAO::sendPosition()
1434 m_last_sent_position = m_base_position;
1436 std::ostringstream os(std::ios::binary);
1437 // command (0 = update position)
1440 writeV3F1000(os, m_base_position);
1442 writeF1000(os, m_yaw);
1443 // create message and add to list
1444 ActiveObjectMessage aom(getId(), false, os.str());
1445 m_messages_out.push_back(aom);
1448 void MobV2SAO::setPropertyDefaults()
1450 m_properties->setDefault("is_peaceful", "false");
1451 m_properties->setDefault("move_type", "ground_nodes");
1452 m_properties->setDefault("speed", "(0,0,0)");
1453 m_properties->setDefault("age", "0");
1454 m_properties->setDefault("yaw", "0");
1455 m_properties->setDefault("pos", "(0,0,0)");
1456 m_properties->setDefault("hp", "0");
1457 m_properties->setDefault("die_age", "-1");
1458 m_properties->setDefault("size", "(1,2)");
1459 m_properties->setDefault("shoot_type", "fireball");
1460 m_properties->setDefault("shoot_y", "0");
1461 m_properties->setDefault("mindless_rage", "false");
1463 void MobV2SAO::readProperties()
1465 m_move_type = m_properties->get("move_type");
1466 m_speed = m_properties->getV3F("speed");
1467 m_age = m_properties->getFloat("age");
1468 m_yaw = m_properties->getFloat("yaw");
1469 m_base_position = m_properties->getV3F("pos");
1470 m_hp = m_properties->getS32("hp");
1471 m_die_age = m_properties->getFloat("die_age");
1472 m_size = m_properties->getV2F("size");
1474 void MobV2SAO::updateProperties()
1476 m_properties->set("move_type", m_move_type);
1477 m_properties->setV3F("speed", m_speed);
1478 m_properties->setFloat("age", m_age);
1479 m_properties->setFloat("yaw", m_yaw);
1480 m_properties->setV3F("pos", m_base_position);
1481 m_properties->setS32("hp", m_hp);
1482 m_properties->setFloat("die_age", m_die_age);
1483 m_properties->setV2F("size", m_size);
1485 m_properties->setS32("version", 0);
1488 void MobV2SAO::doDamage(u16 d)
1490 infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1498 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1499 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1506 std::ostringstream os(std::ios::binary);
1507 // command (1 = damage)
1511 // create message and add to list
1512 ActiveObjectMessage aom(getId(), false, os.str());
1513 m_messages_out.push_back(aom);
1522 #include "scriptapi.h"
1523 #include "luaentity_common.h"
1526 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
1528 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
1529 const std::string &name, const std::string &state):
1530 ServerActiveObject(env, pos),
1532 m_init_state(state),
1533 m_registered(false),
1534 m_prop(new LuaEntityProperties),
1536 m_acceleration(0,0,0),
1539 m_last_sent_position(0,0,0),
1540 m_last_sent_velocity(0,0,0),
1541 m_last_sent_position_timer(0),
1542 m_last_sent_move_precision(0)
1544 // Only register type if no environment supplied
1546 ServerActiveObject::registerType(getType(), create);
1551 LuaEntitySAO::~LuaEntitySAO()
1554 lua_State *L = m_env->getLua();
1555 scriptapi_luaentity_rm(L, m_id);
1560 void LuaEntitySAO::addedToEnvironment()
1562 ServerActiveObject::addedToEnvironment();
1564 // Create entity from name and state
1565 lua_State *L = m_env->getLua();
1566 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str(),
1567 m_init_state.c_str());
1571 scriptapi_luaentity_get_properties(L, m_id, m_prop);
1575 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
1576 const std::string &data)
1578 std::istringstream is(data, std::ios::binary);
1580 u8 version = readU8(is);
1581 // check if version is supported
1585 std::string name = deSerializeString(is);
1587 std::string state = deSerializeLongString(is);
1589 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
1590 <<state<<"\")"<<std::endl;
1591 return new LuaEntitySAO(env, pos, name, state);
1594 void LuaEntitySAO::step(float dtime, bool send_recommended)
1596 m_last_sent_position_timer += dtime;
1598 if(m_prop->physical){
1599 core::aabbox3d<f32> box = m_prop->collisionbox;
1602 collisionMoveResult moveresult;
1603 f32 pos_max_d = BS*0.25; // Distance per iteration
1604 v3f p_pos = getBasePosition();
1605 v3f p_velocity = m_velocity;
1606 IGameDef *gamedef = m_env->getGameDef();
1607 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
1608 pos_max_d, box, dtime, p_pos, p_velocity);
1610 setBasePosition(p_pos);
1611 m_velocity = p_velocity;
1613 m_velocity += dtime * m_acceleration;
1615 m_base_position += dtime * m_velocity + 0.5 * dtime
1616 * dtime * m_acceleration;
1617 m_velocity += dtime * m_acceleration;
1621 lua_State *L = m_env->getLua();
1622 scriptapi_luaentity_step(L, m_id, dtime);
1625 if(send_recommended == false)
1628 // TODO: force send when acceleration changes enough?
1629 float minchange = 0.2*BS;
1630 if(m_last_sent_position_timer > 1.0){
1631 minchange = 0.01*BS;
1632 } else if(m_last_sent_position_timer > 0.2){
1633 minchange = 0.05*BS;
1635 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
1636 move_d += m_last_sent_move_precision;
1637 float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
1638 if(move_d > minchange || vel_d > minchange ||
1639 fabs(m_yaw - m_last_sent_yaw) > 1.0){
1640 sendPosition(true, false);
1644 std::string LuaEntitySAO::getClientInitializationData()
1646 std::ostringstream os(std::ios::binary);
1650 writeV3F1000(os, m_base_position);
1652 writeF1000(os, m_yaw);
1654 std::ostringstream prop_os(std::ios::binary);
1655 m_prop->serialize(prop_os);
1656 os<<serializeLongString(prop_os.str());
1661 std::string LuaEntitySAO::getStaticData()
1663 infostream<<__FUNCTION_NAME<<std::endl;
1664 std::ostringstream os(std::ios::binary);
1668 os<<serializeString(m_init_name);
1671 lua_State *L = m_env->getLua();
1672 std::string state = scriptapi_luaentity_get_staticdata(L, m_id);
1673 os<<serializeLongString(state);
1675 os<<serializeLongString(m_init_state);
1680 InventoryItem* LuaEntitySAO::createPickedUpItem()
1682 // TODO: Ask item from scriptapi
1683 std::istringstream is("CraftItem testobject1 1", std::ios_base::binary);
1684 IGameDef *gamedef = m_env->getGameDef();
1685 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
1689 void LuaEntitySAO::punch(ServerActiveObject *puncher)
1693 lua_State *L = m_env->getLua();
1694 scriptapi_luaentity_punch(L, m_id, puncher);
1697 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
1701 lua_State *L = m_env->getLua();
1702 scriptapi_luaentity_rightclick(L, m_id, clicker);
1705 void LuaEntitySAO::setPos(v3f pos)
1707 m_base_position = pos;
1708 sendPosition(false, true);
1711 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
1713 m_base_position = pos;
1715 sendPosition(true, true);
1718 float LuaEntitySAO::getMinimumSavedMovement()
1723 void LuaEntitySAO::setVelocity(v3f velocity)
1725 m_velocity = velocity;
1728 void LuaEntitySAO::setAcceleration(v3f acceleration)
1730 m_acceleration = acceleration;
1733 v3f LuaEntitySAO::getAcceleration()
1735 return m_acceleration;
1738 void LuaEntitySAO::setTextureMod(const std::string &mod)
1740 std::ostringstream os(std::ios::binary);
1741 // command (1 = set texture modification)
1744 os<<serializeString(mod);
1745 // create message and add to list
1746 ActiveObjectMessage aom(getId(), false, os.str());
1747 m_messages_out.push_back(aom);
1750 void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
1751 bool select_horiz_by_yawpitch)
1753 std::ostringstream os(std::ios::binary);
1754 // command (2 = set sprite)
1758 writeU16(os, num_frames);
1759 writeF1000(os, framelength);
1760 writeU8(os, select_horiz_by_yawpitch);
1761 // create message and add to list
1762 ActiveObjectMessage aom(getId(), false, os.str());
1763 m_messages_out.push_back(aom);
1766 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
1768 m_last_sent_move_precision = m_base_position.getDistanceFrom(
1769 m_last_sent_position);
1770 m_last_sent_position_timer = 0;
1771 m_last_sent_yaw = m_yaw;
1772 m_last_sent_position = m_base_position;
1773 m_last_sent_velocity = m_velocity;
1774 //m_last_sent_acceleration = m_acceleration;
1776 float update_interval = m_env->getSendRecommendedInterval();
1778 std::ostringstream os(std::ios::binary);
1779 // command (0 = update position)
1783 writeU8(os, do_interpolate);
1785 writeV3F1000(os, m_base_position);
1787 writeV3F1000(os, m_velocity);
1789 writeV3F1000(os, m_acceleration);
1791 writeF1000(os, m_yaw);
1792 // is_end_position (for interpolation)
1793 writeU8(os, is_movement_end);
1794 // update_interval (for interpolation)
1795 writeF1000(os, update_interval);
1797 // create message and add to list
1798 ActiveObjectMessage aom(getId(), false, os.str());
1799 m_messages_out.push_back(aom);