fix potential null dereferences on incorrect block.* functions use & refactor

This commit is contained in:
MihailRis
2024-11-06 02:55:37 +03:00
parent 8ef7d43603
commit 961773c9f9
3 changed files with 43 additions and 19 deletions
+17 -5
View File
@@ -4,6 +4,7 @@
#include <math.h>
#include <algorithm>
#include <stdexcept>
#include <vector>
#include "data/StructLayout.hpp"
@@ -61,6 +62,14 @@ voxel* Chunks::get(int32_t x, int32_t y, int32_t z) const {
return &chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
}
voxel& Chunks::require(int32_t x, int32_t y, int32_t z) const {
auto voxel = get(x, y, z);
if (voxel == nullptr) {
throw std::runtime_error("voxel does not exist");
}
return *voxel;
}
const AABB* Chunks::isObstacleAt(float x, float y, float z) const {
int ix = std::floor(x);
int iy = std::floor(y);
@@ -243,7 +252,10 @@ void Chunks::repairSegments(
}
bool Chunks::checkReplaceability(
const Block& def, blockstate state, glm::ivec3 origin, blockid_t ignore
const Block& def,
blockstate state,
const glm::ivec3& origin,
blockid_t ignore
) {
const auto& rotation = def.rotations.variants[state.rotation];
const auto size = def.size;
@@ -269,7 +281,7 @@ bool Chunks::checkReplaceability(
}
void Chunks::setRotationExtended(
const Block& def, blockstate state, glm::ivec3 origin, uint8_t index
const Block& def, blockstate state, const glm::ivec3& origin, uint8_t index
) {
auto newstate = state;
newstate.rotation = index;
@@ -418,8 +430,8 @@ void Chunks::set(
}
voxel* Chunks::rayCast(
glm::vec3 start,
glm::vec3 dir,
const glm::vec3& start,
const glm::vec3& dir,
float maxDist,
glm::vec3& end,
glm::ivec3& norm,
@@ -557,7 +569,7 @@ voxel* Chunks::rayCast(
}
glm::vec3 Chunks::rayCastToObstacle(
glm::vec3 start, glm::vec3 dir, float maxDist
const glm::vec3& start, const glm::vec3& dir, float maxDist
) {
const float px = start.x;
const float py = start.y;
+13 -7
View File
@@ -32,7 +32,10 @@ class Chunks {
const Block& def, blockstate state, int x, int y, int z
);
void setRotationExtended(
const Block& def, blockstate state, glm::ivec3 origin, uint8_t rotation
const Block& def,
blockstate state,
const glm::ivec3& origin,
uint8_t rotation
);
util::AreaMap2D<std::shared_ptr<Chunk>, int32_t> areaMap;
@@ -55,12 +58,13 @@ public:
Chunk* getChunk(int32_t x, int32_t z);
Chunk* getChunkByVoxel(int32_t x, int32_t y, int32_t z);
voxel* get(int32_t x, int32_t y, int32_t z) const;
voxel& require(int32_t x, int32_t y, int32_t z) const;
inline voxel* get(glm::ivec3 pos) {
inline voxel* get(const glm::ivec3& pos) {
return get(pos.x, pos.y, pos.z);
}
inline const voxel* get(glm::ivec3 pos) const {
inline const voxel* get(const glm::ivec3& pos) const {
return get(pos.x, pos.y, pos.z);
}
@@ -85,15 +89,15 @@ public:
bool checkReplaceability(
const Block& def,
blockstate state,
glm::ivec3 coord,
const glm::ivec3& coord,
blockid_t ignore = 0
);
void setRotation(int32_t x, int32_t y, int32_t z, uint8_t rotation);
voxel* rayCast(
glm::vec3 start,
glm::vec3 dir,
const glm::vec3& start,
const glm::vec3& dir,
float maxLength,
glm::vec3& end,
glm::ivec3& norm,
@@ -101,7 +105,9 @@ public:
std::set<blockid_t> filter = {}
);
glm::vec3 rayCastToObstacle(glm::vec3 start, glm::vec3 dir, float maxDist);
glm::vec3 rayCastToObstacle(
const glm::vec3& start, const glm::vec3& dir, float maxDist
);
const AABB* isObstacleAt(float x, float y, float z) const;