Formspecs: Add starting frame to `animated_image` (#9411)
[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 #pragma once
21
22 #include "network/networkprotocol.h"
23 #include "util/numeric.h"
24 #include "serverobject.h"
25 #include "itemgroup.h"
26 #include "object_properties.h"
27 #include "constants.h"
28
29 class UnitSAO: public ServerActiveObject
30 {
31 public:
32         UnitSAO(ServerEnvironment *env, v3f pos);
33         virtual ~UnitSAO() = default;
34
35         void setRotation(v3f rotation) { m_rotation = rotation; }
36         const v3f &getRotation() const { return m_rotation; }
37         v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
38
39         // Deprecated
40         f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
41
42         u16 getHP() const { return m_hp; }
43         // Use a function, if isDead can be defined by other conditions
44         bool isDead() const { return m_hp == 0; }
45
46         inline bool isAttached() const
47         { return getParent(); }
48
49         inline bool isImmortal() const
50         { return itemgroup_get(getArmorGroups(), "immortal"); }
51
52         void setArmorGroups(const ItemGroupList &armor_groups);
53         const ItemGroupList &getArmorGroups() const;
54         void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
55         void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
56         void setAnimationSpeed(float frame_speed);
57         void setBonePosition(const std::string &bone, v3f position, v3f rotation);
58         void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
59         void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
60         void getAttachment(int *parent_id, std::string *bone, v3f *position,
61                         v3f *rotation) const;
62         void clearChildAttachments();
63         void clearParentAttachment();
64         void addAttachmentChild(int child_id);
65         void removeAttachmentChild(int child_id);
66         const std::unordered_set<int> &getAttachmentChildIds() const;
67         ServerActiveObject *getParent() const;
68         ObjectProperties* accessObjectProperties();
69         void notifyObjectPropertiesModified();
70 protected:
71         u16 m_hp = 1;
72
73         v3f m_rotation;
74
75         bool m_properties_sent = true;
76         ObjectProperties m_prop;
77
78         ItemGroupList m_armor_groups;
79         bool m_armor_groups_sent = false;
80
81         v2f m_animation_range;
82         float m_animation_speed = 0.0f;
83         float m_animation_blend = 0.0f;
84         bool m_animation_loop = true;
85         bool m_animation_sent = false;
86         bool m_animation_speed_sent = false;
87
88         // Stores position and rotation for each bone name
89         std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
90         bool m_bone_position_sent = false;
91
92         int m_attachment_parent_id = 0;
93         std::unordered_set<int> m_attachment_child_ids;
94         std::string m_attachment_bone = "";
95         v3f m_attachment_position;
96         v3f m_attachment_rotation;
97         bool m_attachment_sent = false;
98 private:
99         void onAttach(int parent_id);
100         void onDetach(int parent_id);
101 };
102
103 /*
104         LuaEntitySAO needs some internals exposed.
105 */
106
107 class LuaEntitySAO : public UnitSAO
108 {
109 public:
110         LuaEntitySAO(ServerEnvironment *env, v3f pos,
111                 const std::string &name, const std::string &state);
112         ~LuaEntitySAO();
113         ActiveObjectType getType() const
114         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
115         ActiveObjectType getSendType() const
116         { return ACTIVEOBJECT_TYPE_GENERIC; }
117         virtual void addedToEnvironment(u32 dtime_s);
118         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
119                 const std::string &data);
120         void step(float dtime, bool send_recommended);
121         std::string getClientInitializationData(u16 protocol_version);
122         bool isStaticAllowed() const
123         { return m_prop.static_save; }
124         void getStaticData(std::string *result) const;
125         u16 punch(v3f dir,
126                 const ToolCapabilities *toolcap = nullptr,
127                 ServerActiveObject *puncher = nullptr,
128                 float time_from_last_punch = 1000000.0f);
129         void rightClick(ServerActiveObject *clicker);
130         void setPos(const v3f &pos);
131         void moveTo(v3f pos, bool continuous);
132         float getMinimumSavedMovement();
133         std::string getDescription();
134         void setHP(s32 hp, const PlayerHPChangeReason &reason);
135         u16 getHP() const;
136
137         /* LuaEntitySAO-specific */
138         void setVelocity(v3f velocity);
139         void addVelocity(v3f velocity)
140         {
141                 m_velocity += velocity;
142         }
143         v3f getVelocity();
144         void setAcceleration(v3f acceleration);
145         v3f getAcceleration();
146
147         void setTextureMod(const std::string &mod);
148         std::string getTextureMod() const;
149         void setSprite(v2s16 p, int num_frames, float framelength,
150                         bool select_horiz_by_yawpitch);
151         std::string getName();
152         bool getCollisionBox(aabb3f *toset) const;
153         bool getSelectionBox(aabb3f *toset) const;
154         bool collideWithObjects() const;
155 private:
156         std::string getPropertyPacket();
157         void sendPosition(bool do_interpolate, bool is_movement_end);
158
159         std::string m_init_name;
160         std::string m_init_state;
161         bool m_registered = false;
162
163         v3f m_velocity;
164         v3f m_acceleration;
165
166         v3f m_last_sent_position;
167         v3f m_last_sent_velocity;
168         v3f m_last_sent_rotation;
169         float m_last_sent_position_timer = 0.0f;
170         float m_last_sent_move_precision = 0.0f;
171         std::string m_current_texture_modifier = "";
172 };
173
174 /*
175         PlayerSAO needs some internals exposed.
176 */
177
178 class LagPool
179 {
180         float m_pool = 15.0f;
181         float m_max = 15.0f;
182 public:
183         LagPool() = default;
184
185         void setMax(float new_max)
186         {
187                 m_max = new_max;
188                 if(m_pool > new_max)
189                         m_pool = new_max;
190         }
191
192         void add(float dtime)
193         {
194                 m_pool -= dtime;
195                 if(m_pool < 0)
196                         m_pool = 0;
197         }
198
199         void empty()
200         {
201                 m_pool = m_max;
202         }
203
204         bool grab(float dtime)
205         {
206                 if(dtime <= 0)
207                         return true;
208                 if(m_pool + dtime > m_max)
209                         return false;
210                 m_pool += dtime;
211                 return true;
212         }
213 };
214
215 class RemotePlayer;
216
217 class PlayerSAO : public UnitSAO
218 {
219 public:
220         PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
221                         bool is_singleplayer);
222
223         ActiveObjectType getType() const
224         { return ACTIVEOBJECT_TYPE_PLAYER; }
225         ActiveObjectType getSendType() const
226         { return ACTIVEOBJECT_TYPE_GENERIC; }
227         std::string getDescription();
228
229         /*
230                 Active object <-> environment interface
231         */
232
233         void addedToEnvironment(u32 dtime_s);
234         void removingFromEnvironment();
235         bool isStaticAllowed() const { return false; }
236         std::string getClientInitializationData(u16 protocol_version);
237         void getStaticData(std::string *result) const;
238         void step(float dtime, bool send_recommended);
239         void setBasePosition(const v3f &position);
240         void setPos(const v3f &pos);
241         void moveTo(v3f pos, bool continuous);
242         void setPlayerYaw(const float yaw);
243         // Data should not be sent at player initialization
244         void setPlayerYawAndSend(const float yaw);
245         void setLookPitch(const float pitch);
246         // Data should not be sent at player initialization
247         void setLookPitchAndSend(const float pitch);
248         f32 getLookPitch() const { return m_pitch; }
249         f32 getRadLookPitch() const { return m_pitch * core::DEGTORAD; }
250         // Deprecated
251         f32 getRadLookPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
252         void setFov(const float pitch);
253         f32 getFov() const { return m_fov; }
254         void setWantedRange(const s16 range);
255         s16 getWantedRange() const { return m_wanted_range; }
256
257         /*
258                 Interaction interface
259         */
260
261         u16 punch(v3f dir,
262                 const ToolCapabilities *toolcap,
263                 ServerActiveObject *puncher,
264                 float time_from_last_punch);
265         void rightClick(ServerActiveObject *clicker) {}
266         void setHP(s32 hp, const PlayerHPChangeReason &reason);
267         void setHPRaw(u16 hp) { m_hp = hp; }
268         s16 readDamage();
269         u16 getBreath() const { return m_breath; }
270         void setBreath(const u16 breath, bool send = true);
271
272         /*
273                 Inventory interface
274         */
275         Inventory *getInventory() const;
276         InventoryLocation getInventoryLocation() const;
277         void setInventoryModified() {}
278         std::string getWieldList() const { return "main"; }
279         u16 getWieldIndex() const;
280         ItemStack getWieldedItem(ItemStack *selected, ItemStack *hand = nullptr) const;
281         bool setWieldedItem(const ItemStack &item);
282
283         /*
284                 PlayerSAO-specific
285         */
286
287         void disconnected();
288
289         RemotePlayer *getPlayer() { return m_player; }
290         session_t getPeerID() const { return m_peer_id; }
291
292         // Cheat prevention
293
294         v3f getLastGoodPosition() const
295         {
296                 return m_last_good_position;
297         }
298         float resetTimeFromLastPunch()
299         {
300                 float r = m_time_from_last_punch;
301                 m_time_from_last_punch = 0.0;
302                 return r;
303         }
304         void noCheatDigStart(const v3s16 &p)
305         {
306                 m_nocheat_dig_pos = p;
307                 m_nocheat_dig_time = 0;
308         }
309         v3s16 getNoCheatDigPos()
310         {
311                 return m_nocheat_dig_pos;
312         }
313         float getNoCheatDigTime()
314         {
315                 return m_nocheat_dig_time;
316         }
317         void noCheatDigEnd()
318         {
319                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
320         }
321         LagPool& getDigPool()
322         {
323                 return m_dig_pool;
324         }
325         void setMaxSpeedOverride(const v3f &vel);
326         // Returns true if cheated
327         bool checkMovementCheat();
328
329         // Other
330
331         void updatePrivileges(const std::set<std::string> &privs,
332                         bool is_singleplayer)
333         {
334                 m_privs = privs;
335                 m_is_singleplayer = is_singleplayer;
336         }
337
338         bool getCollisionBox(aabb3f *toset) const;
339         bool getSelectionBox(aabb3f *toset) const;
340         bool collideWithObjects() const { return true; }
341
342         void finalize(RemotePlayer *player, const std::set<std::string> &privs);
343
344         v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
345         v3f getEyeOffset() const;
346         float getZoomFOV() const;
347
348         inline Metadata &getMeta() { return m_meta; }
349
350 private:
351         std::string getPropertyPacket();
352         void unlinkPlayerSessionAndSave();
353
354         RemotePlayer *m_player = nullptr;
355         session_t m_peer_id = 0;
356
357         // Cheat prevention
358         LagPool m_dig_pool;
359         LagPool m_move_pool;
360         v3f m_last_good_position;
361         float m_time_from_last_teleport = 0.0f;
362         float m_time_from_last_punch = 0.0f;
363         v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
364         float m_nocheat_dig_time = 0.0f;
365         float m_max_speed_override_time = 0.0f;
366         v3f m_max_speed_override = v3f(0.0f, 0.0f, 0.0f);
367
368         // Timers
369         IntervalLimiter m_breathing_interval;
370         IntervalLimiter m_drowning_interval;
371         IntervalLimiter m_node_hurt_interval;
372
373         bool m_position_not_sent = false;
374
375         // Cached privileges for enforcement
376         std::set<std::string> m_privs;
377         bool m_is_singleplayer;
378
379         u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
380         f32 m_pitch = 0.0f;
381         f32 m_fov = 0.0f;
382         s16 m_wanted_range = 0.0f;
383
384         Metadata m_meta;
385 public:
386         float m_physics_override_speed = 1.0f;
387         float m_physics_override_jump = 1.0f;
388         float m_physics_override_gravity = 1.0f;
389         bool m_physics_override_sneak = true;
390         bool m_physics_override_sneak_glitch = false;
391         bool m_physics_override_new_move = true;
392         bool m_physics_override_sent = false;
393 };
394
395
396 struct PlayerHPChangeReason {
397         enum Type : u8 {
398                 SET_HP,
399                 PLAYER_PUNCH,
400                 FALL,
401                 NODE_DAMAGE,
402                 DROWNING,
403                 RESPAWN
404         };
405
406         Type type = SET_HP;
407         bool from_mod = false;
408         int lua_reference = -1;
409
410         // For PLAYER_PUNCH
411         ServerActiveObject *object = nullptr;
412         // For NODE_DAMAGE
413         std::string node;
414
415         inline bool hasLuaReference() const
416         {
417                 return lua_reference >= 0;
418         }
419
420         bool setTypeFromString(const std::string &typestr)
421         {
422                 if (typestr == "set_hp")
423                         type = SET_HP;
424                 else if (typestr == "punch")
425                         type = PLAYER_PUNCH;
426                 else if (typestr == "fall")
427                         type = FALL;
428                 else if (typestr == "node_damage")
429                         type = NODE_DAMAGE;
430                 else if (typestr == "drown")
431                         type = DROWNING;
432                 else if (typestr == "respawn")
433                         type = RESPAWN;
434                 else
435                         return false;
436
437                 return true;
438         }
439
440         std::string getTypeAsString() const
441         {
442                 switch (type) {
443                 case PlayerHPChangeReason::SET_HP:
444                         return "set_hp";
445                 case PlayerHPChangeReason::PLAYER_PUNCH:
446                         return "punch";
447                 case PlayerHPChangeReason::FALL:
448                         return "fall";
449                 case PlayerHPChangeReason::NODE_DAMAGE:
450                         return "node_damage";
451                 case PlayerHPChangeReason::DROWNING:
452                         return "drown";
453                 case PlayerHPChangeReason::RESPAWN:
454                         return "respawn";
455                 default:
456                         return "?";
457                 }
458         }
459
460         PlayerHPChangeReason(Type type):
461                         type(type)
462         {}
463
464         PlayerHPChangeReason(Type type, ServerActiveObject *object):
465                         type(type), object(object)
466         {}
467
468         PlayerHPChangeReason(Type type, std::string node):
469                         type(type), node(node)
470         {}
471 };