]> git.lizzy.rs Git - dragonfireclient.git/blob - src/content_sao.h
Cleanup: drop Server::hudGetHotbarImage()
[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         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);
121         s16 getHP() const;
122         /* LuaEntitySAO-specific */
123         void setVelocity(v3f velocity);
124         v3f getVelocity();
125         void setAcceleration(v3f acceleration);
126         v3f getAcceleration();
127
128         void setTextureMod(const std::string &mod);
129         std::string getTextureMod() const;
130         void setSprite(v2s16 p, int num_frames, float framelength,
131                         bool select_horiz_by_yawpitch);
132         std::string getName();
133         bool getCollisionBox(aabb3f *toset) const;
134         bool getSelectionBox(aabb3f *toset) const;
135         bool collideWithObjects() const;
136 private:
137         std::string getPropertyPacket();
138         void sendPosition(bool do_interpolate, bool is_movement_end);
139
140         std::string m_init_name;
141         std::string m_init_state;
142         bool m_registered = false;
143
144         v3f m_velocity;
145         v3f m_acceleration;
146
147         float m_last_sent_yaw = 0.0f;
148         v3f m_last_sent_position;
149         v3f m_last_sent_velocity;
150         float m_last_sent_position_timer = 0.0f;
151         float m_last_sent_move_precision = 0.0f;
152         std::string m_current_texture_modifier = "";
153 };
154
155 /*
156         PlayerSAO needs some internals exposed.
157 */
158
159 class LagPool
160 {
161         float m_pool = 15.0f;
162         float m_max = 15.0f;
163 public:
164         LagPool() = default;
165
166         void setMax(float new_max)
167         {
168                 m_max = new_max;
169                 if(m_pool > new_max)
170                         m_pool = new_max;
171         }
172
173         void add(float dtime)
174         {
175                 m_pool -= dtime;
176                 if(m_pool < 0)
177                         m_pool = 0;
178         }
179
180         void empty()
181         {
182                 m_pool = m_max;
183         }
184
185         bool grab(float dtime)
186         {
187                 if(dtime <= 0)
188                         return true;
189                 if(m_pool + dtime > m_max)
190                         return false;
191                 m_pool += dtime;
192                 return true;
193         }
194 };
195
196 typedef std::unordered_map<std::string, std::string> PlayerAttributes;
197 class RemotePlayer;
198
199 class PlayerSAO : public UnitSAO
200 {
201 public:
202         PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
203                         bool is_singleplayer);
204         ~PlayerSAO();
205         ActiveObjectType getType() const
206         { return ACTIVEOBJECT_TYPE_PLAYER; }
207         ActiveObjectType getSendType() const
208         { return ACTIVEOBJECT_TYPE_GENERIC; }
209         std::string getDescription();
210
211         /*
212                 Active object <-> environment interface
213         */
214
215         void addedToEnvironment(u32 dtime_s);
216         void removingFromEnvironment();
217         bool isStaticAllowed() const { return false; }
218         std::string getClientInitializationData(u16 protocol_version);
219         void getStaticData(std::string *result) const;
220         void step(float dtime, bool send_recommended);
221         void setBasePosition(const v3f &position);
222         void setPos(const v3f &pos);
223         void moveTo(v3f pos, bool continuous);
224         void setYaw(const float yaw);
225         // Data should not be sent at player initialization
226         void setYawAndSend(const float yaw);
227         void setPitch(const float pitch);
228         // Data should not be sent at player initialization
229         void setPitchAndSend(const float pitch);
230         f32 getPitch() const { return m_pitch; }
231         f32 getRadPitch() const { return m_pitch * core::DEGTORAD; }
232         // Deprecated
233         f32 getRadPitchDep() const { return -1.0 * m_pitch * core::DEGTORAD; }
234         void setFov(const float pitch);
235         f32 getFov() const { return m_fov; }
236         void setWantedRange(const s16 range);
237         s16 getWantedRange() const { return m_wanted_range; }
238
239         /*
240                 Interaction interface
241         */
242
243         int punch(v3f dir,
244                 const ToolCapabilities *toolcap,
245                 ServerActiveObject *puncher,
246                 float time_from_last_punch);
247         void rightClick(ServerActiveObject *clicker) {}
248         void setHP(s16 hp);
249         void setHPRaw(s16 hp) { m_hp = hp; }
250         s16 readDamage();
251         u16 getBreath() const { return m_breath; }
252         void setBreath(const u16 breath, bool send = true);
253
254         /*
255                 Inventory interface
256         */
257
258         Inventory* getInventory();
259         const Inventory* getInventory() const;
260         InventoryLocation getInventoryLocation() const;
261         std::string getWieldList() const;
262         ItemStack getWieldedItem() const;
263         ItemStack getWieldedItemOrHand() const;
264         bool setWieldedItem(const ItemStack &item);
265         int getWieldIndex() const;
266         void setWieldIndex(int i);
267
268         /*
269                 Modding interface
270         */
271         inline void setExtendedAttribute(const std::string &attr, const std::string &value)
272         {
273                 m_extra_attributes[attr] = value;
274                 m_extended_attributes_modified = true;
275         }
276
277         inline bool getExtendedAttribute(const std::string &attr, std::string *value)
278         {
279                 if (m_extra_attributes.find(attr) == m_extra_attributes.end())
280                         return false;
281
282                 *value = m_extra_attributes[attr];
283                 return true;
284         }
285
286         inline void removeExtendedAttribute(const std::string &attr)
287         {
288                 PlayerAttributes::iterator it = m_extra_attributes.find(attr);
289                 if (it == m_extra_attributes.end())
290                         return;
291
292                 m_extra_attributes.erase(it);
293                 m_extended_attributes_modified = true;
294         }
295
296         inline const PlayerAttributes &getExtendedAttributes()
297         {
298                 return m_extra_attributes;
299         }
300
301         inline bool extendedAttributesModified() const
302         {
303                 return m_extended_attributes_modified;
304         }
305
306         inline void setExtendedAttributeModified(bool v)
307         {
308                 m_extended_attributes_modified = v;
309         }
310
311         /*
312                 PlayerSAO-specific
313         */
314
315         void disconnected();
316
317         RemotePlayer *getPlayer() { return m_player; }
318         session_t getPeerID() const { return m_peer_id; }
319
320         // Cheat prevention
321
322         v3f getLastGoodPosition() const
323         {
324                 return m_last_good_position;
325         }
326         float resetTimeFromLastPunch()
327         {
328                 float r = m_time_from_last_punch;
329                 m_time_from_last_punch = 0.0;
330                 return r;
331         }
332         void noCheatDigStart(const v3s16 &p)
333         {
334                 m_nocheat_dig_pos = p;
335                 m_nocheat_dig_time = 0;
336         }
337         v3s16 getNoCheatDigPos()
338         {
339                 return m_nocheat_dig_pos;
340         }
341         float getNoCheatDigTime()
342         {
343                 return m_nocheat_dig_time;
344         }
345         void noCheatDigEnd()
346         {
347                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
348         }
349         LagPool& getDigPool()
350         {
351                 return m_dig_pool;
352         }
353         // Returns true if cheated
354         bool checkMovementCheat();
355
356         // Other
357
358         void updatePrivileges(const std::set<std::string> &privs,
359                         bool is_singleplayer)
360         {
361                 m_privs = privs;
362                 m_is_singleplayer = is_singleplayer;
363         }
364
365         bool getCollisionBox(aabb3f *toset) const;
366         bool getSelectionBox(aabb3f *toset) const;
367         bool collideWithObjects() const { return true; }
368
369         void finalize(RemotePlayer *player, const std::set<std::string> &privs);
370
371         v3f getEyePosition() const { return m_base_position + getEyeOffset(); }
372         v3f getEyeOffset() const;
373
374 private:
375         std::string getPropertyPacket();
376         void unlinkPlayerSessionAndSave();
377
378         RemotePlayer *m_player = nullptr;
379         session_t m_peer_id = 0;
380         Inventory *m_inventory = nullptr;
381         s16 m_damage = 0;
382
383         // Cheat prevention
384         LagPool m_dig_pool;
385         LagPool m_move_pool;
386         v3f m_last_good_position;
387         float m_time_from_last_teleport = 0.0f;
388         float m_time_from_last_punch = 0.0f;
389         v3s16 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
390         float m_nocheat_dig_time = 0.0f;
391
392         // Timers
393         IntervalLimiter m_breathing_interval;
394         IntervalLimiter m_drowning_interval;
395         IntervalLimiter m_node_hurt_interval;
396
397         int m_wield_index = 0;
398         bool m_position_not_sent = false;
399
400         // Cached privileges for enforcement
401         std::set<std::string> m_privs;
402         bool m_is_singleplayer;
403
404         u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
405         f32 m_pitch = 0.0f;
406         f32 m_fov = 0.0f;
407         s16 m_wanted_range = 0.0f;
408
409         PlayerAttributes m_extra_attributes;
410         bool m_extended_attributes_modified = false;
411 public:
412         float m_physics_override_speed = 1.0f;
413         float m_physics_override_jump = 1.0f;
414         float m_physics_override_gravity = 1.0f;
415         bool m_physics_override_sneak = true;
416         bool m_physics_override_sneak_glitch = false;
417         bool m_physics_override_new_move = true;
418         bool m_physics_override_sent = false;
419 };