diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 5a57d729..8f49635c 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -15,42 +15,6 @@ #include "scripting/scripting.hpp" -Clock::Clock(int tickRate, int tickParts) - : tickRate(tickRate), - tickParts(tickParts) { -} - -bool Clock::update(float delta) { - tickTimer += delta; - float delay = 1.0f / float(tickRate); - if (tickTimer > delay || tickPartsUndone) { - if (tickPartsUndone) { - tickPartsUndone--; - } else { - tickTimer = fmod(tickTimer, delay); - tickPartsUndone = tickParts-1; - } - return true; - } - return false; -} - -int Clock::getParts() const { - return tickParts; -} - -int Clock::getPart() const { - return tickParts-tickPartsUndone-1; -} - -int Clock::getTickRate() const { - return tickRate; -} - -int Clock::getTickId() const { - return tickId; -} - BlocksController::BlocksController(Level* level, uint padding) : level(level), chunks(level->chunks.get()), @@ -134,12 +98,34 @@ void BlocksController::onBlocksTick(int tickid, int parts) { } } +void BlocksController::randomTick( + const Chunk& chunk, int segments, const ContentIndices* indices +) { + const int segheight = CHUNK_H / segments; + + for (int s = 0; s < segments; s++) { + for (int i = 0; i < 4; i++) { + int bx = random.rand() % CHUNK_W; + int by = random.rand() % segheight + s * segheight; + int bz = random.rand() % CHUNK_D; + const voxel& vox = chunk.voxels[(by * CHUNK_D + bz) * CHUNK_W + bx]; + Block* block = indices->blocks.get(vox.id); + if (block->rt.funcsset.randupdate) { + scripting::random_update_block( + block, + chunk.x * CHUNK_W + bx, by, + chunk.z * CHUNK_D + bz + ); + } + } + } +} + void BlocksController::randomTick(int tickid, int parts) { + auto indices = level->content->getIndices(); const int w = chunks->w; const int d = chunks->d; int segments = 4; - int segheight = CHUNK_H / segments; - auto indices = level->content->getIndices(); for (uint z = padding; z < d-padding; z++){ for (uint x = padding; x < w-padding; x++){ @@ -151,22 +137,7 @@ void BlocksController::randomTick(int tickid, int parts) { if (chunk == nullptr || !chunk->flags.lighted) { continue; } - for (int s = 0; s < segments; s++) { - for (int i = 0; i < 4; i++) { - int bx = random.rand() % CHUNK_W; - int by = random.rand() % segheight + s * segheight; - int bz = random.rand() % CHUNK_D; - const voxel& vox = chunk->voxels[(by * CHUNK_D + bz) * CHUNK_W + bx]; - Block* block = indices->blocks.get(vox.id); - if (block->rt.funcsset.randupdate) { - scripting::random_update_block( - block, - chunk->x * CHUNK_W + bx, by, - chunk->z * CHUNK_D + bz - ); - } - } - } + randomTick(*chunk, segments, indices); } } } diff --git a/src/logic/BlocksController.hpp b/src/logic/BlocksController.hpp index 179302f5..0c904932 100644 --- a/src/logic/BlocksController.hpp +++ b/src/logic/BlocksController.hpp @@ -4,6 +4,7 @@ #include "../typedefs.hpp" #include "../maths/fastmaths.hpp" #include "../voxels/voxel.hpp" +#include "../util/Clock.hpp" #include #include @@ -11,8 +12,10 @@ class Player; class Block; class Level; +class Chunk; class Chunks; class Lighting; +class ContentIndices; enum class BlockInteraction { step, @@ -25,32 +28,14 @@ using on_block_interaction = std::function; -class Clock { - int tickRate; - int tickParts; - - float tickTimer = 0.0f; - int tickId = 0; - int tickPartsUndone = 0; -public: - Clock(int tickRate, int tickParts); - - bool update(float delta); - - int getParts() const; - int getPart() const; - int getTickRate() const; - int getTickId() const; -}; - -/* BlocksController manages block updates and block data (aka inventories) */ +/// BlocksController manages block updates and data (inventories, metadata) class BlocksController { Level* level; Chunks* chunks; Lighting* lighting; - Clock randTickClock; - Clock blocksTickClock; - Clock worldTickClock; + util::Clock randTickClock; + util::Clock blocksTickClock; + util::Clock worldTickClock; uint padding; FastRandom random; std::vector blockInteractionCallbacks; @@ -64,6 +49,7 @@ public: void placeBlock(Player* player, const Block* def, blockstate state, int x, int y, int z); void update(float delta); + void randomTick(const Chunk& chunk, int segments, const ContentIndices* indices); void randomTick(int tickid, int parts); void onBlocksTick(int tickid, int parts); int64_t createBlockInventory(int x, int y, int z); diff --git a/src/util/Clock.cpp b/src/util/Clock.cpp new file mode 100644 index 00000000..bb81112e --- /dev/null +++ b/src/util/Clock.cpp @@ -0,0 +1,41 @@ +#include "Clock.hpp" + +#include + +using namespace util; + +Clock::Clock(int tickRate, int tickParts) + : tickRate(tickRate), + tickParts(tickParts) { +} + +bool Clock::update(float delta) { + tickTimer += delta; + float delay = 1.0f / float(tickRate); + if (tickTimer > delay || tickPartsUndone) { + if (tickPartsUndone) { + tickPartsUndone--; + } else { + tickTimer = std::fmod(tickTimer, delay); + tickPartsUndone = tickParts-1; + } + return true; + } + return false; +} + +int Clock::getParts() const { + return tickParts; +} + +int Clock::getPart() const { + return tickParts-tickPartsUndone-1; +} + +int Clock::getTickRate() const { + return tickRate; +} + +int Clock::getTickId() const { + return tickId; +} diff --git a/src/util/Clock.hpp b/src/util/Clock.hpp new file mode 100644 index 00000000..6a59051a --- /dev/null +++ b/src/util/Clock.hpp @@ -0,0 +1,24 @@ +#ifndef UTIL_CLOCK_HPP_ +#define UTIL_CLOCK_HPP_ + +namespace util { + class Clock { + int tickRate; + int tickParts; + + float tickTimer = 0.0f; + int tickId = 0; + int tickPartsUndone = 0; + public: + Clock(int tickRate, int tickParts); + + bool update(float delta); + + int getParts() const; + int getPart() const; + int getTickRate() const; + int getTickId() const; + }; +} + +#endif // UTIL_CLOCK_HPP_