new algorithm draft for diag moves; committed missing files
This commit is contained in:
+109
-7
@@ -154,41 +154,42 @@ 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);
|
||||
float NewGCost = Candidate->GCost + Neighbor->MoveCost * 10.f;
|
||||
float NewGCost = Candidate->GCost + Neighbor->MoveCost;
|
||||
|
||||
if (NewGCost < Neighbor->GCost || !bInToExamine) {
|
||||
Neighbor->GCost = NewGCost;
|
||||
Neighbor->CameFrom = Candidate; // chain
|
||||
Neighbor->bDiagMove = false;
|
||||
|
||||
if (!bInToExamine) {
|
||||
Neighbor->HCost = Neighbor->Distance(Goal) * 10.f;
|
||||
Neighbor->HCost = Neighbor->Distance(Goal);
|
||||
ToExamine.Add(Neighbor);
|
||||
} }
|
||||
}
|
||||
/*
|
||||
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 + 1 + Diag->MoveCost * 10.f;
|
||||
float NewGCost = Candidate->GCost + Diag->MoveCost;
|
||||
|
||||
if (NewGCost < Diag->GCost || !bInToExamine) {
|
||||
Diag->GCost = NewGCost;
|
||||
Diag->CameFrom = Candidate; // chain
|
||||
Diag->bDiagMove = true;
|
||||
|
||||
if (!bInToExamine) {
|
||||
Diag->HCost = Diag->Distance(Goal) * 10.f; // not accounting for diagonals
|
||||
Diag->HCost = Diag->Distance(Goal);
|
||||
ToExamine.Add(Diag);
|
||||
} }
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
TArray<AHexTile*> Path;
|
||||
if (!IsValid(Goal->CameFrom)) { return Path; }
|
||||
@@ -198,5 +199,106 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
|
||||
iPathNode = iPathNode->CameFrom;
|
||||
}
|
||||
Algo::Reverse(Path);
|
||||
|
||||
if (bDiags) {
|
||||
Path = ShortcutAStar(Path);
|
||||
}
|
||||
|
||||
return Path;
|
||||
}
|
||||
|
||||
|
||||
TArray<AHexTile*> AAdventureMap::ShortcutAStar(TArray<AHexTile*> Path)
|
||||
{
|
||||
TArray<AHexTile*> Shortcut;
|
||||
int32 Len = Path.Num();
|
||||
AHexTile* Milestone;
|
||||
int32 BeforeBend;
|
||||
int32 AfterBend;
|
||||
int32 HexIter = 1;
|
||||
FHexVector pDir;
|
||||
FHexVector DirA;
|
||||
FHexVector DirB;
|
||||
AHexTile* Current = Path[0]; // beginning of curve (starts at Start)
|
||||
|
||||
while (Milestone != Path[Len - 1]) {
|
||||
// find Milestone (i.e. end of first curve) & determine curve data
|
||||
BeforeBend = 1;
|
||||
for (HexIter; HexIter < Len; HexIter++) {
|
||||
pDir = FHexVector(Path[HexIter], Path[HexIter - 1]);
|
||||
DirA = FHexVector(Path[HexIter + 1], Path[HexIter]);
|
||||
if (DirA == pDir) { BeforeBend++; }
|
||||
else { break; }
|
||||
}
|
||||
AfterBend = 1;
|
||||
for (HexIter; HexIter < Len; HexIter++) {
|
||||
pDir = FHexVector(Path[HexIter], Path[HexIter - 1]);
|
||||
DirB = FHexVector(Path[HexIter + 1], Path[HexIter]);
|
||||
if (DirB == pDir) { AfterBend++; }
|
||||
else { break; }
|
||||
}
|
||||
FHexVector Diag = UnitDiagFromUnitNB(DirA, DirB); // cardinal direction for potential shortcut
|
||||
TArray<AHexTile*> WorkingSegment; // current curve
|
||||
for (int32 i = Path.Find(Current); i < HexIter; i++) { WorkingSegment.Add(Path[i]); }
|
||||
Milestone = Path[HexIter]; // end of curve
|
||||
int32 ShoCutLen;
|
||||
int32 NumDiags; // max number of tries to take a Diagonal
|
||||
if (BeforeBend >= AfterBend) {
|
||||
ShoCutLen = BeforeBend;
|
||||
NumDiags = AfterBend;
|
||||
}
|
||||
if (BeforeBend < AfterBend) {
|
||||
ShoCutLen = AfterBend;
|
||||
NumDiags = BeforeBend;
|
||||
}
|
||||
|
||||
TArray<AHexTile*> NewSegment;
|
||||
// link from Current to Milestone with diagonals
|
||||
for (int32 i = 0; i < NumDiags; i++) {
|
||||
AHexTile* NewCandidate = Grid[GridIndex(Current->Q + Diag.Q, Current->R + Diag.R)];
|
||||
|
||||
if (i==0 && !DiagIsReachable(Current, Diag) || i==0 && !NewCandidate->bFree) {
|
||||
Current = WorkingSegment[i + 1];
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
NewSegment.Add(NewCandidate);
|
||||
}
|
||||
// connect the rest via A* probably, think about checking whether the result is really shorter
|
||||
int32 CIndex = WorkingSegment.Find(Current);
|
||||
while (Current != Milestone) {
|
||||
Current = WorkingSegment[CIndex];
|
||||
NewSegment.Add(Current);
|
||||
CIndex++;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// Construct shortcut
|
||||
return Shortcut;
|
||||
}
|
||||
|
||||
|
||||
FHexVector AAdventureMap::UnitDiagFromUnitNB(FHexVector InVecA, FHexVector InVecB) {
|
||||
if (InVecA == NNW && InVecB == NNE||InVecB == NNW && InVecA == NNE) { return N; }
|
||||
if (InVecA == NNE && InVecB == E ||InVecB == NNE && InVecA == E) { return ENE; }
|
||||
if (InVecA == E && InVecB == SSE||InVecB == E && InVecA == SSE) { return ESE; }
|
||||
if (InVecA == SSE && InVecB == SSW||InVecB == SSE && InVecA == SSW) { return S; }
|
||||
if (InVecA == SSW && InVecB == W ||InVecB == SSW && InVecA == W) { return WSW; }
|
||||
if (InVecA == W && InVecB == NNW||InVecB == W && InVecA == NNW) { return WNW; }
|
||||
return FHexVector();
|
||||
}
|
||||
bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec) {
|
||||
FHexVector BlockA;
|
||||
FHexVector BlockB;
|
||||
if (InDiagUnitVec == N) { BlockA = NNW, BlockB = NNE; }
|
||||
if (InDiagUnitVec == ENE) { BlockA = NNE, BlockB = E; }
|
||||
if (InDiagUnitVec == ESE) { BlockA = E, BlockB = SSE; }
|
||||
if (InDiagUnitVec == S) { BlockA = SSE, BlockB = SSW; }
|
||||
if (InDiagUnitVec == WSW) { BlockA = SSW, BlockB = W; }
|
||||
if (InDiagUnitVec == WNW) { BlockA = W, BlockB = NNW; }
|
||||
AHexTile* HexA = Grid[GridIndex(InStart->Q + BlockA.Q, InStart->R + BlockA.R)];
|
||||
AHexTile* HexB = Grid[GridIndex(InStart->Q + BlockB.Q, InStart->R + BlockB.R)];
|
||||
return (HexA->bFree && HexB->bFree);
|
||||
}
|
||||
Reference in New Issue
Block a user