From 001b9302124f7253723b7d4783cdb3bf83e1df94 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 21 Sep 2024 23:29:19 +0300 Subject: [PATCH] update world generation pipeline --- res/generators/default.files/tree1.vox | Bin 0 -> 299 bytes res/generators/default.files/tree2.vox | Bin 0 -> 291 bytes res/generators/default.lua | 15 ++++++++------- src/logic/scripting/lua/lua_custom_types.hpp | 1 + .../lua/usertypes/lua_type_heightmap.cpp | 4 ++++ .../scripting/scripting_world_generation.cpp | 8 ++++++-- src/world/generator/GeneratorDef.hpp | 3 ++- src/world/generator/WorldGenerator.cpp | 15 ++++++--------- src/world/generator/WorldGenerator.hpp | 2 +- 9 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 res/generators/default.files/tree1.vox create mode 100644 res/generators/default.files/tree2.vox diff --git a/res/generators/default.files/tree1.vox b/res/generators/default.files/tree1.vox new file mode 100644 index 0000000000000000000000000000000000000000..7dda751dfcc3095bd7f6523019ef26af1fc59f9e GIT binary patch literal 299 zcmV+`0o48tg2TNb}q1WgI_7uHAakqJp+6@@6rKew8 z5SFrqHWQqRe|{(kPE_!lMBUS)$e7+^pT!3Oe&E85`8@!zLH>d5BLBd4k$+&lG=F#e z#a4>`0oc&w*DLGiF+TIpA7*S-jrmtoTQbIFotyTW{xRoY`y9a1^mCtA&A;x8Hh+J= zhwr^Z(?2ZNAEK_ciQG)El z+JEUEOJBcceJS#fwX=Nxt?RYV4*&oF0000000000uo80U)A8lu)V{_nzG+0{n1;A- xyP>(A4i0UO`?r*yo4fg_kL@^}oV$-GKJ3DM*vS}&+@+HQCua}2>!n{%0092&n@9iv literal 0 HcmV?d00001 diff --git a/res/generators/default.files/tree2.vox b/res/generators/default.files/tree2.vox new file mode 100644 index 0000000000000000000000000000000000000000..98fc3bef1ffdf3fc90a02b808229d7e6cb64fe3e GIT binary patch literal 291 zcmV+;0o?u{iwFP!000001MS(tYJ)%!#_`#m9xT1&*q7)FRfD4*-0Sf1q9DA7~f(2iiscfp%H* z&(~$ZSJb*3E;UR3YwH2nS+W!V3vi{cdthJW@Z+Y)*^$cpp z4%~yS`hIO~_*1w3zn*tk#{#s-t9M@Ze~)_yVB?*C0KTmAhjsn$aeV;wwpcq@=l}dW z1pIh?L)_x}0DO^O00000000000001hyC;V+A3t7B{bwo?yG}%oc}n|!n7YUF&Y>@9 p|CRH5_q6Vfsh`gWm*FQ#)SW)-PUbX~A)iEp`~r;2=8Rup003mslwbe= literal 0 HcmV?d00001 diff --git a/res/generators/default.lua b/res/generators/default.lua index cb6be030..41f7f500 100644 --- a/res/generators/default.lua +++ b/res/generators/default.lua @@ -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 diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp index 67faeef4..44fcd511 100644 --- a/src/logic/scripting/lua/lua_custom_types.hpp +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -39,6 +39,7 @@ namespace lua { std::shared_ptr map; std::unique_ptr noise; public: + LuaHeightmap(const std::shared_ptr& map); LuaHeightmap(uint width, uint height); virtual ~LuaHeightmap(); diff --git a/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp b/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp index 3982a5be..8a58e099 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_heightmap.cpp @@ -16,6 +16,10 @@ using namespace lua; +LuaHeightmap::LuaHeightmap(const std::shared_ptr& map) + : map(map), noise(std::make_unique(fnlCreateState())) { +} + LuaHeightmap::LuaHeightmap(uint width, uint height) : map(std::make_shared(width, height)), noise(std::make_unique(fnlCreateState())) diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index 2eb4fd39..99605da6 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -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 biomes; @@ -99,7 +101,8 @@ public: } std::vector 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 ) override { std::vector 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(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); diff --git a/src/world/generator/GeneratorDef.hpp b/src/world/generator/GeneratorDef.hpp index d038c986..8f3c3aea 100644 --- a/src/world/generator/GeneratorDef.hpp +++ b/src/world/generator/GeneratorDef.hpp @@ -116,7 +116,8 @@ public: const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed) = 0; virtual std::vector 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) = 0; /// @brief Get generator biomes virtual const std::vector& getBiomes() const = 0; diff --git a/src/world/generator/WorldGenerator.cpp b/src/world/generator/WorldGenerator.cpp index d22512b4..12078ae5 100644 --- a/src/world/generator/WorldGenerator.cpp +++ b/src/world/generator/WorldGenerator.cpp @@ -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; diff --git a/src/world/generator/WorldGenerator.hpp b/src/world/generator/WorldGenerator.hpp index 3b3fc12e..b8685599 100644 --- a/src/world/generator/WorldGenerator.hpp +++ b/src/world/generator/WorldGenerator.hpp @@ -18,7 +18,7 @@ struct Biome; class VoxelStructure; enum class ChunkPrototypeLevel { - VOID=0, STRUCTURES, BIOMES, HEIGHTMAP + VOID=0, BIOMES, HEIGHTMAP, STRUCTURES }; struct ChunkPrototype {