Context steering for NPCs (#12915)

This commit is contained in:
metalgearsloth
2022-12-12 14:33:43 +11:00
committed by GitHub
parent 881ba0d48d
commit 7910bd3ff4
17 changed files with 952 additions and 228 deletions

View File

@@ -60,7 +60,7 @@ namespace Content.Server.NPC.Pathfinding
private const int PathTickLimit = 256;
private int _portalIndex;
private Dictionary<int, PathPortal> _portals = new();
private readonly Dictionary<int, PathPortal> _portals = new();
public override void Initialize()
{
@@ -317,6 +317,21 @@ namespace Content.Server.NPC.Pathfinding
return await GetPath(request);
}
/// <summary>
/// Gets a path in a thread-safe way.
/// </summary>
public async Task<PathResultEvent> GetPathSafe(
EntityUid entity,
EntityCoordinates start,
EntityCoordinates end,
float range,
CancellationToken cancelToken,
PathFlags flags = PathFlags.None)
{
var request = GetRequest(entity, start, end, range, cancelToken, flags);
return await GetPath(request, true);
}
/// <summary>
/// Asynchronously gets a path.
/// </summary>
@@ -428,12 +443,22 @@ namespace Content.Server.NPC.Pathfinding
}
private async Task<PathResultEvent> GetPath(
PathRequest request)
PathRequest request, bool safe = false)
{
// We could maybe try an initial quick run to avoid forcing time-slicing over ticks.
// For now it seems okay and it shouldn't block on 1 NPC anyway.
_pathRequests.Add(request);
if (safe)
{
lock (_pathRequests)
{
_pathRequests.Add(request);
}
}
else
{
_pathRequests.Add(request);
}
await request.Task;