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