Skip to content

Commit b3cc235

Browse files
authored
CalculatePath: Fix offset calculation, improve approaching (#2013)
* Square minOffset and maxOffset to match DistanceSquared * Rewrite squaring * Add minOffset * Implement h-score selection
1 parent 708815f commit b3cc235

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

MinecraftClient/Mapping/Movement.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public static Queue<Location> CalculatePath(World world, Location start, Locatio
167167

168168
if (minOffset > maxOffset)
169169
throw new ArgumentException("minOffset must be lower or equal to maxOffset", "minOffset");
170+
171+
// We always use distance squared so our limits must also be squared.
172+
minOffset *= minOffset;
173+
maxOffset *= maxOffset;
170174

171175
Location current = new Location(); // Location that is currently processed
172176
Location closestGoal = new Location(); // Closest Location to the goal. Used for approaching if goal can not be reached or was not found.
@@ -186,8 +190,10 @@ public static Queue<Location> CalculatePath(World world, Location start, Locatio
186190
OpenSet.Select(location => f_score.ContainsKey(location)
187191
? new KeyValuePair<Location, int>(location, f_score[location])
188192
: new KeyValuePair<Location, int>(location, int.MaxValue))
189-
.OrderBy(pair => pair.Value).First().Key;
190-
193+
.OrderBy(pair => pair.Value).
194+
// Sort for h-score (f-score - g-score) to get smallest distance to goal if f-scores are equal
195+
ThenBy(pair => f_score[pair.Key]-g_score[pair.Key]).First().Key;
196+
191197
// Only assert a value if it is of actual use later
192198
if (maxOffset > 0 && ClosedSet.Count > 0)
193199
// Get the block that currently is closest to the goal

0 commit comments

Comments
 (0)