CalculatePath: Fix offset calculation, improve approaching (#2013)

* Square minOffset and maxOffset to match DistanceSquared
* Rewrite squaring
* Add minOffset
* Implement h-score selection
This commit is contained in:
Daenges 2022-05-05 18:05:05 +00:00 committed by GitHub
parent 708815fe61
commit b3cc2351ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -168,6 +168,10 @@ namespace MinecraftClient.Mapping
if (minOffset > maxOffset) if (minOffset > maxOffset)
throw new ArgumentException("minOffset must be lower or equal to maxOffset", "minOffset"); throw new ArgumentException("minOffset must be lower or equal to maxOffset", "minOffset");
// We always use distance squared so our limits must also be squared.
minOffset *= minOffset;
maxOffset *= maxOffset;
Location current = new Location(); // Location that is currently processed Location current = new Location(); // Location that is currently processed
Location closestGoal = new Location(); // Closest Location to the goal. Used for approaching if goal can not be reached or was not found. Location closestGoal = new Location(); // Closest Location to the goal. Used for approaching if goal can not be reached or was not found.
HashSet<Location> ClosedSet = new HashSet<Location>(); // The set of locations already evaluated. HashSet<Location> ClosedSet = new HashSet<Location>(); // The set of locations already evaluated.
@ -186,7 +190,9 @@ namespace MinecraftClient.Mapping
OpenSet.Select(location => f_score.ContainsKey(location) OpenSet.Select(location => f_score.ContainsKey(location)
? new KeyValuePair<Location, int>(location, f_score[location]) ? new KeyValuePair<Location, int>(location, f_score[location])
: new KeyValuePair<Location, int>(location, int.MaxValue)) : new KeyValuePair<Location, int>(location, int.MaxValue))
.OrderBy(pair => pair.Value).First().Key; .OrderBy(pair => pair.Value).
// Sort for h-score (f-score - g-score) to get smallest distance to goal if f-scores are equal
ThenBy(pair => f_score[pair.Key]-g_score[pair.Key]).First().Key;
// Only assert a value if it is of actual use later // Only assert a value if it is of actual use later
if (maxOffset > 0 && ClosedSet.Count > 0) if (maxOffset > 0 && ClosedSet.Count > 0)