CraftItem rework and Lua interface
[oweals/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
29 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
30
31
32 class Map;
33 class IGameDef;
34
35 class Player
36 {
37 public:
38
39         Player(IGameDef *gamedef);
40         virtual ~Player();
41
42         void resetInventory();
43
44         //void move(f32 dtime, Map &map);
45         virtual void move(f32 dtime, Map &map, f32 pos_max_d) = 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         v3s16 getLightPosition() const;
66
67         v3f getEyeOffset()
68         {
69                 // This is at the height of the eyes of the current figure
70                 // return v3f(0, BS+BS/2, 0);
71                 // This is more like in minecraft
72                 return v3f(0,BS+(5*BS)/8,0);
73         }
74
75         v3f getEyePosition()
76         {
77                 return m_position + getEyeOffset();
78         }
79
80         virtual void setPosition(const v3f &position)
81         {
82                 m_position = position;
83         }
84
85         void setPitch(f32 pitch)
86         {
87                 m_pitch = pitch;
88         }
89
90         virtual void setYaw(f32 yaw)
91         {
92                 m_yaw = yaw;
93         }
94
95         f32 getPitch()
96         {
97                 return m_pitch;
98         }
99
100         f32 getYaw()
101         {
102                 return m_yaw;
103         }
104
105         virtual void updateName(const char *name)
106         {
107                 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
108         }
109
110         virtual void wieldItem(u16 item);
111         virtual const InventoryItem *getWieldItem() const
112         {
113                 const InventoryList *list = inventory.getList("main");
114                 if (list)
115                         return list->getItem(m_selected_item);
116                 return NULL;
117         }
118
119         const char * getName()
120         {
121                 return m_name;
122         }
123
124         virtual bool isLocal() const = 0;
125
126         virtual void updateLight(u8 light_at_pos)
127         {
128                 light = light_at_pos;
129         }
130         
131         // NOTE: Use peer_id == 0 for disconnected
132         /*virtual bool isClientConnected() { return false; }
133         virtual void setClientConnected(bool) {}*/
134         
135         /*
136                 serialize() writes a bunch of text that can contain
137                 any characters except a '\0', and such an ending that
138                 deSerialize stops reading exactly at the right point.
139         */
140         void serialize(std::ostream &os);
141         void deSerialize(std::istream &is);
142
143         bool touching_ground;
144         // This oscillates so that the player jumps a bit above the surface
145         bool in_water;
146         // This is more stable and defines the maximum speed of the player
147         bool in_water_stable;
148         bool is_climbing;
149         bool swimming_up;
150         
151         u8 light;
152
153         Inventory inventory;
154         // Actual inventory is backed up here when creative mode is used
155         Inventory *inventory_backup;
156
157         bool craftresult_is_preview;
158
159         u16 hp;
160
161         u16 peer_id;
162
163 protected:
164         IGameDef *m_gamedef;
165
166         char m_name[PLAYERNAME_SIZE];
167         u16 m_selected_item;
168         f32 m_pitch;
169         f32 m_yaw;
170         v3f m_speed;
171         v3f m_position;
172
173 public:
174
175 };
176
177 /*
178         Player on the server
179 */
180
181 #include "serverobject.h"
182 #include "content_object.h" // Object type IDs
183
184 class ServerRemotePlayer : public Player, public ServerActiveObject
185 {
186 public:
187         ServerRemotePlayer(ServerEnvironment *env);
188         ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
189                         const char *name_);
190
191         virtual ~ServerRemotePlayer();
192
193         virtual bool isLocal() const
194         { return false; }
195
196         virtual void move(f32 dtime, Map &map, f32 pos_max_d)
197         {
198         }
199         
200         virtual void setPosition(const v3f &position)
201         {
202                 Player::setPosition(position);
203                 ServerActiveObject::setBasePosition(position);
204         }
205         
206         /* ServerActiveObject interface */
207
208         u8 getType() const
209         {return ACTIVEOBJECT_TYPE_PLAYER;}
210         
211         virtual void setPos(v3f pos)
212         {
213                 setPosition(pos);
214                 // Movement caused by this command is always valid
215                 m_last_good_position = pos;
216                 m_last_good_position_age = 0;
217         }
218         virtual void moveTo(v3f pos, bool continuous)
219         {
220                 setPosition(pos);
221                 // Movement caused by this command is always valid
222                 m_last_good_position = pos;
223                 m_last_good_position_age = 0;
224         }
225         
226         virtual std::string getDescription(){return getName();}
227         // Returns a reference
228         virtual InventoryItem* getWieldedItem();
229         virtual void damageWieldedItem(u16 amount);
230         // If all fits, eats item and returns true. Otherwise returns false.
231         virtual bool addToInventory(InventoryItem *item);
232         virtual void addToInventoryLater(InventoryItem *item);
233         void clearAddToInventoryLater();
234         void completeAddToInventoryLater(u16 preferred_index);
235         virtual void setHP(s16 hp_);
236         virtual s16 getHP();
237         
238         v3f m_last_good_position;
239         float m_last_good_position_age;
240         std::vector<InventoryItem*> m_additional_items;
241         
242 private:
243 };
244
245 #ifndef SERVER
246
247 /*
248         All the other players on the client are these
249 */
250
251 class RemotePlayer : public Player, public scene::ISceneNode
252 {
253 public:
254         RemotePlayer(
255                 IGameDef *gamedef,
256                 scene::ISceneNode* parent=NULL,
257                 IrrlichtDevice *device=NULL,
258                 s32 id=0);
259         
260         virtual ~RemotePlayer();
261
262         /*
263                 ISceneNode methods
264         */
265
266         virtual void OnRegisterSceneNode()
267         {
268                 if (IsVisible)
269                         SceneManager->registerNodeForRendering(this);
270
271                 ISceneNode::OnRegisterSceneNode();
272         }
273
274         virtual void render()
275         {
276                 // Do nothing
277         }
278         
279         virtual const core::aabbox3d<f32>& getBoundingBox() const
280         {
281                 return m_box;
282         }
283
284         void setPosition(const v3f &position)
285         {
286                 m_oldpos = m_showpos;
287                 
288                 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
289                         m_pos_animation_time = m_pos_animation_time_counter;
290                 else
291                         m_pos_animation_time = m_pos_animation_time * 0.9
292                                         + m_pos_animation_time_counter * 0.1;
293                 m_pos_animation_time_counter = 0;
294                 m_pos_animation_counter = 0;
295                 
296                 Player::setPosition(position);
297                 //ISceneNode::setPosition(position);
298         }
299
300         virtual void setYaw(f32 yaw)
301         {
302                 Player::setYaw(yaw);
303                 ISceneNode::setRotation(v3f(0, -yaw, 0));
304         }
305
306         bool isLocal() const
307         {
308                 return false;
309         }
310
311         void updateName(const char *name);
312
313         virtual void updateLight(u8 light_at_pos)
314         {
315                 Player::updateLight(light_at_pos);
316
317                 if(m_node == NULL)
318                         return;
319
320                 u8 li = decode_light(light_at_pos);
321                 video::SColor color(255,li,li,li);
322                 setMeshVerticesColor(m_node->getMesh(), color);
323         }
324         
325         void move(f32 dtime, Map &map, f32 pos_max_d);
326
327 private:
328         scene::IMeshSceneNode *m_node;
329         scene::ITextSceneNode* m_text;
330         core::aabbox3d<f32> m_box;
331
332         v3f m_oldpos;
333         f32 m_pos_animation_counter;
334         f32 m_pos_animation_time;
335         f32 m_pos_animation_time_counter;
336         v3f m_showpos;
337 };
338
339 #endif // !SERVER
340
341 #ifndef SERVER
342 struct PlayerControl
343 {
344         PlayerControl()
345         {
346                 up = false;
347                 down = false;
348                 left = false;
349                 right = false;
350                 jump = false;
351                 aux1 = false;
352                 sneak = false;
353                 pitch = 0;
354                 yaw = 0;
355         }
356         PlayerControl(
357                 bool a_up,
358                 bool a_down,
359                 bool a_left,
360                 bool a_right,
361                 bool a_jump,
362                 bool a_aux1,
363                 bool a_sneak,
364                 float a_pitch,
365                 float a_yaw
366         )
367         {
368                 up = a_up;
369                 down = a_down;
370                 left = a_left;
371                 right = a_right;
372                 jump = a_jump;
373                 aux1 = a_aux1;
374                 sneak = a_sneak;
375                 pitch = a_pitch;
376                 yaw = a_yaw;
377         }
378         bool up;
379         bool down;
380         bool left;
381         bool right;
382         bool jump;
383         bool aux1;
384         bool sneak;
385         float pitch;
386         float yaw;
387 };
388
389 class LocalPlayer : public Player
390 {
391 public:
392         LocalPlayer(IGameDef *gamedef);
393         virtual ~LocalPlayer();
394
395         bool isLocal() const
396         {
397                 return true;
398         }
399         
400         void move(f32 dtime, Map &map, f32 pos_max_d,
401                         core::list<CollisionInfo> *collision_info);
402         void move(f32 dtime, Map &map, f32 pos_max_d);
403
404         void applyControl(float dtime);
405         
406         PlayerControl control;
407
408 private:
409         // This is used for determining the sneaking range
410         v3s16 m_sneak_node;
411         // Whether the player is allowed to sneak
412         bool m_sneak_node_exists;
413 };
414 #endif // !SERVER
415
416 #endif
417