Skip to content

Commit

Permalink
Compatible with UnrealEngine version 4.27.2
Browse files Browse the repository at this point in the history
Added link to trailer video to README.md
  • Loading branch information
shun126 committed Mar 15, 2023
1 parent a1c7e55 commit 4a9a247
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 88 deletions.
6 changes: 3 additions & 3 deletions DungeonGenerator.uplugin
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"FileVersion": 3,
"Version": 2,
"VersionName": "1.01",
"Version": 3,
"VersionName": "1.2",
"FriendlyName": "DungeonGenerator",
"Description": "Generates a three-dimensional dungeon.",
"Category": "Other",
Expand Down Expand Up @@ -32,4 +32,4 @@
]
}
]
}
}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
[![license](https://img.shields.io/github/license/shun126/DungeonGenerator)](https://github.com/shun126/DungeonGenerator/blob/main/LICENSE)
[![release](https://img.shields.io/github/v/release/shun126/DungeonGenerator)](https://github.com/shun126/DungeonGenerator/releases)
[![downloads](https://img.shields.io/github/downloads/shun126/DungeonGenerator/total)](https://github.com/shun126/DungeonGenerator/releases)
[![stars](https://img.shields.io/github/stars/shun126/DungeonGenerator?style=social)](https://github.com/shun126/DungeonGenerator/stargazers)
[![youtube](https://img.shields.io/youtube/views/HIW4mRt2_AA?style=social)](https://youtu.be/HIW4mRt2_AA)

![DungeonGeneratorPlugin](https://github.com/shun126/DungeonGenerator/raw/main/Document/DungeonGenerator03.jpg)

Trailer: [YouTube](https://youtu.be/HIW4mRt2_AA)

# 🚩 Table of Contents
- [Why Dungeon Generator?](#-why-dungeon-generator)
- [Features](#-features)
Expand Down Expand Up @@ -44,7 +46,7 @@ The dungeon generator was based on Vazgriz's algorithm. You can read more about
This is an easy to use. Simply drop the DungeonGenerateActor into your level, set the grid scale and number of rooms and start generating out your structures. Please read the [Wiki](https://github.com/shun126/DungeonGenerator/wiki) for more information.

# 🔧 Requirements
* [Unreal Engine 5.1.1](https://www.unrealengine.com/unreal-engine-5)
* [Unreal Engine 5.1.1 or Unreal Engine 4.27.2](https://www.unrealengine.com/)
* [Visual Studio 2022](https://visualstudio.microsoft.com/)

# 📜 License
Expand Down
14 changes: 9 additions & 5 deletions Source/DungeonGenerator/DungeonGenerator.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DungeonGenerator(ReadOnlyTargetRules Target) : base(Target)
new string[]
{
});

PrivateIncludePaths.AddRange(
new string[]
{
Expand All @@ -27,24 +27,28 @@ public DungeonGenerator(ReadOnlyTargetRules Target) : base(Target)
{
"Core",
});

PrivateDependencyModuleNames.AddRange(
new string[]
{
"CoreUObject",
"Engine",
"NavigationSystem",
});
"JsonUtilities"
});
if (Target.bBuildEditor)
{
PrivateDependencyModuleNames.AddRange(
new string[] {
"UnrealEd"
});
"UnrealEd",
});
}

DynamicallyLoadedModuleNames.AddRange(
new string[] {
});

//CppStandard = CppStandardVersion.Cpp17;
CppStandard = CppStandardVersion.Latest;
}
}
4 changes: 2 additions & 2 deletions Source/DungeonGenerator/Private/Core/Generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ namespace dungeon
// 中心から近い順に並べ替える
mRooms.sort([](const std::shared_ptr<const Room>& l, const std::shared_ptr<const Room>& r)
{
const double lsd = l->GetCenter().SquaredLength();
const double rsd = r->GetCenter().SquaredLength();
const double lsd = l->GetCenter().SizeSquared();
const double rsd = r->GetCenter().SizeSquared();
return lsd < rsd;
}
);
Expand Down
1 change: 1 addition & 0 deletions Source/DungeonGenerator/Private/Core/Room.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All Rights Reserved.
#include "Identifier.h"
#include "Math/Math.h"
#include "Math/Point.h"
#include <string_view>

namespace dungeon
{
Expand Down
7 changes: 4 additions & 3 deletions Source/DungeonGenerator/Private/DungeonGenerateActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ void ADungeonGenerateActor::DrawDebugInfomation()
// プレイヤーの位置のボクセルグリッドのデバッグ情報を表示します
if (ShowVoxelGridTypeAtPlayerLocation)
{
if (APawn* playerPawn = GetValid(UGameplayStatics::GetPlayerPawn(this, 0)))
APawn* playerPawn = UGameplayStatics::GetPlayerPawn(this, 0);
if (IsValid(playerPawn))
{
TArray<FString> output;

Expand Down Expand Up @@ -606,7 +607,7 @@ void ADungeonGenerateActor::UnloadStreamLevels()

////////////////////////////////////////////////////////////////////////////////////////////////////
// BluePrint便利関数
int32 ADungeonGenerateActor::FindFloorHeight(const double z) const
int32 ADungeonGenerateActor::FindFloorHeight(const float z) const
{
const int32 gridZ = FindVoxelHeight(z);

Expand All @@ -617,7 +618,7 @@ int32 ADungeonGenerateActor::FindFloorHeight(const double z) const
return generator->FindFloor(gridZ);
}

int32 ADungeonGenerateActor::FindVoxelHeight(const double z) const
int32 ADungeonGenerateActor::FindVoxelHeight(const float z) const
{
if (DungeonGenerateParameter == nullptr)
return 0;
Expand Down
48 changes: 16 additions & 32 deletions Source/DungeonGenerator/Private/DungeonGenerateParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,32 @@ UDungeonGenerateParameter::UDungeonGenerateParameter(const FObjectInitializer& O
{
}

// aka: SelectDungeonMeshParts, SelectDungeonRandomObjectParts
FDungeonObjectParts* UDungeonGenerateParameter::SelectDungeonObjectParts(const size_t gridIndex, const dungeon::Grid& grid, dungeon::Random& random, const TArray<FDungeonObjectParts>& parts, const EDungeonPartsSelectionMethod partsSelectionMethod) const
int32 UDungeonGenerateParameter::SelectDungeonMeshPartsIndex(const size_t gridIndex, const dungeon::Grid& grid, dungeon::Random& random, const int32 size, const EDungeonPartsSelectionMethod partsSelectionMethod) const
{
const int32 size = parts.Num();
if (size <= 0)
return nullptr;

int32 index = 0;

switch (partsSelectionMethod)
{
case EDungeonPartsSelectionMethod::GridIndex:
index = gridIndex % size;
break;
return gridIndex % size;

case EDungeonPartsSelectionMethod::Direction:
index = grid.GetDirection().Get() % size;
break;
return grid.GetDirection().Get() % size;

case EDungeonPartsSelectionMethod::Random:
return random.Get<uint32_t>(size);

default:
index = random.Get<uint32_t>() % size;
break;
return gridIndex % size;
}
}

// aka: SelectDungeonMeshParts, SelectDungeonRandomObjectParts
FDungeonObjectParts* UDungeonGenerateParameter::SelectDungeonObjectParts(const size_t gridIndex, const dungeon::Grid& grid, dungeon::Random& random, const TArray<FDungeonObjectParts>& parts, const EDungeonPartsSelectionMethod partsSelectionMethod) const
{
const int32 size = parts.Num();
if (size <= 0)
return nullptr;

const int32 index = SelectDungeonMeshPartsIndex(gridIndex, grid, random, size, partsSelectionMethod);
FDungeonObjectParts* actorParts = const_cast<FDungeonObjectParts*>(&parts[index]);
return IsValid(actorParts->GetActorClass()) ? actorParts : nullptr;
}
Expand All @@ -184,24 +185,7 @@ FDungeonRandomObjectParts* UDungeonGenerateParameter::SelectDungeonRandomObjectP
if (size <= 0)
return nullptr;

int32 index = 0;

switch (partsSelectionMethod)
{
case EDungeonPartsSelectionMethod::GridIndex:
index = gridIndex % size;
break;

case EDungeonPartsSelectionMethod::Direction:
index = grid.GetDirection().Get() % size;
break;

case EDungeonPartsSelectionMethod::Random:
default:
index = random.Get<uint32_t>() % size;
break;
}

const int32 index = SelectDungeonMeshPartsIndex(gridIndex, grid, random, size, partsSelectionMethod);
FDungeonRandomObjectParts* actorParts = const_cast<FDungeonRandomObjectParts*>(&parts[index]);
if (!IsValid(actorParts->GetActorClass()))
return nullptr;
Expand Down
49 changes: 40 additions & 9 deletions Source/DungeonGenerator/Private/DungeonGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All Rights Reserved.
#include <GameFramework/PlayerStart.h>
#include <Engine/StaticMeshActor.h>
#include <Kismet/GameplayStatics.h>
#include <Misc/EngineVersionComparison.h>
#include <NavMesh/NavMeshBoundsVolume.h>
#include <NavMesh/RecastNavMesh.h>

Expand Down Expand Up @@ -690,20 +691,44 @@ void CDungeonGenerator::MovePlayerStart()

const EComponentMobility::Type mobility = rootComponent->Mobility;
rootComponent->SetMobility(EComponentMobility::Movable);
{
float cylinderRadius, cylinderHalfHeight;
rootComponent->CalcBoundingCylinder(cylinderRadius, cylinderHalfHeight);

FVector location = GetStartLocation();

const UDungeonGenerateParameter* parameter = mParameter.Get();
const auto offsetZ = IsValid(parameter) ? parameter->GetGridSize() : (cylinderHalfHeight * 2);
FHitResult hitResult;
if (playerStart->GetWorld()->LineTraceSingleByChannel(hitResult, location + FVector(0, 0, offsetZ), location, ECollisionChannel::ECC_Pawn))
{
location = hitResult.ImpactPoint;
}
location.Z += cylinderHalfHeight + heightMargine;

FVector location = GetStartLocation();
location.Z += rootComponent->GetLocalBounds().BoxExtent.Z + heightMargine;
playerStart->SetActorLocation(location);
playerStart->SetActorLocation(location);

FCollisionShape collisionShape;
#if UE_VERSION_OLDER_THAN(5, 0, 0)
collisionShape.SetBox(FVector(cylinderRadius, cylinderRadius, cylinderHalfHeight));
#else
collisionShape.SetBox(FVector3f(cylinderRadius, cylinderRadius, cylinderHalfHeight));
#endif
if (playerStart->GetWorld()->OverlapBlockingTestByChannel(location, playerStart->GetActorQuat(), ECollisionChannel::ECC_Pawn, collisionShape))
{
DUNGEON_GENERATOR_ERROR(TEXT("%s(PlayerStart)が何かに接触しています"), *playerStart->GetName());
}
}
rootComponent->SetMobility(mobility);
}
else
{
DUNGEON_GENERATOR_ERROR(TEXT("PlayerStartのRootComponentを設定して下さい"));
DUNGEON_GENERATOR_ERROR(TEXT("%s(PlayerStart)のRootComponentを設定して下さい"), *playerStart->GetName());
}
}
else
{
DUNGEON_GENERATOR_WARNING(TEXT("PlayerStartは発見できませんでした"));
}
}

Expand Down Expand Up @@ -737,10 +762,11 @@ AStaticMeshActor* CDungeonGenerator::SpawnStaticMeshActor(UStaticMesh* staticMes
AStaticMeshActor* actor = SpawnActorDeferred<AStaticMeshActor>(AStaticMeshActor::StaticClass(), folderPath, transform, spawnActorCollisionHandlingMethod);
if (!IsValid(actor))
return nullptr;
if (UStaticMeshComponent* mesh = GetValid(actor->GetStaticMeshComponent()))
{

UStaticMeshComponent* mesh = actor->GetStaticMeshComponent();
if (IsValid(mesh))
mesh->SetStaticMesh(staticMesh);
}

actor->FinishSpawning(transform);
return actor;
}
Expand Down Expand Up @@ -950,9 +976,14 @@ UTexture2D* CDungeonGenerator::GenerateMiniMapTexture(uint32_t& horizontalScale,
//UTexture2D* generateTexture = UTexture2D::CreateTransient(textureWidthHeight, textureWidthHeight, PF_A8);
UTexture2D* generateTexture = UTexture2D::CreateTransient(textureWidthHeight, textureWidthHeight, PF_G8);
{
auto lockedBulkData = generateTexture->GetPlatformData()->Mips[0].BulkData.Lock(LOCK_READ_WRITE);
#if UE_VERSION_OLDER_THAN(5, 0, 0)
auto mips = generateTexture->PlatformData->Mips[0];
#else
auto mips = generateTexture->PlatformData->Mips[0];
#endif
auto lockedBulkData = mips.BulkData.Lock(LOCK_READ_WRITE);
FMemory::Memcpy(lockedBulkData, pixels.get(), totalBufferSize);
generateTexture->GetPlatformData()->Mips[0].BulkData.Unlock();
mips.BulkData.Unlock();
}
//generateTexture->Filter = TextureFilter::TF_Nearest;
generateTexture->AddToRoot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ UDungeonMiniMapTexture* UDungeonMiniMapTextureLayer::GetByFloor(const uint8 floo
return nullptr;
}

UDungeonMiniMapTexture* UDungeonMiniMapTextureLayer::GetByHeight(const double height) const noexcept
UDungeonMiniMapTexture* UDungeonMiniMapTextureLayer::GetByHeight(const float height) const noexcept
{
const uint8_t voxelHeight = static_cast<uint8_t>(height / mGridSize);
return GetByFloor(mGenerator->FindFloor(voxelHeight));
Expand All @@ -67,7 +67,7 @@ int32 UDungeonMiniMapTextureLayer::GetFloorHeight() const noexcept

FVector2D UDungeonMiniMapTextureLayer::ToRelative(const FVector& location) const
{
if (DungeonMiniMapTextures.IsEmpty())
if (DungeonMiniMapTextures.Num() <= 0)
{
return FVector2D::ZeroVector;
}
Expand All @@ -78,7 +78,7 @@ FVector2D UDungeonMiniMapTextureLayer::ToRelative(const FVector& location) const
}
}

FVector2D UDungeonMiniMapTextureLayer::ToRelativeWithScale(const FVector& location, const double scale) const
FVector2D UDungeonMiniMapTextureLayer::ToRelativeWithScale(const FVector& location, const float scale) const
{
return ToRelative(location) * scale;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ void UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::EndTransacti
}
}

#if UE_VERSION_OLDER_THAN(5, 0, 0)
int32 UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::AddInstance(const FTransform& instanceTransforms)
{
// トランスフォーム変更に伴う更新が必要か設定
mNotDirty = mImmediate;

// コリジョンを無効化して物理エンジンへの通知を抑制
CheckAndDisableCollision();

// 親クラスを呼び出します
return Super::AddInstance(instanceTransforms);
}

TArray<int32> UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::AddInstances(const TArray<FTransform>& instanceTransforms, bool bShouldReturnIndices)
{
// トランスフォーム変更に伴う更新が必要か設定
mNotDirty = mImmediate;

// コリジョンを無効化して物理エンジンへの通知を抑制
CheckAndDisableCollision();

// 親クラスを呼び出します
return Super::AddInstances(instanceTransforms, bShouldReturnIndices);
}
#else
int32 UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::AddInstance(const FTransform& instanceTransforms, bool bWorldSpace)
{
// トランスフォーム変更に伴う更新が必要か設定
Expand All @@ -75,6 +100,7 @@ TArray<int32> UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::Add
// 親クラスを呼び出します
return Super::AddInstances(instanceTransforms, bShouldReturnIndices, bWorldSpace);
}
#endif

bool UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::RemoveInstance(int32 instanceIndex)
{
Expand All @@ -88,6 +114,7 @@ bool UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::RemoveInstan
return Super::RemoveInstance(instanceIndex);
}

#if UE_VERSION_NEWER_THAN(5, 0, 0)
bool UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::RemoveInstances(const TArray<int32>& instancesToRemove)
{
// トランスフォーム変更に伴う更新が必要か設定
Expand All @@ -99,6 +126,7 @@ bool UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::RemoveInstan
// 親クラスを呼び出します
return Super::RemoveInstances(instancesToRemove);
}
#endif

void UDungeonTransactionalHierarchicalInstancedStaticMeshComponent::ClearInstances()
{
Expand Down
Loading

0 comments on commit 4a9a247

Please sign in to comment.