3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 #include "common_irrlicht.h"
24 #include "inventory.h"
25 #include "collision.h"
27 #define PLAYERNAME_SIZE 20
29 #define PLAYERNAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"
39 Player(IGameDef *gamedef);
42 void resetInventory();
44 //void move(f32 dtime, Map &map);
45 virtual void move(f32 dtime, Map &map, f32 pos_max_d) = 0;
52 void setSpeed(v3f speed)
57 // Y direction is ignored
58 void accelerate(v3f target_speed, f32 max_increase);
65 v3s16 getLightPosition() const;
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);
77 return m_position + getEyeOffset();
80 virtual void setPosition(const v3f &position)
82 m_position = position;
85 void setPitch(f32 pitch)
90 virtual void setYaw(f32 yaw)
105 virtual void updateName(const char *name)
107 snprintf(m_name, PLAYERNAME_SIZE, "%s", name);
110 virtual void wieldItem(u16 item);
111 virtual const InventoryItem *getWieldItem() const
113 const InventoryList *list = inventory.getList("main");
115 return list->getItem(m_selected_item);
119 const char * getName()
124 virtual bool isLocal() const = 0;
126 virtual void updateLight(u8 light_at_pos)
128 light = light_at_pos;
131 // NOTE: Use peer_id == 0 for disconnected
132 /*virtual bool isClientConnected() { return false; }
133 virtual void setClientConnected(bool) {}*/
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.
140 void serialize(std::ostream &os);
141 void deSerialize(std::istream &is);
143 bool touching_ground;
144 // This oscillates so that the player jumps a bit above the surface
146 // This is more stable and defines the maximum speed of the player
147 bool in_water_stable;
154 // Actual inventory is backed up here when creative mode is used
155 Inventory *inventory_backup;
157 bool craftresult_is_preview;
166 char m_name[PLAYERNAME_SIZE];
181 #include "serverobject.h"
182 #include "content_object.h" // Object type IDs
184 class ServerRemotePlayer : public Player, public ServerActiveObject
187 ServerRemotePlayer(ServerEnvironment *env);
188 ServerRemotePlayer(ServerEnvironment *env, v3f pos_, u16 peer_id_,
191 virtual ~ServerRemotePlayer()
194 virtual bool isLocal() const
197 virtual void move(f32 dtime, Map &map, f32 pos_max_d)
201 virtual void setPosition(const v3f &position)
203 Player::setPosition(position);
204 ServerActiveObject::setBasePosition(position);
207 /* ServerActiveObject interface */
210 {return ACTIVEOBJECT_TYPE_PLAYER;}
212 virtual void setPos(v3f pos)
215 // Movement caused by this command is always valid
216 m_last_good_position = pos;
217 m_last_good_position_age = 0;
219 virtual void moveTo(v3f pos, bool continuous)
222 // Movement caused by this command is always valid
223 m_last_good_position = pos;
224 m_last_good_position_age = 0;
227 virtual std::string getDescription(){return getName();}
228 // Returns a reference
229 virtual InventoryItem* getWieldedItem();
230 virtual void damageWieldedItem(u16 amount);
231 // If all fits, eats item and returns true. Otherwise returns false.
232 virtual bool addToInventory(InventoryItem *item);
233 virtual void setHP(s16 hp_);
236 v3f m_last_good_position;
237 float m_last_good_position_age;
245 All the other players on the client are these
248 class RemotePlayer : public Player, public scene::ISceneNode
253 scene::ISceneNode* parent=NULL,
254 IrrlichtDevice *device=NULL,
257 virtual ~RemotePlayer();
263 virtual void OnRegisterSceneNode()
266 SceneManager->registerNodeForRendering(this);
268 ISceneNode::OnRegisterSceneNode();
271 virtual void render()
276 virtual const core::aabbox3d<f32>& getBoundingBox() const
281 void setPosition(const v3f &position)
283 m_oldpos = m_showpos;
285 if(m_pos_animation_time < 0.001 || m_pos_animation_time > 1.0)
286 m_pos_animation_time = m_pos_animation_time_counter;
288 m_pos_animation_time = m_pos_animation_time * 0.9
289 + m_pos_animation_time_counter * 0.1;
290 m_pos_animation_time_counter = 0;
291 m_pos_animation_counter = 0;
293 Player::setPosition(position);
294 //ISceneNode::setPosition(position);
297 virtual void setYaw(f32 yaw)
300 ISceneNode::setRotation(v3f(0, -yaw, 0));
308 void updateName(const char *name);
310 virtual void updateLight(u8 light_at_pos)
312 Player::updateLight(light_at_pos);
317 u8 li = decode_light(light_at_pos);
318 video::SColor color(255,li,li,li);
319 setMeshVerticesColor(m_node->getMesh(), color);
322 void move(f32 dtime, Map &map, f32 pos_max_d);
325 scene::IMeshSceneNode *m_node;
326 scene::ITextSceneNode* m_text;
327 core::aabbox3d<f32> m_box;
330 f32 m_pos_animation_counter;
331 f32 m_pos_animation_time;
332 f32 m_pos_animation_time_counter;
386 class LocalPlayer : public Player
389 LocalPlayer(IGameDef *gamedef);
390 virtual ~LocalPlayer();
397 void move(f32 dtime, Map &map, f32 pos_max_d,
398 core::list<CollisionInfo> *collision_info);
399 void move(f32 dtime, Map &map, f32 pos_max_d);
401 void applyControl(float dtime);
403 PlayerControl control;
406 // This is used for determining the sneaking range
408 // Whether the player is allowed to sneak
409 bool m_sneak_node_exists;