3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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.
20 #include "localplayer.h"
23 #include "collision.h"
26 #include "environment.h"
29 #include "content_cao.h"
35 LocalPlayer::LocalPlayer(Client *client, const char *name):
36 Player(name, client->idef()),
41 LocalPlayer::~LocalPlayer()
45 static aabb3f getTopBoundingBox(const std::vector<aabb3f> &nodeboxes)
48 b_max.reset(-BS, -BS, -BS);
49 for (std::vector<aabb3f>::const_iterator it = nodeboxes.begin();
50 it != nodeboxes.end(); ++it) {
52 if (box.MaxEdge.Y > b_max.MaxEdge.Y)
54 else if (box.MaxEdge.Y == b_max.MaxEdge.Y)
55 b_max.addInternalBox(box);
57 return aabb3f(v3f(b_max.MinEdge.X, b_max.MaxEdge.Y, b_max.MinEdge.Z), b_max.MaxEdge);
60 #define GETNODE(map, p3, v2, y, valid) \
61 (map)->getNodeNoEx((p3) + v3s16((v2).X, y, (v2).Y), valid)
63 // pos is the node the player is standing inside(!)
64 static bool detectSneakLadder(Map *map, INodeDefManager *nodemgr, v3s16 pos)
66 // Detects a structure known as "sneak ladder" or "sneak elevator"
67 // that relies on bugs to provide a fast means of vertical transportation,
68 // the bugs have since been fixed but this function remains to keep it working.
69 // NOTE: This is just entirely a huge hack and causes way too many problems.
70 bool is_valid_position;
72 // X/Z vectors for 4 neighboring nodes
73 static const v2s16 vecs[] = { v2s16(-1, 0), v2s16(1, 0), v2s16(0, -1), v2s16(0, 1) };
75 for (u16 i = 0; i < ARRLEN(vecs); i++) {
76 const v2s16 vec = vecs[i];
78 // walkability of bottom & top node should differ
79 node = GETNODE(map, pos, vec, 0, &is_valid_position);
80 if (!is_valid_position)
82 bool w = nodemgr->get(node).walkable;
83 node = GETNODE(map, pos, vec, 1, &is_valid_position);
84 if (!is_valid_position || w == nodemgr->get(node).walkable)
87 // check one more node above OR below with corresponding walkability
88 node = GETNODE(map, pos, vec, -1, &is_valid_position);
89 bool ok = is_valid_position && w != nodemgr->get(node).walkable;
91 node = GETNODE(map, pos, vec, 2, &is_valid_position);
92 ok = is_valid_position && w == nodemgr->get(node).walkable;
102 static bool detectLedge(Map *map, INodeDefManager *nodemgr, v3s16 pos)
104 bool is_valid_position;
106 // X/Z vectors for 4 neighboring nodes
107 static const v2s16 vecs[] = {v2s16(-1, 0), v2s16(1, 0), v2s16(0, -1), v2s16(0, 1)};
109 for (u16 i = 0; i < ARRLEN(vecs); i++) {
110 const v2s16 vec = vecs[i];
112 node = GETNODE(map, pos, vec, 1, &is_valid_position);
113 if (is_valid_position && nodemgr->get(node).walkable) {
115 node = GETNODE(map, pos, vec, 2, &is_valid_position);
116 if (is_valid_position && !nodemgr->get(node).walkable)
117 // Space above ledge exists
127 void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d,
128 std::vector<CollisionInfo> *collision_info)
130 // Temporary option for old move code
131 if (!physics_override_new_move) {
132 old_move(dtime, env, pos_max_d, collision_info);
136 Map *map = &env->getMap();
137 INodeDefManager *nodemgr = m_client->ndef();
139 v3f position = getPosition();
141 // Copy parent position if local player is attached
144 setPosition(overridePosition);
145 m_sneak_node_exists = false;
149 // Skip collision detection if noclip mode is used
150 bool fly_allowed = m_client->checkLocalPrivilege("fly");
151 bool noclip = m_client->checkLocalPrivilege("noclip") &&
152 g_settings->getBool("noclip");
153 bool free_move = noclip && fly_allowed && g_settings->getBool("free_move");
155 position += m_speed * dtime;
156 setPosition(position);
157 m_sneak_node_exists = false;
165 bool is_valid_position;
170 Check if player is in liquid (the oscillating value)
173 // If in liquid, the threshold of coming out is at higher y
176 pp = floatToInt(position + v3f(0,BS*0.1,0), BS);
177 node = map->getNodeNoEx(pp, &is_valid_position);
178 if (is_valid_position) {
179 in_liquid = nodemgr->get(node.getContent()).isLiquid();
180 liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
185 // If not in liquid, the threshold of going in is at lower y
188 pp = floatToInt(position + v3f(0,BS*0.5,0), BS);
189 node = map->getNodeNoEx(pp, &is_valid_position);
190 if (is_valid_position) {
191 in_liquid = nodemgr->get(node.getContent()).isLiquid();
192 liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
200 Check if player is in liquid (the stable value)
202 pp = floatToInt(position + v3f(0,0,0), BS);
203 node = map->getNodeNoEx(pp, &is_valid_position);
204 if (is_valid_position) {
205 in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
207 in_liquid_stable = false;
211 Check if player is climbing
215 pp = floatToInt(position + v3f(0,0.5*BS,0), BS);
216 v3s16 pp2 = floatToInt(position + v3f(0,-0.2*BS,0), BS);
217 node = map->getNodeNoEx(pp, &is_valid_position);
218 bool is_valid_position2;
219 MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2);
221 if (!(is_valid_position && is_valid_position2)) {
224 is_climbing = (nodemgr->get(node.getContent()).climbable
225 || nodemgr->get(node2.getContent()).climbable) && !free_move;
230 Collision uncertainty radius
231 Make it a bit larger than the maximum distance of movement
233 //f32 d = pos_max_d * 1.1;
234 // A fairly large value in here makes moving smoother
237 // This should always apply, otherwise there are glitches
238 sanity_check(d > pos_max_d);
240 // Max. distance (X, Z) over border for sneaking determined by collision box
241 // * 0.49 to keep the center just barely on the node
242 v3f sneak_max = m_collisionbox.getExtent() * 0.49;
243 if (m_sneak_ladder_detected) {
244 // restore legacy behaviour (this makes the m_speed.Y hack necessary)
245 sneak_max = v3f(0.4 * BS, 0, 0.4 * BS);
249 If sneaking, keep in range from the last walked node and don't
252 if (control.sneak && m_sneak_node_exists &&
253 !(fly_allowed && g_settings->getBool("free_move")) &&
254 !in_liquid && !is_climbing &&
255 physics_override_sneak) {
256 const v3f sn_f = intToFloat(m_sneak_node, BS);
257 const v3f bmin = sn_f + m_sneak_node_bb_top.MinEdge;
258 const v3f bmax = sn_f + m_sneak_node_bb_top.MaxEdge;
259 const v3f old_pos = position;
260 const v3f old_speed = m_speed;
262 position.X = rangelim(position.X,
263 bmin.X - sneak_max.X, bmax.X + sneak_max.X);
264 position.Z = rangelim(position.Z,
265 bmin.Z - sneak_max.Z, bmax.Z + sneak_max.Z);
267 if (position.X != old_pos.X)
269 if (position.Z != old_pos.Z)
272 // Because we keep the player collision box on the node, limiting
273 // position.Y is not necessary but useful to prevent players from
274 // being inside a node if sneaking on e.g. the lower part of a stair
275 if (!m_sneak_ladder_detected) {
276 position.Y = MYMAX(position.Y, bmax.Y);
278 // legacy behaviour that sometimes causes some weird slow sinking
279 m_speed.Y = MYMAX(m_speed.Y, 0);
282 if (collision_info != NULL &&
283 m_speed.Y - old_speed.Y > BS) {
284 // Collide with sneak node, report fall damage
285 CollisionInfo sn_info;
286 sn_info.node_p = m_sneak_node;
287 sn_info.old_speed = old_speed;
288 sn_info.new_speed = m_speed;
289 collision_info->push_back(sn_info);
293 // TODO: this shouldn't be hardcoded but transmitted from server
294 float player_stepheight = (touching_ground) ? (BS * 0.6f) : (BS * 0.2f);
297 player_stepheight += (0.6f * BS);
300 v3f accel_f = v3f(0,0,0);
302 collisionMoveResult result = collisionMoveSimple(env, m_client,
303 pos_max_d, m_collisionbox, player_stepheight, dtime,
304 &position, &m_speed, accel_f);
307 If the player's feet touch the topside of any node, this is
310 Player is allowed to jump when this is true.
312 bool touching_ground_was = touching_ground;
313 touching_ground = result.touching_ground;
315 // We want the top of the sneak node to be below the players feet
317 if (m_sneak_node_exists)
318 position_y_mod = m_sneak_node_bb_top.MaxEdge.Y - 0.05 * BS;
320 position_y_mod = (0.5 - 0.05) * BS;
321 v3s16 current_node = floatToInt(position - v3f(0, position_y_mod, 0), BS);
323 Check the nodes under the player to see from which node the
324 player is sneaking from, if any. If the node from under
325 the player has been removed, the player falls.
327 if (m_sneak_node_exists &&
328 nodemgr->get(map->getNodeNoEx(m_old_node_below)).name == "air" &&
329 m_old_node_below_type != "air") {
330 // Old node appears to have been removed; that is,
331 // it wasn't air before but now it is
332 m_need_to_get_new_sneak_node = false;
333 m_sneak_node_exists = false;
334 } else if (nodemgr->get(map->getNodeNoEx(current_node)).name != "air") {
335 // We are on something, so make sure to recalculate the sneak
337 m_need_to_get_new_sneak_node = true;
340 if (m_need_to_get_new_sneak_node && physics_override_sneak) {
341 v2f player_p2df(position.X, position.Z);
342 f32 min_distance_f = 100000.0 * BS;
343 v3s16 new_sneak_node = m_sneak_node;
344 for(s16 x=-1; x<=1; x++)
345 for(s16 z=-1; z<=1; z++)
347 v3s16 p = current_node + v3s16(x,0,z);
348 v3f pf = intToFloat(p, BS);
349 v2f node_p2df(pf.X, pf.Z);
350 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
352 if (distance_f > min_distance_f ||
353 fabs(player_p2df.X-node_p2df.X) > (.5+.1)*BS + sneak_max.X ||
354 fabs(player_p2df.Y-node_p2df.Y) > (.5+.1)*BS + sneak_max.Z)
358 // The node to be sneaked on has to be walkable
359 node = map->getNodeNoEx(p, &is_valid_position);
360 if (!is_valid_position || !nodemgr->get(node).walkable)
362 // And the node(s) above have to be nonwalkable
364 if (!physics_override_sneak_glitch) {
366 (m_collisionbox.MaxEdge.Y - m_collisionbox.MinEdge.Y) / BS
368 for (u16 y = 1; y <= height; y++) {
369 node = map->getNodeNoEx(p + v3s16(0,y,0), &is_valid_position);
370 if (!is_valid_position || nodemgr->get(node).walkable) {
376 // legacy behaviour: check just one node
377 node = map->getNodeNoEx(p + v3s16(0,1,0), &is_valid_position);
378 ok = is_valid_position && !nodemgr->get(node).walkable;
383 min_distance_f = distance_f;
387 bool sneak_node_found = (min_distance_f < 100000.0 * BS);
388 m_sneak_node = new_sneak_node;
389 m_sneak_node_exists = sneak_node_found;
391 if (sneak_node_found) {
392 // Update saved top bounding box of sneak node
393 MapNode n = map->getNodeNoEx(m_sneak_node);
394 std::vector<aabb3f> nodeboxes;
395 n.getCollisionBoxes(nodemgr, &nodeboxes);
396 m_sneak_node_bb_top = getTopBoundingBox(nodeboxes);
398 m_sneak_ladder_detected = physics_override_sneak_glitch &&
399 detectSneakLadder(map, nodemgr, floatToInt(position, BS));
401 m_sneak_ladder_detected = false;
406 If 'sneak glitch' enabled detect ledge for old sneak-jump
407 behaviour of climbing onto a ledge 2 nodes up.
409 if (physics_override_sneak_glitch && control.sneak && control.jump)
410 m_ledge_detected = detectLedge(map, nodemgr, floatToInt(position, BS));
413 Set new position but keep sneak node set
415 bool sneak_node_exists = m_sneak_node_exists;
416 setPosition(position);
417 m_sneak_node_exists = sneak_node_exists;
423 // Dont report if flying
424 if(collision_info && !(g_settings->getBool("free_move") && fly_allowed)) {
425 for(size_t i=0; i<result.collisions.size(); i++) {
426 const CollisionInfo &info = result.collisions[i];
427 collision_info->push_back(info);
431 if(!result.standing_on_object && !touching_ground_was && touching_ground) {
432 MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
433 m_client->event()->put(e);
435 // Set camera impact value to be used for view bobbing
436 camera_impact = getSpeed().Y * -1;
440 camera_barely_in_ceiling = false;
441 v3s16 camera_np = floatToInt(getEyePosition(), BS);
442 MapNode n = map->getNodeNoEx(camera_np);
443 if(n.getContent() != CONTENT_IGNORE){
444 if(nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2){
445 camera_barely_in_ceiling = true;
451 Update the node last under the player
453 m_old_node_below = floatToInt(position - v3f(0,BS/2,0), BS);
454 m_old_node_below_type = nodemgr->get(map->getNodeNoEx(m_old_node_below)).name;
457 Check properties of the node on which the player is standing
459 const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(getStandingNodePos()));
460 // Determine if jumping is possible
461 m_can_jump = touching_ground && !in_liquid;
462 if (itemgroup_get(f.groups, "disable_jump"))
464 else if (control.sneak && m_sneak_ladder_detected && !in_liquid && !is_climbing)
467 // Jump key pressed while jumping off from a bouncy block
468 if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") &&
469 m_speed.Y >= -0.5 * BS) {
470 float jumpspeed = movement_speed_jump * physics_override_jump;
472 // Reduce boost when speed already is high
473 m_speed.Y += jumpspeed / (1 + (m_speed.Y / 16 ));
475 m_speed.Y += jumpspeed;
482 void LocalPlayer::move(f32 dtime, Environment *env, f32 pos_max_d)
484 move(dtime, env, pos_max_d, NULL);
487 void LocalPlayer::applyControl(float dtime)
490 swimming_vertical = false;
492 setPitch(control.pitch);
495 // Nullify speed and don't run positioning code if the player is attached
498 setSpeed(v3f(0,0,0));
502 v3f move_direction = v3f(0,0,1);
503 move_direction.rotateXZBy(getYaw());
505 v3f speedH = v3f(0,0,0); // Horizontal (X, Z)
506 v3f speedV = v3f(0,0,0); // Vertical (Y)
508 bool fly_allowed = m_client->checkLocalPrivilege("fly");
509 bool fast_allowed = m_client->checkLocalPrivilege("fast");
511 bool free_move = fly_allowed && g_settings->getBool("free_move");
512 bool fast_move = fast_allowed && g_settings->getBool("fast_move");
513 // When aux1_descends is enabled the fast key is used to go down, so fast isn't possible
514 bool fast_climb = fast_move && control.aux1 && !g_settings->getBool("aux1_descends");
515 bool continuous_forward = g_settings->getBool("continuous_forward");
516 bool always_fly_fast = g_settings->getBool("always_fly_fast");
518 // Whether superspeed mode is used or not
519 bool superspeed = false;
521 if (always_fly_fast && free_move && fast_move)
524 // Old descend control
525 if(g_settings->getBool("aux1_descends"))
527 // If free movement and fast movement, always move fast
528 if(free_move && fast_move)
531 // Auxiliary button 1 (E)
536 // In free movement mode, aux1 descends
538 speedV.Y = -movement_speed_fast;
540 speedV.Y = -movement_speed_walk;
542 else if(in_liquid || in_liquid_stable)
544 speedV.Y = -movement_speed_walk;
545 swimming_vertical = true;
549 speedV.Y = -movement_speed_climb;
553 // If not free movement but fast is allowed, aux1 is
560 // New minecraft-like descend control
563 // Auxiliary button 1 (E)
568 // aux1 is "Turbo button"
578 // In free movement mode, sneak descends
579 if (fast_move && (control.aux1 || always_fly_fast))
580 speedV.Y = -movement_speed_fast;
582 speedV.Y = -movement_speed_walk;
584 else if(in_liquid || in_liquid_stable)
587 speedV.Y = -movement_speed_fast;
589 speedV.Y = -movement_speed_walk;
590 swimming_vertical = true;
595 speedV.Y = -movement_speed_fast;
597 speedV.Y = -movement_speed_climb;
602 if (continuous_forward)
603 speedH += move_direction;
606 if (continuous_forward) {
610 speedH += move_direction;
614 speedH -= move_direction;
616 if (!control.up && !control.down) {
617 speedH -= move_direction *
618 (control.forw_move_joystick_axis / 32767.f);
621 speedH += move_direction.crossProduct(v3f(0,1,0));
624 speedH += move_direction.crossProduct(v3f(0,-1,0));
626 if (!control.left && !control.right) {
627 speedH -= move_direction.crossProduct(v3f(0,1,0)) *
628 (control.sidew_move_joystick_axis / 32767.f);
633 if (g_settings->getBool("aux1_descends") || always_fly_fast) {
635 speedV.Y = movement_speed_fast;
637 speedV.Y = movement_speed_walk;
639 if(fast_move && control.aux1)
640 speedV.Y = movement_speed_fast;
642 speedV.Y = movement_speed_walk;
648 NOTE: The d value in move() affects jump height by
649 raising the height at which the jump speed is kept
650 at its starting value
652 v3f speedJ = getSpeed();
653 if(speedJ.Y >= -0.5 * BS) {
654 if (m_ledge_detected) {
655 // Limit jump speed to a minimum that allows
656 // jumping up onto a ledge 2 nodes up.
657 speedJ.Y = movement_speed_jump *
658 MYMAX(physics_override_jump, 1.3f);
660 m_ledge_detected = false;
662 speedJ.Y = movement_speed_jump * physics_override_jump;
666 MtEvent *e = new SimpleTriggerEvent("PlayerJump");
667 m_client->event()->put(e);
673 speedV.Y = movement_speed_fast;
675 speedV.Y = movement_speed_walk;
676 swimming_vertical = true;
681 speedV.Y = movement_speed_fast;
683 speedV.Y = movement_speed_climb;
687 // The speed of the player (Y is ignored)
688 if(superspeed || (is_climbing && fast_climb) || ((in_liquid || in_liquid_stable) && fast_climb))
689 speedH = speedH.normalize() * movement_speed_fast;
690 else if(control.sneak && !free_move && !in_liquid && !in_liquid_stable)
691 speedH = speedH.normalize() * movement_speed_crouch;
693 speedH = speedH.normalize() * movement_speed_walk;
695 // Acceleration increase
696 f32 incH = 0; // Horizontal (X, Z)
697 f32 incV = 0; // Vertical (Y)
698 if((!touching_ground && !free_move && !is_climbing && !in_liquid) || (!free_move && m_can_jump && control.jump))
700 // Jumping and falling
701 if(superspeed || (fast_move && control.aux1))
702 incH = movement_acceleration_fast * BS * dtime;
704 incH = movement_acceleration_air * BS * dtime;
705 incV = 0; // No vertical acceleration in air
707 else if (superspeed || (is_climbing && fast_climb) || ((in_liquid || in_liquid_stable) && fast_climb))
708 incH = incV = movement_acceleration_fast * BS * dtime;
710 incH = incV = movement_acceleration_default * BS * dtime;
712 // Accelerate to target speed with maximum increment
713 accelerateHorizontal(speedH * physics_override_speed, incH * physics_override_speed);
714 accelerateVertical(speedV * physics_override_speed, incV * physics_override_speed);
717 v3s16 LocalPlayer::getStandingNodePos()
719 if(m_sneak_node_exists)
721 return floatToInt(getPosition() - v3f(0, BS, 0), BS);
724 v3s16 LocalPlayer::getFootstepNodePos()
726 if (in_liquid_stable)
727 // Emit swimming sound if the player is in liquid
728 return floatToInt(getPosition(), BS);
730 // BS * 0.05 below the player's feet ensures a 1/16th height
731 // nodebox is detected instead of the node below it.
732 return floatToInt(getPosition() - v3f(0, BS * 0.05f, 0), BS);
733 // A larger distance below is necessary for a footstep sound
734 // when landing after a jump or fall. BS * 0.5 ensures water
735 // sounds when swimming in 1 node deep water.
736 return floatToInt(getPosition() - v3f(0, BS * 0.5f, 0), BS);
739 v3s16 LocalPlayer::getLightPosition() const
741 return floatToInt(m_position + v3f(0,BS+BS/2,0), BS);
744 v3f LocalPlayer::getEyeOffset() const
746 float eye_height = camera_barely_in_ceiling ? 1.5f : 1.625f;
747 return v3f(0, BS * eye_height, 0);
750 // Horizontal acceleration (X and Z), Y direction is ignored
751 void LocalPlayer::accelerateHorizontal(const v3f &target_speed, const f32 max_increase)
753 if (max_increase == 0)
756 v3f d_wanted = target_speed - m_speed;
758 f32 dl = d_wanted.getLength();
759 if (dl > max_increase)
762 v3f d = d_wanted.normalize() * dl;
768 // Vertical acceleration (Y), X and Z directions are ignored
769 void LocalPlayer::accelerateVertical(const v3f &target_speed, const f32 max_increase)
771 if (max_increase == 0)
774 f32 d_wanted = target_speed.Y - m_speed.Y;
775 if (d_wanted > max_increase)
776 d_wanted = max_increase;
777 else if (d_wanted < -max_increase)
778 d_wanted = -max_increase;
780 m_speed.Y += d_wanted;
783 // Temporary option for old move code
784 void LocalPlayer::old_move(f32 dtime, Environment *env, f32 pos_max_d,
785 std::vector<CollisionInfo> *collision_info)
787 Map *map = &env->getMap();
788 INodeDefManager *nodemgr = m_client->ndef();
790 v3f position = getPosition();
792 // Copy parent position if local player is attached
794 setPosition(overridePosition);
795 m_sneak_node_exists = false;
799 // Skip collision detection if noclip mode is used
800 bool fly_allowed = m_client->checkLocalPrivilege("fly");
801 bool noclip = m_client->checkLocalPrivilege("noclip") &&
802 g_settings->getBool("noclip");
803 bool free_move = noclip && fly_allowed && g_settings->getBool("free_move");
805 position += m_speed * dtime;
806 setPosition(position);
807 m_sneak_node_exists = false;
814 bool is_valid_position;
819 Check if player is in liquid (the oscillating value)
822 // If in liquid, the threshold of coming out is at higher y
823 pp = floatToInt(position + v3f(0, BS * 0.1, 0), BS);
824 node = map->getNodeNoEx(pp, &is_valid_position);
825 if (is_valid_position) {
826 in_liquid = nodemgr->get(node.getContent()).isLiquid();
827 liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
832 // If not in liquid, the threshold of going in is at lower y
833 pp = floatToInt(position + v3f(0, BS * 0.5, 0), BS);
834 node = map->getNodeNoEx(pp, &is_valid_position);
835 if (is_valid_position) {
836 in_liquid = nodemgr->get(node.getContent()).isLiquid();
837 liquid_viscosity = nodemgr->get(node.getContent()).liquid_viscosity;
844 Check if player is in liquid (the stable value)
846 pp = floatToInt(position + v3f(0, 0, 0), BS);
847 node = map->getNodeNoEx(pp, &is_valid_position);
848 if (is_valid_position)
849 in_liquid_stable = nodemgr->get(node.getContent()).isLiquid();
851 in_liquid_stable = false;
854 Check if player is climbing
856 pp = floatToInt(position + v3f(0, 0.5 * BS, 0), BS);
857 v3s16 pp2 = floatToInt(position + v3f(0, -0.2 * BS, 0), BS);
858 node = map->getNodeNoEx(pp, &is_valid_position);
859 bool is_valid_position2;
860 MapNode node2 = map->getNodeNoEx(pp2, &is_valid_position2);
862 if (!(is_valid_position && is_valid_position2))
865 is_climbing = (nodemgr->get(node.getContent()).climbable ||
866 nodemgr->get(node2.getContent()).climbable) && !free_move;
869 Collision uncertainty radius
870 Make it a bit larger than the maximum distance of movement
872 //f32 d = pos_max_d * 1.1;
873 // A fairly large value in here makes moving smoother
875 // This should always apply, otherwise there are glitches
876 sanity_check(d > pos_max_d);
877 // Maximum distance over border for sneaking
878 f32 sneak_max = BS * 0.4;
881 If sneaking, keep in range from the last walked node and don't
884 if (control.sneak && m_sneak_node_exists &&
885 !(fly_allowed && g_settings->getBool("free_move")) && !in_liquid &&
886 physics_override_sneak) {
887 f32 maxd = 0.5 * BS + sneak_max;
888 v3f lwn_f = intToFloat(m_sneak_node, BS);
889 position.X = rangelim(position.X, lwn_f.X - maxd, lwn_f.X + maxd);
890 position.Z = rangelim(position.Z, lwn_f.Z - maxd, lwn_f.Z + maxd);
893 // Move up if necessary
894 f32 new_y = (lwn_f.Y - 0.5 * BS) + m_sneak_node_bb_ymax;
895 if (position.Y < new_y)
898 Collision seems broken, since player is sinking when
899 sneaking over the edges of current sneaking_node.
900 TODO (when fixed): Set Y-speed only to 0 when position.Y < new_y.
907 // this shouldn't be hardcoded but transmitted from server
908 float player_stepheight = touching_ground ? (BS * 0.6) : (BS * 0.2);
911 player_stepheight += (0.6 * BS);
914 v3f accel_f = v3f(0, 0, 0);
916 collisionMoveResult result = collisionMoveSimple(env, m_client,
917 pos_max_d, m_collisionbox, player_stepheight, dtime,
918 &position, &m_speed, accel_f);
921 If the player's feet touch the topside of any node, this is
924 Player is allowed to jump when this is true.
926 bool touching_ground_was = touching_ground;
927 touching_ground = result.touching_ground;
929 //bool standing_on_unloaded = result.standing_on_unloaded;
932 Check the nodes under the player to see from which node the
933 player is sneaking from, if any. If the node from under
934 the player has been removed, the player falls.
936 f32 position_y_mod = 0.05 * BS;
937 if (m_sneak_node_bb_ymax > 0)
938 position_y_mod = m_sneak_node_bb_ymax - position_y_mod;
939 v3s16 current_node = floatToInt(position - v3f(0, position_y_mod, 0), BS);
940 if (m_sneak_node_exists &&
941 nodemgr->get(map->getNodeNoEx(m_old_node_below)).name == "air" &&
942 m_old_node_below_type != "air") {
943 // Old node appears to have been removed; that is,
944 // it wasn't air before but now it is
945 m_need_to_get_new_sneak_node = false;
946 m_sneak_node_exists = false;
947 } else if (nodemgr->get(map->getNodeNoEx(current_node)).name != "air") {
948 // We are on something, so make sure to recalculate the sneak
950 m_need_to_get_new_sneak_node = true;
953 if (m_need_to_get_new_sneak_node && physics_override_sneak) {
954 m_sneak_node_bb_ymax = 0;
955 v3s16 pos_i_bottom = floatToInt(position - v3f(0, position_y_mod, 0), BS);
956 v2f player_p2df(position.X, position.Z);
957 f32 min_distance_f = 100000.0 * BS;
958 // If already seeking from some node, compare to it.
959 v3s16 new_sneak_node = m_sneak_node;
960 for (s16 x= -1; x <= 1; x++)
961 for (s16 z= -1; z <= 1; z++) {
962 v3s16 p = pos_i_bottom + v3s16(x, 0, z);
963 v3f pf = intToFloat(p, BS);
964 v2f node_p2df(pf.X, pf.Z);
965 f32 distance_f = player_p2df.getDistanceFrom(node_p2df);
966 f32 max_axis_distance_f = MYMAX(
967 fabs(player_p2df.X - node_p2df.X),
968 fabs(player_p2df.Y - node_p2df.Y));
970 if (distance_f > min_distance_f ||
971 max_axis_distance_f > 0.5 * BS + sneak_max + 0.1 * BS)
974 // The node to be sneaked on has to be walkable
975 node = map->getNodeNoEx(p, &is_valid_position);
976 if (!is_valid_position || nodemgr->get(node).walkable == false)
978 // And the node above it has to be nonwalkable
979 node = map->getNodeNoEx(p + v3s16(0, 1, 0), &is_valid_position);
980 if (!is_valid_position || nodemgr->get(node).walkable)
982 // If not 'sneak_glitch' the node 2 nodes above it has to be nonwalkable
983 if (!physics_override_sneak_glitch) {
984 node =map->getNodeNoEx(p + v3s16(0, 2, 0), &is_valid_position);
985 if (!is_valid_position || nodemgr->get(node).walkable)
989 min_distance_f = distance_f;
993 bool sneak_node_found = (min_distance_f < 100000.0 * BS * 0.9);
995 m_sneak_node = new_sneak_node;
996 m_sneak_node_exists = sneak_node_found;
998 if (sneak_node_found) {
1000 MapNode n = map->getNodeNoEx(m_sneak_node);
1001 std::vector<aabb3f> nodeboxes;
1002 n.getCollisionBoxes(nodemgr, &nodeboxes);
1003 for (std::vector<aabb3f>::iterator it = nodeboxes.begin();
1004 it != nodeboxes.end(); ++it) {
1006 if (box.MaxEdge.Y > cb_max)
1007 cb_max = box.MaxEdge.Y;
1009 m_sneak_node_bb_ymax = cb_max;
1013 If sneaking, the player's collision box can be in air, so
1014 this has to be set explicitly
1016 if (sneak_node_found && control.sneak)
1017 touching_ground = true;
1021 Set new position but keep sneak node set
1023 bool sneak_node_exists = m_sneak_node_exists;
1024 setPosition(position);
1025 m_sneak_node_exists = sneak_node_exists;
1030 // Dont report if flying
1031 if (collision_info && !(g_settings->getBool("free_move") && fly_allowed)) {
1032 for (size_t i = 0; i < result.collisions.size(); i++) {
1033 const CollisionInfo &info = result.collisions[i];
1034 collision_info->push_back(info);
1038 if (!result.standing_on_object && !touching_ground_was && touching_ground) {
1039 MtEvent *e = new SimpleTriggerEvent("PlayerRegainGround");
1040 m_client->event()->put(e);
1041 // Set camera impact value to be used for view bobbing
1042 camera_impact = getSpeed().Y * -1;
1046 camera_barely_in_ceiling = false;
1047 v3s16 camera_np = floatToInt(getEyePosition(), BS);
1048 MapNode n = map->getNodeNoEx(camera_np);
1049 if (n.getContent() != CONTENT_IGNORE) {
1050 if (nodemgr->get(n).walkable && nodemgr->get(n).solidness == 2)
1051 camera_barely_in_ceiling = true;
1056 Update the node last under the player
1058 m_old_node_below = floatToInt(position - v3f(0, BS / 2, 0), BS);
1059 m_old_node_below_type = nodemgr->get(map->getNodeNoEx(m_old_node_below)).name;
1062 Check properties of the node on which the player is standing
1064 const ContentFeatures &f = nodemgr->get(map->getNodeNoEx(getStandingNodePos()));
1065 // Determine if jumping is possible
1066 m_can_jump = touching_ground && !in_liquid;
1067 if (itemgroup_get(f.groups, "disable_jump"))
1069 // Jump key pressed while jumping off from a bouncy block
1070 if (m_can_jump && control.jump && itemgroup_get(f.groups, "bouncy") &&
1071 m_speed.Y >= -0.5 * BS) {
1072 float jumpspeed = movement_speed_jump * physics_override_jump;
1073 if (m_speed.Y > 1) {
1074 // Reduce boost when speed already is high
1075 m_speed.Y += jumpspeed / (1 + (m_speed.Y / 16 ));
1077 m_speed.Y += jumpspeed;