add GeneratorScript

This commit is contained in:
MihailRis
2024-08-14 17:12:12 +03:00
parent 6f5eb6be48
commit 95cf451cc8
12 changed files with 196 additions and 51 deletions
+42 -1
View File
@@ -14,6 +14,9 @@
#include "items/ItemDef.hpp"
#include "logic/BlocksController.hpp"
#include "logic/LevelController.hpp"
#include "lua/lua_engine.hpp"
#include "lua/lua_custom_types.hpp"
#include "maths/Heightmap.hpp"
#include "objects/Entities.hpp"
#include "objects/EntityDef.hpp"
#include "objects/Player.hpp"
@@ -21,7 +24,7 @@
#include "util/timeutil.hpp"
#include "voxels/Block.hpp"
#include "world/Level.hpp"
#include "lua/lua_engine.hpp"
#include "world/generator/GeneratorDef.hpp"
using namespace scripting;
@@ -62,6 +65,15 @@ void scripting::initialize(Engine* engine) {
return std::make_shared<int>(0);
}
[[nodiscard]] scriptenv scripting::create_environment() {
auto L = lua::get_main_thread();
int id = lua::create_environment(L, 0);
return std::shared_ptr<int>(new int(id), [=](int* id) { //-V508
lua::removeEnvironment(L, *id);
delete id;
});
}
[[nodiscard]] scriptenv scripting::create_pack_environment(
const ContentPack& pack
) {
@@ -672,6 +684,35 @@ void scripting::load_entity_component(
lua::store_in(L, lua::CHUNKS_TABLE, name);
}
class LuaGeneratorScript : public GeneratorScript {
scriptenv env;
public:
LuaGeneratorScript(scriptenv env) : env(std::move(env)) {}
std::shared_ptr<Heightmap> generateHeightmap(
const glm::ivec2& offset, const glm::ivec2& size
) override {
auto L = lua::get_main_thread();
lua::pushenv(L, *env);
if (lua::getfield(L, "generate_heightmap")) {
lua::pushivec_stack(L, offset);
lua::pushivec_stack(L, size);
if (lua::call_nothrow(L, 4)) {
return lua::touserdata<lua::LuaHeightmap>(L, -1)->getHeightmap();
}
}
return std::make_shared<Heightmap>(size.x, size.y);
}
};
std::unique_ptr<GeneratorScript> scripting::load_generator(
const fs::path& file
) {
auto env = create_environment();
load_script(*env, "generator", file);
return std::make_unique<LuaGeneratorScript>(std::move(env));
}
void scripting::load_world_script(
const scriptenv& senv,
const std::string& prefix,