Merge pull request #265 from Cogitary0/main

Add lua func, docs
This commit is contained in:
MihailRis 2024-07-08 23:25:29 +03:00 committed by GitHub
commit 359123bb36
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 474 additions and 9 deletions

View File

@ -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
```

View File

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

View File

@ -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
```

View File

@ -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 <command>'"
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
)

View File

@ -11,12 +11,17 @@
#include "../../../window/Events.hpp"
#include "../../../window/Window.hpp"
#include "../../../world/WorldGenerators.hpp"
#include "../../../constants.hpp"
#include <vector>
#include <memory>
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);

View File

@ -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<l_player_get_pos>},
{"set_pos", lua::wrap<l_player_set_pos>},
@ -139,5 +160,7 @@ const luaL_Reg playerlib [] = {
{"is_noclip", lua::wrap<l_player_is_noclip>},
{"set_noclip", lua::wrap<l_player_set_noclip>},
{"get_selected_block", lua::wrap<l_player_get_selected_block>},
{"set_spawnpoint", lua::wrap<l_player_set_spawnpoint>},
{"get_spawnpoint", lua::wrap<l_player_get_spawnpoint>},
{NULL, NULL}
};

View File

@ -69,6 +69,72 @@ static int l_scalar_op(lua::State* L) {
return lua::pushnumber(L, func(vec));
}
template<int n>
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<n>(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<n, float>(b)));
}
} else {
auto b = lua::tovec<n>(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<int n>
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<n>(L, 1);
const auto& b = lua::tovec<n>(L, 2);
return lua::pushnumber(L, glm::dot(a, b));
}
template<int n>
static int l_inverse(lua::State* L) {
uint argc = lua::gettop(L);
auto vec = lua::tovec<n>(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<int n>
static int l_tostring(lua::State* L) {
auto vec = lua::tovec<n>(L, 1);
@ -95,6 +161,11 @@ const luaL_Reg vec2lib [] = {
{"normalize", lua::wrap<l_unaryop<2, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<2, glm::length>>},
{"tostring", lua::wrap<l_tostring<2>>},
{"abs", lua::wrap<l_unaryop<2, glm::abs>>},
{"round", lua::wrap<l_unaryop<2, glm::round>>},
{"inverse", lua::wrap<l_inverse<2>>},
{"pow", lua::wrap<l_pow<2>>},
{"dot", lua::wrap<l_dot<2>>},
{NULL, NULL}
};
@ -106,6 +177,11 @@ const luaL_Reg vec3lib [] = {
{"normalize", lua::wrap<l_unaryop<3, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<3, glm::length>>},
{"tostring", lua::wrap<l_tostring<3>>},
{"abs", lua::wrap<l_unaryop<3, glm::abs>>},
{"round", lua::wrap<l_unaryop<3, glm::round>>},
{"inverse", lua::wrap<l_inverse<3>>},
{"pow", lua::wrap<l_pow<3>>},
{"dot", lua::wrap<l_dot<3>>},
{NULL, NULL}
};
@ -117,5 +193,10 @@ const luaL_Reg vec4lib [] = {
{"normalize", lua::wrap<l_unaryop<4, glm::normalize>>},
{"length", lua::wrap<l_scalar_op<4, glm::length>>},
{"tostring", lua::wrap<l_tostring<4>>},
{"abs", lua::wrap<l_unaryop<4, glm::abs>>},
{"round", lua::wrap<l_unaryop<4, glm::round>>},
{"inverse", lua::wrap<l_inverse<4>>},
{"pow", lua::wrap<l_pow<4>>},
{"dot", lua::wrap<l_dot<4>>},
{NULL, NULL}
};
};

View File

@ -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<l_world_get_list>},
{"get_total_time", lua::wrap<l_world_get_total_time>},
{"get_day_time", lua::wrap<l_world_get_day_time>},
{"set_day_time", lua::wrap<l_world_set_day_time>},
{"set_day_time_speed", lua::wrap<l_world_set_day_time_speed>},
{"get_day_time_speed", lua::wrap<l_world_get_day_time_speed>},
{"get_seed", lua::wrap<l_world_get_seed>},
{"is_day", lua::wrap<l_world_is_day>},
{"is_night", lua::wrap<l_world_is_night>},
{"exists", lua::wrap<l_world_exists>},
{NULL, NULL}
};

View File

@ -275,4 +275,4 @@ void Player::convert(dynamic::Map* data, const ContentLUT* lut) {
Inventory::convert(inventory, lut);
}
}
}
}

View File

@ -89,4 +89,4 @@ public:
}
};
#endif // SRC_OBJECTS_PLAYER_HPP_
#endif // SRC_OBJECTS_PLAYER_HPP_

View File

@ -19,6 +19,7 @@
#include <utility>
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;
}

View File

@ -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;