Translation to Portuguese of Brazil for Minetest
[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 class 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         virtual bool isLocal() const
184         { return false; }
185         virtual PlayerSAO *getPlayerSAO()
186         { return NULL; }
187         virtual void setPlayerSAO(PlayerSAO *sao)
188         { assert(0); }
189
190         /*
191                 serialize() writes a bunch of text that can contain
192                 any characters except a '\0', and such an ending that
193                 deSerialize stops reading exactly at the right point.
194         */
195         void serialize(std::ostream &os);
196         void deSerialize(std::istream &is);
197
198         bool touching_ground;
199         // This oscillates so that the player jumps a bit above the surface
200         bool in_liquid;
201         // This is more stable and defines the maximum speed of the player
202         bool in_liquid_stable;
203         // Gets the viscosity of water to calculate friction
204         u8 liquid_viscosity;
205         bool is_climbing;
206         bool swimming_vertical;
207         bool camera_barely_in_ceiling;
208         
209         u8 light;
210
211         Inventory inventory;
212
213         f32 movement_acceleration_default;
214         f32 movement_acceleration_air;
215         f32 movement_acceleration_fast;
216         f32 movement_speed_walk;
217         f32 movement_speed_crouch;
218         f32 movement_speed_fast;
219         f32 movement_speed_climb;
220         f32 movement_speed_jump;
221         f32 movement_liquid_fluidity;
222         f32 movement_liquid_fluidity_smooth;
223         f32 movement_liquid_sink;
224         f32 movement_gravity;
225
226         float physics_override_speed;
227         float physics_override_jump;
228         float physics_override_gravity;
229
230         u16 hp;
231
232         float hurt_tilt_timer;
233         float hurt_tilt_strength;
234
235         u16 peer_id;
236         
237         std::string inventory_formspec;
238         
239         PlayerControl control;
240         PlayerControl getPlayerControl()
241         {
242                 return control;
243         }
244         
245         u32 keyPressed;
246         
247         std::vector<HudElement *> hud;
248
249 protected:
250         IGameDef *m_gamedef;
251
252         char m_name[PLAYERNAME_SIZE];
253         f32 m_pitch;
254         f32 m_yaw;
255         v3f m_speed;
256         v3f m_position;
257 };
258
259
260 /*
261         Player on the server
262 */
263 class RemotePlayer : public Player
264 {
265 public:
266         RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
267         virtual ~RemotePlayer() {}
268
269         PlayerSAO *getPlayerSAO()
270         { return m_sao; }
271         void setPlayerSAO(PlayerSAO *sao)
272         { m_sao = sao; }
273         void setPosition(const v3f &position);
274         
275 private:
276         PlayerSAO *m_sao;
277 };
278
279 #endif
280