This commit is contained in:
Boki 2026-02-21 23:35:32 -05:00
parent 152c74fa15
commit 5e1b5c2a48
10 changed files with 211 additions and 150 deletions

View file

@ -44,6 +44,11 @@ public class BossRunExecutor : GameExecutor
public BossRunState State => _state;
/// <summary>
/// Current fight position in world coordinates, or null if not fighting.
/// </summary>
public (double X, double Y)? FightPosition { get; private set; }
private void SetState(BossRunState s)
{
_state = s;
@ -58,77 +63,84 @@ public class BossRunExecutor : GameExecutor
public async Task RunBossLoop()
{
_stopped = false;
ResetStop();
var runCount = _config.Kulemak.RunCount;
Log.Information("Starting boss run loop ({Count} runs)", runCount);
// First run: deposit inventory and grab 1 invitation
if (!await Prepare())
{
SetState(BossRunState.Failed);
await RecoverToHideout();
SetState(BossRunState.Idle);
return;
}
var completed = 0;
for (var i = 0; i < runCount; i++)
try
{
if (_stopped) break;
Log.Information("=== Boss run {N}/{Total} ===", i + 1, runCount);
if (!await TravelToZone())
// First run: deposit inventory and grab 1 invitation
if (!await Prepare())
{
Log.Error("Failed to travel to zone");
SetState(BossRunState.Failed);
await RecoverToHideout();
break;
SetState(BossRunState.Idle);
return;
}
if (_stopped) break;
var entrance = await WalkToEntrance();
if (entrance == null)
for (var i = 0; i < runCount; i++)
{
Log.Error("Failed to find Black Cathedral entrance");
await RecoverToHideout();
break;
if (_stopped) break;
Log.Information("=== Boss run {N}/{Total} ===", i + 1, runCount);
if (!await TravelToZone())
{
Log.Error("Failed to travel to zone");
await RecoverToHideout();
break;
}
if (_stopped) break;
var entrance = await WalkToEntrance();
if (entrance == null)
{
Log.Error("Failed to find Black Cathedral entrance");
await RecoverToHideout();
break;
}
if (_stopped) break;
if (!await UseInvitation(entrance.X, entrance.Y))
{
Log.Error("Failed to use invitation");
await RecoverToHideout();
break;
}
if (_stopped) break;
await Fight();
if (_stopped) break;
SetState(BossRunState.Looting);
await Sleep(1000); // wait for loot labels to render
await Loot();
if (_stopped) break;
if (!await ReturnHome())
{
Log.Error("Failed to return home");
await RecoverToHideout();
break;
}
if (_stopped) break;
bool isLastRun = i == runCount - 1 || _stopped;
await StoreLoot(grabInvitation: !isLastRun);
completed++;
if (_stopped) break;
}
if (_stopped) break;
if (!await UseInvitation(entrance.X, entrance.Y))
{
Log.Error("Failed to use invitation");
await RecoverToHideout();
break;
}
if (_stopped) break;
await Fight();
if (_stopped) break;
SetState(BossRunState.Looting);
await Helpers.Sleep(1000); // wait for loot labels to render
await Loot();
if (_stopped) break;
if (!await ReturnHome())
{
Log.Error("Failed to return home");
await RecoverToHideout();
break;
}
if (_stopped) break;
bool isLastRun = i == runCount - 1 || _stopped;
await StoreLoot(grabInvitation: !isLastRun);
completed++;
if (_stopped) break;
}
catch (OperationCanceledException) when (_stopped)
{
Log.Information("Boss run loop cancelled by user");
}
Log.Information("Boss run loop finished: {Completed}/{Total} runs completed", completed, runCount);
SetState(BossRunState.Complete);
await Helpers.Sleep(1000);
await Helpers.Sleep(1000); // non-cancellable final delay
SetState(BossRunState.Idle);
}
@ -138,7 +150,7 @@ public class BossRunExecutor : GameExecutor
Log.Information("Preparing: depositing inventory and grabbing invitations");
await _game.FocusGame();
await Helpers.Sleep(Delays.PostFocus);
await Sleep(Delays.PostFocus);
// Open stash
var stashPos = await _inventory.FindAndClickNameplate("Stash");
@ -147,7 +159,7 @@ public class BossRunExecutor : GameExecutor
Log.Error("Could not find Stash nameplate");
return false;
}
await Helpers.Sleep(Delays.PostStashOpen);
await Sleep(Delays.PostStashOpen);
// Click loot tab and deposit all inventory items
var (lootTab, lootFolder) = ResolveTabPath(_config.Kulemak.LootTabPath);
@ -166,11 +178,11 @@ public class BossRunExecutor : GameExecutor
{
var center = _screen.Grid.GetCellCenter(GridLayouts.Inventory, cell.Row, cell.Col);
await _game.LeftClickAt(center.X, center.Y);
await Helpers.Sleep(Delays.ClickInterval);
await Sleep(Delays.ClickInterval);
}
await _game.ReleaseCtrl();
await _game.KeyUp(InputSender.VK.SHIFT);
await Helpers.Sleep(Delays.PostEscape);
await Sleep(Delays.PostEscape);
}
}
else
@ -203,7 +215,7 @@ public class BossRunExecutor : GameExecutor
// Close stash
await _game.PressEscape();
_inventory.ResetStashTabState();
await Helpers.Sleep(Delays.PostEscape);
await Sleep(Delays.PostEscape);
Log.Information("Preparation complete");
return true;
@ -215,7 +227,7 @@ public class BossRunExecutor : GameExecutor
Log.Information("Traveling to Well of Souls via waypoint");
await _game.FocusGame();
await Helpers.Sleep(Delays.PostFocus);
await Sleep(Delays.PostFocus);
// Find and click Waypoint
var wpPos = await _inventory.FindAndClickNameplate("Waypoint");
@ -224,7 +236,7 @@ public class BossRunExecutor : GameExecutor
Log.Error("Could not find Waypoint nameplate");
return false;
}
await Helpers.Sleep(500);
await Sleep(500);
// Template match well-of-souls.png and click
var match = await _screen.TemplateMatch(WellOfSoulsTemplate);
@ -246,7 +258,7 @@ public class BossRunExecutor : GameExecutor
return false;
}
await Helpers.Sleep(Delays.PostTravel);
await Sleep(Delays.PostTravel);
Log.Information("Arrived at Well of Souls");
return true;
}
@ -266,9 +278,9 @@ public class BossRunExecutor : GameExecutor
// Hover first so the game registers the target, then use invitation
await _game.MoveMouseTo(x, y);
await Helpers.Sleep(200);
await Sleep(200);
await _game.CtrlLeftClickAt(x, y);
await Helpers.Sleep(500);
await Sleep(500);
// Find "NEW" button via template match — pick the leftmost
var matches = await _screen.TemplateMatchAll(NewInstanceTemplate);
@ -282,7 +294,7 @@ public class BossRunExecutor : GameExecutor
Log.Information("Found {Count} 'NEW' matches, clicking leftmost at ({X},{Y}) conf={Conf:F3}",
matches.Count, target.X, target.Y, target.Confidence);
await _game.MoveMouseTo(target.X, target.Y);
await Helpers.Sleep(150);
await Sleep(150);
await _game.LeftClickAt(target.X, target.Y);
// Wait for area transition into boss arena
@ -293,7 +305,7 @@ public class BossRunExecutor : GameExecutor
return false;
}
await Helpers.Sleep(Delays.PostTravel);
await Sleep(Delays.PostTravel);
Log.Information("Entered boss arena");
return true;
}
@ -317,7 +329,7 @@ public class BossRunExecutor : GameExecutor
Log.Information("Fight phase starting");
// Wait for arena to settle
await Helpers.Sleep(3000);
await Sleep(3000);
if (_stopped) return;
// Find and click the cathedral door
@ -332,7 +344,7 @@ public class BossRunExecutor : GameExecutor
await _game.LeftClickAt(door.X, door.Y);
// Wait for cathedral interior to load
await Helpers.Sleep(14000);
await Sleep(14000);
if (_stopped) return;
StartBossDetection();
@ -343,6 +355,7 @@ public class BossRunExecutor : GameExecutor
const double wellWorldX = -496;
const double wellWorldY = -378;
FightPosition = (fightWorldX, fightWorldY);
await WalkToWorldPosition(fightWorldX, fightWorldY, cancelWhen: IsBossAlive);
_nav.Frozen = true; // Lock canvas — position tracking only
if (_stopped) { _nav.Frozen = false; return; }
@ -367,18 +380,19 @@ public class BossRunExecutor : GameExecutor
{
fightWorldX = lastBossPos.Value.X;
fightWorldY = lastBossPos.Value.Y;
FightPosition = (fightWorldX, fightWorldY);
Log.Information("Fight area updated to ({X:F0},{Y:F0})", fightWorldX, fightWorldY);
}
// Wait for death animation before looking for well
await Helpers.Sleep(3000);
await Sleep(3000);
// Walk to well and click the closest match to screen center
Log.Information("Phase {Phase} done, walking to well", phase);
await WalkToWorldPosition(wellWorldX, wellWorldY);
await Helpers.Sleep(500);
await Sleep(500);
await ClickClosestTemplateToCenter(CathedralWellTemplate);
await Helpers.Sleep(200);
await Sleep(200);
// Walk back to fight position for next phase
await WalkToWorldPosition(fightWorldX, fightWorldY, cancelWhen: IsBossAlive);
@ -405,12 +419,13 @@ public class BossRunExecutor : GameExecutor
{
fightWorldX = finalBossPos.Value.X;
fightWorldY = finalBossPos.Value.Y;
FightPosition = (fightWorldX, fightWorldY);
}
Log.Information("Ring phase: using fightArea=({FX:F0},{FY:F0})", fightWorldX, fightWorldY);
// Walk to known ring position and look for the template
await WalkToWorldPosition(-440, -330);
await Helpers.Sleep(1000);
await Sleep(1000);
if (_stopped) return;
Log.Information("Looking for Return the Ring...");
@ -423,7 +438,7 @@ public class BossRunExecutor : GameExecutor
{
Log.Information("Found Return the Ring at ({X},{Y}), clicking", ring.X, ring.Y);
await _game.LeftClickAt(ring.X, ring.Y);
await Helpers.Sleep(500);
await Sleep(500);
}
else
{
@ -434,17 +449,14 @@ public class BossRunExecutor : GameExecutor
// Walk back to fight area — fightWorldX/Y carries position from all phases
Log.Information("Walking to fight position ({X:F0},{Y:F0})", fightWorldX, fightWorldY);
await WalkToWorldPosition(fightWorldX, fightWorldY);
await Helpers.Sleep(300);
await _game.PressKey(InputSender.VK.Q);
await Helpers.Sleep(300);
await _game.PressKey(InputSender.VK.E);
await Helpers.Sleep(300);
await Sleep(300);
Log.Information("Attacking at ring fight position");
await AttackBossUntilGone(fightWorldX, fightWorldY);
if (_stopped) return;
StopBossDetection();
_nav.Frozen = false;
FightPosition = null;
Log.Information("Fight complete");
}
@ -490,7 +502,7 @@ public class BossRunExecutor : GameExecutor
Log.Warning("Death detected! Clicking resurrect at ({X},{Y})", match.X, match.Y);
await _combat.ReleaseAll();
await _game.LeftClickAt(match.X, match.Y);
await Helpers.Sleep(3000); // wait for respawn + loading
await Sleep(3000); // wait for respawn + loading
return true;
}
@ -547,7 +559,7 @@ public class BossRunExecutor : GameExecutor
var consecutive = 0;
while (sw.ElapsedMilliseconds < timeoutMs && consecutive < minConsecutive)
{
await Helpers.Sleep(50);
await Sleep(50);
if (_nav.LastMatchSucceeded)
consecutive++;
else
@ -566,46 +578,31 @@ public class BossRunExecutor : GameExecutor
Log.Information("Waiting for boss healthbar to appear...");
var sw = Stopwatch.StartNew();
var (combatTask, cts) = StartCombatLoop();
try
while (sw.ElapsedMilliseconds < timeoutMs)
{
while (sw.ElapsedMilliseconds < timeoutMs)
if (_stopped) return false;
if (await CheckDeath()) continue;
if (await IsBossAlive())
{
if (_stopped) return false;
// Update target from YOLO (fast, no capture)
var snapshot = _bossDetector.Latest;
if (snapshot.Bosses.Count > 0)
{
_combatTargetX = snapshot.Bosses[0].Cx;
_combatTargetY = snapshot.Bosses[0].Cy;
}
// Check death + healthbar (blocks main thread, combat continues in background)
if (await CheckDeath()) continue;
if (await IsBossAlive())
{
Log.Information("Boss healthbar detected after {Ms}ms", sw.ElapsedMilliseconds);
return true;
}
Log.Information("Boss healthbar detected after {Ms}ms", sw.ElapsedMilliseconds);
return true;
}
// Final death check before giving up — force check ignoring throttle
_lastDeathCheckMs = 0;
if (await CheckDeath())
{
Log.Warning("Died while waiting for boss spawn");
return false;
}
await Sleep(50);
}
Log.Warning("WaitForBossSpawn timed out after {Ms}ms", timeoutMs);
// Final death check before giving up — force check ignoring throttle
_lastDeathCheckMs = 0;
if (await CheckDeath())
{
Log.Warning("Died while waiting for boss spawn");
return false;
}
finally
{
await StopCombatLoop(combatTask, cts);
}
Log.Warning("WaitForBossSpawn timed out after {Ms}ms", timeoutMs);
return false;
}
/// <summary>
@ -688,8 +685,9 @@ public class BossRunExecutor : GameExecutor
_lastDeathCheckMs = 0;
if (await CheckDeath()) { healthbarMissCount = 0; continue; }
Log.Information("Healthbar gone for {N} checks, boss phase over after {Ms}ms",
healthbarMissCount, sw.ElapsedMilliseconds);
Log.Information("Healthbar gone for {N} checks, boss phase over after {Ms}ms, lastBossPos={Boss}",
healthbarMissCount, sw.ElapsedMilliseconds,
lastBossWorldPos != null ? $"({lastBossWorldPos.Value.X:F1},{lastBossWorldPos.Value.Y:F1})" : "null");
return lastBossWorldPos;
}
}
@ -715,7 +713,7 @@ public class BossRunExecutor : GameExecutor
{
if (_stopped) return;
if (await CheckDeath()) continue;
await Helpers.Sleep(500);
await Sleep(500);
}
}
finally
@ -731,7 +729,7 @@ public class BossRunExecutor : GameExecutor
{
var sw = Stopwatch.StartNew();
// Attack for at least 2s before checking healthbar
await Helpers.Sleep(2000);
await Sleep(2000);
while (sw.ElapsedMilliseconds < timeoutMs)
{
if (_stopped) return;
@ -741,7 +739,7 @@ public class BossRunExecutor : GameExecutor
Log.Information("Boss dead, stopping attack after {Ms}ms", sw.ElapsedMilliseconds);
return;
}
await Helpers.Sleep(500);
await Sleep(500);
}
Log.Warning("AttackUntilBossDead timed out after {Ms}ms", timeoutMs);
}
@ -784,9 +782,9 @@ public class BossRunExecutor : GameExecutor
Log.Warning("No center match for {Template} (attempt {A}/3), nudging", Path.GetFileName(templatePath), attempt + 1);
var nudgeKey = attempt % 2 == 0 ? InputSender.VK.W : InputSender.VK.S;
await _game.KeyDown(nudgeKey);
await Helpers.Sleep(300);
await Sleep(300);
await _game.KeyUp(nudgeKey);
await Helpers.Sleep(500);
await Sleep(500);
}
Log.Warning("No matches found for {Template} after nudges, clicking screen center", Path.GetFileName(templatePath));
@ -836,15 +834,15 @@ public class BossRunExecutor : GameExecutor
var dirX = dx / len;
var dirY = dy / len;
// Blink toward destination every ~2.3s ± 0.3s
// Blink toward destination every ~2.3s ± 0.3s (only when far enough to avoid overshooting)
var blinkCooldown = 2300 + Rng.Next(-300, 301);
if (sw.ElapsedMilliseconds - lastBlinkMs >= blinkCooldown)
if (dist > 200 && sw.ElapsedMilliseconds - lastBlinkMs >= blinkCooldown)
{
// Move mouse in the travel direction so blink goes the right way
var blinkX = screenCx + (int)(dirX * 400);
var blinkY = screenCy + (int)(dirY * 400);
await _game.MoveMouseFast(blinkX, blinkY);
await Helpers.Sleep(30);
await Sleep(30);
await _game.PressKey(InputSender.VK.SPACE);
lastBlinkMs = sw.ElapsedMilliseconds;
}
@ -869,7 +867,7 @@ public class BossRunExecutor : GameExecutor
heldKeys.Add(key);
}
await Helpers.Sleep(100);
await Sleep(100);
}
if (sw.ElapsedMilliseconds >= timeoutMs)
@ -889,17 +887,17 @@ public class BossRunExecutor : GameExecutor
Log.Information("Returning home");
await _game.FocusGame();
await Helpers.Sleep(Delays.PostFocus);
await Sleep(Delays.PostFocus);
// Walk away from loot (hold S briefly)
await _game.KeyDown(InputSender.VK.S);
await Helpers.Sleep(500);
await Sleep(500);
await _game.KeyUp(InputSender.VK.S);
await Helpers.Sleep(200);
await Sleep(200);
// Press + to open portal, then click it at known position
await _game.PressPlus();
await Helpers.Sleep(2500);
await Sleep(2500);
await _game.LeftClickAt(1280, 450);
// Wait for area transition to caravan
@ -909,7 +907,7 @@ public class BossRunExecutor : GameExecutor
Log.Error("Timed out waiting for caravan transition");
return false;
}
await Helpers.Sleep(Delays.PostTravel);
await Sleep(Delays.PostTravel);
// /hideout to go home
var arrivedHome = await _inventory.WaitForAreaTransition(
@ -920,7 +918,7 @@ public class BossRunExecutor : GameExecutor
return false;
}
await Helpers.Sleep(Delays.PostTravel);
await Sleep(Delays.PostTravel);
_inventory.SetLocation(true);
Log.Information("Arrived at hideout");
return true;
@ -932,7 +930,7 @@ public class BossRunExecutor : GameExecutor
Log.Information("Storing loot");
await _game.FocusGame();
await Helpers.Sleep(Delays.PostFocus);
await Sleep(Delays.PostFocus);
// Identify items at Doryani before stashing
await _inventory.IdentifyItems();
@ -944,7 +942,7 @@ public class BossRunExecutor : GameExecutor
Log.Warning("Could not find Stash, skipping loot storage");
return;
}
await Helpers.Sleep(Delays.PostStashOpen);
await Sleep(Delays.PostStashOpen);
// Click loot tab and deposit all inventory items
var (lootTab, lootFolder) = ResolveTabPath(_config.Kulemak.LootTabPath);
@ -968,12 +966,12 @@ public class BossRunExecutor : GameExecutor
{
var center = _screen.Grid.GetCellCenter(GridLayouts.Inventory, item.Row, item.Col);
await _game.LeftClickAt(center.X, center.Y);
await Helpers.Sleep(Delays.ClickInterval);
await Sleep(Delays.ClickInterval);
}
await _game.ReleaseCtrl();
await _game.KeyUp(InputSender.VK.SHIFT);
await Helpers.Sleep(Delays.PostEscape);
await Sleep(Delays.PostEscape);
}
// Grab 1 invitation for the next run while stash is still open
@ -998,7 +996,7 @@ public class BossRunExecutor : GameExecutor
// Close stash
await _game.PressEscape();
_inventory.ResetStashTabState();
await Helpers.Sleep(Delays.PostEscape);
await Sleep(Delays.PostEscape);
Log.Information("Loot stored");
}