I’ve been tinkering away on this for a while and I’ve kind of hit a wall for now. My editor goes unresponsive when I try to run it. I’m just experimenting on attempting some procedural generation. Right now I’m just trying to get the first iteration to work, then I was going to use Cellular Automata to refine the result into something, potentially useful. The example I’ve been emulating is a C# Script for Unity, so I’ve been attempting to translate that into UE4/C++. I commented out the seed based pseudo random number generator as a test, still didn’t help. Any tips would be appreciated.
I’m using 4.10 if that’s important.
.H
#pragma once
#include "GameFramework/Actor.h"
#include "ACaveGenerator.generated.h"
UCLASS()
class PROCTEST_API AACaveGenerator : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AACaveGenerator();
// Called when the game starts or when spawned
virtual void BeginPlay() override;
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Map Settings", meta = (ClampMin = "0", ClampMax = "100", UIMin = "0", UIMax = "100"))
uint8 RandomFillPercentage;
//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Map Settings", meta = (ClampMin ="0", UIMin = "0"))
//int32 Seed;
//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Map Settings")
//bool useRandomSeed;
//UPROPERTY()
//FCaveMap GeneratedCaveMap;
//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Map Settings")
//uint8 MapWidth;
//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Map Settings")
//uint8 MapHeight;
private:
FDateTime DateAndTime;
uint8 CaveMap [10][10] = {0};
//uint32 safeSeed;
// Generate the Map
void GenerateMap();
void RandomFillMap();
void DrawDebug();
};
.CPP
#include "ProcTest.h"
#include "ACaveGenerator.h"
#include "DrawDebugHelpers.h"
// Sets default values
AACaveGenerator::AACaveGenerator()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Set Defaults
RandomFillPercentage = 50;
//useRandomSeed = false;
//Seed = 10;
//safeSeed = uint32(Seed);
//MapWidth = 100;
//MapHeight = 100;
}
// Called when the game starts or when spawned
void AACaveGenerator::BeginPlay()
{
Super::BeginPlay();
GenerateMap();
DrawDebug();
}
// Called every frame
void AACaveGenerator::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}
void AACaveGenerator::GenerateMap()
{
RandomFillMap();
}
void AACaveGenerator::RandomFillMap()
{
/*if (useRandomSeed) {
uint64 rawtime = DateAndTime.ToUnixTimestamp();
safeSeed = uint32 (rawtime);
}*/
//FRandomStream psuRandom = FRandomStream(Seed);
for (uint8 x = 0; x < 10; x++) {
for (uint8 y = 0; x < 10; y++) {
CaveMap[x][y] = (FMath::RandRange(0, 100) < RandomFillPercentage) ? 1 : 0;
// iterate the seed
//uint32 cSeed = psuRandom.GetCurrentSeed();
//uint32 nSeed = (cSeed * 3) + 1;
//psuRandom.Initialize(nSeed);
}
}
}
void AACaveGenerator::DrawDebug()
{
if (CaveMap != NULL) {
for (uint8 x = 0; x < 10; x++) {
for (uint8 y = 0; x < 10; y++) {
FColor TestColor = (CaveMap[x][y] == 1) ? FColor(0, 0, 0, 1) : FColor(1, 1, 1, 1);
FVector position = FVector(-10 / 2 + x + 0.5f, -10 / 2 + y + 0.5f, 0);
DrawDebugSolidPlane(GetWorld(), FPlane(), position, FVector2D(1, 1), TestColor, true, 60.0F, 0);
}
}
}
}