Drafted Vision blocking functionality and respective MapRef->HelperFunctions
This commit is contained in:
		@@ -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,33 @@ 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::LerpAxial(int32 a, int32 b, int32 t)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
						return float(a + (b - a) * t);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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 +171,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)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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 LerpAxial(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);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user