171 lines
4.3 KiB
C++
171 lines
4.3 KiB
C++
// Fill out your copyright notice in the Description page of Project Settings.
|
|
|
|
|
|
#include "AdventureMap.h"
|
|
#include "HexTile.h"
|
|
#include "AdventureCameraPawn.h"
|
|
#include "AdventureCharacter.h"
|
|
#include "Kismet/GameplayStatics.h"
|
|
#include "Algo/Reverse.h"
|
|
#include <unordered_map>
|
|
|
|
// Sets default values
|
|
AAdventureMap::AAdventureMap()
|
|
{
|
|
}
|
|
|
|
// 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);
|
|
|
|
Tile->Q = q - 1 + QOffset;
|
|
Tile->R = r - 1;
|
|
|
|
Grid.Add(Tile);
|
|
}
|
|
}
|
|
|
|
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 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)
|
|
{
|
|
TArray<AHexTile*> Neighbors;
|
|
int32 Arr[] = { GridIndex(OfHex->Q+1, OfHex->R), GridIndex(OfHex->Q+1, OfHex->R-1),
|
|
GridIndex(OfHex->Q, OfHex->R-1), GridIndex(OfHex->Q-1, OfHex->R),
|
|
GridIndex(OfHex->Q-1, OfHex->R+1), GridIndex(OfHex->Q, OfHex->R+1) };
|
|
TArray<int32> Indeces;
|
|
Indeces.Append(Arr, UE_ARRAY_COUNT(Arr));
|
|
|
|
Neighbors.Add(RandomHex());
|
|
for (auto& I : Indeces) { if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } }
|
|
return Neighbors;
|
|
}
|
|
|
|
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
|
{
|
|
TArray<AHexTile*> PQ; // Makeshift Priority Queue. (High value = Low priority)
|
|
PQ.Add(Start);
|
|
|
|
// The goal of this loop is to edit the Hex->CameFrom pointers, So as to chain Hexes from Goal to Start
|
|
while (!PQ.IsEmpty())
|
|
{
|
|
AHexTile* Current = PQ[0];
|
|
PQ.RemoveAt(0);
|
|
// UE_LOG(LogTemp, Warning, TEXT("Popping top priority Hex %d:%d"), Current->Q, Current->R); // debug
|
|
|
|
if (Current == Goal) // early exit
|
|
{
|
|
// UE_LOG(LogTemp, Warning, TEXT("Goal found!!!")); // debug
|
|
break;
|
|
}
|
|
|
|
// UE_LOG(LogTemp, Warning, TEXT("Expanding the frontier...")); // debug
|
|
for (AHexTile* Next : Neighbors(Current))
|
|
{
|
|
int32 NewCost = Current->CostSoFar + Next->MoveCost;
|
|
if (NewCost < Next->CostSoFar || !PQ.Contains(Next))
|
|
{
|
|
Next->CostSoFar = NewCost;
|
|
Next->CameFrom = Current;
|
|
PQ.Remove(Next);
|
|
int32 NewPrio = NewCost + Next->Distance(Goal); // Higher value = Lower priority
|
|
|
|
// no crash up to this point
|
|
|
|
// UE_LOG(LogTemp, Warning, TEXT("Hex %d:%d path info updated. Readjusting priorities..."), Next->Q, Next->R); // debug
|
|
if (!PQ.IsEmpty())
|
|
{
|
|
int32 OldPrio;
|
|
int32 OldIndex = PQ.Num() - 1; // default to lowest priority
|
|
|
|
for (auto& Hex : PQ)
|
|
{
|
|
OldPrio = Hex->CostSoFar + Hex->Distance(Goal);
|
|
if (NewPrio <= OldPrio) // looking for 1. Hex in "PQ" with a lower priority than that of "Next"
|
|
{
|
|
PQ.Find(Hex, OldIndex); // redefine OldIndex
|
|
break;
|
|
}
|
|
}
|
|
// UE_LOG(LogTemp, Warning, TEXT("Saving at PQ index %d"), OldIndex); // debug
|
|
PQ.Insert(Next, OldIndex);
|
|
}
|
|
else
|
|
{
|
|
PQ.Add(Next);
|
|
// UE_LOG(LogTemp, Warning, TEXT("Adding (PQ was empty)")); // debug
|
|
}
|
|
}
|
|
}
|
|
}
|
|
TArray<AHexTile*> Path;
|
|
/*
|
|
AHexTile* Hex = Goal;
|
|
while (Hex != Start)
|
|
{
|
|
Path.Emplace(Hex);
|
|
Hex = Hex->CameFrom;
|
|
}
|
|
Algo::Reverse(Path);
|
|
*/
|
|
return Path;
|
|
} |