From d56d6f890a02009300b5319755e3dc64f017dcd4 Mon Sep 17 00:00:00 2001 From: Nicole Collings <100Toby1@gmail.com> Date: Thu, 23 Jul 2020 18:54:11 -0700 Subject: [PATCH] Update to sol3, vec3 customization, C++ 17 now required. --- CMakeLists.txt | 23 ++- assets/base/script/modules/vector.lua | 174 +++++++++--------- src/CMakeLists.txt | 2 +- src/Main.cpp | 12 ++ src/def/item/BlockDef.h | 2 +- src/game/entity/Model.h | 1 + src/game/hud/GameGuiBuilder.cpp | 16 +- src/game/hud/GuiBuilder.cpp | 16 +- src/game/hud/SerialGui.h | 1 - src/game/hud/components/GuiComponent.h | 2 + src/game/inventory/InventoryList.cpp | 4 +- src/game/inventory/InventoryList.h | 4 +- src/game/inventory/ServerInventoryList.cpp | 1 + src/game/scene/world/LocalWorld.cpp | 6 +- src/lua/Lua.h | 6 + src/lua/LuaKeybindHandler.h | 2 +- src/lua/LuaParser.cpp | 19 +- src/lua/LuaParser.h | 6 +- .../api/class/LocalLuaAnimationManager.cpp | 5 +- src/lua/api/class/LocalLuaAnimationManager.h | 2 +- src/lua/api/class/LocalLuaEntity.cpp | 27 ++- src/lua/api/class/LocalLuaEntity.h | 15 +- src/lua/api/class/LocalLuaInventory.h | 3 +- src/lua/api/class/LocalLuaPlayer.cpp | 23 +-- src/lua/api/class/LocalLuaPlayer.h | 12 +- src/lua/api/class/LuaGuiElement.cpp | 3 +- src/lua/api/class/LuaGuiElement.h | 2 +- src/lua/api/class/LuaItemStack.cpp | 1 + src/lua/api/class/LuaItemStack.h | 3 +- src/lua/api/class/ServerLuaEntity.cpp | 26 ++- src/lua/api/class/ServerLuaEntity.h | 14 +- src/lua/api/class/ServerLuaInventory.cpp | 2 +- src/lua/api/class/ServerLuaInventoryList.cpp | 14 +- src/lua/api/class/ServerLuaInventoryList.h | 4 +- src/lua/api/class/ServerLuaPlayer.cpp | 23 +-- src/lua/api/class/ServerLuaPlayer.h | 13 +- src/lua/api/functions/trigger_event.h | 11 +- src/lua/api/functions/update_entities.h | 2 +- src/lua/api/menu/mDelay.h | 2 +- src/lua/api/menu/mSetGui.h | 3 +- src/lua/api/menu/mStartGame.h | 3 +- src/lua/api/modules/add_entity.h | 34 ++-- src/lua/api/modules/create_structure.h | 4 +- src/lua/api/modules/get_block.h | 7 +- src/lua/api/modules/set_block.h | 5 +- src/lua/api/usertype/cInventoryRef.h | 3 +- src/lua/api/usertype/cItemStack.h | 3 +- src/lua/api/usertype/cLuaEntity.h | 3 +- src/lua/api/usertype/sInventoryRef.h | 3 +- src/lua/api/usertype/sLuaEntity.h | 3 +- src/lua/customization/vec3.hpp | 129 +++++++++++++ src/lua/parser/LocalLuaParser.cpp | 41 +++-- src/lua/parser/ServerLuaParser.cpp | 48 +++-- src/lua/register/RegisterBiomes.h | 3 +- src/lua/register/RegisterBlocks.h | 3 +- src/lua/register/RegisterItems.h | 3 +- src/lua/register/RegisterKeybinds.h | 3 +- src/net/Deserializer.h | 3 +- src/net/Serializer.h | 3 +- src/net/server/Server.cpp | 42 ++--- src/net/server/world/ServerWorld.cpp | 14 +- .../mods/aurailus_crafting/script/init.lua | 4 +- .../script/dropped_item.lua | 2 +- .../mods/zeus_default/script/entity/test.lua | 4 +- .../zeus/mods/zeus_default/script/init.lua | 9 +- .../zeus_mapgen/script/biomes/highlands.lua | 2 +- test/Main.cpp | 2 +- test/tests/TestBlockChunk.cpp | 1 - test/tests/TestSerializer.cpp | 4 +- 69 files changed, 487 insertions(+), 408 deletions(-) create mode 100644 src/lua/Lua.h create mode 100644 src/lua/customization/vec3.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9297085a..bd23c7e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,16 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) +# Set warnings +if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")) + add_compile_options( + -Werror -Wall -Wextra -pedantic -pedantic-errors + -Wnon-virtual-dtor -Wmisleading-indentation -Wlogical-op -Wnull-dereference + -Wno-unused-parameter -Wno-reorder -Wno-sign-compare) +elseif (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") + add_compile_options(/permissive /W4 /w14640) +endif() + set(PROJECT_NAME "Zepha") set(MAIN_EXEC_NAME "Zepha") set(TEST_EXEC_NAME "ZephaTest") @@ -16,7 +26,7 @@ find_path(LUA_HEADERS lua.hpp /usr/local/include/lua5.1) find_path(ASSIMP_HEADERS assimp/Importer.hpp) find_path(ENET_HEADERS enet/enet.h) -find_path(NOISE_HEADERS libnoise/noise.h) +#find_path(NOISE_HEADERS libnoise/noise.h) find_path(GLM_HEADERS glm/glm.hpp) find_path(PTHREAD_HEADERS pthread.h) @@ -34,11 +44,8 @@ else() find_library(PTHREAD_LIB pthread) endif() -if (WIN32) -set(SOL_HEADERS lib/header/sol2/include) -else() -set(SOL_HEADERS lib/header/sol2/include_linux) -endif() +set(SOL_HEADERS lib/header/sol/include) +set(NOISE_HEADERS lib/static/noise/include) include_directories( ${GLM_HEADERS} @@ -69,9 +76,6 @@ if(WIN32) target_link_libraries(${MAIN_EXEC_NAME} winmm ws2_32) endif() -# Enable Safeties -target_compile_definitions(${MAIN_EXEC_NAME} PUBLIC SOL_ALL_SAFETIES_ON) - # Test Build add_subdirectory(test) add_executable(${TEST_EXEC_NAME} test/Main.cpp) @@ -79,7 +83,6 @@ target_link_libraries(${TEST_EXEC_NAME} Zepha_Core) target_link_libraries(${TEST_EXEC_NAME} Zepha_Test) target_include_directories(${TEST_EXEC_NAME} PRIVATE ${GLFW_HEADERS}) -target_compile_definitions(${TEST_EXEC_NAME} PUBLIC SOL_ALL_SAFETIES_ON) target_link_libraries(${TEST_EXEC_NAME} ${LUA_LIB}) target_link_libraries (${TEST_EXEC_NAME} z) target_link_libraries(${TEST_EXEC_NAME} ${ENET_LIB}) \ No newline at end of file diff --git a/assets/base/script/modules/vector.lua b/assets/base/script/modules/vector.lua index 6b201679..18aa7ebd 100644 --- a/assets/base/script/modules/vector.lua +++ b/assets/base/script/modules/vector.lua @@ -8,38 +8,57 @@ _G["vector"] = {} -- x, y, and z must all be numbers. local function create_vector(x, y, z) local v = {x, y, z} - setmetatable(v, vector._mt) + setmetatable(v, vector) return v end +-- vector.equal +-- Return a boolean indicating if v1 == v2 +function vector.equal(v1, v2) + assert(vector.is_vector(v1) and vector.is_vector(v2)) + return (rawget(v1, 1) == rawget(v2, 1) and rawget(v1, 2) == rawget(v2, 2) and rawget(v1, 3) == rawget(v2, 3)) +end + +vector.__eq = vector.equal + +-- vector.copy +-- Copies v and returns a new vector +function vector.copy(v) + assert(vector.is_vector(v)) + return create_vector(rawget(v, 1), rawget(v, 2), rawget(v, 3)) +end + -- vector.add -- Add two vectors or a vector and number function vector.add(v1, v2) - if not vector.is_vector(v1) then return end - if vector.is_vector(v2) then - return create_vector(rawget(v1, 1) + rawget(v2, 1), rawget(v1, 2) + rawget(v2, 2), rawget(v1, 3) + rawget(v2, 3)) - elseif type(v2) == "number" then - return create_vector(rawget(v1, 1) + v2, rawget(v1, 2) + v2, rawget(v1, 3) + v2) - end + assert(vector.is_vector(v1)) + if vector.is_vector(v2) then return create_vector(rawget(v1, 1) + rawget(v2, 1), rawget(v1, 2) + rawget(v2, 2), rawget(v1, 3) + rawget(v2, 3)) + elseif type(v2) == "number" then return create_vector(rawget(v1, 1) + v2, rawget(v1, 2) + v2, rawget(v1, 3) + v2) end end +vector.__add = vector.add + -- vector.negative -- Flips a vector's content's signs function vector.negative(v) - if not vector.is_vector(v) then return end + assert(vector.is_vector(v)) return create_vector(-rawget(v, 1), -rawget(v, 2), -rawget(v, 3)) end +vector.__unm = vector.negative + -- vector.subtract -- Subtract v2 from v1 function vector.subtract(v1, v2) return vector.add(v1, vector.negative(v2)) end +vector.__sub = vector.subtract + -- vector.multiply -- Multiply v1 by a vector or number function vector.multiply(v1, m) - if not vector.is_vector(v1) then return end + assert(vector.is_vector(v1)) if vector.is_vector(m) then return create_vector(rawget(v1, 1) * rawget(m, 1), rawget(v1, 2) * rawget(m, 2), rawget(v1, 3) * rawget(m, 3)) elseif type(m) == "number" then @@ -47,10 +66,12 @@ function vector.multiply(v1, m) end end +vector.__mul = vector.multiply + -- vector.divide -- Divice v1 by a vector or number function vector.divide(v1, m) - if not vector.is_vector(v1) then return end + assert(vector.is_vector(v1)) if vector.is_vector(m) then return create_vector(rawget(v1, 1) / rawget(m, 1), rawget(v1, 2) / rawget(m, 2), rawget(v1, 3) / rawget(m, 3)) elseif type(m) == "number" then @@ -58,134 +79,107 @@ function vector.divide(v1, m) end end +vector.__div = vector.divide + -- vector.pow -- Return v to the power of p function vector.pow(v, m) - if not vector.is_vector(v) then return end + assert(vector.is_vector(v)) local res = create_vector(rawget(v, 1), rawget(v, 2), rawget(v, 3)) for i = 1, power - 1 do res = res * v end return res end --- vector.equal --- Return a boolean indicating if v1 == v2 -function vector.equal(v1, v2) - if not vector.is_vector(v1) or not vector.is_vector(v2) then return end - return (rawget(v1, 1) == rawget(v2, 1) and rawget(v1, 2) == rawget(v2, 2) and rawget(v1, 3) == rawget(v2, 3)) -end +vector.__pow = vector.pow -- vector.abs -- Return the absolute value of v function vector.abs(v) - if not vector.is_vector(v) then return end + assert(vector.is_vector(v)) return create_vector(math.abs(rawget(v, 1)), math.abs(rawget(v, 2)), math.abs(rawget(v, 3))) end -- vector.round -- Round each vector value to the nearest integer function vector.round(v) - if not vector.is_vector(v) then return end + assert(vector.is_vector(v)) return create_vector(math.round(rawget(v, 1)), math.round(rawget(v, 2)), math.round(rawget(v, 3))) end -- vector.floor -- Floor each vector value to the lowest integer function vector.floor(v) - if not vector.is_vector(v) then return end + assert(vector.is_vector(v)) return create_vector(math.floor(rawget(v, 1)), math.floor(rawget(v, 2)), math.floor(rawget(v, 3))) end -- vector.ceil -- Ceil each vector value to the highest integer function vector.ceil(v) - if not vector.is_vector(v) then return end + assert(vector.is_vector(v)) return create_vector(math.ceil(rawget(v, 1)), math.ceil(rawget(v, 2)), math.ceil(rawget(v, 3))) end --- vector.distance_squared +-- vector.length2 +-- Get the square of the length of a vector +function vector.length2(v) + assert(vector.is_vector(v)) + return rawget(v, 1) ^ 2 + rawget(v, 2) ^ 2 + rawget(v, 3) ^ 2 +end + +-- vector.length +-- Get the length of a vector +function vector.length(v) + assert(vector.is_vector(v)) + return math.sqrt(vector.length2(v)) +end + +-- vector.distance2 -- Get the square of the distance between two vectors --- This function is faster if you only need to compare two distances -function vector.distance_squared(v1, v2) - if not vector.is_vector(v1) or not vector.is_vector(v2) then return end - local diff = vector.abs(vector.subtract(v1, v2)) - return diff.x ^ 2 + diff.y ^ 2 + diff.z ^ 2 +function vector.distance2(v1, v2) + assert(vector.is_vector(v1) and vector.is_vector(v2)) + return vector.length2(vector.abs(vector.subtract(v1, v2))) end -- vector.distance -- Get the distance between two vectors function vector.distance(v1, v2) - if not vector.is_vector(v1) or not vector.is_vector(v2) then return end - return math.sqrt(vector.distance_squared(v1, v2)) + assert(vector.is_vector(v1) and vector.is_vector(v2)) + return math.sqrt(vector.distance2(v1, v2)) end --- Vector metatable --- A metatable to be assigned to vectors which applies mathematic operators -vector._mt = { - __is_vector = true, +-- Alias x, y, z to 1, 2, 3 - -- Value manipulation functions - __index = function(tbl, key) - if key == "x" then return rawget(tbl, 1) end - if key == "y" then return rawget(tbl, 2) end - if key == "z" then return rawget(tbl, 3) end +function vector:__index(key) + if key == "x" then return rawget(self, 1) end + if key == "y" then return rawget(self, 2) end + if key == "z" then return rawget(self, 3) end + return getmetatable(self)[key] +end - local val = rawget(tbl, key) - if val == nil then val = rawget(getmetatable(tbl), key) end - return val - end, - __newindex = function(tbl, key, val) - if key == "x" or key == 0 then rawset(tbl, 1, val) - elseif key == "y" or key == 1 then rawset(tbl, 2, val) - elseif key == "z" or key == 2 then rawset(tbl, 3, val) end - end, - __tostring = function(tbl) - return table.concat({ - "{ ", - tostring(tbl[1]), ", ", - tostring(tbl[2]), ", ", - tostring(tbl[3]), " }" - }) - end, - __eq = function(tbl, o) - return vector.equal(tbl, o) - end, +function vector:__newindex(key, val) + if key == "x" then rawset(self, 1, val) + elseif key == "y" then rawset(self, 2, val) + elseif key == "z" then rawset(self, 3, val) end +end - -- Arithmetic functions - __unm = function(tbl) - return vector.negative(tbl) - end, - __add = function(tbl, o) - return vector.add(tbl, o) - end, - __sub = function(tbl, o) - return vector.subtract(tbl, o) - end, - __mul = function(tbl, o) - return vector.multiply(tbl, o) - end, - __div = function(tbl, o) - return vector.divide(tbl, o) - end, - __pow = function(tbl, power) - return vector.pow(tbl, o) - end, +function vector:__tostring() + return table.concat({ + "{ ", + tostring(rawget(self, 1)), ", ", + tostring(rawget(self, 2)), ", ", + tostring(rawget(self, 3)), " }" + }) +end - -- Higher level methods - abs = vector.abs, - round = vector.round, - floor = vector.floor, - ceil = vector.ceil, - dist = vector.distance, - distance, vector.distance, - dist2 = vector.distance_squared, - distance_squared = vector.distance_squared -} +function vector.__concat(a, b) + return tostring(a) .. tostring(b) +end -- vector.is_vector -- Checks if a value is a vector. function vector.is_vector(v) - if type(v) ~= "table" or not v.__is_vector then return false end - return true + return getmetatable(v) == vector end -- vector.new @@ -193,7 +187,7 @@ end -- Table constructor works with 1-3 values vector.new = function(x, y, z) -- Blank new constructor, return empty vector - if x == nil then return create_vector(0, 0, 0) + if x == nil or (type(x) == "table" and x[1] == nil) then return create_vector(0, 0, 0) -- Invalid type passed to function, return nil elseif type(x) ~= "number" and type(x) ~= "table" then return nil -- Passed a table as x with at least x and y parameters, z will be set to table value or 0 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1c76747e..185cd565 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -319,6 +319,6 @@ set(ZEPHA_SRC def/gen/MapGenProps.cpp def/gen/MapGenProps.h lua/api/class/LuaGuiElement.cpp - lua/api/class/LuaGuiElement.h world/Dimension.cpp world/Dimension.h world/fs/FileManipulator.cpp world/fs/FileManipulator.h def/item/BlockModel.cpp net/server/world/ServerPacketStream.cpp net/server/world/ServerPacketStream.h lua/register/CreateBlockModel.cpp lua/register/CreateBlockModel.h) + lua/api/class/LuaGuiElement.h world/Dimension.cpp world/Dimension.h world/fs/FileManipulator.cpp world/fs/FileManipulator.h def/item/BlockModel.cpp net/server/world/ServerPacketStream.cpp net/server/world/ServerPacketStream.h lua/register/CreateBlockModel.cpp lua/register/CreateBlockModel.h lua/customization/vec3.hpp) add_library (Zepha_Core ${ZEPHA_SRC}) \ No newline at end of file diff --git a/src/Main.cpp b/src/Main.cpp index 04b1914c..980ce520 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -20,6 +20,18 @@ #include "StartGame.h" int main(int argc, char* argv[]) { + +// sol::state lua; +// lua.open_libraries(sol::lib::base); +// +// lua.script(R"( +// number = 32 +// )"); +// +// unsigned short n = lua.get("number"); +// std::cout << n << std::endl; + + return StartGame(argc, argv); } diff --git a/src/def/item/BlockDef.h b/src/def/item/BlockDef.h index 6a7c0447..1a7bdc9e 100644 --- a/src/def/item/BlockDef.h +++ b/src/def/item/BlockDef.h @@ -5,12 +5,12 @@ #pragma once #include -#include #include "../ItemDef.h" #include "BlockModel.h" #include "SelectionBox.h" +#include "../../lua/Lua.h" #include "../../util/Util.h" #include "../../lua/Callback.h" diff --git a/src/game/entity/Model.h b/src/game/entity/Model.h index 92632aa6..8ea7f1e6 100644 --- a/src/game/entity/Model.h +++ b/src/game/entity/Model.h @@ -6,6 +6,7 @@ #include #include +#include #include #include #include diff --git a/src/game/hud/GameGuiBuilder.cpp b/src/game/hud/GameGuiBuilder.cpp index fb7f1ad5..7bb52c71 100644 --- a/src/game/hud/GameGuiBuilder.cpp +++ b/src/game/hud/GameGuiBuilder.cpp @@ -23,14 +23,14 @@ std::shared_ptr GameGuiBuilder::createComponent(LuaGuiElement& ele elem.updateFunction = std::bind(&GameGuiBuilder::elementUpdated, this); - if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) { - elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); }); - - if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) { - elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); }); - - if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) { - elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); }); +// if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) { +// elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); }); +// +// if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) { +// elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); }); +// +// if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) { +// elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); }); return c; } diff --git a/src/game/hud/GuiBuilder.cpp b/src/game/hud/GuiBuilder.cpp index abfaa44b..bf790482 100644 --- a/src/game/hud/GuiBuilder.cpp +++ b/src/game/hud/GuiBuilder.cpp @@ -75,14 +75,14 @@ std::shared_ptr GuiBuilder::createComponent(LuaGuiElement& elem, g elem.updateFunction = std::bind(&GuiBuilder::elementUpdated, this); - if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) { - elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); }); - - if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) { - elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); }); - - if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) { - elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); }); +// if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) { +// elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); }); +// +// if (elem.callbacks.count("secondary")) c->setCallback(GuiComponent::CallbackType::SECONDARY, [=](bool b, glm::vec2 v) { +// elem.callbacks.at("secondary")(b, LuaParser::luaVec(elem.callbacks.at("secondary").lua_state(), {v.x, v.y, 0})); }); +// +// if (elem.callbacks.count("hover")) c->setCallback(GuiComponent::CallbackType::HOVER, [=](bool b, glm::vec2 v) { +// elem.callbacks.at("hover")(b, LuaParser::luaVec(elem.callbacks.at("hover").lua_state(), {v.x, v.y, 0})); }); return c; } diff --git a/src/game/hud/SerialGui.h b/src/game/hud/SerialGui.h index 3fd6f10f..debe8bca 100644 --- a/src/game/hud/SerialGui.h +++ b/src/game/hud/SerialGui.h @@ -15,7 +15,6 @@ #include #include -#include #include "../../util/Any.h" #include "../../lua/api/class/LuaGuiElement.h" diff --git a/src/game/hud/components/GuiComponent.h b/src/game/hud/components/GuiComponent.h index 3a1ae36d..890dd325 100644 --- a/src/game/hud/components/GuiComponent.h +++ b/src/game/hud/components/GuiComponent.h @@ -7,6 +7,7 @@ #include #include +#include #include "../../entity/Entity.h" @@ -42,6 +43,7 @@ public: return std::static_pointer_cast(it); } } + return nullptr; }; std::shared_ptr insert(unsigned int index, std::shared_ptr component); diff --git a/src/game/inventory/InventoryList.cpp b/src/game/inventory/InventoryList.cpp index adb36e11..65cc25a5 100644 --- a/src/game/inventory/InventoryList.cpp +++ b/src/game/inventory/InventoryList.cpp @@ -266,10 +266,10 @@ void InventoryList::setStack(unsigned short i, const ItemStack &stack) { } } -void InventoryList::setLuaCallback(InventoryList::Callback type, sol::function cb) { +void InventoryList::setLuaCallback(InventoryList::Callback type, sol::safe_function cb) { luaCallbacks[static_cast(type)] = cb; } -sol::function InventoryList::getLuaCallback(InventoryList::Callback type) { +sol::safe_function InventoryList::getLuaCallback(InventoryList::Callback type) { return luaCallbacks[static_cast(type)]; } \ No newline at end of file diff --git a/src/game/inventory/InventoryList.h b/src/game/inventory/InventoryList.h index b8675fa6..7aff81c0 100644 --- a/src/game/inventory/InventoryList.h +++ b/src/game/inventory/InventoryList.h @@ -5,8 +5,8 @@ #pragma once #include -#include +#include "../../lua/Lua.h" #include "ItemStack.h" class DefinitionAtlas; @@ -53,7 +53,7 @@ public: void setStack(unsigned short i, const ItemStack& stack); sol::function getLuaCallback(Callback type); - void setLuaCallback(Callback type, sol::function cb); + void setLuaCallback(Callback type, sol::safe_function cb); DefinitionAtlas& defs; diff --git a/src/game/inventory/ServerInventoryList.cpp b/src/game/inventory/ServerInventoryList.cpp index b0b51091..a4c3df4c 100644 --- a/src/game/inventory/ServerInventoryList.cpp +++ b/src/game/inventory/ServerInventoryList.cpp @@ -28,6 +28,7 @@ bool ServerInventoryList::addWatcher(unsigned int cid) { watchers.push_back(cid); sendTo(client); + return true; } bool ServerInventoryList::removeWatcher(unsigned int cid) { diff --git a/src/game/scene/world/LocalWorld.cpp b/src/game/scene/world/LocalWorld.cpp index 450b582e..9aef3fb5 100644 --- a/src/game/scene/world/LocalWorld.cpp +++ b/src/game/scene/world/LocalWorld.cpp @@ -62,12 +62,12 @@ void LocalWorld::blockPlace(glm::vec3 pos, unsigned int block) { if (block == LocalDefinitionAtlas::AIR) { auto def = defs.defs.blockFromId(getBlock(pos)); if (def.callbacks.count(Callback::BREAK_CLIENT)) - defs.parser.safe_function(def.callbacks[Callback::BREAK_CLIENT], defs.parser.luaVec(pos)); + defs.parser.safe_function(def.callbacks[Callback::BREAK_CLIENT], pos); } else { auto def = defs.defs.blockFromId(block); if (def.callbacks.count(Callback::PLACE_CLIENT)) - defs.parser.safe_function(def.callbacks[Callback::PLACE_CLIENT], defs.parser.luaVec(pos)); + defs.parser.safe_function(def.callbacks[Callback::PLACE_CLIENT], pos); } net->blockPlace(pos, block); @@ -82,7 +82,7 @@ void LocalWorld::blockInteract(PointedThing &thing) { auto def = defs.defs.blockFromId(getBlock(thing.target.block.pos)); if (def.callbacks.count(Callback::INTERACT_CLIENT)) - defs.parser.safe_function(def.callbacks[Callback::INTERACT_CLIENT], defs.parser.luaVec(thing.target.block.pos)); + defs.parser.safe_function(def.callbacks[Callback::INTERACT_CLIENT], thing.target.block.pos); net->blockInteract(thing.target.block.pos); } diff --git a/src/lua/Lua.h b/src/lua/Lua.h new file mode 100644 index 00000000..a444e852 --- /dev/null +++ b/src/lua/Lua.h @@ -0,0 +1,6 @@ +#pragma once + +#define SOL_ALL_SAFETIES_ON 1 + +#include +#include "customization/vec3.hpp" \ No newline at end of file diff --git a/src/lua/LuaKeybindHandler.h b/src/lua/LuaKeybindHandler.h index d6628aba..c405f61c 100644 --- a/src/lua/LuaKeybindHandler.h +++ b/src/lua/LuaKeybindHandler.h @@ -6,7 +6,7 @@ #pragma once -#include +#include "Lua.h" class LocalLuaParser; diff --git a/src/lua/LuaParser.cpp b/src/lua/LuaParser.cpp index 10c8e312..1a55071e 100644 --- a/src/lua/LuaParser.cpp +++ b/src/lua/LuaParser.cpp @@ -13,21 +13,10 @@ void LuaParser::update(double delta) { DelayedFunction& f = *it; f.timeout -= delta; if (f.timeout <= 0) { - if (f.function(sol::as_args(f.args))) { - f.timeout = f.initial_timeout; - } else { - it = delayed_functions.erase(it); - continue; - } + sol::optional res = f.function(sol::as_args(f.args)); + if (res && *res) f.timeout = f.initial_timeout; + else { it = delayed_functions.erase(it); continue; } } it++; } -} - -sol::table LuaParser::luaVec(glm::vec3 vec) { - return lua["vector"]["new"](vec.x, vec.y, vec.z); -} - -sol::table LuaParser::luaVec(sol::state_view s, glm::vec3 vec) { - return s["vector"]["new"](vec.x, vec.y, vec.z); -} +} \ No newline at end of file diff --git a/src/lua/LuaParser.h b/src/lua/LuaParser.h index 37679add..79b88489 100644 --- a/src/lua/LuaParser.h +++ b/src/lua/LuaParser.h @@ -8,16 +8,14 @@ #include #include -#include + +#include "Lua.h" class LuaParser { public: constexpr static double UPDATE_STEP {1 / 60.0}; virtual void update(double delta); - sol::table luaVec(glm::vec3 vec); - static sol::table luaVec(sol::state_view s, glm::vec3 vec); - sol::state lua; sol::table core; diff --git a/src/lua/api/class/LocalLuaAnimationManager.cpp b/src/lua/api/class/LocalLuaAnimationManager.cpp index 904cdced..82df0dc4 100644 --- a/src/lua/api/class/LocalLuaAnimationManager.cpp +++ b/src/lua/api/class/LocalLuaAnimationManager.cpp @@ -4,13 +4,12 @@ #include "LocalLuaAnimationManager.h" +#include "../../Lua.h" #include "../../../game/entity/Entity.h" #include "../../../game/entity/AnimationSegment.h" LocalLuaAnimationManager::LocalLuaAnimationManager(Entity &entity) : - entity(entity) { - -} + entity(entity) {} void LocalLuaAnimationManager::define(sol::table anims) { std::vector animations; diff --git a/src/lua/api/class/LocalLuaAnimationManager.h b/src/lua/api/class/LocalLuaAnimationManager.h index 3da50f4c..fbf31a12 100644 --- a/src/lua/api/class/LocalLuaAnimationManager.h +++ b/src/lua/api/class/LocalLuaAnimationManager.h @@ -4,7 +4,7 @@ #pragma once -#include +#include "../../Lua.h" class Entity; diff --git a/src/lua/api/class/LocalLuaEntity.cpp b/src/lua/api/class/LocalLuaEntity.cpp index e43a9f3f..312736f4 100644 --- a/src/lua/api/class/LocalLuaEntity.cpp +++ b/src/lua/api/class/LocalLuaEntity.cpp @@ -4,35 +4,34 @@ #include "LocalLuaEntity.h" +#include "../../Lua.h" #include "../../LuaParser.h" #include "../../../def/ClientGame.h" #include "../../../def/item/BlockDef.h" #include "../../../def/item/CraftItemDef.h" -void LocalLuaEntity::snap_pos(const sol::table &pos) { - entity->setPos({pos[1], pos[2], pos[3]}); +void LocalLuaEntity::snap_pos(glm::vec3 pos) { + entity->setPos(pos); } -void LocalLuaEntity::set_pos(const sol::table &pos) { - entity->interpPos({pos[1], pos[2], pos[3]}); +void LocalLuaEntity::set_pos(glm::vec3 pos) { + entity->interpPos(pos); } -sol::table LocalLuaEntity::get_pos(sol::this_state s) { - glm::vec3 pos = entity->getPos(); - return LuaParser::luaVec(sol::state_view(s), pos); +glm::vec3 LocalLuaEntity::get_pos() { + return entity->getPos(); } -void LocalLuaEntity::snap_visual_offset(const sol::table &vs) { - entity->setVisualOffset({vs["x"], vs["y"], vs["z"]}); +void LocalLuaEntity::snap_visual_offset(glm::vec3 vs) { + entity->setVisualOffset(vs); } -void LocalLuaEntity::set_visual_offset(const sol::table &pos) { - entity->interpVisualOffset({pos[1], pos[2], pos[3]}); +void LocalLuaEntity::set_visual_offset(glm::vec3 vs) { + entity->interpVisualOffset(vs); } -sol::table LocalLuaEntity::get_visual_offset(sol::this_state s) { - glm::vec3 v = entity->getVisualOffset(); - return LuaParser::luaVec(sol::state_view(s), v); +glm::vec3 LocalLuaEntity::get_visual_offset() { + return entity->getVisualOffset(); } void LocalLuaEntity::snap_pitch(float rot) { diff --git a/src/lua/api/class/LocalLuaEntity.h b/src/lua/api/class/LocalLuaEntity.h index a46afdc0..4933e50d 100644 --- a/src/lua/api/class/LocalLuaEntity.h +++ b/src/lua/api/class/LocalLuaEntity.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "LocalLuaAnimationManager.h" #include "../../../game/entity/Entity.h" @@ -22,13 +21,13 @@ public: LocalLuaAnimationManager manager; - void snap_pos(const sol::table& pos); - void set_pos(const sol::table& pos); - sol::table get_pos(sol::this_state s); + void snap_pos(glm::vec3 pos); + void set_pos(glm::vec3 pos); + glm::vec3 get_pos(); - void snap_visual_offset(const sol::table& vs); - void set_visual_offset(const sol::table& pos); - sol::table get_visual_offset(sol::this_state s); + void snap_visual_offset(glm::vec3 vs); + void set_visual_offset(glm::vec3 vs); + glm::vec3 get_visual_offset(); void snap_pitch(float rot); void set_pitch(float rot); diff --git a/src/lua/api/class/LocalLuaInventory.h b/src/lua/api/class/LocalLuaInventory.h index b6b1ac32..e04691b4 100644 --- a/src/lua/api/class/LocalLuaInventory.h +++ b/src/lua/api/class/LocalLuaInventory.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../../../game/inventory/LocalInventory.h" class LocalLuaInventory { diff --git a/src/lua/api/class/LocalLuaPlayer.cpp b/src/lua/api/class/LocalLuaPlayer.cpp index 0855a794..3a6bde2c 100644 --- a/src/lua/api/class/LocalLuaPlayer.cpp +++ b/src/lua/api/class/LocalLuaPlayer.cpp @@ -10,27 +10,24 @@ #include "LocalLuaInventoryList.h" #include "../../../game/scene/world/Player.h" -sol::table LocalLuaPlayer::get_pos(sol::this_state s) { - glm::vec3 pos = player.getPos(); - return LuaParser::luaVec(sol::state_view(s), pos); +glm::vec3 LocalLuaPlayer::get_pos() { + return player.getPos(); } -sol::table LocalLuaPlayer::get_block_pos(sol::this_state s) { - glm::vec3 pos = glm::floor(player.getPos()); - return LuaParser::luaVec(sol::state_view(s), pos); +glm::vec3 LocalLuaPlayer::get_block_pos() { + return glm::floor(player.getPos()); } -void LocalLuaPlayer::set_pos(const sol::table &pos) { - player.setPos({pos[1], pos[2], pos[3]}, true); +void LocalLuaPlayer::set_pos(glm::vec3 pos) { + player.setPos(pos, true); } -sol::table LocalLuaPlayer::get_vel(sol::this_state s) { - glm::vec3 vel = player.getVel(); - return LuaParser::luaVec(sol::state_view(s), vel); +glm::vec3 LocalLuaPlayer::get_vel() { + return player.getVel(); } -void LocalLuaPlayer::set_vel(const sol::table &vel) { - player.setVel({vel[1], vel[2], vel[3]}, true); +void LocalLuaPlayer::set_vel(glm::vec3 vel) { + player.setVel(vel, true); } float LocalLuaPlayer::get_look_yaw() { diff --git a/src/lua/api/class/LocalLuaPlayer.h b/src/lua/api/class/LocalLuaPlayer.h index 817b7d54..1967a876 100644 --- a/src/lua/api/class/LocalLuaPlayer.h +++ b/src/lua/api/class/LocalLuaPlayer.h @@ -4,7 +4,7 @@ #pragma once -#include +//#include #include "LocalLuaInventory.h" @@ -17,12 +17,12 @@ public: Player& player; - sol::table get_pos(sol::this_state s); - sol::table get_block_pos(sol::this_state s); - void set_pos(const sol::table& pos); + glm::vec3 get_pos(); + glm::vec3 get_block_pos(); + void set_pos(glm::vec3 pos); - sol::table get_vel(sol::this_state s); - void set_vel(const sol::table& vel); + glm::vec3 get_vel(); + void set_vel(glm::vec3 vel); float get_look_yaw(); void set_look_yaw(float rot); diff --git a/src/lua/api/class/LuaGuiElement.cpp b/src/lua/api/class/LuaGuiElement.cpp index 1a1f28e1..a0cf9fd9 100644 --- a/src/lua/api/class/LuaGuiElement.cpp +++ b/src/lua/api/class/LuaGuiElement.cpp @@ -171,5 +171,6 @@ Any LuaGuiElement::getAsAny(const std::string &key) const { return Any::from(values); } } - else throw std::runtime_error("Invalid type requested in getAsAny"); + + throw std::runtime_error("Invalid type requested in getAsAny"); } diff --git a/src/lua/api/class/LuaGuiElement.h b/src/lua/api/class/LuaGuiElement.h index 8cf5314c..e5b86d5d 100644 --- a/src/lua/api/class/LuaGuiElement.h +++ b/src/lua/api/class/LuaGuiElement.h @@ -5,8 +5,8 @@ #pragma once #include -#include +#include "../../Lua.h" #include "../../../util/Any.h" class LuaGuiElement { diff --git a/src/lua/api/class/LuaItemStack.cpp b/src/lua/api/class/LuaItemStack.cpp index 392974c3..fe5af670 100644 --- a/src/lua/api/class/LuaItemStack.cpp +++ b/src/lua/api/class/LuaItemStack.cpp @@ -4,6 +4,7 @@ #include "LuaItemStack.h" +#include "../../Lua.h" #include "../../../def/ItemDef.h" #include "../../../def/DefinitionAtlas.h" #include "../../../game/inventory/ItemStack.h" diff --git a/src/lua/api/class/LuaItemStack.h b/src/lua/api/class/LuaItemStack.h index 795fdff8..1c10bdbe 100644 --- a/src/lua/api/class/LuaItemStack.h +++ b/src/lua/api/class/LuaItemStack.h @@ -4,7 +4,8 @@ #pragma once -#include +#include +#include class DefinitionAtlas; class ItemStack; diff --git a/src/lua/api/class/ServerLuaEntity.cpp b/src/lua/api/class/ServerLuaEntity.cpp index 2316f1cd..9e51143d 100644 --- a/src/lua/api/class/ServerLuaEntity.cpp +++ b/src/lua/api/class/ServerLuaEntity.cpp @@ -8,30 +8,28 @@ #include "../../../def/ItemDef.h" #include "../../../def/ServerGame.h" -void ServerLuaEntity::snap_pos(const sol::table &pos) { - entity->setPos({pos[1], pos[2], pos[3]}); +void ServerLuaEntity::snap_pos(glm::vec3 pos) { + entity->setPos(pos); } -void ServerLuaEntity::set_pos(const sol::table &pos) { - entity->setPos({pos[1], pos[2], pos[3]}); +void ServerLuaEntity::set_pos(glm::vec3 pos) { + entity->setPos(pos); } -sol::table ServerLuaEntity::get_pos(sol::this_state s) { - glm::vec3 pos = entity->getPos(); - return LuaParser::luaVec(sol::state_view(s), pos); +glm::vec3 ServerLuaEntity::get_pos() { + return entity->getPos(); } -void ServerLuaEntity::snap_visual_offset(const sol::table &vs) { - entity->setVisualOffset({vs[1], vs[2], vs[3]}); +void ServerLuaEntity::snap_visual_offset(glm::vec3 vs) { + entity->setVisualOffset(vs); } -void ServerLuaEntity::set_visual_offset(const sol::table &pos) { - entity->setVisualOffset({pos[1], pos[2], pos[3]}); +void ServerLuaEntity::set_visual_offset(glm::vec3 vs) { + entity->setVisualOffset(vs); } -sol::table ServerLuaEntity::get_visual_offset(sol::this_state s) { - glm::vec3 v = entity->getVisualOffset(); - return LuaParser::luaVec(sol::state_view(s), v); +glm::vec3 ServerLuaEntity::get_visual_offset() { + return entity->getVisualOffset(); } void ServerLuaEntity::snap_pitch(float rot) { diff --git a/src/lua/api/class/ServerLuaEntity.h b/src/lua/api/class/ServerLuaEntity.h index cc014763..c4914196 100644 --- a/src/lua/api/class/ServerLuaEntity.h +++ b/src/lua/api/class/ServerLuaEntity.h @@ -5,8 +5,8 @@ #pragma once #include -#include +#include "../../Lua.h" #include "../../../net/server/world/ServerEntity.h" class ServerGame; @@ -20,13 +20,13 @@ public: unsigned int id; ServerGame& defs; - void snap_pos(const sol::table& pos); - void set_pos(const sol::table& pos); - sol::table get_pos(sol::this_state s); + void snap_pos(glm::vec3 pos); + void set_pos(glm::vec3 pos); + glm::vec3 get_pos(); - void snap_visual_offset(const sol::table& vs); - void set_visual_offset(const sol::table& pos); - sol::table get_visual_offset(sol::this_state s); + void snap_visual_offset(glm::vec3 vs); + void set_visual_offset(glm::vec3 vs); + glm::vec3 get_visual_offset(); void snap_pitch(float rot); void set_pitch(float rot); diff --git a/src/lua/api/class/ServerLuaInventory.cpp b/src/lua/api/class/ServerLuaInventory.cpp index 6dab010d..05f58b09 100644 --- a/src/lua/api/class/ServerLuaInventory.cpp +++ b/src/lua/api/class/ServerLuaInventory.cpp @@ -28,5 +28,5 @@ void ServerLuaInventory::set_default_list(sol::object list) { sol::object ServerLuaInventory::get_default_list(sol::this_state s) { if (inventory.getDefaultList() == "") return sol::nil; - else get_list(s, inventory.getDefaultList()); + else return get_list(s, inventory.getDefaultList()); } diff --git a/src/lua/api/class/ServerLuaInventoryList.cpp b/src/lua/api/class/ServerLuaInventoryList.cpp index de50a270..ca950021 100644 --- a/src/lua/api/class/ServerLuaInventoryList.cpp +++ b/src/lua/api/class/ServerLuaInventoryList.cpp @@ -44,7 +44,7 @@ void ServerLuaInventoryList::set_stack(unsigned short i, LuaItemStack stack) { void ServerLuaInventoryList::set_stack(unsigned short i, sol::table stack) { if (i < 1 || i > list.getLength()) throw "index is outside of list bounds."; - list.setStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get(2))); + list.setStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack[2])); } LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, LuaItemStack stack) { @@ -54,7 +54,7 @@ LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, LuaItemStack LuaItemStack ServerLuaInventoryList::place_stack(unsigned short i, sol::table stack) { if (i < 1 || i > list.getLength()) throw "index is outside of list bounds."; - return LuaItemStack(list.placeStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack.get(2))), list.defs); + return LuaItemStack(list.placeStack(i - 1, ItemStack(list.defs.fromStr(stack[1]).index, stack[2])), list.defs); } LuaItemStack ServerLuaInventoryList::split_stack(unsigned short i) { @@ -67,7 +67,7 @@ LuaItemStack ServerLuaInventoryList::add_stack(LuaItemStack stack) { } LuaItemStack ServerLuaInventoryList::add_stack(sol::table stack) { - return LuaItemStack(list.addStack(ItemStack(list.defs.fromStr(stack[1]).index, stack.get(2))), list.defs); + return LuaItemStack(list.addStack(ItemStack(list.defs.fromStr(stack[1]).index, stack[2])), list.defs); } int ServerLuaInventoryList::stack_fits(LuaItemStack stack) { @@ -75,7 +75,7 @@ int ServerLuaInventoryList::stack_fits(LuaItemStack stack) { } int ServerLuaInventoryList::stack_fits(sol::table stack) { - return list.stackFits(ItemStack(list.defs.fromStr(stack[1]).index, stack.get(2))); + return list.stackFits(ItemStack(list.defs.fromStr(stack[1]).index, stack[2])); } LuaItemStack ServerLuaInventoryList::take_stack(LuaItemStack request) { @@ -83,7 +83,7 @@ LuaItemStack ServerLuaInventoryList::take_stack(LuaItemStack request) { } LuaItemStack ServerLuaInventoryList::take_stack(sol::table request) { - return LuaItemStack(list.takeStack(ItemStack(list.defs.fromStr(request[1]).index, request.get(2))), list.defs); + return LuaItemStack(list.takeStack(ItemStack(list.defs.fromStr(request[1]).index, request[2])), list.defs); } LuaItemStack ServerLuaInventoryList::remove_stack(unsigned short i, unsigned short count) { @@ -91,10 +91,10 @@ LuaItemStack ServerLuaInventoryList::remove_stack(unsigned short i, unsigned sho return LuaItemStack(list.removeStack(i - 1, count), list.defs); } -void ServerLuaInventoryList::set_callback(ServerInventoryList::Callback t, sol::function fun) { +void ServerLuaInventoryList::set_callback(ServerInventoryList::Callback t, sol::safe_function fun) { list.setLuaCallback(t, fun); } -sol::function ServerLuaInventoryList::get_callback(ServerInventoryList::Callback t) { +sol::safe_function ServerLuaInventoryList::get_callback(ServerInventoryList::Callback t) { return list.getLuaCallback(t); } \ No newline at end of file diff --git a/src/lua/api/class/ServerLuaInventoryList.h b/src/lua/api/class/ServerLuaInventoryList.h index 17cafe8d..82fa202d 100644 --- a/src/lua/api/class/ServerLuaInventoryList.h +++ b/src/lua/api/class/ServerLuaInventoryList.h @@ -45,6 +45,6 @@ public: LuaItemStack remove_stack(unsigned short ind, unsigned short count); - void set_callback(ServerInventoryList::Callback t, sol::function fun); - sol::function get_callback(ServerInventoryList::Callback t); + void set_callback(ServerInventoryList::Callback t, sol::safe_function fun); + sol::safe_function get_callback(ServerInventoryList::Callback t); }; diff --git a/src/lua/api/class/ServerLuaPlayer.cpp b/src/lua/api/class/ServerLuaPlayer.cpp index 4e0819f2..ca9161ab 100644 --- a/src/lua/api/class/ServerLuaPlayer.cpp +++ b/src/lua/api/class/ServerLuaPlayer.cpp @@ -20,27 +20,24 @@ std::string ServerLuaPlayer::get_address() { return NetHandler::intToIPString(player.address.host) + ":" + std::to_string(player.address.port); } -sol::table ServerLuaPlayer::get_pos(sol::this_state s) { - glm::vec3 pos = player.getPos(); - return LuaParser::luaVec(sol::state_view(s), pos); +glm::vec3 ServerLuaPlayer::get_pos() { + return player.getPos(); } -sol::table ServerLuaPlayer::get_block_pos(sol::this_state s) { - glm::vec3 pos = glm::floor(player.getPos()); - return LuaParser::luaVec(sol::state_view(s), pos); +glm::vec3 ServerLuaPlayer::get_block_pos() { + return glm::floor(player.getPos()); } -void ServerLuaPlayer::set_pos(const sol::table &pos) { - player.setPos({pos[1], pos[2], pos[3]}, true); +void ServerLuaPlayer::set_pos(glm::vec3 pos) { + player.setPos(pos, true); } -sol::table ServerLuaPlayer::get_vel(sol::this_state s) { - glm::vec3 vel = player.getVel(); - return LuaParser::luaVec(sol::state_view(s), vel); +glm::vec3 ServerLuaPlayer::get_vel() { + return player.getVel(); } -void ServerLuaPlayer::set_vel(const sol::table &vel) { - player.setVel({vel[1], vel[2], vel[3]}, true); +void ServerLuaPlayer::set_vel(glm::vec3 vel) { + player.setVel(vel, true); } float ServerLuaPlayer::get_look_yaw() { diff --git a/src/lua/api/class/ServerLuaPlayer.h b/src/lua/api/class/ServerLuaPlayer.h index 71b4434c..5e6c478d 100644 --- a/src/lua/api/class/ServerLuaPlayer.h +++ b/src/lua/api/class/ServerLuaPlayer.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "ServerLuaInventory.h" #include "../../../net/server/conn/ServerClient.h" @@ -20,12 +19,12 @@ public: unsigned int get_cid(); std::string get_address(); - sol::table get_pos(sol::this_state s); - sol::table get_block_pos(sol::this_state s); - void set_pos(const sol::table& pos); + glm::vec3 get_pos(); + glm::vec3 get_block_pos(); + void set_pos(glm::vec3 pos); - sol::table get_vel(sol::this_state s); - void set_vel(const sol::table& vel); + glm::vec3 get_vel(); + void set_vel(glm::vec3 vel); float get_look_yaw(); void set_look_yaw(float rot); diff --git a/src/lua/api/functions/trigger_event.h b/src/lua/api/functions/trigger_event.h index 86727299..54675efb 100644 --- a/src/lua/api/functions/trigger_event.h +++ b/src/lua/api/functions/trigger_event.h @@ -4,7 +4,7 @@ #pragma once -#include +#include "../../Lua.h" namespace Api { static void trigger_event(sol::state& lua) { @@ -19,14 +19,5 @@ namespace Api { end end )"); - - - lua.script(R"( - zepha.__builtin.add_player = function(player) - table.insert(zepha.players, player) - zepha.__builtin.trigger_event("new_player", player) - zepha.__builtin.trigger_event("player_join", player) - end - )"); } } \ No newline at end of file diff --git a/src/lua/api/functions/update_entities.h b/src/lua/api/functions/update_entities.h index ab7b244a..99bcd654 100644 --- a/src/lua/api/functions/update_entities.h +++ b/src/lua/api/functions/update_entities.h @@ -4,7 +4,7 @@ #pragma once -#include +#include "../../Lua.h" namespace Api { static void update_entities(sol::state& lua) { diff --git a/src/lua/api/menu/mDelay.h b/src/lua/api/menu/mDelay.h index 1938a9a3..76a0cce3 100644 --- a/src/lua/api/menu/mDelay.h +++ b/src/lua/api/menu/mDelay.h @@ -5,8 +5,8 @@ #pragma once #include -#include +#include "../../Lua.h" #include "../../LuaParser.h" namespace MenuApi { diff --git a/src/lua/api/menu/mSetGui.h b/src/lua/api/menu/mSetGui.h index 2fea8e6b..a11cbc87 100644 --- a/src/lua/api/menu/mSetGui.h +++ b/src/lua/api/menu/mSetGui.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../../../game/hud/GuiBuilder.h" class LuaGuiElement; diff --git a/src/lua/api/menu/mStartGame.h b/src/lua/api/menu/mStartGame.h index c985a0c4..21f7d4fd 100644 --- a/src/lua/api/menu/mStartGame.h +++ b/src/lua/api/menu/mStartGame.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../../../game/ClientState.h" namespace MenuApi { diff --git a/src/lua/api/modules/add_entity.h b/src/lua/api/modules/add_entity.h index 231331cb..109f3f2b 100644 --- a/src/lua/api/modules/add_entity.h +++ b/src/lua/api/modules/add_entity.h @@ -17,15 +17,12 @@ namespace Api { static void add_entity_c(sol::state& lua, sol::table& core, ClientGame& defs, LocalWorld& world) { core["entities"] = lua.create_table(); - core.set_function("add_entity", [&](sol::optional pos, sol::optional identifier, sol::object staticData) { - if (!identifier || !identifier->length()) throw std::runtime_error("Expected an identifier as the second argument."); - if (!pos) throw std::runtime_error("Expected a position as the first argument."); - - if (core["registered_entities"][*identifier] == sol::nil) throw std::runtime_error("identifier '" + *identifier + "' is not a valid entity identifier."); - sol::table entityDef = core["registered_entities"][*identifier]; + core.set_function("add_entity", [&](glm::vec3 pos, std::string identifier, sol::object staticData) { + if (core["registered_entities"][identifier] == sol::nil) throw std::runtime_error("identifier '" + identifier + "' is not a valid entity identifier."); + sol::table entityDef = core["registered_entities"][identifier]; auto entity = std::make_unique(); - entity->setPos({pos->get("x"), pos->get("y"), pos->get("z")}); + entity->setPos(pos); auto entityRef = std::make_shared(std::move(entity), entities_ind++, defs); sol::table luaEntity = lua.create_table(); @@ -37,9 +34,9 @@ namespace Api { auto displayObject = luaEntity.get>("display_object"); auto displayTexture = luaEntity.get>("display_texture"); - if (!displayType) throw std::runtime_error("entity '" + *identifier + "' is missing the display property."); - if (!displayObject) throw std::runtime_error("entity '" + *identifier + "' is missing the display_object property."); - if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + *identifier + "' is missing the display_texture property."); + if (!displayType) throw std::runtime_error("entity '" + identifier + "' is missing the display property."); + if (!displayObject) throw std::runtime_error("entity '" + identifier + "' is missing the display_object property."); + if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + identifier + "' is missing the display_texture property."); entityRef->set_display_type(*displayType, *displayObject, displayTexture); @@ -55,15 +52,12 @@ namespace Api { static void add_entity_s(sol::state& lua, sol::table& core, ServerGame& defs, ServerWorld& world) { core["entities"] = lua.create_table(); - core.set_function("add_entity", [&](sol::optional pos, sol::optional identifier, sol::object staticData) { - if (!identifier || !identifier->length()) throw std::runtime_error("Expected an identifier as the second argument."); - if (!pos) throw std::runtime_error("Expected a position as the first argument."); - - if (core["registered_entities"][*identifier] == sol::nil) throw std::runtime_error("identifier '" + *identifier + "' is not a valid entity identifier."); - sol::table entityDef = core["registered_entities"][*identifier]; + core.set_function("add_entity", [&](glm::vec3 pos, std::string identifier, sol::object staticData) { + if (core["registered_entities"][identifier] == sol::nil) throw std::runtime_error("identifier '" + identifier + "' is not a valid entity identifier."); + sol::table entityDef = core["registered_entities"][identifier]; auto entity = std::make_unique(entities_ind); - entity->setPos({pos->get("x"), pos->get("y"), pos->get("z")}); + entity->setPos(pos); auto entityRef = std::make_shared(std::move(entity), entities_ind++, defs); sol::table luaEntity = lua.create_table(); @@ -75,9 +69,9 @@ namespace Api { auto displayObject = luaEntity.get>("display_object"); auto displayTexture = luaEntity.get>("display_texture"); - if (!displayType) throw std::runtime_error("entity '" + *identifier + "' is missing the display property."); - if (!displayObject) throw std::runtime_error("entity '" + *identifier + "' is missing the display_object property."); - if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + *identifier + "' is missing the display_texture property."); + if (!displayType) throw std::runtime_error("entity '" + identifier + "' is missing the display property."); + if (!displayObject) throw std::runtime_error("entity '" + identifier + "' is missing the display_object property."); + if (strncmp(displayType->data(), "model", 5) == 0 && !displayTexture) throw std::runtime_error("entity '" + identifier + "' is missing the display_texture property."); entityRef->set_display_type(*displayType, *displayObject, displayTexture); diff --git a/src/lua/api/modules/create_structure.h b/src/lua/api/modules/create_structure.h index 24369caa..fd3a6c0a 100644 --- a/src/lua/api/modules/create_structure.h +++ b/src/lua/api/modules/create_structure.h @@ -11,7 +11,7 @@ namespace Api { core.set_function("create_structure", [&](sol::optional data) { if (!data) throw "expected a table as the first argument."; - auto origin = data->get>("origin"); + auto origin = data->get>("origin"); auto schematic = data->get>("schematic"); if (!origin) throw std::runtime_error("expected a table as the first argument."); @@ -26,7 +26,7 @@ namespace Api { s->dimensions = {xWid, yWid, zWid}; s->stringData.resize(xWid * yWid * zWid); - s->origin = {origin->get(1), origin->get(2), origin->get(3)}; + s->origin = origin ? glm::ivec3{*origin} : glm::ivec3 {}; for (unsigned int y = 1; y <= yWid; y++) { for (unsigned int z = 1; z <= zWid; z++) { diff --git a/src/lua/api/modules/get_block.h b/src/lua/api/modules/get_block.h index be4fc0f3..0b70f691 100644 --- a/src/lua/api/modules/get_block.h +++ b/src/lua/api/modules/get_block.h @@ -9,11 +9,8 @@ namespace Api { static void get_block(sol::table &core, DefinitionAtlas& defs, World& world) { - core.set_function("get_block", [&](sol::table pos) -> std::string { - if (!pos.get>("x") || !pos.get>("y") || !pos.get>("z")) - throw std::runtime_error("Expected a vector as the first argument."); - - return defs.fromId(world.getBlock({pos.get("x"), pos.get("y"), pos.get("z")})).identifier; + core.set_function("get_block", [&](glm::ivec3 pos) { + return defs.fromId(world.getBlock(pos)).identifier; }); } } diff --git a/src/lua/api/modules/set_block.h b/src/lua/api/modules/set_block.h index 825804a2..6dc5dd46 100644 --- a/src/lua/api/modules/set_block.h +++ b/src/lua/api/modules/set_block.h @@ -9,10 +9,9 @@ namespace Api { static void set_block(sol::table &core, DefinitionAtlas& defs, World& world) { - core.set_function("set_block", [&](sol::table pos, std::string identifier) { - if (!pos["x"] || !pos["y"] || !pos["z"]) throw std::runtime_error("expected a vector as the first argument."); + core.set_function("set_block", [&](glm::vec3 pos, std::string identifier) { auto& block = defs.fromStr(identifier); - world.setBlock({pos.get("x"), pos.get("y"), pos.get("z")}, block.index); + world.setBlock(pos, block.index); }); } } \ No newline at end of file diff --git a/src/lua/api/usertype/cInventoryRef.h b/src/lua/api/usertype/cInventoryRef.h index c3f6dfb8..3fdb5075 100644 --- a/src/lua/api/usertype/cInventoryRef.h +++ b/src/lua/api/usertype/cInventoryRef.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../class/LocalLuaInventory.h" #include "../class/LocalLuaInventoryList.h" diff --git a/src/lua/api/usertype/cItemStack.h b/src/lua/api/usertype/cItemStack.h index dc7163f4..676144f8 100644 --- a/src/lua/api/usertype/cItemStack.h +++ b/src/lua/api/usertype/cItemStack.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../class/LuaItemStack.h" namespace ClientApi { diff --git a/src/lua/api/usertype/cLuaEntity.h b/src/lua/api/usertype/cLuaEntity.h index bdadedc7..a8f502f7 100644 --- a/src/lua/api/usertype/cLuaEntity.h +++ b/src/lua/api/usertype/cLuaEntity.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../class/LocalLuaEntity.h" #include "../../../game/scene/world/LocalWorld.h" diff --git a/src/lua/api/usertype/sInventoryRef.h b/src/lua/api/usertype/sInventoryRef.h index 82594d84..a4342a80 100644 --- a/src/lua/api/usertype/sInventoryRef.h +++ b/src/lua/api/usertype/sInventoryRef.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../class/ServerLuaInventory.h" #include "../class/ServerLuaInventoryList.h" diff --git a/src/lua/api/usertype/sLuaEntity.h b/src/lua/api/usertype/sLuaEntity.h index f8b7651c..7b200cff 100644 --- a/src/lua/api/usertype/sLuaEntity.h +++ b/src/lua/api/usertype/sLuaEntity.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../../Lua.h" #include "../class/ServerLuaEntity.h" namespace ServerApi { diff --git a/src/lua/customization/vec3.hpp b/src/lua/customization/vec3.hpp new file mode 100644 index 00000000..db168750 --- /dev/null +++ b/src/lua/customization/vec3.hpp @@ -0,0 +1,129 @@ +// +// Vec3 to Vector table customization. +// Allows sol to transfer glm::vec3 and glm::vec3 to a Lua vector table and back. +// This customization was based off of code provided by rubenwardy. +// Created by aurailus on 2020-07-22. +// + +#pragma clang diagnostic push +#pragma ide diagnostic ignored "OCUnusedStructInspection" +#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" + +#pragma once + +#include +#include "../../util/Util.h" + +namespace sol { + + // Float + + template <> + struct lua_size : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + inline bool sol_lua_check(types, lua_State* L, int index, Handler&& handler, stack::record& tracking) { + int absoluteIndex = lua_absindex(L, index); + if (!stack::check(L, absoluteIndex, handler)) { + tracking.use(1); + return false; + } + + stack::get_field(L, 1, absoluteIndex); + bool x = stack::check(L, -1); + + stack::get_field(L, 2, absoluteIndex); + bool y = stack::check(L, -1); + + stack::get_field(L, 3, absoluteIndex); + bool z = stack::check(L, -1); + + stack::pop_n(L, 3); + tracking.use(1); + return x && y && z; + } + + inline glm::vec3 sol_lua_get(types, lua_State* L, int index, stack::record& tracking) { + int absoluteIndex = lua_absindex(L, index); + + sol::table table = stack::get(L, absoluteIndex); + float x = table[1]; + float y = table[2]; + float z = table[3]; + + tracking.use(1); + return glm::vec3 { x, y, z }; + } + + + inline int sol_lua_push(sol::types, lua_State* L, const glm::vec3& vec) { + sol::state_view lua(L); + + lua_createtable(L, 3, 0); + sol::stack_table tbl(L, -1); + tbl[1] = vec.x; tbl[2] = vec.y; tbl[3] = vec.z; + tbl[sol::metatable_key] = lua["vector"]; + + return 1; + } + + // Float + + template <> + struct lua_size : std::integral_constant {}; + + template <> + struct lua_type_of : std::integral_constant {}; + + template + inline bool sol_lua_check(types, lua_State* L, int index, Handler&& handler, stack::record& tracking) { + int absoluteIndex = lua_absindex(L, index); + if (!stack::check
(L, absoluteIndex, handler)) { + tracking.use(1); + return false; + } + + stack::get_field(L, 1, absoluteIndex); + bool x = stack::check(L, -1); + + stack::get_field(L, 2, absoluteIndex); + bool y = stack::check(L, -1); + + stack::get_field(L, 3, absoluteIndex); + bool z = stack::check(L, -1); + + stack::pop_n(L, 3); + tracking.use(1); + return x && y && z; + } + + inline glm::ivec3 sol_lua_get(types, lua_State* L, int index, stack::record& tracking) { + int absoluteIndex = lua_absindex(L, index); + + sol::table table = stack::get(L, absoluteIndex); + int x = table[1]; + int y = table[2]; + int z = table[3]; + + tracking.use(1); + return glm::ivec3 { x, y, z }; + } + + + inline int sol_lua_push(sol::types, lua_State* L, const glm::ivec3& vec) { + sol::state_view lua(L); + + lua_createtable(L, 3, 0); + sol::stack_table tbl(L, -1); + tbl[1] = vec.x; tbl[2] = vec.y; tbl[3] = vec.z; + tbl[sol::metatable_key] = lua["vector"]; + + return 1; + } + +} + +#pragma clang diagnostic pop \ No newline at end of file diff --git a/src/lua/parser/LocalLuaParser.cpp b/src/lua/parser/LocalLuaParser.cpp index 7cd7fca3..0d828606 100644 --- a/src/lua/parser/LocalLuaParser.cpp +++ b/src/lua/parser/LocalLuaParser.cpp @@ -123,33 +123,36 @@ sol::protected_function_result LocalLuaParser::errorCallback(sol::protected_func sol::error err = errPfr; std::string errString = err.what(); - std::string::size_type slash = errString.find('/'); - assert(slash != std::string::npos); + try { + std::string::size_type slash = errString.find('/'); + if (slash != std::string::npos) throw "npos"; - std::string modString = errString.substr(0, slash); + std::string modString = errString.substr(0, slash); - std::string::size_type lineNumStart = errString.find(':', slash); - assert(lineNumStart != std::string::npos); - std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1); - assert(lineNumEnd != std::string::npos); + std::string::size_type lineNumStart = errString.find(':', slash); + if (lineNumStart != std::string::npos) throw "lineNumStart"; + std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1); + if (lineNumStart != std::string::npos) throw "lineNumEnd"; - std::string fileName = errString.substr(0, lineNumStart); - int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1)); + std::string fileName = errString.substr(0, lineNumStart); + int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1)); - for (const auto& mod : handler.cGetMods()) { - if (mod.config.name == modString) { - for (auto& file : mod.files) { - if (file.path == fileName) { - std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl; - exit(1); + for (const auto& mod : handler.cGetMods()) { + if (mod.config.name == modString) { + for (auto& file : mod.files) { + if (file.path == fileName) { + std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl; + break; + } } + break; } - break; } } - - std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:" - << std::endl << errString << Log::endl; + catch (...) { + std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:" + << std::endl << std::endl << errString << Log::endl; + } exit(1); return errPfr; diff --git a/src/lua/parser/ServerLuaParser.cpp b/src/lua/parser/ServerLuaParser.cpp index 739503f0..a246d624 100644 --- a/src/lua/parser/ServerLuaParser.cpp +++ b/src/lua/parser/ServerLuaParser.cpp @@ -76,7 +76,12 @@ void ServerLuaParser::sendModsPacket(ENetPeer* peer) const { void ServerLuaParser::playerConnected(std::shared_ptr client) { auto players = core.get("players"); - safe_function(core["__builtin"]["add_player"], ServerLuaPlayer(*client)); + players.add(ServerLuaPlayer(*client)); + + sol::object player = players[players.size()]; + + safe_function(core["__builtin"]["trigger_event"], "new_player", player); + safe_function(core["__builtin"]["trigger_event"], "player_join", player); } void ServerLuaParser::playerDisconnected(std::shared_ptr client) { @@ -148,33 +153,36 @@ sol::protected_function_result ServerLuaParser::errorCallback(sol::protected_fun sol::error err = errPfr; std::string errString = err.what(); - std::string::size_type slash = errString.find_first_of("/"); - assert(slash != std::string::npos); + try { + std::string::size_type slash = errString.find_first_of("/"); + if (slash != std::string::npos) throw "npos"; - std::string modString = errString.substr(0, slash); + std::string modString = errString.substr(0, slash); - std::string::size_type lineNumStart = errString.find(':', slash); - assert(lineNumStart != std::string::npos); - std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1); - assert(lineNumEnd != std::string::npos); + std::string::size_type lineNumStart = errString.find(':', slash); + if (lineNumStart != std::string::npos) throw "lineNumStart"; + std::string::size_type lineNumEnd = errString.find(':', lineNumStart + 1); + if (lineNumStart != std::string::npos) throw "lineNumEnd"; - std::string fileName = errString.substr(0, lineNumStart); - int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1)); + std::string fileName = errString.substr(0, lineNumStart); + int lineNum = std::stoi(errString.substr(lineNumStart + 1, lineNumEnd - lineNumStart - 1)); - for (auto& mod : handler.cGetMods()) { - if (mod.config.name == modString) { - for (auto& file : mod.files) { - if (file.path == fileName) { - std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl; - exit(1); + for (auto& mod : handler.cGetMods()) { + if (mod.config.name == modString) { + for (auto& file : mod.files) { + if (file.path == fileName) { + std::cout << std::endl << ErrorFormatter::formatError(fileName, lineNum, errString, file.file) << std::endl; + break; + } } + break; } - break; } } - - std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:" - << std::endl << std::endl << errString << Log::endl; + catch (...) { + std::cout << Log::err << "Zepha has encountered an error, and ErrorFormatter failed to format it:" + << std::endl << std::endl << errString << Log::endl; + } exit(1); return errPfr; diff --git a/src/lua/register/RegisterBiomes.h b/src/lua/register/RegisterBiomes.h index 5f9c67da..e0eade20 100644 --- a/src/lua/register/RegisterBiomes.h +++ b/src/lua/register/RegisterBiomes.h @@ -4,11 +4,11 @@ #pragma once -#include #include #include #include +#include "../Lua.h" #include "../../util/Util.h" #include "../../def/ClientGame.h" #include "../../def/ServerGame.h" @@ -241,6 +241,7 @@ namespace RegisterBiomes { modules.push_back(module); return module; } + throw std::runtime_error("Invalid noise module specified."); } static void registerBiomes(sol::table source, DefinitionAtlas& defs, BiomeAtlas& biomes) { diff --git a/src/lua/register/RegisterBlocks.h b/src/lua/register/RegisterBlocks.h index 40ec5969..e6742e49 100644 --- a/src/lua/register/RegisterBlocks.h +++ b/src/lua/register/RegisterBlocks.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../Lua.h" #include "../Callback.h" #include "../../def/ItemDef.h" #include "../../def/ClientGame.h" diff --git a/src/lua/register/RegisterItems.h b/src/lua/register/RegisterItems.h index 18153d36..0fe8925a 100644 --- a/src/lua/register/RegisterItems.h +++ b/src/lua/register/RegisterItems.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../Lua.h" #include "../../def/ClientGame.h" #include "../../def/ServerGame.h" #include "../../def/gen/BiomeDef.h" diff --git a/src/lua/register/RegisterKeybinds.h b/src/lua/register/RegisterKeybinds.h index f91ca4e8..32850cf8 100644 --- a/src/lua/register/RegisterKeybinds.h +++ b/src/lua/register/RegisterKeybinds.h @@ -4,8 +4,7 @@ #pragma once -#include - +#include "../Lua.h" #include "../../def/ClientGame.h" #include "../../def/ServerGame.h" #include "../../def/gen/BiomeDef.h" diff --git a/src/net/Deserializer.h b/src/net/Deserializer.h index cc6beb99..ee3905e1 100644 --- a/src/net/Deserializer.h +++ b/src/net/Deserializer.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -14,7 +15,7 @@ public: Deserializer(const std::string& data) : data(&data[0]), len(data.length()) {}; Deserializer(const char* start, size_t len) : data(start), len(len) {}; - template inline T read() { assert(false); }; + template inline T read() { throw std::runtime_error("Tried to append a non-serializable type"); }; template inline Deserializer& read(T& ref) { ref = read(); return *this; diff --git a/src/net/Serializer.h b/src/net/Serializer.h index 7cf50005..fd09905e 100644 --- a/src/net/Serializer.h +++ b/src/net/Serializer.h @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -16,7 +17,7 @@ class Serializer { public: std::string data {}; - template inline Serializer& append(const T& elem) { assert(false); }; + template inline Serializer& append(const T& elem) { throw std::runtime_error("Tried to append a non-serializable type"); }; Packet packet(PacketType p, bool reliable = true) { Packet packet(p, reliable); diff --git a/src/net/server/Server.cpp b/src/net/server/Server.cpp index d90a285b..c6c74348 100644 --- a/src/net/server/Server.cpp +++ b/src/net/server/Server.cpp @@ -130,52 +130,38 @@ void Server::handlePlayerPacket(ServerClient& client, PacketView& p) { if (block == DefinitionAtlas::AIR) { auto def = defs.defs.blockFromId(worldBlock); - if (def.callbacks.count(Callback::BREAK)) { - defs.parser.safe_function(def.callbacks[Callback::BREAK], - defs.parser.luaVec(pos), ServerLuaPlayer(client)); - } - defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], - "break", defs.parser.luaVec(pos), ServerLuaPlayer(client)); + //TODO: stop casting to float vec3 + if (def.callbacks.count(Callback::BREAK)) defs.parser.safe_function(def.callbacks[Callback::BREAK], glm::vec3(pos), ServerLuaPlayer(client)); + defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "break", glm::vec3(pos), ServerLuaPlayer(client)); } else { auto def = defs.defs.blockFromId(block); - if (def.callbacks.count(Callback::PLACE)) { - defs.parser.safe_function(def.callbacks[Callback::PLACE], - defs.parser.luaVec(pos), ServerLuaPlayer(client)); - } - defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], - "place", defs.parser.luaVec(pos), ServerLuaPlayer(client)); + if (def.callbacks.count(Callback::PLACE)) + defs.parser.safe_function(def.callbacks[Callback::PLACE], pos, ServerLuaPlayer(client)); + defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "place", pos, ServerLuaPlayer(client)); } world.setBlock(pos, block); if (block == DefinitionAtlas::AIR) { auto def = defs.defs.blockFromId(worldBlock); - if (def.callbacks.count(Callback::AFTER_BREAK)) { - defs.parser.safe_function(def.callbacks[Callback::AFTER_BREAK], - defs.parser.luaVec(pos), ServerLuaPlayer(client)); - } - defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], - "after_break", defs.parser.luaVec(pos), ServerLuaPlayer(client)); + if (def.callbacks.count(Callback::AFTER_BREAK)) + defs.parser.safe_function(def.callbacks[Callback::AFTER_BREAK], pos, ServerLuaPlayer(client)); + defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "after_break", pos, ServerLuaPlayer(client)); } else { auto def = defs.defs.blockFromId(block); - if (def.callbacks.count(Callback::AFTER_PLACE)) { - defs.parser.safe_function(def.callbacks[Callback::AFTER_PLACE], - defs.parser.luaVec(pos), ServerLuaPlayer(client)); - } - defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], - "after_place", defs.parser.luaVec(pos), ServerLuaPlayer(client)); + if (def.callbacks.count(Callback::AFTER_PLACE)) + defs.parser.safe_function(def.callbacks[Callback::AFTER_PLACE], pos, ServerLuaPlayer(client)); + defs.parser.safe_function(defs.parser.core["__builtin"]["trigger_event"], "after_place", pos, ServerLuaPlayer(client)); } break; } case PacketType::BLOCK_INTERACT: { glm::ivec3 pos = p.d.read(); auto def = defs.defs.blockFromId(world.getBlock(pos)); - if (def.callbacks.count(Callback::INTERACT)) { - defs.parser.safe_function(def.callbacks[Callback::INTERACT], - defs.parser.luaVec(pos), ServerLuaPlayer(client)); - } + if (def.callbacks.count(Callback::INTERACT)) + defs.parser.safe_function(def.callbacks[Callback::INTERACT], pos, ServerLuaPlayer(client)); break; } case PacketType::INV_WATCH: { diff --git a/src/net/server/world/ServerWorld.cpp b/src/net/server/world/ServerWorld.cpp index 9e5fbf6a..e4764b28 100644 --- a/src/net/server/world/ServerWorld.cpp +++ b/src/net/server/world/ServerWorld.cpp @@ -218,15 +218,11 @@ void ServerWorld::setBlock(glm::ivec3 pos, unsigned int block) { if (block == DefinitionAtlas::AIR) { auto def = game.defs.blockFromId(oldBlock); - if (def.callbacks.count(Callback::DESTRUCT)) { - def.callbacks[Callback::DESTRUCT](game.parser.luaVec(pos)); - } + if (def.callbacks.count(Callback::DESTRUCT)) def.callbacks[Callback::DESTRUCT](pos); } else { auto def = game.defs.blockFromId(block); - if (def.callbacks.count(Callback::CONSTRUCT)) { - def.callbacks[Callback::CONSTRUCT](game.parser.luaVec(pos)); - } + if (def.callbacks.count(Callback::CONSTRUCT)) def.callbacks[Callback::CONSTRUCT](pos); } dimension.setBlock(pos, block); @@ -248,13 +244,11 @@ void ServerWorld::setBlock(glm::ivec3 pos, unsigned int block) { if (block == DefinitionAtlas::AIR) { auto def = game.defs.blockFromId(oldBlock); - if (def.callbacks.count(Callback::AFTER_DESTRUCT)) - def.callbacks[Callback::AFTER_DESTRUCT](game.parser.luaVec(pos)); + if (def.callbacks.count(Callback::AFTER_DESTRUCT)) def.callbacks[Callback::AFTER_DESTRUCT](pos); } else { auto def = game.defs.blockFromId(block); - if (def.callbacks.count(Callback::AFTER_CONSTRUCT)) - def.callbacks[Callback::AFTER_CONSTRUCT](game.parser.luaVec(pos)); + if (def.callbacks.count(Callback::AFTER_CONSTRUCT)) def.callbacks[Callback::AFTER_CONSTRUCT](pos); } } diff --git a/subgames/zeus/mods/aurailus_crafting/script/init.lua b/subgames/zeus/mods/aurailus_crafting/script/init.lua index 79d1dd45..726a34d7 100644 --- a/subgames/zeus/mods/aurailus_crafting/script/init.lua +++ b/subgames/zeus/mods/aurailus_crafting/script/init.lua @@ -2,11 +2,11 @@ _G["crafting"] = {} crafting.registered_recipes = {} -crafting.register_recipe = function(tbl) +function crafting.register_recipe(tbl) table.insert(crafting.registered_recipes, tbl) end -crafting.bind = function(craft_input, craft_output) +function crafting.bind(craft_input, craft_output) local width = craft_input.width local length = craft_input.length diff --git a/subgames/zeus/mods/aurailus_item_collection/script/dropped_item.lua b/subgames/zeus/mods/aurailus_item_collection/script/dropped_item.lua index 27dc93b7..115d400f 100644 --- a/subgames/zeus/mods/aurailus_item_collection/script/dropped_item.lua +++ b/subgames/zeus/mods/aurailus_item_collection/script/dropped_item.lua @@ -73,7 +73,7 @@ zepha.register_entity("@aurailus:item_collection:dropped_item", { end, check_collect = function(self) for _,p in pairs(zepha.players) do - if p.pos:dist(self.object.pos) < 2 then + if p.pos:distance(self.object.pos) < 2 then self.object.pos = p.pos + V{0, 0.90, 0} self.scooping = true diff --git a/subgames/zeus/mods/zeus_default/script/entity/test.lua b/subgames/zeus/mods/zeus_default/script/entity/test.lua index bdd276a8..7778c3c0 100644 --- a/subgames/zeus/mods/zeus_default/script/entity/test.lua +++ b/subgames/zeus/mods/zeus_default/script/entity/test.lua @@ -17,6 +17,4 @@ zepha.register_entity("zeus:default:test", { end }) -if zepha.client then - local entity = zepha.add_entity(V{}, "zeus:default:test") -end \ No newline at end of file +if zepha.client then entity = zepha.add_entity(V(), "zeus:default:test") end \ No newline at end of file diff --git a/subgames/zeus/mods/zeus_default/script/init.lua b/subgames/zeus/mods/zeus_default/script/init.lua index 6f86c090..abbf93d6 100644 --- a/subgames/zeus/mods/zeus_default/script/init.lua +++ b/subgames/zeus/mods/zeus_default/script/init.lua @@ -28,11 +28,4 @@ zepha.register_keybind("zeus:default:double_jump_fly", { if press - last_press < 0.25 then toggleFlying() end last_press = press; end -}) - --- if (zepha.client) then --- zepha.delay(function() --- zepha.get_hit_impact(zepha.player, "zeus:default:stone") --- return true --- end, 1) --- end \ No newline at end of file +}) \ No newline at end of file diff --git a/subgames/zeus/mods/zeus_mapgen/script/biomes/highlands.lua b/subgames/zeus/mods/zeus_mapgen/script/biomes/highlands.lua index c9e50862..d0029a49 100644 --- a/subgames/zeus/mods/zeus_mapgen/script/biomes/highlands.lua +++ b/subgames/zeus/mods/zeus_mapgen/script/biomes/highlands.lua @@ -31,7 +31,7 @@ local shrub = zepha.create_structure({ - origin = V{}, + origin = V{1, 2, 3}, schematic = { {{"zeus:default:tallgrass_4"}} } diff --git a/test/Main.cpp b/test/Main.cpp index 59549cd5..b5edfd7c 100644 --- a/test/Main.cpp +++ b/test/Main.cpp @@ -5,7 +5,7 @@ #pragma clang diagnostic pop #include -#include +#include TEST_CASE("Catch2 Library", "[core]") { REQUIRE(true); diff --git a/test/tests/TestBlockChunk.cpp b/test/tests/TestBlockChunk.cpp index c084e67e..f025524b 100644 --- a/test/tests/TestBlockChunk.cpp +++ b/test/tests/TestBlockChunk.cpp @@ -3,7 +3,6 @@ // #include -#include #include "../../src/world/chunk/Chunk.h" #pragma clang diagnostic push diff --git a/test/tests/TestSerializer.cpp b/test/tests/TestSerializer.cpp index 118c0669..4440df28 100644 --- a/test/tests/TestSerializer.cpp +++ b/test/tests/TestSerializer.cpp @@ -4,8 +4,8 @@ #include #include -#include "../../src/util/net/Serializer.h" -#include "../../src/util/net/Deserializer.h" +#include "../../src/net/Serializer.h" +#include "../../src/net/Deserializer.h" TEST_CASE("Serialization", "[net]") { Serializer s;