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