mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-04-20 19:35:08 +02:00
Further chunk parsing
This commit is contained in:
parent
d922636311
commit
18ed3d4c5d
2 changed files with 34 additions and 5 deletions
|
@ -27,6 +27,7 @@
|
||||||
#include "Chunk.hpp"
|
#include "Chunk.hpp"
|
||||||
#include "../Util.hpp"
|
#include "../Util.hpp"
|
||||||
#include "../NBT/ByteTag.hpp"
|
#include "../NBT/ByteTag.hpp"
|
||||||
|
#include "../NBT/ByteArrayTag.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
@ -82,15 +83,32 @@ void Chunk::parseChunk() {
|
||||||
if (!(*this) || tag.first != "")
|
if (!(*this) || tag.first != "")
|
||||||
throw std::invalid_argument("invalid root tag");
|
throw std::invalid_argument("invalid root tag");
|
||||||
|
|
||||||
sections = (*this)->get<const NBT::ListTag<NBT::CompoundTag>>("Level", "Sections");
|
level = assertValue((*this)->get<NBT::CompoundTag>("Level"));
|
||||||
if (!sections)
|
sections = assertValue(level->get<NBT::ListTag<NBT::CompoundTag>>("Sections"));
|
||||||
throw std::invalid_argument("no sections found");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunk::analyzeChunk() {
|
void Chunk::analyzeChunk() {
|
||||||
maxY = (assertValue(sections->back()->get<NBT::ByteTag>("Y"))->getValue() + 1) * SIZE;
|
maxY = (assertValue(sections->back()->get<NBT::ByteTag>("Y"))->getValue() + 1) * SIZE;
|
||||||
|
|
||||||
std::cerr << "maxY: " << maxY << std::endl;
|
for (auto §ion : *sections) {
|
||||||
|
if (assertValue(section->get<NBT::ByteArrayTag>("Blocks"))->getLength() != SIZE*SIZE*SIZE
|
||||||
|
|| assertValue(section->get<NBT::ByteArrayTag>("Data"))->getLength() != SIZE*SIZE*SIZE/2)
|
||||||
|
throw std::invalid_argument("corrupt chunk data");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Chunk::getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) {
|
||||||
|
return (*section->get<NBT::ByteArrayTag>("Blocks"))[getIndex(x, y, z)];
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) {
|
||||||
|
size_t i = getIndex(x, y, z);
|
||||||
|
uint8_t v = (*section->get<NBT::ByteArrayTag>("Data"))[i / 2];
|
||||||
|
|
||||||
|
if (i % 2)
|
||||||
|
return (v >> 4);
|
||||||
|
else
|
||||||
|
return (v & 0xf);
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::Chunk(uint8_t *buffer, size_t buflen) {
|
Chunk::Chunk(uint8_t *buffer, size_t buflen) {
|
||||||
|
|
|
@ -46,13 +46,24 @@ private:
|
||||||
size_t len;
|
size_t len;
|
||||||
UniqueCPtr<uint8_t[]> data;
|
UniqueCPtr<uint8_t[]> data;
|
||||||
|
|
||||||
|
std::shared_ptr<const NBT::CompoundTag> level;
|
||||||
|
std::shared_ptr<const NBT::ListTag<NBT::CompoundTag>> sections;
|
||||||
|
|
||||||
unsigned maxY;
|
unsigned maxY;
|
||||||
|
|
||||||
void inflateChunk(uint8_t *data, size_t len);
|
void inflateChunk(uint8_t *data, size_t len);
|
||||||
void parseChunk();
|
void parseChunk();
|
||||||
void analyzeChunk();
|
void analyzeChunk();
|
||||||
|
|
||||||
std::shared_ptr<const NBT::ListTag<NBT::CompoundTag>> sections;
|
size_t getIndex(size_t x, size_t y, size_t z) {
|
||||||
|
if (x >= SIZE || y >= SIZE || z >= SIZE)
|
||||||
|
throw std::range_error("Chunk::getIndex(): bad coordinates");
|
||||||
|
|
||||||
|
return 256*y + 16*z + x;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z);
|
||||||
|
uint8_t getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Chunk(uint8_t *buffer, size_t buflen);
|
Chunk(uint8_t *buffer, size_t buflen);
|
||||||
|
|
Loading…
Add table
Reference in a new issue