From 1917cbba8be74c1df0025d71a14a4444c20c07f3 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 24 May 2024 01:38:39 +0300 Subject: [PATCH] new player.* functions --- doc/en/8.Scripting.md | 14 +++ doc/ru/8.Скриптинг.md | 14 +++ src/logic/scripting/lua/LuaState.cpp | 10 +- src/logic/scripting/lua/LuaState.hpp | 14 +-- src/logic/scripting/lua/libaudio.cpp | 116 ++++++++----------- src/logic/scripting/lua/libblock.cpp | 107 ++++++++--------- src/logic/scripting/lua/libconsole.cpp | 18 +-- src/logic/scripting/lua/libhud.cpp | 8 +- src/logic/scripting/lua/libinventory.cpp | 52 +++++---- src/logic/scripting/lua/libitem.cpp | 15 ++- src/logic/scripting/lua/libplayer.cpp | 105 ++++++++++++----- src/logic/scripting/lua/lua_commons.hpp | 10 +- src/logic/scripting/lua/lua_util.hpp | 14 +-- src/logic/scripting/scripting_functional.cpp | 7 +- src/objects/Player.cpp | 16 +++ src/objects/Player.hpp | 10 +- 16 files changed, 306 insertions(+), 224 deletions(-) diff --git a/doc/en/8.Scripting.md b/doc/en/8.Scripting.md index e18ab0ad..b0fb326f 100644 --- a/doc/en/8.Scripting.md +++ b/doc/en/8.Scripting.md @@ -69,6 +69,20 @@ player.get_inventory(playerid: int) -> int, int Returns player inventory ID and selected slot index (0-9) +```python +player.is_flight() -> bool +player.set_flight(bool) +``` + +Getter and setter for player flight mode + +```python +player.is_noclip() -> bool +player.set_noclip(bool) +``` + +Getter and setter for player noclip mode (physics disabled) + ## *world* library ```python diff --git a/doc/ru/8.Скриптинг.md b/doc/ru/8.Скриптинг.md index 82f27159..1b1c7f83 100644 --- a/doc/ru/8.Скриптинг.md +++ b/doc/ru/8.Скриптинг.md @@ -63,6 +63,20 @@ player.get_inventory(playerid: int) -> int, int Возвращает id инвентаря игрока и индекс выбранного слота (от 0 до 9) +```python +player.is_flight() -> bool +player.set_flight(bool) +``` + +Геттер и сеттер режима полета + +```python +player.is_noclip() -> bool +player.set_noclip(bool) +``` + +Геттер и сеттер noclip режима (выключенная физика игрока) + ## Библиотека world ```python diff --git a/src/logic/scripting/lua/LuaState.cpp b/src/logic/scripting/lua/LuaState.cpp index 1a860670..49f83439 100644 --- a/src/logic/scripting/lua/LuaState.cpp +++ b/src/logic/scripting/lua/LuaState.cpp @@ -187,12 +187,12 @@ int LuaState::gettop() const { return lua_gettop(L); } -int LuaState::pushinteger(luaint x) { +int LuaState::pushinteger(lua_Integer x) { lua_pushinteger(L, x); return 1; } -int LuaState::pushnumber(luanumber x) { +int LuaState::pushnumber(lua_Number x) { lua_pushnumber(L, x); return 1; } @@ -202,7 +202,7 @@ int LuaState::pushboolean(bool x) { return 1; } -int LuaState::pushivec3(luaint x, luaint y, luaint z) { +int LuaState::pushivec3(lua_Integer x, lua_Integer y, lua_Integer z) { lua::pushivec3(L, x, y, z); return 3; } @@ -291,11 +291,11 @@ bool LuaState::toboolean(int idx) { return lua_toboolean(L, idx); } -luaint LuaState::tointeger(int idx) { +lua_Integer LuaState::tointeger(int idx) { return lua_tointeger(L, idx); } -luanumber LuaState::tonumber(int idx) { +lua_Number LuaState::tonumber(int idx) { return lua_tonumber(L, idx); } diff --git a/src/logic/scripting/lua/LuaState.hpp b/src/logic/scripting/lua/LuaState.hpp index bac4c8aa..58c538bd 100644 --- a/src/logic/scripting/lua/LuaState.hpp +++ b/src/logic/scripting/lua/LuaState.hpp @@ -7,10 +7,6 @@ #include "../../../data/dynamic.hpp" #include "../../../delegates.hpp" -#ifndef LUAJIT_VERSION -#error LuaJIT required -#endif - #include #include @@ -36,9 +32,9 @@ namespace lua { static const std::string envName(int env); void loadbuffer(int env, const std::string& src, const std::string& file); int gettop() const; - int pushivec3(luaint x, luaint y, luaint z); - int pushinteger(luaint x); - int pushnumber(luanumber x); + int pushivec3(lua_Integer x, lua_Integer y, lua_Integer z); + int pushinteger(lua_Integer x); + int pushnumber(lua_Number x); int pushboolean(bool x); int pushstring(const std::string& str); int pushenv(int env); @@ -51,8 +47,8 @@ namespace lua { bool getfield(const std::string& name, int idx = -1); void setfield(const std::string& name, int idx = -2); bool toboolean(int idx); - luaint tointeger(int idx); - luanumber tonumber(int idx); + lua_Integer tointeger(int idx); + lua_Number tonumber(int idx); glm::vec2 tovec2(int idx); glm::vec4 tocolor(int idx); dynamic::Value tovalue(int idx); diff --git a/src/logic/scripting/lua/libaudio.cpp b/src/logic/scripting/lua/libaudio.cpp index 8f19d20e..833b00ae 100644 --- a/src/logic/scripting/lua/libaudio.cpp +++ b/src/logic/scripting/lua/libaudio.cpp @@ -23,22 +23,22 @@ inline int extract_channel_index(lua_State* L, int idx) { inline audio::speakerid_t play_sound( const char* name, bool relative, - lua::luanumber x, - lua::luanumber y, - lua::luanumber z, - lua::luanumber volume, - lua::luanumber pitch, + lua_Number x, + lua_Number y, + lua_Number z, + lua_Number volume, + lua_Number pitch, bool loop, int channel ) { - if (channel == -1) + if (channel == -1) { return 0; + } auto assets = scripting::engine->getAssets(); auto sound = assets->getSound(name); if (sound == nullptr) { return 0; } - return audio::play( sound, glm::vec3( @@ -58,20 +58,20 @@ inline audio::speakerid_t play_sound( inline audio::speakerid_t play_stream( const char* filename, bool relative, - lua::luanumber x, - lua::luanumber y, - lua::luanumber z, - lua::luanumber volume, - lua::luanumber pitch, + lua_Number x, + lua_Number y, + lua_Number z, + lua_Number volume, + lua_Number pitch, bool loop, int channel ) { - if (channel == -1) + if (channel == -1) { return 0; + } auto paths = scripting::engine->getResPaths(); - fs::path file = paths->find(filename); return audio::play_stream( - file, + paths->find(filename), glm::vec3( static_cast(x), static_cast(y), @@ -95,7 +95,7 @@ inline audio::speakerid_t play_stream( /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_stream(lua_State* L) { - lua_pushinteger(L, static_cast( + lua_pushinteger(L, static_cast( play_stream( lua_tostring(L, 1), false, @@ -118,7 +118,7 @@ static int l_audio_play_stream(lua_State* L) { /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_stream_2d(lua_State* L) { - lua_pushinteger(L, static_cast( + lua_pushinteger(L, static_cast( play_stream( lua_tostring(L, 1), true, @@ -142,7 +142,7 @@ static int l_audio_play_stream_2d(lua_State* L) { /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_sound(lua_State* L) { - lua_pushinteger(L, static_cast( + lua_pushinteger(L, static_cast( play_sound( lua_tostring(L, 1), false, @@ -165,7 +165,7 @@ static int l_audio_play_sound(lua_State* L) { /// channel: string = "regular", /// loop: bool = false) static int l_audio_play_sound_2d(lua_State* L) { - lua_pushinteger(L, static_cast( + lua_pushinteger(L, static_cast( play_sound( lua_tostring(L, 1), true, @@ -181,8 +181,7 @@ static int l_audio_play_sound_2d(lua_State* L) { /// @brief audio.stop(speakerid: integer) -> nil static int l_audio_stop(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { speaker->stop(); } @@ -191,8 +190,7 @@ static int l_audio_stop(lua_State* L) { /// @brief audio.pause(speakerid: integer) -> nil static int l_audio_pause(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { speaker->pause(); } @@ -201,8 +199,7 @@ static int l_audio_pause(lua_State* L) { /// @brief audio.resume(speakerid: integer) -> nil static int l_audio_resume(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr && speaker->isPaused()) { speaker->play(); } @@ -211,8 +208,7 @@ static int l_audio_resume(lua_State* L) { /// @brief audio.set_loop(speakerid: integer, value: bool) -> nil static int l_audio_set_loop(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { bool value = lua_toboolean(L, 2); speaker->setLoop(value); @@ -222,45 +218,38 @@ static int l_audio_set_loop(lua_State* L) { /// @brief audio.set_volume(speakerid: integer, value: number) -> nil static int l_audio_set_volume(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { - lua::luanumber value = lua_tonumber(L, 2); - speaker->setVolume(static_cast(value)); + speaker->setVolume(static_cast(lua_tonumber(L, 2))); } return 0; } /// @brief audio.set_pitch(speakerid: integer, value: number) -> nil static int l_audio_set_pitch(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { - lua::luanumber value = lua_tonumber(L, 2); - speaker->setPitch(static_cast(value)); + speaker->setPitch(static_cast(lua_tonumber(L, 2))); } return 0; } /// @brief audio.set_time(speakerid: integer, value: number) -> nil static int l_audio_set_time(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { - lua::luanumber value = lua_tonumber(L, 2); - speaker->setTime(static_cast(value)); + speaker->setTime(static_cast(lua_tonumber(L, 2))); } return 0; } /// @brief audio.set_position(speakerid: integer, x: number, y: number, z: number) -> nil static int l_audio_set_position(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { - lua::luanumber x = lua_tonumber(L, 2); - lua::luanumber y = lua_tonumber(L, 3); - lua::luanumber z = lua_tonumber(L, 4); + auto x = lua_tonumber(L, 2); + auto y = lua_tonumber(L, 3); + auto z = lua_tonumber(L, 4); speaker->setPosition(glm::vec3( static_cast(x), static_cast(y), @@ -272,12 +261,11 @@ static int l_audio_set_position(lua_State* L) { /// @brief audio.set_velocity(speakerid: integer, x: number, y: number, z: number) -> nil static int l_audio_set_velocity(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { - lua::luanumber x = lua_tonumber(L, 2); - lua::luanumber y = lua_tonumber(L, 3); - lua::luanumber z = lua_tonumber(L, 4); + auto x = lua_tonumber(L, 2); + auto y = lua_tonumber(L, 3); + auto z = lua_tonumber(L, 4); speaker->setVelocity(glm::vec3( static_cast(x), static_cast(y), @@ -289,8 +277,7 @@ static int l_audio_set_velocity(lua_State* L) { /// @brief audio.is_playing(speakerid: integer) -> bool static int l_audio_is_playing(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushboolean(L, speaker->isPlaying()); return 1; @@ -301,8 +288,7 @@ static int l_audio_is_playing(lua_State* L) { /// @brief audio.is_paused(speakerid: integer) -> bool static int l_audio_is_paused(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushboolean(L, speaker->isPaused()); return 1; @@ -313,8 +299,7 @@ static int l_audio_is_paused(lua_State* L) { /// @brief audio.is_loop(speakerid: integer) -> bool static int l_audio_is_loop(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushboolean(L, speaker->isLoop()); return 1; @@ -325,8 +310,7 @@ static int l_audio_is_loop(lua_State* L) { /// @brief audio.get_volume(speakerid: integer) -> number static int l_audio_get_volume(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushnumber(L, speaker->getVolume()); return 1; @@ -337,8 +321,7 @@ static int l_audio_get_volume(lua_State* L) { /// @brief audio.get_pitch(speakerid: integer) -> number static int l_audio_get_pitch(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushnumber(L, speaker->getPitch()); return 1; @@ -349,8 +332,7 @@ static int l_audio_get_pitch(lua_State* L) { /// @brief audio.get_time(speakerid: integer) -> number static int l_audio_get_time(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushnumber(L, speaker->getTime()); return 1; @@ -361,8 +343,7 @@ static int l_audio_get_time(lua_State* L) { /// @brief audio.get_duration(speakerid: integer) -> number static int l_audio_get_duration(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { lua_pushnumber(L, speaker->getDuration()); return 1; @@ -373,11 +354,9 @@ static int l_audio_get_duration(lua_State* L) { /// @brief audio.get_position(speakerid: integer) -> number, number, number static int l_audio_get_position(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { - auto vec = speaker->getPosition(); - lua::pushvec3(L, vec); + lua::pushvec3(L, speaker->getPosition()); return 1; } return 0; @@ -385,8 +364,7 @@ static int l_audio_get_position(lua_State* L) { /// @brief audio.get_velocity(speakerid: integer) -> number, number, number static int l_audio_get_velocity(lua_State* L) { - lua::luaint id = lua_tonumber(L, 1); - auto speaker = audio::get_speaker(id); + auto speaker = audio::get_speaker(lua_tointeger(L, 1)); if (speaker != nullptr) { auto vec = speaker->getVelocity(); lua::pushvec3(L, vec); diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index 2de58401..695d8e36 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -1,7 +1,9 @@ #include "lua_commons.hpp" + #include "api_lua.hpp" #include "lua_util.hpp" #include "../scripting.hpp" + #include "../../../world/Level.hpp" #include "../../../voxels/Chunks.hpp" #include "../../../voxels/Chunk.hpp" @@ -13,7 +15,7 @@ int l_block_name(lua_State* L) { auto indices = scripting::content->getIndices(); - lua::luaint id = lua_tointeger(L, 1); + lua_Integer id = lua_tointeger(L, 1); if (id < 0 || size_t(id) >= indices->countBlockDefs()) { return 0; } @@ -22,10 +24,9 @@ int l_block_name(lua_State* L) { return 1; } - int l_block_material(lua_State* L) { auto indices = scripting::content->getIndices(); - lua::luaint id = lua_tointeger(L, 1); + lua_Integer id = lua_tointeger(L, 1); if (id < 0 || size_t(id) >= indices->countBlockDefs()) { return 0; } @@ -35,9 +36,9 @@ int l_block_material(lua_State* L) { } int l_is_solid_at(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); lua_pushboolean(L, scripting::level->chunks->isSolidBlock(x, y, z)); return 1; @@ -49,17 +50,17 @@ int l_blocks_count(lua_State* L) { } int l_block_index(lua_State* L) { - auto name = lua_tostring(L, 1); + std::string name = lua_tostring(L, 1); lua_pushinteger(L, scripting::content->requireBlock(name).rt.id); return 1; } int l_set_block(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); - lua::luaint id = lua_tointeger(L, 4); - lua::luaint states = lua_tointeger(L, 5); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + lua_Integer id = lua_tointeger(L, 4); + lua_Integer states = lua_tointeger(L, 5); bool noupdate = lua_toboolean(L, 6); if (id < 0 || size_t(id) >= scripting::indices->countBlockDefs()) { return 0; @@ -75,9 +76,9 @@ int l_set_block(lua_State* L) { } int l_get_block(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); int id = vox == nullptr ? -1 : vox->id; lua_pushinteger(L, id); @@ -85,9 +86,9 @@ int l_get_block(lua_State* L) { } int l_get_block_x(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 1, 0, 0); @@ -102,9 +103,9 @@ int l_get_block_x(lua_State* L) { } int l_get_block_y(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 0, 1, 0); @@ -119,9 +120,9 @@ int l_get_block_y(lua_State* L) { } int l_get_block_z(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return lua::pushivec3(L, 0, 0, 1); @@ -136,9 +137,9 @@ int l_get_block_z(lua_State* L) { } int l_get_block_rotation(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); int rotation = vox == nullptr ? 0 : vox->rotation(); lua_pushinteger(L, rotation); @@ -146,10 +147,10 @@ int l_get_block_rotation(lua_State* L) { } int l_set_block_rotation(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); - lua::luaint value = lua_tointeger(L, 4); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + lua_Integer value = lua_tointeger(L, 4); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { return 0; @@ -160,9 +161,9 @@ int l_set_block_rotation(lua_State* L) { } int l_get_block_states(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); voxel* vox = scripting::level->chunks->get(x, y, z); int states = vox == nullptr ? 0 : vox->states; lua_pushinteger(L, states); @@ -170,10 +171,10 @@ int l_get_block_states(lua_State* L) { } int l_set_block_states(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); - lua::luaint states = lua_tointeger(L, 4); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + lua_Integer states = lua_tointeger(L, 4); Chunk* chunk = scripting::level->chunks->getChunkByVoxel(x, y, z); if (chunk == nullptr) { @@ -186,11 +187,11 @@ int l_set_block_states(lua_State* L) { } int l_get_block_user_bits(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); - lua::luaint offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; - lua::luaint bits = lua_tointeger(L, 5); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + lua_Integer offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; + lua_Integer bits = lua_tointeger(L, 5); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { @@ -204,14 +205,14 @@ int l_get_block_user_bits(lua_State* L) { } int l_set_block_user_bits(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); - lua::luaint offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; - lua::luaint bits = lua_tointeger(L, 5); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); + lua_Integer offset = lua_tointeger(L, 4) + VOXEL_USER_BITS_OFFSET; + lua_Integer bits = lua_tointeger(L, 5); - uint mask = ((1 << bits) - 1) << offset; - lua::luaint value = (lua_tointeger(L, 6) << offset) & mask; + size_t mask = ((1 << bits) - 1) << offset; + lua_Integer value = (lua_tointeger(L, 6) << offset) & mask; voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { @@ -222,9 +223,9 @@ int l_set_block_user_bits(lua_State* L) { } int l_is_replaceable_at(lua_State* L) { - int x = lua_tointeger(L, 1); - int y = lua_tointeger(L, 2); - int z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); lua_pushboolean(L, scripting::level->chunks->isReplaceableBlock(x, y, z)); return 1; diff --git a/src/logic/scripting/lua/libconsole.cpp b/src/logic/scripting/lua/libconsole.cpp index 52b1010d..88238f65 100644 --- a/src/logic/scripting/lua/libconsole.cpp +++ b/src/logic/scripting/lua/libconsole.cpp @@ -14,11 +14,11 @@ namespace scripting { using namespace scripting; static int l_add_command(lua_State* L) { - if (!lua_isstring(L, 1) || !lua_isstring(L, 2) || !lua_isfunction(L, 3)) { - throw std::runtime_error("invalid argument type"); + if (!lua_isfunction(L, 3)) { + throw std::runtime_error("invalid callback"); } - auto scheme = lua_tostring(L, 1); - auto description = lua_tostring(L, 2); + auto scheme = state->requireString(1); + auto description = state->requireString(2); lua_pushvalue(L, 3); auto func = state->createLambda(); try { @@ -33,15 +33,15 @@ static int l_add_command(lua_State* L) { return 0; } -static int l_execute(lua_State* L) { - auto prompt = lua_tostring(L, 1); +static int l_execute(lua_State*) { + auto prompt = state->requireString(1); auto result = engine->getCommandsInterpreter()->execute(prompt); state->pushvalue(result); return 1; } -static int l_set(lua_State* L) { - auto name = lua_tostring(L, 1); +static int l_set(lua_State*) { + auto name = state->requireString(1); auto value = state->tovalue(2); (*engine->getCommandsInterpreter())[name] = value; return 0; @@ -62,7 +62,7 @@ static int l_get_commands_list(lua_State* L) { } static int l_get_command_info(lua_State* L) { - auto name = lua_tostring(L, 1); + auto name = state->requireString(1); auto interpreter = engine->getCommandsInterpreter(); auto repo = interpreter->getRepository(); auto command = repo->get(name); diff --git a/src/logic/scripting/lua/libhud.cpp b/src/logic/scripting/lua/libhud.cpp index b7c023f5..93d3505b 100644 --- a/src/logic/scripting/lua/libhud.cpp +++ b/src/logic/scripting/lua/libhud.cpp @@ -40,14 +40,14 @@ static int l_hud_close_inventory(lua_State*) { } static int l_hud_open_block(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + auto x = lua_tointeger(L, 1); + auto y = lua_tointeger(L, 2); + auto z = lua_tointeger(L, 3); bool playerInventory = !lua_toboolean(L, 4); voxel* vox = scripting::level->chunks->get(x, y, z); if (vox == nullptr) { - throw std::runtime_error("block does not exists at "+ + throw std::runtime_error("block does not exists at " + std::to_string(x) + " " + std::to_string(y) + " " + std::to_string(z) ); } diff --git a/src/logic/scripting/lua/libinventory.cpp b/src/logic/scripting/lua/libinventory.cpp index 067c7943..f6d164e1 100644 --- a/src/logic/scripting/lua/libinventory.cpp +++ b/src/logic/scripting/lua/libinventory.cpp @@ -1,6 +1,8 @@ #include "lua_commons.hpp" + #include "api_lua.hpp" #include "lua_util.hpp" + #include "../scripting.hpp" #include "../../../content/Content.hpp" #include "../../../world/Level.hpp" @@ -38,8 +40,8 @@ static void validate_slotid(int slotid, Inventory* inv) { } static int l_inventory_get(lua_State* L) { - lua::luaint invid = lua_tointeger(L, 1); - lua::luaint slotid = lua_tointeger(L, 2); + lua_Integer invid = lua_tointeger(L, 1); + lua_Integer slotid = lua_tointeger(L, 2); auto inv = get_inventory(invid); validate_slotid(slotid, inv.get()); const ItemStack& item = inv->getSlot(slotid); @@ -49,10 +51,10 @@ static int l_inventory_get(lua_State* L) { } static int l_inventory_set(lua_State* L) { - lua::luaint invid = lua_tointeger(L, 1); - lua::luaint slotid = lua_tointeger(L, 2); - lua::luaint itemid = lua_tointeger(L, 3); - lua::luaint count = lua_tointeger(L, 4); + lua_Integer invid = lua_tointeger(L, 1); + lua_Integer slotid = lua_tointeger(L, 2); + lua_Integer itemid = lua_tointeger(L, 3); + lua_Integer count = lua_tointeger(L, 4); validate_itemid(itemid); auto inv = get_inventory(invid); @@ -64,16 +66,16 @@ static int l_inventory_set(lua_State* L) { } static int l_inventory_size(lua_State* L) { - lua::luaint invid = lua_tointeger(L, 1); + lua_Integer invid = lua_tointeger(L, 1); auto inv = get_inventory(invid); lua_pushinteger(L, inv->size()); return 1; } static int l_inventory_add(lua_State* L) { - lua::luaint invid = lua_tointeger(L, 1); - lua::luaint itemid = lua_tointeger(L, 2); - lua::luaint count = lua_tointeger(L, 3); + lua_Integer invid = lua_tointeger(L, 1); + lua_Integer itemid = lua_tointeger(L, 2); + lua_Integer count = lua_tointeger(L, 3); validate_itemid(itemid); auto inv = get_inventory(invid); @@ -84,33 +86,33 @@ static int l_inventory_add(lua_State* L) { } static int l_inventory_get_block(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); int64_t id = scripting::blocks->createBlockInventory(x, y, z); lua_pushinteger(L, id); return 1; } static int l_inventory_bind_block(lua_State* L) { - lua::luaint id = lua_tointeger(L, 1); - lua::luaint x = lua_tointeger(L, 2); - lua::luaint y = lua_tointeger(L, 3); - lua::luaint z = lua_tointeger(L, 4); + lua_Integer id = lua_tointeger(L, 1); + lua_Integer x = lua_tointeger(L, 2); + lua_Integer y = lua_tointeger(L, 3); + lua_Integer z = lua_tointeger(L, 4); scripting::blocks->bindInventory(id, x, y, z); return 0; } static int l_inventory_unbind_block(lua_State* L) { - lua::luaint x = lua_tointeger(L, 1); - lua::luaint y = lua_tointeger(L, 2); - lua::luaint z = lua_tointeger(L, 3); + lua_Integer x = lua_tointeger(L, 1); + lua_Integer y = lua_tointeger(L, 2); + lua_Integer z = lua_tointeger(L, 3); scripting::blocks->unbindInventory(x, y, z); return 0; } static int l_inventory_clone(lua_State* L) { - lua::luaint id = lua_tointeger(L, 1); + lua_Integer id = lua_tointeger(L, 1); auto clone = scripting::level->inventories->clone(id); if (clone == nullptr) { lua_pushinteger(L, 0); @@ -121,13 +123,13 @@ static int l_inventory_clone(lua_State* L) { } static int l_inventory_move(lua_State* L) { - lua::luaint invAid = lua_tointeger(L, 1); - lua::luaint slotAid = lua_tointeger(L, 2); + lua_Integer invAid = lua_tointeger(L, 1); + lua_Integer slotAid = lua_tointeger(L, 2); auto invA = get_inventory(invAid, 1); validate_slotid(slotAid, invA.get()); - lua::luaint invBid = lua_tointeger(L, 3); - lua::luaint slotBid = lua_isnil(L, 4) ? -1 : lua_tointeger(L, 4); + lua_Integer invBid = lua_tointeger(L, 3); + lua_Integer slotBid = lua_isnil(L, 4) ? -1 : lua_tointeger(L, 4); auto invB = get_inventory(invBid, 3); auto& slot = invA->getSlot(slotAid); if (slotBid == -1) { diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libitem.cpp index 62760099..c8d6c11a 100644 --- a/src/logic/scripting/lua/libitem.cpp +++ b/src/logic/scripting/lua/libitem.cpp @@ -1,13 +1,20 @@ #include "lua_commons.hpp" #include "api_lua.hpp" +#include "LuaState.hpp" #include "../scripting.hpp" #include "../../../content/Content.hpp" #include "../../../items/ItemDef.hpp" +namespace scripting { + extern lua::LuaState* state; +} + +using namespace scripting; + static int l_item_name(lua_State* L) { auto indices = scripting::content->getIndices(); - lua::luaint id = lua_tointeger(L, 1); - if (id < 0 || size_t(id) >= indices->countItemDefs()) { + lua_Number id = lua_tointeger(L, 1); + if (static_cast(id) >= indices->countItemDefs()) { return 0; } auto def = indices->getItemDef(id); @@ -16,14 +23,14 @@ static int l_item_name(lua_State* L) { } static int l_item_index(lua_State* L) { - auto name = lua_tostring(L, 1); + auto name = scripting::state->requireString(1); lua_pushinteger(L, scripting::content->requireItem(name).rt.id); return 1; } static int l_item_stack_size(lua_State* L) { auto indices = scripting::content->getIndices(); - lua::luaint id = lua_tointeger(L, 1); + lua_Integer id = lua_tointeger(L, 1); if (id < 0 || size_t(id) >= indices->countItemDefs()) { return 0; } diff --git a/src/logic/scripting/lua/libplayer.cpp b/src/logic/scripting/lua/libplayer.cpp index f5f168106..5c27d954 100644 --- a/src/logic/scripting/lua/libplayer.cpp +++ b/src/logic/scripting/lua/libplayer.cpp @@ -9,10 +9,15 @@ #include +inline std::shared_ptr get_player(lua_State* L, int idx) { + return scripting::level->getObject(lua_tointeger(L, idx)); +} + static int l_player_get_pos(lua_State* L) { - int playerid = lua_tointeger(L, 1); - auto player = scripting::level->getObject(playerid); - if (!player) return 0; + auto player = get_player(L, 1); + if (!player) { + return 0; + } glm::vec3 pos = player->hitbox->position; lua_pushnumber(L, pos.x); lua_pushnumber(L, pos.y); @@ -21,19 +26,22 @@ static int l_player_get_pos(lua_State* L) { } static int l_player_set_pos(lua_State* L) { - int playerid = lua_tointeger(L, 1); - lua::luanumber x = lua_tonumber(L, 2); - lua::luanumber y = lua_tonumber(L, 3); - lua::luanumber z = lua_tonumber(L, 4); - auto player = scripting::level->getObject(playerid); - if (player) player->hitbox->position = glm::vec3(x, y, z); + auto player = get_player(L, 1); + if (!player) { + return 0; + } + auto x = lua_tonumber(L, 2); + auto y = lua_tonumber(L, 3); + auto z = lua_tonumber(L, 4); + player->hitbox->position = glm::vec3(x, y, z); return 0; } static int l_player_get_vel(lua_State* L) { - int playerid = lua_tointeger(L, 1); - auto player = scripting::level->getObject(playerid); - if (!player) return 0; + auto player = get_player(L, 1); + if (!player) { + return 0; + } glm::vec3 vel = player->hitbox->velocity; lua_pushnumber(L, vel.x); lua_pushnumber(L, vel.y); @@ -42,19 +50,22 @@ static int l_player_get_vel(lua_State* L) { } static int l_player_set_vel(lua_State* L) { - int playerid = lua_tointeger(L, 1); - lua::luanumber x = lua_tonumber(L, 2); - lua::luanumber y = lua_tonumber(L, 3); - lua::luanumber z = lua_tonumber(L, 4); - auto player = scripting::level->getObject(playerid); - if (player) player->hitbox->velocity = glm::vec3(x, y, z); + auto player = get_player(L, 1); + if (!player) { + return 0; + } + auto x = lua_tonumber(L, 2); + auto y = lua_tonumber(L, 3); + auto z = lua_tonumber(L, 4); + player->hitbox->velocity = glm::vec3(x, y, z); return 0; } static int l_player_get_rot(lua_State* L) { - int playerid = lua_tointeger(L, 1); - auto player = scripting::level->getObject(playerid); - if (!player) return 0; + auto player = get_player(L, 1); + if (!player) { + return 0; + } glm::vec2 rot = player->cam; lua_pushnumber(L, rot.x); lua_pushnumber(L, rot.y); @@ -62,11 +73,12 @@ static int l_player_get_rot(lua_State* L) { } static int l_player_set_rot(lua_State* L) { - int playerid = lua_tointeger(L, 1); - auto player = scripting::level->getObject(playerid); - if (!player) return 0; - lua::luanumber x = lua_tonumber(L, 2); - lua::luanumber y = lua_tonumber(L, 3); + auto player = get_player(L, 1); + if (!player) { + return 0; + } + auto x = lua_tonumber(L, 2); + auto y = lua_tonumber(L, 3); glm::vec2& cam = player->cam; cam.x = x; cam.y = y; @@ -74,14 +86,45 @@ static int l_player_set_rot(lua_State* L) { } static int l_player_get_inv(lua_State* L) { - int playerid = lua_tointeger(L, 1); - auto player = scripting::level->getObject(playerid); - if (!player) return 0; + auto player = get_player(L, 1); + if (!player) { + return 0; + } lua_pushinteger(L, player->getInventory()->getId()); lua_pushinteger(L, player->getChosenSlot()); return 2; } +static int l_player_is_flight(lua_State* L) { + if (auto player = get_player(L, 1)) { + lua_pushboolean(L, player->isFlight()); + return 1; + } + return 0; +} + +static int l_player_set_flight(lua_State* L) { + if (auto player = get_player(L, 1)) { + player->setFlight(lua_toboolean(L, 2)); + } + return 0; +} + +static int l_player_is_noclip(lua_State* L) { + if (auto player = get_player(L, 1)) { + lua_pushboolean(L, player->isNoclip()); + return 1; + } + return 0; +} + +static int l_player_set_noclip(lua_State* L) { + if (auto player = get_player(L, 1)) { + player->setNoclip(lua_toboolean(L, 2)); + } + return 0; +} + const luaL_Reg playerlib [] = { {"get_pos", lua_wrap_errors}, {"set_pos", lua_wrap_errors}, @@ -90,5 +133,9 @@ const luaL_Reg playerlib [] = { {"get_rot", lua_wrap_errors}, {"set_rot", lua_wrap_errors}, {"get_inventory", lua_wrap_errors}, + {"is_flight", lua_wrap_errors}, + {"set_flight", lua_wrap_errors}, + {"is_noclip", lua_wrap_errors}, + {"set_noclip", lua_wrap_errors}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/lua_commons.hpp b/src/logic/scripting/lua/lua_commons.hpp index 38d17ebc..f51b7aac 100644 --- a/src/logic/scripting/lua/lua_commons.hpp +++ b/src/logic/scripting/lua/lua_commons.hpp @@ -7,14 +7,14 @@ #else #include #endif + +#ifndef LUAJIT_VERSION +#error LuaJIT required +#endif + #include #include -namespace lua { - using luaint = lua_Integer; - using luanumber = lua_Number; -} - template int lua_wrap_errors(lua_State *L) { int result = 0; try { diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 4d9e37bf..0c4de9f3 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -14,7 +14,7 @@ #include namespace lua { - inline int pushivec3(lua_State* L, luaint x, luaint y, luaint z) { + inline int pushivec3(lua_State* L, lua_Integer x, lua_Integer y, lua_Integer z) { lua_pushinteger(L, x); lua_pushinteger(L, y); lua_pushinteger(L, z); @@ -100,9 +100,9 @@ namespace lua { throw std::runtime_error("value must be an array of two numbers"); } lua_rawgeti(L, -1, 1); - lua::luanumber x = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number x = lua_tonumber(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); - lua::luanumber y = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number y = lua_tonumber(L, -1); lua_pop(L, 1); lua_pop(L, 1); return glm::vec2(x, y); } @@ -113,13 +113,13 @@ namespace lua { throw std::runtime_error("RGBA array required"); } lua_rawgeti(L, -1, 1); - lua::luanumber r = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number r = lua_tonumber(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 2); - lua::luanumber g = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number g = lua_tonumber(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 3); - lua::luanumber b = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number b = lua_tonumber(L, -1); lua_pop(L, 1); lua_rawgeti(L, -1, 4); - lua::luanumber a = lua_tonumber(L, -1); lua_pop(L, 1); + lua_Number a = lua_tonumber(L, -1); lua_pop(L, 1); lua_pop(L, 1); return glm::vec4(r/255, g/255, b/255, a/255); } diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index 7597ce66..644b2b95 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -137,7 +137,8 @@ doublesupplier scripting::create_number_supplier( if (state->isfunction(-1)) { state->callNoThrow(0); } - lua::luanumber x = state->tonumber(-1); state->pop(); + auto x = state->tonumber(-1); + state->pop(); return x; } return 0.0; @@ -169,8 +170,8 @@ vec2supplier scripting::create_vec2_supplier( if (state->isfunction(-1)) { state->callNoThrow(0); } - lua::luanumber y = state->tonumber(-1); state->pop(); - lua::luanumber x = state->tonumber(-1); state->pop(); + auto y = state->tonumber(-1); state->pop(); + auto x = state->tonumber(-1); state->pop(); return glm::vec2(x, y); } return glm::vec2(0, 0); diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index ba8ccce5..331c1f60 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -167,6 +167,22 @@ float Player::getSpeed() const { return speed; } +bool Player::isFlight() const { + return flight; +} + +void Player::setFlight(bool flag) { + this->flight = flag; +} + +bool Player::isNoclip() const { + return noclip; +} + +void Player::setNoclip(bool flag) { + this->noclip = flag; +} + std::shared_ptr Player::getInventory() const { return inventory; } diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index 54aa4b12..24825e4a 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -37,12 +37,12 @@ class Player : public Object, public Serializable { int chosenSlot; glm::vec3 spawnpoint {}; std::shared_ptr inventory; + bool flight = false; + bool noclip = false; public: std::shared_ptr camera, spCamera, tpCamera; std::shared_ptr currentCamera; std::unique_ptr hitbox; - bool flight = false; - bool noclip = false; bool debug = false; voxel selectedVoxel {0, 0}; glm::vec2 cam {}; @@ -59,6 +59,12 @@ public: int getChosenSlot() const; float getSpeed() const; + + bool isFlight() const; + void setFlight(bool flag); + + bool isNoclip() const; + void setNoclip(bool flag); std::shared_ptr getInventory() const;