refactor scripting_world_generation.cpp

This commit is contained in:
MihailRis 2024-10-06 17:28:44 +03:00
parent 7c73515d2d
commit 091805a16e

View File

@ -15,20 +15,22 @@
#include "util/timeutil.hpp" #include "util/timeutil.hpp"
class LuaGeneratorScript : public GeneratorScript { class LuaGeneratorScript : public GeneratorScript {
lua::State* L;
const GeneratorDef& def; const GeneratorDef& def;
scriptenv env; scriptenv env;
public: public:
LuaGeneratorScript( LuaGeneratorScript(
lua::State* L,
const GeneratorDef& def, const GeneratorDef& def,
scriptenv env) scriptenv env)
: def(def), : L(L),
def(def),
env(std::move(env)) env(std::move(env))
{} {}
std::shared_ptr<Heightmap> generateHeightmap( std::shared_ptr<Heightmap> generateHeightmap(
const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd const glm::ivec2& offset, const glm::ivec2& size, uint64_t seed, uint bpd
) override { ) override {
auto L = lua::get_main_thread();
lua::pushenv(L, *env); lua::pushenv(L, *env);
if (lua::getfield(L, "generate_heightmap")) { if (lua::getfield(L, "generate_heightmap")) {
lua::pushivec_stack(L, offset); lua::pushivec_stack(L, offset);
@ -51,7 +53,6 @@ public:
std::vector<std::shared_ptr<Heightmap>> maps; std::vector<std::shared_ptr<Heightmap>> maps;
uint biomeParameters = def.biomeParameters; uint biomeParameters = def.biomeParameters;
auto L = lua::get_main_thread();
lua::pushenv(L, *env); lua::pushenv(L, *env);
if (lua::getfield(L, "generate_biome_parameters")) { if (lua::getfield(L, "generate_biome_parameters")) {
lua::pushivec_stack(L, offset); lua::pushivec_stack(L, offset);
@ -80,7 +81,6 @@ public:
) override { ) override {
std::vector<StructurePlacement> placements; std::vector<StructurePlacement> placements;
auto L = lua::get_main_thread();
lua::stackguard _(L); lua::stackguard _(L);
lua::pushenv(L, *env); lua::pushenv(L, *env);
if (lua::getfield(L, "place_structures")) { if (lua::getfield(L, "place_structures")) {
@ -97,7 +97,9 @@ public:
lua::rawgeti(L, 1); lua::rawgeti(L, 1);
int structIndex = 0; int structIndex = 0;
if (lua::isstring(L, -1)) { if (lua::isstring(L, -1)) {
const auto& found = def.structuresIndices.find(lua::require_string(L, -1)); const auto& found = def.structuresIndices.find(
lua::require_string(L, -1)
);
if (found != def.structuresIndices.end()) { if (found != def.structuresIndices.end()) {
structIndex = found->second; structIndex = found->second;
} }
@ -144,11 +146,10 @@ std::unique_ptr<GeneratorScript> scripting::load_generator(
lua::pop(L, load_script(*env, "generator", file)); lua::pop(L, load_script(*env, "generator", file));
} else { } else {
// Use default (empty) script // Use default (empty) script
lua::pop(L, lua::execute(lua::get_main_thread(), *env, "", "<empty>")); lua::pop(L, lua::execute(L, *env, "", "<empty>"));
} }
return std::make_unique<LuaGeneratorScript>( return std::make_unique<LuaGeneratorScript>(
def, L, def, std::move(env)
std::move(env)
); );
} }