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