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 //dstream<<"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 dstream<<"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 dstream<<__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 dstream<<__FUNCTION_NAME<<": m_inventorystring=\""
226 <<m_inventorystring<<"\" -> item="<<item
230 catch(SerializationError &e)
232 dstream<<__FUNCTION_NAME<<": serialization error: "
233 <<"m_inventorystring=\""<<m_inventorystring<<"\""<<std::endl;
238 void ItemSAO::rightClick(Player *player)
240 dstream<<__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 //dstream<<__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);
528 player_is_too_close = true;
529 near_player_pos = playerpos;
532 else if(dist < BS*15.0)
534 player_is_close = true;
535 near_player_pos = playerpos;
539 m_is_active = player_is_close;
541 v3f target_speed = m_speed_f;
545 target_speed = v3f(0,0,0);
551 v3f ndir = near_player_pos - m_base_position;
555 f32 nyaw = 180./PI*atan2(ndir.Z,ndir.X);
556 if(nyaw < m_yaw - 180)
558 else if(nyaw > m_yaw + 180)
560 m_yaw = 0.95*m_yaw + 0.05*nyaw;
561 m_yaw = wrapDegrees(m_yaw);
565 if((m_touching_ground || m_after_jump_timer > 0.0)
566 && !player_is_too_close)
568 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
569 target_speed.X = speed * dir.X;
570 target_speed.Z = speed * dir.Z;
573 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
581 target_speed.Y = 5.0*BS;
582 m_after_jump_timer = 1.0;
590 m_counter2 += (float)(myrand()%100)/100*3.0;
591 //m_yaw += ((float)(myrand()%200)-100)/100*180;
592 m_yaw += ((float)(myrand()%200)-100)/100*90;
593 m_yaw = wrapDegrees(m_yaw);
598 if((m_speed_f - target_speed).getLength() > BS*4 || player_is_too_close)
599 accelerate_xz(m_speed_f, target_speed, dtime*BS*8);
601 accelerate_xz(m_speed_f, target_speed, dtime*BS*4);
603 m_oldpos = m_base_position;
606 Move it, with collision detection
609 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*5./3.,BS/3.);
610 collisionMoveResult moveresult;
611 // Maximum movement without glitches
612 f32 pos_max_d = BS*0.25;
614 if(m_speed_f.getLength()*dtime > pos_max_d)
615 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);*/
616 v3f pos_f = getBasePosition();
617 v3f pos_f_old = pos_f;
618 moveresult = collisionMovePrecise(&m_env->getMap(), pos_max_d,
619 box, dtime, pos_f, m_speed_f);
620 m_touching_ground = moveresult.touching_ground;
622 // Do collision damage
623 float tolerance = BS*30;
624 float factor = BS*0.5;
625 v3f speed_diff = old_speed - m_speed_f;
626 // Increase effect in X and Z
629 float vel = speed_diff.getLength();
632 f32 damage_f = (vel - tolerance)/BS*factor;
633 u16 damage = (u16)(damage_f+0.5);
637 setBasePosition(pos_f);
639 if(send_recommended == false && m_speed_f.getLength() < 3.0*BS)
642 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
644 m_last_sent_position = pos_f;
646 std::ostringstream os(std::ios::binary);
647 // command (0 = update position)
650 writeV3F1000(os, m_base_position);
652 writeF1000(os, m_yaw);
653 // create message and add to list
654 ActiveObjectMessage aom(getId(), false, os.str());
655 m_messages_out.push_back(aom);
659 std::string Oerkki1SAO::getClientInitializationData()
661 std::ostringstream os(std::ios::binary);
665 writeV3F1000(os, m_base_position);
669 std::string Oerkki1SAO::getStaticData()
671 //dstream<<__FUNCTION_NAME<<std::endl;
672 std::ostringstream os(std::ios::binary);
680 u16 Oerkki1SAO::punch(const std::string &toolname, v3f dir)
682 m_speed_f += dir*12*BS;
689 void Oerkki1SAO::doDamage(u16 d)
691 dstream<<"oerkki damage: "<<d<<std::endl;
705 std::ostringstream os(std::ios::binary);
706 // command (1 = damage)
710 // create message and add to list
711 ActiveObjectMessage aom(getId(), false, os.str());
712 m_messages_out.push_back(aom);
721 FireflySAO proto_FireflySAO(NULL, 0, v3f(0,0,0));
723 FireflySAO::FireflySAO(ServerEnvironment *env, u16 id, v3f pos):
724 ServerActiveObject(env, id, pos),
728 ServerActiveObject::registerType(getType(), create);
730 m_oldpos = v3f(0,0,0);
731 m_last_sent_position = v3f(0,0,0);
736 m_touching_ground = false;
739 ServerActiveObject* FireflySAO::create(ServerEnvironment *env, u16 id, v3f pos,
740 const std::string &data)
742 std::istringstream is(data, std::ios::binary);
747 // check if version is supported
750 return new FireflySAO(env, id, pos);
753 void FireflySAO::step(float dtime, bool send_recommended)
757 if(m_is_active == false)
759 if(m_inactive_interval.step(dtime, 0.5)==false)
767 // Apply (less) gravity
768 m_speed_f.Y -= dtime*3*BS;
771 Move around if some player is close
773 bool player_is_close = false;
774 // Check connected players
775 core::list<Player*> players = m_env->getPlayers(true);
776 core::list<Player*>::Iterator i;
777 for(i = players.begin();
778 i != players.end(); i++)
781 v3f playerpos = player->getPosition();
782 if(m_base_position.getDistanceFrom(playerpos) < BS*10.0)
784 player_is_close = true;
789 m_is_active = player_is_close;
791 if(player_is_close == false)
799 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
801 m_speed_f.X = speed * dir.X;
802 m_speed_f.Z = speed * dir.Z;
804 if(m_touching_ground && (m_oldpos - m_base_position).getLength()
811 m_speed_f.Y = 5.0*BS;
819 m_counter2 += (float)(myrand()%100)/100*3.0;
820 m_yaw += ((float)(myrand()%200)-100)/100*180;
821 m_yaw = wrapDegrees(m_yaw);
826 m_oldpos = m_base_position;
829 Move it, with collision detection
832 core::aabbox3d<f32> box(-BS/3.,-BS*2/3.0,-BS/3., BS/3.,BS*4./3.,BS/3.);
833 collisionMoveResult moveresult;
834 // Maximum movement without glitches
835 f32 pos_max_d = BS*0.25;
837 if(m_speed_f.getLength()*dtime > pos_max_d)
838 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
839 v3f pos_f = getBasePosition();
840 v3f pos_f_old = pos_f;
841 moveresult = collisionMoveSimple(&m_env->getMap(), pos_max_d,
842 box, dtime, pos_f, m_speed_f);
843 m_touching_ground = moveresult.touching_ground;
845 setBasePosition(pos_f);
847 if(send_recommended == false)
850 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
852 m_last_sent_position = pos_f;
854 std::ostringstream os(std::ios::binary);
855 // command (0 = update position)
858 writeV3F1000(os, m_base_position);
860 writeF1000(os, m_yaw);
861 // create message and add to list
862 ActiveObjectMessage aom(getId(), false, os.str());
863 m_messages_out.push_back(aom);
867 std::string FireflySAO::getClientInitializationData()
869 std::ostringstream os(std::ios::binary);
873 writeV3F1000(os, m_base_position);
877 std::string FireflySAO::getStaticData()
879 //dstream<<__FUNCTION_NAME<<std::endl;
880 std::ostringstream os(std::ios::binary);
886 InventoryItem* FireflySAO::createPickedUpItem()
888 std::istringstream is("CraftItem firefly 1", std::ios_base::binary);
889 InventoryItem *item = InventoryItem::deSerialize(is);
898 MobV2SAO proto_MobV2SAO(NULL, 0, v3f(0,0,0), NULL);
900 MobV2SAO::MobV2SAO(ServerEnvironment *env, u16 id, v3f pos,
901 Settings *init_properties):
902 ServerActiveObject(env, id, pos),
903 m_move_type("ground_nodes"),
905 m_last_sent_position(0,0,0),
911 m_touching_ground(false),
913 m_walk_around(false),
914 m_walk_around_timer(0),
915 m_next_pos_exists(false),
916 m_shoot_reload_timer(0),
920 m_disturb_timer(100000)
922 ServerActiveObject::registerType(getType(), create);
924 m_properties = new Settings();
926 m_properties->update(*init_properties);
928 m_properties->setV3F("pos", pos);
930 setPropertyDefaults();
934 MobV2SAO::~MobV2SAO()
939 ServerActiveObject* MobV2SAO::create(ServerEnvironment *env, u16 id, v3f pos,
940 const std::string &data)
942 std::istringstream is(data, std::ios::binary);
944 properties.parseConfigLines(is, "MobArgsEnd");
945 MobV2SAO *o = new MobV2SAO(env, id, pos, &properties);
949 std::string MobV2SAO::getStaticData()
953 std::ostringstream os(std::ios::binary);
954 m_properties->writeLines(os);
958 std::string MobV2SAO::getClientInitializationData()
960 //dstream<<__FUNCTION_NAME<<std::endl;
964 std::ostringstream os(std::ios::binary);
969 Settings client_properties;
971 /*client_properties.set("version", "0");
972 client_properties.updateValue(*m_properties, "pos");
973 client_properties.updateValue(*m_properties, "yaw");
974 client_properties.updateValue(*m_properties, "hp");*/
976 // Just send everything for simplicity
977 client_properties.update(*m_properties);
979 std::ostringstream os2(std::ios::binary);
980 client_properties.writeLines(os2);
981 compressZlib(os2.str(), os);
986 bool checkFreePosition(Map *map, v3s16 p0, v3s16 size)
988 for(int dx=0; dx<size.X; dx++)
989 for(int dy=0; dy<size.Y; dy++)
990 for(int dz=0; dz<size.Z; dz++){
991 v3s16 dp(dx, dy, dz);
993 MapNode n = map->getNodeNoEx(p);
994 if(n.getContent() != CONTENT_AIR)
1000 bool checkWalkablePosition(Map *map, v3s16 p0)
1002 v3s16 p = p0 + v3s16(0,-1,0);
1003 MapNode n = map->getNodeNoEx(p);
1004 if(n.getContent() != CONTENT_AIR)
1009 bool checkFreeAndWalkablePosition(Map *map, v3s16 p0, v3s16 size)
1011 if(!checkFreePosition(map, p0, size))
1013 if(!checkWalkablePosition(map, p0))
1018 static void get_random_u32_array(u32 a[], u32 len)
1021 for(i=0; i<len; i++)
1025 u32 k = myrand() % n;
1033 #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")"
1035 static void explodeSquare(Map *map, v3s16 p0, v3s16 size)
1037 core::map<v3s16, MapBlock*> modified_blocks;
1039 for(int dx=0; dx<size.X; dx++)
1040 for(int dy=0; dy<size.Y; dy++)
1041 for(int dz=0; dz<size.Z; dz++){
1042 v3s16 dp(dx - size.X/2, dy - size.Y/2, dz - size.Z/2);
1044 MapNode n = map->getNodeNoEx(p);
1045 if(n.getContent() == CONTENT_IGNORE)
1047 //map->removeNodeWithEvent(p);
1048 map->removeNodeAndUpdate(p, modified_blocks);
1051 // Send a MEET_OTHER event
1053 event.type = MEET_OTHER;
1054 for(core::map<v3s16, MapBlock*>::Iterator
1055 i = modified_blocks.getIterator();
1056 i.atEnd() == false; i++)
1058 v3s16 p = i.getNode()->getKey();
1059 event.modified_blocks.insert(p, true);
1061 map->dispatchEvent(&event);
1064 void MobV2SAO::step(float dtime, bool send_recommended)
1067 Map *map = &m_env->getMap();
1071 if(m_die_age >= 0.0 && m_age >= m_die_age){
1076 Player *disturbing_player =
1077 m_env->getPlayer(m_disturbing_player.c_str());
1078 v3f disturbing_player_off = v3f(0,1,0);
1079 float disturbing_player_distance = 1000000;
1080 float disturbing_player_dir = 0;
1081 if(disturbing_player){
1082 disturbing_player_off =
1083 disturbing_player->getPosition() - m_base_position;
1084 disturbing_player_distance = disturbing_player_off.getLength();
1085 disturbing_player_off.normalize();
1086 disturbing_player_dir = 180./M_PI*atan2(disturbing_player_off.Z,
1087 disturbing_player_off.X);
1090 m_disturb_timer += dtime;
1094 m_shooting_timer -= dtime;
1095 if(m_shooting_timer <= 0.0 && m_shooting){
1098 std::string shoot_type = m_properties->get("shoot_type");
1099 v3f shoot_pos(0,0,0);
1100 shoot_pos.Y += m_properties->getFloat("shoot_y") * BS;
1101 if(shoot_type == "fireball"){
1102 v3f dir(cos(m_yaw/180*PI),0,sin(m_yaw/180*PI));
1103 v3f speed = dir * BS * 10.0;
1104 v3f pos = m_base_position + shoot_pos;
1105 dstream<<__FUNCTION_NAME<<": Shooting fireball from "<<PP(pos)
1106 <<" at speed "<<PP(speed)<<std::endl;
1107 Settings properties;
1108 properties.set("looks", "fireball");
1109 properties.setV3F("speed", speed);
1110 properties.setFloat("die_age", 5.0);
1111 properties.set("move_type", "constant_speed");
1112 properties.setFloat("hp", 1000);
1113 properties.set("lock_full_brightness", "true");
1114 properties.set("player_hit_damage", "9");
1115 properties.set("player_hit_distance", "2");
1116 properties.set("player_hit_interval", "1");
1117 ServerActiveObject *obj = new MobV2SAO(m_env, 0,
1119 //m_env->addActiveObjectAsStatic(obj);
1120 m_env->addActiveObject(obj);
1122 dstream<<__FUNCTION_NAME<<": Unknown shoot_type="<<shoot_type
1127 m_shoot_reload_timer += dtime;
1129 float reload_time = 15.0;
1130 if(m_disturb_timer <= 15.0)
1133 if(m_shoot_reload_timer >= reload_time && !m_next_pos_exists)
1135 if(m_disturb_timer < 30.0 && disturbing_player &&
1136 disturbing_player_distance < 16*BS &&
1137 fabs(disturbing_player_off.Y) < 5*BS){
1138 m_yaw = disturbing_player_dir;
1141 m_shoot_reload_timer = 0.0;
1143 m_shooting_timer = 1.5;
1145 std::ostringstream os(std::ios::binary);
1146 // command (2 = shooting)
1149 writeF1000(os, m_shooting_timer + 0.1);
1152 // create message and add to list
1153 ActiveObjectMessage aom(getId(), false, os.str());
1154 m_messages_out.push_back(aom);
1159 if(m_move_type == "ground_nodes")
1162 m_walk_around_timer -= dtime;
1163 if(m_walk_around_timer <= 0.0){
1164 m_walk_around = !m_walk_around;
1166 m_walk_around_timer = 0.1*myrand_range(10,50);
1168 m_walk_around_timer = 0.1*myrand_range(30,70);
1173 if(m_next_pos_exists){
1174 v3f pos_f = m_base_position;
1175 v3f next_pos_f = intToFloat(m_next_pos_i, BS);
1177 v3f v = next_pos_f - pos_f;
1178 m_yaw = atan2(v.Z, v.X) / M_PI * 180;
1180 v3f diff = next_pos_f - pos_f;
1183 float speed = BS * 0.5;
1186 dir *= dtime * speed;
1187 bool arrived = false;
1188 if(dir.getLength() > diff.getLength()){
1193 m_base_position = pos_f;
1195 if((pos_f - next_pos_f).getLength() < 0.1 || arrived){
1196 //dstream<<"id="<<m_id<<": arrived to "<<PP(m_next_pos_i)<<std::endl;
1197 m_next_pos_exists = false;
1201 v3s16 pos_i = floatToInt(m_base_position, BS);
1202 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1203 v3s16 pos_size_off(0,0,0);
1204 if(m_size.X >= 2.5){
1205 pos_size_off.X = -1;
1206 pos_size_off.Y = -1;
1209 if(!m_next_pos_exists){
1210 /* Check whether to drop down */
1211 if(checkFreePosition(map,
1212 pos_i + pos_size_off + v3s16(0,-1,0), size_blocks)){
1213 m_next_pos_i = pos_i + v3s16(0,-1,0);
1214 m_next_pos_exists = true;
1223 if(!m_next_pos_exists){
1224 /* Find some position where to go next */
1227 for(int dx=-1; dx<=1; dx++)
1228 for(int dy=-1; dy<=1; dy++)
1229 for(int dz=-1; dz<=1; dz++){
1230 if(dx == 0 && dy == 0)
1232 if(dx != 0 && dz != 0 && dy != 0)
1234 dps[num_dps++] = v3s16(dx,dy,dz);
1237 get_random_u32_array(order, num_dps);
1238 /*dstream<<"At pos "<<PP(pos_i)<<"; Random array: ";
1239 for(int i=0; i<num_dps; i++){
1240 dstream<<order[i]<<" ";
1242 dstream<<std::endl;*/
1243 for(int i=0; i<num_dps; i++){
1244 v3s16 p = dps[order[i]] + pos_i;
1245 bool is_free = checkFreeAndWalkablePosition(map,
1246 p + pos_size_off, size_blocks);
1247 //dstream<<PP(p)<<" is_free="<<is_free<<std::endl;
1251 m_next_pos_exists = true;
1257 else if(m_move_type == "constant_speed")
1259 m_base_position += m_speed * dtime;
1261 v3s16 pos_i = floatToInt(m_base_position, BS);
1262 v3s16 size_blocks = v3s16(m_size.X+0.5,m_size.Y+0.5,m_size.X+0.5);
1263 v3s16 pos_size_off(0,0,0);
1264 if(m_size.X >= 2.5){
1265 pos_size_off.X = -1;
1266 pos_size_off.Y = -1;
1268 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1270 explodeSquare(map, pos_i, v3s16(3,3,3));
1277 dstream<<"MobV2SAO::step(): id="<<m_id<<" unknown move_type=\""
1278 <<m_move_type<<"\""<<std::endl;
1281 if(send_recommended == false)
1284 if(m_base_position.getDistanceFrom(m_last_sent_position) > 0.05*BS)
1290 u16 MobV2SAO::punch(const std::string &toolname, v3f dir,
1291 const std::string &playername)
1294 Map *map = &m_env->getMap();
1296 m_disturb_timer = 0;
1297 m_disturbing_player = playername;
1299 m_yaw = wrapDegrees_180(180./M_PI*atan2(dir.Z, dir.X) + 180.);
1300 v3f new_base_position = m_base_position + dir * BS;
1302 v3s16 pos_i = floatToInt(new_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;
1309 bool free = checkFreePosition(map, pos_i + pos_size_off, size_blocks);
1311 m_base_position = new_base_position;
1316 dstream<<"id="<<m_id<<": punch with \""<<toolname<<"\""<<std::endl;
1317 /* See tool names in inventory.h */
1318 if(toolname == "WSword")
1320 if(toolname == "STSword")
1322 if(toolname == "SteelSword")
1324 if(toolname == "STAxe")
1326 if(toolname == "SteelAxe")
1328 if(toolname == "SteelPick")
1334 void MobV2SAO::sendPosition()
1336 m_last_sent_position = m_base_position;
1338 std::ostringstream os(std::ios::binary);
1339 // command (0 = update position)
1342 writeV3F1000(os, m_base_position);
1344 writeF1000(os, m_yaw);
1345 // create message and add to list
1346 ActiveObjectMessage aom(getId(), false, os.str());
1347 m_messages_out.push_back(aom);
1350 void MobV2SAO::setPropertyDefaults()
1352 m_properties->setDefault("move_type", "ground_nodes");
1353 m_properties->setDefault("speed", "(0,0,0)");
1354 m_properties->setDefault("age", "0");
1355 m_properties->setDefault("yaw", "0");
1356 m_properties->setDefault("pos", "(0,0,0)");
1357 m_properties->setDefault("hp", "0");
1358 m_properties->setDefault("die_age", "-1");
1359 m_properties->setDefault("size", "(1,2)");
1360 m_properties->setDefault("shoot_type", "fireball");
1361 m_properties->setDefault("shoot_y", "0");
1363 void MobV2SAO::readProperties()
1365 m_move_type = m_properties->get("move_type");
1366 m_speed = m_properties->getV3F("speed");
1367 m_age = m_properties->getFloat("age");
1368 m_yaw = m_properties->getFloat("yaw");
1369 m_base_position = m_properties->getV3F("pos");
1370 m_hp = m_properties->getS32("hp");
1371 m_die_age = m_properties->getFloat("die_age");
1372 m_size = m_properties->getV2F("size");
1374 void MobV2SAO::updateProperties()
1376 m_properties->set("move_type", m_move_type);
1377 m_properties->setV3F("speed", m_speed);
1378 m_properties->setFloat("age", m_age);
1379 m_properties->setFloat("yaw", m_yaw);
1380 m_properties->setV3F("pos", m_base_position);
1381 m_properties->setS32("hp", m_hp);
1382 m_properties->setFloat("die_age", m_die_age);
1383 m_properties->setV2F("size", m_size);
1385 m_properties->setS32("version", 0);
1388 void MobV2SAO::doDamage(u16 d)
1390 dstream<<"MobV2 hp="<<m_hp<<" damage="<<d<<std::endl;
1404 std::ostringstream os(std::ios::binary);
1405 // command (1 = damage)
1409 // create message and add to list
1410 ActiveObjectMessage aom(getId(), false, os.str());
1411 m_messages_out.push_back(aom);