minor controllers refactor

This commit is contained in:
MihailRis
2024-11-20 18:03:39 +03:00
parent 9e02f1122f
commit 005bcfb436
5 changed files with 56 additions and 58 deletions
+26 -28
View File
@@ -14,10 +14,10 @@
#include "world/Level.hpp" #include "world/Level.hpp"
#include "world/World.hpp" #include "world/World.hpp"
BlocksController::BlocksController(Level* level, uint padding) BlocksController::BlocksController(const Level& level, uint padding)
: level(level), : level(level),
chunks(level->chunks.get()), chunks(*level.chunks),
lighting(level->lighting.get()), lighting(*level.lighting),
randTickClock(20, 3), randTickClock(20, 3),
blocksTickClock(20, 1), blocksTickClock(20, 1),
worldTickClock(20, 1), worldTickClock(20, 1),
@@ -34,8 +34,8 @@ void BlocksController::updateSides(int x, int y, int z) {
} }
void BlocksController::updateSides(int x, int y, int z, int w, int h, int d) { void BlocksController::updateSides(int x, int y, int z, int w, int h, int d) {
voxel* vox = chunks->get(x, y, z); voxel* vox = chunks.get(x, y, z);
const auto& def = level->content->getIndices()->blocks.require(vox->id); const auto& def = level.content->getIndices()->blocks.require(vox->id);
const auto& rot = def.rotations.variants[vox->state.rotation]; const auto& rot = def.rotations.variants[vox->state.rotation];
const auto& xaxis = rot.axisX; const auto& xaxis = rot.axisX;
const auto& yaxis = rot.axisY; const auto& yaxis = rot.axisY;
@@ -62,8 +62,8 @@ void BlocksController::breakBlock(
onBlockInteraction( onBlockInteraction(
player, glm::ivec3(x, y, z), def, BlockInteraction::destruction player, glm::ivec3(x, y, z), def, BlockInteraction::destruction
); );
chunks->set(x, y, z, 0, {}); chunks.set(x, y, z, 0, {});
lighting->onBlockSet(x, y, z, 0); lighting.onBlockSet(x, y, z, 0);
scripting::on_block_broken(player, def, glm::ivec3(x, y, z)); scripting::on_block_broken(player, def, glm::ivec3(x, y, z));
if (def.rt.extended) { if (def.rt.extended) {
updateSides(x, y, z , def.size.x, def.size.y, def.size.z); updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
@@ -78,8 +78,8 @@ void BlocksController::placeBlock(
onBlockInteraction( onBlockInteraction(
player, glm::ivec3(x, y, z), def, BlockInteraction::placing player, glm::ivec3(x, y, z), def, BlockInteraction::placing
); );
chunks->set(x, y, z, def.rt.id, state); chunks.set(x, y, z, def.rt.id, state);
lighting->onBlockSet(x, y, z, def.rt.id); lighting.onBlockSet(x, y, z, def.rt.id);
scripting::on_block_placed(player, def, glm::ivec3(x, y, z)); scripting::on_block_placed(player, def, glm::ivec3(x, y, z));
if (def.rt.extended) { if (def.rt.extended) {
updateSides(x, y, z , def.size.x, def.size.y, def.size.z); updateSides(x, y, z , def.size.x, def.size.y, def.size.z);
@@ -89,12 +89,12 @@ void BlocksController::placeBlock(
} }
void BlocksController::updateBlock(int x, int y, int z) { void BlocksController::updateBlock(int x, int y, int z) {
voxel* vox = chunks->get(x, y, z); voxel* vox = chunks.get(x, y, z);
if (vox == nullptr) return; if (vox == nullptr) return;
const auto& def = level->content->getIndices()->blocks.require(vox->id); const auto& def = level.content->getIndices()->blocks.require(vox->id);
if (def.grounded) { if (def.grounded) {
const auto& vec = get_ground_direction(def, vox->state.rotation); const auto& vec = get_ground_direction(def, vox->state.rotation);
if (!chunks->isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) { if (!chunks.isSolidBlock(x + vec.x, y + vec.y, z + vec.z)) {
breakBlock(nullptr, def, x, y, z); breakBlock(nullptr, def, x, y, z);
return; return;
} }
@@ -117,12 +117,11 @@ void BlocksController::update(float delta) {
} }
void BlocksController::onBlocksTick(int tickid, int parts) { void BlocksController::onBlocksTick(int tickid, int parts) {
auto content = level->content; const auto& indices = level.content->getIndices()->blocks;
auto indices = content->getIndices();
int tickRate = blocksTickClock.getTickRate(); int tickRate = blocksTickClock.getTickRate();
for (size_t id = 0; id < indices->blocks.count(); id++) { for (size_t id = 0; id < indices.count(); id++) {
if ((id + tickid) % parts != 0) continue; if ((id + tickid) % parts != 0) continue;
auto& def = indices->blocks.require(id); auto& def = indices.require(id);
auto interval = def.tickInterval; auto interval = def.tickInterval;
if (def.rt.funcsset.onblockstick && tickid / parts % interval == 0) { if (def.rt.funcsset.onblockstick && tickid / parts % interval == 0) {
scripting::on_blocks_tick(def, tickRate / interval); scripting::on_blocks_tick(def, tickRate / interval);
@@ -155,9 +154,9 @@ void BlocksController::randomTick(
} }
void BlocksController::randomTick(int tickid, int parts) { void BlocksController::randomTick(int tickid, int parts) {
auto indices = level->content->getIndices(); auto indices = level.content->getIndices();
int width = chunks->getWidth(); int width = chunks.getWidth();
int height = chunks->getHeight(); int height = chunks.getHeight();
int segments = 4; int segments = 4;
for (uint z = padding; z < height - padding; z++) { for (uint z = padding; z < height - padding; z++) {
@@ -166,7 +165,7 @@ void BlocksController::randomTick(int tickid, int parts) {
if ((index + tickid) % parts != 0) { if ((index + tickid) % parts != 0) {
continue; continue;
} }
auto& chunk = chunks->getChunks()[index]; auto& chunk = chunks.getChunks()[index];
if (chunk == nullptr || !chunk->flags.lighted) { if (chunk == nullptr || !chunk->flags.lighted) {
continue; continue;
} }
@@ -176,7 +175,7 @@ void BlocksController::randomTick(int tickid, int parts) {
} }
int64_t BlocksController::createBlockInventory(int x, int y, int z) { int64_t BlocksController::createBlockInventory(int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z); auto chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr) { if (chunk == nullptr) {
return 0; return 0;
} }
@@ -184,21 +183,20 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) {
int lz = z - chunk->z * CHUNK_D; int lz = z - chunk->z * CHUNK_D;
auto inv = chunk->getBlockInventory(lx, y, lz); auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) { if (inv == nullptr) {
auto indices = level->content->getIndices(); const auto& indices = level.content->getIndices()->blocks;
auto& def = auto& def = indices.require(chunk->voxels[vox_index(lx, y, lz)].id);
indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def.inventorySize; int invsize = def.inventorySize;
if (invsize == 0) { if (invsize == 0) {
return 0; return 0;
} }
inv = level->inventories->create(invsize); inv = level.inventories->create(invsize);
chunk->addBlockInventory(inv, lx, y, lz); chunk->addBlockInventory(inv, lx, y, lz);
} }
return inv->getId(); return inv->getId();
} }
void BlocksController::bindInventory(int64_t invid, int x, int y, int z) { void BlocksController::bindInventory(int64_t invid, int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z); auto chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr) { if (chunk == nullptr) {
throw std::runtime_error("block does not exists"); throw std::runtime_error("block does not exists");
} }
@@ -207,11 +205,11 @@ void BlocksController::bindInventory(int64_t invid, int x, int y, int z) {
} }
int lx = x - chunk->x * CHUNK_W; int lx = x - chunk->x * CHUNK_W;
int lz = z - chunk->z * CHUNK_D; int lz = z - chunk->z * CHUNK_D;
chunk->addBlockInventory(level->inventories->get(invid), lx, y, lz); chunk->addBlockInventory(level.inventories->get(invid), lx, y, lz);
} }
void BlocksController::unbindInventory(int x, int y, int z) { void BlocksController::unbindInventory(int x, int y, int z) {
auto chunk = chunks->getChunkByVoxel(x, y, z); auto chunk = chunks.getChunkByVoxel(x, y, z);
if (chunk == nullptr) { if (chunk == nullptr) {
throw std::runtime_error("block does not exists"); throw std::runtime_error("block does not exists");
} }
+4 -4
View File
@@ -24,9 +24,9 @@ using on_block_interaction = std::function<
/// BlocksController manages block updates and data (inventories, metadata) /// BlocksController manages block updates and data (inventories, metadata)
class BlocksController { class BlocksController {
Level* level; const Level& level;
Chunks* chunks; Chunks& chunks;
Lighting* lighting; Lighting& lighting;
util::Clock randTickClock; util::Clock randTickClock;
util::Clock blocksTickClock; util::Clock blocksTickClock;
util::Clock worldTickClock; util::Clock worldTickClock;
@@ -34,7 +34,7 @@ class BlocksController {
FastRandom random {}; FastRandom random {};
std::vector<on_block_interaction> blockInteractionCallbacks; std::vector<on_block_interaction> blockInteractionCallbacks;
public: public:
BlocksController(Level* level, uint padding); BlocksController(const Level& level, uint padding);
void updateSides(int x, int y, int z); void updateSides(int x, int y, int z);
void updateSides(int x, int y, int z, int w, int h, int d); void updateSides(int x, int y, int z, int w, int h, int d);
+18 -18
View File
@@ -22,15 +22,15 @@
const uint MAX_WORK_PER_FRAME = 128; const uint MAX_WORK_PER_FRAME = 128;
const uint MIN_SURROUNDING = 9; const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level* level, uint padding) ChunksController::ChunksController(Level& level, uint padding)
: level(level), : level(level),
chunks(level->chunks.get()), chunks(*level.chunks),
lighting(level->lighting.get()), lighting(*level.lighting),
padding(padding), padding(padding),
generator(std::make_unique<WorldGenerator>( generator(std::make_unique<WorldGenerator>(
level->content->generators.require(level->getWorld()->getGenerator()), level.content->generators.require(level.getWorld()->getGenerator()),
level->content, level.content,
level->getWorld()->getSeed() level.getWorld()->getSeed()
)) {} )) {}
ChunksController::~ChunksController() = default; ChunksController::~ChunksController() = default;
@@ -56,8 +56,8 @@ void ChunksController::update(
} }
bool ChunksController::loadVisible() { bool ChunksController::loadVisible() {
int sizeX = chunks->getWidth(); int sizeX = chunks.getWidth();
int sizeY = chunks->getHeight(); int sizeY = chunks.getHeight();
int nearX = 0; int nearX = 0;
int nearZ = 0; int nearZ = 0;
@@ -66,7 +66,7 @@ bool ChunksController::loadVisible() {
for (uint z = padding; z < sizeY - padding; z++) { for (uint z = padding; z < sizeY - padding; z++) {
for (uint x = padding; x < sizeX - padding; x++) { for (uint x = padding; x < sizeX - padding; x++) {
int index = z * sizeX + x; int index = z * sizeX + x;
auto& chunk = chunks->getChunks()[index]; auto& chunk = chunks.getChunks()[index];
if (chunk != nullptr) { if (chunk != nullptr) {
if (chunk->flags.loaded && !chunk->flags.lighted) { if (chunk->flags.loaded && !chunk->flags.lighted) {
if (buildLights(chunk)) { if (buildLights(chunk)) {
@@ -87,12 +87,12 @@ bool ChunksController::loadVisible() {
} }
} }
const auto& chunk = chunks->getChunks()[nearZ * sizeX + nearX]; const auto& chunk = chunks.getChunks()[nearZ * sizeX + nearX];
if (chunk != nullptr || !assigned) { if (chunk != nullptr || !assigned) {
return false; return false;
} }
int offsetX = chunks->getOffsetX(); int offsetX = chunks.getOffsetX();
int offsetY = chunks->getOffsetY(); int offsetY = chunks.getOffsetY();
createChunk(nearX + offsetX, nearZ + offsetY); createChunk(nearX + offsetX, nearZ + offsetY);
return true; return true;
} }
@@ -101,15 +101,15 @@ bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
int surrounding = 0; int surrounding = 0;
for (int oz = -1; oz <= 1; oz++) { for (int oz = -1; oz <= 1; oz++) {
for (int ox = -1; ox <= 1; ox++) { for (int ox = -1; ox <= 1; ox++) {
if (chunks->getChunk(chunk->x + ox, chunk->z + oz)) surrounding++; if (chunks.getChunk(chunk->x + ox, chunk->z + oz)) surrounding++;
} }
} }
if (surrounding == MIN_SURROUNDING) { if (surrounding == MIN_SURROUNDING) {
bool lightsCache = chunk->flags.loadedLights; bool lightsCache = chunk->flags.loadedLights;
if (!lightsCache) { if (!lightsCache) {
lighting->buildSkyLight(chunk->x, chunk->z); lighting.buildSkyLight(chunk->x, chunk->z);
} }
lighting->onChunkLoaded(chunk->x, chunk->z, !lightsCache); lighting.onChunkLoaded(chunk->x, chunk->z, !lightsCache);
chunk->flags.lighted = true; chunk->flags.lighted = true;
return true; return true;
} }
@@ -117,8 +117,8 @@ bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
} }
void ChunksController::createChunk(int x, int z) { void ChunksController::createChunk(int x, int z) {
auto chunk = level->chunksStorage->create(x, z); auto chunk = level.chunksStorage->create(x, z);
chunks->putChunk(chunk); chunks.putChunk(chunk);
auto& chunkFlags = chunk->flags; auto& chunkFlags = chunk->flags;
if (!chunkFlags.loaded) { if (!chunkFlags.loaded) {
@@ -128,7 +128,7 @@ void ChunksController::createChunk(int x, int z) {
chunk->updateHeights(); chunk->updateHeights();
if (!chunkFlags.loadedLights) { if (!chunkFlags.loadedLights) {
Lighting::prebuildSkyLight(chunk.get(), level->content->getIndices()); Lighting::prebuildSkyLight(chunk.get(), level.content->getIndices());
} }
chunkFlags.loaded = true; chunkFlags.loaded = true;
chunkFlags.ready = true; chunkFlags.ready = true;
+4 -4
View File
@@ -13,9 +13,9 @@ class WorldGenerator;
/// @brief ChunksController manages chunks dynamic loading/unloading /// @brief ChunksController manages chunks dynamic loading/unloading
class ChunksController { class ChunksController {
private: private:
Level* level; Level& level;
Chunks* chunks; Chunks& chunks;
Lighting* lighting; Lighting& lighting;
uint padding; uint padding;
std::unique_ptr<WorldGenerator> generator; std::unique_ptr<WorldGenerator> generator;
@@ -24,7 +24,7 @@ private:
bool buildLights(const std::shared_ptr<Chunk>& chunk); bool buildLights(const std::shared_ptr<Chunk>& chunk);
void createChunk(int x, int y); void createChunk(int x, int y);
public: public:
ChunksController(Level* level, uint padding); ChunksController(Level& level, uint padding);
~ChunksController(); ~ChunksController();
/// @param maxDuration milliseconds reserved for chunks loading /// @param maxDuration milliseconds reserved for chunks loading
+4 -4
View File
@@ -16,14 +16,14 @@
static debug::Logger logger("level-control"); static debug::Logger logger("level-control");
LevelController::LevelController(Engine* engine, std::unique_ptr<Level> level) LevelController::LevelController(Engine* engine, std::unique_ptr<Level> levelPtr)
: settings(engine->getSettings()), : settings(engine->getSettings()),
level(std::move(level)), level(std::move(levelPtr)),
blocks(std::make_unique<BlocksController>( blocks(std::make_unique<BlocksController>(
this->level.get(), settings.chunks.padding.get() *level, settings.chunks.padding.get()
)), )),
chunks(std::make_unique<ChunksController>( chunks(std::make_unique<ChunksController>(
this->level.get(), settings.chunks.padding.get() *level, settings.chunks.padding.get()
)), )),
player(std::make_unique<PlayerController>( player(std::make_unique<PlayerController>(
settings, this->level.get(), blocks.get() settings, this->level.get(), blocks.get()