3 Copyright (C) 2010-2013 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.
22 #include "network/networkprotocol.h"
23 #include "util/numeric.h"
24 #include "serverobject.h"
25 #include "itemgroup.h"
26 #include "object_properties.h"
27 #include "constants.h"
29 class UnitSAO: public ServerActiveObject
32 UnitSAO(ServerEnvironment *env, v3f pos);
33 virtual ~UnitSAO() = default;
35 virtual void setYaw(const float yaw) { m_yaw = yaw; }
36 float getYaw() const { return m_yaw; };
37 f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
39 f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
41 s16 getHP() const { return m_hp; }
42 // Use a function, if isDead can be defined by other conditions
43 bool isDead() const { return m_hp == 0; }
45 bool isAttached() const;
46 void setArmorGroups(const ItemGroupList &armor_groups);
47 const ItemGroupList &getArmorGroups();
48 void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
49 void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
50 void setAnimationSpeed(float frame_speed);
51 void setBonePosition(const std::string &bone, v3f position, v3f rotation);
52 void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
53 void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
54 void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
55 void addAttachmentChild(int child_id);
56 void removeAttachmentChild(int child_id);
57 const std::unordered_set<int> &getAttachmentChildIds();
58 ObjectProperties* accessObjectProperties();
59 void notifyObjectPropertiesModified();
64 bool m_properties_sent = true;
65 struct ObjectProperties m_prop;
67 ItemGroupList m_armor_groups;
68 bool m_armor_groups_sent = false;
70 v2f m_animation_range;
71 float m_animation_speed = 0.0f;
72 float m_animation_blend = 0.0f;
73 bool m_animation_loop = true;
74 bool m_animation_sent = false;
75 bool m_animation_speed_sent = false;
77 // Stores position and rotation for each bone name
78 std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
79 bool m_bone_position_sent = false;
81 int m_attachment_parent_id = 0;
82 std::unordered_set<int> m_attachment_child_ids;
83 std::string m_attachment_bone = "";
84 v3f m_attachment_position;
85 v3f m_attachment_rotation;
86 bool m_attachment_sent = false;
90 LuaEntitySAO needs some internals exposed.
93 class LuaEntitySAO : public UnitSAO
96 LuaEntitySAO(ServerEnvironment *env, v3f pos,
97 const std::string &name, const std::string &state);
99 ActiveObjectType getType() const
100 { return ACTIVEOBJECT_TYPE_LUAENTITY; }
101 ActiveObjectType getSendType() const
102 { return ACTIVEOBJECT_TYPE_GENERIC; }
103 virtual void addedToEnvironment(u32 dtime_s);
104 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
105 const std::string &data);
106 void step(float dtime, bool send_recommended);
107 std::string getClientInitializationData(u16 protocol_version);
108 bool isStaticAllowed() const
109 { return m_prop.static_save; }
110 void getStaticData(std::string *result) const;
112 const ToolCapabilities *toolcap=NULL,
113 ServerActiveObject *puncher=NULL,
114 float time_from_last_punch=1000000);
115 void rightClick(ServerActiveObject *clicker);
116 void setPos(const v3f &pos);
117 void moveTo(v3f pos, bool continuous);
118 float getMinimumSavedMovement();
119 std::string getDescription();
120 void setHP(s16 hp, const PlayerHPChangeReason &reason);
122 /* LuaEntitySAO-specific */
123 void setVelocity(v3f velocity);
124 void addVelocity(v3f velocity)
126 m_velocity += velocity;
129 void setAcceleration(v3f acceleration);
130 v3f getAcceleration();
132 void setTextureMod(const std::string &mod);
133 std::string getTextureMod() const;
134 void setSprite(v2s16 p, int num_frames, float framelength,
135 bool select_horiz_by_yawpitch);
136 std::string getName();
137 bool getCollisionBox(aabb3f *toset) const;
138 bool getSelectionBox(aabb3f *toset) const;
139 bool collideWithObjects() const;
141 std::string getPropertyPacket();
142 void sendPosition(bool do_interpolate, bool is_movement_end);
144 std::string m_init_name;
145 std::string m_init_state;
146 bool m_registered = false;
151 float m_last_sent_yaw = 0.0f;
152 v3f m_last_sent_position;
153 v3f m_last_sent_velocity;
154 float m_last_sent_position_timer = 0.0f;
155 float m_last_sent_move_precision = 0.0f;
156 std::string m_current_texture_modifier = "";
160 PlayerSAO needs some internals exposed.
165 float m_pool = 15.0f;
170 void setMax(float new_max)
177 void add(float dtime)
189 bool grab(float dtime)
193 if(m_pool + dtime > m_max)
202 class PlayerSAO : public UnitSAO
205 PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
206 bool is_singleplayer);
208 ActiveObjectType getType() const
209 { return ACTIVEOBJECT_TYPE_PLAYER; }
210 ActiveObjectType getSendType() const
211 { return ACTIVEOBJECT_TYPE_GENERIC; }
212 std::string getDescription();
215 Active object <-> environment interface
218 void addedToEnvironment(u32 dtime_s);
219 void removingFromEnvironment();
220 bool isStaticAllowed() const { return false; }
221 std::string getClientInitializationData(u16 protocol_version);
222 void getStaticData(std::string *result) const;
223 void step(float dtime, bool send_recommended);
224 void setBasePosition(const v3f &position);
225 void setPos(const v3f &pos);
226 void moveTo(v3f pos, bool continuous);
227 void setYaw(const float yaw);
228 // Data should not be sent at player initialization
229 void setYawAndSend(const float yaw);
230 void setPitch(const float pitch);
231 // Data should not be sent at player initialization
232 void setPitchAndSend(const float pitch);
233 f32 getPitch() const { return m_pitch; }
234 f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
236 f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
237 void setFov(const float pitch);
238 f32 getFov() const { return m_fov; }
239 void setWantedRange(const s16 range);
240 s16 getWantedRange() const { return m_wanted_range; }
243 Interaction interface
247 const ToolCapabilities *toolcap,
248 ServerActiveObject *puncher,
249 float time_from_last_punch);
250 void rightClick(ServerActiveObject *clicker) {}
251 void setHP(s16 hp, const PlayerHPChangeReason &reason);
252 void setHPRaw(s16 hp) { m_hp = hp; }
254 u16 getBreath() const { return m_breath; }
255 void setBreath(const u16 breath, bool send = true);
261 Inventory* getInventory();
262 const Inventory* getInventory() const;
263 InventoryLocation getInventoryLocation() const;
264 std::string getWieldList() const;
265 ItemStack getWieldedItem() const;
266 ItemStack getWieldedItemOrHand() const;
267 bool setWieldedItem(const ItemStack &item);
268 int getWieldIndex() const;
269 void setWieldIndex(int i);
277 RemotePlayer *getPlayer() { return m_player; }
278 session_t getPeerID() const { return m_peer_id; }
282 v3f getLastGoodPosition() const
284 return m_last_good_position;
286 float resetTimeFromLastPunch()
288 float r = m_time_from_last_punch;
289 m_time_from_last_punch = 0.0;
292 void noCheatDigStart(const v3s16 &p)
294 m_nocheat_dig_pos = p;
295 m_nocheat_dig_time = 0;
297 v3s16 getNoCheatDigPos()
299 return m_nocheat_dig_pos;
301 float getNoCheatDigTime()
303 return m_nocheat_dig_time;
307 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
309 LagPool& getDigPool()
313 // Returns true if cheated
314 bool checkMovementCheat();
318 void updatePrivileges(const std::set<std::string> &privs,
319 bool is_singleplayer)
322 m_is_singleplayer = is_singleplayer;
325 bool getCollisionBox(aabb3f *toset) const;
326 bool getSelectionBox(aabb3f *toset) const;
327 bool collideWithObjects() const { return true; }
329 void finalize(RemotePlayer *player, const std::set<std::string> &privs);
331 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
332 v3f getEyeOffset() const;
334 inline Metadata &getMeta() { return m_meta; }
337 std::string getPropertyPacket();
338 void unlinkPlayerSessionAndSave();
340 RemotePlayer *m_player = nullptr;
341 session_t m_peer_id = 0;
342 Inventory *m_inventory = nullptr;
348 v3f m_last_good_position;
349 float m_time_from_last_teleport = 0.0f;
350 float m_time_from_last_punch = 0.0f;
351 v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
352 float m_nocheat_dig_time = 0.0f;
355 IntervalLimiter m_breathing_interval;
356 IntervalLimiter m_drowning_interval;
357 IntervalLimiter m_node_hurt_interval;
359 int m_wield_index = 0;
360 bool m_position_not_sent = false;
362 // Cached privileges for enforcement
363 std::set<std::string> m_privs;
364 bool m_is_singleplayer;
366 u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
369 s16 m_wanted_range = 0.0f;
373 float m_physics_override_speed = 1.0f;
374 float m_physics_override_jump = 1.0f;
375 float m_physics_override_gravity = 1.0f;
376 bool m_physics_override_sneak = true;
377 bool m_physics_override_sneak_glitch = false;
378 bool m_physics_override_new_move = true;
379 bool m_physics_override_sent = false;
383 struct PlayerHPChangeReason {
394 ServerActiveObject *object;
395 bool from_mod = false;
396 int lua_reference = -1;
398 bool setTypeFromString(const std::string &typestr)
400 if (typestr == "set_hp")
402 else if (typestr == "punch")
404 else if (typestr == "fall")
406 else if (typestr == "node_damage")
408 else if (typestr == "drown")
410 else if (typestr == "respawn")
418 std::string getTypeAsString() const
421 case PlayerHPChangeReason::SET_HP:
423 case PlayerHPChangeReason::PLAYER_PUNCH:
425 case PlayerHPChangeReason::FALL:
427 case PlayerHPChangeReason::NODE_DAMAGE:
428 return "node_damage";
429 case PlayerHPChangeReason::DROWNING:
431 case PlayerHPChangeReason::RESPAWN:
438 PlayerHPChangeReason(Type type, ServerActiveObject *object=NULL):
439 type(type), object(object)