Added recursive fill.

This commit is contained in:
sk89q
2010-11-16 20:03:54 -08:00
parent 06d87a84cc
commit 780ccd37ec
2 changed files with 81 additions and 44 deletions

View File

@ -329,16 +329,15 @@ public class EditSession {
/**
* Fills an area recursively in the X/Z directions.
*
* @param x
* @param z
* @param origin
* @param block
* @param radius
* @param depth
* @param recursive
* @return number of blocks affected
*/
public int fillXZ(int x, int z, Vector origin, BaseBlock block,
int radius, int depth)
public int fillXZ(Vector origin, BaseBlock block,
int radius, int depth, boolean recursive)
throws MaxChangedBlocksException {
int affected = 0;
@ -349,37 +348,55 @@ public class EditSession {
HashSet<BlockVector> visited = new HashSet<BlockVector>();
Stack<BlockVector> queue = new Stack<BlockVector>();
queue.push(new BlockVector(x, 0, z));
queue.push(new BlockVector(originX, originY, originZ));
while (!queue.empty()) {
BlockVector pt = queue.pop();
int cx = pt.getBlockX();
int cy = pt.getBlockY();
int cz = pt.getBlockZ();
if (visited.contains(pt)) {
if (cy < 0 || cy > originY || visited.contains(pt)) {
continue;
}
visited.add(pt);
double dist = Math.sqrt(Math.pow(originX - cx, 2)
+ Math.pow(originZ - cz, 2));
int minY = originY - depth + 1;
if (recursive) {
if (origin.distance(pt) > radius) {
continue;
}
if (dist > radius) {
continue;
}
if (getBlock(pt).isAir()) {
if (setBlock(pt, block)) {
affected++;
}
} else {
continue;
}
if (getBlock(new Vector(cx, originY, cz)).isAir()) {
affected += fillY(cx, originY, cz, block, minY);
queue.push(new BlockVector(cx, cy - 1, cz));
queue.push(new BlockVector(cx, cy + 1, cz));
} else {
continue;
double dist = Math.sqrt(Math.pow(originX - cx, 2)
+ Math.pow(originZ - cz, 2));
int minY = originY - depth + 1;
if (dist > radius) {
continue;
}
if (getBlock(pt).isAir()) {
affected += fillY(cx, originY, cz, block, minY);
} else {
continue;
}
}
queue.push(new BlockVector(cx + 1, 0, cz));
queue.push(new BlockVector(cx - 1, 0, cz));
queue.push(new BlockVector(cx, 0, cz + 1));
queue.push(new BlockVector(cx, 0, cz - 1));
queue.push(new BlockVector(cx + 1, cy, cz));
queue.push(new BlockVector(cx - 1, cy, cz));
queue.push(new BlockVector(cx, cy, cz + 1));
queue.push(new BlockVector(cx, cy, cz - 1));
}
return affected;
@ -417,16 +434,15 @@ public class EditSession {
/**
* Fills an area recursively in the X/Z directions.
*
* @param x
* @param z
* @param origin
* @param pattern
* @param radius
* @param depth
* @param recursive
* @return number of blocks affected
*/
public int fillXZ(int x, int z, Vector origin, Pattern pattern,
int radius, int depth)
public int fillXZ(Vector origin, Pattern pattern,
int radius, int depth, boolean recursive)
throws MaxChangedBlocksException {
int affected = 0;
@ -437,37 +453,55 @@ public class EditSession {
HashSet<BlockVector> visited = new HashSet<BlockVector>();
Stack<BlockVector> queue = new Stack<BlockVector>();
queue.push(new BlockVector(x, 0, z));
queue.push(new BlockVector(originX, originY, originZ));
while (!queue.empty()) {
BlockVector pt = queue.pop();
int cx = pt.getBlockX();
int cy = pt.getBlockY();
int cz = pt.getBlockZ();
if (visited.contains(pt)) {
if (cy < 0 || cy > originY || visited.contains(pt)) {
continue;
}
visited.add(pt);
double dist = Math.sqrt(Math.pow(originX - cx, 2)
+ Math.pow(originZ - cz, 2));
int minY = originY - depth + 1;
if (recursive) {
if (origin.distance(pt) > radius) {
continue;
}
if (dist > radius) {
continue;
}
if (getBlock(pt).isAir()) {
if (setBlock(pt, pattern.next(pt))) {
affected++;
}
} else {
continue;
}
if (getBlock(new Vector(cx, originY, cz)).isAir()) {
affected += fillY(cx, originY, cz, pattern, minY);
queue.push(new BlockVector(cx, cy - 1, cz));
queue.push(new BlockVector(cx, cy + 1, cz));
} else {
continue;
double dist = Math.sqrt(Math.pow(originX - cx, 2)
+ Math.pow(originZ - cz, 2));
int minY = originY - depth + 1;
if (dist > radius) {
continue;
}
if (getBlock(pt).isAir()) {
affected += fillY(cx, originY, cz, pattern, minY);
} else {
continue;
}
}
queue.push(new BlockVector(cx + 1, 0, cz));
queue.push(new BlockVector(cx - 1, 0, cz));
queue.push(new BlockVector(cx, 0, cz + 1));
queue.push(new BlockVector(cx, 0, cz - 1));
queue.push(new BlockVector(cx + 1, cy, cz));
queue.push(new BlockVector(cx - 1, cy, cz));
queue.push(new BlockVector(cx, cy, cz + 1));
queue.push(new BlockVector(cx, cy, cz - 1));
}
return affected;