move world-related functions from core library

This commit is contained in:
MihailRis 2026-01-07 20:12:06 +03:00
parent 126a8485a1
commit c57fe21120
6 changed files with 100 additions and 122 deletions

View file

@ -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()

View file

@ -2,7 +2,7 @@
<button onclick='menu:reset()'>@Continue</button>
<button onclick='menu.page="content?mode=world"'>@Content</button>
<button onclick='menu.page="settings"'>@Settings</button>
<button onclick='core.close_world(true)'>
<button onclick='app.close_world(true)'>
@Save and Quit to Menu
</button>
</panel>

View file

@ -2,7 +2,7 @@
size='380,66'
color='#0F1E2DB2'
hover-color='#162B3399'
onclick='core.open_world("%{name}")'
onclick='app.open_world("%{name}")'
>
<image pos='1' src='%{icon}' size='96,64'></image>
<label pos='104,8'>%{name}</label>
@ -11,7 +11,7 @@
color='#00000000'
hover-color='#FFFFFF2B'
padding='0'
onclick='core.delete_world("%{name}")'>
onclick='app.delete_world("%{name}")'>
<image src='gui/delete_icon' size='32,32' color='#FFFFFF50'/>
</button>
<label color='%{versionColor}' pos='317,44'>%{versionString}</label>

View file

@ -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, ...)

View file

@ -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<l_set_setting>},
{"str_setting", lua::wrap<l_str_setting>},
{"get_setting_info", lua::wrap<l_get_setting_info>},
{"new_world", lua::wrap<l_new_world>},
{"open_world", lua::wrap<l_open_world>},
{"reopen_world", lua::wrap<l_reopen_world>},
{"save_world", lua::wrap<l_save_world>},
{"close_world", lua::wrap<l_close_world>},
{"delete_world", lua::wrap<l_delete_world>},
{"quit", lua::wrap<l_quit>},
// for other functions see libcore.cpp and stdlib.lua
{nullptr, nullptr}

View file

@ -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 <memory>
#include <vector>
#include <sstream>
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<l_blank>},
{"new_world", lua::wrap<l_new_world>},
{"open_world", lua::wrap<l_open_world>},
{"reopen_world", lua::wrap<l_reopen_world>},
{"save_world", lua::wrap<l_save_world>},
{"close_world", lua::wrap<l_close_world>},
{"delete_world", lua::wrap<l_delete_world>},
{"capture_output", lua::wrap<l_capture_output>},
{nullptr, nullptr}
};