move seekOrigin logic to the blocks_agent

This commit is contained in:
MihailRis 2024-12-16 22:37:55 +03:00
parent 87c19d69dc
commit c7c2fe29d2
3 changed files with 43 additions and 25 deletions

View File

@ -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<CHUNK_W>(x);
int cz = floordiv<CHUNK_D>(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);

View File

@ -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(

View File

@ -11,6 +11,7 @@
#include "maths/voxmaths.hpp"
#include <stdexcept>
#include <glm/glm.hpp>
/// Using templates to minimize OOP overhead
@ -136,4 +137,27 @@ inline void repair_segments(
}
}
template <class Storage>
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