From 9ec8788838773ad87b050a9d7c4f6656bcbc0901 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 12 Dec 2024 15:54:56 +0300 Subject: [PATCH] add player.create, test.set_setting, test.sleep_until, world.count_chunks --- dev/tests/example.lua | 13 ++++++- res/scripts/stdlib.lua | 12 +++++++ src/frontend/screens/LevelScreen.cpp | 11 ------ src/lighting/Lighting.cpp | 41 +++++++++++----------- src/logic/LevelController.cpp | 13 +++++++ src/logic/scripting/lua/libs/libplayer.cpp | 9 +++++ src/logic/scripting/lua/libs/libworld.cpp | 9 +++++ src/util/AreaMap2D.hpp | 4 +-- src/voxels/ChunksStorage.cpp | 4 +++ src/voxels/ChunksStorage.hpp | 2 ++ 10 files changed, 84 insertions(+), 34 deletions(-) diff --git a/dev/tests/example.lua b/dev/tests/example.lua index 8cde7829..27833cd1 100644 --- a/dev/tests/example.lua +++ b/dev/tests/example.lua @@ -1 +1,12 @@ -print("hello world") +test.set_setting("chunks.load-distance", 2) +test.set_setting("chunks.load-speed", 16) + +test.new_world("demo", "2019", "core:default") +local pid = player.create("Xerxes") +assert(player.get_name(pid) == "Xerxes") +test.sleep_until(function() return world.count_chunks() >= 9 end, 1000) +print(world.count_chunks()) +assert(block.get(0, 0, 0) == block.index("core:obstacle")) +block.destruct(0, 0, 0, pid) +assert(block.get(0, 0, 0) == 0) +test.close_world(true) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 3d21def6..0298a2b9 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -16,7 +16,19 @@ if test then test.open_world = core.open_world test.close_world = core.close_world test.reconfig_packs = core.reconfig_packs + test.set_setting = core.set_setting test.tick = coroutine.yield + + function test.sleep_until(predicate, max_ticks) + max_ticks = max_ticks or 1e9 + local ticks = 0 + while ticks < max_ticks and not predicate() do + test.tick() + end + if ticks == max_ticks then + error("max ticks exceed") + end + end end ------------------------------------------------ diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index 98b21613..9470e7e0 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -187,17 +187,6 @@ void LevelScreen::update(float delta) { level->getWorld()->updateTimers(delta); animator->update(delta); } - - glm::vec3 position = player->getPosition(); - level->loadMatrix( - position.x, - position.z, - settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2 - ); - controller->getChunksController()->update( - settings.chunks.loadSpeed.get(), settings.chunks.loadDistance.get(), - floordiv(position.x, CHUNK_W), floordiv(position.z, CHUNK_D) - ); if (!hud->isPause()) { playerController->update(delta, !inputLocked); } diff --git a/src/lighting/Lighting.cpp b/src/lighting/Lighting.cpp index 6aad3049..8d496b47 100644 --- a/src/lighting/Lighting.cpp +++ b/src/lighting/Lighting.cpp @@ -86,11 +86,12 @@ void Lighting::buildSkyLight(int cx, int cz){ solverS->solve(); } -void Lighting::onChunkLoaded(int cx, int cz, bool expand){ - LightSolver* solverR = this->solverR.get(); - LightSolver* solverG = this->solverG.get(); - LightSolver* solverB = this->solverB.get(); - LightSolver* solverS = this->solverS.get(); + +void Lighting::onChunkLoaded(int cx, int cz, bool expand) { + auto& solverR = *this->solverR; + auto& solverG = *this->solverG; + auto& solverB = *this->solverB; + auto& solverS = *this->solverS; auto blockDefs = content->getIndices()->blocks.getDefs(); auto chunk = chunks->getChunk(cx, cz); @@ -103,9 +104,9 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){ int gx = x + cx * CHUNK_W; int gz = z + cz * CHUNK_D; if (block->rt.emissive){ - solverR->add(gx,y,gz,block->emission[0]); - solverG->add(gx,y,gz,block->emission[1]); - solverB->add(gx,y,gz,block->emission[2]); + solverR.add(gx,y,gz,block->emission[0]); + solverG.add(gx,y,gz,block->emission[1]); + solverB.add(gx,y,gz,block->emission[2]); } } } @@ -119,10 +120,10 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){ int gz = z + cz * CHUNK_D; int rgbs = chunk->lightmap.get(x, y, z); if (rgbs){ - solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0)); - solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1)); - solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2)); - solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3)); + solverR.add(gx,y,gz, Lightmap::extract(rgbs, 0)); + solverG.add(gx,y,gz, Lightmap::extract(rgbs, 1)); + solverB.add(gx,y,gz, Lightmap::extract(rgbs, 2)); + solverS.add(gx,y,gz, Lightmap::extract(rgbs, 3)); } } } @@ -134,19 +135,19 @@ void Lighting::onChunkLoaded(int cx, int cz, bool expand){ int gz = z + cz * CHUNK_D; int rgbs = chunk->lightmap.get(x, y, z); if (rgbs){ - solverR->add(gx,y,gz, Lightmap::extract(rgbs, 0)); - solverG->add(gx,y,gz, Lightmap::extract(rgbs, 1)); - solverB->add(gx,y,gz, Lightmap::extract(rgbs, 2)); - solverS->add(gx,y,gz, Lightmap::extract(rgbs, 3)); + solverR.add(gx,y,gz, Lightmap::extract(rgbs, 0)); + solverG.add(gx,y,gz, Lightmap::extract(rgbs, 1)); + solverB.add(gx,y,gz, Lightmap::extract(rgbs, 2)); + solverS.add(gx,y,gz, Lightmap::extract(rgbs, 3)); } } } } } - solverR->solve(); - solverG->solve(); - solverB->solve(); - solverS->solve(); + solverR.solve(); + solverG.solve(); + solverB.solve(); + solverS.solve(); } void Lighting::onBlockSet(int x, int y, int z, blockid_t id){ diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index 9e30db48..a9c002b1 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -8,6 +8,7 @@ #include "maths/voxmaths.hpp" #include "objects/Entities.hpp" #include "objects/Players.hpp" +#include "objects/Player.hpp" #include "physics/Hitbox.hpp" #include "scripting/scripting.hpp" #include "settings.hpp" @@ -29,6 +30,18 @@ LevelController::LevelController(Engine* engine, std::unique_ptr levelPtr } void LevelController::update(float delta, bool pause) { + for (const auto& [uid, player] : *level->players) { + glm::vec3 position = player->getPosition(); + level->loadMatrix( + position.x, + position.z, + settings.chunks.loadDistance.get() + settings.chunks.padding.get() * 2 + ); + chunks->update( + settings.chunks.loadSpeed.get(), settings.chunks.loadDistance.get(), + floordiv(position.x, CHUNK_W), floordiv(position.z, CHUNK_D) + ); + } if (!pause) { // update all objects that needed blocks->update(delta); diff --git a/src/logic/scripting/lua/libs/libplayer.cpp b/src/logic/scripting/lua/libs/libplayer.cpp index 0628c5af..00c4585d 100644 --- a/src/logic/scripting/lua/libs/libplayer.cpp +++ b/src/logic/scripting/lua/libs/libplayer.cpp @@ -250,6 +250,14 @@ static int l_set_name(lua::State* L) { return 0; } +static int l_create(lua::State* L) { + auto player = level->players->create(); + if (lua::isstring(L, 1)) { + player->setName(lua::require_string(L, 1)); + } + return lua::pushinteger(L, player->getId()); +} + const luaL_Reg playerlib[] = { {"get_pos", lua::wrap}, {"set_pos", lua::wrap}, @@ -277,5 +285,6 @@ const luaL_Reg playerlib[] = { {"set_camera", lua::wrap}, {"get_name", lua::wrap}, {"set_name", lua::wrap}, + {"create", lua::wrap}, {NULL, NULL} }; diff --git a/src/logic/scripting/lua/libs/libworld.cpp b/src/logic/scripting/lua/libs/libworld.cpp index 99d2fec3..747c68a4 100644 --- a/src/logic/scripting/lua/libs/libworld.cpp +++ b/src/logic/scripting/lua/libs/libworld.cpp @@ -13,6 +13,7 @@ #include "lighting/Lighting.hpp" #include "voxels/Chunk.hpp" #include "voxels/Chunks.hpp" +#include "voxels/ChunksStorage.hpp" #include "world/Level.hpp" #include "world/World.hpp" @@ -231,6 +232,13 @@ static int l_set_chunk_data(lua::State* L) { return 1; } +static int l_count_chunks(lua::State* L) { + if (level == nullptr) { + return 0; + } + return lua::pushinteger(L, level->chunksStorage->size()); +} + const luaL_Reg worldlib[] = { {"is_open", lua::wrap}, {"get_list", lua::wrap}, @@ -246,5 +254,6 @@ const luaL_Reg worldlib[] = { {"exists", lua::wrap}, {"get_chunk_data", lua::wrap}, {"set_chunk_data", lua::wrap}, + {"count_chunks", lua::wrap}, {NULL, NULL} }; diff --git a/src/util/AreaMap2D.hpp b/src/util/AreaMap2D.hpp index 1a6c97ce..b7eda1fb 100644 --- a/src/util/AreaMap2D.hpp +++ b/src/util/AreaMap2D.hpp @@ -27,7 +27,7 @@ namespace util { std::fill(secondBuffer.begin(), secondBuffer.end(), T{}); for (TCoord y = 0; y < sizeY; y++) { for (TCoord x = 0; x < sizeX; x++) { - auto& value = firstBuffer[y * sizeX + x]; + auto value = std::move(firstBuffer[y * sizeX + x]); auto nx = x - dx; auto ny = y - dy; if (value == T{}) { @@ -40,7 +40,7 @@ namespace util { valuesCount--; continue; } - secondBuffer[ny * sizeX + nx] = value; + secondBuffer[ny * sizeX + nx] = std::move(value); } } std::swap(firstBuffer, secondBuffer); diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index c19803f4..d3dd2546 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -109,3 +109,7 @@ std::shared_ptr ChunksStorage::create(int x, int z) { chunk->blocksMetadata = regions.getBlocksData(chunk->x, chunk->z); return chunk; } + +size_t ChunksStorage::size() const { + return chunksMap->size(); +} diff --git a/src/voxels/ChunksStorage.hpp b/src/voxels/ChunksStorage.hpp index 62cccaf3..cda81bcb 100644 --- a/src/voxels/ChunksStorage.hpp +++ b/src/voxels/ChunksStorage.hpp @@ -17,4 +17,6 @@ public: std::shared_ptr fetch(int x, int z); std::shared_ptr create(int x, int z); + + size_t size() const; };