Getting close
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using ConsoleApp.Maps;
|
||||
using HexCore;
|
||||
|
||||
namespace ConsoleApp.PathPlanners
|
||||
@@ -6,5 +7,6 @@ namespace ConsoleApp.PathPlanners
|
||||
public interface IReactivePathPlanner
|
||||
{
|
||||
Queue<Coordinate2D> ReactiveHexPath { get; }
|
||||
void GenerateReactiveHexPath(IHexMap hexMap, Queue<Coordinate2D> optimalPath, Coordinate2D vehicleCurrentHexCell);
|
||||
}
|
||||
}
|
||||
@@ -55,7 +55,7 @@ namespace ConsoleApp.PathPlanners
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
|
||||
public Queue<Coordinate2D> GenerateOptimalHexPath(IHexMap hexMap, IVehicle vehicle)
|
||||
{
|
||||
var currentPostion = vehicle.CurrentHexCell;
|
||||
@@ -64,55 +64,44 @@ namespace ConsoleApp.PathPlanners
|
||||
var path = new List<Coordinate2D>();
|
||||
while (!finished)
|
||||
{
|
||||
//Heading East
|
||||
if (currentHeading == GlobalDirection.East)
|
||||
{
|
||||
path.AddRange(hexMap.Graph.GetShortestPath(
|
||||
currentPostion,
|
||||
new Coordinate2D(hexMap.Width, currentPostion.Y, hexMap.OffsetType),
|
||||
new Coordinate2D(hexMap.Width-1, currentPostion.Y, hexMap.OffsetType),
|
||||
hexMap.DefaultMovement));
|
||||
currentPostion = new Coordinate2D(hexMap.Width, currentPostion.Y, hexMap.OffsetType);
|
||||
currentPostion = new Coordinate2D(hexMap.Width-1, currentPostion.Y, hexMap.OffsetType);
|
||||
}
|
||||
|
||||
else if(currentHeading == GlobalDirection.West)
|
||||
//Heading West
|
||||
else if (currentHeading == GlobalDirection.West)
|
||||
{
|
||||
path.AddRange(hexMap.Graph.GetShortestPath(
|
||||
currentPostion,
|
||||
new Coordinate2D(0, currentPostion.Y, hexMap.OffsetType),
|
||||
hexMap.DefaultMovement));
|
||||
currentPostion = new Coordinate2D(0, currentPostion.Y, hexMap.OffsetType);
|
||||
|
||||
|
||||
}
|
||||
|
||||
if (currentPostion.Y + vehicle.DetectorRadius * 2 > hexMap.Height)
|
||||
//Check for finish
|
||||
if (currentPostion.Y + (vehicle.DetectorRadius * 2) >= hexMap.Height - 1)
|
||||
{
|
||||
finished = true;
|
||||
break;
|
||||
}
|
||||
|
||||
Coordinate2D tmpPosition;
|
||||
if(currentHeading == GlobalDirection.East)
|
||||
tmpPosition = new Coordinate3D(
|
||||
currentPostion.To3D().X-1,
|
||||
currentPostion.To3D().Y,
|
||||
currentPostion.To3D().Z+1).To2D(hexMap.OffsetType);
|
||||
else
|
||||
tmpPosition = new Coordinate3D(
|
||||
currentPostion.To3D().X,
|
||||
currentPostion.To3D().Y-1,
|
||||
currentPostion.To3D().Z+1).To2D(hexMap.OffsetType);
|
||||
while (hexMap.Graph.GetRange(currentPostion, vehicle.DetectorRadius*2).Contains(tmpPosition))
|
||||
//Move Up edges of map
|
||||
var tmpPosition = currentPostion;
|
||||
var range = hexMap.Graph.GetRange(currentPostion, vehicle.DetectorRadius * 2);
|
||||
for (var i = currentPostion.Y; i < currentPostion.Y + vehicle.DetectorRadius * 2; i++)
|
||||
{
|
||||
if(currentHeading == GlobalDirection.East)
|
||||
tmpPosition = new Coordinate3D(
|
||||
tmpPosition.To3D().X-1,
|
||||
tmpPosition.To3D().Y,
|
||||
tmpPosition.To3D().Z+1).To2D(hexMap.OffsetType);
|
||||
else
|
||||
tmpPosition = new Coordinate3D(
|
||||
tmpPosition.To3D().X,
|
||||
tmpPosition.To3D().Y-1,
|
||||
tmpPosition.To3D().Z+1).To2D(hexMap.OffsetType);
|
||||
var newCoord = new Coordinate2D(currentPostion.X, i, hexMap.OffsetType);
|
||||
if (range.Contains(newCoord))
|
||||
tmpPosition = newCoord;
|
||||
|
||||
}
|
||||
|
||||
path.AddRange(hexMap.Graph.GetShortestPath(
|
||||
currentPostion,
|
||||
tmpPosition,
|
||||
@@ -122,9 +111,11 @@ namespace ConsoleApp.PathPlanners
|
||||
currentHeading = GlobalDirection.West;
|
||||
else
|
||||
currentHeading = GlobalDirection.East;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
return new Queue<Coordinate2D>(path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,44 @@
|
||||
using System.Collections.Generic;
|
||||
using ConsoleApp.Maps;
|
||||
using HexCore;
|
||||
|
||||
namespace ConsoleApp.PathPlanners
|
||||
{
|
||||
public class ReactivePathPlanner : IReactivePathPlanner
|
||||
{
|
||||
public Queue<Coordinate2D> ReactiveHexPath { get; }
|
||||
private Queue<Coordinate2D> _reactiveHexPath;
|
||||
public Queue<Coordinate2D> ReactiveHexPath => _reactiveHexPath;
|
||||
|
||||
|
||||
public ReactivePathPlanner()
|
||||
{
|
||||
ReactiveHexPath = new Queue<Coordinate2D>();
|
||||
_reactiveHexPath = new Queue<Coordinate2D>();
|
||||
}
|
||||
|
||||
public void GenerateReactiveHexPath(Graph graph, ref List<Coordinate3D> optimalPath, Coordinate2D minePosition)
|
||||
public void GenerateReactiveHexPath(IHexMap hexMap, Queue<Coordinate2D> optimalPath,
|
||||
Coordinate2D vehicleCurrentHexCell)
|
||||
{
|
||||
//Clean out any old path
|
||||
_reactiveHexPath.Clear();
|
||||
|
||||
//If the optimal path is empty well we are done, otherwise pop 1
|
||||
if (!optimalPath.TryDequeue(out var convergentPoint))
|
||||
return;
|
||||
|
||||
//Dequeue until the cell is not blocked
|
||||
while (hexMap.Graph.IsCellBlocked(convergentPoint))
|
||||
{
|
||||
//if optimal path is empty were done and we can not clear around this position, otherwise
|
||||
//lest pop another.
|
||||
if (!optimalPath.TryDequeue(out convergentPoint))
|
||||
return;
|
||||
}
|
||||
|
||||
var reactivePath = hexMap.Graph.GetShortestPath(vehicleCurrentHexCell, convergentPoint, hexMap.DefaultMovement);
|
||||
foreach (var node in reactivePath)
|
||||
{
|
||||
_reactiveHexPath.Enqueue(node);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user