update world generation pipeline

This commit is contained in:
MihailRis 2024-09-21 23:29:19 +03:00
parent f4793ac9db
commit 001b930212
9 changed files with 28 additions and 20 deletions

Binary file not shown.

Binary file not shown.

View File

@ -45,10 +45,12 @@ biomes = {
{value=0.2, weight=0.5},
},
sea_layers = {
{block="base:brick", height=1},
{block="base:water", height=-1},
},
layers = {
{block="base:stone", height=6},
{block="base:grass_block", height=1, below_sea_level=false},
{block="base:dirt", height=3, below_sea_level=false},
{block="base:stone", height=-1},
{block="base:bazalt", height=1},
}
@ -57,7 +59,7 @@ biomes = {
function load_structures()
local structures = {}
local names = {"tree0", "tower"}
local names = {"tree0", "tree1", "tree2", "tower"}
for i, name in ipairs(names) do
local filename = "core:default.files/"..name
debug.log("loading structure "..filename)
@ -66,9 +68,8 @@ function load_structures()
return structures
end
function place_structures(x, z, w, d, seed)
function place_structures(x, z, w, d, seed, hmap)
local placements = {}
local hmap = generate_heightmap(x, z, w, d, seed)
for i=0,math.floor(math.random()*3) do
local px = math.random() * w
local pz = math.random() * d
@ -76,15 +77,15 @@ function place_structures(x, z, w, d, seed)
if py <= sea_level then
goto continue
end
table.insert(placements, {0, {px-8, py, pz-8}})
table.insert(placements, {math.floor(math.random() * 3), {px-8, py, pz-8}})
::continue::
end
if math.random() < 0.03 then
if math.random() < 0.01 then
local px = math.random() * w
local pz = math.random() * d
local py = hmap:at(px, pz) * 256
table.insert(placements, {1, {px-8, py, pz-8}})
table.insert(placements, {3, {px-8, py, pz-8}})
end
return placements
end

View File

@ -39,6 +39,7 @@ namespace lua {
std::shared_ptr<Heightmap> map;
std::unique_ptr<fnl_state> noise;
public:
LuaHeightmap(const std::shared_ptr<Heightmap>& map);
LuaHeightmap(uint width, uint height);
virtual ~LuaHeightmap();

View File

@ -16,6 +16,10 @@
using namespace lua;
LuaHeightmap::LuaHeightmap(const std::shared_ptr<Heightmap>& map)
: map(map), noise(std::make_unique<fnl_state>(fnlCreateState())) {
}
LuaHeightmap::LuaHeightmap(uint width, uint height)
: map(std::make_shared<Heightmap>(width, height)),
noise(std::make_unique<fnl_state>(fnlCreateState()))

View File

@ -13,6 +13,8 @@
#include "data/dv.hpp"
#include "world/generator/GeneratorDef.hpp"
#include "util/timeutil.hpp"
class LuaGeneratorScript : public GeneratorScript {
scriptenv env;
std::vector<Biome> biomes;
@ -99,7 +101,8 @@ public:
}
std::vector<StructurePlacement> placeStructures(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed,
const std::shared_ptr<Heightmap>& heightmap
) override {
std::vector<StructurePlacement> placements;
@ -110,7 +113,8 @@ public:
lua::pushivec_stack(L, offset);
lua::pushivec_stack(L, size);
lua::pushinteger(L, seed);
if (lua::call_nothrow(L, 5, 1)) {
lua::newuserdata<lua::LuaHeightmap>(L, heightmap);
if (lua::call_nothrow(L, 6, 1)) {
int len = lua::objlen(L, -1);
for (int i = 1; i <= len; i++) {
lua::rawgeti(L, i);

View File

@ -116,7 +116,8 @@ public:
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed) = 0;
virtual std::vector<StructurePlacement> placeStructures(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed) = 0;
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed,
const std::shared_ptr<Heightmap>& heightmap) = 0;
/// @brief Get generator biomes
virtual const std::vector<Biome>& getBiomes() const = 0;

View File

@ -41,18 +41,14 @@ 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");
}
generateStructures(requirePrototype(x, z), x, z);
});
surroundMap.setLevelCallback(3, [this](int const x, int const z) {
generateBiomes(requirePrototype(x, z), x, z);
});
surroundMap.setLevelCallback(4, [this](int const x, int const z) {
surroundMap.setLevelCallback(3, [this](int const x, int const z) {
generateHeightmap(requirePrototype(x, z), x, z);
});
surroundMap.setLevelCallback(4, [this](int const x, int const z) {
generateStructures(requirePrototype(x, z), x, z);
});
structures = def.script->loadStructures();
for (auto& structure : structures) {
@ -145,7 +141,8 @@ void WorldGenerator::generateStructures(
return;
}
util::concat(prototype.structures, def.script->placeStructures(
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed
{chunkX * CHUNK_W, chunkZ * CHUNK_D}, {CHUNK_W, CHUNK_D}, seed,
prototype.heightmap
));
for (const auto& placement : prototype.structures) {
const auto& offset = placement.position;

View File

@ -18,7 +18,7 @@ struct Biome;
class VoxelStructure;
enum class ChunkPrototypeLevel {
VOID=0, STRUCTURES, BIOMES, HEIGHTMAP
VOID=0, BIOMES, HEIGHTMAP, STRUCTURES
};
struct ChunkPrototype {