Simplistic wielded tool lighting, added setMeshVerticesColor to utility.h and refacto...
[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 getEyeOffset()
71         {
72                 // This is at the height of the eyes of the current figure
73                 // return v3f(0, BS+BS/2, 0);
74                 // This is more like in minecraft
75                 return v3f(0,BS+(5*BS)/8,0);
76         }
77
78         v3f getEyePosition()
79         {
80                 return m_position + getEyeOffset();
81         }
82
83         virtual void setPosition(const v3f &position)
84         {
85                 m_position = position;
86         }
87
88         void setPitch(f32 pitch)
89         {
90                 m_pitch = pitch;
91         }
92
93         virtual void setYaw(f32 yaw)
94         {
95                 m_yaw = yaw;
96         }
97
98         f32 getPitch()
99         {
100                 return m_pitch;
101         }
102
103         f32 getYaw()
104         {
105                 return m_yaw;
106         }
107
108         virtual void updateName(const char *name)
109         {
110                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
111         }
112
113         virtual void wieldItem(u16 item);
114         virtual const InventoryItem *getWieldItem() const
115         {
116                 const InventoryList *list = inventory.getList("main");
117                 if (list)
118                         return list->getItem(m_selected_item);
119                 return NULL;
120         }
121
122         const char * getName()
123         {
124                 return m_name;
125         }
126
127         virtual bool isLocal() const = 0;
128
129         virtual void updateLight(u8 light_at_pos)
130         {
131                 light = light_at_pos;
132         }
133         
134         // NOTE: Use peer_id == 0 for disconnected
135         /*virtual bool isClientConnected() { return false; }
136         virtual void setClientConnected(bool) {}*/
137         
138         /*
139                 serialize() writes a bunch of text that can contain
140                 any characters except a '\0', and such an ending that
141                 deSerialize stops reading exactly at the right point.
142         */
143         void serialize(std::ostream &os);
144         void deSerialize(std::istream &is);
145
146         bool touching_ground;
147         // This oscillates so that the player jumps a bit above the surface
148         bool in_water;
149         // This is more stable and defines the maximum speed of the player
150         bool in_water_stable;
151         bool is_climbing;
152         bool swimming_up;
153         bool is_frozen;
154         
155         u8 light;
156
157         Inventory inventory;
158         // Actual inventory is backed up here when creative mode is used
159         Inventory *inventory_backup;
160
161         bool craftresult_is_preview;
162
163         u16 hp;
164
165         u16 peer_id;
166
167 protected:
168         char m_name[PLAYERNAME_SIZE];
169         u16 m_selected_item;
170         f32 m_pitch;
171         f32 m_yaw;
172         v3f m_speed;
173         v3f m_position;
174
175 public:
176
177 };
178
179 /*
180         Player on the server
181 */
182
183 class ServerRemotePlayer : public Player
184 {
185 public:
186         ServerRemotePlayer()
187         {
188         }
189         virtual ~ServerRemotePlayer()
190         {
191         }
192
193         virtual bool isLocal() const
194         {
195                 return false;
196         }
197
198         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
199         {
200         }
201         
202 private:
203 };
204
205 #ifndef SERVER
206
207 /*
208         All the other players on the client are these
209 */
210
211 class RemotePlayer : public Player, public scene::ISceneNode
212 {
213 public:
214         RemotePlayer(
215                 scene::ISceneNode* parent=NULL,
216                 IrrlichtDevice *device=NULL,
217                 s32 id=0);
218         
219         virtual ~RemotePlayer();
220
221         /*
222                 ISceneNode methods
223         */
224
225         virtual void OnRegisterSceneNode()
226         {
227                 if (IsVisible)
228                         SceneManager->registerNodeForRendering(this);
229
230                 ISceneNode::OnRegisterSceneNode();
231         }
232
233         virtual void render()
234         {
235                 // Do nothing
236         }
237         
238         virtual const core::aabbox3d<f32>& getBoundingBox() const
239         {
240                 return m_box;
241         }
242
243         void setPosition(const v3f &position)
244         {
245                 m_oldpos = m_showpos;
246                 
247                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
248                         m_pos_animation_time = m_pos_animation_time_counter;
249                 else
250                         m_pos_animation_time = m_pos_animation_time * 0.9
251                                         + m_pos_animation_time_counter * 0.1;
252                 m_pos_animation_time_counter = 0;
253                 m_pos_animation_counter = 0;
254                 
255                 Player::setPosition(position);
256                 //ISceneNode::setPosition(position);
257         }
258
259         virtual void setYaw(f32 yaw)
260         {
261                 Player::setYaw(yaw);
262                 ISceneNode::setRotation(v3f(0, -yaw, 0));
263         }
264
265         bool isLocal() const
266         {
267                 return false;
268         }
269
270         void updateName(const char *name);
271
272         virtual void updateLight(u8 light_at_pos)
273         {
274                 Player::updateLight(light_at_pos);
275
276                 if(m_node == NULL)
277                         return;
278
279                 u8 li = decode_light(light_at_pos);
280                 video::SColor color(255,li,li,li);
281                 setMeshVerticesColor(m_node->getMesh(), color);
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