refactor: change pointer parameters to references for Level and Content in various classes

This commit is contained in:
MihailRis
2024-12-25 18:53:53 +03:00
parent ef2c0b2370
commit c1b311f3c4
41 changed files with 258 additions and 251 deletions
+14 -14
View File
@@ -27,7 +27,7 @@ Chunks::Chunks(
int32_t ox,
int32_t oz,
LevelEvents* events,
const ContentIndices* indices
const ContentIndices& indices
)
: events(events),
indices(indices),
@@ -67,7 +67,7 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) const {
return ∅
}
}
const auto& def = indices->blocks.require(v->id);
const auto& def = indices.blocks.require(v->id);
if (def.obstacle) {
glm::ivec3 offset {};
if (v->state.segment) {
@@ -98,7 +98,7 @@ bool Chunks::isReplaceableBlock(int32_t x, int32_t y, int32_t z) {
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.require(v->id).obstacle;
return indices.blocks.require(v->id).obstacle;
}
ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel) const {
@@ -254,7 +254,7 @@ glm::vec3 Chunks::rayCastToObstacle(
while (t <= maxDist) {
voxel* voxel = get(ix, iy, iz);
if (voxel) {
const auto& def = indices->blocks.require(voxel->id);
const auto& def = indices.blocks.require(voxel->id);
if (def.obstacle) {
if (!def.rt.solid) {
const std::vector<AABB>& hitboxes =
@@ -333,16 +333,16 @@ bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
// reduce nesting on next modification
// 25.06.2024: not now
// 11.11.2024: not now
void Chunks::getVoxels(VoxelsVolume* volume, bool backlight) const {
voxel* voxels = volume->getVoxels();
light_t* lights = volume->getLights();
int x = volume->getX();
int y = volume->getY();
int z = volume->getZ();
void Chunks::getVoxels(VoxelsVolume& volume, bool backlight) const {
voxel* voxels = volume.getVoxels();
light_t* lights = volume.getLights();
int x = volume.getX();
int y = volume.getY();
int z = volume.getZ();
int w = volume->getW();
int h = volume->getH();
int d = volume->getD();
int w = volume.getW();
int h = volume.getH();
int d = volume.getD();
int scx = floordiv<CHUNK_W>(x);
int scz = floordiv<CHUNK_D>(z);
@@ -394,7 +394,7 @@ void Chunks::getVoxels(VoxelsVolume* volume, bool backlight) const {
light_t light = clights[cidx];
if (backlight) {
const auto block =
indices->blocks.get(voxels[vidx].id);
indices.blocks.get(voxels[vidx].id);
if (block && block->lightPassing) {
light = Lightmap::combine(
std::min(15,
+12 -4
View File
@@ -25,7 +25,7 @@ class VoxelsVolume;
/// Player-centred chunks matrix
class Chunks {
LevelEvents* events;
const ContentIndices* const indices;
const ContentIndices& indices;
void eraseSegments(const Block& def, blockstate state, int x, int y, int z);
void repairSegments(
@@ -46,7 +46,7 @@ public:
int32_t ox,
int32_t oz,
LevelEvents* events,
const ContentIndices* indices
const ContentIndices& indices
);
~Chunks() = default;
@@ -56,6 +56,14 @@ public:
Chunk* getChunk(int32_t x, int32_t z) const;
Chunk* getChunkByVoxel(int32_t x, int32_t y, int32_t z) const;
template <typename T>
Chunk* getChunkByVoxel(const glm::vec<3, T>& pos) const {
return getChunkByVoxel(
glm::floor(pos.x), glm::floor(pos.y), glm::floor(pos.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;
@@ -118,7 +126,7 @@ public:
bool isReplaceableBlock(int32_t x, int32_t y, int32_t z);
bool isObstacleBlock(int32_t x, int32_t y, int32_t z);
void getVoxels(VoxelsVolume* volume, bool backlight = false) const;
void getVoxels(VoxelsVolume& volume, bool backlight = false) const;
void setCenter(int32_t x, int32_t z);
void resize(uint32_t newW, uint32_t newD);
@@ -154,7 +162,7 @@ public:
}
const ContentIndices& getContentIndices() const {
return *indices;
return indices;
}
static inline constexpr unsigned matrixSize(int loadDistance, int padding) {
+10 -10
View File
@@ -19,8 +19,8 @@
static debug::Logger logger("chunks-storage");
GlobalChunks::GlobalChunks(Level* level)
: level(level), indices(level ? level->content->getIndices() : nullptr) {
GlobalChunks::GlobalChunks(Level& level)
: level(level), indices(*level.content.getIndices()) {
chunksMap.max_load_factor(CHUNKS_MAP_MAX_LOAD_FACTOR);
}
@@ -96,11 +96,11 @@ std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
auto chunk = std::make_shared<Chunk>(x, z);
chunksMap[keyfrom(x, z)] = chunk;
World* world = level->getWorld();
auto& regions = world->wfile.get()->getRegions();
World& world = *level.getWorld();
auto& regions = world.wfile.get()->getRegions();
if (auto data = regions.getVoxels(chunk->x, chunk->z)) {
const auto& indices = *level->content->getIndices();
const auto& indices = *level.content.getIndices();
chunk->decode(data.get());
check_voxels(indices, *chunk);
@@ -111,13 +111,13 @@ std::shared_ptr<Chunk> GlobalChunks::create(int x, int z) {
auto entitiesData = regions.fetchEntities(chunk->x, chunk->z);
if (entitiesData.getType() == dv::value_type::object) {
level->entities->loadEntities(std::move(entitiesData));
level.entities->loadEntities(std::move(entitiesData));
chunk->flags.entities = true;
}
chunk->flags.loaded = true;
for (auto& entry : chunk->inventories) {
level->inventories->store(entry.second);
level.inventories->store(entry.second);
}
}
if (auto lights = regions.getLights(chunk->x, chunk->z)) {
@@ -178,13 +178,13 @@ void GlobalChunks::save(Chunk* chunk) {
return;
}
AABB aabb = chunk->getAABB();
auto entities = level->entities->getAllInside(aabb);
auto entities = level.entities->getAllInside(aabb);
auto root = dv::object();
root["data"] = level->entities->serialize(entities);
root["data"] = level.entities->serialize(entities);
if (!entities.empty()) {
chunk->flags.entities = true;
}
level->getWorld()->wfile->getRegions().put(
level.getWorld()->wfile->getRegions().put(
chunk,
chunk->flags.entities ? json::to_binary(root, true)
: std::vector<ubyte>()
+4 -4
View File
@@ -26,15 +26,15 @@ class GlobalChunks {
return ekey.key;
}
Level* level;
const ContentIndices* indices;
Level& level;
const ContentIndices& indices;
std::unordered_map<uint64_t, std::shared_ptr<Chunk>> chunksMap;
std::unordered_map<glm::ivec2, std::shared_ptr<Chunk>> pinnedChunks;
std::unordered_map<ptrdiff_t, int> refCounters;
consumer<Chunk&> onUnload;
public:
GlobalChunks(Level* level);
GlobalChunks(Level& level);
~GlobalChunks() = default;
void setOnUnload(consumer<Chunk&> onUnload);
@@ -68,6 +68,6 @@ public:
}
const ContentIndices& getContentIndices() const {
return *indices;
return indices;
}
};