poe2-bot/src/Nexus.Bot/FlaskManager.cs
2026-03-06 14:37:05 -05:00

56 lines
1.7 KiB
C#

using System.Diagnostics;
using Nexus.Game;
using Nexus.Screen;
using Serilog;
namespace Nexus.Bot;
/// <summary>
/// Monitors life/mana and presses flask keys when they drop below thresholds.
/// Call <see cref="Tick"/> from any combat loop iteration.
/// </summary>
public class FlaskManager
{
private const float LifeThreshold = 0.50f;
private const float ManaThreshold = 0.50f;
private const long CooldownMs = 4000;
// VK codes for number keys 1 and 2
private const int VK_1 = 0x31;
private const int VK_2 = 0x32;
private readonly IGameController _game;
private readonly HudReader _hudReader;
private readonly Stopwatch _sw = Stopwatch.StartNew();
private long _lastLifeFlaskMs = -CooldownMs;
private long _lastManaFlaskMs = -CooldownMs;
public FlaskManager(IGameController game, HudReader hudReader)
{
_game = game;
_hudReader = hudReader;
}
/// <summary>
/// Check life/mana and press flask keys if needed. Call every loop iteration.
/// </summary>
public async Task Tick()
{
var hud = _hudReader.Current;
var now = _sw.ElapsedMilliseconds;
if (hud.LifePct < LifeThreshold && now - _lastLifeFlaskMs >= CooldownMs)
{
Log.Debug("Life flask: {Life:P0} < {Threshold:P0}", hud.LifePct, LifeThreshold);
await _game.PressKey(VK_1);
_lastLifeFlaskMs = now;
}
if (hud.ManaPct < ManaThreshold && now - _lastManaFlaskMs >= CooldownMs)
{
Log.Debug("Mana flask: {Mana:P0} < {Threshold:P0}", hud.ManaPct, ManaThreshold);
await _game.PressKey(VK_2);
_lastManaFlaskMs = now;
}
}
}