From 245b39be62a2b5c779e8ddef50bf9a0d8936a681 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 4 Aug 2024 01:12:42 +0300 Subject: [PATCH] refactor: PVS-Studio warnings fixes --- src/audio/AL/ALAudio.cpp | 12 +- src/content/Content.hpp | 14 +- src/content/ContentPack.cpp | 6 +- src/engine.cpp | 4 +- src/frontend/ContentGfxCache.cpp | 2 +- src/frontend/LevelFrontend.cpp | 4 +- src/graphics/render/WorldRenderer.cpp | 248 +++++++++++---------- src/graphics/ui/elements/InventoryView.cpp | 12 +- src/lighting/Lighting.cpp | 14 +- src/logic/BlocksController.cpp | 28 +-- src/logic/BlocksController.hpp | 8 +- src/logic/PlayerController.cpp | 44 ++-- src/logic/PlayerController.hpp | 2 +- src/logic/scripting/lua/libblock.cpp | 11 +- src/logic/scripting/lua/libentity.cpp | 5 +- src/logic/scripting/lua/libitem.cpp | 5 +- src/logic/scripting/scripting.cpp | 46 ++-- src/logic/scripting/scripting.hpp | 18 +- src/voxels/Block.hpp | 4 +- src/voxels/Chunks.cpp | 110 ++++----- src/voxels/Chunks.hpp | 10 +- src/voxels/ChunksStorage.cpp | 6 +- 22 files changed, 311 insertions(+), 302 deletions(-) diff --git a/src/audio/AL/ALAudio.cpp b/src/audio/AL/ALAudio.cpp index 18af5705..ed84a59c 100644 --- a/src/audio/AL/ALAudio.cpp +++ b/src/audio/AL/ALAudio.cpp @@ -96,8 +96,9 @@ void ALStream::bindSpeaker(speakerid_t speakerid) { this->speaker = speakerid; sp = audio::get_speaker(speakerid); if (sp) { - auto alspeaker = dynamic_cast(sp); //FIXME: Potentional null pointer - alspeaker->stream = this; //-V522 + auto alspeaker = dynamic_cast(sp); + assert(alspeaker != nullptr); // backends must not be mixed + alspeaker->stream = this; alspeaker->duration = source->getTotalDuration(); } } @@ -145,12 +146,13 @@ void ALStream::update(double delta) { } auto p_speaker = audio::get_speaker(this->speaker); if (p_speaker == nullptr) { - p_speaker = 0; //FIXME: Not used + this->speaker = 0; return; } - ALSpeaker* alspeaker = dynamic_cast(p_speaker); //FIXME: Potentional null pointer + ALSpeaker* alspeaker = dynamic_cast(p_speaker); + assert(alspeaker != nullptr); if (alspeaker->stopped) { //-V522 - p_speaker = 0; //FIXME: Not used + this->speaker = 0; return; } diff --git a/src/content/Content.hpp b/src/content/Content.hpp index 1f5245be..eb007399 100644 --- a/src/content/Content.hpp +++ b/src/content/Content.hpp @@ -59,13 +59,25 @@ public: ContentUnitIndices(std::vector defs) : defs(std::move(defs)) { } - inline T* get(blockid_t id) const { + inline const T* get(blockid_t id) const { if (id >= defs.size()) { return nullptr; } return defs[id]; } + [[deprecated]] + inline T* getWriteable(blockid_t id) const { // TODO: remove + if (id >= defs.size()) { + return nullptr; + } + return defs[id]; + } + + inline const T& require(blockid_t id) const { + return *defs.at(id); + } + inline size_t count() const { return defs.size(); } diff --git a/src/content/ContentPack.cpp b/src/content/ContentPack.cpp index ae65646e..6fdd83e3 100644 --- a/src/content/ContentPack.cpp +++ b/src/content/ContentPack.cpp @@ -132,11 +132,7 @@ fs::path ContentPack::findPack( if (fs::is_directory(folder)) { return folder; } - folder = paths->getResources() / fs::path("content") / fs::path(name); - if (fs::is_directory(folder)) { - return folder; //-V523 - } - return folder; // FIXME: V523 The 'then' statement is equivalent to the subsequent code fragment //-V523 + return paths->getResources() / fs::path("content") / fs::path(name); } ContentPackRuntime::ContentPackRuntime(ContentPack info, scriptenv env) diff --git a/src/engine.cpp b/src/engine.cpp index bc0681d0..de4f7b4d 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -268,8 +268,8 @@ void Engine::loadAssets() { // no need // correct log messages order is more useful - bool threading = false; - if (threading) { // TODO: Why is always false? + bool threading = false; // look at two upper lines + if (threading) { auto task = loader.startTask([=](){}); task->waitForEnd(); } else { diff --git a/src/frontend/ContentGfxCache.cpp b/src/frontend/ContentGfxCache.cpp index b10a360f..e77032ce 100644 --- a/src/frontend/ContentGfxCache.cpp +++ b/src/frontend/ContentGfxCache.cpp @@ -18,7 +18,7 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : conte auto atlas = assets->get("blocks"); for (uint i = 0; i < indices->blocks.count(); i++) { - Block* def = indices->blocks.get(i); //FIXME: Potential null pointer + auto def = indices->blocks.getWriteable(i); //FIXME: Potential null pointer for (uint side = 0; side < 6; side++) { const std::string& tex = def->textureFaces[side]; //-V522 if (atlas->has(tex)) { diff --git a/src/frontend/LevelFrontend.cpp b/src/frontend/LevelFrontend.cpp index e5cadff5..d67c9261 100644 --- a/src/frontend/LevelFrontend.cpp +++ b/src/frontend/LevelFrontend.cpp @@ -25,8 +25,8 @@ LevelFrontend::LevelFrontend( "block-previews" ); controller->getBlocksController()->listenBlockInteraction( - [=](Player* player, glm::ivec3 pos, const Block* def, BlockInteraction type) { - auto material = level->content->findBlockMaterial(def->material); + [=](Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type) { + auto material = level->content->findBlockMaterial(def.material); if (material == nullptr) { return; } diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 8a7b77b8..08defc67 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -1,8 +1,12 @@ #include "WorldRenderer.hpp" -#include "ChunksRenderer.hpp" -#include "ModelBatch.hpp" -#include "Skybox.hpp" +#include +#include + +#include +#include +#include +#include #include "../../assets/Assets.hpp" #include "../../content/Content.hpp" @@ -15,8 +19,8 @@ #include "../../logic/scripting/scripting_hud.hpp" #include "../../maths/FrustumCulling.hpp" #include "../../maths/voxmaths.hpp" -#include "../../objects/Player.hpp" #include "../../objects/Entities.hpp" +#include "../../objects/Player.hpp" #include "../../settings.hpp" #include "../../voxels/Block.hpp" #include "../../voxels/Chunk.hpp" @@ -31,46 +35,41 @@ #include "../core/DrawContext.hpp" #include "../core/LineBatch.hpp" #include "../core/Mesh.hpp" +#include "../core/Model.hpp" #include "../core/PostProcessing.hpp" #include "../core/Shader.hpp" #include "../core/Texture.hpp" -#include "../core/Model.hpp" - -#include -#include -#include -#include - -#include -#include +#include "ChunksRenderer.hpp" +#include "ModelBatch.hpp" +#include "Skybox.hpp" bool WorldRenderer::showChunkBorders = false; bool WorldRenderer::showEntitiesDebug = false; -WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player) - : engine(engine), - level(frontend->getLevel()), - player(player), - frustumCulling(std::make_unique()), - lineBatch(std::make_unique()), - modelBatch(std::make_unique(20'000, engine->getAssets(), level->chunks.get())) -{ +WorldRenderer::WorldRenderer( + Engine* engine, LevelFrontend* frontend, Player* player +) + : engine(engine), + level(frontend->getLevel()), + player(player), + frustumCulling(std::make_unique()), + lineBatch(std::make_unique()), + modelBatch(std::make_unique( + 20'000, engine->getAssets(), level->chunks.get() + )) { renderer = std::make_unique( - level, - frontend->getContentGfxCache(), - &engine->getSettings() + level, frontend->getContentGfxCache(), &engine->getSettings() ); batch3d = std::make_unique(4096); auto& settings = engine->getSettings(); - level->events->listen(EVT_CHUNK_HIDDEN, - [this](lvl_event_type, Chunk* chunk) { - renderer->unload(chunk); - } + level->events->listen( + EVT_CHUNK_HIDDEN, + [this](lvl_event_type, Chunk* chunk) { renderer->unload(chunk); } ); auto assets = engine->getAssets(); skybox = std::make_unique( - settings.graphics.skyboxResolution.get(), + settings.graphics.skyboxResolution.get(), assets->get("skybox_gen") ); } @@ -78,41 +77,35 @@ WorldRenderer::WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* pl WorldRenderer::~WorldRenderer() = default; bool WorldRenderer::drawChunk( - size_t index, - Camera* camera, - Shader* shader, - bool culling -){ + size_t index, Camera* camera, Shader* shader, bool culling +) { auto chunk = level->chunks->chunks[index]; if (!chunk->flags.lighted) { return false; } float distance = glm::distance( - camera->position, - glm::vec3((chunk->x + 0.5f) * CHUNK_W, - camera->position.y, - (chunk->z + 0.5f) * CHUNK_D) + camera->position, + glm::vec3( + (chunk->x + 0.5f) * CHUNK_W, + camera->position.y, + (chunk->z + 0.5f) * CHUNK_D + ) ); - auto mesh = renderer->getOrRender(chunk, distance < CHUNK_W*1.5f); + auto mesh = renderer->getOrRender(chunk, distance < CHUNK_W * 1.5f); if (mesh == nullptr) { return false; } - if (culling){ - glm::vec3 min( - chunk->x * CHUNK_W, - chunk->bottom, - chunk->z * CHUNK_D - ); + if (culling) { + glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D); glm::vec3 max( - chunk->x * CHUNK_W + CHUNK_W, - chunk->top, + chunk->x * CHUNK_W + CHUNK_W, + chunk->top, chunk->z * CHUNK_D + CHUNK_D ); - if (!frustumCulling->isBoxVisible(min, max)) - return false; + if (!frustumCulling->isBoxVisible(min, max)) return false; } - glm::vec3 coord(chunk->x*CHUNK_W+0.5f, 0.5f, chunk->z*CHUNK_D+0.5f); + glm::vec3 coord(chunk->x * CHUNK_W + 0.5f, 0.5f, chunk->z * CHUNK_D + 0.5f); glm::mat4 model = glm::translate(glm::mat4(1.0f), coord); shader->uniformMatrix("u_model", model); mesh->draw(); @@ -125,9 +118,8 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) { // [warning] this whole method is not thread-safe for chunks std::vector indices; - for (size_t i = 0; i < chunks->volume; i++){ - if (chunks->chunks[i] == nullptr) - continue; + for (size_t i = 0; i < chunks->volume; i++) { + if (chunks->chunks[i] == nullptr) continue; indices.emplace_back(i); } float px = camera->position.x / (float)CHUNK_W - 0.5f; @@ -139,20 +131,22 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) { auto adz = (a->z - pz); auto bdx = (b->x - px); auto bdz = (b->z - pz); - return (adx*adx + adz*adz > bdx*bdx + bdz*bdz); + return (adx * adx + adz * adz > bdx * bdx + bdz * bdz); }); bool culling = engine->getSettings().graphics.frustumCulling.get(); if (culling) { frustumCulling->update(camera->getProjView()); } chunks->visible = 0; - for (size_t i = 0; i < indices.size(); i++){ + for (size_t i = 0; i < indices.size(); i++) { chunks->visible += drawChunk(indices[i], camera, shader, culling); } } void WorldRenderer::setupWorldShader( - Shader* shader, Camera* camera, const EngineSettings& settings, + Shader* shader, + Camera* camera, + const EngineSettings& settings, float fogFactor ) { shader->use(); @@ -172,12 +166,13 @@ void WorldRenderer::setupWorldShader( { auto inventory = player->getInventory(); ItemStack& stack = inventory->getSlot(player->getChosenSlot()); - auto item = indices->items.get(stack.getItemId()); //FIXME: Potentional null pointer + auto& item = indices->items.require(stack.getItemId()); float multiplier = 0.5f; - shader->uniform3f("u_torchlightColor", - item->emission[0] / 15.0f * multiplier, //-V522 - item->emission[1] / 15.0f * multiplier, - item->emission[2] / 15.0f * multiplier + shader->uniform3f( + "u_torchlightColor", + item.emission[0] / 15.0f * multiplier, + item.emission[1] / 15.0f * multiplier, + item.emission[2] / 15.0f * multiplier ); shader->uniform1f("u_torchlightDistance", 6.0f); } @@ -185,7 +180,7 @@ void WorldRenderer::setupWorldShader( void WorldRenderer::renderLevel( const DrawContext&, - Camera* camera, + Camera* camera, const EngineSettings& settings, float delta, bool pause @@ -194,7 +189,7 @@ void WorldRenderer::renderLevel( auto atlas = assets->get("blocks"); bool culling = engine->getSettings().graphics.frustumCulling.get(); - float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get()-2); + float fogFactor = 15.0f / ((float)settings.chunks.loadDistance.get() - 2); auto shader = assets->get("main"); setupWorldShader(shader, camera, settings, fogFactor); @@ -208,7 +203,12 @@ void WorldRenderer::renderLevel( setupWorldShader(entityShader, camera, settings, fogFactor); level->entities->render( - assets, *modelBatch, culling ? frustumCulling.get() : nullptr, delta, pause); + assets, + *modelBatch, + culling ? frustumCulling.get() : nullptr, + delta, + pause + ); if (!pause) { scripting::on_frontend_render(); @@ -222,22 +222,26 @@ void WorldRenderer::renderBlockSelection() { const auto& selection = player->selection; auto indices = level->content->getIndices(); blockid_t id = selection.vox.id; - auto block = indices->blocks.get(id); //FIXME: Potentional null pointer + auto& block = indices->blocks.require(id); const glm::ivec3 pos = player->selection.position; const glm::vec3 point = selection.hitPosition; const glm::vec3 norm = selection.normal; - const std::vector& hitboxes = block->rotatable //-V522 - ? block->rt.hitboxes[selection.vox.state.rotation] - : block->hitboxes; + const std::vector& hitboxes = + block.rotatable ? block.rt.hitboxes[selection.vox.state.rotation] + : block.hitboxes; lineBatch->lineWidth(2.0f); - for (auto& hitbox: hitboxes) { + for (auto& hitbox : hitboxes) { const glm::vec3 center = glm::vec3(pos) + hitbox.center(); const glm::vec3 size = hitbox.size(); - lineBatch->box(center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f)); + lineBatch->box( + center, size + glm::vec3(0.02), glm::vec4(0.f, 0.f, 0.f, 0.5f) + ); if (player->debug) { - lineBatch->line(point, point+norm*0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f)); + lineBatch->line( + point, point + norm * 0.5f, glm::vec4(1.0f, 0.0f, 1.0f, 1.0f) + ); } } lineBatch->flush(); @@ -255,25 +259,24 @@ void WorldRenderer::renderLines( auto ctx = pctx.sub(lineBatch.get()); bool culling = engine->getSettings().graphics.frustumCulling.get(); level->entities->renderDebug( - *lineBatch, culling ? frustumCulling.get() : nullptr, ctx); + *lineBatch, culling ? frustumCulling.get() : nullptr, ctx + ); } } void WorldRenderer::renderDebugLines( - const DrawContext& pctx, - Camera* camera, - Shader* linesShader + const DrawContext& pctx, Camera* camera, Shader* linesShader ) { DrawContext ctx = pctx.sub(lineBatch.get()); const auto& viewport = ctx.getViewport(); uint displayWidth = viewport.getWidth(); uint displayHeight = viewport.getHeight(); - + ctx.setDepthTest(true); linesShader->use(); - if (showChunkBorders){ + if (showChunkBorders) { linesShader->uniformMatrix("u_projview", camera->getProjView()); glm::vec3 coord = player->camera->position; if (coord.x < 0) coord.x--; @@ -282,18 +285,29 @@ void WorldRenderer::renderDebugLines( int cz = floordiv(static_cast(coord.z), CHUNK_D); drawBorders( - cx * CHUNK_W, 0, cz * CHUNK_D, - (cx + 1) * CHUNK_W, CHUNK_H, (cz + 1) * CHUNK_D + cx * CHUNK_W, + 0, + cz * CHUNK_D, + (cx + 1) * CHUNK_W, + CHUNK_H, + (cz + 1) * CHUNK_D ); } float length = 40.f; - glm::vec3 tsl(displayWidth/2, displayHeight/2, 0.f); + glm::vec3 tsl(displayWidth / 2, displayHeight / 2, 0.f); glm::mat4 model(glm::translate(glm::mat4(1.f), tsl)); - linesShader->uniformMatrix("u_projview", glm::ortho( - 0.f, static_cast(displayWidth), - 0.f, static_cast(displayHeight), - -length, length) * model * glm::inverse(camera->rotation) + linesShader->uniformMatrix( + "u_projview", + glm::ortho( + 0.f, + static_cast(displayWidth), + 0.f, + static_cast(displayHeight), + -length, + length + ) * model * + glm::inverse(camera->rotation) ); ctx.setDepthTest(false); @@ -311,24 +325,24 @@ void WorldRenderer::renderDebugLines( } void WorldRenderer::draw( - const DrawContext& pctx, - Camera* camera, + const DrawContext& pctx, + Camera* camera, bool hudVisible, bool pause, float delta, PostProcessing* postProcessing -){ +) { timer += delta * !pause; auto world = level->getWorld(); const Viewport& vp = pctx.getViewport(); camera->aspect = vp.getWidth() / static_cast(vp.getHeight()); const EngineSettings& settings = engine->getSettings(); - skybox->refresh(pctx, world->daytime, 1.0f+world->fog*2.0f, 4); + skybox->refresh(pctx, world->daytime, 1.0f + world->fog * 2.0f, 4); auto assets = engine->getAssets(); auto linesShader = assets->get("lines"); - + // World render scope with diegetic HUD included { DrawContext wctx = pctx.sub(); @@ -338,7 +352,7 @@ void WorldRenderer::draw( // Drawing background sky plane skybox->draw(pctx, camera, assets, world->daytime, world->fog); - + // Actually world render with depth buffer on { DrawContext ctx = wctx.sub(); @@ -346,7 +360,7 @@ void WorldRenderer::draw( ctx.setCullFace(true); renderLevel(ctx, camera, settings, delta, pause); // Debug lines - if (hudVisible){ + if (hudVisible) { renderLines(camera, linesShader, ctx); } } @@ -356,7 +370,7 @@ void WorldRenderer::draw( } } - // Rendering fullscreen quad with + // Rendering fullscreen quad with auto screenShader = assets->get("screen"); screenShader->use(); screenShader->uniform1f("u_timer", timer); @@ -364,40 +378,30 @@ void WorldRenderer::draw( postProcessing->render(pctx, screenShader); } -void WorldRenderer::drawBorders(int sx, int sy, int sz, int ex, int ey, int ez) { - int ww = ex-sx; - int dd = ez-sz; +void WorldRenderer::drawBorders( + int sx, int sy, int sz, int ex, int ey, int ez +) { + int ww = ex - sx; + int dd = ez - sz; /*corner*/ { - lineBatch->line(sx, sy, sz, - sx, ey, sz, 0.8f, 0, 0.8f, 1); - lineBatch->line(sx, sy, ez, - sx, ey, ez, 0.8f, 0, 0.8f, 1); - lineBatch->line(ex, sy, sz, - ex, ey, sz, 0.8f, 0, 0.8f, 1); - lineBatch->line(ex, sy, ez, - ex, ey, ez, 0.8f, 0, 0.8f, 1); + lineBatch->line(sx, sy, sz, sx, ey, sz, 0.8f, 0, 0.8f, 1); + lineBatch->line(sx, sy, ez, sx, ey, ez, 0.8f, 0, 0.8f, 1); + lineBatch->line(ex, sy, sz, ex, ey, sz, 0.8f, 0, 0.8f, 1); + lineBatch->line(ex, sy, ez, ex, ey, ez, 0.8f, 0, 0.8f, 1); } - for (int i = 2; i < ww; i+=2) { - lineBatch->line(sx + i, sy, sz, - sx + i, ey, sz, 0, 0, 0.8f, 1); - lineBatch->line(sx + i, sy, ez, - sx + i, ey, ez, 0, 0, 0.8f, 1); + for (int i = 2; i < ww; i += 2) { + lineBatch->line(sx + i, sy, sz, sx + i, ey, sz, 0, 0, 0.8f, 1); + lineBatch->line(sx + i, sy, ez, sx + i, ey, ez, 0, 0, 0.8f, 1); } - for (int i = 2; i < dd; i+=2) { - lineBatch->line(sx, sy, sz + i, - sx, ey, sz + i, 0.8f, 0, 0, 1); - lineBatch->line(ex, sy, sz + i, - ex, ey, sz + i, 0.8f, 0, 0, 1); + for (int i = 2; i < dd; i += 2) { + lineBatch->line(sx, sy, sz + i, sx, ey, sz + i, 0.8f, 0, 0, 1); + lineBatch->line(ex, sy, sz + i, ex, ey, sz + i, 0.8f, 0, 0, 1); } - for (int i = sy; i < ey; i+=2){ - lineBatch->line(sx, i, sz, - sx, i, ez, 0, 0.8f, 0, 1); - lineBatch->line(sx, i, ez, - ex, i, ez, 0, 0.8f, 0, 1); - lineBatch->line(ex, i, ez, - ex, i, sz, 0, 0.8f, 0, 1); - lineBatch->line(ex, i, sz, - sx, i, sz, 0, 0.8f, 0, 1); + for (int i = sy; i < ey; i += 2) { + lineBatch->line(sx, i, sz, sx, i, ez, 0, 0.8f, 0, 1); + lineBatch->line(sx, i, ez, ex, i, ez, 0, 0.8f, 0, 1); + lineBatch->line(ex, i, ez, ex, i, sz, 0, 0.8f, 0, 1); + lineBatch->line(ex, i, sz, sx, i, sz, 0, 0.8f, 0, 1); } lineBatch->flush(); } diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index cb7e6b71..a0644a3f 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -159,12 +159,12 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { auto previews = assets->get("block-previews"); auto indices = content->getIndices(); - ItemDef* item = indices->items.get(stack.getItemId()); //FIXME: Potentional null pointer - switch (item->iconType) { //-V522 + auto& item = indices->items.require(stack.getItemId()); //FIXME: Potentional null pointer + switch (item.iconType) { case item_icon_type::none: break; case item_icon_type::block: { - const Block& cblock = content->blocks.require(item->icon); + const Block& cblock = content->blocks.require(item.icon); batch->texture(previews->getTexture()); UVRegion region = previews->get(cblock.name); @@ -174,13 +174,13 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) { break; } case item_icon_type::sprite: { - size_t index = item->icon.find(':'); - std::string name = item->icon.substr(index+1); + size_t index = item.icon.find(':'); + std::string name = item.icon.substr(index+1); UVRegion region(0.0f, 0.0, 1.0f, 1.0f); if (index == std::string::npos) { batch->texture(assets->get(name)); } else { - std::string atlasname = item->icon.substr(0, index); + std::string atlasname = item.icon.substr(0, index); auto atlas = assets->get(atlasname); if (atlas && atlas->has(name)) { region = atlas->get(name); diff --git a/src/lighting/Lighting.cpp b/src/lighting/Lighting.cpp index e6cfbd54..6c00af35 100644 --- a/src/lighting/Lighting.cpp +++ b/src/lighting/Lighting.cpp @@ -149,7 +149,7 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){ } void Lighting::onBlockSet(int x, int y, int z, blockid_t id){ - Block* block = content->getIndices()->blocks.get(id); //FIXME: Potentional null pointer + const auto& block = content->getIndices()->blocks.require(id); solverR->remove(x,y,z); solverG->remove(x,y,z); solverB->remove(x,y,z); @@ -161,7 +161,7 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){ 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) //-V522 + if ((vox == nullptr || vox->id != 0) && block.skyLightPassing) break; solverS->add(x,i,z, 0xF); } @@ -177,7 +177,7 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){ solverB->solve(); solverS->solve(); } else { - if (!block->skyLightPassing){ + if (!block.skyLightPassing){ solverS->remove(x,y,z); for (int i = y-1; i >= 0; i--){ solverS->remove(x,i,z); @@ -191,10 +191,10 @@ void Lighting::onBlockSet(int x, int y, int z, blockid_t id){ 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]); + 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/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 31cd9685..d72258fc 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -34,7 +34,7 @@ void BlocksController::updateSides(int x, int y, int z) { } void BlocksController::breakBlock( - Player* player, const Block* def, int x, int y, int z + Player* player, const Block& def, int x, int y, int z ) { onBlockInteraction( player, glm::ivec3(x, y, z), def, BlockInteraction::destruction @@ -46,14 +46,14 @@ void BlocksController::breakBlock( } void BlocksController::placeBlock( - Player* player, const Block* def, blockstate state, int x, int y, int z + Player* player, const Block& def, blockstate state, int x, int y, int z ) { 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); - if (def->rt.funcsset.onplaced) { + chunks->set(x, y, z, def.rt.id, state); + lighting->onBlockSet(x, y, z, def.rt.id); + if (def.rt.funcsset.onplaced) { scripting::on_block_placed(player, def, x, y, z); } updateSides(x, y, z); @@ -62,15 +62,15 @@ void BlocksController::placeBlock( void BlocksController::updateBlock(int x, int y, int z) { voxel* vox = chunks->get(x, y, z); if (vox == nullptr) return; - auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer - if (def->grounded) { //-V522 + 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)) { breakBlock(nullptr, def, x, y, z); return; } } - if (def->rt.funcsset.update) { + if (def.rt.funcsset.update) { scripting::update_block(def, x, y, z); } } @@ -93,9 +93,9 @@ void BlocksController::onBlocksTick(int tickid, int parts) { int tickRate = blocksTickClock.getTickRate(); for (size_t id = 0; id < indices->blocks.count(); id++) { if ((id + tickid) % parts != 0) continue; - auto def = indices->blocks.get(id); //FIXME: Potentional null pointer - auto interval = def->tickInterval; //-V522 - if (def->rt.funcsset.onblockstick && tickid / parts % interval == 0) { + auto& def = indices->blocks.require(id); + auto interval = def.tickInterval; + if (def.rt.funcsset.onblockstick && tickid / parts % interval == 0) { scripting::on_blocks_tick(def, tickRate / interval); } } @@ -112,8 +112,8 @@ void BlocksController::randomTick( 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); //FIXME: Potentional null pointer - if (block->rt.funcsset.randupdate) { //-V522 + auto& block = indices->blocks.require(vox.id); + if (block.rt.funcsset.randupdate) { scripting::random_update_block( block, chunk.x * CHUNK_W + bx, by, chunk.z * CHUNK_D + bz ); @@ -188,7 +188,7 @@ void BlocksController::unbindInventory(int x, int y, int z) { } void BlocksController::onBlockInteraction( - Player* player, glm::ivec3 pos, const Block* def, BlockInteraction type + Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type ) { for (const auto& callback : blockInteractionCallbacks) { callback(player, pos, def, type); diff --git a/src/logic/BlocksController.hpp b/src/logic/BlocksController.hpp index e2fcf0d4..f2a74738 100644 --- a/src/logic/BlocksController.hpp +++ b/src/logic/BlocksController.hpp @@ -21,7 +21,7 @@ enum class BlockInteraction { step, destruction, placing }; /// @brief Player argument is nullable using on_block_interaction = std::function< - void(Player*, glm::ivec3, const Block*, BlockInteraction type)>; + void(Player*, glm::ivec3, const Block&, BlockInteraction type)>; /// BlocksController manages block updates and data (inventories, metadata) class BlocksController { @@ -40,9 +40,9 @@ public: void updateSides(int x, int y, int z); void updateBlock(int x, int y, int z); - void breakBlock(Player* player, const Block* def, int x, int y, int z); + void breakBlock(Player* player, const Block& def, int x, int y, int z); void placeBlock( - Player* player, const Block* def, blockstate state, int x, int y, int z + Player* player, const Block& def, blockstate state, int x, int y, int z ); void update(float delta); @@ -56,7 +56,7 @@ public: void unbindInventory(int x, int y, int z); void onBlockInteraction( - Player* player, glm::ivec3 pos, const Block* def, BlockInteraction type + Player* player, glm::ivec3 pos, const Block& def, BlockInteraction type ); /// @brief Add block interaction callback diff --git a/src/logic/PlayerController.cpp b/src/logic/PlayerController.cpp index 839a568c..71779ae7 100644 --- a/src/logic/PlayerController.cpp +++ b/src/logic/PlayerController.cpp @@ -208,8 +208,10 @@ void PlayerController::onFootstep(const Hitbox& hitbox) { int z = std::floor(pos.z + half.z * offsetZ); auto vox = level->chunks->get(x, y, z); if (vox) { - auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer - if (!def->obstacle) continue; //-V522 + auto& def = level->content->getIndices()->blocks.require(vox->id); + if (!def.obstacle) { + continue; + } blocksController->onBlockInteraction( player.get(), glm::ivec3(x, y, z), @@ -299,7 +301,7 @@ void PlayerController::updatePlayer(float delta) { } static int determine_rotation( - Block* def, const glm::ivec3& norm, glm::vec3& camDir + const Block* def, const glm::ivec3& norm, glm::vec3& camDir ) { if (def && def->rotatable) { const std::string& name = def->rotations.name; @@ -396,7 +398,7 @@ voxel* PlayerController::updateSelection(float maxDistance) { selection.vox = *vox; if (selectedState.segment) { selection.position = chunks->seekOrigin( - iend, indices->blocks.get(selection.vox.id), selectedState + iend, indices->blocks.require(selection.vox.id), selectedState ); auto origin = chunks->get(selection.position); if (origin && origin->id != vox->id) { @@ -411,15 +413,15 @@ voxel* PlayerController::updateSelection(float maxDistance) { return vox; } -void PlayerController::processRightClick(Block* def, Block* target) { +void PlayerController::processRightClick(const Block& def, const Block& target) { const auto& selection = player->selection; auto chunks = level->chunks.get(); auto camera = player->camera.get(); blockstate state {}; - state.rotation = determine_rotation(def, selection.normal, camera->dir); + state.rotation = determine_rotation(&def, selection.normal, camera->dir); - if (!input.shift && target->rt.funcsset.oninteract) { + if (!input.shift && target.rt.funcsset.oninteract) { if (scripting::on_block_interact( player.get(), target, selection.position )) { @@ -427,17 +429,17 @@ void PlayerController::processRightClick(Block* def, Block* target) { } } auto coord = selection.actualPosition; - if (!target->replaceable) { + if (!target.replaceable) { coord += selection.normal; - } else if (def->rotations.name == BlockRotProfile::PIPE_NAME) { + } else if (def.rotations.name == BlockRotProfile::PIPE_NAME) { state.rotation = BLOCK_DIR_UP; } - blockid_t chosenBlock = def->rt.id; + blockid_t chosenBlock = def.rt.id; AABB blockAABB(coord, coord + 1); bool blocked = level->entities->hasBlockingInside(blockAABB); - if (def->obstacle && blocked) { + if (def.obstacle && blocked) { return; } auto vox = chunks->get(coord); @@ -447,7 +449,7 @@ void PlayerController::processRightClick(Block* def, Block* target) { if (!chunks->checkReplaceability(def, state, coord)) { return; } - if (def->grounded) { + if (def.grounded) { const auto& vec = get_ground_direction(def, state.rotation); if (!chunks->isSolidBlock( coord.x + vec.x, coord.y + vec.y, coord.z + vec.z @@ -492,11 +494,11 @@ void PlayerController::updateInteraction() { auto inventory = player->getInventory(); const ItemStack& stack = inventory->getSlot(player->getChosenSlot()); - ItemDef* item = indices->items.get(stack.getItemId()); //FIXME: Potentional null pointer + auto& item = indices->items.require(stack.getItemId()); auto vox = updateSelection(maxDistance); if (vox == nullptr) { - if (rclick && item->rt.funcsset.on_use) { //-V522 + if (rclick && item.rt.funcsset.on_use) { scripting::on_item_use(player.get(), item); } if (selection.entity) { @@ -506,35 +508,35 @@ void PlayerController::updateInteraction() { } auto iend = selection.position; - if (lclick && !input.shift && item->rt.funcsset.on_block_break_by) { + if (lclick && !input.shift && item.rt.funcsset.on_block_break_by) { if (scripting::on_item_break_block( player.get(), item, iend.x, iend.y, iend.z )) { return; } } - auto target = indices->blocks.get(vox->id); //FIXME: Potentional null pointer - if (lclick && target->breakable) { //-V522 + auto& target = indices->blocks.require(vox->id); + if (lclick && target.breakable) { blocksController->breakBlock( player.get(), target, iend.x, iend.y, iend.z ); } if (rclick && !input.shift) { bool preventDefault = false; - if (item->rt.funcsset.on_use_on_block) { + if (item.rt.funcsset.on_use_on_block) { preventDefault = scripting::on_item_use_on_block( player.get(), item, iend, selection.normal ); - } else if (item->rt.funcsset.on_use) { + } else if (item.rt.funcsset.on_use) { preventDefault = scripting::on_item_use(player.get(), item); } if (preventDefault) { return; } } - auto def = indices->blocks.get(item->rt.placingBlock); + auto def = indices->blocks.get(item.rt.placingBlock); if (def && rclick) { - processRightClick(def, target); + processRightClick(*def, target); } if (Events::jactive(BIND_PLAYER_PICK)) { auto coord = selection.actualPosition; diff --git a/src/logic/PlayerController.hpp b/src/logic/PlayerController.hpp index 5df66dc3..e115e97c 100644 --- a/src/logic/PlayerController.hpp +++ b/src/logic/PlayerController.hpp @@ -61,7 +61,7 @@ class PlayerController { float stepsTimer = 0.0f; void onFootstep(const Hitbox& hitbox); void updateFootsteps(float delta); - void processRightClick(Block* def, Block* target); + void processRightClick(const Block& def, const Block& target); voxel* updateSelection(float maxDistance); public: diff --git a/src/logic/scripting/lua/libblock.cpp b/src/logic/scripting/lua/libblock.cpp index ea73df72..4e535b57 100644 --- a/src/logic/scripting/lua/libblock.cpp +++ b/src/logic/scripting/lua/libblock.cpp @@ -11,12 +11,9 @@ using namespace scripting; -static Block* require_block(lua::State* L) { +static const Block* require_block(lua::State* L) { auto indices = content->getIndices(); auto id = lua::tointeger(L, 1); - if (static_cast(id) >= indices->blocks.count()) { - return nullptr; - } return indices->blocks.get(id); } @@ -78,7 +75,7 @@ static int l_seek_origin(lua::State* L) { auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); auto vox = level->chunks->get(x, y, z); - auto def = indices->blocks.get(vox->id); + auto& def = indices->blocks.require(vox->id); return lua::pushivec3_stack( L, level->chunks->seekOrigin({x, y, z}, def, vox->state) ); @@ -330,7 +327,7 @@ static int l_place(lua::State* L) { } auto player = level->getObject(playerid); controller->getBlocksController()->placeBlock( - player ? player.get() : nullptr, def, int2blockstate(state), x, y, z + player ? player.get() : nullptr, *def, int2blockstate(state), x, y, z ); return 0; } @@ -344,7 +341,7 @@ static int l_destruct(lua::State* L) { if (voxel == nullptr) { return 0; } - const auto def = level->content->getIndices()->blocks.get(voxel->id); + auto& def = level->content->getIndices()->blocks.require(voxel->id); auto player = level->getObject(playerid); controller->getBlocksController()->breakBlock( player ? player.get() : nullptr, def, x, y, z diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index ec24f54f..9e87cc11 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -12,12 +12,9 @@ using namespace scripting; -static EntityDef* require_entity_def(lua::State* L) { +static const EntityDef* require_entity_def(lua::State* L) { auto indices = content->getIndices(); auto id = lua::tointeger(L, 1); - if (static_cast(id) >= indices->entities.count()) { - return nullptr; - } return indices->entities.get(id); } diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libitem.cpp index 1213620f..099172e9 100644 --- a/src/logic/scripting/lua/libitem.cpp +++ b/src/logic/scripting/lua/libitem.cpp @@ -4,12 +4,9 @@ using namespace scripting; -static ItemDef* get_item_def(lua::State* L, int idx) { +static const ItemDef* get_item_def(lua::State* L, int idx) { auto indices = content->getIndices(); auto id = lua::tointeger(L, idx); - if (static_cast(id) >= indices->items.count()) { - return nullptr; - } return indices->items.get(id); } diff --git a/src/logic/scripting/scripting.cpp b/src/logic/scripting/scripting.cpp index 04f3bb75..d928ad93 100644 --- a/src/logic/scripting/scripting.cpp +++ b/src/logic/scripting/scripting.cpp @@ -196,38 +196,38 @@ void scripting::on_world_quit() { scripting::controller = nullptr; } -void scripting::on_blocks_tick(const Block* block, int tps) { - std::string name = block->name + ".blockstick"; +void scripting::on_blocks_tick(const Block& block, int tps) { + std::string name = block.name + ".blockstick"; lua::emit_event(lua::get_main_thread(), name, [tps](auto L) { return lua::pushinteger(L, tps); }); } -void scripting::update_block(const Block* block, int x, int y, int z) { - std::string name = block->name + ".update"; +void scripting::update_block(const Block& block, int x, int y, int z) { + std::string name = block.name + ".update"; lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) { return lua::pushivec3_stack(L, x, y, z); }); } -void scripting::random_update_block(const Block* block, int x, int y, int z) { - std::string name = block->name + ".randupdate"; +void scripting::random_update_block(const Block& block, int x, int y, int z) { + std::string name = block.name + ".randupdate"; lua::emit_event(lua::get_main_thread(), name, [x, y, z](auto L) { return lua::pushivec3_stack(L, x, y, z); }); } void scripting::on_block_placed( - Player* player, const Block* block, int x, int y, int z + Player* player, const Block& block, int x, int y, int z ) { - std::string name = block->name + ".placed"; + std::string name = block.name + ".placed"; lua::emit_event(lua::get_main_thread(), name, [x, y, z, player](auto L) { lua::pushivec3_stack(L, x, y, z); lua::pushinteger(L, player ? player->getId() : -1); return 4; }); - auto world_event_args = [block, x, y, z, player](lua::State* L) { - lua::pushinteger(L, block->rt.id); + auto world_event_args = [&](lua::State* L) { + lua::pushinteger(L, block.rt.id); lua::pushivec3_stack(L, x, y, z); lua::pushinteger(L, player ? player->getId() : -1); return 5; @@ -244,10 +244,10 @@ void scripting::on_block_placed( } void scripting::on_block_broken( - Player* player, const Block* block, int x, int y, int z + Player* player, const Block& block, int x, int y, int z ) { - if (block->rt.funcsset.onbroken) { - std::string name = block->name + ".broken"; + if (block.rt.funcsset.onbroken) { + std::string name = block.name + ".broken"; lua::emit_event( lua::get_main_thread(), name, @@ -258,8 +258,8 @@ void scripting::on_block_broken( } ); } - auto world_event_args = [block, x, y, z, player](lua::State* L) { - lua::pushinteger(L, block->rt.id); + auto world_event_args = [&](lua::State* L) { + lua::pushinteger(L, block.rt.id); lua::pushivec3_stack(L, x, y, z); lua::pushinteger(L, player ? player->getId() : -1); return 5; @@ -276,9 +276,9 @@ void scripting::on_block_broken( } bool scripting::on_block_interact( - Player* player, const Block* block, glm::ivec3 pos + Player* player, const Block& block, glm::ivec3 pos ) { - std::string name = block->name + ".interact"; + std::string name = block.name + ".interact"; return lua::emit_event(lua::get_main_thread(), name, [pos, player](auto L) { lua::pushivec3_stack(L, pos.x, pos.y, pos.z); lua::pushinteger(L, player->getId()); @@ -286,8 +286,8 @@ bool scripting::on_block_interact( }); } -bool scripting::on_item_use(Player* player, const ItemDef* item) { - std::string name = item->name + ".use"; +bool scripting::on_item_use(Player* player, const ItemDef& item) { + std::string name = item.name + ".use"; return lua::emit_event( lua::get_main_thread(), name, @@ -296,9 +296,9 @@ bool scripting::on_item_use(Player* player, const ItemDef* item) { } bool scripting::on_item_use_on_block( - Player* player, const ItemDef* item, glm::ivec3 ipos, glm::ivec3 normal + Player* player, const ItemDef& item, glm::ivec3 ipos, glm::ivec3 normal ) { - std::string name = item->name + ".useon"; + std::string name = item.name + ".useon"; return lua::emit_event( lua::get_main_thread(), name, @@ -312,9 +312,9 @@ bool scripting::on_item_use_on_block( } bool scripting::on_item_break_block( - Player* player, const ItemDef* item, int x, int y, int z + Player* player, const ItemDef& item, int x, int y, int z ) { - std::string name = item->name + ".blockbreakby"; + std::string name = item.name + ".blockbreakby"; return lua::emit_event( lua::get_main_thread(), name, diff --git a/src/logic/scripting/scripting.hpp b/src/logic/scripting/scripting.hpp index e389320d..85959b82 100644 --- a/src/logic/scripting/scripting.hpp +++ b/src/logic/scripting/scripting.hpp @@ -61,31 +61,31 @@ namespace scripting { void on_world_tick(); void on_world_save(); void on_world_quit(); - void on_blocks_tick(const Block* block, int tps); - void update_block(const Block* block, int x, int y, int z); - void random_update_block(const Block* block, int x, int y, int z); + void on_blocks_tick(const Block& block, int tps); + void update_block(const Block& block, int x, int y, int z); + void random_update_block(const Block& block, int x, int y, int z); void on_block_placed( - Player* player, const Block* block, int x, int y, int z + Player* player, const Block& block, int x, int y, int z ); void on_block_broken( - Player* player, const Block* block, int x, int y, int z + Player* player, const Block& block, int x, int y, int z ); - bool on_block_interact(Player* player, const Block* block, glm::ivec3 pos); + bool on_block_interact(Player* player, const Block& block, glm::ivec3 pos); /// @brief Called on RMB click with the item selected /// @return true if prevents default action - bool on_item_use(Player* player, const ItemDef* item); + bool on_item_use(Player* player, const ItemDef& item); /// @brief Called on RMB click on block with the item selected /// @return true if prevents default action bool on_item_use_on_block( - Player* player, const ItemDef* item, glm::ivec3 ipos, glm::ivec3 normal + Player* player, const ItemDef& item, glm::ivec3 ipos, glm::ivec3 normal ); /// @brief Called on LMB click on block with the item selected /// @return true if prevents default action bool on_item_break_block( - Player* player, const ItemDef* item, int x, int y, int z + Player* player, const ItemDef& item, int x, int y, int z ); dynamic::Value get_component_value( diff --git a/src/voxels/Block.hpp b/src/voxels/Block.hpp index 28a01389..9a01bb44 100644 --- a/src/voxels/Block.hpp +++ b/src/voxels/Block.hpp @@ -214,8 +214,8 @@ public: Block(const Block&) = delete; }; -inline glm::ivec3 get_ground_direction(const Block* def, int rotation) { - return -def->rotations.variants[rotation].axisY; +inline glm::ivec3 get_ground_direction(const Block& def, int rotation) { + return -def.rotations.variants[rotation].axisY; } #endif // VOXELS_BLOCK_HPP_ diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 06a868dd..014712c6 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -76,15 +76,15 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) { return ∅ } } - const auto def = indices->blocks.get(v->id); //FIXME: Potentional null pointer - if (def->obstacle) { //-V522 + const auto& def = indices->blocks.require(v->id); + if (def.obstacle) { glm::ivec3 offset {}; if (v->state.segment) { glm::ivec3 point(ix, iy, iz); offset = seekOrigin(point, def, v->state) - point; } - const auto& boxes = def->rotatable ? def->rt.hitboxes[v->state.rotation] - : def->hitboxes; + const auto& boxes = + def.rotatable ? def.rt.hitboxes[v->state.rotation] : def.hitboxes; for (const auto& hitbox : boxes) { if (hitbox.contains( {x - ix - offset.x, y - iy - offset.y, z - iz - offset.z} @@ -99,19 +99,19 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) { bool Chunks::isSolidBlock(int32_t x, int32_t y, int32_t z) { voxel* v = get(x, y, z); if (v == nullptr) return false; - return indices->blocks.get(v->id)->rt.solid; //-V522 + return indices->blocks.get(v->id)->rt.solid; //-V522 } bool Chunks::isReplaceableBlock(int32_t x, int32_t y, int32_t z) { voxel* v = get(x, y, z); if (v == nullptr) return false; - return indices->blocks.get(v->id)->replaceable; //-V522 + return indices->blocks.get(v->id)->replaceable; //-V522 } bool Chunks::isObstacleBlock(int32_t x, int32_t y, int32_t z) { voxel* v = get(x, y, z); if (v == nullptr) return false; - return indices->blocks.get(v->id)->obstacle; //-V522 + return indices->blocks.get(v->id)->obstacle; //-V522 } ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel) { @@ -173,9 +173,9 @@ Chunk* Chunks::getChunk(int x, int z) { } glm::ivec3 Chunks::seekOrigin( - glm::ivec3 pos, const Block* def, blockstate state + glm::ivec3 pos, const Block& def, blockstate state ) { - const auto& rotation = def->rotations.variants[state.rotation]; + const auto& rotation = def.rotations.variants[state.rotation]; auto segment = state.segment; while (true) { if (!segment) { @@ -194,12 +194,12 @@ glm::ivec3 Chunks::seekOrigin( } void Chunks::eraseSegments( - const Block* def, blockstate state, int x, int y, int z + const Block& def, blockstate state, int x, int y, int z ) { - const auto& rotation = def->rotations.variants[state.rotation]; - for (int sy = 0; sy < def->size.y; sy++) { - for (int sz = 0; sz < def->size.z; sz++) { - for (int sx = 0; sx < def->size.x; sx++) { + const auto& rotation = def.rotations.variants[state.rotation]; + for (int sy = 0; sy < def.size.y; sy++) { + for (int sz = 0; sz < def.size.z; sz++) { + for (int sx = 0; sx < def.size.x; sx++) { if ((sx | sy | sz) == 0) { continue; } @@ -218,11 +218,11 @@ static constexpr uint8_t segment_to_int(int sx, int sy, int sz) { } void Chunks::repairSegments( - const Block* def, blockstate state, int x, int y, int z + const Block& def, blockstate state, int x, int y, int z ) { - const auto& rotation = def->rotations.variants[state.rotation]; - const auto id = def->rt.id; - const auto size = def->size; + const auto& rotation = def.rotations.variants[state.rotation]; + const auto id = def.rt.id; + const auto size = def.size; for (int sy = 0; sy < size.y; sy++) { for (int sz = 0; sz < size.z; sz++) { for (int sx = 0; sx < size.x; sx++) { @@ -243,10 +243,10 @@ void Chunks::repairSegments( } bool Chunks::checkReplaceability( - const Block* def, blockstate state, glm::ivec3 origin, blockid_t ignore + const Block& def, blockstate state, glm::ivec3 origin, blockid_t ignore ) { - const auto& rotation = def->rotations.variants[state.rotation]; - const auto size = def->size; + const auto& rotation = def.rotations.variants[state.rotation]; + const auto size = def.size; for (int sy = 0; sy < size.y; sy++) { for (int sz = 0; sz < size.z; sz++) { for (int sx = 0; sx < size.x; sx++) { @@ -255,8 +255,8 @@ bool Chunks::checkReplaceability( pos += rotation.axisY * sy; pos += rotation.axisZ * sz; if (auto vox = get(pos.x, pos.y, pos.z)) { - auto target = indices->blocks.get(vox->id); //FIXME: Potentional null pointer - if (!target->replaceable && vox->id != ignore) { //-V522 + auto& target = indices->blocks.require(vox->id); + if (!target.replaceable && vox->id != ignore) { return false; } } else { @@ -269,18 +269,18 @@ bool Chunks::checkReplaceability( } void Chunks::setRotationExtended( - Block* def, blockstate state, glm::ivec3 origin, uint8_t index + const Block& def, blockstate state, glm::ivec3 origin, uint8_t index ) { auto newstate = state; newstate.rotation = index; // unable to rotate block (cause: obstacles) - if (!checkReplaceability(def, newstate, origin, def->rt.id)) { + if (!checkReplaceability(def, newstate, origin, def.rt.id)) { return; } - const auto& rotation = def->rotations.variants[index]; - const auto size = def->size; + const auto& rotation = def.rotations.variants[index]; + const auto size = def.size; std::vector segmentBlocks; for (int sy = 0; sy < size.y; sy++) { @@ -296,18 +296,19 @@ void Chunks::setRotationExtended( auto vox = get(pos); // checked for nullptr by checkReplaceability - if (vox->id != def->rt.id) { - set(pos.x, pos.y, pos.z, def->rt.id, segState); + if (vox->id != def.rt.id) { + set(pos.x, pos.y, pos.z, def.rt.id, segState); } else { vox->state = segState; - auto chunk = getChunkByVoxel(pos.x, pos.y, pos.z); //FIXME: Potentional null pointer - chunk->setModifiedAndUnsaved(); //-V522 + auto chunk = getChunkByVoxel(pos.x, pos.y, pos.z); + assert(chunk != nullptr); + chunk->setModifiedAndUnsaved(); segmentBlocks.emplace_back(pos); } } } } - const auto& prevRotation = def->rotations.variants[state.rotation]; + const auto& prevRotation = def.rotations.variants[state.rotation]; for (int sy = 0; sy < size.y; sy++) { for (int sz = 0; sz < size.z; sz++) { for (int sx = 0; sx < size.x; sx++) { @@ -333,16 +334,17 @@ void Chunks::setRotation(int32_t x, int32_t y, int32_t z, uint8_t index) { if (vox == nullptr) { return; } - auto def = indices->blocks.get(vox->id); //FIXME: Potentional null pointer - if (!def->rotatable || vox->state.rotation == index) { //-V522 + auto& def = indices->blocks.require(vox->id); + if (!def.rotatable || vox->state.rotation == index) { return; } - if (def->rt.extended) { + if (def.rt.extended) { setRotationExtended(def, vox->state, {x, y, z}, index); } else { vox->state.rotation = index; - auto chunk = getChunkByVoxel(x, y, z); //FIXME: Potentional null pointer - chunk->setModifiedAndUnsaved(); //-V522 + auto chunk = getChunkByVoxel(x, y, z); + assert(chunk != nullptr); + chunk->setModifiedAndUnsaved(); } } @@ -371,20 +373,20 @@ void Chunks::set( // block finalization voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx]; - auto prevdef = indices->blocks.get(vox.id); //FIXME: Potentional null pointer - if (prevdef->inventorySize == 0) { //-V522 + const auto& prevdef = indices->blocks.require(vox.id); + if (prevdef.inventorySize == 0) { chunk->removeBlockInventory(lx, y, lz); } - if (prevdef->rt.extended && !vox.state.segment) { + if (prevdef.rt.extended && !vox.state.segment) { eraseSegments(prevdef, vox.state, gx, y, gz); } // block initialization - auto newdef = indices->blocks.get(id); //FIXME: Potentional null pointer + const auto& newdef = indices->blocks.require(id); vox.id = id; vox.state = state; chunk->setModifiedAndUnsaved(); - if (!state.segment && newdef->rt.extended) { //-V522 + if (!state.segment && newdef.rt.extended) { repairSegments(newdef, state, gx, y, gz); } @@ -432,7 +434,7 @@ voxel* Chunks::rayCast( int stepz = (dz > 0.0f) ? 1 : -1; constexpr float infinity = std::numeric_limits::infinity(); - constexpr float epsilon = 1e-6f; // 0.000001 + constexpr float epsilon = 1e-6f; // 0.000001 float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx); float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy); float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz); @@ -452,8 +454,8 @@ voxel* Chunks::rayCast( if (voxel == nullptr) { return nullptr; } - const auto def = indices->blocks.get(voxel->id); //FIXME: Potentional null pointer - if (def->selectable) { //-V522 + const auto& def = indices->blocks.require(voxel->id); + if (def.selectable) { end.x = px + t * dx; end.y = py + t * dy; end.z = pz + t * dz; @@ -461,10 +463,10 @@ voxel* Chunks::rayCast( iend.y = iy; iend.z = iz; - if (!def->rt.solid) { + if (!def.rt.solid) { const std::vector& hitboxes = - def->rotatable ? def->rt.hitboxes[voxel->state.rotation] - : def->hitboxes; + def.rotatable ? def.rt.hitboxes[voxel->state.rotation] + : def.hitboxes; scalar_t distance = maxDist; Ray ray(start, dir); @@ -563,7 +565,7 @@ glm::vec3 Chunks::rayCastToObstacle( int stepz = (dz > 0.0f) ? 1 : -1; constexpr float infinity = std::numeric_limits::infinity(); - constexpr float epsilon = 1e-6f; // 0.000001 + constexpr float epsilon = 1e-6f; // 0.000001 float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx); float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy); float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz); @@ -579,12 +581,12 @@ glm::vec3 Chunks::rayCastToObstacle( while (t <= maxDist) { voxel* voxel = get(ix, iy, iz); if (voxel) { - const auto def = indices->blocks.get(voxel->id); //FIXME: Potentional null pointer - if (def->obstacle) { //-V522 - if (!def->rt.solid) { + const auto& def = indices->blocks.require(voxel->id); + if (def.obstacle) { + if (!def.rt.solid) { const std::vector& hitboxes = - def->rotatable ? def->rt.hitboxes[voxel->state.rotation] - : def->modelBoxes; + def.rotatable ? def.rt.hitboxes[voxel->state.rotation] + : def.modelBoxes; scalar_t distance; glm::ivec3 norm; diff --git a/src/voxels/Chunks.hpp b/src/voxels/Chunks.hpp index 795b6046..fbc1b9d1 100644 --- a/src/voxels/Chunks.hpp +++ b/src/voxels/Chunks.hpp @@ -26,12 +26,12 @@ class Chunks { Level* level; const ContentIndices* const indices; - void eraseSegments(const Block* def, blockstate state, int x, int y, int z); + void eraseSegments(const Block& def, blockstate state, int x, int y, int z); void repairSegments( - const Block* def, blockstate state, int x, int y, int z + const Block& def, blockstate state, int x, int y, int z ); void setRotationExtended( - Block* def, blockstate state, glm::ivec3 origin, uint8_t rotation + const Block& def, blockstate state, glm::ivec3 origin, uint8_t rotation ); public: std::vector> chunks; @@ -72,7 +72,7 @@ public: /// @param def segment block definition /// @param state segment block state /// @return origin block position or `pos` if block is not extended - glm::ivec3 seekOrigin(glm::ivec3 pos, const Block* def, blockstate state); + glm::ivec3 seekOrigin(glm::ivec3 pos, const Block& def, blockstate state); /// @brief Check if required zone is replaceable /// @param def definition of the block that requires a replaceable zone @@ -80,7 +80,7 @@ public: /// @param coord position of the zone start /// @param ignore ignored block id (will be counted as replaceable) bool checkReplaceability( - const Block* def, + const Block& def, blockstate state, glm::ivec3 coord, blockid_t ignore = 0 diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index 0f3bfc97..389ac3b8 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -149,9 +149,9 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const { voxels[vidx] = cvoxels[cidx]; light_t light = clights[cidx]; if (backlight) { - auto block = - indices->blocks.get(voxels[vidx].id); //FIXME: Potentional null pointer - if (block->lightPassing) { //-V522 + const auto& block = + indices->blocks.require(voxels[vidx].id); + if (block.lightPassing) { //-V522 light = Lightmap::combine( min(15, Lightmap::extract(light, 0) + 1),