port to UE5 and new development environment; consequential leeway

This commit is contained in:
Maximilian Fajnberg 2022-05-25 20:07:51 +02:00
parent 1485621698
commit 21f7dd4e67
5 changed files with 74 additions and 35 deletions

View File

@ -55,6 +55,14 @@ void AAdventureCameraPawn::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// quick fix for edgescrolling after viewport resizing.
//
//TickIncrement++;
//if (TickIncrement >= 60) {
// TickIncrement = 0;
// ViewSize = Viewport->Viewport->GetSizeXY();
//}
if (IsValid(FollowPawn)) { if (FollowPawn->bIsMoving) { FollowAdvPawn(DeltaTime); } }
}

View File

@ -19,6 +19,8 @@ public:
UGameViewportClient* Viewport;
UPROPERTY()
FIntPoint ViewSize;
UPROPERTY()
int32 TickIncrement = 0;
UPROPERTY(BlueprintReadWrite, Category = "Config")
class AAdventureMap* AdvMapRef;

View File

@ -63,7 +63,7 @@ int32 AAdventureMap::GridIndex(int32 qAxial, int32 rAxial)
* The Q axis is (i.e. columns are) oriented diagonally.
* The Hex Grid has a rough square shape, hence the Q coordinates must be offset by -1 every other row.
*/
int32 column = qAxial + FMath::FloorToInt(rAxial / 2);
int32 column = qAxial + FMath::FloorToInt(rAxial / 2.);
return (rAxial * GridSize) + column;
}
@ -142,7 +142,7 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
while (!ToExamine.IsEmpty()) {
AHexTile* Candidate = ToExamine[0];
ToExamine.Remove(Candidate);
// try for Hex with lower estimatet (F)cost
// find Hex with lower estimatet F-cost
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; }
@ -181,64 +181,88 @@ TArray<AHexTile*> AAdventureMap::FindPathAStar(AHexTile* Start, AHexTile* Goal,
Algo::Reverse(Path);
if (bDiags) {
TArray<AHexTile*> PathS = ShortcutAStar(Path);
Path = ShortcutAStar(Path);
}
return Path;
}
// very bro-sciency approach to pathfinding for diagonal Hex-movement
TArray<AHexTile*> AAdventureMap::ShortcutAStar(TArray<AHexTile*> Path)
{
TArray<AHexTile*> Shortcut;
int32 Len = Path.Num();
TArray<AHexTile*> WorkingSegment;
AHexTile* Start = PCRef->CurrentHex;
WorkingSegment.Add(Start);
int32 i = 0;
AHexTile* CurrentHex = PCRef->CurrentHex;
WorkingSegment.Add(CurrentHex);
int32 h = 0;
FHexVector PrevDir = FHexVector(Path[0], Start);
// create segments for each bend
FHexVector PrevDir = FHexVector(Path[0], CurrentHex);
FHexVector DirASave = PrevDir;
FHexVector DirA;
int32 HexesBeforeBend = 1;
for (i; i < Len-1; i++) {
WorkingSegment.Add(Path[i]);
DirA = FHexVector(Path[i+1], Path[i]);
if (DirA != PrevDir) { break; }
for (h; h < Len-1; h++) {
WorkingSegment.Add(Path[h]);
DirA = FHexVector(Path[h+1], Path[h]);
if (DirA != PrevDir) { break; } // save Path[h] into Array of Bends
HexesBeforeBend++;
PrevDir = DirA;
}
PrevDir = DirA;
FHexVector DirB;
int32 HexesAfterBend = 0;
for (i; i < Len - 1; i++) {
WorkingSegment.Add(Path[i+1]);
DirB = FHexVector(Path[i+1], Path[i]);
for (h; h < Len - 1; h++) {
WorkingSegment.Add(Path[h+1]);
DirB = FHexVector(Path[h+1], Path[h]);
if (DirB != PrevDir) { break; }
HexesAfterBend++;
PrevDir = DirB;
}
if (HexesAfterBend == 0) { return Path; }
// debug
UE_LOG(LogTemp, Warning, TEXT("Before bend: %d"), HexesBeforeBend);
UE_LOG(LogTemp, Warning, TEXT("After bend: %d"), HexesAfterBend);
UE_LOG(LogTemp, Warning, TEXT("Working segment length: %d"), WorkingSegment.Num());
for (AHexTile* HexDebug : WorkingSegment) { UE_LOG(LogTemp, Warning, TEXT("HexID: %d"), HexDebug->Index); }
FHexVector UnitDiag = UnitDiagFromUnitNB(DirA, DirB);
if (HexesAfterBend == 0)
{ return Path; }
FHexVector UnitDiag = UnitDiagFromUnitNB(DirASave, DirB);
AHexTile* Milestone = WorkingSegment.Last();
int32 NumDiags = (HexesBeforeBend >= HexesAfterBend) ? HexesAfterBend : HexesBeforeBend;
int32 NumTries = (HexesBeforeBend >= HexesAfterBend) ? HexesBeforeBend : HexesAfterBend;
// Try adding diagonal Hexes to Shortcut from Start until Milestone (if BeforeBend>=AferBend: do this AfterBend times, else: BeforeBend)
// Set a bool to 'true' as soon as the first Hex is added; Set CurrentHex to that;
// if the path is blocked and the bool is 'false': Set CurrentHex to the next in WorkingSegment; Retry.
// if the path is blocked and the bool is 'true': Append the rest recursively calling A*(Shortcut.Last(), Path.Last(), true);
// when iterated AfterBend (or BeforeBend) times: Append the rest recursively calling A*(Shortcut.Last(), Path.Last(), true);
bool bDiagAdded = false;
for (int i = 0; i < NumTries; i++) {
if (NumDiags == 0) {
Shortcut.Append(FindPathAStar(CurrentHex, Milestone, false));
break;
}
if (DiagIsReachable(CurrentHex, UnitDiag)) {
int32 CanIndex = GridIndex(CurrentHex->Q + UnitDiag.Q, CurrentHex->R + UnitDiag.R);
if (Grid.IsValidIndex(CanIndex)) {
AHexTile* Candidate = Grid[CanIndex];
if (Candidate->bFree) {
Shortcut.Add(Candidate);
bDiagAdded = true;
CurrentHex = Candidate;
NumDiags--;
continue;
} } }
if (!bDiagAdded && !DiagIsReachable(CurrentHex, UnitDiag)) {
Shortcut.Add(CurrentHex);
CurrentHex = WorkingSegment[i + 1];
NumDiags--;
continue;
}
if (bDiagAdded && !DiagIsReachable(CurrentHex, UnitDiag)) {
Shortcut.Append(FindPathAStar(CurrentHex, Milestone, true));
break;
}
}
if (Milestone != Path.Last()) { Shortcut.Append(FindPathAStar(Milestone, Path.Last(), true)); }
UE_LOG(LogTemp, Warning, TEXT("Hexes before bend: %d"), HexesBeforeBend);
UE_LOG(LogTemp, Warning, TEXT("Hexes after bend: %d"), HexesAfterBend);
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; }
@ -248,6 +272,7 @@ FHexVector AAdventureMap::UnitDiagFromUnitNB(FHexVector InVecA, FHexVector InVec
if (InVecA == W && InVecB == NNW||InVecB == W && InVecA == NNW) { return WNW; }
return FHexVector();
}
bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec) {
FHexVector BlockA;
FHexVector BlockB;
@ -257,7 +282,10 @@ bool AAdventureMap::DiagIsReachable(AHexTile* InStart, FHexVector InDiagUnitVec)
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)];
int32 IndexA = GridIndex(InStart->Q + BlockA.Q, InStart->R + BlockA.R);
int32 IndexB = GridIndex(InStart->Q + BlockB.Q, InStart->R + BlockB.R);
if (!Grid.IsValidIndex(IndexA) || !Grid.IsValidIndex(IndexB)) { return false; }
AHexTile* HexA = Grid[IndexA];
AHexTile* HexB = Grid[IndexB];
return (HexA->bFree && HexB->bFree);
}

View File

@ -26,7 +26,7 @@ public:
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
TSubclassOf<AHexTile> BaseTileClass;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 GridSize = 100;
int32 GridSize = 60;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 TileSize = 100;
@ -90,6 +90,7 @@ public:
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> ShortcutAStar(TArray<AHexTile*> Path);
// considering a MapObjectManager class or moving pathfinding & search to PlayerController
UPROPERTY(BlueprintReadWrite, EditAnywhere)

View File

@ -26,9 +26,9 @@ public:
USceneComponent* SceneComponent;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Config")
UStaticMeshComponent* OrientHexMesh;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Config")
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Config")
bool bCollectable;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Config")
UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Config")
bool bActivatable;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Category = "Generation")