In collaborative builds, a large construction plan is partitioned into quadrants (North-West, North-East, South-West, South-East). Each quadrant is managed by a BuildSection object.
To ensure thread safety and prevent multiple agents from placing the same block, BuildSection uses Atomic Block Claiming. Each section maintains an AtomicInteger to track the next block index, allowing agents to claim blocks in a lock-free manner.
Agents are assigned to sections using a two-pass logic:
- Primary Assignment: Find an unassigned section that is not yet complete.
- Load Balancing: If all sections have an agent, an additional agent can join an incomplete section to help finish it.
Blocks within a section are sorted from bottom-to-top (Y-axis) to ensure structural integrity during construction.
public static class BuildSection {
public final int yLevel; // Section ID
public final String sectionName;
private final List<BlockPlacement> blocks;
private final AtomicInteger nextBlockIndex; // Thread-safe counter
public BlockPlacement getNextBlock() {
int index = nextBlockIndex.getAndIncrement(); // Atomic increment
if (index < blocks.size()) {
return blocks.get(index);
}
return null; // Section complete
}
public int getBlocksPlaced() {
return Math.min(nextBlockIndex.get(), blocks.size());
}
public boolean isComplete() {
return nextBlockIndex.get() >= blocks.size();
}
}