Support > 4 bitsPerBlock

This commit is contained in:
Joshua Ashton 2020-08-13 15:54:57 +01:00
parent f6bb87ab2b
commit 71f78fa89c
4 changed files with 17 additions and 25 deletions

View File

@ -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<int16_t>(1024);
const size_t bitsPerBlock = 4;
const uint8_t bitsPerBlock = sect.bitsPerBlock;
sections.Write<uint8_t>(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<uint64_t>(sect.blockStates[i]);
}
Log::Trace("Section bits: {:#b}", sectionsBits);

View File

@ -46,7 +46,7 @@ namespace Feather
return m_IDtoValue.at(id);
}
inline int Size() const
inline size_t Size() const
{
return m_IDtoValue.size();
}

View File

@ -49,7 +49,7 @@ namespace Feather
ListTag<CompoundTag> palette = sectData.GetList<CompoundTag>("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<StringTag>("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++;
}

View File

@ -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<uint64_t, MaximumLongCount> blockStates;
// TODO: this should eventually be Palette<BlockState>
// Palette of local indexs to indexs in the global palette
IDMapper<int> localPalette;
uint8_t bitsPerBlock;
};
// Chunk: 16x256x16 world chunk