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