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"
26 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
28 /* Some helper functions */
30 // Y is copied, X and Z change is limited
31 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
33 v3f d_wanted = target_speed - speed;
35 f32 dl_wanted = d_wanted.getLength();
40 v3f d = d_wanted.normalize() * dl;
44 speed.Y = target_speed.Y;
52 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
54 TestSAO::TestSAO(ServerEnvironment *env, v3f pos):
55 ServerActiveObject(env, pos),
59 ServerActiveObject::registerType(getType(), create);
62 ServerActiveObject* TestSAO::create(ServerEnvironment *env, v3f pos,
63 const std::string &data)
65 return new TestSAO(env, pos);
68 void TestSAO::step(float dtime, bool send_recommended)
77 m_base_position.Y += dtime * BS * 2;
78 if(m_base_position.Y > 8*BS)
79 m_base_position.Y = 2*BS;
81 if(send_recommended == false)
91 data += itos(0); // 0 = position
93 data += itos(m_base_position.X);
95 data += itos(m_base_position.Y);
97 data += itos(m_base_position.Z);
99 ActiveObjectMessage aom(getId(), false, data);
100 m_messages_out.push_back(aom);
110 ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), "");
112 ItemSAO::ItemSAO(ServerEnvironment *env, v3f pos,
113 const std::string inventorystring):
114 ServerActiveObject(env, pos),
115 m_inventorystring(inventorystring),
117 m_last_sent_position(0,0,0)
119 ServerActiveObject::registerType(getType(), create);
122 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, v3f pos,
123 const std::string &data)
125 std::istringstream is(data, std::ios::binary);
130 // check if version is supported
133 std::string inventorystring = deSerializeString(is);
134 infostream<<"ItemSAO::create(): Creating item \""
135 <<inventorystring<<"\""<<std::endl;
136 return new ItemSAO(env, pos, inventorystring);
139 void ItemSAO::step(float dtime, bool send_recommended)
141 ScopeProfiler sp2(g_profiler, "ItemSAO::step avg", SPT_AVG);
145 const float interval = 0.2;
146 if(m_move_interval.step(dtime, interval)==false)
150 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
151 collisionMoveResult moveresult;
153 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
154 // Maximum movement without glitches
155 f32 pos_max_d = BS*0.25;
157 if(m_speed_f.getLength()*dtime > pos_max_d)
158 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
159 v3f pos_f = getBasePosition();
160 v3f pos_f_old = pos_f;
161 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
162 box, dtime, pos_f, m_speed_f);
164 if(send_recommended == false)
167 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
169 setBasePosition(pos_f);
170 m_last_sent_position = pos_f;
172 std::ostringstream os(std::ios::binary);
174 // command (0 = update position)
178 writeS32((u8*)buf, m_base_position.X*1000);
180 writeS32((u8*)buf, m_base_position.Y*1000);
182 writeS32((u8*)buf, m_base_position.Z*1000);
184 // create message and add to list
185 ActiveObjectMessage aom(getId(), false, os.str());
186 m_messages_out.push_back(aom);
190 std::string ItemSAO::getClientInitializationData()
192 std::ostringstream os(std::ios::binary);
198 writeS32((u8*)buf, m_base_position.X*1000);
200 writeS32((u8*)buf, m_base_position.Y*1000);
202 writeS32((u8*)buf, m_base_position.Z*1000);
205 os<<serializeString(m_inventorystring);
209 std::string ItemSAO::getStaticData()
211 infostream<<__FUNCTION_NAME<<std::endl;
212 std::ostringstream os(std::ios::binary);
218 os<<serializeString(m_inventorystring);
222 InventoryItem * ItemSAO::createInventoryItem()
225 std::istringstream is(m_inventorystring, std::ios_base::binary);
226 InventoryItem *item = InventoryItem::deSerialize(is);
227 infostream<<__FUNCTION_NAME<<": m_inventorystring=\""
228 <<m_inventorystring<<"\" -> item="<<item
232 catch(SerializationError &e)
234 infostream<<__FUNCTION_NAME<<": serialization error: "
235 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
240 void ItemSAO::rightClick(Player *player)
242 infostream<<__FUNCTION_NAME<<std::endl;
243 InventoryItem *item = createInventoryItem();
247 bool to_be_deleted = item->use(m_env, player);
252 // Reflect changes to the item here
253 m_inventorystring = item->getItemString();
263 RatSAO proto_RatSAO(NULL, v3f(0,0,0));
265 RatSAO::RatSAO(ServerEnvironment *env, v3f pos):
266 ServerActiveObject(env, pos),
270 ServerActiveObject::registerType(getType(), create);
272 m_oldpos = v3f(0,0,0);
273 m_last_sent_position = v3f(0,0,0);
274 m_yaw = myrand_range(0,PI*2);
278 m_touching_ground = false;
281 ServerActiveObject* RatSAO::create(ServerEnvironment *env, v3f pos,
282 const std::string &data)
284 std::istringstream is(data, std::ios::binary);
289 // check if version is supported
292 return new RatSAO(env, pos);
295 void RatSAO::step(float dtime, bool send_recommended)
297 ScopeProfiler sp2(g_profiler, "RatSAO::step avg", SPT_AVG);
301 if(m_is_active == false)
303 if(m_inactive_interval.step(dtime, 0.5)==false)
320 m_speed_f.Y -= dtime*9.81*BS;
323 Move around if some player is close
325 bool player_is_close = false;
326 // Check connected players
327 core::list<Player*> players = m_env->getPlayers(true);
328 core::list<Player*>::Iterator i;
329 for(i = players.begin();
330 i != players.end(); i++)
333 v3f playerpos = player->getPosition();
334 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
336 player_is_close = true;
341 m_is_active = player_is_close;
343 if(player_is_close == false)
351 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
353 m_speed_f.X = speed * dir.X;
354 m_speed_f.Z = speed * dir.Z;
356 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
363 m_speed_f.Y = 5.0*BS;
371 m_counter2 += (float)(myrand()%100)/100*3.0;
372 m_yaw += ((float)(myrand()%200)-100)/100*180;
373 m_yaw = wrapDegrees(m_yaw);
378 m_oldpos = m_base_position;
381 Move it, with collision detection
384 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
385 collisionMoveResult moveresult;
386 // Maximum movement without glitches
387 f32 pos_max_d = BS*0.25;
389 if(m_speed_f.getLength()*dtime > pos_max_d)
390 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
391 v3f pos_f = getBasePosition();
392 v3f pos_f_old = pos_f;
393 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
394 box, dtime, pos_f, m_speed_f);
395 m_touching_ground = moveresult.touching_ground;
397 setBasePosition(pos_f);
399 if(send_recommended == false)
402 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
404 m_last_sent_position = pos_f;
406 std::ostringstream os(std::ios::binary);
407 // command (0 = update position)
410 writeV3F1000(os, m_base_position);
412 writeF1000(os, m_yaw);
413 // create message and add to list
414 ActiveObjectMessage aom(getId(), false, os.str());
415 m_messages_out.push_back(aom);
419 std::string RatSAO::getClientInitializationData()
421 std::ostringstream os(std::ios::binary);
425 writeV3F1000(os, m_base_position);
429 std::string RatSAO::getStaticData()
431 //infostream<<__FUNCTION_NAME<<std::endl;
432 std::ostringstream os(std::ios::binary);
438 InventoryItem* RatSAO::createPickedUpItem()
440 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
441 InventoryItem *item = InventoryItem::deSerialize(is);
450 Oerkki1SAO proto_Oerkki1SAO(NULL, v3f(0,0,0));
452 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, v3f pos):
453 ServerActiveObject(env, pos),
457 ServerActiveObject::registerType(getType(), create);
459 m_oldpos = v3f(0,0,0);
460 m_last_sent_position = v3f(0,0,0);
465 m_touching_ground = false;
467 m_after_jump_timer = 0;
470 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, v3f pos,
471 const std::string &data)
473 std::istringstream is(data, std::ios::binary);
475 u8 version = readU8(is);
478 // check if version is supported
481 Oerkki1SAO *o = new Oerkki1SAO(env, pos);
486 void Oerkki1SAO::step(float dtime, bool send_recommended)
488 ScopeProfiler sp2(g_profiler, "Oerkki1SAO::step avg", SPT_AVG);
492 if(m_is_active == false)
494 if(m_inactive_interval.step(dtime, 0.5)==false)
510 m_after_jump_timer -= dtime;
512 v3f old_speed = m_speed_f;
515 m_speed_f.Y -= dtime*9.81*BS;
518 Move around if some player is close
520 bool player_is_close = false;
521 bool player_is_too_close = false;
523 // Check connected players
524 core::list<Player*> players = m_env->getPlayers(true);
525 core::list<Player*>::Iterator i;
526 for(i = players.begin();
527 i != players.end(); i++)
530 v3f playerpos = player->getPosition();
531 f32 dist = m_base_position.getDistanceFrom(playerpos);
536 player_is_too_close = true;
537 near_player_pos = playerpos;
539 else if(dist < BS*15.0 && !player_is_too_close)
541 player_is_close = true;
542 near_player_pos = playerpos;
546 m_is_active = player_is_close;
548 v3f target_speed = m_speed_f;
552 target_speed = v3f(0,0,0);
558 v3f ndir = near_player_pos - m_base_position;
562 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
563 if(nyaw < m_yaw - 180)
565 else if(nyaw > m_yaw + 180)
567 m_yaw = 0.95*m_yaw + 0.05*nyaw;
568 m_yaw = wrapDegrees(m_yaw);
572 if((m_touching_ground || m_after_jump_timer > 0.0)
573 && !player_is_too_close)
575 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
576 target_speed.X = speed * dir.X;
577 target_speed.Z = speed * dir.Z;
580 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
588 target_speed.Y = 5.0*BS;
589 m_after_jump_timer = 1.0;
597 m_counter2 += (float)(myrand()%100)/100*3.0;
598 //m_yaw += ((float)(myrand()%200)-100)/100*180;
599 m_yaw += ((float)(myrand()%200)-100)/100*90;
600 m_yaw = wrapDegrees(m_yaw);
605 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
606 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
608 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
610 m_oldpos = m_base_position;
613 Move it, with collision detection
616 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
617 collisionMoveResult moveresult;
618 // Maximum movement without glitches
619 f32 pos_max_d = BS*0.25;
621 if(m_speed_f.getLength()*dtime > pos_max_d)
622 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
623 v3f pos_f = getBasePosition();
624 v3f pos_f_old = pos_f;
625 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
626 box, dtime, pos_f, m_speed_f);
627 m_touching_ground = moveresult.touching_ground;
629 // Do collision damage
630 float tolerance = BS*30;
631 float factor = BS*0.5;
632 v3f speed_diff = old_speed - m_speed_f;
633 // Increase effect in X and Z
636 float vel = speed_diff.getLength();
639 f32 damage_f = (vel - tolerance)/BS*factor;
640 u16 damage = (u16)(damage_f+0.5);
644 setBasePosition(pos_f);
646 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
649 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
651 m_last_sent_position = pos_f;
653 std::ostringstream os(std::ios::binary);
654 // command (0 = update position)
657 writeV3F1000(os, m_base_position);
659 writeF1000(os, m_yaw);
660 // create message and add to list
661 ActiveObjectMessage aom(getId(), false, os.str());
662 m_messages_out.push_back(aom);
666 std::string Oerkki1SAO::getClientInitializationData()
668 std::ostringstream os(std::ios::binary);
672 writeV3F1000(os, m_base_position);
676 std::string Oerkki1SAO::getStaticData()
678 //infostream<<__FUNCTION_NAME<<std::endl;
679 std::ostringstream os(std::ios::binary);
687 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir,
688 const std::string &playername)
690 m_speed_f += dir*12*BS;
693 /* See tool names in inventory.h */
694 if(toolname == "WSword")
696 if(toolname == "STSword")
698 if(toolname == "SteelSword")
700 if(toolname == "STAxe")
702 if(toolname == "SteelAxe")
704 if(toolname == "SteelPick")
710 void Oerkki1SAO::doDamage(u16 d)
712 infostream<<"oerkki damage: "<<d<<std::endl;
726 std::ostringstream os(std::ios::binary);
727 // command (1 = damage)
731 // create message and add to list
732 ActiveObjectMessage aom(getId(), false, os.str());
733 m_messages_out.push_back(aom);
742 FireflySAO proto_FireflySAO(NULL, v3f(0,0,0));
744 FireflySAO::FireflySAO(ServerEnvironment *env, v3f pos):
745 ServerActiveObject(env, pos),
749 ServerActiveObject::registerType(getType(), create);
751 m_oldpos = v3f(0,0,0);
752 m_last_sent_position = v3f(0,0,0);
757 m_touching_ground = false;
760 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, v3f pos,
761 const std::string &data)
763 std::istringstream is(data, std::ios::binary);
768 // check if version is supported
771 return new FireflySAO(env, pos);
774 void FireflySAO::step(float dtime, bool send_recommended)
776 ScopeProfiler sp2(g_profiler, "FireflySAO::step avg", SPT_AVG);
780 if(m_is_active == false)
782 if(m_inactive_interval.step(dtime, 0.5)==false)
790 // Apply (less) gravity
791 m_speed_f.Y -= dtime*3*BS;
794 Move around if some player is close
796 bool player_is_close = false;
797 // Check connected players
798 core::list<Player*> players = m_env->getPlayers(true);
799 core::list<Player*>::Iterator i;
800 for(i = players.begin();
801 i != players.end(); i++)
804 v3f playerpos = player->getPosition();
805 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
807 player_is_close = true;
812 m_is_active = player_is_close;
814 if(player_is_close == false)
822 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
824 m_speed_f.X = speed * dir.X;
825 m_speed_f.Z = speed * dir.Z;
827 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
834 m_speed_f.Y = 5.0*BS;
842 m_counter2 += (float)(myrand()%100)/100*3.0;
843 m_yaw += ((float)(myrand()%200)-100)/100*180;
844 m_yaw = wrapDegrees(m_yaw);
849 m_oldpos = m_base_position;
852 Move it, with collision detection
855 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
856 collisionMoveResult moveresult;
857 // Maximum movement without glitches
858 f32 pos_max_d = BS*0.25;
860 if(m_speed_f.getLength()*dtime > pos_max_d)
861 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
862 v3f pos_f = getBasePosition();
863 v3f pos_f_old = pos_f;
864 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
865 box, dtime, pos_f, m_speed_f);
866 m_touching_ground = moveresult.touching_ground;
868 setBasePosition(pos_f);
870 if(send_recommended == false)
873 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
875 m_last_sent_position = pos_f;
877 std::ostringstream os(std::ios::binary);
878 // command (0 = update position)
881 writeV3F1000(os, m_base_position);
883 writeF1000(os, m_yaw);
884 // create message and add to list
885 ActiveObjectMessage aom(getId(), false, os.str());
886 m_messages_out.push_back(aom);
890 std::string FireflySAO::getClientInitializationData()
892 std::ostringstream os(std::ios::binary);
896 writeV3F1000(os, m_base_position);
900 std::string FireflySAO::getStaticData()
902 //infostream<<__FUNCTION_NAME<<std::endl;
903 std::ostringstream os(std::ios::binary);
909 InventoryItem* FireflySAO::createPickedUpItem()
911 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
912 InventoryItem *item = InventoryItem::deSerialize(is);
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 u16 MobV2SAO::punch(const std::string &toolname, v3f dir,
1355 const std::string &playername)
1358 Map *map = &m_env->getMap();
1360 actionstream<<playername<<" punches mob id="<<m_id
1361 <<" with a \""<<toolname<<"\" at "
1362 <<PP(m_base_position/BS)<<std::endl;
1364 m_disturb_timer = 0;
1365 m_disturbing_player = playername;
1366 m_next_pos_exists = false; // Cancel moving immediately
1368 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1369 v3f new_base_position = m_base_position + dir * BS;
1371 v3s16 pos_i = floatToInt(new_base_position, BS);
1372 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1373 v3s16 pos_size_off(0,0,0);
1374 if(m_size.X >= 2.5){
1375 pos_size_off.X = -1;
1376 pos_size_off.Y = -1;
1378 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1380 m_base_position = new_base_position;
1385 /* See tool names in inventory.h */
1386 if(toolname == "WSword")
1388 if(toolname == "STSword")
1390 if(toolname == "SteelSword")
1392 if(toolname == "STAxe")
1394 if(toolname == "SteelAxe")
1396 if(toolname == "SteelPick")
1402 bool MobV2SAO::isPeaceful()
1404 return m_properties->getBool("is_peaceful");
1407 void MobV2SAO::sendPosition()
1409 m_last_sent_position = m_base_position;
1411 std::ostringstream os(std::ios::binary);
1412 // command (0 = update position)
1415 writeV3F1000(os, m_base_position);
1417 writeF1000(os, m_yaw);
1418 // create message and add to list
1419 ActiveObjectMessage aom(getId(), false, os.str());
1420 m_messages_out.push_back(aom);
1423 void MobV2SAO::setPropertyDefaults()
1425 m_properties->setDefault("is_peaceful", "false");
1426 m_properties->setDefault("move_type", "ground_nodes");
1427 m_properties->setDefault("speed", "(0,0,0)");
1428 m_properties->setDefault("age", "0");
1429 m_properties->setDefault("yaw", "0");
1430 m_properties->setDefault("pos", "(0,0,0)");
1431 m_properties->setDefault("hp", "0");
1432 m_properties->setDefault("die_age", "-1");
1433 m_properties->setDefault("size", "(1,2)");
1434 m_properties->setDefault("shoot_type", "fireball");
1435 m_properties->setDefault("shoot_y", "0");
1436 m_properties->setDefault("mindless_rage", "false");
1438 void MobV2SAO::readProperties()
1440 m_move_type = m_properties->get("move_type");
1441 m_speed = m_properties->getV3F("speed");
1442 m_age = m_properties->getFloat("age");
1443 m_yaw = m_properties->getFloat("yaw");
1444 m_base_position = m_properties->getV3F("pos");
1445 m_hp = m_properties->getS32("hp");
1446 m_die_age = m_properties->getFloat("die_age");
1447 m_size = m_properties->getV2F("size");
1449 void MobV2SAO::updateProperties()
1451 m_properties->set("move_type", m_move_type);
1452 m_properties->setV3F("speed", m_speed);
1453 m_properties->setFloat("age", m_age);
1454 m_properties->setFloat("yaw", m_yaw);
1455 m_properties->setV3F("pos", m_base_position);
1456 m_properties->setS32("hp", m_hp);
1457 m_properties->setFloat("die_age", m_die_age);
1458 m_properties->setV2F("size", m_size);
1460 m_properties->setS32("version", 0);
1463 void MobV2SAO::doDamage(u16 d)
1465 infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1473 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1474 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1481 std::ostringstream os(std::ios::binary);
1482 // command (1 = damage)
1486 // create message and add to list
1487 ActiveObjectMessage aom(getId(), false, os.str());
1488 m_messages_out.push_back(aom);
1497 #include "scriptapi.h"
1500 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
1502 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
1503 const std::string &name, const std::string &state):
1504 ServerActiveObject(env, pos),
1506 m_init_state(state),
1510 ServerActiveObject::registerType(getType(), create);
1515 LuaEntitySAO::~LuaEntitySAO()
1518 lua_State *L = m_env->getLua();
1519 scriptapi_luaentity_rm(L, m_id);
1523 void LuaEntitySAO::addedToEnvironment(u16 id)
1525 ServerActiveObject::addedToEnvironment(id);
1527 // Create entity by name and state
1528 m_registered = true;
1529 lua_State *L = m_env->getLua();
1530 scriptapi_luaentity_add(L, id, m_init_name.c_str(), m_init_state.c_str());
1533 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
1534 const std::string &data)
1536 std::istringstream is(data, std::ios::binary);
1538 u8 version = readU8(is);
1539 // check if version is supported
1543 std::string name = deSerializeString(is);
1545 std::string state = deSerializeLongString(is);
1547 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
1548 <<state<<"\")"<<std::endl;
1549 return new LuaEntitySAO(env, pos, name, state);
1552 void LuaEntitySAO::step(float dtime, bool send_recommended)
1555 lua_State *L = m_env->getLua();
1556 scriptapi_luaentity_step(L, m_id, dtime);
1560 std::string LuaEntitySAO::getClientInitializationData()
1562 std::ostringstream os(std::ios::binary);
1566 writeV3F1000(os, m_base_position);
1570 std::string LuaEntitySAO::getStaticData()
1572 infostream<<__FUNCTION_NAME<<std::endl;
1573 std::ostringstream os(std::ios::binary);
1577 os<<serializeString(m_init_name);
1580 lua_State *L = m_env->getLua();
1581 std::string state = scriptapi_luaentity_get_state(L, m_id);
1582 os<<serializeLongString(state);
1584 os<<serializeLongString(m_init_state);
1589 InventoryItem* LuaEntitySAO::createPickedUpItem()
1591 std::istringstream is("CraftItem testobject1 1", std::ios_base::binary);
1592 InventoryItem *item = InventoryItem::deSerialize(is);
1596 u16 LuaEntitySAO::punch(const std::string &toolname, v3f dir,
1597 const std::string &playername)
1602 void LuaEntitySAO::rightClick(Player *player)
1606 lua_State *L = m_env->getLua();
1607 scriptapi_luaentity_rightclick_player(L, m_id, player->getName());