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; private string _heldBy = ""; private long _heldUntilMs; /// 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) { if (_heldBy.Length > 0 && now < _heldUntilMs) return false; // a multi-step swap is running (Hold) long since = now - _lastSwapTickMs; if (since < MinIntervalMs && !LastSwapDone(since)) return false; _lastSwapTickMs = now; _lastSwapBy = requestedBy ?? ""; _movingId = 0; _claims++; return true; } } /// /// A multi-step swap (HandSwapSequence, 2026-10-22) that has claimed the gate keeps it for its /// steps: no other swap goes until , or as a backstop. /// Its own steps are paced on the server's answers, not by this gate. /// public void Hold(string by, long maxMs) { lock (_lock) { _heldBy = by ?? "?"; _heldUntilMs = NowMs() + maxMs; } } /// /// The held swap is over. (its last pack move or slot wield, 0 = /// none) holds the next swap the full from now, as a noted move does. /// public void Release(string by, int lastMovedId) { lock (_lock) { if (_heldBy != (by ?? "?")) return; _heldBy = ""; _lastSwapTickMs = NowMs(); _movingId = lastMovedId; } } /// Who holds the gate for a multi-step swap ("" = no one; diagnostics). public string HeldBy { get { lock (_lock) { return _heldBy.Length > 0 && NowMs() < _heldUntilMs ? _heldBy : ""; } } } /// /// 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; } /// /// The caller saw the noted move of finish and settle: the next swap /// is paced on the server's answers () instead of the full MinIntervalMs. /// Only for moves with no unwield motion behind them (ammo out of the ammunition slot); a /// weapon or shield pack move keeps the full wait (see ). /// public void MoveDone(int itemId) { lock (_lock) { if (_movingId == itemId) _movingId = 0; } } private bool LastSwapDone(long since) { if (since < FloorMs || ServerIdle == null || IsWielded == null) return false; try { return _movingId == 0 && ServerIdle(); } catch { return false; } } /// /// True while may still be on its way into or out of a hand: a /// multi-step swap holds the gate, or the last claimed swap noted a move of this item and the /// next swap would still wait for it (MinIntervalMs). Buffing doesn't aim a spell at a shield /// then (BuffManager.Shield.cs). /// public bool MayBeMoving(int itemId) { long now = NowMs(); lock (_lock) { if (_heldBy.Length > 0 && now < _heldUntilMs) return true; return itemId != 0 && _movingId == itemId && now - _lastSwapTickMs < MinIntervalMs; } } /// 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; } } } }