update voxel fragments lua api & replace structure.save command with fragment.save & add 'export' entry point
This commit is contained in:
parent
cc6891dde8
commit
fd2bd15658
2
.gitignore
vendored
2
.gitignore
vendored
@ -6,6 +6,8 @@ Debug/voxel_engine
|
||||
/build
|
||||
/cmake-build-**
|
||||
/screenshots
|
||||
/export
|
||||
/config
|
||||
/out
|
||||
|
||||
/misc
|
||||
|
||||
@ -151,8 +151,8 @@ console.add_command(
|
||||
)
|
||||
|
||||
console.add_command(
|
||||
"structure.save x:int y:int z:int w:int h:int d:int name:str='untitled'",
|
||||
"Save structure",
|
||||
"fragment.save x:int y:int z:int w:int h:int d:int name:str='untitled' crop:bool=false",
|
||||
"Save fragment",
|
||||
function(args, kwargs)
|
||||
local x = args[1]
|
||||
local y = args[2]
|
||||
@ -163,6 +163,14 @@ console.add_command(
|
||||
local d = args[6]
|
||||
|
||||
local name = args[7]
|
||||
generation.save_structure({x, y, z}, {x+w, y+h, z+d}, name..'.vox', false)
|
||||
local crop = args[8]
|
||||
|
||||
local fragment = generation.create_fragment(
|
||||
{x, y, z}, {x + w, y + h, z + d}, crop, false
|
||||
)
|
||||
local filename = 'export:'..name..'.vox'
|
||||
generation.save_fragment(fragment, filename, crop)
|
||||
console.log("fragment with size "..vec3.tostring(fragment.size)..
|
||||
" has been saved as "..file.resolve(filename))
|
||||
end
|
||||
)
|
||||
|
||||
@ -18,6 +18,7 @@ static inline auto SCREENSHOTS_FOLDER = std::filesystem::u8path("screenshots");
|
||||
static inline auto CONTENT_FOLDER = std::filesystem::u8path("content");
|
||||
static inline auto WORLDS_FOLDER = std::filesystem::u8path("worlds");
|
||||
static inline auto CONFIG_FOLDER = std::filesystem::u8path("config");
|
||||
static inline auto EXPORT_FOLDER = std::filesystem::u8path("export");
|
||||
static inline auto CONTROLS_FILE = std::filesystem::u8path("controls.toml");
|
||||
static inline auto SETTINGS_FILE = std::filesystem::u8path("settings.toml");
|
||||
|
||||
@ -51,6 +52,10 @@ void EnginePaths::prepare() {
|
||||
if (!fs::is_directory(contentFolder)) {
|
||||
fs::create_directories(contentFolder);
|
||||
}
|
||||
auto exportFolder = userFilesFolder / EXPORT_FOLDER;
|
||||
if (!fs::is_directory(exportFolder)) {
|
||||
fs::create_directories(exportFolder);
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getUserFilesFolder() const {
|
||||
@ -187,6 +192,9 @@ std::filesystem::path EnginePaths::resolve(
|
||||
if (prefix == "world") {
|
||||
return currentWorldFolder / fs::u8path(filename);
|
||||
}
|
||||
if (prefix == "export") {
|
||||
return userFilesFolder / EXPORT_FOLDER / fs::u8path(filename);
|
||||
}
|
||||
|
||||
if (contentPacks) {
|
||||
for (auto& pack : *contentPacks) {
|
||||
|
||||
@ -11,24 +11,30 @@
|
||||
|
||||
using namespace scripting;
|
||||
|
||||
static int l_save_structure(lua::State* L) {
|
||||
auto pointA = lua::tovec<3>(L, 1);
|
||||
auto pointB = lua::tovec<3>(L, 2);
|
||||
auto filename = lua::require_string(L, 3);
|
||||
if (!files::is_valid_name(filename)) {
|
||||
throw std::runtime_error("invalid file name");
|
||||
}
|
||||
bool saveEntities = lua::toboolean(L, 4);
|
||||
|
||||
auto structure = VoxelFragment::create(level, pointA, pointB, saveEntities);
|
||||
auto map = structure->serialize();
|
||||
|
||||
auto bytes = json::to_binary(map);
|
||||
files::write_bytes(fs::u8path(filename), bytes.data(), bytes.size());
|
||||
static int l_save_fragment(lua::State* L) {
|
||||
auto paths = engine->getPaths();
|
||||
auto fragment = lua::touserdata<lua::LuaVoxelFragment>(L, 1);
|
||||
auto file = paths->resolve(lua::require_string(L, 2), true);
|
||||
auto map = fragment->getFragment()->serialize();
|
||||
auto bytes = json::to_binary(map, true);
|
||||
files::write_bytes(file, bytes.data(), bytes.size());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int l_load_structure(lua::State* L) {
|
||||
static int l_create_fragment(lua::State* L) {
|
||||
auto pointA = lua::tovec<3>(L, 1);
|
||||
auto pointB = lua::tovec<3>(L, 2);
|
||||
bool crop = lua::toboolean(L, 3);
|
||||
bool saveEntities = lua::toboolean(L, 4);
|
||||
|
||||
auto fragment =
|
||||
VoxelFragment::create(level, pointA, pointB, crop, saveEntities);
|
||||
return lua::newuserdata<lua::LuaVoxelFragment>(
|
||||
L, std::shared_ptr<VoxelFragment>(std::move(fragment))
|
||||
);
|
||||
}
|
||||
|
||||
static int l_load_fragment(lua::State* L) {
|
||||
auto paths = engine->getPaths();
|
||||
auto [prefix, filename] = EnginePaths::parsePath(lua::require_string(L, 1));
|
||||
|
||||
@ -38,9 +44,9 @@ static int l_load_structure(lua::State* L) {
|
||||
}
|
||||
auto map = files::read_binary_json(path);
|
||||
|
||||
auto structure = std::make_shared<VoxelFragment>();
|
||||
structure->deserialize(map);
|
||||
return lua::newuserdata<lua::LuaVoxelStructure>(L, std::move(structure));
|
||||
auto fragment = std::make_shared<VoxelFragment>();
|
||||
fragment->deserialize(map);
|
||||
return lua::newuserdata<lua::LuaVoxelFragment>(L, std::move(fragment));
|
||||
}
|
||||
|
||||
/// @brief Get a list of all world generators
|
||||
@ -72,8 +78,9 @@ static int l_get_default_generator(lua::State* L) {
|
||||
}
|
||||
|
||||
const luaL_Reg generationlib[] = {
|
||||
{"save_structure", lua::wrap<l_save_structure>},
|
||||
{"load_structure", lua::wrap<l_load_structure>},
|
||||
{"create_fragment", lua::wrap<l_create_fragment>},
|
||||
{"save_fragment", lua::wrap<l_save_fragment>},
|
||||
{"load_fragment", lua::wrap<l_load_fragment>},
|
||||
{"get_generators", lua::wrap<l_get_generators>},
|
||||
{"get_default_generator", lua::wrap<l_get_default_generator>},
|
||||
{NULL, NULL}};
|
||||
|
||||
@ -71,15 +71,15 @@ namespace lua {
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaHeightmap>());
|
||||
|
||||
class LuaVoxelStructure : public Userdata {
|
||||
std::shared_ptr<VoxelFragment> structure;
|
||||
class LuaVoxelFragment : public Userdata {
|
||||
std::shared_ptr<VoxelFragment> fragment;
|
||||
public:
|
||||
LuaVoxelStructure(std::shared_ptr<VoxelFragment> structure);
|
||||
LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment);
|
||||
|
||||
virtual ~LuaVoxelStructure();
|
||||
virtual ~LuaVoxelFragment();
|
||||
|
||||
std::shared_ptr<VoxelFragment> getStructure() const {
|
||||
return structure;
|
||||
std::shared_ptr<VoxelFragment> getFragment() const {
|
||||
return fragment;
|
||||
}
|
||||
|
||||
const std::string& getTypeName() const override {
|
||||
@ -87,7 +87,7 @@ namespace lua {
|
||||
}
|
||||
|
||||
static int createMetatable(lua::State*);
|
||||
inline static std::string TYPENAME = "VoxelStructure";
|
||||
inline static std::string TYPENAME = "VoxelFragment";
|
||||
};
|
||||
static_assert(!std::is_abstract<LuaVoxelStructure>());
|
||||
static_assert(!std::is_abstract<LuaVoxelFragment>());
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ void lua::init_state(State* L, StateType stateType) {
|
||||
|
||||
newusertype<LuaBytearray>(L);
|
||||
newusertype<LuaHeightmap>(L);
|
||||
newusertype<LuaVoxelStructure>(L);
|
||||
newusertype<LuaVoxelFragment>(L);
|
||||
}
|
||||
|
||||
void lua::initialize(const EnginePaths& paths) {
|
||||
|
||||
@ -7,20 +7,37 @@
|
||||
|
||||
using namespace lua;
|
||||
|
||||
LuaVoxelStructure::LuaVoxelStructure(std::shared_ptr<VoxelFragment> structure)
|
||||
: structure(std::move(structure)) {}
|
||||
LuaVoxelFragment::LuaVoxelFragment(std::shared_ptr<VoxelFragment> fragment)
|
||||
: fragment(std::move(fragment)) {}
|
||||
|
||||
LuaVoxelStructure::~LuaVoxelStructure() {
|
||||
LuaVoxelFragment::~LuaVoxelFragment() {
|
||||
}
|
||||
|
||||
static int l_meta_tostring(lua::State* L) {
|
||||
return pushstring(L, "VoxelStructure(0x" + util::tohex(
|
||||
return pushstring(L, "VoxelFragment(0x" + util::tohex(
|
||||
reinterpret_cast<uint64_t>(topointer(L, 1)))+")");
|
||||
}
|
||||
|
||||
int LuaVoxelStructure::createMetatable(lua::State* L) {
|
||||
createtable(L, 0, 1);
|
||||
static int l_meta_index(lua::State* L) {
|
||||
auto fragment = touserdata<LuaVoxelFragment>(L, 1);
|
||||
if (fragment == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
if (isstring(L, 2)) {
|
||||
auto fieldname = tostring(L, 2);
|
||||
if (!std::strcmp(fieldname, "size")) {
|
||||
return pushivec(L, fragment->getFragment()->getSize());
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int LuaVoxelFragment::createMetatable(lua::State* L) {
|
||||
createtable(L, 0, 2);
|
||||
pushcfunction(L, lua::wrap<l_meta_tostring>);
|
||||
setfield(L, "__tostring");
|
||||
pushcfunction(L, lua::wrap<l_meta_index>);
|
||||
setfield(L, "__index");
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -11,11 +11,41 @@
|
||||
#include "world/Level.hpp"
|
||||
|
||||
std::unique_ptr<VoxelFragment> VoxelFragment::create(
|
||||
Level* level, const glm::ivec3& a, const glm::ivec3& b, bool entities
|
||||
Level* level,
|
||||
const glm::ivec3& a,
|
||||
const glm::ivec3& b,
|
||||
bool crop,
|
||||
bool entities
|
||||
) {
|
||||
auto start = glm::min(a, b);
|
||||
auto size = glm::abs(a - b);
|
||||
|
||||
if (crop) {
|
||||
VoxelsVolume volume(size.x, size.y, size.z);
|
||||
volume.setPosition(start.x, start.y, start.z);
|
||||
level->chunksStorage->getVoxels(&volume);
|
||||
|
||||
auto end = start + size;
|
||||
|
||||
auto min = end;
|
||||
auto max = start;
|
||||
|
||||
for (int y = start.y; y < end.y; y++) {
|
||||
for (int z = start.z; z < end.z; z++) {
|
||||
for (int x = start.x; x < end.x; x++) {
|
||||
if (volume.pickBlockId(x, y, z)) {
|
||||
min = glm::min(min, {x, y, z});
|
||||
max = glm::max(max, {x+1, y+1, z+1});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (glm::min(min, max) == min) {
|
||||
start = min;
|
||||
size = max - min;
|
||||
}
|
||||
}
|
||||
|
||||
VoxelsVolume volume(size.x, size.y, size.z);
|
||||
volume.setPosition(start.x, start.y, start.z);
|
||||
level->chunksStorage->getVoxels(&volume);
|
||||
|
||||
@ -44,7 +44,12 @@ public:
|
||||
std::unique_ptr<VoxelFragment> rotated(const Content& content) const;
|
||||
|
||||
static std::unique_ptr<VoxelFragment> create(
|
||||
Level* level, const glm::ivec3& a, const glm::ivec3& b, bool entities);
|
||||
Level* level,
|
||||
const glm::ivec3& a,
|
||||
const glm::ivec3& b,
|
||||
bool crop,
|
||||
bool entities
|
||||
);
|
||||
|
||||
const glm::ivec3& getSize() const {
|
||||
return size;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user