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