Drafted Vision blocking functionality and respective MapRef->HelperFunctions

This commit is contained in:
Maximilian Fajnberg 2022-06-18 01:20:01 +02:00
parent 18af6a56be
commit 068beeacfd
4 changed files with 66 additions and 13 deletions

View File

@ -8,6 +8,7 @@
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Algo/Reverse.h" #include "Algo/Reverse.h"
#include "Util/IndexPriorityQueue.h" #include "Util/IndexPriorityQueue.h"
#include <cmath.h>
// Sets default values // Sets default values
AAdventureMap::AAdventureMap() AAdventureMap::AAdventureMap()
@ -83,6 +84,36 @@ AHexTile* AAdventureMap::RandomHex()
return Grid[RandHex]; return Grid[RandHex];
} }
FHexVector AAdventureMap::AxialRound(float qf, float rf)
{
float sf = -qf - rf;
int32 q = round(qf);
int32 r = round(rf);
int32 s = round(sf);
float q_diff = abs(q - qf);
float r_diff = abs(r - rf);
float s_diff = abs(s - sf);
if (q_diff > r_diff && q_diff > s_diff)
{ q = -r - s; }
else if (r_diff > s_diff)
{ r = -q - s; }
else
{ s = -q - r; }
return FHexVector(q, r);
}
float AAdventureMap::Lerp(int32 a, int32 b, int32 t)
{
return float(a + (b - a) * t);
}
// FHexVector SampleHex = MapRef->AxialRound(Lerpl(a, b, t), Lerp(x, y, z));
//
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false) TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
{ {
TArray<AHexTile*> Results; TArray<AHexTile*> Results;
@ -143,6 +174,8 @@ TSet<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
return Results; return Results;
} }
/* // Faulty implementation which uses an actual PriorityQueue /* // Faulty implementation which uses an actual PriorityQueue
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags) TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags)
{ {

View File

@ -80,11 +80,16 @@ public:
UFUNCTION(BlueprintCallable, Category = "Utility") UFUNCTION(BlueprintCallable, Category = "Utility")
bool DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec); bool DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Utility")
float Lerp(int32 a, int32 b, int32 t);
UFUNCTION(BlueprintCallable, Category = "Utility")
FHexVector AxialRound(float q, float r)
UFUNCTION(BlueprintCallable, Category = "Utility")
TArray<AHexTile*> Neighbors(AHexTile* OfHex, bool bFreeOnly); TArray<AHexTile*> Neighbors(AHexTile* OfHex, bool bFreeOnly);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Utility")
TArray<AHexTile*> FreeDiagonals(AHexTile* OfHex); TArray<AHexTile*> FreeDiagonals(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Utility")
TSet<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius); TSet<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags); TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags);

View File

@ -55,15 +55,30 @@ void AAdventurePlayerController::LeftClick()
else { return; } else { return; }
} }
TArray<AHexTile*> AAdventurePlayerController::Vision(int32 Radius) TSet<AHexTile*> AAdventurePlayerController::Vision(int32 Radius)
{ {
TArray<AHexTile*> Results; TSet<AHexTile*> InRange = MapRef->BreadthFirstSearch(CurrentHex, Radius);
TSet<AHexTile*> Visible; TSet<AHexTile*> Results;
Visible = MapRef->BreadthFirstSearch(CurrentHex, Radius);
for (auto& Hex : Visible) { for (auto& Hex : InRange) {
if (ExploredHexes.Contains(Hex)) { continue; } if (Hex->Distance(CurrentHex) == Radius)
Results.Add(Hex); {
AHexTile* Target;
for (int32 i = 1; i <= Radius; i++)
{
float t = 1.0f / Radius * i;
FHexVector Sample;
Sample = MapRef->AxialRound(MapRef->Lerp(CurrentHex->Q, Hex->Q, t), MapRef->Lerp(CurrentHex->R, Hex->R, t));
AHexTile* SampleHex = MapRef->Grid[MapRef->GridIndex(Sample.Q, Sample.R)];
Results.Add(SampleHex);
ExploredHexes.Add(Hex); ExploredHexes.Add(Hex);
if (!SampleHex->bFree || ExploredHexes.Contains(SampleHex))
{ break; }
}
}
} }
return Results; return Results;
} }

View File

@ -57,7 +57,7 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
int32 VisionRadius = 6; int32 VisionRadius = 6;
UFUNCTION(BlueprintCallable) UFUNCTION(BlueprintCallable)
TArray<AHexTile*> Vision(int32 Radius); TSet<AHexTile*> Vision(int32 Radius);
UPROPERTY(EditAnywhere, BlueprintReadWrite) UPROPERTY(EditAnywhere, BlueprintReadWrite)
TSet<AHexTile*> ExploredHexes; TSet<AHexTile*> ExploredHexes;