small changes to pathfinding; plan to fix bad performance

This commit is contained in:
Maximilian Fajnberg 2022-01-16 21:19:34 +01:00
parent 180207f441
commit 32f4c6a278
3 changed files with 69 additions and 8 deletions

View File

@ -118,44 +118,64 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
TArray<AHexTile*> Priorities;
Priorities.Init(Start, 1);
Goal->CameFrom = Start;
// Editing Hex->CameFrom pointers, i.e. chaining Hexes
while (Priorities.IsValidIndex(0))
while (!Priorities.IsEmpty())
{
AHexTile* Current = Priorities[0];
Priorities.RemoveAt(0);
if (*Current == *Goal) { break; }
if (*Current == *Goal)
{
// UE_LOG(LogTemp, Warning, TEXT("Goal found!")); // debug
break;
}
// Expanding the Frontier
for (AHexTile* Next : Neighbors(Current))
{
int32 NewCost = Current->CostSoFar + Next->MoveCost;
// UE_LOG(LogTemp, Warning, TEXT("Cost calculated.")); // debug
if (!Priorities.Contains(Next) || NewCost < Next->CostSoFar)
{
// UE_LOG(LogTemp, Warning, TEXT("New candidate found.")); // debug
Next->CostSoFar = NewCost;
int32 NewPrio = NewCost + Next->Distance(Goal);
// Adjust the Priority Queue
if (Priorities.Contains(Next)) { Priorities.Remove(Next); }
for (AHexTile* Hex : Priorities)
for (AHexTile* Hex : Priorities) // at this point Priorities is empty, need to make sure it's not.
{
int32 OldPrio = Hex->CostSoFar + Hex->Distance(Goal);
int32 Index;
Priorities.Find(Hex, Index);
// UE_LOG(LogTemp, Warning, TEXT("Comparing priorities...")); // debug
if (OldPrio > NewPrio)
{
Priorities.Insert(Next, Index);
Next->CameFrom = Current;
// UE_LOG(LogTemp, Warning, TEXT("Looks promising!")); // debug
break;
}
if (Index == Priorities.Num() - 1 && OldPrio <= NewPrio)
{
Priorities.Emplace(Next);
Next->CameFrom = Current;
// UE_LOG(LogTemp, Warning, TEXT("Low prio added")); // debug
break;
}
}
if (Priorities.IsEmpty())
{
Priorities.Emplace(Next);
}
}
}
}
@ -169,5 +189,5 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
}
Algo::Reverse(Path);
return Path; // currently always length of 1
return Path;
}

View File

@ -14,3 +14,30 @@ AAdventurePlayerController::AAdventurePlayerController()
PrimaryActorTick.bStartWithTickEnabled = true;
}
void AAdventurePlayerController::BeginPlay()
{
Super::BeginPlay();
World = GetWorld();
}
void AAdventurePlayerController::SetupInputComponent()
{
// Always call this.
Super::SetupInputComponent();
// This is initialized on startup, you can go straight to binding
InputComponent->BindAction("LeftClick", IE_Pressed, this, &AAdventurePlayerController::AdvClick);
}
void AAdventurePlayerController::AdvClick()
{
FHitResult Hit;
GetHitResultUnderCursor(ECollisionChannel::ECC_Vehicle,false,Hit);
if (IsValid(Hit.GetActor()))
{
AHexTile* HitHex = (AHexTile*)Hit.GetActor();
// MapRef->FindPathAStar(CurrentHex, HitHex); // this would currently cause a crash...
UE_LOG(LogTemp, Warning, TEXT("%d"), HitHex->Index);
}
}

View File

@ -23,5 +23,19 @@ class FRAY_API AAdventurePlayerController : public APlayerController
public:
AAdventurePlayerController();
UPROPERTY()
UWorld* World;
UPROPERTY()
AAdventureMap* MapRef;
UPROPERTY()
AHexTile* SpawnHex;
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Runtime")
AHexTile* CurrentHex;
protected:
virtual void BeginPlay() override;
virtual void SetupInputComponent() override;
public:
void AdvClick();
};