Improved Player Physics
[oweals/minetest.git] / src / player.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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
91 class Player
92 {
93 public:
94
95         Player(IGameDef *gamedef);
96         virtual ~Player() = 0;
97
98         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
99         {}
100
101         v3f getSpeed()
102         {
103                 return m_speed;
104         }
105
106         void setSpeed(v3f speed)
107         {
108                 m_speed = speed;
109         }
110         
111         void accelerateHorizontal(v3f target_speed, f32 max_increase);
112         void accelerateVertical(v3f target_speed, f32 max_increase);
113
114         v3f getPosition()
115         {
116                 return m_position;
117         }
118
119         v3s16 getLightPosition() const;
120
121         v3f getEyeOffset()
122         {
123                 // This is at the height of the eyes of the current figure
124                 // return v3f(0, BS*1.5, 0);
125                 // This is more like in minecraft
126                 if(camera_barely_in_ceiling)
127                         return v3f(0,BS*1.5,0);
128                 else
129                         return v3f(0,BS*1.625,0);
130         }
131
132         v3f getEyePosition()
133         {
134                 return m_position + getEyeOffset();
135         }
136
137         virtual void setPosition(const v3f &position)
138         {
139                 m_position = position;
140         }
141
142         void setPitch(f32 pitch)
143         {
144                 m_pitch = pitch;
145         }
146
147         virtual void setYaw(f32 yaw)
148         {
149                 m_yaw = yaw;
150         }
151
152         f32 getPitch()
153         {
154                 return m_pitch;
155         }
156
157         f32 getYaw()
158         {
159                 return m_yaw;
160         }
161
162         f32 getRadPitch()
163         {
164                 return -1.0 * m_pitch * core::DEGTORAD;
165         }
166
167         f32 getRadYaw()
168         {
169                 return (m_yaw + 90.) * core::DEGTORAD;
170         }
171
172         void updateName(const char *name)
173         {
174                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
175         }
176
177         const char * getName() const
178         {
179                 return m_name;
180         }
181
182         virtual bool isLocal() const
183         { return false; }
184         virtual PlayerSAO *getPlayerSAO()
185         { return NULL; }
186         virtual void setPlayerSAO(PlayerSAO *sao)
187         { assert(0); }
188
189         /*
190                 serialize() writes a bunch of text that can contain
191                 any characters except a '\0', and such an ending that
192                 deSerialize stops reading exactly at the right point.
193         */
194         void serialize(std::ostream &os);
195         void deSerialize(std::istream &is);
196
197         bool touching_ground;
198         // This oscillates so that the player jumps a bit above the surface
199         bool in_liquid;
200         // This is more stable and defines the maximum speed of the player
201         bool in_liquid_stable;
202         // Gets the viscosity of water to calculate friction
203         u8 liquid_viscosity;
204         bool is_climbing;
205         bool swimming_vertical;
206         bool camera_barely_in_ceiling;
207         
208         u8 light;
209
210         Inventory inventory;
211
212         f32 movement_acceleration_default;
213         f32 movement_acceleration_air;
214         f32 movement_acceleration_fast;
215         f32 movement_speed_walk;
216         f32 movement_speed_crouch;
217         f32 movement_speed_fast;
218         f32 movement_speed_climb;
219         f32 movement_speed_jump;
220         f32 movement_liquid_fluidity;
221         f32 movement_liquid_fluidity_smooth;
222         f32 movement_liquid_sink;
223         f32 movement_gravity;
224
225         u16 hp;
226
227         float hurt_tilt_timer;
228         float hurt_tilt_strength;
229
230         u16 peer_id;
231         
232         std::string inventory_formspec;
233         
234         PlayerControl control;
235         PlayerControl getPlayerControl()
236         {
237                 return control;
238         }
239         
240         u32 keyPressed;
241
242 protected:
243         IGameDef *m_gamedef;
244
245         char m_name[PLAYERNAME_SIZE];
246         f32 m_pitch;
247         f32 m_yaw;
248         v3f m_speed;
249         v3f m_position;
250 };
251
252 /*
253         Player on the server
254 */
255 class RemotePlayer : public Player
256 {
257 public:
258         RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
259         virtual ~RemotePlayer() {}
260
261         PlayerSAO *getPlayerSAO()
262         { return m_sao; }
263         void setPlayerSAO(PlayerSAO *sao)
264         { m_sao = sao; }
265         void setPosition(const v3f &position);
266         
267 private:
268         PlayerSAO *m_sao;
269 };
270
271 #endif
272