pathfinding now working for distance 2 or less
This commit is contained in:
parent
32f4c6a278
commit
6c1c0579cb
127
AdventureMap.cpp
127
AdventureMap.cpp
@ -7,6 +7,7 @@
|
|||||||
#include "AdventureCharacter.h"
|
#include "AdventureCharacter.h"
|
||||||
#include "Kismet/GameplayStatics.h"
|
#include "Kismet/GameplayStatics.h"
|
||||||
#include "Algo/Reverse.h"
|
#include "Algo/Reverse.h"
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
// Sets default values
|
// Sets default values
|
||||||
AAdventureMap::AAdventureMap()
|
AAdventureMap::AAdventureMap()
|
||||||
@ -17,7 +18,7 @@ AAdventureMap::AAdventureMap()
|
|||||||
void AAdventureMap::BeginPlay()
|
void AAdventureMap::BeginPlay()
|
||||||
{
|
{
|
||||||
Super::BeginPlay();
|
Super::BeginPlay();
|
||||||
UWorld* World = GetWorld();
|
World = GetWorld();
|
||||||
if (IsValid(BaseTileClass))
|
if (IsValid(BaseTileClass))
|
||||||
{
|
{
|
||||||
MakeGrid();
|
MakeGrid();
|
||||||
@ -27,7 +28,6 @@ void AAdventureMap::BeginPlay()
|
|||||||
// Called once on Begin Play
|
// Called once on Begin Play
|
||||||
void AAdventureMap::MakeGrid()
|
void AAdventureMap::MakeGrid()
|
||||||
{
|
{
|
||||||
UWorld* World = GetWorld();
|
|
||||||
FVector NextHexAt = FVector();
|
FVector NextHexAt = FVector();
|
||||||
float HexWidth = sqrt(3) * TileSize;
|
float HexWidth = sqrt(3) * TileSize;
|
||||||
int QOffset = 0;
|
int QOffset = 0;
|
||||||
@ -112,20 +112,94 @@ TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
|
|||||||
return Neighbors;
|
return Neighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Be aware that the respective character will become relevant to this function at some point
|
// Massive memory leak when Goal is more than 2 distance away from Start (leading to the Editor freezing)
|
||||||
|
// Also freezes when the Goal is on the Edge of the map (i.e. has only 3 Neighbors)
|
||||||
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
||||||
{
|
{
|
||||||
TArray<AHexTile*> Priorities;
|
TArray<AHexTile*> PQ; // Makeshift Priority Queue. (High value = Low priority)
|
||||||
Priorities.Init(Start, 1);
|
PQ.Init(Start, 1);
|
||||||
|
|
||||||
// Editing Hex->CameFrom pointers, i.e. chaining Hexes
|
// The goal of this loop is to edit the Hex->CameFrom pointers, So as to chain Hexes from Goal to Start
|
||||||
while (!Priorities.IsEmpty())
|
while (!PQ.IsEmpty())
|
||||||
{
|
{
|
||||||
AHexTile* Current = Priorities[0];
|
AHexTile* Current = PQ[0];
|
||||||
Priorities.RemoveAt(0);
|
PQ.RemoveAt(0);
|
||||||
if (*Current == *Goal)
|
UE_LOG(LogTemp, Warning, TEXT("Popping top priority Hex Q:%d|R:%d"), Current->Q, Current->R);
|
||||||
|
|
||||||
|
if (Current == Goal) // early exit
|
||||||
{
|
{
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Goal found!")); // debug
|
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
|
||||||
|
|
||||||
|
UE_LOG(LogTemp, Warning, TEXT("Hex Q:%d|R:%d pathing updated. Readjusting priorities..."), Next->Q, Next->R); // debug
|
||||||
|
if (!PQ.IsEmpty())
|
||||||
|
{
|
||||||
|
int32 OldPrio;
|
||||||
|
int32 OldIndex = PQ.Num() - 1; // To be inserted as lowest priority by default
|
||||||
|
|
||||||
|
for (AHexTile* 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); // redefining of "OldIndex"
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
UE_LOG(LogTemp, Warning, TEXT("Inserting Hex at priority %d"), OldIndex); // debug
|
||||||
|
PQ.Insert(Next, OldIndex);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PQ.Add(Next);
|
||||||
|
UE_LOG(LogTemp, Warning, TEXT("Adding one last Candidate.")); // debug
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TArray<AHexTile*> Path;
|
||||||
|
AHexTile* Hex = Goal;
|
||||||
|
while (Hex != Start)
|
||||||
|
{
|
||||||
|
Path.Emplace(Hex);
|
||||||
|
Hex = Hex->CameFrom;
|
||||||
|
}
|
||||||
|
Algo::Reverse(Path);
|
||||||
|
return Path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
// alternative implementation using faster built in priority queue data structure
|
||||||
|
TArray<AHexTile*> AAdventureMap::FindPathAStarPQ(AHexTile* Start, AHexTile* Goal)
|
||||||
|
{
|
||||||
|
// TArray<AHexTile*> PQ;
|
||||||
|
std::priority_queue<AHexTile*, std::vector<AHexTile*>> PQ; // currently missing a custom comparator as third template arg (designating effective priority of pq elements)
|
||||||
|
std::unordered_map<AHexTile*, AHexTile*> CameFrom; // if in doubt: switch to templates instead of predefined hex types
|
||||||
|
// PQ.Init(Start, 1);
|
||||||
|
PQ.push(Start);
|
||||||
|
CameFrom[Start] = Start;
|
||||||
|
|
||||||
|
// while (!PQ.IsEmpty())
|
||||||
|
while (!PQ.empty())
|
||||||
|
{
|
||||||
|
AHexTile* Current = PQ.top();
|
||||||
|
// PQ.RemoveAt(0);
|
||||||
|
PQ.pop();
|
||||||
|
if (Current == Goal)
|
||||||
|
{
|
||||||
|
UE_LOG(LogTemp, Warning, TEXT("Goal found!")); // debug
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,56 +207,48 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
|||||||
for (AHexTile* Next : Neighbors(Current))
|
for (AHexTile* Next : Neighbors(Current))
|
||||||
{
|
{
|
||||||
int32 NewCost = Current->CostSoFar + Next->MoveCost;
|
int32 NewCost = Current->CostSoFar + Next->MoveCost;
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Cost calculated.")); // debug
|
// UE_LOG(LogTemp, Warning, TEXT("Cost calculated.")); // debug
|
||||||
|
|
||||||
if (!Priorities.Contains(Next) || NewCost < Next->CostSoFar)
|
if (!PQ.Contains(Next) || NewCost < Next->CostSoFar)
|
||||||
{
|
{
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("New candidate found.")); // debug
|
// UE_LOG(LogTemp, Warning, TEXT("New candidate found.")); // debug
|
||||||
|
|
||||||
Next->CostSoFar = NewCost;
|
Next->CostSoFar = NewCost;
|
||||||
int32 NewPrio = NewCost + Next->Distance(Goal);
|
int32 NewPrio = NewCost + Next->Distance(Goal);
|
||||||
|
|
||||||
// Adjust the Priority Queue
|
// Adjust the Priority Queue
|
||||||
if (Priorities.Contains(Next)) { Priorities.Remove(Next); }
|
if (PQ.Contains(Next)) { PQ.Remove(Next); }
|
||||||
for (AHexTile* Hex : Priorities) // at this point Priorities is empty, need to make sure it's not.
|
for (AHexTile* Hex : PQ) // at this point PQ is empty, need to make sure it's not.
|
||||||
{
|
{
|
||||||
int32 OldPrio = Hex->CostSoFar + Hex->Distance(Goal);
|
int32 OldPrio = Hex->CostSoFar + Hex->Distance(Goal);
|
||||||
int32 Index;
|
int32 Index;
|
||||||
Priorities.Find(Hex, Index);
|
PQ.Find(Hex, Index);
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Comparing priorities...")); // debug
|
// UE_LOG(LogTemp, Warning, TEXT("Comparing priorities...")); // debug
|
||||||
|
|
||||||
if (OldPrio > NewPrio)
|
if (OldPrio > NewPrio)
|
||||||
{
|
{
|
||||||
Priorities.Insert(Next, Index);
|
PQ.Insert(Next, Index);
|
||||||
Next->CameFrom = Current;
|
Next->CameFrom = Current;
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Looks promising!")); // debug
|
// UE_LOG(LogTemp, Warning, TEXT("Looks promising!")); // debug
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (Index == Priorities.Num() - 1 && OldPrio <= NewPrio)
|
|
||||||
|
if (Index == PQ.Num() - 1 && OldPrio <= NewPrio)
|
||||||
{
|
{
|
||||||
Priorities.Emplace(Next);
|
PQ.Emplace(Next);
|
||||||
Next->CameFrom = Current;
|
Next->CameFrom = Current;
|
||||||
|
|
||||||
// UE_LOG(LogTemp, Warning, TEXT("Low prio added")); // debug
|
// UE_LOG(LogTemp, Warning, TEXT("Low prio added")); // debug
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Priorities.IsEmpty())
|
|
||||||
{
|
if (PQ.IsEmpty()) { PQ.Emplace(Next); }
|
||||||
Priorities.Emplace(Next);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TArray<AHexTile*> Path;
|
TArray<AHexTile*> Path;
|
||||||
AHexTile* Hex = Goal;
|
AHexTile* Hex = Goal;
|
||||||
while (*Hex != *Start)
|
while (Hex != Start)
|
||||||
{
|
{
|
||||||
Path.Emplace(Hex);
|
Path.Emplace(Hex);
|
||||||
Hex = Hex->CameFrom;
|
Hex = Hex->CameFrom;
|
||||||
@ -191,3 +257,4 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
|||||||
Algo::Reverse(Path);
|
Algo::Reverse(Path);
|
||||||
return Path;
|
return Path;
|
||||||
}
|
}
|
||||||
|
*/
|
@ -26,6 +26,8 @@ public:
|
|||||||
int32 GridSize = 100; // squared is the number of Tiles
|
int32 GridSize = 100; // squared is the number of Tiles
|
||||||
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
|
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
|
||||||
int32 TileSize = 100;
|
int32 TileSize = 100;
|
||||||
|
UPROPERTY()
|
||||||
|
UWorld* World;
|
||||||
|
|
||||||
UFUNCTION(BlueprintCallable, Category = "Generation")
|
UFUNCTION(BlueprintCallable, Category = "Generation")
|
||||||
void MakeGrid();
|
void MakeGrid();
|
||||||
@ -42,6 +44,8 @@ public:
|
|||||||
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
|
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
|
||||||
UFUNCTION(BlueprintCallable, Category = "Runtime")
|
UFUNCTION(BlueprintCallable, Category = "Runtime")
|
||||||
TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal);
|
TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal);
|
||||||
|
//UFUNCTION(BlueprintCallable, Category = "Runtime")
|
||||||
|
// TArray<AHexTile*> FindPathAStarPQ(AHexTile* Start, AHexTile* Goal);
|
||||||
|
|
||||||
// Player spawn section
|
// Player spawn section
|
||||||
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
|
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
|
||||||
|
@ -26,7 +26,7 @@ void AAdventurePlayerController::SetupInputComponent()
|
|||||||
Super::SetupInputComponent();
|
Super::SetupInputComponent();
|
||||||
|
|
||||||
// This is initialized on startup, you can go straight to binding
|
// This is initialized on startup, you can go straight to binding
|
||||||
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::AdvClick);
|
// InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::AdvClick);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AAdventurePlayerController::AdvClick()
|
void AAdventurePlayerController::AdvClick()
|
||||||
@ -37,7 +37,7 @@ void AAdventurePlayerController::AdvClick()
|
|||||||
if (IsValid(Hit.GetActor()))
|
if (IsValid(Hit.GetActor()))
|
||||||
{
|
{
|
||||||
AHexTile* HitHex = (AHexTile*)Hit.GetActor();
|
AHexTile* HitHex = (AHexTile*)Hit.GetActor();
|
||||||
// MapRef->FindPathAStar(CurrentHex, HitHex); // this would currently cause a crash...
|
MapRef->FindPathAStar(CurrentHex, HitHex);
|
||||||
UE_LOG(LogTemp, Warning, TEXT("%d"), HitHex->Index);
|
// UE_LOG(LogTemp, Warning, TEXT("%d"), HitHex->Index);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user