From 8516cf4f7f7ef3e3a7c7a31cf6e4af4e6900ebc6 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 11 Sep 2024 15:23:09 +0300 Subject: [PATCH] add world generator level 2 (heightmaps) --- res/generators/default.lua | 4 ++-- src/maths/Heightmap.cpp | 8 ++++---- src/world/generator/WorldGenerator.cpp | 15 +++++++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/res/generators/default.lua b/res/generators/default.lua index 10f3b08c..3641db57 100644 --- a/res/generators/default.lua +++ b/res/generators/default.lua @@ -112,10 +112,10 @@ local function _generate_biome_parameters(x, y, w, h, seed, s) end function generate_biome_parameters(x, y, w, h, seed) - local bpd = 4 + local bpd = 8 local tmap, hmap = _generate_biome_parameters( 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:crop(0, 0, w, h) diff --git a/src/maths/Heightmap.cpp b/src/maths/Heightmap.cpp index 165c0040..d3b048de 100644 --- a/src/maths/Heightmap.cpp +++ b/src/maths/Heightmap.cpp @@ -11,7 +11,7 @@ static inline float smootherstep(float x) { static inline float sample_at( const float* buffer, - uint width, uint height, + uint width, uint x, uint y ) { return buffer[y*width+x]; @@ -36,11 +36,11 @@ static inline float sample_at( switch (interp) { case InterpolationType::LINEAR: { float s00 = val; - float s10 = sample_at(buffer, width, height, + float s10 = sample_at(buffer, width, 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); - 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); float a00 = s00; diff --git a/src/world/generator/WorldGenerator.cpp b/src/world/generator/WorldGenerator.cpp index c1e568fe..3e0216a4 100644 --- a/src/world/generator/WorldGenerator.cpp +++ b/src/world/generator/WorldGenerator.cpp @@ -38,6 +38,13 @@ WorldGenerator::WorldGenerator( } 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( @@ -119,6 +126,9 @@ std::unique_ptr WorldGenerator::generatePrototype( void WorldGenerator::generateHeightmap( ChunkPrototype* prototype, int chunkX, int chunkZ ) { + if (prototype->level >= ChunkPrototypeLevel::HEIGHTMAP) { + return; + } prototype->heightmap = def.script->generateHeightmap( {chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed); prototype->level = ChunkPrototypeLevel::HEIGHTMAP; @@ -126,12 +136,11 @@ void WorldGenerator::generateHeightmap( void WorldGenerator::update(int centerX, int centerY, int loadDistance) { surroundMap.setCenter(centerX, centerY); - surroundMap.resize(loadDistance); + surroundMap.resize(loadDistance + 1 /* additional safety padding */); surroundMap.setCenter(centerX, centerY); } void WorldGenerator::generate(voxel* voxels, int chunkX, int chunkZ) { - //timeutil::ScopeLogTimer log(555); surroundMap.completeAt(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(); - generateHeightmap(prototype, chunkX, chunkZ); - const auto values = prototype->heightmap->getValues(); uint seaLevel = def.script->getSeaLevel();