Skip to content

Commit

Permalink
Merge pull request #255 from Xian55/feature/npc-name-finder-fuzzy-search
Browse files Browse the repository at this point in the history
Feature: NpcNameFinder fuzzy search option - Processing with `Parallel.For`
  • Loading branch information
Xian55 authored Feb 12, 2022
2 parents 4a83342 + 2667e64 commit aa6eb68
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 25 deletions.
5 changes: 4 additions & 1 deletion BlazorServer/Pages/GoalsComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<div class="card-header">
Goals -> @addonReader.PlayerReader.MinRange - @addonReader.PlayerReader.MaxRange
| Expand <input type="checkbox" @bind="@Expand" />
<span class="float-right">Update: @addonReader.AvgUpdateLatency.ToString("0.00") ms</span>
<span class="float-right">
Cap: @botController.AvgScreenLatency.ToString("0.0")ms |
Npc: @botController.AvgNPCLatency.ToString("0.0")ms |
Bot: @addonReader.AvgUpdateLatency.ToString("0.0")ms</span>
</div>
@if (ShowGoals)
{
Expand Down
44 changes: 41 additions & 3 deletions Core/BotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using WinAPI;
using Microsoft.Extensions.Configuration;
using SharedLib.NpcFinder;
using Cyotek.Collections.Generic;

namespace Core
{
Expand All @@ -34,7 +35,7 @@ public sealed class BotController : IBotController, IDisposable

public Thread? screenshotThread { get; set; }

private const int screenshotTickMs = 150;
private const int screenshotTickMs = 200;
private DateTime lastScreenshot;

public Thread addonThread { get; set; }
Expand Down Expand Up @@ -74,6 +75,34 @@ public sealed class BotController : IBotController, IDisposable
private readonly AutoResetEvent addonAutoResetEvent = new(false);
private readonly AutoResetEvent npcNameFinderAutoResetEvent = new(false);

public double AvgScreenLatency
{
get
{
double avg = 0;
for (int i = 0; i < ScreenLatencys.Size; i++)
{
avg += ScreenLatencys.PeekAt(i);
}
return avg /= ScreenLatencys.Size;
}
}
private readonly CircularBuffer<double> ScreenLatencys;

public double AvgNPCLatency
{
get
{
double avg = 0;
for (int i = 0; i < NPCLatencys.Size; i++)
{
avg += NPCLatencys.PeekAt(i);
}
return avg /= NPCLatencys.Size;
}
}
private readonly CircularBuffer<double> NPCLatencys;

public BotController(ILogger logger, IPPather pather, DataConfig dataConfig, IConfiguration configuration)
{
this.logger = logger;
Expand Down Expand Up @@ -112,6 +141,9 @@ public BotController(ILogger logger, IPPather pather, DataConfig dataConfig, ICo
minimapNodeFinder = new MinimapNodeFinder(WowScreen, new PixelClassifier());
MinimapImageFinder = minimapNodeFinder as IImageProvider;

ScreenLatencys = new CircularBuffer<double>(5);
NPCLatencys = new CircularBuffer<double>(5);

addonThread = new Thread(AddonRefreshThread);
addonThread.Start();

Expand Down Expand Up @@ -157,14 +189,21 @@ public void AddonRefreshThread()
public void ScreenshotRefreshThread()
{
var nodeFound = false;
var stopWatch = new Stopwatch();
while (this.Enabled)
{
if ((DateTime.UtcNow - lastScreenshot).TotalMilliseconds > screenshotTickMs)
{
if (this.WowScreen.Enabled)
{
stopWatch.Restart();
this.WowScreen.UpdateScreenshot();
ScreenLatencys.Put(stopWatch.ElapsedMilliseconds);

stopWatch.Restart();
this.npcNameFinder.Update();
NPCLatencys.Put(stopWatch.ElapsedMilliseconds);

this.WowScreen.PostProcess();
}
else
Expand All @@ -189,11 +228,10 @@ public void ScreenshotRefreshThread()
MapId = this.AddonReader.UIMapId.Value,
Spot = this.AddonReader.PlayerReader.PlayerLocation
});
updatePlayerPostion.Reset();
updatePlayerPostion.Restart();
}

Thread.Sleep(10);
Thread.Sleep(5);
}
this.logger.LogInformation("Screenshot thread stoppped!");
}
Expand Down
3 changes: 3 additions & 0 deletions Core/ConfigBotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class ConfigBotController : IBotController
public event EventHandler? ProfileLoaded;
public event EventHandler<bool>? StatusChanged;

public double AvgScreenLatency { get => throw new NotImplementedException(); }
public double AvgNPCLatency { get => throw new NotImplementedException(); }

public void Shutdown()
{

Expand Down
3 changes: 3 additions & 0 deletions Core/IBotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public interface IBotController
event System.EventHandler? ProfileLoaded;
event System.EventHandler<bool> StatusChanged;

double AvgScreenLatency { get; }
double AvgNPCLatency { get; }

void ToggleBotStatus();
void StopBot();

Expand Down
4 changes: 2 additions & 2 deletions CoreTests/NpcNameFinder/MockWoWProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace CoreTests
{
public class MockWoWProcess : IMouseInput
{
public ValueTask RightClickMouse(Point position)
public void RightClickMouse(Point position)
{
throw new System.NotImplementedException();
}

public ValueTask LeftClickMouse(Point position)
public void LeftClickMouse(Point position)
{
throw new System.NotImplementedException();
}
Expand Down
7 changes: 5 additions & 2 deletions CoreTests/NpcNameFinder/Test_NpcNameFinderTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ public void Execute()
{
npcNameFinder.ChangeNpcType(NpcNames.Enemy | NpcNames.Neutral);

capturer.Capture();

System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();
capturer.Capture();
stopwatch.Stop();
logger.LogInformation($"Capture: {stopwatch.ElapsedMilliseconds}ms");

stopwatch.Restart();
this.npcNameFinder.Update();
stopwatch.Stop();
logger.LogInformation($"Update: {stopwatch.ElapsedMilliseconds}ms");
Expand Down
10 changes: 9 additions & 1 deletion CoreTests/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Serilog;
using Serilog.Extensions.Logging;
using System.Threading;
using System.Threading.Tasks;

namespace CoreTests
Expand All @@ -25,7 +26,14 @@ public static void Main()

//var test = new Test_NpcNameFinderTarget(logger);
var test = new Test_NpcNameFinderLoot(logger);
test.Execute();
int count = 1;
int i = 0;
while (i < count)
{
test.Execute();
i++;
Thread.Sleep(150);
}

//MainAsync().GetAwaiter().GetResult();
}
Expand Down
Loading

0 comments on commit aa6eb68

Please sign in to comment.