limit chunks non-unloading zone to a circle

This commit is contained in:
MihailRis 2025-07-13 15:57:57 +03:00
parent b02b454573
commit 15a778299c
6 changed files with 68 additions and 6 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -133,6 +133,8 @@ public:
void saveAndClear();
void remove(int32_t x, int32_t z);
const std::vector<std::shared_ptr<Chunk>>& getChunks() const {
return areaMap.getBuffer();
}