Made dungeons a bit rarer
[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 class Map;
33
34 class Player
35 {
36 public:
37         Player();
38         virtual ~Player();
39
40         //void move(f32 dtime, Map &map);
41         virtual void move(f32 dtime, Map &map) = 0;
42
43         v3f getSpeed()
44         {
45                 return m_speed;
46         }
47
48         void setSpeed(v3f speed)
49         {
50                 m_speed = speed;
51         }
52         
53         // Y direction is ignored
54         void accelerate(v3f target_speed, f32 max_increase);
55
56         v3f getPosition()
57         {
58                 return m_position;
59         }
60
61         virtual void setPosition(v3f position)
62         {
63                 m_position = position;
64         }
65
66         void setPitch(f32 pitch)
67         {
68                 m_pitch = pitch;
69         }
70
71         virtual void setYaw(f32 yaw)
72         {
73                 m_yaw = yaw;
74         }
75
76         f32 getPitch()
77         {
78                 return m_pitch;
79         }
80
81         f32 getYaw()
82         {
83                 return m_yaw;
84         }
85
86         virtual void updateName(const char *name)
87         {
88                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
89         }
90
91         const char * getName()
92         {
93                 return m_name;
94         }
95
96         virtual bool isLocal() const = 0;
97
98         virtual void updateLight(u8 light_at_pos) {};
99
100         bool touching_ground;
101         bool in_water;
102         
103         Inventory inventory;
104
105         u16 peer_id;
106
107 protected:
108         char m_name[PLAYERNAME_SIZE];
109         f32 m_pitch;
110         f32 m_yaw;
111         v3f m_speed;
112         v3f m_position;
113 };
114
115 class ServerRemotePlayer : public Player
116 {
117 public:
118         ServerRemotePlayer()
119         {
120         }
121         virtual ~ServerRemotePlayer()
122         {
123         }
124
125         bool isLocal() const
126         {
127                 return false;
128         }
129
130         void move(f32 dtime, Map &map)
131         {
132         }
133
134 private:
135 };
136
137 #ifndef SERVER
138
139 class RemotePlayer : public Player, public scene::ISceneNode
140 {
141 public:
142         RemotePlayer(
143                 scene::ISceneNode* parent=NULL,
144                 IrrlichtDevice *device=NULL,
145                 s32 id=0);
146         
147         virtual ~RemotePlayer();
148
149         /*
150                 ISceneNode methods
151         */
152
153         virtual void OnRegisterSceneNode()
154         {
155                 if (IsVisible)
156                         SceneManager->registerNodeForRendering(this);
157
158                 ISceneNode::OnRegisterSceneNode();
159         }
160
161         virtual void render()
162         {
163                 // Do nothing
164         }
165         
166         virtual const core::aabbox3d<f32>& getBoundingBox() const
167         {
168                 return m_box;
169         }
170
171         void setPosition(v3f position)
172         {
173                 m_oldpos = m_showpos;
174                 
175                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
176                         m_pos_animation_time = m_pos_animation_time_counter;
177                 else
178                         m_pos_animation_time = m_pos_animation_time * 0.9
179                                         + m_pos_animation_time_counter * 0.1;
180                 m_pos_animation_time_counter = 0;
181                 m_pos_animation_counter = 0;
182                 
183                 Player::setPosition(position);
184                 //ISceneNode::setPosition(position);
185         }
186
187         virtual void setYaw(f32 yaw)
188         {
189                 Player::setYaw(yaw);
190                 ISceneNode::setRotation(v3f(0, -yaw, 0));
191         }
192
193         bool isLocal() const
194         {
195                 return false;
196         }
197
198         void updateName(const char *name);
199
200         virtual void updateLight(u8 light_at_pos)
201         {
202                 if(m_node == NULL)
203                         return;
204
205                 u8 li = decode_light(light_at_pos);
206                 video::SColor color(255,li,li,li);
207
208                 scene::IMesh *mesh = m_node->getMesh();
209                 
210                 u16 mc = mesh->getMeshBufferCount();
211                 for(u16 j=0; j<mc; j++)
212                 {
213                         scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
214                         video::S3DVertex *vertices = (video::S3DVertex*)buf->getVertices();
215                         u16 vc = buf->getVertexCount();
216                         for(u16 i=0; i<vc; i++)
217                         {
218                                 vertices[i].Color = color;
219                         }
220                 }
221         }
222         
223         void move(f32 dtime, Map &map);
224
225 private:
226         scene::IMeshSceneNode *m_node;
227         scene::ITextSceneNode* m_text;
228         core::aabbox3d<f32> m_box;
229
230         v3f m_oldpos;
231         f32 m_pos_animation_counter;
232         f32 m_pos_animation_time;
233         f32 m_pos_animation_time_counter;
234         v3f m_showpos;
235 };
236
237 #endif
238
239 #ifndef SERVER
240 struct PlayerControl
241 {
242         PlayerControl()
243         {
244                 up = false;
245                 down = false;
246                 left = false;
247                 right = false;
248                 jump = false;
249                 superspeed = false;
250                 pitch = 0;
251                 yaw = 0;
252         }
253         PlayerControl(
254                 bool a_up,
255                 bool a_down,
256                 bool a_left,
257                 bool a_right,
258                 bool a_jump,
259                 bool a_superspeed,
260                 float a_pitch,
261                 float a_yaw
262         )
263         {
264                 up = a_up;
265                 down = a_down;
266                 left = a_left;
267                 right = a_right;
268                 jump = a_jump;
269                 superspeed = a_superspeed;
270                 pitch = a_pitch;
271                 yaw = a_yaw;
272         }
273         bool up;
274         bool down;
275         bool left;
276         bool right;
277         bool jump;
278         bool superspeed;
279         float pitch;
280         float yaw;
281 };
282
283 class LocalPlayer : public Player
284 {
285 public:
286         LocalPlayer();
287         virtual ~LocalPlayer();
288
289         bool isLocal() const
290         {
291                 return true;
292         }
293
294         void move(f32 dtime, Map &map);
295
296         void applyControl(float dtime);
297         
298         PlayerControl control;
299
300 private:
301 };
302 #endif // !SERVER
303
304 #endif
305