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