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