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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "content_sao.h"
21 #include "collision.h"
22 #include "environment.h"
24 #include "main.h" // For g_profiler
26 #include "serialization.h" // For compressZlib
27 #include "tool.h" // For ToolCapabilities
30 #include "scriptapi.h"
31 #include "genericobject.h"
32 #include "util/serialize.h"
34 core::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
40 class DummyLoadSAO : public ServerActiveObject
43 DummyLoadSAO(ServerEnvironment *env, v3f pos, u8 type):
44 ServerActiveObject(env, pos)
46 ServerActiveObject::registerType(type, create);
48 // Pretend to be the test object (to fool the client)
50 { return ACTIVEOBJECT_TYPE_TEST; }
51 // And never save to disk
52 bool isStaticAllowed() const
55 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
56 const std::string &data)
58 return new DummyLoadSAO(env, pos, 0);
61 void step(float dtime, bool send_recommended)
64 infostream<<"DummyLoadSAO step"<<std::endl;
70 // Prototype (registers item for deserialization)
71 DummyLoadSAO proto1_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_RAT);
72 DummyLoadSAO proto2_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_OERKKI1);
73 DummyLoadSAO proto3_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_FIREFLY);
74 DummyLoadSAO proto4_DummyLoadSAO(NULL, v3f(0,0,0), ACTIVEOBJECT_TYPE_MOBV2);
80 class TestSAO : public ServerActiveObject
83 TestSAO(ServerEnvironment *env, v3f pos):
84 ServerActiveObject(env, pos),
88 ServerActiveObject::registerType(getType(), create);
91 { return ACTIVEOBJECT_TYPE_TEST; }
93 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
94 const std::string &data)
96 return new TestSAO(env, pos);
99 void step(float dtime, bool send_recommended)
108 m_base_position.Y += dtime * BS * 2;
109 if(m_base_position.Y > 8*BS)
110 m_base_position.Y = 2*BS;
112 if(send_recommended == false)
122 data += itos(0); // 0 = position
124 data += itos(m_base_position.X);
126 data += itos(m_base_position.Y);
128 data += itos(m_base_position.Z);
130 ActiveObjectMessage aom(getId(), false, data);
131 m_messages_out.push_back(aom);
140 // Prototype (registers item for deserialization)
141 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
146 DEPRECATED: New dropped items are implemented in Lua; see
147 builtin/item_entity.lua.
150 class ItemSAO : public ServerActiveObject
154 { return ACTIVEOBJECT_TYPE_ITEM; }
156 float getMinimumSavedMovement()
159 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
160 const std::string &data)
162 std::istringstream is(data, std::ios::binary);
167 // check if version is supported
170 std::string itemstring = deSerializeString(is);
171 infostream<<"create(): Creating item \""
172 <<itemstring<<"\""<<std::endl;
173 return new ItemSAO(env, pos, itemstring);
176 ItemSAO(ServerEnvironment *env, v3f pos,
177 const std::string itemstring):
178 ServerActiveObject(env, pos),
179 m_itemstring(itemstring),
180 m_itemstring_changed(false),
182 m_last_sent_position(0,0,0)
184 ServerActiveObject::registerType(getType(), create);
187 void step(float dtime, bool send_recommended)
189 ScopeProfiler sp2(g_profiler, "step avg", SPT_AVG);
193 const float interval = 0.2;
194 if(m_move_interval.step(dtime, interval)==false)
198 core::aabbox3d<f32> box(-BS/3.,0.0,-BS/3., BS/3.,BS*2./3.,BS/3.);
199 collisionMoveResult moveresult;
201 m_speed_f += v3f(0, -dtime*9.81*BS, 0);
202 // Maximum movement without glitches
203 f32 pos_max_d = BS*0.25;
205 if(m_speed_f.getLength()*dtime > pos_max_d)
206 m_speed_f *= pos_max_d / (m_speed_f.getLength()*dtime);
207 v3f pos_f = getBasePosition();
208 v3f pos_f_old = pos_f;
209 v3f accel_f = v3f(0,0,0);
211 IGameDef *gamedef = m_env->getGameDef();
212 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
213 pos_max_d, box, stepheight, dtime,
214 pos_f, m_speed_f, accel_f);
216 if(send_recommended == false)
219 if(pos_f.getDistanceFrom(m_last_sent_position) > 0.05*BS)
221 setBasePosition(pos_f);
222 m_last_sent_position = pos_f;
224 std::ostringstream os(std::ios::binary);
225 // command (0 = update position)
228 writeV3F1000(os, m_base_position);
229 // create message and add to list
230 ActiveObjectMessage aom(getId(), false, os.str());
231 m_messages_out.push_back(aom);
233 if(m_itemstring_changed)
235 m_itemstring_changed = false;
237 std::ostringstream os(std::ios::binary);
238 // command (1 = update itemstring)
241 os<<serializeString(m_itemstring);
242 // create message and add to list
243 ActiveObjectMessage aom(getId(), false, os.str());
244 m_messages_out.push_back(aom);
248 std::string getClientInitializationData()
250 std::ostringstream os(std::ios::binary);
254 writeV3F1000(os, m_base_position);
256 os<<serializeString(m_itemstring);
260 std::string getStaticData()
262 infostream<<__FUNCTION_NAME<<std::endl;
263 std::ostringstream os(std::ios::binary);
267 os<<serializeString(m_itemstring);
271 ItemStack createItemStack()
274 IItemDefManager *idef = m_env->getGameDef()->idef();
276 item.deSerialize(m_itemstring, idef);
277 infostream<<__FUNCTION_NAME<<": m_itemstring=\""<<m_itemstring
278 <<"\" -> item=\""<<item.getItemString()<<"\""
282 catch(SerializationError &e)
284 infostream<<__FUNCTION_NAME<<": serialization error: "
285 <<"m_itemstring=\""<<m_itemstring<<"\""<<std::endl;
291 const ToolCapabilities *toolcap,
292 ServerActiveObject *puncher,
293 float time_from_last_punch)
295 // Take item into inventory
296 ItemStack item = createItemStack();
297 Inventory *inv = puncher->getInventory();
300 std::string wieldlist = puncher->getWieldList();
301 ItemStack leftover = inv->addItem(wieldlist, item);
302 puncher->setInventoryModified();
309 m_itemstring = leftover.getItemString();
310 m_itemstring_changed = true;
319 std::string m_itemstring;
320 bool m_itemstring_changed;
322 v3f m_last_sent_position;
323 IntervalLimiter m_move_interval;
326 // Prototype (registers item for deserialization)
327 ItemSAO proto_ItemSAO(NULL, v3f(0,0,0), "");
329 ServerActiveObject* createItemSAO(ServerEnvironment *env, v3f pos,
330 const std::string itemstring)
332 return new ItemSAO(env, pos, itemstring);
339 // Prototype (registers item for deserialization)
340 LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");
342 LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
343 const std::string &name, const std::string &state):
344 ServerActiveObject(env, pos),
350 m_acceleration(0,0,0),
352 m_properties_sent(true),
354 m_last_sent_position(0,0,0),
355 m_last_sent_velocity(0,0,0),
356 m_last_sent_position_timer(0),
357 m_last_sent_move_precision(0),
358 m_armor_groups_sent(false)
360 // Only register type if no environment supplied
362 ServerActiveObject::registerType(getType(), create);
366 // Initialize something to armor groups
367 m_armor_groups["fleshy"] = 3;
368 m_armor_groups["snappy"] = 2;
371 LuaEntitySAO::~LuaEntitySAO()
374 lua_State *L = m_env->getLua();
375 scriptapi_luaentity_rm(L, m_id);
379 void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
381 ServerActiveObject::addedToEnvironment(dtime_s);
383 // Create entity from name
384 lua_State *L = m_env->getLua();
385 m_registered = scriptapi_luaentity_add(L, m_id, m_init_name.c_str());
389 scriptapi_luaentity_get_properties(L, m_id, &m_prop);
390 // Initialize HP from properties
391 m_hp = m_prop.hp_max;
392 // Activate entity, supplying serialized state
393 scriptapi_luaentity_activate(L, m_id, m_init_state.c_str(), dtime_s);
397 ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
398 const std::string &data)
406 std::istringstream is(data, std::ios::binary);
408 u8 version = readU8(is);
409 // check if version is supported
411 name = deSerializeString(is);
412 state = deSerializeLongString(is);
414 else if(version == 1){
415 name = deSerializeString(is);
416 state = deSerializeLongString(is);
418 velocity = readV3F1000(is);
423 infostream<<"LuaEntitySAO::create(name=\""<<name<<"\" state=\""
424 <<state<<"\")"<<std::endl;
425 LuaEntitySAO *sao = new LuaEntitySAO(env, pos, name, state);
427 sao->m_velocity = velocity;
432 void LuaEntitySAO::step(float dtime, bool send_recommended)
434 if(!m_properties_sent)
436 m_properties_sent = true;
437 std::string str = getPropertyPacket();
438 // create message and add to list
439 ActiveObjectMessage aom(getId(), true, str);
440 m_messages_out.push_back(aom);
443 m_last_sent_position_timer += dtime;
446 core::aabbox3d<f32> box = m_prop.collisionbox;
449 collisionMoveResult moveresult;
450 f32 pos_max_d = BS*0.25; // Distance per iteration
451 f32 stepheight = 0; // Maximum climbable step height
452 v3f p_pos = m_base_position;
453 v3f p_velocity = m_velocity;
454 v3f p_acceleration = m_acceleration;
455 IGameDef *gamedef = m_env->getGameDef();
456 moveresult = collisionMoveSimple(&m_env->getMap(), gamedef,
457 pos_max_d, box, stepheight, dtime,
458 p_pos, p_velocity, p_acceleration);
460 m_base_position = p_pos;
461 m_velocity = p_velocity;
462 m_acceleration = p_acceleration;
464 m_base_position += dtime * m_velocity + 0.5 * dtime
465 * dtime * m_acceleration;
466 m_velocity += dtime * m_acceleration;
470 lua_State *L = m_env->getLua();
471 scriptapi_luaentity_step(L, m_id, dtime);
474 if(send_recommended == false)
477 // TODO: force send when acceleration changes enough?
478 float minchange = 0.2*BS;
479 if(m_last_sent_position_timer > 1.0){
481 } else if(m_last_sent_position_timer > 0.2){
484 float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
485 move_d += m_last_sent_move_precision;
486 float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
487 if(move_d > minchange || vel_d > minchange ||
488 fabs(m_yaw - m_last_sent_yaw) > 1.0){
489 sendPosition(true, false);
492 if(m_armor_groups_sent == false){
493 m_armor_groups_sent = true;
494 std::string str = gob_cmd_update_armor_groups(
496 // create message and add to list
497 ActiveObjectMessage aom(getId(), true, str);
498 m_messages_out.push_back(aom);
502 std::string LuaEntitySAO::getClientInitializationData()
504 std::ostringstream os(std::ios::binary);
505 writeU8(os, 0); // version
506 os<<serializeString(""); // name
507 writeU8(os, 0); // is_player
508 writeV3F1000(os, m_base_position);
509 writeF1000(os, m_yaw);
511 writeU8(os, 2); // number of messages stuffed in here
512 os<<serializeLongString(getPropertyPacket()); // message 1
513 os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
518 std::string LuaEntitySAO::getStaticData()
520 verbosestream<<__FUNCTION_NAME<<std::endl;
521 std::ostringstream os(std::ios::binary);
525 os<<serializeString(m_init_name);
528 lua_State *L = m_env->getLua();
529 std::string state = scriptapi_luaentity_get_staticdata(L, m_id);
530 os<<serializeLongString(state);
532 os<<serializeLongString(m_init_state);
537 writeV3F1000(os, m_velocity);
539 writeF1000(os, m_yaw);
543 int LuaEntitySAO::punch(v3f dir,
544 const ToolCapabilities *toolcap,
545 ServerActiveObject *puncher,
546 float time_from_last_punch)
549 // Delete unknown LuaEntities when punched
554 ItemStack *punchitem = NULL;
555 ItemStack punchitem_static;
557 punchitem_static = puncher->getWieldedItem();
558 punchitem = &punchitem_static;
561 PunchDamageResult result = getPunchDamage(
565 time_from_last_punch);
569 setHP(getHP() - result.damage);
571 actionstream<<getDescription()<<" punched by "
572 <<puncher->getDescription()<<", damage "<<result.damage
573 <<" hp, health now "<<getHP()<<" hp"<<std::endl;
576 std::string str = gob_cmd_punched(result.damage, getHP());
577 // create message and add to list
578 ActiveObjectMessage aom(getId(), true, str);
579 m_messages_out.push_back(aom);
586 lua_State *L = m_env->getLua();
587 scriptapi_luaentity_punch(L, m_id, puncher,
588 time_from_last_punch, toolcap, dir);
593 void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
597 lua_State *L = m_env->getLua();
598 scriptapi_luaentity_rightclick(L, m_id, clicker);
601 void LuaEntitySAO::setPos(v3f pos)
603 m_base_position = pos;
604 sendPosition(false, true);
607 void LuaEntitySAO::moveTo(v3f pos, bool continuous)
609 m_base_position = pos;
611 sendPosition(true, true);
614 float LuaEntitySAO::getMinimumSavedMovement()
619 std::string LuaEntitySAO::getDescription()
621 std::ostringstream os(std::ios::binary);
622 os<<"LuaEntitySAO at (";
623 os<<(m_base_position.X/BS)<<",";
624 os<<(m_base_position.Y/BS)<<",";
625 os<<(m_base_position.Z/BS);
630 void LuaEntitySAO::setHP(s16 hp)
636 s16 LuaEntitySAO::getHP() const
641 void LuaEntitySAO::setArmorGroups(const ItemGroupList &armor_groups)
643 m_armor_groups = armor_groups;
644 m_armor_groups_sent = false;
647 ObjectProperties* LuaEntitySAO::accessObjectProperties()
652 void LuaEntitySAO::notifyObjectPropertiesModified()
654 m_properties_sent = false;
657 void LuaEntitySAO::setVelocity(v3f velocity)
659 m_velocity = velocity;
662 v3f LuaEntitySAO::getVelocity()
667 void LuaEntitySAO::setAcceleration(v3f acceleration)
669 m_acceleration = acceleration;
672 v3f LuaEntitySAO::getAcceleration()
674 return m_acceleration;
677 void LuaEntitySAO::setYaw(float yaw)
682 float LuaEntitySAO::getYaw()
687 void LuaEntitySAO::setTextureMod(const std::string &mod)
689 std::string str = gob_cmd_set_texture_mod(mod);
690 // create message and add to list
691 ActiveObjectMessage aom(getId(), true, str);
692 m_messages_out.push_back(aom);
695 void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
696 bool select_horiz_by_yawpitch)
698 std::string str = gob_cmd_set_sprite(
702 select_horiz_by_yawpitch
704 // create message and add to list
705 ActiveObjectMessage aom(getId(), true, str);
706 m_messages_out.push_back(aom);
709 std::string LuaEntitySAO::getName()
714 std::string LuaEntitySAO::getPropertyPacket()
716 return gob_cmd_set_properties(m_prop);
719 void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
721 m_last_sent_move_precision = m_base_position.getDistanceFrom(
722 m_last_sent_position);
723 m_last_sent_position_timer = 0;
724 m_last_sent_yaw = m_yaw;
725 m_last_sent_position = m_base_position;
726 m_last_sent_velocity = m_velocity;
727 //m_last_sent_acceleration = m_acceleration;
729 float update_interval = m_env->getSendRecommendedInterval();
731 std::string str = gob_cmd_update_position(
740 // create message and add to list
741 ActiveObjectMessage aom(getId(), false, str);
742 m_messages_out.push_back(aom);
749 // No prototype, PlayerSAO does not need to be deserialized
751 PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
752 const std::set<std::string> &privs, bool is_singleplayer):
753 ServerActiveObject(env_, v3f(0,0,0)),
757 m_last_good_position(0,0,0),
758 m_last_good_position_age(0),
759 m_time_from_last_punch(0),
760 m_nocheat_dig_pos(32767, 32767, 32767),
761 m_nocheat_dig_time(0),
763 m_position_not_sent(false),
764 m_armor_groups_sent(false),
765 m_properties_sent(true),
767 m_is_singleplayer(is_singleplayer),
770 m_inventory_not_sent(false),
771 m_hp_not_sent(false),
772 m_wielded_item_not_sent(false)
775 assert(m_peer_id != 0);
776 setBasePosition(m_player->getPosition());
777 m_inventory = &m_player->inventory;
778 m_armor_groups["choppy"] = 2;
779 m_armor_groups["fleshy"] = 3;
781 m_prop.hp_max = PLAYER_MAX_HP;
782 m_prop.physical = false;
784 m_prop.collisionbox = core::aabbox3d<f32>(-1/3.,-1.0,-1/3., 1/3.,1.0,1/3.);
785 m_prop.visual = "upright_sprite";
786 m_prop.visual_size = v2f(1, 2);
787 m_prop.textures.clear();
788 m_prop.textures.push_back("player.png");
789 m_prop.textures.push_back("player_back.png");
790 m_prop.spritediv = v2s16(1,1);
791 m_prop.is_visible = (getHP() != 0);
792 m_prop.makes_footstep_sound = true;
795 PlayerSAO::~PlayerSAO()
797 if(m_inventory != &m_player->inventory)
802 std::string PlayerSAO::getDescription()
804 return std::string("player ") + m_player->getName();
807 // Called after id has been set and has been inserted in environment
808 void PlayerSAO::addedToEnvironment(u32 dtime_s)
810 ServerActiveObject::addedToEnvironment(dtime_s);
811 ServerActiveObject::setBasePosition(m_player->getPosition());
812 m_player->setPlayerSAO(this);
813 m_player->peer_id = m_peer_id;
814 m_last_good_position = m_player->getPosition();
815 m_last_good_position_age = 0.0;
818 // Called before removing from environment
819 void PlayerSAO::removingFromEnvironment()
821 ServerActiveObject::removingFromEnvironment();
822 if(m_player->getPlayerSAO() == this)
824 m_player->setPlayerSAO(NULL);
825 m_player->peer_id = 0;
829 bool PlayerSAO::isStaticAllowed() const
834 bool PlayerSAO::unlimitedTransferDistance() const
836 return g_settings->getBool("unlimited_player_transfer_distance");
839 std::string PlayerSAO::getClientInitializationData()
841 std::ostringstream os(std::ios::binary);
842 writeU8(os, 0); // version
843 os<<serializeString(m_player->getName()); // name
844 writeU8(os, 1); // is_player
845 writeV3F1000(os, m_player->getPosition() + v3f(0,BS*1,0));
846 writeF1000(os, m_player->getYaw());
847 writeS16(os, getHP());
848 writeU8(os, 2); // number of messages stuffed in here
849 os<<serializeLongString(getPropertyPacket()); // message 1
850 os<<serializeLongString(gob_cmd_update_armor_groups(m_armor_groups)); // 2
854 std::string PlayerSAO::getStaticData()
860 void PlayerSAO::step(float dtime, bool send_recommended)
862 if(!m_properties_sent)
864 m_properties_sent = true;
865 std::string str = getPropertyPacket();
866 // create message and add to list
867 ActiveObjectMessage aom(getId(), true, str);
868 m_messages_out.push_back(aom);
871 m_time_from_last_punch += dtime;
872 m_nocheat_dig_time += dtime;
874 if(m_is_singleplayer || g_settings->getBool("disable_anticheat"))
876 m_last_good_position = m_player->getPosition();
877 m_last_good_position_age = 0;
882 Check player movements
884 NOTE: Actually the server should handle player physics like the
885 client does and compare player's position to what is calculated
886 on our side. This is required when eg. players fly due to an
887 explosion. Altough a node-based alternative might be possible
888 too, and much more lightweight.
891 float player_max_speed = 0;
892 float player_max_speed_up = 0;
893 if(m_privs.count("fast") != 0){
895 player_max_speed = BS * 20;
896 player_max_speed_up = BS * 20;
899 player_max_speed = BS * 4.0;
900 player_max_speed_up = BS * 4.0;
903 player_max_speed *= 2.5;
904 player_max_speed_up *= 2.5;
906 m_last_good_position_age += dtime;
907 if(m_last_good_position_age >= 1.0){
908 float age = m_last_good_position_age;
909 v3f diff = (m_player->getPosition() - m_last_good_position);
910 float d_vert = diff.Y;
912 float d_horiz = diff.getLength();
913 /*infostream<<m_player->getName()<<"'s horizontal speed is "
914 <<(d_horiz/age)<<std::endl;*/
915 if(d_horiz <= age * player_max_speed &&
916 (d_vert < 0 || d_vert < age * player_max_speed_up)){
917 m_last_good_position = m_player->getPosition();
919 actionstream<<"Player "<<m_player->getName()
920 <<" moved too fast; resetting position"
922 m_player->setPosition(m_last_good_position);
925 m_last_good_position_age = 0;
929 if(send_recommended == false)
932 if(m_position_not_sent)
934 m_position_not_sent = false;
935 float update_interval = m_env->getSendRecommendedInterval();
936 std::string str = gob_cmd_update_position(
937 m_player->getPosition() + v3f(0,BS*1,0),
945 // create message and add to list
946 ActiveObjectMessage aom(getId(), false, str);
947 m_messages_out.push_back(aom);
950 if(m_wielded_item_not_sent)
952 m_wielded_item_not_sent = false;
953 // GenericCAO has no special way to show this
956 if(m_armor_groups_sent == false){
957 m_armor_groups_sent = true;
958 std::string str = gob_cmd_update_armor_groups(
960 // create message and add to list
961 ActiveObjectMessage aom(getId(), true, str);
962 m_messages_out.push_back(aom);
966 void PlayerSAO::setBasePosition(const v3f &position)
968 ServerActiveObject::setBasePosition(position);
969 m_position_not_sent = true;
972 void PlayerSAO::setPos(v3f pos)
974 m_player->setPosition(pos);
975 // Movement caused by this command is always valid
976 m_last_good_position = pos;
977 m_last_good_position_age = 0;
978 // Force position change on client
982 void PlayerSAO::moveTo(v3f pos, bool continuous)
984 m_player->setPosition(pos);
985 // Movement caused by this command is always valid
986 m_last_good_position = pos;
987 m_last_good_position_age = 0;
988 // Force position change on client
992 int PlayerSAO::punch(v3f dir,
993 const ToolCapabilities *toolcap,
994 ServerActiveObject *puncher,
995 float time_from_last_punch)
1000 // No effect if PvP disabled
1001 if(g_settings->getBool("enable_pvp") == false){
1002 if(puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER){
1003 std::string str = gob_cmd_punched(0, getHP());
1004 // create message and add to list
1005 ActiveObjectMessage aom(getId(), true, str);
1006 m_messages_out.push_back(aom);
1011 HitParams hitparams = getHitParams(m_armor_groups, toolcap,
1012 time_from_last_punch);
1014 actionstream<<"Player "<<m_player->getName()<<" punched by "
1015 <<puncher->getDescription()<<", damage "<<hitparams.hp
1018 setHP(getHP() - hitparams.hp);
1020 if(hitparams.hp != 0)
1022 std::string str = gob_cmd_punched(hitparams.hp, getHP());
1023 // create message and add to list
1024 ActiveObjectMessage aom(getId(), true, str);
1025 m_messages_out.push_back(aom);
1028 return hitparams.wear;
1031 void PlayerSAO::rightClick(ServerActiveObject *clicker)
1035 s16 PlayerSAO::getHP() const
1037 return m_player->hp;
1040 void PlayerSAO::setHP(s16 hp)
1042 s16 oldhp = m_player->hp;
1046 else if(hp > PLAYER_MAX_HP)
1049 if(hp < oldhp && g_settings->getBool("enable_damage") == false)
1051 m_hp_not_sent = true; // fix wrong prediction on client
1058 m_hp_not_sent = true;
1060 // On death or reincarnation send an active object message
1061 if((hp == 0) != (oldhp == 0))
1063 // Will send new is_visible value based on (getHP()!=0)
1064 m_properties_sent = false;
1066 std::string str = gob_cmd_punched(0, getHP());
1067 ActiveObjectMessage aom(getId(), true, str);
1068 m_messages_out.push_back(aom);
1072 void PlayerSAO::setArmorGroups(const ItemGroupList &armor_groups)
1074 m_armor_groups = armor_groups;
1075 m_armor_groups_sent = false;
1078 ObjectProperties* PlayerSAO::accessObjectProperties()
1083 void PlayerSAO::notifyObjectPropertiesModified()
1085 m_properties_sent = false;
1088 Inventory* PlayerSAO::getInventory()
1092 const Inventory* PlayerSAO::getInventory() const
1097 InventoryLocation PlayerSAO::getInventoryLocation() const
1099 InventoryLocation loc;
1100 loc.setPlayer(m_player->getName());
1104 void PlayerSAO::setInventoryModified()
1106 m_inventory_not_sent = true;
1109 std::string PlayerSAO::getWieldList() const
1114 int PlayerSAO::getWieldIndex() const
1116 return m_wield_index;
1119 void PlayerSAO::setWieldIndex(int i)
1121 if(i != m_wield_index)
1124 m_wielded_item_not_sent = true;
1128 void PlayerSAO::disconnected()
1132 if(m_player->getPlayerSAO() == this)
1134 m_player->setPlayerSAO(NULL);
1135 m_player->peer_id = 0;
1139 std::string PlayerSAO::getPropertyPacket()
1141 m_prop.is_visible = (getHP() != 0);
1142 return gob_cmd_set_properties(m_prop);