implemented diagonal movement

This commit is contained in:
2022-01-28 21:58:04 +01:00
parent 70ef60f08e
commit 2bf072e3c6
5 changed files with 124 additions and 109 deletions

View File

@ -10,18 +10,10 @@
// Sets default values
AAdventureMap::AAdventureMap()
{
NBVectors.Add(NNE);
NBVectors.Add(E);
NBVectors.Add(SSE);
NBVectors.Add(SSW);
NBVectors.Add(W);
NBVectors.Add(NNW);
NBVectorsDiag.Add(N);
NBVectorsDiag.Add(ENE);
NBVectorsDiag.Add(ESE);
NBVectorsDiag.Add(S);
NBVectorsDiag.Add(WSW);
NBVectorsDiag.Add(WNW);
FHexVector NBs[] = { NNE, E, SSE, SSW, W, NNW };
NeighborUnitVectors.Append(NBs, UE_ARRAY_COUNT(NBs));
FHexVector DNBs[] = { N, ENE, ESE, S, WSW, WNW };
DiagonalUnitVectors.Append(DNBs, UE_ARRAY_COUNT(DNBs));
}
// Called when the game starts or when spawned
@ -45,25 +37,20 @@ void AAdventureMap::MakeGrid()
for (int r = 1; r <= GridSize; r++) {
float XOffset = 0.f;
if (r % 2 != 0) {
if (r > 1) {
QOffset--;
}
}
if (r % 2 != 0) { if (r > 1) { QOffset--; } }
else { XOffset = HexWidth / 2; }
for (int q = 1; q <= GridSize; q++) {
for (int q = 1; q <= GridSize; q++) {
NextHexAt.X = XOffset + (HexWidth * q);
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;
Grid.Add(Tile);
}
}
@ -99,52 +86,57 @@ AHexTile* AAdventureMap::RandomHex()
* This function instead returns
TMap<bool bDiag, AHexTile* Neighbor>
*/
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex)
TArray<AHexTile*> AAdventureMap::Neighbors(AHexTile* OfHex, bool bFreeOnly = false)
{
TArray<AHexTile*> Results;
TArray<int32> Indeces;
for (auto& Vec : NBVectors) {
Indeces.Add(GridIndex(OfHex->Q + Vec.Key, OfHex->R + Vec.Value));
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); }
}
}
for (auto& Ind : Indeces) {
if (Grid.IsValidIndex(Ind)) {
if (OfHex->Distance(Grid[Ind]) == 1) {
Results.Add(Grid[Ind]);
}
}
for (auto& R : Results) {
if (bFreeOnly && !R->bFree) { Results.Remove(R); }
}
return Results;
}
TArray<AHexTile*> AAdventureMap::Diagonals(AHexTile* OfHex)
TArray<AHexTile*> AAdventureMap::FreeDiags(AHexTile* OfHex)
{
TArray<AHexTile*> Results;
TArray<int32> Indeces;
for (auto& Vec : NBVectorsDiag) {
Indeces.Add(GridIndex(OfHex->Q + Vec.Key, OfHex->R + Vec.Value));
}
for (auto& Ind : Indeces) {
if (Grid.IsValidIndex(Ind)) {
if (OfHex->Distance(Grid[Ind]) == 2) {
Results.Add(Grid[Ind]);
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)) {
if (PotentialBlock->Distance(Grid[I]) != 1) { continue; }
if (!PotentialBlock->bFree) {
bReachable = false;
break;
}
}
if (bReachable) {
Results.Add(Grid[I]);
}
}
}
return Results;
}
TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
TSet<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radius)
{
TArray<AHexTile*> Results;
TSet<AHexTile*> Results;
TArray<AHexTile*> ToExamine;
TSet<AHexTile*> Processed;
Results.Add(Start);
ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0];
Processed.Add(Candidate);
ToExamine.Remove(Candidate);
@ -160,58 +152,67 @@ TArray<AHexTile*> AAdventureMap::BreadthFirstSearch(AHexTile* Start, int32 Radiu
return Results;
}
TArray<AHexTile*> AAdventureMap::AStar(AHexTile* Start, AHexTile* Goal)
TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags)
{
TArray<AHexTile*> ToExamine;
TSet<AHexTile*> Processed;
ToExamine.Add(Start);
while (!ToExamine.IsEmpty()) {
while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0];
ToExamine.Remove(Candidate);
// estimate closest known Hex to Goal
// try for Hex with lower estimatet (F)cost
for (auto& t : ToExamine) {
t->FCost = t->GCost + t->HCost;
if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; }
if (t->FCost < Candidate->FCost || t->FCost == Candidate->FCost && t->HCost < Candidate->HCost) { Candidate = t; }
}
Processed.Add(Candidate);
// exit
if (Candidate == Goal) { break; }
// expand frontier & adjust path data
for (AHexTile* Neighbor : Neighbors(Candidate)) {
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);
int32 NewGCost = Candidate->GCost + Neighbor->MoveCost;
float NewGCost = Candidate->GCost + Neighbor->MoveCost * 10.f;
if (NewGCost < Neighbor->GCost || !bInToExamine) {
Neighbor->GCost = NewGCost;
Neighbor->CameFrom = Candidate; // chain
if (!bInToExamine) {
Neighbor->HCost = Neighbor->Distance(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 (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;
if (NewGCost < Diag->GCost || !bInToExamine) {
Diag->GCost = NewGCost;
Diag->CameFrom = Candidate; // chain
if (!bInToExamine) {
Diag->HCost = Diag->Distance(Goal) * 10.f;
ToExamine.Add(Diag);
}
}
}
}
}
return LinkPath(Start, Goal);
}
TArray<AHexTile*> AAdventureMap::LinkPath(AHexTile* Start, AHexTile* Goal)
{
TArray<AHexTile*> Path;
if (!IsValid(Goal->CameFrom)) { return Path; }
AHexTile* iPathNode = Goal;
while (iPathNode != Start) {
Path.Emplace(iPathNode);
iPathNode = iPathNode->CameFrom;