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 <util/numeric.h>
23 #include "serverobject.h"
24 #include "itemgroup.h"
25 #include "object_properties.h"
26 #include "constants.h"
28 class UnitSAO: public ServerActiveObject
31 UnitSAO(ServerEnvironment *env, v3f pos);
32 virtual ~UnitSAO() = default;
34 virtual void setYaw(const float yaw) { m_yaw = yaw; }
35 float getYaw() const { return m_yaw; };
36 f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
38 f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
40 s16 getHP() const { return m_hp; }
41 // Use a function, if isDead can be defined by other conditions
42 bool isDead() const { return m_hp == 0; }
44 bool isAttached() const;
45 void setArmorGroups(const ItemGroupList &armor_groups);
46 const ItemGroupList &getArmorGroups();
47 void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
48 void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
49 void setAnimationSpeed(float frame_speed);
50 void setBonePosition(const std::string &bone, v3f position, v3f rotation);
51 void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
52 void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
53 void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
54 void addAttachmentChild(int child_id);
55 void removeAttachmentChild(int child_id);
56 const std::unordered_set<int> &getAttachmentChildIds();
57 ObjectProperties* accessObjectProperties();
58 void notifyObjectPropertiesModified();
63 bool m_properties_sent = true;
64 struct ObjectProperties m_prop;
66 ItemGroupList m_armor_groups;
67 bool m_armor_groups_sent = false;
69 v2f m_animation_range;
70 float m_animation_speed = 0.0f;
71 float m_animation_blend = 0.0f;
72 bool m_animation_loop = true;
73 bool m_animation_sent = false;
74 bool m_animation_speed_sent = false;
76 // Stores position and rotation for each bone name
77 std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
78 bool m_bone_position_sent = false;
80 int m_attachment_parent_id = 0;
81 std::unordered_set<int> m_attachment_child_ids;
82 std::string m_attachment_bone = "";
83 v3f m_attachment_position;
84 v3f m_attachment_rotation;
85 bool m_attachment_sent = false;
89 LuaEntitySAO needs some internals exposed.
92 class LuaEntitySAO : public UnitSAO
95 LuaEntitySAO(ServerEnvironment *env, v3f pos,
96 const std::string &name, const std::string &state);
98 ActiveObjectType getType() const
99 { return ACTIVEOBJECT_TYPE_LUAENTITY; }
100 ActiveObjectType getSendType() const
101 { return ACTIVEOBJECT_TYPE_GENERIC; }
102 virtual void addedToEnvironment(u32 dtime_s);
103 static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
104 const std::string &data);
105 void step(float dtime, bool send_recommended);
106 std::string getClientInitializationData(u16 protocol_version);
107 void getStaticData(std::string *result) const;
109 const ToolCapabilities *toolcap=NULL,
110 ServerActiveObject *puncher=NULL,
111 float time_from_last_punch=1000000);
112 void rightClick(ServerActiveObject *clicker);
113 void setPos(const v3f &pos);
114 void moveTo(v3f pos, bool continuous);
115 float getMinimumSavedMovement();
116 std::string getDescription();
119 /* LuaEntitySAO-specific */
120 void setVelocity(v3f velocity);
122 void setAcceleration(v3f acceleration);
123 v3f getAcceleration();
125 void setTextureMod(const std::string &mod);
126 std::string getTextureMod() const;
127 void setSprite(v2s16 p, int num_frames, float framelength,
128 bool select_horiz_by_yawpitch);
129 std::string getName();
130 bool getCollisionBox(aabb3f *toset) const;
131 bool getSelectionBox(aabb3f *toset) const;
132 bool collideWithObjects() const;
134 std::string getPropertyPacket();
135 void sendPosition(bool do_interpolate, bool is_movement_end);
137 std::string m_init_name;
138 std::string m_init_state;
139 bool m_registered = false;
144 float m_last_sent_yaw = 0.0f;
145 v3f m_last_sent_position;
146 v3f m_last_sent_velocity;
147 float m_last_sent_position_timer = 0.0f;
148 float m_last_sent_move_precision = 0.0f;
149 std::string m_current_texture_modifier = "";
153 PlayerSAO needs some internals exposed.
158 float m_pool = 15.0f;
163 void setMax(float new_max)
170 void add(float dtime)
182 bool grab(float dtime)
186 if(m_pool + dtime > m_max)
193 typedef std::unordered_map<std::string, std::string> PlayerAttributes;
196 class PlayerSAO : public UnitSAO
199 PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, bool is_singleplayer);
201 ActiveObjectType getType() const
202 { return ACTIVEOBJECT_TYPE_PLAYER; }
203 ActiveObjectType getSendType() const
204 { return ACTIVEOBJECT_TYPE_GENERIC; }
205 std::string getDescription();
208 Active object <-> environment interface
211 void addedToEnvironment(u32 dtime_s);
212 void removingFromEnvironment();
213 bool isStaticAllowed() const { return false; }
214 std::string getClientInitializationData(u16 protocol_version);
215 void getStaticData(std::string *result) const;
216 void step(float dtime, bool send_recommended);
217 void setBasePosition(const v3f &position);
218 void setPos(const v3f &pos);
219 void moveTo(v3f pos, bool continuous);
220 void setYaw(const float yaw);
221 // Data should not be sent at player initialization
222 void setYawAndSend(const float yaw);
223 void setPitch(const float pitch);
224 // Data should not be sent at player initialization
225 void setPitchAndSend(const float pitch);
226 f32 getPitch() const { return m_pitch; }
227 f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
229 f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
230 void setFov(const float pitch);
231 f32 getFov() const { return m_fov; }
232 void setWantedRange(const s16 range);
233 s16 getWantedRange() const { return m_wanted_range; }
236 Interaction interface
240 const ToolCapabilities *toolcap,
241 ServerActiveObject *puncher,
242 float time_from_last_punch);
243 void rightClick(ServerActiveObject *clicker) {}
245 void setHPRaw(s16 hp) { m_hp = hp; }
247 u16 getBreath() const { return m_breath; }
248 void setBreath(const u16 breath, bool send = true);
254 Inventory* getInventory();
255 const Inventory* getInventory() const;
256 InventoryLocation getInventoryLocation() const;
257 std::string getWieldList() const;
258 ItemStack getWieldedItem() const;
259 ItemStack getWieldedItemOrHand() const;
260 bool setWieldedItem(const ItemStack &item);
261 int getWieldIndex() const;
262 void setWieldIndex(int i);
267 inline void setExtendedAttribute(const std::string &attr, const std::string &value)
269 m_extra_attributes[attr] = value;
270 m_extended_attributes_modified = true;
273 inline bool getExtendedAttribute(const std::string &attr, std::string *value)
275 if (m_extra_attributes.find(attr) == m_extra_attributes.end())
278 *value = m_extra_attributes[attr];
282 inline void removeExtendedAttribute(const std::string &attr)
284 PlayerAttributes::iterator it = m_extra_attributes.find(attr);
285 if (it == m_extra_attributes.end())
288 m_extra_attributes.erase(it);
289 m_extended_attributes_modified = true;
292 inline const PlayerAttributes &getExtendedAttributes()
294 return m_extra_attributes;
297 inline bool extendedAttributesModified() const
299 return m_extended_attributes_modified;
302 inline void setExtendedAttributeModified(bool v)
304 m_extended_attributes_modified = v;
313 RemotePlayer *getPlayer() { return m_player; }
314 u16 getPeerID() const { return m_peer_id; }
318 v3f getLastGoodPosition() const
320 return m_last_good_position;
322 float resetTimeFromLastPunch()
324 float r = m_time_from_last_punch;
325 m_time_from_last_punch = 0.0;
328 void noCheatDigStart(const v3s16 &p)
330 m_nocheat_dig_pos = p;
331 m_nocheat_dig_time = 0;
333 v3s16 getNoCheatDigPos()
335 return m_nocheat_dig_pos;
337 float getNoCheatDigTime()
339 return m_nocheat_dig_time;
343 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
345 LagPool& getDigPool()
349 // Returns true if cheated
350 bool checkMovementCheat();
354 void updatePrivileges(const std::set<std::string> &privs,
355 bool is_singleplayer)
358 m_is_singleplayer = is_singleplayer;
361 bool getCollisionBox(aabb3f *toset) const;
362 bool getSelectionBox(aabb3f *toset) const;
363 bool collideWithObjects() const { return true; }
365 void finalize(RemotePlayer *player, const std::set<std::string> &privs);
367 v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
368 v3f getEyeOffset() const;
371 std::string getPropertyPacket();
372 void unlinkPlayerSessionAndSave();
374 RemotePlayer *m_player = nullptr;
376 Inventory *m_inventory = nullptr;
382 v3f m_last_good_position;
383 float m_time_from_last_teleport = 0.0f;
384 float m_time_from_last_punch = 0.0f;
385 v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
386 float m_nocheat_dig_time = 0.0f;
389 IntervalLimiter m_breathing_interval;
390 IntervalLimiter m_drowning_interval;
391 IntervalLimiter m_node_hurt_interval;
393 int m_wield_index = 0;
394 bool m_position_not_sent = false;
396 // Cached privileges for enforcement
397 std::set<std::string> m_privs;
398 bool m_is_singleplayer;
400 u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
403 s16 m_wanted_range = 0.0f;
405 PlayerAttributes m_extra_attributes;
406 bool m_extended_attributes_modified = false;
408 float m_physics_override_speed = 1.0f;
409 float m_physics_override_jump = 1.0f;
410 float m_physics_override_gravity = 1.0f;
411 bool m_physics_override_sneak = true;
412 bool m_physics_override_sneak_glitch = false;
413 bool m_physics_override_new_move = true;
414 bool m_physics_override_sent = false;