add :block placement for single blocks

This commit is contained in:
eliotbyte
2025-11-24 21:23:59 +03:00
committed by MihailRis
parent 2af7b23b2e
commit 9ccaecddd2
4 changed files with 153 additions and 7 deletions
+103 -5
View File
@@ -204,6 +204,46 @@ void WorldGenerator::placeLine(const LinePlacement& line, int priority) {
}
}
void WorldGenerator::placeBlock(const BlockPlacement& block, int priority) {
// Compute world-space AABB of the extended block to distribute to prototypes
const auto& indices = content.getIndices()->blocks;
const auto& def = indices.require(block.block);
const auto& rot = def.rotations.variants[block.rotation & 0b11];
glm::ivec3 minp = block.position;
glm::ivec3 maxp = block.position;
const auto size = def.size;
for (int sy = 0; sy < size.y; sy++) {
for (int sz = 0; sz < size.z; sz++) {
for (int sx = 0; sx < size.x; sx++) {
glm::ivec3 p = block.position;
p += rot.axes[0] * sx;
p += rot.axes[1] * sy;
p += rot.axes[2] * sz;
minp = glm::min(minp, p);
maxp = glm::max(maxp, p);
}
}
}
// inclusive-exclusive for max; expand by 1 to compute chunk coverage
maxp += glm::ivec3(1, 1, 1);
AABB aabb(minp, maxp);
int cxa = floordiv<CHUNK_W>(aabb.a.x);
int cza = floordiv<CHUNK_D>(aabb.a.z);
int cxb = floordiv<CHUNK_W>(aabb.b.x);
int czb = floordiv<CHUNK_D>(aabb.b.z);
for (int cz = cza; cz <= czb; cz++) {
for (int cx = cxa; cx <= cxb; cx++) {
const auto& found = prototypes.find({cx, cz});
if (found != prototypes.end()) {
// position becomes relative to prototype chunk
glm::ivec3 rel = block.position - glm::ivec3(cx * CHUNK_W, 0, cz * CHUNK_D);
found->second->placements.emplace_back(priority, BlockPlacement{block.block, rel, block.rotation});
}
}
}
}
void WorldGenerator::placeStructures(
const std::vector<Placement>& placements,
ChunkPrototype& prototype,
@@ -217,9 +257,11 @@ void WorldGenerator::placeStructures(
continue;
}
placeStructure(*sp, placement.priority, chunkX, chunkZ);
} else if (auto lp = std::get_if<LinePlacement>(&placement.placement)) {
placeLine(*lp, placement.priority);
} else {
const auto& line = std::get<LinePlacement>(placement.placement);
placeLine(line, placement.priority);
const auto& bp = std::get<BlockPlacement>(placement.placement);
placeBlock(bp, placement.priority);
}
}
}
@@ -482,9 +524,10 @@ void WorldGenerator::generatePlacements(
for (const auto& placement : placements) {
if (auto structure = std::get_if<StructurePlacement>(&placement.placement)) {
generateStructure(prototype, *structure, voxels, chunkX, chunkZ);
} else {
const auto& line = std::get<LinePlacement>(placement.placement);
generateLine(prototype, line, voxels, chunkX, chunkZ);
} else if (auto line = std::get_if<LinePlacement>(&placement.placement)) {
generateLine(prototype, *line, voxels, chunkX, chunkZ);
} else if (auto block = std::get_if<BlockPlacement>(&placement.placement)) {
generateBlock(prototype, *block, voxels, chunkX, chunkZ);
}
}
}
@@ -591,6 +634,61 @@ void WorldGenerator::generateLine(
}
}
void WorldGenerator::generateBlock(
const ChunkPrototype& prototype,
const BlockPlacement& placement,
voxel* voxels,
int chunkX, int chunkZ
) {
const auto& indices = content.getIndices()->blocks;
const auto& def = indices.require(placement.block);
int cgx = chunkX * CHUNK_W;
int cgz = chunkZ * CHUNK_D;
glm::ivec3 origin = placement.position; // already relative to chunk in placeBlock
if (origin.x < 0 || origin.x >= CHUNK_W ||
origin.z < 0 || origin.z >= CHUNK_D ||
origin.y < 0 || origin.y >= CHUNK_H) {
return;
}
// write origin voxel
auto& vox = voxels[vox_index(origin.x, origin.y, origin.z)];
vox.id = placement.block;
vox.state = {};
vox.state.rotation = placement.rotation & 0b11;
// expand extended blocks
if (def.rt.extended) {
const auto& rot = def.rotations.variants[vox.state.rotation];
const auto size = def.size;
for (int sy = 0; sy < size.y; sy++) {
for (int sz = 0; sz < size.z; sz++) {
for (int sx = 0; sx < size.x; sx++) {
if ((sx | sy | sz) == 0) continue;
glm::ivec3 pos = origin;
pos += rot.axes[0] * sx;
pos += rot.axes[1] * sy;
pos += rot.axes[2] * sz;
if (pos.x < 0 || pos.x >= CHUNK_W ||
pos.y < 0 || pos.y >= CHUNK_H ||
pos.z < 0 || pos.z >= CHUNK_D) {
continue;
}
struct voxel seg;
seg.id = placement.block;
seg.state = {};
seg.state.rotation = vox.state.rotation;
seg.state.segment = ((sx > 0) | ((sy > 0) << 1) | ((sz > 0) << 2));
voxels[vox_index(pos.x, pos.y, pos.z)] = seg;
}
}
}
}
}
WorldGenDebugInfo WorldGenerator::createDebugInfo() const {
const auto& area = surroundMap.getArea();
const auto& levels = area.getBuffer();