// KitAuto.cs - "Make kits when low" (Mini Remote > Crafting > Kits, /ra kits auto): with the macro // on, a kit run starts by itself when fewer than MakeKitsBelow Plentiful Healing Kits are carried // and nothing needs the character. Pure (System only), so RynthCore.RynthAiTests links it; the // plugin side (RynthAiPlugin.Kits.cs) reads the facts each tick, holds the bot while a run goes // (the way it holds for buffs) and KitRunner does the combines. // // A start needs every one of: the setting on, the macro on, fewer kits than the line, nothing else // going (buffing or healing, a fight, looting, salvaging, a portal, a vendor, following, ...), // vitals over their heal lines, no monster within the safe radius (35 yd, or Monster Range when // that is bigger), no hit for 20 s, and the pack making one kit at the Min chance % floor with // Healing trained. Then the bot is held still: peace mode is asked for (combat mode is the only // one left from a fight), and once in peace and standing still for a second the run starts. // // A run stops at once on a monster inside the radius, a hit, a heal line, buffing, the macro off, // or moving (KitRunner checks the last three itself). Waits after a run: // - a stop for danger (monster, damage, heal, moving, leaving peace): 60 s; // - a run that made nothing (every combine failed, the server's chance under the floor, a // refusal) or the player's Stop: 10 min; // - a run that made kits: the next look in 30 s (the count decides then). // Before a start, a pack that can't make a kit (out of an ingredient, under the floor, untrained) // is said once per reason, then looked at again every 30 s. // // "Make ammo when low" (MissileCraftingManager.Auto, RynthAiPlugin.Ammo.cs, 2026-11-23) runs on the // same rules with its own instance: its Name and Doing, and the facts' CountText ("230 arrows"), // BelowMax (ammo counts run to thousands) and IdleWhy ("no launcher in hand"). Their defaults are the // kits' own, so a kit look reads exactly as before. using System; namespace RynthCore.Plugin.RynthAi.MiniRemote.Crafting; /// Why a kit run ended (KitRunner sets it; the "when low" waits follow it). internal enum KitStopKind { /// Logged out, settings gone: no wait of its own. Other, /// Made the count asked for, or the pack ran out. Finished, /// A monster, a hit, a heal line, buffing, moving, leaving peace mode. Danger, /// The server's chance under the floor, a refusal, no answer, AC's dialog wanting a click. Refused, /// The player's Stop (the button, /ra kits stop). User, /// The macro was turned off during a "when low" run. MacroOff, } /// What stops a "when low" start (the first that holds, cheap ones first). internal enum KitBlock { None, Off, MacroOff, Enough, Busy, Vitals, Monster, Hurt, Pack } /// What the next tick does about "Make kits when low". internal enum KitAutoStep { /// Nothing: the bot goes on as usual. Wait, /// Hold the bot still (getting ready). Hold, /// Hold the bot and ask for peace mode. GoPeace, /// Start the run now (and hold the bot while it goes). Start, } /// One look at the character for "Make kits when low" (RynthAiPlugin.Kits.cs fills it). internal sealed class KitAutoFacts { public bool Enabled; public bool MacroOn; /// Plentiful Healing Kits carried (main pack and side packs). public int Kits; /// A run starts with fewer kits than this. public int Below = KitAuto.DefaultBelow; /// "" when the pack makes a kit at the floor; else the planner's why (out of X, under the floor, untrained). public string PackWhy = string.Empty; public bool InPeace = true; /// The character moved since the last tick. public bool Moving; /// The nearest live monster (PositiveInfinity = none known). public double NearestMonsterYd = double.PositiveInfinity; public double RadiusYd = KitAuto.DefaultRadiusYd; /// Since health last went down (MaxValue = not this session). public long SinceHurtMs = long.MaxValue; /// "" when health, stamina and mana are over their lines; else which one. public string VitalsWhy = string.Empty; /// "" when nothing else needs the character; else what does (buffing, a fight, looting, ...). public string BusyWhy = string.Empty; /// The most may be (kits: ). public int BelowMax = KitAuto.MaxBelow; /// "N kits" for the count in the status and chat (null = ). public Func? CountText; /// "" = look as usual; else nothing to do for now, and why (no launcher in hand, ...): no start, no chat. public string IdleWhy = string.Empty; } internal sealed class KitAuto { public const int DefaultBelow = 2; public const int DefaultUpTo = 5; public const int MaxBelow = 50; public const int MaxUpTo = 100; /// No monster this close (or within Monster Range, when that is bigger) for a start; one coming this close stops a run. public const double DefaultRadiusYd = 35; /// No start for this long after a hit. public const long HurtQuietMs = 20_000; /// The next look while something else has the character, or the kits are enough. public const long CheckMs = 5_000; /// The next look after the pack couldn't make a kit, and after a run that made some. public const long PackCheckMs = 30_000; /// The wait after a stop for danger (a monster, a hit, a heal line, moving). public const long CooldownMs = 60_000; /// The wait after a run that made nothing, or the player's Stop. public const long BackoffMs = 600_000; /// Getting ready (peace mode, standing still) gives up after this. public const long ReadyTimeoutMs = 6_000; /// Standing still this long in peace mode before the first combine. public const long SettleMs = 1_000; /// Peace mode is asked for again after this. public const long PeaceResendMs = 2_000; /// Clock (ms, monotonic); tests replace it. public Func NowMs = static () => Environment.TickCount64; /// A chat line for the player (a reason said once). public Action? Say; /// The feature's name in chat ("Make kits when low"). public string Name = "Make kits when low"; /// What a run does, for the status ("stopping to make kits"). public string Doing = "make kits"; private long _nextCheckAt; private string _waitWhy = string.Empty; private long _readySince = -1; private long _stillSince = -1; private long _peaceSentAt = long.MinValue / 2; private string _said = string.Empty; private volatile string _status = "off"; /// What it is doing, for the Kits tab and /ra kits auto status. public string Status => _status; /// Getting ready for a run (the bot is held). public bool Readying => _readySince >= 0; /// No look before this (0 = the next tick). public long NextCheckAt => _nextCheckAt; /// The reason last said in chat ("" = none since the last run that made kits). public string Said => _said; // ── Rules ─────────────────────────────────────────────────────────────── /// The first thing that stops a start now (KitBlock.None and "" when it can go, peace mode aside). public static (KitBlock Block, string Why) Blocker(KitAutoFacts f) { if (!f.Enabled) return (KitBlock.Off, "off"); if (!f.MacroOn) return (KitBlock.MacroOff, "the macro is off"); if (f.IdleWhy.Length > 0) return (KitBlock.Enough, f.IdleWhy); int below = Math.Clamp(f.Below, 1, Math.Max(1, f.BelowMax)); if (f.Kits >= below) return (KitBlock.Enough, $"{(f.CountText ?? KitsText)(f.Kits)} carried (makes more under {below})"); if (f.BusyWhy.Length > 0) return (KitBlock.Busy, f.BusyWhy); if (f.VitalsWhy.Length > 0) return (KitBlock.Vitals, f.VitalsWhy); if (f.NearestMonsterYd <= f.RadiusYd) return (KitBlock.Monster, $"monster {f.NearestMonsterYd:0} yd away"); if (f.SinceHurtMs < HurtQuietMs) return (KitBlock.Hurt, $"hit {f.SinceHurtMs / 1000} s ago"); if (f.PackWhy.Length > 0) return (KitBlock.Pack, f.PackWhy); return (KitBlock.None, string.Empty); } /// /// Why a "when low" run must stop now ("" = go on): buffing or a fight, a heal line, a monster /// within the radius. The macro off, moving, a hit and leaving peace mode KitRunner stops on itself. /// public static string StopReason(KitAutoFacts f) { if (f.BusyWhy.Length > 0) return f.BusyWhy; if (f.VitalsWhy.Length > 0) return f.VitalsWhy; if (f.NearestMonsterYd <= f.RadiusYd) return $"a monster came within {f.NearestMonsterYd:0} yd"; return string.Empty; } public static string KitsText(int n) => n == 1 ? "1 kit" : $"{n} kits"; // ── The tick ──────────────────────────────────────────────────────────── /// /// One tick with no run going. is read only when a look is due (it walks /// the pack and the world), every tick while getting ready. /// public KitAutoStep Decide(Func facts) { long now = NowMs(); if (_readySince < 0 && now < _nextCheckAt) { long left = (_nextCheckAt - now + 999) / 1000; _status = (_waitWhy.Length > 0 ? "waiting: " + _waitWhy + "; " : "") + $"next check in {left} s"; return KitAutoStep.Wait; } KitAutoFacts f = facts(); var (block, why) = Blocker(f); if (block != KitBlock.None) { bool wasReady = _readySince >= 0; EndReady(); switch (block) { case KitBlock.Off: _status = "off"; _nextCheckAt = 0; break; case KitBlock.MacroOff: _status = "waiting: the macro is off"; _nextCheckAt = 0; break; case KitBlock.Pack: // Said once per reason; not "can't read your skill" (the qualities arrive late after a login). if (_said != why && !why.StartsWith("can't read", StringComparison.Ordinal)) { _said = why; SayLine($"[RynthAi] {Name}: {(f.CountText ?? KitsText)(f.Kits)} left, but none can be made now: {why}."); } Wait(now, PackCheckMs, why); break; case KitBlock.Enough: Wait(now, CheckMs, why); break; default: // Something came up while getting ready: the danger wait, as after a stopped run. bool danger = block is KitBlock.Monster or KitBlock.Hurt or KitBlock.Vitals; Wait(now, wasReady && danger ? CooldownMs : CheckMs, why); break; } if (block is not (KitBlock.Off or KitBlock.MacroOff)) _status = "waiting: " + why; return KitAutoStep.Wait; } // Everything clear: hold the bot, go to peace mode, stand still, then start. if (_readySince < 0) { _readySince = now; _stillSince = -1; _peaceSentAt = long.MinValue / 2; } if (now - _readySince > ReadyTimeoutMs) { EndReady(); Wait(now, CooldownMs, f.InPeace ? "couldn't stand still" : "couldn't go to peace mode"); _status = "waiting: " + _waitWhy; return KitAutoStep.Wait; } if (!f.InPeace) { _stillSince = -1; _status = "going to peace mode"; if (now - _peaceSentAt >= PeaceResendMs) { _peaceSentAt = now; return KitAutoStep.GoPeace; } return KitAutoStep.Hold; } if (f.Moving) { _stillSince = -1; _status = "stopping to " + Doing; return KitAutoStep.Hold; } if (_stillSince < 0) _stillSince = now; if (now - _stillSince < SettleMs) { _status = "getting ready"; return KitAutoStep.Hold; } EndReady(); _status = "starting"; return KitAutoStep.Start; } /// The run went out (the Kits tab shows "running made/limit"). public void Running(int made, int limit) => _status = limit > 0 ? $"running {made}/{limit}" : $"running ({made} made)"; /// KitRunner refused the start after all (its own checks): look again later. public void StartRefused(string why) { Wait(NowMs(), PackCheckMs, why); _status = "waiting: " + why; } /// /// A "when low" run ended: the wait that follows it. Returns what the run's summary line adds /// ("" = nothing), so one line says how it went and when the next try is. /// public string RunEnded(KitStopKind kind, int made) { long now = NowMs(); EndReady(); switch (kind) { case KitStopKind.MacroOff: case KitStopKind.Other: _nextCheckAt = 0; _waitWhy = string.Empty; _status = kind == KitStopKind.MacroOff ? "waiting: the macro is off" : "idle"; if (made > 0) _said = string.Empty; return string.Empty; case KitStopKind.Danger: if (made > 0) _said = string.Empty; Wait(now, CooldownMs, "the last run was stopped"); return $" Next try in {CooldownMs / 1000} s at the earliest."; case KitStopKind.User: Wait(now, BackoffMs, "you stopped the last run"); return $" Next try in {BackoffMs / 60_000} min (turn {Name} off to stop it for good)."; default: if (made <= 0) { Wait(now, BackoffMs, "the last run made nothing"); return $" Next try in {BackoffMs / 60_000} min."; } _said = string.Empty; Wait(now, PackCheckMs, string.Empty); return string.Empty; } } /// The setting was turned on, or the player asked (/ra kits auto on): look on the next tick. public void LookNow() { EndReady(); _nextCheckAt = 0; _waitWhy = string.Empty; _said = string.Empty; } /// The setting is off (or the character logged out). public void Off() { EndReady(); _nextCheckAt = 0; _waitWhy = string.Empty; _status = "off"; } /// Logout: nothing carries over to the next character. public void Reset() { Off(); _said = string.Empty; } private void Wait(long now, long ms, string why) { _nextCheckAt = now + ms; _waitWhy = why; } private void EndReady() { _readySince = -1; _stillSince = -1; } private void SayLine(string line) { try { Say?.Invoke(line); } catch { } } }