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