]> git.lizzy.rs Git - minetest.git/blob - src/player.h
def428847e35d5f90655869a360a15a950c4d8ff
[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         { FATAL_ERROR("FIXME"); }
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() const
230         {
231                 return m_dirty || inventory.checkModified();
232         }
233
234         void setModified(const bool x)
235         {
236                 m_dirty = x;
237                 if (x == false)
238                         inventory.setModified(x);
239         }
240
241         // Use a function, if isDead can be defined by other conditions
242         bool isDead() { return hp == 0; }
243
244         bool touching_ground;
245         // This oscillates so that the player jumps a bit above the surface
246         bool in_liquid;
247         // This is more stable and defines the maximum speed of the player
248         bool in_liquid_stable;
249         // Gets the viscosity of water to calculate friction
250         u8 liquid_viscosity;
251         bool is_climbing;
252         bool swimming_vertical;
253         bool camera_barely_in_ceiling;
254         
255         Inventory inventory;
256
257         f32 movement_acceleration_default;
258         f32 movement_acceleration_air;
259         f32 movement_acceleration_fast;
260         f32 movement_speed_walk;
261         f32 movement_speed_crouch;
262         f32 movement_speed_fast;
263         f32 movement_speed_climb;
264         f32 movement_speed_jump;
265         f32 movement_liquid_fluidity;
266         f32 movement_liquid_fluidity_smooth;
267         f32 movement_liquid_sink;
268         f32 movement_gravity;
269
270         float physics_override_speed;
271         float physics_override_jump;
272         float physics_override_gravity;
273         bool physics_override_sneak;
274         bool physics_override_sneak_glitch;
275
276         v2s32 local_animations[4];
277         float local_animation_speed;
278
279         u16 hp;
280
281         float hurt_tilt_timer;
282         float hurt_tilt_strength;
283
284         u16 peer_id;
285
286         std::string inventory_formspec;
287         
288         PlayerControl control;
289         PlayerControl getPlayerControl()
290         {
291                 return control;
292         }
293         
294         u32 keyPressed;
295         
296
297         HudElement* getHud(u32 id);
298         u32         addHud(HudElement* hud);
299         HudElement* removeHud(u32 id);
300         void        clearHud();
301         u32         maxHudId() {
302                 return hud.size();
303         }
304
305         u32 hud_flags;
306         s32 hud_hotbar_itemcount;
307 protected:
308         IGameDef *m_gamedef;
309
310         char m_name[PLAYERNAME_SIZE];
311         u16 m_breath;
312         f32 m_pitch;
313         f32 m_yaw;
314         v3f m_speed;
315         v3f m_position;
316         core::aabbox3d<f32> m_collisionbox;
317
318         bool m_dirty;
319
320         std::vector<HudElement *> hud;
321 };
322
323
324 /*
325         Player on the server
326 */
327 class RemotePlayer : public Player
328 {
329 public:
330         RemotePlayer(IGameDef *gamedef, const char *name):
331                 Player(gamedef, name),
332                 m_sao(NULL)
333         {}
334         virtual ~RemotePlayer() {}
335
336         void save(std::string savedir);
337
338         PlayerSAO *getPlayerSAO()
339         { return m_sao; }
340         void setPlayerSAO(PlayerSAO *sao)
341         { m_sao = sao; }
342         void setPosition(const v3f &position);
343         
344 private:
345         PlayerSAO *m_sao;
346 };
347
348 #endif
349