b6856b1d0015963175083a59c14806ad542f5026
[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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
24 #include "inventory.h"
25
26 #define PLAYERNAME_SIZE 20
27
28 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
29
30
31 class Map;
32 class IGameDef;
33 struct CollisionInfo;
34 class PlayerSAO;
35
36 class Player
37 {
38 public:
39
40         Player(IGameDef *gamedef);
41         virtual ~Player() = 0;
42
43         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
44         {}
45
46         v3f getSpeed()
47         {
48                 return m_speed;
49         }
50
51         void setSpeed(v3f speed)
52         {
53                 m_speed = speed;
54         }
55         
56         // Y direction is ignored
57         void accelerate(v3f target_speed, f32 max_increase);
58
59         v3f getPosition()
60         {
61                 return m_position;
62         }
63
64         v3s16 getLightPosition() const;
65
66         v3f getEyeOffset()
67         {
68                 // This is at the height of the eyes of the current figure
69                 // return v3f(0, BS+BS/2, 0);
70                 // This is more like in minecraft
71                 return v3f(0,BS+(5*BS)/8,0);
72         }
73
74         v3f getEyePosition()
75         {
76                 return m_position + getEyeOffset();
77         }
78
79         virtual void setPosition(const v3f &position)
80         {
81                 m_position = position;
82         }
83
84         void setPitch(f32 pitch)
85         {
86                 m_pitch = pitch;
87         }
88
89         virtual void setYaw(f32 yaw)
90         {
91                 m_yaw = yaw;
92         }
93
94         f32 getPitch()
95         {
96                 return m_pitch;
97         }
98
99         f32 getYaw()
100         {
101                 return m_yaw;
102         }
103
104         f32 getRadPitch()
105         {
106                 return -1.0 * m_pitch * core::DEGTORAD;
107         }
108
109         f32 getRadYaw()
110         {
111                 return (m_yaw + 90.) * core::DEGTORAD;
112         }
113
114         void updateName(const char *name)
115         {
116                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
117         }
118
119         const char * getName() const
120         {
121                 return m_name;
122         }
123
124         virtual bool isLocal() const
125         { return false; }
126         virtual PlayerSAO *getPlayerSAO()
127         { return NULL; }
128         virtual void setPlayerSAO(PlayerSAO *sao)
129         { assert(0); }
130
131         /*
132                 serialize() writes a bunch of text that can contain
133                 any characters except a '\0', and such an ending that
134                 deSerialize stops reading exactly at the right point.
135         */
136         void serialize(std::ostream &os);
137         void deSerialize(std::istream &is);
138
139         bool touching_ground;
140         // This oscillates so that the player jumps a bit above the surface
141         bool in_water;
142         // This is more stable and defines the maximum speed of the player
143         bool in_water_stable;
144         bool is_climbing;
145         bool swimming_up;
146         
147         u8 light;
148
149         // In creative mode, this is the invisible backup inventory
150         Inventory inventory;
151
152         u16 hp;
153
154         u16 peer_id;
155
156 protected:
157         IGameDef *m_gamedef;
158
159         char m_name[PLAYERNAME_SIZE];
160         f32 m_pitch;
161         f32 m_yaw;
162         v3f m_speed;
163         v3f m_position;
164 };
165
166 #ifndef SERVER
167 struct PlayerControl
168 {
169         PlayerControl()
170         {
171                 up = false;
172                 down = false;
173                 left = false;
174                 right = false;
175                 jump = false;
176                 aux1 = false;
177                 sneak = false;
178                 pitch = 0;
179                 yaw = 0;
180         }
181         PlayerControl(
182                 bool a_up,
183                 bool a_down,
184                 bool a_left,
185                 bool a_right,
186                 bool a_jump,
187                 bool a_aux1,
188                 bool a_sneak,
189                 float a_pitch,
190                 float a_yaw
191         )
192         {
193                 up = a_up;
194                 down = a_down;
195                 left = a_left;
196                 right = a_right;
197                 jump = a_jump;
198                 aux1 = a_aux1;
199                 sneak = a_sneak;
200                 pitch = a_pitch;
201                 yaw = a_yaw;
202         }
203         bool up;
204         bool down;
205         bool left;
206         bool right;
207         bool jump;
208         bool aux1;
209         bool sneak;
210         float pitch;
211         float yaw;
212 };
213
214 class LocalPlayer : public Player
215 {
216 public:
217         LocalPlayer(IGameDef *gamedef);
218         virtual ~LocalPlayer();
219
220         bool isLocal() const
221         {
222                 return true;
223         }
224         
225         void move(f32 dtime, Map &map, f32 pos_max_d,
226                         core::list<CollisionInfo> *collision_info);
227         void move(f32 dtime, Map &map, f32 pos_max_d);
228
229         void applyControl(float dtime);
230
231         v3s16 getStandingNodePos();
232         
233         PlayerControl control;
234
235 private:
236         // This is used for determining the sneaking range
237         v3s16 m_sneak_node;
238         // Whether the player is allowed to sneak
239         bool m_sneak_node_exists;
240 };
241 #endif // !SERVER
242
243 /*
244         Player on the server
245 */
246 class RemotePlayer : public Player
247 {
248 public:
249         RemotePlayer(IGameDef *gamedef): Player(gamedef), m_sao(0) {}
250         virtual ~RemotePlayer() {}
251
252         PlayerSAO *getPlayerSAO()
253         { return m_sao; }
254         void setPlayerSAO(PlayerSAO *sao)
255         { m_sao = sao; }
256         void setPosition(const v3f &position);
257
258 private:
259         PlayerSAO *m_sao;
260 };
261
262 #endif
263