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 #include "content_sao.h"
37 Player::Player(IGameDef *gamedef):
38 touching_ground(false),
40 in_water_stable(false),
43 inventory(gamedef->idef()),
45 peer_id(PEER_ID_INEXISTENT),
53 updateName("<not set>");
55 inventory.addList("main", PLAYER_INVENTORY_SIZE);
56 inventory.addList("craft", 9);
57 inventory.addList("craftpreview", 1);
58 inventory.addList("craftresult", 1);
65 // Y direction is ignored
66 void Player::accelerate(v3f target_speed, f32 max_increase)
68 v3f d_wanted = target_speed - m_speed;
70 f32 dl_wanted = d_wanted.getLength();
75 v3f d = d_wanted.normalize() * dl;
82 if(m_speed.X < target_speed.X - max_increase)
83 m_speed.X += max_increase;
84 else if(m_speed.X > target_speed.X + max_increase)
85 m_speed.X -= max_increase;
86 else if(m_speed.X < target_speed.X)
87 m_speed.X = target_speed.X;
88 else if(m_speed.X > target_speed.X)
89 m_speed.X = target_speed.X;
91 if(m_speed.Z < target_speed.Z - max_increase)
92 m_speed.Z += max_increase;
93 else if(m_speed.Z > target_speed.Z + max_increase)
94 m_speed.Z -= max_increase;
95 else if(m_speed.Z < target_speed.Z)
96 m_speed.Z = target_speed.Z;
97 else if(m_speed.Z > target_speed.Z)
98 m_speed.Z = target_speed.Z;
102 v3s16 Player::getLightPosition() const
104 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
107 void Player::serialize(std::ostream &os)
109 // Utilize a Settings object for storing values
111 args.setS32("version", 1);
112 args.set("name", m_name);
113 //args.set("password", m_password);
114 args.setFloat("pitch", m_pitch);
115 args.setFloat("yaw", m_yaw);
116 args.setV3F("position", m_position);
117 args.setS32("hp", hp);
121 os<<"PlayerArgsEnd\n";
123 inventory.serialize(os);
126 void Player::deSerialize(std::istream &is)
133 throw SerializationError
134 ("Player::deSerialize(): PlayerArgsEnd not found");
136 std::getline(is, line);
137 std::string trimmedline = trim(line);
138 if(trimmedline == "PlayerArgsEnd")
140 args.parseConfigLine(line);
143 //args.getS32("version"); // Version field value not used
144 std::string name = args.get("name");
145 updateName(name.c_str());
146 setPitch(args.getFloat("pitch"));
147 setYaw(args.getFloat("yaw"));
148 setPosition(args.getV3F("position"));
150 hp = args.getS32("hp");
151 }catch(SettingNotFoundException &e){
155 inventory.deSerialize(is);
157 if(inventory.getList("craftpreview") == NULL)
159 // Convert players without craftpreview
160 inventory.addList("craftpreview", 1);
162 bool craftresult_is_preview = true;
163 if(args.exists("craftresult_is_preview"))
164 craftresult_is_preview = args.getBool("craftresult_is_preview");
165 if(craftresult_is_preview)
168 inventory.getList("craftresult")->changeItem(0, ItemStack());
178 LocalPlayer::LocalPlayer(IGameDef *gamedef):
180 m_sneak_node(32767,32767,32767),
181 m_sneak_node_exists(false)
183 // Initialize hp to 0, so that no hearts will be shown if server
184 // doesn't support health points
188 LocalPlayer::~LocalPlayer()
192 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d,
193 core::list<CollisionInfo> *collision_info)
195 INodeDefManager *nodemgr = m_gamedef->ndef();
197 v3f position = getPosition();
198 v3f oldpos = position;
199 v3s16 oldpos_i = floatToInt(oldpos, BS);
201 v3f old_speed = m_speed;
203 /*std::cout<<"oldpos_i=("<<oldpos_i.X<<","<<oldpos_i.Y<<","
204 <<oldpos_i.Z<<")"<<std::endl;*/
207 Calculate new position
209 position += m_speed * dtime;
211 // Skip collision detection if a special movement mode is used
212 bool free_move = g_settings->getBool("free_move");
215 setPosition(position);
223 // Player position in nodes
224 v3s16 pos_i = floatToInt(position, BS);
227 Check if player is in water (the oscillating value)
230 // If in water, the threshold of coming out is at higher y
233 v3s16 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
234 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
236 // If not in water, the threshold of going in is at lower y
239 v3s16 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
240 in_water = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
243 catch(InvalidPositionException &e)
249 Check if player is in water (the stable value)
252 v3s16 pp = floatToInt(position + v3f(0,0,0), BS);
253 in_water_stable = nodemgr->get(map.getNode(pp).getContent()).isLiquid();
255 catch(InvalidPositionException &e)
257 in_water_stable = false;
261 Check if player is climbing
265 v3s16 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
266 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
267 is_climbing = ((nodemgr->get(map.getNode(pp).getContent()).climbable ||
268 nodemgr->get(map.getNode(pp2).getContent()).climbable) && !free_move);
270 catch(InvalidPositionException &e)
276 Collision uncertainty radius
277 Make it a bit larger than the maximum distance of movement
279 //f32 d = pos_max_d * 1.1;
280 // A fairly large value in here makes moving smoother
283 // This should always apply, otherwise there are glitches
284 assert(d > pos_max_d);
286 float player_radius = BS*0.30;
287 float player_height = BS*1.55;
289 // Maximum distance over border for sneaking
290 f32 sneak_max = BS*0.4;
293 If sneaking, player has larger collision radius to keep from
297 player_radius = sneak_max + d*1.1;*/
300 If sneaking, keep in range from the last walked node and don't
303 if(control.sneak && m_sneak_node_exists)
305 f32 maxd = 0.5*BS + sneak_max;
306 v3f lwn_f = intToFloat(m_sneak_node, BS);
307 position.X = rangelim(position.X, lwn_f.X-maxd, lwn_f.X+maxd);
308 position.Z = rangelim(position.Z, lwn_f.Z-maxd, lwn_f.Z+maxd);
310 f32 min_y = lwn_f.Y + 0.5*BS;
311 if(position.Y < min_y)
315 //v3f old_speed = m_speed;
322 // Report fall collision
323 if(old_speed.Y < m_speed.Y - 0.1)
326 info.t = COLLISION_FALL;
327 info.speed = m_speed.Y - old_speed.Y;
328 collision_info->push_back(info);
335 Calculate player collision box (new and old)
337 core::aabbox3d<f32> playerbox(
338 position.X - player_radius,
340 position.Z - player_radius,
341 position.X + player_radius,
342 position.Y + player_height,
343 position.Z + player_radius
345 core::aabbox3d<f32> playerbox_old(
346 oldpos.X - player_radius,
348 oldpos.Z - player_radius,
349 oldpos.X + player_radius,
350 oldpos.Y + player_height,
351 oldpos.Z + player_radius
355 If the player's feet touch the topside of any node, this is
358 Player is allowed to jump when this is true.
360 bool touching_ground_was = touching_ground;
361 touching_ground = false;
363 /*std::cout<<"Checking collisions for ("
364 <<oldpos_i.X<<","<<oldpos_i.Y<<","<<oldpos_i.Z
366 <<pos_i.X<<","<<pos_i.Y<<","<<pos_i.Z
369 bool standing_on_unloaded = false;
372 Go through every node around the player
374 for(s16 y = oldpos_i.Y - 1; y <= oldpos_i.Y + 2; y++)
375 for(s16 z = oldpos_i.Z - 1; z <= oldpos_i.Z + 1; z++)
376 for(s16 x = oldpos_i.X - 1; x <= oldpos_i.X + 1; x++)
378 bool is_unloaded = false;
380 // Player collides into walkable nodes
381 if(nodemgr->get(map.getNode(v3s16(x,y,z))).walkable == false)
384 catch(InvalidPositionException &e)
387 // Doing nothing here will block the player from
388 // walking over map borders
391 core::aabbox3d<f32> nodebox = getNodeBox(v3s16(x,y,z), BS);
394 See if the player is touching ground.
396 Player touches ground if player's minimum Y is near node's
397 maximum Y and player's X-Z-area overlaps with the node's
400 Use 0.15*BS so that it is easier to get on a node.
403 //fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < d
404 fabs(nodebox.MaxEdge.Y-playerbox.MinEdge.Y) < 0.15*BS
405 && nodebox.MaxEdge.X-d > playerbox.MinEdge.X
406 && nodebox.MinEdge.X+d < playerbox.MaxEdge.X
407 && nodebox.MaxEdge.Z-d > playerbox.MinEdge.Z
408 && nodebox.MinEdge.Z+d < playerbox.MaxEdge.Z
410 touching_ground = true;
412 standing_on_unloaded = true;
415 // If player doesn't intersect with node, ignore node.
416 if(playerbox.intersectsWithBox(nodebox) == false)
420 Go through every axis
423 v3f(0,0,1), // back-front
424 v3f(0,1,0), // top-bottom
425 v3f(1,0,0), // right-left
427 for(u16 i=0; i<3; i++)
430 Calculate values along the axis
432 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[i]);
433 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[i]);
434 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[i]);
435 f32 playermin = playerbox.MinEdge.dotProduct(dirs[i]);
436 f32 playermax_old = playerbox_old.MaxEdge.dotProduct(dirs[i]);
437 f32 playermin_old = playerbox_old.MinEdge.dotProduct(dirs[i]);
440 Check collision for the axis.
441 Collision happens when player is going through a surface.
445 // Make it easier to get on top of a node
448 bool negative_axis_collides =
449 (nodemax > playermin && nodemax <= playermin_old + neg_d
450 && m_speed.dotProduct(dirs[i]) < 0);
451 bool positive_axis_collides =
452 (nodemin < playermax && nodemin >= playermax_old - pos_d
453 && m_speed.dotProduct(dirs[i]) > 0);*/
454 bool negative_axis_collides =
455 (nodemax > playermin && nodemax <= playermin_old + d
456 && m_speed.dotProduct(dirs[i]) < 0);
457 bool positive_axis_collides =
458 (nodemin < playermax && nodemin >= playermax_old - d
459 && m_speed.dotProduct(dirs[i]) > 0);
460 bool main_axis_collides =
461 negative_axis_collides || positive_axis_collides;
464 Check overlap of player and node in other axes
466 bool other_axes_overlap = true;
467 for(u16 j=0; j<3; j++)
471 f32 nodemax = nodebox.MaxEdge.dotProduct(dirs[j]);
472 f32 nodemin = nodebox.MinEdge.dotProduct(dirs[j]);
473 f32 playermax = playerbox.MaxEdge.dotProduct(dirs[j]);
474 f32 playermin = playerbox.MinEdge.dotProduct(dirs[j]);
475 if(!(nodemax - d > playermin && nodemin + d < playermax))
477 other_axes_overlap = false;
483 If this is a collision, revert the position in the main
486 if(other_axes_overlap && main_axis_collides)
488 //v3f old_speed = m_speed;
490 m_speed -= m_speed.dotProduct(dirs[i]) * dirs[i];
491 position -= position.dotProduct(dirs[i]) * dirs[i];
492 position += oldpos.dotProduct(dirs[i]) * dirs[i];
496 // Report fall collision
497 if(old_speed.Y < m_speed.Y - 0.1)
500 info.t = COLLISION_FALL;
501 info.speed = m_speed.Y - old_speed.Y;
502 collision_info->push_back(info);
511 Check the nodes under the player to see from which node the
512 player is sneaking from, if any.
515 v3s16 pos_i_bottom = floatToInt(position - v3f(0,BS/2,0), BS);
516 v2f player_p2df(position.X, position.Z);
517 f32 min_distance_f = 100000.0*BS;
518 // If already seeking from some node, compare to it.
519 /*if(m_sneak_node_exists)
521 v3f sneaknode_pf = intToFloat(m_sneak_node, BS);
522 v2f sneaknode_p2df(sneaknode_pf.X, sneaknode_pf.Z);
523 f32 d_horiz_f = player_p2df.getDistanceFrom(sneaknode_p2df);
524 f32 d_vert_f = fabs(sneaknode_pf.Y + BS*0.5 - position.Y);
525 // Ignore if player is not on the same level (likely dropped)
526 if(d_vert_f < 0.15*BS)
527 min_distance_f = d_horiz_f;
529 v3s16 new_sneak_node = m_sneak_node;
530 for(s16 x=-1; x<=1; x++)
531 for(s16 z=-1; z<=1; z++)
533 v3s16 p = pos_i_bottom + v3s16(x,0,z);
534 v3f pf = intToFloat(p, BS);
535 v2f node_p2df(pf.X, pf.Z);
536 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
537 f32 max_axis_distance_f = MYMAX(
538 fabs(player_p2df.X-node_p2df.X),
539 fabs(player_p2df.Y-node_p2df.Y));
541 if(distance_f > min_distance_f ||
542 max_axis_distance_f > 0.5*BS + sneak_max + 0.1*BS)
546 // The node to be sneaked on has to be walkable
547 if(nodemgr->get(map.getNode(p)).walkable == false)
549 // And the node above it has to be nonwalkable
550 if(nodemgr->get(map.getNode(p+v3s16(0,1,0))).walkable == true)
553 catch(InvalidPositionException &e)
558 min_distance_f = distance_f;
562 bool sneak_node_found = (min_distance_f < 100000.0*BS*0.9);
564 if(control.sneak && m_sneak_node_exists)
567 m_sneak_node = new_sneak_node;
571 m_sneak_node = new_sneak_node;
572 m_sneak_node_exists = sneak_node_found;
576 If sneaking, the player's collision box can be in air, so
577 this has to be set explicitly
579 if(sneak_node_found && control.sneak)
580 touching_ground = true;
586 setPosition(position);
593 // Report fall collision
594 if(old_speed.Y < m_speed.Y - 0.1 && !standing_on_unloaded)
597 info.t = COLLISION_FALL;
598 info.speed = m_speed.Y - old_speed.Y;
599 collision_info->push_back(info);
603 if(!touching_ground_was && touching_ground){
604 MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
605 m_gamedef->event()->put(e);
609 void LocalPlayer::move(f32 dtime, Map &map, f32 pos_max_d)
611 move(dtime, map, pos_max_d, NULL);
614 void LocalPlayer::applyControl(float dtime)
620 f32 walk_acceleration = 4.0 * BS;
621 f32 walkspeed_max = 4.0 * BS;
623 setPitch(control.pitch);
626 v3f move_direction = v3f(0,0,1);
627 move_direction.rotateXZBy(getYaw());
629 v3f speed = v3f(0,0,0);
631 bool free_move = g_settings->getBool("free_move");
632 bool fast_move = g_settings->getBool("fast_move");
633 bool continuous_forward = g_settings->getBool("continuous_forward");
635 if(free_move || is_climbing)
637 v3f speed = getSpeed();
642 // Whether superspeed mode is used or not
643 bool superspeed = false;
645 // If free movement and fast movement, always move fast
646 if(free_move && fast_move)
649 // Auxiliary button 1 (E)
654 // In free movement mode, aux1 descends
655 v3f speed = getSpeed();
659 speed.Y = -walkspeed_max;
664 v3f speed = getSpeed();
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)
713 NOTE: The d value in move() affects jump height by
714 raising the height at which the jump speed is kept
715 at its starting value
717 v3f speed = getSpeed();
718 if(speed.Y >= -0.5*BS)
723 MtEvent *e = new SimpleTriggerEvent("PlayerJump");
724 m_gamedef->event()->put(e);
727 // Use the oscillating value for getting out of water
728 // (so that the player doesn't fly on the surface)
731 v3f speed = getSpeed();
738 v3f speed = getSpeed();
744 // The speed of the player (Y is ignored)
746 speed = speed.normalize() * walkspeed_max * 5.0;
747 else if(control.sneak)
748 speed = speed.normalize() * walkspeed_max / 3.0;
750 speed = speed.normalize() * walkspeed_max;
752 f32 inc = walk_acceleration * BS * dtime;
754 // Faster acceleration if fast and free movement
755 if(free_move && fast_move)
756 inc = walk_acceleration * BS * dtime * 10;
758 // Accelerate to target speed with maximum increment
759 accelerate(speed, inc);
762 v3s16 LocalPlayer::getStandingNodePos()
764 if(m_sneak_node_exists)
766 return floatToInt(getPosition(), BS);
775 void RemotePlayer::setPosition(const v3f &position)
777 Player::setPosition(position);
779 m_sao->setBasePosition(position);