move class Clock to a separate unit

This commit is contained in:
MihailRis 2024-07-19 20:49:04 +03:00
parent fbced2504e
commit eedce588b1
4 changed files with 98 additions and 76 deletions

View File

@ -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);
}
}
}

View File

@ -4,6 +4,7 @@
#include "../typedefs.hpp"
#include "../maths/fastmaths.hpp"
#include "../voxels/voxel.hpp"
#include "../util/Clock.hpp"
#include <functional>
#include <glm/glm.hpp>
@ -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<void(
Player*, glm::ivec3, const Block*, BlockInteraction type
)>;
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<on_block_interaction> 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);

41
src/util/Clock.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "Clock.hpp"
#include <cmath>
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;
}

24
src/util/Clock.hpp Normal file
View File

@ -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_