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