diff --git a/res/generators/default.lua b/res/generators/default.lua index 3641db57..89b03707 100644 --- a/res/generators/default.lua +++ b/res/generators/default.lua @@ -55,6 +55,12 @@ biomes = { } } +function load_structures() + local structures = {} + table.insert(structures, generation.load_structure("core:default.files/tree0")) + return structures +end + local function _generate_heightmap(x, y, w, h, seed, s) local umap = Heightmap(w, h) local vmap = Heightmap(w, h) diff --git a/res/scripts/world.lua b/res/scripts/world.lua index 18ecea91..964100e7 100644 --- a/res/scripts/world.lua +++ b/res/scripts/world.lua @@ -1,4 +1,3 @@ -- use for engine development tests -- must be empty in release -- must not be modified by content-packs -print(generation.load_structure("core:default.files/tree0")) diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index a296de22..b291d600 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -30,6 +30,26 @@ public: seaLevel(seaLevel) {} + std::vector> loadStructures() override { + std::vector> structures; + + auto L = lua::get_main_thread(); + lua::pushenv(L, *env); + if (lua::getfield(L, "load_structures")) { + if (lua::call_nothrow(L, 0, 1)) { + for (int i = 1; i <= lua::objlen(L, -1); i++) { + lua::rawgeti(L, i); + if (auto lstruct = + lua::touserdata(L, -1)) { + structures.push_back(lstruct->getStructure()); + } + } + } + } + lua::pop(L); + return structures; + } + std::shared_ptr generateHeightmap( const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed ) override { diff --git a/src/world/generator/GeneratorDef.hpp b/src/world/generator/GeneratorDef.hpp index 3b5df181..f219e6d4 100644 --- a/src/world/generator/GeneratorDef.hpp +++ b/src/world/generator/GeneratorDef.hpp @@ -101,7 +101,7 @@ public: virtual ~GeneratorScript() = default; /// @brief Load all structures - //virtual std::vector> loadStructures() = 0; + virtual std::vector> loadStructures() = 0; /// @brief Generates a heightmap with values in range 0..1 /// @param offset position of the heightmap in the world diff --git a/src/world/generator/WorldGenerator.cpp b/src/world/generator/WorldGenerator.cpp index 25899272..5991a699 100644 --- a/src/world/generator/WorldGenerator.cpp +++ b/src/world/generator/WorldGenerator.cpp @@ -46,6 +46,8 @@ WorldGenerator::WorldGenerator( } generateHeightmap(found->second.get(), x, z); }); + + structures = def.script->loadStructures(); } WorldGenerator::~WorldGenerator() {} diff --git a/src/world/generator/WorldGenerator.hpp b/src/world/generator/WorldGenerator.hpp index 4241433e..8d6306c9 100644 --- a/src/world/generator/WorldGenerator.hpp +++ b/src/world/generator/WorldGenerator.hpp @@ -62,7 +62,7 @@ class WorldGenerator { /// @brief Chunk prototypes loading surround map SurroundMap surroundMap; - std::vector> structures; + std::vector> structures; /// @brief Generate chunk prototype (see ChunkPrototype) /// @param x chunk position X divided by CHUNK_W