mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-05 17:44:52 +01:00
World: add support for negative Y coordinates and MC 1.18 chunks
MC 1.18 biomes are not handled yet.
This commit is contained in:
parent
9ee1007ade
commit
aacea26716
5 changed files with 92 additions and 45 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <limits>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
@ -29,13 +30,15 @@ static inline float clamp(float v, float min, float max) {
|
||||||
// A block coordinate relative to a chunk/section (X/Y/Z)
|
// A block coordinate relative to a chunk/section (X/Y/Z)
|
||||||
typedef uint8_t block_idx_t;
|
typedef uint8_t block_idx_t;
|
||||||
// A section index in a chunk (Y)
|
// A section index in a chunk (Y)
|
||||||
typedef uint8_t section_idx_t;
|
typedef int8_t section_idx_t;
|
||||||
// A chunk coordinate relative to a region (X/Z)
|
// A chunk coordinate relative to a region (X/Z)
|
||||||
typedef uint8_t chunk_idx_t;
|
typedef uint8_t chunk_idx_t;
|
||||||
|
|
||||||
// A block coordinate relative to a region (X/Z)
|
// A block coordinate relative to a region (X/Z)
|
||||||
typedef uint16_t region_block_idx_t;
|
typedef uint16_t region_block_idx_t;
|
||||||
// A block coordinate (Y)
|
// A block coordinate (Y)
|
||||||
typedef uint16_t y_idx_t;
|
typedef int16_t y_idx_t;
|
||||||
|
|
||||||
|
const y_idx_t y_idx_min = std::numeric_limits<y_idx_t>::min();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,10 +18,11 @@ namespace MinedMap {
|
||||||
namespace World {
|
namespace World {
|
||||||
|
|
||||||
Chunk::Chunk(const ChunkData *data) {
|
Chunk::Chunk(const ChunkData *data) {
|
||||||
std::shared_ptr<const NBT::CompoundTag> level =
|
std::shared_ptr<const NBT::ListTag> sectionsTag;
|
||||||
assertValue(data->getRoot()->get<NBT::CompoundTag>("Level"));
|
|
||||||
|
|
||||||
std::shared_ptr<const NBT::ListTag> sectionsTag = level->get<NBT::ListTag>("Sections");
|
auto level = data->getRoot()->get<NBT::CompoundTag>("Level");
|
||||||
|
if (level) {
|
||||||
|
sectionsTag = level->get<NBT::ListTag>("Sections");
|
||||||
if (!sectionsTag || sectionsTag->empty())
|
if (!sectionsTag || sectionsTag->empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -40,6 +41,13 @@ Chunk::Chunk(const ChunkData *data) {
|
||||||
} else {
|
} else {
|
||||||
throw std::invalid_argument("corrupt biome data");
|
throw std::invalid_argument("corrupt biome data");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
sectionsTag = data->getRoot()->get<NBT::ListTag>("sections");
|
||||||
|
if (!sectionsTag || sectionsTag->empty())
|
||||||
|
return;
|
||||||
|
|
||||||
|
biomeFormat = SECTION;
|
||||||
|
}
|
||||||
|
|
||||||
auto dataVersionTag = data->getRoot()->get<NBT::IntTag>("DataVersion");
|
auto dataVersionTag = data->getRoot()->get<NBT::IntTag>("DataVersion");
|
||||||
uint32_t dataVersion = dataVersionTag ? dataVersionTag->getValue() : 0;
|
uint32_t dataVersion = dataVersionTag ? dataVersionTag->getValue() : 0;
|
||||||
|
@ -48,25 +56,34 @@ Chunk::Chunk(const ChunkData *data) {
|
||||||
auto s = std::dynamic_pointer_cast<const NBT::CompoundTag>(sTag);
|
auto s = std::dynamic_pointer_cast<const NBT::CompoundTag>(sTag);
|
||||||
std::unique_ptr<Section> section = Section::makeSection(s, dataVersion);
|
std::unique_ptr<Section> section = Section::makeSection(s, dataVersion);
|
||||||
section_idx_t Y = section->getY();
|
section_idx_t Y = section->getY();
|
||||||
|
if (sections.empty())
|
||||||
|
sectionOffset = Y;
|
||||||
|
|
||||||
|
Y -= sectionOffset;
|
||||||
|
assertValue(Y >= 0 && size_t(Y) >= sections.size());
|
||||||
sections.resize(Y);
|
sections.resize(Y);
|
||||||
sections.push_back(std::move(section));
|
sections.push_back(std::move(section));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Chunk::getBiome(block_idx_t x, y_idx_t y, block_idx_t z) const {
|
uint8_t Chunk::getBiome(block_idx_t x, y_idx_t y, block_idx_t z) const {
|
||||||
if (x > SIZE || y > MAXY || z > SIZE)
|
if (x >= SIZE || z >= SIZE)
|
||||||
throw std::invalid_argument("corrupt chunk data");
|
throw std::invalid_argument("getBiome: invalid block coordinate");
|
||||||
|
|
||||||
switch (biomeFormat) {
|
switch (biomeFormat) {
|
||||||
case INT_ARRAY:
|
case INT_ARRAY:
|
||||||
|
if (y < 0 || y >= MAXY)
|
||||||
|
break;
|
||||||
return biomeInts->getValue((y>>BSHIFT)*BSIZE*BSIZE + (z>>BSHIFT)*BSIZE + (x>>BSHIFT));
|
return biomeInts->getValue((y>>BSHIFT)*BSIZE*BSIZE + (z>>BSHIFT)*BSIZE + (x>>BSHIFT));
|
||||||
case INT_ARRAY_PRE1_15:
|
case INT_ARRAY_PRE1_15:
|
||||||
return biomeInts->getValue(z*SIZE + x);
|
return biomeInts->getValue(z*SIZE + x);
|
||||||
case BYTE_ARRAY:
|
case BYTE_ARRAY:
|
||||||
return biomeBytes->getValue(z*SIZE + x);
|
return biomeBytes->getValue(z*SIZE + x);
|
||||||
default:
|
default:
|
||||||
return 0xff;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block Chunk::getBlock(block_idx_t x, Chunk::Height height, block_idx_t z) const {
|
Block Chunk::getBlock(block_idx_t x, Chunk::Height height, block_idx_t z) const {
|
||||||
|
@ -74,16 +91,19 @@ Block Chunk::getBlock(block_idx_t x, Chunk::Height height, block_idx_t z) const
|
||||||
|
|
||||||
block.depth = height.depth;
|
block.depth = height.depth;
|
||||||
|
|
||||||
section_idx_t Y = height.y >> HSHIFT;
|
if (height.y == y_idx_min)
|
||||||
|
return block;
|
||||||
|
|
||||||
|
section_idx_t Y = (height.y >> HSHIFT) - sectionOffset;
|
||||||
block_idx_t y = height.y & HMASK;
|
block_idx_t y = height.y & HMASK;
|
||||||
|
|
||||||
if (Y < sections.size() && sections[Y])
|
if (Y >= 0 && size_t(Y) < sections.size() && sections[Y])
|
||||||
block.type = sections[Y]->getBlockStateAt(x, y, z);
|
block.type = sections[Y]->getBlockStateAt(x, y, z);
|
||||||
|
|
||||||
section_idx_t Yt = (height.y + 1) >> HSHIFT;
|
section_idx_t Yt = ((height.y + 1) >> HSHIFT) - sectionOffset;
|
||||||
block_idx_t yt = (height.y + 1) & HMASK;
|
block_idx_t yt = (height.y + 1) & HMASK;
|
||||||
|
|
||||||
if (Yt < sections.size() && sections[Yt])
|
if (Yt >= 0 && size_t(Yt) < sections.size() && sections[Yt])
|
||||||
block.blockLight = sections[Yt]->getBlockLightAt(x, yt, z);
|
block.blockLight = sections[Yt]->getBlockLightAt(x, yt, z);
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
|
@ -93,17 +113,17 @@ bool Chunk::getHeight(
|
||||||
Chunk::Height *height, const Section *section,
|
Chunk::Height *height, const Section *section,
|
||||||
block_idx_t x, block_idx_t y, block_idx_t z, int flags
|
block_idx_t x, block_idx_t y, block_idx_t z, int flags
|
||||||
) const {
|
) const {
|
||||||
if (height->depth > 0)
|
if (height->depth > y_idx_min)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (!(flags & WITH_DEPTH) && height->y > 0)
|
if (!(flags & WITH_DEPTH) && height->y > y_idx_min)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
const Resource::BlockType *type = section->getBlockStateAt(x, y, z);
|
const Resource::BlockType *type = section->getBlockStateAt(x, y, z);
|
||||||
if (!type || !(type->flags & BLOCK_OPAQUE))
|
if (!type || !(type->flags & BLOCK_OPAQUE))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (height->y == 0)
|
if (height->y == y_idx_min)
|
||||||
height->y = (section->getY() << HSHIFT) + y;
|
height->y = (section->getY() << HSHIFT) + y;
|
||||||
|
|
||||||
if (!(flags & WITH_DEPTH))
|
if (!(flags & WITH_DEPTH))
|
||||||
|
@ -119,9 +139,14 @@ bool Chunk::getHeight(
|
||||||
|
|
||||||
Chunk::Heightmap Chunk::getTopLayer(int flags) const {
|
Chunk::Heightmap Chunk::getTopLayer(int flags) const {
|
||||||
uint32_t done = 0;
|
uint32_t done = 0;
|
||||||
Heightmap ret = {};
|
Heightmap ret;
|
||||||
|
|
||||||
for (int16_t Y = sections.size() - 1; Y >= 0; Y--) {
|
for (block_idx_t z = 0; z < SIZE; z++) {
|
||||||
|
for (block_idx_t x = 0; x < SIZE; x++)
|
||||||
|
ret.v[x][z] = Height { .y = y_idx_min, .depth = y_idx_min };
|
||||||
|
}
|
||||||
|
|
||||||
|
for (section_idx_t Y = sections.size() - 1; Y >= 0; Y--) {
|
||||||
if (done == SIZE*SIZE)
|
if (done == SIZE*SIZE)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,7 @@ class Chunk {
|
||||||
public:
|
public:
|
||||||
// Number of blocks in a chunk in x/z dimensions
|
// Number of blocks in a chunk in x/z dimensions
|
||||||
static const uint32_t SIZE = Section::SIZE;
|
static const uint32_t SIZE = Section::SIZE;
|
||||||
// Maximum Y value
|
// Maximum Y value for pre-1.18 chunks
|
||||||
static const y_idx_t MAXY = 256;
|
static const y_idx_t MAXY = 256;
|
||||||
|
|
||||||
// Shift to get from height to section index
|
// Shift to get from height to section index
|
||||||
|
@ -55,6 +55,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
section_idx_t sectionOffset;
|
||||||
std::vector<std::unique_ptr<Section>> sections;
|
std::vector<std::unique_ptr<Section>> sections;
|
||||||
|
|
||||||
enum BiomeFormat {
|
enum BiomeFormat {
|
||||||
|
@ -62,6 +63,7 @@ private:
|
||||||
BYTE_ARRAY,
|
BYTE_ARRAY,
|
||||||
INT_ARRAY_PRE1_15,
|
INT_ARRAY_PRE1_15,
|
||||||
INT_ARRAY,
|
INT_ARRAY,
|
||||||
|
SECTION,
|
||||||
} biomeFormat = UNKNOWN;
|
} biomeFormat = UNKNOWN;
|
||||||
|
|
||||||
std::shared_ptr<const NBT::ByteArrayTag> biomeBytes;
|
std::shared_ptr<const NBT::ByteArrayTag> biomeBytes;
|
||||||
|
@ -73,9 +75,9 @@ private:
|
||||||
) const;
|
) const;
|
||||||
|
|
||||||
const Resource::BlockType * getBlockStateAt(block_idx_t x, y_idx_t y, block_idx_t z) const {
|
const Resource::BlockType * getBlockStateAt(block_idx_t x, y_idx_t y, block_idx_t z) const {
|
||||||
section_idx_t Y = y >> HSHIFT;
|
section_idx_t Y = (y >> HSHIFT) - sectionOffset;
|
||||||
|
|
||||||
if (Y >= sections.size() || !sections[Y])
|
if (Y < 0 || size_t(Y) >= sections.size() || !sections[Y])
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|
||||||
return sections[Y]->getBlockStateAt(x, y & HMASK, z);
|
return sections[Y]->getBlockStateAt(x, y & HMASK, z);
|
||||||
|
|
|
@ -16,7 +16,7 @@ namespace MinedMap {
|
||||||
namespace World {
|
namespace World {
|
||||||
|
|
||||||
Section::Section(const std::shared_ptr<const NBT::CompoundTag> §ion) {
|
Section::Section(const std::shared_ptr<const NBT::CompoundTag> §ion) {
|
||||||
Y = assertValue(section->get<NBT::ByteTag>("Y"))->getValue();
|
Y = int8_t(assertValue(section->get<NBT::ByteTag>("Y"))->getValue());
|
||||||
blockLight = section->get<NBT::ByteArrayTag>("BlockLight");
|
blockLight = section->get<NBT::ByteArrayTag>("BlockLight");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,19 +25,33 @@ const Resource::BlockType * Section::getBlockStateAt(block_idx_t, block_idx_t, b
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<Section> Section::makeSection(const std::shared_ptr<const NBT::CompoundTag> §ion, uint32_t dataVersion) {
|
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");
|
||||||
|
if (blockStates) {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
std::shared_ptr<const NBT::LongArrayTag> blockStates = section->get<NBT::LongArrayTag>("BlockStates");
|
std::shared_ptr<const NBT::LongArrayTag> blockStates = section->get<NBT::LongArrayTag>("BlockStates");
|
||||||
if (blockStates) {
|
if (blockStates) {
|
||||||
const std::shared_ptr<const NBT::ListTag> palette = assertValue(section->get<NBT::ListTag>("Palette"));
|
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, dataVersion));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
std::shared_ptr<const NBT::ByteArrayTag> blocks = section->get<NBT::ByteArrayTag>("Blocks");
|
std::shared_ptr<const NBT::ByteArrayTag> blocks = section->get<NBT::ByteArrayTag>("Blocks");
|
||||||
if (blocks) {
|
if (blocks) {
|
||||||
std::shared_ptr<const NBT::ByteArrayTag> data = assertValue(section->get<NBT::ByteArrayTag>("Data"));
|
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)));
|
return std::unique_ptr<Section>(new LegacySection(section, std::move(blocks), std::move(data)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return std::unique_ptr<Section>(new Section(section));
|
return std::unique_ptr<Section>(new Section(section));
|
||||||
}
|
}
|
||||||
|
@ -81,7 +95,7 @@ PaletteSection::PaletteSection(
|
||||||
expectedLength = (4096 + blocksPerWord - 1) / blocksPerWord;
|
expectedLength = (4096 + blocksPerWord - 1) / blocksPerWord;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (blockStates->getLength() != expectedLength)
|
if (blockStates && blockStates->getLength() != expectedLength)
|
||||||
throw std::invalid_argument("corrupt section data");
|
throw std::invalid_argument("corrupt section data");
|
||||||
|
|
||||||
for (const auto &entry : *paletteData) {
|
for (const auto &entry : *paletteData) {
|
||||||
|
@ -97,6 +111,9 @@ PaletteSection::PaletteSection(
|
||||||
}
|
}
|
||||||
|
|
||||||
const Resource::BlockType * PaletteSection::getBlockStateAt(block_idx_t x, block_idx_t y, block_idx_t z) const {
|
const Resource::BlockType * PaletteSection::getBlockStateAt(block_idx_t x, block_idx_t y, block_idx_t z) const {
|
||||||
|
if (!blockStates)
|
||||||
|
return palette.at(0);
|
||||||
|
|
||||||
size_t index = getIndex(x, y, z);
|
size_t index = getIndex(x, y, z);
|
||||||
size_t bitIndex;
|
size_t bitIndex;
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ int main(int argc, char *argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
World::Region::visitChunks(argv[1], [&] (chunk_idx_t X, chunk_idx_t Z, const World::ChunkData *chunk) {
|
World::Region::visitChunks(argv[1], [&] (chunk_idx_t X, chunk_idx_t Z, const World::ChunkData *chunk) {
|
||||||
std::cout << "Chunk(" << X << ", " << Z << "): "
|
std::cout << "Chunk(" << unsigned(X) << ", " << unsigned(Z) << "): "
|
||||||
<< *chunk->getRoot() << std::endl;
|
<< *chunk->getRoot() << std::endl;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue