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