unreal/AdventureMap.cpp

221 lines
5.1 KiB
C++
Raw Normal View History

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"
#include "Algo/Reverse.h"
2022-01-11 11:07:17 +01:00
// Sets default values
AAdventureMap::AAdventureMap()
{
NBVectors.Add(NNE);
NBVectors.Add(E);
NBVectors.Add(SSE);
NBVectors.Add(SSW);
NBVectors.Add(W);
NBVectors.Add(NNW);
NBVectorsDiag.Add(N);
NBVectorsDiag.Add(ENE);
NBVectorsDiag.Add(ESE);
NBVectorsDiag.Add(S);
NBVectorsDiag.Add(WSW);
NBVectorsDiag.Add(WNW);
2022-01-11 11:07:17 +01:00
}
// Called when the game starts or when spawned
void AAdventureMap::BeginPlay()
{
Super::BeginPlay();
World = GetWorld();
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;
for (int r = 1; r <= GridSize; r++) {
2022-01-11 11:07:17 +01:00
float XOffset = 0.f;
if (r % 2 != 0) {
if (r > 1) {
QOffset--;
}
2022-01-11 11:07:17 +01:00
}
else { XOffset = HexWidth / 2; }
2022-01-11 11:07:17 +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);
Tile->Q = q - 1 + QOffset;
Tile->R = r - 1;
Grid.Add(Tile);
}
}
for (auto& tile : Grid) {
2022-01-11 11:07:17 +01:00
tile->Index = GridIndex(tile->Q, tile->R);
}
bHexGridReady = true;
2022-01-11 11:07:17 +01:00
}
// 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;
}
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);
return Grid[RandHex];
}
2022-01-13 16:34:06 +01:00
/*
* Add two TArray<TPair<int32, int32>> members containing the Cardinal Directions
(one for immediate neighbors, one for diagonals)
{ fill them in AAdventureMap::AAdventureMap }
* This function instead returns
TMap<bool bDiag, AHexTile* Neighbor>
*/
2022-01-13 16:34:06 +01:00
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
{
TArray<AHexTile*> Results;
2022-01-18 17:01:31 +01:00
TArray<int32> Indeces;
for (auto& Vec : NBVectors) {
Indeces.Add(GridIndex(OfHex->Q + Vec.Key, OfHex->R + Vec.Value));
}
for (auto& Ind : Indeces) {
if (Grid.IsValidIndex(Ind)) {
if (OfHex->Distance(Grid[Ind]) == 1) {
Results.Add(Grid[Ind]);
}
}
2022-01-26 16:48:58 +01:00
}
return Results;
}
2022-01-26 16:48:58 +01:00
TArray<AHexTile*> AAdventureMap::Diagonals(AHexTile* OfHex)
{
TArray<AHexTile*> Results;
TArray<int32> Indeces;
for (auto& Vec : NBVectorsDiag) {
Indeces.Add(GridIndex(OfHex->Q + Vec.Key, OfHex->R + Vec.Value));
}
for (auto& Ind : Indeces) {
if (Grid.IsValidIndex(Ind)) {
if (OfHex->Distance(Grid[Ind]) == 2) {
Results.Add(Grid[Ind]);
}
}
}
return Results;
2022-01-13 16:34:06 +01:00
}
TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
{
TArray<AHexTile*> Results;
TArray<AHexTile*> ToExamine;
TSet<AHexTile*> Processed;
2022-01-26 16:48:58 +01:00
Results.Add(Start);
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-26 16:48:58 +01:00
ToExamine.Add(Neighbor);
Results.Add(Neighbor);
}
}
return Results;
}
2022-01-26 16:48:58 +01:00
TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
{
TArray<AHexTile*> ToExamine;
TSet<AHexTile*> Processed;
ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0];
2022-01-26 16:48:58 +01:00
ToExamine.Remove(Candidate);
// estimate closest known Hex to Goal
for (auto& t : ToExamine) {
2022-01-26 16:48:58 +01:00
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)) {
if (Neighbor->Distance(Candidate) > 1) { continue; }
if (!(Neighbor->bFree)) { continue; }
if (Processed.Contains(Neighbor)) { continue; }
bool bInToExamine = ToExamine.Contains(Neighbor);
int32 NewGCost = Candidate->GCost + Neighbor->MoveCost;
if (NewGCost < Neighbor->GCost || !bInToExamine) {
Neighbor->GCost = NewGCost;
Neighbor->CameFrom = Candidate; // chain
if (!bInToExamine) {
Neighbor->HCost = Neighbor->Distance(Goal);
ToExamine.Add(Neighbor);
}
}
}
}
return LinkPath(Start, Goal);
}
TArray<AHexTile*> AAdventureMap::LinkPath(AHexTile* Start, AHexTile* Goal)
{
TArray<AHexTile*> Path;
if (!IsValid(Goal->CameFrom)) { return Path; }
AHexTile* iPathNode = Goal;
while (iPathNode != Start) {
Path.Emplace(iPathNode);
iPathNode = iPathNode->CameFrom;
}
Algo::Reverse(Path);
return Path;
}