From 71f78fa89cd29843fb7e186f9744d8a2c4adc5f2 Mon Sep 17 00:00:00 2001 From: Joshua Ashton Date: Thu, 13 Aug 2020 15:54:57 +0100 Subject: [PATCH] Support > 4 bitsPerBlock --- src/DedicatedServer.cpp | 14 ++++++-------- src/data/IDMapper.h | 2 +- src/world/Chunk.cpp | 13 +++++-------- src/world/Chunk.h | 13 +++++-------- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/DedicatedServer.cpp b/src/DedicatedServer.cpp index d55dcbd..015e142 100644 --- a/src/DedicatedServer.cpp +++ b/src/DedicatedServer.cpp @@ -217,11 +217,7 @@ namespace Feather for (int i = 0; i < Chunk::NUM_SECTIONS; i++) { - ChunkSection sect = chunk->sections[i]; - - // Don't support bitsPerBlock != 4 yet! - if (i == 4) - continue; + const ChunkSection& sect = chunk->sections[i]; Log::Trace("Chunk: Got Section {} with local palette size {}", i, sect.localPalette.Size()); @@ -230,7 +226,7 @@ namespace Feather // Block Count: fudge sections.Write(1024); - const size_t bitsPerBlock = 4; + const uint8_t bitsPerBlock = sect.bitsPerBlock; sections.Write(bitsPerBlock); const size_t dataLength = (16 * 16 * 16) * bitsPerBlock / 64; @@ -240,12 +236,14 @@ namespace Feather // Palette sections.WriteVarInt(sect.localPalette.Size()); - for (int j = 0; j < sect.localPalette.Size(); j++) + for (size_t j = 0; j < sect.localPalette.Size(); j++) sections.WriteVarInt(sect.localPalette.ByID(j)); } sections.WriteVarInt(dataLength); - sections.WriteData(sect.blockStates, dataLength * sizeof(int64_t)); + // Can't just copy because endian-ness! + for (size_t i = 0; i < dataLength; i++) + sections.Write(sect.blockStates[i]); } Log::Trace("Section bits: {:#b}", sectionsBits); diff --git a/src/data/IDMapper.h b/src/data/IDMapper.h index 3103578..93db410 100644 --- a/src/data/IDMapper.h +++ b/src/data/IDMapper.h @@ -46,7 +46,7 @@ namespace Feather return m_IDtoValue.at(id); } - inline int Size() const + inline size_t Size() const { return m_IDtoValue.size(); } diff --git a/src/world/Chunk.cpp b/src/world/Chunk.cpp index b546f42..35bf153 100644 --- a/src/world/Chunk.cpp +++ b/src/world/Chunk.cpp @@ -49,7 +49,7 @@ namespace Feather ListTag palette = sectData.GetList("Palette"); - for (int i = 0; i < palette.GetLength(); i++) + for (size_t i = 0; i < palette.GetLength(); i++) { CompoundTag state = palette[i]; const char* name = state.Get("Name").GetValue(); @@ -82,15 +82,12 @@ namespace Feather continue; } - int len = blockStates.GetLength(); - if (len != ChunkSection::NUM_LONGS) { - Log::Warn("Section {} has {} longs, expected {}", y, len, ChunkSection::NUM_LONGS); - } + assert(blockStates.GetLength() < ChunkSection::MaximumLongCount); - for (int i = 0; i < blockStates.GetLength() && i < ChunkSection::NUM_LONGS; i++) - { + for (size_t i = 0; i < blockStates.GetLength(); i++) section->blockStates[i] = blockStates[i]; - } + + section->bitsPerBlock = (blockStates.GetLength() * 64) / (16 * 16 * 16); n++; } diff --git a/src/world/Chunk.h b/src/world/Chunk.h index 0c0efc8..4928e6e 100644 --- a/src/world/Chunk.h +++ b/src/world/Chunk.h @@ -12,20 +12,17 @@ namespace Feather { // 16x16x16 sections of a chunk - class ChunkSection + struct ChunkSection { - public: - // 4 bits per block - // 16^3 = 4096 blocks total - // 4096 * 4 = 16384 bits = 256x int64 - // 16 blocks per int64 - static constexpr int NUM_LONGS = 256; + static constexpr size_t MaximumLongCount = (16 * 16 * 16) * 14 / 64; - int64_t blockStates[NUM_LONGS]; + std::array blockStates; // TODO: this should eventually be Palette // Palette of local indexs to indexs in the global palette IDMapper localPalette; + + uint8_t bitsPerBlock; }; // Chunk: 16x256x16 world chunk