fix fragment:place rotation (#593)

* fix fragment:place rotation
This commit is contained in:
MihailRis 2025-08-22 22:26:43 +03:00 committed by GitHub
parent eba7eed8c0
commit a46aafa562
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 17 deletions

View File

@ -16,7 +16,7 @@ using namespace scripting;
static int l_save_fragment(lua::State* L) {
auto fragment = lua::touserdata<lua::LuaVoxelFragment>(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<std::shared_ptr<VoxelFragment>, 4> fragmentVariants {
std::move(fragment)
};
for (size_t i = 1; i < 4; i++) {
fragmentVariants[i] = fragmentVariants[i - 1]->rotated(*content);
}
return lua::newuserdata<lua::LuaVoxelFragment>(
L, std::shared_ptr<VoxelFragment>(std::move(fragment))
L, std::move(fragmentVariants)
);
}
@ -45,7 +51,15 @@ static int l_load_fragment(lua::State* L) {
auto fragment = std::make_shared<VoxelFragment>();
fragment->deserialize(map);
fragment->prepare(*content);
return lua::newuserdata<lua::LuaVoxelFragment>(L, std::move(fragment));
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants {
std::move(fragment)
};
for (size_t i = 1; i < 4; i++) {
fragmentVariants[i] = fragmentVariants[i - 1]->rotated(*content);
}
return lua::newuserdata<lua::LuaVoxelFragment>(
L, std::move(fragmentVariants)
);
}
/// @brief Get a list of all world generators

View File

@ -2,6 +2,7 @@
#include <string>
#include <vector>
#include <array>
#include "lua_commons.hpp"
@ -55,14 +56,16 @@ namespace lua {
static_assert(!std::is_abstract<LuaHeightmap>());
class LuaVoxelFragment : public Userdata {
std::shared_ptr<VoxelFragment> fragment;
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants;
public:
LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment);
LuaVoxelFragment(
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants
);
virtual ~LuaVoxelFragment();
std::shared_ptr<VoxelFragment> getFragment() const {
return fragment;
std::shared_ptr<VoxelFragment> getFragment(size_t rotation) const {
return fragmentVariants.at(rotation & 0b11);
}
const std::string& getTypeName() const override {

View File

@ -8,15 +8,20 @@
using namespace lua;
LuaVoxelFragment::LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment)
: fragment(std::move(fragment)) {}
LuaVoxelFragment::LuaVoxelFragment(
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants
)
: fragmentVariants(std::move(fragmentVariants)) {
}
LuaVoxelFragment::~LuaVoxelFragment() {
}
static int l_crop(lua::State* L) {
if (auto fragment = touserdata<LuaVoxelFragment>(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<LuaVoxelFragment>(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()) {

View File

@ -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++) {

View File

@ -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<VoxelFragment> rotated(const Content& content) const;