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