using RynthCore.Plugin.RynthAi.LegacyUi;
namespace RynthCore.Plugin.RynthAi;
// Item chores and fights (structural audit 2026-10-21, finding 3): missile crafting held combat with
// no danger check, and mana tapping and AutoCram/AutoStack acted between swings (their only gate was
// the busy count, and a swing repeat is not a paced action). They now all wait on one signal,
// FightIsOn, the one salvage already used.
public sealed partial class RynthAiPlugin
{
///
/// A fight is on: a monster within Monster Range or on top of us, or the target combat is locked
/// on (CombatManager.HasEngageableTarget, the arbiter's Combat input, and IsUnderCloseAttack).
/// Optional item work waits for it to end: salvage, AutoCram/AutoStack, draining loot into mana
/// stones, reading scrolls, and missile crafting's optional crafts (MissileCraftingManager).
/// Only with the macro on: the target scan stops with the macro, and its last picture must not
/// hold a /ub autocram or a salvage the player asks for.
///
internal bool FightIsOn(LegacyUiSettings? s = null)
{
s ??= _dashboard?.Settings;
if (s == null || !s.IsMacroRunning || !s.EnableCombat) return false;
var combat = _combatManager;
return combat != null && (combat.HasEngageableTarget || combat.IsUnderCloseAttack);
}
///
/// Idle for Rebuff When Idle's early top-off (BuffManager.IdleForTopOff): the macro on, no fight
/// (the target combat is locked on, a monster within Monster Range or on top of us; read with
/// Enable Combat off too, so a fight by hand counts), no corpse to loot (the arbiter's looting
/// input, last tick) and no salvage. Navigating counts as idle: buffing pauses the route as always.
///
internal bool IdleForBuffTopOff()
{
var s = _dashboard?.Settings;
if (s == null || !s.IsMacroRunning) return false;
var combat = _combatManager;
if (combat == null || combat.HasEngageableTarget || combat.IsUnderCloseAttack) return false;
if (_lastWantLoot || _openedContainerId != 0 || _targetCorpseId != 0) return false;
return _salvageManager?.IsBusy != true;
}
///
/// AutoCram / AutoStack, one tick: not while a corpse is open or claimed, and not in a fight (a
/// pack move went out between melee swings). The manager itself waits for the busy count.
///
private void TickCramStack(LegacyUiSettings settings)
{
if (_inventoryManager == null || _openedContainerId != 0 || _targetCorpseId != 0) return;
if (FightIsOn(settings)) return;
Guard(TickStep.CramStack, () => _inventoryManager?.OnHeartbeat(PacedBusy));
}
}