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"
25 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
27 /* Some helper functions */
29 // Y is copied, X and Z change is limited
30 void accelerate_xz(v3f &speed, v3f target_speed, f32 max_increase)
32 v3f d_wanted = target_speed - speed;
34 f32 dl_wanted = d_wanted.getLength();
39 v3f d = d_wanted.normalize() * dl;
43 speed.Y = target_speed.Y;
51 TestSAO proto_TestSAO(NULL, 0, v3f(0,0,0));
53 TestSAO::TestSAO(ServerEnvironment *env, u16 id, v3f pos):
54 ServerActiveObject(env, id, pos),
58 ServerActiveObject::registerType(getType(), create);
61 ServerActiveObject* TestSAO::create(ServerEnvironment *env, u16 id, v3f pos,
62 const std::string &data)
64 return new TestSAO(env, id, pos);
67 void TestSAO::step(float dtime, bool send_recommended)
76 m_base_position.Y += dtime * BS * 2;
77 if(m_base_position.Y > 8*BS)
78 m_base_position.Y = 2*BS;
80 if(send_recommended == false)
87 //infostream<<"TestSAO: id="<<getId()<<" sending data"<<std::endl;
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, 0, v3f(0,0,0), "");
112 ItemSAO::ItemSAO(ServerEnvironment *env, u16 id, v3f pos,
113 const std::string inventorystring):
114 ServerActiveObject(env, id, pos),
115 m_inventorystring(inventorystring),
117 m_last_sent_position(0,0,0)
119 ServerActiveObject::registerType(getType(), create);
122 ServerActiveObject* ItemSAO::create(ServerEnvironment *env, u16 id, 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, id, pos, inventorystring);
139 void ItemSAO::step(float dtime, bool send_recommended)
143 const float interval = 0.2;
144 if(m_move_interval.step(dtime, interval)==false)
148 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
149 collisionMoveResult moveresult;
151 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
152 // Maximum movement without glitches
153 f32 pos_max_d = BS*0.25;
155 if(m_speed_f.getLength()*dtime > pos_max_d)
156 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
157 v3f pos_f = getBasePosition();
158 v3f pos_f_old = pos_f;
159 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
160 box, dtime, pos_f, m_speed_f);
162 if(send_recommended == false)
165 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
167 setBasePosition(pos_f);
168 m_last_sent_position = pos_f;
170 std::ostringstream os(std::ios::binary);
172 // command (0 = update position)
176 writeS32((u8*)buf, m_base_position.X*1000);
178 writeS32((u8*)buf, m_base_position.Y*1000);
180 writeS32((u8*)buf, m_base_position.Z*1000);
182 // create message and add to list
183 ActiveObjectMessage aom(getId(), false, os.str());
184 m_messages_out.push_back(aom);
188 std::string ItemSAO::getClientInitializationData()
190 std::ostringstream os(std::ios::binary);
196 writeS32((u8*)buf, m_base_position.X*1000);
198 writeS32((u8*)buf, m_base_position.Y*1000);
200 writeS32((u8*)buf, m_base_position.Z*1000);
203 os<<serializeString(m_inventorystring);
207 std::string ItemSAO::getStaticData()
209 infostream<<__FUNCTION_NAME<<std::endl;
210 std::ostringstream os(std::ios::binary);
216 os<<serializeString(m_inventorystring);
220 InventoryItem * ItemSAO::createInventoryItem()
223 std::istringstream is(m_inventorystring, std::ios_base::binary);
224 InventoryItem *item = InventoryItem::deSerialize(is);
225 infostream<<__FUNCTION_NAME<<": m_inventorystring=\""
226 <<m_inventorystring<<"\" -> item="<<item
230 catch(SerializationError &e)
232 infostream<<__FUNCTION_NAME<<": serialization error: "
233 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
238 void ItemSAO::rightClick(Player *player)
240 infostream<<__FUNCTION_NAME<<std::endl;
241 InventoryItem *item = createInventoryItem();
245 bool to_be_deleted = item->use(m_env, player);
250 // Reflect changes to the item here
251 m_inventorystring = item->getItemString();
261 RatSAO proto_RatSAO(NULL, 0, v3f(0,0,0));
263 RatSAO::RatSAO(ServerEnvironment *env, u16 id, v3f pos):
264 ServerActiveObject(env, id, 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, u16 id, 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, id, pos);
293 void RatSAO::step(float dtime, bool send_recommended)
297 if(m_is_active == false)
299 if(m_inactive_interval.step(dtime, 0.5)==false)
316 m_speed_f.Y -= dtime*9.81*BS;
319 Move around if some player is close
321 bool player_is_close = false;
322 // Check connected players
323 core::list<Player*> players = m_env->getPlayers(true);
324 core::list<Player*>::Iterator i;
325 for(i = players.begin();
326 i != players.end(); i++)
329 v3f playerpos = player->getPosition();
330 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
332 player_is_close = true;
337 m_is_active = player_is_close;
339 if(player_is_close == false)
347 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
349 m_speed_f.X = speed * dir.X;
350 m_speed_f.Z = speed * dir.Z;
352 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
359 m_speed_f.Y = 5.0*BS;
367 m_counter2 += (float)(myrand()%100)/100*3.0;
368 m_yaw += ((float)(myrand()%200)-100)/100*180;
369 m_yaw = wrapDegrees(m_yaw);
374 m_oldpos = m_base_position;
377 Move it, with collision detection
380 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
381 collisionMoveResult moveresult;
382 // Maximum movement without glitches
383 f32 pos_max_d = BS*0.25;
385 if(m_speed_f.getLength()*dtime > pos_max_d)
386 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
387 v3f pos_f = getBasePosition();
388 v3f pos_f_old = pos_f;
389 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
390 box, dtime, pos_f, m_speed_f);
391 m_touching_ground = moveresult.touching_ground;
393 setBasePosition(pos_f);
395 if(send_recommended == false)
398 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
400 m_last_sent_position = pos_f;
402 std::ostringstream os(std::ios::binary);
403 // command (0 = update position)
406 writeV3F1000(os, m_base_position);
408 writeF1000(os, m_yaw);
409 // create message and add to list
410 ActiveObjectMessage aom(getId(), false, os.str());
411 m_messages_out.push_back(aom);
415 std::string RatSAO::getClientInitializationData()
417 std::ostringstream os(std::ios::binary);
421 writeV3F1000(os, m_base_position);
425 std::string RatSAO::getStaticData()
427 //infostream<<__FUNCTION_NAME<<std::endl;
428 std::ostringstream os(std::ios::binary);
434 InventoryItem* RatSAO::createPickedUpItem()
436 std::istringstream is("CraftItem rat 1", std::ios_base::binary);
437 InventoryItem *item = InventoryItem::deSerialize(is);
446 Oerkki1SAO proto_Oerkki1SAO(NULL, 0, v3f(0,0,0));
448 Oerkki1SAO::Oerkki1SAO(ServerEnvironment *env, u16 id, v3f pos):
449 ServerActiveObject(env, id, pos),
453 ServerActiveObject::registerType(getType(), create);
455 m_oldpos = v3f(0,0,0);
456 m_last_sent_position = v3f(0,0,0);
461 m_touching_ground = false;
463 m_after_jump_timer = 0;
466 ServerActiveObject* Oerkki1SAO::create(ServerEnvironment *env, u16 id, v3f pos,
467 const std::string &data)
469 std::istringstream is(data, std::ios::binary);
471 u8 version = readU8(is);
474 // check if version is supported
477 Oerkki1SAO *o = new Oerkki1SAO(env, id, pos);
482 void Oerkki1SAO::step(float dtime, bool send_recommended)
486 if(m_is_active == false)
488 if(m_inactive_interval.step(dtime, 0.5)==false)
504 m_after_jump_timer -= dtime;
506 v3f old_speed = m_speed_f;
509 m_speed_f.Y -= dtime*9.81*BS;
512 Move around if some player is close
514 bool player_is_close = false;
515 bool player_is_too_close = false;
517 // Check connected players
518 core::list<Player*> players = m_env->getPlayers(true);
519 core::list<Player*>::Iterator i;
520 for(i = players.begin();
521 i != players.end(); i++)
524 v3f playerpos = player->getPosition();
525 f32 dist = m_base_position.getDistanceFrom(playerpos);
530 player_is_too_close = true;
531 near_player_pos = playerpos;
533 else if(dist < BS*15.0 && !player_is_too_close)
535 player_is_close = true;
536 near_player_pos = playerpos;
540 m_is_active = player_is_close;
542 v3f target_speed = m_speed_f;
546 target_speed = v3f(0,0,0);
552 v3f ndir = near_player_pos - m_base_position;
556 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
557 if(nyaw < m_yaw - 180)
559 else if(nyaw > m_yaw + 180)
561 m_yaw = 0.95*m_yaw + 0.05*nyaw;
562 m_yaw = wrapDegrees(m_yaw);
566 if((m_touching_ground || m_after_jump_timer > 0.0)
567 && !player_is_too_close)
569 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
570 target_speed.X = speed * dir.X;
571 target_speed.Z = speed * dir.Z;
574 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
582 target_speed.Y = 5.0*BS;
583 m_after_jump_timer = 1.0;
591 m_counter2 += (float)(myrand()%100)/100*3.0;
592 //m_yaw += ((float)(myrand()%200)-100)/100*180;
593 m_yaw += ((float)(myrand()%200)-100)/100*90;
594 m_yaw = wrapDegrees(m_yaw);
599 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
600 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
602 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
604 m_oldpos = m_base_position;
607 Move it, with collision detection
610 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
611 collisionMoveResult moveresult;
612 // Maximum movement without glitches
613 f32 pos_max_d = BS*0.25;
615 if(m_speed_f.getLength()*dtime > pos_max_d)
616 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
617 v3f pos_f = getBasePosition();
618 v3f pos_f_old = pos_f;
619 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
620 box, dtime, pos_f, m_speed_f);
621 m_touching_ground = moveresult.touching_ground;
623 // Do collision damage
624 float tolerance = BS*30;
625 float factor = BS*0.5;
626 v3f speed_diff = old_speed - m_speed_f;
627 // Increase effect in X and Z
630 float vel = speed_diff.getLength();
633 f32 damage_f = (vel - tolerance)/BS*factor;
634 u16 damage = (u16)(damage_f+0.5);
638 setBasePosition(pos_f);
640 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
643 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
645 m_last_sent_position = pos_f;
647 std::ostringstream os(std::ios::binary);
648 // command (0 = update position)
651 writeV3F1000(os, m_base_position);
653 writeF1000(os, m_yaw);
654 // create message and add to list
655 ActiveObjectMessage aom(getId(), false, os.str());
656 m_messages_out.push_back(aom);
660 std::string Oerkki1SAO::getClientInitializationData()
662 std::ostringstream os(std::ios::binary);
666 writeV3F1000(os, m_base_position);
670 std::string Oerkki1SAO::getStaticData()
672 //infostream<<__FUNCTION_NAME<<std::endl;
673 std::ostringstream os(std::ios::binary);
681 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
683 m_speed_f += dir*12*BS;
690 void Oerkki1SAO::doDamage(u16 d)
692 infostream<<"oerkki damage: "<<d<<std::endl;
706 std::ostringstream os(std::ios::binary);
707 // command (1 = damage)
711 // create message and add to list
712 ActiveObjectMessage aom(getId(), false, os.str());
713 m_messages_out.push_back(aom);
722 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
724 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
725 ServerActiveObject(env, id, pos),
729 ServerActiveObject::registerType(getType(), create);
731 m_oldpos = v3f(0,0,0);
732 m_last_sent_position = v3f(0,0,0);
737 m_touching_ground = false;
740 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
741 const std::string &data)
743 std::istringstream is(data, std::ios::binary);
748 // check if version is supported
751 return new FireflySAO(env, id, pos);
754 void FireflySAO::step(float dtime, bool send_recommended)
758 if(m_is_active == false)
760 if(m_inactive_interval.step(dtime, 0.5)==false)
768 // Apply (less) gravity
769 m_speed_f.Y -= dtime*3*BS;
772 Move around if some player is close
774 bool player_is_close = false;
775 // Check connected players
776 core::list<Player*> players = m_env->getPlayers(true);
777 core::list<Player*>::Iterator i;
778 for(i = players.begin();
779 i != players.end(); i++)
782 v3f playerpos = player->getPosition();
783 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
785 player_is_close = true;
790 m_is_active = player_is_close;
792 if(player_is_close == false)
800 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
802 m_speed_f.X = speed * dir.X;
803 m_speed_f.Z = speed * dir.Z;
805 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
812 m_speed_f.Y = 5.0*BS;
820 m_counter2 += (float)(myrand()%100)/100*3.0;
821 m_yaw += ((float)(myrand()%200)-100)/100*180;
822 m_yaw = wrapDegrees(m_yaw);
827 m_oldpos = m_base_position;
830 Move it, with collision detection
833 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
834 collisionMoveResult moveresult;
835 // Maximum movement without glitches
836 f32 pos_max_d = BS*0.25;
838 if(m_speed_f.getLength()*dtime > pos_max_d)
839 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
840 v3f pos_f = getBasePosition();
841 v3f pos_f_old = pos_f;
842 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
843 box, dtime, pos_f, m_speed_f);
844 m_touching_ground = moveresult.touching_ground;
846 setBasePosition(pos_f);
848 if(send_recommended == false)
851 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
853 m_last_sent_position = pos_f;
855 std::ostringstream os(std::ios::binary);
856 // command (0 = update position)
859 writeV3F1000(os, m_base_position);
861 writeF1000(os, m_yaw);
862 // create message and add to list
863 ActiveObjectMessage aom(getId(), false, os.str());
864 m_messages_out.push_back(aom);
868 std::string FireflySAO::getClientInitializationData()
870 std::ostringstream os(std::ios::binary);
874 writeV3F1000(os, m_base_position);
878 std::string FireflySAO::getStaticData()
880 //infostream<<__FUNCTION_NAME<<std::endl;
881 std::ostringstream os(std::ios::binary);
887 InventoryItem* FireflySAO::createPickedUpItem()
889 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
890 InventoryItem *item = InventoryItem::deSerialize(is);
899 MobV2SAO proto_MobV2SAO(NULL, 0, v3f(0,0,0), NULL);
901 MobV2SAO::MobV2SAO(ServerEnvironment *env, u16 id, v3f pos,
902 Settings *init_properties):
903 ServerActiveObject(env, id, pos),
904 m_move_type("ground_nodes"),
906 m_last_sent_position(0,0,0),
912 m_touching_ground(false),
914 m_walk_around(false),
915 m_walk_around_timer(0),
916 m_next_pos_exists(false),
917 m_shoot_reload_timer(0),
921 m_disturb_timer(100000),
922 m_random_disturb_timer(0),
925 ServerActiveObject::registerType(getType(), create);
927 m_properties = new Settings();
929 m_properties->update(*init_properties);
931 m_properties->setV3F("pos", pos);
933 setPropertyDefaults();
937 MobV2SAO::~MobV2SAO()
942 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, u16 id, v3f pos,
943 const std::string &data)
945 std::istringstream is(data, std::ios::binary);
947 properties.parseConfigLines(is, "MobArgsEnd");
948 MobV2SAO *o = new MobV2SAO(env, id, pos, &properties);
952 std::string MobV2SAO::getStaticData()
956 std::ostringstream os(std::ios::binary);
957 m_properties->writeLines(os);
961 std::string MobV2SAO::getClientInitializationData()
963 //infostream<<__FUNCTION_NAME<<std::endl;
967 std::ostringstream os(std::ios::binary);
972 Settings client_properties;
974 /*client_properties.set("version", "0");
975 client_properties.updateValue(*m_properties, "pos");
976 client_properties.updateValue(*m_properties, "yaw");
977 client_properties.updateValue(*m_properties, "hp");*/
979 // Just send everything for simplicity
980 client_properties.update(*m_properties);
982 std::ostringstream os2(std::ios::binary);
983 client_properties.writeLines(os2);
984 compressZlib(os2.str(), os);
989 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
991 for(int dx=0; dx<size.X; dx++)
992 for(int dy=0; dy<size.Y; dy++)
993 for(int dz=0; dz<size.Z; dz++){
994 v3s16 dp(dx, dy, dz);
996 MapNode n = map->getNodeNoEx(p);
997 if(n.getContent() != CONTENT_AIR)
1003 bool checkWalkablePosition(Map *map, v3s16 p0)
1005 v3s16 p = p0 + v3s16(0,-1,0);
1006 MapNode n = map->getNodeNoEx(p);
1007 if(n.getContent() != CONTENT_AIR)
1012 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1014 if(!checkFreePosition(map, p0, size))
1016 if(!checkWalkablePosition(map, p0))
1021 static void get_random_u32_array(u32 a[], u32 len)
1024 for(i=0; i<len; i++)
1028 u32 k = myrand() % n;
1036 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1038 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1040 core::map<v3s16, MapBlock*> modified_blocks;
1042 for(int dx=0; dx<size.X; dx++)
1043 for(int dy=0; dy<size.Y; dy++)
1044 for(int dz=0; dz<size.Z; dz++){
1045 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1047 MapNode n = map->getNodeNoEx(p);
1048 if(n.getContent() == CONTENT_IGNORE)
1050 //map->removeNodeWithEvent(p);
1051 map->removeNodeAndUpdate(p, modified_blocks);
1054 // Send a MEET_OTHER event
1056 event.type = MEET_OTHER;
1057 for(core::map<v3s16, MapBlock*>::Iterator
1058 i = modified_blocks.getIterator();
1059 i.atEnd() == false; i++)
1061 v3s16 p = i.getNode()->getKey();
1062 event.modified_blocks.insert(p, true);
1064 map->dispatchEvent(&event);
1067 void MobV2SAO::step(float dtime, bool send_recommended)
1070 Map *map = &m_env->getMap();
1074 if(m_die_age >= 0.0 && m_age >= m_die_age){
1079 m_random_disturb_timer += dtime;
1080 if(m_random_disturb_timer >= 5.0)
1082 m_random_disturb_timer = 0;
1083 // Check connected players
1084 core::list<Player*> players = m_env->getPlayers(true);
1085 core::list<Player*>::Iterator i;
1086 for(i = players.begin();
1087 i != players.end(); i++)
1089 Player *player = *i;
1090 v3f playerpos = player->getPosition();
1091 f32 dist = m_base_position.getDistanceFrom(playerpos);
1094 if(myrand_range(0,2) == 0){
1095 infostream<<"ACTION: id="<<m_id<<" got randomly disturbed by "
1096 <<player->getName()<<std::endl;
1097 m_disturbing_player = player->getName();
1098 m_disturb_timer = 0;
1105 Player *disturbing_player =
1106 m_env->getPlayer(m_disturbing_player.c_str());
1107 v3f disturbing_player_off = v3f(0,1,0);
1108 v3f disturbing_player_norm = v3f(0,1,0);
1109 float disturbing_player_distance = 1000000;
1110 float disturbing_player_dir = 0;
1111 if(disturbing_player){
1112 disturbing_player_off =
1113 disturbing_player->getPosition() - m_base_position;
1114 disturbing_player_distance = disturbing_player_off.getLength();
1115 disturbing_player_norm = disturbing_player_off;
1116 disturbing_player_norm.normalize();
1117 disturbing_player_dir = 180./PI*atan2(disturbing_player_norm.Z,
1118 disturbing_player_norm.X);
1121 m_disturb_timer += dtime;
1125 m_shooting_timer -= dtime;
1126 if(m_shooting_timer <= 0.0 && m_shooting){
1129 std::string shoot_type = m_properties->get("shoot_type");
1130 v3f shoot_pos(0,0,0);
1131 shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1132 if(shoot_type == "fireball"){
1133 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1136 v3f speed = dir * BS * 10.0;
1137 v3f pos = m_base_position + shoot_pos;
1138 infostream<<__FUNCTION_NAME<<": Shooting fireball from "<<PP(pos)
1139 <<" at speed "<<PP(speed)<<std::endl;
1140 Settings properties;
1141 properties.set("looks", "fireball");
1142 properties.setV3F("speed", speed);
1143 properties.setFloat("die_age", 5.0);
1144 properties.set("move_type", "constant_speed");
1145 properties.setFloat("hp", 1000);
1146 properties.set("lock_full_brightness", "true");
1147 properties.set("player_hit_damage", "9");
1148 properties.set("player_hit_distance", "2");
1149 properties.set("player_hit_interval", "1");
1150 ServerActiveObject *obj = new MobV2SAO(m_env, 0,
1152 //m_env->addActiveObjectAsStatic(obj);
1153 m_env->addActiveObject(obj);
1155 infostream<<__FUNCTION_NAME<<": Unknown shoot_type="<<shoot_type
1160 m_shoot_reload_timer += dtime;
1162 float reload_time = 15.0;
1163 if(m_disturb_timer <= 15.0)
1166 if(!m_shooting && m_shoot_reload_timer >= reload_time &&
1170 if(m_disturb_timer < 30.0 && disturbing_player &&
1171 disturbing_player_distance < 16*BS &&
1172 fabs(disturbing_player_norm.Y) < 0.8){
1173 m_yaw = disturbing_player_dir;
1175 m_shoot_y += disturbing_player_norm.Y;
1177 m_shoot_reload_timer = 0.0;
1179 m_shooting_timer = 1.5;
1181 std::ostringstream os(std::ios::binary);
1182 // command (2 = shooting)
1185 writeF1000(os, m_shooting_timer + 0.1);
1188 // create message and add to list
1189 ActiveObjectMessage aom(getId(), false, os.str());
1190 m_messages_out.push_back(aom);
1195 if(m_move_type == "ground_nodes")
1198 m_walk_around_timer -= dtime;
1199 if(m_walk_around_timer <= 0.0){
1200 m_walk_around = !m_walk_around;
1202 m_walk_around_timer = 0.1*myrand_range(10,50);
1204 m_walk_around_timer = 0.1*myrand_range(30,70);
1209 if(m_next_pos_exists){
1210 v3f pos_f = m_base_position;
1211 v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1213 v3f v = next_pos_f - pos_f;
1214 m_yaw = atan2(v.Z, v.X) / PI * 180;
1216 v3f diff = next_pos_f - pos_f;
1219 float speed = BS * 0.5;
1222 dir *= dtime * speed;
1223 bool arrived = false;
1224 if(dir.getLength() > diff.getLength()){
1229 m_base_position = pos_f;
1231 if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1232 //infostream<<"id="<<m_id<<": arrived to "<<PP(m_next_pos_i)<<std::endl;
1233 m_next_pos_exists = false;
1237 v3s16 pos_i = floatToInt(m_base_position, BS);
1238 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1239 v3s16 pos_size_off(0,0,0);
1240 if(m_size.X >= 2.5){
1241 pos_size_off.X = -1;
1242 pos_size_off.Y = -1;
1245 if(!m_next_pos_exists){
1246 /* Check whether to drop down */
1247 if(checkFreePosition(map,
1248 pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1249 m_next_pos_i = pos_i + v3s16(0,-1,0);
1250 m_next_pos_exists = true;
1259 if(!m_next_pos_exists){
1260 /* Find some position where to go next */
1263 for(int dx=-1; dx<=1; dx++)
1264 for(int dy=-1; dy<=1; dy++)
1265 for(int dz=-1; dz<=1; dz++){
1266 if(dx == 0 && dy == 0)
1268 if(dx != 0 && dz != 0 && dy != 0)
1270 dps[num_dps++] = v3s16(dx,dy,dz);
1273 get_random_u32_array(order, num_dps);
1274 /*infostream<<"At pos "<<PP(pos_i)<<"; Random array: ";
1275 for(int i=0; i<num_dps; i++){
1276 infostream<<order[i]<<" ";
1278 infostream<<std::endl;*/
1279 for(int i=0; i<num_dps; i++){
1280 v3s16 p = dps[order[i]] + pos_i;
1281 bool is_free = checkFreeAndWalkablePosition(map,
1282 p + pos_size_off, size_blocks);
1283 //infostream<<PP(p)<<" is_free="<<is_free<<std::endl;
1287 m_next_pos_exists = true;
1293 else if(m_move_type == "constant_speed")
1295 m_base_position += m_speed * dtime;
1297 v3s16 pos_i = floatToInt(m_base_position, BS);
1298 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1299 v3s16 pos_size_off(0,0,0);
1300 if(m_size.X >= 2.5){
1301 pos_size_off.X = -1;
1302 pos_size_off.Y = -1;
1304 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1306 explodeSquare(map, pos_i, v3s16(3,3,3));
1313 infostream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1314 <<m_move_type<<"\""<<std::endl;
1317 if(send_recommended == false)
1320 if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1326 u16 MobV2SAO::punch(const std::string &toolname, v3f dir,
1327 const std::string &playername)
1330 Map *map = &m_env->getMap();
1332 infostream<<"ACTION: "<<playername<<" punches id="<<m_id
1333 <<" with a \""<<toolname<<"\" at "
1334 <<PP(m_base_position/BS)<<std::endl;
1336 m_disturb_timer = 0;
1337 m_disturbing_player = playername;
1338 m_next_pos_exists = false; // Cancel moving immediately
1340 m_yaw = wrapDegrees_180(180./PI*atan2(dir.Z, dir.X) + 180.);
1341 v3f new_base_position = m_base_position + dir * BS;
1343 v3s16 pos_i = floatToInt(new_base_position, BS);
1344 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1345 v3s16 pos_size_off(0,0,0);
1346 if(m_size.X >= 2.5){
1347 pos_size_off.X = -1;
1348 pos_size_off.Y = -1;
1350 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1352 m_base_position = new_base_position;
1357 /* See tool names in inventory.h */
1358 if(toolname == "WSword")
1360 if(toolname == "STSword")
1362 if(toolname == "SteelSword")
1364 if(toolname == "STAxe")
1366 if(toolname == "SteelAxe")
1368 if(toolname == "SteelPick")
1374 bool MobV2SAO::isPeaceful()
1376 return m_properties->getBool("is_peaceful");
1379 void MobV2SAO::sendPosition()
1381 m_last_sent_position = m_base_position;
1383 std::ostringstream os(std::ios::binary);
1384 // command (0 = update position)
1387 writeV3F1000(os, m_base_position);
1389 writeF1000(os, m_yaw);
1390 // create message and add to list
1391 ActiveObjectMessage aom(getId(), false, os.str());
1392 m_messages_out.push_back(aom);
1395 void MobV2SAO::setPropertyDefaults()
1397 m_properties->setDefault("is_peaceful", "false");
1398 m_properties->setDefault("move_type", "ground_nodes");
1399 m_properties->setDefault("speed", "(0,0,0)");
1400 m_properties->setDefault("age", "0");
1401 m_properties->setDefault("yaw", "0");
1402 m_properties->setDefault("pos", "(0,0,0)");
1403 m_properties->setDefault("hp", "0");
1404 m_properties->setDefault("die_age", "-1");
1405 m_properties->setDefault("size", "(1,2)");
1406 m_properties->setDefault("shoot_type", "fireball");
1407 m_properties->setDefault("shoot_y", "0");
1409 void MobV2SAO::readProperties()
1411 m_move_type = m_properties->get("move_type");
1412 m_speed = m_properties->getV3F("speed");
1413 m_age = m_properties->getFloat("age");
1414 m_yaw = m_properties->getFloat("yaw");
1415 m_base_position = m_properties->getV3F("pos");
1416 m_hp = m_properties->getS32("hp");
1417 m_die_age = m_properties->getFloat("die_age");
1418 m_size = m_properties->getV2F("size");
1420 void MobV2SAO::updateProperties()
1422 m_properties->set("move_type", m_move_type);
1423 m_properties->setV3F("speed", m_speed);
1424 m_properties->setFloat("age", m_age);
1425 m_properties->setFloat("yaw", m_yaw);
1426 m_properties->setV3F("pos", m_base_position);
1427 m_properties->setS32("hp", m_hp);
1428 m_properties->setFloat("die_age", m_die_age);
1429 m_properties->setV2F("size", m_size);
1431 m_properties->setS32("version", 0);
1434 void MobV2SAO::doDamage(u16 d)
1436 infostream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1444 actionstream<<"A "<<(isPeaceful()?"peaceful":"non-peaceful")
1445 <<" mob id="<<m_id<<" dies at "<<PP(m_base_position)<<std::endl;
1452 std::ostringstream os(std::ios::binary);
1453 // command (1 = damage)
1457 // create message and add to list
1458 ActiveObjectMessage aom(getId(), false, os.str());
1459 m_messages_out.push_back(aom);