feat: world.get_chunk_data(...) saved chunk data when chunk is not loaded
This commit is contained in:
parent
f1f81371af
commit
02a93d8bc3
@ -126,11 +126,21 @@ static int l_get_chunk_data(lua::State* L) {
|
|||||||
int x = static_cast<int>(lua::tointeger(L, 1));
|
int x = static_cast<int>(lua::tointeger(L, 1));
|
||||||
int z = static_cast<int>(lua::tointeger(L, 2));
|
int z = static_cast<int>(lua::tointeger(L, 2));
|
||||||
const auto& chunk = level->chunks->getChunk(x, z);
|
const auto& chunk = level->chunks->getChunk(x, z);
|
||||||
|
|
||||||
|
std::vector<ubyte> chunkData;
|
||||||
if (chunk == nullptr) {
|
if (chunk == nullptr) {
|
||||||
lua::pushnil(L);
|
auto& regions = level->getWorld()->wfile->getRegions();
|
||||||
|
auto voxelData = regions.getVoxels(x, z);
|
||||||
|
if (voxelData == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
auto chunkData = compressed_chunks::encode(*chunk);
|
static util::Buffer<ubyte> rleBuffer(CHUNK_DATA_LEN * 2);
|
||||||
|
auto metadata = regions.getBlocksData(x, z);
|
||||||
|
chunkData =
|
||||||
|
compressed_chunks::encode(voxelData.get(), metadata, rleBuffer);
|
||||||
|
} else {
|
||||||
|
chunkData = compressed_chunks::encode(*chunk);
|
||||||
|
}
|
||||||
return lua::newuserdata<lua::LuaBytearray>(L, std::move(chunkData));
|
return lua::newuserdata<lua::LuaBytearray>(L, std::move(chunkData));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,28 +2,24 @@
|
|||||||
|
|
||||||
#include "coders/rle.hpp"
|
#include "coders/rle.hpp"
|
||||||
#include "coders/gzip.hpp"
|
#include "coders/gzip.hpp"
|
||||||
#include "coders/byte_utils.hpp"
|
|
||||||
#include "files/WorldFiles.hpp"
|
#include "files/WorldFiles.hpp"
|
||||||
#include "voxels/Chunk.hpp"
|
|
||||||
|
|
||||||
inline constexpr int HAS_VOXELS = 0x1;
|
inline constexpr int HAS_VOXELS = 0x1;
|
||||||
inline constexpr int HAS_METADATA = 0x2;
|
inline constexpr int HAS_METADATA = 0x2;
|
||||||
|
|
||||||
std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
|
std::vector<ubyte> compressed_chunks::encode(
|
||||||
auto data = chunk.encode();
|
const ubyte* data,
|
||||||
|
const BlocksMetadata& metadata,
|
||||||
/// world.get_chunk_data is only available in the main Lua state
|
util::Buffer<ubyte>& rleBuffer
|
||||||
static util::Buffer<ubyte> rleBuffer;
|
) {
|
||||||
if (rleBuffer.size() < CHUNK_DATA_LEN * 2) {
|
|
||||||
rleBuffer = util::Buffer<ubyte>(CHUNK_DATA_LEN * 2);
|
|
||||||
}
|
|
||||||
size_t rleCompressedSize =
|
size_t rleCompressedSize =
|
||||||
extrle::encode16(data.get(), CHUNK_DATA_LEN, rleBuffer.data());
|
extrle::encode16(data, CHUNK_DATA_LEN, rleBuffer.data());
|
||||||
|
|
||||||
const auto gzipCompressedData = gzip::compress(
|
const auto gzipCompressedData = gzip::compress(
|
||||||
rleBuffer.data(), rleCompressedSize
|
rleBuffer.data(), rleCompressedSize
|
||||||
);
|
);
|
||||||
auto metadataBytes = chunk.blocksMetadata.serialize();
|
auto metadataBytes = metadata.serialize();
|
||||||
|
|
||||||
ByteBuilder builder(2 + 8 + gzipCompressedData.size() + metadataBytes.size());
|
ByteBuilder builder(2 + 8 + gzipCompressedData.size() + metadataBytes.size());
|
||||||
builder.put(HAS_VOXELS | HAS_METADATA); // flags
|
builder.put(HAS_VOXELS | HAS_METADATA); // flags
|
||||||
@ -35,6 +31,14 @@ std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
|
|||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<ubyte> compressed_chunks::encode(const Chunk& chunk) {
|
||||||
|
auto data = chunk.encode();
|
||||||
|
|
||||||
|
/// world.get_chunk_data is only available in the main Lua state
|
||||||
|
static util::Buffer<ubyte> rleBuffer(CHUNK_DATA_LEN * 2);
|
||||||
|
return encode(data.get(), chunk.blocksMetadata, rleBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
static void read_voxel_data(ByteReader& reader, util::Buffer<ubyte>& dst) {
|
static void read_voxel_data(ByteReader& reader, util::Buffer<ubyte>& dst) {
|
||||||
size_t gzipCompressedSize = reader.getInt32();
|
size_t gzipCompressedSize = reader.getInt32();
|
||||||
|
|
||||||
|
|||||||
@ -1,13 +1,19 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "typedefs.hpp"
|
#include "typedefs.hpp"
|
||||||
|
#include "Chunk.hpp"
|
||||||
|
#include "coders/byte_utils.hpp"
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
class Chunk;
|
|
||||||
class WorldRegions;
|
class WorldRegions;
|
||||||
|
|
||||||
namespace compressed_chunks {
|
namespace compressed_chunks {
|
||||||
|
std::vector<ubyte> encode(
|
||||||
|
const ubyte* voxelData,
|
||||||
|
const BlocksMetadata& metadata,
|
||||||
|
util::Buffer<ubyte>& rleBuffer
|
||||||
|
);
|
||||||
std::vector<ubyte> encode(const Chunk& chunk);
|
std::vector<ubyte> encode(const Chunk& chunk);
|
||||||
void decode(Chunk& chunk, const ubyte* src, size_t size);
|
void decode(Chunk& chunk, const ubyte* src, size_t size);
|
||||||
void save(int x, int z, std::vector<ubyte> bytes, WorldRegions& regions);
|
void save(int x, int z, std::vector<ubyte> bytes, WorldRegions& regions);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user