Skip to content

Commit

Permalink
move Lighting instance to ChunksController
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 17, 2024
1 parent c5049d0 commit b7664b4
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 61 deletions.
6 changes: 1 addition & 5 deletions dev/tests/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ assert(player.get_name(pid) == "Xerxes")
test.sleep_until(function() return world.count_chunks() >= 9 end, 1000)
print(world.count_chunks())

for i=1,10 do
print("--------------------")
timeit(10000000, block.get_fast, 0, 0, 0)
timeit(10000000, block.get, 0, 0, 0)
end
timeit(10000000, block.get, 0, 0, 0)

block.destruct(0, 0, 0, pid)
assert(block.get(0, 0, 0) == 0)
Expand Down
4 changes: 2 additions & 2 deletions src/logic/BlocksController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/logic/BlocksController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/logic/ChunksController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 2 additions & 1 deletion src/logic/ChunksController.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class ChunksController {
private:
Level& level;
Chunks& chunks;
Lighting& lighting;
uint padding;
std::unique_ptr<WorldGenerator> generator;

Expand All @@ -24,6 +23,8 @@ class ChunksController {
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();

Expand Down
6 changes: 3 additions & 3 deletions src/logic/LevelController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
18 changes: 7 additions & 11 deletions src/logic/scripting/lua/libs/libblock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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>},
Expand Down
21 changes: 15 additions & 6 deletions src/logic/scripting/lua/libs/libworld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/objects/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ Player::Player(
position(position),
inventory(std::move(inv)),
eid(eid),
chunks(std::make_unique<Chunks>(
3, 3, 0, 0, level->events.get(), level->content->getIndices()
)),
fpCamera(level->getCamera("core:first-person")),
spCamera(level->getCamera("core:third-person-front")),
tpCamera(level->getCamera("core:third-person-back")),
Expand Down
2 changes: 2 additions & 0 deletions src/objects/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "settings.hpp"
#include "voxels/voxel.hpp"

class Chunks;
class Camera;
class Inventory;
class ContentReport;
Expand Down Expand Up @@ -55,6 +56,7 @@ class Player : public Serializable {
entityid_t eid;
entityid_t selectedEid = 0;
public:
std::unique_ptr<Chunks> chunks; // not in use yet
std::shared_ptr<Camera> fpCamera, spCamera, tpCamera;
std::shared_ptr<Camera> currentCamera;
bool debug = false;
Expand Down
24 changes: 9 additions & 15 deletions src/voxels/Chunks.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Chunks.hpp"

#include <limits.h>
#include <math.h>

#include <algorithm>
Expand All @@ -20,26 +19,22 @@
#include "world/Level.hpp"
#include "world/LevelEvents.hpp"
#include "VoxelsVolume.hpp"
#include "Block.hpp"
#include "Chunk.hpp"
#include "voxel.hpp"
#include "blocks_agent.hpp"

Chunks::Chunks(
int32_t w,
int32_t d,
int32_t ox,
int32_t oz,
WorldFiles* wfile,
Level* level
LevelEvents* events,
const ContentIndices* indices
)
: level(level),
indices(level ? level->content->getIndices() : nullptr),
areaMap(w, d),
worldFiles(wfile) {
: events(events),
indices(indices),
areaMap(w, d) {
areaMap.setCenter(ox-w/2, oz-d/2);
areaMap.setOutCallback([this](int, int, const auto& chunk) {
this->level->events->trigger(EVT_CHUNK_HIDDEN, chunk.get());
this->events->trigger(EVT_CHUNK_HIDDEN, chunk.get());
});
}

Expand Down Expand Up @@ -319,8 +314,9 @@ void Chunks::resize(uint32_t newW, uint32_t newD) {

bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
if (areaMap.set(chunk->x, chunk->z, chunk)) {
if (level)
level->events->trigger(LevelEventType::EVT_CHUNK_SHOWN, chunk.get());
if (events) {
events->trigger(LevelEventType::EVT_CHUNK_SHOWN, chunk.get());
}
return true;
}
return false;
Expand All @@ -330,8 +326,6 @@ bool Chunks::putChunk(const std::shared_ptr<Chunk>& chunk) {
// 25.06.2024: not now
// 11.11.2024: not now
void Chunks::getVoxels(VoxelsVolume* volume, bool backlight) const {
const Content* content = level->content;
auto indices = content->getIndices();
voxel* voxels = volume->getVoxels();
light_t* lights = volume->getLights();
int x = volume->getX();
Expand Down
12 changes: 7 additions & 5 deletions src/voxels/Chunks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ class Chunk;
class WorldFiles;
class LevelEvents;
class Block;
class Level;
class VoxelsVolume;

/// Player-centred chunks matrix
class Chunks {
Level* level;
LevelEvents* events;
const ContentIndices* const indices;

void eraseSegments(const Block& def, blockstate state, int x, int y, int z);
Expand All @@ -40,15 +39,14 @@ class Chunks {
);

util::AreaMap2D<std::shared_ptr<Chunk>, int32_t> areaMap;
WorldFiles* worldFiles;
public:
Chunks(
int32_t w,
int32_t d,
int32_t ox,
int32_t oz,
WorldFiles* worldFiles,
Level* level
LevelEvents* events,
const ContentIndices* indices
);
~Chunks() = default;

Expand Down Expand Up @@ -156,4 +154,8 @@ class Chunks {
const ContentIndices& getContentIndices() const {
return *indices;
}

static inline constexpr unsigned matrixSize(int loadDistance, int padding) {
return (loadDistance + padding) * 2;
}
};
10 changes: 4 additions & 6 deletions src/world/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ Level::Level(
const Content* content,
EngineSettings& settings
)
: world(std::move(worldPtr)),
: settings(settings),
world(std::move(worldPtr)),
content(content),
chunksStorage(std::make_unique<GlobalChunks>(this)),
physics(std::make_unique<PhysicsSolver>(glm::vec3(0, -22.6f, 0))),
events(std::make_unique<LevelEvents>()),
entities(std::make_unique<Entities>(this)),
players(std::make_unique<Players>(this)),
settings(settings) {
players(std::make_unique<Players>(this)) {
const auto& worldInfo = world->getInfo();
auto& cameraIndices = content->getIndices(ResourceType::CAMERA);
for (size_t i = 0; i < cameraIndices.size(); i++) {
Expand Down Expand Up @@ -65,11 +65,9 @@ Level::Level(
(settings.chunks.loadDistance.get() + settings.chunks.padding.get()) *
2;
chunks = std::make_unique<Chunks>(
matrixSize, matrixSize, 0, 0, world->wfile.get(), this
matrixSize, matrixSize, 0, 0, events.get(), content->getIndices()
);

lighting = std::make_unique<Lighting>(content, chunks.get());

inventories = std::make_unique<Inventories>(*this);
}

Expand Down
4 changes: 1 addition & 3 deletions src/world/Level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct EngineSettings;

/// @brief A level, contains chunks and objects
class Level {
const EngineSettings& settings;
std::unique_ptr<World> world;
public:
const Content* const content;
Expand All @@ -31,14 +32,11 @@ class Level {
std::unique_ptr<Inventories> inventories;

std::unique_ptr<PhysicsSolver> physics;
std::unique_ptr<Lighting> lighting;
std::unique_ptr<LevelEvents> events;
std::unique_ptr<Entities> entities;
std::unique_ptr<Players> players;
std::vector<std::shared_ptr<Camera>> cameras; // move somewhere?

const EngineSettings& settings;

Level(
std::unique_ptr<World> world,
const Content* content,
Expand Down

0 comments on commit b7664b4

Please sign in to comment.