Detach the player from entities on death. (#5077)
[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 detachFromParent();
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 UNORDERED_SET<int> &getAttachmentChildIds();
57         ObjectProperties* accessObjectProperties();
58         void notifyObjectPropertiesModified();
59 protected:
60         s16 m_hp;
61         float m_yaw;
62
63         bool m_properties_sent;
64         struct ObjectProperties m_prop;
65
66         ItemGroupList m_armor_groups;
67         bool m_armor_groups_sent;
68
69         v2f m_animation_range;
70         float m_animation_speed;
71         float m_animation_blend;
72         bool m_animation_loop;
73         bool m_animation_sent;
74
75         // Stores position and rotation for each bone name
76         UNORDERED_MAP<std::string, core::vector2d<v3f> > m_bone_position;
77         bool m_bone_position_sent;
78
79         int m_attachment_parent_id;
80         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;
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         void setSprite(v2s16 p, int num_frames, float framelength,
126                         bool select_horiz_by_yawpitch);
127         std::string getName();
128         bool getCollisionBox(aabb3f *toset) const;
129         bool collideWithObjects() const;
130 private:
131         std::string getPropertyPacket();
132         void sendPosition(bool do_interpolate, bool is_movement_end);
133
134         std::string m_init_name;
135         std::string m_init_state;
136         bool m_registered;
137
138         v3f m_velocity;
139         v3f m_acceleration;
140
141         float m_last_sent_yaw;
142         v3f m_last_sent_position;
143         v3f m_last_sent_velocity;
144         float m_last_sent_position_timer;
145         float m_last_sent_move_precision;
146 };
147
148 /*
149         PlayerSAO needs some internals exposed.
150 */
151
152 class LagPool
153 {
154         float m_pool;
155         float m_max;
156 public:
157         LagPool(): m_pool(15), m_max(15)
158         {}
159         void setMax(float new_max)
160         {
161                 m_max = new_max;
162                 if(m_pool > new_max)
163                         m_pool = new_max;
164         }
165         void add(float dtime)
166         {
167                 m_pool -= dtime;
168                 if(m_pool < 0)
169                         m_pool = 0;
170         }
171         bool grab(float dtime)
172         {
173                 if(dtime <= 0)
174                         return true;
175                 if(m_pool + dtime > m_max)
176                         return false;
177                 m_pool += dtime;
178                 return true;
179         }
180 };
181
182 class RemotePlayer;
183
184 class PlayerSAO : public UnitSAO
185 {
186 public:
187         PlayerSAO(ServerEnvironment *env_, u16 peer_id_, bool is_singleplayer);
188         ~PlayerSAO();
189         ActiveObjectType getType() const
190         { return ACTIVEOBJECT_TYPE_PLAYER; }
191         ActiveObjectType getSendType() const
192         { return ACTIVEOBJECT_TYPE_GENERIC; }
193         std::string getDescription();
194
195         /*
196                 Active object <-> environment interface
197         */
198
199         void addedToEnvironment(u32 dtime_s);
200         void removingFromEnvironment();
201         bool isStaticAllowed() const { return false; }
202         std::string getClientInitializationData(u16 protocol_version);
203         void getStaticData(std::string *result) const;
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
238         /*
239                 Inventory interface
240         */
241
242         Inventory* getInventory();
243         const Inventory* getInventory() const;
244         InventoryLocation getInventoryLocation() const;
245         std::string getWieldList() const;
246         ItemStack getWieldedItem() const;
247         bool setWieldedItem(const ItemStack &item);
248         int getWieldIndex() const;
249         void setWieldIndex(int i);
250
251         /*
252                 PlayerSAO-specific
253         */
254
255         void disconnected();
256
257         RemotePlayer *getPlayer() { return m_player; }
258         u16 getPeerID() const { return m_peer_id; }
259
260         // Cheat prevention
261
262         v3f getLastGoodPosition() const
263         {
264                 return m_last_good_position;
265         }
266         float resetTimeFromLastPunch()
267         {
268                 float r = m_time_from_last_punch;
269                 m_time_from_last_punch = 0.0;
270                 return r;
271         }
272         void noCheatDigStart(v3s16 p)
273         {
274                 m_nocheat_dig_pos = p;
275                 m_nocheat_dig_time = 0;
276         }
277         v3s16 getNoCheatDigPos()
278         {
279                 return m_nocheat_dig_pos;
280         }
281         float getNoCheatDigTime()
282         {
283                 return m_nocheat_dig_time;
284         }
285         void noCheatDigEnd()
286         {
287                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
288         }
289         LagPool& getDigPool()
290         {
291                 return m_dig_pool;
292         }
293         // Returns true if cheated
294         bool checkMovementCheat();
295
296         // Other
297
298         void updatePrivileges(const std::set<std::string> &privs,
299                         bool is_singleplayer)
300         {
301                 m_privs = privs;
302                 m_is_singleplayer = is_singleplayer;
303         }
304
305         bool getCollisionBox(aabb3f *toset) const;
306         bool collideWithObjects() const { return true; }
307
308         void initialize(RemotePlayer *player, const std::set<std::string> &privs);
309
310         v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
311         v3f getEyeOffset() const;
312
313 private:
314         std::string getPropertyPacket();
315         void unlinkPlayerSessionAndSave();
316
317         RemotePlayer *m_player;
318         u16 m_peer_id;
319         Inventory *m_inventory;
320         s16 m_damage;
321
322         // Cheat prevention
323         LagPool m_dig_pool;
324         LagPool m_move_pool;
325         v3f m_last_good_position;
326         float m_time_from_last_punch;
327         v3s16 m_nocheat_dig_pos;
328         float m_nocheat_dig_time;
329
330         // Timers
331         IntervalLimiter m_breathing_interval;
332         IntervalLimiter m_drowning_interval;
333
334         int m_wield_index;
335         bool m_position_not_sent;
336
337         // Cached privileges for enforcement
338         std::set<std::string> m_privs;
339         bool m_is_singleplayer;
340
341         u16 m_breath;
342         f32 m_pitch;
343         f32 m_fov;
344         s16 m_wanted_range;
345 public:
346         float m_physics_override_speed;
347         float m_physics_override_jump;
348         float m_physics_override_gravity;
349         bool m_physics_override_sneak;
350         bool m_physics_override_sneak_glitch;
351         bool m_physics_override_sent;
352 };
353
354 #endif