add LuaGeneratorScript::loadStructures

This commit is contained in:
MihailRis 2024-09-21 01:32:38 +03:00
parent aa1fb26ea5
commit cba1a5c23e
6 changed files with 30 additions and 3 deletions

View File

@ -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)

View File

@ -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"))

View File

@ -30,6 +30,26 @@ public:
seaLevel(seaLevel)
{}
std::vector<std::shared_ptr<VoxelStructure>> loadStructures() override {
std::vector<std::shared_ptr<VoxelStructure>> 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<lua::LuaVoxelStructure>(L, -1)) {
structures.push_back(lstruct->getStructure());
}
}
}
}
lua::pop(L);
return structures;
}
std::shared_ptr<Heightmap> generateHeightmap(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed
) override {

View File

@ -101,7 +101,7 @@ public:
virtual ~GeneratorScript() = default;
/// @brief Load all structures
//virtual std::vector<std::shared_ptr<VoxelStructure>> loadStructures() = 0;
virtual std::vector<std::shared_ptr<VoxelStructure>> loadStructures() = 0;
/// @brief Generates a heightmap with values in range 0..1
/// @param offset position of the heightmap in the world

View File

@ -46,6 +46,8 @@ WorldGenerator::WorldGenerator(
}
generateHeightmap(found->second.get(), x, z);
});
structures = def.script->loadStructures();
}
WorldGenerator::~WorldGenerator() {}

View File

@ -62,7 +62,7 @@ class WorldGenerator {
/// @brief Chunk prototypes loading surround map
SurroundMap surroundMap;
std::vector<std::unique_ptr<VoxelStructure>> structures;
std::vector<std::shared_ptr<VoxelStructure>> structures;
/// @brief Generate chunk prototype (see ChunkPrototype)
/// @param x chunk position X divided by CHUNK_W