From 0ebbb897dcde55addb5b65d4b2c88d852496cdbd Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Thu, 1 Jun 2023 21:20:19 +0200 Subject: [PATCH] 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 --- src/chunk.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/chunk.cpp b/src/chunk.cpp index 4f98a8d..66feb49 100644 --- a/src/chunk.cpp +++ b/src/chunk.cpp @@ -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]); }