Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save extremeheat/cf9c950e6153e0e53d674bb298013f78 to your computer and use it in GitHub Desktop.
Save extremeheat/cf9c950e6153e0e53d674bb298013f78 to your computer and use it in GitHub Desktop.
C++ class for reading/writing of new MCPE 1.3 paletted blocks - https://gist.github.com/Tomcc/a96af509e275b1af483b25c543cfbf37
template <unsigned int NBitsPerBlock, typename Word = unsigned int>
struct PalettedBlockStateStorage {
static const int wordByteSize = sizeof(Word)/*4*/;
static const int wordBitSize = wordByteSize * 8;
static const byte blocksPerWord = NAIVE_FLOOR(wordBitSize / NBitsPerBlock);
static const byte paddingPerWord = wordBitSize % NBitsPerBlock;
static const int wordsCount = NAIVE_CEIL(4096.0 / blocksPerWord);
Word blockArrayI[wordsCount];
constexpr int readBits(const Word &what, int bitoff) {
return (what >> bitoff) & ((1 << NBitsPerBlock) - 1);
}
constexpr void writeBits(Word &what, int bitoff, int data) {
what &= ~(((1 << NBitsPerBlock) - 1) << bitoff);
what |= (data & ((1 << NBitsPerBlock) - 1)) << bitoff;
}
constexpr void getIndex(byte x, byte y, byte z, int &index, int &offset) {
// safety checks
x = x & 0xf;
y = y & 0xf;
z = z & 0xf;
index = floor(((x << 8) | (z << 4) | y) / blocksPerWord);
offset = (((x << 8) | (z << 4) | y) % blocksPerWord) * NBitsPerBlock;
}
constexpr int getBlockStateAt(byte x, byte y, byte z) {
int index, offset;
getIndex(x, y, z, index, offset);
return readBits(blockArrayI[index], offset);
}
constexpr void setBlockStateAt(byte x, byte y, byte z, int data) {
int index, offset;
getIndex(x, y, z, index, offset);
writeBits(blockArrayI[index], offset, data);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment