]> git.lizzy.rs Git - minetest.git/blob - src/content_sao.h
Add get and set functions for the nametag color
[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 "serverobject.h"
24 #include "itemgroup.h"
25 #include "player.h"
26 #include "object_properties.h"
27
28 /*
29         LuaEntitySAO needs some internals exposed.
30 */
31
32 class LuaEntitySAO : public ServerActiveObject
33 {
34 public:
35         LuaEntitySAO(ServerEnvironment *env, v3f pos,
36                      const std::string &name, const std::string &state);
37         ~LuaEntitySAO();
38         ActiveObjectType getType() const
39         { return ACTIVEOBJECT_TYPE_LUAENTITY; }
40         ActiveObjectType getSendType() const
41         { return ACTIVEOBJECT_TYPE_GENERIC; }
42         virtual void addedToEnvironment(u32 dtime_s);
43         static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
44                         const std::string &data);
45         bool isAttached();
46         void step(float dtime, bool send_recommended);
47         std::string getClientInitializationData(u16 protocol_version);
48         std::string getStaticData();
49         int punch(v3f dir,
50                         const ToolCapabilities *toolcap=NULL,
51                         ServerActiveObject *puncher=NULL,
52                         float time_from_last_punch=1000000);
53         void rightClick(ServerActiveObject *clicker);
54         void setPos(v3f pos);
55         void moveTo(v3f pos, bool continuous);
56         float getMinimumSavedMovement();
57         std::string getDescription();
58         void setHP(s16 hp);
59         s16 getHP() const;
60         void setArmorGroups(const ItemGroupList &armor_groups);
61         void setAnimation(v2f frame_range, float frame_speed, float frame_blend);
62         void setBonePosition(std::string bone, v3f position, v3f rotation);
63         void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation);
64         ObjectProperties* accessObjectProperties();
65         void notifyObjectPropertiesModified();
66         /* LuaEntitySAO-specific */
67         void setVelocity(v3f velocity);
68         v3f getVelocity();
69         void setAcceleration(v3f acceleration);
70         v3f getAcceleration();
71         void setYaw(float yaw);
72         float getYaw();
73         void setTextureMod(const std::string &mod);
74         void setSprite(v2s16 p, int num_frames, float framelength,
75                         bool select_horiz_by_yawpitch);
76         std::string getName();
77         bool getCollisionBox(aabb3f *toset);
78         bool collideWithObjects();
79 private:
80         std::string getPropertyPacket();
81         void sendPosition(bool do_interpolate, bool is_movement_end);
82
83         std::string m_init_name;
84         std::string m_init_state;
85         bool m_registered;
86         struct ObjectProperties m_prop;
87         
88         s16 m_hp;
89         v3f m_velocity;
90         v3f m_acceleration;
91         float m_yaw;
92         ItemGroupList m_armor_groups;
93         
94         bool m_properties_sent;
95         float m_last_sent_yaw;
96         v3f m_last_sent_position;
97         v3f m_last_sent_velocity;
98         float m_last_sent_position_timer;
99         float m_last_sent_move_precision;
100         bool m_armor_groups_sent;
101
102         v2f m_animation_range;
103         float m_animation_speed;
104         float m_animation_blend;
105         bool m_animation_sent;
106
107         std::map<std::string, core::vector2d<v3f> > m_bone_position;
108         bool m_bone_position_sent;
109
110         int m_attachment_parent_id;
111         std::string m_attachment_bone;
112         v3f m_attachment_position;
113         v3f m_attachment_rotation;
114         bool m_attachment_sent;
115 };
116
117 /*
118         PlayerSAO needs some internals exposed.
119 */
120
121 class LagPool
122 {
123         float m_pool;
124         float m_max;
125 public:
126         LagPool(): m_pool(15), m_max(15)
127         {}
128         void setMax(float new_max)
129         {
130                 m_max = new_max;
131                 if(m_pool > new_max)
132                         m_pool = new_max;
133         }
134         void add(float dtime)
135         {
136                 m_pool -= dtime;
137                 if(m_pool < 0)
138                         m_pool = 0;
139         }
140         bool grab(float dtime)
141         {
142                 if(dtime <= 0)
143                         return true;
144                 if(m_pool + dtime > m_max)
145                         return false;
146                 m_pool += dtime;
147                 return true;
148         }
149 };
150
151 class PlayerSAO : public ServerActiveObject
152 {
153 public:
154         PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
155                         const std::set<std::string> &privs, bool is_singleplayer);
156         ~PlayerSAO();
157         ActiveObjectType getType() const
158         { return ACTIVEOBJECT_TYPE_PLAYER; }
159         ActiveObjectType getSendType() const
160         { return ACTIVEOBJECT_TYPE_GENERIC; }
161         std::string getDescription();
162
163         /*
164                 Active object <-> environment interface
165         */
166
167         void addedToEnvironment(u32 dtime_s);
168         void removingFromEnvironment();
169         bool isStaticAllowed() const;
170         std::string getClientInitializationData(u16 protocol_version);
171         std::string getStaticData();
172         bool isAttached();
173         void step(float dtime, bool send_recommended);
174         void setBasePosition(const v3f &position);
175         void setPos(v3f pos);
176         void moveTo(v3f pos, bool continuous);
177         void setYaw(float);
178         void setPitch(float);
179
180         /*
181                 Interaction interface
182         */
183
184         int punch(v3f dir,
185                 const ToolCapabilities *toolcap,
186                 ServerActiveObject *puncher,
187                 float time_from_last_punch);
188         void rightClick(ServerActiveObject *clicker);
189         s16 getHP() const;
190         void setHP(s16 hp);
191         s16 readDamage();
192         u16 getBreath() const;
193         void setBreath(u16 breath);
194         void setArmorGroups(const ItemGroupList &armor_groups);
195         void setAnimation(v2f frame_range, float frame_speed, float frame_blend);
196         void setBonePosition(std::string bone, v3f position, v3f rotation);
197         void setAttachment(int parent_id, std::string bone, v3f position, v3f rotation);
198         ObjectProperties* accessObjectProperties();
199         void notifyObjectPropertiesModified();
200         void setNametagColor(video::SColor color);
201         video::SColor getNametagColor();
202
203         /*
204                 Inventory interface
205         */
206
207         Inventory* getInventory();
208         const Inventory* getInventory() const;
209         InventoryLocation getInventoryLocation() const;
210         std::string getWieldList() const;
211         int getWieldIndex() const;
212         void setWieldIndex(int i);
213
214         /*
215                 PlayerSAO-specific
216         */
217
218         void disconnected();
219
220         Player* getPlayer()
221         {
222                 return m_player;
223         }
224         u16 getPeerID() const
225         {
226                 return m_peer_id;
227         }
228
229         // Cheat prevention
230
231         v3f getLastGoodPosition() const
232         {
233                 return m_last_good_position;
234         }
235         float resetTimeFromLastPunch()
236         {
237                 float r = m_time_from_last_punch;
238                 m_time_from_last_punch = 0.0;
239                 return r;
240         }
241         void noCheatDigStart(v3s16 p)
242         {
243                 m_nocheat_dig_pos = p;
244                 m_nocheat_dig_time = 0;
245         }
246         v3s16 getNoCheatDigPos()
247         {
248                 return m_nocheat_dig_pos;
249         }
250         float getNoCheatDigTime()
251         {
252                 return m_nocheat_dig_time;
253         }
254         void noCheatDigEnd()
255         {
256                 m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
257         }
258         LagPool& getDigPool()
259         {
260                 return m_dig_pool;
261         }
262         // Returns true if cheated
263         bool checkMovementCheat();
264
265         // Other
266
267         void updatePrivileges(const std::set<std::string> &privs,
268                         bool is_singleplayer)
269         {
270                 m_privs = privs;
271                 m_is_singleplayer = is_singleplayer;
272         }
273
274         bool getCollisionBox(aabb3f *toset);
275         bool collideWithObjects();
276
277 private:
278         std::string getPropertyPacket();
279         
280         Player *m_player;
281         u16 m_peer_id;
282         Inventory *m_inventory;
283         s16 m_damage;
284
285         // Cheat prevention
286         LagPool m_dig_pool;
287         LagPool m_move_pool;
288         v3f m_last_good_position;
289         float m_time_from_last_punch;
290         v3s16 m_nocheat_dig_pos;
291         float m_nocheat_dig_time;
292
293         int m_wield_index;
294         bool m_position_not_sent;
295         ItemGroupList m_armor_groups;
296         bool m_armor_groups_sent;
297
298         bool m_properties_sent;
299         struct ObjectProperties m_prop;
300         // Cached privileges for enforcement
301         std::set<std::string> m_privs;
302         bool m_is_singleplayer;
303
304         v2f m_animation_range;
305         float m_animation_speed;
306         float m_animation_blend;
307         bool m_animation_sent;
308
309         std::map<std::string, core::vector2d<v3f> > m_bone_position; // Stores position and rotation for each bone name
310         bool m_bone_position_sent;
311
312         int m_attachment_parent_id;
313         std::string m_attachment_bone;
314         v3f m_attachment_position;
315         v3f m_attachment_rotation;
316         bool m_attachment_sent;
317
318         video::SColor m_nametag_color;
319         bool m_nametag_sent;
320
321 public:
322         float m_physics_override_speed;
323         float m_physics_override_jump;
324         float m_physics_override_gravity;
325         bool m_physics_override_sneak;
326         bool m_physics_override_sneak_glitch;
327         bool m_physics_override_sent;
328 };
329
330 #endif
331