Performance fix + SAO factorization
[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
28 class UnitSAO: public ServerActiveObject
29 {
30 public:
31         UnitSAO(ServerEnvironment *env, v3f pos);
32         virtual ~UnitSAO() {}
33
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; }
37         // Deprecated
38         f32 getRadYawDep() const { return (m_yaw + 90.) * core::DEGTORAD; }
39
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; }
43 protected:
44         s16 m_hp;
45         float m_yaw;
46
47         bool m_properties_sent;
48         struct ObjectProperties m_prop;
49
50         ItemGroupList m_armor_groups;
51         bool m_armor_groups_sent;
52
53         v2f m_animation_range;
54         float m_animation_speed;
55         float m_animation_blend;
56         bool m_animation_loop;
57         bool m_animation_sent;
58
59         // Stores position and rotation for each bone name
60         UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
61         bool m_bone_position_sent;
62
63         int m_attachment_parent_id;
64         UNORDERED_SET<int> m_attachment_child_ids;
65         std::string m_attachment_bone;
66         v3f m_attachment_position;
67         v3f m_attachment_rotation;
68         bool m_attachment_sent;
69 };
70
71 /*
72         LuaEntitySAO needs some internals exposed.
73 */
74
75 class LuaEntitySAO : public UnitSAO
76 {
77 public:
78         LuaEntitySAO(ServerEnvironment *env, v3f pos,
79                      const std::string &name, const std::string &state);
80         ~LuaEntitySAO();
81         ActiveObjectType getType() const
82         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
83         ActiveObjectType getSendType() const
84         { return ACTIVEOBJECT_TYPE_GENERIC; }
85         virtual void addedToEnvironment(u32 dtime_s);
86         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
87                         const std::string &data);
88         bool isAttached();
89         void step(float dtime, bool send_recommended);
90         std::string getClientInitializationData(u16 protocol_version);
91         std::string getStaticData();
92         int punch(v3f dir,
93                         const ToolCapabilities *toolcap=NULL,
94                         ServerActiveObject *puncher=NULL,
95                         float time_from_last_punch=1000000);
96         void rightClick(ServerActiveObject *clicker);
97         void setPos(const v3f &pos);
98         void moveTo(v3f pos, bool continuous);
99         float getMinimumSavedMovement();
100         std::string getDescription();
101         void setHP(s16 hp);
102         s16 getHP() const;
103         void setArmorGroups(const ItemGroupList &armor_groups);
104         const ItemGroupList &getArmorGroups();
105         void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
106         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
107         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
108         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
109         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
110         void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
111         void addAttachmentChild(int child_id);
112         void removeAttachmentChild(int child_id);
113         const UNORDERED_SET<int> &getAttachmentChildIds();
114         ObjectProperties* accessObjectProperties();
115         void notifyObjectPropertiesModified();
116         /* LuaEntitySAO-specific */
117         void setVelocity(v3f velocity);
118         v3f getVelocity();
119         void setAcceleration(v3f acceleration);
120         v3f getAcceleration();
121
122         void setTextureMod(const std::string &mod);
123         void setSprite(v2s16 p, int num_frames, float framelength,
124                         bool select_horiz_by_yawpitch);
125         std::string getName();
126         bool getCollisionBox(aabb3f *toset) const;
127         bool collideWithObjects() const;
128 private:
129         std::string getPropertyPacket();
130         void sendPosition(bool do_interpolate, bool is_movement_end);
131
132         std::string m_init_name;
133         std::string m_init_state;
134         bool m_registered;
135
136         v3f m_velocity;
137         v3f m_acceleration;
138
139         float m_last_sent_yaw;
140         v3f m_last_sent_position;
141         v3f m_last_sent_velocity;
142         float m_last_sent_position_timer;
143         float m_last_sent_move_precision;
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         void setFov(const float pitch);
218         f32 getFov() const { return m_fov; }
219         void setWantedRange(const s16 range);
220         s16 getWantedRange() const { return m_wanted_range; }
221
222         /*
223                 Interaction interface
224         */
225
226         int punch(v3f dir,
227                 const ToolCapabilities *toolcap,
228                 ServerActiveObject *puncher,
229                 float time_from_last_punch);
230         void rightClick(ServerActiveObject *clicker);
231         void setHP(s16 hp);
232         void setHPRaw(s16 hp) { m_hp = hp; }
233         s16 readDamage();
234         u16 getBreath() const { return m_breath; }
235         void setBreath(const u16 breath, bool send = true);
236         void setArmorGroups(const ItemGroupList &armor_groups);
237         const ItemGroupList &getArmorGroups();
238         void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
239         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
240         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
241         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
242         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
243         void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
244         void addAttachmentChild(int child_id);
245         void removeAttachmentChild(int child_id);
246         const UNORDERED_SET<int> &getAttachmentChildIds();
247         ObjectProperties* accessObjectProperties();
248         void notifyObjectPropertiesModified();
249
250         /*
251                 Inventory interface
252         */
253
254         Inventory* getInventory();
255         const Inventory* getInventory() const;
256         InventoryLocation getInventoryLocation() const;
257         std::string getWieldList() const;
258         ItemStack getWieldedItem() const;
259         bool setWieldedItem(const ItemStack &item);
260         int getWieldIndex() const;
261         void setWieldIndex(int i);
262
263         /*
264                 PlayerSAO-specific
265         */
266
267         void disconnected();
268
269         RemotePlayer *getPlayer() { return m_player; }
270         u16 getPeerID() const { return m_peer_id; }
271
272         // Cheat prevention
273
274         v3f getLastGoodPosition() const
275         {
276                 return m_last_good_position;
277         }
278         float resetTimeFromLastPunch()
279         {
280                 float r = m_time_from_last_punch;
281                 m_time_from_last_punch = 0.0;
282                 return r;
283         }
284         void noCheatDigStart(v3s16 p)
285         {
286                 m_nocheat_dig_pos = p;
287                 m_nocheat_dig_time = 0;
288         }
289         v3s16 getNoCheatDigPos()
290         {
291                 return m_nocheat_dig_pos;
292         }
293         float getNoCheatDigTime()
294         {
295                 return m_nocheat_dig_time;
296         }
297         void noCheatDigEnd()
298         {
299                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
300         }
301         LagPool& getDigPool()
302         {
303                 return m_dig_pool;
304         }
305         // Returns true if cheated
306         bool checkMovementCheat();
307
308         // Other
309
310         void updatePrivileges(const std::set<std::string> &privs,
311                         bool is_singleplayer)
312         {
313                 m_privs = privs;
314                 m_is_singleplayer = is_singleplayer;
315         }
316
317         bool getCollisionBox(aabb3f *toset) const;
318         bool collideWithObjects() const { return true; }
319
320         void initialize(RemotePlayer *player, const std::set<std::string> &privs);
321
322         v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
323         v3f getEyeOffset() const;
324
325 private:
326         std::string getPropertyPacket();
327         void unlinkPlayerSessionAndSave();
328
329         RemotePlayer *m_player;
330         u16 m_peer_id;
331         Inventory *m_inventory;
332         s16 m_damage;
333
334         // Cheat prevention
335         LagPool m_dig_pool;
336         LagPool m_move_pool;
337         v3f m_last_good_position;
338         float m_time_from_last_punch;
339         v3s16 m_nocheat_dig_pos;
340         float m_nocheat_dig_time;
341
342         // Timers
343         IntervalLimiter m_breathing_interval;
344         IntervalLimiter m_drowning_interval;
345
346         int m_wield_index;
347         bool m_position_not_sent;
348
349         // Cached privileges for enforcement
350         std::set<std::string> m_privs;
351         bool m_is_singleplayer;
352
353         u16 m_breath;
354         f32 m_pitch;
355         f32 m_fov;
356         s16 m_wanted_range;
357 public:
358         float m_physics_override_speed;
359         float m_physics_override_jump;
360         float m_physics_override_gravity;
361         bool m_physics_override_sneak;
362         bool m_physics_override_sneak_glitch;
363         bool m_physics_override_sent;
364 };
365
366 #endif