]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.h
Create framework for getting rid of global definitions of node/tool/item/whatever...
[dragonfireclient.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, IGameDef *gamedef);
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         
154         u8 light;
155
156         Inventory inventory;
157         // Actual inventory is backed up here when creative mode is used
158         Inventory *inventory_backup;
159
160         bool craftresult_is_preview;
161
162         u16 hp;
163
164         u16 peer_id;
165
166 protected:
167         char m_name[PLAYERNAME_SIZE];
168         u16 m_selected_item;
169         f32 m_pitch;
170         f32 m_yaw;
171         v3f m_speed;
172         v3f m_position;
173
174 public:
175
176 };
177
178 /*
179         Player on the server
180 */
181
182 #include "serverobject.h"
183 #include "content_object.h" // Object type IDs
184
185 class ServerRemotePlayer : public Player, public ServerActiveObject
186 {
187 public:
188         ServerRemotePlayer(ServerEnvironment *env):
189                 ServerActiveObject(env, v3f(0,0,0))
190         {
191         }
192         ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
193                         const char *name_):
194                 ServerActiveObject(env, pos_)
195         {
196                 setPosition(pos_);
197                 peer_id = peer_id_;
198                 updateName(name_);
199         }
200         virtual ~ServerRemotePlayer()
201         {
202         }
203
204         virtual bool isLocal() const
205         {
206                 return false;
207         }
208
209         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
210         {
211         }
212         
213         virtual void setPosition(const v3f &position)
214         {
215                 Player::setPosition(position);
216                 ServerActiveObject::setBasePosition(position);
217         }
218         
219         /* ServerActiveObject interface */
220
221         u8 getType() const
222                 {return ACTIVEOBJECT_TYPE_PLAYER;}
223         virtual std::string getDescription(){return getName();}
224         // Returns a reference
225         virtual InventoryItem* getWieldedItem();
226         virtual void damageWieldedItem(u16 amount);
227         // If all fits, eats item and returns true. Otherwise returns false.
228         virtual bool addToInventory(InventoryItem *item);
229         virtual void setHP(s16 hp_);
230         virtual s16 getHP();
231
232 private:
233 };
234
235 #ifndef SERVER
236
237 /*
238         All the other players on the client are these
239 */
240
241 class RemotePlayer : public Player, public scene::ISceneNode
242 {
243 public:
244         RemotePlayer(
245                 scene::ISceneNode* parent=NULL,
246                 IrrlichtDevice *device=NULL,
247                 s32 id=0);
248         
249         virtual ~RemotePlayer();
250
251         /*
252                 ISceneNode methods
253         */
254
255         virtual void OnRegisterSceneNode()
256         {
257                 if (IsVisible)
258                         SceneManager->registerNodeForRendering(this);
259
260                 ISceneNode::OnRegisterSceneNode();
261         }
262
263         virtual void render()
264         {
265                 // Do nothing
266         }
267         
268         virtual const core::aabbox3d<f32>& getBoundingBox() const
269         {
270                 return m_box;
271         }
272
273         void setPosition(const v3f &position)
274         {
275                 m_oldpos = m_showpos;
276                 
277                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
278                         m_pos_animation_time = m_pos_animation_time_counter;
279                 else
280                         m_pos_animation_time = m_pos_animation_time * 0.9
281                                         + m_pos_animation_time_counter * 0.1;
282                 m_pos_animation_time_counter = 0;
283                 m_pos_animation_counter = 0;
284                 
285                 Player::setPosition(position);
286                 //ISceneNode::setPosition(position);
287         }
288
289         virtual void setYaw(f32 yaw)
290         {
291                 Player::setYaw(yaw);
292                 ISceneNode::setRotation(v3f(0, -yaw, 0));
293         }
294
295         bool isLocal() const
296         {
297                 return false;
298         }
299
300         void updateName(const char *name);
301
302         virtual void updateLight(u8 light_at_pos)
303         {
304                 Player::updateLight(light_at_pos);
305
306                 if(m_node == NULL)
307                         return;
308
309                 u8 li = decode_light(light_at_pos);
310                 video::SColor color(255,li,li,li);
311                 setMeshVerticesColor(m_node->getMesh(), color);
312         }
313         
314         void move(f32 dtime, Map &map, f32 pos_max_d);
315
316 private:
317         scene::IMeshSceneNode *m_node;
318         scene::ITextSceneNode* m_text;
319         core::aabbox3d<f32> m_box;
320
321         v3f m_oldpos;
322         f32 m_pos_animation_counter;
323         f32 m_pos_animation_time;
324         f32 m_pos_animation_time_counter;
325         v3f m_showpos;
326 };
327
328 #endif // !SERVER
329
330 #ifndef SERVER
331 struct PlayerControl
332 {
333         PlayerControl()
334         {
335                 up = false;
336                 down = false;
337                 left = false;
338                 right = false;
339                 jump = false;
340                 aux1 = false;
341                 sneak = false;
342                 pitch = 0;
343                 yaw = 0;
344         }
345         PlayerControl(
346                 bool a_up,
347                 bool a_down,
348                 bool a_left,
349                 bool a_right,
350                 bool a_jump,
351                 bool a_aux1,
352                 bool a_sneak,
353                 float a_pitch,
354                 float a_yaw
355         )
356         {
357                 up = a_up;
358                 down = a_down;
359                 left = a_left;
360                 right = a_right;
361                 jump = a_jump;
362                 aux1 = a_aux1;
363                 sneak = a_sneak;
364                 pitch = a_pitch;
365                 yaw = a_yaw;
366         }
367         bool up;
368         bool down;
369         bool left;
370         bool right;
371         bool jump;
372         bool aux1;
373         bool sneak;
374         float pitch;
375         float yaw;
376 };
377
378 class LocalPlayer : public Player
379 {
380 public:
381         LocalPlayer();
382         virtual ~LocalPlayer();
383
384         bool isLocal() const
385         {
386                 return true;
387         }
388         
389         void move(f32 dtime, Map &map, f32 pos_max_d,
390                         core::list<CollisionInfo> *collision_info);
391         void move(f32 dtime, Map &map, f32 pos_max_d);
392
393         void applyControl(float dtime);
394         
395         PlayerControl control;
396
397 private:
398         // This is used for determining the sneaking range
399         v3s16 m_sneak_node;
400         // Whether the player is allowed to sneak
401         bool m_sneak_node_exists;
402 };
403 #endif // !SERVER
404
405 #endif
406