LocalPlayer::accelerateHorizontal: cleanups
[oweals/minetest.git] / src / content_sao.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef CONTENT_SAO_HEADER
21 #define CONTENT_SAO_HEADER
22
23 #include <util/numeric.h>
24 #include "serverobject.h"
25 #include "itemgroup.h"
26 #include "object_properties.h"
27 #include "constants.h"
28
29 class UnitSAO: public ServerActiveObject
30 {
31 public:
32         UnitSAO(ServerEnvironment *env, v3f pos);
33         virtual ~UnitSAO() {}
34
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; }
38         // Deprecated
39         f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
40
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; }
44
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 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();
59 protected:
60         s16 m_hp = -1;
61         float m_yaw = 0.0f;
62
63         bool m_properties_sent = true;
64         struct ObjectProperties m_prop;
65
66         ItemGroupList m_armor_groups;
67         bool m_armor_groups_sent = false;
68
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
75         // Stores position and rotation for each bone name
76         std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
77         bool m_bone_position_sent = false;
78
79         int m_attachment_parent_id = 0;
80         std::unordered_set<int> m_attachment_child_ids;
81         std::string m_attachment_bone = "";
82         v3f m_attachment_position;
83         v3f m_attachment_rotation;
84         bool m_attachment_sent = false;
85 };
86
87 /*
88         LuaEntitySAO needs some internals exposed.
89 */
90
91 class LuaEntitySAO : public UnitSAO
92 {
93 public:
94         LuaEntitySAO(ServerEnvironment *env, v3f pos,
95                      const std::string &name, const std::string &state);
96         ~LuaEntitySAO();
97         ActiveObjectType getType() const
98         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
99         ActiveObjectType getSendType() const
100         { return ACTIVEOBJECT_TYPE_GENERIC; }
101         virtual void addedToEnvironment(u32 dtime_s);
102         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
103                         const std::string &data);
104         void step(float dtime, bool send_recommended);
105         std::string getClientInitializationData(u16 protocol_version);
106         void getStaticData(std::string *result) const;
107         int punch(v3f dir,
108                         const ToolCapabilities *toolcap=NULL,
109                         ServerActiveObject *puncher=NULL,
110                         float time_from_last_punch=1000000);
111         void rightClick(ServerActiveObject *clicker);
112         void setPos(const v3f &pos);
113         void moveTo(v3f pos, bool continuous);
114         float getMinimumSavedMovement();
115         std::string getDescription();
116         void setHP(s16 hp);
117         s16 getHP() const;
118         /* LuaEntitySAO-specific */
119         void setVelocity(v3f velocity);
120         v3f getVelocity();
121         void setAcceleration(v3f acceleration);
122         v3f getAcceleration();
123
124         void setTextureMod(const std::string &mod);
125         std::string getTextureMod() const;
126         void setSprite(v2s16 p, int num_frames, float framelength,
127                         bool select_horiz_by_yawpitch);
128         std::string getName();
129         bool getCollisionBox(aabb3f *toset) const;
130         bool getSelectionBox(aabb3f *toset) const;
131         bool collideWithObjects() const;
132 private:
133         std::string getPropertyPacket();
134         void sendPosition(bool do_interpolate, bool is_movement_end);
135
136         std::string m_init_name;
137         std::string m_init_state;
138         bool m_registered = false;
139
140         v3f m_velocity;
141         v3f m_acceleration;
142
143         float m_last_sent_yaw = 0.0f;
144         v3f m_last_sent_position;
145         v3f m_last_sent_velocity;
146         float m_last_sent_position_timer = 0.0f;
147         float m_last_sent_move_precision = 0.0f;
148         std::string m_current_texture_modifier = "";
149 };
150
151 /*
152         PlayerSAO needs some internals exposed.
153 */
154
155 class LagPool
156 {
157         float m_pool = 15.0f;
158         float m_max = 15.0f;
159 public:
160         LagPool() {}
161
162         void setMax(float new_max)
163         {
164                 m_max = new_max;
165                 if(m_pool > new_max)
166                         m_pool = new_max;
167         }
168
169         void add(float dtime)
170         {
171                 m_pool -= dtime;
172                 if(m_pool < 0)
173                         m_pool = 0;
174         }
175
176         void empty()
177         {
178                 m_pool = m_max;
179         }
180
181         bool grab(float dtime)
182         {
183                 if(dtime <= 0)
184                         return true;
185                 if(m_pool + dtime > m_max)
186                         return false;
187                 m_pool += dtime;
188                 return true;
189         }
190 };
191
192 typedef std::unordered_map<std::string, std::string> PlayerAttributes;
193 class RemotePlayer;
194
195 class PlayerSAO : public UnitSAO
196 {
197 public:
198         PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, u16 peer_id_, bool is_singleplayer);
199         ~PlayerSAO();
200         ActiveObjectType getType() const
201         { return ACTIVEOBJECT_TYPE_PLAYER; }
202         ActiveObjectType getSendType() const
203         { return ACTIVEOBJECT_TYPE_GENERIC; }
204         std::string getDescription();
205
206         /*
207                 Active object <-> environment interface
208         */
209
210         void addedToEnvironment(u32 dtime_s);
211         void removingFromEnvironment();
212         bool isStaticAllowed() const { return false; }
213         std::string getClientInitializationData(u16 protocol_version);
214         void getStaticData(std::string *result) const;
215         void step(float dtime, bool send_recommended);
216         void setBasePosition(const v3f &position);
217         void setPos(const v3f &pos);
218         void moveTo(v3f pos, bool continuous);
219         void setYaw(const float yaw);
220         // Data should not be sent at player initialization
221         void setYawAndSend(const float yaw);
222         void setPitch(const float pitch);
223         // Data should not be sent at player initialization
224         void setPitchAndSend(const float pitch);
225         f32 getPitch() const { return m_pitch; }
226         f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
227         // Deprecated
228         f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
229         void setFov(const float pitch);
230         f32 getFov() const { return m_fov; }
231         void setWantedRange(const s16 range);
232         s16 getWantedRange() const { return m_wanted_range; }
233
234         /*
235                 Interaction interface
236         */
237
238         int punch(v3f dir,
239                 const ToolCapabilities *toolcap,
240                 ServerActiveObject *puncher,
241                 float time_from_last_punch);
242         void rightClick(ServerActiveObject *clicker) {}
243         void setHP(s16 hp);
244         void setHPRaw(s16 hp) { m_hp = hp; }
245         s16 readDamage();
246         u16 getBreath() const { return m_breath; }
247         void setBreath(const u16 breath, bool send = true);
248
249         /*
250                 Inventory interface
251         */
252
253         Inventory* getInventory();
254         const Inventory* getInventory() const;
255         InventoryLocation getInventoryLocation() const;
256         std::string getWieldList() const;
257         ItemStack getWieldedItem() const;
258         ItemStack getWieldedItemOrHand() const;
259         bool setWieldedItem(const ItemStack &item);
260         int getWieldIndex() const;
261         void setWieldIndex(int i);
262
263         /*
264                 Modding interface
265         */
266         inline void setExtendedAttribute(const std::string &attr, const std::string &value)
267         {
268                 m_extra_attributes[attr] = value;
269                 m_extended_attributes_modified = true;
270         }
271
272         inline bool getExtendedAttribute(const std::string &attr, std::string *value)
273         {
274                 if (m_extra_attributes.find(attr) == m_extra_attributes.end())
275                         return false;
276
277                 *value = m_extra_attributes[attr];
278                 return true;
279         }
280
281         inline void removeExtendedAttribute(const std::string &attr)
282         {
283                 PlayerAttributes::iterator it = m_extra_attributes.find(attr);
284                 if (it == m_extra_attributes.end())
285                         return;
286
287                 m_extra_attributes.erase(it);
288                 m_extended_attributes_modified = true;
289         }
290
291         inline const PlayerAttributes &getExtendedAttributes()
292         {
293                 return m_extra_attributes;
294         }
295
296         inline bool extendedAttributesModified() const
297         {
298                 return m_extended_attributes_modified;
299         }
300
301         inline void setExtendedAttributeModified(bool v)
302         {
303                 m_extended_attributes_modified = v;
304         }
305
306         /*
307                 PlayerSAO-specific
308         */
309
310         void disconnected();
311
312         RemotePlayer *getPlayer() { return m_player; }
313         u16 getPeerID() const { return m_peer_id; }
314
315         // Cheat prevention
316
317         v3f getLastGoodPosition() const
318         {
319                 return m_last_good_position;
320         }
321         float resetTimeFromLastPunch()
322         {
323                 float r = m_time_from_last_punch;
324                 m_time_from_last_punch = 0.0;
325                 return r;
326         }
327         void noCheatDigStart(v3s16 p)
328         {
329                 m_nocheat_dig_pos = p;
330                 m_nocheat_dig_time = 0;
331         }
332         v3s16 getNoCheatDigPos()
333         {
334                 return m_nocheat_dig_pos;
335         }
336         float getNoCheatDigTime()
337         {
338                 return m_nocheat_dig_time;
339         }
340         void noCheatDigEnd()
341         {
342                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
343         }
344         LagPool& getDigPool()
345         {
346                 return m_dig_pool;
347         }
348         // Returns true if cheated
349         bool checkMovementCheat();
350
351         // Other
352
353         void updatePrivileges(const std::set<std::string> &privs,
354                         bool is_singleplayer)
355         {
356                 m_privs = privs;
357                 m_is_singleplayer = is_singleplayer;
358         }
359
360         bool getCollisionBox(aabb3f *toset) const;
361         bool getSelectionBox(aabb3f *toset) const;
362         bool collideWithObjects() const { return true; }
363
364         void finalize(RemotePlayer *player, const std::set<std::string> &privs);
365
366         v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
367         v3f getEyeOffset() const;
368
369 private:
370         std::string getPropertyPacket();
371         void unlinkPlayerSessionAndSave();
372
373         RemotePlayer *m_player = nullptr;
374         u16 m_peer_id = 0;
375         Inventory *m_inventory = nullptr;
376         s16 m_damage = 0;
377
378         // Cheat prevention
379         LagPool m_dig_pool;
380         LagPool m_move_pool;
381         v3f m_last_good_position;
382         float m_time_from_last_teleport = 0.0f;
383         float m_time_from_last_punch = 0.0f;
384         v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
385         float m_nocheat_dig_time = 0.0f;
386
387         // Timers
388         IntervalLimiter m_breathing_interval;
389         IntervalLimiter m_drowning_interval;
390         IntervalLimiter m_node_hurt_interval;
391
392         int m_wield_index = 0;
393         bool m_position_not_sent = false;
394
395         // Cached privileges for enforcement
396         std::set<std::string> m_privs;
397         bool m_is_singleplayer;
398
399         u16 m_breath = PLAYER_MAX_BREATH;
400         f32 m_pitch = 0.0f;
401         f32 m_fov = 0.0f;
402         s16 m_wanted_range = 0.0f;
403
404         PlayerAttributes m_extra_attributes;
405         bool m_extended_attributes_modified = false;
406 public:
407         float m_physics_override_speed = 1.0f;
408         float m_physics_override_jump = 1.0f;
409         float m_physics_override_gravity = 1.0f;
410         bool m_physics_override_sneak = true;
411         bool m_physics_override_sneak_glitch = false;
412         bool m_physics_override_new_move = true;
413         bool m_physics_override_sent = false;
414 };
415
416 #endif