chunk: handle special cases for getBlocks

Returning Block::AIR when chunk is not yet generated (CHUNK_STATE_GENERATED set to false) is also a way to avoid thread-unsafe concurrent
access to the IntervalMaps data structure, since CHUNK_STATE_GENERATED is set to false before
generating the Chunk and set again to true after generation is complete
pull/4/head
EmaMaker 2023-06-01 21:20:19 +02:00
parent 32d0475dbf
commit 0ebbb897dc
1 changed files with 3 additions and 0 deletions

View File

@ -19,6 +19,7 @@ namespace Chunk
{
this->position = pos;
this->setState(CHUNK_STATE_EMPTY, true);
this->setBlocks(0, CHUNK_MAX_INDEX, Block::AIR);
}
Chunk ::~Chunk()
@ -43,6 +44,8 @@ namespace Chunk
Block Chunk::getBlock(int x, int y, int z)
{
if(x < 0 || y < 0 || z < 0 || x > CHUNK_SIZE -1 || y > CHUNK_SIZE -1 || z > CHUNK_SIZE-1 ||
!getState(CHUNK_STATE_GENERATED)) return Block::AIR;
return blocks.at(HILBERT_XYZ_ENCODE[x][y][z]);
}