Merge pull request #590 from MihailRis/speed-up-block-set

Speed up block.set
This commit is contained in:
MihailRis 2025-08-16 13:56:48 +03:00 committed by GitHub
commit 6ef27faf89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 18 additions and 14 deletions

View File

@ -171,6 +171,9 @@ const Mesh<ChunkVertex>* ChunksRenderer::retrieveChunk(
if (mesh == nullptr) { if (mesh == nullptr) {
return nullptr; return nullptr;
} }
if (chunk->flags.dirtyHeights) {
chunk->updateHeights();
}
if (culling) { if (culling) {
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D); glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D);
glm::vec3 max( glm::vec3 max(

View File

@ -101,12 +101,9 @@ static int l_set(lua::State* L) {
if (static_cast<size_t>(id) >= indices->blocks.count()) { if (static_cast<size_t>(id) >= indices->blocks.count()) {
return 0; return 0;
} }
int cx = floordiv<CHUNK_W>(x); if (!blocks_agent::set(*level->chunks, x, y, z, id, int2blockstate(state))) {
int cz = floordiv<CHUNK_D>(z);
if (!blocks_agent::get_chunk(*level->chunks, cx, cz)) {
return 0; return 0;
} }
blocks_agent::set(*level->chunks, x, y, z, id, int2blockstate(state));
auto chunksController = controller->getChunksController(); auto chunksController = controller->getChunksController();
if (chunksController == nullptr) { if (chunksController == nullptr) {

View File

@ -14,6 +14,7 @@ Chunk::Chunk(int xpos, int zpos) : x(xpos), z(zpos) {
} }
void Chunk::updateHeights() { void Chunk::updateHeights() {
flags.dirtyHeights = false;
for (uint i = 0; i < CHUNK_VOL; i++) { for (uint i = 0; i < CHUNK_VOL; i++) {
if (voxels[i].id != 0) { if (voxels[i].id != 0) {
bottom = i / (CHUNK_D * CHUNK_W); bottom = i / (CHUNK_D * CHUNK_W);

View File

@ -37,6 +37,7 @@ public:
bool loadedLights : 1; bool loadedLights : 1;
bool entities : 1; bool entities : 1;
bool blocksData : 1; bool blocksData : 1;
bool dirtyHeights : 1;
} flags {}; } flags {};
/// @brief Block inventories map where key is index of block in voxels array /// @brief Block inventories map where key is index of block in voxels array

View File

@ -7,7 +7,7 @@
using namespace blocks_agent; using namespace blocks_agent;
template <class Storage> template <class Storage>
static inline void set_block( static inline bool set_block(
Storage& chunks, Storage& chunks,
int32_t x, int32_t x,
int32_t y, int32_t y,
@ -16,14 +16,14 @@ static inline void set_block(
blockstate state blockstate state
) { ) {
if (y < 0 || y >= CHUNK_H) { if (y < 0 || y >= CHUNK_H) {
return; return false;
} }
const auto& indices = chunks.getContentIndices(); const auto& indices = chunks.getContentIndices();
int cx = floordiv<CHUNK_W>(x); int cx = floordiv<CHUNK_W>(x);
int cz = floordiv<CHUNK_D>(z); int cz = floordiv<CHUNK_D>(z);
Chunk* chunk = get_chunk(chunks, cx, cz); Chunk* chunk = get_chunk(chunks, cx, cz);
if (chunk == nullptr) { if (chunk == nullptr) {
return; return false;
} }
int lx = x - cx * CHUNK_W; int lx = x - cx * CHUNK_W;
int lz = z - cz * CHUNK_D; int lz = z - cz * CHUNK_D;
@ -60,7 +60,8 @@ static inline void set_block(
else if (y + 1 > chunk->top) else if (y + 1 > chunk->top)
chunk->top = y + 1; chunk->top = y + 1;
else if (id == 0) else if (id == 0)
chunk->updateHeights(); chunk->flags.dirtyHeights = true;
if (lx == 0 && (chunk = get_chunk(chunks, cx - 1, cz))) { if (lx == 0 && (chunk = get_chunk(chunks, cx - 1, cz))) {
chunk->flags.modified = true; chunk->flags.modified = true;
@ -74,9 +75,10 @@ static inline void set_block(
if (lz == CHUNK_D - 1 && (chunk = get_chunk(chunks, cx, cz + 1))) { if (lz == CHUNK_D - 1 && (chunk = get_chunk(chunks, cx, cz + 1))) {
chunk->flags.modified = true; chunk->flags.modified = true;
} }
return true;
} }
void blocks_agent::set( bool blocks_agent::set(
Chunks& chunks, Chunks& chunks,
int32_t x, int32_t x,
int32_t y, int32_t y,
@ -84,10 +86,10 @@ void blocks_agent::set(
uint32_t id, uint32_t id,
blockstate state blockstate state
) { ) {
set_block(chunks, x, y, z, id, state); return set_block(chunks, x, y, z, id, state);
} }
void blocks_agent::set( bool blocks_agent::set(
GlobalChunks& chunks, GlobalChunks& chunks,
int32_t x, int32_t x,
int32_t y, int32_t y,
@ -95,7 +97,7 @@ void blocks_agent::set(
uint32_t id, uint32_t id,
blockstate state blockstate state
) { ) {
set_block(chunks, x, y, z, id, state); return set_block(chunks, x, y, z, id, state);
} }
template <class Storage> template <class Storage>

View File

@ -119,7 +119,7 @@ inline bool is_replaceable_at(const Storage& chunks, int32_t x, int32_t y, int32
/// @param z block position Z /// @param z block position Z
/// @param id new block id /// @param id new block id
/// @param state new block state /// @param state new block state
void set( bool set(
Chunks& chunks, Chunks& chunks,
int32_t x, int32_t x,
int32_t y, int32_t y,
@ -135,7 +135,7 @@ void set(
/// @param z block position Z /// @param z block position Z
/// @param id new block id /// @param id new block id
/// @param state new block state /// @param state new block state
void set( bool set(
GlobalChunks& chunks, GlobalChunks& chunks,
int32_t x, int32_t x,
int32_t y, int32_t y,