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

View File

@ -124,12 +124,13 @@ void BlocksController::randomTick(
void BlocksController::randomTick(int tickid, int parts) { void BlocksController::randomTick(int tickid, int parts) {
auto indices = level->content->getIndices(); auto indices = level->content->getIndices();
const auto& size = chunks->getSize(); int width = chunks->getWidth();
int height = chunks->getHeight();
int segments = 4; int segments = 4;
for (uint z = padding; z < size.y - padding; z++) { for (uint z = padding; z < height - padding; z++) {
for (uint x = padding; x < size.x - padding; x++) { for (uint x = padding; x < width - padding; x++) {
int index = z * size.x + x; int index = z * width + x;
if ((index + tickid) % parts != 0) { if ((index + tickid) % parts != 0) {
continue; continue;
} }

View File

@ -52,14 +52,15 @@ void ChunksController::update(int64_t maxDuration) {
} }
bool ChunksController::loadVisible() { bool ChunksController::loadVisible() {
const auto& size = chunks->getSize(); int sizeX = chunks->getWidth();
int sizeY = chunks->getHeight();
int nearX = 0; int nearX = 0;
int nearZ = 0; int nearZ = 0;
int minDistance = ((size.x - padding * 2) / 2) * ((size.y - padding * 2) / 2); int minDistance = ((sizeX - padding * 2) / 2) * ((sizeY - padding * 2) / 2);
for (uint z = padding; z < size.y - padding; z++) { for (uint z = padding; z < sizeY - padding; z++) {
for (uint x = padding; x < size.x - padding; x++) { for (uint x = padding; x < sizeX - padding; x++) {
int index = z * size.x + x; int index = z * sizeX + x;
auto& chunk = chunks->getChunks()[index]; auto& chunk = chunks->getChunks()[index];
if (chunk != nullptr) { if (chunk != nullptr) {
if (chunk->flags.loaded && !chunk->flags.lighted) { if (chunk->flags.loaded && !chunk->flags.lighted) {
@ -69,8 +70,8 @@ bool ChunksController::loadVisible() {
} }
continue; continue;
} }
int lx = x - size.x / 2; int lx = x - sizeX / 2;
int lz = z - size.y / 2; int lz = z - sizeY / 2;
int distance = (lx * lx + lz * lz); int distance = (lx * lx + lz * lz);
if (distance < minDistance) { if (distance < minDistance) {
minDistance = distance; minDistance = distance;
@ -80,12 +81,13 @@ bool ChunksController::loadVisible() {
} }
} }
const auto& chunk = chunks->getChunks()[nearZ * size.x + nearX]; const auto& chunk = chunks->getChunks()[nearZ * sizeX + nearX];
if (chunk != nullptr) { if (chunk != nullptr) {
return false; return false;
} }
const auto& offset = chunks->getOffset(); int offsetX = chunks->getOffsetX();
createChunk(nearX + offset.x, nearZ + offset.y); int offsetY = chunks->getOffsetY();
createChunk(nearX + offsetX, nearZ + offsetY);
return true; return true;
} }

View File

@ -11,80 +11,81 @@ namespace util {
template<class T, typename TCoord=int> template<class T, typename TCoord=int>
class AreaMap2D { class AreaMap2D {
glm::vec<2, TCoord> offset; TCoord offsetX, offsetY;
glm::vec<2, TCoord> size; TCoord sizeX, sizeY;
std::vector<T> firstBuffer; std::vector<T> firstBuffer;
std::vector<T> secondBuffer; std::vector<T> secondBuffer;
OutCallback<T> outCallback; OutCallback<T> outCallback;
size_t valuesCount = 0; size_t valuesCount = 0;
void translate(const glm::vec<2, TCoord>& delta) { void translate(TCoord dx, TCoord dy) {
if (delta.x == 0 && delta.y == 0) { if (dx == 0 && dy == 0) {
return; return;
} }
std::fill(secondBuffer.begin(), secondBuffer.end(), T{}); std::fill(secondBuffer.begin(), secondBuffer.end(), T{});
for (TCoord y = 0; y < size.y; y++) { for (TCoord y = 0; y < sizeY; y++) {
for (TCoord x = 0; x < size.x; x++) { for (TCoord x = 0; x < sizeX; x++) {
auto& value = firstBuffer[y * size.x + x]; auto& value = firstBuffer[y * sizeX + x];
auto nx = x - delta.x; auto nx = x - dx;
auto ny = y - delta.y; auto ny = y - dy;
if (value == T{}) { if (value == T{}) {
continue; continue;
} }
if (nx < 0 || ny < 0 || nx >= size.x || ny >= size.y) { if (nx < 0 || ny < 0 || nx >= sizeX || ny >= sizeY) {
if (outCallback) { if (outCallback) {
outCallback(value); outCallback(value);
} }
valuesCount--; valuesCount--;
continue; continue;
} }
secondBuffer[ny * size.x + nx] = value; secondBuffer[ny * sizeX + nx] = value;
} }
} }
std::swap(firstBuffer, secondBuffer); std::swap(firstBuffer, secondBuffer);
offset += delta; offsetX += dx;
offsetY += dy;
} }
public: public:
AreaMap2D(glm::vec<2, TCoord> size) AreaMap2D(TCoord width, TCoord height)
: size(size), : sizeX(width), sizeY(height),
firstBuffer(size.x * size.y), secondBuffer(size.x * size.y) { firstBuffer(width * height), secondBuffer(width * height) {
} }
const T* getIf(const glm::vec<2, TCoord>& pos) const { const T* getIf(TCoord x, TCoord y) const {
auto localPos = pos - offset; auto lx = x - offsetX;
if (localPos.x < 0 || localPos.y < 0 || localPos.x >= size.x || auto ly = y - offsetY;
localPos.y >= size.y) { if (lx < 0 || ly < 0 || lx >= sizeX || ly >= sizeY) {
return nullptr; return nullptr;
} }
return &firstBuffer[localPos.y * size.x + localPos.x]; return &firstBuffer[ly * sizeX + lx];
} }
T get(const glm::vec<2, TCoord>& pos) { T get(TCoord x, TCoord y) {
auto localPos = pos - offset; auto lx = x - offsetX;
if (localPos.x < 0 || localPos.y < 0 || localPos.x >= size.x || auto ly = y - offsetY;
localPos.y >= size.y) { if (lx < 0 || ly < 0 || lx >= sizeX || ly >= sizeY) {
return T{}; return T{};
} }
return firstBuffer[localPos.y * size.x + localPos.x]; return firstBuffer[ly * sizeX + lx];
} }
const T& require(const glm::vec<2, TCoord>& pos) const { const T& require(TCoord x, TCoord y) const {
auto localPos = pos - offset; auto lx = x - offsetX;
if (localPos.x < 0 || localPos.y < 0 || localPos.x >= size.x || auto ly = y - offsetY;
localPos.y >= size.y) { if (lx < 0 || ly < 0 || lx >= sizeX || ly >= sizeY) {
throw std::invalid_argument("position is out of window"); throw std::invalid_argument("position is out of window");
} }
return firstBuffer[localPos.y * size.x + localPos.x]; return firstBuffer[ly * sizeX + lx];
} }
bool set(const glm::vec<2, TCoord>& pos, T value) { bool set(TCoord x, TCoord y, T value) {
auto localPos = pos - offset; auto lx = x - offsetX;
if (localPos.x < 0 || localPos.y < 0 || localPos.x >= size.x || auto ly = y - offsetY;
localPos.y >= size.y) { if (lx < 0 || ly < 0 || lx >= sizeX || ly >= sizeY) {
return false; return false;
} }
auto& element = firstBuffer[localPos.y * size.x + localPos.x]; auto& element = firstBuffer[ly * sizeX + lx];
if (!element) { if (!element) {
valuesCount++; valuesCount++;
} }
@ -96,41 +97,43 @@ namespace util {
outCallback = callback; outCallback = callback;
} }
void resize(const glm::vec<2, TCoord>& newSize) { void resize(TCoord newSizeX, TCoord newSizeY) {
if (newSize.x < size.x) { if (newSizeX < sizeX) {
TCoord delta = size.x - newSize.x; TCoord delta = sizeX - newSizeX;
translate({delta / 2, 0}); translate(delta / 2, 0);
translate({-delta, 0}); translate(-delta, 0);
translate({delta, 0}); translate(delta, 0);
} }
if (newSize.y < size.y) { if (newSizeY < sizeY) {
TCoord delta = size.y - newSize.y; TCoord delta = sizeY - newSizeY;
translate({0, delta / 2}); translate(0, delta / 2);
translate({0, -delta}); translate(0, -delta);
translate({0, delta}); translate(0, delta);
} }
const TCoord newVolume = newSize.x * newSize.y; const TCoord newVolume = newSizeX * newSizeY;
std::vector<T> newFirstBuffer(newVolume); std::vector<T> newFirstBuffer(newVolume);
std::vector<T> newSecondBuffer(newVolume); std::vector<T> newSecondBuffer(newVolume);
for (TCoord y = 0; y < size.y && y < newSize.y; y++) { for (TCoord y = 0; y < sizeY && y < newSizeY; y++) {
for (TCoord x = 0; x < size.x && x < newSize.x; x++) { for (TCoord x = 0; x < sizeX && x < newSizeX; x++) {
newFirstBuffer[y * newSize.x + x] = firstBuffer[y * size.x + x]; newFirstBuffer[y * newSizeX + x] = firstBuffer[y * sizeX + x];
} }
} }
size = newSize; sizeX = newSizeX;
sizeY = newSizeY;
firstBuffer = std::move(newFirstBuffer); firstBuffer = std::move(newFirstBuffer);
secondBuffer = std::move(newSecondBuffer); secondBuffer = std::move(newSecondBuffer);
} }
void setCenter(const glm::vec<2, TCoord>& center) { void setCenter(TCoord centerX, TCoord centerY) {
auto delta = center - (offset + size / 2); auto deltaX = centerX - (offsetX + sizeX / 2);
if (delta.x | delta.y) { auto deltaY = centerY - (offsetY + sizeY / 2);
translate({delta.x, delta.y}); if (deltaX | deltaY) {
translate(deltaX, deltaY);
} }
} }
void clear() { void clear() {
for (TCoord i = 0; i < size.x * size.y; i++) { for (TCoord i = 0; i < sizeX * sizeY; i++) {
auto value = firstBuffer[i]; auto value = firstBuffer[i];
firstBuffer[i] = {}; firstBuffer[i] = {};
if (outCallback) { if (outCallback) {
@ -140,12 +143,20 @@ namespace util {
valuesCount = 0; valuesCount = 0;
} }
const glm::vec<2, TCoord>& getOffset() const { TCoord getOffsetX() const {
return offset; return offsetX;
} }
const glm::vec<2, TCoord>& getSize() const { TCoord getOffsetY() const {
return size; return offsetY;
}
TCoord getWidth() const {
return sizeX;
}
TCoord getHeight() const {
return sizeX;
} }
const std::vector<T>& getBuffer() const { const std::vector<T>& getBuffer() const {
@ -157,7 +168,7 @@ namespace util {
} }
TCoord area() const { TCoord area() const {
return size.x * size.y; return sizeX * sizeY;
} }
}; };
} }

View File

@ -23,8 +23,8 @@
#include "voxel.hpp" #include "voxel.hpp"
Chunks::Chunks( Chunks::Chunks(
uint32_t w, int32_t w,
uint32_t d, int32_t d,
int32_t ox, int32_t ox,
int32_t oz, int32_t oz,
WorldFiles* wfile, WorldFiles* wfile,
@ -32,9 +32,9 @@ Chunks::Chunks(
) )
: level(level), : level(level),
indices(level->content->getIndices()), indices(level->content->getIndices()),
areaMap({w, d}), areaMap(w, d),
worldFiles(wfile) { worldFiles(wfile) {
areaMap.setCenter({ox-w/2, oz-d/2}); areaMap.setCenter(ox-w/2, oz-d/2);
areaMap.setOutCallback([this](const auto& chunk) { areaMap.setOutCallback([this](const auto& chunk) {
save(chunk.get()); 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 cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz}); auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) { if (ptr == nullptr) {
return 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; def.rotatable ? def.rt.hitboxes[v->state.rotation] : def.hitboxes;
for (const auto& hitbox : boxes) { for (const auto& hitbox : boxes) {
if (hitbox.contains( 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; 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 cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz}); auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) { if (ptr == nullptr) {
return 0; 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 cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz}); auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) { if (ptr == nullptr) {
return 0; 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 cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
if (auto ptr = areaMap.getIf({cx, cz})) { if (auto ptr = areaMap.getIf(cx, cz)) {
return ptr->get(); return ptr->get();
} }
return nullptr; return nullptr;
} }
Chunk* Chunks::getChunk(int x, int z) { 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 ptr->get();
} }
return nullptr; return nullptr;
@ -351,11 +351,9 @@ void Chunks::set(
if (y < 0 || y >= CHUNK_H) { if (y < 0 || y >= CHUNK_H) {
return; return;
} }
int32_t gx = x;
int32_t gz = z;
int cx = floordiv(x, CHUNK_W); int cx = floordiv(x, CHUNK_W);
int cz = floordiv(z, CHUNK_D); int cz = floordiv(z, CHUNK_D);
auto ptr = areaMap.getIf({cx, cz}); auto ptr = areaMap.getIf(cx, cz);
if (ptr == nullptr) { if (ptr == nullptr) {
return; return;
} }
@ -373,7 +371,7 @@ void Chunks::set(
chunk->removeBlockInventory(lx, y, lz); chunk->removeBlockInventory(lx, y, lz);
} }
if (prevdef.rt.extended && !vox.state.segment) { if (prevdef.rt.extended && !vox.state.segment) {
eraseSegments(prevdef, vox.state, gx, y, gz); eraseSegments(prevdef, vox.state, x, y, z);
} }
// block initialization // block initialization
@ -382,7 +380,7 @@ void Chunks::set(
vox.state = state; vox.state = state;
chunk->setModifiedAndUnsaved(); chunk->setModifiedAndUnsaved();
if (!state.segment && newdef.rt.extended) { if (!state.segment && newdef.rt.extended) {
repairSegments(newdef, state, gx, y, gz); repairSegments(newdef, state, x, y, z);
} }
if (y < chunk->bottom) if (y < chunk->bottom)
@ -424,9 +422,9 @@ voxel* Chunks::rayCast(
float dz = dir.z; float dz = dir.z;
float t = 0.0f; float t = 0.0f;
int ix = floor(px); int ix = std::floor(px);
int iy = floor(py); int iy = std::floor(py);
int iz = floor(pz); int iz = std::floor(pz);
int stepx = (dx > 0.0f) ? 1 : -1; int stepx = (dx > 0.0f) ? 1 : -1;
int stepy = (dy > 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 infinity = std::numeric_limits<float>::infinity();
constexpr float epsilon = 1e-6f; // 0.000001 constexpr float epsilon = 1e-6f; // 0.000001
float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx); float txDelta = (std::fabs(dx) < epsilon) ? infinity : std::fabs(1.0f / dx);
float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy); float tyDelta = (std::fabs(dy) < epsilon) ? infinity : std::fabs(1.0f / dy);
float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz); float tzDelta = (std::fabs(dz) < epsilon) ? infinity : std::fabs(1.0f / dz);
float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix); float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix);
float ydist = (stepy > 0) ? (iy + 1 - py) : (py - iy); 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 infinity = std::numeric_limits<float>::infinity();
constexpr float epsilon = 1e-6f; // 0.000001 constexpr float epsilon = 1e-6f; // 0.000001
float txDelta = (fabs(dx) < epsilon) ? infinity : abs(1.0f / dx); float txDelta = (std::fabs(dx) < epsilon) ? infinity : std::fabs(1.0f / dx);
float tyDelta = (fabs(dy) < epsilon) ? infinity : abs(1.0f / dy); float tyDelta = (std::fabs(dy) < epsilon) ? infinity : std::fabs(1.0f / dy);
float tzDelta = (fabs(dz) < epsilon) ? infinity : abs(1.0f / dz); float tzDelta = (std::fabs(dz) < epsilon) ? infinity : std::fabs(1.0f / dz);
float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix); float xdist = (stepx > 0) ? (ix + 1 - px) : (px - ix);
float ydist = (stepy > 0) ? (iy + 1 - py) : (py - iy); 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) { 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) { 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) { 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() { void Chunks::saveAndClear() {

View File

@ -35,14 +35,14 @@ class Chunks {
const Block& def, blockstate state, glm::ivec3 origin, uint8_t rotation const Block& def, blockstate state, glm::ivec3 origin, uint8_t rotation
); );
util::AreaMap2D<std::shared_ptr<Chunk>> areaMap; util::AreaMap2D<std::shared_ptr<Chunk>, int32_t> areaMap;
public: public:
size_t visible = 0; size_t visible = 0;
WorldFiles* worldFiles; WorldFiles* worldFiles;
Chunks( Chunks(
uint32_t w, int32_t w,
uint32_t d, int32_t d,
int32_t ox, int32_t ox,
int32_t oz, int32_t oz,
WorldFiles* worldFiles, WorldFiles* worldFiles,
@ -113,12 +113,20 @@ public:
return areaMap.getBuffer(); return areaMap.getBuffer();
} }
const glm::ivec2& getSize() const { int getWidth() const {
return areaMap.getSize(); return areaMap.getWidth();
} }
const glm::ivec2& getOffset() const { int getHeight() const {
return areaMap.getOffset(); return areaMap.getHeight();
}
int getOffsetX() const {
return areaMap.getOffsetX();
}
int getOffsetY() const {
return areaMap.getOffsetY();
} }
size_t getChunksCount() const { size_t getChunksCount() const {

View File

@ -86,7 +86,7 @@ void Level::loadMatrix(int32_t x, int32_t z, uint32_t radius) {
(settings.chunks.loadDistance.get() + settings.chunks.padding.get()) * (settings.chunks.loadDistance.get() + settings.chunks.padding.get()) *
2LL 2LL
); );
if (chunks->getSize().x != diameter) { if (chunks->getWidth() != diameter) {
chunks->resize(diameter, diameter); chunks->resize(diameter, diameter);
} }
} }