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