World: Chunk: replace division/modulo with shift/mask for height values

Preparation for negative y coordinates.
This commit is contained in:
Matthias Schiffer 2021-11-17 13:16:00 +01:00
parent 457e993c92
commit 0f6c467566
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 17 additions and 12 deletions

View file

@ -53,7 +53,7 @@ uint8_t Chunk::getBiome(block_idx_t x, y_idx_t y, block_idx_t z) const {
throw std::invalid_argument("corrupt chunk data"); throw std::invalid_argument("corrupt chunk data");
if (biomeInts) if (biomeInts)
return biomeInts->getValue((y/BGROUP)*BSIZE*BSIZE + (z/BGROUP)*BSIZE + (x/BGROUP)); return biomeInts->getValue((y>>BSHIFT)*BSIZE*BSIZE + (z>>BSHIFT)*BSIZE + (x>>BSHIFT));
else if (biomeIntsPre115) else if (biomeIntsPre115)
return biomeIntsPre115->getValue(z*SIZE + x); return biomeIntsPre115->getValue(z*SIZE + x);
else if (biomeBytes) else if (biomeBytes)
@ -67,14 +67,14 @@ 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 / SIZE; section_idx_t Y = height.y >> HSHIFT;
block_idx_t y = height.y % SIZE; block_idx_t y = height.y & HMASK;
if (Y < sections.size() && sections[Y]) if (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) / SIZE; section_idx_t Yt = (height.y + 1) >> HSHIFT;
block_idx_t yt = (height.y + 1) % SIZE; block_idx_t yt = (height.y + 1) & HMASK;
if (Yt < sections.size() && sections[Yt]) if (Yt < sections.size() && sections[Yt])
block.blockLight = sections[Yt]->getBlockLightAt(x, yt, z); block.blockLight = sections[Yt]->getBlockLightAt(x, yt, z);
@ -97,7 +97,7 @@ bool Chunk::getHeight(
return false; return false;
if (height->y == 0) if (height->y == 0)
height->y = SIZE*section->getY() + y; height->y = (section->getY() << HSHIFT) + y;
if (!(flags & WITH_DEPTH)) if (!(flags & WITH_DEPTH))
return true; return true;
@ -105,7 +105,7 @@ bool Chunk::getHeight(
if (type->flags & BLOCK_WATER) if (type->flags & BLOCK_WATER)
return false; return false;
height->depth = SIZE*section->getY() + y; height->depth = (section->getY() << HSHIFT) + y;
return true; return true;
} }

View file

@ -29,13 +29,18 @@ public:
// Maximum Y value // Maximum Y value
static const y_idx_t MAXY = 256; static const y_idx_t MAXY = 256;
// Shift to get from height to section index
static const unsigned HSHIFT = 4;
// Mask to get from height to y index inside section
static const block_idx_t HMASK = 0xf;
// Since Minecraft 1.15, biome information is stored for // Since Minecraft 1.15, biome information is stored for
// 4x4x4 block groups // 4x4x4 block groups
static const uint32_t BGROUP = 4; static const unsigned BSHIFT = 2;
// Number of biome values in a chunk in x/z dimensions // Number of biome values in a chunk in x/z dimensions
static const uint32_t BSIZE = SIZE / BGROUP; static const uint32_t BSIZE = SIZE >> BSHIFT;
// Number of biome values in a chunk in y dimension // Number of biome values in a chunk in y dimension
static const uint32_t BMAXY = MAXY / BGROUP; static const uint32_t BMAXY = MAXY >> BSHIFT;
// Flags // Flags
static const int WITH_DEPTH = (1 << 0); static const int WITH_DEPTH = (1 << 0);
@ -63,12 +68,12 @@ 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 / SIZE; section_idx_t Y = y >> HSHIFT;
if (Y >= sections.size() || !sections[Y]) if (Y >= sections.size() || !sections[Y])
return nullptr; return nullptr;
return sections[Y]->getBlockStateAt(x, y % SIZE, z); return sections[Y]->getBlockStateAt(x, y & HMASK, z);
} }