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