2022-01-11 11:07:17 +01:00
|
|
|
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
|
|
|
|
|
|
#include "AdventureMap.h"
|
|
|
|
#include "HexTile.h"
|
2022-01-26 16:48:58 +01:00
|
|
|
#include "AdventurePlayerController.h"
|
2022-01-11 11:07:17 +01:00
|
|
|
#include "Kismet/GameplayStatics.h"
|
2022-01-16 18:33:43 +01:00
|
|
|
#include "Algo/Reverse.h"
|
2022-01-11 11:07:17 +01:00
|
|
|
|
|
|
|
// Sets default values
|
|
|
|
AAdventureMap::AAdventureMap()
|
|
|
|
{
|
2022-01-28 21:58:04 +01:00
|
|
|
FHexVector NBs[] = { NNE, E, SSE, SSW, W, NNW };
|
|
|
|
NeighborUnitVectors.Append(NBs, UE_ARRAY_COUNT(NBs));
|
|
|
|
FHexVector DNBs[] = { N, ENE, ESE, S, WSW, WNW };
|
|
|
|
DiagonalUnitVectors.Append(DNBs, UE_ARRAY_COUNT(DNBs));
|
2022-01-11 11:07:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the game starts or when spawned
|
|
|
|
void AAdventureMap::BeginPlay()
|
|
|
|
{
|
|
|
|
Super::BeginPlay();
|
2022-01-17 17:53:54 +01:00
|
|
|
World = GetWorld();
|
2022-01-26 18:09:39 +01:00
|
|
|
|
|
|
|
if (IsValid(BaseTileClass)) {
|
2022-01-11 11:07:17 +01:00
|
|
|
MakeGrid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called once on Begin Play
|
|
|
|
void AAdventureMap::MakeGrid()
|
|
|
|
{
|
|
|
|
FVector NextHexAt = FVector();
|
|
|
|
float HexWidth = sqrt(3) * TileSize;
|
|
|
|
int QOffset = 0;
|
|
|
|
|
2022-01-18 20:09:37 +01:00
|
|
|
for (int r = 1; r <= GridSize; r++) {
|
2022-01-11 11:07:17 +01:00
|
|
|
float XOffset = 0.f;
|
2022-01-28 21:58:04 +01:00
|
|
|
if (r % 2 != 0) { if (r > 1) { QOffset--; } }
|
2022-01-16 18:33:43 +01:00
|
|
|
else { XOffset = HexWidth / 2; }
|
2022-01-11 11:07:17 +01:00
|
|
|
|
2022-01-28 21:58:04 +01:00
|
|
|
for (int q = 1; q <= GridSize; q++) {
|
2022-01-11 11:07:17 +01:00
|
|
|
NextHexAt.X = XOffset + (HexWidth * q);
|
|
|
|
NextHexAt.Y = TileSize * 1.5f * r;
|
|
|
|
NextHexAt.Z = 0.f;
|
|
|
|
FTransform SpawnTransform = FTransform(NextHexAt);
|
|
|
|
AHexTile* Tile = World->SpawnActor<AHexTile>(BaseTileClass, SpawnTransform);
|
2022-01-28 21:58:04 +01:00
|
|
|
Grid.Add(Tile);
|
2022-01-11 11:07:17 +01:00
|
|
|
Tile->Q = q - 1 + QOffset;
|
|
|
|
Tile->R = r - 1;
|
|
|
|
}
|
|
|
|
}
|
2022-01-18 20:09:37 +01:00
|
|
|
for (auto& tile : Grid) {
|
2022-01-11 11:07:17 +01:00
|
|
|
tile->Index = GridIndex(tile->Q, tile->R);
|
|
|
|
}
|
2022-01-11 18:16:26 +01:00
|
|
|
bHexGridReady = true;
|
2022-01-11 11:07:17 +01:00
|
|
|
}
|
|
|
|
|
2022-01-29 20:54:35 +01:00
|
|
|
// Every Hex Tile's index within the Grid Array can be derived from its Axial Q and R coordinates
|
2022-01-12 18:22:16 +01:00
|
|
|
int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The Q axis is (i.e. columns are) oriented diagonally.
|
|
|
|
* The Hex Grid has a rough square shape, hence the Q coordinates must be offset by -1 every other row.
|
|
|
|
*/
|
|
|
|
int32 column = qAxial + FMath::FloorToInt(rAxial / 2);
|
|
|
|
return (rAxial * GridSize) + column;
|
|
|
|
}
|
|
|
|
|
2022-01-11 11:07:17 +01:00
|
|
|
AHexTile* AAdventureMap::RandomHex()
|
|
|
|
{
|
2022-01-18 17:01:31 +01:00
|
|
|
int32 RandHex = FMath::RandRange(0, GridSize*GridSize-1);
|
2022-01-11 18:16:26 +01:00
|
|
|
return Grid[RandHex];
|
2022-01-12 18:22:16 +01:00
|
|
|
}
|
2022-01-13 16:34:06 +01:00
|
|
|
|
2022-01-28 21:58:04 +01:00
|
|
|
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
|
2022-01-13 16:34:06 +01:00
|
|
|
{
|
2022-01-26 18:09:39 +01:00
|
|
|
TArray<AHexTile*> Results;
|
2022-01-28 21:58:04 +01:00
|
|
|
for (auto& V : NeighborUnitVectors) {
|
|
|
|
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
|
|
|
|
if (Grid.IsValidIndex(I)) {
|
2022-01-29 20:54:35 +01:00
|
|
|
AHexTile* H = Grid[I];
|
|
|
|
if (bFreeOnly && !H->bFree) { continue; }
|
|
|
|
if (H->Distance(OfHex) == 1) { Results.Add(H); }
|
2022-01-28 21:58:04 +01:00
|
|
|
}
|
2022-01-26 18:09:39 +01:00
|
|
|
}
|
|
|
|
return Results;
|
|
|
|
}
|
2022-01-26 16:48:58 +01:00
|
|
|
|
2022-01-29 20:54:35 +01:00
|
|
|
TArray<AHexTile*> AAdventureMap::FreeDiagonals(AHexTile* OfHex)
|
2022-01-26 18:09:39 +01:00
|
|
|
{
|
|
|
|
TArray<AHexTile*> Results;
|
2022-01-28 21:58:04 +01:00
|
|
|
for (auto& V : DiagonalUnitVectors) {
|
|
|
|
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
|
|
|
|
if (!Grid.IsValidIndex(I)) { continue; }
|
|
|
|
else {
|
|
|
|
bool bReachable = true;
|
2022-01-29 20:54:35 +01:00
|
|
|
for (AHexTile* PotentialBlock : Neighbors(OfHex)) {
|
2022-01-28 21:58:04 +01:00
|
|
|
if (PotentialBlock->Distance(Grid[I]) != 1) { continue; }
|
|
|
|
if (!PotentialBlock->bFree) {
|
|
|
|
bReachable = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 20:54:35 +01:00
|
|
|
if (bReachable) { Results.Add(Grid[I]); }
|
2022-01-26 18:09:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Results;
|
2022-01-13 16:34:06 +01:00
|
|
|
}
|
2022-01-16 18:33:43 +01:00
|
|
|
|
2022-01-28 21:58:04 +01:00
|
|
|
TSet<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
|
2022-01-25 18:30:49 +01:00
|
|
|
{
|
2022-01-28 21:58:04 +01:00
|
|
|
TSet<AHexTile*> Results;
|
2022-01-25 18:30:49 +01:00
|
|
|
TArray<AHexTile*> ToExamine;
|
|
|
|
TSet<AHexTile*> Processed;
|
2022-01-26 16:48:58 +01:00
|
|
|
Results.Add(Start);
|
2022-01-25 18:30:49 +01:00
|
|
|
ToExamine.Add(Start);
|
|
|
|
|
|
|
|
while (!ToExamine.IsEmpty()) {
|
|
|
|
AHexTile* Candidate = ToExamine[0];
|
|
|
|
Processed.Add(Candidate);
|
|
|
|
ToExamine.Remove(Candidate);
|
|
|
|
|
|
|
|
for (AHexTile* Neighbor : Neighbors(Candidate)) {
|
2022-01-26 16:48:58 +01:00
|
|
|
if (Neighbor->Distance(Candidate) > 1) { continue; }
|
|
|
|
if (Processed.Contains(Neighbor)) { continue; }
|
|
|
|
if (Neighbor->Distance(Start) > Radius) { continue; }
|
2022-01-25 18:30:49 +01:00
|
|
|
|
2022-01-26 16:48:58 +01:00
|
|
|
ToExamine.Add(Neighbor);
|
|
|
|
Results.Add(Neighbor);
|
2022-01-25 18:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Results;
|
|
|
|
}
|
2022-01-26 16:48:58 +01:00
|
|
|
|
2022-01-28 21:58:04 +01:00
|
|
|
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags)
|
2022-01-16 18:33:43 +01:00
|
|
|
{
|
2022-01-18 20:09:37 +01:00
|
|
|
TArray<AHexTile*> ToExamine;
|
2022-01-25 18:30:49 +01:00
|
|
|
TSet<AHexTile*> Processed;
|
2022-01-18 20:09:37 +01:00
|
|
|
ToExamine.Add(Start);
|
|
|
|
|
2022-01-28 21:58:04 +01:00
|
|
|
while (!ToExamine.IsEmpty()) {
|
2022-01-18 20:09:37 +01:00
|
|
|
AHexTile* Candidate = ToExamine[0];
|
2022-01-26 16:48:58 +01:00
|
|
|
ToExamine.Remove(Candidate);
|
2022-01-28 21:58:04 +01:00
|
|
|
// try for Hex with lower estimatet (F)cost
|
2022-01-29 20:54:35 +01:00
|
|
|
for (AHexTile* t : ToExamine) {
|
2022-01-26 16:48:58 +01:00
|
|
|
t->FCost = t->GCost + t->HCost;
|
2022-01-28 21:58:04 +01:00
|
|
|
if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; }
|
2022-01-16 21:19:34 +01:00
|
|
|
}
|
2022-01-16 18:33:43 +01:00
|
|
|
|
2022-01-18 20:09:37 +01:00
|
|
|
Processed.Add(Candidate);
|
|
|
|
// exit
|
|
|
|
if (Candidate == Goal) { break; }
|
|
|
|
|
|
|
|
// expand frontier & adjust path data
|
2022-01-28 21:58:04 +01:00
|
|
|
for (AHexTile* Neighbor : Neighbors(Candidate, true)) {
|
2022-01-18 20:19:30 +01:00
|
|
|
if (Neighbor->Distance(Candidate) > 1) { continue; }
|
2022-01-29 20:54:35 +01:00
|
|
|
if (!Neighbor->bFree) { continue; }
|
2022-01-18 20:09:37 +01:00
|
|
|
if (Processed.Contains(Neighbor)) { continue; }
|
|
|
|
|
|
|
|
bool bInToExamine = ToExamine.Contains(Neighbor);
|
2022-01-28 21:58:04 +01:00
|
|
|
float NewGCost = Candidate->GCost + Neighbor->MoveCost * 10.f;
|
2022-01-18 20:09:37 +01:00
|
|
|
|
|
|
|
if (NewGCost < Neighbor->GCost || !bInToExamine) {
|
|
|
|
Neighbor->GCost = NewGCost;
|
|
|
|
Neighbor->CameFrom = Candidate; // chain
|
|
|
|
|
2022-01-28 21:58:04 +01:00
|
|
|
if (!bInToExamine) {
|
|
|
|
Neighbor->HCost = Neighbor->Distance(Goal) * 10.f;
|
2022-01-18 20:09:37 +01:00
|
|
|
ToExamine.Add(Neighbor);
|
2022-01-29 20:54:35 +01:00
|
|
|
} }
|
2022-01-28 21:58:04 +01:00
|
|
|
}
|
2022-01-29 20:54:35 +01:00
|
|
|
if (bDiags) {
|
|
|
|
for (AHexTile* Diag : FreeDiagonals(Candidate)) {
|
2022-01-28 21:58:04 +01:00
|
|
|
if (Diag->Distance(Candidate) > 2) { continue; }
|
|
|
|
if (!Diag->bFree) { continue; }
|
|
|
|
if (Processed.Contains(Diag)) { continue; }
|
|
|
|
|
|
|
|
bool bInToExamine = ToExamine.Contains(Diag);
|
2022-01-29 20:54:35 +01:00
|
|
|
float NewGCost = Candidate->GCost + 1 + Diag->MoveCost * 10.f;
|
2022-01-28 21:58:04 +01:00
|
|
|
|
|
|
|
if (NewGCost < Diag->GCost || !bInToExamine) {
|
|
|
|
Diag->GCost = NewGCost;
|
|
|
|
Diag->CameFrom = Candidate; // chain
|
|
|
|
|
|
|
|
if (!bInToExamine) {
|
2022-01-29 20:54:35 +01:00
|
|
|
Diag->HCost = Diag->Distance(Goal) * 10.f; // not accounting for diagonals
|
2022-01-28 21:58:04 +01:00
|
|
|
ToExamine.Add(Diag);
|
2022-01-29 20:54:35 +01:00
|
|
|
} }
|
2022-01-28 21:58:04 +01:00
|
|
|
}
|
|
|
|
}
|
2022-01-17 17:53:54 +01:00
|
|
|
}
|
|
|
|
TArray<AHexTile*> Path;
|
2022-01-24 21:26:06 +01:00
|
|
|
if (!IsValid(Goal->CameFrom)) { return Path; }
|
2022-01-18 20:09:37 +01:00
|
|
|
AHexTile* iPathNode = Goal;
|
|
|
|
while (iPathNode != Start) {
|
|
|
|
Path.Emplace(iPathNode);
|
|
|
|
iPathNode = iPathNode->CameFrom;
|
2022-01-17 17:53:54 +01:00
|
|
|
}
|
2022-01-18 20:19:30 +01:00
|
|
|
Algo::Reverse(Path);
|
2022-01-17 17:53:54 +01:00
|
|
|
return Path;
|
2022-01-17 17:58:30 +01:00
|
|
|
}
|