Merge branch 'main' into dev

This commit is contained in:
MihailRis 2025-08-24 17:31:34 +03:00
commit 383dacf616
7 changed files with 46 additions and 18 deletions

View File

@ -28,3 +28,9 @@ assert(#arr == arr:get_capacity())
arr = Bytearray({0, 2, 7, 1, 16, 75, 25})
assert(arr[6] == 75)
arr:insert(2, {5, 6})
assert(arr[#arr] == 25)
assert(arr[2] == 5)
assert(arr[3] == 6)

View File

@ -71,7 +71,7 @@ local function insert(self, index, b)
if self.size + elems > self.capacity then
grow_buffer(self, elems)
end
for i=self.size, index - 1, -1 do
for i = self.size - 1, index - 1, -1 do
self.bytes[i + elems] = self.bytes[i]
end
if _type(b) == "number" then

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,15 @@ static int l_create_fragment(lua::State* L) {
auto fragment =
VoxelFragment::create(*level, pointA, pointB, crop, saveEntities);
fragment->prepare(*content);
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)
);
}
@ -53,7 +60,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;