Fix sqlite3 map shutdown fails due to missing to finalize list statement
[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 "content_object.h"
25 #include "itemgroup.h"
26 #include "player.h"
27 #include "object_properties.h"
28
29 ServerActiveObject* createItemSAO(ServerEnvironment *env, v3f pos,
30                                   const std::string &itemstring);
31
32 /*
33         LuaEntitySAO needs some internals exposed.
34 */
35
36 class LuaEntitySAO : public ServerActiveObject
37 {
38 public:
39         LuaEntitySAO(ServerEnvironment *env, v3f pos,
40                      const std::string &name, const std::string &state);
41         ~LuaEntitySAO();
42         u8 getType() const
43         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
44         u8 getSendType() const
45         { return ACTIVEOBJECT_TYPE_GENERIC; }
46         virtual void addedToEnvironment(u32 dtime_s);
47         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
48                         const std::string &data);
49         bool isAttached();
50         void step(float dtime, bool send_recommended);
51         std::string getClientInitializationData(u16 protocol_version);
52         std::string getStaticData();
53         int punch(v3f dir,
54                         const ToolCapabilities *toolcap=NULL,
55                         ServerActiveObject *puncher=NULL,
56                         float time_from_last_punch=1000000);
57         void rightClick(ServerActiveObject *clicker);
58         void setPos(v3f pos);
59         void moveTo(v3f pos, bool continuous);
60         float getMinimumSavedMovement();
61         std::string getDescription();
62         void setHP(s16 hp);
63         s16 getHP() const;
64         void setArmorGroups(const ItemGroupList &armor_groups);
65         void setAnimation(v2f frame_range, float frame_speed, float frame_blend);
66         void setBonePosition(std::string bone, v3f position, v3f rotation);
67         void setAttachment(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         u8 getType() const
162         { return ACTIVEOBJECT_TYPE_PLAYER; }
163         u8 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         bool unlimitedTransferDistance() const;
175         std::string getClientInitializationData(u16 protocol_version);
176         std::string getStaticData();
177         bool isAttached();
178         void step(float dtime, bool send_recommended);
179         void setBasePosition(const v3f &position);
180         void setPos(v3f pos);
181         void moveTo(v3f pos, bool continuous);
182         void setYaw(float);
183         void setPitch(float);
184
185         /*
186                 Interaction interface
187         */
188
189         int punch(v3f dir,
190                 const ToolCapabilities *toolcap,
191                 ServerActiveObject *puncher,
192                 float time_from_last_punch);
193         void rightClick(ServerActiveObject *clicker);
194         s16 getHP() const;
195         void setHP(s16 hp);
196         s16 readDamage();
197         u16 getBreath() const;
198         void setBreath(u16 breath);
199         void setArmorGroups(const ItemGroupList &armor_groups);
200         void setAnimation(v2f frame_range, float frame_speed, float frame_blend);
201         void setBonePosition(std::string bone, v3f position, v3f rotation);
202         void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation);
203         ObjectProperties* accessObjectProperties();
204         void notifyObjectPropertiesModified();
205
206         /*
207                 Inventory interface
208         */
209
210         Inventory* getInventory();
211         const Inventory* getInventory() const;
212         InventoryLocation getInventoryLocation() const;
213         void setInventoryModified();
214         std::string getWieldList() const;
215         int getWieldIndex() const;
216         void setWieldIndex(int i);
217
218         /*
219                 PlayerSAO-specific
220         */
221
222         void disconnected();
223
224         Player* getPlayer()
225         {
226                 return m_player;
227         }
228         u16 getPeerID() const
229         {
230                 return m_peer_id;
231         }
232
233         // Cheat prevention
234
235         v3f getLastGoodPosition() const
236         {
237                 return m_last_good_position;
238         }
239         float resetTimeFromLastPunch()
240         {
241                 float r = m_time_from_last_punch;
242                 m_time_from_last_punch = 0.0;
243                 return r;
244         }
245         void noCheatDigStart(v3s16 p)
246         {
247                 m_nocheat_dig_pos = p;
248                 m_nocheat_dig_time = 0;
249         }
250         v3s16 getNoCheatDigPos()
251         {
252                 return m_nocheat_dig_pos;
253         }
254         float getNoCheatDigTime()
255         {
256                 return m_nocheat_dig_time;
257         }
258         void noCheatDigEnd()
259         {
260                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
261         }
262         LagPool& getDigPool()
263         {
264                 return m_dig_pool;
265         }
266         // Returns true if cheated
267         bool checkMovementCheat();
268
269         // Other
270
271         void updatePrivileges(const std::set<std::string> &privs,
272                         bool is_singleplayer)
273         {
274                 m_privs = privs;
275                 m_is_singleplayer = is_singleplayer;
276         }
277
278         bool getCollisionBox(aabb3f *toset);
279         bool collideWithObjects();
280
281 private:
282         std::string getPropertyPacket();
283         
284         Player *m_player;
285         u16 m_peer_id;
286         Inventory *m_inventory;
287         s16 m_damage;
288
289         // Cheat prevention
290         LagPool m_dig_pool;
291         LagPool m_move_pool;
292         v3f m_last_good_position;
293         float m_time_from_last_punch;
294         v3s16 m_nocheat_dig_pos;
295         float m_nocheat_dig_time;
296
297         int m_wield_index;
298         bool m_position_not_sent;
299         ItemGroupList m_armor_groups;
300         bool m_armor_groups_sent;
301
302         bool m_properties_sent;
303         struct ObjectProperties m_prop;
304         // Cached privileges for enforcement
305         std::set<std::string> m_privs;
306         bool m_is_singleplayer;
307
308         v2f m_animation_range;
309         float m_animation_speed;
310         float m_animation_blend;
311         bool m_animation_sent;
312
313         std::map<std::string, core::vector2d<v3f> > m_bone_position; // Stores position and rotation for each bone name
314         bool m_bone_position_sent;
315
316         int m_attachment_parent_id;
317         std::string m_attachment_bone;
318         v3f m_attachment_position;
319         v3f m_attachment_rotation;
320         bool m_attachment_sent;
321
322 public:
323         // Some flags used by Server
324         bool m_moved;
325         bool m_inventory_not_sent;
326         bool m_hp_not_sent;
327         bool m_breath_not_sent;
328         bool m_wielded_item_not_sent;
329
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