From 9d98ca3db8cd40171e01546fc45dea4956793e64 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Sun, 7 Aug 2011 05:10:59 +0200 Subject: [PATCH] Added library support for non-integer radius spheres and cylinders. --- .../java/com/sk89q/worldedit/EditSession.java | 69 ++++++++++--------- .../com/sk89q/worldedit/tools/BrushTool.java | 10 +-- .../sk89q/worldedit/tools/brushes/Brush.java | 2 +- .../tools/brushes/ClipboardBrush.java | 2 +- .../tools/brushes/CylinderBrush.java | 2 +- .../tools/brushes/HollowCylinderBrush.java | 2 +- .../tools/brushes/HollowSphereBrush.java | 2 +- .../worldedit/tools/brushes/SmoothBrush.java | 4 +- .../worldedit/tools/brushes/SphereBrush.java | 2 +- 9 files changed, 49 insertions(+), 46 deletions(-) diff --git a/src/main/java/com/sk89q/worldedit/EditSession.java b/src/main/java/com/sk89q/worldedit/EditSession.java index 27057d84f..8b08b3fd5 100644 --- a/src/main/java/com/sk89q/worldedit/EditSession.java +++ b/src/main/java/com/sk89q/worldedit/EditSession.java @@ -1626,36 +1626,37 @@ public class EditSession { * @param block * @throws MaxChangedBlocksException */ - private int makeHCylinderPoints(Vector center, int x, int z, int height, + private int makeHCylinderPoints(Vector center, int x, double z, int height, Pattern block) throws MaxChangedBlocksException { + int ceilZ = (int) Math.ceil(z); int affected = 0; if (x == 0) { for (int y = 0; y < height; ++y) { - setBlock(center.add(0, y, z), block); - setBlock(center.add(0, y, -z), block); - setBlock(center.add(z, y, 0), block); - setBlock(center.add(-z, y, 0), block); + setBlock(center.add(0, y, ceilZ), block); + setBlock(center.add(0, y, -ceilZ), block); + setBlock(center.add(ceilZ, y, 0), block); + setBlock(center.add(-ceilZ, y, 0), block); affected += 4; } } else if (x == z) { for (int y = 0; y < height; ++y) { - setBlock(center.add(x, y, z), block); - setBlock(center.add(-x, y, z), block); - setBlock(center.add(x, y, -z), block); - setBlock(center.add(-x, y, -z), block); + setBlock(center.add(x, y, ceilZ), block); + setBlock(center.add(-x, y, ceilZ), block); + setBlock(center.add(x, y, -ceilZ), block); + setBlock(center.add(-x, y, -ceilZ), block); affected += 4; } } else if (x < z) { for (int y = 0; y < height; ++y) { - setBlock(center.add(x, y, z), block); - setBlock(center.add(-x, y, z), block); - setBlock(center.add(x, y, -z), block); - setBlock(center.add(-x, y, -z), block); - setBlock(center.add(z, y, x), block); - setBlock(center.add(-z, y, x), block); - setBlock(center.add(z, y, -x), block); - setBlock(center.add(-z, y, -x), block); + setBlock(center.add(x, y, ceilZ), block); + setBlock(center.add(-x, y, ceilZ), block); + setBlock(center.add(x, y, -ceilZ), block); + setBlock(center.add(-x, y, -ceilZ), block); + setBlock(center.add(ceilZ, y, x), block); + setBlock(center.add(-ceilZ, y, x), block); + setBlock(center.add(ceilZ, y, -x), block); + setBlock(center.add(-ceilZ, y, -x), block); affected += 8; } } @@ -1673,11 +1674,11 @@ public class EditSession { * @return number of blocks set * @throws MaxChangedBlocksException */ - public int makeHollowCylinder(Vector pos, Pattern block, int radius, + public int makeHollowCylinder(Vector pos, Pattern block, double radius, int height) throws MaxChangedBlocksException { int x = 0; - int z = radius; - int d = (5 - radius * 4) / 4; + double z = radius; + double d = (5 - radius * 4) / 4; int affected = 0; if (height == 0) { @@ -1721,13 +1722,14 @@ public class EditSession { * @param block * @throws MaxChangedBlocksException */ - private int makeCylinderPoints(Vector center, int x, int z, int height, + private int makeCylinderPoints(Vector center, int x, double z, int height, Pattern block) throws MaxChangedBlocksException { - int affected = 0; + int ceilZ = (int) Math.ceil(z); + int affected = 0; if (x == z) { for (int y = 0; y < height; ++y) { - for (int z2 = -z; z2 <= z; ++z2) { + for (int z2 = -ceilZ; z2 <= ceilZ; ++z2) { setBlock(center.add(x, y, z2), block); setBlock(center.add(-x, y, z2), block); affected += 2; @@ -1736,12 +1738,12 @@ public class EditSession { } else if (x < z) { for (int y = 0; y < height; ++y) { for (int x2 = -x; x2 <= x; ++x2) { - for (int z2 = -z; z2 <= z; ++z2) { + for (int z2 = -ceilZ; z2 <= ceilZ; ++z2) { setBlock(center.add(x2, y, z2), block); ++affected; } - setBlock(center.add(z, y, x2), block); - setBlock(center.add(-z, y, x2), block); + setBlock(center.add(ceilZ, y, x2), block); + setBlock(center.add(-ceilZ, y, x2), block); affected += 2; } } @@ -1760,11 +1762,11 @@ public class EditSession { * @return number of blocks set * @throws MaxChangedBlocksException */ - public int makeCylinder(Vector pos, Pattern block, int radius, int height) + public int makeCylinder(Vector pos, Pattern block, double radius, int height) throws MaxChangedBlocksException { int x = 0; - int z = radius; - int d = (5 - radius * 4) / 4; + double z = radius; + double d = (5 - radius * 4) / 4; int affected = 0; if (height == 0) { @@ -1807,13 +1809,14 @@ public class EditSession { * @return number of blocks changed * @throws MaxChangedBlocksException */ - public int makeSphere(Vector pos, Pattern block, int radius, + public int makeSphere(Vector pos, Pattern block, double radius, boolean filled) throws MaxChangedBlocksException { int affected = 0; - for (int x = 0; x <= radius; ++x) { - for (int y = 0; y <= radius; ++y) { - for (int z = 0; z <= radius; ++z) { + int ceilRadius = (int) Math.ceil(radius); + for (int x = 0; x <= ceilRadius; ++x) { + for (int y = 0; y <= ceilRadius; ++y) { + for (int z = 0; z <= ceilRadius; ++z) { Vector vec = pos.add(x, y, z); double d = vec.distance(pos); diff --git a/src/main/java/com/sk89q/worldedit/tools/BrushTool.java b/src/main/java/com/sk89q/worldedit/tools/BrushTool.java index b34dfc3d6..0bbc1854c 100644 --- a/src/main/java/com/sk89q/worldedit/tools/BrushTool.java +++ b/src/main/java/com/sk89q/worldedit/tools/BrushTool.java @@ -41,7 +41,7 @@ public class BrushTool implements TraceTool { private Mask mask = null; private Brush brush = new SphereBrush(); private Pattern material = new SingleBlockPattern(new BaseBlock(BlockID.COBBLESTONE)); - private int size = 1; + private double size = 1; private String permission; /** @@ -125,17 +125,17 @@ public class BrushTool implements TraceTool { * * @return */ - public int getSize() { + public double getSize() { return size; } /** * Set the set brush size. * - * @param size + * @param radius */ - public void setSize(int size) { - this.size = size; + public void setSize(double radius) { + this.size = radius; } /** diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/Brush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/Brush.java index 815681a6e..67a98f38e 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/Brush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/Brush.java @@ -39,6 +39,6 @@ public interface Brush { * @param size * @throws MaxChangedBlocksException */ - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException; } diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/ClipboardBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/ClipboardBrush.java index baf636364..166a4769a 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/ClipboardBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/ClipboardBrush.java @@ -34,7 +34,7 @@ public class ClipboardBrush implements Brush { this.noAir = noAir; } - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { clipboard.place(editSession, pos.subtract(clipboard.getSize().divide(2)), noAir); diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/CylinderBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/CylinderBrush.java index e7794e732..ea08a1050 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/CylinderBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/CylinderBrush.java @@ -31,7 +31,7 @@ public class CylinderBrush implements Brush { this.height = height; } - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { editSession.makeCylinder(pos, mat, size, height); } diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/HollowCylinderBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/HollowCylinderBrush.java index 6984058f1..282f60dc8 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/HollowCylinderBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/HollowCylinderBrush.java @@ -31,7 +31,7 @@ public class HollowCylinderBrush implements Brush { this.height = height; } - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { editSession.makeHollowCylinder(pos, mat, size, height); } diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/HollowSphereBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/HollowSphereBrush.java index f383b5c44..e31f28b70 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/HollowSphereBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/HollowSphereBrush.java @@ -28,7 +28,7 @@ public class HollowSphereBrush implements Brush { public HollowSphereBrush() { } - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { editSession.makeSphere(pos, mat, size, false); } diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java index eca54d841..8dea6a6c0 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/SmoothBrush.java @@ -36,9 +36,9 @@ public class SmoothBrush implements Brush { this.iterations = iterations; } - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { - int rad = size; + double rad = size; Vector min = pos.subtract(rad, rad, rad); Vector max = pos.add(rad, rad + 10, rad); Region region = new CuboidRegion(min, max); diff --git a/src/main/java/com/sk89q/worldedit/tools/brushes/SphereBrush.java b/src/main/java/com/sk89q/worldedit/tools/brushes/SphereBrush.java index 98fe4d9db..f02088722 100644 --- a/src/main/java/com/sk89q/worldedit/tools/brushes/SphereBrush.java +++ b/src/main/java/com/sk89q/worldedit/tools/brushes/SphereBrush.java @@ -28,7 +28,7 @@ public class SphereBrush implements Brush { public SphereBrush() { } - public void build(EditSession editSession, Vector pos, Pattern mat, int size) + public void build(EditSession editSession, Vector pos, Pattern mat, double size) throws MaxChangedBlocksException { editSession.makeSphere(pos, mat, size, true); }