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