return new IntVector3Array(xArray, yArray, zArray, count);
}
private int getBoundary(Chunk[] chunks, int count, int[] xArray, int[] yArray, int[] zArray, int startChunk, int endChunk) {
Chunk first = chunks[startChunk];
// Note: X and Z are sorted low to high and Y is sorted high to low
int baseX = first.getX();
int baseY = first.getY();
int baseZ = first.getZ();
int topX = first.getX();
int topY = first.getY();
int topZ = first.getZ();
for (int i = startChunk + 1; i < endChunk; i++) {
Chunk c = chunks[i];
if (c.getX() < baseX) {
baseX = c.getX();
}
if (c.getY() < baseY) {
baseY = c.getY();
}
if (c.getZ() < baseZ) {
baseZ = c.getZ();
}
if (c.getX() > topX) {
topX = c.getX();
}
if (c.getY() > topY) {
topY = c.getY();
}
if (c.getZ() > topZ) {
topZ = c.getZ();
}
}
topX++;
topY++;
topZ++;
int sizeX = topX - baseX;
int sizeY = topY - baseY;
int sizeZ = topZ - baseZ;
int volume = sizeX * sizeY * sizeZ;
boolean fullCuboid = volume == endChunk - startChunk;
Chunk[][][] cuboid = new Chunk[sizeX][sizeY][sizeZ];
for (int i = startChunk; i < endChunk; i++) {
Chunk c = chunks[i];
int cx = c.getX() - baseX;
int cy = c.getY() - baseY;
int cz = c.getZ() - baseZ;
if (cuboid[cx][cy][cz] == null) {
cuboid[cx][cy][cz] = c;
} else {
throw new IllegalStateException("Chunks appears twice in list, " + cx + ", " + cy + ", " + cz);
}
}
int baseBlockX = baseX << Chunk.BLOCKS.BITS;
int baseBlockY = baseY << Chunk.BLOCKS.BITS;
int baseBlockZ = baseZ << Chunk.BLOCKS.BITS;
int topBlockX = topX << Chunk.BLOCKS.BITS;
int topBlockY = topY << Chunk.BLOCKS.BITS;
int topBlockZ = topZ << Chunk.BLOCKS.BITS;
if (fullCuboid) {
for (int x = baseBlockX; x < topBlockX; x++) {
for (int y = baseBlockY; y < topBlockY; y++) {
xArray[count] = x;
yArray[count] = y;
zArray[count] = baseBlockZ;
count++;
xArray[count] = x;
yArray[count] = y;
zArray[count] = topBlockZ - 1;
count++;
}
}
for (int x = baseBlockX; x < topBlockX; x++) {
for (int z = baseBlockZ; z < topBlockZ; z++) {
xArray[count] = x;
yArray[count] = baseBlockY;
zArray[count] = z;
count++;
xArray[count] = x;
yArray[count] = topBlockY - 1;
zArray[count] = z;
count++;
}
}
for (int z = baseBlockZ; z < topBlockZ; z++) {
for (int y = baseBlockY; y < topBlockY; y++) {
xArray[count] = baseBlockX;
yArray[count] = y;
zArray[count] = z;
count++;
xArray[count] = topBlockX - 1;
yArray[count] = y;
zArray[count] = z;
count++;
}
}
} else {
int size = Chunk.BLOCKS.SIZE;
for (int x = 0; x < sizeX; x++) {
int wX = (baseX + x) << Chunk.BLOCKS.BITS;
for (int z = 0; z < sizeZ; z++) {
int wZ = (baseZ + z) << Chunk.BLOCKS.BITS;
for (int y = sizeY - 1; y >= 0; y--) {
Chunk c = cuboid[x][y][z];
if (c == null) {
continue;
}