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