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()
{
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());

View File

@ -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")

View File

@ -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

View File

@ -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<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
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*> ToExamine;
TArray<AHexTile*> Processed;
TSet<AHexTile*> Processed;
ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) {

View File

@ -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<AHexTile*> Grid;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
TMap<AHexTile*, AMapObject*> MapObjects;
UPROPERTY(BlueprintReadOnly)
TMap<int32, AMapObject*> 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<AHexTile*> Neighbors(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> AStar(AHexTile* Start, AHexTile* Goal);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> LinkPath(AHexTile* Start, AHexTile* Goal);
// Player spawn section
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
APawn* CameraPawn;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
AAdventureCharacter* PlayerCharacter;
// Cardinal direction vectors
TPair<int32, int32> N = TPair<int32,int32>(1, -2); //diag
TPair<int32, int32> NE = TPair<int32,int32>(1, -1);
TPair<int32, int32> ENE = TPair<int32,int32>(2, -1); //diag
TPair<int32, int32> E = TPair<int32,int32>(1, 0);
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:
// Called when the game starts or when spawned

View File

@ -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<AMapObject> MapObjClass
{
// spawn this Actor at World location of Origin Hex;
AMapObject* SpawnedObj = World->SpawnActor<AMapObject>(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);
}

View File

@ -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;