fix: correct PathNode.Pack bit overlap causing hash collisions

The X and Z fields shared bit 36, causing nodes like (1,80,0)
and (0,80,0) to hash to the same value. Fixed by using proper
non-overlapping bit allocation: X in bits 38-63, Z in bits 12-37,
Y in bits 0-11. Added diagnostic logging to pathfind command.

Made-with: Cursor
This commit is contained in:
BruceChen 2026-04-11 02:08:16 +08:00
parent 1abab20f17
commit e9b19d3cbb
7 changed files with 1389 additions and 3 deletions

View file

@ -68,6 +68,31 @@ namespace MinecraftClient.Commands
Task.Run(() =>
{
try
{
handler.Log.Info($"[Pathfind] Diagnosing blocks around start ({startX},{startY},{startZ}):");
for (int ddx = -1; ddx <= 1; ddx++)
{
for (int ddz = -1; ddz <= 1; ddz++)
{
int tx = startX + ddx, tz = startZ + ddz;
var below = ctx.GetMaterial(tx, startY - 1, tz);
var body = ctx.GetMaterial(tx, startY, tz);
var head = ctx.GetMaterial(tx, startY + 1, tz);
bool canOn = ctx.CanWalkOn(tx, startY - 1, tz);
bool canThru = ctx.CanWalkThrough(tx, startY, tz);
bool canThruH = ctx.CanWalkThrough(tx, startY + 1, tz);
handler.Log.Info($" ({tx},{tz}): below={below}(walkOn={canOn}) body={body}(thru={canThru}) head={head}(thru={canThruH})");
}
}
handler.Log.Info($"[Pathfind] ChunkLoaded at start: {ctx.IsChunkLoaded(startX, startZ)}");
handler.Log.Info($"[Pathfind] ChunkLoaded at goal: {ctx.IsChunkLoaded(goalX, goalZ)}");
}
catch (Exception ex)
{
handler.Log.Warn($"[Pathfind] Diagnostic exception: {ex.Message}");
}
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var result = finder.Calculate(ctx, startX, startY, startZ, goalObj, cts.Token, timeoutMs: 10000);

View file

@ -96,6 +96,9 @@ namespace MinecraftClient.Pathing.Core
current.IsClosed = true;
nodesExplored++;
if (nodesExplored <= 10)
DebugLog?.Invoke($"[A*] Expand #{nodesExplored}: ({current.X},{current.Y},{current.Z}) F={current.FCost:F2} G={current.GCost:F2} H={current.HCost:F2}, openSet={openSet.Count}");
if (goal.IsInGoal(current.X, current.Y, current.Z))
{
DebugLog?.Invoke($"[A*] Goal reached! {nodesExplored} nodes, {sw.ElapsedMilliseconds}ms");
@ -108,6 +111,15 @@ namespace MinecraftClient.Pathing.Core
moveResult.Cost = 0;
move.Calculate(ctx, current.X, current.Y, current.Z, ref moveResult);
if (nodesExplored <= 2)
{
if (moveResult.IsImpossible)
DebugLog?.Invoke($"[A*] move {move.Type}({move.XOffset},{move.ZOffset}) from ({current.X},{current.Y},{current.Z}): IMPOSSIBLE");
else
DebugLog?.Invoke($"[A*] move {move.Type}({move.XOffset},{move.ZOffset}) from ({current.X},{current.Y},{current.Z}): " +
$"-> ({moveResult.DestX},{moveResult.DestY},{moveResult.DestZ}) cost={moveResult.Cost:F2}");
}
if (moveResult.IsImpossible)
continue;
@ -127,6 +139,8 @@ namespace MinecraftClient.Pathing.Core
if (nodeMap.TryGetValue(packed, out var neighbor))
{
if (nodesExplored <= 2)
DebugLog?.Invoke($"[A*] EXISTS ({nx},{ny},{nz}) pack={packed} actual=({neighbor.X},{neighbor.Y},{neighbor.Z}) closed={neighbor.IsClosed} tentG={tentativeG:F2} existG={neighbor.GCost:F2}");
if (neighbor.IsClosed)
continue;
if (tentativeG >= neighbor.GCost)
@ -140,6 +154,8 @@ namespace MinecraftClient.Pathing.Core
}
else
{
if (nodesExplored <= 2)
DebugLog?.Invoke($"[A*] NEW ({nx},{ny},{nz}) pack={packed} G={tentativeG:F2} H={goal.Heuristic(nx, ny, nz):F2}");
neighbor = new PathNode(nx, ny, nz)
{
GCost = tentativeG,
@ -155,6 +171,8 @@ namespace MinecraftClient.Pathing.Core
double partialScore = neighbor.HCost + neighbor.GCost * 0.5;
if (partialScore < bestPartialScore)
{
if (nodesExplored <= 3)
DebugLog?.Invoke($"[A*] partial improved: ({neighbor.X},{neighbor.Y},{neighbor.Z}) score={partialScore:F2} < {bestPartialScore:F2}");
bestPartialScore = partialScore;
bestPartialNode = neighbor;
}

View file

@ -31,9 +31,11 @@ namespace MinecraftClient.Pathing.Core
public static long Pack(int x, int y, int z)
{
return ((long)(x + 30_000_000) << 36)
| ((long)(z + 30_000_000) << 12)
| (long)((y + 64) & 0xFFF);
// 26 bits for X (0..60M), 26 bits for Z (0..60M), 12 bits for Y (-2048..2047)
long px = (long)(x + 30_000_000) & 0x3FFFFFF;
long pz = (long)(z + 30_000_000) & 0x3FFFFFF;
long py = (long)(y + 2048) & 0xFFF;
return (px << 38) | (pz << 12) | py;
}
}
}