From a46aafa562d22740948beba23b582850587ee4a2 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 22 Aug 2025 22:26:43 +0300 Subject: [PATCH] fix fragment:place rotation (#593) * fix fragment:place rotation --- .../scripting/lua/libs/libgeneration.cpp | 20 ++++++++++++++++--- src/logic/scripting/lua/lua_custom_types.hpp | 11 ++++++---- .../lua/usertypes/lua_type_voxelfragment.cpp | 19 +++++++++++------- src/world/generator/VoxelFragment.cpp | 2 +- src/world/generator/VoxelFragment.hpp | 3 +-- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/logic/scripting/lua/libs/libgeneration.cpp b/src/logic/scripting/lua/libs/libgeneration.cpp index 291b847f..f0cbb2f8 100644 --- a/src/logic/scripting/lua/libs/libgeneration.cpp +++ b/src/logic/scripting/lua/libs/libgeneration.cpp @@ -16,7 +16,7 @@ using namespace scripting; static int l_save_fragment(lua::State* L) { auto fragment = lua::touserdata(L, 1); auto file = lua::require_string(L, 2); - auto map = fragment->getFragment()->serialize(); + auto map = fragment->getFragment(0)->serialize(); auto bytes = json::to_binary(map, true); io::write_bytes(file, bytes.data(), bytes.size()); return 0; @@ -30,8 +30,14 @@ static int l_create_fragment(lua::State* L) { auto fragment = VoxelFragment::create(*level, pointA, pointB, crop, saveEntities); + std::array, 4> fragmentVariants { + std::move(fragment) + }; + for (size_t i = 1; i < 4; i++) { + fragmentVariants[i] = fragmentVariants[i - 1]->rotated(*content); + } return lua::newuserdata( - L, std::shared_ptr(std::move(fragment)) + L, std::move(fragmentVariants) ); } @@ -45,7 +51,15 @@ static int l_load_fragment(lua::State* L) { auto fragment = std::make_shared(); fragment->deserialize(map); fragment->prepare(*content); - return lua::newuserdata(L, std::move(fragment)); + std::array, 4> fragmentVariants { + std::move(fragment) + }; + for (size_t i = 1; i < 4; i++) { + fragmentVariants[i] = fragmentVariants[i - 1]->rotated(*content); + } + return lua::newuserdata( + L, std::move(fragmentVariants) + ); } /// @brief Get a list of all world generators diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp index 1185ecbe..87080274 100644 --- a/src/logic/scripting/lua/lua_custom_types.hpp +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -2,6 +2,7 @@ #include #include +#include #include "lua_commons.hpp" @@ -55,14 +56,16 @@ namespace lua { static_assert(!std::is_abstract()); class LuaVoxelFragment : public Userdata { - std::shared_ptr fragment; + std::array, 4> fragmentVariants; public: - LuaVoxelFragment(std::shared_ptr fragment); + LuaVoxelFragment( + std::array, 4> fragmentVariants + ); virtual ~LuaVoxelFragment(); - std::shared_ptr getFragment() const { - return fragment; + std::shared_ptr getFragment(size_t rotation) const { + return fragmentVariants.at(rotation & 0b11); } const std::string& getTypeName() const override { diff --git a/src/logic/scripting/lua/usertypes/lua_type_voxelfragment.cpp b/src/logic/scripting/lua/usertypes/lua_type_voxelfragment.cpp index f123be6f..271a2a5a 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_voxelfragment.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_voxelfragment.cpp @@ -8,15 +8,20 @@ using namespace lua; -LuaVoxelFragment::LuaVoxelFragment(std::shared_ptr fragment) - : fragment(std::move(fragment)) {} +LuaVoxelFragment::LuaVoxelFragment( + std::array, 4> fragmentVariants +) + : fragmentVariants(std::move(fragmentVariants)) { +} LuaVoxelFragment::~LuaVoxelFragment() { } static int l_crop(lua::State* L) { if (auto fragment = touserdata(L, 1)) { - fragment->getFragment()->crop(); + for (size_t i = 0; i < 4; i++) { + fragment->getFragment(i)->crop(); + } } return 0; } @@ -24,9 +29,9 @@ static int l_crop(lua::State* L) { static int l_place(lua::State* L) { if (auto fragment = touserdata(L, 1)) { auto offset = tovec3(L, 2); - int rotation = tointeger(L, 3) & 0b11; - fragment->getFragment()->place( - *scripting::level->chunks, offset, rotation + int rotation = tointeger(L, 3); + fragment->getFragment(rotation)->place( + *scripting::level->chunks, offset ); } return 0; @@ -50,7 +55,7 @@ static int l_meta_index(lua::State* L) { if (isstring(L, 2)) { auto fieldname = tostring(L, 2); if (!std::strcmp(fieldname, "size")) { - return pushivec(L, fragment->getFragment()->getSize()); + return pushivec(L, fragment->getFragment(0)->getSize()); } else { auto found = methods.find(tostring(L, 2)); if (found != methods.end()) { diff --git a/src/world/generator/VoxelFragment.cpp b/src/world/generator/VoxelFragment.cpp index 344f0450..8e86c1a7 100644 --- a/src/world/generator/VoxelFragment.cpp +++ b/src/world/generator/VoxelFragment.cpp @@ -171,7 +171,7 @@ void VoxelFragment::prepare(const Content& content) { } void VoxelFragment::place( - GlobalChunks& chunks, const glm::ivec3& offset, ubyte rotation + GlobalChunks& chunks, const glm::ivec3& offset ) { auto& structVoxels = getRuntimeVoxels(); for (int y = 0; y < size.y; y++) { diff --git a/src/world/generator/VoxelFragment.hpp b/src/world/generator/VoxelFragment.hpp index 81944900..8b49110e 100644 --- a/src/world/generator/VoxelFragment.hpp +++ b/src/world/generator/VoxelFragment.hpp @@ -45,8 +45,7 @@ public: /// @brief Place fragment to the world /// @param offset target location - /// @param rotation rotation index - void place(GlobalChunks& chunks, const glm::ivec3& offset, ubyte rotation); + void place(GlobalChunks& chunks, const glm::ivec3& offset); /// @brief Create structure copy rotated 90 deg. clockwise std::unique_ptr rotated(const Content& content) const;