From 6214e1a507cfb9922e90541dded8e6228a042b52 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 17 Jan 2024 23:41:11 +0300 Subject: [PATCH] randomTick sped up with faster rand implementation --- src/logic/BlocksController.cpp | 16 +++++++++------- src/maths/fastmaths.h | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) create mode 100644 src/maths/fastmaths.h diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index ab308342..d41a822d 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -8,6 +8,7 @@ #include "../content/Content.h" #include "../lighting/Lighting.h" #include "../util/timeutil.h" +#include "../maths/fastmaths.h" #include "scripting/scripting.h" @@ -86,10 +87,13 @@ void BlocksController::update(float delta) { } void BlocksController::randomTick(int tickid, int parts) { - // timeutil::ScopeLogTimer timer(5000+tickid); + timeutil::ScopeLogTimer timer(5000+tickid); 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++){ int index = z * w + x; @@ -98,13 +102,11 @@ void BlocksController::randomTick(int tickid, int parts) { std::shared_ptr chunk = chunks->chunks[index]; if (chunk == nullptr || !chunk->isLighted()) continue; - int segments = 4; - int segheight = CHUNK_H / segments; for (int s = 0; s < segments; s++) { - for (int i = 0; i < 3; i++) { - int bx = rand() % CHUNK_W; - int by = rand() % segheight + s * segheight; - int bz = rand() % CHUNK_D; + for (int i = 0; i < 4; i++) { + int bx = fastmaths::rand() % CHUNK_W; + int by = fastmaths::rand() % segheight + s * segheight; + int bz = fastmaths::rand() % CHUNK_D; const voxel& vox = chunk->voxels[(by * CHUNK_D + bz) * CHUNK_W + bx]; Block* block = indices->getBlockDef(vox.id); if (block->rt.funcsset.randupdate) { diff --git a/src/maths/fastmaths.h b/src/maths/fastmaths.h new file mode 100644 index 00000000..73ea99e5 --- /dev/null +++ b/src/maths/fastmaths.h @@ -0,0 +1,17 @@ +#ifndef MATHS_FASTMATHS_H_ +#define MATHS_FASTMATHS_H_ + +namespace fastmaths { + static unsigned int g_seed; + + inline void srand(int seed) { + g_seed = seed; + } + + inline int rand(void) { + g_seed = (214013*g_seed+2531011); + return (g_seed>>16)&0x7FFF; + } +} + +#endif // MATHS_FASTMATHS_H_