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