diff --git a/AdventureMap.cpp b/AdventureMap.cpp index 7a4cc32..7759544 100644 --- a/AdventureMap.cpp +++ b/AdventureMap.cpp @@ -8,6 +8,7 @@ #include "Kismet/GameplayStatics.h" #include "Algo/Reverse.h" #include "Util/IndexPriorityQueue.h" +#include // Sets default values AAdventureMap::AAdventureMap() @@ -83,6 +84,33 @@ AHexTile* AAdventureMap::RandomHex() 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::LerpAxial(int32 a, int32 b, int32 t) +{ + return float(a + (b - a) * t); +} + TArray AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false) { TArray Results; @@ -143,6 +171,8 @@ TSet AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius) return Results; } + + /* // Faulty implementation which uses an actual PriorityQueue TArray AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags) { diff --git a/AdventureMap.h b/AdventureMap.h index b7c2685..45cd8a3 100644 --- a/AdventureMap.h +++ b/AdventureMap.h @@ -80,11 +80,16 @@ public: UFUNCTION(BlueprintCallable, Category = "Utility") bool DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec); - UFUNCTION(BlueprintCallable, Category = "Runtime") + UFUNCTION(BlueprintCallable, Category = "Utility") + float LerpAxial(int32 a, int32 b, int32 t); + UFUNCTION(BlueprintCallable, Category = "Utility") + FHexVector AxialRound(float q, float r) + + UFUNCTION(BlueprintCallable, Category = "Utility") TArray Neighbors(AHexTile* OfHex, bool bFreeOnly); - UFUNCTION(BlueprintCallable, Category = "Runtime") + UFUNCTION(BlueprintCallable, Category = "Utility") TArray FreeDiagonals(AHexTile* OfHex); - UFUNCTION(BlueprintCallable, Category = "Runtime") + UFUNCTION(BlueprintCallable, Category = "Utility") TSet BreadthFirstSearch(AHexTile* Start, int32 Radius); UFUNCTION(BlueprintCallable, Category = "Runtime") TArray FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags); diff --git a/AdventurePlayerController.cpp b/AdventurePlayerController.cpp index d085210..d3b4e83 100644 --- a/AdventurePlayerController.cpp +++ b/AdventurePlayerController.cpp @@ -55,15 +55,30 @@ void AAdventurePlayerController::LeftClick() else { return; } } -TArray AAdventurePlayerController::Vision(int32 Radius) -{ - TArray Results; - TSet Visible; - Visible = MapRef->BreadthFirstSearch(CurrentHex, Radius); - for (auto& Hex : Visible) { - if (ExploredHexes.Contains(Hex)) { continue; } - Results.Add(Hex); - ExploredHexes.Add(Hex); +TSet AAdventurePlayerController::Vision(int32 Radius) +{ + TSet InRange = MapRef->BreadthFirstSearch(CurrentHex, Radius); + TSet Results; + + for (auto& Hex : InRange) { + if (Hex->Distance(CurrentHex) == Radius) + { + 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); + + if (!SampleHex->bFree || ExploredHexes.Contains(SampleHex)) + { break; } + } + } } return Results; } diff --git a/AdventurePlayerController.h b/AdventurePlayerController.h index a1bb6b4..8ac4964 100644 --- a/AdventurePlayerController.h +++ b/AdventurePlayerController.h @@ -57,7 +57,7 @@ public: UPROPERTY(EditAnywhere, BlueprintReadWrite) int32 VisionRadius = 6; UFUNCTION(BlueprintCallable) - TArray Vision(int32 Radius); + TSet Vision(int32 Radius); UPROPERTY(EditAnywhere, BlueprintReadWrite) TSet ExploredHexes;