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.
22 #include "connection.h"
23 #include "constants.h"
26 #include <ITextSceneNode.h>
28 #include "main.h" // For g_settings
31 #include "collision.h"
32 #include "environment.h"
35 Player::Player(IGameDef *gamedef):
36 touching_ground(false),
38 in_water_stable(false),
41 inventory(gamedef->idef()),
42 inventory_backup(NULL),
44 peer_id(PEER_ID_INEXISTENT),
52 updateName("<not set>");
58 delete inventory_backup;
61 void Player::resetInventory()
64 inventory.addList("main", PLAYER_INVENTORY_SIZE);
65 inventory.addList("craft", 9);
66 inventory.addList("craftpreview", 1);
67 inventory.addList("craftresult", 1);
70 // Y direction is ignored
71 void Player::accelerate(v3f target_speed, f32 max_increase)
73 v3f d_wanted = target_speed - m_speed;
75 f32 dl_wanted = d_wanted.getLength();
80 v3f d = d_wanted.normalize() * dl;
87 if(m_speed.X < target_speed.X - max_increase)
88 m_speed.X += max_increase;
89 else if(m_speed.X > target_speed.X + max_increase)
90 m_speed.X -= max_increase;
91 else if(m_speed.X < target_speed.X)
92 m_speed.X = target_speed.X;
93 else if(m_speed.X > target_speed.X)
94 m_speed.X = target_speed.X;
96 if(m_speed.Z < target_speed.Z - max_increase)
97 m_speed.Z += max_increase;
98 else if(m_speed.Z > target_speed.Z + max_increase)
99 m_speed.Z -= max_increase;
100 else if(m_speed.Z < target_speed.Z)
101 m_speed.Z = target_speed.Z;
102 else if(m_speed.Z > target_speed.Z)
103 m_speed.Z = target_speed.Z;
107 v3s16 Player::getLightPosition() const
109 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
112 void Player::serialize(std::ostream &os)
114 // Utilize a Settings object for storing values
116 args.setS32("version", 1);
117 args.set("name", m_name);
118 //args.set("password", m_password);
119 args.setFloat("pitch", m_pitch);
120 args.setFloat("yaw", m_yaw);
121 args.setV3F("position", m_position);
122 args.setS32("hp", hp);
126 os<<"PlayerArgsEnd\n";
128 // If actual inventory is backed up due to creative mode, save it
129 // instead of the dummy creative mode inventory
131 inventory_backup->serialize(os);
133 inventory.serialize(os);
136 void Player::deSerialize(std::istream &is)
143 throw SerializationError
144 ("Player::deSerialize(): PlayerArgsEnd not found");
146 std::getline(is, line);
147 std::string trimmedline = trim(line);
148 if(trimmedline == "PlayerArgsEnd")
150 args.parseConfigLine(line);
153 //args.getS32("version"); // Version field value not used
154 std::string name = args.get("name");
155 updateName(name.c_str());
156 setPitch(args.getFloat("pitch"));
157 setYaw(args.getFloat("yaw"));
158 setPosition(args.getV3F("position"));
159 bool craftresult_is_preview = true;
161 craftresult_is_preview = args.getBool("craftresult_is_preview");
162 }catch(SettingNotFoundException &e){}
164 hp = args.getS32("hp");
165 }catch(SettingNotFoundException &e){
169 inventory.deSerialize(is);
171 if(inventory.getList("craftpreview") == NULL)
173 // Convert players without craftpreview
174 inventory.addList("craftpreview", 1);
176 if(craftresult_is_preview)
179 inventory.getList("craftresult")->changeItem(0, ItemStack());
189 LocalPlayer::LocalPlayer(IGameDef *gamedef):
191 m_sneak_node(32767,32767,32767),
192 m_sneak_node_exists(false)
194 // Initialize hp to 0, so that no hearts will be shown if server
195 // doesn't support health points
199 LocalPlayer::~LocalPlayer()
203 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
204 core::list<CollisionInfo> *collision_info)
206 INodeDefManager *nodemgr = m_gamedef->ndef();
208 v3f position = getPosition();
209 v3f oldpos = position;
210 v3s16 oldpos_i = floatToInt(oldpos, BS);
212 v3f old_speed = m_speed;
214 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
215 <<oldpos_i.Z<<")"<<std::endl;*/
218 Calculate new position
220 position += m_speed * dtime;
222 // Skip collision detection if a special movement mode is used
223 bool free_move = g_settings->getBool("free_move");
226 setPosition(position);
234 // Player position in nodes
235 v3s16 pos_i = floatToInt(position, BS);
238 Check if player is in water (the oscillating value)
241 // If in water, the threshold of coming out is at higher y
244 v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
245 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
247 // If not in water, the threshold of going in is at lower y
250 v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
251 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
254 catch(InvalidPositionException &e)
260 Check if player is in water (the stable value)
263 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
264 in_water_stable = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
266 catch(InvalidPositionException &e)
268 in_water_stable = false;
272 Check if player is climbing
276 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
277 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
278 is_climbing = ((nodemgr->get(map.getNode(pp).getContent()).climbable ||
279 nodemgr->get(map.getNode(pp2).getContent()).climbable) && !free_move);
281 catch(InvalidPositionException &e)
287 Collision uncertainty radius
288 Make it a bit larger than the maximum distance of movement
290 //f32 d = pos_max_d * 1.1;
291 // A fairly large value in here makes moving smoother
294 // This should always apply, otherwise there are glitches
295 assert(d > pos_max_d);
297 float player_radius = BS*0.35;
298 float player_height = BS*1.7;
300 // Maximum distance over border for sneaking
301 f32 sneak_max = BS*0.4;
304 If sneaking, player has larger collision radius to keep from
308 player_radius = sneak_max + d*1.1;*/
311 If sneaking, keep in range from the last walked node and don't
314 if(control.sneak && m_sneak_node_exists)
316 f32 maxd = 0.5*BS + sneak_max;
317 v3f lwn_f = intToFloat(m_sneak_node, BS);
318 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
319 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
321 f32 min_y = lwn_f.Y + 0.5*BS;
322 if(position.Y < min_y)
326 //v3f old_speed = m_speed;
333 // Report fall collision
334 if(old_speed.Y < m_speed.Y - 0.1)
337 info.t = COLLISION_FALL;
338 info.speed = m_speed.Y - old_speed.Y;
339 collision_info->push_back(info);
346 Calculate player collision box (new and old)
348 core::aabbox3d<f32> playerbox(
349 position.X - player_radius,
351 position.Z - player_radius,
352 position.X + player_radius,
353 position.Y + player_height,
354 position.Z + player_radius
356 core::aabbox3d<f32> playerbox_old(
357 oldpos.X - player_radius,
359 oldpos.Z - player_radius,
360 oldpos.X + player_radius,
361 oldpos.Y + player_height,
362 oldpos.Z + player_radius
366 If the player's feet touch the topside of any node, this is
369 Player is allowed to jump when this is true.
371 touching_ground = false;
373 /*std::cout<<"Checking collisions for ("
374 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
376 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
379 bool standing_on_unloaded = false;
382 Go through every node around the player
384 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
385 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
386 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
388 bool is_unloaded = false;
390 // Player collides into walkable nodes
391 if(nodemgr->get(map.getNode(v3s16(x,y,z))).walkable == false)
394 catch(InvalidPositionException &e)
397 // Doing nothing here will block the player from
398 // walking over map borders
401 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
404 See if the player is touching ground.
406 Player touches ground if player's minimum Y is near node's
407 maximum Y and player's X-Z-area overlaps with the node's
410 Use 0.15*BS so that it is easier to get on a node.
413 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
414 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
415 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
416 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
417 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
418 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
420 touching_ground = true;
422 standing_on_unloaded = true;
425 // If player doesn't intersect with node, ignore node.
426 if(playerbox.intersectsWithBox(nodebox) == false)
430 Go through every axis
433 v3f(0,0,1), // back-front
434 v3f(0,1,0), // top-bottom
435 v3f(1,0,0), // right-left
437 for(u16 i=0; i<3; i++)
440 Calculate values along the axis
442 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
443 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
444 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
445 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
446 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
447 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
450 Check collision for the axis.
451 Collision happens when player is going through a surface.
455 // Make it easier to get on top of a node
458 bool negative_axis_collides =
459 (nodemax > playermin && nodemax <= playermin_old + neg_d
460 && m_speed.dotProduct(dirs[i]) < 0);
461 bool positive_axis_collides =
462 (nodemin < playermax && nodemin >= playermax_old - pos_d
463 && m_speed.dotProduct(dirs[i]) > 0);*/
464 bool negative_axis_collides =
465 (nodemax > playermin && nodemax <= playermin_old + d
466 && m_speed.dotProduct(dirs[i]) < 0);
467 bool positive_axis_collides =
468 (nodemin < playermax && nodemin >= playermax_old - d
469 && m_speed.dotProduct(dirs[i]) > 0);
470 bool main_axis_collides =
471 negative_axis_collides || positive_axis_collides;
474 Check overlap of player and node in other axes
476 bool other_axes_overlap = true;
477 for(u16 j=0; j<3; j++)
481 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
482 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
483 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
484 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
485 if(!(nodemax - d > playermin && nodemin + d < playermax))
487 other_axes_overlap = false;
493 If this is a collision, revert the position in the main
496 if(other_axes_overlap && main_axis_collides)
498 //v3f old_speed = m_speed;
500 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
501 position -= position.dotProduct(dirs[i]) * dirs[i];
502 position += oldpos.dotProduct(dirs[i]) * dirs[i];
506 // Report fall collision
507 if(old_speed.Y < m_speed.Y - 0.1)
510 info.t = COLLISION_FALL;
511 info.speed = m_speed.Y - old_speed.Y;
512 collision_info->push_back(info);
521 Check the nodes under the player to see from which node the
522 player is sneaking from, if any.
525 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
526 v2f player_p2df(position.X, position.Z);
527 f32 min_distance_f = 100000.0*BS;
528 // If already seeking from some node, compare to it.
529 /*if(m_sneak_node_exists)
531 v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
532 v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
533 f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
534 f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
535 // Ignore if player is not on the same level (likely dropped)
536 if(d_vert_f < 0.15*BS)
537 min_distance_f = d_horiz_f;
539 v3s16 new_sneak_node = m_sneak_node;
540 for(s16 x=-1; x<=1; x++)
541 for(s16 z=-1; z<=1; z++)
543 v3s16 p = pos_i_bottom + v3s16(x,0,z);
544 v3f pf = intToFloat(p, BS);
545 v2f node_p2df(pf.X, pf.Z);
546 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
547 f32 max_axis_distance_f = MYMAX(
548 fabs(player_p2df.X-node_p2df.X),
549 fabs(player_p2df.Y-node_p2df.Y));
551 if(distance_f > min_distance_f ||
552 max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
556 // The node to be sneaked on has to be walkable
557 if(nodemgr->get(map.getNode(p)).walkable == false)
559 // And the node above it has to be nonwalkable
560 if(nodemgr->get(map.getNode(p+v3s16(0,1,0))).walkable == true)
563 catch(InvalidPositionException &e)
568 min_distance_f = distance_f;
572 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
574 if(control.sneak && m_sneak_node_exists)
577 m_sneak_node = new_sneak_node;
581 m_sneak_node = new_sneak_node;
582 m_sneak_node_exists = sneak_node_found;
586 If sneaking, the player's collision box can be in air, so
587 this has to be set explicitly
589 if(sneak_node_found && control.sneak)
590 touching_ground = true;
596 setPosition(position);
603 // Report fall collision
604 if(old_speed.Y < m_speed.Y - 0.1 && !standing_on_unloaded)
607 info.t = COLLISION_FALL;
608 info.speed = m_speed.Y - old_speed.Y;
609 collision_info->push_back(info);
614 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
616 move(dtime, map, pos_max_d, NULL);
619 void LocalPlayer::applyControl(float dtime)
625 f32 walk_acceleration = 4.0 * BS;
626 f32 walkspeed_max = 4.0 * BS;
628 setPitch(control.pitch);
631 v3f move_direction = v3f(0,0,1);
632 move_direction.rotateXZBy(getYaw());
634 v3f speed = v3f(0,0,0);
636 bool free_move = g_settings->getBool("free_move");
637 bool fast_move = g_settings->getBool("fast_move");
638 bool continuous_forward = g_settings->getBool("continuous_forward");
640 if(free_move || is_climbing)
642 v3f speed = getSpeed();
647 // Whether superspeed mode is used or not
648 bool superspeed = false;
650 // If free movement and fast movement, always move fast
651 if(free_move && fast_move)
654 // Auxiliary button 1 (E)
659 // In free movement mode, aux1 descends
660 v3f speed = getSpeed();
664 speed.Y = -walkspeed_max;
669 v3f speed = getSpeed();
675 // If not free movement but fast is allowed, aux1 is
682 if(continuous_forward)
683 speed += move_direction;
687 if(continuous_forward)
690 speed += move_direction;
694 speed -= move_direction;
698 speed += move_direction.crossProduct(v3f(0,1,0));
702 speed += move_direction.crossProduct(v3f(0,-1,0));
708 v3f speed = getSpeed();
712 speed.Y = walkspeed_max;
715 else if(touching_ground)
717 v3f speed = getSpeed();
719 NOTE: The d value in move() affects jump height by
720 raising the height at which the jump speed is kept
721 at its starting value
726 // Use the oscillating value for getting out of water
727 // (so that the player doesn't fly on the surface)
730 v3f speed = getSpeed();
737 v3f speed = getSpeed();
743 // The speed of the player (Y is ignored)
745 speed = speed.normalize() * walkspeed_max * 5.0;
746 else if(control.sneak)
747 speed = speed.normalize() * walkspeed_max / 3.0;
749 speed = speed.normalize() * walkspeed_max;
751 f32 inc = walk_acceleration * BS * dtime;
753 // Faster acceleration if fast and free movement
754 if(free_move && fast_move)
755 inc = walk_acceleration * BS * dtime * 10;
757 // Accelerate to target speed with maximum increment
758 accelerate(speed, inc);