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
27 #include "materials.h" // For MaterialProperties and ToolDiggingProperties
30 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
32 /* Some helper functions */
34 // Y is copied, X and Z change is limited
35 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
37 v3f d_wanted = target_speed - speed;
39 f32 dl_wanted = d_wanted.getLength();
44 v3f d = d_wanted.normalize() * dl;
48 speed.Y = target_speed.Y;
56 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
58 TestSAO::TestSAO(ServerEnvironment *env, v3f pos):
59 ServerActiveObject(env, pos),
63 ServerActiveObject::registerType(getType(), create);
66 ServerActiveObject* TestSAO::create(ServerEnvironment *env, v3f pos,
67 const std::string &data)
69 return new TestSAO(env, pos);
72 void TestSAO::step(float dtime, bool send_recommended)
81 m_base_position.Y += dtime * BS * 2;
82 if(m_base_position.Y > 8*BS)
83 m_base_position.Y = 2*BS;
85 if(send_recommended == false)
95 data += itos(0); // 0 = position
97 data += itos(m_base_position.X);
99 data += itos(m_base_position.Y);
101 data += itos(m_base_position.Z);
103 ActiveObjectMessage aom(getId(), false, data);
104 m_messages_out.push_back(aom);
114 ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), "");
116 ItemSAO::ItemSAO(ServerEnvironment *env, v3f pos,
117 const std::string itemstring):
118 ServerActiveObject(env, pos),
119 m_itemstring(itemstring),
120 m_itemstring_changed(false),
122 m_last_sent_position(0,0,0)
124 ServerActiveObject::registerType(getType(), create);
127 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, v3f pos,
128 const std::string &data)
130 std::istringstream is(data, std::ios::binary);
135 // check if version is supported
138 std::string itemstring = deSerializeString(is);
139 infostream<<"ItemSAO::create(): Creating item \""
140 <<itemstring<<"\""<<std::endl;
141 return new ItemSAO(env, pos, itemstring);
144 void ItemSAO::step(float dtime, bool send_recommended)
146 ScopeProfiler sp2(g_profiler, "ItemSAO::step avg", SPT_AVG);
150 const float interval = 0.2;
151 if(m_move_interval.step(dtime, interval)==false)
155 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
156 collisionMoveResult moveresult;
158 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
159 // Maximum movement without glitches
160 f32 pos_max_d = BS*0.25;
162 if(m_speed_f.getLength()*dtime > pos_max_d)
163 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
164 v3f pos_f = getBasePosition();
165 v3f pos_f_old = pos_f;
166 IGameDef *gamedef = m_env->getGameDef();
167 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
168 pos_max_d, box, dtime, pos_f, m_speed_f);
170 if(send_recommended == false)
173 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
175 setBasePosition(pos_f);
176 m_last_sent_position = pos_f;
178 std::ostringstream os(std::ios::binary);
179 // command (0 = update position)
182 writeV3F1000(os, m_base_position);
183 // create message and add to list
184 ActiveObjectMessage aom(getId(), false, os.str());
185 m_messages_out.push_back(aom);
187 if(m_itemstring_changed)
189 m_itemstring_changed = false;
191 std::ostringstream os(std::ios::binary);
192 // command (1 = update itemstring)
195 os<<serializeString(m_itemstring);
196 // create message and add to list
197 ActiveObjectMessage aom(getId(), false, os.str());
198 m_messages_out.push_back(aom);
202 std::string ItemSAO::getClientInitializationData()
204 std::ostringstream os(std::ios::binary);
208 writeV3F1000(os, m_base_position);
210 os<<serializeString(m_itemstring);
214 std::string ItemSAO::getStaticData()
216 infostream<<__FUNCTION_NAME<<std::endl;
217 std::ostringstream os(std::ios::binary);
221 os<<serializeString(m_itemstring);
225 ItemStack ItemSAO::createItemStack()
228 IItemDefManager *idef = m_env->getGameDef()->idef();
230 item.deSerialize(m_itemstring, idef);
231 infostream<<__FUNCTION_NAME<<": m_itemstring=\""<<m_itemstring
232 <<"\" -> item=\""<<item.getItemString()<<"\""
236 catch(SerializationError &e)
238 infostream<<__FUNCTION_NAME<<": serialization error: "
239 <<"m_itemstring=\""<<m_itemstring<<"\""<<std::endl;
244 void ItemSAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
246 // Allow removing items in creative mode
247 if(g_settings->getBool("creative_mode") == true)
253 ItemStack item = createItemStack();
254 Inventory *inv = puncher->getInventory();
257 std::string wieldlist = puncher->getWieldList();
258 ItemStack leftover = inv->addItem(wieldlist, item);
259 puncher->setInventoryModified();
266 m_itemstring = leftover.getItemString();
267 m_itemstring_changed = true;
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, float time_from_last_punch)
455 // Allow removing rats in creative mode
456 if(g_settings->getBool("creative_mode") == true)
462 IItemDefManager *idef = m_env->getGameDef()->idef();
463 ItemStack item("rat", 1, 0, "", idef);
464 Inventory *inv = puncher->getInventory();
467 std::string wieldlist = puncher->getWieldList();
468 ItemStack leftover = inv->addItem(wieldlist, item);
469 puncher->setInventoryModified();
480 Oerkki1SAO proto_Oerkki1SAO(NULL, v3f(0,0,0));
482 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, v3f pos):
483 ServerActiveObject(env, pos),
487 ServerActiveObject::registerType(getType(), create);
489 m_oldpos = v3f(0,0,0);
490 m_last_sent_position = v3f(0,0,0);
495 m_touching_ground = false;
497 m_after_jump_timer = 0;
500 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, v3f pos,
501 const std::string &data)
503 std::istringstream is(data, std::ios::binary);
505 u8 version = readU8(is);
508 // check if version is supported
511 Oerkki1SAO *o = new Oerkki1SAO(env, pos);
516 void Oerkki1SAO::step(float dtime, bool send_recommended)
518 ScopeProfiler sp2(g_profiler, "Oerkki1SAO::step avg", SPT_AVG);
522 if(m_is_active == false)
524 if(m_inactive_interval.step(dtime, 0.5)==false)
540 m_after_jump_timer -= dtime;
542 v3f old_speed = m_speed_f;
545 m_speed_f.Y -= dtime*9.81*BS;
548 Move around if some player is close
550 bool player_is_close = false;
551 bool player_is_too_close = false;
553 // Check connected players
554 core::list<Player*> players = m_env->getPlayers(true);
555 core::list<Player*>::Iterator i;
556 for(i = players.begin();
557 i != players.end(); i++)
560 v3f playerpos = player->getPosition();
561 f32 dist = m_base_position.getDistanceFrom(playerpos);
566 player_is_too_close = true;
567 near_player_pos = playerpos;
569 else if(dist < BS*15.0 && !player_is_too_close)
571 player_is_close = true;
572 near_player_pos = playerpos;
576 m_is_active = player_is_close;
578 v3f target_speed = m_speed_f;
582 target_speed = v3f(0,0,0);
588 v3f ndir = near_player_pos - m_base_position;
592 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
593 if(nyaw < m_yaw - 180)
595 else if(nyaw > m_yaw + 180)
597 m_yaw = 0.95*m_yaw + 0.05*nyaw;
598 m_yaw = wrapDegrees(m_yaw);
602 if((m_touching_ground || m_after_jump_timer > 0.0)
603 && !player_is_too_close)
605 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
606 target_speed.X = speed * dir.X;
607 target_speed.Z = speed * dir.Z;
610 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
618 target_speed.Y = 5.0*BS;
619 m_after_jump_timer = 1.0;
627 m_counter2 += (float)(myrand()%100)/100*3.0;
628 //m_yaw += ((float)(myrand()%200)-100)/100*180;
629 m_yaw += ((float)(myrand()%200)-100)/100*90;
630 m_yaw = wrapDegrees(m_yaw);
635 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
636 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
638 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
640 m_oldpos = m_base_position;
643 Move it, with collision detection
646 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
647 collisionMoveResult moveresult;
648 // Maximum movement without glitches
649 f32 pos_max_d = BS*0.25;
651 if(m_speed_f.getLength()*dtime > pos_max_d)
652 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
653 v3f pos_f = getBasePosition();
654 v3f pos_f_old = pos_f;
655 IGameDef *gamedef = m_env->getGameDef();
656 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
657 pos_max_d, box, dtime, pos_f, m_speed_f);
658 m_touching_ground = moveresult.touching_ground;
660 // Do collision damage
661 float tolerance = BS*30;
662 float factor = BS*0.5;
663 v3f speed_diff = old_speed - m_speed_f;
664 // Increase effect in X and Z
667 float vel = speed_diff.getLength();
670 f32 damage_f = (vel - tolerance)/BS*factor;
671 u16 damage = (u16)(damage_f+0.5);
675 setBasePosition(pos_f);
677 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
680 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
682 m_last_sent_position = pos_f;
684 std::ostringstream os(std::ios::binary);
685 // command (0 = update position)
688 writeV3F1000(os, m_base_position);
690 writeF1000(os, m_yaw);
691 // create message and add to list
692 ActiveObjectMessage aom(getId(), false, os.str());
693 m_messages_out.push_back(aom);
697 std::string Oerkki1SAO::getClientInitializationData()
699 std::ostringstream os(std::ios::binary);
703 writeV3F1000(os, m_base_position);
707 std::string Oerkki1SAO::getStaticData()
709 //infostream<<__FUNCTION_NAME<<std::endl;
710 std::ostringstream os(std::ios::binary);
718 void Oerkki1SAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
723 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
724 m_speed_f += dir*12*BS;
726 // "Material" properties of an oerkki
727 MaterialProperties mp;
728 mp.diggability = DIGGABLE_NORMAL;
729 mp.crackiness = -1.0;
730 mp.cuttability = 1.0;
732 IItemDefManager *idef = m_env->getGameDef()->idef();
733 ItemStack punchitem = puncher->getWieldedItem();
734 ToolDiggingProperties tp =
735 punchitem.getToolDiggingProperties(idef);
737 HittingProperties hitprop = getHittingProperties(&mp, &tp,
738 time_from_last_punch);
740 doDamage(hitprop.hp);
741 if(g_settings->getBool("creative_mode") == false)
743 punchitem.addWear(hitprop.wear, idef);
744 puncher->setWieldedItem(punchitem);
748 void Oerkki1SAO::doDamage(u16 d)
750 infostream<<"oerkki damage: "<<d<<std::endl;
764 std::ostringstream os(std::ios::binary);
765 // command (1 = damage)
769 // create message and add to list
770 ActiveObjectMessage aom(getId(), false, os.str());
771 m_messages_out.push_back(aom);
780 FireflySAO proto_FireflySAO(NULL, v3f(0,0,0));
782 FireflySAO::FireflySAO(ServerEnvironment *env, v3f pos):
783 ServerActiveObject(env, pos),
787 ServerActiveObject::registerType(getType(), create);
789 m_oldpos = v3f(0,0,0);
790 m_last_sent_position = v3f(0,0,0);
795 m_touching_ground = false;
798 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, v3f pos,
799 const std::string &data)
801 std::istringstream is(data, std::ios::binary);
806 // check if version is supported
809 return new FireflySAO(env, pos);
812 void FireflySAO::step(float dtime, bool send_recommended)
814 ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG);
818 if(m_is_active == false)
820 if(m_inactive_interval.step(dtime, 0.5)==false)
828 // Apply (less) gravity
829 m_speed_f.Y -= dtime*3*BS;
832 Move around if some player is close
834 bool player_is_close = false;
835 // Check connected players
836 core::list<Player*> players = m_env->getPlayers(true);
837 core::list<Player*>::Iterator i;
838 for(i = players.begin();
839 i != players.end(); i++)
842 v3f playerpos = player->getPosition();
843 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
845 player_is_close = true;
850 m_is_active = player_is_close;
852 if(player_is_close == false)
860 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
862 m_speed_f.X = speed * dir.X;
863 m_speed_f.Z = speed * dir.Z;
865 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
872 m_speed_f.Y = 5.0*BS;
880 m_counter2 += (float)(myrand()%100)/100*3.0;
881 m_yaw += ((float)(myrand()%200)-100)/100*180;
882 m_yaw = wrapDegrees(m_yaw);
887 m_oldpos = m_base_position;
890 Move it, with collision detection
893 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
894 collisionMoveResult moveresult;
895 // Maximum movement without glitches
896 f32 pos_max_d = BS*0.25;
898 if(m_speed_f.getLength()*dtime > pos_max_d)
899 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
900 v3f pos_f = getBasePosition();
901 v3f pos_f_old = pos_f;
902 IGameDef *gamedef = m_env->getGameDef();
903 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
904 pos_max_d, box, dtime, pos_f, m_speed_f);
905 m_touching_ground = moveresult.touching_ground;
907 setBasePosition(pos_f);
909 if(send_recommended == false)
912 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
914 m_last_sent_position = pos_f;
916 std::ostringstream os(std::ios::binary);
917 // command (0 = update position)
920 writeV3F1000(os, m_base_position);
922 writeF1000(os, m_yaw);
923 // create message and add to list
924 ActiveObjectMessage aom(getId(), false, os.str());
925 m_messages_out.push_back(aom);
929 std::string FireflySAO::getClientInitializationData()
931 std::ostringstream os(std::ios::binary);
935 writeV3F1000(os, m_base_position);
939 std::string FireflySAO::getStaticData()
941 //infostream<<__FUNCTION_NAME<<std::endl;
942 std::ostringstream os(std::ios::binary);
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, float time_from_last_punch)
1391 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
1393 // A quick hack; SAO description is player name for player
1394 std::string playername = puncher->getDescription();
1396 Map *map = &m_env->getMap();
1398 actionstream<<playername<<" punches mob id="<<m_id
1399 <<" at "<<PP(m_base_position/BS)<<std::endl;
1401 m_disturb_timer = 0;
1402 m_disturbing_player = playername;
1403 m_next_pos_exists = false; // Cancel moving immediately
1405 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1406 v3f new_base_position = m_base_position + dir * BS;
1408 v3s16 pos_i = floatToInt(new_base_position, BS);
1409 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1410 v3s16 pos_size_off(0,0,0);
1411 if(m_size.X >= 2.5){
1412 pos_size_off.X = -1;
1413 pos_size_off.Y = -1;
1415 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1417 m_base_position = new_base_position;
1422 // "Material" properties of the MobV2
1423 MaterialProperties mp;
1424 mp.diggability = DIGGABLE_NORMAL;
1425 mp.crackiness = -1.0;
1426 mp.cuttability = 1.0;
1428 IItemDefManager *idef = m_env->getGameDef()->idef();
1429 ItemStack punchitem = puncher->getWieldedItem();
1430 ToolDiggingProperties tp =
1431 punchitem.getToolDiggingProperties(idef);
1433 HittingProperties hitprop = getHittingProperties(&mp, &tp,
1434 time_from_last_punch);
1436 doDamage(hitprop.hp);
1437 if(g_settings->getBool("creative_mode") == false)
1439 punchitem.addWear(hitprop.wear, idef);
1440 puncher->setWieldedItem(punchitem);
1444 bool MobV2SAO::isPeaceful()
1446 return m_properties->getBool("is_peaceful");
1449 void MobV2SAO::sendPosition()
1451 m_last_sent_position = m_base_position;
1453 std::ostringstream os(std::ios::binary);
1454 // command (0 = update position)
1457 writeV3F1000(os, m_base_position);
1459 writeF1000(os, m_yaw);
1460 // create message and add to list
1461 ActiveObjectMessage aom(getId(), false, os.str());
1462 m_messages_out.push_back(aom);
1465 void MobV2SAO::setPropertyDefaults()
1467 m_properties->setDefault("is_peaceful", "false");
1468 m_properties->setDefault("move_type", "ground_nodes");
1469 m_properties->setDefault("speed", "(0,0,0)");
1470 m_properties->setDefault("age", "0");
1471 m_properties->setDefault("yaw", "0");
1472 m_properties->setDefault("pos", "(0,0,0)");
1473 m_properties->setDefault("hp", "0");
1474 m_properties->setDefault("die_age", "-1");
1475 m_properties->setDefault("size", "(1,2)");
1476 m_properties->setDefault("shoot_type", "fireball");
1477 m_properties->setDefault("shoot_y", "0");
1478 m_properties->setDefault("mindless_rage", "false");
1480 void MobV2SAO::readProperties()
1482 m_move_type = m_properties->get("move_type");
1483 m_speed = m_properties->getV3F("speed");
1484 m_age = m_properties->getFloat("age");
1485 m_yaw = m_properties->getFloat("yaw");
1486 m_base_position = m_properties->getV3F("pos");
1487 m_hp = m_properties->getS32("hp");
1488 m_die_age = m_properties->getFloat("die_age");
1489 m_size = m_properties->getV2F("size");
1491 void MobV2SAO::updateProperties()
1493 m_properties->set("move_type", m_move_type);
1494 m_properties->setV3F("speed", m_speed);
1495 m_properties->setFloat("age", m_age);
1496 m_properties->setFloat("yaw", m_yaw);
1497 m_properties->setV3F("pos", m_base_position);
1498 m_properties->setS32("hp", m_hp);
1499 m_properties->setFloat("die_age", m_die_age);
1500 m_properties->setV2F("size", m_size);
1502 m_properties->setS32("version", 0);
1505 void MobV2SAO::doDamage(u16 d)
1507 infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1515 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1516 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1523 std::ostringstream os(std::ios::binary);
1524 // command (1 = damage)
1528 // create message and add to list
1529 ActiveObjectMessage aom(getId(), false, os.str());
1530 m_messages_out.push_back(aom);
1539 #include "scriptapi.h"
1540 #include "luaentity_common.h"
1543 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
1545 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
1546 const std::string &name, const std::string &state):
1547 ServerActiveObject(env, pos),
1549 m_init_state(state),
1550 m_registered(false),
1551 m_prop(new LuaEntityProperties),
1553 m_acceleration(0,0,0),
1556 m_last_sent_position(0,0,0),
1557 m_last_sent_velocity(0,0,0),
1558 m_last_sent_position_timer(0),
1559 m_last_sent_move_precision(0)
1561 // Only register type if no environment supplied
1563 ServerActiveObject::registerType(getType(), create);
1568 LuaEntitySAO::~LuaEntitySAO()
1571 lua_State *L = m_env->getLua();
1572 scriptapi_luaentity_rm(L, m_id);
1577 void LuaEntitySAO::addedToEnvironment()
1579 ServerActiveObject::addedToEnvironment();
1581 // Create entity from name and state
1582 lua_State *L = m_env->getLua();
1583 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str(),
1584 m_init_state.c_str());
1588 scriptapi_luaentity_get_properties(L, m_id, m_prop);
1592 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
1593 const std::string &data)
1595 std::istringstream is(data, std::ios::binary);
1597 u8 version = readU8(is);
1598 // check if version is supported
1602 std::string name = deSerializeString(is);
1604 std::string state = deSerializeLongString(is);
1606 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
1607 <<state<<"\")"<<std::endl;
1608 return new LuaEntitySAO(env, pos, name, state);
1611 void LuaEntitySAO::step(float dtime, bool send_recommended)
1613 m_last_sent_position_timer += dtime;
1615 if(m_prop->physical){
1616 core::aabbox3d<f32> box = m_prop->collisionbox;
1619 collisionMoveResult moveresult;
1620 f32 pos_max_d = BS*0.25; // Distance per iteration
1621 v3f p_pos = getBasePosition();
1622 v3f p_velocity = m_velocity;
1623 IGameDef *gamedef = m_env->getGameDef();
1624 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
1625 pos_max_d, box, dtime, p_pos, p_velocity);
1627 setBasePosition(p_pos);
1628 m_velocity = p_velocity;
1630 m_velocity += dtime * m_acceleration;
1632 m_base_position += dtime * m_velocity + 0.5 * dtime
1633 * dtime * m_acceleration;
1634 m_velocity += dtime * m_acceleration;
1638 lua_State *L = m_env->getLua();
1639 scriptapi_luaentity_step(L, m_id, dtime);
1642 if(send_recommended == false)
1645 // TODO: force send when acceleration changes enough?
1646 float minchange = 0.2*BS;
1647 if(m_last_sent_position_timer > 1.0){
1648 minchange = 0.01*BS;
1649 } else if(m_last_sent_position_timer > 0.2){
1650 minchange = 0.05*BS;
1652 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
1653 move_d += m_last_sent_move_precision;
1654 float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
1655 if(move_d > minchange || vel_d > minchange ||
1656 fabs(m_yaw - m_last_sent_yaw) > 1.0){
1657 sendPosition(true, false);
1661 std::string LuaEntitySAO::getClientInitializationData()
1663 std::ostringstream os(std::ios::binary);
1667 writeV3F1000(os, m_base_position);
1669 writeF1000(os, m_yaw);
1671 std::ostringstream prop_os(std::ios::binary);
1672 m_prop->serialize(prop_os);
1673 os<<serializeLongString(prop_os.str());
1678 std::string LuaEntitySAO::getStaticData()
1680 infostream<<__FUNCTION_NAME<<std::endl;
1681 std::ostringstream os(std::ios::binary);
1685 os<<serializeString(m_init_name);
1688 lua_State *L = m_env->getLua();
1689 std::string state = scriptapi_luaentity_get_staticdata(L, m_id);
1690 os<<serializeLongString(state);
1692 os<<serializeLongString(m_init_state);
1697 void LuaEntitySAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
1700 // Delete unknown LuaEntities when punched
1704 lua_State *L = m_env->getLua();
1705 scriptapi_luaentity_punch(L, m_id, puncher, time_from_last_punch);
1708 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
1712 lua_State *L = m_env->getLua();
1713 scriptapi_luaentity_rightclick(L, m_id, clicker);
1716 void LuaEntitySAO::setPos(v3f pos)
1718 m_base_position = pos;
1719 sendPosition(false, true);
1722 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
1724 m_base_position = pos;
1726 sendPosition(true, true);
1729 float LuaEntitySAO::getMinimumSavedMovement()
1734 void LuaEntitySAO::setVelocity(v3f velocity)
1736 m_velocity = velocity;
1739 v3f LuaEntitySAO::getVelocity()
1744 void LuaEntitySAO::setAcceleration(v3f acceleration)
1746 m_acceleration = acceleration;
1749 v3f LuaEntitySAO::getAcceleration()
1751 return m_acceleration;
1754 void LuaEntitySAO::setYaw(float yaw)
1759 float LuaEntitySAO::getYaw()
1764 void LuaEntitySAO::setTextureMod(const std::string &mod)
1766 std::ostringstream os(std::ios::binary);
1767 // command (1 = set texture modification)
1770 os<<serializeString(mod);
1771 // create message and add to list
1772 ActiveObjectMessage aom(getId(), false, os.str());
1773 m_messages_out.push_back(aom);
1776 void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
1777 bool select_horiz_by_yawpitch)
1779 std::ostringstream os(std::ios::binary);
1780 // command (2 = set sprite)
1784 writeU16(os, num_frames);
1785 writeF1000(os, framelength);
1786 writeU8(os, select_horiz_by_yawpitch);
1787 // create message and add to list
1788 ActiveObjectMessage aom(getId(), false, os.str());
1789 m_messages_out.push_back(aom);
1792 std::string LuaEntitySAO::getName()
1797 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
1799 m_last_sent_move_precision = m_base_position.getDistanceFrom(
1800 m_last_sent_position);
1801 m_last_sent_position_timer = 0;
1802 m_last_sent_yaw = m_yaw;
1803 m_last_sent_position = m_base_position;
1804 m_last_sent_velocity = m_velocity;
1805 //m_last_sent_acceleration = m_acceleration;
1807 float update_interval = m_env->getSendRecommendedInterval();
1809 std::ostringstream os(std::ios::binary);
1810 // command (0 = update position)
1814 writeU8(os, do_interpolate);
1816 writeV3F1000(os, m_base_position);
1818 writeV3F1000(os, m_velocity);
1820 writeV3F1000(os, m_acceleration);
1822 writeF1000(os, m_yaw);
1823 // is_end_position (for interpolation)
1824 writeU8(os, is_movement_end);
1825 // update_interval (for interpolation)
1826 writeF1000(os, update_interval);
1828 // create message and add to list
1829 ActiveObjectMessage aom(getId(), false, os.str());
1830 m_messages_out.push_back(aom);