move seekOrigin logic to the blocks_agent
This commit is contained in:
parent
87c19d69dc
commit
c7c2fe29d2
@ -70,7 +70,7 @@ static int l_is_segment(lua::State* L) {
|
|||||||
auto x = lua::tointeger(L, 1);
|
auto x = lua::tointeger(L, 1);
|
||||||
auto y = lua::tointeger(L, 2);
|
auto y = lua::tointeger(L, 2);
|
||||||
auto z = lua::tointeger(L, 3);
|
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);
|
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 x = lua::tointeger(L, 1);
|
||||||
auto y = lua::tointeger(L, 2);
|
auto y = lua::tointeger(L, 2);
|
||||||
auto z = lua::tointeger(L, 3);
|
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);
|
auto& def = indices->blocks.require(vox.id);
|
||||||
return lua::pushivec_stack(
|
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 y = lua::tointeger(L, 2);
|
||||||
auto z = lua::tointeger(L, 3);
|
auto z = lua::tointeger(L, 3);
|
||||||
auto states = lua::tointeger(L, 4);
|
auto states = lua::tointeger(L, 4);
|
||||||
|
if (y < 0 || y >= CHUNK_H) {
|
||||||
auto chunk = level->chunks->getChunkByVoxel(x, y, z);
|
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) {
|
if (chunk == nullptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
auto vox = level->chunks->get(x, y, z);
|
int lx = x - cx * CHUNK_W;
|
||||||
vox->state = int2blockstate(states);
|
int lz = z - cz * CHUNK_D;
|
||||||
|
chunk->voxels[vox_index(lx, y, lz)].state = int2blockstate(states);
|
||||||
chunk->setModifiedAndUnsaved();
|
chunk->setModifiedAndUnsaved();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -223,7 +231,9 @@ static int l_get_user_bits(lua::State* L) {
|
|||||||
}
|
}
|
||||||
const auto& def = content->getIndices()->blocks.require(vox->id);
|
const auto& def = content->getIndices()->blocks.require(vox->id);
|
||||||
if (def.rt.extended) {
|
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);
|
vox = level->chunks->get(origin.x, origin.y, origin.z);
|
||||||
if (vox == nullptr) {
|
if (vox == nullptr) {
|
||||||
return lua::pushinteger(L, 0);
|
return lua::pushinteger(L, 0);
|
||||||
|
|||||||
@ -160,23 +160,7 @@ Chunk* Chunks::getChunk(int x, int z) const {
|
|||||||
glm::ivec3 Chunks::seekOrigin(
|
glm::ivec3 Chunks::seekOrigin(
|
||||||
const glm::ivec3& srcpos, const Block& def, blockstate state
|
const glm::ivec3& srcpos, const Block& def, blockstate state
|
||||||
) const {
|
) const {
|
||||||
auto pos = srcpos;
|
return blocks_agent::seek_origin(*this, srcpos, def, state);
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::eraseSegments(
|
void Chunks::eraseSegments(
|
||||||
|
|||||||
@ -11,6 +11,7 @@
|
|||||||
#include "maths/voxmaths.hpp"
|
#include "maths/voxmaths.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
/// Using templates to minimize OOP overhead
|
/// 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
|
} // blocks_agent
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user