namespace RynthCore.Plugin.RynthAi;
///
/// One Recall On Death attempt (RynthAiPlugin.DeathRecall.cs), as a pure clock-driven state machine
/// so tests can run it. The plugin calls every frame and does what it answers:
/// = cast the spell / use the item now. A teleport is portal space seen, or
/// the landblock changing; once one is seen nothing more is fired, and after
/// out of portal space the run is .
///
internal sealed class DeathRecallRun
{
public enum Step { Wait, Fire, Done, Failed }
public const long SpellRetryMs = 4_000; // a fizzle or interrupted cast: try again
public const long ItemRetryMs = 6_000; // a gem's use animation takes ~3 s before the portal
public const int MaxFires = 5;
public const long TimeoutMs = 60_000;
public const long SettleMs = 3_000; // out of portal space this long before nav/corpse travel
private readonly long _retryMs;
private readonly long _startedAt;
private readonly uint _startLandblock;
private long _lastFireAt;
private long _teleportedAt;
private bool _sawPortal;
public int Fires { get; private set; }
public string FailReason { get; private set; } = string.Empty;
public bool Teleported => _teleportedAt != 0 || _sawPortal;
/// True for a gem or other item (slower retry), false for a spell.
/// Clock in ms.
/// Landblock (cell >> 16) at the start; 0 when unknown.
public DeathRecallRun(bool useItem, long now, uint startLandblock)
{
_retryMs = useItem ? ItemRetryMs : SpellRetryMs;
_startedAt = now;
_startLandblock = startLandblock;
}
/// Clock in ms.
/// The client is in portal space.
/// Current landblock; 0 when unknown.
/// Ready to fire (a spell: wand wielded and in magic mode).
public Step Tick(long now, bool inPortalSpace, uint landblock, bool ready)
{
if (inPortalSpace) { _sawPortal = true; return Step.Wait; }
bool moved = landblock != 0 && _startLandblock != 0 && landblock != _startLandblock;
if (_sawPortal || moved)
{
if (_teleportedAt == 0) _teleportedAt = now;
return now - _teleportedAt >= SettleMs ? Step.Done : Step.Wait;
}
if (now - _startedAt > TimeoutMs)
return Fail($"no teleport within {TimeoutMs / 1000} s");
if (Fires >= MaxFires && now - _lastFireAt >= _retryMs)
return Fail($"no teleport after {MaxFires} tries");
if (!ready || Fires >= MaxFires)
return Step.Wait;
if (Fires > 0 && now - _lastFireAt < _retryMs)
return Step.Wait;
Fires++;
_lastFireAt = now;
return Step.Fire;
}
private Step Fail(string why)
{
FailReason = why;
return Step.Failed;
}
}