From d7316f27f02c8dbf7f472371107e535388a9d459 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Fri, 27 Feb 2026 22:36:22 +0300 Subject: [PATCH] fix blocks random tick pattern --- src/logic/BlocksController.cpp | 38 ++++++++++++++++++++++++++-------- src/logic/BlocksController.hpp | 4 +--- 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 1e3ad2441..2de34442f 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -16,6 +16,11 @@ #include "world/World.hpp" #include "objects/Player.hpp" #include "objects/Players.hpp" +#include "util/random.hpp" + +#include + +static inline constexpr int CHUNK_RANDOM_TICK_SEGMENTS = 4; BlocksController::BlocksController(const Level& level, Lighting* lighting) : level(level), @@ -143,20 +148,36 @@ void BlocksController::onBlocksTick(int tickid, int parts) { } void BlocksController::randomTick( - const Chunk& chunk, int segments, const ContentIndices* indices + const Chunk& chunk, const ContentIndices* indices ) { + const int segments = CHUNK_RANDOM_TICK_SEGMENTS; const int segheight = CHUNK_H / segments; + static std::array randomPattern; + static bool randomPatternInitialized = false; + if (!randomPatternInitialized) { + randomPatternInitialized = true; + for (int i = 0; i < randomPattern.size(); i++) { + randomPattern[i] = i; + } + auto randomDevice = std::random_device{}; + auto randomEngine = + util::seeded_random_engine(randomDevice); + std::shuffle(randomPattern.begin(), randomPattern.end(), randomEngine); + } + for (int s = 0; s < segments; s++) { - for (int i = 0; i < 4; i++) { + int repetions = 4; + for (int i = 0; i < repetions; i++) { int segmentY = s * segheight; - if (segmentY > chunk.top) { + if (segmentY > chunk.top) { break; } - int bx = random.rand() % CHUNK_W; - int by = random.rand() % segheight + segmentY; - int bz = random.rand() % CHUNK_D; - const voxel& vox = chunk.voxels[vox_index(bx, by, bz)]; + size_t index = randomPattern[(s * repetions + i + randomTickId) % randomPattern.size()]; + int bx = index % CHUNK_W; + int bz = (index / CHUNK_W) % CHUNK_D; + int by = (index / (CHUNK_W * CHUNK_D)) + segmentY; + const voxel& vox = chunk.voxels[index + segmentY * CHUNK_W * CHUNK_D]; auto& block = indices->blocks.require(vox.id); if (block.rt.funcsset.randupdate) { scripting::random_update_block( @@ -177,7 +198,6 @@ void BlocksController::randomTick(int tickid, int parts, uint padding) { const auto& chunks = *player->chunks; int width = chunks.getWidth(); int height = chunks.getHeight(); - int segments = 4; for (uint z = padding; z < height - padding; z++) { for (uint x = padding; x < width - padding; x++) { @@ -193,7 +213,7 @@ void BlocksController::randomTick(int tickid, int parts, uint padding) { continue; } chunk->lastRandomTickId = randomTickId; - randomTick(*chunk, segments, indices); + randomTick(*chunk, indices); } } } diff --git a/src/logic/BlocksController.hpp b/src/logic/BlocksController.hpp index ed6ebca5b..35461f349 100644 --- a/src/logic/BlocksController.hpp +++ b/src/logic/BlocksController.hpp @@ -47,9 +47,7 @@ public: ); void update(float delta, uint padding); - void randomTick( - const Chunk& chunk, int segments, const ContentIndices* indices - ); + void randomTick(const Chunk& chunk, const ContentIndices* indices); void randomTick(int tickid, int parts, uint padding); void onBlocksTick(int tickid, int parts); int64_t createBlockInventory(int x, int y, int z);