From 017c8722ae1cfb9767fd23c0c94953081b48bc8b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 13 May 2024 00:11:20 +0300 Subject: [PATCH] memory related refactor --- src/content/ContentLUT.cpp | 10 +- src/content/ContentLUT.hpp | 2 +- src/debug/Logger.cpp | 1 + src/debug/Logger.hpp | 1 - src/frontend/locale.cpp | 13 +- src/frontend/screens/LevelScreen.cpp | 4 +- src/frontend/screens/LevelScreen.hpp | 2 +- src/lighting/LightSolver.hpp | 30 +- src/lighting/Lighting.cpp | 323 +++++++++---------- src/lighting/Lighting.hpp | 26 +- src/lighting/Lightmap.hpp | 2 +- src/logic/ChunksController.cpp | 94 +++--- src/logic/ChunksController.hpp | 2 +- src/logic/EngineController.cpp | 8 +- src/logic/LevelController.cpp | 12 +- src/logic/LevelController.hpp | 2 +- src/logic/PlayerController.cpp | 3 +- src/logic/PlayerController.hpp | 10 +- src/logic/scripting/scripting_functional.cpp | 2 +- src/logic/scripting/scripting_functional.hpp | 4 +- src/world/Level.cpp | 11 +- src/world/Level.hpp | 6 +- src/world/LevelEvents.hpp | 2 +- src/world/World.cpp | 73 +++-- src/world/World.hpp | 13 +- 25 files changed, 336 insertions(+), 320 deletions(-) diff --git a/src/content/ContentLUT.cpp b/src/content/ContentLUT.cpp index 6e76642c..779277d5 100644 --- a/src/content/ContentLUT.cpp +++ b/src/content/ContentLUT.cpp @@ -33,8 +33,10 @@ ContentLUT::ContentLUT(const Content* content, size_t blocksCount, size_t itemsC } } -ContentLUT* ContentLUT::create(const fs::path& filename, - const Content* content) { +std::shared_ptr ContentLUT::create( + const fs::path& filename, + const Content* content +) { auto root = files::read_json(filename); auto blocklist = root->list("blocks"); auto itemlist = root->list("items"); @@ -47,7 +49,7 @@ ContentLUT* ContentLUT::create(const fs::path& filename, ? std::max(itemlist->size(), indices->countItemDefs()) : indices->countItemDefs(); - auto lut = std::make_unique(content, blocks_c, items_c); + auto lut = std::make_shared(content, blocks_c, items_c); if (blocklist) { for (size_t i = 0; i < blocklist->size(); i++) { @@ -74,7 +76,7 @@ ContentLUT* ContentLUT::create(const fs::path& filename, } if (lut->hasContentReorder() || lut->hasMissingContent()) { - return lut.release(); + return lut; } else { return nullptr; } diff --git a/src/content/ContentLUT.hpp b/src/content/ContentLUT.hpp index 399ad36f..46204d19 100644 --- a/src/content/ContentLUT.hpp +++ b/src/content/ContentLUT.hpp @@ -70,7 +70,7 @@ public: } } - static ContentLUT* create( + static std::shared_ptr create( const fs::path& filename, const Content* content ); diff --git a/src/debug/Logger.cpp b/src/debug/Logger.cpp index c0f8ef38..75582642 100644 --- a/src/debug/Logger.cpp +++ b/src/debug/Logger.cpp @@ -3,6 +3,7 @@ #include #include #include +#include using namespace debug; diff --git a/src/debug/Logger.hpp b/src/debug/Logger.hpp index 6bb2ff5c..b3024b0a 100644 --- a/src/debug/Logger.hpp +++ b/src/debug/Logger.hpp @@ -3,7 +3,6 @@ #include #include -#include #include namespace debug { diff --git a/src/frontend/locale.cpp b/src/frontend/locale.cpp index 1c9730b8..abe41f76 100644 --- a/src/frontend/locale.cpp +++ b/src/frontend/locale.cpp @@ -74,7 +74,8 @@ void langs::loadLocalesInfo(const fs::path& resdir, std::string& fallback) { auto langs = root->map("langs"); if (langs) { - std::cout << "locales "; + auto logline = logger.info(); + logline << "locales "; for (auto& entry : langs->values) { auto langInfo = entry.second; @@ -84,10 +85,10 @@ void langs::loadLocalesInfo(const fs::path& resdir, std::string& fallback) { } else { continue; } - std::cout << "[" << entry.first << " (" << name << ")] "; + logline << "[" << entry.first << " (" << name << ")] "; langs::locales_info[entry.first] = LocaleInfo {entry.first, name}; } - std::cout << "added" << std::endl; + logline << "added"; } } @@ -97,17 +98,17 @@ std::string langs::locale_by_envlocale(const std::string& envlocale, const fs::p loadLocalesInfo(resdir, fallback); } if (locales_info.find(envlocale) != locales_info.end()) { - std::cout << "locale " << envlocale << " is automatically selected" << std::endl; + logger.info() << "locale " << envlocale << " is automatically selected"; return envlocale; } else { for (const auto& loc : locales_info) { if (loc.first.find(envlocale.substr(0, 2)) != std::string::npos) { - std::cout << "locale " << loc.first << " is automatically selected" << std::endl; + logger.info() << "locale " << loc.first << " is automatically selected"; return loc.first; } } - std::cout << "locale " << fallback << " is automatically selected" << std::endl; + logger.info() << "locale " << fallback << " is automatically selected"; return fallback; } } diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index 7882d033..22cbaed3 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -24,7 +24,7 @@ static debug::Logger logger("level-screen"); -LevelScreen::LevelScreen(Engine* engine, Level* level) +LevelScreen::LevelScreen(Engine* engine, std::unique_ptr level) : Screen(engine), postProcessing(std::make_unique()) { auto& settings = engine->getSettings(); @@ -32,7 +32,7 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) auto menu = engine->getGUI()->getMenu(); menu->reset(); - controller = std::make_unique(settings, level); + controller = std::make_unique(settings, std::move(level)); frontend = std::make_unique(controller.get(), assets); worldRenderer = std::make_unique(engine, frontend.get(), controller->getPlayer()); diff --git a/src/frontend/screens/LevelScreen.hpp b/src/frontend/screens/LevelScreen.hpp index 8f70cf12..8548c06c 100644 --- a/src/frontend/screens/LevelScreen.hpp +++ b/src/frontend/screens/LevelScreen.hpp @@ -28,7 +28,7 @@ class LevelScreen : public Screen { void updateHotkeys(); void initializeContent(); public: - LevelScreen(Engine* engine, Level* level); + LevelScreen(Engine* engine, std::unique_ptr level); ~LevelScreen(); void update(float delta) override; diff --git a/src/lighting/LightSolver.hpp b/src/lighting/LightSolver.hpp index e57c6a79..a15f9734 100644 --- a/src/lighting/LightSolver.hpp +++ b/src/lighting/LightSolver.hpp @@ -7,25 +7,25 @@ class Chunks; class ContentIndices; struct lightentry { - int x; - int y; - int z; - unsigned char light; + int x; + int y; + int z; + unsigned char light; }; class LightSolver { - std::queue addqueue; - std::queue remqueue; - const ContentIndices* const contentIds; - Chunks* chunks; - int channel; + std::queue addqueue; + std::queue remqueue; + const ContentIndices* const contentIds; + Chunks* chunks; + int channel; public: - LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel); + LightSolver(const ContentIndices* contentIds, Chunks* chunks, int channel); - void add(int x, int y, int z); - void add(int x, int y, int z, int emission); - void remove(int x, int y, int z); - void solve(); + void add(int x, int y, int z); + void add(int x, int y, int z, int emission); + void remove(int x, int y, int z); + void solve(); }; -#endif /* LIGHTING_LIGHTSOLVER_HPP_ */ +#endif // LIGHTING_LIGHTSOLVER_HPP_ diff --git a/src/lighting/Lighting.cpp b/src/lighting/Lighting.cpp index 3bd33300..de4d0e6e 100644 --- a/src/lighting/Lighting.cpp +++ b/src/lighting/Lighting.cpp @@ -1,5 +1,3 @@ -#include - #include "Lighting.hpp" #include "LightSolver.hpp" #include "Lightmap.hpp" @@ -9,82 +7,83 @@ #include "../voxels/voxel.hpp" #include "../voxels/Block.hpp" #include "../constants.hpp" -#include "../typedefs.hpp" #include "../util/timeutil.hpp" +#include + Lighting::Lighting(const Content* content, Chunks* chunks) - : content(content), chunks(chunks) { - auto indices = content->getIndices(); - solverR = std::make_unique(indices, chunks, 0); - solverG = std::make_unique(indices, chunks, 1); - solverB = std::make_unique(indices, chunks, 2); - solverS = std::make_unique(indices, chunks, 3); + : content(content), chunks(chunks) { + auto indices = content->getIndices(); + solverR = std::make_unique(indices, chunks, 0); + solverG = std::make_unique(indices, chunks, 1); + solverB = std::make_unique(indices, chunks, 2); + solverS = std::make_unique(indices, chunks, 3); } Lighting::~Lighting(){ } void Lighting::clear(){ - for (size_t index = 0; index < chunks->volume; index++){ - auto chunk = chunks->chunks[index]; - if (chunk == nullptr) - continue; - Lightmap& lightmap = chunk->lightmap; - for (int i = 0; i < CHUNK_VOL; i++){ - lightmap.map[i] = 0; - } - } + for (size_t index = 0; index < chunks->volume; index++){ + auto chunk = chunks->chunks[index]; + if (chunk == nullptr) + continue; + Lightmap& lightmap = chunk->lightmap; + for (int i = 0; i < CHUNK_VOL; i++){ + lightmap.map[i] = 0; + } + } } void Lighting::prebuildSkyLight(Chunk* chunk, const ContentIndices* indices){ - auto* blockDefs = indices->getBlockDefs(); + auto* blockDefs = indices->getBlockDefs(); - int highestPoint = 0; - for (int z = 0; z < CHUNK_D; z++){ - for (int x = 0; x < CHUNK_W; x++){ - for (int y = CHUNK_H-1; y >= 0; y--){ + int highestPoint = 0; + for (int z = 0; z < CHUNK_D; z++){ + for (int x = 0; x < CHUNK_W; x++){ + for (int y = CHUNK_H-1; y >= 0; y--){ int index = (y * CHUNK_D + z) * CHUNK_W + x; - voxel& vox = chunk->voxels[index]; - const Block* block = blockDefs[vox.id]; - if (!block->skyLightPassing) { - if (highestPoint < y) - highestPoint = y; - break; - } - chunk->lightmap.setS(x,y,z, 15); - } - } - } - if (highestPoint < CHUNK_H-1) - highestPoint++; - chunk->lightmap.highestPoint = highestPoint; + voxel& vox = chunk->voxels[index]; + const Block* block = blockDefs[vox.id]; + if (!block->skyLightPassing) { + if (highestPoint < y) + highestPoint = y; + break; + } + chunk->lightmap.setS(x,y,z, 15); + } + } + } + if (highestPoint < CHUNK_H-1) + highestPoint++; + chunk->lightmap.highestPoint = highestPoint; } void Lighting::buildSkyLight(int cx, int cz){ - const Block* const* blockDefs = content->getIndices()->getBlockDefs(); + const Block* const* blockDefs = content->getIndices()->getBlockDefs(); - Chunk* chunk = chunks->getChunk(cx, cz); - for (int z = 0; z < CHUNK_D; z++){ - for (int x = 0; x < CHUNK_W; x++){ - for (int y = chunk->lightmap.highestPoint; y >= 0; y--){ - int gx = x + cx * CHUNK_W; - int gz = z + cz * CHUNK_D; - while (y > 0 && !blockDefs[chunk->voxels[vox_index(x, y, z)].id]->lightPassing) { - y--; - } - if (chunk->lightmap.getS(x, y, z) != 15) { - solverS->add(gx,y+1,gz); - for (; y >= 0; y--){ - solverS->add(gx+1,y,gz); - solverS->add(gx-1,y,gz); - solverS->add(gx,y,gz+1); - solverS->add(gx,y,gz-1); - } - } - } - } - } - solverS->solve(); + Chunk* chunk = chunks->getChunk(cx, cz); + for (int z = 0; z < CHUNK_D; z++){ + for (int x = 0; x < CHUNK_W; x++){ + for (int y = chunk->lightmap.highestPoint; y >= 0; y--){ + int gx = x + cx * CHUNK_W; + int gz = z + cz * CHUNK_D; + while (y > 0 && !blockDefs[chunk->voxels[vox_index(x, y, z)].id]->lightPassing) { + y--; + } + if (chunk->lightmap.getS(x, y, z) != 15) { + solverS->add(gx,y+1,gz); + for (; y >= 0; y--){ + solverS->add(gx+1,y,gz); + solverS->add(gx-1,y,gz); + solverS->add(gx,y,gz+1); + solverS->add(gx,y,gz-1); + } + } + } + } + } + solverS->solve(); } void Lighting::onChunkLoaded(int cx, int cz, bool expand){ @@ -93,113 +92,113 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){ LightSolver* solverB = this->solverB.get(); LightSolver* solverS = this->solverS.get(); - const Block* const* blockDefs = content->getIndices()->getBlockDefs(); - const Chunk* chunk = chunks->getChunk(cx, cz); + const Block* const* blockDefs = content->getIndices()->getBlockDefs(); + const Chunk* chunk = chunks->getChunk(cx, cz); - for (uint y = 0; y < CHUNK_H; y++){ - for (uint z = 0; z < CHUNK_D; z++){ - for (uint x = 0; x < CHUNK_W; x++){ - const voxel& vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x]; - const Block* block = blockDefs[vox.id]; - int gx = x + cx * CHUNK_W; - int gz = z + cz * CHUNK_D; - if (block->rt.emissive){ - solverR->add(gx,y,gz,block->emission[0]); - solverG->add(gx,y,gz,block->emission[1]); - solverB->add(gx,y,gz,block->emission[2]); - } - } - } - } + for (uint y = 0; y < CHUNK_H; y++){ + for (uint z = 0; z < CHUNK_D; z++){ + for (uint x = 0; x < CHUNK_W; x++){ + const voxel& vox = chunk->voxels[(y * CHUNK_D + z) * CHUNK_W + x]; + const Block* block = blockDefs[vox.id]; + int gx = x + cx * CHUNK_W; + int gz = z + cz * CHUNK_D; + if (block->rt.emissive){ + solverR->add(gx,y,gz,block->emission[0]); + solverG->add(gx,y,gz,block->emission[1]); + solverB->add(gx,y,gz,block->emission[2]); + } + } + } + } - if (expand) { - for (int x = 0; x < CHUNK_W; x += CHUNK_W-1) { - for (int y = 0; y < CHUNK_H; y++) { - for (int z = 0; z < CHUNK_D; z++) { - int gx = x + cx * CHUNK_W; - int gz = z + cz * CHUNK_D; - int rgbs = chunk->lightmap.get(x, y, z); - if (rgbs){ - solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0)); - solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1)); - solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2)); - solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3)); - } - } - } - } - for (int z = 0; z < CHUNK_D; z += CHUNK_D-1) { - for (int y = 0; y < CHUNK_H; y++) { - for (int x = 0; x < CHUNK_W; x++) { - int gx = x + cx * CHUNK_W; - int gz = z + cz * CHUNK_D; - int rgbs = chunk->lightmap.get(x, y, z); - if (rgbs){ - solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0)); - solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1)); - solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2)); - solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3)); - } - } - } - } - } - solverR->solve(); - solverG->solve(); - solverB->solve(); - solverS->solve(); + if (expand) { + for (int x = 0; x < CHUNK_W; x += CHUNK_W-1) { + for (int y = 0; y < CHUNK_H; y++) { + for (int z = 0; z < CHUNK_D; z++) { + int gx = x + cx * CHUNK_W; + int gz = z + cz * CHUNK_D; + int rgbs = chunk->lightmap.get(x, y, z); + if (rgbs){ + solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0)); + solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1)); + solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2)); + solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3)); + } + } + } + } + for (int z = 0; z < CHUNK_D; z += CHUNK_D-1) { + for (int y = 0; y < CHUNK_H; y++) { + for (int x = 0; x < CHUNK_W; x++) { + int gx = x + cx * CHUNK_W; + int gz = z + cz * CHUNK_D; + int rgbs = chunk->lightmap.get(x, y, z); + if (rgbs){ + solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0)); + solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1)); + solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2)); + solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3)); + } + } + } + } + } + solverR->solve(); + solverG->solve(); + solverB->solve(); + solverS->solve(); } void Lighting::onBlockSet(int x, int y, int z, blockid_t id){ - Block* block = content->getIndices()->getBlockDef(id); - solverR->remove(x,y,z); - solverG->remove(x,y,z); - solverB->remove(x,y,z); + Block* block = content->getIndices()->getBlockDef(id); + solverR->remove(x,y,z); + solverG->remove(x,y,z); + solverB->remove(x,y,z); - if (id == 0){ - solverR->solve(); - solverG->solve(); - solverB->solve(); - if (chunks->getLight(x,y+1,z, 3) == 0xF){ - for (int i = y; i >= 0; i--){ - voxel* vox = chunks->get(x,i,z); - if ((vox == nullptr || vox->id != 0) && block->skyLightPassing) - break; - solverS->add(x,i,z, 0xF); - } - } - solverR->add(x,y+1,z); solverG->add(x,y+1,z); solverB->add(x,y+1,z); solverS->add(x,y+1,z); - solverR->add(x,y-1,z); solverG->add(x,y-1,z); solverB->add(x,y-1,z); solverS->add(x,y-1,z); - solverR->add(x+1,y,z); solverG->add(x+1,y,z); solverB->add(x+1,y,z); solverS->add(x+1,y,z); - solverR->add(x-1,y,z); solverG->add(x-1,y,z); solverB->add(x-1,y,z); solverS->add(x-1,y,z); - solverR->add(x,y,z+1); solverG->add(x,y,z+1); solverB->add(x,y,z+1); solverS->add(x,y,z+1); - solverR->add(x,y,z-1); solverG->add(x,y,z-1); solverB->add(x,y,z-1); solverS->add(x,y,z-1); - solverR->solve(); - solverG->solve(); - solverB->solve(); - solverS->solve(); - } else { - if (!block->skyLightPassing){ - solverS->remove(x,y,z); - for (int i = y-1; i >= 0; i--){ - solverS->remove(x,i,z); - if (i == 0 || chunks->get(x,i-1,z)->id != 0){ - break; - } - } - solverS->solve(); - } - solverR->solve(); - solverG->solve(); - solverB->solve(); + if (id == 0){ + solverR->solve(); + solverG->solve(); + solverB->solve(); + if (chunks->getLight(x,y+1,z, 3) == 0xF){ + for (int i = y; i >= 0; i--){ + voxel* vox = chunks->get(x,i,z); + if ((vox == nullptr || vox->id != 0) && block->skyLightPassing) + break; + solverS->add(x,i,z, 0xF); + } + } + solverR->add(x,y+1,z); solverG->add(x,y+1,z); solverB->add(x,y+1,z); solverS->add(x,y+1,z); + solverR->add(x,y-1,z); solverG->add(x,y-1,z); solverB->add(x,y-1,z); solverS->add(x,y-1,z); + solverR->add(x+1,y,z); solverG->add(x+1,y,z); solverB->add(x+1,y,z); solverS->add(x+1,y,z); + solverR->add(x-1,y,z); solverG->add(x-1,y,z); solverB->add(x-1,y,z); solverS->add(x-1,y,z); + solverR->add(x,y,z+1); solverG->add(x,y,z+1); solverB->add(x,y,z+1); solverS->add(x,y,z+1); + solverR->add(x,y,z-1); solverG->add(x,y,z-1); solverB->add(x,y,z-1); solverS->add(x,y,z-1); + solverR->solve(); + solverG->solve(); + solverB->solve(); + solverS->solve(); + } else { + if (!block->skyLightPassing){ + solverS->remove(x,y,z); + for (int i = y-1; i >= 0; i--){ + solverS->remove(x,i,z); + if (i == 0 || chunks->get(x,i-1,z)->id != 0){ + break; + } + } + solverS->solve(); + } + solverR->solve(); + solverG->solve(); + solverB->solve(); - if (block->emission[0] || block->emission[1] || block->emission[2]){ - solverR->add(x,y,z,block->emission[0]); - solverG->add(x,y,z,block->emission[1]); - solverB->add(x,y,z,block->emission[2]); - solverR->solve(); - solverG->solve(); - solverB->solve(); - } - } + if (block->emission[0] || block->emission[1] || block->emission[2]){ + solverR->add(x,y,z,block->emission[0]); + solverG->add(x,y,z,block->emission[1]); + solverB->add(x,y,z,block->emission[2]); + solverR->solve(); + solverG->solve(); + solverB->solve(); + } + } } diff --git a/src/lighting/Lighting.hpp b/src/lighting/Lighting.hpp index 83514206..ad867819 100644 --- a/src/lighting/Lighting.hpp +++ b/src/lighting/Lighting.hpp @@ -10,22 +10,22 @@ class Chunks; class LightSolver; class Lighting { - const Content* const content; - Chunks* chunks; - std::unique_ptr solverR; - std::unique_ptr solverG; - std::unique_ptr solverB; - std::unique_ptr solverS; + const Content* const content; + Chunks* chunks; + std::unique_ptr solverR; + std::unique_ptr solverG; + std::unique_ptr solverB; + std::unique_ptr solverS; public: - Lighting(const Content* content, Chunks* chunks); - ~Lighting(); + Lighting(const Content* content, Chunks* chunks); + ~Lighting(); - void clear(); - void buildSkyLight(int cx, int cz); - void onChunkLoaded(int cx, int cz, bool expand); - void onBlockSet(int x, int y, int z, blockid_t id); + void clear(); + void buildSkyLight(int cx, int cz); + void onChunkLoaded(int cx, int cz, bool expand); + void onBlockSet(int x, int y, int z, blockid_t id); - static void prebuildSkyLight(Chunk* chunk, const ContentIndices* indices); + static void prebuildSkyLight(Chunk* chunk, const ContentIndices* indices); }; #endif /* LIGHTING_LIGHTING_HPP_ */ diff --git a/src/lighting/Lightmap.hpp b/src/lighting/Lightmap.hpp index 0ff18ff9..43b6b4b0 100644 --- a/src/lighting/Lightmap.hpp +++ b/src/lighting/Lightmap.hpp @@ -87,4 +87,4 @@ public: static std::unique_ptr decode(const ubyte* buffer); }; -#endif /* LIGHTING_LIGHTMAP_HPP_ */ +#endif // LIGHTING_LIGHTMAP_HPP_ diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index 667fc073..e633831d 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -24,10 +24,10 @@ const uint MIN_SURROUNDING = 9; ChunksController::ChunksController(Level* level, uint padding) : level(level), - chunks(level->chunks.get()), - lighting(level->lighting.get()), - padding(padding), - generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) { + chunks(level->chunks.get()), + lighting(level->lighting.get()), + padding(padding), + generator(WorldGenerators::createGenerator(level->getWorld()->getGenerator(), level->content)) { } ChunksController::~ChunksController(){ @@ -37,7 +37,7 @@ void ChunksController::update(int64_t maxDuration) { int64_t mcstotal = 0; for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) { - timeutil::Timer timer; + timeutil::Timer timer; if (loadVisible()) { int64_t mcs = timer.stop(); if (mcstotal + mcs < maxDuration * 1000) { @@ -50,44 +50,44 @@ void ChunksController::update(int64_t maxDuration) { } bool ChunksController::loadVisible(){ - const int w = chunks->w; - const int d = chunks->d; + const int w = chunks->w; + const int d = chunks->d; - int nearX = 0; - int nearZ = 0; - int minDistance = ((w-padding*2)/2)*((w-padding*2)/2); - for (uint z = padding; z < d-padding; z++){ - for (uint x = padding; x < w-padding; x++){ - int index = z * w + x; - auto chunk = chunks->chunks[index]; - if (chunk != nullptr){ - if (chunk->isLoaded() && !chunk->isLighted()) { - if (buildLights(chunk)) { + int nearX = 0; + int nearZ = 0; + int minDistance = ((w-padding*2)/2)*((w-padding*2)/2); + for (uint z = padding; z < d-padding; z++){ + for (uint x = padding; x < w-padding; x++){ + int index = z * w + x; + auto chunk = chunks->chunks[index]; + if (chunk != nullptr){ + if (chunk->isLoaded() && !chunk->isLighted()) { + if (buildLights(chunk)) { return true; } - } - continue; - } - int lx = x - w / 2; - int lz = z - d / 2; - int distance = (lx * lx + lz * lz); - if (distance < minDistance){ - minDistance = distance; - nearX = x; - nearZ = z; - } - } - } + } + continue; + } + int lx = x - w / 2; + int lz = z - d / 2; + int distance = (lx * lx + lz * lz); + if (distance < minDistance){ + minDistance = distance; + nearX = x; + nearZ = z; + } + } + } - auto chunk = chunks->chunks[nearZ * w + nearX]; - if (chunk != nullptr) { - return false; - } + auto chunk = chunks->chunks[nearZ * w + nearX]; + if (chunk != nullptr) { + return false; + } const int ox = chunks->ox; - const int oz = chunks->oz; - createChunk(nearX+ox, nearZ+oz); - return true; + const int oz = chunks->oz; + createChunk(nearX+ox, nearZ+oz); + return true; } bool ChunksController::buildLights(std::shared_ptr chunk) { @@ -112,22 +112,22 @@ bool ChunksController::buildLights(std::shared_ptr chunk) { void ChunksController::createChunk(int x, int z) { auto chunk = level->chunksStorage->create(x, z); - chunks->putChunk(chunk); + chunks->putChunk(chunk); - if (!chunk->isLoaded()) { - generator->generate( + if (!chunk->isLoaded()) { + generator->generate( chunk->voxels, x, z, level->getWorld()->getSeed() ); - chunk->setUnsaved(true); - } - chunk->updateHeights(); + chunk->setUnsaved(true); + } + chunk->updateHeights(); - if (!chunk->isLoadedLights()) { - Lighting::prebuildSkyLight( + if (!chunk->isLoadedLights()) { + Lighting::prebuildSkyLight( chunk.get(), level->content->getIndices() ); - } + } chunk->setLoaded(true); - chunk->setReady(true); + chunk->setReady(true); } diff --git a/src/logic/ChunksController.hpp b/src/logic/ChunksController.hpp index 71d028f8..97c403d4 100644 --- a/src/logic/ChunksController.hpp +++ b/src/logic/ChunksController.hpp @@ -31,4 +31,4 @@ public: void update(int64_t maxDuration); }; -#endif /* VOXELS_CHUNKSCONTROLLER_HPP_ */ +#endif // VOXELS_CHUNKSCONTROLLER_HPP_ diff --git a/src/logic/EngineController.cpp b/src/logic/EngineController.cpp index 0e0a9541..8a2be95e 100644 --- a/src/logic/EngineController.cpp +++ b/src/logic/EngineController.cpp @@ -110,8 +110,8 @@ static void loadWorld(Engine* engine, fs::path folder) { auto& packs = engine->getContentPacks(); auto& settings = engine->getSettings(); - Level* level = World::load(folder, settings, content, packs); - engine->setScreen(std::make_shared(engine, level)); + auto level = World::load(folder, settings, content, packs); + engine->setScreen(std::make_shared(engine, std::move(level))); } catch (const world_load_error& error) { guiutil::alert( engine->getGUI(), langs::get(L"Error")+L": "+ @@ -197,13 +197,13 @@ void EngineController::createWorld( return; } - Level* level = World::create( + auto level = World::create( name, generatorID, folder, seed, engine->getSettings(), engine->getContent(), engine->getContentPacks() ); - engine->setScreen(std::make_shared(engine, level)); + engine->setScreen(std::make_shared(engine, std::move(level))); } void EngineController::reopenWorld(World* world) { diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index 58f5ea1c..5e0cb198 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -10,18 +10,18 @@ static debug::Logger logger("level-control"); -LevelController::LevelController(EngineSettings& settings, Level* level) - : settings(settings), level(level), - blocks(std::make_unique(level, settings.chunks.padding.get())), - chunks(std::make_unique(level, settings.chunks.padding.get())), - player(std::make_unique(level, settings, blocks.get())) { +LevelController::LevelController(EngineSettings& settings, std::unique_ptr level) + : settings(settings), level(std::move(level)), + blocks(std::make_unique(this->level.get(), settings.chunks.padding.get())), + chunks(std::make_unique(this->level.get(), settings.chunks.padding.get())), + player(std::make_unique(this->level.get(), settings, blocks.get())) { scripting::on_world_load(this); } void LevelController::update(float delta, bool input, bool pause) { player->update(delta, input, pause); - glm::vec3 position = player->getPlayer()->hitbox->position; + glm::vec3 position = player->getPlayer()->hitbox->position; level->loadMatrix(position.x, position.z, settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2); diff --git a/src/logic/LevelController.hpp b/src/logic/LevelController.hpp index 93d608c8..4c23ad77 100644 --- a/src/logic/LevelController.hpp +++ b/src/logic/LevelController.hpp @@ -20,7 +20,7 @@ class LevelController { std::unique_ptr chunks; std::unique_ptr player; public: - LevelController(EngineSettings& settings, Level* level); + LevelController(EngineSettings& settings, std::unique_ptr level); /// @param delta time elapsed since the last update /// @param input is user input allowed to be handled diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index f6b4f103..91bda4e8 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -34,7 +34,6 @@ const float RUN_ZOOM = 1.1f; const float C_ZOOM = 0.1f; const float CROUCH_SHIFT_Y = -0.2f; - CameraControl::CameraControl(std::shared_ptr player, const CameraSettings& settings) : player(player), camera(player->camera), @@ -136,7 +135,7 @@ void CameraControl::switchCamera() { } } -void CameraControl::update(PlayerInput& input, float delta, Chunks* chunks) { +void CameraControl::update(const PlayerInput& input, float delta, Chunks* chunks) { offset = glm::vec3(0.0f, 0.7f, 0.0f); if (settings.shaking.get() && !input.cheat) { diff --git a/src/logic/PlayerController.hpp b/src/logic/PlayerController.hpp index f74a988e..bc2d1fb7 100644 --- a/src/logic/PlayerController.hpp +++ b/src/logic/PlayerController.hpp @@ -1,14 +1,14 @@ #ifndef PLAYER_CONTROL_HPP_ #define PLAYER_CONTROL_HPP_ +#include "../settings.hpp" +#include "../objects/Player.hpp" + #include #include #include #include -#include "../settings.hpp" -#include "../objects/Player.hpp" - class Camera; class Level; class Block; @@ -38,7 +38,7 @@ class CameraControl { public: CameraControl(std::shared_ptr player, const CameraSettings& settings); void updateMouse(PlayerInput& input); - void update(PlayerInput& input, float delta, Chunks* chunks); + void update(const PlayerInput& input, float delta, Chunks* chunks); void refresh(); }; @@ -55,7 +55,7 @@ using on_block_interaction = std::function player; - PlayerInput input; + PlayerInput input {}; CameraControl camControl; BlocksController* blocksController; diff --git a/src/logic/scripting/scripting_functional.cpp b/src/logic/scripting/scripting_functional.cpp index aa6ce4ff..7597ce66 100644 --- a/src/logic/scripting/scripting_functional.cpp +++ b/src/logic/scripting/scripting_functional.cpp @@ -34,7 +34,7 @@ static bool processCallback( try { return state->eval(*env, src, file) != 0; } catch (lua::luaerror& err) { - std::cerr << err.what() << std::endl; + logger.error() << err.what(); return false; } } diff --git a/src/logic/scripting/scripting_functional.hpp b/src/logic/scripting/scripting_functional.hpp index 6ab338ce..2ac0e767 100644 --- a/src/logic/scripting/scripting_functional.hpp +++ b/src/logic/scripting/scripting_functional.hpp @@ -1,11 +1,11 @@ #ifndef LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_HPP_ #define LOGIC_SCRIPTING_SCRIPTING_FUNCTIONAL_HPP_ -#include -#include #include "../../typedefs.hpp" #include "../../delegates.hpp" +#include + namespace scripting { runnable create_runnable( const scriptenv& env, diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 81e23b40..36f75e30 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -12,8 +12,11 @@ #include "../items/Inventory.hpp" #include "../items/Inventories.hpp" -Level::Level(World* world, const Content* content, EngineSettings& settings) - : world(world), +Level::Level( + std::unique_ptr world, + const Content* content, + EngineSettings& settings +) : world(std::move(world)), content(content), chunksStorage(std::make_unique(this)), physics(std::make_unique(glm::vec3(0, -22.6f, 0))), @@ -21,7 +24,7 @@ Level::Level(World* world, const Content* content, EngineSettings& settings) settings(settings) { auto inv = std::make_shared( - world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE + this->world->getNextInventoryId(), DEF_PLAYER_INVENTORY_SIZE ); auto player = spawnObject( glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, inv @@ -32,7 +35,7 @@ Level::Level(World* world, const Content* content, EngineSettings& settings) settings.chunks.padding.get() ) * 2; chunks = std::make_unique( - matrixSize, matrixSize, 0, 0, world->wfile.get(), events.get(), content + matrixSize, matrixSize, 0, 0, this->world->wfile.get(), events.get(), content ); lighting = std::make_unique(content, chunks.get()); diff --git a/src/world/Level.hpp b/src/world/Level.hpp index d8248153..6b8f35c4 100644 --- a/src/world/Level.hpp +++ b/src/world/Level.hpp @@ -38,7 +38,11 @@ public: const EngineSettings& settings; - Level(World* world, const Content* content, EngineSettings& settings); + Level( + std::unique_ptr world, + const Content* content, + EngineSettings& settings + ); ~Level(); void loadMatrix(int32_t x, int32_t z, uint32_t radius); diff --git a/src/world/LevelEvents.hpp b/src/world/LevelEvents.hpp index bdbe07ab..2cfed6f6 100644 --- a/src/world/LevelEvents.hpp +++ b/src/world/LevelEvents.hpp @@ -11,7 +11,7 @@ enum lvl_event_type { EVT_CHUNK_HIDDEN, }; -typedef std::function chunk_event_func; +using chunk_event_func = std::function; class LevelEvents { std::unordered_map> chunk_callbacks; diff --git a/src/world/World.cpp b/src/world/World.cpp index 887307b9..6fb17b1e 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -1,8 +1,10 @@ #include "World.hpp" #include "Level.hpp" + #include "../content/Content.hpp" #include "../content/ContentLUT.hpp" +#include "../debug/Logger.hpp" #include "../files/WorldFiles.hpp" #include "../items/Inventories.hpp" #include "../objects/Player.hpp" @@ -13,9 +15,10 @@ #include "../world/WorldGenerators.hpp" #include -#include #include +static debug::Logger logger("world"); + world_load_error::world_load_error(std::string message) : std::runtime_error(message) { } @@ -64,35 +67,38 @@ void World::write(Level* level) { } wfile->write(this, content); - auto playerFile = dynamic::Map(); - { - auto& players = playerFile.putList("players"); - for (auto object : level->objects) { - if (std::shared_ptr player = std::dynamic_pointer_cast(object)) { - players.put(player->serialize()); - } + auto playerFile = dynamic::Map(); + + auto& players = playerFile.putList("players"); + for (auto object : level->objects) { + if (auto player = std::dynamic_pointer_cast(object)) { + players.put(player->serialize()); } } files::write_json(wfile->getPlayerFile(), &playerFile); } -Level* World::create(std::string name, - std::string generator, - fs::path directory, - uint64_t seed, - EngineSettings& settings, - const Content* content, - const std::vector& packs +std::unique_ptr World::create( + std::string name, + std::string generator, + fs::path directory, + uint64_t seed, + EngineSettings& settings, + const Content* content, + const std::vector& packs ) { - auto world = new World(name, generator, directory, seed, settings, content, packs); - auto level = new Level(world, content, settings); - return level; + auto world = std::make_unique( + name, generator, directory, seed, settings, content, packs + ); + return std::make_unique(std::move(world), content, settings); } -Level* World::load(fs::path directory, - EngineSettings& settings, - const Content* content, - const std::vector& packs) { +std::unique_ptr World::load( + fs::path directory, + EngineSettings& settings, + const Content* content, + const std::vector& packs +) { auto world = std::make_unique( ".", WorldGenerators::getDefaultGeneratorID(), directory, 0, settings, content, packs ); @@ -102,23 +108,27 @@ Level* World::load(fs::path directory, throw world_load_error("could not to find world.json"); } - auto level = new Level(world.get(), content, settings); + auto level = std::make_unique(std::move(world), content, settings); { fs::path file = wfile->getPlayerFile(); if (!fs::is_regular_file(file)) { - std::cerr << "warning: player.json does not exists" << std::endl; + logger.warning() << "player.json does not exists"; } else { auto playerFile = files::read_json(file); if (playerFile->has("players")) { level->objects.clear(); auto players = playerFile->list("players"); for (size_t i = 0; i < players->size(); i++) { - auto player = level->spawnObject(glm::vec3(0, DEF_PLAYER_Y, 0), DEF_PLAYER_SPEED, level->inventories->create(DEF_PLAYER_INVENTORY_SIZE)); + auto player = level->spawnObject( + glm::vec3(0, DEF_PLAYER_Y, 0), + DEF_PLAYER_SPEED, + level->inventories->create(DEF_PLAYER_INVENTORY_SIZE) + ); player->deserialize(players->map(i)); level->inventories->store(player->getInventory()); } } else { - auto player = level->getObject(0); + auto player = level->getObject(0); player->deserialize(playerFile.get()); level->inventories->store(player->getInventory()); } @@ -128,8 +138,10 @@ Level* World::load(fs::path directory, return level; } -ContentLUT* World::checkIndices(const fs::path& directory, - const Content* content) { +std::shared_ptr World::checkIndices( + const fs::path& directory, + const Content* content +) { fs::path indicesFile = directory/fs::path("indices.json"); if (fs::is_regular_file(indicesFile)) { return ContentLUT::create(indicesFile, content); @@ -178,10 +190,9 @@ void World::deserialize(dynamic::Map* root) { generator = root->get("generator", generator); seed = root->get("seed", seed); - if(generator == "") { + if (generator == "") { generator = WorldGenerators::getDefaultGeneratorID(); } - auto verobj = root->map("version"); if (verobj) { int major=0, minor=-1; @@ -189,14 +200,12 @@ void World::deserialize(dynamic::Map* root) { verobj->num("minor", minor); std::cout << "world version: " << major << "." << minor << std::endl; } - auto timeobj = root->map("time"); if (timeobj) { timeobj->num("day-time", daytime); timeobj->num("day-time-speed", daytimeSpeed); timeobj->num("total-time", totalTime); } - nextInventoryId = root->get("next-inventory-id", 2); } diff --git a/src/world/World.hpp b/src/world/World.hpp index 1c5d4e70..57d94956 100644 --- a/src/world/World.hpp +++ b/src/world/World.hpp @@ -12,13 +12,10 @@ #include #include #include -#include class Content; class WorldFiles; -class Chunks; class Level; -class Player; class ContentLUT; namespace fs = std::filesystem; @@ -75,7 +72,9 @@ public: /// @param directory world directory /// @param content current Content instance /// @return ContentLUT if world convert required else nullptr - static ContentLUT* checkIndices(const fs::path& directory, const Content* content); + static std::shared_ptr checkIndices( + const fs::path& directory, const Content* content + ); /// @brief Create new world /// @param name internal world name @@ -87,7 +86,7 @@ public: /// with all world content-packs applied /// @param packs vector of all world content-packs /// @return Level instance containing World instance - static Level* create( + static std::unique_ptr create( std::string name, std::string generator, fs::path directory, @@ -105,7 +104,7 @@ public: /// @param packs vector of all world content-packs /// @return Level instance containing World instance /// @throws world_load_error on world.json load error - static Level* load( + static std::unique_ptr load( fs::path directory, EngineSettings& settings, const Content* content, @@ -148,4 +147,4 @@ public: void deserialize(dynamic::Map *src) override; }; -#endif /* WORLD_WORLD_HPP_ */ +#endif // WORLD_WORLD_HPP_