Pathfinder rework (#11452)

This commit is contained in:
metalgearsloth
2022-09-30 14:39:48 +10:00
committed by GitHub
parent fd3b29fb03
commit f456ad911e
80 changed files with 3606 additions and 4374 deletions

View File

@@ -1,3 +1,4 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Content.Server.NPC.Pathfinding;
@@ -13,8 +14,7 @@ public sealed class PickAccessibleComponentOperator : HTNOperator
{
[Dependency] private readonly IComponentFactory _factory = default!;
[Dependency] private readonly IEntityManager _entManager = default!;
[Dependency] private readonly IRobustRandom _random = default!;
private PathfindingSystem _path = default!;
private PathfindingSystem _pathfinding = default!;
private EntityLookupSystem _lookup = default!;
[DataField("rangeKey", required: true)]
@@ -26,15 +26,22 @@ public sealed class PickAccessibleComponentOperator : HTNOperator
[ViewVariables, DataField("component", required: true)]
public string Component = string.Empty;
/// <summary>
/// Where the pathfinding result will be stored (if applicable). This gets removed after execution.
/// </summary>
[ViewVariables, DataField("pathfindKey")]
public string PathfindKey = "MovementPathfind";
public override void Initialize(IEntitySystemManager sysManager)
{
base.Initialize(sysManager);
_path = sysManager.GetEntitySystem<PathfindingSystem>();
_lookup = sysManager.GetEntitySystem<EntityLookupSystem>();
_pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
}
/// <inheritdoc/>
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard)
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
CancellationToken cancelToken)
{
// Check if the component exists
if (!_factory.TryGetRegistration(Component, out var registration))
@@ -56,6 +63,7 @@ public sealed class PickAccessibleComponentOperator : HTNOperator
// TODO: Need to get ones that are accessible.
// TODO: Look at unreal HTN to see repeatable ones maybe?
// TODO: Need type
foreach (var entity in _lookup.GetEntitiesInRange(coordinates, range))
{
if (entity == owner || !query.TryGetComponent(entity, out var comp))
@@ -69,27 +77,31 @@ public sealed class PickAccessibleComponentOperator : HTNOperator
return (false, null);
}
blackboard.TryGetValue<float>(RangeKey, out var maxRange);
if (maxRange == 0f)
maxRange = 7f;
while (targets.Count > 0)
{
// TODO: Get nearest at some stage
var target = _random.PickAndTake(targets);
var path = await _pathfinding.GetRandomPath(
owner,
1.4f,
maxRange,
cancelToken,
flags: _pathfinding.GetFlags(blackboard));
// TODO: God the path api sucks PLUS I need some fast way to get this.
var job = _path.RequestPath(owner, target.Owner, CancellationToken.None);
if (job == null)
continue;
await job.AsTask;
if (job.Result == null || !_entManager.TryGetComponent<TransformComponent>(target.Owner, out var targetXform))
if (path.Result != PathResult.Path)
{
continue;
return (false, null);
}
var target = path.Path.Last().Coordinates;
return (true, new Dictionary<string, object>()
{
{ TargetKey, targetXform.Coordinates },
{ TargetKey, target },
{ PathfindKey, path}
});
}