mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
World: Section: implement new section-based biome data format
This commit is contained in:
parent
76e5d322b1
commit
baa20494bf
4 changed files with 113 additions and 8 deletions
|
@ -79,6 +79,14 @@ uint8_t Chunk::getBiome(block_idx_t x, y_idx_t y, block_idx_t z) const {
|
|||
return biomeInts->getValue(z*SIZE + x);
|
||||
case BYTE_ARRAY:
|
||||
return biomeBytes->getValue(z*SIZE + x);
|
||||
case SECTION: {
|
||||
section_idx_t Y = (y >> HSHIFT) - sectionOffset;
|
||||
|
||||
if (Y < 0 || size_t(Y) >= sections.size() || !sections[Y])
|
||||
break;
|
||||
|
||||
return sections[Y]->getBiomeAt(x, y & HMASK, z);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
|
|
@ -36,9 +36,9 @@ public:
|
|||
|
||||
// Since Minecraft 1.15, biome information is stored for
|
||||
// 4x4x4 block groups
|
||||
static const unsigned BSHIFT = 2;
|
||||
static const unsigned BSHIFT = Section::BSHIFT;
|
||||
// Number of biome values in a chunk in x/z dimensions
|
||||
static const uint32_t BSIZE = SIZE >> BSHIFT;
|
||||
static const uint32_t BSIZE = Section::BSIZE;
|
||||
// Number of biome values in a chunk in y dimension
|
||||
static const uint32_t BMAXY = MAXY >> BSHIFT;
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
|
||||
#include "Section.hpp"
|
||||
#include "../Resource/Biome.hpp"
|
||||
#include "../NBT/ByteTag.hpp"
|
||||
#include "../NBT/StringTag.hpp"
|
||||
|
||||
|
@ -24,6 +25,10 @@ const Resource::BlockType * Section::getBlockStateAt(block_idx_t, block_idx_t, b
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
uint8_t Section::getBiomeAt(block_idx_t x, block_idx_t y, block_idx_t z) const {
|
||||
return 0xff;
|
||||
}
|
||||
|
||||
std::unique_ptr<Section> Section::makeSection(const std::shared_ptr<const NBT::CompoundTag> §ion, uint32_t dataVersion) {
|
||||
{
|
||||
const std::shared_ptr<const NBT::CompoundTag> blockStates = section->get<NBT::CompoundTag>("block_states");
|
||||
|
@ -31,7 +36,13 @@ std::unique_ptr<Section> Section::makeSection(const std::shared_ptr<const NBT::C
|
|||
const std::shared_ptr<const NBT::ListTag> palette = assertValue(blockStates->get<NBT::ListTag>("palette"));
|
||||
std::shared_ptr<const NBT::LongArrayTag> data = blockStates->get<NBT::LongArrayTag>("data");
|
||||
|
||||
return std::unique_ptr<Section>(new PaletteSection(section, std::move(data), palette, dataVersion));
|
||||
const std::shared_ptr<const NBT::CompoundTag> biomes = assertValue(section->get<NBT::CompoundTag>("biomes"));
|
||||
const std::shared_ptr<const NBT::ListTag> biomePalette = assertValue(biomes->get<NBT::ListTag>("palette"));
|
||||
std::shared_ptr<const NBT::LongArrayTag> biomeData = biomes->get<NBT::LongArrayTag>("data");
|
||||
|
||||
return std::unique_ptr<Section>(new PaletteSection(
|
||||
section, std::move(data), palette, std::move(biomeData), biomePalette, dataVersion
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -40,7 +51,10 @@ std::unique_ptr<Section> Section::makeSection(const std::shared_ptr<const NBT::C
|
|||
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, dataVersion));
|
||||
return std::unique_ptr<Section>(new PaletteSection(
|
||||
section, std::move(blockStates), palette,
|
||||
std::shared_ptr<const NBT::LongArrayTag>(), std::shared_ptr<const NBT::ListTag>(), dataVersion
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -76,8 +90,10 @@ PaletteSection::PaletteSection(
|
|||
const std::shared_ptr<const NBT::CompoundTag> §ion,
|
||||
std::shared_ptr<const NBT::LongArrayTag> &&blockStates0,
|
||||
const std::shared_ptr<const NBT::ListTag> &paletteData,
|
||||
std::shared_ptr<const NBT::LongArrayTag> &&biomes0,
|
||||
const std::shared_ptr<const NBT::ListTag> &biomePaletteData,
|
||||
uint32_t dataVersion0
|
||||
) : Section(section), blockStates(blockStates0), dataVersion(dataVersion0) {
|
||||
) : Section(section), blockStates(blockStates0), biomes(biomes0), dataVersion(dataVersion0) {
|
||||
bits = 4;
|
||||
while ((1u << bits) < paletteData->size()) {
|
||||
bits++;
|
||||
|
@ -86,6 +102,16 @@ PaletteSection::PaletteSection(
|
|||
throw std::invalid_argument("unsupported block palette size");
|
||||
}
|
||||
|
||||
biomeBits = 1;
|
||||
if (biomePaletteData) {
|
||||
while ((1u << biomeBits) < biomePaletteData->size()) {
|
||||
biomeBits++;
|
||||
|
||||
if (bits > 6)
|
||||
throw std::invalid_argument("unsupported biome palette size");
|
||||
}
|
||||
}
|
||||
|
||||
unsigned expectedLength;
|
||||
|
||||
if (dataVersion < 2529) {
|
||||
|
@ -96,7 +122,13 @@ PaletteSection::PaletteSection(
|
|||
}
|
||||
|
||||
if (blockStates && blockStates->getLength() != expectedLength)
|
||||
throw std::invalid_argument("corrupt section data");
|
||||
throw std::invalid_argument("corrupt section block data");
|
||||
|
||||
unsigned biomesPerWord = (64 / biomeBits);
|
||||
unsigned expectedBiomeLength = (64 + biomesPerWord - 1) / biomesPerWord;
|
||||
|
||||
if (biomes && biomes->getLength() != expectedBiomeLength)
|
||||
throw std::invalid_argument("corrupt section biome data");
|
||||
|
||||
for (const auto &entry : *paletteData) {
|
||||
const NBT::CompoundTag &paletteEntry = *assertValue(dynamic_cast<const NBT::CompoundTag *>(entry.get()));
|
||||
|
@ -108,6 +140,25 @@ PaletteSection::PaletteSection(
|
|||
|
||||
palette.push_back(type);
|
||||
}
|
||||
|
||||
if (biomePaletteData) {
|
||||
for (const auto &entry : *biomePaletteData) {
|
||||
const NBT::StringTag &paletteEntry =
|
||||
*assertValue(dynamic_cast<const NBT::StringTag *>(entry.get()));
|
||||
std::string name = paletteEntry.getValue();
|
||||
|
||||
auto it = Resource::Biome::Names.find(name);
|
||||
uint8_t biome;
|
||||
if (it != Resource::Biome::Names.end()) {
|
||||
biome = it->second;
|
||||
} else {
|
||||
std::fprintf(stderr, "Warning: unknown biome: %s\n", name.c_str());
|
||||
biome = 0xff;
|
||||
}
|
||||
|
||||
biomePalette.push_back(biome);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Resource::BlockType * PaletteSection::getBlockStateAt(block_idx_t x, block_idx_t y, block_idx_t z) const {
|
||||
|
@ -141,5 +192,29 @@ const Resource::BlockType * PaletteSection::getBlockStateAt(block_idx_t x, block
|
|||
return palette.at((v >> shift) & mask);
|
||||
}
|
||||
|
||||
uint8_t PaletteSection::getBiomeAt(block_idx_t x, block_idx_t y, block_idx_t z) const {
|
||||
if (!biomes)
|
||||
return biomePalette.at(0);
|
||||
|
||||
size_t index = getBiomeIndex(x, y, z);
|
||||
|
||||
unsigned biomesPerWord = (64 / biomeBits);
|
||||
size_t word = index / biomesPerWord;
|
||||
size_t bitIndex = 64 * word + biomeBits * (index % biomesPerWord);
|
||||
|
||||
size_t pos = bitIndex >> 3;
|
||||
unsigned shift = bitIndex & 7;
|
||||
uint16_t mask = (1u << biomeBits) - 1;
|
||||
|
||||
uint32_t v = biomes->getPointer()[mangleByteIndex(pos)];
|
||||
|
||||
if (shift + biomeBits > 8)
|
||||
v |= biomes->getPointer()[mangleByteIndex(pos + 1)] << 8;
|
||||
/* We do not need to check for shift+bits > 16: biomeBits should never
|
||||
be greater than 6, so our value will never span more than 2 bytes */
|
||||
|
||||
return biomePalette.at((v >> shift) & mask);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,13 @@ public:
|
|||
// Number of blocks in a section in each dimension
|
||||
static const uint32_t SIZE = 16;
|
||||
|
||||
// Since Minecraft 1.15, biome information is stored for
|
||||
// 4x4x4 block groups
|
||||
static const unsigned BSHIFT = 2;
|
||||
// Number of biome values in a chunk in x/z dimensions
|
||||
static const uint32_t BSIZE = SIZE >> BSHIFT;
|
||||
|
||||
|
||||
private:
|
||||
section_idx_t Y;
|
||||
std::shared_ptr<const NBT::ByteArrayTag> blockLight;
|
||||
|
@ -34,11 +41,18 @@ private:
|
|||
protected:
|
||||
static size_t getIndex(block_idx_t x, block_idx_t y, block_idx_t z) {
|
||||
if (x >= SIZE || y >= SIZE || z >= SIZE)
|
||||
throw std::range_error("Chunk::getIndex(): bad coordinates");
|
||||
throw std::range_error("Section::getIndex(): bad coordinates");
|
||||
|
||||
return SIZE*SIZE*y + SIZE*z + x;
|
||||
}
|
||||
|
||||
static size_t getBiomeIndex(block_idx_t x, block_idx_t y, block_idx_t z) {
|
||||
if (x >= SIZE || y >= SIZE || z >= SIZE)
|
||||
throw std::range_error("Section::getBiomeIndex(): bad coordinates");
|
||||
|
||||
return BSIZE*BSIZE*(y>>BSHIFT) + BSIZE*(z>>BSHIFT) + (x>>BSHIFT);
|
||||
}
|
||||
|
||||
static uint8_t getHalf(const uint8_t *v, block_idx_t x, block_idx_t y, block_idx_t z) {
|
||||
size_t i = getIndex(x, y, z);
|
||||
|
||||
|
@ -56,6 +70,7 @@ public:
|
|||
section_idx_t getY() const { return Y; };
|
||||
|
||||
virtual const Resource::BlockType * getBlockStateAt(block_idx_t x, block_idx_t y, block_idx_t z) const;
|
||||
virtual uint8_t getBiomeAt(block_idx_t x, block_idx_t y, block_idx_t z) const;
|
||||
|
||||
uint8_t getBlockLightAt(block_idx_t x, block_idx_t y, block_idx_t z) const {
|
||||
if (!blockLight)
|
||||
|
@ -95,9 +110,13 @@ class PaletteSection : public Section {
|
|||
private:
|
||||
std::shared_ptr<const NBT::LongArrayTag> blockStates;
|
||||
std::vector<const Resource::BlockType *> palette;
|
||||
uint32_t dataVersion;
|
||||
unsigned bits;
|
||||
|
||||
std::shared_ptr<const NBT::LongArrayTag> biomes;
|
||||
std::vector<uint8_t> biomePalette;
|
||||
unsigned biomeBits;
|
||||
|
||||
uint32_t dataVersion;
|
||||
|
||||
static const Resource::BlockType * lookup(const std::string &name, uint32_t dataVersion);
|
||||
|
||||
|
@ -110,10 +129,13 @@ public:
|
|||
const std::shared_ptr<const NBT::CompoundTag> §ion,
|
||||
std::shared_ptr<const NBT::LongArrayTag> &&blockStates0,
|
||||
const std::shared_ptr<const NBT::ListTag> &paletteData,
|
||||
std::shared_ptr<const NBT::LongArrayTag> &&biomes0,
|
||||
const std::shared_ptr<const NBT::ListTag> &biomePaletteData,
|
||||
uint32_t dataVersion0
|
||||
);
|
||||
|
||||
virtual const Resource::BlockType * getBlockStateAt(block_idx_t x, block_idx_t y, block_idx_t z) const;
|
||||
virtual uint8_t getBiomeAt(block_idx_t x, block_idx_t y, block_idx_t z) const;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue