]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.h
Simplify player modification checks
[dragonfireclient.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                 m_dirty = true;
146                 m_position = position;
147         }
148
149         void setPitch(f32 pitch)
150         {
151                 m_dirty = true;
152                 m_pitch = pitch;
153         }
154
155         virtual void setYaw(f32 yaw)
156         {
157                 m_dirty = true;
158                 m_yaw = yaw;
159         }
160
161         f32 getPitch()
162         {
163                 return m_pitch;
164         }
165
166         f32 getYaw()
167         {
168                 return m_yaw;
169         }
170
171         u16 getBreath()
172         {
173                 return m_breath;
174         }
175
176         virtual void setBreath(u16 breath)
177         {
178                 m_dirty = true;
179                 m_breath = breath;
180         }
181
182         f32 getRadPitch()
183         {
184                 return -1.0 * m_pitch * core::DEGTORAD;
185         }
186
187         f32 getRadYaw()
188         {
189                 return (m_yaw + 90.) * core::DEGTORAD;
190         }
191
192         const char * getName() const
193         {
194                 return m_name;
195         }
196
197         core::aabbox3d<f32> getCollisionbox() {
198                 return m_collisionbox;
199         }
200
201         u32 getFreeHudID() const {
202                 size_t size = hud.size();
203                 for (size_t i = 0; i != size; i++) {
204                         if (!hud[i])
205                                 return i;
206                 }
207                 return size;
208         }
209
210         virtual bool isLocal() const
211         { return false; }
212         virtual PlayerSAO *getPlayerSAO()
213         { return NULL; }
214         virtual void setPlayerSAO(PlayerSAO *sao)
215         { assert(0); }
216
217         /*
218                 serialize() writes a bunch of text that can contain
219                 any characters except a '\0', and such an ending that
220                 deSerialize stops reading exactly at the right point.
221         */
222         void serialize(std::ostream &os);
223         void deSerialize(std::istream &is, std::string playername);
224
225         bool checkModified()
226         {
227                 return m_dirty;
228         }
229
230         bool touching_ground;
231         // This oscillates so that the player jumps a bit above the surface
232         bool in_liquid;
233         // This is more stable and defines the maximum speed of the player
234         bool in_liquid_stable;
235         // Gets the viscosity of water to calculate friction
236         u8 liquid_viscosity;
237         bool is_climbing;
238         bool swimming_vertical;
239         bool camera_barely_in_ceiling;
240         
241         u8 light;
242
243         Inventory inventory;
244
245         f32 movement_acceleration_default;
246         f32 movement_acceleration_air;
247         f32 movement_acceleration_fast;
248         f32 movement_speed_walk;
249         f32 movement_speed_crouch;
250         f32 movement_speed_fast;
251         f32 movement_speed_climb;
252         f32 movement_speed_jump;
253         f32 movement_liquid_fluidity;
254         f32 movement_liquid_fluidity_smooth;
255         f32 movement_liquid_sink;
256         f32 movement_gravity;
257
258         float physics_override_speed;
259         float physics_override_jump;
260         float physics_override_gravity;
261         bool physics_override_sneak;
262         bool physics_override_sneak_glitch;
263
264         v2s32 local_animations[4];
265         float local_animation_speed;
266
267         u16 hp;
268
269         float hurt_tilt_timer;
270         float hurt_tilt_strength;
271
272         u16 peer_id;
273
274         std::string inventory_formspec;
275         
276         PlayerControl control;
277         PlayerControl getPlayerControl()
278         {
279                 return control;
280         }
281         
282         u32 keyPressed;
283         
284
285         HudElement* getHud(u32 id);
286         u32         addHud(HudElement* hud);
287         HudElement* removeHud(u32 id);
288         void        clearHud();
289         u32         maxHudId() {
290                 return hud.size();
291         }
292
293         u32 hud_flags;
294         s32 hud_hotbar_itemcount;
295 protected:
296         IGameDef *m_gamedef;
297
298         char m_name[PLAYERNAME_SIZE];
299         u16 m_breath;
300         f32 m_pitch;
301         f32 m_yaw;
302         v3f m_speed;
303         v3f m_position;
304         core::aabbox3d<f32> m_collisionbox;
305
306         bool m_dirty;
307
308         std::vector<HudElement *> hud;
309 };
310
311
312 /*
313         Player on the server
314 */
315 class RemotePlayer : public Player
316 {
317 public:
318         RemotePlayer(IGameDef *gamedef, const char *name):
319                 Player(gamedef, name),
320                 m_sao(NULL)
321         {}
322         virtual ~RemotePlayer() {}
323
324         void save(std::string savedir);
325
326         PlayerSAO *getPlayerSAO()
327         { return m_sao; }
328         void setPlayerSAO(PlayerSAO *sao)
329         { m_sao = sao; }
330         void setPosition(const v3f &position);
331         
332 private:
333         PlayerSAO *m_sao;
334 };
335
336 #endif
337