Significantly improve NPC steering (#17931)

This commit is contained in:
metalgearsloth
2023-07-14 00:48:04 +10:00
committed by GitHub
parent 72f4560483
commit c43db830ea
7 changed files with 62 additions and 37 deletions

View File

@@ -17,7 +17,7 @@ public abstract class PathRequest
public Task<PathResult> Task => Tcs.Task;
public readonly TaskCompletionSource<PathResult> Tcs;
public Queue<PathPoly> Polys = new();
public List<PathPoly> Polys = new();
public bool Started = false;
@@ -103,9 +103,9 @@ public sealed class BFSPathRequest : PathRequest
public sealed class PathResultEvent
{
public PathResult Result;
public readonly Queue<PathPoly> Path;
public readonly List<PathPoly> Path;
public PathResultEvent(PathResult result, Queue<PathPoly> path)
public PathResultEvent(PathResult result, List<PathPoly> path)
{
Result = result;
Path = path;

View File

@@ -24,7 +24,7 @@ public sealed partial class PathfindingSystem
private static readonly PathComparer PathPolyComparer = new();
private Queue<PathPoly> ReconstructPath(Dictionary<PathPoly, PathPoly> path, PathPoly currentNodeRef)
private List<PathPoly> ReconstructPath(Dictionary<PathPoly, PathPoly> path, PathPoly currentNodeRef)
{
var running = new List<PathPoly> { currentNodeRef };
while (path.ContainsKey(currentNodeRef))
@@ -35,10 +35,8 @@ public sealed partial class PathfindingSystem
running.Add(currentNodeRef);
}
running = Simplify(running);
running.Reverse();
var result = new Queue<PathPoly>(running);
return result;
return running;
}
private float GetTileCost(PathRequest request, PathPoly start, PathPoly end)

View File

@@ -238,7 +238,7 @@ namespace Content.Server.NPC.Pathfinding
PathFlags flags = PathFlags.None)
{
if (!TryComp<TransformComponent>(entity, out var start))
return new PathResultEvent(PathResult.NoPath, new Queue<PathPoly>());
return new PathResultEvent(PathResult.NoPath, new List<PathPoly>());
var layer = 0;
var mask = 0;
@@ -252,7 +252,7 @@ namespace Content.Server.NPC.Pathfinding
var path = await GetPath(request);
if (path.Result != PathResult.Path)
return new PathResultEvent(PathResult.NoPath, new Queue<PathPoly>());
return new PathResultEvent(PathResult.NoPath, new List<PathPoly>());
return new PathResultEvent(PathResult.Path, path.Path);
}
@@ -280,14 +280,13 @@ namespace Content.Server.NPC.Pathfinding
return 0f;
var distance = 0f;
var node = path.Path.Dequeue();
var lastNode = node;
var lastNode = path.Path[0];
do
for (var i = 1; i < path.Path.Count; i++)
{
var node = path.Path[i];
distance += GetTileCost(request, lastNode, node);
lastNode = node;
} while (path.Path.TryDequeue(out node));
}
return distance;
}
@@ -301,7 +300,7 @@ namespace Content.Server.NPC.Pathfinding
{
if (!TryComp<TransformComponent>(entity, out var xform) ||
!TryComp<TransformComponent>(target, out var targetXform))
return new PathResultEvent(PathResult.NoPath, new Queue<PathPoly>());
return new PathResultEvent(PathResult.NoPath, new List<PathPoly>());
var request = GetRequest(entity, xform.Coordinates, targetXform.Coordinates, range, cancelToken, flags);
return await GetPath(request);
@@ -471,7 +470,7 @@ namespace Content.Server.NPC.Pathfinding
if (!request.Task.IsCompletedSuccessfully)
{
return new PathResultEvent(PathResult.NoPath, new Queue<PathPoly>());
return new PathResultEvent(PathResult.NoPath, new List<PathPoly>());
}
// Same context as do_after and not synchronously blocking soooo