Basically rewrote the tree remover tool to use breadth-first search to get rid of deep recursions.

Also changed a message to be more precise.
Dieser Commit ist enthalten in:
TomyLobo 2011-12-02 23:53:06 +01:00
Ursprung 771d068826
Commit 4753574bae

Datei anzeigen

@ -20,7 +20,7 @@
package com.sk89q.worldedit.tools; package com.sk89q.worldedit.tools;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.LinkedList;
import java.util.Set; import java.util.Set;
import com.sk89q.worldedit.*; import com.sk89q.worldedit.*;
import com.sk89q.worldedit.blocks.BaseBlock; import com.sk89q.worldedit.blocks.BaseBlock;
@ -33,11 +33,11 @@ import com.sk89q.worldedit.blocks.BlockID;
* @author Moo0 * @author Moo0
*/ */
public class FloatingTreeRemover implements BlockTool { public class FloatingTreeRemover implements BlockTool {
private static final BaseBlock air = new BaseBlock(BlockID.AIR); private static final BaseBlock AIR = new BaseBlock(BlockID.AIR);
private int range; private int rangeSq;
public FloatingTreeRemover() { public FloatingTreeRemover() {
this.range = 100; rangeSq = 100*100;
} }
public boolean canUse(LocalPlayer player) { public boolean canUse(LocalPlayer player) {
@ -46,36 +46,38 @@ public class FloatingTreeRemover implements BlockTool {
public boolean actPrimary(ServerInterface server, LocalConfiguration config, public boolean actPrimary(ServerInterface server, LocalConfiguration config,
LocalPlayer player, LocalSession session, WorldVector clicked) { LocalPlayer player, LocalSession session, WorldVector clicked) {
LocalWorld world = clicked.getWorld();
int initialType = world.getBlockType(clicked); final LocalWorld world = clicked.getWorld();
int block;
EditSession editSession = session.createEditSession(player); switch (world.getBlockType(clicked)) {
case BlockID.LOG:
case BlockID.LEAVES:
case BlockID.BROWN_MUSHROOM_CAP:
case BlockID.RED_MUSHROOM_CAP:
break;
if (initialType != BlockID.LOG default:
&& initialType != BlockID.LEAVES player.printError("That's not a tree.");
&& initialType != BlockID.BROWN_MUSHROOM_CAP
&& initialType != BlockID.RED_MUSHROOM_CAP) {
player.printError("That's not a floating tree.");
return true; return true;
} }
HashSet<BlockVector> blockSet = new HashSet<BlockVector>(); final EditSession editSession = session.createEditSession(player);
try { try {
if (!recurse(server, editSession, world, clicked.toBlockVector(), final Set<Vector> blockSet = bfs(world, clicked);
clicked, range, blockSet, 0)) { if (blockSet == null) {
player.printError("That's not a floating tree."); player.printError("That's not a floating tree.");
return true; return true;
} }
for (Iterator<BlockVector> iterator = blockSet.iterator(); iterator.hasNext();) {
BlockVector blockVector = iterator.next(); for (Vector blockVector : blockSet) {
block = editSession.getBlock(blockVector).getType(); final int typeId = editSession.getBlock(blockVector).getType();
if (block == BlockID.LOG switch (typeId) {
|| block == BlockID.LEAVES case BlockID.LOG:
|| block == BlockID.BROWN_MUSHROOM_CAP case BlockID.LEAVES:
|| block == BlockID.RED_MUSHROOM_CAP) { case BlockID.BROWN_MUSHROOM_CAP:
editSession.setBlock(blockVector, air); case BlockID.RED_MUSHROOM_CAP:
editSession.setBlock(blockVector, AIR);
} }
} }
} catch (MaxChangedBlocksException e) { } catch (MaxChangedBlocksException e) {
@ -87,60 +89,67 @@ public class FloatingTreeRemover implements BlockTool {
return true; return true;
} }
Vector[] recurseDirections = {
PlayerDirection.NORTH.vector(),
PlayerDirection.EAST.vector(),
PlayerDirection.SOUTH.vector(),
PlayerDirection.WEST.vector(),
PlayerDirection.UP.vector(),
PlayerDirection.DOWN.vector(),
};
/** /**
* Helper method. * Helper method.
* *
* @param server * @param world the world that contains the tree
* @param editSession * @param origin any point contained in the floating tree
* @param world * @return a set containing all blocks in the tree/shroom or null if this is not a floating tree/shroom.
* @param pos
* @param origin
* @param size
* @param initialType
* @param visited
* @param lastBlock
*/ */
private boolean recurse(ServerInterface server, EditSession editSession, private Set<Vector> bfs(LocalWorld world, Vector origin) throws MaxChangedBlocksException {
LocalWorld world, BlockVector pos, final Set<Vector> visited = new HashSet<Vector>();
Vector origin, int size, final LinkedList<Vector> queue = new LinkedList<Vector>();
Set<BlockVector> visited, int lastBlock)
throws MaxChangedBlocksException {
if (origin.distance(pos) > size || visited.contains(pos)) { queue.addLast(origin);
return true; visited.add(origin);
while (!queue.isEmpty()) {
final Vector current = queue.removeFirst();
for (Vector recurseDirection : recurseDirections) {
final Vector next = current.add(recurseDirection);
if (origin.distanceSq(next) > rangeSq) {
// Maximum range exceeded => stop walking
continue;
} }
visited.add(pos); if (visited.add(next)) {
switch (world.getBlockType(next)) {
case BlockID.AIR:
case BlockID.SNOW:
// we hit air or snow => stop walking this route
continue;
int block = editSession.getBlock(pos).getType(); case BlockID.LOG:
if (block == BlockID.AIR || block == BlockID.SNOW) { case BlockID.LEAVES:
return true; case BlockID.BROWN_MUSHROOM_CAP:
} case BlockID.RED_MUSHROOM_CAP:
if (block != BlockID.LOG // queue next point
&& block != BlockID.LEAVES queue.addLast(next);
&& block != BlockID.BROWN_MUSHROOM_CAP break;
&& block != BlockID.RED_MUSHROOM_CAP) {
if (lastBlock == BlockID.LEAVES) { default:
return true; // we hit something solid - evaluate where we came from
if (world.getBlockType(current) == BlockID.LEAVES) {
// leaves touching a wall/the ground => stop walking this route
continue;
} else { } else {
return false; // log/shroom touching a wall/the ground => this is not a floating tree, bail out
} return null;
}
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
for (int k = -1; k <= 1; ++k) {
if (Math.abs(i) + Math.abs(j) + Math.abs(k) == 1) {
if (!recurse(server, editSession, world, pos.add(i, j, k).toBlockVector(),
origin, size, visited, block)) {
return false;
}
}
}
}
}
return true;
} }
} // switch
} // if
} // for
} // while
return visited;
} // bfs
} }