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