From 45c2868d4cb8f36a610b8deb37c5519d267827cb Mon Sep 17 00:00:00 2001 From: sk89q Date: Wed, 26 Mar 2014 23:09:55 -0700 Subject: [PATCH] Added various visitors (BFS, Downward, Recursive, Region). --- .../operation/BreadthFirstSearch.java | 172 ++++++++++++++++++ .../worldedit/operation/DownwardVisitor.java | 66 +++++++ .../worldedit/operation/RecursiveVisitor.java | 49 +++++ .../worldedit/operation/RegionVisitor.java | 65 +++++++ 4 files changed, 352 insertions(+) create mode 100644 src/main/java/com/sk89q/worldedit/operation/BreadthFirstSearch.java create mode 100644 src/main/java/com/sk89q/worldedit/operation/DownwardVisitor.java create mode 100644 src/main/java/com/sk89q/worldedit/operation/RecursiveVisitor.java create mode 100644 src/main/java/com/sk89q/worldedit/operation/RegionVisitor.java diff --git a/src/main/java/com/sk89q/worldedit/operation/BreadthFirstSearch.java b/src/main/java/com/sk89q/worldedit/operation/BreadthFirstSearch.java new file mode 100644 index 000000000..47d9feaa5 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/operation/BreadthFirstSearch.java @@ -0,0 +1,172 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.operation; + +import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; + +import java.util.*; + +/** + * Performs a breadth-first search starting from points added with + * {@link #visit(com.sk89q.worldedit.Vector)}. The search continues + * to a certain adjacent point provided that the method + * {@link #isVisitable(com.sk89q.worldedit.Vector, com.sk89q.worldedit.Vector)} + * returns true for that point. + *

+ * As an abstract implementation, this class can be used to implement + * functionality that starts at certain points and extends outward from + * those points. + */ +public abstract class BreadthFirstSearch implements Operation { + + private final RegionFunction function; + private final Queue queue = new ArrayDeque(); + private final Set visited = new HashSet(); + private final List directions = new ArrayList(); + private int affected = 0; + + /** + * Create a new instance. + * + * @param function the function to apply to visited blocks + */ + public BreadthFirstSearch(RegionFunction function) { + this.function = function; + addAxes(); + } + + /** + * Get the list of directions will be visited. + *

+ * Directions are {@link com.sk89q.worldedit.Vector}s that determine + * what adjacent points area available. Vectors should not be + * unit vectors. An example of a valid direction is {@code new Vector(1, 0, 1)}. + *

+ * The list of directions can be cleared. + * + * @return the list of directions + */ + protected List getDirections() { + return directions; + } + + /** + * Add the directions along the axes as directions to visit. + */ + protected void addAxes() { + directions.add(new Vector(0, -1, 0)); + directions.add(new Vector(0, 1, 0)); + directions.add(new Vector(-1, 0, 0)); + directions.add(new Vector(1, 0, 0)); + directions.add(new Vector(0, 0, -1)); + directions.add(new Vector(0, 0, 1)); + } + + /** + * Add the diagonal directions as directions to visit. + */ + protected void addDiagonal() { + directions.add(new Vector(1, 0, 1)); + directions.add(new Vector(-1, 0, -1)); + directions.add(new Vector(1, 0, -1)); + directions.add(new Vector(-1, 0, 1)); + } + + /** + * Add the given location to the list of locations to visit, provided + * that it has not been visited. The position passed to this method + * will still be visited even if it fails + * {@link #isVisitable(com.sk89q.worldedit.Vector, com.sk89q.worldedit.Vector)}. + *

+ * This method should be used before the search begins, because if + * the position does fail the test, and the search has already + * visited it (because it is connected to another root point), + * the search will mark the position as "visited" and a call to this + * method will do nothing. + * + * @param position the position + */ + public void visit(Vector position) { + BlockVector blockVector = position.toBlockVector(); + if (!visited.contains(blockVector)) { + queue.add(blockVector); + visited.add(blockVector); + } + } + + /** + * Try to visit the given 'to' location. + * + * @param from the origin block + * @param to the block under question + */ + private void visit(Vector from, Vector to) { + BlockVector blockVector = to.toBlockVector(); + if (!visited.contains(blockVector)) { + visited.add(blockVector); + if (isVisitable(from, to)) { + queue.add(blockVector); + } + } + } + + /** + * Return whether the given 'to' block should be visited, starting from the + * 'from' block. + * + * @param from the origin block + * @param to the block under question + * @return true if the 'to' block should be visited + */ + protected abstract boolean isVisitable(Vector from, Vector to); + + /** + * Get the number of affected objects. + * + * @return the number of affected + */ + public int getAffected() { + return affected; + } + + @Override + public Operation resume() throws WorldEditException { + Vector position; + + while ((position = queue.poll()) != null) { + if (function.apply(position)) { + affected++; + } + + for (Vector dir : directions) { + visit(position, position.add(dir)); + } + } + + return null; + } + + @Override + public void cancel() { + } + +} \ No newline at end of file diff --git a/src/main/java/com/sk89q/worldedit/operation/DownwardVisitor.java b/src/main/java/com/sk89q/worldedit/operation/DownwardVisitor.java new file mode 100644 index 000000000..393ca35c7 --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/operation/DownwardVisitor.java @@ -0,0 +1,66 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.operation; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.masks.Mask; + +import java.util.List; + +/** + * Visits adjacent points on the same X-Z plane as long as the points + * pass the given mask, and then executes the provided region + * function on the entire column. + *

+ * This is used by //fill. + */ +public class DownwardVisitor extends RecursiveVisitor { + + private int baseY; + + /** + * Create a new visitor. + * + * @param editSession the edit session + * @param mask the mask + * @param function the function + * @param baseY the base Y + */ + public DownwardVisitor(EditSession editSession, Mask mask, RegionFunction function, int baseY) { + super(editSession, mask, function); + + this.baseY = baseY; + + List directions = getDirections(); + directions.clear(); + directions.add(new Vector(1, 0, 0)); + directions.add(new Vector(-1, 0, 0)); + directions.add(new Vector(0, 0, 1)); + directions.add(new Vector(0, 0, -1)); + directions.add(new Vector(0, -1, 0)); + } + + @Override + protected boolean isVisitable(Vector from, Vector to) { + int fromY = from.getBlockY(); + return (fromY == baseY || to.subtract(from).getBlockY() < 0) && super.isVisitable(from, to); + } +} diff --git a/src/main/java/com/sk89q/worldedit/operation/RecursiveVisitor.java b/src/main/java/com/sk89q/worldedit/operation/RecursiveVisitor.java new file mode 100644 index 000000000..7bc6eb82c --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/operation/RecursiveVisitor.java @@ -0,0 +1,49 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.operation; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.masks.Mask; + +/** + * An implementation of an {@link BreadthFirstSearch} that uses a mask to + * determine where a block should be visited. + */ +public class RecursiveVisitor extends BreadthFirstSearch { + + private final EditSession editSession; + private final Mask mask; + + public RecursiveVisitor(EditSession editSession, Mask mask, RegionFunction function) { + super(function); + this.editSession = editSession; + this.mask = mask; + } + + @Override + protected boolean isVisitable(Vector from, Vector to) { + int y = to.getBlockY(); + if (y < 0 || y > editSession.getWorld().getMaxY()) { + return false; + } + return mask.matches(editSession, to); + } +} diff --git a/src/main/java/com/sk89q/worldedit/operation/RegionVisitor.java b/src/main/java/com/sk89q/worldedit/operation/RegionVisitor.java new file mode 100644 index 000000000..51772d57b --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/operation/RegionVisitor.java @@ -0,0 +1,65 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.operation; + +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.WorldEditException; +import com.sk89q.worldedit.regions.Region; + +/** + * Utility class to apply region functions to {@link com.sk89q.worldedit.regions.Region}. + */ +public class RegionVisitor implements Operation { + + private final Region region; + private final RegionFunction function; + private int affected = 0; + + public RegionVisitor(Region region, RegionFunction function) { + this.region = region; + this.function = function; + } + + /** + * Get the number of affected objects. + * + * @return the number of affected + */ + public int getAffected() { + return affected; + } + + @Override + public Operation resume() throws WorldEditException { + for (Vector pt : region) { + if (function.apply(pt)) { + affected++; + } + } + + return null; + } + + @Override + public void cancel() { + } + +} +