minor refactor

This commit is contained in:
MihailRis 2024-10-06 21:07:27 +03:00
parent 73d96fd4f7
commit d9a44f3b87
3 changed files with 64 additions and 74 deletions

View File

@ -73,10 +73,6 @@ void scripting::initialize(Engine* engine) {
return std::make_shared<int>(0); return std::make_shared<int>(0);
} }
[[nodiscard]] scriptenv scripting::create_environment() {
return lua::create_environment(lua::get_main_state());
}
[[nodiscard]] scriptenv scripting::create_pack_environment( [[nodiscard]] scriptenv scripting::create_pack_environment(
const ContentPack& pack const ContentPack& pack
) { ) {

View File

@ -52,7 +52,6 @@ namespace scripting {
scriptenv get_root_environment(); scriptenv get_root_environment();
scriptenv create_pack_environment(const ContentPack& pack); scriptenv create_pack_environment(const ContentPack& pack);
scriptenv create_environment();
scriptenv create_doc_environment( scriptenv create_doc_environment(
const scriptenv& parent, const std::string& name const scriptenv& parent, const std::string& name
); );

View File

@ -16,45 +16,42 @@
#include "files/files.hpp" #include "files/files.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
using namespace lua;
static debug::Logger logger("generator-scripting"); static debug::Logger logger("generator-scripting");
class LuaGeneratorScript : public GeneratorScript { class LuaGeneratorScript : public GeneratorScript {
lua::State* L; State* L;
const GeneratorDef& def; const GeneratorDef& def;
scriptenv env; scriptenv env;
public: public:
LuaGeneratorScript( LuaGeneratorScript(State* L, const GeneratorDef& def, scriptenv env)
lua::State* L, : L(L), def(def), env(std::move(env)) {
const GeneratorDef& def, }
scriptenv env)
: L(L),
def(def),
env(std::move(env))
{}
virtual ~LuaGeneratorScript() { virtual ~LuaGeneratorScript() {
env.reset(); env.reset();
if (L != lua::get_main_state()) { if (L != get_main_state()) {
lua::close(L); close(L);
} }
} }
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 {
lua::pushenv(L, *env); pushenv(L, *env);
if (lua::getfield(L, "generate_heightmap")) { if (getfield(L, "generate_heightmap")) {
lua::pushivec_stack(L, offset); pushivec_stack(L, offset);
lua::pushivec_stack(L, size); pushivec_stack(L, size);
lua::pushinteger(L, seed); pushinteger(L, seed);
lua::pushinteger(L, bpd); pushinteger(L, bpd);
if (lua::call_nothrow(L, 6)) { if (call_nothrow(L, 6)) {
auto map = lua::touserdata<lua::LuaHeightmap>(L, -1)->getHeightmap(); auto map = touserdata<LuaHeightmap>(L, -1)->getHeightmap();
lua::pop(L, 2); pop(L, 2);
return map; return map;
} }
} }
lua::pop(L); pop(L);
return std::make_shared<Heightmap>(size.x, size.y); return std::make_shared<Heightmap>(size.x, size.y);
} }
@ -64,22 +61,22 @@ public:
std::vector<std::shared_ptr<Heightmap>> maps; std::vector<std::shared_ptr<Heightmap>> maps;
uint biomeParameters = def.biomeParameters; uint biomeParameters = def.biomeParameters;
lua::pushenv(L, *env); pushenv(L, *env);
if (lua::getfield(L, "generate_biome_parameters")) { if (getfield(L, "generate_biome_parameters")) {
lua::pushivec_stack(L, offset); pushivec_stack(L, offset);
lua::pushivec_stack(L, size); pushivec_stack(L, size);
lua::pushinteger(L, seed); pushinteger(L, seed);
lua::pushinteger(L, bpd); pushinteger(L, bpd);
if (lua::call_nothrow(L, 6, biomeParameters)) { if (call_nothrow(L, 6, biomeParameters)) {
for (int i = biomeParameters-1; i >= 0; i--) { for (int i = biomeParameters-1; i >= 0; i--) {
maps.push_back( maps.push_back(
lua::touserdata<lua::LuaHeightmap>(L, -1-i)->getHeightmap()); touserdata<LuaHeightmap>(L, -1-i)->getHeightmap());
} }
lua::pop(L, 1+biomeParameters); pop(L, 1+biomeParameters);
return maps; return maps;
} }
} }
lua::pop(L); pop(L);
for (uint i = 0; i < biomeParameters; i++) { for (uint i = 0; i < biomeParameters; i++) {
maps.push_back(std::make_shared<Heightmap>(size.x, size.y)); maps.push_back(std::make_shared<Heightmap>(size.x, size.y));
} }
@ -92,46 +89,46 @@ public:
) override { ) override {
std::vector<StructurePlacement> placements; std::vector<StructurePlacement> placements;
lua::stackguard _(L); stackguard _(L);
lua::pushenv(L, *env); pushenv(L, *env);
if (lua::getfield(L, "place_structures")) { if (getfield(L, "place_structures")) {
lua::pushivec_stack(L, offset); pushivec_stack(L, offset);
lua::pushivec_stack(L, size); pushivec_stack(L, size);
lua::pushinteger(L, seed); pushinteger(L, seed);
lua::newuserdata<lua::LuaHeightmap>(L, heightmap); newuserdata<LuaHeightmap>(L, heightmap);
lua::pushinteger(L, chunkHeight); pushinteger(L, chunkHeight);
if (lua::call_nothrow(L, 7, 1)) { if (call_nothrow(L, 7, 1)) {
int len = lua::objlen(L, -1); int len = objlen(L, -1);
for (int i = 1; i <= len; i++) { for (int i = 1; i <= len; i++) {
lua::rawgeti(L, i); rawgeti(L, i);
lua::rawgeti(L, 1); rawgeti(L, 1);
int structIndex = 0; int structIndex = 0;
if (lua::isstring(L, -1)) { if (isstring(L, -1)) {
const auto& found = def.structuresIndices.find( const auto& found = def.structuresIndices.find(
lua::require_string(L, -1) require_string(L, -1)
); );
if (found != def.structuresIndices.end()) { if (found != def.structuresIndices.end()) {
structIndex = found->second; structIndex = found->second;
} }
} else { } else {
structIndex = lua::tointeger(L, -1); structIndex = tointeger(L, -1);
} }
lua::pop(L); pop(L);
lua::rawgeti(L, 2); rawgeti(L, 2);
glm::ivec3 pos = lua::tovec3(L, -1); glm::ivec3 pos = tovec3(L, -1);
lua::pop(L); pop(L);
lua::rawgeti(L, 3); rawgeti(L, 3);
int rotation = lua::tointeger(L, -1) & 0b11; int rotation = tointeger(L, -1) & 0b11;
lua::pop(L); pop(L);
lua::pop(L); pop(L);
placements.emplace_back(structIndex, pos, rotation); placements.emplace_back(structIndex, pos, rotation);
} }
lua::pop(L); pop(L);
} }
} }
return placements; return placements;
@ -141,28 +138,26 @@ public:
std::unique_ptr<GeneratorScript> scripting::load_generator( std::unique_ptr<GeneratorScript> scripting::load_generator(
const GeneratorDef& def, const fs::path& file, const std::string& dirPath const GeneratorDef& def, const fs::path& file, const std::string& dirPath
) { ) {
auto L = lua::create_state(lua::StateType::GENERATOR); auto L = create_state(StateType::GENERATOR);
auto env = lua::create_environment(L); auto env = create_environment(L);
lua::stackguard _(L); stackguard _(L);
lua::pushenv(L, *env); pushenv(L, *env);
lua::pushstring(L, dirPath); pushstring(L, dirPath);
lua::setfield(L, "__DIR__"); setfield(L, "__DIR__");
lua::pushstring(L, dirPath + "/script.lua"); pushstring(L, dirPath + "/script.lua");
lua::setfield(L, "__FILE__"); setfield(L, "__FILE__");
lua::pop(L); pop(L);
if (fs::exists(file)) { if (fs::exists(file)) {
std::string src = files::read_string(file); std::string src = files::read_string(file);
logger.info() << "script (generator) " << file.u8string(); 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 { } else {
// Use default (empty) script // Use default (empty) script
lua::pop(L, lua::execute(L, *env, "", "<empty>")); pop(L, execute(L, *env, "", "<empty>"));
} }
return std::make_unique<LuaGeneratorScript>( return std::make_unique<LuaGeneratorScript>(L, def, std::move(env));
L, def, std::move(env)
);
} }