move Chunks from Level to Player

This commit is contained in:
MihailRis
2024-12-17 19:40:00 +03:00
parent b7664b4188
commit 1c18c02092
40 changed files with 450 additions and 220 deletions
+23 -16
View File
@@ -11,6 +11,7 @@
#include "lighting/Lighting.hpp"
#include "maths/voxmaths.hpp"
#include "util/timeutil.hpp"
#include "objects/Player.hpp"
#include "voxels/Block.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Chunks.hpp"
@@ -24,8 +25,6 @@ const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level& level, uint padding)
: level(level),
chunks(*level.chunks),
lighting(std::make_unique<Lighting>(level.content, level.chunks.get())),
padding(padding),
generator(std::make_unique<WorldGenerator>(
level.content->generators.require(level.getWorld()->getGenerator()),
@@ -36,15 +35,19 @@ ChunksController::ChunksController(Level& level, uint padding)
ChunksController::~ChunksController() = default;
void ChunksController::update(
int64_t maxDuration, int loadDistance, int centerX, int centerY
) {
int64_t maxDuration, int loadDistance, Player& player
) const {
const auto& position = player.getPosition();
int centerX = floordiv<CHUNK_W>(position.x);
int centerY = floordiv<CHUNK_D>(position.z);
generator->update(centerX, centerY, loadDistance);
int64_t mcstotal = 0;
for (uint i = 0; i < MAX_WORK_PER_FRAME; i++) {
timeutil::Timer timer;
if (loadVisible()) {
if (loadVisible(player)) {
int64_t mcs = timer.stop();
if (mcstotal + mcs < maxDuration * 1000) {
mcstotal += mcs;
@@ -55,7 +58,8 @@ void ChunksController::update(
}
}
bool ChunksController::loadVisible() {
bool ChunksController::loadVisible(const Player& player) const {
const auto& chunks = *player.chunks;
int sizeX = chunks.getWidth();
int sizeY = chunks.getHeight();
@@ -69,7 +73,7 @@ bool ChunksController::loadVisible() {
auto& chunk = chunks.getChunks()[index];
if (chunk != nullptr) {
if (chunk->flags.loaded && !chunk->flags.lighted) {
if (buildLights(chunk)) {
if (buildLights(player, chunk)) {
return true;
}
}
@@ -93,32 +97,35 @@ bool ChunksController::loadVisible() {
}
int offsetX = chunks.getOffsetX();
int offsetY = chunks.getOffsetY();
createChunk(nearX + offsetX, nearZ + offsetY);
createChunk(player, nearX + offsetX, nearZ + offsetY);
return true;
}
bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
bool ChunksController::buildLights(const Player& player, const std::shared_ptr<Chunk>& chunk) const {
int surrounding = 0;
for (int oz = -1; oz <= 1; oz++) {
for (int ox = -1; ox <= 1; ox++) {
if (chunks.getChunk(chunk->x + ox, chunk->z + oz)) surrounding++;
if (player.chunks->getChunk(chunk->x + ox, chunk->z + oz))
surrounding++;
}
}
if (surrounding == MIN_SURROUNDING) {
bool lightsCache = chunk->flags.loadedLights;
if (!lightsCache) {
lighting->buildSkyLight(chunk->x, chunk->z);
if (lighting) {
bool lightsCache = chunk->flags.loadedLights;
if (!lightsCache) {
lighting->buildSkyLight(chunk->x, chunk->z);
}
lighting->onChunkLoaded(chunk->x, chunk->z, !lightsCache);
}
lighting->onChunkLoaded(chunk->x, chunk->z, !lightsCache);
chunk->flags.lighted = true;
return true;
}
return false;
}
void ChunksController::createChunk(int x, int z) {
void ChunksController::createChunk(const Player& player, int x, int z) const {
auto chunk = level.chunksStorage->create(x, z);
chunks.putChunk(chunk);
player.chunks->putChunk(chunk);
auto& chunkFlags = chunk->flags;
if (!chunkFlags.loaded) {