3 Copyright (C) 2010 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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
26 #include "connection.h"
27 #include "constants.h"
31 touching_ground(false),
33 peer_id(PEER_ID_INEXISTENT),
37 updateName("<not set>");
45 void Player::resetInventory()
48 inventory.addList("main", PLAYER_INVENTORY_SIZE);
49 inventory.addList("craft", 9);
50 inventory.addList("craftresult", 1);
53 // Y direction is ignored
54 void Player::accelerate(v3f target_speed, f32 max_increase)
56 v3f d_wanted = target_speed - m_speed;
58 f32 dl_wanted = d_wanted.getLength();
63 v3f d = d_wanted.normalize() * dl;
70 if(m_speed.X < target_speed.X - max_increase)
71 m_speed.X += max_increase;
72 else if(m_speed.X > target_speed.X + max_increase)
73 m_speed.X -= max_increase;
74 else if(m_speed.X < target_speed.X)
75 m_speed.X = target_speed.X;
76 else if(m_speed.X > target_speed.X)
77 m_speed.X = target_speed.X;
79 if(m_speed.Z < target_speed.Z - max_increase)
80 m_speed.Z += max_increase;
81 else if(m_speed.Z > target_speed.Z + max_increase)
82 m_speed.Z -= max_increase;
83 else if(m_speed.Z < target_speed.Z)
84 m_speed.Z = target_speed.Z;
85 else if(m_speed.Z > target_speed.Z)
86 m_speed.Z = target_speed.Z;
90 void Player::serialize(std::ostream &os)
92 // Utilize a Settings object for storing values
94 args.setS32("version", 1);
95 args.set("name", m_name);
96 args.setFloat("pitch", m_pitch);
97 args.setFloat("yaw", m_yaw);
98 args.setV3F("position", m_position);
102 os<<"PlayerArgsEnd\n";
104 inventory.serialize(os);
107 void Player::deSerialize(std::istream &is)
114 throw SerializationError
115 ("Player::deSerialize(): PlayerArgsEnd not found");
117 std::getline(is, line);
118 std::string trimmedline = trim(line);
119 if(trimmedline == "PlayerArgsEnd")
121 args.parseConfigLine(line);
124 //args.getS32("version");
125 std::string name = args.get("name");
126 updateName(name.c_str());
127 m_pitch = args.getFloat("pitch");
128 m_yaw = args.getFloat("yaw");
129 m_position = args.getV3F("position");
131 inventory.deSerialize(is);
140 RemotePlayer::RemotePlayer(
141 scene::ISceneNode* parent,
142 IrrlichtDevice *device,
144 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
147 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
149 if(parent != NULL && device != NULL)
151 // ISceneNode stores a member called SceneManager
152 scene::ISceneManager* mgr = SceneManager;
153 video::IVideoDriver* driver = mgr->getVideoDriver();
154 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
156 // Add a text node for showing the name
157 wchar_t wname[1] = {0};
158 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
159 wname, video::SColor(255,255,255,255), this);
160 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
162 // Attach a simple mesh to the player for showing an image
163 scene::SMesh *mesh = new scene::SMesh();
165 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
166 video::SColor c(255,255,255,255);
167 video::S3DVertex vertices[4] =
169 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
170 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
171 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
172 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
174 u16 indices[] = {0,1,2,2,3,0};
175 buf->append(vertices, 4, indices, 6);
177 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
178 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
179 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player.png").c_str()));
180 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
181 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
182 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
183 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
185 mesh->addMeshBuffer(buf);
189 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
190 video::SColor c(255,255,255,255);
191 video::S3DVertex vertices[4] =
193 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
194 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
195 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
196 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
198 u16 indices[] = {0,1,2,2,3,0};
199 buf->append(vertices, 4, indices, 6);
201 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
202 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
203 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player_back.png").c_str()));
204 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
205 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
206 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
208 mesh->addMeshBuffer(buf);
211 m_node = mgr->addMeshSceneNode(mesh, this);
213 m_node->setPosition(v3f(0,0,0));
217 RemotePlayer::~RemotePlayer()
219 if(SceneManager != NULL)
220 ISceneNode::remove();
223 void RemotePlayer::updateName(const char *name)
225 Player::updateName(name);
228 wchar_t wname[PLAYERNAME_SIZE];
229 mbstowcs(wname, m_name, strlen(m_name)+1);
230 m_text->setText(wname);
234 void RemotePlayer::move(f32 dtime, Map &map)
236 m_pos_animation_time_counter += dtime;
237 m_pos_animation_counter += dtime;
238 v3f movevector = m_position - m_oldpos;
240 if(m_pos_animation_time < 0.001)
243 moveratio = m_pos_animation_counter / m_pos_animation_time;
246 m_showpos = m_oldpos + movevector * moveratio;
248 ISceneNode::setPosition(m_showpos);
258 LocalPlayer::LocalPlayer()
262 LocalPlayer::~LocalPlayer()
266 void LocalPlayer::move(f32 dtime, Map &map)
268 v3f position = getPosition();
269 v3f oldpos = position;
270 v3s16 oldpos_i = floatToInt(oldpos);
272 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
273 <<oldpos_i.Z<<")"<<std::endl;*/
275 position += m_speed * dtime;
277 bool haxmode = g_settings.getBool("haxmode");
279 // Skip collision detection if player is non-local
280 if(isLocal() == false || haxmode)
282 setPosition(position);
290 v3s16 pos_i = floatToInt(position);
293 Check if player is in water
298 v3s16 pp = floatToInt(position + v3f(0,0,0));
299 in_water = content_liquid(map.getNode(pp).d);
303 v3s16 pp = floatToInt(position + v3f(0,BS/2,0));
304 in_water = content_liquid(map.getNode(pp).d);
307 catch(InvalidPositionException &e)
312 // The frame length is limited to the player going 0.1*BS per call
313 f32 d = (float)BS * 0.15;
315 #define PLAYER_RADIUS (BS*0.3)
316 #define PLAYER_HEIGHT (BS*1.7)
318 core::aabbox3d<f32> playerbox(
319 position.X - PLAYER_RADIUS,
321 position.Z - PLAYER_RADIUS,
322 position.X + PLAYER_RADIUS,
323 position.Y + PLAYER_HEIGHT,
324 position.Z + PLAYER_RADIUS
326 core::aabbox3d<f32> playerbox_old(
327 oldpos.X - PLAYER_RADIUS,
329 oldpos.Z - PLAYER_RADIUS,
330 oldpos.X + PLAYER_RADIUS,
331 oldpos.Y + PLAYER_HEIGHT,
332 oldpos.Z + PLAYER_RADIUS
335 //hilightboxes.push_back(playerbox);
337 touching_ground = false;
339 /*std::cout<<"Checking collisions for ("
340 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
342 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
345 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++){
346 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++){
347 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++){
349 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false){
353 catch(InvalidPositionException &e)
355 // Doing nothing here will block the player from
356 // walking over map borders
359 core::aabbox3d<f32> nodebox = Map::getNodeBox(
362 // See if the player is touching ground
364 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
365 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
366 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
367 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
368 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
370 touching_ground = true;
373 if(playerbox.intersectsWithBox(nodebox))
381 for(u16 i=0; i<3; i++)
383 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
384 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
385 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
386 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
387 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
388 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
390 bool main_edge_collides =
391 ((nodemax > playermin && nodemax <= playermin_old + d
392 && m_speed.dotProduct(dirs[i]) < 0)
394 (nodemin < playermax && nodemin >= playermax_old - d
395 && m_speed.dotProduct(dirs[i]) > 0));
397 bool other_edges_collide = true;
398 for(u16 j=0; j<3; j++)
402 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
403 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
404 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
405 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
406 if(!(nodemax - d > playermin && nodemin + d < playermax))
408 other_edges_collide = false;
413 if(main_edge_collides && other_edges_collide)
415 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
416 position -= position.dotProduct(dirs[i]) * dirs[i];
417 position += oldpos.dotProduct(dirs[i]) * dirs[i];
421 } // if(playerbox.intersectsWithBox(nodebox))
426 setPosition(position);
429 void LocalPlayer::applyControl(float dtime)
435 #define WALK_ACCELERATION (4.0 * BS)
436 #define WALKSPEED_MAX (4.0 * BS)
437 f32 walk_acceleration = WALK_ACCELERATION;
438 f32 walkspeed_max = WALKSPEED_MAX;
440 setPitch(control.pitch);
443 v3f move_direction = v3f(0,0,1);
444 move_direction.rotateXZBy(getYaw());
446 v3f speed = v3f(0,0,0);
448 bool haxmode = g_settings.getBool("haxmode");
452 v3f speed = getSpeed();
458 bool superspeed = false;
459 if(control.superspeed)
463 v3f speed = getSpeed();
470 /*speed += move_direction;
480 speed += move_direction;
484 speed -= move_direction;
488 speed += move_direction.crossProduct(v3f(0,1,0));
492 speed += move_direction.crossProduct(v3f(0,-1,0));
498 v3f speed = getSpeed();
499 /*speed.Y += 20.*BS * dtime * 2;
505 else if(touching_ground)
507 v3f speed = getSpeed();
513 v3f speed = getSpeed();
520 // The speed of the player (Y is ignored)
522 speed = speed.normalize() * walkspeed_max * 5;
524 speed = speed.normalize() * walkspeed_max;
526 f32 inc = walk_acceleration * BS * dtime;
529 inc = walk_acceleration * BS * dtime * 10;
531 // Accelerate to target speed with maximum increment
532 accelerate(speed, inc);