small changes & cleanup
This commit is contained in:
+15
-35
@@ -36,7 +36,6 @@ void AAdventureMap::MakeGrid()
|
||||
|
||||
for (int r = 1; r <= GridSize; r++) {
|
||||
float XOffset = 0.f;
|
||||
|
||||
if (r % 2 != 0) { if (r > 1) { QOffset--; } }
|
||||
else { XOffset = HexWidth / 2; }
|
||||
|
||||
@@ -45,23 +44,19 @@ void AAdventureMap::MakeGrid()
|
||||
NextHexAt.Y = TileSize * 1.5f * r;
|
||||
NextHexAt.Z = 0.f;
|
||||
FTransform SpawnTransform = FTransform(NextHexAt);
|
||||
|
||||
AHexTile* Tile = World->SpawnActor<AHexTile>(BaseTileClass, SpawnTransform);
|
||||
Grid.Add(Tile);
|
||||
|
||||
Tile->Q = q - 1 + QOffset;
|
||||
Tile->R = r - 1;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& tile : Grid) {
|
||||
tile->Index = GridIndex(tile->Q, tile->R);
|
||||
}
|
||||
|
||||
bHexGridReady = true;
|
||||
}
|
||||
|
||||
// Every Hex Tile's index within the Grid Array can be derived from its Q and R coordinates
|
||||
// Every Hex Tile's index within the Grid Array can be derived from its Axial Q and R coordinates
|
||||
int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
|
||||
{
|
||||
/*
|
||||
@@ -78,50 +73,36 @@ AHexTile* AAdventureMap::RandomHex()
|
||||
return Grid[RandHex];
|
||||
}
|
||||
|
||||
/*
|
||||
* Add two TArray<TPair<int32, int32>> members containing the Cardinal Directions
|
||||
(one for immediate neighbors, one for diagonals)
|
||||
{ fill them in AAdventureMap::AAdventureMap }
|
||||
|
||||
* This function instead returns
|
||||
TMap<bool bDiag, AHexTile* Neighbor>
|
||||
*/
|
||||
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
|
||||
{
|
||||
TArray<AHexTile*> Results;
|
||||
for (auto& V : NeighborUnitVectors) {
|
||||
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
|
||||
if (Grid.IsValidIndex(I)) {
|
||||
AHexTile* R = Grid[I];
|
||||
Results.Add(Grid[I]);
|
||||
if (bFreeOnly && !R->bFree) { Results.Remove(R); }
|
||||
AHexTile* H = Grid[I];
|
||||
if (bFreeOnly && !H->bFree) { continue; }
|
||||
if (H->Distance(OfHex) == 1) { Results.Add(H); }
|
||||
}
|
||||
}
|
||||
for (auto& R : Results) {
|
||||
if (bFreeOnly && !R->bFree) { Results.Remove(R); }
|
||||
}
|
||||
return Results;
|
||||
}
|
||||
|
||||
TArray<AHexTile*> AAdventureMap::FreeDiags(AHexTile* OfHex)
|
||||
TArray<AHexTile*> AAdventureMap::FreeDiagonals(AHexTile* OfHex)
|
||||
{
|
||||
TArray<AHexTile*> Results;
|
||||
for (auto& V : DiagonalUnitVectors) {
|
||||
int32 I = GridIndex(OfHex->Q + V.Q, OfHex->R + V.R);
|
||||
if (!Grid.IsValidIndex(I)) { continue; }
|
||||
// if (!bFreeOnly) { if (Grid[I]->Distance(OfHex) == 1) { Results.Add(Grid[I]); } }
|
||||
else {
|
||||
bool bReachable = true;
|
||||
for (auto& PotentialBlock : Neighbors(OfHex)) {
|
||||
for (AHexTile* PotentialBlock : Neighbors(OfHex)) {
|
||||
if (PotentialBlock->Distance(Grid[I]) != 1) { continue; }
|
||||
if (!PotentialBlock->bFree) {
|
||||
bReachable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bReachable) {
|
||||
Results.Add(Grid[I]);
|
||||
}
|
||||
if (bReachable) { Results.Add(Grid[I]); }
|
||||
}
|
||||
}
|
||||
return Results;
|
||||
@@ -162,7 +143,7 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
|
||||
AHexTile* Candidate = ToExamine[0];
|
||||
ToExamine.Remove(Candidate);
|
||||
// try for Hex with lower estimatet (F)cost
|
||||
for (auto& t : ToExamine) {
|
||||
for (AHexTile* t : ToExamine) {
|
||||
t->FCost = t->GCost + t->HCost;
|
||||
if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; }
|
||||
}
|
||||
@@ -174,6 +155,7 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
|
||||
// expand frontier & adjust path data
|
||||
for (AHexTile* Neighbor : Neighbors(Candidate, true)) {
|
||||
if (Neighbor->Distance(Candidate) > 1) { continue; }
|
||||
if (!Neighbor->bFree) { continue; }
|
||||
if (Processed.Contains(Neighbor)) { continue; }
|
||||
|
||||
bool bInToExamine = ToExamine.Contains(Neighbor);
|
||||
@@ -186,27 +168,25 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
|
||||
if (!bInToExamine) {
|
||||
Neighbor->HCost = Neighbor->Distance(Goal) * 10.f;
|
||||
ToExamine.Add(Neighbor);
|
||||
}
|
||||
}
|
||||
} }
|
||||
}
|
||||
if (bDiags) { // right now the heuristic for HCost (Distance func) does NOT take diagonals into account
|
||||
for (AHexTile* Diag : FreeDiags(Candidate)) {
|
||||
if (bDiags) {
|
||||
for (AHexTile* Diag : FreeDiagonals(Candidate)) {
|
||||
if (Diag->Distance(Candidate) > 2) { continue; }
|
||||
if (!Diag->bFree) { continue; }
|
||||
if (Processed.Contains(Diag)) { continue; }
|
||||
|
||||
bool bInToExamine = ToExamine.Contains(Diag);
|
||||
float NewGCost = Candidate->GCost + Diag->MoveCost * 10.f;
|
||||
float NewGCost = Candidate->GCost + 1 + Diag->MoveCost * 10.f;
|
||||
|
||||
if (NewGCost < Diag->GCost || !bInToExamine) {
|
||||
Diag->GCost = NewGCost;
|
||||
Diag->CameFrom = Candidate; // chain
|
||||
|
||||
if (!bInToExamine) {
|
||||
Diag->HCost = Diag->Distance(Goal) * 10.f;
|
||||
Diag->HCost = Diag->Distance(Goal) * 10.f; // not accounting for diagonals
|
||||
ToExamine.Add(Diag);
|
||||
}
|
||||
}
|
||||
} }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user