Only set player dirty flag if values change
[oweals/minetest.git] / src / player.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 PLAYER_HEADER
21 #define PLAYER_HEADER
22
23 #include "irrlichttypes_bloated.h"
24 #include "inventory.h"
25 #include "constants.h" // BS
26 #include <list>
27
28 #define PLAYERNAME_SIZE 20
29
30 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
31
32 struct PlayerControl
33 {
34         PlayerControl()
35         {
36                 up = false;
37                 down = false;
38                 left = false;
39                 right = false;
40                 jump = false;
41                 aux1 = false;
42                 sneak = false;
43                 LMB = false;
44                 RMB = false;
45                 pitch = 0;
46                 yaw = 0;
47         }
48         PlayerControl(
49                 bool a_up,
50                 bool a_down,
51                 bool a_left,
52                 bool a_right,
53                 bool a_jump,
54                 bool a_aux1,
55                 bool a_sneak,
56                 bool a_LMB,
57                 bool a_RMB,
58                 float a_pitch,
59                 float a_yaw
60         )
61         {
62                 up = a_up;
63                 down = a_down;
64                 left = a_left;
65                 right = a_right;
66                 jump = a_jump;
67                 aux1 = a_aux1;
68                 sneak = a_sneak;
69                 LMB = a_LMB;
70                 RMB = a_RMB;
71                 pitch = a_pitch;
72                 yaw = a_yaw;
73         }
74         bool up;
75         bool down;
76         bool left;
77         bool right;
78         bool jump;
79         bool aux1;
80         bool sneak;
81         bool LMB;
82         bool RMB;
83         float pitch;
84         float yaw;
85 };
86
87 class Map;
88 class IGameDef;
89 struct CollisionInfo;
90 class PlayerSAO;
91 struct HudElement;
92 class Environment;
93
94 class Player
95 {
96 public:
97
98         Player(IGameDef *gamedef, const char *name);
99         virtual ~Player() = 0;
100
101         virtual void move(f32 dtime, Environment *env, f32 pos_max_d)
102         {}
103         virtual void move(f32 dtime, Environment *env, f32 pos_max_d,
104                         std::list<CollisionInfo> *collision_info)
105         {}
106
107         v3f getSpeed()
108         {
109                 return m_speed;
110         }
111
112         void setSpeed(v3f speed)
113         {
114                 m_speed = speed;
115         }
116         
117         void accelerateHorizontal(v3f target_speed, f32 max_increase);
118         void accelerateVertical(v3f target_speed, f32 max_increase);
119
120         v3f getPosition()
121         {
122                 return m_position;
123         }
124
125         v3s16 getLightPosition() const;
126
127         v3f getEyeOffset()
128         {
129                 // This is at the height of the eyes of the current figure
130                 // return v3f(0, BS*1.5, 0);
131                 // This is more like in minecraft
132                 if(camera_barely_in_ceiling)
133                         return v3f(0,BS*1.5,0);
134                 else
135                         return v3f(0,BS*1.625,0);
136         }
137
138         v3f getEyePosition()
139         {
140                 return m_position + getEyeOffset();
141         }
142
143         virtual void setPosition(const v3f &position)
144         {
145                 if (position != m_position)
146                         m_dirty = true;
147                 m_position = position;
148         }
149
150         void setPitch(f32 pitch)
151         {
152                 if (pitch != m_pitch)
153                         m_dirty = true;
154                 m_pitch = pitch;
155         }
156
157         virtual void setYaw(f32 yaw)
158         {
159                 if (yaw != m_yaw)
160                         m_dirty = true;
161                 m_yaw = yaw;
162         }
163
164         f32 getPitch()
165         {
166                 return m_pitch;
167         }
168
169         f32 getYaw()
170         {
171                 return m_yaw;
172         }
173
174         u16 getBreath()
175         {
176                 return m_breath;
177         }
178
179         virtual void setBreath(u16 breath)
180         {
181                 if (breath != m_breath)
182                         m_dirty = true;
183                 m_breath = breath;
184         }
185
186         f32 getRadPitch()
187         {
188                 return -1.0 * m_pitch * core::DEGTORAD;
189         }
190
191         f32 getRadYaw()
192         {
193                 return (m_yaw + 90.) * core::DEGTORAD;
194         }
195
196         const char * getName() const
197         {
198                 return m_name;
199         }
200
201         core::aabbox3d<f32> getCollisionbox() {
202                 return m_collisionbox;
203         }
204
205         u32 getFreeHudID() const {
206                 size_t size = hud.size();
207                 for (size_t i = 0; i != size; i++) {
208                         if (!hud[i])
209                                 return i;
210                 }
211                 return size;
212         }
213
214         virtual bool isLocal() const
215         { return false; }
216         virtual PlayerSAO *getPlayerSAO()
217         { return NULL; }
218         virtual void setPlayerSAO(PlayerSAO *sao)
219         { assert(0); }
220
221         /*
222                 serialize() writes a bunch of text that can contain
223                 any characters except a '\0', and such an ending that
224                 deSerialize stops reading exactly at the right point.
225         */
226         void serialize(std::ostream &os);
227         void deSerialize(std::istream &is, std::string playername);
228
229         bool checkModified()
230         {
231                 return m_dirty;
232         }
233
234         bool touching_ground;
235         // This oscillates so that the player jumps a bit above the surface
236         bool in_liquid;
237         // This is more stable and defines the maximum speed of the player
238         bool in_liquid_stable;
239         // Gets the viscosity of water to calculate friction
240         u8 liquid_viscosity;
241         bool is_climbing;
242         bool swimming_vertical;
243         bool camera_barely_in_ceiling;
244         
245         u8 light;
246
247         Inventory inventory;
248
249         f32 movement_acceleration_default;
250         f32 movement_acceleration_air;
251         f32 movement_acceleration_fast;
252         f32 movement_speed_walk;
253         f32 movement_speed_crouch;
254         f32 movement_speed_fast;
255         f32 movement_speed_climb;
256         f32 movement_speed_jump;
257         f32 movement_liquid_fluidity;
258         f32 movement_liquid_fluidity_smooth;
259         f32 movement_liquid_sink;
260         f32 movement_gravity;
261
262         float physics_override_speed;
263         float physics_override_jump;
264         float physics_override_gravity;
265         bool physics_override_sneak;
266         bool physics_override_sneak_glitch;
267
268         v2s32 local_animations[4];
269         float local_animation_speed;
270
271         u16 hp;
272
273         float hurt_tilt_timer;
274         float hurt_tilt_strength;
275
276         u16 peer_id;
277
278         std::string inventory_formspec;
279         
280         PlayerControl control;
281         PlayerControl getPlayerControl()
282         {
283                 return control;
284         }
285         
286         u32 keyPressed;
287         
288
289         HudElement* getHud(u32 id);
290         u32         addHud(HudElement* hud);
291         HudElement* removeHud(u32 id);
292         void        clearHud();
293         u32         maxHudId() {
294                 return hud.size();
295         }
296
297         u32 hud_flags;
298         s32 hud_hotbar_itemcount;
299 protected:
300         IGameDef *m_gamedef;
301
302         char m_name[PLAYERNAME_SIZE];
303         u16 m_breath;
304         f32 m_pitch;
305         f32 m_yaw;
306         v3f m_speed;
307         v3f m_position;
308         core::aabbox3d<f32> m_collisionbox;
309
310         bool m_dirty;
311
312         std::vector<HudElement *> hud;
313 };
314
315
316 /*
317         Player on the server
318 */
319 class RemotePlayer : public Player
320 {
321 public:
322         RemotePlayer(IGameDef *gamedef, const char *name):
323                 Player(gamedef, name),
324                 m_sao(NULL)
325         {}
326         virtual ~RemotePlayer() {}
327
328         void save(std::string savedir);
329
330         PlayerSAO *getPlayerSAO()
331         { return m_sao; }
332         void setPlayerSAO(PlayerSAO *sao)
333         { m_sao = sao; }
334         void setPosition(const v3f &position);
335         
336 private:
337         PlayerSAO *m_sao;
338 };
339
340 #endif
341