diff --git a/doc/en/scripting.md b/doc/en/scripting.md index 1a1c5d38..ce062577 100644 --- a/doc/en/scripting.md +++ b/doc/en/scripting.md @@ -91,6 +91,13 @@ player.set_noclip(bool) Getter and setter for player noclip mode (collisions disabled) +``` python +player.set_spawnpoint(playerid: int, x: number, y: number, z: number) +player.get_spawnpoint(playerid: int) -> number, number, number +``` + +Point setter and getter added by player + ```python player.get_selected_block(playerid: int) -> x,y,z ``` @@ -122,6 +129,18 @@ world.set_day_time(time: number) Set day time value. +```python +world.set_day_time_speed(value: number) +``` + +Sets the specified speed for game time. + +```python +world.get_day_time_speed() -> number +``` + +Returns the speed for game time. + ```python world.get_total_time() -> number ``` @@ -134,6 +153,18 @@ world.get_seed() -> int Returns world seed. +``` python +world.is_day() -> boolean +``` + +Proves that this is the current time during the day. From 0.2(8 am) to 0.8(8 pm) + +``` python +world.is_night() -> bool +``` + +Checks that it is the current time at night. From 0.8(8 pm) to 0.2(8 am) + ```python world.exists() -> bool ``` diff --git a/doc/ru/scripting.md b/doc/ru/scripting.md index cd85c39d..6d2a0d53 100644 --- a/doc/ru/scripting.md +++ b/doc/ru/scripting.md @@ -87,6 +87,13 @@ player.set_noclip(bool) Геттер и сеттер noclip режима (выключенная коллизия игрока) +```python +player.set_spawnpoint(playerid: int, x: number, y: number, z: number) +player.get_spawnpoint(playerid: int) -> number, number, number +``` + +Сеттер и геттер точки спавна игрока + ```python player.get_selected_block(playerid: int) -> x,y,z ``` @@ -116,6 +123,18 @@ world.set_day_time(time: number) Устанавливает указанное игровое время. +```python +world.set_day_time_speed(value: number) +``` + +Устанавливает указанную скорость для игрового времени. + +```python +world.get_day_time_speed() -> number +``` + +Возвращает скорость для игрового времени. + ```python world.get_total_time() -> number ``` @@ -134,6 +153,18 @@ world.exists() -> bool Проверяет существование мира по имени. +```python +world.is_day() -> bool +``` + +Проверяет является ли текущее время днём. От 0.2(8 утра) до 0.8(8 вечера) + +```python +world.is_night() -> bool +``` + +Проверяет является ли текущее время ночью. От 0.8(8 вечера) до 0.2(8 утра) + ## Библиотека *pack* ```python diff --git a/doc/ru/scripting/builtins/libvecn.md b/doc/ru/scripting/builtins/libvecn.md new file mode 100644 index 00000000..bc847bf9 --- /dev/null +++ b/doc/ru/scripting/builtins/libvecn.md @@ -0,0 +1,196 @@ +# Библиотека Vec*n* + +*vecn* содержит набор функций для работы с векторами размерностью 2, 3 или 4. +Большинство функций имеют несколько вариантов списка аргументов (перегрузок). + +> [!WARNING] +> +> vecn, где n == размерность вектора (2, 3, 4), т.е vec2, vec3, vec4 +> + +## Типы данных + +На данной странице будут использоваться условные обозначения типов. +- vector - массив из двух, трех или четырех чисел +- vec2 - массив из двух чисел +- vec3 - массив из трех чисел +- vec4 - массив из четырех чисел + +> [!WARNING] +> +> Аннотации типов являются частью документации и не указываются при вызове использовании. + + +## Операции с векторами + +#### Сложение - *vecn.add(...)* + +```lua +-- возвращает результат сложения векторов +vecn.add(a: vector, b: vector) + +-- возвращает результат сложения вектора и скаляра +vecn.add(a: vector, b: number) + +-- записывает результат сложения двух векторов в dst +vec.add(a: vector, b: vector, dst: vector) +``` + +#### Вычитание - *vecn.sub(...)* + +```lua +-- возвращает результат вычитания векторов +vecn.sub(a: vector, b: vector) + +-- возвращает результат вычитания скаляра из вектора +vecn.sub(a: vector, b: number) + +-- записывает результат вычитания двух векторов в dst +vec.sub(a: vector, b: vector, dst: vector) +``` + +#### Умножение - *vecn.mul(...)* + +```lua +-- возвращает результат умножения векторов +vecn.mul(a: vector, b: vector) + +-- возвращает результат умножения вектора на скаляр +vecn.mul(a: vector, b: number) +``` + +#### Инверсия - *vecn.inv(...)* + +```lua +-- возвращает результат инверсии (противоположный) вектора +vecn.inverse(a: vector) + +-- записывает инвертированный вектор в dst +vec.inverse(v: vector, dst: vector) +``` + +#### Деление - *vecn.div(...)* + +```lua +-- возвращает результат деления векторов +vecn.div(a: vector, b: vector) + +-- возвращает результат деления вектора на скаляр +vecn.div(a: vector, b: number) + +-- записывает результат деления двух векторов в dst +vec.div(a: vector, b: vector, dst: vector) +``` + +#### Нормализация - *vecn.norm(...)* + +```lua +-- возвращает нормализованный вектор +vecn.normalize(a: vector) + +-- записывает нормализованный вектор в dst +vec.normalize(v: vector, dst: vector) +``` + +#### Длина вектора - *vecn.len(...)* + +```lua +-- возвращает длину вектора +vecn.length(a: vector) + +``` + +#### Абсолютное значение - *vecn.abs(...)* + +```lua +-- возвращает вектор с абсолютными значениями +vecn.abs(a: vector) + +-- записывает абсолютное значение вектора в dst +vec.abs(v: vector, dst: vector) +``` + +#### Округление - *vecn.round(...)* + +```lua +-- возвращает вектор с округленными значениями +vecn.round(a: vector) + +-- записывает округленный вектор в dst +vec.round(v: vector, dst: vector) +``` + +#### Степень - *vecn.pow(...)* + +```lua +-- возвращает вектор с элементами, возведенными в степень +vecn.pow(a: vector, b: number) + +-- записывает вектор, возведенный в степень, в dst +vec.pow(v: vector, exponent: number, dst: vector) +``` + +#### Скалярное произведение - *vecn.dot(...)* +```lua +-- возвращает скалярное произведение векторов +vecn.dot(a: vector, b: vector) +``` + +#### Перевод в строку - *vecn.tostring(...)* +> [!WARNING] +> Возвращает только тогда, когда содержимым является вектор +```lua +-- возвращает строку представляющую содержимое вектора +vecn.tostring(a: vector) +``` + + +## Пример +```lua +-- создание векторов разной размерности +local v1_3d = {1, 2, 2} +local v2_3d = {10, 20, 40} +local v3_4d = {1, 2, 4, 1} +local v4_2d = {1, 0} +local scal = 6 -- обычный скаляр + +-- сложение векторов +local result_add = vec3.add(v1_3d, v2_3d) +print("add: " .. vec3.tostring(result_add)) -- {11, 22, 42} + +-- вычитание векторов +local result_sub = vec3.sub(v2_3d, v1_3d) +print("sub: " .. vec3.tostring(result_sub)) -- {9, 18, 38} + +-- умножение векторов +local result_mul = vec3.mul(v1_3d, v2_3d) +print("mul: " .. vec3.tostring(result_mul)) -- {10, 40, 80} + +-- умножение вектора на скаляр +local result_mul_scal = vec3.mul(v1_3d, scal) +print("mul_scal: " .. vec3.tostring(result_mul_scal)) -- {6, 12, 12} + +-- нормализация вектора +local result_norm = vec3.normalize(v1_3d) +print("norm: " .. vec3.tostring(result_norm)) -- {0.333, 0.667, 0.667} + +-- длина вектора +local result_len = vec3.length(v1_3d) +print("len: " .. result_len) -- 3 + +-- абсолютное значение вектора +local result_abs = vec3.abs(v1_3d) +print("abs: " .. vec3.tostring(result_abs)) -- {1, 2, 2} + +-- округление вектора +local result_round = vec3.round(v1_3d) +print("round: " .. vec3.tostring(result_round)) -- {1, 2, 2} + +-- степень вектора +local result_pow = vec3.pow(v1_3d, 2) +print("pow: " .. vec3.tostring(result_pow)) -- {1, 4, 4} + +-- скалярное произведение векторов +local result_dot = vec3.dot(v1_3d, v2_3d) +print("dot: " .. result_dot) -- 250 +``` diff --git a/res/scripts/stdcmd.lua b/res/scripts/stdcmd.lua index 33f5721b..4368174b 100644 --- a/res/scripts/stdcmd.lua +++ b/res/scripts/stdcmd.lua @@ -26,24 +26,34 @@ console.add_command( "help name:str=''", "Show help infomation for the specified command", function (args, kwargs) + local name = args[1] if #name == 0 then + local commands = console.get_commands_list() table.sort(commands) local str = "Available commands:" + for i,k in ipairs(commands) do str = str.."\n "..build_scheme(console.get_command_info(k)) end + return str.."\nuse 'help '" + end + local command = console.get_command_info(name) + if command == nil then return string.format("command %q not found", name) end - local where = "" + + local where = ":" local str = SEPARATOR.."\n"..command.description.."\n"..name.." " - for i,arg in ipairs(command.args) do + + for _, arg in ipairs(command.args) do where = where.."\n "..arg.name.." - "..arg.type + if arg.optional then str = str.."["..arg.name.."] " where = where.." (optional)" @@ -51,11 +61,47 @@ console.add_command( str = str.."<"..arg.name.."> " end end - if #command.args then + + if #command.args > 0 then str = str.."\nwhere"..where end - + return str.."\n"..SEPARATOR + + end +) + +console.add_command( + "time.uptime", + "Get time elapsed since the engine started", + function() + + local uptime = time.uptime() + local years = math.floor(uptime / 31536000) + local days = math.floor((uptime % 31536000) / 86400) % 365 + local hours = math.floor((uptime % 86400) / 3600) % 24 + local minutes = math.floor((uptime % 3600) / 60) % 60 + local seconds = math.floor(uptime % 60) + + local formatted_uptime = "" + + if years > 0 then + formatted_uptime = formatted_uptime .. years .. "y " + end + if days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. days .. "d " + end + if hours > 0 or days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. hours .. "h " + end + if minutes > 0 or hours > 0 or days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. minutes .. "m " + end + if seconds > 0 or minutes > 0 or hours > 0 or days > 0 or years > 0 then + formatted_uptime = formatted_uptime .. seconds .. "s" + end + + return uptime .. " (" .. formatted_uptime .. ")" end ) diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 61b8340d..2b3e2ca8 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -11,12 +11,17 @@ #include "../../../window/Events.hpp" #include "../../../window/Window.hpp" #include "../../../world/WorldGenerators.hpp" +#include "../../../constants.hpp" #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); @@ -26,6 +31,8 @@ static int l_new_world(lua::State* L) { return 0; } +/// @brief Open world +/// @param name Name world static int l_open_world(lua::State* L) { auto name = lua::require_string(L, 1); @@ -34,12 +41,15 @@ static int l_open_world(lua::State* L) { return 0; } +/// @brief Reopen world static int l_reopen_world(lua::State*) { auto controller = engine->getController(); controller->reopenWorld(level->getWorld()); 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"); @@ -55,6 +65,8 @@ static int l_close_world(lua::State* L) { 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(); @@ -62,6 +74,9 @@ static int l_delete_world(lua::State* L) { return 0; } +/// @brief Reconfigure packs +/// @param addPacks An array of packs to add +/// @param remPacks An array of packs to remove static int l_reconfig_packs(lua::State* L) { if (!lua::istable(L, 1)) { throw std::runtime_error("strings array expected as the first argument"); @@ -95,12 +110,18 @@ static int l_reconfig_packs(lua::State* L) { return 0; } +/// @brief Get a setting value +/// @param name The name of the setting +/// @return The value of the setting static int l_get_setting(lua::State* L) { auto name = lua::require_string(L, 1); const auto value = engine->getSettingsHandler().getValue(name); return lua::pushvalue(L, value); } +/// @brief Set a setting value +/// @param name The name of the setting +/// @param value The new value for the setting static int l_set_setting(lua::State* L) { auto name = lua::require_string(L, 1); const auto value = lua::tovalue(L, 2); @@ -108,12 +129,18 @@ static int l_set_setting(lua::State* L) { return 0; } +/// @brief Convert a setting value to a string +/// @param name The name of the setting +/// @return The string representation of the setting value static int l_str_setting(lua::State* L) { auto name = lua::require_string(L, 1); const auto string = engine->getSettingsHandler().toString(name); return lua::pushstring(L, string); } +/// @brief Get information about a setting +/// @param name The name of the setting +/// @return A table with information about the setting static int l_get_setting_info(lua::State* L) { auto name = lua::require_string(L, 1); auto setting = engine->getSettingsHandler().getSetting(name); @@ -136,15 +163,20 @@ static int l_get_setting_info(lua::State* L) { throw std::runtime_error("unsupported setting type"); } +/// @brief Quit the game static int l_quit(lua::State*) { Window::setShouldClose(true); return 0; } +/// @brief Get the default world generator +/// @return The ID of the default world generator static int l_get_default_generator(lua::State* L) { return lua::pushstring(L, WorldGenerators::getDefaultGeneratorID()); } +/// @brief Get a list of all world generators +/// @return A table with the IDs of all world generators static int l_get_generators(lua::State* L) { const auto& generators = WorldGenerators::getGeneratorsIDs(); lua::createtable(L, generators.size(), 0); diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index b9315de7..4002ad5b 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -126,6 +126,27 @@ static int l_player_get_selected_block(lua::State* L) { return 0; } +static int l_player_get_spawnpoint(lua::State* L) { + if (auto player = get_player(L, 1)) { + return lua::pushvec3(L, player->getSpawnPoint()); + } + return 0; +} + + +static int l_player_set_spawnpoint(lua::State* L) { + auto player = get_player(L, 1); + + if (player) { + auto x = lua::tonumber(L, 2); + auto y = lua::tonumber(L, 3); + auto z = lua::tonumber(L, 4); + player->setSpawnPoint(glm::vec3(x, y, z)); + } + + return 0; +} + const luaL_Reg playerlib [] = { {"get_pos", lua::wrap}, {"set_pos", lua::wrap}, @@ -139,5 +160,7 @@ const luaL_Reg playerlib [] = { {"is_noclip", lua::wrap}, {"set_noclip", lua::wrap}, {"get_selected_block", lua::wrap}, + {"set_spawnpoint", lua::wrap}, + {"get_spawnpoint", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libvecn.cpp b/src/logic/scripting/lua/libvecn.cpp index 9a1b4f9b..875a0562 100644 --- a/src/logic/scripting/lua/libvecn.cpp +++ b/src/logic/scripting/lua/libvecn.cpp @@ -69,6 +69,72 @@ static int l_scalar_op(lua::State* L) { return lua::pushnumber(L, func(vec)); } +template +static int l_pow(lua::State* L) { + uint argc = lua::gettop(L); + if (argc != 2 && argc != 3) { + throw std::runtime_error("invalid arguments number (2 or 3 expected)"); + } + auto a = lua::tovec(L, 1); + + if (lua::isnumber(L, 2)) { + auto b = lua::tonumber(L, 2); + if (argc == 2) { + lua::createtable(L, n, 0); + for (uint i = 0; i < n; i++) { + lua::pushnumber(L, pow(a[i], b)); + lua::rawseti(L, i+1); + } + return 1; + } else { + return lua::setvec(L, 3, pow(a, glm::vec(b))); + } + } else { + auto b = lua::tovec(L, 2); + if (argc == 2) { + lua::createtable(L, n, 0); + for (uint i = 0; i < n; i++) { + lua::pushnumber(L, pow(a[i], b[i])); + lua::rawseti(L, i+1); + } + return 1; + } else { + return lua::setvec(L, 3, pow(a, b)); + } + } +} + +template +static int l_dot(lua::State* L) { + uint argc = lua::gettop(L); + if (argc != 1) { + throw std::runtime_error("invalid arguments number (1 expected)"); + } + const auto& a = lua::tovec(L, 1); + const auto& b = lua::tovec(L, 2); + return lua::pushnumber(L, glm::dot(a, b)); +} + +template +static int l_inverse(lua::State* L) { + uint argc = lua::gettop(L); + auto vec = lua::tovec(L, 1); + switch (argc) { + case 1: + lua::createtable(L, n, 0); + for (uint i = 0; i < n; i++) { + lua::pushnumber(L, (-1)*vec[i]); + lua::rawseti(L, i+1); + } + return 1; + case 2: + return lua::setvec(L, 2, -vec); + default: { + throw std::runtime_error("invalid arguments number (1 or 2 expected)"); + } + } +} + template static int l_tostring(lua::State* L) { auto vec = lua::tovec(L, 1); @@ -95,6 +161,11 @@ const luaL_Reg vec2lib [] = { {"normalize", lua::wrap>}, {"length", lua::wrap>}, {"tostring", lua::wrap>}, + {"abs", lua::wrap>}, + {"round", lua::wrap>}, + {"inverse", lua::wrap>}, + {"pow", lua::wrap>}, + {"dot", lua::wrap>}, {NULL, NULL} }; @@ -106,6 +177,11 @@ const luaL_Reg vec3lib [] = { {"normalize", lua::wrap>}, {"length", lua::wrap>}, {"tostring", lua::wrap>}, + {"abs", lua::wrap>}, + {"round", lua::wrap>}, + {"inverse", lua::wrap>}, + {"pow", lua::wrap>}, + {"dot", lua::wrap>}, {NULL, NULL} }; @@ -117,5 +193,10 @@ const luaL_Reg vec4lib [] = { {"normalize", lua::wrap>}, {"length", lua::wrap>}, {"tostring", lua::wrap>}, + {"abs", lua::wrap>}, + {"round", lua::wrap>}, + {"inverse", lua::wrap>}, + {"pow", lua::wrap>}, + {"dot", lua::wrap>}, {NULL, NULL} -}; +}; \ No newline at end of file diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 642c85d8..e7d24005 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -54,6 +54,16 @@ static int l_world_set_day_time(lua::State* L) { return 0; } +static int l_world_set_day_time_speed(lua::State* L) { + auto value = lua::tonumber(L, 1); + level->getWorld()->daytimeSpeed = std::abs(value); + return 0; +} + +static int l_world_get_day_time_speed(lua::State* L) { + return lua::pushnumber(L, level->getWorld()->daytimeSpeed); +} + static int l_world_get_seed(lua::State* L) { return lua::pushinteger(L, level->getWorld()->getSeed()); } @@ -64,12 +74,26 @@ static int l_world_exists(lua::State* L) { return lua::pushboolean(L, fs::is_directory(worldsDir)); } +static int l_world_is_day(lua::State* L) { + auto daytime = level->getWorld()->daytime; + return lua::pushboolean(L, daytime >= 0.2 && daytime <= 0.8); +} + +static int l_world_is_night(lua::State* L) { + auto daytime = level->getWorld()->daytime; + return lua::pushboolean(L, daytime < 0.2 || daytime > 0.8); +} + const luaL_Reg worldlib [] = { {"get_list", lua::wrap}, {"get_total_time", lua::wrap}, {"get_day_time", lua::wrap}, {"set_day_time", lua::wrap}, + {"set_day_time_speed", lua::wrap}, + {"get_day_time_speed", lua::wrap}, {"get_seed", lua::wrap}, + {"is_day", lua::wrap}, + {"is_night", lua::wrap}, {"exists", lua::wrap}, {NULL, NULL} }; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index fe81a5c7..590e625b 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -275,4 +275,4 @@ void Player::convert(dynamic::Map* data, const ContentLUT* lut) { Inventory::convert(inventory, lut); } } -} +} \ No newline at end of file diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index fdae39b5..e35162b0 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -89,4 +89,4 @@ public: } }; -#endif // SRC_OBJECTS_PLAYER_HPP_ +#endif // SRC_OBJECTS_PLAYER_HPP_ \ No newline at end of file diff --git a/src/world/World.cpp b/src/world/World.cpp index 3e92ccb6..39b7aeab 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -19,6 +19,7 @@ #include static debug::Logger logger("world"); +const float DAYIME_SPECIFIC_SPEED = 1.0f/1440.0f; //1.0f/60.0f/24.0f; world_load_error::world_load_error(const std::string& message) : std::runtime_error(message) { @@ -44,7 +45,7 @@ World::~World(){ } void World::updateTimers(float delta) { - daytime += delta * daytimeSpeed; + daytime += delta * daytimeSpeed * DAYIME_SPECIFIC_SPEED; daytime = fmod(daytime, 1.0f); totalTime += delta; } diff --git a/src/world/World.hpp b/src/world/World.hpp index 904ed027..9b0acf36 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -43,7 +43,7 @@ public: float daytime = timeutil::time_value(10, 00, 00); // looking bad - float daytimeSpeed = 1.0f/60.0f/24.0f; + float daytimeSpeed = 1.0f; /// @brief total time passed in the world (not depending on daytimeSpeed) double totalTime = 0.0;