diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index 74003b2b..60cee6c4 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -270,6 +270,7 @@ void Hud::updateHotbarControl() { void Hud::updateWorldGenDebug() { auto& level = frontend.getLevel(); const auto& chunks = *player.chunks; + uint padding = engine.getSettings().chunks.padding.get(); auto generator = frontend.getController()->getChunksController()->getGenerator(); auto debugInfo = generator->createDebugInfo(); @@ -291,8 +292,15 @@ void Hud::updateWorldGenDebug() { int ax = x - (width - areaWidth) / 2; int az = z - (height - areaHeight) / 2; - data[(flippedZ * width + x) * 4 + 1] = - chunks.getChunk(ax + ox, az + oz) ? 255 : 0; + bool isInLoadingZone = + frontend.getController() + ->getChunksController() + ->isInLoadingZone(player, padding, ax + ox, az + oz); + + data[(flippedZ * width + x) * 4 + 1] = + chunks.getChunk(ax + ox, az + oz) + ? (isInLoadingZone ? 255 : 128) + : 0; data[(flippedZ * width + x) * 4 + 0] = level.chunks->fetch(ax + ox, az + oz) ? 255 : 0; diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index 1c90b122..97bbdf99 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -60,18 +60,55 @@ void ChunksController::update( } } -bool ChunksController::loadVisible(const Player& player, uint padding) const { +bool ChunksController::isInLoadingZone( + const Player& player, uint padding, int x, int z +) const { const auto& chunks = *player.chunks; int sizeX = chunks.getWidth(); int sizeY = chunks.getHeight(); + int minDistance = ((sizeX - padding * 2) / 2) * ((sizeY - padding * 2) / 2); + + int lx = (x - chunks.getOffsetX()) - sizeX / 2; + int lz = (z - chunks.getOffsetY()) - sizeY / 2; + int distance = (lx * lx + lz * lz); + + return distance < minDistance; +} + +bool ChunksController::loadVisible(const Player& player, uint padding) const { + auto& chunks = *player.chunks; + int sizeX = chunks.getWidth(); + int sizeY = chunks.getHeight(); + int nearX = 0; int nearZ = 0; bool assigned = false; int minDistance = ((sizeX - padding * 2) / 2) * ((sizeY - padding * 2) / 2); + int maxDistance = ((sizeX) / 2) * ((sizeY) / 2); + for (uint z = 0; z < sizeY; z++) { + for (uint x = 0; x < sizeX; x++) { + int index = z * sizeX + x; + int lx = x - sizeX / 2; + int lz = z - sizeY / 2; + int distance = (lx * lx + lz * lz); + auto& chunk = chunks.getChunks()[index]; + if (chunk != nullptr) { + if (distance >= maxDistance) { + chunks.remove( + x + chunks.getOffsetX(), z + chunks.getOffsetY() + ); + } + continue; + } + } + } for (uint z = padding; z < sizeY - padding; z++) { for (uint x = padding; x < sizeX - padding; x++) { int index = z * sizeX + x; + int lx = x - sizeX / 2; + int lz = z - sizeY / 2; + int distance = (lx * lx + lz * lz); auto& chunk = chunks.getChunks()[index]; if (chunk != nullptr) { if (chunk->flags.loaded && !chunk->flags.lighted) { @@ -81,9 +118,7 @@ bool ChunksController::loadVisible(const Player& player, uint padding) const { } continue; } - int lx = x - sizeX / 2; - int lz = z - sizeY / 2; - int distance = (lx * lx + lz * lz); + if (distance < minDistance) { minDistance = distance; nearX = x; diff --git a/src/logic/ChunksController.hpp b/src/logic/ChunksController.hpp index 80901bff..e9f367da 100644 --- a/src/logic/ChunksController.hpp +++ b/src/logic/ChunksController.hpp @@ -32,6 +32,8 @@ public: int64_t maxDuration, int loadDistance, uint padding, Player& player ) const; + bool isInLoadingZone(const Player& player, uint padding, int x, int z) const; + const WorldGenerator* getGenerator() const { return generator.get(); } diff --git a/src/util/AreaMap2D.hpp b/src/util/AreaMap2D.hpp index b7eda1fb..9ec0e439 100644 --- a/src/util/AreaMap2D.hpp +++ b/src/util/AreaMap2D.hpp @@ -114,6 +114,17 @@ namespace util { return true; } + void remove(TCoord x, TCoord y) { + auto lx = x - offsetX; + auto ly = y - offsetY; + if (lx < 0 || ly < 0 || lx >= sizeX || ly >= sizeY) { + return; + } + if (outCallback) + outCallback(x, y, firstBuffer[ly * sizeX + lx]); + firstBuffer[ly * sizeX + lx] = T{}; + } + void setOutCallback(const OutCallback& callback) { outCallback = callback; } diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 60422c60..b9650b99 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -419,3 +419,7 @@ void Chunks::getVoxels(VoxelsVolume& volume, bool backlight) const { void Chunks::saveAndClear() { areaMap.clear(); } + +void Chunks::remove(int32_t x, int32_t z) { + areaMap.remove(x, z); +} diff --git a/src/voxels/Chunks.hpp b/src/voxels/Chunks.hpp index 172cc329..2b2183f5 100644 --- a/src/voxels/Chunks.hpp +++ b/src/voxels/Chunks.hpp @@ -133,6 +133,8 @@ public: void saveAndClear(); + void remove(int32_t x, int32_t z); + const std::vector>& getChunks() const { return areaMap.getBuffer(); }