Breadth First Search; considering use of diagonals for A*PF

This commit is contained in:
Maximilian Fajnberg 2022-01-25 18:30:49 +01:00
parent 237c056b30
commit 986233d960
7 changed files with 71 additions and 26 deletions

View File

@ -35,7 +35,7 @@ AAdventureCameraPawn::AAdventureCameraPawn()
void AAdventureCameraPawn::BeginPlay() void AAdventureCameraPawn::BeginPlay()
{ {
Super::BeginPlay(); Super::BeginPlay();
ControllerRef = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0); OwningPC = (AAdventurePlayerController*)UGameplayStatics::GetPlayerController(GetWorld(), 0); // for now
// Viewport properties not accurate on BeginPlay (must wait before accessing) // Viewport properties not accurate on BeginPlay (must wait before accessing)
FTimerHandle UnusedHandle; FTimerHandle UnusedHandle;
@ -85,7 +85,7 @@ void AAdventureCameraPawn::EdgeScrollSide(float AxisValue)
{ {
float mouseX; float mouseX;
float mouseY; float mouseY;
ControllerRef->GetMousePosition(mouseX, mouseY); OwningPC->GetMousePosition(mouseX, mouseY);
float mousePosX = mouseX / ViewSize.X; float mousePosX = mouseX / ViewSize.X;
float mousePosY = mouseY / ViewSize.Y; float mousePosY = mouseY / ViewSize.Y;
float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75); float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75);
@ -133,7 +133,7 @@ void AAdventureCameraPawn::EdgeScrollVert(float AxisValue)
{ {
float mouseX; float mouseX;
float mouseY; float mouseY;
ControllerRef->GetMousePosition(mouseX, mouseY); OwningPC->GetMousePosition(mouseX, mouseY);
float mousePosY = mouseY / ViewSize.Y; float mousePosY = mouseY / ViewSize.Y;
float mousePosX = mouseX / ViewSize.X; float mousePosX = mouseX / ViewSize.X;
float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75); float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75);
@ -182,7 +182,7 @@ void AAdventureCameraPawn::FollowAdvPawn()
{ {
UE_LOG(LogTemp, Warning, TEXT("following...")); UE_LOG(LogTemp, Warning, TEXT("following..."));
AdvPawnLocationCurrent = AdvPawnRef->GetActorLocation(); AdvPawnLocationCurrent = FollowPawn->GetActorLocation();
AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious; AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious;
UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString()); UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString());

View File

@ -23,9 +23,9 @@ public:
UPROPERTY(BlueprintReadWrite, Category = "Config") UPROPERTY(BlueprintReadWrite, Category = "Config")
class AAdventureMap* AdvMapRef; class AAdventureMap* AdvMapRef;
UPROPERTY(BlueprintReadWrite, Category = "Config") UPROPERTY(BlueprintReadWrite, Category = "Config")
class AAdventurePlayerController* ControllerRef; class AAdventurePlayerController* OwningPC;
UPROPERTY(BlueprintReadWrite, Category = "Config") UPROPERTY(BlueprintReadWrite, Category = "Config")
class AAdventureCharacter* AdvPawnRef; class AAdventureCharacter* FollowPawn;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input")

View File

@ -20,10 +20,10 @@ public:
// Sets default values for this character's properties // Sets default values for this character's properties
AAdventureCharacter(); AAdventureCharacter();
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Config")
class AAdventureMap* MapRef;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
AHexTile* GridLocation; AHexTile* Location;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
AHexTile* SelectedHex;
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned

View File

@ -3,8 +3,6 @@
#include "AdventureMap.h" #include "AdventureMap.h"
#include "HexTile.h" #include "HexTile.h"
#include "AdventureCameraPawn.h"
#include "AdventureCharacter.h"
#include "Kismet/GameplayStatics.h" #include "Kismet/GameplayStatics.h"
#include "Algo/Reverse.h" #include "Algo/Reverse.h"
@ -94,10 +92,38 @@ TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
return Neighbors; return Neighbors;
} }
TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
{
TArray<AHexTile*> Results;
TArray<AHexTile*> ToExamine;
TSet<AHexTile*> Processed;
ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0];
Processed.Add(Candidate);
ToExamine.Remove(Candidate);
for (AHexTile* Neighbor : Neighbors(Candidate)) {
if (Neighbor->Distance(Candidate) > 1) { continue; }
if (Processed.Contains(Neighbor)) { continue; }
if (Neighbor->Distance(Start) <= Radius) {
ToExamine.Add(Neighbor);
Results.Add(Neighbor);
}
}
}
Results.Add(Start);
return Results;
}
TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal) TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
{ {
TArray<AHexTile*> ToExamine; TArray<AHexTile*> ToExamine;
TArray<AHexTile*> Processed; TSet<AHexTile*> Processed;
ToExamine.Add(Start); ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) { while (!ToExamine.IsEmpty()) {

View File

@ -7,7 +7,7 @@
#include "AdventureMap.generated.h" #include "AdventureMap.generated.h"
class AHexTile; class AHexTile;
class AAdventureCharacter; class AMapObject;
UCLASS() UCLASS()
class FRAY_API AAdventureMap : public AActor class FRAY_API AAdventureMap : public AActor
@ -33,26 +33,41 @@ public:
void MakeGrid(); void MakeGrid();
UPROPERTY(BlueprintReadOnly, Category = "Generation") UPROPERTY(BlueprintReadOnly, Category = "Generation")
bool bHexGridReady; bool bHexGridReady;
UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Generation") UPROPERTY(BlueprintReadOnly, Category = "Generation")
TArray<AHexTile*> Grid; TArray<AHexTile*> Grid;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
TMap<AHexTile*, AMapObject*> MapObjects;
UPROPERTY(BlueprintReadOnly)
TMap<int32, AMapObject*> MapObjectsByID;
int32 IncID = -1;
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
int32 GridIndex(int32 q, int32 r); int32 GridIndex(int32 q, int32 r);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
AHexTile* RandomHex(); AHexTile* RandomHex();
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Neighbors(AHexTile* OfHex); TArray<AHexTile*> Neighbors(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> AStar(AHexTile* Start, AHexTile* Goal); TArray<AHexTile*> AStar(AHexTile* Start, AHexTile* Goal);
UFUNCTION(BlueprintCallable, Category = "Runtime") UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> LinkPath(AHexTile* Start, AHexTile* Goal); TArray<AHexTile*> LinkPath(AHexTile* Start, AHexTile* Goal);
// Cardinal direction vectors
// Player spawn section TPair<int32, int32> N = TPair<int32,int32>(1, -2); //diag
UPROPERTY(VisibleAnywhere, BlueprintReadWrite) TPair<int32, int32> NE = TPair<int32,int32>(1, -1);
APawn* CameraPawn; TPair<int32, int32> ENE = TPair<int32,int32>(2, -1); //diag
UPROPERTY(VisibleAnywhere, BlueprintReadWrite) TPair<int32, int32> E = TPair<int32,int32>(1, 0);
AAdventureCharacter* PlayerCharacter; TPair<int32, int32> ESE = TPair<int32,int32>(1, 1); //diag
TPair<int32, int32> SE = TPair<int32,int32>(0, 1);
TPair<int32, int32> S = TPair<int32,int32>(-1, 2); //diag
TPair<int32, int32> SW = TPair<int32,int32>(-1, 1);
TPair<int32, int32> WSW = TPair<int32,int32>(-2, 1); //diag
TPair<int32, int32> W = TPair<int32,int32>(-1, 0);
TPair<int32, int32> WNW = TPair<int32,int32>(-1, -1); //diag
TPair<int32, int32> NW = TPair<int32,int32>(0, -1);
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned

View File

@ -41,6 +41,7 @@ void AAdventurePlayerController::SetupInputComponent()
void AAdventurePlayerController::LeftClick() void AAdventurePlayerController::LeftClick()
{ {
if (!IsValid(HoveredHex)) { return; }
if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); } if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); }
} }
@ -66,10 +67,13 @@ void AAdventurePlayerController::PlaceObject(TSubclassOf<AMapObject> MapObjClass
{ {
// spawn this Actor at World location of Origin Hex; // spawn this Actor at World location of Origin Hex;
AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation())); AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation()));
// Origin = OnHex;
SpawnedObj->Origin = OnHex; SpawnedObj->Origin = OnHex;
// set Hexes to bOccupied according to BlockVectors; // set Hexes to bOccupied according to BlockVectors;
OnHex->bFree = false; OnHex->bFree = false; // exact Hexes are eventually to be determined by MapObjectClass's BlockingVectors
// set bPlacementMode = false;
// MapRef->MapObjects.Add(OnHex, SpawnedObj); // maybe this as well
MapRef->IncID++;
SpawnedObj->ID = MapRef->IncID;
MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj);
} }

View File

@ -26,9 +26,9 @@ public:
// General // General
UPROPERTY() UPROPERTY()
UWorld* World; UWorld* World;
UPROPERTY() UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AAdventureMap* MapRef; AAdventureMap* MapRef;
UPROPERTY() UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AHexTile* SpawnHex; AHexTile* SpawnHex;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
AHexTile* CurrentHex; AHexTile* CurrentHex;