optimize AreaMap2D

This commit is contained in:
MihailRis
2024-09-09 20:37:42 +03:00
parent c3569b5dd4
commit 7c0c268508
6 changed files with 131 additions and 111 deletions
+26 -28
View File
@@ -23,8 +23,8 @@
#include "voxel.hpp"
Chunks::Chunks(
uint32_t w,
uint32_t d,
int32_t w,
int32_t d,
int32_t ox,
int32_t oz,
WorldFiles* wfile,
@@ -32,9 +32,9 @@ Chunks::Chunks(
)
: level(level),
indices(level->content->getIndices()),
areaMap({w, d}),
areaMap(w, d),
worldFiles(wfile) {
areaMap.setCenter({ox-w/2, oz-d/2});
areaMap.setCenter(ox-w/2, oz-d/2);
areaMap.setOutCallback([this](const auto& chunk) {
save(chunk.get());
});
@@ -46,7 +46,7 @@ voxel* Chunks::get(int32_t x, int32_t y, int32_t z) const {
}
int cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz});
auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) {
return nullptr;
}
@@ -83,8 +83,8 @@ const AABB* Chunks::isObstacleAt(float x, float y, float z) {
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}
)) {
{x - ix - offset.x, y - iy - offset.y, z - iz - offset.z}
)) {
return &hitbox;
}
}
@@ -117,7 +117,7 @@ ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel) {
int cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz});
auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) {
return 0;
}
@@ -137,7 +137,7 @@ light_t Chunks::getLight(int32_t x, int32_t y, int32_t z) {
int cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz});
auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) {
return 0;
}
@@ -156,14 +156,14 @@ Chunk* Chunks::getChunkByVoxel(int32_t x, int32_t y, int32_t z) {
}
int cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D);
if (auto ptr = areaMap.getIf({cx, cz})) {
if (auto ptr = areaMap.getIf(cx, cz)) {
return ptr->get();
}
return nullptr;
}
Chunk* Chunks::getChunk(int x, int z) {
if (auto ptr = areaMap.getIf({x, z})) {
if (auto ptr = areaMap.getIf(x, z)) {
return ptr->get();
}
return nullptr;
@@ -351,11 +351,9 @@ void Chunks::set(
if (y < 0 || y >= CHUNK_H) {
return;
}
int32_t gx = x;
int32_t gz = z;
int cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz});
auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) {
return;
}
@@ -373,7 +371,7 @@ void Chunks::set(
chunk->removeBlockInventory(lx, y, lz);
}
if (prevdef.rt.extended && !vox.state.segment) {
eraseSegments(prevdef, vox.state, gx, y, gz);
eraseSegments(prevdef, vox.state, x, y, z);
}
// block initialization
@@ -382,7 +380,7 @@ void Chunks::set(
vox.state = state;
chunk->setModifiedAndUnsaved();
if (!state.segment && newdef.rt.extended) {
repairSegments(newdef, state, gx, y, gz);
repairSegments(newdef, state, x, y, z);
}
if (y < chunk->bottom)
@@ -424,9 +422,9 @@ voxel* Chunks::rayCast(
float dz = dir.z;
float t = 0.0f;
int ix = floor(px);
int iy = floor(py);
int iz = floor(pz);
int ix = std::floor(px);
int iy = std::floor(py);
int iz = std::floor(pz);
int stepx = (dx > 0.0f) ? 1 : -1;
int stepy = (dy > 0.0f) ? 1 : -1;
@@ -434,9 +432,9 @@ voxel* Chunks::rayCast(
constexpr float infinity = std::numeric_limits<float>::infinity();
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);
float txDelta = (std::fabs(dx) < epsilon) ? infinity : std::fabs(1.0f / dx);
float tyDelta = (std::fabs(dy) < epsilon) ? infinity : std::fabs(1.0f / dy);
float tzDelta = (std::fabs(dz) < epsilon) ? infinity : std::fabs(1.0f / dz);
float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix);
float ydist = (stepy > 0) ? (iy + 1 - py) : (py - iy);
@@ -567,9 +565,9 @@ glm::vec3 Chunks::rayCastToObstacle(
constexpr float infinity = std::numeric_limits<float>::infinity();
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);
float txDelta = (std::fabs(dx) < epsilon) ? infinity : std::fabs(1.0f / dx);
float tyDelta = (std::fabs(dy) < epsilon) ? infinity : std::fabs(1.0f / dy);
float tzDelta = (std::fabs(dz) < epsilon) ? infinity : std::fabs(1.0f / dz);
float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix);
float ydist = (stepy > 0) ? (iy + 1 - py) : (py - iy);
@@ -642,15 +640,15 @@ glm::vec3 Chunks::rayCastToObstacle(
}
void Chunks::setCenter(int32_t x, int32_t z) {
areaMap.setCenter({floordiv(x, CHUNK_W), floordiv(z, CHUNK_D)});
areaMap.setCenter(floordiv(x, CHUNK_W), floordiv(z, CHUNK_D));
}
void Chunks::resize(uint32_t newW, uint32_t newD) {
areaMap.resize({newW, newD});
areaMap.resize(newW, newD);
}
bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
return areaMap.set({chunk->x, chunk->z}, chunk);
return areaMap.set(chunk->x, chunk->z, chunk);
}
void Chunks::saveAndClear() {