small changes to pathfinding; plan to fix bad performance
This commit is contained in:
parent
180207f441
commit
32f4c6a278
@ -118,44 +118,64 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal)
|
|||||||
TArray<AHexTile*> Priorities;
|
TArray<AHexTile*> Priorities;
|
||||||
Priorities.Init(Start, 1);
|
Priorities.Init(Start, 1);
|
||||||
|
|
||||||
Goal->CameFrom = Start;
|
|
||||||
|
|
||||||
// Editing Hex->CameFrom pointers, i.e. chaining Hexes
|
// Editing Hex->CameFrom pointers, i.e. chaining Hexes
|
||||||
while (Priorities.IsValidIndex(0))
|
while (!Priorities.IsEmpty())
|
||||||
{
|
{
|
||||||
AHexTile* Current = Priorities[0];
|
AHexTile* Current = Priorities[0];
|
||||||
Priorities.RemoveAt(0);
|
Priorities.RemoveAt(0);
|
||||||
if (*Current == *Goal) { break; }
|
if (*Current == *Goal)
|
||||||
|
{
|
||||||
|
// UE_LOG(LogTemp, Warning, TEXT("Goal found!")); // debug
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// Expanding the Frontier
|
// Expanding the Frontier
|
||||||
for (AHexTile* Next : Neighbors(Current))
|
for (AHexTile* Next : Neighbors(Current))
|
||||||
{
|
{
|
||||||
int32 NewCost = Current->CostSoFar + Next->MoveCost;
|
int32 NewCost = Current->CostSoFar + Next->MoveCost;
|
||||||
|
|
||||||
|
// UE_LOG(LogTemp, Warning, TEXT("Cost calculated.")); // debug
|
||||||
|
|
||||||
if (!Priorities.Contains(Next) || NewCost < Next->CostSoFar)
|
if (!Priorities.Contains(Next) || NewCost < Next->CostSoFar)
|
||||||
{
|
{
|
||||||
|
// UE_LOG(LogTemp, Warning, TEXT("New candidate found.")); // debug
|
||||||
|
|
||||||
Next->CostSoFar = NewCost;
|
Next->CostSoFar = NewCost;
|
||||||
int32 NewPrio = NewCost + Next->Distance(Goal);
|
int32 NewPrio = NewCost + Next->Distance(Goal);
|
||||||
|
|
||||||
// Adjust the Priority Queue
|
// Adjust the Priority Queue
|
||||||
if (Priorities.Contains(Next)) { Priorities.Remove(Next); }
|
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 OldPrio = Hex->CostSoFar + Hex->Distance(Goal);
|
||||||
int32 Index;
|
int32 Index;
|
||||||
Priorities.Find(Hex, Index);
|
Priorities.Find(Hex, Index);
|
||||||
|
|
||||||
|
// UE_LOG(LogTemp, Warning, TEXT("Comparing priorities...")); // debug
|
||||||
|
|
||||||
if (OldPrio > NewPrio)
|
if (OldPrio > NewPrio)
|
||||||
{
|
{
|
||||||
Priorities.Insert(Next, Index);
|
Priorities.Insert(Next, Index);
|
||||||
Next->CameFrom = Current;
|
Next->CameFrom = Current;
|
||||||
|
|
||||||
|
// UE_LOG(LogTemp, Warning, TEXT("Looks promising!")); // debug
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (Index == Priorities.Num() - 1 && OldPrio <= NewPrio)
|
if (Index == Priorities.Num() - 1 && OldPrio <= NewPrio)
|
||||||
{
|
{
|
||||||
Priorities.Emplace(Next);
|
Priorities.Emplace(Next);
|
||||||
Next->CameFrom = Current;
|
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);
|
Algo::Reverse(Path);
|
||||||
return Path; // currently always length of 1
|
return Path;
|
||||||
}
|
}
|
||||||
|
@ -13,4 +13,31 @@ AAdventurePlayerController::AAdventurePlayerController()
|
|||||||
PrimaryActorTick.bCanEverTick = true;
|
PrimaryActorTick.bCanEverTick = true;
|
||||||
PrimaryActorTick.bStartWithTickEnabled = true;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
@ -23,5 +23,19 @@ class FRAY_API AAdventurePlayerController : public APlayerController
|
|||||||
public:
|
public:
|
||||||
AAdventurePlayerController();
|
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:
|
public:
|
||||||
|
void AdvClick();
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user