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 in_water_stable(false),
35 craftresult_is_preview(true),
36 peer_id(PEER_ID_INEXISTENT),
42 updateName("<not set>");
50 void Player::resetInventory()
53 inventory.addList("main", PLAYER_INVENTORY_SIZE);
54 inventory.addList("craft", 9);
55 inventory.addList("craftresult", 1);
58 // Y direction is ignored
59 void Player::accelerate(v3f target_speed, f32 max_increase)
61 v3f d_wanted = target_speed - m_speed;
63 f32 dl_wanted = d_wanted.getLength();
68 v3f d = d_wanted.normalize() * dl;
75 if(m_speed.X < target_speed.X - max_increase)
76 m_speed.X += max_increase;
77 else if(m_speed.X > target_speed.X + max_increase)
78 m_speed.X -= max_increase;
79 else if(m_speed.X < target_speed.X)
80 m_speed.X = target_speed.X;
81 else if(m_speed.X > target_speed.X)
82 m_speed.X = target_speed.X;
84 if(m_speed.Z < target_speed.Z - max_increase)
85 m_speed.Z += max_increase;
86 else if(m_speed.Z > target_speed.Z + max_increase)
87 m_speed.Z -= max_increase;
88 else if(m_speed.Z < target_speed.Z)
89 m_speed.Z = target_speed.Z;
90 else if(m_speed.Z > target_speed.Z)
91 m_speed.Z = target_speed.Z;
95 void Player::serialize(std::ostream &os)
97 // Utilize a Settings object for storing values
99 args.setS32("version", 1);
100 args.set("name", m_name);
101 args.setFloat("pitch", m_pitch);
102 args.setFloat("yaw", m_yaw);
103 args.setV3F("position", m_position);
104 args.setBool("craftresult_is_preview", craftresult_is_preview);
108 os<<"PlayerArgsEnd\n";
110 inventory.serialize(os);
113 void Player::deSerialize(std::istream &is)
120 throw SerializationError
121 ("Player::deSerialize(): PlayerArgsEnd not found");
123 std::getline(is, line);
124 std::string trimmedline = trim(line);
125 if(trimmedline == "PlayerArgsEnd")
127 args.parseConfigLine(line);
130 //args.getS32("version");
131 std::string name = args.get("name");
132 updateName(name.c_str());
133 m_pitch = args.getFloat("pitch");
134 m_yaw = args.getFloat("yaw");
135 m_position = args.getV3F("position");
137 craftresult_is_preview = args.getBool("craftresult_is_preview");
138 }catch(SettingNotFoundException &e){
139 craftresult_is_preview = true;
142 inventory.deSerialize(is);
151 RemotePlayer::RemotePlayer(
152 scene::ISceneNode* parent,
153 IrrlichtDevice *device,
155 scene::ISceneNode(parent, (device==NULL)?NULL:device->getSceneManager(), id),
158 m_box = core::aabbox3d<f32>(-BS/2,0,-BS/2,BS/2,BS*2,BS/2);
160 if(parent != NULL && device != NULL)
162 // ISceneNode stores a member called SceneManager
163 scene::ISceneManager* mgr = SceneManager;
164 video::IVideoDriver* driver = mgr->getVideoDriver();
165 gui::IGUIEnvironment* gui = device->getGUIEnvironment();
167 // Add a text node for showing the name
168 wchar_t wname[1] = {0};
169 m_text = mgr->addTextSceneNode(gui->getBuiltInFont(),
170 wname, video::SColor(255,255,255,255), this);
171 m_text->setPosition(v3f(0, (f32)BS*2.1, 0));
173 // Attach a simple mesh to the player for showing an image
174 scene::SMesh *mesh = new scene::SMesh();
176 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
177 video::SColor c(255,255,255,255);
178 video::S3DVertex vertices[4] =
180 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
181 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
182 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
183 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
185 u16 indices[] = {0,1,2,2,3,0};
186 buf->append(vertices, 4, indices, 6);
188 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
189 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
190 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player.png").c_str()));
191 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
192 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
193 //buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
194 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
196 mesh->addMeshBuffer(buf);
200 scene::IMeshBuffer *buf = new scene::SMeshBuffer();
201 video::SColor c(255,255,255,255);
202 video::S3DVertex vertices[4] =
204 video::S3DVertex(BS/2,0,0, 0,0,0, c, 1,1),
205 video::S3DVertex(-BS/2,0,0, 0,0,0, c, 0,1),
206 video::S3DVertex(-BS/2,BS*2,0, 0,0,0, c, 0,0),
207 video::S3DVertex(BS/2,BS*2,0, 0,0,0, c, 1,0),
209 u16 indices[] = {0,1,2,2,3,0};
210 buf->append(vertices, 4, indices, 6);
212 buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
213 //buf->getMaterial().setFlag(video::EMF_BACK_FACE_CULLING, false);
214 buf->getMaterial().setTexture(0, driver->getTexture(porting::getDataPath("player_back.png").c_str()));
215 buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
216 buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
217 buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
219 mesh->addMeshBuffer(buf);
222 m_node = mgr->addMeshSceneNode(mesh, this);
224 m_node->setPosition(v3f(0,0,0));
228 RemotePlayer::~RemotePlayer()
230 if(SceneManager != NULL)
231 ISceneNode::remove();
234 void RemotePlayer::updateName(const char *name)
236 Player::updateName(name);
239 wchar_t wname[PLAYERNAME_SIZE];
240 mbstowcs(wname, m_name, strlen(m_name)+1);
241 m_text->setText(wname);
245 void RemotePlayer::move(f32 dtime, Map &map, f32 pos_max_d)
247 m_pos_animation_time_counter += dtime;
248 m_pos_animation_counter += dtime;
249 v3f movevector = m_position - m_oldpos;
251 if(m_pos_animation_time < 0.001)
254 moveratio = m_pos_animation_counter / m_pos_animation_time;
257 m_showpos = m_oldpos + movevector * moveratio;
259 ISceneNode::setPosition(m_showpos);
269 LocalPlayer::LocalPlayer():
270 m_sneak_node(32767,32767,32767),
271 m_sneak_node_exists(false)
275 LocalPlayer::~LocalPlayer()
279 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
281 v3f position = getPosition();
282 v3f oldpos = position;
283 v3s16 oldpos_i = floatToInt(oldpos, BS);
285 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
286 <<oldpos_i.Z<<")"<<std::endl;*/
289 Calculate new position
291 position += m_speed * dtime;
293 // Skip collision detection if a special movement mode is used
294 bool free_move = g_settings.getBool("free_move");
297 setPosition(position);
305 // Player position in nodes
306 v3s16 pos_i = floatToInt(position, BS);
309 Check if player is in water (the oscillating value)
312 // If in water, the threshold of coming out is at higher y
315 v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
316 in_water = content_liquid(map.getNode(pp).d);
318 // If not in water, the threshold of going in is at lower y
321 v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
322 in_water = content_liquid(map.getNode(pp).d);
325 catch(InvalidPositionException &e)
331 Check if player is in water (the stable value)
334 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
335 in_water_stable = content_liquid(map.getNode(pp).d);
337 catch(InvalidPositionException &e)
339 in_water_stable = false;
343 Collision uncertainty radius
344 Make it a bit larger than the maximum distance of movement
346 //f32 d = pos_max_d * 1.1;
347 // A fairly large value in here makes moving smoother
350 // This should always apply, otherwise there are glitches
351 assert(d > pos_max_d);
353 float player_radius = BS*0.35;
354 float player_height = BS*1.7;
356 // Maximum distance over border for sneaking
357 f32 sneak_max = BS*0.4;
360 If sneaking, player has larger collision radius to keep from
364 player_radius = sneak_max + d*1.1;*/
367 If sneaking, keep in range from the last walked node and don't
370 if(control.sneak && m_sneak_node_exists)
372 f32 maxd = 0.5*BS + sneak_max;
373 v3f lwn_f = intToFloat(m_sneak_node, BS);
374 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
375 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
377 f32 min_y = lwn_f.Y + 0.5*BS;
378 if(position.Y < min_y)
387 Calculate player collision box (new and old)
389 core::aabbox3d<f32> playerbox(
390 position.X - player_radius,
392 position.Z - player_radius,
393 position.X + player_radius,
394 position.Y + player_height,
395 position.Z + player_radius
397 core::aabbox3d<f32> playerbox_old(
398 oldpos.X - player_radius,
400 oldpos.Z - player_radius,
401 oldpos.X + player_radius,
402 oldpos.Y + player_height,
403 oldpos.Z + player_radius
407 If the player's feet touch the topside of any node, this is
410 Player is allowed to jump when this is true.
412 touching_ground = false;
414 /*std::cout<<"Checking collisions for ("
415 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
417 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
421 Go through every node around the player
423 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
424 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
425 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
428 // Player collides into walkable nodes
429 if(content_walkable(map.getNode(v3s16(x,y,z)).d) == false)
432 catch(InvalidPositionException &e)
434 // Doing nothing here will block the player from
435 // walking over map borders
438 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
441 See if the player is touching ground.
443 Player touches ground if player's minimum Y is near node's
444 maximum Y and player's X-Z-area overlaps with the node's
447 Use 0.15*BS so that it is easier to get on a node.
450 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
451 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
452 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
453 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
454 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
455 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
457 touching_ground = true;
460 // If player doesn't intersect with node, ignore node.
461 if(playerbox.intersectsWithBox(nodebox) == false)
465 Go through every axis
468 v3f(0,0,1), // back-front
469 v3f(0,1,0), // top-bottom
470 v3f(1,0,0), // right-left
472 for(u16 i=0; i<3; i++)
475 Calculate values along the axis
477 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
478 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
479 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
480 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
481 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
482 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
485 Check collision for the axis.
486 Collision happens when player is going through a surface.
490 // Make it easier to get on top of a node
493 bool negative_axis_collides =
494 (nodemax > playermin && nodemax <= playermin_old + neg_d
495 && m_speed.dotProduct(dirs[i]) < 0);
496 bool positive_axis_collides =
497 (nodemin < playermax && nodemin >= playermax_old - pos_d
498 && m_speed.dotProduct(dirs[i]) > 0);*/
499 bool negative_axis_collides =
500 (nodemax > playermin && nodemax <= playermin_old + d
501 && m_speed.dotProduct(dirs[i]) < 0);
502 bool positive_axis_collides =
503 (nodemin < playermax && nodemin >= playermax_old - d
504 && m_speed.dotProduct(dirs[i]) > 0);
505 bool main_axis_collides =
506 negative_axis_collides || positive_axis_collides;
509 Check overlap of player and node in other axes
511 bool other_axes_overlap = true;
512 for(u16 j=0; j<3; j++)
516 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
517 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
518 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
519 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
520 if(!(nodemax - d > playermin && nodemin + d < playermax))
522 other_axes_overlap = false;
528 If this is a collision, revert the position in the main
531 if(other_axes_overlap && main_axis_collides)
533 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
534 position -= position.dotProduct(dirs[i]) * dirs[i];
535 position += oldpos.dotProduct(dirs[i]) * dirs[i];
542 Check the nodes under the player to see from which node the
543 player is sneaking from, if any.
546 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
547 v2f player_p2df(position.X, position.Z);
548 f32 min_distance_f = 100000.0*BS;
549 // If already seeking from some node, compare to it.
550 /*if(m_sneak_node_exists)
552 v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
553 v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
554 f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
555 f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
556 // Ignore if player is not on the same level (likely dropped)
557 if(d_vert_f < 0.15*BS)
558 min_distance_f = d_horiz_f;
560 v3s16 new_sneak_node = m_sneak_node;
561 for(s16 x=-1; x<=1; x++)
562 for(s16 z=-1; z<=1; z++)
564 v3s16 p = pos_i_bottom + v3s16(x,0,z);
565 v3f pf = intToFloat(p, BS);
566 v2f node_p2df(pf.X, pf.Z);
567 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
568 f32 max_axis_distance_f = MYMAX(
569 fabs(player_p2df.X-node_p2df.X),
570 fabs(player_p2df.Y-node_p2df.Y));
572 if(distance_f > min_distance_f ||
573 max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
577 // The node to be sneaked on has to be walkable
578 if(content_walkable(map.getNode(p).d) == false)
580 // And the node above it has to be nonwalkable
581 if(content_walkable(map.getNode(p+v3s16(0,1,0)).d) == true)
584 catch(InvalidPositionException &e)
589 min_distance_f = distance_f;
593 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
595 if(control.sneak && m_sneak_node_exists)
598 m_sneak_node = new_sneak_node;
602 m_sneak_node = new_sneak_node;
603 m_sneak_node_exists = sneak_node_found;
607 If sneaking, the player's collision box can be in air, so
608 this has to be set explicitly
610 if(sneak_node_found && control.sneak)
611 touching_ground = true;
617 setPosition(position);
620 void LocalPlayer::applyControl(float dtime)
626 f32 walk_acceleration = 4.0 * BS;
627 f32 walkspeed_max = 4.0 * BS;
629 setPitch(control.pitch);
632 v3f move_direction = v3f(0,0,1);
633 move_direction.rotateXZBy(getYaw());
635 v3f speed = v3f(0,0,0);
637 bool free_move = g_settings.getBool("free_move");
638 bool fast_move = g_settings.getBool("fast_move");
639 bool continuous_forward = g_settings.getBool("continuous_forward");
643 v3f speed = getSpeed();
648 // Whether superspeed mode is used or not
649 bool superspeed = false;
651 // If free movement and fast movement, always move fast
652 if(free_move && fast_move)
655 // Auxiliary button 1 (E)
660 // In free movement mode, aux1 descends
661 v3f speed = getSpeed();
665 speed.Y = -walkspeed_max;
670 // If not free movement but fast is allowed, aux1 is
677 if(continuous_forward)
678 speed += move_direction;
682 if(continuous_forward)
685 speed += move_direction;
689 speed -= move_direction;
693 speed += move_direction.crossProduct(v3f(0,1,0));
697 speed += move_direction.crossProduct(v3f(0,-1,0));
703 v3f speed = getSpeed();
707 speed.Y = walkspeed_max;
710 else if(touching_ground)
712 v3f speed = getSpeed();
714 NOTE: The d value in move() affects jump height by
715 raising the height at which the jump speed is kept
716 at its starting value
721 // Use the oscillating value for getting out of water
722 // (so that the player doesn't fly on the surface)
725 v3f speed = getSpeed();
732 // The speed of the player (Y is ignored)
734 speed = speed.normalize() * walkspeed_max * 5.0;
735 else if(control.sneak)
736 speed = speed.normalize() * walkspeed_max / 3.0;
738 speed = speed.normalize() * walkspeed_max;
740 f32 inc = walk_acceleration * BS * dtime;
742 // Faster acceleration if fast and free movement
743 if(free_move && fast_move)
744 inc = walk_acceleration * BS * dtime * 10;
746 // Accelerate to target speed with maximum increment
747 accelerate(speed, inc);