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