using System;
namespace RynthCore.Plugin.RynthAi;
///
/// One heading snap for a missile target the character has its back to at point blank.
///
/// Missile combat normally never turns the character: the server rotates to the target before
/// each shot, and a client turn servo on top of that spun archers in circles (2026-09-27, Lucy).
/// But with the target inside the server's rotate doesn't happen, so
/// the character stands facing away and fires nothing. Silentkelpie 2026-10-14/15: 7 of 13
/// crossbow stalls were at 1.7 m with the target 177 degrees behind; the stall watchdog's
/// re-issue never fixed one of them (only dropping the target did, at step 2 or later).
///
/// So, only when all of these hold, one instant heading snap (TurnToHeading, as for walls):
/// * the target is within ,
/// * it is more than off the character's facing,
/// * no hit, miss or kill for (a full crossbow cycle: a shot in
/// progress is never interrupted),
/// * no snap for this target within .
/// Combat then holds the attack for so the turn reaches the server first.
/// Pure: the caller passes the time and readings in.
///
internal sealed class PointBlankFacing
{
/// Targets this close (metres) count as point blank (the stall watchdog's distance).
public double MaxDistanceM { get; set; } = CombatStallWatchdog.PointBlankMissileM;
/// Facing error (degrees) above which the character is turned.
public double MinErrorDeg { get; set; } = 90;
/// No swing evidence for this long before a snap.
public double NoShotMs { get; set; } = 2500;
/// At most one snap per target this often.
public double RetryMs { get; set; } = 3000;
/// Attack held this long after a snap.
public double SettleMs { get; set; } = 300;
private int _target;
private DateTime _snapAt = DateTime.MinValue;
/// Snaps done (diagnostics).
public int Snaps { get; private set; }
/// True when the character should be turned to now.
public bool ShouldSnap(DateTime now, int targetId, double distanceM, double facingErrorDeg, DateTime lastEvidenceAt)
{
if (targetId == 0 || distanceM < 0 || distanceM > MaxDistanceM) return false;
if (facingErrorDeg <= MinErrorDeg) return false;
if (lastEvidenceAt != DateTime.MinValue && (now - lastEvidenceAt).TotalMilliseconds < NoShotMs) return false;
if (targetId == _target && _snapAt != DateTime.MinValue && (now - _snapAt).TotalMilliseconds < RetryMs) return false;
return true;
}
/// The snap for was sent.
public void Snapped(DateTime now, int targetId)
{
_target = targetId;
_snapAt = now;
Snaps++;
}
/// True while the attack should wait for the last snap to settle.
public bool Settling(DateTime now, int targetId)
=> targetId == _target && _snapAt != DateTime.MinValue && (now - _snapAt).TotalMilliseconds < SettleMs;
}