diff --git a/astar/Astar.cs b/astar/Astar.cs index fc55dbb..f998ea4 100644 --- a/astar/Astar.cs +++ b/astar/Astar.cs @@ -64,7 +64,7 @@ namespace astar Node neighborNode = graph.Nodes[neighborId]; if (neighborNode.PreviousIsFromStart is false)//Check if we found the opposite End - return PathFound(graph, currentNodeStart, neighborNode, logger); + return PathFound(graph, currentNodeStart, neighborNode, car, logger); float distance = (currentNodeStart.Distance??float.MaxValue) + (float)currentNodeStart.DistanceTo(neighborNode); if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance) @@ -102,7 +102,7 @@ namespace astar Node neighborNode = graph.Nodes[neighborId]; if (neighborNode.PreviousIsFromStart is true)//Check if we found the opposite End - return PathFound(graph, neighborNode, currentNodeEnd, logger); + return PathFound(graph, neighborNode, currentNodeEnd, car, logger); float distance = (currentNodeEnd.Distance??float.MaxValue) + (float)currentNodeEnd.DistanceTo(neighborNode); if (neighborNode.PreviousNodeId is null || neighborNode.Distance > distance) @@ -120,25 +120,30 @@ namespace astar return new Route(graph, Array.Empty().ToList(), false); } - private static Route PathFound(Graph graph, Node fromStart, Node fromEnd, ILogger? logger = null) + private static Route PathFound(Graph graph, Node fromStart, Node fromEnd, bool car = true, ILogger? logger = null) { logger?.LogInformation("Path found!"); List path = new(); - path.Add(new Step((float)NodeUtils.DistanceBetween(fromStart, fromEnd), fromStart, fromEnd)); + OSM_Graph.Way toNeighbor = graph.Ways[fromStart.Neighbors.First(n => graph.Nodes[n.Key] == fromEnd).Value.Key]; + path.Add(new Step(fromStart, fromEnd, (float)fromStart.DistanceTo(fromEnd), SpeedHelper.GetSpeed(toNeighbor, car))); Node current = fromStart; while (current.Distance != 0f) { - Step step = new((float)NodeUtils.DistanceBetween(graph.Nodes[(ulong)current.PreviousNodeId!], current), graph.Nodes[(ulong)current.PreviousNodeId!], current); + Node previous = graph.Nodes[(ulong)current.PreviousNodeId!]; + OSM_Graph.Way previousToCurrent = graph.Ways[previous.Neighbors.First(n => graph.Nodes[n.Key] == current).Value.Key]; + Step step = new(previous, current, (float)previous.DistanceTo(current), SpeedHelper.GetSpeed(previousToCurrent, car)); path.Add(step); - current = graph.Nodes[(ulong)current.PreviousNodeId!]; + current = previous; } path.Reverse();//Since we go from the middle backwards until here current = fromEnd; while (current.Distance != 0f) { - Step step = new((float)NodeUtils.DistanceBetween(graph.Nodes[(ulong)current.PreviousNodeId!], current), current, graph.Nodes[(ulong)current.PreviousNodeId!]); + Node next = graph.Nodes[(ulong)current.PreviousNodeId!]; + OSM_Graph.Way currentToNext = graph.Ways[current.Neighbors.First(n => graph.Nodes[n.Key] == next).Value.Key]; + Step step = new(current, next, (float)current.DistanceTo(next), SpeedHelper.GetSpeed(currentToNext, car)); path.Add(step); - current = graph.Nodes[(ulong)current.PreviousNodeId!]; + current = next; } Route r = new (graph, path, true); diff --git a/astar/PathingHelper/PriorityHelper.cs b/astar/PathingHelper/PriorityHelper.cs index 5560183..def1bf4 100644 --- a/astar/PathingHelper/PriorityHelper.cs +++ b/astar/PathingHelper/PriorityHelper.cs @@ -20,6 +20,6 @@ public class PriorityHelper(double totalDistance, byte maxSpeed) //double angleRating = Math.Abs((180 - angle) / 180) * 100; double speedRating = speed * 1.0 / _maxSpeed * 100; - return 350 - (int)(distanceRating * 2 + speedRating * 1.5); + return 300 - (int)(distanceRating * 2 + speedRating * 1); } } \ No newline at end of file diff --git a/astar/Route.cs b/astar/Route.cs index aeb08de..faae735 100644 --- a/astar/Route.cs +++ b/astar/Route.cs @@ -7,6 +7,8 @@ public bool RouteFound { get; } = routeFound; public float Distance => Steps.Sum(step => step.Distance); + public TimeSpan Time => TimeSpan.FromHours(Steps.Sum(step => step.Distance / 1000 / step.Speed)); + public ValueTuple MinCoordinates() { float minLat = Graph.Nodes.MinBy(node => node.Value.Lat).Value.Lat; @@ -24,18 +26,20 @@ public override string ToString() { return $"{string.Join("\n", Steps)}\n" + - $"Distance: {Distance:000000.00}m"; + $"Distance: {Distance:000000.00}m\n" + + $"Time: {Time:000000.00}"; } } - public struct Step(float distance, Node node1, Node node2) + public struct Step(Node node1, Node node2, float distance, byte speed) { public readonly Node Node1 = node1, Node2 = node2; public readonly float Distance = distance; + public readonly byte Speed = speed; public override string ToString() { - return $"{Node1.Lat:00.000000} {Node1.Lon:000.000000} --- {Distance:0000.00}m ---> {Node2.Lat:00.000000} {Node2.Lon:000.000000}"; + return $"{Node1.Lat:00.000000} {Node1.Lon:000.000000} --- {Distance:0000.00}m {Speed:000} ---> {Node2.Lat:00.000000} {Node2.Lon:000.000000}"; } } }