mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29:44 +00:00
add 'chunk.update-distance' setting
This commit is contained in:
parent
f0a407c20a
commit
cfdc8672ec
5 changed files with 32 additions and 6 deletions
|
|
@ -66,6 +66,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
|
|||
builder.addSection("chunks");
|
||||
builder.add("load-distance", &settings.chunks.loadDistance);
|
||||
builder.add("load-speed", &settings.chunks.loadSpeed);
|
||||
builder.add("update-distance", &settings.chunks.updateDistance);
|
||||
builder.add("padding", &settings.chunks.padding);
|
||||
|
||||
builder.addSection("graphics");
|
||||
|
|
|
|||
|
|
@ -4,9 +4,7 @@
|
|||
#include "items/Inventories.hpp"
|
||||
#include "items/Inventory.hpp"
|
||||
#include "lighting/Lighting.hpp"
|
||||
#include "maths/fastmaths.hpp"
|
||||
#include "scripting/scripting.hpp"
|
||||
#include "util/timeutil.hpp"
|
||||
#include "voxels/Block.hpp"
|
||||
#include "voxels/Chunk.hpp"
|
||||
#include "voxels/Chunks.hpp"
|
||||
|
|
@ -17,15 +15,19 @@
|
|||
#include "objects/Player.hpp"
|
||||
#include "objects/Players.hpp"
|
||||
#include "util/random.hpp"
|
||||
#include "settings.hpp"
|
||||
|
||||
#include <random>
|
||||
|
||||
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),
|
||||
chunks(*level.chunks),
|
||||
lighting(lighting),
|
||||
settings(settings),
|
||||
randTickClock(20, 3),
|
||||
blocksTickClock(20, 3),
|
||||
worldTickClock(20, 1) {
|
||||
|
|
@ -189,6 +191,7 @@ void BlocksController::randomTick(
|
|||
std::shuffle(randomPattern.begin(), randomPattern.end(), randomEngine);
|
||||
}
|
||||
|
||||
int chunkRand = (chunk.x * 73856093) ^ (chunk.z * 19349663);
|
||||
for (int s = 0; s < segments; s++) {
|
||||
int repetions = 4;
|
||||
for (int i = 0; i < repetions; i++) {
|
||||
|
|
@ -196,7 +199,9 @@ void BlocksController::randomTick(
|
|||
if (segmentY > chunk.top) {
|
||||
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 bz = (index / CHUNK_W) % CHUNK_D;
|
||||
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) {
|
||||
auto indices = level.content.getIndices();
|
||||
|
||||
int updateDistance = settings.chunks.updateDistance.get();
|
||||
for (const auto& [pid, player] : *level.players) {
|
||||
const auto& chunks = *player->chunks;
|
||||
int width = chunks.getWidth();
|
||||
int height = chunks.getHeight();
|
||||
const auto& playerPos = player->getPosition();
|
||||
|
||||
for (uint z = padding; z < height - padding; z++) {
|
||||
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) {
|
||||
continue;
|
||||
}
|
||||
long long distance = calc_chunk_distance2(playerPos, *chunk);
|
||||
if (distance > updateDistance * updateDistance) {
|
||||
continue;
|
||||
}
|
||||
if (chunk->lastRandomTickId == randomTickId) {
|
||||
continue;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ class Chunks;
|
|||
class Lighting;
|
||||
class GlobalChunks;
|
||||
class ContentIndices;
|
||||
struct EngineSettings;
|
||||
|
||||
enum class BlockInteraction { step, destruction, placing };
|
||||
|
||||
|
|
@ -27,13 +28,16 @@ class BlocksController {
|
|||
const Level& level;
|
||||
GlobalChunks& chunks;
|
||||
Lighting* lighting;
|
||||
const EngineSettings& settings;
|
||||
util::Clock randTickClock;
|
||||
util::Clock blocksTickClock;
|
||||
util::Clock worldTickClock;
|
||||
std::vector<OnBlockInteraction> blockInteractionCallbacks;
|
||||
uint64_t randomTickId = 0;
|
||||
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, int w, int h, int d);
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ LevelController::LevelController(
|
|||
);
|
||||
}
|
||||
blocks = std::make_unique<BlocksController>(
|
||||
*level, chunks ? chunks->lighting.get() : nullptr
|
||||
*level, chunks ? chunks->lighting.get() : nullptr, settings
|
||||
);
|
||||
scripting::on_world_load(this);
|
||||
|
||||
|
|
|
|||
|
|
@ -45,6 +45,8 @@ struct ChunksSettings {
|
|||
IntegerSetting loadSpeed {4, 1, 32};
|
||||
/// @brief Radius of chunks loading zone (chunk is unit)
|
||||
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)
|
||||
IntegerSetting padding {2, 1, 8};
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue