Add support for Minecraft 1.13 sections

This commit is contained in:
Matthias Schiffer 2018-07-24 20:31:53 +02:00
parent 210f651807
commit b6f14f0c72
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 80 additions and 0 deletions

View file

@ -26,8 +26,11 @@
#include "Section.hpp"
#include "../NBT/ByteTag.hpp"
#include "../NBT/StringTag.hpp"
#include "../Util.hpp"
#include <cstdio>
namespace MinedMap {
namespace World {
@ -39,12 +42,20 @@ Section::Section(const std::shared_ptr<const NBT::CompoundTag> &section) {
std::unique_ptr<Section> Section::makeSection(const std::shared_ptr<const NBT::CompoundTag> &section) {
std::shared_ptr<const NBT::LongArrayTag> blockStates = section->get<NBT::LongArrayTag>("BlockStates");
if (blockStates) {
const std::shared_ptr<const NBT::ListTag> palette = assertValue(section->get<NBT::ListTag>("Palette"));
return std::unique_ptr<Section>(new PaletteSection(section, std::move(blockStates), palette));
}
std::shared_ptr<const NBT::ByteArrayTag> blocks = assertValue(section->get<NBT::ByteArrayTag>("Blocks"));
std::shared_ptr<const NBT::ByteArrayTag> data = assertValue(section->get<NBT::ByteArrayTag>("Data"));
return std::unique_ptr<Section>(new LegacySection(section, std::move(blocks), std::move(data)));
}
const Resource::BlockType * LegacySection::getBlockStateAt(size_t x, size_t y, size_t z) const {
uint8_t type = getBlockAt(x, y, z);
uint8_t data = getDataAt(x, y, z);
@ -52,5 +63,50 @@ const Resource::BlockType * LegacySection::getBlockStateAt(size_t x, size_t y, s
return Resource::LEGACY_BLOCK_TYPES.types[type][data];
}
PaletteSection::PaletteSection(
const std::shared_ptr<const NBT::CompoundTag> &section,
std::shared_ptr<const NBT::LongArrayTag> &&blockStates0,
const std::shared_ptr<const NBT::ListTag> &paletteData
) : Section(section), blockStates(blockStates0) {
if (blockStates->getLength() % 64)
throw std::invalid_argument("corrupt section data");
bits = blockStates->getLength() / 64;
if (bits > 16)
throw std::invalid_argument("unsupported block state bits");
mask = (1u << bits) - 1;
for (const auto &entry : *paletteData) {
const NBT::CompoundTag &paletteEntry = *assertValue(dynamic_cast<const NBT::CompoundTag *>(entry.get()));
std::string name = assertValue(paletteEntry.get<NBT::StringTag>("Name"))->getValue();
const Resource::BlockType *type = Resource::BlockType::lookup(name);
if (!type)
std::fprintf(stderr, "Warning: unknown block type: %s\n", name.c_str());
palette.push_back(type);
}
}
const Resource::BlockType * PaletteSection::getBlockStateAt(size_t x, size_t y, size_t z) const {
size_t index = bits * getIndex(x, y, z);
size_t pos = index >> 3;
size_t shift = index & 7;
uint32_t v = blockStates->getPointer()[mangleByteIndex(pos)];
if (shift + bits > 8)
v |= blockStates->getPointer()[mangleByteIndex(pos + 1)] << 8;
if (shift + bits > 16)
v |= blockStates->getPointer()[mangleByteIndex(pos + 2)] << 16;
/* We do not need to check for shift+bits > 24: bits should never
be greater than 12, so our value will never span more than 3 bytes */
return palette.at((v >> shift) & mask);
}
}
}

View file

@ -29,6 +29,8 @@
#include "../NBT/ByteArrayTag.hpp"
#include "../NBT/CompoundTag.hpp"
#include "../NBT/ListTag.hpp"
#include "../NBT/LongArrayTag.hpp"
#include "../Resource/BlockType.hpp"
#include <cstdint>
@ -106,5 +108,27 @@ public:
virtual const Resource::BlockType * getBlockStateAt(size_t x, size_t y, size_t z) const;
};
class PaletteSection : public Section {
private:
std::shared_ptr<const NBT::LongArrayTag> blockStates;
std::vector<const Resource::BlockType *> palette;
size_t bits;
uint16_t mask;
static size_t mangleByteIndex(size_t index) {
return (index & ~(size_t)7) + 7 - (index & 7);
}
public:
PaletteSection(
const std::shared_ptr<const NBT::CompoundTag> &section,
std::shared_ptr<const NBT::LongArrayTag> &&blockStates0,
const std::shared_ptr<const NBT::ListTag> &paletteData
);
virtual const Resource::BlockType * getBlockStateAt(size_t x, size_t y, size_t z) const;
};
}
}