263 lines
8.0 KiB
C++
263 lines
8.0 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "AdventureMap.h"
|
|
#include "HexTile.h"
|
|
#include "AdventurePlayerController.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Algo/Reverse.h"
|
|
|
|
// Sets default values
|
|
AAdventureMap::AAdventureMap()
|
|
{
|
|
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));
|
|
}
|
|
|
|
// Called when the game starts or when spawned
|
|
void AAdventureMap::BeginPlay()
|
|
{
|
|
Super::BeginPlay();
|
|
World = GetWorld();
|
|
|
|
if (IsValid(BaseTileClass)) {
|
|
MakeGrid();
|
|
}
|
|
}
|
|
|
|
// Called once on Begin Play
|
|
void AAdventureMap::MakeGrid()
|
|
{
|
|
FVector NextHexAt = FVector();
|
|
float HexWidth = sqrt(3) * TileSize;
|
|
int QOffset = 0;
|
|
|
|
for (int r = 1; r <= GridSize; r++) {
|
|
float XOffset = 0.f;
|
|
if (r % 2 != 0) { if (r > 1) { QOffset--; } }
|
|
else { XOffset = HexWidth / 2; }
|
|
|
|
for (int q = 1; q <= GridSize; q++) {
|
|
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);
|
|
Grid.Add(Tile);
|
|
Tile->Q = q - 1 + QOffset;
|
|
Tile->R = r - 1;
|
|
}
|
|
}
|
|
for (auto& tile : Grid) {
|
|
tile->Index = GridIndex(tile->Q, tile->R);
|
|
}
|
|
bHexGridReady = true;
|
|
}
|
|
|
|
// Every Hex Tile's index within the Grid Array can be derived from its Axial Q and R coordinates
|
|
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;
|
|
}
|
|
|
|
AHexTile* AAdventureMap::RandomHex()
|
|
{
|
|
int32 RandHex = FMath::RandRange(0, GridSize*GridSize-1);
|
|
return Grid[RandHex];
|
|
}
|
|
|
|
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
|
|
{
|
|
TArray<AHexTile*> Results;
|
|
for (auto& V : NeighborUnitVectors) {
|
|
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
|
|
if (Grid.IsValidIndex(I)) {
|
|
AHexTile* H = Grid[I];
|
|
if (bFreeOnly && !H->bFree) { continue; }
|
|
if (H->Distance(OfHex) == 1) { Results.Add(H); }
|
|
}
|
|
}
|
|
return Results;
|
|
}
|
|
|
|
TArray<AHexTile*> AAdventureMap::FreeDiagonals(AHexTile* OfHex)
|
|
{
|
|
TArray<AHexTile*> Results;
|
|
for (auto& V : DiagonalUnitVectors) {
|
|
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
|
|
if (!Grid.IsValidIndex(I)) { continue; }
|
|
else {
|
|
bool bReachable = true;
|
|
for (AHexTile* PotentialBlock : Neighbors(OfHex)) {
|
|
if (PotentialBlock->Distance(Grid[I]) != 1) { continue; }
|
|
if (!PotentialBlock->bFree) {
|
|
bReachable = false;
|
|
break;
|
|
}
|
|
}
|
|
if (bReachable) { Results.Add(Grid[I]); }
|
|
}
|
|
}
|
|
return Results;
|
|
}
|
|
|
|
TSet<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
|
|
{
|
|
TSet<AHexTile*> Results;
|
|
TArray<AHexTile*> ToExamine;
|
|
TSet<AHexTile*> Processed;
|
|
Results.Add(Start);
|
|
ToExamine.Add(Start);
|
|
|
|
while (!ToExamine.IsEmpty()) {
|
|
AHexTile* Candidate = ToExamine[0];
|
|
Processed.Add(Candidate);
|
|
ToExamine.Remove(Candidate);
|
|
|
|
for (AHexTile* Neighbor : Neighbors(Candidate)) {
|
|
if (Neighbor->Distance(Candidate) > 1) { continue; }
|
|
if (Processed.Contains(Neighbor)) { continue; }
|
|
if (Neighbor->Distance(Start) > Radius) { continue; }
|
|
|
|
ToExamine.Add(Neighbor);
|
|
Results.Add(Neighbor);
|
|
}
|
|
}
|
|
return Results;
|
|
}
|
|
|
|
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags)
|
|
{
|
|
TArray<AHexTile*> ToExamine;
|
|
TSet<AHexTile*> Processed;
|
|
ToExamine.Add(Start);
|
|
|
|
while (!ToExamine.IsEmpty()) {
|
|
AHexTile* Candidate = ToExamine[0];
|
|
ToExamine.Remove(Candidate);
|
|
// try for Hex with lower estimatet (F)cost
|
|
for (AHexTile* t : ToExamine) {
|
|
t->FCost = t->GCost + t->HCost;
|
|
if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; }
|
|
}
|
|
|
|
Processed.Add(Candidate);
|
|
// exit
|
|
if (Candidate == Goal) { break; }
|
|
|
|
// expand frontier & adjust path data
|
|
for (AHexTile* Neighbor : Neighbors(Candidate, true)) {
|
|
if (!Neighbor->bFree) { continue; }
|
|
if (Processed.Contains(Neighbor)) { continue; }
|
|
|
|
bool bInToExamine = ToExamine.Contains(Neighbor);
|
|
float NewGCost = Candidate->GCost + Neighbor->MoveCost;
|
|
|
|
if (NewGCost < Neighbor->GCost || !bInToExamine) {
|
|
Neighbor->GCost = NewGCost;
|
|
Neighbor->CameFrom = Candidate; // chain
|
|
Neighbor->bDiagMove = false;
|
|
|
|
if (!bInToExamine) {
|
|
Neighbor->HCost = Neighbor->Distance(Goal);
|
|
ToExamine.Add(Neighbor);
|
|
} }
|
|
}
|
|
}
|
|
TArray<AHexTile*> Path;
|
|
if (!IsValid(Goal->CameFrom)) { return Path; }
|
|
AHexTile* iPathNode = Goal;
|
|
while (iPathNode != Start) {
|
|
Path.Emplace(iPathNode);
|
|
iPathNode = iPathNode->CameFrom;
|
|
}
|
|
Algo::Reverse(Path);
|
|
|
|
if (bDiags) {
|
|
TArray<AHexTile*> PathS = ShortcutAStar(Path);
|
|
}
|
|
|
|
return Path;
|
|
}
|
|
|
|
TArray<AHexTile*> AAdventureMap::ShortcutAStar(TArray<AHexTile*> Path)
|
|
{
|
|
TArray<AHexTile*> Shortcut;
|
|
|
|
int32 Len = Path.Num();
|
|
TArray<AHexTile*> WorkingSegment;
|
|
AHexTile* Start = PCRef->CurrentHex;
|
|
WorkingSegment.Add(Start);
|
|
int32 i = 0;
|
|
|
|
FHexVector PrevDir = FHexVector(Path[0], Start);
|
|
FHexVector DirA;
|
|
int32 HexesBeforeBend = 1;
|
|
for (i; i < Len-1; i++) {
|
|
WorkingSegment.Add(Path[i]);
|
|
DirA = FHexVector(Path[i+1], Path[i]);
|
|
if (DirA != PrevDir) { break; }
|
|
HexesBeforeBend++;
|
|
PrevDir = DirA;
|
|
}
|
|
PrevDir = DirA;
|
|
FHexVector DirB;
|
|
int32 HexesAfterBend = 0;
|
|
for (i; i < Len - 1; i++) {
|
|
WorkingSegment.Add(Path[i+1]);
|
|
DirB = FHexVector(Path[i+1], Path[i]);
|
|
if (DirB != PrevDir) { break; }
|
|
HexesAfterBend++;
|
|
PrevDir = DirB;
|
|
}
|
|
if (HexesAfterBend == 0) { return Path; }
|
|
|
|
// debug
|
|
UE_LOG(LogTemp, Warning, TEXT("Before bend: %d"), HexesBeforeBend);
|
|
UE_LOG(LogTemp, Warning, TEXT("After bend: %d"), HexesAfterBend);
|
|
UE_LOG(LogTemp, Warning, TEXT("Working segment length: %d"), WorkingSegment.Num());
|
|
for (AHexTile* HexDebug : WorkingSegment) { UE_LOG(LogTemp, Warning, TEXT("HexID: %d"), HexDebug->Index); }
|
|
|
|
|
|
FHexVector UnitDiag = UnitDiagFromUnitNB(DirA, DirB);
|
|
AHexTile* Milestone = WorkingSegment.Last();
|
|
|
|
// Try adding diagonal Hexes to Shortcut from Start until Milestone (if BeforeBend>=AferBend: do this AfterBend times, else: BeforeBend)
|
|
// Set a bool to 'true' as soon as the first Hex is added; Set CurrentHex to that;
|
|
// if the path is blocked and the bool is 'false': Set CurrentHex to the next in WorkingSegment; Retry.
|
|
// if the path is blocked and the bool is 'true': Append the rest recursively calling A*(Shortcut.Last(), Path.Last(), true);
|
|
// when iterated AfterBend (or BeforeBend) times: Append the rest recursively calling A*(Shortcut.Last(), Path.Last(), true);
|
|
|
|
return Shortcut;
|
|
}
|
|
|
|
|
|
FHexVector AAdventureMap::UnitDiagFromUnitNB(FHexVector InVecA, FHexVector InVecB) {
|
|
if (InVecA == NNW && InVecB == NNE||InVecB == NNW && InVecA == NNE) { return N; }
|
|
if (InVecA == NNE && InVecB == E ||InVecB == NNE && InVecA == E) { return ENE; }
|
|
if (InVecA == E && InVecB == SSE||InVecB == E && InVecA == SSE) { return ESE; }
|
|
if (InVecA == SSE && InVecB == SSW||InVecB == SSE && InVecA == SSW) { return S; }
|
|
if (InVecA == SSW && InVecB == W ||InVecB == SSW && InVecA == W) { return WSW; }
|
|
if (InVecA == W && InVecB == NNW||InVecB == W && InVecA == NNW) { return WNW; }
|
|
return FHexVector();
|
|
}
|
|
bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec) {
|
|
FHexVector BlockA;
|
|
FHexVector BlockB;
|
|
if (InDiagUnitVec == N) { BlockA = NNW, BlockB = NNE; }
|
|
if (InDiagUnitVec == ENE) { BlockA = NNE, BlockB = E; }
|
|
if (InDiagUnitVec == ESE) { BlockA = E, BlockB = SSE; }
|
|
if (InDiagUnitVec == S) { BlockA = SSE, BlockB = SSW; }
|
|
if (InDiagUnitVec == WSW) { BlockA = SSW, BlockB = W; }
|
|
if (InDiagUnitVec == WNW) { BlockA = W, BlockB = NNW; }
|
|
AHexTile* HexA = Grid[GridIndex(InStart->Q + BlockA.Q, InStart->R + BlockA.R)];
|
|
AHexTile* HexB = Grid[GridIndex(InStart->Q + BlockB.Q, InStart->R + BlockB.R)];
|
|
return (HexA->bFree && HexB->bFree);
|
|
} |