diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index b06e7258..eaab27d3 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -73,10 +73,6 @@ void scripting::initialize(Engine* engine) { return std::make_shared(0); } -[[nodiscard]] scriptenv scripting::create_environment() { - return lua::create_environment(lua::get_main_state()); -} - [[nodiscard]] scriptenv scripting::create_pack_environment( const ContentPack& pack ) { diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index 8a1a39cd..a34c21cf 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -52,7 +52,6 @@ namespace scripting { scriptenv get_root_environment(); scriptenv create_pack_environment(const ContentPack& pack); - scriptenv create_environment(); scriptenv create_doc_environment( const scriptenv& parent, const std::string& name ); diff --git a/src/logic/scripting/scripting_world_generation.cpp b/src/logic/scripting/scripting_world_generation.cpp index c8bb2799..c7655084 100644 --- a/src/logic/scripting/scripting_world_generation.cpp +++ b/src/logic/scripting/scripting_world_generation.cpp @@ -16,45 +16,42 @@ #include "files/files.hpp" #include "debug/Logger.hpp" +using namespace lua; + static debug::Logger logger("generator-scripting"); class LuaGeneratorScript : public GeneratorScript { - lua::State* L; + State* L; const GeneratorDef& def; scriptenv env; public: - LuaGeneratorScript( - lua::State* L, - const GeneratorDef& def, - scriptenv env) - : L(L), - def(def), - env(std::move(env)) - {} + LuaGeneratorScript(State* L, const GeneratorDef& def, scriptenv env) + : L(L), def(def), env(std::move(env)) { + } virtual ~LuaGeneratorScript() { env.reset(); - if (L != lua::get_main_state()) { - lua::close(L); + if (L != get_main_state()) { + close(L); } } std::shared_ptr generateHeightmap( const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd ) override { - lua::pushenv(L, *env); - if (lua::getfield(L, "generate_heightmap")) { - lua::pushivec_stack(L, offset); - lua::pushivec_stack(L, size); - lua::pushinteger(L, seed); - lua::pushinteger(L, bpd); - if (lua::call_nothrow(L, 6)) { - auto map = lua::touserdata(L, -1)->getHeightmap(); - lua::pop(L, 2); + pushenv(L, *env); + if (getfield(L, "generate_heightmap")) { + pushivec_stack(L, offset); + pushivec_stack(L, size); + pushinteger(L, seed); + pushinteger(L, bpd); + if (call_nothrow(L, 6)) { + auto map = touserdata(L, -1)->getHeightmap(); + pop(L, 2); return map; } } - lua::pop(L); + pop(L); return std::make_shared(size.x, size.y); } @@ -64,22 +61,22 @@ public: std::vector> maps; uint biomeParameters = def.biomeParameters; - lua::pushenv(L, *env); - if (lua::getfield(L, "generate_biome_parameters")) { - lua::pushivec_stack(L, offset); - lua::pushivec_stack(L, size); - lua::pushinteger(L, seed); - lua::pushinteger(L, bpd); - if (lua::call_nothrow(L, 6, biomeParameters)) { + pushenv(L, *env); + if (getfield(L, "generate_biome_parameters")) { + pushivec_stack(L, offset); + pushivec_stack(L, size); + pushinteger(L, seed); + pushinteger(L, bpd); + if (call_nothrow(L, 6, biomeParameters)) { for (int i = biomeParameters-1; i >= 0; i--) { maps.push_back( - lua::touserdata(L, -1-i)->getHeightmap()); + touserdata(L, -1-i)->getHeightmap()); } - lua::pop(L, 1+biomeParameters); + pop(L, 1+biomeParameters); return maps; } } - lua::pop(L); + pop(L); for (uint i = 0; i < biomeParameters; i++) { maps.push_back(std::make_shared(size.x, size.y)); } @@ -92,46 +89,46 @@ public: ) override { std::vector placements; - lua::stackguard _(L); - lua::pushenv(L, *env); - if (lua::getfield(L, "place_structures")) { - lua::pushivec_stack(L, offset); - lua::pushivec_stack(L, size); - lua::pushinteger(L, seed); - lua::newuserdata(L, heightmap); - lua::pushinteger(L, chunkHeight); - if (lua::call_nothrow(L, 7, 1)) { - int len = lua::objlen(L, -1); + stackguard _(L); + pushenv(L, *env); + if (getfield(L, "place_structures")) { + pushivec_stack(L, offset); + pushivec_stack(L, size); + pushinteger(L, seed); + newuserdata(L, heightmap); + pushinteger(L, chunkHeight); + if (call_nothrow(L, 7, 1)) { + int len = objlen(L, -1); for (int i = 1; i <= len; i++) { - lua::rawgeti(L, i); + rawgeti(L, i); - lua::rawgeti(L, 1); + rawgeti(L, 1); int structIndex = 0; - if (lua::isstring(L, -1)) { + if (isstring(L, -1)) { const auto& found = def.structuresIndices.find( - lua::require_string(L, -1) + require_string(L, -1) ); if (found != def.structuresIndices.end()) { structIndex = found->second; } } else { - structIndex = lua::tointeger(L, -1); + structIndex = tointeger(L, -1); } - lua::pop(L); + pop(L); - lua::rawgeti(L, 2); - glm::ivec3 pos = lua::tovec3(L, -1); - lua::pop(L); + rawgeti(L, 2); + glm::ivec3 pos = tovec3(L, -1); + pop(L); - lua::rawgeti(L, 3); - int rotation = lua::tointeger(L, -1) & 0b11; - lua::pop(L); + rawgeti(L, 3); + int rotation = tointeger(L, -1) & 0b11; + pop(L); - lua::pop(L); + pop(L); placements.emplace_back(structIndex, pos, rotation); } - lua::pop(L); + pop(L); } } return placements; @@ -141,28 +138,26 @@ public: std::unique_ptr scripting::load_generator( const GeneratorDef& def, const fs::path& file, const std::string& dirPath ) { - auto L = lua::create_state(lua::StateType::GENERATOR); - auto env = lua::create_environment(L); - lua::stackguard _(L); + auto L = create_state(StateType::GENERATOR); + auto env = create_environment(L); + stackguard _(L); - lua::pushenv(L, *env); - lua::pushstring(L, dirPath); - lua::setfield(L, "__DIR__"); - lua::pushstring(L, dirPath + "/script.lua"); - lua::setfield(L, "__FILE__"); + pushenv(L, *env); + pushstring(L, dirPath); + setfield(L, "__DIR__"); + pushstring(L, dirPath + "/script.lua"); + setfield(L, "__FILE__"); - lua::pop(L); + pop(L); if (fs::exists(file)) { std::string src = files::read_string(file); logger.info() << "script (generator) " << file.u8string(); - lua::pop(L, lua::execute(L, *env, src, file.u8string())); + pop(L, execute(L, *env, src, file.u8string())); } else { // Use default (empty) script - lua::pop(L, lua::execute(L, *env, "", "")); + pop(L, execute(L, *env, "", "")); } - return std::make_unique( - L, def, std::move(env) - ); + return std::make_unique(L, def, std::move(env)); }