diff --git a/dev/tests/example.lua b/dev/tests/example.lua index 7eccf8741..f7147d067 100644 --- a/dev/tests/example.lua +++ b/dev/tests/example.lua @@ -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) diff --git a/src/logic/BlocksController.cpp b/src/logic/BlocksController.cpp index 980c8bfee..bcc1f7281 100644 --- a/src/logic/BlocksController.cpp +++ b/src/logic/BlocksController.cpp @@ -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), diff --git a/src/logic/BlocksController.hpp b/src/logic/BlocksController.hpp index 0937b3f78..a8f286dcb 100644 --- a/src/logic/BlocksController.hpp +++ b/src/logic/BlocksController.hpp @@ -34,7 +34,7 @@ class BlocksController { FastRandom random {}; std::vector 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); diff --git a/src/logic/ChunksController.cpp b/src/logic/ChunksController.cpp index 0d8d97b4a..55ab262db 100644 --- a/src/logic/ChunksController.cpp +++ b/src/logic/ChunksController.cpp @@ -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(level.content, level.chunks.get())), padding(padding), generator(std::make_unique( level.content->generators.require(level.getWorld()->getGenerator()), @@ -107,9 +107,9 @@ bool ChunksController::buildLights(const std::shared_ptr& 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; } diff --git a/src/logic/ChunksController.hpp b/src/logic/ChunksController.hpp index e9f1ab62a..71dada6fc 100644 --- a/src/logic/ChunksController.hpp +++ b/src/logic/ChunksController.hpp @@ -15,7 +15,6 @@ class ChunksController { private: Level& level; Chunks& chunks; - Lighting& lighting; uint padding; std::unique_ptr generator; @@ -24,6 +23,8 @@ class ChunksController { bool buildLights(const std::shared_ptr& chunk); void createChunk(int x, int y); public: + std::unique_ptr lighting; + ChunksController(Level& level, uint padding); ~ChunksController(); diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index a9c002b19..2f363dad4 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -20,12 +20,12 @@ static debug::Logger logger("level-control"); LevelController::LevelController(Engine* engine, std::unique_ptr levelPtr) : settings(engine->getSettings()), level(std::move(levelPtr)), - blocks(std::make_unique( - *level, settings.chunks.padding.get() - )), chunks(std::make_unique( *level, settings.chunks.padding.get() )) { + blocks = std::make_unique( + *level, *chunks->lighting, settings.chunks.padding.get() + ); scripting::on_world_load(this); } diff --git a/src/logic/scripting/lua/libs/libblock.cpp b/src/logic/scripting/lua/libs/libblock.cpp index fb2a55609..24b001242 100644 --- a/src/logic/scripting/lua/libs/libblock.cpp +++ b/src/logic/scripting/lua/libs/libblock.cpp @@ -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}, {"is_replaceable_at", lua::wrap}, {"set", lua::wrap}, - {"get_fast", lua::wrap}, {"get", lua::wrap}, {"get_X", lua::wrap}, {"get_Y", lua::wrap}, diff --git a/src/logic/scripting/lua/libs/libworld.cpp b/src/logic/scripting/lua/libs/libworld.cpp index 680047778..58e2bdc46 100644 --- a/src/logic/scripting/lua/libs/libworld.cpp +++ b/src/logic/scripting/lua/libs/libworld.cpp @@ -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; diff --git a/src/objects/Player.cpp b/src/objects/Player.cpp index 000d2c041..0dd58f91c 100644 --- a/src/objects/Player.cpp +++ b/src/objects/Player.cpp @@ -44,6 +44,9 @@ Player::Player( position(position), inventory(std::move(inv)), eid(eid), + chunks(std::make_unique( + 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")), diff --git a/src/objects/Player.hpp b/src/objects/Player.hpp index 920e06e43..1f57c6f70 100644 --- a/src/objects/Player.hpp +++ b/src/objects/Player.hpp @@ -8,6 +8,7 @@ #include "settings.hpp" #include "voxels/voxel.hpp" +class Chunks; class Camera; class Inventory; class ContentReport; @@ -55,6 +56,7 @@ class Player : public Serializable { entityid_t eid; entityid_t selectedEid = 0; public: + std::unique_ptr chunks; // not in use yet std::shared_ptr fpCamera, spCamera, tpCamera; std::shared_ptr currentCamera; bool debug = false; diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 8d5231a67..91b05d694 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -1,6 +1,5 @@ #include "Chunks.hpp" -#include #include #include @@ -20,9 +19,6 @@ #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( @@ -30,16 +26,15 @@ Chunks::Chunks( 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()); }); } @@ -319,8 +314,9 @@ void Chunks::resize(uint32_t newW, uint32_t newD) { bool Chunks::putChunk(const std::shared_ptr& 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; @@ -330,8 +326,6 @@ bool Chunks::putChunk(const std::shared_ptr& 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(); diff --git a/src/voxels/Chunks.hpp b/src/voxels/Chunks.hpp index a39c89606..c1c75e9cd 100644 --- a/src/voxels/Chunks.hpp +++ b/src/voxels/Chunks.hpp @@ -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); @@ -40,15 +39,14 @@ class Chunks { ); util::AreaMap2D, 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; @@ -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; + } }; diff --git a/src/world/Level.cpp b/src/world/Level.cpp index 99f47d866..c289579fc 100644 --- a/src/world/Level.cpp +++ b/src/world/Level.cpp @@ -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(this)), physics(std::make_unique(glm::vec3(0, -22.6f, 0))), events(std::make_unique()), entities(std::make_unique(this)), - players(std::make_unique(this)), - settings(settings) { + players(std::make_unique(this)) { const auto& worldInfo = world->getInfo(); auto& cameraIndices = content->getIndices(ResourceType::CAMERA); for (size_t i = 0; i < cameraIndices.size(); i++) { @@ -65,11 +65,9 @@ Level::Level( (settings.chunks.loadDistance.get() + settings.chunks.padding.get()) * 2; chunks = std::make_unique( - matrixSize, matrixSize, 0, 0, world->wfile.get(), this + matrixSize, matrixSize, 0, 0, events.get(), content->getIndices() ); - lighting = std::make_unique(content, chunks.get()); - inventories = std::make_unique(*this); } diff --git a/src/world/Level.hpp b/src/world/Level.hpp index 632746720..b30bde3be 100644 --- a/src/world/Level.hpp +++ b/src/world/Level.hpp @@ -22,6 +22,7 @@ struct EngineSettings; /// @brief A level, contains chunks and objects class Level { + const EngineSettings& settings; std::unique_ptr world; public: const Content* const content; @@ -31,14 +32,11 @@ class Level { std::unique_ptr inventories; std::unique_ptr physics; - std::unique_ptr lighting; std::unique_ptr events; std::unique_ptr entities; std::unique_ptr players; std::vector> cameras; // move somewhere? - const EngineSettings& settings; - Level( std::unique_ptr world, const Content* content,