Refactor player's eye position coding
[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 #include "collision.h"
26
27 #define PLAYERNAME_SIZE 20
28
29 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
30
31
32 class Map;
33
34 class Player
35 {
36 public:
37
38
39         Player();
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                 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
68         }
69
70         v3f getEyePosition()
71         {
72                 // This is at the height of the eyes of the current figure
73                 // return m_position + v3f(0, BS+BS/2, 0);
74                 // This is more like in minecraft
75                 return m_position + v3f(0,BS+(5*BS)/8,0);
76         }
77
78         virtual void setPosition(const v3f &position)
79         {
80                 m_position = position;
81         }
82
83         void setPitch(f32 pitch)
84         {
85                 m_pitch = pitch;
86         }
87
88         virtual void setYaw(f32 yaw)
89         {
90                 m_yaw = yaw;
91         }
92
93         f32 getPitch()
94         {
95                 return m_pitch;
96         }
97
98         f32 getYaw()
99         {
100                 return m_yaw;
101         }
102
103         virtual void updateName(const char *name)
104         {
105                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
106         }
107
108         const char * getName()
109         {
110                 return m_name;
111         }
112
113         virtual bool isLocal() const = 0;
114
115         virtual void updateLight(u8 light_at_pos) {};
116         
117         // NOTE: Use peer_id == 0 for disconnected
118         /*virtual bool isClientConnected() { return false; }
119         virtual void setClientConnected(bool) {}*/
120         
121         /*
122                 serialize() writes a bunch of text that can contain
123                 any characters except a '\0', and such an ending that
124                 deSerialize stops reading exactly at the right point.
125         */
126         void serialize(std::ostream &os);
127         void deSerialize(std::istream &is);
128
129         bool touching_ground;
130         // This oscillates so that the player jumps a bit above the surface
131         bool in_water;
132         // This is more stable and defines the maximum speed of the player
133         bool in_water_stable;
134         bool is_climbing;
135         bool swimming_up;
136         
137         Inventory inventory;
138         // Actual inventory is backed up here when creative mode is used
139         Inventory *inventory_backup;
140
141         bool craftresult_is_preview;
142
143         u16 hp;
144
145         u16 peer_id;
146
147 protected:
148         char m_name[PLAYERNAME_SIZE];
149         f32 m_pitch;
150         f32 m_yaw;
151         v3f m_speed;
152         v3f m_position;
153
154 public:
155
156 };
157
158 /*
159         Player on the server
160 */
161
162 class ServerRemotePlayer : public Player
163 {
164 public:
165         ServerRemotePlayer()
166         {
167         }
168         virtual ~ServerRemotePlayer()
169         {
170         }
171
172         virtual bool isLocal() const
173         {
174                 return false;
175         }
176
177         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
178         {
179         }
180         
181 private:
182 };
183
184 #ifndef SERVER
185
186 /*
187         All the other players on the client are these
188 */
189
190 class RemotePlayer : public Player, public scene::ISceneNode
191 {
192 public:
193         RemotePlayer(
194                 scene::ISceneNode* parent=NULL,
195                 IrrlichtDevice *device=NULL,
196                 s32 id=0);
197         
198         virtual ~RemotePlayer();
199
200         /*
201                 ISceneNode methods
202         */
203
204         virtual void OnRegisterSceneNode()
205         {
206                 if (IsVisible)
207                         SceneManager->registerNodeForRendering(this);
208
209                 ISceneNode::OnRegisterSceneNode();
210         }
211
212         virtual void render()
213         {
214                 // Do nothing
215         }
216         
217         virtual const core::aabbox3d<f32>& getBoundingBox() const
218         {
219                 return m_box;
220         }
221
222         void setPosition(const v3f &position)
223         {
224                 m_oldpos = m_showpos;
225                 
226                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
227                         m_pos_animation_time = m_pos_animation_time_counter;
228                 else
229                         m_pos_animation_time = m_pos_animation_time * 0.9
230                                         + m_pos_animation_time_counter * 0.1;
231                 m_pos_animation_time_counter = 0;
232                 m_pos_animation_counter = 0;
233                 
234                 Player::setPosition(position);
235                 //ISceneNode::setPosition(position);
236         }
237
238         virtual void setYaw(f32 yaw)
239         {
240                 Player::setYaw(yaw);
241                 ISceneNode::setRotation(v3f(0, -yaw, 0));
242         }
243
244         bool isLocal() const
245         {
246                 return false;
247         }
248
249         void updateName(const char *name);
250
251         virtual void updateLight(u8 light_at_pos)
252         {
253                 if(m_node == NULL)
254                         return;
255
256                 u8 li = decode_light(light_at_pos);
257                 video::SColor color(255,li,li,li);
258
259                 scene::IMesh *mesh = m_node->getMesh();
260                 
261                 u16 mc = mesh->getMeshBufferCount();
262                 for(u16 j=0; j<mc; j++)
263                 {
264                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
265                         video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
266                         u16 vc = buf->getVertexCount();
267                         for(u16 i=0; i<vc; i++)
268                         {
269                                 vertices[i].Color = color;
270                         }
271                 }
272         }
273         
274         void move(f32 dtime, Map &map, f32 pos_max_d);
275
276 private:
277         scene::IMeshSceneNode *m_node;
278         scene::ITextSceneNode* m_text;
279         core::aabbox3d<f32> m_box;
280
281         v3f m_oldpos;
282         f32 m_pos_animation_counter;
283         f32 m_pos_animation_time;
284         f32 m_pos_animation_time_counter;
285         v3f m_showpos;
286 };
287
288 #endif // !SERVER
289
290 #ifndef SERVER
291 struct PlayerControl
292 {
293         PlayerControl()
294         {
295                 up = false;
296                 down = false;
297                 left = false;
298                 right = false;
299                 jump = false;
300                 aux1 = false;
301                 sneak = false;
302                 pitch = 0;
303                 yaw = 0;
304         }
305         PlayerControl(
306                 bool a_up,
307                 bool a_down,
308                 bool a_left,
309                 bool a_right,
310                 bool a_jump,
311                 bool a_aux1,
312                 bool a_sneak,
313                 float a_pitch,
314                 float a_yaw
315         )
316         {
317                 up = a_up;
318                 down = a_down;
319                 left = a_left;
320                 right = a_right;
321                 jump = a_jump;
322                 aux1 = a_aux1;
323                 sneak = a_sneak;
324                 pitch = a_pitch;
325                 yaw = a_yaw;
326         }
327         bool up;
328         bool down;
329         bool left;
330         bool right;
331         bool jump;
332         bool aux1;
333         bool sneak;
334         float pitch;
335         float yaw;
336 };
337
338 class LocalPlayer : public Player
339 {
340 public:
341         LocalPlayer();
342         virtual ~LocalPlayer();
343
344         bool isLocal() const
345         {
346                 return true;
347         }
348
349         void move(f32 dtime, Map &map, f32 pos_max_d,
350                         core::list<CollisionInfo> *collision_info);
351         void move(f32 dtime, Map &map, f32 pos_max_d);
352
353         void applyControl(float dtime);
354         
355         PlayerControl control;
356
357 private:
358         // This is used for determining the sneaking range
359         v3s16 m_sneak_node;
360         // Whether the player is allowed to sneak
361         bool m_sneak_node_exists;
362 };
363 #endif // !SERVER
364
365 #endif
366