From 7a1c035f01644b859236d921060185ee4a38ea3e Mon Sep 17 00:00:00 2001 From: Cogi Asd Date: Sun, 7 Jul 2024 23:53:27 +0300 Subject: [PATCH] add: lua func --- src/logic/scripting/lua/libcore.cpp | 31 +++++++++++++++++++ src/logic/scripting/lua/libplayer.cpp | 44 +++++++++++++++++++++++++++ src/logic/scripting/lua/libworld.cpp | 19 ++++++++++++ src/objects/Player.cpp | 13 ++++++-- src/objects/Player.hpp | 4 +++ src/world/World.cpp | 2 +- src/world/World.hpp | 5 ++- 7 files changed, 114 insertions(+), 4 deletions(-) diff --git a/src/logic/scripting/lua/libcore.cpp b/src/logic/scripting/lua/libcore.cpp index 61b8340d..4298890e 100644 --- a/src/logic/scripting/lua/libcore.cpp +++ b/src/logic/scripting/lua/libcore.cpp @@ -11,6 +11,7 @@ #include "../../../window/Events.hpp" #include "../../../window/Window.hpp" #include "../../../world/WorldGenerators.hpp" +#include "../../../constants.hpp" #include #include @@ -158,6 +159,35 @@ static int l_get_generators(lua::State* L) { return 1; } +static int l_get_constants(lua::State* L) { + + lua::createtable(L, 0, 20); + + const std::pair numberConstants[] = { + {"ENGINE_VERSION_MAJOR", ENGINE_VERSION_MAJOR}, + {"ENGINE_VERSION_MINOR", ENGINE_VERSION_MINOR}, + {"BLOCK_AIR", BLOCK_AIR}, + {"ITEM_EMPTY", ITEM_EMPTY}, + {"CHUNK_W", CHUNK_W}, + {"CHUNK_H", CHUNK_H}, + {"CHUNK_D", CHUNK_D}, + {"VOXEL_USER_BITS", VOXEL_USER_BITS}, + {"VOXEL_USER_BITS_OFFSET", VOXEL_USER_BITS_OFFSET}, + {"ITEM_ICON_SIZE", ITEM_ICON_SIZE}, + {"CHUNK_VOL", CHUNK_VOL}, + {"BLOCK_VOID", BLOCK_VOID}, + {"ITEM_VOID", ITEM_VOID}, + {"MAX_BLOCKS", MAX_BLOCKS} + }; + + for (const auto& constant : numberConstants) { + lua::pushnumber(L, constant.second); + lua::setfield(L, constant.first); + } + + return 1; +} + const luaL_Reg corelib [] = { {"new_world", lua::wrap}, {"open_world", lua::wrap}, @@ -172,5 +202,6 @@ const luaL_Reg corelib [] = { {"quit", lua::wrap}, {"get_default_generator", lua::wrap}, {"get_generators", lua::wrap}, + {"get_constants", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index b9315de7..98c23c25 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -126,6 +126,46 @@ 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; +} + +static int l_player_set_jump_force(lua::State* L) { + + if (auto player = get_player(L, 1)) { + player->setJumpForce(std::abs(lua::tonumber(L, 2))); + } + + return 0; +} + +static int l_player_get_jump_force(lua::State* L) { + + if (auto player = get_player(L, 1)) { + return lua::pushnumber(L, player->getJumpForce()); + } + + return 0; +} + + const luaL_Reg playerlib [] = { {"get_pos", lua::wrap}, {"set_pos", lua::wrap}, @@ -139,5 +179,9 @@ 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}, + {"get_jump_force", lua::wrap}, + {"set_jump_force", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libworld.cpp b/src/logic/scripting/lua/libworld.cpp index 642c85d8..aca3bb7f 100644 --- a/src/logic/scripting/lua/libworld.cpp +++ b/src/logic/scripting/lua/libworld.cpp @@ -54,6 +54,12 @@ static int l_world_set_day_time(lua::State* L) { return 0; } +static int l_wolrd_set_speed_time(lua::State* L) { + auto value = lua::tonumber(L, 1); + level->getWorld()->factorSpeedTime = std::abs(value); + return 0; +} + static int l_world_get_seed(lua::State* L) { return lua::pushinteger(L, level->getWorld()->getSeed()); } @@ -64,12 +70,25 @@ 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_speed_time", 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..df71c0c3 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -18,7 +18,7 @@ const float PLAYER_GROUND_DAMPING = 10.0f; const float PLAYER_AIR_DAMPING = 7.0f; const float FLIGHT_SPEED_MUL = 4.0f; const float CHEAT_SPEED_MUL = 5.0f; -const float JUMP_FORCE = 8.0f; +const float JUMP_FACTOR = 8.0f; Player::Player(glm::vec3 position, float speed, std::shared_ptr inv) : speed(speed), @@ -96,7 +96,7 @@ void Player::updateInput( } if (input.jump && hitbox->grounded){ - hitbox->velocity.y = JUMP_FORCE; + hitbox->velocity.y = JUMP_FACTOR * jumpForce; } if ((input.flight && !noclip) || @@ -106,6 +106,7 @@ void Player::updateInput( hitbox->grounded = false; } } + if (input.noclip) { noclip = !noclip; } @@ -184,6 +185,14 @@ void Player::setNoclip(bool flag) { this->noclip = flag; } +float Player::getJumpForce() const { + return jumpForce; +} + +void Player::setJumpForce(float value) { + jumpForce = value; +} + std::shared_ptr Player::getInventory() const { return inventory; } diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index fdae39b5..4a9c154d 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -45,6 +45,7 @@ class Player : public Object, public Serializable { int chosenSlot; glm::vec3 spawnpoint {}; std::shared_ptr inventory; + float jumpForce = 1.0f; bool flight = false; bool noclip = false; public: @@ -73,6 +74,9 @@ public: bool isNoclip() const; void setNoclip(bool flag); + + float getJumpForce() const; + void setJumpForce(float value); std::shared_ptr getInventory() const; diff --git a/src/world/World.cpp b/src/world/World.cpp index 3e92ccb6..5d559a46 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -44,7 +44,7 @@ World::~World(){ } void World::updateTimers(float delta) { - daytime += delta * daytimeSpeed; + daytime += delta * daytimeSpeed * factorSpeedTime; daytime = fmod(daytime, 1.0f); totalTime += delta; } diff --git a/src/world/World.hpp b/src/world/World.hpp index 904ed027..81be7af0 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -42,8 +42,11 @@ public: /// 0.5 - is noon float daytime = timeutil::time_value(10, 00, 00); + // factor speed time + float factorSpeedTime = 1.0f; + // looking bad - float daytimeSpeed = 1.0f/60.0f/24.0f; + float daytimeSpeed = 0.000694444444444f; //1.0f/60.0f/24.0f; /// @brief total time passed in the world (not depending on daytimeSpeed) double totalTime = 0.0;