Skip to content

Commit

Permalink
Merge pull request #584 from Xian55/refactor/tidyup-pathingapi
Browse files Browse the repository at this point in the history
PathingAPI improvements REST.API and Swagger
  • Loading branch information
Xian55 authored May 11, 2024
2 parents f894bc8 + db33ba1 commit bf7e72e
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 186 deletions.
8 changes: 8 additions & 0 deletions PPather/Data/DrawPathDtos.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

using System.Numerics;

namespace PPather;

public readonly record struct DrawMapPathRequest(int uiMapId, Vector3[] path);

public readonly record struct DrawWorldPathRequest(int mapId, Vector3[] path);
17 changes: 7 additions & 10 deletions PPather/Graph/PathGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,17 @@ private void CheckForChunkEvict()

public void Save()
{
lock (chunks)
long timestamp = Stopwatch.GetTimestamp();
foreach (GraphChunk gc in chunks.GetAllElements())
{
long timestamp = Stopwatch.GetTimestamp();
foreach (GraphChunk gc in chunks.GetAllElements())
if (gc.modified)
{
if (gc.modified)
{
gc.Save();
}
gc.Save();
}

if (logger.IsEnabled(LogLevel.Trace))
logger.LogTrace($"Saved GraphChunks {Stopwatch.GetElapsedTime(timestamp).TotalMilliseconds} ms");
}

if (logger.IsEnabled(LogLevel.Trace))
logger.LogTrace($"Saved GraphChunks {Stopwatch.GetElapsedTime(timestamp).TotalMilliseconds} ms");
}

// Create and load from file if exisiting
Expand Down
47 changes: 46 additions & 1 deletion PPather/Search/PPatherService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using SharedLib.Data;
using Microsoft.Extensions.Logging;
using System.Numerics;
using SharedLib.Extensions;

namespace PPather;

Expand All @@ -25,6 +26,8 @@ public sealed class PPatherService

private Search search { get; set; }

public bool Initialised => search != null;

public Vector4 SearchFrom => search.locationFrom;
public Vector4 SearchTo => search.locationTo;
public Vector3 ClosestLocation => search?.PathGraph?.ClosestSpot?.Loc ?? Vector3.Zero;
Expand Down Expand Up @@ -83,6 +86,20 @@ public void ChunkAdded(ChunkEventArgs e)
OnChunkAdded?.Invoke(e);
}

public Vector4[] CreateLocations(LineArgs lines)
{
Vector4[] result = new Vector4[lines.Spots.Length];
Span<Vector4> span = result.AsSpan();

for (int i = 0; i < span.Length; i++)
{
Vector3 spot = lines.Spots[i];
span[i] = ToWorld(lines.MapId, spot.X, spot.Y, spot.Z);
}

return result;
}

public Vector4 ToWorld(int uiMap, float mapX, float mapY, float z = 0)
{
if (!worldMapAreaDB.TryGet(uiMap, out WorldMapArea wma))
Expand All @@ -106,6 +123,11 @@ public Vector4 ToWorldZ(int uiMap, float x, float y, float z)
return search.CreateWorldLocation(x, y, z, wma.MapID);
}

public int GetMapId(int uiMap)
{
return worldMapAreaDB.GetMapId(uiMap);
}

public Vector3 ToLocal(Vector3 world, float mapId, int uiMapId)
{
WorldMapArea wma = worldMapAreaDB.GetWorldMapArea(world.X, world.Y, (int)mapId, uiMapId);
Expand Down Expand Up @@ -143,6 +165,29 @@ public List<Spot> GetCurrentSearchPath()
return search.PathGraph.CurrentSearchPath();
}

public float TransformMapToWorld(int uiMapId, Vector3[] path)
{
float mapId = -1;

Span<Vector3> span = path;
for (int i = 0; i < span.Length; i++)
{
Vector3 p = span[i];
if (p.Z != 0)
{
mapId = GetMapId(uiMapId);
break;
}

Vector4 world = ToWorld(uiMapId, p.X, p.Y, p.Z);

span[i] = world.AsVector3();
mapId = world.W;
}

return mapId;
}

public void DrawPath(float mapId, ReadOnlySpan<Vector3> path)
{
Vector4 from = new(path[0], mapId);
Expand All @@ -155,7 +200,7 @@ public void DrawPath(float mapId, ReadOnlySpan<Vector3> path)
search.CreatePathGraph(mapId);
}

List<Spot> spots = new();
List<Spot> spots = new(path.Length);
for (int i = 0; i < path.Length; i++)
{
Spot spot = new(path[i]);
Expand Down
Loading

0 comments on commit bf7e72e

Please sign in to comment.