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++) for (int i = 0; i < Chunk::NUM_SECTIONS; i++)
{ {
ChunkSection sect = chunk->sections[i]; const ChunkSection& sect = chunk->sections[i];
// Don't support bitsPerBlock != 4 yet!
if (i == 4)
continue;
Log::Trace("Chunk: Got Section {} with local palette size {}", i, sect.localPalette.Size()); Log::Trace("Chunk: Got Section {} with local palette size {}", i, sect.localPalette.Size());
@ -230,7 +226,7 @@ namespace Feather
// Block Count: fudge // Block Count: fudge
sections.Write<int16_t>(1024); sections.Write<int16_t>(1024);
const size_t bitsPerBlock = 4; const uint8_t bitsPerBlock = sect.bitsPerBlock;
sections.Write<uint8_t>(bitsPerBlock); sections.Write<uint8_t>(bitsPerBlock);
const size_t dataLength = (16 * 16 * 16) * bitsPerBlock / 64; const size_t dataLength = (16 * 16 * 16) * bitsPerBlock / 64;
@ -240,12 +236,14 @@ namespace Feather
// Palette // Palette
sections.WriteVarInt(sect.localPalette.Size()); 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(sect.localPalette.ByID(j));
} }
sections.WriteVarInt(dataLength); 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); Log::Trace("Section bits: {:#b}", sectionsBits);

View File

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

View File

@ -49,7 +49,7 @@ namespace Feather
ListTag<CompoundTag> palette = sectData.GetList<CompoundTag>("Palette"); 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]; CompoundTag state = palette[i];
const char* name = state.Get<StringTag>("Name").GetValue(); const char* name = state.Get<StringTag>("Name").GetValue();
@ -82,15 +82,12 @@ namespace Feather
continue; continue;
} }
int len = blockStates.GetLength(); assert(blockStates.GetLength() < ChunkSection::MaximumLongCount);
if (len != ChunkSection::NUM_LONGS) {
Log::Warn("Section {} has {} longs, expected {}", y, len, ChunkSection::NUM_LONGS);
}
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->blockStates[i] = blockStates[i];
}
section->bitsPerBlock = (blockStates.GetLength() * 64) / (16 * 16 * 16);
n++; n++;
} }

View File

@ -12,20 +12,17 @@
namespace Feather namespace Feather
{ {
// 16x16x16 sections of a chunk // 16x16x16 sections of a chunk
class ChunkSection struct ChunkSection
{ {
public: static constexpr size_t MaximumLongCount = (16 * 16 * 16) * 14 / 64;
// 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;
int64_t blockStates[NUM_LONGS]; std::array<uint64_t, MaximumLongCount> blockStates;
// TODO: this should eventually be Palette<BlockState> // TODO: this should eventually be Palette<BlockState>
// Palette of local indexs to indexs in the global palette // Palette of local indexs to indexs in the global palette
IDMapper<int> localPalette; IDMapper<int> localPalette;
uint8_t bitsPerBlock;
}; };
// Chunk: 16x256x16 world chunk // Chunk: 16x256x16 world chunk