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
28 #include "tooldef.h" // 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 inventorystring):
118 ServerActiveObject(env, pos),
119 m_inventorystring(inventorystring),
121 m_last_sent_position(0,0,0)
123 ServerActiveObject::registerType(getType(), create);
126 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, v3f pos,
127 const std::string &data)
129 std::istringstream is(data, std::ios::binary);
134 // check if version is supported
137 std::string inventorystring = deSerializeString(is);
138 infostream<<"ItemSAO::create(): Creating item \""
139 <<inventorystring<<"\""<<std::endl;
140 return new ItemSAO(env, pos, inventorystring);
143 void ItemSAO::step(float dtime, bool send_recommended)
145 ScopeProfiler sp2(g_profiler, "ItemSAO::step avg", SPT_AVG);
149 const float interval = 0.2;
150 if(m_move_interval.step(dtime, interval)==false)
154 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
155 collisionMoveResult moveresult;
157 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
158 // Maximum movement without glitches
159 f32 pos_max_d = BS*0.25;
161 if(m_speed_f.getLength()*dtime > pos_max_d)
162 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
163 v3f pos_f = getBasePosition();
164 v3f pos_f_old = pos_f;
165 IGameDef *gamedef = m_env->getGameDef();
166 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
167 pos_max_d, box, dtime, pos_f, m_speed_f);
169 if(send_recommended == false)
172 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
174 setBasePosition(pos_f);
175 m_last_sent_position = pos_f;
177 std::ostringstream os(std::ios::binary);
179 // command (0 = update position)
183 writeS32((u8*)buf, m_base_position.X*1000);
185 writeS32((u8*)buf, m_base_position.Y*1000);
187 writeS32((u8*)buf, m_base_position.Z*1000);
189 // create message and add to list
190 ActiveObjectMessage aom(getId(), false, os.str());
191 m_messages_out.push_back(aom);
195 std::string ItemSAO::getClientInitializationData()
197 std::ostringstream os(std::ios::binary);
203 writeS32((u8*)buf, m_base_position.X*1000);
205 writeS32((u8*)buf, m_base_position.Y*1000);
207 writeS32((u8*)buf, m_base_position.Z*1000);
210 os<<serializeString(m_inventorystring);
214 std::string ItemSAO::getStaticData()
216 infostream<<__FUNCTION_NAME<<std::endl;
217 std::ostringstream os(std::ios::binary);
223 os<<serializeString(m_inventorystring);
227 InventoryItem * ItemSAO::createInventoryItem()
230 std::istringstream is(m_inventorystring, std::ios_base::binary);
231 IGameDef *gamedef = m_env->getGameDef();
232 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
233 infostream<<__FUNCTION_NAME<<": m_inventorystring=\""
234 <<m_inventorystring<<"\" -> item="<<item
238 catch(SerializationError &e)
240 infostream<<__FUNCTION_NAME<<": serialization error: "
241 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
246 void ItemSAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
248 InventoryItem *item = createInventoryItem();
249 bool fits = puncher->addToInventory(item);
261 RatSAO proto_RatSAO(NULL, v3f(0,0,0));
263 RatSAO::RatSAO(ServerEnvironment *env, v3f pos):
264 ServerActiveObject(env, pos),
268 ServerActiveObject::registerType(getType(), create);
270 m_oldpos = v3f(0,0,0);
271 m_last_sent_position = v3f(0,0,0);
272 m_yaw = myrand_range(0,PI*2);
276 m_touching_ground = false;
279 ServerActiveObject* RatSAO::create(ServerEnvironment *env, v3f pos,
280 const std::string &data)
282 std::istringstream is(data, std::ios::binary);
287 // check if version is supported
290 return new RatSAO(env, pos);
293 void RatSAO::step(float dtime, bool send_recommended)
295 ScopeProfiler sp2(g_profiler, "RatSAO::step avg", SPT_AVG);
299 if(m_is_active == false)
301 if(m_inactive_interval.step(dtime, 0.5)==false)
318 m_speed_f.Y -= dtime*9.81*BS;
321 Move around if some player is close
323 bool player_is_close = false;
324 // Check connected players
325 core::list<Player*> players = m_env->getPlayers(true);
326 core::list<Player*>::Iterator i;
327 for(i = players.begin();
328 i != players.end(); i++)
331 v3f playerpos = player->getPosition();
332 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
334 player_is_close = true;
339 m_is_active = player_is_close;
341 if(player_is_close == false)
349 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
351 m_speed_f.X = speed * dir.X;
352 m_speed_f.Z = speed * dir.Z;
354 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
361 m_speed_f.Y = 5.0*BS;
369 m_counter2 += (float)(myrand()%100)/100*3.0;
370 m_yaw += ((float)(myrand()%200)-100)/100*180;
371 m_yaw = wrapDegrees(m_yaw);
376 m_oldpos = m_base_position;
379 Move it, with collision detection
382 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
383 collisionMoveResult moveresult;
384 // Maximum movement without glitches
385 f32 pos_max_d = BS*0.25;
387 if(m_speed_f.getLength()*dtime > pos_max_d)
388 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
389 v3f pos_f = getBasePosition();
390 v3f pos_f_old = pos_f;
391 IGameDef *gamedef = m_env->getGameDef();
392 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
393 pos_max_d, box, dtime, pos_f, m_speed_f);
394 m_touching_ground = moveresult.touching_ground;
396 setBasePosition(pos_f);
398 if(send_recommended == false)
401 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
403 m_last_sent_position = pos_f;
405 std::ostringstream os(std::ios::binary);
406 // command (0 = update position)
409 writeV3F1000(os, m_base_position);
411 writeF1000(os, m_yaw);
412 // create message and add to list
413 ActiveObjectMessage aom(getId(), false, os.str());
414 m_messages_out.push_back(aom);
418 std::string RatSAO::getClientInitializationData()
420 std::ostringstream os(std::ios::binary);
424 writeV3F1000(os, m_base_position);
428 std::string RatSAO::getStaticData()
430 //infostream<<__FUNCTION_NAME<<std::endl;
431 std::ostringstream os(std::ios::binary);
437 void RatSAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
439 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
440 IGameDef *gamedef = m_env->getGameDef();
441 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
442 bool fits = puncher->addToInventory(item);
454 Oerkki1SAO proto_Oerkki1SAO(NULL, v3f(0,0,0));
456 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, v3f pos):
457 ServerActiveObject(env, pos),
461 ServerActiveObject::registerType(getType(), create);
463 m_oldpos = v3f(0,0,0);
464 m_last_sent_position = v3f(0,0,0);
469 m_touching_ground = false;
471 m_after_jump_timer = 0;
474 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, v3f pos,
475 const std::string &data)
477 std::istringstream is(data, std::ios::binary);
479 u8 version = readU8(is);
482 // check if version is supported
485 Oerkki1SAO *o = new Oerkki1SAO(env, pos);
490 void Oerkki1SAO::step(float dtime, bool send_recommended)
492 ScopeProfiler sp2(g_profiler, "Oerkki1SAO::step avg", SPT_AVG);
496 if(m_is_active == false)
498 if(m_inactive_interval.step(dtime, 0.5)==false)
514 m_after_jump_timer -= dtime;
516 v3f old_speed = m_speed_f;
519 m_speed_f.Y -= dtime*9.81*BS;
522 Move around if some player is close
524 bool player_is_close = false;
525 bool player_is_too_close = false;
527 // Check connected players
528 core::list<Player*> players = m_env->getPlayers(true);
529 core::list<Player*>::Iterator i;
530 for(i = players.begin();
531 i != players.end(); i++)
534 v3f playerpos = player->getPosition();
535 f32 dist = m_base_position.getDistanceFrom(playerpos);
540 player_is_too_close = true;
541 near_player_pos = playerpos;
543 else if(dist < BS*15.0 && !player_is_too_close)
545 player_is_close = true;
546 near_player_pos = playerpos;
550 m_is_active = player_is_close;
552 v3f target_speed = m_speed_f;
556 target_speed = v3f(0,0,0);
562 v3f ndir = near_player_pos - m_base_position;
566 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
567 if(nyaw < m_yaw - 180)
569 else if(nyaw > m_yaw + 180)
571 m_yaw = 0.95*m_yaw + 0.05*nyaw;
572 m_yaw = wrapDegrees(m_yaw);
576 if((m_touching_ground || m_after_jump_timer > 0.0)
577 && !player_is_too_close)
579 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
580 target_speed.X = speed * dir.X;
581 target_speed.Z = speed * dir.Z;
584 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
592 target_speed.Y = 5.0*BS;
593 m_after_jump_timer = 1.0;
601 m_counter2 += (float)(myrand()%100)/100*3.0;
602 //m_yaw += ((float)(myrand()%200)-100)/100*180;
603 m_yaw += ((float)(myrand()%200)-100)/100*90;
604 m_yaw = wrapDegrees(m_yaw);
609 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
610 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
612 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
614 m_oldpos = m_base_position;
617 Move it, with collision detection
620 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
621 collisionMoveResult moveresult;
622 // Maximum movement without glitches
623 f32 pos_max_d = BS*0.25;
625 if(m_speed_f.getLength()*dtime > pos_max_d)
626 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
627 v3f pos_f = getBasePosition();
628 v3f pos_f_old = pos_f;
629 IGameDef *gamedef = m_env->getGameDef();
630 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
631 pos_max_d, box, dtime, pos_f, m_speed_f);
632 m_touching_ground = moveresult.touching_ground;
634 // Do collision damage
635 float tolerance = BS*30;
636 float factor = BS*0.5;
637 v3f speed_diff = old_speed - m_speed_f;
638 // Increase effect in X and Z
641 float vel = speed_diff.getLength();
644 f32 damage_f = (vel - tolerance)/BS*factor;
645 u16 damage = (u16)(damage_f+0.5);
649 setBasePosition(pos_f);
651 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
654 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
656 m_last_sent_position = pos_f;
658 std::ostringstream os(std::ios::binary);
659 // command (0 = update position)
662 writeV3F1000(os, m_base_position);
664 writeF1000(os, m_yaw);
665 // create message and add to list
666 ActiveObjectMessage aom(getId(), false, os.str());
667 m_messages_out.push_back(aom);
671 std::string Oerkki1SAO::getClientInitializationData()
673 std::ostringstream os(std::ios::binary);
677 writeV3F1000(os, m_base_position);
681 std::string Oerkki1SAO::getStaticData()
683 //infostream<<__FUNCTION_NAME<<std::endl;
684 std::ostringstream os(std::ios::binary);
692 void Oerkki1SAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
697 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
698 m_speed_f += dir*12*BS;
700 // "Material" properties of an oerkki
701 MaterialProperties mp;
702 mp.diggability = DIGGABLE_NORMAL;
703 mp.crackiness = -1.0;
704 mp.cuttability = 1.0;
706 ToolDiggingProperties tp;
707 puncher->getWieldDiggingProperties(&tp);
709 HittingProperties hitprop = getHittingProperties(&mp, &tp,
710 time_from_last_punch);
712 doDamage(hitprop.hp);
713 puncher->damageWieldedItem(hitprop.wear);
716 void Oerkki1SAO::doDamage(u16 d)
718 infostream<<"oerkki damage: "<<d<<std::endl;
732 std::ostringstream os(std::ios::binary);
733 // command (1 = damage)
737 // create message and add to list
738 ActiveObjectMessage aom(getId(), false, os.str());
739 m_messages_out.push_back(aom);
748 FireflySAO proto_FireflySAO(NULL, v3f(0,0,0));
750 FireflySAO::FireflySAO(ServerEnvironment *env, v3f pos):
751 ServerActiveObject(env, pos),
755 ServerActiveObject::registerType(getType(), create);
757 m_oldpos = v3f(0,0,0);
758 m_last_sent_position = v3f(0,0,0);
763 m_touching_ground = false;
766 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, v3f pos,
767 const std::string &data)
769 std::istringstream is(data, std::ios::binary);
774 // check if version is supported
777 return new FireflySAO(env, pos);
780 void FireflySAO::step(float dtime, bool send_recommended)
782 ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG);
786 if(m_is_active == false)
788 if(m_inactive_interval.step(dtime, 0.5)==false)
796 // Apply (less) gravity
797 m_speed_f.Y -= dtime*3*BS;
800 Move around if some player is close
802 bool player_is_close = false;
803 // Check connected players
804 core::list<Player*> players = m_env->getPlayers(true);
805 core::list<Player*>::Iterator i;
806 for(i = players.begin();
807 i != players.end(); i++)
810 v3f playerpos = player->getPosition();
811 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
813 player_is_close = true;
818 m_is_active = player_is_close;
820 if(player_is_close == false)
828 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
830 m_speed_f.X = speed * dir.X;
831 m_speed_f.Z = speed * dir.Z;
833 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
840 m_speed_f.Y = 5.0*BS;
848 m_counter2 += (float)(myrand()%100)/100*3.0;
849 m_yaw += ((float)(myrand()%200)-100)/100*180;
850 m_yaw = wrapDegrees(m_yaw);
855 m_oldpos = m_base_position;
858 Move it, with collision detection
861 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
862 collisionMoveResult moveresult;
863 // Maximum movement without glitches
864 f32 pos_max_d = BS*0.25;
866 if(m_speed_f.getLength()*dtime > pos_max_d)
867 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
868 v3f pos_f = getBasePosition();
869 v3f pos_f_old = pos_f;
870 IGameDef *gamedef = m_env->getGameDef();
871 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
872 pos_max_d, box, dtime, pos_f, m_speed_f);
873 m_touching_ground = moveresult.touching_ground;
875 setBasePosition(pos_f);
877 if(send_recommended == false)
880 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
882 m_last_sent_position = pos_f;
884 std::ostringstream os(std::ios::binary);
885 // command (0 = update position)
888 writeV3F1000(os, m_base_position);
890 writeF1000(os, m_yaw);
891 // create message and add to list
892 ActiveObjectMessage aom(getId(), false, os.str());
893 m_messages_out.push_back(aom);
897 std::string FireflySAO::getClientInitializationData()
899 std::ostringstream os(std::ios::binary);
903 writeV3F1000(os, m_base_position);
907 std::string FireflySAO::getStaticData()
909 //infostream<<__FUNCTION_NAME<<std::endl;
910 std::ostringstream os(std::ios::binary);
921 MobV2SAO proto_MobV2SAO(NULL, v3f(0,0,0), NULL);
923 MobV2SAO::MobV2SAO(ServerEnvironment *env, v3f pos,
924 Settings *init_properties):
925 ServerActiveObject(env, pos),
926 m_move_type("ground_nodes"),
928 m_last_sent_position(0,0,0),
934 m_touching_ground(false),
936 m_walk_around(false),
937 m_walk_around_timer(0),
938 m_next_pos_exists(false),
939 m_shoot_reload_timer(0),
943 m_disturb_timer(100000),
944 m_random_disturb_timer(0),
947 ServerActiveObject::registerType(getType(), create);
949 m_properties = new Settings();
951 m_properties->update(*init_properties);
953 m_properties->setV3F("pos", pos);
955 setPropertyDefaults();
959 MobV2SAO::~MobV2SAO()
964 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, v3f pos,
965 const std::string &data)
967 std::istringstream is(data, std::ios::binary);
969 properties.parseConfigLines(is, "MobArgsEnd");
970 MobV2SAO *o = new MobV2SAO(env, pos, &properties);
974 std::string MobV2SAO::getStaticData()
978 std::ostringstream os(std::ios::binary);
979 m_properties->writeLines(os);
983 std::string MobV2SAO::getClientInitializationData()
985 //infostream<<__FUNCTION_NAME<<std::endl;
989 std::ostringstream os(std::ios::binary);
994 Settings client_properties;
996 /*client_properties.set("version", "0");
997 client_properties.updateValue(*m_properties, "pos");
998 client_properties.updateValue(*m_properties, "yaw");
999 client_properties.updateValue(*m_properties, "hp");*/
1001 // Just send everything for simplicity
1002 client_properties.update(*m_properties);
1004 std::ostringstream os2(std::ios::binary);
1005 client_properties.writeLines(os2);
1006 compressZlib(os2.str(), os);
1011 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
1013 for(int dx=0; dx<size.X; dx++)
1014 for(int dy=0; dy<size.Y; dy++)
1015 for(int dz=0; dz<size.Z; dz++){
1016 v3s16 dp(dx, dy, dz);
1018 MapNode n = map->getNodeNoEx(p);
1019 if(n.getContent() != CONTENT_AIR)
1025 bool checkWalkablePosition(Map *map, v3s16 p0)
1027 v3s16 p = p0 + v3s16(0,-1,0);
1028 MapNode n = map->getNodeNoEx(p);
1029 if(n.getContent() != CONTENT_AIR)
1034 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1036 if(!checkFreePosition(map, p0, size))
1038 if(!checkWalkablePosition(map, p0))
1043 static void get_random_u32_array(u32 a[], u32 len)
1046 for(i=0; i<len; i++)
1050 u32 k = myrand() % n;
1058 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1060 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1062 core::map<v3s16, MapBlock*> modified_blocks;
1064 for(int dx=0; dx<size.X; dx++)
1065 for(int dy=0; dy<size.Y; dy++)
1066 for(int dz=0; dz<size.Z; dz++){
1067 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1069 MapNode n = map->getNodeNoEx(p);
1070 if(n.getContent() == CONTENT_IGNORE)
1072 //map->removeNodeWithEvent(p);
1073 map->removeNodeAndUpdate(p, modified_blocks);
1076 // Send a MEET_OTHER event
1078 event.type = MEET_OTHER;
1079 for(core::map<v3s16, MapBlock*>::Iterator
1080 i = modified_blocks.getIterator();
1081 i.atEnd() == false; i++)
1083 v3s16 p = i.getNode()->getKey();
1084 event.modified_blocks.insert(p, true);
1086 map->dispatchEvent(&event);
1089 void MobV2SAO::step(float dtime, bool send_recommended)
1091 ScopeProfiler sp2(g_profiler, "MobV2SAO::step avg", SPT_AVG);
1094 Map *map = &m_env->getMap();
1098 if(m_die_age >= 0.0 && m_age >= m_die_age){
1103 m_random_disturb_timer += dtime;
1104 if(m_random_disturb_timer >= 5.0)
1106 m_random_disturb_timer = 0;
1107 // Check connected players
1108 core::list<Player*> players = m_env->getPlayers(true);
1109 core::list<Player*>::Iterator i;
1110 for(i = players.begin();
1111 i != players.end(); i++)
1113 Player *player = *i;
1114 v3f playerpos = player->getPosition();
1115 f32 dist = m_base_position.getDistanceFrom(playerpos);
1118 if(myrand_range(0,3) == 0){
1119 actionstream<<"Mob id="<<m_id<<" at "
1120 <<PP(m_base_position/BS)
1121 <<" got randomly disturbed by "
1122 <<player->getName()<<std::endl;
1123 m_disturbing_player = player->getName();
1124 m_disturb_timer = 0;
1131 Player *disturbing_player =
1132 m_env->getPlayer(m_disturbing_player.c_str());
1133 v3f disturbing_player_off = v3f(0,1,0);
1134 v3f disturbing_player_norm = v3f(0,1,0);
1135 float disturbing_player_distance = 1000000;
1136 float disturbing_player_dir = 0;
1137 if(disturbing_player){
1138 disturbing_player_off =
1139 disturbing_player->getPosition() - m_base_position;
1140 disturbing_player_distance = disturbing_player_off.getLength();
1141 disturbing_player_norm = disturbing_player_off;
1142 disturbing_player_norm.normalize();
1143 disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z,
1144 disturbing_player_norm.X);
1147 m_disturb_timer += dtime;
1151 m_shooting_timer -= dtime;
1152 if(m_shooting_timer <= 0.0 && m_shooting){
1155 std::string shoot_type = m_properties->get("shoot_type");
1156 v3f shoot_pos(0,0,0);
1157 shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1158 if(shoot_type == "fireball"){
1159 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1162 v3f speed = dir * BS * 10.0;
1163 v3f pos = m_base_position + shoot_pos;
1164 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1165 <<" shooting fireball from "<<PP(pos)
1166 <<" at speed "<<PP(speed)<<std::endl;
1167 Settings properties;
1168 properties.set("looks", "fireball");
1169 properties.setV3F("speed", speed);
1170 properties.setFloat("die_age", 5.0);
1171 properties.set("move_type", "constant_speed");
1172 properties.setFloat("hp", 1000);
1173 properties.set("lock_full_brightness", "true");
1174 properties.set("player_hit_damage", "9");
1175 properties.set("player_hit_distance", "2");
1176 properties.set("player_hit_interval", "1");
1177 ServerActiveObject *obj = new MobV2SAO(m_env,
1179 //m_env->addActiveObjectAsStatic(obj);
1180 m_env->addActiveObject(obj);
1182 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1183 <<": Unknown shoot_type="<<shoot_type
1188 m_shoot_reload_timer += dtime;
1190 float reload_time = 15.0;
1191 if(m_disturb_timer <= 15.0)
1194 bool shoot_without_player = false;
1195 if(m_properties->getBool("mindless_rage"))
1196 shoot_without_player = true;
1198 if(!m_shooting && m_shoot_reload_timer >= reload_time &&
1199 !m_next_pos_exists &&
1200 (m_disturb_timer <= 60.0 || shoot_without_player))
1203 if(m_disturb_timer < 60.0 && disturbing_player &&
1204 disturbing_player_distance < 16*BS &&
1205 fabs(disturbing_player_norm.Y) < 0.8){
1206 m_yaw = disturbing_player_dir;
1208 m_shoot_y += disturbing_player_norm.Y;
1210 m_shoot_y = 0.01 * myrand_range(-30,10);
1212 m_shoot_reload_timer = 0.0;
1214 m_shooting_timer = 1.5;
1216 std::ostringstream os(std::ios::binary);
1217 // command (2 = shooting)
1220 writeF1000(os, m_shooting_timer + 0.1);
1223 // create message and add to list
1224 ActiveObjectMessage aom(getId(), false, os.str());
1225 m_messages_out.push_back(aom);
1230 if(m_move_type == "ground_nodes")
1233 m_walk_around_timer -= dtime;
1234 if(m_walk_around_timer <= 0.0){
1235 m_walk_around = !m_walk_around;
1237 m_walk_around_timer = 0.1*myrand_range(10,50);
1239 m_walk_around_timer = 0.1*myrand_range(30,70);
1244 if(m_next_pos_exists){
1245 v3f pos_f = m_base_position;
1246 v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1248 v3f v = next_pos_f - pos_f;
1249 m_yaw = atan2(v.Z, v.X) / PI * 180;
1251 v3f diff = next_pos_f - pos_f;
1254 float speed = BS * 0.5;
1257 dir *= dtime * speed;
1258 bool arrived = false;
1259 if(dir.getLength() > diff.getLength()){
1264 m_base_position = pos_f;
1266 if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1267 m_next_pos_exists = false;
1271 v3s16 pos_i = floatToInt(m_base_position, BS);
1272 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1273 v3s16 pos_size_off(0,0,0);
1274 if(m_size.X >= 2.5){
1275 pos_size_off.X = -1;
1276 pos_size_off.Y = -1;
1279 if(!m_next_pos_exists){
1280 /* Check whether to drop down */
1281 if(checkFreePosition(map,
1282 pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1283 m_next_pos_i = pos_i + v3s16(0,-1,0);
1284 m_next_pos_exists = true;
1293 if(!m_next_pos_exists){
1294 /* Find some position where to go next */
1297 for(int dx=-1; dx<=1; dx++)
1298 for(int dy=-1; dy<=1; dy++)
1299 for(int dz=-1; dz<=1; dz++){
1300 if(dx == 0 && dy == 0)
1302 if(dx != 0 && dz != 0 && dy != 0)
1304 dps[num_dps++] = v3s16(dx,dy,dz);
1307 get_random_u32_array(order, num_dps);
1308 for(int i=0; i<num_dps; i++){
1309 v3s16 p = dps[order[i]] + pos_i;
1310 bool is_free = checkFreeAndWalkablePosition(map,
1311 p + pos_size_off, size_blocks);
1315 m_next_pos_exists = true;
1321 else if(m_move_type == "constant_speed")
1323 m_base_position += m_speed * dtime;
1325 v3s16 pos_i = floatToInt(m_base_position, BS);
1326 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1327 v3s16 pos_size_off(0,0,0);
1328 if(m_size.X >= 2.5){
1329 pos_size_off.X = -1;
1330 pos_size_off.Y = -1;
1332 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1334 explodeSquare(map, pos_i, v3s16(3,3,3));
1341 errorstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1342 <<m_move_type<<"\""<<std::endl;
1345 if(send_recommended == false)
1348 if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1354 void MobV2SAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
1359 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
1361 // A quick hack; SAO description is player name for player
1362 std::string playername = puncher->getDescription();
1364 Map *map = &m_env->getMap();
1366 actionstream<<playername<<" punches mob id="<<m_id
1367 <<" at "<<PP(m_base_position/BS)<<std::endl;
1369 m_disturb_timer = 0;
1370 m_disturbing_player = playername;
1371 m_next_pos_exists = false; // Cancel moving immediately
1373 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1374 v3f new_base_position = m_base_position + dir * BS;
1376 v3s16 pos_i = floatToInt(new_base_position, BS);
1377 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1378 v3s16 pos_size_off(0,0,0);
1379 if(m_size.X >= 2.5){
1380 pos_size_off.X = -1;
1381 pos_size_off.Y = -1;
1383 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1385 m_base_position = new_base_position;
1390 // "Material" properties of the MobV2
1391 MaterialProperties mp;
1392 mp.diggability = DIGGABLE_NORMAL;
1393 mp.crackiness = -1.0;
1394 mp.cuttability = 1.0;
1396 ToolDiggingProperties tp;
1397 puncher->getWieldDiggingProperties(&tp);
1399 HittingProperties hitprop = getHittingProperties(&mp, &tp,
1400 time_from_last_punch);
1402 doDamage(hitprop.hp);
1403 puncher->damageWieldedItem(hitprop.wear);
1406 bool MobV2SAO::isPeaceful()
1408 return m_properties->getBool("is_peaceful");
1411 void MobV2SAO::sendPosition()
1413 m_last_sent_position = m_base_position;
1415 std::ostringstream os(std::ios::binary);
1416 // command (0 = update position)
1419 writeV3F1000(os, m_base_position);
1421 writeF1000(os, m_yaw);
1422 // create message and add to list
1423 ActiveObjectMessage aom(getId(), false, os.str());
1424 m_messages_out.push_back(aom);
1427 void MobV2SAO::setPropertyDefaults()
1429 m_properties->setDefault("is_peaceful", "false");
1430 m_properties->setDefault("move_type", "ground_nodes");
1431 m_properties->setDefault("speed", "(0,0,0)");
1432 m_properties->setDefault("age", "0");
1433 m_properties->setDefault("yaw", "0");
1434 m_properties->setDefault("pos", "(0,0,0)");
1435 m_properties->setDefault("hp", "0");
1436 m_properties->setDefault("die_age", "-1");
1437 m_properties->setDefault("size", "(1,2)");
1438 m_properties->setDefault("shoot_type", "fireball");
1439 m_properties->setDefault("shoot_y", "0");
1440 m_properties->setDefault("mindless_rage", "false");
1442 void MobV2SAO::readProperties()
1444 m_move_type = m_properties->get("move_type");
1445 m_speed = m_properties->getV3F("speed");
1446 m_age = m_properties->getFloat("age");
1447 m_yaw = m_properties->getFloat("yaw");
1448 m_base_position = m_properties->getV3F("pos");
1449 m_hp = m_properties->getS32("hp");
1450 m_die_age = m_properties->getFloat("die_age");
1451 m_size = m_properties->getV2F("size");
1453 void MobV2SAO::updateProperties()
1455 m_properties->set("move_type", m_move_type);
1456 m_properties->setV3F("speed", m_speed);
1457 m_properties->setFloat("age", m_age);
1458 m_properties->setFloat("yaw", m_yaw);
1459 m_properties->setV3F("pos", m_base_position);
1460 m_properties->setS32("hp", m_hp);
1461 m_properties->setFloat("die_age", m_die_age);
1462 m_properties->setV2F("size", m_size);
1464 m_properties->setS32("version", 0);
1467 void MobV2SAO::doDamage(u16 d)
1469 infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1477 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1478 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1485 std::ostringstream os(std::ios::binary);
1486 // command (1 = damage)
1490 // create message and add to list
1491 ActiveObjectMessage aom(getId(), false, os.str());
1492 m_messages_out.push_back(aom);
1501 #include "scriptapi.h"
1502 #include "luaentity_common.h"
1505 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
1507 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
1508 const std::string &name, const std::string &state):
1509 ServerActiveObject(env, pos),
1511 m_init_state(state),
1512 m_registered(false),
1513 m_prop(new LuaEntityProperties),
1515 m_acceleration(0,0,0),
1518 m_last_sent_position(0,0,0),
1519 m_last_sent_velocity(0,0,0),
1520 m_last_sent_position_timer(0),
1521 m_last_sent_move_precision(0)
1523 // Only register type if no environment supplied
1525 ServerActiveObject::registerType(getType(), create);
1530 LuaEntitySAO::~LuaEntitySAO()
1533 lua_State *L = m_env->getLua();
1534 scriptapi_luaentity_rm(L, m_id);
1539 void LuaEntitySAO::addedToEnvironment()
1541 ServerActiveObject::addedToEnvironment();
1543 // Create entity from name and state
1544 lua_State *L = m_env->getLua();
1545 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str(),
1546 m_init_state.c_str());
1550 scriptapi_luaentity_get_properties(L, m_id, m_prop);
1554 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
1555 const std::string &data)
1557 std::istringstream is(data, std::ios::binary);
1559 u8 version = readU8(is);
1560 // check if version is supported
1564 std::string name = deSerializeString(is);
1566 std::string state = deSerializeLongString(is);
1568 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
1569 <<state<<"\")"<<std::endl;
1570 return new LuaEntitySAO(env, pos, name, state);
1573 void LuaEntitySAO::step(float dtime, bool send_recommended)
1575 m_last_sent_position_timer += dtime;
1577 if(m_prop->physical){
1578 core::aabbox3d<f32> box = m_prop->collisionbox;
1581 collisionMoveResult moveresult;
1582 f32 pos_max_d = BS*0.25; // Distance per iteration
1583 v3f p_pos = getBasePosition();
1584 v3f p_velocity = m_velocity;
1585 IGameDef *gamedef = m_env->getGameDef();
1586 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
1587 pos_max_d, box, dtime, p_pos, p_velocity);
1589 setBasePosition(p_pos);
1590 m_velocity = p_velocity;
1592 m_velocity += dtime * m_acceleration;
1594 m_base_position += dtime * m_velocity + 0.5 * dtime
1595 * dtime * m_acceleration;
1596 m_velocity += dtime * m_acceleration;
1600 lua_State *L = m_env->getLua();
1601 scriptapi_luaentity_step(L, m_id, dtime);
1604 if(send_recommended == false)
1607 // TODO: force send when acceleration changes enough?
1608 float minchange = 0.2*BS;
1609 if(m_last_sent_position_timer > 1.0){
1610 minchange = 0.01*BS;
1611 } else if(m_last_sent_position_timer > 0.2){
1612 minchange = 0.05*BS;
1614 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
1615 move_d += m_last_sent_move_precision;
1616 float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
1617 if(move_d > minchange || vel_d > minchange ||
1618 fabs(m_yaw - m_last_sent_yaw) > 1.0){
1619 sendPosition(true, false);
1623 std::string LuaEntitySAO::getClientInitializationData()
1625 std::ostringstream os(std::ios::binary);
1629 writeV3F1000(os, m_base_position);
1631 writeF1000(os, m_yaw);
1633 std::ostringstream prop_os(std::ios::binary);
1634 m_prop->serialize(prop_os);
1635 os<<serializeLongString(prop_os.str());
1640 std::string LuaEntitySAO::getStaticData()
1642 infostream<<__FUNCTION_NAME<<std::endl;
1643 std::ostringstream os(std::ios::binary);
1647 os<<serializeString(m_init_name);
1650 lua_State *L = m_env->getLua();
1651 std::string state = scriptapi_luaentity_get_staticdata(L, m_id);
1652 os<<serializeLongString(state);
1654 os<<serializeLongString(m_init_state);
1659 void LuaEntitySAO::punch(ServerActiveObject *puncher, float time_from_last_punch)
1662 // Delete unknown LuaEntities when punched
1666 lua_State *L = m_env->getLua();
1667 scriptapi_luaentity_punch(L, m_id, puncher, time_from_last_punch);
1670 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
1674 lua_State *L = m_env->getLua();
1675 scriptapi_luaentity_rightclick(L, m_id, clicker);
1678 void LuaEntitySAO::setPos(v3f pos)
1680 m_base_position = pos;
1681 sendPosition(false, true);
1684 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
1686 m_base_position = pos;
1688 sendPosition(true, true);
1691 float LuaEntitySAO::getMinimumSavedMovement()
1696 void LuaEntitySAO::setVelocity(v3f velocity)
1698 m_velocity = velocity;
1701 v3f LuaEntitySAO::getVelocity()
1706 void LuaEntitySAO::setAcceleration(v3f acceleration)
1708 m_acceleration = acceleration;
1711 v3f LuaEntitySAO::getAcceleration()
1713 return m_acceleration;
1716 void LuaEntitySAO::setYaw(float yaw)
1721 float LuaEntitySAO::getYaw()
1726 void LuaEntitySAO::setTextureMod(const std::string &mod)
1728 std::ostringstream os(std::ios::binary);
1729 // command (1 = set texture modification)
1732 os<<serializeString(mod);
1733 // create message and add to list
1734 ActiveObjectMessage aom(getId(), false, os.str());
1735 m_messages_out.push_back(aom);
1738 void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
1739 bool select_horiz_by_yawpitch)
1741 std::ostringstream os(std::ios::binary);
1742 // command (2 = set sprite)
1746 writeU16(os, num_frames);
1747 writeF1000(os, framelength);
1748 writeU8(os, select_horiz_by_yawpitch);
1749 // create message and add to list
1750 ActiveObjectMessage aom(getId(), false, os.str());
1751 m_messages_out.push_back(aom);
1754 std::string LuaEntitySAO::getName()
1759 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
1761 m_last_sent_move_precision = m_base_position.getDistanceFrom(
1762 m_last_sent_position);
1763 m_last_sent_position_timer = 0;
1764 m_last_sent_yaw = m_yaw;
1765 m_last_sent_position = m_base_position;
1766 m_last_sent_velocity = m_velocity;
1767 //m_last_sent_acceleration = m_acceleration;
1769 float update_interval = m_env->getSendRecommendedInterval();
1771 std::ostringstream os(std::ios::binary);
1772 // command (0 = update position)
1776 writeU8(os, do_interpolate);
1778 writeV3F1000(os, m_base_position);
1780 writeV3F1000(os, m_velocity);
1782 writeV3F1000(os, m_acceleration);
1784 writeF1000(os, m_yaw);
1785 // is_end_position (for interpolation)
1786 writeU8(os, is_movement_end);
1787 // update_interval (for interpolation)
1788 writeF1000(os, update_interval);
1790 // create message and add to list
1791 ActiveObjectMessage aom(getId(), false, os.str());
1792 m_messages_out.push_back(aom);