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