2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2022-08-03 16:02:50 +10:00
|
|
|
using System.Threading;
|
2022-09-30 14:39:48 +10:00
|
|
|
using Content.Server.NPC.Pathfinding;
|
2023-04-21 15:05:29 +10:00
|
|
|
using Content.Shared.DoAfter;
|
2022-12-12 14:33:43 +11:00
|
|
|
using Content.Shared.NPC;
|
2022-08-03 16:02:50 +10:00
|
|
|
using Robust.Shared.Map;
|
2023-03-05 16:13:09 +11:00
|
|
|
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
|
2022-08-03 16:02:50 +10:00
|
|
|
|
2022-09-06 00:28:23 +10:00
|
|
|
namespace Content.Server.NPC.Components;
|
2022-08-03 16:02:50 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Added to NPCs that are moving.
|
|
|
|
|
/// </summary>
|
2024-02-26 04:36:19 +01:00
|
|
|
[RegisterComponent, AutoGenerateComponentPause]
|
2023-08-22 18:14:33 -07:00
|
|
|
public sealed partial class NPCSteeringComponent : Component
|
2022-08-03 16:02:50 +10:00
|
|
|
{
|
2022-12-12 14:33:43 +11:00
|
|
|
#region Context Steering
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used to override seeking behavior for context steering.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public bool CanSeek = true;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Radius for collision avoidance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)]
|
|
|
|
|
public float Radius = 0.35f;
|
|
|
|
|
|
2024-02-28 17:41:15 +11:00
|
|
|
[ViewVariables, DataField]
|
|
|
|
|
public float[] Interest = new float[SharedNPCSteeringSystem.InterestDirections];
|
2022-12-12 14:33:43 +11:00
|
|
|
|
2024-02-28 17:41:15 +11:00
|
|
|
[ViewVariables, DataField]
|
|
|
|
|
public float[] Danger = new float[SharedNPCSteeringSystem.InterestDirections];
|
2022-12-12 14:33:43 +11:00
|
|
|
|
|
|
|
|
// TODO: Update radius, also danger points debug only
|
|
|
|
|
public readonly List<Vector2> DangerPoints = new();
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-02-15 16:54:06 +11:00
|
|
|
/// <summary>
|
2023-08-03 07:34:54 +10:00
|
|
|
/// Set to true from other systems if you wish to force the NPC to move closer.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataField("forceMove")]
|
|
|
|
|
public bool ForceMove = false;
|
|
|
|
|
|
2023-03-05 16:13:09 +11:00
|
|
|
[DataField("lastSteerDirection")]
|
2023-02-15 16:54:06 +11:00
|
|
|
public Vector2 LastSteerDirection = Vector2.Zero;
|
|
|
|
|
|
2023-03-05 16:13:09 +11:00
|
|
|
/// <summary>
|
|
|
|
|
/// Last position we considered for being stuck.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataField("lastStuckCoordinates")]
|
|
|
|
|
public EntityCoordinates LastStuckCoordinates;
|
|
|
|
|
|
|
|
|
|
[DataField("lastStuckTime", customTypeSerializer:typeof(TimeOffsetSerializer))]
|
2024-02-26 04:36:19 +01:00
|
|
|
[AutoPausedField]
|
2023-03-05 16:13:09 +11:00
|
|
|
public TimeSpan LastStuckTime;
|
|
|
|
|
|
2023-03-23 23:53:17 +11:00
|
|
|
public const float StuckDistance = 1f;
|
2023-03-05 16:13:09 +11:00
|
|
|
|
2022-09-30 14:39:48 +10:00
|
|
|
/// <summary>
|
|
|
|
|
/// Have we currently requested a path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables]
|
|
|
|
|
public bool Pathfind => PathfindToken != null;
|
2023-08-02 10:48:56 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Are we considered arrived if we have line of sight of the target.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataField("arriveOnLineOfSight")]
|
|
|
|
|
public bool ArriveOnLineOfSight = false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How long the target has been in line of sight if applicable.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataField("lineOfSightTimer")]
|
|
|
|
|
public float LineOfSightTimer = 0f;
|
|
|
|
|
|
|
|
|
|
[DataField("lineOfSightTimeRequired")]
|
|
|
|
|
public float LineOfSightTimeRequired = 0.5f;
|
|
|
|
|
|
2022-08-03 16:02:50 +10:00
|
|
|
[ViewVariables] public CancellationTokenSource? PathfindToken = null;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Current path we're following to our coordinates.
|
|
|
|
|
/// </summary>
|
2022-09-30 14:39:48 +10:00
|
|
|
[ViewVariables] public Queue<PathPoly> CurrentPath = new();
|
2022-08-03 16:02:50 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
2022-09-06 00:28:23 +10:00
|
|
|
/// End target that we're trying to move to.
|
2022-08-03 16:02:50 +10:00
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public EntityCoordinates Coordinates;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How close are we trying to get to the coordinates before being considered in range.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public float Range = 0.2f;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How far does the last node in the path need to be before considering re-pathfinding.
|
|
|
|
|
/// </summary>
|
2022-12-07 10:33:44 +11:00
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public float RepathRange = 1.5f;
|
2022-09-30 14:39:48 +10:00
|
|
|
|
|
|
|
|
public const int FailedPathLimit = 3;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How many times we've failed to pathfind. Once this hits the limit we'll stop steering.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[ViewVariables] public int FailedPathCount;
|
2022-08-03 16:02:50 +10:00
|
|
|
|
|
|
|
|
[ViewVariables] public SteeringStatus Status = SteeringStatus.Moving;
|
2022-09-30 14:39:48 +10:00
|
|
|
|
|
|
|
|
[ViewVariables(VVAccess.ReadWrite)] public PathFlags Flags = PathFlags.None;
|
2023-04-21 15:05:29 +10:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If the NPC is using a do_after to clear an obstacle.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[DataField("doAfterId")]
|
|
|
|
|
public DoAfterId? DoAfterId = null;
|
2022-08-03 16:02:50 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum SteeringStatus : byte
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If we can't reach the target (e.g. different map).
|
|
|
|
|
/// </summary>
|
|
|
|
|
NoPath,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Are we moving towards our target
|
|
|
|
|
/// </summary>
|
|
|
|
|
Moving,
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Are we currently in range of our target.
|
|
|
|
|
/// </summary>
|
|
|
|
|
InRange,
|
|
|
|
|
}
|