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