From c7c2fe29d2bb2f1fe89b9815cf013e239c99dbf0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 16 Dec 2024 22:37:55 +0300 Subject: [PATCH] move seekOrigin logic to the blocks_agent --- src/logic/scripting/lua/libs/libblock.cpp | 26 ++++++++++++++++------- src/voxels/Chunks.cpp | 18 +--------------- src/voxels/blocks_agent.hpp | 24 +++++++++++++++++++++ 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/src/logic/scripting/lua/libs/libblock.cpp b/src/logic/scripting/lua/libs/libblock.cpp index e454903c..743c042b 100644 --- a/src/logic/scripting/lua/libs/libblock.cpp +++ b/src/logic/scripting/lua/libs/libblock.cpp @@ -70,7 +70,7 @@ static int l_is_segment(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); - const auto& vox = level->chunks->require(x, y, z); + const auto& vox = blocks_agent::require(*level->chunksStorage, x, y, z); return lua::pushboolean(L, vox.state.segment); } @@ -78,10 +78,13 @@ static int l_seek_origin(lua::State* L) { auto x = lua::tointeger(L, 1); auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); - const auto& vox = level->chunks->require(x, y, z); + const auto& vox = blocks_agent::require(*level->chunksStorage, x, y, z); auto& def = indices->blocks.require(vox.id); return lua::pushivec_stack( - L, level->chunks->seekOrigin({x, y, z}, def, vox.state) + L, + blocks_agent::seek_origin( + *level->chunksStorage, {x, y, z}, def, vox.state + ) ); } @@ -198,13 +201,18 @@ static int l_set_states(lua::State* L) { auto y = lua::tointeger(L, 2); auto z = lua::tointeger(L, 3); auto states = lua::tointeger(L, 4); - - auto chunk = level->chunks->getChunkByVoxel(x, y, z); + if (y < 0 || y >= CHUNK_H) { + return 0; + } + int cx = floordiv(x); + int cz = floordiv(z); + auto chunk = blocks_agent::get_chunk(*level->chunksStorage, cx, cz); if (chunk == nullptr) { return 0; } - auto vox = level->chunks->get(x, y, z); - vox->state = int2blockstate(states); + int lx = x - cx * CHUNK_W; + int lz = z - cz * CHUNK_D; + chunk->voxels[vox_index(lx, y, lz)].state = int2blockstate(states); chunk->setModifiedAndUnsaved(); return 0; } @@ -223,7 +231,9 @@ static int l_get_user_bits(lua::State* L) { } const auto& def = content->getIndices()->blocks.require(vox->id); if (def.rt.extended) { - auto origin = level->chunks->seekOrigin({x, y, z}, def, vox->state); + auto origin = blocks_agent::seek_origin( + *level->chunksStorage, {x, y, z}, def, vox->state + ); vox = level->chunks->get(origin.x, origin.y, origin.z); if (vox == nullptr) { return lua::pushinteger(L, 0); diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 50812d07..a56c3581 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -160,23 +160,7 @@ Chunk* Chunks::getChunk(int x, int z) const { glm::ivec3 Chunks::seekOrigin( const glm::ivec3& srcpos, const Block& def, blockstate state ) const { - auto pos = srcpos; - const auto& rotation = def.rotations.variants[state.rotation]; - auto segment = state.segment; - while (true) { - if (!segment) { - return pos; - } - if (segment & 1) pos -= rotation.axisX; - if (segment & 2) pos -= rotation.axisY; - if (segment & 4) pos -= rotation.axisZ; - - if (auto* voxel = get(pos.x, pos.y, pos.z)) { - segment = voxel->state.segment; - } else { - return pos; - } - } + return blocks_agent::seek_origin(*this, srcpos, def, state); } void Chunks::eraseSegments( diff --git a/src/voxels/blocks_agent.hpp b/src/voxels/blocks_agent.hpp index 22850f40..3ce5b49b 100644 --- a/src/voxels/blocks_agent.hpp +++ b/src/voxels/blocks_agent.hpp @@ -11,6 +11,7 @@ #include "maths/voxmaths.hpp" #include +#include /// Using templates to minimize OOP overhead @@ -136,4 +137,27 @@ inline void repair_segments( } } +template +inline glm::ivec3 seek_origin( + Storage& chunks, const glm::ivec3& srcpos, const Block& def, blockstate state +) { + auto pos = srcpos; + const auto& rotation = def.rotations.variants[state.rotation]; + auto segment = state.segment; + while (true) { + if (!segment) { + return pos; + } + if (segment & 1) pos -= rotation.axisX; + if (segment & 2) pos -= rotation.axisY; + if (segment & 4) pos -= rotation.axisZ; + + if (auto* voxel = get(chunks, pos.x, pos.y, pos.z)) { + segment = voxel->state.segment; + } else { + return pos; + } + } +} + } // blocks_agent