From b6f14f0c72ecbc2bc255861bbb3982352177eccb Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 24 Jul 2018 20:31:53 +0200 Subject: Add support for Minecraft 1.13 sections --- src/World/Section.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/World/Section.hpp | 24 ++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/src/World/Section.cpp b/src/World/Section.cpp index 6f06435..fbf1c33 100644 --- a/src/World/Section.cpp +++ b/src/World/Section.cpp @@ -26,8 +26,11 @@ #include "Section.hpp" #include "../NBT/ByteTag.hpp" +#include "../NBT/StringTag.hpp" #include "../Util.hpp" +#include + namespace MinedMap { namespace World { @@ -39,12 +42,20 @@ Section::Section(const std::shared_ptr §ion) { std::unique_ptr
Section::makeSection(const std::shared_ptr §ion) { + std::shared_ptr blockStates = section->get("BlockStates"); + if (blockStates) { + const std::shared_ptr palette = assertValue(section->get("Palette")); + + return std::unique_ptr
(new PaletteSection(section, std::move(blockStates), palette)); + } + std::shared_ptr blocks = assertValue(section->get("Blocks")); std::shared_ptr data = assertValue(section->get("Data")); return std::unique_ptr
(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 §ion, + std::shared_ptr &&blockStates0, + const std::shared_ptr &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(entry.get())); + std::string name = assertValue(paletteEntry.get("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); +} + } } diff --git a/src/World/Section.hpp b/src/World/Section.hpp index a4686e3..3444bce 100644 --- a/src/World/Section.hpp +++ b/src/World/Section.hpp @@ -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 @@ -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 blockStates; + std::vector 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 §ion, + std::shared_ptr &&blockStates0, + const std::shared_ptr &paletteData + ); + + virtual const Resource::BlockType * getBlockStateAt(size_t x, size_t y, size_t z) const; +}; + } } -- cgit v1.2.3