poe2-bot/src/Automata.Navigation/StuckDetector.cs
2026-02-28 15:13:31 -05:00

40 lines
981 B
C#

namespace Automata.Navigation;
/// <summary>
/// Detects when the player hasn't moved significantly over a window of frames.
/// </summary>
internal class StuckDetector
{
private readonly double _threshold;
private readonly int _frameCount;
private int _counter;
private MapPosition? _lastPosition;
public bool IsStuck => _counter >= _frameCount;
public StuckDetector(double threshold, int frameCount)
{
_threshold = threshold;
_frameCount = frameCount;
}
public void Update(MapPosition position)
{
if (_lastPosition != null)
{
var dx = position.X - _lastPosition.X;
var dy = position.Y - _lastPosition.Y;
if (Math.Sqrt(dx * dx + dy * dy) < _threshold)
_counter++;
else
_counter = 0;
}
_lastPosition = position;
}
public void Reset()
{
_counter = 0;
_lastPosition = null;
}
}