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