]> git.lizzy.rs Git - minetest.git/blob - src/content_sao.h
2417880660df13659995a384494b492019181856
[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 "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);
63         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend);
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         ObjectProperties* accessObjectProperties();
69         void notifyObjectPropertiesModified();
70         /* LuaEntitySAO-specific */
71         void setVelocity(v3f velocity);
72         v3f getVelocity();
73         void setAcceleration(v3f acceleration);
74         v3f getAcceleration();
75         void setYaw(float yaw);
76         float getYaw();
77         void setTextureMod(const std::string &mod);
78         void setSprite(v2s16 p, int num_frames, float framelength,
79                         bool select_horiz_by_yawpitch);
80         std::string getName();
81         bool getCollisionBox(aabb3f *toset);
82         bool collideWithObjects();
83 private:
84         std::string getPropertyPacket();
85         void sendPosition(bool do_interpolate, bool is_movement_end);
86
87         std::string m_init_name;
88         std::string m_init_state;
89         bool m_registered;
90         struct ObjectProperties m_prop;
91         
92         s16 m_hp;
93         v3f m_velocity;
94         v3f m_acceleration;
95         float m_yaw;
96         ItemGroupList m_armor_groups;
97         
98         bool m_properties_sent;
99         float m_last_sent_yaw;
100         v3f m_last_sent_position;
101         v3f m_last_sent_velocity;
102         float m_last_sent_position_timer;
103         float m_last_sent_move_precision;
104         bool m_armor_groups_sent;
105
106         v2f m_animation_range;
107         float m_animation_speed;
108         float m_animation_blend;
109         bool m_animation_sent;
110
111         std::map<std::string, core::vector2d<v3f> > m_bone_position;
112         bool m_bone_position_sent;
113
114         int m_attachment_parent_id;
115         std::string m_attachment_bone;
116         v3f m_attachment_position;
117         v3f m_attachment_rotation;
118         bool m_attachment_sent;
119 };
120
121 /*
122         PlayerSAO needs some internals exposed.
123 */
124
125 class LagPool
126 {
127         float m_pool;
128         float m_max;
129 public:
130         LagPool(): m_pool(15), m_max(15)
131         {}
132         void setMax(float new_max)
133         {
134                 m_max = new_max;
135                 if(m_pool > new_max)
136                         m_pool = new_max;
137         }
138         void add(float dtime)
139         {
140                 m_pool -= dtime;
141                 if(m_pool < 0)
142                         m_pool = 0;
143         }
144         bool grab(float dtime)
145         {
146                 if(dtime <= 0)
147                         return true;
148                 if(m_pool + dtime > m_max)
149                         return false;
150                 m_pool += dtime;
151                 return true;
152         }
153 };
154
155 class PlayerSAO : public ServerActiveObject
156 {
157 public:
158         PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
159                         const std::set<std::string> &privs, bool is_singleplayer);
160         ~PlayerSAO();
161         ActiveObjectType getType() const
162         { return ACTIVEOBJECT_TYPE_PLAYER; }
163         ActiveObjectType getSendType() const
164         { return ACTIVEOBJECT_TYPE_GENERIC; }
165         std::string getDescription();
166
167         /*
168                 Active object <-> environment interface
169         */
170
171         void addedToEnvironment(u32 dtime_s);
172         void removingFromEnvironment();
173         bool isStaticAllowed() const;
174         std::string getClientInitializationData(u16 protocol_version);
175         std::string getStaticData();
176         bool isAttached();
177         void step(float dtime, bool send_recommended);
178         void setBasePosition(const v3f &position);
179         void setPos(v3f pos);
180         void moveTo(v3f pos, bool continuous);
181         void setYaw(float);
182         void setPitch(float);
183
184         /*
185                 Interaction interface
186         */
187
188         int punch(v3f dir,
189                 const ToolCapabilities *toolcap,
190                 ServerActiveObject *puncher,
191                 float time_from_last_punch);
192         void rightClick(ServerActiveObject *clicker);
193         s16 getHP() const;
194         void setHP(s16 hp);
195         s16 readDamage();
196         u16 getBreath() const;
197         void setBreath(u16 breath);
198         void setArmorGroups(const ItemGroupList &armor_groups);
199         ItemGroupList getArmorGroups();
200         void setAnimation(v2f frame_range, float frame_speed, float frame_blend);
201         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend);
202         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
203         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
204         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
205         void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
206         ObjectProperties* accessObjectProperties();
207         void notifyObjectPropertiesModified();
208         void setNametagColor(video::SColor color);
209         video::SColor getNametagColor();
210
211         /*
212                 Inventory interface
213         */
214
215         Inventory* getInventory();
216         const Inventory* getInventory() const;
217         InventoryLocation getInventoryLocation() const;
218         std::string getWieldList() const;
219         int getWieldIndex() const;
220         void setWieldIndex(int i);
221
222         /*
223                 PlayerSAO-specific
224         */
225
226         void disconnected();
227
228         Player* getPlayer()
229         {
230                 return m_player;
231         }
232         u16 getPeerID() const
233         {
234                 return m_peer_id;
235         }
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         Player *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_sent;
316
317         std::map<std::string, core::vector2d<v3f> > m_bone_position; // Stores position and rotation for each bone name
318         bool m_bone_position_sent;
319
320         int m_attachment_parent_id;
321         std::string m_attachment_bone;
322         v3f m_attachment_position;
323         v3f m_attachment_rotation;
324         bool m_attachment_sent;
325
326         video::SColor m_nametag_color;
327         bool m_nametag_sent;
328
329 public:
330         float m_physics_override_speed;
331         float m_physics_override_jump;
332         float m_physics_override_gravity;
333         bool m_physics_override_sneak;
334         bool m_physics_override_sneak_glitch;
335         bool m_physics_override_sent;
336 };
337
338 #endif
339