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.HashMap;
import java.util.HashSet;
import java.util.Stack;
import com.sk89q.worldedit.*;
/**
@ -634,62 +635,53 @@ public class EditSession {
int affected = 0;
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 z = pos.getBlockZ() - 1; z <= pos.getBlockZ() + 1; z++) {
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();
/**
* 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;
int type = getBlock(cur);
if (type != 8 && type != 9 && type != 10 && type != 11) {
return 0;
}
// Check block type
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)) {
return 0;
}
visited.add(blockPos);
visited.add(cur);
if (pos.distance(origin) > radius) {
return 0;
}
// Check radius
if (pos.distance(cur) > radius) {
continue;
}
for (int x = pos.getBlockX() - 1; x <= pos.getBlockX() + 1; x++) {
for (int z = pos.getBlockZ() - 1; z <= pos.getBlockZ() + 1; z++) {
for (int y = pos.getBlockY() - 1; y <= pos.getBlockY() + 1; y++) {
Point newPos = new Point(x, y, z);
for (int x = cur.getBlockX() - 1; x <= cur.getBlockX() + 1; x++) {
for (int z = cur.getBlockZ() - 1; z <= cur.getBlockZ() + 1; z++) {
for (int y = cur.getBlockY() - 1; y <= cur.getBlockY() + 1; y++) {
BlockPoint newPos = new BlockPoint(x, y, z);
if (!pos.equals(newPos)) {
affected += drainPool(newPos, origin, radius, visited);
if (!cur.equals(newPos)) {
queue.push(newPos);
}
}
}
}
}
if (setBlock(pos, 0)) {
affected++;
if (setBlock(cur, 0)) {
affected++;
}
}
return affected;

View File

@ -34,6 +34,33 @@ public class BlockPoint extends Point {
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.
*