Fix some issues with animations, and allow non-looped animations to be defined
[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         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         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_loop;
110         bool m_animation_sent;
111
112         std::map<std::string, core::vector2d<v3f> > m_bone_position;
113         bool m_bone_position_sent;
114
115         int m_attachment_parent_id;
116         std::string m_attachment_bone;
117         v3f m_attachment_position;
118         v3f m_attachment_rotation;
119         bool m_attachment_sent;
120 };
121
122 /*
123         PlayerSAO needs some internals exposed.
124 */
125
126 class LagPool
127 {
128         float m_pool;
129         float m_max;
130 public:
131         LagPool(): m_pool(15), m_max(15)
132         {}
133         void setMax(float new_max)
134         {
135                 m_max = new_max;
136                 if(m_pool > new_max)
137                         m_pool = new_max;
138         }
139         void add(float dtime)
140         {
141                 m_pool -= dtime;
142                 if(m_pool < 0)
143                         m_pool = 0;
144         }
145         bool grab(float dtime)
146         {
147                 if(dtime <= 0)
148                         return true;
149                 if(m_pool + dtime > m_max)
150                         return false;
151                 m_pool += dtime;
152                 return true;
153         }
154 };
155
156 class PlayerSAO : public ServerActiveObject
157 {
158 public:
159         PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
160                         const std::set<std::string> &privs, bool is_singleplayer);
161         ~PlayerSAO();
162         ActiveObjectType getType() const
163         { return ACTIVEOBJECT_TYPE_PLAYER; }
164         ActiveObjectType getSendType() const
165         { return ACTIVEOBJECT_TYPE_GENERIC; }
166         std::string getDescription();
167
168         /*
169                 Active object <-> environment interface
170         */
171
172         void addedToEnvironment(u32 dtime_s);
173         void removingFromEnvironment();
174         bool isStaticAllowed() 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         ItemGroupList getArmorGroups();
201         void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
202         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
203         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
204         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
205         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
206         void getAttachment(int *parent_id, std::string *bone, v3f *position, v3f *rotation);
207         ObjectProperties* accessObjectProperties();
208         void notifyObjectPropertiesModified();
209         void setNametagColor(video::SColor color);
210         video::SColor getNametagColor();
211
212         /*
213                 Inventory interface
214         */
215
216         Inventory* getInventory();
217         const Inventory* getInventory() const;
218         InventoryLocation getInventoryLocation() const;
219         std::string getWieldList() const;
220         int getWieldIndex() const;
221         void setWieldIndex(int i);
222
223         /*
224                 PlayerSAO-specific
225         */
226
227         void disconnected();
228
229         Player* getPlayer()
230         {
231                 return m_player;
232         }
233         u16 getPeerID() const
234         {
235                 return m_peer_id;
236         }
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         Player *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         std::map<std::string, core::vector2d<v3f> > m_bone_position; // Stores position and rotation for each bone name
320         bool m_bone_position_sent;
321
322         int m_attachment_parent_id;
323         std::string m_attachment_bone;
324         v3f m_attachment_position;
325         v3f m_attachment_rotation;
326         bool m_attachment_sent;
327
328         video::SColor m_nametag_color;
329         bool m_nametag_sent;
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
341