small changes to pathfinding; plan to fix bad performance
This commit is contained in:
@ -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;
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user