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