minor refactor
This commit is contained in:
parent
2204cb795e
commit
51531e2621
@ -171,10 +171,10 @@ void Chunks::eraseSegments(
|
|||||||
blocks_agent::erase_segments(*this, def, state, x, y, z);
|
blocks_agent::erase_segments(*this, def, state, x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Chunks::repairSegments(
|
void Chunks::restoreSegments(
|
||||||
const Block& def, blockstate state, int x, int y, int z
|
const Block& def, blockstate state, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
blocks_agent::repair_segments(*this, def, state, x, y, z);
|
blocks_agent::restore_segments(*this, def, state, x, y, z);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Chunks::checkReplaceability(
|
bool Chunks::checkReplaceability(
|
||||||
|
|||||||
@ -28,7 +28,7 @@ class Chunks {
|
|||||||
const ContentIndices& indices;
|
const ContentIndices& indices;
|
||||||
|
|
||||||
void eraseSegments(const Block& def, blockstate state, int x, int y, int z);
|
void eraseSegments(const Block& def, blockstate state, int x, int y, int z);
|
||||||
void repairSegments(
|
void restoreSegments(
|
||||||
const Block& def, blockstate state, int x, int y, int z
|
const Block& def, blockstate state, int x, int y, int z
|
||||||
);
|
);
|
||||||
void setRotationExtended(
|
void setRotationExtended(
|
||||||
|
|||||||
@ -7,62 +7,10 @@
|
|||||||
using namespace blocks_agent;
|
using namespace blocks_agent;
|
||||||
|
|
||||||
template <class Storage>
|
template <class Storage>
|
||||||
static inline bool set_block(
|
static void mark_neighboirs_modified(
|
||||||
Storage& chunks,
|
Storage& chunks, int32_t cx, int32_t cz, int32_t lx, int32_t lz
|
||||||
int32_t x,
|
|
||||||
int32_t y,
|
|
||||||
int32_t z,
|
|
||||||
uint32_t id,
|
|
||||||
blockstate state
|
|
||||||
) {
|
) {
|
||||||
if (y < 0 || y >= CHUNK_H) {
|
Chunk* chunk;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
const auto& indices = chunks.getContentIndices();
|
|
||||||
int cx = floordiv<CHUNK_W>(x);
|
|
||||||
int cz = floordiv<CHUNK_D>(z);
|
|
||||||
Chunk* chunk = get_chunk(chunks, cx, cz);
|
|
||||||
if (chunk == nullptr) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
int lx = x - cx * CHUNK_W;
|
|
||||||
int lz = z - cz * CHUNK_D;
|
|
||||||
size_t index = vox_index(lx, y, lz);
|
|
||||||
|
|
||||||
// block finalization
|
|
||||||
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
|
|
||||||
const auto& prevdef = indices.blocks.require(vox.id);
|
|
||||||
if (prevdef.inventorySize != 0) {
|
|
||||||
chunk->removeBlockInventory(lx, y, lz);
|
|
||||||
}
|
|
||||||
if (prevdef.rt.extended && !vox.state.segment) {
|
|
||||||
erase_segments(chunks, prevdef, vox.state, x, y, z);
|
|
||||||
}
|
|
||||||
if (prevdef.dataStruct) {
|
|
||||||
if (auto found = chunk->blocksMetadata.find(index)) {
|
|
||||||
chunk->blocksMetadata.free(found);
|
|
||||||
chunk->flags.unsaved = true;
|
|
||||||
chunk->flags.blocksData = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// block initialization
|
|
||||||
const auto& newdef = indices.blocks.require(id);
|
|
||||||
vox.id = id;
|
|
||||||
vox.state = state;
|
|
||||||
chunk->setModifiedAndUnsaved();
|
|
||||||
if (!state.segment && newdef.rt.extended) {
|
|
||||||
repair_segments(chunks, newdef, state, x, y, z);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (y < chunk->bottom)
|
|
||||||
chunk->bottom = y;
|
|
||||||
else if (y + 1 > chunk->top)
|
|
||||||
chunk->top = y + 1;
|
|
||||||
else if (id == 0)
|
|
||||||
chunk->flags.dirtyHeights = true;
|
|
||||||
|
|
||||||
|
|
||||||
if (lx == 0 && (chunk = get_chunk(chunks, cx - 1, cz))) {
|
if (lx == 0 && (chunk = get_chunk(chunks, cx - 1, cz))) {
|
||||||
chunk->flags.modified = true;
|
chunk->flags.modified = true;
|
||||||
}
|
}
|
||||||
@ -75,6 +23,92 @@ static inline bool set_block(
|
|||||||
if (lz == CHUNK_D - 1 && (chunk = get_chunk(chunks, cx, cz + 1))) {
|
if (lz == CHUNK_D - 1 && (chunk = get_chunk(chunks, cx, cz + 1))) {
|
||||||
chunk->flags.modified = true;
|
chunk->flags.modified = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void refresh_chunk_heights(Chunk& chunk, bool isAir, int y) {
|
||||||
|
if (y < chunk.bottom)
|
||||||
|
chunk.bottom = y;
|
||||||
|
else if (y + 1 > chunk.top)
|
||||||
|
chunk.top = y + 1;
|
||||||
|
else if (isAir)
|
||||||
|
chunk.flags.dirtyHeights = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Storage>
|
||||||
|
static void finalize_block(
|
||||||
|
Storage& chunks,
|
||||||
|
Chunk& chunk,
|
||||||
|
voxel& vox,
|
||||||
|
int32_t x, int32_t y, int32_t z,
|
||||||
|
int32_t lx, int32_t lz
|
||||||
|
) {
|
||||||
|
size_t index = vox_index(lx, y, lz);
|
||||||
|
const auto& indices = chunks.getContentIndices();
|
||||||
|
const auto& def = indices.blocks.require(vox.id);
|
||||||
|
if (def.inventorySize != 0) {
|
||||||
|
chunk.removeBlockInventory(lx, y, lz);
|
||||||
|
}
|
||||||
|
if (def.rt.extended && !vox.state.segment) {
|
||||||
|
erase_segments(chunks, def, vox.state, x, y, z);
|
||||||
|
}
|
||||||
|
if (def.dataStruct) {
|
||||||
|
if (auto found = chunk.blocksMetadata.find(index)) {
|
||||||
|
chunk.blocksMetadata.free(found);
|
||||||
|
chunk.flags.unsaved = true;
|
||||||
|
chunk.flags.blocksData = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Storage>
|
||||||
|
static void initialize_block(
|
||||||
|
Storage& chunks,
|
||||||
|
Chunk& chunk,
|
||||||
|
voxel& vox,
|
||||||
|
blockid_t id,
|
||||||
|
blockstate state,
|
||||||
|
int32_t x, int32_t y, int32_t z,
|
||||||
|
int32_t lx, int32_t lz,
|
||||||
|
int32_t cx, int32_t cz
|
||||||
|
) {
|
||||||
|
const auto& indices = chunks.getContentIndices();
|
||||||
|
const auto& def = indices.blocks.require(id);
|
||||||
|
vox.id = id;
|
||||||
|
vox.state = state;
|
||||||
|
chunk.setModifiedAndUnsaved();
|
||||||
|
if (!state.segment && def.rt.extended) {
|
||||||
|
restore_segments(chunks, def, state, x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
refresh_chunk_heights(chunk, id == BLOCK_AIR, y);
|
||||||
|
mark_neighboirs_modified(chunks, cx, cz, lx, lz);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Storage>
|
||||||
|
static inline bool set_block(
|
||||||
|
Storage& chunks,
|
||||||
|
int32_t x,
|
||||||
|
int32_t y,
|
||||||
|
int32_t z,
|
||||||
|
blockid_t id,
|
||||||
|
blockstate state
|
||||||
|
) {
|
||||||
|
if (y < 0 || y >= CHUNK_H) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int cx = floordiv<CHUNK_W>(x);
|
||||||
|
int cz = floordiv<CHUNK_D>(z);
|
||||||
|
Chunk* chunk = get_chunk(chunks, cx, cz);
|
||||||
|
if (chunk == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int lx = x - cx * CHUNK_W;
|
||||||
|
int lz = z - cz * CHUNK_D;
|
||||||
|
|
||||||
|
voxel& vox = chunk->voxels[(y * CHUNK_D + lz) * CHUNK_W + lx];
|
||||||
|
|
||||||
|
finalize_block(chunks, *chunk, vox, x, y, z, lx, lz);
|
||||||
|
initialize_block(chunks, *chunk, vox, id, state, x, y, z, lx, lz, cx, cz);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -191,7 +191,7 @@ static constexpr inline uint8_t segment_to_int(int sx, int sy, int sz) {
|
|||||||
/// @param y origin position Y
|
/// @param y origin position Y
|
||||||
/// @param z origin position Z
|
/// @param z origin position Z
|
||||||
template <class Storage>
|
template <class Storage>
|
||||||
inline void repair_segments(
|
inline void restore_segments(
|
||||||
Storage& chunks, const Block& def, blockstate state, int x, int y, int z
|
Storage& chunks, const Block& def, blockstate state, int x, int y, int z
|
||||||
) {
|
) {
|
||||||
const auto& rotation = def.rotations.variants[state.rotation];
|
const auto& rotation = def.rotations.variants[state.rotation];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user