]> git.lizzy.rs Git - dragonfireclient.git/blob - src/player.h
forgot to add mapblock_nodemod.h
[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 swimming_up;
122         
123         Inventory inventory;
124
125         bool craftresult_is_preview;
126
127         u16 hp;
128
129         u16 peer_id;
130
131 protected:
132         char m_name[PLAYERNAME_SIZE];
133         f32 m_pitch;
134         f32 m_yaw;
135         v3f m_speed;
136         v3f m_position;
137
138 public:
139
140 };
141
142 /*
143         Player on the server
144 */
145
146 class ServerRemotePlayer : public Player
147 {
148 public:
149         ServerRemotePlayer()
150         {
151         }
152         virtual ~ServerRemotePlayer()
153         {
154         }
155
156         virtual bool isLocal() const
157         {
158                 return false;
159         }
160
161         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
162         {
163         }
164         
165 private:
166 };
167
168 #ifndef SERVER
169
170 /*
171         All the other players on the client are these
172 */
173
174 class RemotePlayer : public Player, public scene::ISceneNode
175 {
176 public:
177         RemotePlayer(
178                 scene::ISceneNode* parent=NULL,
179                 IrrlichtDevice *device=NULL,
180                 s32 id=0);
181         
182         virtual ~RemotePlayer();
183
184         /*
185                 ISceneNode methods
186         */
187
188         virtual void OnRegisterSceneNode()
189         {
190                 if (IsVisible)
191                         SceneManager->registerNodeForRendering(this);
192
193                 ISceneNode::OnRegisterSceneNode();
194         }
195
196         virtual void render()
197         {
198                 // Do nothing
199         }
200         
201         virtual const core::aabbox3d<f32>& getBoundingBox() const
202         {
203                 return m_box;
204         }
205
206         void setPosition(v3f position)
207         {
208                 m_oldpos = m_showpos;
209                 
210                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
211                         m_pos_animation_time = m_pos_animation_time_counter;
212                 else
213                         m_pos_animation_time = m_pos_animation_time * 0.9
214                                         + m_pos_animation_time_counter * 0.1;
215                 m_pos_animation_time_counter = 0;
216                 m_pos_animation_counter = 0;
217                 
218                 Player::setPosition(position);
219                 //ISceneNode::setPosition(position);
220         }
221
222         virtual void setYaw(f32 yaw)
223         {
224                 Player::setYaw(yaw);
225                 ISceneNode::setRotation(v3f(0, -yaw, 0));
226         }
227
228         bool isLocal() const
229         {
230                 return false;
231         }
232
233         void updateName(const char *name);
234
235         virtual void updateLight(u8 light_at_pos)
236         {
237                 if(m_node == NULL)
238                         return;
239
240                 u8 li = decode_light(light_at_pos);
241                 video::SColor color(255,li,li,li);
242
243                 scene::IMesh *mesh = m_node->getMesh();
244                 
245                 u16 mc = mesh->getMeshBufferCount();
246                 for(u16 j=0; j<mc; j++)
247                 {
248                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
249                         video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
250                         u16 vc = buf->getVertexCount();
251                         for(u16 i=0; i<vc; i++)
252                         {
253                                 vertices[i].Color = color;
254                         }
255                 }
256         }
257         
258         void move(f32 dtime, Map &map, f32 pos_max_d);
259
260 private:
261         scene::IMeshSceneNode *m_node;
262         scene::ITextSceneNode* m_text;
263         core::aabbox3d<f32> m_box;
264
265         v3f m_oldpos;
266         f32 m_pos_animation_counter;
267         f32 m_pos_animation_time;
268         f32 m_pos_animation_time_counter;
269         v3f m_showpos;
270 };
271
272 #endif // !SERVER
273
274 #ifndef SERVER
275 struct PlayerControl
276 {
277         PlayerControl()
278         {
279                 up = false;
280                 down = false;
281                 left = false;
282                 right = false;
283                 jump = false;
284                 aux1 = false;
285                 sneak = false;
286                 pitch = 0;
287                 yaw = 0;
288         }
289         PlayerControl(
290                 bool a_up,
291                 bool a_down,
292                 bool a_left,
293                 bool a_right,
294                 bool a_jump,
295                 bool a_aux1,
296                 bool a_sneak,
297                 float a_pitch,
298                 float a_yaw
299         )
300         {
301                 up = a_up;
302                 down = a_down;
303                 left = a_left;
304                 right = a_right;
305                 jump = a_jump;
306                 aux1 = a_aux1;
307                 sneak = a_sneak;
308                 pitch = a_pitch;
309                 yaw = a_yaw;
310         }
311         bool up;
312         bool down;
313         bool left;
314         bool right;
315         bool jump;
316         bool aux1;
317         bool sneak;
318         float pitch;
319         float yaw;
320 };
321
322 class LocalPlayer : public Player
323 {
324 public:
325         LocalPlayer();
326         virtual ~LocalPlayer();
327
328         bool isLocal() const
329         {
330                 return true;
331         }
332
333         void move(f32 dtime, Map &map, f32 pos_max_d,
334                         core::list<CollisionInfo> *collision_info);
335         void move(f32 dtime, Map &map, f32 pos_max_d);
336
337         void applyControl(float dtime);
338         
339         PlayerControl control;
340
341 private:
342         // This is used for determining the sneaking range
343         v3s16 m_sneak_node;
344         // Whether the player is allowed to sneak
345         bool m_sneak_node_exists;
346 };
347 #endif // !SERVER
348
349 #endif
350