From d5e8f037c9d8ac7cf2a36eedc5bf4a881607ad45 Mon Sep 17 00:00:00 2001 From: sk89q Date: Fri, 18 Feb 2011 23:48:27 -0800 Subject: [PATCH] Added simple smoothing brush. --- plugin.yml | 9 ++-- .../worldedit/commands/BrushCommands.java | 32 ++++++++++++ .../worldedit/tools/brushes/SmoothBrush.java | 49 +++++++++++++++++++ 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/com/sk89q/worldedit/tools/brushes/SmoothBrush.java diff --git a/plugin.yml b/plugin.yml index 080e0790b..902515fb2 100644 --- a/plugin.yml +++ b/plugin.yml @@ -14,6 +14,9 @@ commands: description: Choose the clipboard brush usage: / [-a] aliases: ['copy'] + smooth: + description: Choose the terrain softener brush + usage: / [size] [iterations] chunkinfo: description: Get information about the chunk that you are inside usage: / @@ -107,6 +110,9 @@ commands: up: description: Go upwards some distance usage: / + /smooth: + description: Smooth the elevation in the selection + usage: / [iterations] /overlay: description: Set a block on top of blocks in the region usage: / @@ -117,9 +123,6 @@ commands: description: Build the walls, ceiling, and roof of a selection usage: / aliases: ['/outline'] - /smooth: - description: Smooth the elevation in the selection - usage: / [iterations] /replace: description: Replace all blocks in the selection with another usage: / [from-block] diff --git a/src/com/sk89q/worldedit/commands/BrushCommands.java b/src/com/sk89q/worldedit/commands/BrushCommands.java index e53c22a36..c4c05978b 100644 --- a/src/com/sk89q/worldedit/commands/BrushCommands.java +++ b/src/com/sk89q/worldedit/commands/BrushCommands.java @@ -36,6 +36,7 @@ import com.sk89q.worldedit.tools.brushes.ClipboardBrush; import com.sk89q.worldedit.tools.brushes.CylinderBrush; import com.sk89q.worldedit.tools.brushes.HollowCylinderBrush; import com.sk89q.worldedit.tools.brushes.HollowSphereBrush; +import com.sk89q.worldedit.tools.brushes.SmoothBrush; import com.sk89q.worldedit.tools.brushes.SphereBrush; /** @@ -162,4 +163,35 @@ public class BrushCommands { player.print("Clipboard brush shape equipped."); } + + @Command( + aliases = {"smooth"}, + usage = "[size] [iterations]", + desc = "Choose the terrain softener brush", + min = 0, + max = 2 + ) + @CommandPermissions({"worldedit.brush.smooth"}) + public static void smoothBrush(CommandContext args, WorldEdit we, + LocalSession session, LocalPlayer player, EditSession editSession) + throws WorldEditException { + + LocalConfiguration config = we.getConfiguration(); + + int radius = args.argsLength() > 0 ? args.getInteger(0) : 2; + if (radius > config.maxBrushRadius) { + player.printError("Maximum allowed brush radius: " + + config.maxBrushRadius); + return; + } + + int iterations = args.argsLength() > 1 ? args.getInteger(1) : 4; + + BrushTool tool = session.getBrushTool(player.getItemInHand()); + tool.setSize(radius); + tool.setBrush(new SmoothBrush(iterations)); + + player.print(String.format("Smooth brush equipped (%d x %dx).", + radius, iterations)); + } } diff --git a/src/com/sk89q/worldedit/tools/brushes/SmoothBrush.java b/src/com/sk89q/worldedit/tools/brushes/SmoothBrush.java new file mode 100644 index 000000000..eca54d841 --- /dev/null +++ b/src/com/sk89q/worldedit/tools/brushes/SmoothBrush.java @@ -0,0 +1,49 @@ +// $Id$ +/* + * WorldEdit + * Copyright (C) 2010, 2011 sk89q + * + * 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.tools.brushes; + +import com.sk89q.worldedit.EditSession; +import com.sk89q.worldedit.HeightMap; +import com.sk89q.worldedit.MaxChangedBlocksException; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.filtering.GaussianKernel; +import com.sk89q.worldedit.filtering.HeightMapFilter; +import com.sk89q.worldedit.patterns.Pattern; +import com.sk89q.worldedit.regions.CuboidRegion; +import com.sk89q.worldedit.regions.Region; + +public class SmoothBrush implements Brush { + private int iterations; + + public SmoothBrush(int iterations) { + this.iterations = iterations; + } + + public void build(EditSession editSession, Vector pos, Pattern mat, int size) + throws MaxChangedBlocksException { + int rad = size; + Vector min = pos.subtract(rad, rad, rad); + Vector max = pos.add(rad, rad + 10, rad); + Region region = new CuboidRegion(min, max); + HeightMap heightMap = new HeightMap(editSession, region); + HeightMapFilter filter = new HeightMapFilter(new GaussianKernel(5, 1.0)); + heightMap.applyFilter(filter, iterations); + } +}