2023-07-15 21:50:38 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2022-12-02 21:09:40 +01:00
|
|
|
using Godot;
|
2023-07-15 21:50:38 +02:00
|
|
|
using Priority_Queue;
|
2023-08-13 21:18:46 +02:00
|
|
|
using AxialCoordDirectionPair = System.Tuple<Godot.Vector2, Godot.Vector3>;
|
2022-12-02 21:09:40 +01:00
|
|
|
|
|
|
|
public class HexGrid : Resource
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
private readonly Vector2 _baseHexSize = new(1, Mathf.Sqrt(3) / 2);
|
|
|
|
private Rect2 _boundsAxialCoords;
|
2023-08-13 21:18:46 +02:00
|
|
|
private Vector2 _hexScale = new(1, 1);
|
2023-11-05 21:46:28 +01:00
|
|
|
private Vector2 _hexSize = new(1, Mathf.Sqrt(3) / 2);
|
2023-08-13 21:18:46 +02:00
|
|
|
private Transform2D _hexTransform;
|
|
|
|
private Transform2D _hexTransformInv;
|
|
|
|
private HexCell _maxCoords = new();
|
2023-11-05 21:46:28 +01:00
|
|
|
private HexCell _minCoords = new();
|
2023-08-13 21:18:46 +02:00
|
|
|
|
|
|
|
public Dictionary<(Vector2, Vector3), float> Barriers = new();
|
2023-11-05 21:46:28 +01:00
|
|
|
public int FindPathCheckedCellCount;
|
|
|
|
|
|
|
|
public Dictionary<Vector2, float> Obstacles = new();
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
public float PathCostDefault = 1;
|
2022-12-02 21:09:40 +01:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
public HexGrid()
|
2022-12-02 21:09:40 +01:00
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
HexScale = new Vector2(1, 1);
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|
2022-12-28 16:22:53 +01:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
public Vector2 HexSize => _hexSize;
|
|
|
|
|
2022-12-02 21:09:40 +01:00
|
|
|
public Vector2 HexScale
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
get => _hexScale;
|
2022-12-02 21:09:40 +01:00
|
|
|
set
|
|
|
|
{
|
|
|
|
_hexScale = value;
|
|
|
|
_hexSize = _baseHexSize * _hexScale;
|
|
|
|
_hexTransform = new Transform2D(
|
2022-12-28 16:22:53 +01:00
|
|
|
new Vector2(_hexSize.x * 3 / 4, -_hexSize.y / 2),
|
2022-12-02 21:09:40 +01:00
|
|
|
new Vector2(0, -_hexSize.y),
|
|
|
|
new Vector2(0, 0)
|
|
|
|
);
|
2022-12-28 16:22:53 +01:00
|
|
|
|
2022-12-02 21:09:40 +01:00
|
|
|
_hexTransformInv = _hexTransform.AffineInverse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 GetHexCenter(HexCell cell)
|
|
|
|
{
|
|
|
|
return _hexTransform * cell.AxialCoords;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Vector2 GetHexCenterFromOffset(Vector2 offsetCoord)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var cell = new HexCell();
|
2022-12-02 21:09:40 +01:00
|
|
|
cell.OffsetCoords = offsetCoord;
|
|
|
|
return GetHexCenter(cell);
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
public Vector3 GetHexCenterVec3FromOffset(Vector2 offsetCoord)
|
|
|
|
{
|
|
|
|
var cell = new HexCell();
|
|
|
|
cell.OffsetCoords = offsetCoord;
|
|
|
|
var hexCenter = GetHexCenter(cell);
|
|
|
|
return new Vector3(hexCenter.x, 0, hexCenter.y);
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:09:40 +01:00
|
|
|
public HexCell GetHexAtOffset(Vector2 offsetCoord)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var cell = new HexCell();
|
2022-12-02 21:09:40 +01:00
|
|
|
cell.OffsetCoords = offsetCoord;
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HexCell GetHexAt(Vector2 planeCoord)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var result = new HexCell(_hexTransformInv * planeCoord);
|
2022-12-02 21:09:40 +01:00
|
|
|
return result;
|
|
|
|
}
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
public void SetBounds(Vector2 minAxial, Vector2 maxAxial)
|
|
|
|
{
|
|
|
|
SetBounds(new HexCell(minAxial), new HexCell(maxAxial));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetBounds(HexCell minCell, HexCell maxCell)
|
|
|
|
{
|
|
|
|
_minCoords = minCell;
|
|
|
|
_maxCoords = maxCell;
|
2023-08-13 21:18:46 +02:00
|
|
|
_boundsAxialCoords = new Rect2(_minCoords.AxialCoords,
|
2023-11-05 21:46:28 +01:00
|
|
|
_maxCoords.AxialCoords - _minCoords.AxialCoords + Vector2.One);
|
2023-07-15 21:50:38 +02:00
|
|
|
}
|
|
|
|
|
2023-10-27 21:49:37 +02:00
|
|
|
public void SetBounds(HexCell center, int size)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var centerOffset = center.OffsetCoords;
|
2023-10-27 21:49:37 +02:00
|
|
|
SetBounds(GetHexAtOffset(centerOffset - Vector2.One * size / 2),
|
|
|
|
GetHexAtOffset(centerOffset + Vector2.One * size / 2));
|
|
|
|
}
|
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
public void AddObstacle(Vector2 axialCoords, float cost = 0)
|
|
|
|
{
|
|
|
|
AddObstacle(new HexCell(axialCoords), cost);
|
|
|
|
}
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
public void AddObstacle(HexCell cell, float cost = 0)
|
|
|
|
{
|
2023-08-13 21:18:46 +02:00
|
|
|
Obstacles[cell.AxialCoords] = cost;
|
2023-07-15 21:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveObstacle(HexCell cell)
|
|
|
|
{
|
|
|
|
Obstacles.Remove(cell.AxialCoords);
|
|
|
|
}
|
2023-08-13 21:18:46 +02:00
|
|
|
|
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
public void AddBarrier(Vector2 axialCoords, Vector3 directionCube, float cost = 0)
|
|
|
|
{
|
|
|
|
AddBarrier(new HexCell(axialCoords), directionCube, cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddBarrier(HexCell cell, Vector3 directionCube, float cost = 0)
|
|
|
|
{
|
|
|
|
Barriers.Add((cell.AxialCoords, directionCube), cost);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void RemoveBarrier(HexCell cell, Vector3 directionCube)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
if (Barriers.ContainsKey((cell.AxialCoords, directionCube))) Barriers.Remove((cell.AxialCoords, directionCube));
|
2023-07-15 21:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public float GetHexCost(HexCell cell)
|
|
|
|
{
|
|
|
|
return GetHexCost(cell.AxialCoords);
|
|
|
|
}
|
|
|
|
|
|
|
|
public float GetHexCost(Vector2 axialCoords)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
if (!_boundsAxialCoords.HasPoint(axialCoords)) return 0;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
float value;
|
|
|
|
return Obstacles.TryGetValue(axialCoords, out value) ? value : PathCostDefault;
|
|
|
|
}
|
|
|
|
|
|
|
|
public float GetMoveCost(Vector2 axialCoords, Vector3 directionCube)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var startCell = new HexCell(axialCoords);
|
|
|
|
var targetCell = new HexCell(startCell.CubeCoords + directionCube);
|
2023-07-15 21:50:38 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
var cost = GetHexCost(axialCoords);
|
|
|
|
if (cost == 0) return 0;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
cost = GetHexCost(targetCell.AxialCoords);
|
2023-11-05 21:46:28 +01:00
|
|
|
if (cost == 0) return 0;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
float barrierCost;
|
2023-08-13 21:18:46 +02:00
|
|
|
if (Barriers.ContainsKey((axialCoords, directionCube)))
|
2023-07-15 21:50:38 +02:00
|
|
|
{
|
|
|
|
barrierCost = Barriers[(axialCoords, directionCube)];
|
2023-11-05 21:46:28 +01:00
|
|
|
if (barrierCost == 0) return 0;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
cost += barrierCost;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Barriers.ContainsKey((targetCell.AxialCoords, -directionCube)))
|
|
|
|
{
|
|
|
|
barrierCost = Barriers[(targetCell.AxialCoords, -directionCube)];
|
2023-11-05 21:46:28 +01:00
|
|
|
if (barrierCost == 0) return 0;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
cost += barrierCost;
|
|
|
|
}
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
return cost;
|
|
|
|
}
|
|
|
|
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-07-28 22:14:28 +02:00
|
|
|
public HexCell GetClosestWalkableCell(HexCell fromCell, HexCell toCell)
|
|
|
|
{
|
|
|
|
if (GetHexCost(toCell) == 0)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var line = fromCell.LineTo(toCell);
|
2023-07-28 22:14:28 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
foreach (var i in Enumerable.Range(1, line.Length))
|
2023-07-28 22:14:28 +02:00
|
|
|
if (GetHexCost(line[i]) == 0)
|
|
|
|
{
|
|
|
|
toCell = line[i - 1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return toCell;
|
|
|
|
}
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
public List<HexCell> FindPath(HexCell startHex, HexCell goalHex)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var goalAxialCoords = goalHex.AxialCoords;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
var frontier = new SimplePriorityQueue<Vector2, float>();
|
2023-07-15 21:50:38 +02:00
|
|
|
frontier.Enqueue(startHex.AxialCoords, 0);
|
2023-11-05 21:46:28 +01:00
|
|
|
var cameFrom =
|
2023-08-13 21:18:46 +02:00
|
|
|
new Dictionary<Vector2, Vector2>();
|
2023-11-05 21:46:28 +01:00
|
|
|
var costSoFar =
|
2023-08-13 21:18:46 +02:00
|
|
|
new Dictionary<Vector2, float>();
|
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
cameFrom.Add(startHex.AxialCoords, startHex.AxialCoords);
|
|
|
|
costSoFar.Add(startHex.AxialCoords, 0);
|
|
|
|
|
2023-10-27 21:49:37 +02:00
|
|
|
FindPathCheckedCellCount = 0;
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-07-15 21:50:38 +02:00
|
|
|
while (frontier.Any())
|
|
|
|
{
|
2023-10-27 21:49:37 +02:00
|
|
|
FindPathCheckedCellCount++;
|
2023-11-05 21:46:28 +01:00
|
|
|
var currentHex = new HexCell(frontier.Dequeue());
|
|
|
|
var currentAxial = currentHex.AxialCoords;
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
if (currentHex == goalHex) break;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
foreach (var nextHex in currentHex.GetAllAdjacent())
|
2023-07-15 21:50:38 +02:00
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var nextAxial = nextHex.AxialCoords;
|
|
|
|
var nextCost = GetMoveCost(currentAxial, new HexCell(nextAxial - currentHex.AxialCoords).CubeCoords);
|
2023-07-15 21:50:38 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
if (nextHex == goalHex && GetHexCost(nextAxial) == 0)
|
2023-07-15 21:50:38 +02:00
|
|
|
{
|
|
|
|
// Goal ist an obstacle
|
|
|
|
cameFrom[nextHex.AxialCoords] = currentHex.AxialCoords;
|
|
|
|
frontier.Clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
if (nextCost == 0) continue;
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
nextCost += costSoFar[currentHex.AxialCoords];
|
|
|
|
if (!costSoFar.ContainsKey(nextHex.AxialCoords) || nextCost < costSoFar[nextHex.AxialCoords])
|
|
|
|
{
|
|
|
|
costSoFar[nextHex.AxialCoords] = nextCost;
|
2023-11-05 21:46:28 +01:00
|
|
|
var priority = nextCost + nextHex.DistanceTo(goalHex);
|
2023-07-15 21:50:38 +02:00
|
|
|
|
|
|
|
frontier.Enqueue(nextHex.AxialCoords, priority);
|
|
|
|
cameFrom[nextHex.AxialCoords] = currentHex.AxialCoords;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 21:49:37 +02:00
|
|
|
// GD.Print("Checked Cell Count: " + FindPathCheckedCellCount);
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
var result = new List<HexCell>();
|
2023-07-15 21:50:38 +02:00
|
|
|
if (!cameFrom.ContainsKey(goalHex.AxialCoords))
|
|
|
|
{
|
2023-08-12 18:16:45 +02:00
|
|
|
GD.Print("Failed to find path from " + startHex + " to " + goalHex);
|
2023-08-13 21:18:46 +02:00
|
|
|
return result;
|
2023-07-15 21:50:38 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
if (GetHexCost(goalAxialCoords) != 0) result.Add(goalHex);
|
2023-07-15 21:50:38 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
var pathHex = goalHex;
|
2023-07-15 21:50:38 +02:00
|
|
|
while (pathHex != startHex)
|
|
|
|
{
|
|
|
|
pathHex = new HexCell(cameFrom[pathHex.AxialCoords]);
|
|
|
|
result.Insert(0, pathHex);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-08-13 21:18:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
public List<HexCell> GetCellsForLine(Vector2 fromPlane, Vector2 toPlane)
|
|
|
|
{
|
2023-11-05 21:46:28 +01:00
|
|
|
var result = new List<HexCell>();
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
var distance = (toPlane - fromPlane).Length();
|
|
|
|
var direction = (toPlane - fromPlane) / distance;
|
2023-08-13 21:18:46 +02:00
|
|
|
|
2023-11-05 21:46:28 +01:00
|
|
|
var currentPointPlane = fromPlane;
|
|
|
|
var currentCell = GetHexAt(currentPointPlane);
|
2023-08-30 08:53:49 +02:00
|
|
|
float currentDistance = 0;
|
2023-08-13 21:18:46 +02:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
result.Add(currentCell);
|
|
|
|
GetHexCenter(currentCell);
|
2023-11-05 21:46:28 +01:00
|
|
|
var currentPointLocal = currentPointPlane - GetHexCenter(currentCell);
|
2023-08-13 21:18:46 +02:00
|
|
|
|
|
|
|
int neighbourIndex;
|
|
|
|
float boundaryPlaneDistance;
|
|
|
|
currentCell.QueryClosestCellBoundary(currentPointLocal, direction, out neighbourIndex,
|
|
|
|
out boundaryPlaneDistance);
|
2023-11-05 21:46:28 +01:00
|
|
|
|
2023-08-13 21:18:46 +02:00
|
|
|
currentCell = currentCell.GetAdjacent(HexCell.NeighborDirections[neighbourIndex]);
|
2023-08-30 08:53:49 +02:00
|
|
|
currentDistance += boundaryPlaneDistance * 1.001f;
|
|
|
|
currentPointPlane = fromPlane + direction * boundaryPlaneDistance;
|
|
|
|
} while (currentDistance < distance);
|
2023-08-13 21:18:46 +02:00
|
|
|
|
|
|
|
result.Add(currentCell);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2022-12-02 21:09:40 +01:00
|
|
|
}
|