using System; using System.Collections.Generic; using RynthCore.Plugin.RynthDefective.Items; using RynthCore.Plugin.RynthDefective.Model; namespace RynthCore.Plugin.RynthDefective.Use; /// /// What the controller needs from the game. The host implements it (HostDefWorld); the tests /// script it. Plugin pump thread only. /// internal interface IDefWorld { long NowMs { get; } /// A character is in the world and not in portal space. bool InWorld { get; } uint PlayerId { get; } uint SelectedId { get; } /// The client's busy count is above zero (an item action it sent has not been answered). bool ClientBusy { get; } /// A cast or use gesture is animating. bool Animating { get; } /// RynthAi is waiting on the server for an action it sent (null = RynthAi isn't there). bool? BotWaitingOnServer { get; } /// RynthAi's macro is running (null = RynthAi isn't there). bool? BotMacroRunning { get; } /// Count of the server's UseDone events; 0 when the engine can't report them. int UseDoneSeq { get; } /// 1 NonCombat, 2 Melee, 4 Missile, 8 Magic. int CombatMode { get; } /// The server's refusals so far (0x028A / 0x00A0 ... count), to tell a new one. int RefusalSeq { get; } /// The newest refusal named this object (a refused wield: InventoryServerSaveFailed). bool RefusalNamed(uint objectId); /// Everything carried and worn, as the client has it now. IReadOnlyList Items(); /// The item as the client has it this instant (wielded or not); null if it is gone. CarriedItem? Live(uint id); bool UseObject(uint id); bool UseObjectOn(uint id, uint target); bool SetCombatMode(int mode); } /// One thing the player asked to use: an exact item (a slot's) or a name typed in a command. internal sealed record UseRequest(ItemRef? Item, string? Name, UseMode Mode, string Source, long AtMs) { /// Same item, same way: pressing the key again must not queue a second one. public bool SameAs(UseRequest o) => Mode == o.Mode && ((Item != null && o.Item != null && Item.SameAs(o.Item)) || (Name != null && o.Name != null && string.Equals(Name, o.Name, StringComparison.OrdinalIgnoreCase))); public string Label => Item?.FullName ?? Name ?? "item"; } /// The combat stance a weapon swap leaves the character in. internal static class Stance { public const int NonCombat = 1, Melee = 2, Missile = 4, Magic = 8; /// /// The stance to ask for after was wielded, 0 = leave it alone. /// A character at peace stays at peace. One in a fighting stance goes to the new weapon's: /// Melee, Missile (a launcher needs its ammunition in hand: without it the client refuses the /// stance, so the character goes to peace rather than being left in a stance the weapon can't /// use), or Magic for a caster. Shields, armor and ammunition keep the stance they are in. /// public static int Wanted(ItemKind kind, bool launcherNeedsAmmo, bool ammoInHand, int modeBefore) { if (modeBefore == NonCombat || modeBefore == 0) return 0; switch (kind) { case ItemKind.Melee: return Melee; case ItemKind.WarWand: case ItemKind.VoidWand: return Magic; case ItemKind.Missile: return launcherNeedsAmmo && !ammoInHand ? NonCombat : Missile; default: return 0; } } public static string Name(int mode) => mode switch { NonCombat => "peace", Melee => "melee", Missile => "missile", Magic => "magic", _ => "mode " + mode, }; } /// /// One use at a time. A request (a slot pressed, /rdef use, the picker) is held until the /// character is free, then sent; the next waits for the server's answer. A second request while one /// is waiting replaces it (the newest wish wins) unless it is the same one (a key held down does /// not queue twice). Nothing is retried in a loop: a refused or unanswered action is reported once. /// /// What is sent (the host calls RynthAi's own one-touch heal and weapon swap use): /// kits UseObjectOn(kit, you) answered by the server's UseDone /// consumables UseObject(item) answered by UseDone /// weapons etc. UseObject(item): the client's double-click, which takes what is in the way off; /// answered when the item reads wielded (a wield sends no UseDone), then the stance /// is put right () once the character is still. /// Busy: nothing is sent while the client's busy count is up, a gesture animates, RynthAi waits on /// the server, the last action is unanswered, or for after it - the "one item /// action at a time" rule RynthAi's ActionPacer keeps. Weapon swaps also wait out a running RynthAi /// macro unless the player allowed them (it would swap straight back). /// internal sealed class UseController { /// How long a request waits for the character to be free before it gives up and says why. public const long WaitLimitMs = 6000; /// A use with no UseDone by now is let go (RynthAi waits 8 s for a kit's result). public const long UseTimeoutMs = 8000; /// A use the client no longer holds busy, with no UseDone, is let go after this. public const long UseQuietMs = 1500; public const long WieldTimeoutMs = 3500; public const long MinGapMs = 250; public const long StanceSettleMs = 500; public const long StanceTimeoutMs = 4000; private enum Stage { Sent, WaitWield, Stance } private sealed class Flight { public UseRequest Request = null!; public CarriedItem Item = null!; public UseAction Action; public ItemKind Kind; public long SentMs; public int UseDoneAtSend, RefusalAtSend, ModeBefore; public Stage Stage; public long StageMs; } private UseRequest? _pending; private Flight? _flight; private long _lastDoneMs = long.MinValue / 4; private string _lastWait = string.Empty; /// Plain information ("Using Peerless Healing Kit ..."): the status line, not the chat. public Action? Say; /// Something went wrong or didn't happen: the status line and the chat. public Action? Warn; public Action? Log; /// A weapon was wielded (the picker's recent list). public Action? Wielded; public bool HasPending => _pending != null; public bool InFlight => _flight != null; public UseRequest? Pending => _pending; /// The item a request in flight or waiting is about, for highlighting its slot. public bool Involves(ItemRef r) => (_pending?.Item != null && _pending.Item.SameAs(r)) || (_flight?.Request.Item != null && _flight.Request.Item.SameAs(r)); public DefSettings Settings = new(); /// Queues a request. False when it is the one already waiting or in flight (a held key). public bool Request(UseRequest r) { if (_pending != null && _pending.SameAs(r)) return false; if (_flight != null && _flight.Action == UseAction.Equip && _flight.Request.SameAs(r)) return false; // already wielding it _pending = r; _lastWait = string.Empty; return true; } /// Drops what is waiting and forgets what is in flight (logout, portal space, shutdown). public void Reset() { _pending = null; _flight = null; _lastWait = string.Empty; } public void Tick(IDefWorld w) { if (!w.InWorld) { if (_pending != null || _flight != null) Reset(); return; } long now = w.NowMs; if (_flight != null && !AdvanceFlight(w, now)) return; // still in flight: nothing new goes out if (_pending == null) return; UseRequest req = _pending; string? wait = WaitReason(w, now); if (wait != null) { _lastWait = wait; if (now - req.AtMs >= WaitLimitMs) { _pending = null; Warn?.Invoke($"{req.Label}: not used, {wait}."); } return; } _pending = null; Send(w, req, now); } private string? WaitReason(IDefWorld w, long now) { if (now - _lastDoneMs < MinGapMs) return "the last action just finished"; if (w.ClientBusy) return "the character is busy"; if (w.Animating) return "an animation is playing"; if (w.BotWaitingOnServer == true) return "RynthAi is waiting on the server"; return null; } // ── Sending ───────────────────────────────────────────────────────────── private void Send(IDefWorld w, UseRequest req, long now) { IReadOnlyList items = w.Items(); CarriedItem? item = req.Item != null ? ItemMatcher.Find(req.Item, items, out _) : ItemMatcher.FindByName(req.Name ?? string.Empty, items); if (item == null) { Warn?.Invoke(req.Item != null ? $"{req.Label}: you aren't carrying one." : $"No item named '{req.Name}' in your pack."); return; } ItemKind kind = ItemKinds.Classify(item); UseAction action = UseModes.ActionFor(req.Mode, kind); string name = item.FullName; if (action == UseAction.Equip) { if (item.Wielded) { Say?.Invoke($"{name} is already in your hands."); return; } if (ItemKinds.IsWeapon(kind) && !Settings.SwapWhileBotRuns && w.BotMacroRunning == true) { Warn?.Invoke($"{name}: not wielded, RynthAi's macro is running and picks the weapon itself (stop it, or allow swaps in the Defective settings)."); return; } } uint target = 0; if (action == UseAction.UseOnSelf) target = w.PlayerId; else if (action == UseAction.UseOnSelected) { target = w.SelectedId; if (target == 0) { Warn?.Invoke($"{name}: select a target first."); return; } } if ((action == UseAction.UseOnSelf || action == UseAction.UseOnSelected) && target == 0) { Warn?.Invoke($"{name}: the character isn't ready."); return; } bool sent = target != 0 ? w.UseObjectOn(item.Id, target) : w.UseObject(item.Id); if (!sent) { Warn?.Invoke($"{name}: the client didn't take the request (nothing was sent)."); return; } _flight = new Flight { Request = req, Item = item, Action = action, Kind = kind, SentMs = now, StageMs = now, UseDoneAtSend = w.UseDoneSeq, RefusalAtSend = w.RefusalSeq, ModeBefore = w.CombatMode, Stage = action == UseAction.Equip ? Stage.WaitWield : Stage.Sent, }; Log?.Invoke($"[RynthDefective] {req.Source}: {(action == UseAction.Equip ? "wield" : target != 0 ? "use on " + (target == w.PlayerId ? "you" : "0x" + target.ToString("X8")) : "use")} {item} (stack {item.Stack}, mode {Stance.Name(w.CombatMode)})"); Say?.Invoke(action == UseAction.Equip ? $"Wielding {name}..." : $"Using {name}..."); } // ── In flight ─────────────────────────────────────────────────────────── /// Steps the action in flight; true when it is finished (a new one may go out this tick). private bool AdvanceFlight(IDefWorld w, long now) { Flight f = _flight!; long age = now - f.SentMs; switch (f.Stage) { case Stage.Sent: { bool answered = w.UseDoneSeq != f.UseDoneAtSend; bool quiet = age >= UseQuietMs && !w.ClientBusy && !w.Animating; if (answered || quiet || age >= UseTimeoutMs) { if (!answered && age >= UseTimeoutMs) Log?.Invoke($"[RynthDefective] no UseDone for {f.Item} after {age} ms; carrying on"); Finish(now); return true; } return false; } case Stage.WaitWield: { CarriedItem? live = w.Live(f.Item.Id); if (live != null && live.Wielded) { Wielded?.Invoke(live); f.Item = live; f.Stage = Stage.Stance; f.StageMs = now; return AdvanceStance(w, f, now); } if (w.RefusalSeq != f.RefusalAtSend && w.RefusalNamed(f.Item.Id)) { Warn?.Invoke($"{f.Item.FullName}: the server wouldn't let you wield it (a skill, level or slot requirement?)."); Finish(now); return true; } if (age >= WieldTimeoutMs) { Warn?.Invoke($"{f.Item.FullName}: not in your hands after {age / 1000.0:0.0} s - something may be in the way, or the character was busy. Try again."); Finish(now); return true; } return false; } default: return AdvanceStance(w, f, now); } } private bool AdvanceStance(IDefWorld w, Flight f, long now) { bool launcher = f.Kind == ItemKind.Missile && ItemKinds.NeedsAmmo(f.Item); int want = Stance.Wanted(f.Kind, launcher, AmmoInHand(w), f.ModeBefore); if (want == 0 || w.CombatMode == want) { Say?.Invoke($"{f.Item.FullName} is in your hands."); Finish(now); return true; } long since = now - f.StageMs; if (since >= StanceTimeoutMs) { Warn?.Invoke($"{f.Item.FullName} is wielded but the {Stance.Name(want)} stance wasn't taken ({Stance.Name(w.CombatMode)} now)."); Finish(now); return true; } if (since < StanceSettleMs || w.ClientBusy || w.Animating) return false; if (w.SetCombatMode(want)) { Log?.Invoke($"[RynthDefective] stance {Stance.Name(w.CombatMode)} -> {Stance.Name(want)} for {f.Item}"); Say?.Invoke(want == Stance.NonCombat && f.ModeBefore != Stance.NonCombat ? $"{f.Item.FullName} is in your hands; no ammunition in hand, so you are back at peace." : $"{f.Item.FullName} is in your hands, {Stance.Name(want)} stance."); Finish(now); return true; } return false; // the engine turned the request away this tick (a teleport, a pending action): ask again } private static bool AmmoInHand(IDefWorld w) { foreach (CarriedItem it in w.Items()) if (it.Wielded && ItemKinds.Classify(it) == ItemKind.Ammo) return true; return false; } private void Finish(long now) { _flight = null; _lastDoneMs = now; } }