refactor: PVS-Studio warnings fixes

This commit is contained in:
MihailRis
2024-08-04 01:12:42 +03:00
parent 7bc96affbb
commit 245b39be62
22 changed files with 311 additions and 302 deletions
+14 -14
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
);
@@ -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);
+4 -4
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
+23 -21
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;
@@ -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;
+1 -1
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:
+4 -7
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)
);
@@ -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
+1 -4
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);
}
+1 -4
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);
}
+23 -23
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,
+9 -9
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(