Merge: New map directory structure and player passwords
[oweals/minetest.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 #define PASSWORD_SIZE 28       // Maximum password length. Allows for
29                                // base64-encoded SHA-1.
30
31 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.,"
32
33 // Player privileges. These form a bitmask stored in the privs field
34 // of the player, and define things they're allowed to do. See also
35 // the static methods Player::privsToString and stringToPrivs that
36 // convert these to human-readable form.
37 const u64 PRIV_BUILD = 1;       // Can build - i.e. modify the world
38                                 //  (not enforced yet)
39 const u64 PRIV_TELEPORT = 2;    // Can teleport
40 const u64 PRIV_SETTIME = 4;     // Can set the time
41 const u64 PRIV_PRIVS = 8;       // Can grant and revoke privileges
42 const u64 PRIV_SERVER = 16;     // Can manage the server (e.g. shutodwn ,settings)
43
44 const u64 PRIV_DEFAULT = PRIV_BUILD;
45 const u64 PRIV_ALL = 0x7FFFFFFFFFFFFFFFULL;
46 const u64 PRIV_INVALID = 0x8000000000000000ULL;
47
48 // Convert a privileges value into a human-readable string,
49 // with each component separated by a comma.
50 std::wstring privsToString(u64 privs);
51
52 // Converts a comma-seperated list of privilege values into a
53 // privileges value. The reverse of privsToString(). Returns
54 // PRIV_INVALID if there is anything wrong with the input.
55 u64 stringToPrivs(std::wstring str);
56
57
58 class Map;
59
60 class Player
61 {
62 public:
63
64
65         Player();
66         virtual ~Player();
67
68         void resetInventory();
69
70         //void move(f32 dtime, Map &map);
71         virtual void move(f32 dtime, Map &map, f32 pos_max_d) = 0;
72
73         v3f getSpeed()
74         {
75                 return m_speed;
76         }
77
78         void setSpeed(v3f speed)
79         {
80                 m_speed = speed;
81         }
82         
83         // Y direction is ignored
84         void accelerate(v3f target_speed, f32 max_increase);
85
86         v3f getPosition()
87         {
88                 return m_position;
89         }
90
91         virtual void setPosition(v3f position)
92         {
93                 m_position = position;
94         }
95
96         void setPitch(f32 pitch)
97         {
98                 m_pitch = pitch;
99         }
100
101         virtual void setYaw(f32 yaw)
102         {
103                 m_yaw = yaw;
104         }
105
106         f32 getPitch()
107         {
108                 return m_pitch;
109         }
110
111         f32 getYaw()
112         {
113                 return m_yaw;
114         }
115
116         virtual void updateName(const char *name)
117         {
118                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
119         }
120
121         const char * getName()
122         {
123                 return m_name;
124         }
125
126         virtual void updatePassword(const char *password)
127         {
128                 snprintf(m_password, PASSWORD_SIZE, "%s", password);
129         }
130
131         const char * getPassword()
132         {
133                 return m_password;
134         }
135
136         virtual bool isLocal() const = 0;
137
138         virtual void updateLight(u8 light_at_pos) {};
139         
140         // NOTE: Use peer_id == 0 for disconnected
141         /*virtual bool isClientConnected() { return false; }
142         virtual void setClientConnected(bool) {}*/
143         
144         /*
145                 serialize() writes a bunch of text that can contain
146                 any characters except a '\0', and such an ending that
147                 deSerialize stops reading exactly at the right point.
148         */
149         void serialize(std::ostream &os);
150         void deSerialize(std::istream &is);
151
152         bool touching_ground;
153         // This oscillates so that the player jumps a bit above the surface
154         bool in_water;
155         // This is more stable and defines the maximum speed of the player
156         bool in_water_stable;
157         bool swimming_up;
158         
159         Inventory inventory;
160
161         bool craftresult_is_preview;
162
163         u16 hp;
164
165         // Player's privileges - a bitmaps of PRIV_xxxx.
166         u64 privs;
167
168         u16 peer_id;
169
170 protected:
171         char m_name[PLAYERNAME_SIZE];
172         char m_password[PASSWORD_SIZE];
173         f32 m_pitch;
174         f32 m_yaw;
175         v3f m_speed;
176         v3f m_position;
177
178 public:
179
180 };
181
182 /*
183         Player on the server
184 */
185
186 class ServerRemotePlayer : public Player
187 {
188 public:
189         ServerRemotePlayer()
190         {
191         }
192         virtual ~ServerRemotePlayer()
193         {
194         }
195
196         virtual bool isLocal() const
197         {
198                 return false;
199         }
200
201         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
202         {
203         }
204         
205 private:
206 };
207
208 #ifndef SERVER
209
210 /*
211         All the other players on the client are these
212 */
213
214 class RemotePlayer : public Player, public scene::ISceneNode
215 {
216 public:
217         RemotePlayer(
218                 scene::ISceneNode* parent=NULL,
219                 IrrlichtDevice *device=NULL,
220                 s32 id=0);
221         
222         virtual ~RemotePlayer();
223
224         /*
225                 ISceneNode methods
226         */
227
228         virtual void OnRegisterSceneNode()
229         {
230                 if (IsVisible)
231                         SceneManager->registerNodeForRendering(this);
232
233                 ISceneNode::OnRegisterSceneNode();
234         }
235
236         virtual void render()
237         {
238                 // Do nothing
239         }
240         
241         virtual const core::aabbox3d<f32>& getBoundingBox() const
242         {
243                 return m_box;
244         }
245
246         void setPosition(v3f position)
247         {
248                 m_oldpos = m_showpos;
249                 
250                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
251                         m_pos_animation_time = m_pos_animation_time_counter;
252                 else
253                         m_pos_animation_time = m_pos_animation_time * 0.9
254                                         + m_pos_animation_time_counter * 0.1;
255                 m_pos_animation_time_counter = 0;
256                 m_pos_animation_counter = 0;
257                 
258                 Player::setPosition(position);
259                 //ISceneNode::setPosition(position);
260         }
261
262         virtual void setYaw(f32 yaw)
263         {
264                 Player::setYaw(yaw);
265                 ISceneNode::setRotation(v3f(0, -yaw, 0));
266         }
267
268         bool isLocal() const
269         {
270                 return false;
271         }
272
273         void updateName(const char *name);
274
275         virtual void updateLight(u8 light_at_pos)
276         {
277                 if(m_node == NULL)
278                         return;
279
280                 u8 li = decode_light(light_at_pos);
281                 video::SColor color(255,li,li,li);
282
283                 scene::IMesh *mesh = m_node->getMesh();
284                 
285                 u16 mc = mesh->getMeshBufferCount();
286                 for(u16 j=0; j<mc; j++)
287                 {
288                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
289                         video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
290                         u16 vc = buf->getVertexCount();
291                         for(u16 i=0; i<vc; i++)
292                         {
293                                 vertices[i].Color = color;
294                         }
295                 }
296         }
297         
298         void move(f32 dtime, Map &map, f32 pos_max_d);
299
300 private:
301         scene::IMeshSceneNode *m_node;
302         scene::ITextSceneNode* m_text;
303         core::aabbox3d<f32> m_box;
304
305         v3f m_oldpos;
306         f32 m_pos_animation_counter;
307         f32 m_pos_animation_time;
308         f32 m_pos_animation_time_counter;
309         v3f m_showpos;
310 };
311
312 #endif // !SERVER
313
314 #ifndef SERVER
315 struct PlayerControl
316 {
317         PlayerControl()
318         {
319                 up = false;
320                 down = false;
321                 left = false;
322                 right = false;
323                 jump = false;
324                 aux1 = false;
325                 sneak = false;
326                 pitch = 0;
327                 yaw = 0;
328         }
329         PlayerControl(
330                 bool a_up,
331                 bool a_down,
332                 bool a_left,
333                 bool a_right,
334                 bool a_jump,
335                 bool a_aux1,
336                 bool a_sneak,
337                 float a_pitch,
338                 float a_yaw
339         )
340         {
341                 up = a_up;
342                 down = a_down;
343                 left = a_left;
344                 right = a_right;
345                 jump = a_jump;
346                 aux1 = a_aux1;
347                 sneak = a_sneak;
348                 pitch = a_pitch;
349                 yaw = a_yaw;
350         }
351         bool up;
352         bool down;
353         bool left;
354         bool right;
355         bool jump;
356         bool aux1;
357         bool sneak;
358         float pitch;
359         float yaw;
360 };
361
362 class LocalPlayer : public Player
363 {
364 public:
365         LocalPlayer();
366         virtual ~LocalPlayer();
367
368         bool isLocal() const
369         {
370                 return true;
371         }
372
373         void move(f32 dtime, Map &map, f32 pos_max_d,
374                         core::list<CollisionInfo> *collision_info);
375         void move(f32 dtime, Map &map, f32 pos_max_d);
376
377         void applyControl(float dtime);
378         
379         PlayerControl control;
380
381 private:
382         // This is used for determining the sneaking range
383         v3s16 m_sneak_node;
384         // Whether the player is allowed to sneak
385         bool m_sneak_node_exists;
386 };
387 #endif // !SERVER
388
389 #endif
390