Print playername when failing to read playerfile and ignore files starting with .
[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
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         f32 getRadPitch()
164         {
165                 return -1.0 * m_pitch * core::DEGTORAD;
166         }
167
168         f32 getRadYaw()
169         {
170                 return (m_yaw + 90.) * core::DEGTORAD;
171         }
172
173         void updateName(const char *name)
174         {
175                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
176         }
177
178         const char * getName() const
179         {
180                 return m_name;
181         }
182
183         core::aabbox3d<f32> getCollisionbox() {
184                 return m_collisionbox;
185         }
186
187         virtual bool isLocal() const
188         { return false; }
189         virtual PlayerSAO *getPlayerSAO()
190         { return NULL; }
191         virtual void setPlayerSAO(PlayerSAO *sao)
192         { assert(0); }
193
194         /*
195                 serialize() writes a bunch of text that can contain
196                 any characters except a '\0', and such an ending that
197                 deSerialize stops reading exactly at the right point.
198         */
199         void serialize(std::ostream &os);
200         void deSerialize(std::istream &is, std::string playername);
201
202         bool touching_ground;
203         // This oscillates so that the player jumps a bit above the surface
204         bool in_liquid;
205         // This is more stable and defines the maximum speed of the player
206         bool in_liquid_stable;
207         // Gets the viscosity of water to calculate friction
208         u8 liquid_viscosity;
209         bool is_climbing;
210         bool swimming_vertical;
211         bool camera_barely_in_ceiling;
212         
213         u8 light;
214
215         Inventory inventory;
216
217         f32 movement_acceleration_default;
218         f32 movement_acceleration_air;
219         f32 movement_acceleration_fast;
220         f32 movement_speed_walk;
221         f32 movement_speed_crouch;
222         f32 movement_speed_fast;
223         f32 movement_speed_climb;
224         f32 movement_speed_jump;
225         f32 movement_liquid_fluidity;
226         f32 movement_liquid_fluidity_smooth;
227         f32 movement_liquid_sink;
228         f32 movement_gravity;
229
230         float physics_override_speed;
231         float physics_override_jump;
232         float physics_override_gravity;
233
234         u16 hp;
235         u16 breath;
236
237         float hurt_tilt_timer;
238         float hurt_tilt_strength;
239
240         u16 peer_id;
241         
242         std::string inventory_formspec;
243         
244         PlayerControl control;
245         PlayerControl getPlayerControl()
246         {
247                 return control;
248         }
249         
250         u32 keyPressed;
251         
252         std::vector<HudElement *> hud;
253         u32 hud_flags;
254         s32 hud_hotbar_itemcount;
255
256 protected:
257         IGameDef *m_gamedef;
258
259         char m_name[PLAYERNAME_SIZE];
260         f32 m_pitch;
261         f32 m_yaw;
262         v3f m_speed;
263         v3f m_position;
264         core::aabbox3d<f32> m_collisionbox;
265 };
266
267
268 /*
269         Player on the server
270 */
271 class RemotePlayer : public Player
272 {
273 public:
274         RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
275         virtual ~RemotePlayer() {}
276
277         PlayerSAO *getPlayerSAO()
278         { return m_sao; }
279         void setPlayerSAO(PlayerSAO *sao)
280         { m_sao = sao; }
281         void setPosition(const v3f &position);
282         
283 private:
284         PlayerSAO *m_sao;
285 };
286
287 #endif
288