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
27 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
29 /* Some helper functions */
31 // Y is copied, X and Z change is limited
32 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
34 v3f d_wanted = target_speed - speed;
36 f32 dl_wanted = d_wanted.getLength();
41 v3f d = d_wanted.normalize() * dl;
45 speed.Y = target_speed.Y;
53 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
55 TestSAO::TestSAO(ServerEnvironment *env, v3f pos):
56 ServerActiveObject(env, pos),
60 ServerActiveObject::registerType(getType(), create);
63 ServerActiveObject* TestSAO::create(ServerEnvironment *env, v3f pos,
64 const std::string &data)
66 return new TestSAO(env, pos);
69 void TestSAO::step(float dtime, bool send_recommended)
78 m_base_position.Y += dtime * BS * 2;
79 if(m_base_position.Y > 8*BS)
80 m_base_position.Y = 2*BS;
82 if(send_recommended == false)
92 data += itos(0); // 0 = position
94 data += itos(m_base_position.X);
96 data += itos(m_base_position.Y);
98 data += itos(m_base_position.Z);
100 ActiveObjectMessage aom(getId(), false, data);
101 m_messages_out.push_back(aom);
111 ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), "");
113 ItemSAO::ItemSAO(ServerEnvironment *env, v3f pos,
114 const std::string inventorystring):
115 ServerActiveObject(env, pos),
116 m_inventorystring(inventorystring),
118 m_last_sent_position(0,0,0)
120 ServerActiveObject::registerType(getType(), create);
123 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, v3f pos,
124 const std::string &data)
126 std::istringstream is(data, std::ios::binary);
131 // check if version is supported
134 std::string inventorystring = deSerializeString(is);
135 infostream<<"ItemSAO::create(): Creating item \""
136 <<inventorystring<<"\""<<std::endl;
137 return new ItemSAO(env, pos, inventorystring);
140 void ItemSAO::step(float dtime, bool send_recommended)
142 ScopeProfiler sp2(g_profiler, "ItemSAO::step avg", SPT_AVG);
146 const float interval = 0.2;
147 if(m_move_interval.step(dtime, interval)==false)
151 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
152 collisionMoveResult moveresult;
154 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
155 // Maximum movement without glitches
156 f32 pos_max_d = BS*0.25;
158 if(m_speed_f.getLength()*dtime > pos_max_d)
159 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
160 v3f pos_f = getBasePosition();
161 v3f pos_f_old = pos_f;
162 IGameDef *gamedef = m_env->getGameDef();
163 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
164 pos_max_d, box, dtime, pos_f, m_speed_f);
166 if(send_recommended == false)
169 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
171 setBasePosition(pos_f);
172 m_last_sent_position = pos_f;
174 std::ostringstream os(std::ios::binary);
176 // command (0 = update position)
180 writeS32((u8*)buf, m_base_position.X*1000);
182 writeS32((u8*)buf, m_base_position.Y*1000);
184 writeS32((u8*)buf, m_base_position.Z*1000);
186 // create message and add to list
187 ActiveObjectMessage aom(getId(), false, os.str());
188 m_messages_out.push_back(aom);
192 std::string ItemSAO::getClientInitializationData()
194 std::ostringstream os(std::ios::binary);
200 writeS32((u8*)buf, m_base_position.X*1000);
202 writeS32((u8*)buf, m_base_position.Y*1000);
204 writeS32((u8*)buf, m_base_position.Z*1000);
207 os<<serializeString(m_inventorystring);
211 std::string ItemSAO::getStaticData()
213 infostream<<__FUNCTION_NAME<<std::endl;
214 std::ostringstream os(std::ios::binary);
220 os<<serializeString(m_inventorystring);
224 InventoryItem * ItemSAO::createInventoryItem()
227 std::istringstream is(m_inventorystring, std::ios_base::binary);
228 IGameDef *gamedef = m_env->getGameDef();
229 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
230 infostream<<__FUNCTION_NAME<<": m_inventorystring=\""
231 <<m_inventorystring<<"\" -> item="<<item
235 catch(SerializationError &e)
237 infostream<<__FUNCTION_NAME<<": serialization error: "
238 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
243 void ItemSAO::punch(ServerActiveObject *puncher)
245 InventoryItem *item = createInventoryItem();
246 bool fits = puncher->addToInventory(item);
253 void ItemSAO::rightClick(ServerActiveObject *clicker)
255 infostream<<__FUNCTION_NAME<<std::endl;
256 InventoryItem *item = createInventoryItem();
260 bool to_be_deleted = item->use(m_env, clicker);
265 // Reflect changes to the item here
266 m_inventorystring = item->getItemString();
268 delete item; // Delete temporary item
276 RatSAO proto_RatSAO(NULL, v3f(0,0,0));
278 RatSAO::RatSAO(ServerEnvironment *env, v3f pos):
279 ServerActiveObject(env, pos),
283 ServerActiveObject::registerType(getType(), create);
285 m_oldpos = v3f(0,0,0);
286 m_last_sent_position = v3f(0,0,0);
287 m_yaw = myrand_range(0,PI*2);
291 m_touching_ground = false;
294 ServerActiveObject* RatSAO::create(ServerEnvironment *env, v3f pos,
295 const std::string &data)
297 std::istringstream is(data, std::ios::binary);
302 // check if version is supported
305 return new RatSAO(env, pos);
308 void RatSAO::step(float dtime, bool send_recommended)
310 ScopeProfiler sp2(g_profiler, "RatSAO::step avg", SPT_AVG);
314 if(m_is_active == false)
316 if(m_inactive_interval.step(dtime, 0.5)==false)
333 m_speed_f.Y -= dtime*9.81*BS;
336 Move around if some player is close
338 bool player_is_close = false;
339 // Check connected players
340 core::list<Player*> players = m_env->getPlayers(true);
341 core::list<Player*>::Iterator i;
342 for(i = players.begin();
343 i != players.end(); i++)
346 v3f playerpos = player->getPosition();
347 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
349 player_is_close = true;
354 m_is_active = player_is_close;
356 if(player_is_close == false)
364 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
366 m_speed_f.X = speed * dir.X;
367 m_speed_f.Z = speed * dir.Z;
369 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
376 m_speed_f.Y = 5.0*BS;
384 m_counter2 += (float)(myrand()%100)/100*3.0;
385 m_yaw += ((float)(myrand()%200)-100)/100*180;
386 m_yaw = wrapDegrees(m_yaw);
391 m_oldpos = m_base_position;
394 Move it, with collision detection
397 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
398 collisionMoveResult moveresult;
399 // Maximum movement without glitches
400 f32 pos_max_d = BS*0.25;
402 if(m_speed_f.getLength()*dtime > pos_max_d)
403 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
404 v3f pos_f = getBasePosition();
405 v3f pos_f_old = pos_f;
406 IGameDef *gamedef = m_env->getGameDef();
407 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
408 pos_max_d, box, dtime, pos_f, m_speed_f);
409 m_touching_ground = moveresult.touching_ground;
411 setBasePosition(pos_f);
413 if(send_recommended == false)
416 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
418 m_last_sent_position = pos_f;
420 std::ostringstream os(std::ios::binary);
421 // command (0 = update position)
424 writeV3F1000(os, m_base_position);
426 writeF1000(os, m_yaw);
427 // create message and add to list
428 ActiveObjectMessage aom(getId(), false, os.str());
429 m_messages_out.push_back(aom);
433 std::string RatSAO::getClientInitializationData()
435 std::ostringstream os(std::ios::binary);
439 writeV3F1000(os, m_base_position);
443 std::string RatSAO::getStaticData()
445 //infostream<<__FUNCTION_NAME<<std::endl;
446 std::ostringstream os(std::ios::binary);
452 void RatSAO::punch(ServerActiveObject *puncher)
454 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
455 IGameDef *gamedef = m_env->getGameDef();
456 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
457 bool fits = puncher->addToInventory(item);
469 Oerkki1SAO proto_Oerkki1SAO(NULL, v3f(0,0,0));
471 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, v3f pos):
472 ServerActiveObject(env, pos),
476 ServerActiveObject::registerType(getType(), create);
478 m_oldpos = v3f(0,0,0);
479 m_last_sent_position = v3f(0,0,0);
484 m_touching_ground = false;
486 m_after_jump_timer = 0;
489 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, v3f pos,
490 const std::string &data)
492 std::istringstream is(data, std::ios::binary);
494 u8 version = readU8(is);
497 // check if version is supported
500 Oerkki1SAO *o = new Oerkki1SAO(env, pos);
505 void Oerkki1SAO::step(float dtime, bool send_recommended)
507 ScopeProfiler sp2(g_profiler, "Oerkki1SAO::step avg", SPT_AVG);
511 if(m_is_active == false)
513 if(m_inactive_interval.step(dtime, 0.5)==false)
529 m_after_jump_timer -= dtime;
531 v3f old_speed = m_speed_f;
534 m_speed_f.Y -= dtime*9.81*BS;
537 Move around if some player is close
539 bool player_is_close = false;
540 bool player_is_too_close = false;
542 // Check connected players
543 core::list<Player*> players = m_env->getPlayers(true);
544 core::list<Player*>::Iterator i;
545 for(i = players.begin();
546 i != players.end(); i++)
549 v3f playerpos = player->getPosition();
550 f32 dist = m_base_position.getDistanceFrom(playerpos);
555 player_is_too_close = true;
556 near_player_pos = playerpos;
558 else if(dist < BS*15.0 && !player_is_too_close)
560 player_is_close = true;
561 near_player_pos = playerpos;
565 m_is_active = player_is_close;
567 v3f target_speed = m_speed_f;
571 target_speed = v3f(0,0,0);
577 v3f ndir = near_player_pos - m_base_position;
581 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
582 if(nyaw < m_yaw - 180)
584 else if(nyaw > m_yaw + 180)
586 m_yaw = 0.95*m_yaw + 0.05*nyaw;
587 m_yaw = wrapDegrees(m_yaw);
591 if((m_touching_ground || m_after_jump_timer > 0.0)
592 && !player_is_too_close)
594 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
595 target_speed.X = speed * dir.X;
596 target_speed.Z = speed * dir.Z;
599 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
607 target_speed.Y = 5.0*BS;
608 m_after_jump_timer = 1.0;
616 m_counter2 += (float)(myrand()%100)/100*3.0;
617 //m_yaw += ((float)(myrand()%200)-100)/100*180;
618 m_yaw += ((float)(myrand()%200)-100)/100*90;
619 m_yaw = wrapDegrees(m_yaw);
624 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
625 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
627 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
629 m_oldpos = m_base_position;
632 Move it, with collision detection
635 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
636 collisionMoveResult moveresult;
637 // Maximum movement without glitches
638 f32 pos_max_d = BS*0.25;
640 if(m_speed_f.getLength()*dtime > pos_max_d)
641 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
642 v3f pos_f = getBasePosition();
643 v3f pos_f_old = pos_f;
644 IGameDef *gamedef = m_env->getGameDef();
645 moveresult = collisionMovePrecise(&m_env->getMap(), gamedef,
646 pos_max_d, box, dtime, pos_f, m_speed_f);
647 m_touching_ground = moveresult.touching_ground;
649 // Do collision damage
650 float tolerance = BS*30;
651 float factor = BS*0.5;
652 v3f speed_diff = old_speed - m_speed_f;
653 // Increase effect in X and Z
656 float vel = speed_diff.getLength();
659 f32 damage_f = (vel - tolerance)/BS*factor;
660 u16 damage = (u16)(damage_f+0.5);
664 setBasePosition(pos_f);
666 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
669 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
671 m_last_sent_position = pos_f;
673 std::ostringstream os(std::ios::binary);
674 // command (0 = update position)
677 writeV3F1000(os, m_base_position);
679 writeF1000(os, m_yaw);
680 // create message and add to list
681 ActiveObjectMessage aom(getId(), false, os.str());
682 m_messages_out.push_back(aom);
686 std::string Oerkki1SAO::getClientInitializationData()
688 std::ostringstream os(std::ios::binary);
692 writeV3F1000(os, m_base_position);
696 std::string Oerkki1SAO::getStaticData()
698 //infostream<<__FUNCTION_NAME<<std::endl;
699 std::ostringstream os(std::ios::binary);
707 void Oerkki1SAO::punch(ServerActiveObject *puncher)
709 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
711 std::string toolname = "";
712 InventoryItem *item = puncher->getWieldedItem();
713 if(item && (std::string)item->getName() == "ToolItem"){
714 ToolItem *titem = (ToolItem*)item;
715 toolname = titem->getToolName();
718 m_speed_f += dir*12*BS;
721 /* See tool names in inventory.h */
722 if(toolname == "WSword")
724 if(toolname == "STSword")
726 if(toolname == "SteelSword")
728 if(toolname == "STAxe")
730 if(toolname == "SteelAxe")
732 if(toolname == "SteelPick")
736 puncher->damageWieldedItem(65536/100);
739 void Oerkki1SAO::doDamage(u16 d)
741 infostream<<"oerkki damage: "<<d<<std::endl;
755 std::ostringstream os(std::ios::binary);
756 // command (1 = damage)
760 // create message and add to list
761 ActiveObjectMessage aom(getId(), false, os.str());
762 m_messages_out.push_back(aom);
771 FireflySAO proto_FireflySAO(NULL, v3f(0,0,0));
773 FireflySAO::FireflySAO(ServerEnvironment *env, v3f pos):
774 ServerActiveObject(env, pos),
778 ServerActiveObject::registerType(getType(), create);
780 m_oldpos = v3f(0,0,0);
781 m_last_sent_position = v3f(0,0,0);
786 m_touching_ground = false;
789 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, v3f pos,
790 const std::string &data)
792 std::istringstream is(data, std::ios::binary);
797 // check if version is supported
800 return new FireflySAO(env, pos);
803 void FireflySAO::step(float dtime, bool send_recommended)
805 ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG);
809 if(m_is_active == false)
811 if(m_inactive_interval.step(dtime, 0.5)==false)
819 // Apply (less) gravity
820 m_speed_f.Y -= dtime*3*BS;
823 Move around if some player is close
825 bool player_is_close = false;
826 // Check connected players
827 core::list<Player*> players = m_env->getPlayers(true);
828 core::list<Player*>::Iterator i;
829 for(i = players.begin();
830 i != players.end(); i++)
833 v3f playerpos = player->getPosition();
834 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
836 player_is_close = true;
841 m_is_active = player_is_close;
843 if(player_is_close == false)
851 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
853 m_speed_f.X = speed * dir.X;
854 m_speed_f.Z = speed * dir.Z;
856 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
863 m_speed_f.Y = 5.0*BS;
871 m_counter2 += (float)(myrand()%100)/100*3.0;
872 m_yaw += ((float)(myrand()%200)-100)/100*180;
873 m_yaw = wrapDegrees(m_yaw);
878 m_oldpos = m_base_position;
881 Move it, with collision detection
884 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
885 collisionMoveResult moveresult;
886 // Maximum movement without glitches
887 f32 pos_max_d = BS*0.25;
889 if(m_speed_f.getLength()*dtime > pos_max_d)
890 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
891 v3f pos_f = getBasePosition();
892 v3f pos_f_old = pos_f;
893 IGameDef *gamedef = m_env->getGameDef();
894 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
895 pos_max_d, box, dtime, pos_f, m_speed_f);
896 m_touching_ground = moveresult.touching_ground;
898 setBasePosition(pos_f);
900 if(send_recommended == false)
903 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
905 m_last_sent_position = pos_f;
907 std::ostringstream os(std::ios::binary);
908 // command (0 = update position)
911 writeV3F1000(os, m_base_position);
913 writeF1000(os, m_yaw);
914 // create message and add to list
915 ActiveObjectMessage aom(getId(), false, os.str());
916 m_messages_out.push_back(aom);
920 std::string FireflySAO::getClientInitializationData()
922 std::ostringstream os(std::ios::binary);
926 writeV3F1000(os, m_base_position);
930 std::string FireflySAO::getStaticData()
932 //infostream<<__FUNCTION_NAME<<std::endl;
933 std::ostringstream os(std::ios::binary);
939 InventoryItem* FireflySAO::createPickedUpItem()
941 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
942 IGameDef *gamedef = m_env->getGameDef();
943 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
952 MobV2SAO proto_MobV2SAO(NULL, v3f(0,0,0), NULL);
954 MobV2SAO::MobV2SAO(ServerEnvironment *env, v3f pos,
955 Settings *init_properties):
956 ServerActiveObject(env, pos),
957 m_move_type("ground_nodes"),
959 m_last_sent_position(0,0,0),
965 m_touching_ground(false),
967 m_walk_around(false),
968 m_walk_around_timer(0),
969 m_next_pos_exists(false),
970 m_shoot_reload_timer(0),
974 m_disturb_timer(100000),
975 m_random_disturb_timer(0),
978 ServerActiveObject::registerType(getType(), create);
980 m_properties = new Settings();
982 m_properties->update(*init_properties);
984 m_properties->setV3F("pos", pos);
986 setPropertyDefaults();
990 MobV2SAO::~MobV2SAO()
995 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, v3f pos,
996 const std::string &data)
998 std::istringstream is(data, std::ios::binary);
1000 properties.parseConfigLines(is, "MobArgsEnd");
1001 MobV2SAO *o = new MobV2SAO(env, pos, &properties);
1005 std::string MobV2SAO::getStaticData()
1009 std::ostringstream os(std::ios::binary);
1010 m_properties->writeLines(os);
1014 std::string MobV2SAO::getClientInitializationData()
1016 //infostream<<__FUNCTION_NAME<<std::endl;
1020 std::ostringstream os(std::ios::binary);
1025 Settings client_properties;
1027 /*client_properties.set("version", "0");
1028 client_properties.updateValue(*m_properties, "pos");
1029 client_properties.updateValue(*m_properties, "yaw");
1030 client_properties.updateValue(*m_properties, "hp");*/
1032 // Just send everything for simplicity
1033 client_properties.update(*m_properties);
1035 std::ostringstream os2(std::ios::binary);
1036 client_properties.writeLines(os2);
1037 compressZlib(os2.str(), os);
1042 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
1044 for(int dx=0; dx<size.X; dx++)
1045 for(int dy=0; dy<size.Y; dy++)
1046 for(int dz=0; dz<size.Z; dz++){
1047 v3s16 dp(dx, dy, dz);
1049 MapNode n = map->getNodeNoEx(p);
1050 if(n.getContent() != CONTENT_AIR)
1056 bool checkWalkablePosition(Map *map, v3s16 p0)
1058 v3s16 p = p0 + v3s16(0,-1,0);
1059 MapNode n = map->getNodeNoEx(p);
1060 if(n.getContent() != CONTENT_AIR)
1065 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1067 if(!checkFreePosition(map, p0, size))
1069 if(!checkWalkablePosition(map, p0))
1074 static void get_random_u32_array(u32 a[], u32 len)
1077 for(i=0; i<len; i++)
1081 u32 k = myrand() % n;
1089 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1091 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1093 core::map<v3s16, MapBlock*> modified_blocks;
1095 for(int dx=0; dx<size.X; dx++)
1096 for(int dy=0; dy<size.Y; dy++)
1097 for(int dz=0; dz<size.Z; dz++){
1098 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1100 MapNode n = map->getNodeNoEx(p);
1101 if(n.getContent() == CONTENT_IGNORE)
1103 //map->removeNodeWithEvent(p);
1104 map->removeNodeAndUpdate(p, modified_blocks);
1107 // Send a MEET_OTHER event
1109 event.type = MEET_OTHER;
1110 for(core::map<v3s16, MapBlock*>::Iterator
1111 i = modified_blocks.getIterator();
1112 i.atEnd() == false; i++)
1114 v3s16 p = i.getNode()->getKey();
1115 event.modified_blocks.insert(p, true);
1117 map->dispatchEvent(&event);
1120 void MobV2SAO::step(float dtime, bool send_recommended)
1122 ScopeProfiler sp2(g_profiler, "MobV2SAO::step avg", SPT_AVG);
1125 Map *map = &m_env->getMap();
1129 if(m_die_age >= 0.0 && m_age >= m_die_age){
1134 m_random_disturb_timer += dtime;
1135 if(m_random_disturb_timer >= 5.0)
1137 m_random_disturb_timer = 0;
1138 // Check connected players
1139 core::list<Player*> players = m_env->getPlayers(true);
1140 core::list<Player*>::Iterator i;
1141 for(i = players.begin();
1142 i != players.end(); i++)
1144 Player *player = *i;
1145 v3f playerpos = player->getPosition();
1146 f32 dist = m_base_position.getDistanceFrom(playerpos);
1149 if(myrand_range(0,3) == 0){
1150 actionstream<<"Mob id="<<m_id<<" at "
1151 <<PP(m_base_position/BS)
1152 <<" got randomly disturbed by "
1153 <<player->getName()<<std::endl;
1154 m_disturbing_player = player->getName();
1155 m_disturb_timer = 0;
1162 Player *disturbing_player =
1163 m_env->getPlayer(m_disturbing_player.c_str());
1164 v3f disturbing_player_off = v3f(0,1,0);
1165 v3f disturbing_player_norm = v3f(0,1,0);
1166 float disturbing_player_distance = 1000000;
1167 float disturbing_player_dir = 0;
1168 if(disturbing_player){
1169 disturbing_player_off =
1170 disturbing_player->getPosition() - m_base_position;
1171 disturbing_player_distance = disturbing_player_off.getLength();
1172 disturbing_player_norm = disturbing_player_off;
1173 disturbing_player_norm.normalize();
1174 disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z,
1175 disturbing_player_norm.X);
1178 m_disturb_timer += dtime;
1182 m_shooting_timer -= dtime;
1183 if(m_shooting_timer <= 0.0 && m_shooting){
1186 std::string shoot_type = m_properties->get("shoot_type");
1187 v3f shoot_pos(0,0,0);
1188 shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1189 if(shoot_type == "fireball"){
1190 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1193 v3f speed = dir * BS * 10.0;
1194 v3f pos = m_base_position + shoot_pos;
1195 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1196 <<" shooting fireball from "<<PP(pos)
1197 <<" at speed "<<PP(speed)<<std::endl;
1198 Settings properties;
1199 properties.set("looks", "fireball");
1200 properties.setV3F("speed", speed);
1201 properties.setFloat("die_age", 5.0);
1202 properties.set("move_type", "constant_speed");
1203 properties.setFloat("hp", 1000);
1204 properties.set("lock_full_brightness", "true");
1205 properties.set("player_hit_damage", "9");
1206 properties.set("player_hit_distance", "2");
1207 properties.set("player_hit_interval", "1");
1208 ServerActiveObject *obj = new MobV2SAO(m_env,
1210 //m_env->addActiveObjectAsStatic(obj);
1211 m_env->addActiveObject(obj);
1213 infostream<<__FUNCTION_NAME<<": Mob id="<<m_id
1214 <<": Unknown shoot_type="<<shoot_type
1219 m_shoot_reload_timer += dtime;
1221 float reload_time = 15.0;
1222 if(m_disturb_timer <= 15.0)
1225 bool shoot_without_player = false;
1226 if(m_properties->getBool("mindless_rage"))
1227 shoot_without_player = true;
1229 if(!m_shooting && m_shoot_reload_timer >= reload_time &&
1230 !m_next_pos_exists &&
1231 (m_disturb_timer <= 60.0 || shoot_without_player))
1234 if(m_disturb_timer < 60.0 && disturbing_player &&
1235 disturbing_player_distance < 16*BS &&
1236 fabs(disturbing_player_norm.Y) < 0.8){
1237 m_yaw = disturbing_player_dir;
1239 m_shoot_y += disturbing_player_norm.Y;
1241 m_shoot_y = 0.01 * myrand_range(-30,10);
1243 m_shoot_reload_timer = 0.0;
1245 m_shooting_timer = 1.5;
1247 std::ostringstream os(std::ios::binary);
1248 // command (2 = shooting)
1251 writeF1000(os, m_shooting_timer + 0.1);
1254 // create message and add to list
1255 ActiveObjectMessage aom(getId(), false, os.str());
1256 m_messages_out.push_back(aom);
1261 if(m_move_type == "ground_nodes")
1264 m_walk_around_timer -= dtime;
1265 if(m_walk_around_timer <= 0.0){
1266 m_walk_around = !m_walk_around;
1268 m_walk_around_timer = 0.1*myrand_range(10,50);
1270 m_walk_around_timer = 0.1*myrand_range(30,70);
1275 if(m_next_pos_exists){
1276 v3f pos_f = m_base_position;
1277 v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1279 v3f v = next_pos_f - pos_f;
1280 m_yaw = atan2(v.Z, v.X) / PI * 180;
1282 v3f diff = next_pos_f - pos_f;
1285 float speed = BS * 0.5;
1288 dir *= dtime * speed;
1289 bool arrived = false;
1290 if(dir.getLength() > diff.getLength()){
1295 m_base_position = pos_f;
1297 if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1298 m_next_pos_exists = false;
1302 v3s16 pos_i = floatToInt(m_base_position, BS);
1303 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1304 v3s16 pos_size_off(0,0,0);
1305 if(m_size.X >= 2.5){
1306 pos_size_off.X = -1;
1307 pos_size_off.Y = -1;
1310 if(!m_next_pos_exists){
1311 /* Check whether to drop down */
1312 if(checkFreePosition(map,
1313 pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1314 m_next_pos_i = pos_i + v3s16(0,-1,0);
1315 m_next_pos_exists = true;
1324 if(!m_next_pos_exists){
1325 /* Find some position where to go next */
1328 for(int dx=-1; dx<=1; dx++)
1329 for(int dy=-1; dy<=1; dy++)
1330 for(int dz=-1; dz<=1; dz++){
1331 if(dx == 0 && dy == 0)
1333 if(dx != 0 && dz != 0 && dy != 0)
1335 dps[num_dps++] = v3s16(dx,dy,dz);
1338 get_random_u32_array(order, num_dps);
1339 for(int i=0; i<num_dps; i++){
1340 v3s16 p = dps[order[i]] + pos_i;
1341 bool is_free = checkFreeAndWalkablePosition(map,
1342 p + pos_size_off, size_blocks);
1346 m_next_pos_exists = true;
1352 else if(m_move_type == "constant_speed")
1354 m_base_position += m_speed * dtime;
1356 v3s16 pos_i = floatToInt(m_base_position, BS);
1357 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1358 v3s16 pos_size_off(0,0,0);
1359 if(m_size.X >= 2.5){
1360 pos_size_off.X = -1;
1361 pos_size_off.Y = -1;
1363 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1365 explodeSquare(map, pos_i, v3s16(3,3,3));
1372 errorstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1373 <<m_move_type<<"\""<<std::endl;
1376 if(send_recommended == false)
1379 if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1385 void MobV2SAO::punch(ServerActiveObject *puncher)
1387 v3f dir = (getBasePosition() - puncher->getBasePosition()).normalize();
1389 std::string toolname = "";
1390 InventoryItem *item = puncher->getWieldedItem();
1391 if(item && (std::string)item->getName() == "ToolItem"){
1392 ToolItem *titem = (ToolItem*)item;
1393 toolname = titem->getToolName();
1396 // A quick hack; SAO description is player name for player
1397 std::string playername = puncher->getDescription();
1399 Map *map = &m_env->getMap();
1401 actionstream<<playername<<" punches mob id="<<m_id
1402 <<" with a \""<<toolname<<"\" at "
1403 <<PP(m_base_position/BS)<<std::endl;
1405 m_disturb_timer = 0;
1406 m_disturbing_player = playername;
1407 m_next_pos_exists = false; // Cancel moving immediately
1409 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1410 v3f new_base_position = m_base_position + dir * BS;
1412 v3s16 pos_i = floatToInt(new_base_position, BS);
1413 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1414 v3s16 pos_size_off(0,0,0);
1415 if(m_size.X >= 2.5){
1416 pos_size_off.X = -1;
1417 pos_size_off.Y = -1;
1419 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1421 m_base_position = new_base_position;
1426 /* See tool names in inventory.h */
1427 if(toolname == "WSword")
1429 if(toolname == "STSword")
1431 if(toolname == "SteelSword")
1433 if(toolname == "STAxe")
1435 if(toolname == "SteelAxe")
1437 if(toolname == "SteelPick")
1441 puncher->damageWieldedItem(65536/100);
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),
1554 m_last_sent_position(0,0,0),
1555 m_last_sent_position_timer(0),
1556 m_last_sent_move_precision(0)
1558 // Only register type if no environment supplied
1560 ServerActiveObject::registerType(getType(), create);
1565 LuaEntitySAO::~LuaEntitySAO()
1568 lua_State *L = m_env->getLua();
1569 scriptapi_luaentity_rm(L, m_id);
1574 void LuaEntitySAO::addedToEnvironment()
1576 ServerActiveObject::addedToEnvironment();
1578 // Create entity from name and state
1579 lua_State *L = m_env->getLua();
1580 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str(),
1581 m_init_state.c_str());
1585 scriptapi_luaentity_get_properties(L, m_id, m_prop);
1589 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
1590 const std::string &data)
1592 std::istringstream is(data, std::ios::binary);
1594 u8 version = readU8(is);
1595 // check if version is supported
1599 std::string name = deSerializeString(is);
1601 std::string state = deSerializeLongString(is);
1603 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
1604 <<state<<"\")"<<std::endl;
1605 return new LuaEntitySAO(env, pos, name, state);
1608 void LuaEntitySAO::step(float dtime, bool send_recommended)
1610 m_last_sent_position_timer += dtime;
1613 lua_State *L = m_env->getLua();
1614 scriptapi_luaentity_step(L, m_id, dtime);
1617 if(send_recommended == false)
1620 float minchange = 0.2*BS;
1621 if(m_last_sent_position_timer > 1.0){
1622 minchange = 0.01*BS;
1623 } else if(m_last_sent_position_timer > 0.2){
1624 minchange = 0.05*BS;
1626 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
1627 move_d += m_last_sent_move_precision;
1628 if(move_d > minchange || fabs(m_yaw - m_last_sent_yaw) > 1.0){
1629 sendPosition(true, false);
1633 std::string LuaEntitySAO::getClientInitializationData()
1635 std::ostringstream os(std::ios::binary);
1639 writeV3F1000(os, m_base_position);
1641 writeF1000(os, m_yaw);
1643 std::ostringstream prop_os(std::ios::binary);
1644 m_prop->serialize(prop_os);
1645 os<<serializeLongString(prop_os.str());
1650 std::string LuaEntitySAO::getStaticData()
1652 infostream<<__FUNCTION_NAME<<std::endl;
1653 std::ostringstream os(std::ios::binary);
1657 os<<serializeString(m_init_name);
1660 lua_State *L = m_env->getLua();
1661 std::string state = scriptapi_luaentity_get_state(L, m_id);
1662 os<<serializeLongString(state);
1664 os<<serializeLongString(m_init_state);
1669 InventoryItem* LuaEntitySAO::createPickedUpItem()
1671 std::istringstream is("CraftItem testobject1 1", std::ios_base::binary);
1672 IGameDef *gamedef = m_env->getGameDef();
1673 InventoryItem *item = InventoryItem::deSerialize(is, gamedef);
1677 void LuaEntitySAO::punch(ServerActiveObject *puncher)
1681 lua_State *L = m_env->getLua();
1682 scriptapi_luaentity_punch(L, m_id, puncher);
1685 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
1689 lua_State *L = m_env->getLua();
1690 scriptapi_luaentity_rightclick(L, m_id, clicker);
1693 void LuaEntitySAO::setPos(v3f pos)
1695 m_base_position = pos;
1696 sendPosition(false, true);
1699 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
1701 m_base_position = pos;
1703 sendPosition(true, true);
1706 float LuaEntitySAO::getMinimumSavedMovement()
1711 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
1713 m_last_sent_move_precision = m_base_position.getDistanceFrom(
1714 m_last_sent_position);
1715 m_last_sent_position_timer = 0;
1716 m_last_sent_yaw = m_yaw;
1717 m_last_sent_position = m_base_position;
1719 float update_interval = m_env->getSendRecommendedInterval();
1721 std::ostringstream os(std::ios::binary);
1722 // command (0 = update position)
1726 writeU8(os, do_interpolate);
1728 writeV3F1000(os, m_base_position);
1730 writeF1000(os, m_yaw);
1731 // is_end_position (for interpolation)
1732 writeU8(os, is_movement_end);
1733 // update_interval (for interpolation)
1734 writeF1000(os, update_interval);
1736 // create message and add to list
1737 ActiveObjectMessage aom(getId(), false, os.str());
1738 m_messages_out.push_back(aom);