Breath cheat fix: server side
[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                         ServerActiveObject(env, pos),
34                         m_hp(-1), m_yaw(0) {}
35         virtual ~UnitSAO() {}
36
37         virtual void setYaw(const float yaw) { m_yaw = yaw; }
38         float getYaw() const { return m_yaw; };
39         f32 getRadYaw() const { return m_yaw * core::DEGTORAD; }
40         // Deprecated
41         f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
42
43         s16 getHP() const { return m_hp; }
44         // Use a function, if isDead can be defined by other conditions
45         bool isDead() const { return m_hp == 0; }
46 protected:
47         s16 m_hp;
48         float m_yaw;
49 };
50
51 /*
52         LuaEntitySAO needs some internals exposed.
53 */
54
55 class LuaEntitySAO : public UnitSAO
56 {
57 public:
58         LuaEntitySAO(ServerEnvironment *env, v3f pos,
59                      const std::string &name, const std::string &state);
60         ~LuaEntitySAO();
61         ActiveObjectType getType() const
62         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
63         ActiveObjectType getSendType() const
64         { return ACTIVEOBJECT_TYPE_GENERIC; }
65         virtual void addedToEnvironment(u32 dtime_s);
66         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
67                         const std::string &data);
68         bool isAttached();
69         void step(float dtime, bool send_recommended);
70         std::string getClientInitializationData(u16 protocol_version);
71         std::string getStaticData();
72         int punch(v3f dir,
73                         const ToolCapabilities *toolcap=NULL,
74                         ServerActiveObject *puncher=NULL,
75                         float time_from_last_punch=1000000);
76         void rightClick(ServerActiveObject *clicker);
77         void setPos(const v3f &pos);
78         void moveTo(v3f pos, bool continuous);
79         float getMinimumSavedMovement();
80         std::string getDescription();
81         void setHP(s16 hp);
82         s16 getHP() const;
83         void setArmorGroups(const ItemGroupList &armor_groups);
84         ItemGroupList getArmorGroups();
85         void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
86         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
87         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
88         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
89         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
90         void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
91         void addAttachmentChild(int child_id);
92         void removeAttachmentChild(int child_id);
93         UNORDERED_SET<int> getAttachmentChildIds();
94         ObjectProperties* accessObjectProperties();
95         void notifyObjectPropertiesModified();
96         /* LuaEntitySAO-specific */
97         void setVelocity(v3f velocity);
98         v3f getVelocity();
99         void setAcceleration(v3f acceleration);
100         v3f getAcceleration();
101
102         void setTextureMod(const std::string &mod);
103         void setSprite(v2s16 p, int num_frames, float framelength,
104                         bool select_horiz_by_yawpitch);
105         std::string getName();
106         bool getCollisionBox(aabb3f *toset);
107         bool collideWithObjects();
108 private:
109         std::string getPropertyPacket();
110         void sendPosition(bool do_interpolate, bool is_movement_end);
111
112         std::string m_init_name;
113         std::string m_init_state;
114         bool m_registered;
115         struct ObjectProperties m_prop;
116
117         v3f m_velocity;
118         v3f m_acceleration;
119
120         ItemGroupList m_armor_groups;
121
122         bool m_properties_sent;
123         float m_last_sent_yaw;
124         v3f m_last_sent_position;
125         v3f m_last_sent_velocity;
126         float m_last_sent_position_timer;
127         float m_last_sent_move_precision;
128         bool m_armor_groups_sent;
129
130         v2f m_animation_range;
131         float m_animation_speed;
132         float m_animation_blend;
133         bool m_animation_loop;
134         bool m_animation_sent;
135
136         UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
137         bool m_bone_position_sent;
138
139         int m_attachment_parent_id;
140         UNORDERED_SET<int> m_attachment_child_ids;
141         std::string m_attachment_bone;
142         v3f m_attachment_position;
143         v3f m_attachment_rotation;
144         bool m_attachment_sent;
145 };
146
147 /*
148         PlayerSAO needs some internals exposed.
149 */
150
151 class LagPool
152 {
153         float m_pool;
154         float m_max;
155 public:
156         LagPool(): m_pool(15), m_max(15)
157         {}
158         void setMax(float new_max)
159         {
160                 m_max = new_max;
161                 if(m_pool > new_max)
162                         m_pool = new_max;
163         }
164         void add(float dtime)
165         {
166                 m_pool -= dtime;
167                 if(m_pool < 0)
168                         m_pool = 0;
169         }
170         bool grab(float dtime)
171         {
172                 if(dtime <= 0)
173                         return true;
174                 if(m_pool + dtime > m_max)
175                         return false;
176                 m_pool += dtime;
177                 return true;
178         }
179 };
180
181 class RemotePlayer;
182
183 class PlayerSAO : public UnitSAO
184 {
185 public:
186         PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer);
187         ~PlayerSAO();
188         ActiveObjectType getType() const
189         { return ACTIVEOBJECT_TYPE_PLAYER; }
190         ActiveObjectType getSendType() const
191         { return ACTIVEOBJECT_TYPE_GENERIC; }
192         std::string getDescription();
193
194         /*
195                 Active object <-> environment interface
196         */
197
198         void addedToEnvironment(u32 dtime_s);
199         void removingFromEnvironment();
200         bool isStaticAllowed() const;
201         std::string getClientInitializationData(u16 protocol_version);
202         std::string getStaticData();
203         bool isAttached();
204         void step(float dtime, bool send_recommended);
205         void setBasePosition(const v3f &position);
206         void setPos(const v3f &pos);
207         void moveTo(v3f pos, bool continuous);
208         void setYaw(const float yaw);
209         // Data should not be sent at player initialization
210         void setYawAndSend(const float yaw);
211         void setPitch(const float pitch);
212         // Data should not be sent at player initialization
213         void setPitchAndSend(const float pitch);
214         f32 getPitch() const { return m_pitch; }
215         f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
216         // Deprecated
217         f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
218         void setFov(const float pitch);
219         f32 getFov() const { return m_fov; }
220         void setWantedRange(const s16 range);
221         s16 getWantedRange() const { return m_wanted_range; }
222
223         /*
224                 Interaction interface
225         */
226
227         int punch(v3f dir,
228                 const ToolCapabilities *toolcap,
229                 ServerActiveObject *puncher,
230                 float time_from_last_punch);
231         void rightClick(ServerActiveObject *clicker);
232         void setHP(s16 hp);
233         void setHPRaw(s16 hp) { m_hp = hp; }
234         s16 readDamage();
235         u16 getBreath() const { return m_breath; }
236         void setBreath(const u16 breath, bool send = true);
237         void setArmorGroups(const ItemGroupList &armor_groups);
238         ItemGroupList getArmorGroups();
239         void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
240         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
241         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
242         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
243         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
244         void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
245         void addAttachmentChild(int child_id);
246         void removeAttachmentChild(int child_id);
247         UNORDERED_SET<int> getAttachmentChildIds();
248         ObjectProperties* accessObjectProperties();
249         void notifyObjectPropertiesModified();
250
251         /*
252                 Inventory interface
253         */
254
255         Inventory* getInventory();
256         const Inventory* getInventory() const;
257         InventoryLocation getInventoryLocation() const;
258         std::string getWieldList() const;
259         ItemStack getWieldedItem() const;
260         bool setWieldedItem(const ItemStack &item);
261         int getWieldIndex() const;
262         void setWieldIndex(int i);
263
264         /*
265                 PlayerSAO-specific
266         */
267
268         void disconnected();
269
270         RemotePlayer *getPlayer() { return m_player; }
271         u16 getPeerID() const { return m_peer_id; }
272
273         // Cheat prevention
274
275         v3f getLastGoodPosition() const
276         {
277                 return m_last_good_position;
278         }
279         float resetTimeFromLastPunch()
280         {
281                 float r = m_time_from_last_punch;
282                 m_time_from_last_punch = 0.0;
283                 return r;
284         }
285         void noCheatDigStart(v3s16 p)
286         {
287                 m_nocheat_dig_pos = p;
288                 m_nocheat_dig_time = 0;
289         }
290         v3s16 getNoCheatDigPos()
291         {
292                 return m_nocheat_dig_pos;
293         }
294         float getNoCheatDigTime()
295         {
296                 return m_nocheat_dig_time;
297         }
298         void noCheatDigEnd()
299         {
300                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
301         }
302         LagPool& getDigPool()
303         {
304                 return m_dig_pool;
305         }
306         // Returns true if cheated
307         bool checkMovementCheat();
308
309         // Other
310
311         void updatePrivileges(const std::set<std::string> &privs,
312                         bool is_singleplayer)
313         {
314                 m_privs = privs;
315                 m_is_singleplayer = is_singleplayer;
316         }
317
318         bool getCollisionBox(aabb3f *toset);
319         bool collideWithObjects();
320
321         void initialize(RemotePlayer *player, const std::set<std::string> &privs);
322
323         v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
324         v3f getEyeOffset() const { return v3f(0, BS * 1.625f, 0); }
325
326 private:
327         std::string getPropertyPacket();
328         void unlinkPlayerSessionAndSave();
329
330         RemotePlayer *m_player;
331         u16 m_peer_id;
332         Inventory *m_inventory;
333         s16 m_damage;
334
335         // Cheat prevention
336         LagPool m_dig_pool;
337         LagPool m_move_pool;
338         v3f m_last_good_position;
339         float m_time_from_last_punch;
340         v3s16 m_nocheat_dig_pos;
341         float m_nocheat_dig_time;
342
343         // Timers
344         IntervalLimiter m_breathing_interval;
345         IntervalLimiter m_drowning_interval;
346
347         int m_wield_index;
348         bool m_position_not_sent;
349         ItemGroupList m_armor_groups;
350         bool m_armor_groups_sent;
351
352         bool m_properties_sent;
353         struct ObjectProperties m_prop;
354         // Cached privileges for enforcement
355         std::set<std::string> m_privs;
356         bool m_is_singleplayer;
357
358         v2f m_animation_range;
359         float m_animation_speed;
360         float m_animation_blend;
361         bool m_animation_loop;
362         bool m_animation_sent;
363
364         // Stores position and rotation for each bone name
365         UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
366         bool m_bone_position_sent;
367
368         int m_attachment_parent_id;
369         UNORDERED_SET<int> m_attachment_child_ids;
370         std::string m_attachment_bone;
371         v3f m_attachment_position;
372         v3f m_attachment_rotation;
373         bool m_attachment_sent;
374         u16 m_breath;
375         f32 m_pitch;
376         f32 m_fov;
377         s16 m_wanted_range;
378 public:
379         float m_physics_override_speed;
380         float m_physics_override_jump;
381         float m_physics_override_gravity;
382         bool m_physics_override_sneak;
383         bool m_physics_override_sneak_glitch;
384         bool m_physics_override_sent;
385 };
386
387 #endif