move Lighting instance to ChunksController

This commit is contained in:
MihailRis
2024-12-17 05:13:49 +03:00
parent c5049d0e24
commit b7664b4188
14 changed files with 60 additions and 61 deletions
+2 -2
View File
@@ -14,10 +14,10 @@
#include "world/Level.hpp"
#include "world/World.hpp"
BlocksController::BlocksController(const Level& level, uint padding)
BlocksController::BlocksController(const Level& level, Lighting& lighting, uint padding)
: level(level),
chunks(*level.chunks),
lighting(*level.lighting),
lighting(lighting),
randTickClock(20, 3),
blocksTickClock(20, 1),
worldTickClock(20, 1),
+1 -1
View File
@@ -34,7 +34,7 @@ class BlocksController {
FastRandom random {};
std::vector<on_block_interaction> blockInteractionCallbacks;
public:
BlocksController(const Level& level, uint padding);
BlocksController(const Level& level, Lighting& lighting, uint padding);
void updateSides(int x, int y, int z);
void updateSides(int x, int y, int z, int w, int h, int d);
+3 -3
View File
@@ -25,7 +25,7 @@ const uint MIN_SURROUNDING = 9;
ChunksController::ChunksController(Level& level, uint padding)
: level(level),
chunks(*level.chunks),
lighting(*level.lighting),
lighting(std::make_unique<Lighting>(level.content, level.chunks.get())),
padding(padding),
generator(std::make_unique<WorldGenerator>(
level.content->generators.require(level.getWorld()->getGenerator()),
@@ -107,9 +107,9 @@ bool ChunksController::buildLights(const std::shared_ptr<Chunk>& chunk) {
if (surrounding == MIN_SURROUNDING) {
bool lightsCache = chunk->flags.loadedLights;
if (!lightsCache) {
lighting.buildSkyLight(chunk->x, chunk->z);
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;
}
+2 -1
View File
@@ -15,7 +15,6 @@ class ChunksController {
private:
Level& level;
Chunks& chunks;
Lighting& lighting;
uint padding;
std::unique_ptr<WorldGenerator> generator;
@@ -24,6 +23,8 @@ private:
bool buildLights(const std::shared_ptr<Chunk>& chunk);
void createChunk(int x, int y);
public:
std::unique_ptr<Lighting> lighting;
ChunksController(Level& level, uint padding);
~ChunksController();
+3 -3
View File
@@ -20,12 +20,12 @@ static debug::Logger logger("level-control");
LevelController::LevelController(Engine* engine, std::unique_ptr<Level> levelPtr)
: settings(engine->getSettings()),
level(std::move(levelPtr)),
blocks(std::make_unique<BlocksController>(
*level, settings.chunks.padding.get()
)),
chunks(std::make_unique<ChunksController>(
*level, settings.chunks.padding.get()
)) {
blocks = std::make_unique<BlocksController>(
*level, *chunks->lighting, settings.chunks.padding.get()
);
scripting::on_world_load(this);
}
+7 -11
View File
@@ -104,7 +104,13 @@ static int l_set(lua::State* L) {
return 0;
}
blocks_agent::set(*level->chunksStorage, x, y, z, id, int2blockstate(state));
level->lighting->onBlockSet(x, y, z, id);
auto chunksController = controller->getChunksController();
if (chunksController == nullptr) {
return 1;
}
Lighting& lighting = *chunksController->lighting;
lighting.onBlockSet(x, y, z, id);
if (!noupdate) {
blocks->updateSides(x, y, z);
}
@@ -120,15 +126,6 @@ static int l_get(lua::State* L) {
return lua::pushinteger(L, id);
}
static int l_get_fast(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
auto z = lua::tointeger(L, 3);
auto vox = blocks_agent::get(*level->chunks, x, y, z);
int id = vox == nullptr ? -1 : vox->id;
return lua::pushinteger(L, id);
}
static int l_get_x(lua::State* L) {
auto x = lua::tointeger(L, 1);
auto y = lua::tointeger(L, 2);
@@ -638,7 +635,6 @@ const luaL_Reg blocklib[] = {
{"is_solid_at", lua::wrap<l_is_solid_at>},
{"is_replaceable_at", lua::wrap<l_is_replaceable_at>},
{"set", lua::wrap<l_set>},
{"get_fast", lua::wrap<l_get_fast>},
{"get", lua::wrap<l_get>},
{"get_X", lua::wrap<l_get_x>},
{"get_Y", lua::wrap<l_get_y>},
+15 -6
View File
@@ -16,6 +16,8 @@
#include "voxels/GlobalChunks.hpp"
#include "world/Level.hpp"
#include "world/World.hpp"
#include "logic/LevelController.hpp"
#include "logic/ChunksController.hpp"
using namespace scripting;
namespace fs = std::filesystem;
@@ -203,30 +205,37 @@ static int l_set_chunk_data(lua::State* L) {
} else {
chunk->decode(buffer->data().data());
}
auto chunksController = controller->getChunksController();
if (chunksController == nullptr) {
return 1;
}
Lighting& lighting = *chunksController->lighting;
chunk->updateHeights();
level->lighting->buildSkyLight(x, y);
lighting.buildSkyLight(x, y);
chunk->flags.modified = true;
level->lighting->onChunkLoaded(x, y, true);
lighting.onChunkLoaded(x, y, true);
chunk = level->chunks->getChunk(x - 1, y);
if (chunk != nullptr) {
chunk->flags.modified = true;
level->lighting->onChunkLoaded(x - 1, y, true);
lighting.onChunkLoaded(x - 1, y, true);
}
chunk = level->chunks->getChunk(x + 1, y);
if (chunk != nullptr) {
chunk->flags.modified = true;
level->lighting->onChunkLoaded(x + 1, y, true);
lighting.onChunkLoaded(x + 1, y, true);
}
chunk = level->chunks->getChunk(x, y - 1);
if (chunk != nullptr) {
chunk->flags.modified = true;
level->lighting->onChunkLoaded(x, y - 1, true);
lighting.onChunkLoaded(x, y - 1, true);
}
chunk = level->chunks->getChunk(x, y + 1);
if (chunk != nullptr) {
chunk->flags.modified = true;
level->lighting->onChunkLoaded(x, y + 1, true);
lighting.onChunkLoaded(x, y + 1, true);
}
return 1;