move getVoxels from ChunksStorage to Chunks

This commit is contained in:
MihailRis
2024-11-11 22:30:42 +03:00
parent 79693580e0
commit c5ca9128e9
8 changed files with 107 additions and 107 deletions
+92 -2
View File
@@ -20,6 +20,7 @@
#include "objects/Entities.hpp"
#include "world/Level.hpp"
#include "world/LevelEvents.hpp"
#include "VoxelsVolume.hpp"
#include "Block.hpp"
#include "Chunk.hpp"
#include "voxel.hpp"
@@ -161,7 +162,7 @@ light_t Chunks::getLight(int32_t x, int32_t y, int32_t z) const {
return chunk->lightmap.get(lx, y, lz);
}
Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z) {
Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z) const {
if (y < 0 || y >= CHUNK_H) {
return nullptr;
}
@@ -173,7 +174,7 @@ Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z) {
return nullptr;
}
Chunk* Chunks::getChunk(int x, int z) {
Chunk* Chunks::getChunk(int x, int z) const {
if (auto ptr = areaMap.getIf(x, z)) {
return ptr->get();
}
@@ -675,6 +676,95 @@ bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
return areaMap.set(chunk->x, chunk->z, chunk);
}
// reduce nesting on next modification
// 25.06.2024: not now
// 11.11.2024: not now
void Chunks::getVoxels(VoxelsVolume* volume, bool backlight) const {
const Content* content = level->content;
auto indices = content->getIndices();
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 scx = floordiv(x, CHUNK_W);
int scz = floordiv(z, CHUNK_D);
int ecx = floordiv(x + w, CHUNK_W);
int ecz = floordiv(z + d, CHUNK_D);
int cw = ecx - scx + 1;
int cd = ecz - scz + 1;
// cw*cd chunks will be scanned
for (int cz = scz; cz < scz + cd; cz++) {
for (int cx = scx; cx < scx + cw; cx++) {
const auto chunk = getChunk(cx, cz);
if (chunk == nullptr) {
// no chunk loaded -> filling with BLOCK_VOID
for (int ly = y; ly < y + h; ly++) {
for (int lz = std::max(z, cz * CHUNK_D);
lz < std::min(z + d, (cz + 1) * CHUNK_D);
lz++) {
for (int lx = std::max(x, cx * CHUNK_W);
lx < std::min(x + w, (cx + 1) * CHUNK_W);
lx++) {
uint idx = vox_index(lx - x, ly - y, lz - z, w, d);
voxels[idx].id = BLOCK_VOID;
lights[idx] = 0;
}
}
}
} else {
const voxel* cvoxels = chunk->voxels;
const light_t* clights = chunk->lightmap.getLights();
for (int ly = y; ly < y + h; ly++) {
for (int lz = std::max(z, cz * CHUNK_D);
lz < std::min(z + d, (cz + 1) * CHUNK_D);
lz++) {
for (int lx = std::max(x, cx * CHUNK_W);
lx < std::min(x + w, (cx + 1) * CHUNK_W);
lx++) {
uint vidx = vox_index(lx - x, ly - y, lz - z, w, d);
uint cidx = vox_index(
lx - cx * CHUNK_W,
ly,
lz - cz * CHUNK_D,
CHUNK_W,
CHUNK_D
);
voxels[vidx] = cvoxels[cidx];
light_t light = clights[cidx];
if (backlight) {
const auto block =
indices->blocks.get(voxels[vidx].id);
if (block && block->lightPassing) {
light = Lightmap::combine(
std::min(15,
Lightmap::extract(light, 0) + 1),
std::min(15,
Lightmap::extract(light, 1) + 1),
std::min(15,
Lightmap::extract(light, 2) + 1),
std::min(15,
static_cast<int>(Lightmap::extract(light, 3)))
);
}
}
lights[vidx] = light;
}
}
}
}
}
}
}
void Chunks::saveAndClear() {
areaMap.clear();
}