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