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