From c57fe211202b564557c180e10e81e4cbe303749c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 7 Jan 2026 20:12:06 +0300 Subject: [PATCH] move world-related functions from core library --- res/layouts/pages/new_world.xml.lua | 2 +- res/layouts/pages/pause.xml | 2 +- res/layouts/templates/world.xml | 4 +- res/scripts/stdlib.lua | 6 -- src/logic/scripting/lua/libs/libapp.cpp | 106 ++++++++++++++++++++--- src/logic/scripting/lua/libs/libcore.cpp | 102 ---------------------- 6 files changed, 100 insertions(+), 122 deletions(-) diff --git a/res/layouts/pages/new_world.xml.lua b/res/layouts/pages/new_world.xml.lua index 694ca9565..ad95aad0d 100644 --- a/res/layouts/pages/new_world.xml.lua +++ b/res/layouts/pages/new_world.xml.lua @@ -21,7 +21,7 @@ function create_world() local seed = document.seed_box.text local generator = settings.generator session.reset_entry('new_world') - core.new_world(name, seed, generator) + app.new_world(name, seed, generator) end function world_name_placeholder() diff --git a/res/layouts/pages/pause.xml b/res/layouts/pages/pause.xml index 63d827e10..3e4427cae 100644 --- a/res/layouts/pages/pause.xml +++ b/res/layouts/pages/pause.xml @@ -2,7 +2,7 @@ - diff --git a/res/layouts/templates/world.xml b/res/layouts/templates/world.xml index a258604b4..4ae936b6e 100644 --- a/res/layouts/templates/world.xml +++ b/res/layouts/templates/world.xml @@ -2,7 +2,7 @@ size='380,66' color='#0F1E2DB2' hover-color='#162B3399' - onclick='core.open_world("%{name}")' + onclick='app.open_world("%{name}")' > @@ -11,7 +11,7 @@ color='#00000000' hover-color='#FFFFFF2B' padding='0' - onclick='core.delete_world("%{name}")'> + onclick='app.delete_world("%{name}")'> diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index cc0131fa7..4eb6dd17e 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -36,12 +36,6 @@ local function complete_app_lib(app) app.sleep = sleep app.script = __VC_SCRIPT_NAME - app.new_world = core.new_world - app.open_world = core.open_world - app.save_world = core.save_world - app.close_world = core.close_world - app.reopen_world = core.reopen_world - app.delete_world = core.delete_world app.set_setting = function(name, value, ...) __app_set_setting(name, value, ...) diff --git a/src/logic/scripting/lua/libs/libapp.cpp b/src/logic/scripting/lua/libs/libapp.cpp index 9412baa36..736fc8624 100644 --- a/src/logic/scripting/lua/libs/libapp.cpp +++ b/src/logic/scripting/lua/libs/libapp.cpp @@ -1,22 +1,24 @@ #include "api_lua.hpp" -#include "io/io.hpp" -#include "io/settings_io.hpp" -#include "io/devices/MemoryDevice.hpp" -#include "logic/scripting/scripting.hpp" -#include "logic/EngineController.hpp" #include "content/ContentControl.hpp" +#include "devtools/Project.hpp" #include "engine/Engine.hpp" #include "engine/EnginePaths.hpp" -#include "devtools/Project.hpp" +#include "frontend/locale.hpp" +#include "graphics/ui/elements/Menu.hpp" +#include "graphics/ui/gui_util.hpp" +#include "graphics/ui/GUI.hpp" +#include "io/devices/MemoryDevice.hpp" +#include "io/io.hpp" +#include "io/settings_io.hpp" +#include "logic/EngineController.hpp" +#include "logic/LevelController.hpp" +#include "logic/scripting/scripting.hpp" #include "network/Network.hpp" #include "util/platform.hpp" #include "util/stringutil.hpp" #include "window/Window.hpp" -#include "frontend/locale.hpp" -#include "graphics/ui/GUI.hpp" -#include "graphics/ui/gui_util.hpp" -#include "graphics/ui/elements/Menu.hpp" +#include "world/Level.hpp" using namespace scripting; @@ -266,6 +268,84 @@ static int l_open_url(lua::State* L) { return 0; } +/// @brief Creating new world +/// @param name Name world +/// @param seed Seed world +/// @param generator Type of generation +static int l_new_world(lua::State* L) { + auto name = lua::require_string(L, 1); + auto seed = lua::require_string(L, 2); + auto generator = lua::require_string(L, 3); + int64_t localPlayer = 0; + if (lua::gettop(L) >= 4) { + localPlayer = lua::tointeger(L, 4); + } + if (level != nullptr) { + throw std::runtime_error("world must be closed before"); + } + auto controller = engine->getController(); + controller->setLocalPlayer(localPlayer); + controller->createWorld(name, seed, generator); + return 0; +} + +/// @brief Open world +/// @param name Name world +static int l_open_world(lua::State* L) { + auto name = lua::require_string(L, 1); + if (level != nullptr) { + throw std::runtime_error("world must be closed before"); + } + auto controller = engine->getController(); + controller->setLocalPlayer(0); + controller->openWorld(name, false); + return 0; +} + +/// @brief Reopen world +static int l_reopen_world(lua::State*) { + auto controller = engine->getController(); + if (level == nullptr) { + throw std::runtime_error("no world open"); + } + controller->reopenWorld(level->getWorld()); + return 0; +} + +/// @brief Save world +static int l_save_world(lua::State* L) { + if (controller == nullptr) { + throw std::runtime_error("no world open"); + } + controller->saveWorld(); + return 0; +} + +/// @brief Close world +/// @param flag Save world (bool) +static int l_close_world(lua::State* L) { + if (controller == nullptr) { + throw std::runtime_error("no world open"); + } + controller->processBeforeQuit(); + bool save_world = lua::toboolean(L, 1); + if (save_world) { + controller->saveWorld(); + } + engine->onWorldClosed(); + return 0; +} + +/// @brief Delete world +/// @param name Name world +static int l_delete_world(lua::State* L) { + auto name = lua::require_string(L, 1); + auto controller = engine->getController(); + controller->deleteWorld(name); + return 0; +} + + /// @brief Quit the game static int l_quit(lua::State*) { engine->quit(); @@ -291,6 +371,12 @@ const luaL_Reg applib[] = { {"set_setting", lua::wrap}, {"str_setting", lua::wrap}, {"get_setting_info", lua::wrap}, + {"new_world", lua::wrap}, + {"open_world", lua::wrap}, + {"reopen_world", lua::wrap}, + {"save_world", lua::wrap}, + {"close_world", lua::wrap}, + {"delete_world", lua::wrap}, {"quit", lua::wrap}, // for other functions see libcore.cpp and stdlib.lua {nullptr, nullptr} diff --git a/src/logic/scripting/lua/libs/libcore.cpp b/src/logic/scripting/lua/libs/libcore.cpp index b9648ad35..324cd5471 100644 --- a/src/logic/scripting/lua/libs/libcore.cpp +++ b/src/logic/scripting/lua/libs/libcore.cpp @@ -1,105 +1,9 @@ #include "api_lua.hpp" -#include "constants.hpp" -#include "assets/Assets.hpp" -#include "content/Content.hpp" -#include "content/ContentControl.hpp" -#include "engine/Engine.hpp" -#include "engine/EnginePaths.hpp" -#include "io/io.hpp" -#include "frontend/menu.hpp" -#include "frontend/screens/MenuScreen.hpp" -#include "graphics/core/Texture.hpp" -#include "logic/EngineController.hpp" -#include "logic/LevelController.hpp" -#include "util/listutil.hpp" -#include "util/platform.hpp" -#include "world/Level.hpp" -#include "world/generator/WorldGenerator.hpp" -#include "window/Window.hpp" -#include -#include #include using namespace scripting; -/// @brief Creating new world -/// @param name Name world -/// @param seed Seed world -/// @param generator Type of generation -static int l_new_world(lua::State* L) { - auto name = lua::require_string(L, 1); - auto seed = lua::require_string(L, 2); - auto generator = lua::require_string(L, 3); - int64_t localPlayer = 0; - if (lua::gettop(L) >= 4) { - localPlayer = lua::tointeger(L, 4); - } - if (level != nullptr) { - throw std::runtime_error("world must be closed before"); - } - auto controller = engine->getController(); - controller->setLocalPlayer(localPlayer); - controller->createWorld(name, seed, generator); - return 0; -} - -/// @brief Open world -/// @param name Name world -static int l_open_world(lua::State* L) { - auto name = lua::require_string(L, 1); - if (level != nullptr) { - throw std::runtime_error("world must be closed before"); - } - auto controller = engine->getController(); - controller->setLocalPlayer(0); - controller->openWorld(name, false); - return 0; -} - -/// @brief Reopen world -static int l_reopen_world(lua::State*) { - auto controller = engine->getController(); - if (level == nullptr) { - throw std::runtime_error("no world open"); - } - controller->reopenWorld(level->getWorld()); - return 0; -} - -/// @brief Save world -static int l_save_world(lua::State* L) { - if (controller == nullptr) { - throw std::runtime_error("no world open"); - } - controller->saveWorld(); - return 0; -} - -/// @brief Close world -/// @param flag Save world (bool) -static int l_close_world(lua::State* L) { - if (controller == nullptr) { - throw std::runtime_error("no world open"); - } - controller->processBeforeQuit(); - bool save_world = lua::toboolean(L, 1); - if (save_world) { - controller->saveWorld(); - } - engine->onWorldClosed(); - return 0; -} - -/// @brief Delete world -/// @param name Name world -static int l_delete_world(lua::State* L) { - auto name = lua::require_string(L, 1); - auto controller = engine->getController(); - controller->deleteWorld(name); - return 0; -} - static int l_blank(lua::State*) { return 0; } @@ -133,12 +37,6 @@ static int l_capture_output(lua::State* L) { const luaL_Reg corelib[] = { {"blank", lua::wrap}, - {"new_world", lua::wrap}, - {"open_world", lua::wrap}, - {"reopen_world", lua::wrap}, - {"save_world", lua::wrap}, - {"close_world", lua::wrap}, - {"delete_world", lua::wrap}, {"capture_output", lua::wrap}, {nullptr, nullptr} };