add 'chunk.update-distance' setting

This commit is contained in:
MihailRis 2026-11-12 22:13:38 +03:00
parent f0a407c20a
commit cfdc8672ec
5 changed files with 32 additions and 6 deletions

View file

@ -66,6 +66,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
builder.addSection("chunks"); builder.addSection("chunks");
builder.add("load-distance", &settings.chunks.loadDistance); builder.add("load-distance", &settings.chunks.loadDistance);
builder.add("load-speed", &settings.chunks.loadSpeed); builder.add("load-speed", &settings.chunks.loadSpeed);
builder.add("update-distance", &settings.chunks.updateDistance);
builder.add("padding", &settings.chunks.padding); builder.add("padding", &settings.chunks.padding);
builder.addSection("graphics"); builder.addSection("graphics");

View file

@ -4,9 +4,7 @@
#include "items/Inventories.hpp" #include "items/Inventories.hpp"
#include "items/Inventory.hpp" #include "items/Inventory.hpp"
#include "lighting/Lighting.hpp" #include "lighting/Lighting.hpp"
#include "maths/fastmaths.hpp"
#include "scripting/scripting.hpp" #include "scripting/scripting.hpp"
#include "util/timeutil.hpp"
#include "voxels/Block.hpp" #include "voxels/Block.hpp"
#include "voxels/Chunk.hpp" #include "voxels/Chunk.hpp"
#include "voxels/Chunks.hpp" #include "voxels/Chunks.hpp"
@ -17,15 +15,19 @@
#include "objects/Player.hpp" #include "objects/Player.hpp"
#include "objects/Players.hpp" #include "objects/Players.hpp"
#include "util/random.hpp" #include "util/random.hpp"
#include "settings.hpp"
#include <random> #include <random>
static inline constexpr int CHUNK_RANDOM_TICK_SEGMENTS = 4; static inline constexpr int CHUNK_RANDOM_TICK_SEGMENTS = 4;
BlocksController::BlocksController(const Level& level, Lighting* lighting) BlocksController::BlocksController(
const Level& level, Lighting* lighting, const EngineSettings& settings
)
: level(level), : level(level),
chunks(*level.chunks), chunks(*level.chunks),
lighting(lighting), lighting(lighting),
settings(settings),
randTickClock(20, 3), randTickClock(20, 3),
blocksTickClock(20, 3), blocksTickClock(20, 3),
worldTickClock(20, 1) { worldTickClock(20, 1) {
@ -189,6 +191,7 @@ void BlocksController::randomTick(
std::shuffle(randomPattern.begin(), randomPattern.end(), randomEngine); std::shuffle(randomPattern.begin(), randomPattern.end(), randomEngine);
} }
int chunkRand = (chunk.x * 73856093) ^ (chunk.z * 19349663);
for (int s = 0; s < segments; s++) { for (int s = 0; s < segments; s++) {
int repetions = 4; int repetions = 4;
for (int i = 0; i < repetions; i++) { for (int i = 0; i < repetions; i++) {
@ -196,7 +199,9 @@ void BlocksController::randomTick(
if (segmentY > chunk.top) { if (segmentY > chunk.top) {
break; break;
} }
size_t index = randomPattern[(s * repetions + i + randomTickId) % randomPattern.size()]; size_t index = randomPattern
[(s * repetions + i + randomTickId + chunkRand) %
randomPattern.size()];
int bx = index % CHUNK_W; int bx = index % CHUNK_W;
int bz = (index / CHUNK_W) % CHUNK_D; int bz = (index / CHUNK_W) % CHUNK_D;
int by = (index / (CHUNK_W * CHUNK_D)) + segmentY; int by = (index / (CHUNK_W * CHUNK_D)) + segmentY;
@ -214,13 +219,23 @@ void BlocksController::randomTick(
} }
} }
static long long calc_chunk_distance2(
const glm::vec3& pos, const Chunk& chunk
) {
int dx = chunk.x - static_cast<int>(std::floor(pos.x / CHUNK_W));
int dz = chunk.z - static_cast<int>(std::floor(pos.z / CHUNK_D));
return dx * dx + dz * dz;
}
void BlocksController::randomTick(int tickid, int parts, uint padding) { void BlocksController::randomTick(int tickid, int parts, uint padding) {
auto indices = level.content.getIndices(); auto indices = level.content.getIndices();
int updateDistance = settings.chunks.updateDistance.get();
for (const auto& [pid, player] : *level.players) { for (const auto& [pid, player] : *level.players) {
const auto& chunks = *player->chunks; const auto& chunks = *player->chunks;
int width = chunks.getWidth(); int width = chunks.getWidth();
int height = chunks.getHeight(); int height = chunks.getHeight();
const auto& playerPos = player->getPosition();
for (uint z = padding; z < height - padding; z++) { for (uint z = padding; z < height - padding; z++) {
for (uint x = padding; x < width - padding; x++) { for (uint x = padding; x < width - padding; x++) {
@ -232,6 +247,10 @@ void BlocksController::randomTick(int tickid, int parts, uint padding) {
if (chunk == nullptr || !chunk->flags.ready) { if (chunk == nullptr || !chunk->flags.ready) {
continue; continue;
} }
long long distance = calc_chunk_distance2(playerPos, *chunk);
if (distance > updateDistance * updateDistance) {
continue;
}
if (chunk->lastRandomTickId == randomTickId) { if (chunk->lastRandomTickId == randomTickId) {
continue; continue;
} }

View file

@ -15,6 +15,7 @@ class Chunks;
class Lighting; class Lighting;
class GlobalChunks; class GlobalChunks;
class ContentIndices; class ContentIndices;
struct EngineSettings;
enum class BlockInteraction { step, destruction, placing }; enum class BlockInteraction { step, destruction, placing };
@ -27,13 +28,16 @@ class BlocksController {
const Level& level; const Level& level;
GlobalChunks& chunks; GlobalChunks& chunks;
Lighting* lighting; Lighting* lighting;
const EngineSettings& settings;
util::Clock randTickClock; util::Clock randTickClock;
util::Clock blocksTickClock; util::Clock blocksTickClock;
util::Clock worldTickClock; util::Clock worldTickClock;
std::vector<OnBlockInteraction> blockInteractionCallbacks; std::vector<OnBlockInteraction> blockInteractionCallbacks;
uint64_t randomTickId = 0; uint64_t randomTickId = 0;
public: public:
BlocksController(const Level& level, Lighting* lighting); BlocksController(
const Level& level, Lighting* lighting, const EngineSettings& settings
);
void updateSides(int x, int y, int z); void updateSides(int x, int y, int z);
void updateSides(int x, int y, int z, int w, int h, int d); void updateSides(int x, int y, int z, int w, int h, int d);

View file

@ -46,7 +46,7 @@ LevelController::LevelController(
); );
} }
blocks = std::make_unique<BlocksController>( blocks = std::make_unique<BlocksController>(
*level, chunks ? chunks->lighting.get() : nullptr *level, chunks ? chunks->lighting.get() : nullptr, settings
); );
scripting::on_world_load(this); scripting::on_world_load(this);

View file

@ -45,6 +45,8 @@ struct ChunksSettings {
IntegerSetting loadSpeed {4, 1, 32}; IntegerSetting loadSpeed {4, 1, 32};
/// @brief Radius of chunks loading zone (chunk is unit) /// @brief Radius of chunks loading zone (chunk is unit)
IntegerSetting loadDistance {22, 3, 80}; IntegerSetting loadDistance {22, 3, 80};
/// @brief Radius of chunks update zone (chunk is unit)
IntegerSetting updateDistance {22, 3, 80};
/// @brief Buffer zone where chunks are not unloading (chunk is unit) /// @brief Buffer zone where chunks are not unloading (chunk is unit)
IntegerSetting padding {2, 1, 8}; IntegerSetting padding {2, 1, 8};
}; };