parent
eba7eed8c0
commit
a46aafa562
@ -16,7 +16,7 @@ using namespace scripting;
|
|||||||
static int l_save_fragment(lua::State* L) {
|
static int l_save_fragment(lua::State* L) {
|
||||||
auto fragment = lua::touserdata<lua::LuaVoxelFragment>(L, 1);
|
auto fragment = lua::touserdata<lua::LuaVoxelFragment>(L, 1);
|
||||||
auto file = lua::require_string(L, 2);
|
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);
|
auto bytes = json::to_binary(map, true);
|
||||||
io::write_bytes(file, bytes.data(), bytes.size());
|
io::write_bytes(file, bytes.data(), bytes.size());
|
||||||
return 0;
|
return 0;
|
||||||
@ -30,8 +30,14 @@ static int l_create_fragment(lua::State* L) {
|
|||||||
|
|
||||||
auto fragment =
|
auto fragment =
|
||||||
VoxelFragment::create(*level, pointA, pointB, crop, saveEntities);
|
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>(
|
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>();
|
auto fragment = std::make_shared<VoxelFragment>();
|
||||||
fragment->deserialize(map);
|
fragment->deserialize(map);
|
||||||
fragment->prepare(*content);
|
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
|
/// @brief Get a list of all world generators
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
#include "lua_commons.hpp"
|
#include "lua_commons.hpp"
|
||||||
|
|
||||||
@ -55,14 +56,16 @@ namespace lua {
|
|||||||
static_assert(!std::is_abstract<LuaHeightmap>());
|
static_assert(!std::is_abstract<LuaHeightmap>());
|
||||||
|
|
||||||
class LuaVoxelFragment : public Userdata {
|
class LuaVoxelFragment : public Userdata {
|
||||||
std::shared_ptr<VoxelFragment> fragment;
|
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants;
|
||||||
public:
|
public:
|
||||||
LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment);
|
LuaVoxelFragment(
|
||||||
|
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants
|
||||||
|
);
|
||||||
|
|
||||||
virtual ~LuaVoxelFragment();
|
virtual ~LuaVoxelFragment();
|
||||||
|
|
||||||
std::shared_ptr<VoxelFragment> getFragment() const {
|
std::shared_ptr<VoxelFragment> getFragment(size_t rotation) const {
|
||||||
return fragment;
|
return fragmentVariants.at(rotation & 0b11);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& getTypeName() const override {
|
const std::string& getTypeName() const override {
|
||||||
|
|||||||
@ -8,15 +8,20 @@
|
|||||||
|
|
||||||
using namespace lua;
|
using namespace lua;
|
||||||
|
|
||||||
LuaVoxelFragment::LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment)
|
LuaVoxelFragment::LuaVoxelFragment(
|
||||||
: fragment(std::move(fragment)) {}
|
std::array<std::shared_ptr<VoxelFragment>, 4> fragmentVariants
|
||||||
|
)
|
||||||
|
: fragmentVariants(std::move(fragmentVariants)) {
|
||||||
|
}
|
||||||
|
|
||||||
LuaVoxelFragment::~LuaVoxelFragment() {
|
LuaVoxelFragment::~LuaVoxelFragment() {
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_crop(lua::State* L) {
|
static int l_crop(lua::State* L) {
|
||||||
if (auto fragment = touserdata<LuaVoxelFragment>(L, 1)) {
|
if (auto fragment = touserdata<LuaVoxelFragment>(L, 1)) {
|
||||||
fragment->getFragment()->crop();
|
for (size_t i = 0; i < 4; i++) {
|
||||||
|
fragment->getFragment(i)->crop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -24,9 +29,9 @@ static int l_crop(lua::State* L) {
|
|||||||
static int l_place(lua::State* L) {
|
static int l_place(lua::State* L) {
|
||||||
if (auto fragment = touserdata<LuaVoxelFragment>(L, 1)) {
|
if (auto fragment = touserdata<LuaVoxelFragment>(L, 1)) {
|
||||||
auto offset = tovec3(L, 2);
|
auto offset = tovec3(L, 2);
|
||||||
int rotation = tointeger(L, 3) & 0b11;
|
int rotation = tointeger(L, 3);
|
||||||
fragment->getFragment()->place(
|
fragment->getFragment(rotation)->place(
|
||||||
*scripting::level->chunks, offset, rotation
|
*scripting::level->chunks, offset
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
@ -50,7 +55,7 @@ static int l_meta_index(lua::State* L) {
|
|||||||
if (isstring(L, 2)) {
|
if (isstring(L, 2)) {
|
||||||
auto fieldname = tostring(L, 2);
|
auto fieldname = tostring(L, 2);
|
||||||
if (!std::strcmp(fieldname, "size")) {
|
if (!std::strcmp(fieldname, "size")) {
|
||||||
return pushivec(L, fragment->getFragment()->getSize());
|
return pushivec(L, fragment->getFragment(0)->getSize());
|
||||||
} else {
|
} else {
|
||||||
auto found = methods.find(tostring(L, 2));
|
auto found = methods.find(tostring(L, 2));
|
||||||
if (found != methods.end()) {
|
if (found != methods.end()) {
|
||||||
|
|||||||
@ -171,7 +171,7 @@ void VoxelFragment::prepare(const Content& content) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VoxelFragment::place(
|
void VoxelFragment::place(
|
||||||
GlobalChunks& chunks, const glm::ivec3& offset, ubyte rotation
|
GlobalChunks& chunks, const glm::ivec3& offset
|
||||||
) {
|
) {
|
||||||
auto& structVoxels = getRuntimeVoxels();
|
auto& structVoxels = getRuntimeVoxels();
|
||||||
for (int y = 0; y < size.y; y++) {
|
for (int y = 0; y < size.y; y++) {
|
||||||
|
|||||||
@ -45,8 +45,7 @@ public:
|
|||||||
|
|
||||||
/// @brief Place fragment to the world
|
/// @brief Place fragment to the world
|
||||||
/// @param offset target location
|
/// @param offset target location
|
||||||
/// @param rotation rotation index
|
void place(GlobalChunks& chunks, const glm::ivec3& offset);
|
||||||
void place(GlobalChunks& chunks, const glm::ivec3& offset, ubyte rotation);
|
|
||||||
|
|
||||||
/// @brief Create structure copy rotated 90 deg. clockwise
|
/// @brief Create structure copy rotated 90 deg. clockwise
|
||||||
std::unique_ptr<VoxelFragment> rotated(const Content& content) const;
|
std::unique_ptr<VoxelFragment> rotated(const Content& content) const;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user