add VoxelStructure lua usertype

This commit is contained in:
MihailRis
2024-09-19 18:41:34 +03:00
parent bf9f81a98c
commit 88b0f8e3d6
17 changed files with 180 additions and 60 deletions
+17
View File
@@ -5,6 +5,8 @@
#include "coders/binary_json.hpp"
#include "world/Level.hpp"
#include "world/generator/VoxelStructure.hpp"
#include "engine.hpp"
#include "lua_custom_types.hpp"
using namespace scripting;
@@ -25,6 +27,21 @@ static int l_save_structure(lua::State* L) {
return 0;
}
static int l_load_structure(lua::State* L) {
auto paths = engine->getPaths();
auto filename = lua::require_string(L, 1);
auto path = paths->resolve(filename);
if (!std::filesystem::exists(path)) {
throw std::runtime_error("file "+path.u8string()+" does not exist");
}
auto map = files::read_binary_json(path);
auto structure = std::make_shared<VoxelStructure>();
structure->deserialize(map);
return lua::newuserdata<lua::LuaVoxelStructure>(L, std::move(structure));
}
const luaL_Reg generationlib[] = {
{"save_structure", lua::wrap<l_save_structure>},
{"load_structure", lua::wrap<l_load_structure>},
{NULL, NULL}};