implemented diagonal movement

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

View File

@ -19,8 +19,8 @@ AAdventureCameraPawn::AAdventureCameraPawn()
PrimaryActorTick.bCanEverTick = true; PrimaryActorTick.bCanEverTick = true;
ESASize = .01; ESASize = .01;
BaseScrollSpeed = 16; BaseScrollSpeed = 20;
ScrollAccelleration = BaseScrollSpeed * 2.4; ScrollAccelleration = BaseScrollSpeed * 3;
ESAMaxBoundSlope = 1 / (1 - (1-ESASize)); ESAMaxBoundSlope = 1 / (1 - (1-ESASize));
ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope; ESAMaxBoundIntercept = 1 - ESAMaxBoundSlope;
@ -73,13 +73,13 @@ void AAdventureCameraPawn::SetupPlayerInputComponent(UInputComponent* PlayerInpu
void AAdventureCameraPawn::ScrollSide(float AxisValue) void AAdventureCameraPawn::ScrollSide(float AxisValue)
{ {
float DeltaLoc = AxisValue * BaseScrollSpeed * 3; float DeltaLoc = AxisValue * BaseScrollSpeed * 1.7;
AddActorLocalOffset(FVector(DeltaLoc, 0, 0)); AddActorLocalOffset(FVector(DeltaLoc, 0, 0));
} }
void AAdventureCameraPawn::ScrollVert(float AxisValue) void AAdventureCameraPawn::ScrollVert(float AxisValue)
{ {
float DeltaLoc = AxisValue * BaseScrollSpeed * -3; float DeltaLoc = AxisValue * BaseScrollSpeed * -1.7;
AddActorLocalOffset(FVector(0, DeltaLoc, 0)); AddActorLocalOffset(FVector(0, DeltaLoc, 0));
} }

View File

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

View File

@ -5,6 +5,7 @@
#include "CoreMinimal.h" #include "CoreMinimal.h"
#include "GameFramework/Actor.h" #include "GameFramework/Actor.h"
#include "Containers/Map.h" #include "Containers/Map.h"
#include "HexVector.h"
#include "AdventureMap.generated.h" #include "AdventureMap.generated.h"
class AHexTile; class AHexTile;
@ -20,16 +21,14 @@ public:
// Sets default values for this actor's properties // Sets default values for this actor's properties
AAdventureMap(); AAdventureMap();
UPROPERTY()
UWorld* World;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
TSubclassOf<AHexTile> BaseTileClass; TSubclassOf<AHexTile> BaseTileClass;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
TSubclassOf<ACharacter> BasePartyCharacterClass;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 GridSize = 100; // squared is the number of Tiles int32 GridSize = 100; // squared is the number of Tiles
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config") UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category = "Config")
int32 TileSize = 100; int32 TileSize = 100;
UPROPERTY()
UWorld* World;
UFUNCTION(BlueprintCallable, Category = "Generation") UFUNCTION(BlueprintCallable, Category = "Generation")
void MakeGrid(); void MakeGrid();
@ -38,43 +37,58 @@ public:
UPROPERTY(BlueprintReadOnly, Category = "Generation") UPROPERTY(BlueprintReadOnly, Category = "Generation")
TArray<AHexTile*> Grid; TArray<AHexTile*> Grid;
UFUNCTION(BlueprintCallable, Category = "Utility")
int32 GridIndex(int32 q, int32 r);
UFUNCTION(BlueprintCallable, Category = "Utility")
AHexTile* RandomHex();
// Cardinal direction vectors
UPROPERTY(BlueprintReadOnly, VisibleAnywhere) //diag
FHexVector N = FHexVector(1, -2);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
FHexVector NNE = FHexVector(1, -1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere) //diag
FHexVector ENE = FHexVector(2, -1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
FHexVector E = FHexVector(1, 0);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere) //diag
FHexVector ESE = FHexVector(1, 1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
FHexVector SSE = FHexVector(0, 1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere) //diag
FHexVector S = FHexVector(-1, 2);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
FHexVector SSW = FHexVector(-1, 1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere) //diag
FHexVector WSW = FHexVector(-2, 1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
FHexVector W = FHexVector(-1, 0);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere) //diag
FHexVector WNW = FHexVector(-1, -1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
FHexVector NNW = FHexVector(0, -1);
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
TArray<FHexVector> NeighborUnitVectors;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere)
TArray<FHexVector> DiagonalUnitVectors;
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Neighbors(AHexTile* OfHex, bool bFreeOnly);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> FreeDiags(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TSet<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> FindPathAStar(AHexTile* Start, AHexTile* Goal, bool bDiags);
UPROPERTY(BlueprintReadWrite, EditAnywhere) UPROPERTY(BlueprintReadWrite, EditAnywhere)
TMap<AHexTile*, AMapObject*> MapObjects; TMap<AHexTile*, AMapObject*> MapObjects;
UPROPERTY(BlueprintReadOnly) UPROPERTY(BlueprintReadOnly)
TMap<int32, AMapObject*> MapObjectsByID; TMap<int32, AMapObject*> MapObjectsByID;
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
int32 IncID = -1; int32 IncID = -1;
UFUNCTION(BlueprintCallable, Category = "Runtime")
int32 GridIndex(int32 q, int32 r);
UFUNCTION(BlueprintCallable, Category = "Runtime")
AHexTile* RandomHex();
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Neighbors(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> Diagonals(AHexTile* OfHex);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> BreadthFirstSearch(AHexTile* Start, int32 Radius);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> AStar(AHexTile* Start, AHexTile* Goal);
UFUNCTION(BlueprintCallable, Category = "Runtime")
TArray<AHexTile*> LinkPath(AHexTile* Start, AHexTile* Goal);
// Cardinal direction vectors
TPair<int32, int32> N = TPair<int32,int32>(1, -2); //diag
TPair<int32, int32> NNE = TPair<int32,int32>(1, -1);
TPair<int32, int32> ENE = TPair<int32,int32>(2, -1); //diag
TPair<int32, int32> E = TPair<int32,int32>(1, 0);
TPair<int32, int32> ESE = TPair<int32,int32>(1, 1); //diag
TPair<int32, int32> SSE = TPair<int32,int32>(0, 1);
TPair<int32, int32> S = TPair<int32,int32>(-1, 2); //diag
TPair<int32, int32> SSW = TPair<int32,int32>(-1, 1);
TPair<int32, int32> WSW = TPair<int32,int32>(-2, 1); //diag
TPair<int32, int32> W = TPair<int32,int32>(-1, 0);
TPair<int32, int32> WNW = TPair<int32,int32>(-1, -1); //diag
TPair<int32, int32> NNW = TPair<int32,int32>(0, -1);
TArray<TPair<int32, int32>> NBVectors;
TArray<TPair<int32, int32>> NBVectorsDiag;
protected: protected:
// Called when the game starts or when spawned // Called when the game starts or when spawned

View File

@ -48,7 +48,7 @@ void AAdventurePlayerController::LeftClick()
TArray<AHexTile*> AAdventurePlayerController::Vision() TArray<AHexTile*> AAdventurePlayerController::Vision()
{ {
TArray<AHexTile*> Results; TArray<AHexTile*> Results;
TArray<AHexTile*> Visible = MapRef->BreadthFirstSearch(CurrentHex, 7); TSet<AHexTile*> Visible = MapRef->BreadthFirstSearch(CurrentHex, 4);
for (auto& Hex : Visible) { for (auto& Hex : Visible) {
if (ExploredHexes.Contains(Hex)) { continue; } if (ExploredHexes.Contains(Hex)) { continue; }
Results.Add(Hex); Results.Add(Hex);

View File

@ -46,15 +46,15 @@ public:
// Pathfinding // Pathfinding
UPROPERTY(BlueprintReadWrite, Category = "Movement") UPROPERTY(BlueprintReadWrite, Category = "Movement")
int32 MoveCost = 1; float MoveCost = 10;
UPROPERTY(BlueprintReadWrite, VisibleInstanceOnly, Category = "Movement") UPROPERTY(BlueprintReadWrite, VisibleInstanceOnly, Category = "Movement")
AHexTile* CameFrom; AHexTile* CameFrom;
UPROPERTY() UPROPERTY()
int32 FCost; float FCost;
UPROPERTY() UPROPERTY()
int32 GCost; float GCost;
UPROPERTY() UPROPERTY()
int32 HCost; float HCost;
// MapObject Placement // MapObject Placement
UPROPERTY(BlueprintReadWrite, VisibleAnywhere) UPROPERTY(BlueprintReadWrite, VisibleAnywhere)