Minecraft-Console-Client/MinecraftClient/Pathing/Execution/Templates/WalkTemplate.cs
BruceChen 5de169db64 pathing: stabilize 0-replan round-trip on ledge/descend runs
Fix a cluster of execution-layer issues that caused replans and void
falls when traversing narrow ledges and multi-block descents between
(251.5,141,210.5) and (252.5,138,220.5):

- WalkTemplate / GroundedSegmentController: suppress the pre-rotation
  bias toward the next segment's exit heading on stable-footing Turn
  exits where the next segment is not a jump.  The next template
  snaps yaw on its first tick anyway, and pre-rotating mid-stride on
  a 1-block walkway pushes sprint drift perpendicular to the path and
  walks the bot off the edge.  Turn exits into a jump still get the
  bias so the takeoff direction stays aligned.

- GroundedSegmentController.ShouldComplete: relax the headingReady
  gate for Turn exits with stable footing so the segment can complete
  once yaw is aligned with either the current or the next segment
  heading (within 25/15 deg).  Without this the removed bias would
  leave the bot stuck at the end of a walkway waiting for a rotation
  that never happens.

- DescendTemplate: restrict the airborne exit-heading bias so it only
  kicks in when the footprint is inside the landing block, or on
  single-step drops where the fall is too short for lateral drift to
  miss the landing column.  On 2+ block drops the bot now keeps yaw
  pointed at the landing center for the whole fall.

- DescendTemplate: add a multi-block overshoot guard on PrepareJump
  exits.  Once airborne and past the landing end-plane on a 2+ Y
  drop, release forward/sprint and press back briefly so air drag
  pulls the bot back into the 1x1 landing column instead of sailing
  one block past it into the neighbouring void.

Live round-trip between the two goal coordinates now completes with
zero replans in three consecutive runs in each direction.  Full unit
test suite is unchanged from the pre-existing baseline (22 failing
tests, all orthogonal to this change).

Made-with: Cursor
2026-04-22 16:43:43 +00:00

107 lines
5.1 KiB
C#

using System;
using MinecraftClient.Mapping;
using MinecraftClient.Physics;
namespace MinecraftClient.Pathing.Execution.Templates
{
/// <summary>
/// Walk/sprint toward a destination on the same Y level.
/// Used for Traverse and Diagonal moves.
/// </summary>
public sealed class WalkTemplate : IActionTemplate
{
public Location ExpectedStart { get; }
public Location ExpectedEnd { get; }
private readonly PathSegment _segment;
private readonly PathSegment? _nextSegment;
private int _tickCount;
private Location _lastPos;
private int _stuckTicks;
private int _airborneTicks;
public WalkTemplate(PathSegment segment, PathSegment? nextSegment)
{
_segment = segment;
_nextSegment = nextSegment;
ExpectedStart = segment.Start;
ExpectedEnd = segment.End;
_lastPos = segment.Start;
}
public TemplateState Tick(Location pos, PlayerPhysics physics, MovementInput input, World world)
{
_tickCount++;
double dx = ExpectedEnd.X - pos.X;
double dz = ExpectedEnd.Z - pos.Z;
double dy = ExpectedEnd.Y - pos.Y;
// While approaching the end, steer via pos->end so lateral drift self-
// corrects. Once the center has entered the target block the pos->end
// vector becomes tiny/negative and flips yaw by ~180 degrees, which
// fights GroundedSegmentController's exit-heading rotation and locks
// yaw at a local equilibrium (e.g. 333 deg on a 1,1 diagonal) where
// HeadingPenalty never drops below the 8 deg ShouldComplete gate.
// Fall back to the stable quantized segment heading once inside the
// target block so the completion check and exit rotation converge.
// Skip the exit-heading bias on stable-footing Turn exits: it
// rotates yaw mid-segment while the bot still has along-segment
// momentum, which on a 1-block walkway drifts the bot
// perpendicular and walks it off the edge. The next segment's
// template snaps yaw on its first tick, so nothing is lost by
// deferring the rotation. Keep the bias when the next segment
// is a jump (RequireJumpReady): we need yaw aligned before
// takeoff or the jump direction will be off.
bool suppressBiasForSafeTurn = _segment.ExitTransition == PathTransitionType.Turn
&& _segment.ExitHints.RequireStableFooting
&& !_segment.ExitHints.RequireJumpReady;
float targetYaw;
if (!suppressBiasForSafeTurn
&& TemplateHelper.ShouldBiasTowardExitHeading(pos, _segment))
targetYaw = TemplateHelper.GetExitHeadingYaw(_segment);
else if (TemplateFootingHelper.IsCenterInsideTargetBlock(pos, _segment.End))
targetYaw = TemplateHelper.CalculateYaw(_segment.HeadingX, _segment.HeadingZ);
else
targetYaw = TemplateHelper.CalculateYaw(dx, dz);
float targetPitch = TemplateHelper.CalculatePitch(dx, dy, dz);
// Snap yaw on the first tick so we don't push forward input while the
// bot is still rotating from whatever yaw it had before this segment
// started (e.g. a random post-teleport orientation). Baritone-style:
// the server accepts instant yaw updates and the narrow 1-block lanes
// in parkour courses don't tolerate 3 ticks of sideways drift.
physics.Yaw = _tickCount == 1
? targetYaw
: TemplateHelper.SmoothYaw(physics.Yaw, targetYaw);
physics.Pitch = TemplateHelper.SmoothPitch(physics.Pitch, targetPitch);
GroundedSegmentController.Apply(_segment, _nextSegment, pos, physics, input, world);
if (GroundedSegmentController.ShouldComplete(_segment, pos, physics))
return TemplateState.Complete;
double movedSq = TemplateHelper.HorizontalDistanceSq(pos, _lastPos);
_stuckTicks = movedSq < 0.0005 ? _stuckTicks + 1 : 0;
_lastPos = pos;
// Walk/Diagonal is a grounded move: if the bot is airborne for more
// than a handful of ticks the platform is gone beneath us (e.g. we
// rotated toward an exit heading on a narrow 1-block walkway and
// stepped off the edge). Fail fast so the replanner can recover
// before gravity carries the bot 10+ blocks out of position.
_airborneTicks = physics.OnGround ? 0 : _airborneTicks + 1;
if (_airborneTicks > 8)
return TemplateState.Failed;
int maxTicks = _segment.ExitTransition switch
{
PathTransitionType.ContinueStraight => 100,
PathTransitionType.PrepareJump => 80,
_ => 140
};
if (_stuckTicks > 40 || _tickCount > maxTicks)
return TemplateState.Failed;
return TemplateState.InProgress;
}
}
}