using System;
namespace RynthCore.Plugin.RynthAi;
///
/// Shared serializer for weapon/wand equip actions across BuffManager and
/// CombatManager (and any other subsystem that swaps the wielded item).
///
/// AC allows only ONE item action in flight at a time. Two UseObject equips
/// fired within ~1s — the buff↔combat wand/sword flap, or CombatManager's own
/// re-equip + EquipWeaponAndSetStance in the same tick — collide and trigger
/// the "you can only move or use one item at a time" notice, and in the worst
/// case an object-teardown access violation (DBOCache::DestroyObj). Each
/// subsystem had its OWN equip throttle (_lastEquipTime, _pendingWieldId) but
/// they didn't know about each other, so cross-subsystem swaps still raced.
///
/// This gate is a single shared instance both managers consult: a swap is only
/// allowed once the last one is done on the server (or MinIntervalMs has passed).
/// The loser yields and retries on a later tick. Spell casts and non-weapon item actions
/// (loot moves, corpse opens) do NOT go through here.
///
public sealed class WeaponSwapGate
{
private readonly object _lock = new();
private long _lastSwapTickMs = long.MinValue / 4;
private string _lastSwapBy = "";
private int _movingId;
private long _claims;
/// Swaps claimed so far: a caller can tell whether another swap went out after its own.
public long Claims { get { lock (_lock) { return _claims; } } }
///
/// The longest one swap holds the next: a swap the server's answer can't be seen for (or
/// with no ) waits this long, as before 2026-10-06.
///
public int MinIntervalMs { get; set; } = 3000;
///
/// Swap speed (2026-10-06): the next swap may go once the last one is done on the server,
/// after at least this long. A weapon change takes up to three steps (a shield or arrows
/// into the pack, the weapon, the off hand); at a flat 3 s per step it took 6-9 s.
///
public int FloorMs { get; set; } = 400;
/// True when no use or cast is waiting for the server's answer (ActionPacer).
public Func? ServerIdle { get; set; }
/// Reads whether the character wields an item (for a noted move, ).
public Func? IsWielded { get; set; }
public Func NowMs { get; set; } = () => Environment.TickCount64;
///
/// Atomically claim the swap slot. Returns true (recording the swap) when the last swap is
/// done on the server (see ) or MinIntervalMs has passed; false when
/// the caller must wait. Only mutates state when it returns true, so a denied caller never
/// "wastes" the slot.
///
public bool TryBeginSwap(string requestedBy)
{
long now = NowMs();
lock (_lock)
{
long since = now - _lastSwapTickMs;
if (since < MinIntervalMs && !LastSwapDone(since))
return false;
_lastSwapTickMs = now;
_lastSwapBy = requestedBy ?? "";
_movingId = 0;
_claims++;
return true;
}
}
///
/// The claimed swap moved into or out of a hand without a use (a
/// pack move or a slot wield): the next swap waits the full MinIntervalMs, as before.
/// Drakkon 2026-10-06 13:58: the wand went out 1.3 s after the axe's pack move (the axe read
/// out of hand) and the server then held the first cast open until a relog. The server's
/// unwield motion outlasts the item move, and nothing tells us when it ends.
///
public void NoteMove(int itemId, bool wieldedNow)
{
lock (_lock) _movingId = itemId;
}
private bool LastSwapDone(long since)
{
if (since < FloorMs || ServerIdle == null || IsWielded == null) return false;
try
{
return _movingId == 0 && ServerIdle();
}
catch
{
return false;
}
}
/// Milliseconds since the last successful swap (diagnostics).
public long MsSinceLastSwap
{
get { lock (_lock) { return NowMs() - _lastSwapTickMs; } }
}
/// Who last claimed a swap (diagnostics).
public string LastSwapBy { get { lock (_lock) { return _lastSwapBy; } } }
}