2022-09-30 14:39:48 +10:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2022-09-06 00:28:23 +10:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Content.Server.NPC.Pathfinding;
|
|
|
|
|
|
|
|
|
|
namespace Content.Server.NPC.HTN.PrimitiveTasks.Operators;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Chooses a nearby coordinate and puts it into the resulting key.
|
|
|
|
|
/// </summary>
|
2023-08-22 18:14:33 -07:00
|
|
|
public sealed partial class PickAccessibleOperator : HTNOperator
|
2022-09-06 00:28:23 +10:00
|
|
|
{
|
2022-11-19 08:07:52 +11:00
|
|
|
[Dependency] private readonly IEntityManager _entManager = default!;
|
2022-09-30 14:39:48 +10:00
|
|
|
private PathfindingSystem _pathfinding = default!;
|
2022-09-06 00:28:23 +10:00
|
|
|
|
|
|
|
|
[DataField("rangeKey", required: true)]
|
|
|
|
|
public string RangeKey = string.Empty;
|
|
|
|
|
|
2023-08-02 10:48:56 +10:00
|
|
|
[DataField("targetCoordinates")]
|
|
|
|
|
public string TargetCoordinates = "TargetCoordinates";
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Where the pathfinding result will be stored (if applicable). This gets removed after execution.
|
|
|
|
|
/// </summary>
|
2022-11-16 20:22:11 +01:00
|
|
|
[DataField("pathfindKey")]
|
2022-10-13 21:36:29 +11:00
|
|
|
public string PathfindKey = NPCBlackboard.PathfindKey;
|
2022-09-30 14:39:48 +10:00
|
|
|
|
2022-09-06 00:28:23 +10:00
|
|
|
public override void Initialize(IEntitySystemManager sysManager)
|
|
|
|
|
{
|
|
|
|
|
base.Initialize(sysManager);
|
2022-09-30 14:39:48 +10:00
|
|
|
_pathfinding = sysManager.GetEntitySystem<PathfindingSystem>();
|
2022-09-06 00:28:23 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-09-30 14:39:48 +10:00
|
|
|
public override async Task<(bool Valid, Dictionary<string, object>? Effects)> Plan(NPCBlackboard blackboard,
|
|
|
|
|
CancellationToken cancelToken)
|
2022-09-06 00:28:23 +10:00
|
|
|
{
|
|
|
|
|
// Very inefficient (should weight each region by its node count) but better than the old system
|
|
|
|
|
var owner = blackboard.GetValue<EntityUid>(NPCBlackboard.Owner);
|
|
|
|
|
|
2022-11-19 08:07:52 +11:00
|
|
|
blackboard.TryGetValue<float>(RangeKey, out var maxRange, _entManager);
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
if (maxRange == 0f)
|
|
|
|
|
maxRange = 7f;
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
var path = await _pathfinding.GetRandomPath(
|
|
|
|
|
owner,
|
|
|
|
|
maxRange,
|
|
|
|
|
cancelToken,
|
|
|
|
|
flags: _pathfinding.GetFlags(blackboard));
|
2022-09-06 00:28:23 +10:00
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
if (path.Result != PathResult.Path)
|
2022-09-06 00:28:23 +10:00
|
|
|
{
|
2022-09-30 14:39:48 +10:00
|
|
|
return (false, null);
|
2022-09-06 00:28:23 +10:00
|
|
|
}
|
|
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
var target = path.Path.Last().Coordinates;
|
2022-09-06 00:28:23 +10:00
|
|
|
|
|
|
|
|
return (true, new Dictionary<string, object>()
|
|
|
|
|
{
|
2023-08-02 10:48:56 +10:00
|
|
|
{ TargetCoordinates, target },
|
2022-09-30 14:39:48 +10:00
|
|
|
{ PathfindKey, path}
|
2022-09-06 00:28:23 +10:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|