work on navigation

This commit is contained in:
Boki 2026-02-13 11:23:30 -05:00
parent 468e0a7246
commit 802f1030d5
12 changed files with 582 additions and 64 deletions

View file

@ -67,6 +67,7 @@ public partial class App : Application
desktop.ShutdownRequested += async (_, _) =>
{
mainVm.Shutdown();
await bot.DisposeAsync();
};
}

View file

@ -160,6 +160,21 @@ public partial class DebugViewModel : ObservableObject
}
}
[RelayCommand]
private void SaveMinimapDebug()
{
try
{
_bot.Navigation.SaveDebugCapture();
DebugResult = $"Minimap debug images saved to {Path.GetFullPath("debug-minimap")}";
}
catch (Exception ex)
{
DebugResult = $"Failed: {ex.Message}";
Log.Error(ex, "Minimap debug save failed");
}
}
[RelayCommand]
private async Task ClickAnge()
{

View file

@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using System.IO;
using System.Runtime.InteropServices;
using Avalonia.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
@ -29,6 +30,11 @@ public partial class CellState : ObservableObject
public partial class MainWindowViewModel : ObservableObject
{
private readonly BotOrchestrator _bot;
private readonly CancellationTokenSource _cts = new();
[DllImport("user32.dll")]
private static extern short GetAsyncKeyState(int vKey);
private const int VK_F12 = 0x7B;
[ObservableProperty]
private string _state = "Idle";
@ -45,7 +51,6 @@ public partial class MainWindowViewModel : ObservableObject
[ObservableProperty] private Bitmap? _inventoryImage;
[ObservableProperty] private Bitmap? _minimapImage;
[ObservableProperty] private string _navigationStateText = "";
private long _lastMinimapUpdate;
[ObservableProperty] private string _newUrl = "";
[ObservableProperty] private string _newLinkName = "";
@ -80,7 +85,6 @@ public partial class MainWindowViewModel : ObservableObject
ActiveLinksCount = status.Links.Count(l => l.Active);
OnPropertyChanged(nameof(Links));
UpdateInventoryGrid();
UpdateMinimapImage();
});
};
@ -97,6 +101,9 @@ public partial class MainWindowViewModel : ObservableObject
if (Logs.Count > 500) Logs.RemoveAt(0);
});
};
// Background loop: minimap capture + F12 hotkey polling
_ = RunBackgroundLoop(_cts.Token);
}
public string PauseButtonText => IsPaused ? "Resume" : "Pause";
@ -167,6 +174,57 @@ public partial class MainWindowViewModel : ObservableObject
if (link != null) _bot.ToggleLink(id, !link.Active);
}
private async Task RunBackgroundLoop(CancellationToken ct)
{
var f12WasDown = false;
while (!ct.IsCancellationRequested)
{
try
{
// F12 hotkey — edge-detect (trigger once per press)
var f12Down = (GetAsyncKeyState(VK_F12) & 0x8000) != 0;
if (f12Down && !f12WasDown)
{
Log.Information("F12 pressed — emergency stop");
await _bot.Navigation.Stop();
_bot.Pause();
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
IsPaused = true;
State = "Stopped (F12)";
});
}
f12WasDown = f12Down;
// Minimap capture + display
var bytes = _bot.Navigation.ProcessFrame();
if (bytes != null)
{
var bmp = new Bitmap(new MemoryStream(bytes));
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
var old = MinimapImage;
MinimapImage = bmp;
NavigationStateText = _bot.Navigation.State == NavigationState.Idle
? "" : _bot.Navigation.State.ToString();
old?.Dispose();
});
}
await Task.Delay(100, ct);
}
catch (OperationCanceledException) { break; }
catch (Exception ex)
{
Log.Debug(ex, "Background loop error");
await Task.Delay(500, ct);
}
}
}
public void Shutdown() => _cts.Cancel();
private void UpdateInventoryGrid()
{
if (!_bot.IsReady) return;
@ -213,39 +271,4 @@ public partial class MainWindowViewModel : ObservableObject
OnPropertyChanged(nameof(InventoryFreeCells));
}
private void UpdateMinimapImage()
{
var nav = _bot.Navigation;
var navState = nav.State;
NavigationStateText = navState == NavigationState.Idle ? "" : navState.ToString();
if (navState == NavigationState.Idle)
{
if (MinimapImage != null)
{
var old = MinimapImage;
MinimapImage = null;
old.Dispose();
}
return;
}
// Throttle: update at most once per second
var now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (now - _lastMinimapUpdate < 1000) return;
_lastMinimapUpdate = now;
try
{
var bytes = nav.GetViewportSnapshot();
var old = MinimapImage;
MinimapImage = new Bitmap(new MemoryStream(bytes));
old?.Dispose();
}
catch (Exception ex)
{
Log.Debug(ex, "Failed to update minimap image");
}
}
}

View file

@ -115,7 +115,7 @@
<TextBlock Text="MINIMAP" FontSize="11" FontWeight="SemiBold"
Foreground="#8b949e" />
<TextBlock Text="{Binding NavigationStateText}"
FontSize="11" Foreground="#8b949e" Margin="8,0,0,0" />
FontSize="11" Foreground="#58a6ff" Margin="8,0,0,0" />
</StackPanel>
<Grid>
<Image Source="{Binding MinimapImage}" Stretch="Uniform"
@ -239,6 +239,7 @@
<Button Content="Screenshot" Command="{Binding TakeScreenshotCommand}" />
<Button Content="OCR Screen" Command="{Binding RunOcrCommand}" />
<Button Content="Go Hideout" Command="{Binding GoHideoutCommand}" />
<Button Content="Minimap Debug" Command="{Binding SaveMinimapDebugCommand}" />
</StackPanel>
<StackPanel Orientation="Horizontal" Spacing="8">
<Button Content="ANGE" Command="{Binding ClickAngeCommand}" />