Merge pull request #282 from MihailRis/PVS-Studio-warnings-fixes

PVS studio warnings fix & refactor
This commit is contained in:
MihailRis 2024-08-04 01:51:41 +03:00 committed by GitHub
commit 936f233571
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
30 changed files with 393 additions and 371 deletions

View File

@ -96,8 +96,9 @@ void ALStream::bindSpeaker(speakerid_t speakerid) {
this->speaker = speakerid;
sp = audio::get_speaker(speakerid);
if (sp) {
auto alspeaker = dynamic_cast<ALSpeaker*>(sp); //FIXME: Potentional null pointer
alspeaker->stream = this; //-V522
auto alspeaker = dynamic_cast<ALSpeaker*>(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<ALSpeaker*>(p_speaker); //FIXME: Potentional null pointer
if (alspeaker->stopped) { //-V522
p_speaker = 0; //FIXME: Not used
ALSpeaker* alspeaker = dynamic_cast<ALSpeaker*>(p_speaker);
assert(alspeaker != nullptr);
if (alspeaker->stopped) {
this->speaker = 0;
return;
}
@ -159,7 +161,8 @@ void ALStream::update(double delta) {
unqueueBuffers(alsource);
uint preloaded = enqueueBuffers(alsource);
if (p_speaker->isStopped() && !alspeaker->stopped) { //FIXME: !alspeaker->stopped always true //-V560
// alspeaker->stopped is assigned to false at ALSpeaker::play(...)
if (p_speaker->isStopped() && !alspeaker->stopped) { //TODO: -V560 false-positive?
if (preloaded) {
p_speaker->play();
} else {

View File

@ -59,17 +59,33 @@ public:
ContentUnitIndices(std::vector<T*> 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();
}
inline const auto& getIterable() const {
return defs;
}
inline const T* const* getDefs() const {
return defs.data();
}

View File

@ -37,8 +37,8 @@ public:
for (size_t i = 0; i < count; i++) {
indices.push_back(i);
}
for (size_t i = 0; i < unitIndices.count(); i++) {
names.push_back(unitIndices.get(i)->name); //FIXME: .get(i) potentional null pointer //-V522
for (auto unit : unitIndices.getIterable()) {
names.push_back(unit->name);
}
for (size_t i = unitIndices.count(); i < count; i++) {
names.emplace_back("");

View File

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

View File

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

View File

@ -103,9 +103,8 @@ template <class T>
static void write_indices(
const ContentUnitIndices<T>& indices, dynamic::List& list
) {
size_t count = indices.count();
for (size_t i = 0; i < count; i++) {
list.put(indices.get(i)->name); //FIXME: .get(i) potential null pointer //-V522
for (auto unit : indices.getIterable()) {
list.put(unit->name);
}
}

View File

@ -17,10 +17,11 @@ ContentGfxCache::ContentGfxCache(const Content* content, Assets* assets) : conte
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6);
auto atlas = assets->get<Atlas>("blocks");
for (uint i = 0; i < indices->blocks.count(); i++) {
Block* def = indices->blocks.get(i); //FIXME: Potential null pointer
const auto& blocks = indices->blocks.getIterable();
for (uint i = 0; i < blocks.size(); i++) {
auto def = blocks[i];
for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side]; //-V522
const std::string& tex = def->textureFaces[side];
if (atlas->has(tex)) {
sideregions[i * 6 + side] = atlas->get(tex);
} else if (atlas->has(TEXTURE_NOTFOUND)) {

View File

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

View File

@ -22,30 +22,30 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
Shader* shader,
Framebuffer* fbo,
Batch3D* batch,
const Block* def,
const Block& def,
int size
){
Window::clear();
blockid_t id = def->rt.id;
blockid_t id = def.rt.id;
const UVRegion texfaces[6]{cache->getRegion(id, 0), cache->getRegion(id, 1),
cache->getRegion(id, 2), cache->getRegion(id, 3),
cache->getRegion(id, 4), cache->getRegion(id, 5)};
glm::vec3 offset(0.1f, 0.5f, 0.1f);
switch (def->model) {
switch (def.model) {
case BlockModel::none:
// something went wrong...
break;
case BlockModel::block:
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
batch->blockCube(glm::vec3(size * 0.63f), texfaces,
glm::vec4(1.0f), !def->rt.emissive);
glm::vec4(1.0f), !def.rt.emissive);
batch->flush();
break;
case BlockModel::aabb:
{
glm::vec3 hitbox {};
for (const auto& box : def->hitboxes) {
for (const auto& box : def.hitboxes) {
hitbox = glm::max(hitbox, box.size());
}
offset = glm::vec3(1, 1, 0.0f);
@ -55,7 +55,7 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
-hitbox * scaledSize * 0.5f * glm::vec3(1,1,-1),
hitbox * scaledSize,
texfaces, glm::vec4(1.0f),
!def->rt.emissive
!def.rt.emissive
);
}
batch->flush();
@ -64,32 +64,32 @@ std::unique_ptr<ImageData> BlocksPreview::draw(
{
glm::vec3 pmul = glm::vec3(size * 0.63f);
glm::vec3 hitbox = glm::vec3();
for (const auto& box : def->modelBoxes) {
for (const auto& box : def.modelBoxes) {
hitbox = glm::max(hitbox, box.size());
}
offset.y += (1.0f - hitbox).y * 0.5f;
shader->uniformMatrix("u_apply", glm::translate(glm::mat4(1.0f), offset));
for (size_t i = 0; i < def->modelBoxes.size(); i++) {
for (size_t i = 0; i < def.modelBoxes.size(); i++) {
const UVRegion (&boxtexfaces)[6] = {
def->modelUVs[i * 6],
def->modelUVs[i * 6 + 1],
def->modelUVs[i * 6 + 2],
def->modelUVs[i * 6 + 3],
def->modelUVs[i * 6 + 4],
def->modelUVs[i * 6 + 5]
def.modelUVs[i * 6],
def.modelUVs[i * 6 + 1],
def.modelUVs[i * 6 + 2],
def.modelUVs[i * 6 + 3],
def.modelUVs[i * 6 + 4],
def.modelUVs[i * 6 + 5]
};
batch->cube(
def->modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
def->modelBoxes[i].size() * pmul,
boxtexfaces, glm::vec4(1.0f), !def->rt.emissive
def.modelBoxes[i].a * glm::vec3(1.0f, 1.0f, -1.0f) * pmul,
def.modelBoxes[i].size() * pmul,
boxtexfaces, glm::vec4(1.0f), !def.rt.emissive
);
}
auto& points = def->modelExtraPoints;
auto& points = def.modelExtraPoints;
glm::vec3 poff = glm::vec3(0.0f, 0.0f, 1.0f);
for (size_t i = 0; i < def->modelExtraPoints.size() / 4; i++) {
const UVRegion& reg = def->modelUVs[def->modelBoxes.size() * 6 + i];
for (size_t i = 0; i < def.modelExtraPoints.size() / 4; i++) {
const UVRegion& reg = def.modelUVs[def.modelBoxes.size() * 6 + i];
batch->point((points[i * 4 + 0] - poff) * pmul, glm::vec2(reg.u1, reg.v1), glm::vec4(1.0));
batch->point((points[i * 4 + 1] - poff) * pmul, glm::vec2(reg.u2, reg.v1), glm::vec4(1.0));
@ -154,9 +154,9 @@ std::unique_ptr<Atlas> BlocksPreview::build(
fbo.bind();
for (size_t i = 0; i < count; i++) {
auto def = indices->blocks.get(i); //FIXME: Potentional null pointer
auto& def = indices->blocks.require(i);
atlas->getTexture()->bind();
builder.add(def->name, draw(cache, shader, &fbo, &batch, def, iconSize)); //-V522
builder.add(def.name, draw(cache, shader, &fbo, &batch, def, iconSize));
}
fbo.unbind();

View File

@ -22,7 +22,7 @@ class BlocksPreview {
Shader* shader,
Framebuffer* framebuffer,
Batch3D* batch,
const Block* block,
const Block& block,
int size
);
public:

View File

@ -142,11 +142,12 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
ctx.setFramebuffer(fbo.get());
ctx.setViewport(Viewport(size, size));
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture()); //FIXME: Potentional null pointer
auto cubemap = dynamic_cast<Cubemap*>(fbo->getTexture());
assert(cubemap != nullptr);
ready = true;
glActiveTexture(GL_TEXTURE1);
cubemap->bind(); //-V522
cubemap->bind();
shader->use();
const glm::vec3 xaxs[] = {

View File

@ -1,8 +1,12 @@
#include "WorldRenderer.hpp"
#include "ChunksRenderer.hpp"
#include "ModelBatch.hpp"
#include "Skybox.hpp"
#include <GL/glew.h>
#include <assert.h>
#include <algorithm>
#include <glm/ext.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <memory>
#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 <assert.h>
#include <GL/glew.h>
#include <algorithm>
#include <memory>
#include <glm/ext.hpp>
#include <glm/gtc/matrix_transform.hpp>
#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<Frustum>()),
lineBatch(std::make_unique<LineBatch>()),
modelBatch(std::make_unique<ModelBatch>(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<Frustum>()),
lineBatch(std::make_unique<LineBatch>()),
modelBatch(std::make_unique<ModelBatch>(
20'000, engine->getAssets(), level->chunks.get()
)) {
renderer = std::make_unique<ChunksRenderer>(
level,
frontend->getContentGfxCache(),
&engine->getSettings()
level, frontend->getContentGfxCache(), &engine->getSettings()
);
batch3d = std::make_unique<Batch3D>(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<Skybox>(
settings.graphics.skyboxResolution.get(),
settings.graphics.skyboxResolution.get(),
assets->get<Shader>("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<size_t> 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<Atlas>("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<Shader>("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<AABB>& hitboxes = block->rotatable //-V522
? block->rt.hitboxes[selection.vox.state.rotation]
: block->hitboxes;
const std::vector<AABB>& 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<int>(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<float>(displayWidth),
0.f, static_cast<float>(displayHeight),
-length, length) * model * glm::inverse(camera->rotation)
linesShader->uniformMatrix(
"u_projview",
glm::ortho(
0.f,
static_cast<float>(displayWidth),
0.f,
static_cast<float>(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<float>(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<Shader>("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<Shader>("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();
}

View File

@ -121,9 +121,9 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
itemid_t itemid = bound->getItemId();
if (itemid != prevItem) {
if (itemid) {
auto def = content->getIndices()->items.get(itemid); //FIXME: Potentional null pointer
auto& def = content->getIndices()->items.require(itemid);
tooltip = util::pascal_case(
langs::get(util::str2wstr_utf8(def->caption)) //-V522
langs::get(util::str2wstr_utf8(def.caption))
);
} else {
tooltip.clear();
@ -159,12 +159,12 @@ void SlotView::draw(const DrawContext* pctx, Assets* assets) {
auto previews = assets->get<Atlas>("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());
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<Texture>(name));
} else {
std::string atlasname = item->icon.substr(0, index);
std::string atlasname = item.icon.substr(0, index);
auto atlas = assets->get<Atlas>(atlasname);
if (atlas && atlas->has(name)) {
region = atlas->get(name);
@ -268,14 +268,15 @@ void SlotView::clicked(gui::GUI* gui, mousecode button) {
stack.setCount(halfremain);
}
} else {
auto stackDef = content->getIndices()->items.get(stack.getItemId()); //FIXME: Potentional null pointer
auto& stackDef =
content->getIndices()->items.require(stack.getItemId());
if (stack.isEmpty()) {
stack.set(grabbed);
stack.setCount(1);
grabbed.setCount(grabbed.getCount()-1);
} else if (stack.accepts(grabbed) && stack.getCount() < stackDef->stackSize){ //-V522
stack.setCount(stack.getCount()+1);
grabbed.setCount(grabbed.getCount()-1);
grabbed.setCount(grabbed.getCount() - 1);
} else if (stack.accepts(grabbed) && stack.getCount() < stackDef.stackSize) {
stack.setCount(stack.getCount() + 1);
grabbed.setCount(grabbed.getCount() - 1);
}
}
}

View File

@ -26,8 +26,8 @@ bool ItemStack::accepts(const ItemStack& other) const {
}
void ItemStack::move(ItemStack& item, const ContentIndices* indices) {
auto def = indices->items.get(item.getItemId()); //FIXME: Potentional null pointer
int count = std::min(item.count, def->stackSize - this->count); //-V522
auto& def = indices->items.require(item.getItemId());
int count = std::min(item.count, def.stackSize - this->count);
if (isEmpty()) {
set(ItemStack(item.getItemId(), count));
} else {

View File

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

View File

@ -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
);
@ -153,8 +153,8 @@ int64_t BlocksController::createBlockInventory(int x, int y, int z) {
auto inv = chunk->getBlockInventory(lx, y, lz);
if (inv == nullptr) {
auto indices = level->content->getIndices();
auto def = indices->blocks.get(chunk->voxels[vox_index(lx, y, lz)].id); //FIXME: Potentional null pointer
int invsize = def->inventorySize; //-V522
auto& def = indices->blocks.require(chunk->voxels[vox_index(lx, y, lz)].id);
int invsize = def.inventorySize;
if (invsize == 0) {
return 0;
}
@ -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);

View File

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

View File

@ -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;
@ -333,8 +335,8 @@ static int determine_rotation(
static void pick_block(
ContentIndices* indices, Chunks* chunks, Player* player, int x, int y, int z
) {
auto block = indices->blocks.get(chunks->get(x, y, z)->id); //FIXME: Potentional null pointer
itemid_t id = block->rt.pickingItem; //-V522
auto& block = indices->blocks.require(chunks->get(x, y, z)->id);
itemid_t id = block.rt.pickingItem;
auto inventory = player->getInventory();
size_t slotid = inventory->findSlotByItem(id, 0, 10);
if (slotid == Inventory::npos) {
@ -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;

View File

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

View File

@ -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<size_t>(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)
);
@ -122,11 +119,11 @@ static int l_get_x(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec3_stack(L, 1, 0, 0);
}
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
if (!def->rotatable) { //-V522
auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec3_stack(L, 1, 0, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisX.x, rot.axisX.y, rot.axisX.z);
}
}
@ -139,11 +136,11 @@ static int l_get_y(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec3_stack(L, 0, 1, 0);
}
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
if (!def->rotatable) { //-V522
auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec3_stack(L, 0, 1, 0);
} else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisY.x, rot.axisY.y, rot.axisY.z);
}
}
@ -156,11 +153,11 @@ static int l_get_z(lua::State* L) {
if (vox == nullptr) {
return lua::pushivec3_stack(L, 0, 0, 1);
}
auto def = level->content->getIndices()->blocks.get(vox->id); //FIXME: Potentional null pointer
if (!def->rotatable) { //-V522
auto& def = level->content->getIndices()->blocks.require(vox->id);
if (!def.rotatable) {
return lua::pushivec3_stack(L, 0, 0, 1);
} else {
const CoordSystem& rot = def->rotations.variants[vox->state.rotation];
const CoordSystem& rot = def.rotations.variants[vox->state.rotation];
return lua::pushivec3_stack(L, rot.axisZ.x, rot.axisZ.y, rot.axisZ.z);
}
}
@ -330,7 +327,7 @@ static int l_place(lua::State* L) {
}
auto player = level->getObject<Player>(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<Player>(playerid);
controller->getBlocksController()->breakBlock(
player ? player.get() : nullptr, def, x, y, z

View File

@ -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<size_t>(id) >= indices->entities.count()) {
return nullptr;
}
return indices->entities.get(id);
}

View File

@ -53,34 +53,40 @@ static DocumentNode getDocumentNode(lua::State* L, int idx = 1) {
static int l_menu_back(lua::State* L) {
auto node = getDocumentNode(L);
auto menu = dynamic_cast<Menu*>(node.node.get()); //FIXME: Potentional null pointer
menu->back(); //-V522
if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->back();
}
return 0;
}
static int l_menu_reset(lua::State* L) {
auto node = getDocumentNode(L);
auto menu = dynamic_cast<Menu*>(node.node.get()); //FIXME: Potentional null pointer
menu->reset(); //-V522
if (auto menu = dynamic_cast<Menu*>(node.node.get())) {
menu->reset();
}
return 0;
}
static int l_textbox_paste(lua::State* L) {
auto node = getDocumentNode(L);
auto box = dynamic_cast<TextBox*>(node.node.get()); //FIXME: Potentional null pointer
auto text = lua::require_string(L, 2);
box->paste(util::str2wstr_utf8(text)); //-V522
if (auto box = dynamic_cast<TextBox*>(node.node.get())) {
auto text = lua::require_string(L, 2);
box->paste(util::str2wstr_utf8(text));
}
return 0;
}
static int l_container_add(lua::State* L) {
auto docnode = getDocumentNode(L);
auto node = dynamic_cast<Container*>(docnode.node.get()); //FIXME: Potentional null pointer
auto node = dynamic_cast<Container*>(docnode.node.get());
if (node == nullptr) {
return 0;
}
auto xmlsrc = lua::require_string(L, 2);
try {
auto subnode =
guiutil::create(xmlsrc, docnode.document->getEnvironment());
node->add(subnode); //-V522
node->add(subnode);
UINode::getIndices(subnode, docnode.document->getMapWriteable());
} catch (const std::exception& err) {
throw std::runtime_error(err.what());

View File

@ -49,11 +49,11 @@ static int l_hud_open_block(lua::State* L) {
std::to_string(y) + " " + std::to_string(z)
);
}
auto def = content->getIndices()->blocks.get(vox->id);
auto& def = content->getIndices()->blocks.require(vox->id);
auto assets = engine->getAssets();
auto layout = assets->get<UiDocument>(def->uiLayout);//FIXME: Potentional null pointer //-V522
auto layout = assets->get<UiDocument>(def.uiLayout);
if (layout == nullptr) {
throw std::runtime_error("block '" + def->name + "' has no ui layout");
throw std::runtime_error("block '" + def.name + "' has no ui layout");
}
auto id = blocks->createBlockInventory(x, y, z);
@ -65,7 +65,7 @@ static int l_hud_open_block(lua::State* L) {
);
lua::pushinteger(L, id);
lua::pushstring(L, def->uiLayout);
lua::pushstring(L, def.uiLayout);
return 2;
}

View File

@ -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<size_t>(id) >= indices->items.count()) {
return nullptr;
}
return indices->items.get(id);
}

View File

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

View File

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

View File

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

View File

@ -76,15 +76,15 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) {
return &empty;
}
}
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<glm::ivec3> 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<float>::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<AABB>& 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<float>::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<AABB>& 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;

View File

@ -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<std::shared_ptr<Chunk>> 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

View File

@ -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),