Merge branch 'main' into entities

This commit is contained in:
MihailRis
2024-07-08 23:29:48 +03:00
15 changed files with 554 additions and 9 deletions
+32
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);
+23
View File
@@ -138,6 +138,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>},
@@ -152,5 +173,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}
};
+82 -1
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}
};
};
+24
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}
};