Converted /editdrain to use own stack.

This commit is contained in:
sk89q 2010-10-11 11:30:11 -07:00
parent 6882aa416d
commit 2f2ff86bac
2 changed files with 58 additions and 39 deletions

View File

@ -20,6 +20,7 @@
import java.util.Map; import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Stack;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
/** /**
@ -634,62 +635,53 @@ public class EditSession {
int affected = 0; int affected = 0;
HashSet<BlockPoint> visited = new HashSet<BlockPoint>(); HashSet<BlockPoint> visited = new HashSet<BlockPoint>();
Stack<BlockPoint> queue = new Stack<BlockPoint>();
for (int x = pos.getBlockX() - 1; x <= pos.getBlockX() + 1; x++) { for (int x = pos.getBlockX() - 1; x <= pos.getBlockX() + 1; x++) {
for (int z = pos.getBlockZ() - 1; z <= pos.getBlockZ() + 1; z++) { for (int z = pos.getBlockZ() - 1; z <= pos.getBlockZ() + 1; z++) {
for (int y = pos.getBlockY() - 1; y <= pos.getBlockY() + 1; y++) { for (int y = pos.getBlockY() - 1; y <= pos.getBlockY() + 1; y++) {
affected += drainPool(new Point(x, y, z), pos, radius, visited); queue.push(new BlockPoint(x, y, z));
} }
} }
} }
return affected; while (!queue.empty()) {
} BlockPoint cur = queue.pop();
/** int type = getBlock(cur);
* Drains a water or lava block and any lava/water blocks next to it.
*
* @param pos
* @param origin
* @param radius
* @param visited
* @return number of blocks affected
* @throws MaxChangedBlocksException
*/
private int drainPool(Point pos, Point origin, int radius, HashSet<BlockPoint> visited)
throws MaxChangedBlocksException {
int type = getBlock(pos);
int affected = 0;
if (type != 8 && type != 9 && type != 10 && type != 11) { // Check block type
return 0; if (type != 8 && type != 9 && type != 10 && type != 11) {
} continue;
}
BlockPoint blockPos = new BlockPoint(pos); // Don't want to revisit
if (visited.contains(cur)) {
continue;
}
if (visited.contains(blockPos)) { visited.add(cur);
return 0;
}
visited.add(blockPos);
if (pos.distance(origin) > radius) { // Check radius
return 0; if (pos.distance(cur) > radius) {
} continue;
}
for (int x = pos.getBlockX() - 1; x <= pos.getBlockX() + 1; x++) { for (int x = cur.getBlockX() - 1; x <= cur.getBlockX() + 1; x++) {
for (int z = pos.getBlockZ() - 1; z <= pos.getBlockZ() + 1; z++) { for (int z = cur.getBlockZ() - 1; z <= cur.getBlockZ() + 1; z++) {
for (int y = pos.getBlockY() - 1; y <= pos.getBlockY() + 1; y++) { for (int y = cur.getBlockY() - 1; y <= cur.getBlockY() + 1; y++) {
Point newPos = new Point(x, y, z); BlockPoint newPos = new BlockPoint(x, y, z);
if (!pos.equals(newPos)) { if (!cur.equals(newPos)) {
affected += drainPool(newPos, origin, radius, visited); queue.push(newPos);
}
} }
} }
} }
}
if (setBlock(pos, 0)) { if (setBlock(cur, 0)) {
affected++; affected++;
}
} }
return affected; return affected;

View File

@ -34,6 +34,33 @@ public class BlockPoint extends Point {
super(pt); super(pt);
} }
/**
* Construct the Point object.
*
* @param pt
*/
public BlockPoint(int x, int y, int z) {
super(x, y, z);
}
/**
* Construct the Point object.
*
* @param pt
*/
public BlockPoint(float x, float y, float z) {
super(x, y, z);
}
/**
* Construct the Point object.
*
* @param pt
*/
public BlockPoint(double x, double y, double z) {
super(x, y, z);
}
/** /**
* Checks if another object is equivalent. * Checks if another object is equivalent.
* *