140 lines
3.4 KiB
C++
140 lines
3.4 KiB
C++
#include "LevelGenerator.h"
|
|
#include "Base.h"
|
|
#include <time.h>
|
|
#include <random>
|
|
|
|
FastNoiseLite perlin, os, cellular;
|
|
int seed = 138;
|
|
|
|
|
|
|
|
LevelGenerator::LevelGenerator()
|
|
{
|
|
perlin.SetSeed(seed);
|
|
perlin.SetNoiseType(FastNoiseLite::NoiseType_Perlin);
|
|
perlin.SetFrequency(.01F);
|
|
|
|
os.SetSeed(seed);
|
|
os.SetNoiseType(FastNoiseLite::NoiseType_OpenSimplex2);
|
|
os.SetFrequency(.01F);
|
|
|
|
cellular.SetSeed(seed);
|
|
cellular.SetNoiseType(FastNoiseLite::NoiseType_Cellular);
|
|
cellular.SetFrequency(.1F);
|
|
}
|
|
|
|
// Change seed midgame - not for production use
|
|
void LevelGenerator::setSeed()
|
|
{
|
|
perlin.SetSeed(time(0));
|
|
os.SetSeed(time(0));
|
|
cellular.SetSeed(time(0));
|
|
}
|
|
|
|
void LevelGenerator::generateBlock()
|
|
{
|
|
for(int x = 0; x < 256; x++)
|
|
{
|
|
for(int z = 0; z < 256; z++)
|
|
{
|
|
for(int y = 0; y < 256; y++)
|
|
{
|
|
blockManager.addNode(0, 0, x, y, z);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
for(int x = 0; x < 256; x++)
|
|
{
|
|
for(int z = 0; z < 256; z++)
|
|
{
|
|
float cX = (float)x;
|
|
float cZ = (float)z;
|
|
for(int y = 0; y < 48 * abs(perlin.GetNoise(cX, cZ)) / 2 + abs(cellular.GetNoise(cX, cZ)) * 2 + abs(os.GetNoise(cX, cZ)) * 4 + 10; y++)
|
|
{
|
|
blockManager.addNode(1, 0, x, y, z);
|
|
}
|
|
}
|
|
}
|
|
|
|
terraformBlock();
|
|
}
|
|
|
|
void LevelGenerator::terraformBlock()
|
|
{
|
|
for(int x = 0; x < 256; x++)
|
|
{
|
|
for(int z = 0; z < 256; z++)
|
|
{
|
|
for(int y = 0; y < 256; y++)
|
|
{
|
|
int currentNode = blockManager.getNodeAt(x, y, z);
|
|
if(currentNode != 0)
|
|
{
|
|
if(y == 15 && (blockManager.getNodeAt(x, y + 1, z) == 0 || blockManager.getNodeAt(x, y + 1, z) == 20))
|
|
{
|
|
currentNode = 3;
|
|
}
|
|
|
|
else if(y < 15)
|
|
{
|
|
currentNode = 2;
|
|
}
|
|
|
|
else
|
|
{
|
|
continue;
|
|
}
|
|
|
|
blockManager.addNode(currentNode, 0, x, y, z);
|
|
}
|
|
else if(y < 16)
|
|
{
|
|
blockManager.addNode(20, 0, x, y, z); // Water
|
|
}
|
|
}
|
|
}
|
|
}
|
|
populate();
|
|
}
|
|
|
|
void LevelGenerator::populate()
|
|
{
|
|
for(int counter = 0; counter < 30; counter++)
|
|
{
|
|
int x = rand() % 256;
|
|
int z = rand() % 256;
|
|
|
|
for(int y = 10; y < 256; y++)
|
|
{
|
|
if(blockManager.getNodeAt(x, y, z) == 0)
|
|
{
|
|
if(blockManager.getNodeAt(x, y - 1, z) == 20)
|
|
{
|
|
break;
|
|
counter -= 1;
|
|
}
|
|
|
|
for(int y1 = y; y1 < y + 6; y1++)
|
|
{
|
|
blockManager.addNode(12, 0, x, y1, z);
|
|
}
|
|
|
|
for(int i = 0; i < 3; i++)
|
|
{
|
|
for(int j = 0; j < 3; j++)
|
|
{
|
|
for(int k = 0; k < 3; k++)
|
|
{
|
|
blockManager.addNode(13, 0, x + i - 1, y + j + 6, z + k - 1);
|
|
}
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|