using System; using RynthCore.Plugin.RynthAi.LegacyUi; using RynthCore.PluginSdk; namespace RynthCore.Plugin.RynthAi; /// /// One bot per client under the Decal bridge: when VTank's macro starts, RynthAi's stops, and /// it isn't started again while VTank runs. VTank stopping only allows it again; nothing is /// restarted. The engine says whether VTank's macro runs (RynthCoreHost.TryGetVTankState, API /// v74: VTank's documented /vt start and /vt stop through Decal's chat parser). Without Decal /// the engine isn't watching and nothing here ever stops or refuses anything. /// /// Pure decisions only (the plugin does the stopping and the chat), so the host tests drive it /// with a fake VTank signal. /// internal sealed class VTankYield { public const string StoppedForStartLine = "[RynthAi] VTank started - RynthAi macro stopped (one bot per client)."; public const string StoppedWhileRunningLine = "[RynthAi] VTank is running - RynthAi macro stopped (one bot per client)."; public const string RefusedLine = "[RynthAi] VTank is running - RynthAi macro not started (one bot per client). Stop VTank first (/vt stop), or turn off Yield to VTank (/ra vtankyield off)."; /// Refusals the tick itself makes (a start that bypassed the checked paths) print at most this often. public const long RefuseRepeatMs = 10_000; public enum Verdict { /// Nothing to do. None, /// VTank just started and RynthAi's macro is on: stop it, print . StopForVTankStart, /// VTank was already running and RynthAi's macro was running too (e.g. yield was just turned /// back on): stop it, print . StopWhileVTankRuns, /// Something switched RynthAi's macro on while VTank runs: switch it back off before it acts, /// print (throttled, see ). RefuseStart, } private bool _vtankWasRunning; private long _lastRefusalMs = long.MinValue / 2; /// The engine watches VTank (Decal bridge mode), as of the last . public bool Watching { get; private set; } /// VTank's macro runs, as of the last (false whenever not watching). public bool VTankRunning { get; private set; } /// Asks the engine (API v74). An older engine, or no Decal bridge: not watching. public void Poll(RynthCoreHost host) { if (host.TryGetVTankState(out bool watching, out bool running, out _)) Observe(watching, running); else Observe(false, false); } /// /// The per-tick step the plugin runs before the macro acts: decides, switches the macro flag /// off when it must ( does the rest of a /ra stop: nav stop, pause /// cancel), and prints the line. Returns what it decided. /// public Verdict Apply(LegacyUiSettings settings, bool macroWasRunning, Action? onStop, Action chat, long nowMs) { Verdict v = Tick(settings.IsMacroRunning, macroWasRunning, settings.YieldToVTank); if (v == Verdict.None) return v; settings.SetMacroRunning(false, "VTank yield (VTank's macro runs: one bot per client)"); onStop?.Invoke(); if (v == Verdict.StopForVTankStart) chat(StoppedForStartLine); else if (v == Verdict.StopWhileVTankRuns) chat(StoppedWhileRunningLine); else if (ShouldPrintRefusal(nowMs)) chat(RefusedLine); return v; } /// An explicit start: true = refused (the line is printed), the caller must not start. public bool RefuseStart(LegacyUiSettings settings, Action chat) { if (!BlocksStart(settings.YieldToVTank)) return false; chat(RefusedLine); return true; } /// Takes the engine's answer (call before deciding; cheap, any number of times). public void Observe(bool watching, bool vtankRunning) { Watching = watching; VTankRunning = watching && vtankRunning; } /// /// Once per plugin tick, BEFORE the macro acts. = the macro flag now; /// = it was on at the end of the previous tick. /// public Verdict Tick(bool macroOn, bool macroWasRunning, bool yieldEnabled) { bool started = VTankRunning && !_vtankWasRunning; _vtankWasRunning = VTankRunning; if (!yieldEnabled || !VTankRunning || !macroOn) return Verdict.None; if (started) return Verdict.StopForVTankStart; return macroWasRunning ? Verdict.StopWhileVTankRuns : Verdict.RefuseStart; } /// An explicit start (button, /ra start, a meta, the phone, a script) must be refused. public bool BlocksStart(bool yieldEnabled) => yieldEnabled && VTankRunning; /// Throttle for lines from the tick (explicit starts always print). public bool ShouldPrintRefusal(long nowMs) { if (nowMs - _lastRefusalMs < RefuseRepeatMs) return false; _lastRefusalMs = nowMs; return true; } public string Describe(bool yieldEnabled, bool engineHasIt) { if (!engineHasIt) return "[RynthAi] Yield to VTank: " + (yieldEnabled ? "on" : "off") + " - this engine can't tell whether VTank runs (needs API v74), so it does nothing."; if (!Watching) return "[RynthAi] Yield to VTank: " + (yieldEnabled ? "on" : "off") + " - no Decal bridge in this client, so it does nothing."; return "[RynthAi] Yield to VTank: " + (yieldEnabled ? "on" : "off") + ". VTank macro: " + (VTankRunning ? "RUNNING" : "not running") + " (seen through /vt start and /vt stop; VTank's own Run Macro button isn't seen - /rc vtank clear if it was stopped that way)."; } }