diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index b0362fd9..01679546 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -14,10 +14,10 @@ #include "world/Level.hpp" #include "world/World.hpp" -BlocksController::BlocksController(Level* level, uint padding) +BlocksController::BlocksController(const Level& level, uint padding) : level(level), - chunks(level->chunks.get()), - lighting(level->lighting.get()), + chunks(*level.chunks), + lighting(*level.lighting), randTickClock(20, 3), blocksTickClock(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) { - voxel* vox = chunks->get(x, y, z); - const auto& def = level->content->getIndices()->blocks.require(vox->id); + voxel* vox = chunks.get(x, y, z); + const auto& def = level.content->getIndices()->blocks.require(vox->id); const auto& rot = def.rotations.variants[vox->state.rotation]; const auto& xaxis = rot.axisX; const auto& yaxis = rot.axisY; @@ -62,8 +62,8 @@ void BlocksController::breakBlock( onBlockInteraction( player, glm::ivec3(x, y, z), def, BlockInteraction::destruction ); - chunks->set(x, y, z, 0, {}); - lighting->onBlockSet(x, y, z, 0); + chunks.set(x, y, z, 0, {}); + lighting.onBlockSet(x, y, z, 0); scripting::on_block_broken(player, def, glm::ivec3(x, y, z)); if (def.rt.extended) { updateSides(x, y, z , def.size.x, def.size.y, def.size.z); @@ -78,8 +78,8 @@ void BlocksController::placeBlock( onBlockInteraction( player, glm::ivec3(x, y, z), def, BlockInteraction::placing ); - chunks->set(x, y, z, def.rt.id, state); - lighting->onBlockSet(x, y, z, def.rt.id); + chunks.set(x, y, z, def.rt.id, state); + lighting.onBlockSet(x, y, z, def.rt.id); scripting::on_block_placed(player, def, glm::ivec3(x, y, z)); if (def.rt.extended) { 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) { - voxel* vox = chunks->get(x, y, z); + voxel* vox = chunks.get(x, y, z); 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) { 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); return; } @@ -117,12 +117,11 @@ void BlocksController::update(float delta) { } void BlocksController::onBlocksTick(int tickid, int parts) { - auto content = level->content; - auto indices = content->getIndices(); + const auto& indices = level.content->getIndices()->blocks; 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; - auto& def = indices->blocks.require(id); + auto& def = indices.require(id); auto interval = def.tickInterval; if (def.rt.funcsset.onblockstick && tickid / parts % interval == 0) { scripting::on_blocks_tick(def, tickRate / interval); @@ -155,9 +154,9 @@ void BlocksController::randomTick( } void BlocksController::randomTick(int tickid, int parts) { - auto indices = level->content->getIndices(); - int width = chunks->getWidth(); - int height = chunks->getHeight(); + auto indices = level.content->getIndices(); + int width = chunks.getWidth(); + int height = chunks.getHeight(); int segments = 4; for (uint z = padding; z < height - padding; z++) { @@ -166,7 +165,7 @@ void BlocksController::randomTick(int tickid, int parts) { if ((index + tickid) % parts != 0) { continue; } - auto& chunk = chunks->getChunks()[index]; + auto& chunk = chunks.getChunks()[index]; if (chunk == nullptr || !chunk->flags.lighted) { continue; } @@ -176,7 +175,7 @@ void BlocksController::randomTick(int tickid, int parts) { } 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) { return 0; } @@ -184,21 +183,20 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) { int lz = z - chunk->z * CHUNK_D; auto inv = chunk->getBlockInventory(lx, y, lz); if (inv == nullptr) { - auto indices = level->content->getIndices(); - auto& def = - indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id); + const auto& indices = level.content->getIndices()->blocks; + auto& def = indices.require(chunk->voxels[vox_index(lx, y, lz)].id); int invsize = def.inventorySize; if (invsize == 0) { return 0; } - inv = level->inventories->create(invsize); + inv = level.inventories->create(invsize); chunk->addBlockInventory(inv, lx, y, lz); } return inv->getId(); } 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) { 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 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) { - auto chunk = chunks->getChunkByVoxel(x, y, z); + auto chunk = chunks.getChunkByVoxel(x, y, z); if (chunk == nullptr) { throw std::runtime_error("block does not exists"); } diff --git a/src/logic/BlocksController.hpp b/src/logic/BlocksController.hpp index 86bbe395..0937b3f7 100644 --- a/src/logic/BlocksController.hpp +++ b/src/logic/BlocksController.hpp @@ -24,9 +24,9 @@ using on_block_interaction = std::function< /// BlocksController manages block updates and data (inventories, metadata) class BlocksController { - Level* level; - Chunks* chunks; - Lighting* lighting; + const Level& level; + Chunks& chunks; + Lighting& lighting; util::Clock randTickClock; util::Clock blocksTickClock; util::Clock worldTickClock; @@ -34,7 +34,7 @@ class BlocksController { FastRandom random {}; std::vector blockInteractionCallbacks; 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, int w, int h, int d); diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index 07492f29..f61a2755 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -22,15 +22,15 @@ const uint MAX_WORK_PER_FRAME = 128; const uint MIN_SURROUNDING = 9; -ChunksController::ChunksController(Level* level, uint padding) +ChunksController::ChunksController(Level& level, uint padding) : level(level), - chunks(level->chunks.get()), - lighting(level->lighting.get()), + chunks(*level.chunks), + lighting(*level.lighting), padding(padding), generator(std::make_unique( - level->content->generators.require(level->getWorld()->getGenerator()), - level->content, - level->getWorld()->getSeed() + level.content->generators.require(level.getWorld()->getGenerator()), + level.content, + level.getWorld()->getSeed() )) {} ChunksController::~ChunksController() = default; @@ -56,8 +56,8 @@ void ChunksController::update( } bool ChunksController::loadVisible() { - int sizeX = chunks->getWidth(); - int sizeY = chunks->getHeight(); + int sizeX = chunks.getWidth(); + int sizeY = chunks.getHeight(); int nearX = 0; int nearZ = 0; @@ -66,7 +66,7 @@ bool ChunksController::loadVisible() { for (uint z = padding; z < sizeY - padding; z++) { for (uint x = padding; x < sizeX - padding; x++) { int index = z * sizeX + x; - auto& chunk = chunks->getChunks()[index]; + auto& chunk = chunks.getChunks()[index]; if (chunk != nullptr) { if (chunk->flags.loaded && !chunk->flags.lighted) { 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) { return false; } - int offsetX = chunks->getOffsetX(); - int offsetY = chunks->getOffsetY(); + int offsetX = chunks.getOffsetX(); + int offsetY = chunks.getOffsetY(); createChunk(nearX + offsetX, nearZ + offsetY); return true; } @@ -101,15 +101,15 @@ bool ChunksController::buildLights(const std::shared_ptr& chunk) { int surrounding = 0; for (int oz = -1; oz <= 1; oz++) { 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) { bool lightsCache = chunk->flags.loadedLights; 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; return true; } @@ -117,8 +117,8 @@ bool ChunksController::buildLights(const std::shared_ptr& chunk) { } void ChunksController::createChunk(int x, int z) { - auto chunk = level->chunksStorage->create(x, z); - chunks->putChunk(chunk); + auto chunk = level.chunksStorage->create(x, z); + chunks.putChunk(chunk); auto& chunkFlags = chunk->flags; if (!chunkFlags.loaded) { @@ -128,7 +128,7 @@ void ChunksController::createChunk(int x, int z) { chunk->updateHeights(); if (!chunkFlags.loadedLights) { - Lighting::prebuildSkyLight(chunk.get(), level->content->getIndices()); + Lighting::prebuildSkyLight(chunk.get(), level.content->getIndices()); } chunkFlags.loaded = true; chunkFlags.ready = true; diff --git a/src/logic/ChunksController.hpp b/src/logic/ChunksController.hpp index 8566ee1b..e9f1ab62 100644 --- a/src/logic/ChunksController.hpp +++ b/src/logic/ChunksController.hpp @@ -13,9 +13,9 @@ class WorldGenerator; /// @brief ChunksController manages chunks dynamic loading/unloading class ChunksController { private: - Level* level; - Chunks* chunks; - Lighting* lighting; + Level& level; + Chunks& chunks; + Lighting& lighting; uint padding; std::unique_ptr generator; @@ -24,7 +24,7 @@ private: bool buildLights(const std::shared_ptr& chunk); void createChunk(int x, int y); public: - ChunksController(Level* level, uint padding); + ChunksController(Level& level, uint padding); ~ChunksController(); /// @param maxDuration milliseconds reserved for chunks loading diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index 2cd491d4..bd8af15c 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -16,14 +16,14 @@ static debug::Logger logger("level-control"); -LevelController::LevelController(Engine* engine, std::unique_ptr level) +LevelController::LevelController(Engine* engine, std::unique_ptr levelPtr) : settings(engine->getSettings()), - level(std::move(level)), + level(std::move(levelPtr)), blocks(std::make_unique( - this->level.get(), settings.chunks.padding.get() + *level, settings.chunks.padding.get() )), chunks(std::make_unique( - this->level.get(), settings.chunks.padding.get() + *level, settings.chunks.padding.get() )), player(std::make_unique( settings, this->level.get(), blocks.get()