add world generator level 2 (heightmaps)

This commit is contained in:
MihailRis 2024-09-11 15:23:09 +03:00
parent 8268176527
commit 8516cf4f7f
3 changed files with 17 additions and 10 deletions

View File

@ -112,10 +112,10 @@ local function _generate_biome_parameters(x, y, w, h, seed, s)
end end
function generate_biome_parameters(x, y, w, h, seed) function generate_biome_parameters(x, y, w, h, seed)
local bpd = 4 local bpd = 8
local tmap, hmap = _generate_biome_parameters( local tmap, hmap = _generate_biome_parameters(
math.floor(x/bpd), math.floor(y/bpd), math.floor(x/bpd), math.floor(y/bpd),
math.floor(w/bpd)+1, math.floor(h/bpd)+1, seed, bpd) math.floor(w/bpd)+1, math.floor(h/bpd)+1, seed, bpd)
tmap:resize(w+bpd, h+bpd, 'linear') tmap:resize(w+bpd, h+bpd, 'linear')
tmap:crop(0, 0, w, h) tmap:crop(0, 0, w, h)

View File

@ -11,7 +11,7 @@ static inline float smootherstep(float x) {
static inline float sample_at( static inline float sample_at(
const float* buffer, const float* buffer,
uint width, uint height, uint width,
uint x, uint y uint x, uint y
) { ) {
return buffer[y*width+x]; return buffer[y*width+x];
@ -36,11 +36,11 @@ static inline float sample_at(
switch (interp) { switch (interp) {
case InterpolationType::LINEAR: { case InterpolationType::LINEAR: {
float s00 = val; float s00 = val;
float s10 = sample_at(buffer, width, height, float s10 = sample_at(buffer, width,
ix + 1 < width ? ix + 1 : ix, iy); ix + 1 < width ? ix + 1 : ix, iy);
float s01 = sample_at(buffer, width, height, ix, float s01 = sample_at(buffer, width, ix,
iy + 1 < height ? iy + 1 : iy); iy + 1 < height ? iy + 1 : iy);
float s11 = sample_at(buffer, width, height, float s11 = sample_at(buffer, width,
ix + 1 < width ? ix + 1 : ix, iy + 1 < height ? iy + 1 : iy); ix + 1 < width ? ix + 1 : ix, iy + 1 < height ? iy + 1 : iy);
float a00 = s00; float a00 = s00;

View File

@ -38,6 +38,13 @@ WorldGenerator::WorldGenerator(
} }
prototypes[{x, z}] = generatePrototype(x, z); prototypes[{x, z}] = generatePrototype(x, z);
}); });
surroundMap.setLevelCallback(2, [this](int const x, int const z) {
const auto& found = prototypes.find({x, z});
if (found == prototypes.end()) {
throw std::runtime_error("prototype not found");
}
generateHeightmap(found->second.get(), x, z);
});
} }
static inline void generate_pole( static inline void generate_pole(
@ -119,6 +126,9 @@ std::unique_ptr<ChunkPrototype> WorldGenerator::generatePrototype(
void WorldGenerator::generateHeightmap( void WorldGenerator::generateHeightmap(
ChunkPrototype* prototype, int chunkX, int chunkZ ChunkPrototype* prototype, int chunkX, int chunkZ
) { ) {
if (prototype->level >= ChunkPrototypeLevel::HEIGHTMAP) {
return;
}
prototype->heightmap = def.script->generateHeightmap( prototype->heightmap = def.script->generateHeightmap(
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed); {chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed);
prototype->level = ChunkPrototypeLevel::HEIGHTMAP; prototype->level = ChunkPrototypeLevel::HEIGHTMAP;
@ -126,12 +136,11 @@ void WorldGenerator::generateHeightmap(
void WorldGenerator::update(int centerX, int centerY, int loadDistance) { void WorldGenerator::update(int centerX, int centerY, int loadDistance) {
surroundMap.setCenter(centerX, centerY); surroundMap.setCenter(centerX, centerY);
surroundMap.resize(loadDistance); surroundMap.resize(loadDistance + 1 /* additional safety padding */);
surroundMap.setCenter(centerX, centerY); surroundMap.setCenter(centerX, centerY);
} }
void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) { void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
//timeutil::ScopeLogTimer log(555);
surroundMap.completeAt(chunkX, chunkZ); surroundMap.completeAt(chunkX, chunkZ);
const auto& found = prototypes.find({chunkX, chunkZ}); const auto& found = prototypes.find({chunkX, chunkZ});
@ -140,8 +149,6 @@ void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) {
} }
auto prototype = found->second.get(); auto prototype = found->second.get();
generateHeightmap(prototype, chunkX, chunkZ);
const auto values = prototype->heightmap->getValues(); const auto values = prototype->heightmap->getValues();
uint seaLevel = def.script->getSeaLevel(); uint seaLevel = def.script->getSeaLevel();