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