add world.get_generator

This commit is contained in:
MihailRis 2024-10-26 22:46:26 +03:00
parent 84ba063908
commit 7afdde5912
3 changed files with 48 additions and 28 deletions

View File

@ -1,6 +1,7 @@
# *world* library # *world* library
```lua ```lua
-- Returns worlds information.
world.get_list() -> tables array { world.get_list() -> tables array {
-- world name -- world name
name: str, name: str,
@ -27,6 +28,9 @@ world.get_total_time() -> number
-- Returns world seed. -- Returns world seed.
world.get_seed() -> int world.get_seed() -> int
-- Returns generator name.
world.get_generator() -> str
-- Proves that this is the current time during the day -- Proves that this is the current time during the day
-- from 0.333(8 am) to 0.833(8 pm). -- from 0.333(8 am) to 0.833(8 pm).
world.is_day() -> boolean world.is_day() -> boolean

View File

@ -27,6 +27,9 @@ world.get_total_time() -> number
-- Возвращает зерно мира. -- Возвращает зерно мира.
world.get_seed() -> int world.get_seed() -> int
-- Возвращает имя генератора.
world.get_generator() -> str
-- Проверяет существование мира по имени. -- Проверяет существование мира по имени.
world.exists() -> bool world.exists() -> bool

View File

@ -1,4 +1,5 @@
#include <cmath> #include <cmath>
#include <stdexcept>
#include <filesystem> #include <filesystem>
#include "assets/Assets.hpp" #include "assets/Assets.hpp"
@ -12,7 +13,14 @@
using namespace scripting; using namespace scripting;
namespace fs = std::filesystem; namespace fs = std::filesystem;
static int l_world_get_list(lua::State* L) { static WorldInfo& require_world_info() {
if (level == nullptr) {
throw std::runtime_error("no world open");
}
return level->getWorld()->getInfo();
}
static int l_get_list(lua::State* L) {
auto paths = engine->getPaths(); auto paths = engine->getPaths();
auto worlds = paths->scanForWorlds(); auto worlds = paths->scanForWorlds();
@ -41,59 +49,64 @@ static int l_world_get_list(lua::State* L) {
return 1; return 1;
} }
static int l_world_get_total_time(lua::State* L) { static int l_get_total_time(lua::State* L) {
return lua::pushnumber(L, level->getWorld()->getInfo().totalTime); return lua::pushnumber(L, require_world_info().totalTime);
} }
static int l_world_get_day_time(lua::State* L) { static int l_get_day_time(lua::State* L) {
return lua::pushnumber(L, level->getWorld()->getInfo().daytime); return lua::pushnumber(L, require_world_info().daytime);
} }
static int l_world_set_day_time(lua::State* L) { static int l_set_day_time(lua::State* L) {
auto value = lua::tonumber(L, 1); auto value = lua::tonumber(L, 1);
level->getWorld()->getInfo().daytime = std::fmod(value, 1.0); require_world_info().daytime = std::fmod(value, 1.0);
return 0; return 0;
} }
static int l_world_set_day_time_speed(lua::State* L) { static int l_set_day_time_speed(lua::State* L) {
auto value = lua::tonumber(L, 1); auto value = lua::tonumber(L, 1);
level->getWorld()->getInfo().daytimeSpeed = std::abs(value); require_world_info().daytimeSpeed = std::abs(value);
return 0; return 0;
} }
static int l_world_get_day_time_speed(lua::State* L) { static int l_get_day_time_speed(lua::State* L) {
return lua::pushnumber(L, level->getWorld()->getInfo().daytimeSpeed); return lua::pushnumber(L, require_world_info().daytimeSpeed);
} }
static int l_world_get_seed(lua::State* L) { static int l_get_seed(lua::State* L) {
return lua::pushinteger(L, level->getWorld()->getSeed()); return lua::pushinteger(L, require_world_info().seed);
} }
static int l_world_exists(lua::State* L) { static int l_exists(lua::State* L) {
auto name = lua::require_string(L, 1); auto name = lua::require_string(L, 1);
auto worldsDir = engine->getPaths()->getWorldFolderByName(name); auto worldsDir = engine->getPaths()->getWorldFolderByName(name);
return lua::pushboolean(L, fs::is_directory(worldsDir)); return lua::pushboolean(L, fs::is_directory(worldsDir));
} }
static int l_world_is_day(lua::State* L) { static int l_is_day(lua::State* L) {
auto daytime = level->getWorld()->getInfo().daytime; auto daytime = require_world_info().daytime;
return lua::pushboolean(L, daytime >= 0.333 && daytime <= 0.833); return lua::pushboolean(L, daytime >= 0.333 && daytime <= 0.833);
} }
static int l_world_is_night(lua::State* L) { static int l_is_night(lua::State* L) {
auto daytime = level->getWorld()->getInfo().daytime; auto daytime = require_world_info().daytime;
return lua::pushboolean(L, daytime < 0.333 || daytime > 0.833); return lua::pushboolean(L, daytime < 0.333 || daytime > 0.833);
} }
static int l_get_generator(lua::State* L) {
return lua::pushstring(L, require_world_info().generator);
}
const luaL_Reg worldlib[] = { const luaL_Reg worldlib[] = {
{"get_list", lua::wrap<l_world_get_list>}, {"get_list", lua::wrap<l_get_list>},
{"get_total_time", lua::wrap<l_world_get_total_time>}, {"get_total_time", lua::wrap<l_get_total_time>},
{"get_day_time", lua::wrap<l_world_get_day_time>}, {"get_day_time", lua::wrap<l_get_day_time>},
{"set_day_time", lua::wrap<l_world_set_day_time>}, {"set_day_time", lua::wrap<l_set_day_time>},
{"set_day_time_speed", lua::wrap<l_world_set_day_time_speed>}, {"set_day_time_speed", lua::wrap<l_set_day_time_speed>},
{"get_day_time_speed", lua::wrap<l_world_get_day_time_speed>}, {"get_day_time_speed", lua::wrap<l_get_day_time_speed>},
{"get_seed", lua::wrap<l_world_get_seed>}, {"get_seed", lua::wrap<l_get_seed>},
{"is_day", lua::wrap<l_world_is_day>}, {"get_generator", lua::wrap<l_get_generator>},
{"is_night", lua::wrap<l_world_is_night>}, {"is_day", lua::wrap<l_is_day>},
{"exists", lua::wrap<l_world_exists>}, {"is_night", lua::wrap<l_is_night>},
{"exists", lua::wrap<l_exists>},
{NULL, NULL}}; {NULL, NULL}};