From 986233d960cd8da5dd15c234910463e7f759344e Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Tue, 25 Jan 2022 18:30:49 +0100 Subject: [PATCH] Breadth First Search; considering use of diagonals for A*PF --- AdventureCameraPawn.cpp | 8 ++++---- AdventureCameraPawn.h | 4 ++-- AdventureCharacter.h | 6 +++--- AdventureMap.cpp | 32 +++++++++++++++++++++++++++++--- AdventureMap.h | 31 +++++++++++++++++++++++-------- AdventurePlayerController.cpp | 12 ++++++++---- AdventurePlayerController.h | 4 ++-- 7 files changed, 71 insertions(+), 26 deletions(-) diff --git a/AdventureCameraPawn.cpp b/AdventureCameraPawn.cpp index 843e83d..69a102b 100644 --- a/AdventureCameraPawn.cpp +++ b/AdventureCameraPawn.cpp @@ -35,7 +35,7 @@ AAdventureCameraPawn::AAdventureCameraPawn() void AAdventureCameraPawn::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) FTimerHandle UnusedHandle; @@ -85,7 +85,7 @@ void AAdventureCameraPawn::EdgeScrollSide(float AxisValue) { float mouseX; float mouseY; - ControllerRef->GetMousePosition(mouseX, mouseY); + OwningPC->GetMousePosition(mouseX, mouseY); float mousePosX = mouseX / ViewSize.X; float mousePosY = mouseY / ViewSize.Y; float VertAccelleration = GetInputAxisValue("Mouse Y") * (ScrollAccelleration * .75); @@ -133,7 +133,7 @@ void AAdventureCameraPawn::EdgeScrollVert(float AxisValue) { float mouseX; float mouseY; - ControllerRef->GetMousePosition(mouseX, mouseY); + OwningPC->GetMousePosition(mouseX, mouseY); float mousePosY = mouseY / ViewSize.Y; float mousePosX = mouseX / ViewSize.X; float SideAccelleration = GetInputAxisValue("Mouse X") * (ScrollAccelleration * .75); @@ -182,7 +182,7 @@ void AAdventureCameraPawn::FollowAdvPawn() { UE_LOG(LogTemp, Warning, TEXT("following...")); - AdvPawnLocationCurrent = AdvPawnRef->GetActorLocation(); + AdvPawnLocationCurrent = FollowPawn->GetActorLocation(); AdvPawnLocationDelta = AdvPawnLocationCurrent - AdvPawnLocationPrevious; UE_LOG(LogTemp, Warning, TEXT("%s"), *AdvPawnLocationDelta.ToString()); diff --git a/AdventureCameraPawn.h b/AdventureCameraPawn.h index 2295b2b..b531dda 100644 --- a/AdventureCameraPawn.h +++ b/AdventureCameraPawn.h @@ -23,9 +23,9 @@ public: UPROPERTY(BlueprintReadWrite, Category = "Config") class AAdventureMap* AdvMapRef; UPROPERTY(BlueprintReadWrite, Category = "Config") - class AAdventurePlayerController* ControllerRef; + class AAdventurePlayerController* OwningPC; UPROPERTY(BlueprintReadWrite, Category = "Config") - class AAdventureCharacter* AdvPawnRef; + class AAdventureCharacter* FollowPawn; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Input") diff --git a/AdventureCharacter.h b/AdventureCharacter.h index 7ffaf50..a472d6f 100644 --- a/AdventureCharacter.h +++ b/AdventureCharacter.h @@ -20,10 +20,10 @@ public: // Sets default values for this character's properties AAdventureCharacter(); + UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Config") + class AAdventureMap* MapRef; UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") - AHexTile* GridLocation; - UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") - AHexTile* SelectedHex; + AHexTile* Location; protected: // Called when the game starts or when spawned diff --git a/AdventureMap.cpp b/AdventureMap.cpp index 554fbb6..c502325 100644 --- a/AdventureMap.cpp +++ b/AdventureMap.cpp @@ -3,8 +3,6 @@ #include "AdventureMap.h" #include "HexTile.h" -#include "AdventureCameraPawn.h" -#include "AdventureCharacter.h" #include "Kismet/GameplayStatics.h" #include "Algo/Reverse.h" @@ -94,10 +92,38 @@ TArray AAdventureMap::Neighbors(AHexTile* OfHex) return Neighbors; } +TArray AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius) +{ + TArray Results; + TArray ToExamine; + TSet 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 AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal) { TArray ToExamine; - TArray Processed; + TSet Processed; ToExamine.Add(Start); while (!ToExamine.IsEmpty()) { diff --git a/AdventureMap.h b/AdventureMap.h index c5caa76..baf2ffb 100644 --- a/AdventureMap.h +++ b/AdventureMap.h @@ -7,7 +7,7 @@ #include "AdventureMap.generated.h" class AHexTile; -class AAdventureCharacter; +class AMapObject; UCLASS() class FRAY_API AAdventureMap : public AActor @@ -33,26 +33,41 @@ public: void MakeGrid(); UPROPERTY(BlueprintReadOnly, Category = "Generation") bool bHexGridReady; - UPROPERTY(VisibleInstanceOnly, BlueprintReadWrite, Category = "Generation") + UPROPERTY(BlueprintReadOnly, Category = "Generation") TArray Grid; + UPROPERTY(BlueprintReadWrite, EditAnywhere) + TMap MapObjects; + UPROPERTY(BlueprintReadOnly) + TMap MapObjectsByID; + int32 IncID = -1; + UFUNCTION(BlueprintCallable, Category = "Runtime") int32 GridIndex(int32 q, int32 r); UFUNCTION(BlueprintCallable, Category = "Runtime") AHexTile* RandomHex(); UFUNCTION(BlueprintCallable, Category = "Runtime") TArray Neighbors(AHexTile* OfHex); + UFUNCTION(BlueprintCallable, Category = "Runtime") + TArray BreadthFirstSearch(AHexTile* Start, int32 Radius); UFUNCTION(BlueprintCallable, Category = "Runtime") TArray AStar(AHexTile* Start, AHexTile* Goal); UFUNCTION(BlueprintCallable, Category = "Runtime") TArray LinkPath(AHexTile* Start, AHexTile* Goal); - - // Player spawn section - UPROPERTY(VisibleAnywhere, BlueprintReadWrite) - APawn* CameraPawn; - UPROPERTY(VisibleAnywhere, BlueprintReadWrite) - AAdventureCharacter* PlayerCharacter; + // Cardinal direction vectors + TPair N = TPair(1, -2); //diag + TPair NE = TPair(1, -1); + TPair ENE = TPair(2, -1); //diag + TPair E = TPair(1, 0); + TPair ESE = TPair(1, 1); //diag + TPair SE = TPair(0, 1); + TPair S = TPair(-1, 2); //diag + TPair SW = TPair(-1, 1); + TPair WSW = TPair(-2, 1); //diag + TPair W = TPair(-1, 0); + TPair WNW = TPair(-1, -1); //diag + TPair NW = TPair(0, -1); protected: // Called when the game starts or when spawned diff --git a/AdventurePlayerController.cpp b/AdventurePlayerController.cpp index ad5f2cc..59cdf47 100644 --- a/AdventurePlayerController.cpp +++ b/AdventurePlayerController.cpp @@ -41,6 +41,7 @@ void AAdventurePlayerController::SetupInputComponent() void AAdventurePlayerController::LeftClick() { + if (!IsValid(HoveredHex)) { return; } if (bInPlacementMode) { PlaceObject(PlaceObjClass, HoveredHex); } } @@ -66,10 +67,13 @@ void AAdventurePlayerController::PlaceObject(TSubclassOf MapObjClass { // spawn this Actor at World location of Origin Hex; AMapObject* SpawnedObj = World->SpawnActor(MapObjClass, FTransform(OnHex->GetActorTransform().GetLocation())); - // Origin = OnHex; SpawnedObj->Origin = OnHex; + // set Hexes to bOccupied according to BlockVectors; - OnHex->bFree = false; - // set bPlacementMode = false; - // + OnHex->bFree = false; // exact Hexes are eventually to be determined by MapObjectClass's BlockingVectors + + MapRef->MapObjects.Add(OnHex, SpawnedObj); // maybe this as well + MapRef->IncID++; + SpawnedObj->ID = MapRef->IncID; + MapRef->MapObjectsByID.Add(MapRef->IncID, SpawnedObj); } \ No newline at end of file diff --git a/AdventurePlayerController.h b/AdventurePlayerController.h index 4fdd63a..390c46b 100644 --- a/AdventurePlayerController.h +++ b/AdventurePlayerController.h @@ -26,9 +26,9 @@ public: // General UPROPERTY() UWorld* World; - UPROPERTY() + UPROPERTY(VisibleAnywhere, BlueprintReadWrite) AAdventureMap* MapRef; - UPROPERTY() + UPROPERTY(VisibleAnywhere, BlueprintReadWrite) AHexTile* SpawnHex; UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime") AHexTile* CurrentHex;