// 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" // Sets default values AAdventureMap::AAdventureMap() { } // Called when the game starts or when spawned void AAdventureMap::BeginPlay() { Super::BeginPlay(); UWorld* World = GetWorld(); if (IsValid(BaseTileClass)) { MakeGrid(); } } // Called once on Begin Play void AAdventureMap::MakeGrid() { UWorld* World = GetWorld(); 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(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 = GridIndex(FMath::RandRange(0, GridSize-1), FMath::RandRange(0, GridSize-1)); return Grid[RandHex]; } TArray AAdventureMap::Neighbors(AHexTile* OfHex) { TArray Neighbors; int32 I; I = GridIndex(OfHex->Q + 1 , OfHex->R + 0 ); if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } I = GridIndex(OfHex->Q + 1 , OfHex->R - 1 ); if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } I = GridIndex(OfHex->Q + 0 , OfHex->R - 1 ); if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } I = GridIndex(OfHex->Q - 1 , OfHex->R + 0 ); if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } I = GridIndex(OfHex->Q - 1 , OfHex->R + 1 ); if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } I = GridIndex(OfHex->Q + 0 , OfHex->R + 1 ); if (Grid.IsValidIndex(I) && OfHex->Distance(Grid[I]) == 1) { Neighbors.Add(Grid[I]); } return Neighbors; } // Be aware that the respective character will become relevant to this function at some point TArray AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal) { TArray Priorities; Priorities.Init(Start, 1); Goal->CameFrom = Start; // Editing Hex->CameFrom pointers, i.e. chaining Hexes while (Priorities.IsValidIndex(0)) { AHexTile* Current = Priorities[0]; Priorities.RemoveAt(0); if (*Current == *Goal) { break; } // Expanding the Frontier for (AHexTile* Next : Neighbors(Current)) { int32 NewCost = Current->CostSoFar + Next->MoveCost; if (!Priorities.Contains(Next) || NewCost < Next->CostSoFar) { Next->CostSoFar = NewCost; int32 NewPrio = NewCost + Next->Distance(Goal); // Adjust the Priority Queue if (Priorities.Contains(Next)) { Priorities.Remove(Next); } for (AHexTile* Hex : Priorities) { int32 OldPrio = Hex->CostSoFar + Hex->Distance(Goal); int32 Index; Priorities.Find(Hex, Index); if (OldPrio > NewPrio) { Priorities.Insert(Next, Index); Next->CameFrom = Current; break; } if (Index == Priorities.Num() - 1 && OldPrio <= NewPrio) { Priorities.Emplace(Next); Next->CameFrom = Current; } } } } } TArray Path; AHexTile* Hex = Goal; while (*Hex != *Start) { Path.Emplace(Hex); Hex = Hex->CameFrom; } Algo::Reverse(Path); return Path; // currently always length of 1 }