I think its done

This commit is contained in:
2020-12-16 17:19:25 -07:00
parent a792de99f3
commit e335ce3c68
34 changed files with 5679 additions and 927 deletions

View File

@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using HexCore;
namespace ConsoleApp.Maps
{
public class AstarSquareSearch
{
private static double Heuristic(ICell a, ICell b)
{
return Math.Abs(a.X - b.X) + Math.Abs(a.Y - b.Y);
}
public static List<ICell> FindShortestPath(ISquareMap graph, ICell start, ICell goal)
{
var costSoFar = new Dictionary<ICell, int>();
var cameFrom = new Dictionary<ICell, ICell>();
var frontier = new PriorityQueue<ICell>();
frontier.Enqueue(start, 0);
cameFrom[start] = start;
costSoFar[start] = 0;
while (frontier.Count > 0)
{
var current = frontier.Dequeue();
if (current.Equals(goal)) break;
foreach (var next in graph.GetPassableNeighbors(current))
{
var newCost = costSoFar[current] + (int)graph.GetCell(next.X, next.Y).Coverage;
if (costSoFar.ContainsKey(next) && newCost >= costSoFar[next]) continue;
costSoFar[next] = newCost;
var priority = newCost + Heuristic(next, goal);
frontier.Enqueue(next, priority);
cameFrom[next] = current;
}
}
var path = new List<ICell>();
var pathWasNotFound = !cameFrom.ContainsKey(goal);
// Returning an empty list if the path wasn't found
if (pathWasNotFound) return path;
// Reconstructing path
var curr = goal;
while (!curr.Equals(start))
{
path.Add(curr);
curr = cameFrom[curr];
}
// Reverse it to start at actual start point
path.Reverse();
return path;
}
}
}

View File

@@ -4,20 +4,23 @@ namespace ConsoleApp.Maps
{
public enum Coverage
{
Uncoverd,
//Order here matters these are cast to ints for the heuristic algorithm
Uncovered,
Covered
}
public struct Cell : ICell
public class Cell : ICell
{
public int X { get; }
public int Y { get; }
public Coverage Coverage { get; set; }
public bool Blocked { get; set; }
public Cell(int x, int y)
{
X = x;
Y = y;
Coverage = Coverage.Uncoverd;
Coverage = Coverage.Uncovered;
Blocked = false;
}

View File

@@ -5,5 +5,6 @@ namespace ConsoleApp.Maps
int X { get; }
int Y { get; }
Coverage Coverage { get; set; }
bool Blocked { get; set; }
}
}

View File

@@ -12,6 +12,9 @@ namespace ConsoleApp.Maps
Cell GetCell(int x, int y);
int Height { get; }
int Width { get; }
List<Cell> GetRange(Cell centerCell, int radius);
List<Cell> GetRange(ICell centerCell, int radius);
List<Cell> GetNeighbors(ICell cell);
List<ICell> GetPassableNeighbors(ICell cell);
List<ICell> GetShortestPath(ICell start, ICell goal);
}
}

View File

@@ -1,5 +1,12 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Example;
namespace ConsoleApp.Maps
{
public static class MapExtensions
{
public static void Fill2DArray<T>(this T[,] arr, T value)

View File

@@ -1,5 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Threading;
using HexCore;
namespace ConsoleApp.Maps
@@ -29,8 +33,7 @@ namespace ConsoleApp.Maps
//Initialize Map
Map = new Cell[x, y];
//set last cell;
StartingCell = Map[0, 0];
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
@@ -38,6 +41,9 @@ namespace ConsoleApp.Maps
Map[i,j] = new Cell(i, j);
}
}
//set Starting cell;
StartingCell = GetCell(0,0);
}
@@ -56,8 +62,13 @@ namespace ConsoleApp.Maps
return possibles;
}
public List<ICell> GetShortestPath(ICell start, ICell goal)
{
return AstarSquareSearch.FindShortestPath(this, start, goal);
}
public List<Cell> GetRange(Cell centerCell, int radius)
public List<Cell> GetRange(ICell centerCell, int radius)
{
var inRange = new List<Cell>();
var cx = centerCell.X;
@@ -77,6 +88,32 @@ namespace ConsoleApp.Maps
return inRange;
}
public List<ICell> GetPassableNeighbors(ICell cell) =>
GetNeighbors(cell).Where(neighbor => !neighbor.Blocked).Cast<ICell>().ToList();
private static int ComputeHScore(ICell src, Cell dest)
{
return Math.Abs(dest.X - src.X) + Math.Abs(dest.Y - src.Y);
}
public List<Cell> GetNeighbors(ICell cell)
{
var rlist = new List<Cell>();
var rowOperations = new int[] { -1, 0, 0, 1};
var colOperations = new int[] {0, -1, 1, 0};
for (var i = 0; i < 4; i++)
{
var curCell = GetCell(cell.X + rowOperations[i], cell.Y + colOperations[i]);
if (curCell != default)
{
rlist.Add(curCell);
}
}
return rlist;
}
private Cell GetTopCellInBoundingBox(int cx, int cy, int radius)
{
int topX, topY;
@@ -91,16 +128,21 @@ namespace ConsoleApp.Maps
private Cell GetBottomCellInBoundingBox(int cx, int cy, int radius)
{
int bottomX, bottomY;
if (cy - radius > 0) bottomY = 0;
if (cy - radius < 0) bottomY = 0;
else
bottomY = cy - radius;
if (cx + radius < Width) bottomX = Width;
if (cx + radius > Width) bottomX = Width;
else
bottomX = cx + radius;
return Map[bottomX, bottomY];
}
public Cell GetCell(int x, int y) => Map[x, y];
public Cell GetCell(int x, int y)
{
if (x > Width || y > Height || x < 0 || y < 0)
return default;
return Map[x, y];
}
}
}