Skip to content

Commit

Permalink
Merge pull request #585 from Xian55/feature/cata-navigation-prep
Browse files Browse the repository at this point in the history
Core: RemoteV3 new branch of AmeisenNavigation `multi-version-guess-z-coord`
  • Loading branch information
Xian55 authored May 11, 2024
2 parents bf7e72e + 47e02cb commit 9462057
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 60 deletions.
50 changes: 35 additions & 15 deletions Core/BotController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public sealed partial class BotController : IBotController, IDisposable
private readonly IServiceProvider serviceProvider;
private readonly ILogger<BotController> logger;
private readonly IPPather pather;
private readonly IPathVizualizer pathViz;
private readonly MinimapNodeFinder minimapNodeFinder;
private readonly DataConfig dataConfig;
private readonly CancellationTokenSource cts;
Expand Down Expand Up @@ -64,7 +65,9 @@ public sealed partial class BotController : IBotController, IDisposable
public BotController(
ILogger<BotController> logger,
CancellationTokenSource cts,
IPPather pather, DataConfig dataConfig,
IPPather pather,
IPathVizualizer pathViz,
DataConfig dataConfig,
WowProcess process,
IWowScreen screen,
NpcNameFinder npcNameFinder,
Expand All @@ -81,6 +84,7 @@ public BotController(

this.logger = logger;
this.pather = pather;
this.pathViz = pathViz;
this.dataConfig = dataConfig;

this.screen = screen;
Expand Down Expand Up @@ -121,7 +125,7 @@ public BotController(
screenshotThread = new(ScreenshotThread);
screenshotThread.Start();

if (pather is RemotePathingAPI)
if (pathViz is not NoPathVisualizer)
{
remotePathing = new(RemotePathingThread);
remotePathing.Start();
Expand Down Expand Up @@ -239,35 +243,44 @@ static double Average(ReadOnlySpan<double> span)

private void RemotePathingThread()
{
bool newLoaded = false;
bool routeChanged = false;
RouteInfo? routeInfo = null;

ProfileLoaded += OnProfileLoaded;
void OnProfileLoaded() => newLoaded = true;
void OnProfileLoaded()
{
routeChanged = true;
routeInfo = sessionScope!.ServiceProvider.GetRequiredService<RouteInfo>();
}

Vector3 oldPos = Vector3.Zero;
Vector3[] mapRoute = Array.Empty<Vector3>();

while (!cts.IsCancellationRequested)
{
cts.Token.WaitHandle.WaitOne(remotePathingTickMs);

if (sessionScope == null)
if (sessionScope == null || routeInfo == null)
continue;

if (newLoaded)
if (routeChanged)
{
Vector3[] mapRoute = sessionScope
.ServiceProvider.GetRequiredService<Vector3[]>();

pather.DrawLines(new()
mapRoute = routeInfo.Route;
if (mapRoute.Length == 0)
{
new LineArgs("grindpath",
mapRoute, 2, playerReader.UIMapId.Value)
}).AsTask().Wait(cts.Token);
continue;
}

pather.DrawLines(
[
new LineArgs("grindpath", mapRoute, 2, playerReader.UIMapId.Value),
]).AsTask().Wait(cts.Token);

oldPos = Vector3.Zero;
newLoaded = false;
routeChanged = false;
}

if (playerReader.MapPos != oldPos)
if (!routeChanged && playerReader.MapPos != oldPos)
{
oldPos = playerReader.MapPos;

Expand All @@ -276,6 +289,13 @@ private void RemotePathingThread()
bits.Combat() ? 1 : bits.Target() ? 6 : 2,
playerReader.UIMapId.Value))
.AsTask().Wait(cts.Token);

_ = routeInfo.NextPoint();

if (!routeInfo.Route.SequenceEqual(mapRoute))
{
routeChanged = true;
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions Core/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ public static IServiceCollection AddCoreNormal(
s.AddSingleton<IScreenCapture>(x =>
GetScreenCapture(x.GetRequiredService<IServiceProvider>(), log));

s.AddSingleton<IPathVizualizer>(x =>
GetPathVizualizer(x.GetRequiredService<IServiceProvider>(), log));

s.AddSingleton<IPPather>(x =>
GetPather(x.GetRequiredService<IServiceProvider>(), log));

Expand Down Expand Up @@ -306,12 +309,14 @@ private static IPPather GetPather(IServiceProvider sp, ILogger logger)
var scp = sp.GetRequiredService<IOptions<StartupConfigPathing>>().Value;
var dataConfig = sp.GetRequiredService<DataConfig>();
var worldMapAreaDB = sp.GetRequiredService<WorldMapAreaDB>();
var pathViz = sp.GetRequiredService<IPathVizualizer>();

bool failed = false;
if (scp.Type == StartupConfigPathing.Types.RemoteV3)
{
var remoteLogger = loggerFactory.CreateLogger<RemotePathingAPIV3>();
RemotePathingAPIV3 api = new(
pathViz,
remoteLogger,
scp.hostv3, scp.portv3, worldMapAreaDB);
if (api.PingServer())
Expand Down Expand Up @@ -360,4 +365,27 @@ private static IPPather GetPather(IServiceProvider sp, ILogger logger)

return localApi;
}

private static IPathVizualizer GetPathVizualizer(IServiceProvider sp, ILogger logger)
{
var loggerFactory = sp.GetRequiredService<ILoggerFactory>();
var remoteLogger = loggerFactory.CreateLogger<RemotePathingAPI>();

var scp = sp.GetRequiredService<IOptions<StartupConfigPathing>>().Value;
RemotePathingAPI? api = new(remoteLogger, scp.hostv1, scp.portv1);

if (!api.PingServer())
{
api.Dispose();
api = null;
}
else
{
logger.LogInformation(
$"Found PathViz {StartupConfigPathing.Types.RemoteV1}({api.GetType().Name}) " +
$"{scp.hostv1}:{scp.portv1}");
}

return api ?? (IPathVizualizer)new NoPathVisualizer();
}
}
18 changes: 18 additions & 0 deletions Core/PPather/IPathVizualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

using PPather.Data;

namespace Core;

public interface IPathVizualizer : IDisposable
{
HttpClient Client { get; }
JsonSerializerOptions Options { get; }

ValueTask DrawLines(List<LineArgs> lineArgs);
ValueTask DrawSphere(SphereArgs args);
}
21 changes: 21 additions & 0 deletions Core/PPather/NoPathVisualizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections.Generic;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

using PPather.Data;

namespace Core;

internal sealed class NoPathVisualizer : IPathVizualizer
{
public HttpClient Client => throw new System.NotImplementedException();

public JsonSerializerOptions Options => throw new System.NotImplementedException();

public void Dispose() { }

public ValueTask DrawLines(List<LineArgs> lineArgs) => ValueTask.CompletedTask;

public ValueTask DrawSphere(SphereArgs args) => ValueTask.CompletedTask;
}
7 changes: 4 additions & 3 deletions Core/PPather/RemotePathingAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
using System.Numerics;
using SharedLib.Converters;
using System.Net.Sockets;
using System.Diagnostics;

namespace Core;

public sealed class RemotePathingAPI : IPPather, IDisposable
public sealed class RemotePathingAPI : IPPather, IPathVizualizer, IDisposable
{
private readonly ILogger<RemotePathingAPI> logger;

private readonly string host = "127.0.0.1";
private readonly int port = 5001;

private readonly JsonSerializerOptions options;

private readonly HttpClient client;

public HttpClient Client => client;
public JsonSerializerOptions Options => options;

public RemotePathingAPI(ILogger<RemotePathingAPI> logger,
string host, int port)
{
Expand Down
Loading

0 comments on commit 9462057

Please sign in to comment.