Skip to content

Commit

Permalink
Merge pull request #668 from Xian55/feature/pathingapi/messagepack
Browse files Browse the repository at this point in the history
PathingAPI use SignalR with MessagePack to transfer large chunk of geometry data from c# backend to BabylonJS frontend
  • Loading branch information
Xian55 authored Jan 27, 2025
2 parents b0ceb30 + c5f7e31 commit bb66dd3
Show file tree
Hide file tree
Showing 15 changed files with 472 additions and 222 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
<PackageVersion Include="Blazor.Bootstrap" Version="3.2.0" />
<PackageVersion Include="MemoryPack" Version="1.21.3" />
<PackageVersion Include="MessagePack" Version="3.1.1" />
<PackageVersion Include="Microsoft.AspNetCore.Components" Version="9.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.Components.QuickGrid" Version="9.0.0" />
<PackageVersion Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="9.0.0" />
<PackageVersion Include="Serilog.AspNetCore" Version="9.0.0" />
<PackageVersion Include="Serilog.Enrichers.Environment" Version="3.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion PPather/Graph/Path.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace PPather.Graph;

public sealed class Path
{
public List<Vector3> locations { get; } = new();
public List<Vector3> locations { get; } = [];

public Vector3 GetLast => locations[^1];

Expand Down
36 changes: 24 additions & 12 deletions PPather/Graph/PathGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ private static float TurnCost(float x0, float y0, float z0, float x1, float y1,

public Spot ClosestSpot;
public Spot PeekSpot;
public Vector3[] TestPoints = [];
public HashSet<Vector3> TestPoints = [];

private Spot Search(Spot fromSpot, Spot destinationSpot, eSearchScoreSpot searchScoreSpot, float minHowClose)
{
Expand Down Expand Up @@ -608,7 +608,7 @@ private Spot Search(Spot fromSpot, Spot destinationSpot, eSearchScoreSpot search
if (GetElapsedTime(timeSinceProgress).TotalSeconds > ProgressTimeoutSeconds ||
GetElapsedTime(searchDuration).TotalSeconds > TimeoutSeconds)
{
logger.LogWarning("search failed, 10 seconds since last progress, returning the closest spot.");
logger.LogWarning($"search failed, {ProgressTimeoutSeconds} seconds since last progress, returning the closest spot.");
return ClosestSpot;
}

Expand All @@ -617,13 +617,14 @@ private Spot Search(Spot fromSpot, Spot destinationSpot, eSearchScoreSpot search

//score each spot around the current search spot and add them to the queue
ReadOnlySpan<Spot> spots = currentSearchSpot.GetPathsToSpots(this);
//TestPoints = spots.ToVecArray();

for (int i = 0; i < spots.Length; i++)
{
Spot linked = spots[i];
if (linked != null && !linked.IsBlocked() && !linked.SearchIsClosed(currentSearchID))
{
TestPoints.Add(linked.Loc);

ScoreSpot(linked, destinationSpot, searchScoreSpot, currentSearchID, prioritySpotQueue);
}
}
Expand Down Expand Up @@ -741,7 +742,7 @@ public void CreateSpotsAroundSpot(Spot currentSearchSpot, bool mapped)
currentSearchSpot.SetFlag(Spot.FLAG_MPQ_MAPPED, true);

//loop through the spots in a circle around the current search spot
for (float radianAngle = 0; radianAngle < Tau; radianAngle += PI / 8)
for (float radianAngle = 0; radianAngle < Tau; radianAngle += PI / 8) // 4
{
//calculate the location of the spot at the angle
float nx = currentSearchSpot.Loc.X + (Sin(radianAngle) * WantedStepLength);// *0.8f;
Expand Down Expand Up @@ -810,27 +811,38 @@ public void CreateSpotsAroundSpot(Spot currentSearchSpot, bool mapped)

private Spot lastCurrentSearchSpot;

public List<Spot> CurrentSearchPath()
public List<Vector3> CurrentSearchPath()
{
if (lastCurrentSearchSpot == currentSearchSpot)
{
return null;
return [];
}

lastCurrentSearchSpot = currentSearchSpot;
return FollowTraceBack(currentSearchStartSpot, currentSearchSpot);
return FollowTraceBackLocations(currentSearchStartSpot, currentSearchSpot);
}

private static List<Spot> FollowTraceBack(Spot from, Spot to)
{
List<Spot> path = new();
Spot backtrack = to;
while (backtrack != from && backtrack != null)
List<Spot> path = [];
for (Spot backtrack = to; backtrack != null; backtrack = backtrack.traceBack)
{
path.Insert(0, backtrack);
backtrack = backtrack.traceBack;
if (backtrack == from)
break;
}
return path;
}

private static List<Vector3> FollowTraceBackLocations(Spot from, Spot to)
{
List<Vector3> path = [];
for (Spot backtrack = to; backtrack != null; backtrack = backtrack.traceBack)
{
path.Insert(0, backtrack.Loc);
if (backtrack == from)
break;
}
path.Insert(0, from);
return path;
}

Expand Down
16 changes: 5 additions & 11 deletions PPather/Search/MeshFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ public static List<Vector3> CreatePoints(TriangleCollection collection)
}


public static int[] CreateTriangles(TriangleType modelType, TriangleCollection tc)
public static int CreateTriangles(TriangleType modelType, TriangleCollection tc, int[] output)
{
var pooler = ArrayPool<int>.Shared;
var triangles = pooler.Rent(tc.TriangleCount * 3);
int c = 0;

for (int i = 0; i < tc.TriangleCount; i++)
Expand All @@ -27,15 +25,11 @@ public static int[] CreateTriangles(TriangleType modelType, TriangleCollection t
if (flags != modelType)
continue;

triangles[c++] = v0;
triangles[c++] = v1;
triangles[c++] = v2;
output[c++] = v0;
output[c++] = v1;
output[c++] = v2;
}

pooler.Return(triangles);

return c == 0
? Array.Empty<int>()
: triangles.AsSpan(0, c).ToArray();
return c;
}
}
15 changes: 6 additions & 9 deletions PPather/Search/PPatherService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging;

using PPather.Data;
using PPather.Graph;
Expand Down Expand Up @@ -37,7 +37,7 @@ public sealed class PPatherService
public Vector3 ClosestLocation => search?.PathGraph?.ClosestSpot?.Loc ?? Vector3.Zero;
public Vector3 PeekLocation => search?.PathGraph?.PeekSpot?.Loc ?? Vector3.Zero;

public Vector3[] TestPoints => search?.PathGraph?.TestPoints ?? Array.Empty<Vector3>();
public HashSet<Vector3> TestPoints => search?.PathGraph?.TestPoints ?? [];

public PPatherService(ILogger<PPatherService> logger, DataConfig dataConfig, WorldMapAreaDB worldMapAreaDB)
{
Expand Down Expand Up @@ -164,14 +164,11 @@ public void SetLocations(Vector4 from, Vector4 to)
search.locationTo = to;
}

public List<Spot> GetCurrentSearchPath()
public List<Vector3> GetCurrentSearchPath()
{
if (search == null || search.PathGraph == null)
{
return null;
}

return search.PathGraph.CurrentSearchPath();
return search == null || search.PathGraph == null
? []
: search.PathGraph.CurrentSearchPath();
}

public float TransformMapToWorld(int uiMapId, Vector3[] path)
Expand Down
16 changes: 15 additions & 1 deletion PPather/Triangles/Data/TriangleType.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace PPather;
using System;

namespace PPather;

[System.Flags]
public enum TriangleType : byte
Expand All @@ -16,4 +18,16 @@ public static bool Has(this TriangleType flags, TriangleType flag)
{
return (flags & flag) != 0;
}

public static int ToIndex(this TriangleType type)
{
return type switch
{
TriangleType.Terrain => 0,
TriangleType.Water => 1,
TriangleType.Object => 2,
TriangleType.Model => 3,
_ => throw new ArgumentOutOfRangeException(nameof(type), $"Unexpected value: {type}")
};
}
}
8 changes: 8 additions & 0 deletions PathingAPI/Hubs/WatchHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Microsoft.AspNetCore.SignalR;

namespace PathingAPI;

public sealed class WatchHub : Hub
{
public const string Url = "/watchHub";
}
4 changes: 2 additions & 2 deletions PathingAPI/Pages/Search.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@
{
try
{
await jsRuntime.InvokeVoidAsync("removeMeshes", name);
await jsRuntime.InvokeVoidAsync("removeMeshes", "search");
await jsRuntime.InvokeVoidAsync("removeMesh", name);
await jsRuntime.InvokeVoidAsync("removeMesh", "search");

pPatherService.SetLocations(parameters.From.Location, parameters.To.Location);

Expand Down
Loading

0 comments on commit bb66dd3

Please sign in to comment.