summaryrefslogtreecommitdiffstats
path: root/src/World/Chunk.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/World/Chunk.cpp')
-rw-r--r--src/World/Chunk.cpp57
1 files changed, 45 insertions, 12 deletions
diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp
index 915e5f3..2dc9014 100644
--- a/src/World/Chunk.cpp
+++ b/src/World/Chunk.cpp
@@ -39,6 +39,20 @@
namespace MinedMap {
namespace World {
+Chunk::Chunk(Buffer buffer) {
+ size_t size = buffer.get32();
+
+ Buffer buffer2(buffer.get(size), size);
+
+ uint8_t format = buffer2.get8();
+ if (format != 2)
+ throw std::invalid_argument("unknown chunk format");
+
+ inflateChunk(buffer2);
+ parseChunk();
+ analyzeChunk();
+}
+
void Chunk::inflateChunk(Buffer buffer) {
size_t outlen = 0;
uint8_t *output = nullptr;
@@ -97,11 +111,11 @@ void Chunk::analyzeChunk() {
}
}
-uint8_t Chunk::getBlockAt(const std::shared_ptr<const NBT::CompoundTag> &section, size_t x, size_t y, size_t z) {
+uint8_t Chunk::getBlockAt(const std::shared_ptr<const NBT::CompoundTag> &section, size_t x, size_t y, size_t z) const {
return (*section->get<NBT::ByteArrayTag>("Blocks"))[getIndex(x, y, z)];
}
-uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> &section, size_t x, size_t y, size_t z) {
+uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> &section, size_t x, size_t y, size_t z) const {
size_t i = getIndex(x, y, z);
uint8_t v = (*section->get<NBT::ByteArrayTag>("Data"))[i / 2];
@@ -111,18 +125,37 @@ uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> &section,
return (v & 0xf);
}
-Chunk::Chunk(Buffer buffer) {
- size_t size = buffer.get32();
+Chunk::Blocks Chunk::getTopLayer() const {
+ size_t done = 0;
+ Blocks blocks = {};
+
+ for (auto it = sections->rbegin(); it != sections->rend(); ++it) {
+ if (done == SIZE*SIZE)
+ break;
+
+ for (ssize_t y = SIZE-1; y >= 0; y--) {
+ if (done == SIZE*SIZE)
+ break;
+
+ for (size_t x = 0; x < SIZE; x++) {
+ for (size_t z = 0; z < SIZE; z++) {
+ if (blocks.blocks[x][z].id)
+ continue;
+
+ uint8_t block = getBlockAt(*it, x, y, z);
+ if (block) {
+ blocks.blocks[x][z].id = block;
+ blocks.blocks[x][z].data = getDataAt(*it, x, y, z);
+ done++;
+ }
+ }
+ }
+ }
+ }
- Buffer buffer2(buffer.get(size), size);
+ std::cerr << "Done: " << done << std::endl;
- uint8_t format = buffer2.get8();
- if (format != 2)
- throw std::invalid_argument("unknown chunk format");
-
- inflateChunk(buffer2);
- parseChunk();
- analyzeChunk();
+ return blocks;
}
}