From 180be7de07014aa33bc6066f12a0819b731c1c9d Mon Sep 17 00:00:00 2001 From: Dr Scofield Date: Tue, 10 Feb 2009 13:10:57 +0000 Subject: this is step 2 of 2 of the OpenSim.Region.Environment refactor. NOTHING has been deleted or moved off to forge at this point. what has happened is that OpenSim.Region.Environment.Modules has been split in two: - OpenSim.Region.CoreModules: all those modules that are either directly or indirectly referenced from other OpenSim packages, or that provide functionality that the OpenSim developer community considers core functionality: CoreModules/Agent/AssetTransaction CoreModules/Agent/Capabilities CoreModules/Agent/TextureDownload CoreModules/Agent/TextureSender CoreModules/Agent/TextureSender/Tests CoreModules/Agent/Xfer CoreModules/Avatar/AvatarFactory CoreModules/Avatar/Chat/ChatModule CoreModules/Avatar/Combat CoreModules/Avatar/Currency/SampleMoney CoreModules/Avatar/Dialog CoreModules/Avatar/Friends CoreModules/Avatar/Gestures CoreModules/Avatar/Groups CoreModules/Avatar/InstantMessage CoreModules/Avatar/Inventory CoreModules/Avatar/Inventory/Archiver CoreModules/Avatar/Inventory/Transfer CoreModules/Avatar/Lure CoreModules/Avatar/ObjectCaps CoreModules/Avatar/Profiles CoreModules/Communications/Local CoreModules/Communications/REST CoreModules/Framework/EventQueue CoreModules/Framework/InterfaceCommander CoreModules/Hypergrid CoreModules/InterGrid CoreModules/Scripting/DynamicTexture CoreModules/Scripting/EMailModules CoreModules/Scripting/HttpRequest CoreModules/Scripting/LoadImageURL CoreModules/Scripting/VectorRender CoreModules/Scripting/WorldComm CoreModules/Scripting/XMLRPC CoreModules/World/Archiver CoreModules/World/Archiver/Tests CoreModules/World/Estate CoreModules/World/Land CoreModules/World/Permissions CoreModules/World/Serialiser CoreModules/World/Sound CoreModules/World/Sun CoreModules/World/Terrain CoreModules/World/Terrain/DefaultEffects CoreModules/World/Terrain/DefaultEffects/bin CoreModules/World/Terrain/DefaultEffects/bin/Debug CoreModules/World/Terrain/Effects CoreModules/World/Terrain/FileLoaders CoreModules/World/Terrain/FloodBrushes CoreModules/World/Terrain/PaintBrushes CoreModules/World/Terrain/Tests CoreModules/World/Vegetation CoreModules/World/Wind CoreModules/World/WorldMap - OpenSim.Region.OptionalModules: all those modules that are not core modules: OptionalModules/Avatar/Chat/IRC-stuff OptionalModules/Avatar/Concierge OptionalModules/Avatar/Voice/AsterixVoice OptionalModules/Avatar/Voice/SIPVoice OptionalModules/ContentManagementSystem OptionalModules/Grid/Interregion OptionalModules/Python OptionalModules/SvnSerialiser OptionalModules/World/NPC OptionalModules/World/TreePopulator --- .../World/Terrain/DefaultEffects/ChannelDigger.cs | 107 +++ .../World/Terrain/Effects/CookieCutter.cs | 125 +++ .../Terrain/Effects/DefaultTerrainGenerator.cs | 56 ++ .../CoreModules/World/Terrain/FileLoaders/BMP.cs | 76 ++ .../CoreModules/World/Terrain/FileLoaders/GIF.cs | 61 ++ .../Terrain/FileLoaders/GenericSystemDrawing.cs | 195 ++++ .../CoreModules/World/Terrain/FileLoaders/JPEG.cs | 112 +++ .../CoreModules/World/Terrain/FileLoaders/LLRAW.cs | 250 +++++ .../CoreModules/World/Terrain/FileLoaders/PNG.cs | 61 ++ .../CoreModules/World/Terrain/FileLoaders/RAW32.cs | 170 ++++ .../CoreModules/World/Terrain/FileLoaders/TIFF.cs | 61 ++ .../World/Terrain/FileLoaders/Terragen.cs | 142 +++ .../World/Terrain/FloodBrushes/FlattenArea.cs | 70 ++ .../World/Terrain/FloodBrushes/LowerArea.cs | 54 ++ .../World/Terrain/FloodBrushes/NoiseArea.cs | 58 ++ .../World/Terrain/FloodBrushes/RaiseArea.cs | 54 ++ .../World/Terrain/FloodBrushes/RevertArea.cs | 67 ++ .../World/Terrain/FloodBrushes/SmoothArea.cs | 114 +++ .../CoreModules/World/Terrain/ITerrainEffect.cs | 36 + .../World/Terrain/ITerrainFloodEffect.cs | 37 + .../CoreModules/World/Terrain/ITerrainLoader.cs | 42 + .../CoreModules/World/Terrain/ITerrainModule.cs | 61 ++ .../World/Terrain/ITerrainPaintableEffect.cs | 36 + .../World/Terrain/PaintBrushes/ErodeSphere.cs | 318 +++++++ .../World/Terrain/PaintBrushes/FlattenSphere.cs | 101 ++ .../World/Terrain/PaintBrushes/LowerSphere.cs | 84 ++ .../World/Terrain/PaintBrushes/NoiseSphere.cs | 67 ++ .../World/Terrain/PaintBrushes/OlsenSphere.cs | 223 +++++ .../World/Terrain/PaintBrushes/RaiseSphere.cs | 80 ++ .../World/Terrain/PaintBrushes/RevertSphere.cs | 80 ++ .../World/Terrain/PaintBrushes/SmoothSphere.cs | 100 ++ .../World/Terrain/PaintBrushes/WeatherSphere.cs | 211 +++++ .../CoreModules/World/Terrain/TerrainException.cs | 46 + .../CoreModules/World/Terrain/TerrainModule.cs | 1001 ++++++++++++++++++++ .../CoreModules/World/Terrain/Tests/TerrainTest.cs | 118 +++ 35 files changed, 4474 insertions(+) create mode 100644 OpenSim/Region/CoreModules/World/Terrain/DefaultEffects/ChannelDigger.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/Effects/CookieCutter.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/Effects/DefaultTerrainGenerator.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/BMP.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GIF.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/JPEG.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/PNG.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/RAW32.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/TIFF.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/FlattenArea.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/LowerArea.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/NoiseArea.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RaiseArea.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RevertArea.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/SmoothArea.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/ITerrainEffect.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/ITerrainFloodEffect.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/ITerrainLoader.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/ITerrainModule.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/ITerrainPaintableEffect.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/ErodeSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/FlattenSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/LowerSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/NoiseSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/OlsenSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RaiseSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RevertSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/SmoothSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/WeatherSphere.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/TerrainException.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/TerrainModule.cs create mode 100644 OpenSim/Region/CoreModules/World/Terrain/Tests/TerrainTest.cs (limited to 'OpenSim/Region/CoreModules/World/Terrain') diff --git a/OpenSim/Region/CoreModules/World/Terrain/DefaultEffects/ChannelDigger.cs b/OpenSim/Region/CoreModules/World/Terrain/DefaultEffects/ChannelDigger.cs new file mode 100644 index 0000000..f96ab88 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/DefaultEffects/ChannelDigger.cs @@ -0,0 +1,107 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.CoreModules.World.Terrain; +using OpenSim.Region.CoreModules.World.Terrain.FloodBrushes; + +namespace OpenSim.Region.Modules.Terrain.Extensions.DefaultEffects.Effects +{ + public class ChannelDigger : ITerrainEffect + { + private readonly int num_h = 4; + private readonly int num_w = 4; + + private readonly ITerrainFloodEffect raiseFunction = new RaiseArea(); + private readonly ITerrainFloodEffect smoothFunction = new SmoothArea(); + + #region ITerrainEffect Members + + public void RunEffect(ITerrainChannel map) + { + FillMap(map, 15); + BuildTiles(map, 7); + SmoothMap(map, 3); + } + + #endregion + + private void SmoothMap(ITerrainChannel map, int rounds) + { + Boolean[,] bitmap = new bool[map.Width,map.Height]; + for (int x = 0; x < map.Width; x++) + { + for (int y = 0; y < map.Height; y++) + { + bitmap[x, y] = true; + } + } + + for (int i = 0; i < rounds; i++) + { + smoothFunction.FloodEffect(map, bitmap, 1.0); + } + } + + private void FillMap(ITerrainChannel map, double val) + { + for (int x = 0; x < map.Width; x++) + for (int y = 0; y < map.Height; y++) + map[x, y] = val; + } + + private void BuildTiles(ITerrainChannel map, double height) + { + int channelWidth = (int) Math.Floor((map.Width / num_w) * 0.8); + int channelHeight = (int) Math.Floor((map.Height / num_h) * 0.8); + int channelXOffset = (map.Width / num_w) - channelWidth; + int channelYOffset = (map.Height / num_h) - channelHeight; + + for (int x = 0; x < num_w; x++) + { + for (int y = 0; y < num_h; y++) + { + int xoff = ((channelXOffset + channelWidth) * x) + (channelXOffset / 2); + int yoff = ((channelYOffset + channelHeight) * y) + (channelYOffset / 2); + + Boolean[,] bitmap = new bool[map.Width,map.Height]; + + for (int dx = 0; dx < channelWidth; dx++) + { + for (int dy = 0; dy < channelHeight; dy++) + { + bitmap[dx + xoff, dy + yoff] = true; + } + } + + raiseFunction.FloodEffect(map, bitmap, height); + } + } + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/Effects/CookieCutter.cs b/OpenSim/Region/CoreModules/World/Terrain/Effects/CookieCutter.cs new file mode 100644 index 0000000..cb8112c --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/Effects/CookieCutter.cs @@ -0,0 +1,125 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; +using OpenSim.Region.CoreModules.World.Terrain.PaintBrushes; + +namespace OpenSim.Region.CoreModules.World.Terrain.Effects +{ + internal class CookieCutter : ITerrainEffect + { + #region ITerrainEffect Members + + public void RunEffect(ITerrainChannel map) + { + ITerrainPaintableEffect eroder = new WeatherSphere(); + + bool[,] cliffMask = new bool[map.Width,map.Height]; + bool[,] channelMask = new bool[map.Width,map.Height]; + bool[,] smoothMask = new bool[map.Width,map.Height]; + bool[,] allowMask = new bool[map.Width,map.Height]; + + Console.WriteLine("S1"); + + // Step one, generate rough mask + int x, y; + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + Console.Write("."); + smoothMask[x, y] = true; + allowMask[x,y] = true; + + // Start underwater + map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 3, 0.25) * 5; + // Add a little height. (terrain should now be above water, mostly.) + map[x, y] += 20; + + const int channelsX = 4; + int channelWidth = (map.Width / channelsX / 4); + const int channelsY = 4; + int channelHeight = (map.Height / channelsY / 4); + + SetLowerChannel(map, cliffMask, channelMask, x, y, channelsX, channelWidth, map.Width, x); + SetLowerChannel(map, cliffMask, channelMask, x, y, channelsY, channelHeight, map.Height, y); + } + } + + Console.WriteLine("S2"); + //smooth.FloodEffect(map, smoothMask, 4.0); + + Console.WriteLine("S3"); + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (cliffMask[x, y]) + eroder.PaintEffect(map, allowMask, x, y, -1, 4, 0.1); + } + } + + for (x = 0; x < map.Width; x += 2) + { + for (y = 0; y < map.Height; y += 2) + { + if (map[x, y] < 0.1) + map[x, y] = 0.1; + if (map[x, y] > 256) + map[x, y] = 256; + } + } + //smooth.FloodEffect(map, smoothMask, 4.0); + } + + #endregion + + private static void SetLowerChannel(ITerrainChannel map, bool[,] cliffMask, bool[,] channelMask, int x, int y, int numChannels, int channelWidth, + int mapSize, int rp) + { + for (int i = 0; i < numChannels; i++) + { + double distanceToLine = Math.Abs(rp - ((mapSize / numChannels) * i)); + + if (distanceToLine < channelWidth) + { + if (channelMask[x, y]) + return; + + // Remove channels + map[x, y] -= 10; + channelMask[x, y] = true; + } + if (distanceToLine < 1) + { + cliffMask[x, y] = true; + } + } + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/Effects/DefaultTerrainGenerator.cs b/OpenSim/Region/CoreModules/World/Terrain/Effects/DefaultTerrainGenerator.cs new file mode 100644 index 0000000..da6ee12 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/Effects/DefaultTerrainGenerator.cs @@ -0,0 +1,56 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +using OpenSim.Framework; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.Effects +{ + internal class DefaultTerrainGenerator : ITerrainEffect + { + #region ITerrainEffect Members + + public void RunEffect(ITerrainChannel map) + { + int x, y; + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 3, 0.25) * 10; + double spherFac = TerrainUtil.SphericalFactor(x, y, Constants.RegionSize / 2, Constants.RegionSize / 2, 50) * 0.01; + if (map[x, y] < spherFac) + { + map[x, y] = spherFac; + } + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/BMP.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/BMP.cs new file mode 100644 index 0000000..4f395b5 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/BMP.cs @@ -0,0 +1,76 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + /// + /// A generic windows bitmap loader. + /// Should be capable of handling 24-bit RGB images. + /// + /// Uses the System.Drawing filesystem loader. + /// + internal class BMP : GenericSystemDrawing + { + /// + /// Exports a file to a image on the disk using a System.Drawing exporter. + /// + /// The target filename + /// The terrain channel being saved + public override void SaveFile(string filename, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(filename, ImageFormat.Bmp); + } + + /// + /// Exports a stream using a System.Drawing exporter. + /// + /// The target stream + /// The terrain channel being saved + public override void SaveStream(Stream stream, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(stream, ImageFormat.Png); + } + + /// + /// The human readable version of the file format(s) this loader handles + /// + /// + public override string ToString() + { + return "BMP"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GIF.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GIF.cs new file mode 100644 index 0000000..cff82d1 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GIF.cs @@ -0,0 +1,61 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + internal class GIF : GenericSystemDrawing + { + public override void SaveFile(string filename, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(filename, ImageFormat.Gif); + } + + /// + /// Exports a stream using a System.Drawing exporter. + /// + /// The target stream + /// The terrain channel being saved + public override void SaveStream(Stream stream, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(stream, ImageFormat.Gif); + } + + public override string ToString() + { + return "GIF"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs new file mode 100644 index 0000000..477c73c --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/GenericSystemDrawing.cs @@ -0,0 +1,195 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + /// + /// A virtual class designed to have methods overloaded, + /// this class provides an interface for a generic image + /// saving and loading mechanism, but does not specify the + /// format. It should not be insubstantiated directly. + /// + public class GenericSystemDrawing : ITerrainLoader + { + #region ITerrainLoader Members + + public string FileExtension + { + get { return ".gsd"; } + } + + /// + /// Loads a file from a specified filename on the disk, + /// parses the image using the System.Drawing parsers + /// then returns a terrain channel. Values are + /// returned based on HSL brightness between 0m and 128m + /// + /// The target image to load + /// A terrain channel generated from the image. + public virtual ITerrainChannel LoadFile(string filename) + { + return LoadBitmap(new Bitmap(filename)); + } + + public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h) + { + throw new NotImplementedException(); + } + + public virtual ITerrainChannel LoadStream(Stream stream) + { + return LoadBitmap(new Bitmap(stream)); + } + + protected virtual ITerrainChannel LoadBitmap(Bitmap bitmap) + { + ITerrainChannel retval = new TerrainChannel(bitmap.Width, bitmap.Height); + + int x; + for (x = 0; x < bitmap.Width; x++) + { + int y; + for (y = 0; y < bitmap.Height; y++) + { + retval[x, y] = bitmap.GetPixel(x, bitmap.Height - y - 1).GetBrightness() * 128; + } + } + + return retval; + } + + /// + /// Exports a file to a image on the disk using a System.Drawing exporter. + /// + /// The target filename + /// The terrain channel being saved + public virtual void SaveFile(string filename, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(filename, ImageFormat.Png); + } + + /// + /// Exports a stream using a System.Drawing exporter. + /// + /// The target stream + /// The terrain channel being saved + public virtual void SaveStream(Stream stream, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(stream, ImageFormat.Png); + } + + #endregion + + public override string ToString() + { + return "SYS.DRAWING"; + } + + /// + /// Protected method, generates a grayscale bitmap + /// image from a specified terrain channel. + /// + /// The terrain channel to export to bitmap + /// A System.Drawing.Bitmap containing a grayscale image + protected static Bitmap CreateGrayscaleBitmapFromMap(ITerrainChannel map) + { + Bitmap bmp = new Bitmap(map.Width, map.Height); + + const int pallete = 256; + + Color[] grays = new Color[pallete]; + for (int i = 0; i < grays.Length; i++) + { + grays[i] = Color.FromArgb(i, i, i); + } + + for (int y = 0; y < map.Height; y++) + { + for (int x = 0; x < map.Width; x++) + { + // 512 is the largest possible height before colours clamp + int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 128.0), 0.0) * (pallete - 1)); + + // Handle error conditions + if (colorindex > pallete - 1 || colorindex < 0) + bmp.SetPixel(x, map.Height - y - 1, Color.Red); + else + bmp.SetPixel(x, map.Height - y - 1, grays[colorindex]); + } + } + return bmp; + } + + /// + /// Protected method, generates a coloured bitmap + /// image from a specified terrain channel. + /// + /// The terrain channel to export to bitmap + /// A System.Drawing.Bitmap containing a coloured image + protected static Bitmap CreateBitmapFromMap(ITerrainChannel map) + { + Bitmap gradientmapLd = new Bitmap("defaultstripe.png"); + + int pallete = gradientmapLd.Height; + + Bitmap bmp = new Bitmap(map.Width, map.Height); + Color[] colours = new Color[pallete]; + + for (int i = 0; i < pallete; i++) + { + colours[i] = gradientmapLd.GetPixel(0, i); + } + + for (int y = 0; y < map.Height; y++) + { + for (int x = 0; x < map.Width; x++) + { + // 512 is the largest possible height before colours clamp + int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1)); + + // Handle error conditions + if (colorindex > pallete - 1 || colorindex < 0) + bmp.SetPixel(x, map.Height - y - 1, Color.Red); + else + bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]); + } + } + return bmp; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/JPEG.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/JPEG.cs new file mode 100644 index 0000000..f8e31f8 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/JPEG.cs @@ -0,0 +1,112 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + public class JPEG : ITerrainLoader + { + #region ITerrainLoader Members + + public string FileExtension + { + get { return ".jpg"; } + } + + public ITerrainChannel LoadFile(string filename) + { + throw new NotImplementedException(); + } + + public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h) + { + throw new NotImplementedException(); + } + + public ITerrainChannel LoadStream(Stream stream) + { + throw new NotImplementedException(); + } + + public void SaveFile(string filename, ITerrainChannel map) + { + Bitmap colours = CreateBitmapFromMap(map); + + colours.Save(filename, ImageFormat.Jpeg); + } + + /// + /// Exports a stream using a System.Drawing exporter. + /// + /// The target stream + /// The terrain channel being saved + public void SaveStream(Stream stream, ITerrainChannel map) + { + Bitmap colours = CreateBitmapFromMap(map); + + colours.Save(stream, ImageFormat.Jpeg); + } + + #endregion + + public override string ToString() + { + return "JPEG"; + } + + private static Bitmap CreateBitmapFromMap(ITerrainChannel map) + { + Bitmap gradientmapLd = new Bitmap("defaultstripe.png"); + + int pallete = gradientmapLd.Height; + + Bitmap bmp = new Bitmap(map.Width, map.Height); + Color[] colours = new Color[pallete]; + + for (int i = 0; i < pallete; i++) + { + colours[i] = gradientmapLd.GetPixel(0, i); + } + + for (int y = 0; y < map.Height; y++) + { + for (int x = 0; x < map.Width; x++) + { + // 512 is the largest possible height before colours clamp + int colorindex = (int) (Math.Max(Math.Min(1.0, map[x, y] / 512.0), 0.0) * (pallete - 1)); + bmp.SetPixel(x, map.Height - y - 1, colours[colorindex]); + } + } + return bmp; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs new file mode 100644 index 0000000..a86ae00 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/LLRAW.cs @@ -0,0 +1,250 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.IO; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + public class LLRAW : ITerrainLoader + { + public struct HeightmapLookupValue : IComparable + { + public int Index; + public double Value; + + public HeightmapLookupValue(int index, double value) + { + Index = index; + Value = value; + } + + public int CompareTo(HeightmapLookupValue val) + { + return Value.CompareTo(val.Value); + } + } + + /// Lookup table to speed up terrain exports + HeightmapLookupValue[] LookupHeightTable; + + public LLRAW() + { + LookupHeightTable = new HeightmapLookupValue[256 * 256]; + + for (int i = 0; i < 256; i++) + { + for (int j = 0; j < 256; j++) + { + LookupHeightTable[i + (j * 256)] = new HeightmapLookupValue(i + (j * 256), ((double)i * ((double)j / 128.0d))); + } + } + Array.Sort(LookupHeightTable); + } + + #region ITerrainLoader Members + + public ITerrainChannel LoadFile(string filename) + { + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.Open, FileAccess.Read); + ITerrainChannel retval = LoadStream(s); + + s.Close(); + + return retval; + } + + public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) + { + TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); + + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.Open, FileAccess.Read); + BinaryReader bs = new BinaryReader(s); + + int currFileYOffset = fileHeight - 1; + + // if our region isn't on the first Y section of the areas to be landscaped, then + // advance to our section of the file + while (currFileYOffset > offsetY) + { + // read a whole strip of regions + int heightsToRead = sectionHeight * (fileWidth * sectionWidth); + bs.ReadBytes(heightsToRead * 13); // because there are 13 fun channels + currFileYOffset--; + } + + // got to the Y start offset within the file of our region + // so read the file bits associated with our region + int y; + // for each Y within our Y offset + for (y = sectionHeight - 1; y >= 0; y--) + { + int currFileXOffset = 0; + + // if our region isn't the first X section of the areas to be landscaped, then + // advance the stream to the X start pos of our section in the file + // i.e. eat X upto where we start + while (currFileXOffset < offsetX) + { + bs.ReadBytes(sectionWidth * 13); + currFileXOffset++; + } + + // got to our X offset, so write our regions X line + int x; + for (x = 0; x < sectionWidth; x++) + { + // Read a strip and continue + retval[x, y] = bs.ReadByte() * (bs.ReadByte() / 128.0); + bs.ReadBytes(11); + } + // record that we wrote it + currFileXOffset++; + + // if our region isn't the last X section of the areas to be landscaped, then + // advance the stream to the end of this Y column + while (currFileXOffset < fileWidth) + { + // eat the next regions x line + bs.ReadBytes(sectionWidth * 13); //The 13 channels again + currFileXOffset++; + } + } + + bs.Close(); + s.Close(); + + return retval; + } + + public ITerrainChannel LoadStream(Stream s) + { + TerrainChannel retval = new TerrainChannel(); + + BinaryReader bs = new BinaryReader(s); + int y; + for (y = 0; y < retval.Height; y++) + { + int x; + for (x = 0; x < retval.Width; x++) + { + retval[x, (retval.Height - 1) - y] = bs.ReadByte() * (bs.ReadByte() / 128.0); + bs.ReadBytes(11); // Advance the stream to next bytes. + } + } + + bs.Close(); + + return retval; + } + + public void SaveFile(string filename, ITerrainChannel map) + { + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.CreateNew, FileAccess.Write); + SaveStream(s, map); + + s.Close(); + } + + public void SaveStream(Stream s, ITerrainChannel map) + { + BinaryWriter binStream = new BinaryWriter(s); + + // Output the calculated raw + for (int y = 0; y < map.Height; y++) + { + for (int x = 0; x < map.Width; x++) + { + double t = map[x, (map.Height - 1) - y]; + //if height is less than 0, set it to 0 as + //can't save -ve values in a LLRAW file + if (t < 0d) + { + t = 0d; + } + + int index = 0; + + // The lookup table is pre-sorted, so we either find an exact match or + // the next closest (smaller) match with a binary search + index = Array.BinarySearch(LookupHeightTable, new HeightmapLookupValue(0, t)); + if (index < 0) + index = ~index - 1; + + index = LookupHeightTable[index].Index; + + byte red = (byte) (index & 0xFF); + byte green = (byte) ((index >> 8) & 0xFF); + const byte blue = 20; + const byte alpha1 = 0; + const byte alpha2 = 0; + const byte alpha3 = 0; + const byte alpha4 = 0; + const byte alpha5 = 255; + const byte alpha6 = 255; + const byte alpha7 = 255; + const byte alpha8 = 255; + byte alpha9 = red; + byte alpha10 = green; + + binStream.Write(red); + binStream.Write(green); + binStream.Write(blue); + binStream.Write(alpha1); + binStream.Write(alpha2); + binStream.Write(alpha3); + binStream.Write(alpha4); + binStream.Write(alpha5); + binStream.Write(alpha6); + binStream.Write(alpha7); + binStream.Write(alpha8); + binStream.Write(alpha9); + binStream.Write(alpha10); + } + } + + binStream.Close(); + } + + public string FileExtension + { + get { return ".raw"; } + } + + #endregion + + public override string ToString() + { + return "LL/SL RAW"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/PNG.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/PNG.cs new file mode 100644 index 0000000..0dea282 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/PNG.cs @@ -0,0 +1,61 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + internal class PNG : GenericSystemDrawing + { + public override void SaveFile(string filename, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(filename, ImageFormat.Png); + } + + /// + /// Exports a stream using a System.Drawing exporter. + /// + /// The target stream + /// The terrain channel being saved + public override void SaveStream(Stream stream, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(stream, ImageFormat.Png); + } + + public override string ToString() + { + return "PNG"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/RAW32.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/RAW32.cs new file mode 100644 index 0000000..178104f --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/RAW32.cs @@ -0,0 +1,170 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.IO; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + public class RAW32 : ITerrainLoader + { + #region ITerrainLoader Members + + public string FileExtension + { + get { return ".r32"; } + } + + public ITerrainChannel LoadFile(string filename) + { + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.Open, FileAccess.Read); + ITerrainChannel retval = LoadStream(s); + + s.Close(); + + return retval; + } + + public ITerrainChannel LoadFile(string filename, int offsetX, int offsetY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight) + { + TerrainChannel retval = new TerrainChannel(sectionWidth, sectionHeight); + + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.Open, FileAccess.Read); + BinaryReader bs = new BinaryReader(s); + + int currFileYOffset = 0; + + // if our region isn't on the first Y section of the areas to be landscaped, then + // advance to our section of the file + while (currFileYOffset < offsetY) + { + // read a whole strip of regions + int heightsToRead = sectionHeight * (fileWidth * sectionWidth); + bs.ReadBytes(heightsToRead * 4); // because the floats are 4 bytes in the file + currFileYOffset++; + } + + // got to the Y start offset within the file of our region + // so read the file bits associated with our region + int y; + // for each Y within our Y offset + for (y = 0; y < sectionHeight; y++) + { + int currFileXOffset = 0; + + // if our region isn't the first X section of the areas to be landscaped, then + // advance the stream to the X start pos of our section in the file + // i.e. eat X upto where we start + while (currFileXOffset < offsetX) + { + bs.ReadBytes(sectionWidth * 4); // 4 bytes = single + currFileXOffset++; + } + + // got to our X offset, so write our regions X line + int x; + for (x = 0; x < sectionWidth; x++) + { + // Read a strip and continue + retval[x, y] = bs.ReadSingle(); + } + // record that we wrote it + currFileXOffset++; + + // if our region isn't the last X section of the areas to be landscaped, then + // advance the stream to the end of this Y column + while (currFileXOffset < fileWidth) + { + // eat the next regions x line + bs.ReadBytes(sectionWidth * 4); // 4 bytes = single + currFileXOffset++; + } + } + + bs.Close(); + s.Close(); + + return retval; + } + + public ITerrainChannel LoadStream(Stream s) + { + TerrainChannel retval = new TerrainChannel(); + + BinaryReader bs = new BinaryReader(s); + int y; + for (y = 0; y < retval.Height; y++) + { + int x; + for (x = 0; x < retval.Width; x++) + { + retval[x, y] = bs.ReadSingle(); + } + } + + bs.Close(); + + return retval; + } + + public void SaveFile(string filename, ITerrainChannel map) + { + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.Create, FileAccess.Write); + SaveStream(s, map); + + s.Close(); + } + + public void SaveStream(Stream s, ITerrainChannel map) + { + BinaryWriter bs = new BinaryWriter(s); + + int y; + for (y = 0; y < map.Height; y++) + { + int x; + for (x = 0; x < map.Width; x++) + { + bs.Write((float) map[x, y]); + } + } + + bs.Close(); + } + + #endregion + + public override string ToString() + { + return "RAW32"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/TIFF.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/TIFF.cs new file mode 100644 index 0000000..220431f --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/TIFF.cs @@ -0,0 +1,61 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + internal class TIFF : GenericSystemDrawing + { + public override void SaveFile(string filename, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(filename, ImageFormat.Tiff); + } + + /// + /// Exports a stream using a System.Drawing exporter. + /// + /// The target stream + /// The terrain channel being saved + public override void SaveStream(Stream stream, ITerrainChannel map) + { + Bitmap colours = CreateGrayscaleBitmapFromMap(map); + + colours.Save(stream, ImageFormat.Tiff); + } + + public override string ToString() + { + return "TIFF"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs new file mode 100644 index 0000000..426708d --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs @@ -0,0 +1,142 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.IO; +using System.Text; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders +{ + /// + /// Terragen File Format Loader + /// Built from specification at + /// http://www.planetside.co.uk/terragen/dev/tgterrain.html + /// + internal class Terragen : ITerrainLoader + { + #region ITerrainLoader Members + + public ITerrainChannel LoadFile(string filename) + { + FileInfo file = new FileInfo(filename); + FileStream s = file.Open(FileMode.Open, FileAccess.Read); + ITerrainChannel retval = LoadStream(s); + + s.Close(); + + return retval; + } + + public ITerrainChannel LoadStream(Stream s) + { + TerrainChannel retval = new TerrainChannel(); + + BinaryReader bs = new BinaryReader(s); + + bool eof = false; + if (Encoding.ASCII.GetString(bs.ReadBytes(16)) == "TERRAGENTERRAIN ") + { + int w = 256; + int h = 256; + + // Terragen file + while (eof == false) + { + string tmp = Encoding.ASCII.GetString(bs.ReadBytes(4)); + switch (tmp) + { + case "SIZE": + int sztmp = bs.ReadInt16() + 1; + w = sztmp; + h = sztmp; + bs.ReadInt16(); + break; + case "XPTS": + w = bs.ReadInt16(); + bs.ReadInt16(); + break; + case "YPTS": + h = bs.ReadInt16(); + bs.ReadInt16(); + break; + case "ALTW": + eof = true; + Int16 heightScale = bs.ReadInt16(); + Int16 baseHeight = bs.ReadInt16(); + retval = new TerrainChannel(w, h); + int x; + for (x = 0; x < w; x++) + { + int y; + for (y = 0; y < h; y++) + { + retval[x, y] = baseHeight + bs.ReadInt16() * (double) heightScale / 65536.0; + } + } + break; + default: + bs.ReadInt32(); + break; + } + } + } + + bs.Close(); + + return retval; + } + + public void SaveFile(string filename, ITerrainChannel map) + { + throw new NotImplementedException(); + } + + public void SaveStream(Stream stream, ITerrainChannel map) + { + throw new NotImplementedException(); + } + + public string FileExtension + { + get { return ".ter"; } + } + + public ITerrainChannel LoadFile(string filename, int x, int y, int fileWidth, int fileHeight, int w, int h) + { + throw new NotImplementedException(); + } + + #endregion + + public override string ToString() + { + return "Terragen"; + } + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/FlattenArea.cs b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/FlattenArea.cs new file mode 100644 index 0000000..fe79c0b --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/FlattenArea.cs @@ -0,0 +1,70 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FloodBrushes +{ + public class FlattenArea : ITerrainFloodEffect + { + #region ITerrainFloodEffect Members + + public void FloodEffect(ITerrainChannel map, bool[,] fillArea, double strength) + { + double sum = 0.0; + double steps = 0.0; + + int x, y; + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (fillArea[x, y]) + { + sum += map[x, y]; + steps += 1.0; + } + } + } + + double avg = sum / steps; + + double str = 0.1 * strength; // == 0.2 in the default client + + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (fillArea[x, y]) + map[x, y] = (map[x, y] * (1.0 - str)) + (avg * str); + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/LowerArea.cs b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/LowerArea.cs new file mode 100644 index 0000000..5c0aace --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/LowerArea.cs @@ -0,0 +1,54 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FloodBrushes +{ + public class LowerArea : ITerrainFloodEffect + { + #region ITerrainFloodEffect Members + + public void FloodEffect(ITerrainChannel map, bool[,] fillArea, double strength) + { + int x; + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (fillArea[x, y]) + { + map[x, y] -= strength; + } + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/NoiseArea.cs b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/NoiseArea.cs new file mode 100644 index 0000000..02f2b53 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/NoiseArea.cs @@ -0,0 +1,58 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Framework; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.FloodBrushes +{ + public class NoiseArea : ITerrainFloodEffect + { + #region ITerrainFloodEffect Members + + public void FloodEffect(ITerrainChannel map, bool[,] fillArea, double strength) + { + int x; + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (fillArea[x, y]) + { + double noise = TerrainUtil.PerlinNoise2D((double) x / Constants.RegionSize, (double) y / Constants.RegionSize, 8, 1.0); + + map[x, y] += noise * strength; + } + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RaiseArea.cs b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RaiseArea.cs new file mode 100644 index 0000000..768b31f --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RaiseArea.cs @@ -0,0 +1,54 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FloodBrushes +{ + public class RaiseArea : ITerrainFloodEffect + { + #region ITerrainFloodEffect Members + + public void FloodEffect(ITerrainChannel map, bool[,] fillArea, double strength) + { + int x; + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (fillArea[x, y]) + { + map[x, y] += strength; + } + } + } + } + + #endregion + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RevertArea.cs b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RevertArea.cs new file mode 100644 index 0000000..66b9055 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/RevertArea.cs @@ -0,0 +1,67 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FloodBrushes +{ + public class RevertArea : ITerrainFloodEffect + { + private readonly ITerrainChannel m_revertmap; + + public RevertArea(ITerrainChannel revertmap) + { + m_revertmap = revertmap; + } + + #region ITerrainFloodEffect Members + + /// + /// reverts an area of the map to the heightfield stored in the revertmap + /// + /// the current heightmap + /// array indicating which sections of the map are to be reverted + /// unused + public void FloodEffect(ITerrainChannel map, bool[,] fillArea, double strength) + { + int x; + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (fillArea[x, y]) + { + map[x, y] = m_revertmap[x, y]; + } + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/SmoothArea.cs b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/SmoothArea.cs new file mode 100644 index 0000000..a75dde1 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/FloodBrushes/SmoothArea.cs @@ -0,0 +1,114 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.FloodBrushes +{ + public class SmoothArea : ITerrainFloodEffect + { + #region ITerrainFloodEffect Members + + public void FloodEffect(ITerrainChannel map, bool[,] fillArea, double strength) + { + double area = strength; + double step = strength / 4.0; + + double[,] manipulate = new double[map.Width,map.Height]; + int x, y; + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (!fillArea[x, y]) + continue; + + double average = 0.0; + int avgsteps = 0; + + double n; + for (n = 0.0 - area; n < area; n += step) + { + double l; + for (l = 0.0 - area; l < area; l += step) + { + avgsteps++; + average += GetBilinearInterpolate(x + n, y + l, map); + } + } + + manipulate[x, y] = average / avgsteps; + } + } + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (!fillArea[x, y]) + continue; + + map[x, y] = manipulate[x, y]; + } + } + } + + #endregion + + private static double GetBilinearInterpolate(double x, double y, ITerrainChannel map) + { + int w = map.Width; + int h = map.Height; + + if (x > w - 2.0) + x = w - 2.0; + if (y > h - 2.0) + y = h - 2.0; + if (x < 0.0) + x = 0.0; + if (y < 0.0) + y = 0.0; + + const int stepSize = 1; + double h00 = map[(int) x, (int) y]; + double h10 = map[(int) x + stepSize, (int) y]; + double h01 = map[(int) x, (int) y + stepSize]; + double h11 = map[(int) x + stepSize, (int) y + stepSize]; + double h1 = h00; + double h2 = h10; + double h3 = h01; + double h4 = h11; + double a00 = h1; + double a10 = h2 - h1; + double a01 = h3 - h1; + double a11 = h1 - h2 - h3 + h4; + double partialx = x - (int) x; + double partialz = y - (int) y; + double hi = a00 + (a10 * partialx) + (a01 * partialz) + (a11 * partialx * partialz); + return hi; + } + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/ITerrainEffect.cs b/OpenSim/Region/CoreModules/World/Terrain/ITerrainEffect.cs new file mode 100644 index 0000000..40b9f5a --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/ITerrainEffect.cs @@ -0,0 +1,36 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public interface ITerrainEffect + { + void RunEffect(ITerrainChannel map); + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/ITerrainFloodEffect.cs b/OpenSim/Region/CoreModules/World/Terrain/ITerrainFloodEffect.cs new file mode 100644 index 0000000..eee7a83 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/ITerrainFloodEffect.cs @@ -0,0 +1,37 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public interface ITerrainFloodEffect + { + void FloodEffect(ITerrainChannel map, Boolean[,] fillArea, double strength); + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/ITerrainLoader.cs b/OpenSim/Region/CoreModules/World/Terrain/ITerrainLoader.cs new file mode 100644 index 0000000..c62b897 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/ITerrainLoader.cs @@ -0,0 +1,42 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System.IO; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public interface ITerrainLoader + { + string FileExtension { get; } + ITerrainChannel LoadFile(string filename); + ITerrainChannel LoadFile(string filename, int fileStartX, int fileStartY, int fileWidth, int fileHeight, int sectionWidth, int sectionHeight); + ITerrainChannel LoadStream(Stream stream); + void SaveFile(string filename, ITerrainChannel map); + void SaveStream(Stream stream, ITerrainChannel map); + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/ITerrainModule.cs b/OpenSim/Region/CoreModules/World/Terrain/ITerrainModule.cs new file mode 100644 index 0000000..8c5d1d9 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/ITerrainModule.cs @@ -0,0 +1,61 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + +using System.IO; +using OpenMetaverse; +using OpenSim.Framework; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public interface ITerrainModule + { + void LoadFromFile(string filename); + void SaveToFile(string filename); + void ModifyTerrain(UUID user, Vector3 pos, byte size, byte action, UUID agentId); + + /// + /// Load a terrain from a stream. + /// + /// + /// Only required here to identify the image type. Not otherwise used in the loading itself. + /// + /// + void LoadFromStream(string filename, Stream stream); + + /// + /// Save a terrain to a stream. + /// + /// + /// Only required here to identify the image type. Not otherwise used in the saving itself. + /// + /// + void SaveToStream(string filename, Stream stream); + + void InstallPlugin(string name, ITerrainEffect plug); + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/ITerrainPaintableEffect.cs b/OpenSim/Region/CoreModules/World/Terrain/ITerrainPaintableEffect.cs new file mode 100644 index 0000000..15d9f6e --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/ITerrainPaintableEffect.cs @@ -0,0 +1,36 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public interface ITerrainPaintableEffect + { + void PaintEffect(ITerrainChannel map, bool[,] allowMask, double x, double y, double z, double strength, double duration); + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/ErodeSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/ErodeSphere.cs new file mode 100644 index 0000000..6ce6994 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/ErodeSphere.cs @@ -0,0 +1,318 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + /// + /// Hydraulic Erosion Brush + /// + public class ErodeSphere : ITerrainPaintableEffect + { + private const double rainHeight = 0.2; + private const int rounds = 10; + private const NeighbourSystem type = NeighbourSystem.Moore; + private const double waterSaturation = 0.30; + + #region Supporting Functions + + private static int[] Neighbours(NeighbourSystem neighbourType, int index) + { + int[] coord = new int[2]; + + index++; + + switch (neighbourType) + { + case NeighbourSystem.Moore: + switch (index) + { + case 1: + coord[0] = -1; + coord[1] = -1; + break; + + case 2: + coord[0] = -0; + coord[1] = -1; + break; + + case 3: + coord[0] = +1; + coord[1] = -1; + break; + + case 4: + coord[0] = -1; + coord[1] = -0; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + case 6: + coord[0] = +1; + coord[1] = -0; + break; + + case 7: + coord[0] = -1; + coord[1] = +1; + break; + + case 8: + coord[0] = -0; + coord[1] = +1; + break; + + case 9: + coord[0] = +1; + coord[1] = +1; + break; + + default: + break; + } + break; + + case NeighbourSystem.VonNeumann: + switch (index) + { + case 1: + coord[0] = 0; + coord[1] = -1; + break; + + case 2: + coord[0] = -1; + coord[1] = 0; + break; + + case 3: + coord[0] = +1; + coord[1] = 0; + break; + + case 4: + coord[0] = 0; + coord[1] = +1; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + default: + break; + } + break; + } + + return coord; + } + + private enum NeighbourSystem + { + Moore, + VonNeumann + } ; + + #endregion + + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + int x, y; + // Using one 'rain' round for this, so skipping a useless loop + // Will need to adapt back in for the Flood brush + + ITerrainChannel water = new TerrainChannel(map.Width, map.Height); + ITerrainChannel sediment = new TerrainChannel(map.Width, map.Height); + + // Fill with rain + for (x = 0; x < water.Width; x++) + for (y = 0; y < water.Height; y++) + water[x, y] = Math.Max(0.0, TerrainUtil.SphericalFactor(x, y, rx, ry, strength) * rainHeight * duration); + + for (int i = 0; i < rounds; i++) + { + // Erode underlying terrain + for (x = 0; x < water.Width; x++) + { + for (y = 0; y < water.Height; y++) + { + if (mask[x,y]) + { + const double solConst = (1.0 / rounds); + double sedDelta = water[x, y] * solConst; + map[x, y] -= sedDelta; + sediment[x, y] += sedDelta; + } + } + } + + // Move water + for (x = 0; x < water.Width; x++) + { + for (y = 0; y < water.Height; y++) + { + if (water[x, y] <= 0) + continue; + + // Step 1. Calculate average of neighbours + + int neighbours = 0; + double altitudeTotal = 0.0; + double altitudeMe = map[x, y] + water[x, y]; + + const int NEIGHBOUR_ME = 4; + const int NEIGHBOUR_MAX = 9; + + for (int j = 0; j < NEIGHBOUR_MAX; j++) + { + if (j != NEIGHBOUR_ME) + { + int[] coords = Neighbours(type, j); + + coords[0] += x; + coords[1] += y; + + if (coords[0] > map.Width - 1) + continue; + if (coords[1] > map.Height - 1) + continue; + if (coords[0] < 0) + continue; + if (coords[1] < 0) + continue; + + // Calculate total height of this neighbour + double altitudeNeighbour = water[coords[0], coords[1]] + map[coords[0], coords[1]]; + + // If it's greater than me... + if (altitudeNeighbour - altitudeMe < 0) + { + // Add it to our calculations + neighbours++; + altitudeTotal += altitudeNeighbour; + } + } + } + + if (neighbours == 0) + continue; + + double altitudeAvg = altitudeTotal / neighbours; + + // Step 2. Allocate water to neighbours. + for (int j = 0; j < NEIGHBOUR_MAX; j++) + { + if (j != NEIGHBOUR_ME) + { + int[] coords = Neighbours(type, j); + + coords[0] += x; + coords[1] += y; + + if (coords[0] > map.Width - 1) + continue; + if (coords[1] > map.Height - 1) + continue; + if (coords[0] < 0) + continue; + if (coords[1] < 0) + continue; + + // Skip if we dont have water to begin with. + if (water[x, y] < 0) + continue; + + // Calculate our delta average + double altitudeDelta = altitudeMe - altitudeAvg; + + if (altitudeDelta < 0) + continue; + + // Calculate how much water we can move + double waterMin = Math.Min(water[x, y], altitudeDelta); + double waterDelta = waterMin * ((water[coords[0], coords[1]] + map[coords[0], coords[1]]) + / altitudeTotal); + + double sedimentDelta = sediment[x, y] * (waterDelta / water[x, y]); + + if (sedimentDelta > 0) + { + sediment[x, y] -= sedimentDelta; + sediment[coords[0], coords[1]] += sedimentDelta; + } + } + } + } + } + + // Evaporate + + for (x = 0; x < water.Width; x++) + { + for (y = 0; y < water.Height; y++) + { + water[x, y] *= 1.0 - (rainHeight / rounds); + + double waterCapacity = waterSaturation * water[x, y]; + + double sedimentDeposit = sediment[x, y] - waterCapacity; + if (sedimentDeposit > 0) + { + if (mask[x,y]) + { + sediment[x, y] -= sedimentDeposit; + map[x, y] += sedimentDeposit; + } + } + } + } + } + + // Deposit any remainder (should be minimal) + for (x = 0; x < water.Width; x++) + for (y = 0; y < water.Height; y++) + if (mask[x,y] && sediment[x, y] > 0) + map[x, y] += sediment[x, y]; + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/FlattenSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/FlattenSphere.cs new file mode 100644 index 0000000..928a595 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/FlattenSphere.cs @@ -0,0 +1,101 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + public class FlattenSphere : ITerrainPaintableEffect + { + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + int x, y; + + if (rz < 0) { + double sum = 0.0; + double step2 = 0.0; + duration = 0.009; //MCP Should be read from ini file + + + // compute delta map + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); + + if (z > 0) // add in non-zero amount + { + sum += map[x, y] * z; + step2 += z; + } + } + } + rz = sum / step2; + } + + + // blend in map + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (!mask[x,y]) + continue; + + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength) * duration; + + if (z > 0) // add in non-zero amount + { + if (z > 1.0) + z = 1.0; + + map[x, y] = (map[x, y] * (1.0 - z)) + (rz * z); + } + + double delta = rz - map[x, y]; + if (Math.Abs(delta) > 0.1) + delta *= 0.25; + + if (delta != 0) // add in non-zero amount + { + map[x, y] += delta; + } + + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/LowerSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/LowerSphere.cs new file mode 100644 index 0000000..8c40088 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/LowerSphere.cs @@ -0,0 +1,84 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + public class LowerSphere : ITerrainPaintableEffect + { + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + int s = (int) (Math.Pow(2, strength) + 0.5); + + int x; + int xFrom = (int)(rx-s+0.5); + int xTo = (int)(rx+s+0.5) + 1; + int yFrom = (int)(ry-s+0.5); + int yTo = (int)(ry+s+0.5) + 1; + + if (xFrom < 0) + xFrom = 0; + + if (yFrom < 0) + yFrom = 0; + + if (xTo > map.Width) + xTo = map.Width; + + if (yTo > map.Width) + yTo = map.Width; + + for (x = xFrom; x < xTo; x++) + { + int y; + for (y = yFrom; y < yTo; y++) + { + if (!mask[x,y]) + continue; + + // Calculate a cos-sphere and add it to the heighmap + double r = Math.Sqrt((x-rx) * (x-rx) + ((y-ry) * (y-ry))); + double z = Math.Cos(r * Math.PI / (s * 2)); + if (z > 0.0) + { + double newz = map[x, y] - z * duration; + if (newz < 0.0) + map[x, y] = 0.0; + else + map[x, y] = newz; + } + } + } + + } + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/NoiseSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/NoiseSphere.cs new file mode 100644 index 0000000..95a8c33 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/NoiseSphere.cs @@ -0,0 +1,67 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Framework; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + public class NoiseSphere : ITerrainPaintableEffect + { + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + int x; + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (!mask[x,y]) + continue; + + // Calculate a sphere and add it to the heighmap + double z = strength; + z *= z; + z -= ((x - rx) * (x - rx)) + ((y - ry) * (y - ry)); + + double noise = TerrainUtil.PerlinNoise2D(x / (double) Constants.RegionSize, y / (double) Constants.RegionSize, 8, 1.0); + + if (z > 0.0) + map[x, y] += noise * z * duration; + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/OlsenSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/OlsenSphere.cs new file mode 100644 index 0000000..1a2528a --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/OlsenSphere.cs @@ -0,0 +1,223 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + /// + /// Speed-Optimised Hybrid Erosion Brush + /// + /// As per Jacob Olsen's Paper + /// http://www.oddlabs.com/download/terrain_generation.pdf + /// + public class OlsenSphere : ITerrainPaintableEffect + { + private const double nConst = 1024.0; + private const NeighbourSystem type = NeighbourSystem.Moore; + + #region Supporting Functions + + private static int[] Neighbours(NeighbourSystem neighbourType, int index) + { + int[] coord = new int[2]; + + index++; + + switch (neighbourType) + { + case NeighbourSystem.Moore: + switch (index) + { + case 1: + coord[0] = -1; + coord[1] = -1; + break; + + case 2: + coord[0] = -0; + coord[1] = -1; + break; + + case 3: + coord[0] = +1; + coord[1] = -1; + break; + + case 4: + coord[0] = -1; + coord[1] = -0; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + case 6: + coord[0] = +1; + coord[1] = -0; + break; + + case 7: + coord[0] = -1; + coord[1] = +1; + break; + + case 8: + coord[0] = -0; + coord[1] = +1; + break; + + case 9: + coord[0] = +1; + coord[1] = +1; + break; + + default: + break; + } + break; + + case NeighbourSystem.VonNeumann: + switch (index) + { + case 1: + coord[0] = 0; + coord[1] = -1; + break; + + case 2: + coord[0] = -1; + coord[1] = 0; + break; + + case 3: + coord[0] = +1; + coord[1] = 0; + break; + + case 4: + coord[0] = 0; + coord[1] = +1; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + default: + break; + } + break; + } + + return coord; + } + + private enum NeighbourSystem + { + Moore, + VonNeumann + } ; + + #endregion + + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + int x; + + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (!mask[x,y]) + continue; + + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); + + if (z > 0) // add in non-zero amount + { + const int NEIGHBOUR_ME = 4; + const int NEIGHBOUR_MAX = 9; + + double max = Double.MinValue; + int loc = 0; + + + for (int j = 0; j < NEIGHBOUR_MAX; j++) + { + if (j != NEIGHBOUR_ME) + { + int[] coords = Neighbours(type, j); + + coords[0] += x; + coords[1] += y; + + if (coords[0] > map.Width - 1) + continue; + if (coords[1] > map.Height - 1) + continue; + if (coords[0] < 0) + continue; + if (coords[1] < 0) + continue; + + double cellmax = map[x, y] - map[coords[0], coords[1]]; + if (cellmax > max) + { + max = cellmax; + loc = j; + } + } + } + + double T = nConst / ((map.Width + map.Height) / 2.0); + // Apply results + if (0 < max && max <= T) + { + int[] maxCoords = Neighbours(type, loc); + double heightDelta = 0.5 * max * z * duration; + map[x, y] -= heightDelta; + map[x + maxCoords[0], y + maxCoords[1]] += heightDelta; + } + } + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RaiseSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RaiseSphere.cs new file mode 100644 index 0000000..c53bb7d --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RaiseSphere.cs @@ -0,0 +1,80 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + public class RaiseSphere : ITerrainPaintableEffect + { + #region ITerrainPaintableEffect Members + + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + int s = (int) (Math.Pow(2, strength) + 0.5); + + int x; + int xFrom = (int)(rx-s+0.5); + int xTo = (int)(rx+s+0.5) + 1; + int yFrom = (int)(ry-s+0.5); + int yTo = (int)(ry+s+0.5) + 1; + + if (xFrom < 0) + xFrom = 0; + + if (yFrom < 0) + yFrom = 0; + + if (xTo > map.Width) + xTo = map.Width; + + if (yTo > map.Width) + yTo = map.Width; + + for (x = xFrom; x < xTo; x++) + { + int y; + for (y = yFrom; y < yTo; y++) + { + if (!mask[x,y]) + continue; + + // Calculate a cos-sphere and add it to the heighmap + double r = Math.Sqrt((x-rx) * (x-rx) + ((y-ry) * (y-ry))); + double z = Math.Cos(r * Math.PI / (s * 2)); + if (z > 0.0) + map[x, y] += z * duration; + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RevertSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RevertSphere.cs new file mode 100644 index 0000000..4ed8a13 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/RevertSphere.cs @@ -0,0 +1,80 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + public class RevertSphere : ITerrainPaintableEffect + { + private readonly ITerrainChannel m_revertmap; + + public RevertSphere(ITerrainChannel revertmap) + { + m_revertmap = revertmap; + } + + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + duration = 0.03; //MCP Should be read from ini file + + if (duration > 1.0) + duration = 1.0; + if (duration < 0) + return; + + int x; + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (!mask[x,y]) + continue; + + // Calculate a sphere and add it to the heighmap + double z = strength; + z *= z; + z -= ((x - rx) * (x - rx)) + ((y - ry) * (y - ry)); + + if (z > 0.0) + { + z *= duration; + map[x, y] = (map[x, y] * (1.0 - z)) + (m_revertmap[x, y] * z); + } + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/SmoothSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/SmoothSphere.cs new file mode 100644 index 0000000..6636d8f --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/SmoothSphere.cs @@ -0,0 +1,100 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + public class SmoothSphere : ITerrainPaintableEffect + { + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + int x, y; + double[,] tweak = new double[map.Width,map.Height]; + + double area = strength; + double step = strength / 4.0; + duration = 0.03; //MCP Should be read from ini file + + + // compute delta map + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); + + if (z > 0) // add in non-zero amount + { + double average = 0.0; + int avgsteps = 0; + + double n; + for (n = 0.0 - area; n < area; n += step) + { + double l; + for (l = 0.0 - area; l < area; l += step) + { + avgsteps++; + average += TerrainUtil.GetBilinearInterpolate(x + n, y + l, map); + } + } + tweak[x, y] = average / avgsteps; + } + } + } + // blend in map + for (x = 0; x < map.Width; x++) + { + for (y = 0; y < map.Height; y++) + { + if (!mask[x,y]) + continue; + + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); + + if (z > 0) // add in non-zero amount + { + double da = z; + double a = (map[x, y] - tweak[x, y]) * da; + double newz = map[x, y] - (a * duration); + + if (newz > 0.0) + map[x, y] = newz; + } + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/WeatherSphere.cs b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/WeatherSphere.cs new file mode 100644 index 0000000..6b00cc8 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/WeatherSphere.cs @@ -0,0 +1,211 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; + +namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes +{ + /// + /// Thermal Weathering Paint Brush + /// + public class WeatherSphere : ITerrainPaintableEffect + { + private const double talus = 0.2; + private const NeighbourSystem type = NeighbourSystem.Moore; + + #region Supporting Functions + + private static int[] Neighbours(NeighbourSystem neighbourType, int index) + { + int[] coord = new int[2]; + + index++; + + switch (neighbourType) + { + case NeighbourSystem.Moore: + switch (index) + { + case 1: + coord[0] = -1; + coord[1] = -1; + break; + + case 2: + coord[0] = -0; + coord[1] = -1; + break; + + case 3: + coord[0] = +1; + coord[1] = -1; + break; + + case 4: + coord[0] = -1; + coord[1] = -0; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + case 6: + coord[0] = +1; + coord[1] = -0; + break; + + case 7: + coord[0] = -1; + coord[1] = +1; + break; + + case 8: + coord[0] = -0; + coord[1] = +1; + break; + + case 9: + coord[0] = +1; + coord[1] = +1; + break; + + default: + break; + } + break; + + case NeighbourSystem.VonNeumann: + switch (index) + { + case 1: + coord[0] = 0; + coord[1] = -1; + break; + + case 2: + coord[0] = -1; + coord[1] = 0; + break; + + case 3: + coord[0] = +1; + coord[1] = 0; + break; + + case 4: + coord[0] = 0; + coord[1] = +1; + break; + + case 5: + coord[0] = -0; + coord[1] = -0; + break; + + default: + break; + } + break; + } + + return coord; + } + + private enum NeighbourSystem + { + Moore, + VonNeumann + } ; + + #endregion + + #region ITerrainPaintableEffect Members + + public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) + { + strength = TerrainUtil.MetersToSphericalStrength(strength); + + int x; + + for (x = 0; x < map.Width; x++) + { + int y; + for (y = 0; y < map.Height; y++) + { + if (!mask[x,y]) + continue; + + double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); + + if (z > 0) // add in non-zero amount + { + const int NEIGHBOUR_ME = 4; + const int NEIGHBOUR_MAX = 9; + + for (int j = 0; j < NEIGHBOUR_MAX; j++) + { + if (j != NEIGHBOUR_ME) + { + int[] coords = Neighbours(type, j); + + coords[0] += x; + coords[1] += y; + + if (coords[0] > map.Width - 1) + continue; + if (coords[1] > map.Height - 1) + continue; + if (coords[0] < 0) + continue; + if (coords[1] < 0) + continue; + + double heightF = map[x, y]; + double target = map[coords[0], coords[1]]; + + if (target > heightF + talus) + { + double calc = duration * ((target - heightF) - talus) * z; + heightF += calc; + target -= calc; + } + + map[x, y] = heightF; + map[coords[0], coords[1]] = target; + } + } + } + } + } + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/TerrainException.cs b/OpenSim/Region/CoreModules/World/Terrain/TerrainException.cs new file mode 100644 index 0000000..ff9b8ec --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/TerrainException.cs @@ -0,0 +1,46 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public class TerrainException : Exception + { + public TerrainException() + { + } + + public TerrainException(string msg) : base(msg) + { + } + + public TerrainException(string msg, Exception e) : base(msg, e) + { + } + } +} \ No newline at end of file diff --git a/OpenSim/Region/CoreModules/World/Terrain/TerrainModule.cs b/OpenSim/Region/CoreModules/World/Terrain/TerrainModule.cs new file mode 100644 index 0000000..9de7338 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/TerrainModule.cs @@ -0,0 +1,1001 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using System.Collections.Generic; +using System.IO; +using System.Reflection; +using OpenMetaverse; +using log4net; +using Nini.Config; +using OpenSim.Framework; +using OpenSim.Region.Framework.Interfaces; +using OpenSim.Region.Framework.Scenes; +using OpenSim.Region.CoreModules.Framework.InterfaceCommander; +using OpenSim.Region.CoreModules.World.Terrain.FileLoaders; +using OpenSim.Region.CoreModules.World.Terrain.FloodBrushes; +using OpenSim.Region.CoreModules.World.Terrain.PaintBrushes; + +namespace OpenSim.Region.CoreModules.World.Terrain +{ + public class TerrainModule : IRegionModule, ICommandableModule, ITerrainModule + { + #region StandardTerrainEffects enum + + /// + /// A standard set of terrain brushes and effects recognised by viewers + /// + public enum StandardTerrainEffects : byte + { + Flatten = 0, + Raise = 1, + Lower = 2, + Smooth = 3, + Noise = 4, + Revert = 5, + + // Extended brushes + Erode = 255, + Weather = 254, + Olsen = 253 + } + + #endregion + + private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); + + private readonly Commander m_commander = new Commander("terrain"); + + private readonly Dictionary m_floodeffects = + new Dictionary(); + + private readonly Dictionary m_loaders = new Dictionary(); + + private readonly Dictionary m_painteffects = + new Dictionary(); + + private ITerrainChannel m_channel; + private Dictionary m_plugineffects; + private ITerrainChannel m_revert; + private Scene m_scene; + private bool m_tainted; + + #region ICommandableModule Members + + public ICommander CommandInterface + { + get { return m_commander; } + } + + #endregion + + #region IRegionModule Members + + /// + /// Creates and initialises a terrain module for a region + /// + /// Region initialising + /// Config for the region + public void Initialise(Scene scene, IConfigSource config) + { + m_scene = scene; + + // Install terrain module in the simulator + if (m_scene.Heightmap == null) + { + lock (m_scene) + { + m_channel = new TerrainChannel(); + m_scene.Heightmap = m_channel; + m_revert = new TerrainChannel(); + UpdateRevertMap(); + } + } + else + { + m_channel = m_scene.Heightmap; + m_revert = new TerrainChannel(); + UpdateRevertMap(); + } + + m_scene.RegisterModuleInterface(this); + m_scene.EventManager.OnNewClient += EventManager_OnNewClient; + m_scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole; + m_scene.EventManager.OnTerrainTick += EventManager_OnTerrainTick; + } + + /// + /// Enables terrain module when called + /// + public void PostInitialise() + { + InstallDefaultEffects(); + InstallInterfaces(); + LoadPlugins(); + } + + public void Close() + { + } + + public string Name + { + get { return "TerrainModule"; } + } + + public bool IsSharedModule + { + get { return false; } + } + + #endregion + + #region ITerrainModule Members + + /// + /// Loads a terrain file from disk and installs it in the scene. + /// + /// Filename to terrain file. Type is determined by extension. + public void LoadFromFile(string filename) + { + foreach (KeyValuePair loader in m_loaders) + { + if (filename.EndsWith(loader.Key)) + { + lock (m_scene) + { + try + { + ITerrainChannel channel = loader.Value.LoadFile(filename); + if (channel.Width != Constants.RegionSize || channel.Height != Constants.RegionSize) + { + // TerrainChannel expects a RegionSize x RegionSize map, currently + throw new ArgumentException(String.Format("wrong size, use a file with size {0} x {1}", + Constants.RegionSize, Constants.RegionSize)); + } + m_log.DebugFormat("[TERRAIN]: Loaded terrain, wd/ht: {0}/{1}", channel.Width, channel.Height); + m_scene.Heightmap = channel; + m_channel = channel; + UpdateRevertMap(); + } + catch (NotImplementedException) + { + m_log.Error("[TERRAIN]: Unable to load heightmap, the " + loader.Value + + " parser does not support file loading. (May be save only)"); + throw new TerrainException(String.Format("unable to load heightmap: parser {0} does not support loading", loader.Value)); + } + catch (FileNotFoundException) + { + m_log.Error( + "[TERRAIN]: Unable to load heightmap, file not found. (A directory permissions error may also cause this)"); + throw new TerrainException( + String.Format("unable to load heightmap: file {0} not found (or permissions do not allow access", filename)); + } + catch (ArgumentException e) + { + m_log.ErrorFormat("[TERRAIN]: Unable to load heightmap: {0}", e.Message); + throw new TerrainException( + String.Format("Unable to load heightmap: {0}", e.Message)); + } + } + CheckForTerrainUpdates(); + m_log.Info("[TERRAIN]: File (" + filename + ") loaded successfully"); + return; + } + } + + m_log.Error("[TERRAIN]: Unable to load heightmap, no file loader available for that format."); + throw new TerrainException(String.Format("unable to load heightmap from file {0}: no loader available for that format", filename)); + } + + /// + /// Saves the current heightmap to a specified file. + /// + /// The destination filename + public void SaveToFile(string filename) + { + try + { + foreach (KeyValuePair loader in m_loaders) + { + if (filename.EndsWith(loader.Key)) + { + loader.Value.SaveFile(filename, m_channel); + return; + } + } + } + catch (NotImplementedException) + { + m_log.Error("Unable to save to " + filename + ", saving of this file format has not been implemented."); + throw new TerrainException(String.Format("Unable to save heightmap: saving of this file format not implemented")); + } + } + + /// + /// Loads a terrain file from a stream and installs it in the scene. + /// + /// Filename to terrain file. Type is determined by extension. + /// + public void LoadFromStream(string filename, Stream stream) + { + foreach (KeyValuePair loader in m_loaders) + { + if (@filename.EndsWith(loader.Key)) + { + lock (m_scene) + { + try + { + ITerrainChannel channel = loader.Value.LoadStream(stream); + m_scene.Heightmap = channel; + m_channel = channel; + UpdateRevertMap(); + } + catch (NotImplementedException) + { + m_log.Error("[TERRAIN]: Unable to load heightmap, the " + loader.Value + + " parser does not support file loading. (May be save only)"); + throw new TerrainException(String.Format("unable to load heightmap: parser {0} does not support loading", loader.Value)); + } + } + + CheckForTerrainUpdates(); + m_log.Info("[TERRAIN]: File (" + filename + ") loaded successfully"); + return; + } + } + m_log.Error("[TERRAIN]: Unable to load heightmap, no file loader available for that format."); + throw new TerrainException(String.Format("unable to load heightmap from file {0}: no loader available for that format", filename)); + } + + /// + /// Modify Land + /// + /// Land-position (X,Y,0) + /// The size of the brush (0=small, 1=medium, 2=large) + /// 0=LAND_LEVEL, 1=LAND_RAISE, 2=LAND_LOWER, 3=LAND_SMOOTH, 4=LAND_NOISE, 5=LAND_REVERT + /// UUID of script-owner + public void ModifyTerrain(UUID user, Vector3 pos, byte size, byte action, UUID agentId) + { + client_OnModifyTerrain(user, (float)pos.Z, (float)0.25, size, action, pos.Y, pos.X, pos.Y, pos.X, agentId); + } + + /// + /// Saves the current heightmap to a specified stream. + /// + /// The destination filename. Used here only to identify the image type + /// + public void SaveToStream(string filename, Stream stream) + { + try + { + foreach (KeyValuePair loader in m_loaders) + { + if (filename.EndsWith(loader.Key)) + { + loader.Value.SaveStream(stream, m_channel); + return; + } + } + } + catch (NotImplementedException) + { + m_log.Error("Unable to save to " + filename + ", saving of this file format has not been implemented."); + throw new TerrainException(String.Format("Unable to save heightmap: saving of this file format not implemented")); + } + } + + #region Plugin Loading Methods + + private void LoadPlugins() + { + m_plugineffects = new Dictionary(); + // Load the files in the Terrain/ dir + string[] files = Directory.GetFiles("Terrain"); + foreach (string file in files) + { + m_log.Info("Loading effects in " + file); + try + { + Assembly library = Assembly.LoadFrom(file); + foreach (Type pluginType in library.GetTypes()) + { + try + { + if (pluginType.IsAbstract || pluginType.IsNotPublic) + continue; + + string typeName = pluginType.Name; + + if (pluginType.GetInterface("ITerrainEffect", false) != null) + { + ITerrainEffect terEffect = (ITerrainEffect) Activator.CreateInstance(library.GetType(pluginType.ToString())); + + InstallPlugin(typeName, terEffect); + } + else if (pluginType.GetInterface("ITerrainLoader", false) != null) + { + ITerrainLoader terLoader = (ITerrainLoader) Activator.CreateInstance(library.GetType(pluginType.ToString())); + m_loaders[terLoader.FileExtension] = terLoader; + m_log.Info("L ... " + typeName); + } + } + catch (AmbiguousMatchException) + { + } + } + } + catch (BadImageFormatException) + { + } + } + } + + public void InstallPlugin(string pluginName, ITerrainEffect effect) + { + lock (m_plugineffects) + { + if (!m_plugineffects.ContainsKey(pluginName)) + { + m_plugineffects.Add(pluginName, effect); + m_log.Info("E ... " + pluginName); + } + else + { + m_plugineffects[pluginName] = effect; + m_log.Warn("E ... " + pluginName + " (Replaced)"); + } + } + } + + #endregion + + #endregion + + /// + /// Installs into terrain module the standard suite of brushes + /// + private void InstallDefaultEffects() + { + // Draggable Paint Brush Effects + m_painteffects[StandardTerrainEffects.Raise] = new RaiseSphere(); + m_painteffects[StandardTerrainEffects.Lower] = new LowerSphere(); + m_painteffects[StandardTerrainEffects.Smooth] = new SmoothSphere(); + m_painteffects[StandardTerrainEffects.Noise] = new NoiseSphere(); + m_painteffects[StandardTerrainEffects.Flatten] = new FlattenSphere(); + m_painteffects[StandardTerrainEffects.Revert] = new RevertSphere(m_revert); + m_painteffects[StandardTerrainEffects.Erode] = new ErodeSphere(); + m_painteffects[StandardTerrainEffects.Weather] = new WeatherSphere(); + m_painteffects[StandardTerrainEffects.Olsen] = new OlsenSphere(); + + // Area of effect selection effects + m_floodeffects[StandardTerrainEffects.Raise] = new RaiseArea(); + m_floodeffects[StandardTerrainEffects.Lower] = new LowerArea(); + m_floodeffects[StandardTerrainEffects.Smooth] = new SmoothArea(); + m_floodeffects[StandardTerrainEffects.Noise] = new NoiseArea(); + m_floodeffects[StandardTerrainEffects.Flatten] = new FlattenArea(); + m_floodeffects[StandardTerrainEffects.Revert] = new RevertArea(m_revert); + + // Filesystem load/save loaders + m_loaders[".r32"] = new RAW32(); + m_loaders[".f32"] = m_loaders[".r32"]; + m_loaders[".ter"] = new Terragen(); + m_loaders[".raw"] = new LLRAW(); + m_loaders[".jpg"] = new JPEG(); + m_loaders[".jpeg"] = m_loaders[".jpg"]; + m_loaders[".bmp"] = new BMP(); + m_loaders[".png"] = new PNG(); + m_loaders[".gif"] = new GIF(); + m_loaders[".tif"] = new TIFF(); + m_loaders[".tiff"] = m_loaders[".tif"]; + } + + /// + /// Saves the current state of the region into the revert map buffer. + /// + public void UpdateRevertMap() + { + int x; + for (x = 0; x < m_channel.Width; x++) + { + int y; + for (y = 0; y < m_channel.Height; y++) + { + m_revert[x, y] = m_channel[x, y]; + } + } + } + + /// + /// Loads a tile from a larger terrain file and installs it into the region. + /// + /// The terrain file to load + /// The width of the file in units + /// The height of the file in units + /// Where to begin our slice + /// Where to begin our slice + public void LoadFromFile(string filename, int fileWidth, int fileHeight, int fileStartX, int fileStartY) + { + int offsetX = (int) m_scene.RegionInfo.RegionLocX - fileStartX; + int offsetY = (int) m_scene.RegionInfo.RegionLocY - fileStartY; + + if (offsetX >= 0 && offsetX < fileWidth && offsetY >= 0 && offsetY < fileHeight) + { + // this region is included in the tile request + foreach (KeyValuePair loader in m_loaders) + { + if (filename.EndsWith(loader.Key)) + { + lock (m_scene) + { + ITerrainChannel channel = loader.Value.LoadFile(filename, offsetX, offsetY, + fileWidth, fileHeight, + (int) Constants.RegionSize, + (int) Constants.RegionSize); + m_scene.Heightmap = channel; + m_channel = channel; + UpdateRevertMap(); + } + return; + } + } + } + } + + /// + /// Performs updates to the region periodically, synchronising physics and other heightmap aware sections + /// + private void EventManager_OnTerrainTick() + { + if (m_tainted) + { + m_tainted = false; + m_scene.PhysicsScene.SetTerrain(m_channel.GetFloatsSerialised()); + m_scene.SaveTerrain(); + + // Clients who look at the map will never see changes after they looked at the map, so i've commented this out. + //m_scene.CreateTerrainTexture(true); + } + } + + /// + /// Processes commandline input. Do not call directly. + /// + /// Commandline arguments + private void EventManager_OnPluginConsole(string[] args) + { + if (args[0] == "terrain") + { + if (args.Length == 1) + { + m_commander.ProcessConsoleCommand("help", new string[0]); + return; + } + + string[] tmpArgs = new string[args.Length - 2]; + int i; + for (i = 2; i < args.Length; i++) + tmpArgs[i - 2] = args[i]; + + m_commander.ProcessConsoleCommand(args[1], tmpArgs); + } + } + + /// + /// Installs terrain brush hook to IClientAPI + /// + /// + private void EventManager_OnNewClient(IClientAPI client) + { + client.OnModifyTerrain += client_OnModifyTerrain; + client.OnBakeTerrain += client_OnBakeTerrain; + } + + /// + /// Checks to see if the terrain has been modified since last check + /// but won't attempt to limit those changes to the limits specified in the estate settings + /// currently invoked by the command line operations in the region server only + /// + private void CheckForTerrainUpdates() + { + CheckForTerrainUpdates(false); + } + + /// + /// Checks to see if the terrain has been modified since last check. + /// If it has been modified, every all the terrain patches are sent to the client. + /// If the call is asked to respect the estate settings for terrain_raise_limit and + /// terrain_lower_limit, it will clamp terrain updates between these values + /// currently invoked by client_OnModifyTerrain only and not the Commander interfaces + /// should height map deltas be limited to the estate settings limits + /// + private void CheckForTerrainUpdates(bool respectEstateSettings) + { + bool shouldTaint = false; + float[] serialised = m_channel.GetFloatsSerialised(); + int x; + for (x = 0; x < m_channel.Width; x += Constants.TerrainPatchSize) + { + int y; + for (y = 0; y < m_channel.Height; y += Constants.TerrainPatchSize) + { + if (m_channel.Tainted(x, y)) + { + // if we should respect the estate settings then + // fixup and height deltas that don't respect them + if (respectEstateSettings && LimitChannelChanges(x, y)) + { + // this has been vetoed, so update + // what we are going to send to the client + serialised = m_channel.GetFloatsSerialised(); + } + + SendToClients(serialised, x, y); + shouldTaint = true; + } + } + } + if (shouldTaint) + { + m_tainted = true; + } + } + + /// + /// Checks to see height deltas in the tainted terrain patch at xStart ,yStart + /// are all within the current estate limits + /// true if changes were limited, false otherwise + /// + private bool LimitChannelChanges(int xStart, int yStart) + { + bool changesLimited = false; + double minDelta = m_scene.RegionInfo.RegionSettings.TerrainLowerLimit; + double maxDelta = m_scene.RegionInfo.RegionSettings.TerrainRaiseLimit; + + // loop through the height map for this patch and compare it against + // the revert map + for (int x = xStart; x < xStart + Constants.TerrainPatchSize; x++) + { + for (int y = yStart; y < yStart + Constants.TerrainPatchSize; y++) + { + + double requestedHeight = m_channel[x, y]; + double bakedHeight = m_revert[x, y]; + double requestedDelta = requestedHeight - bakedHeight; + + if (requestedDelta > maxDelta) + { + m_channel[x, y] = bakedHeight + maxDelta; + changesLimited = true; + } + else if (requestedDelta < minDelta) + { + m_channel[x, y] = bakedHeight + minDelta; //as lower is a -ve delta + changesLimited = true; + } + } + } + + return changesLimited; + } + + /// + /// Sends a copy of the current terrain to the scenes clients + /// + /// A copy of the terrain as a 1D float array of size w*h + /// The patch corner to send + /// The patch corner to send + private void SendToClients(float[] serialised, int x, int y) + { + m_scene.ForEachClient( + delegate(IClientAPI controller) + { controller.SendLayerData( + x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize, serialised); + } + ); + } + + private void client_OnModifyTerrain(UUID user, float height, float seconds, byte size, byte action, + float north, float west, float south, float east, UUID agentId) + { + bool god = m_scene.Permissions.IsGod(user); + bool allowed = false; + if (north == south && east == west) + { + if (m_painteffects.ContainsKey((StandardTerrainEffects) action)) + { + bool[,] allowMask = new bool[m_channel.Width,m_channel.Height]; + allowMask.Initialize(); + int n = size + 1; + if (n > 2) + n = 4; + + int zx = (int) (west + 0.5); + int zy = (int) (north + 0.5); + + int dx; + for (dx=-n; dx<=n; dx++) + { + int dy; + for (dy=-n; dy<=n; dy++) + { + int x = zx + dx; + int y = zy + dy; + if (x>=0 && y>=0 && x west) + { + if (y < north && y > south) + { + if (m_scene.Permissions.CanTerraformLand(agentId, new Vector3(x,y,0))) + { + fillArea[x, y] = true; + allowed = true; + } + } + } + } + } + + if (allowed) + { + m_floodeffects[(StandardTerrainEffects) action].FloodEffect( + m_channel, fillArea, size); + + CheckForTerrainUpdates(!god); //revert changes outside estate limits + } + } + else + { + m_log.Debug("Unknown terrain flood type " + action); + } + } + } + + private void client_OnBakeTerrain(IClientAPI remoteClient) + { + // Not a good permissions check (see client_OnModifyTerrain above), need to check the entire area. + // for now check a point in the centre of the region + + if (m_scene.Permissions.CanIssueEstateCommand(remoteClient.AgentId, true)) + { + InterfaceBakeTerrain(null); //bake terrain does not use the passed in parameter + } + } + + #region Console Commands + + private void InterfaceLoadFile(Object[] args) + { + LoadFromFile((string) args[0]); + CheckForTerrainUpdates(); + } + + private void InterfaceLoadTileFile(Object[] args) + { + LoadFromFile((string) args[0], + (int) args[1], + (int) args[2], + (int) args[3], + (int) args[4]); + CheckForTerrainUpdates(); + } + + private void InterfaceSaveFile(Object[] args) + { + SaveToFile((string) args[0]); + } + + private void InterfaceBakeTerrain(Object[] args) + { + UpdateRevertMap(); + } + + private void InterfaceRevertTerrain(Object[] args) + { + int x, y; + for (x = 0; x < m_channel.Width; x++) + for (y = 0; y < m_channel.Height; y++) + m_channel[x, y] = m_revert[x, y]; + + CheckForTerrainUpdates(); + } + + private void InterfaceFlipTerrain(Object[] args) + { + String direction = (String)args[0]; + + if (direction.ToLower().StartsWith("y")) + { + for (int x = 0; x < Constants.RegionSize; x++) + { + for (int y = 0; y < Constants.RegionSize / 2; y++) + { + double height = m_channel[x, y]; + double flippedHeight = m_channel[x, (int)Constants.RegionSize - 1 - y]; + m_channel[x, y] = flippedHeight; + m_channel[x, (int)Constants.RegionSize - 1 - y] = height; + + } + } + } + else if (direction.ToLower().StartsWith("x")) + { + for (int y = 0; y < Constants.RegionSize; y++) + { + for (int x = 0; x < Constants.RegionSize / 2; x++) + { + double height = m_channel[x, y]; + double flippedHeight = m_channel[(int)Constants.RegionSize - 1 - x, y]; + m_channel[x, y] = flippedHeight; + m_channel[(int)Constants.RegionSize - 1 - x, y] = height; + + } + } + } + else + { + m_log.Error("Unrecognised direction - need x or y"); + } + + + CheckForTerrainUpdates(); + } + + private void InterfaceElevateTerrain(Object[] args) + { + int x, y; + for (x = 0; x < m_channel.Width; x++) + for (y = 0; y < m_channel.Height; y++) + m_channel[x, y] += (double) args[0]; + CheckForTerrainUpdates(); + } + + private void InterfaceMultiplyTerrain(Object[] args) + { + int x, y; + for (x = 0; x < m_channel.Width; x++) + for (y = 0; y < m_channel.Height; y++) + m_channel[x, y] *= (double) args[0]; + CheckForTerrainUpdates(); + } + + private void InterfaceLowerTerrain(Object[] args) + { + int x, y; + for (x = 0; x < m_channel.Width; x++) + for (y = 0; y < m_channel.Height; y++) + m_channel[x, y] -= (double) args[0]; + CheckForTerrainUpdates(); + } + + private void InterfaceFillTerrain(Object[] args) + { + int x, y; + + for (x = 0; x < m_channel.Width; x++) + for (y = 0; y < m_channel.Height; y++) + m_channel[x, y] = (double) args[0]; + CheckForTerrainUpdates(); + } + + private void InterfaceShowDebugStats(Object[] args) + { + double max = Double.MinValue; + double min = double.MaxValue; + double sum = 0; + + int x; + for (x = 0; x < m_channel.Width; x++) + { + int y; + for (y = 0; y < m_channel.Height; y++) + { + sum += m_channel[x, y]; + if (max < m_channel[x, y]) + max = m_channel[x, y]; + if (min > m_channel[x, y]) + min = m_channel[x, y]; + } + } + + double avg = sum / (m_channel.Height * m_channel.Width); + + m_log.Info("Channel " + m_channel.Width + "x" + m_channel.Height); + m_log.Info("max/min/avg/sum: " + max + "/" + min + "/" + avg + "/" + sum); + } + + private void InterfaceEnableExperimentalBrushes(Object[] args) + { + if ((bool) args[0]) + { + m_painteffects[StandardTerrainEffects.Revert] = new WeatherSphere(); + m_painteffects[StandardTerrainEffects.Flatten] = new OlsenSphere(); + m_painteffects[StandardTerrainEffects.Smooth] = new ErodeSphere(); + } + else + { + InstallDefaultEffects(); + } + } + + private void InterfaceRunPluginEffect(Object[] args) + { + if ((string) args[0] == "list") + { + m_log.Info("List of loaded plugins"); + foreach (KeyValuePair kvp in m_plugineffects) + { + m_log.Info(kvp.Key); + } + return; + } + if ((string) args[0] == "reload") + { + LoadPlugins(); + return; + } + if (m_plugineffects.ContainsKey((string) args[0])) + { + m_plugineffects[(string) args[0]].RunEffect(m_channel); + CheckForTerrainUpdates(); + } + else + { + m_log.Warn("No such plugin effect loaded."); + } + } + + private void InstallInterfaces() + { + // Load / Save + string supportedFileExtensions = ""; + foreach (KeyValuePair loader in m_loaders) + supportedFileExtensions += " " + loader.Key + " (" + loader.Value + ")"; + + Command loadFromFileCommand = + new Command("load", CommandIntentions.COMMAND_HAZARDOUS, InterfaceLoadFile, "Loads a terrain from a specified file."); + loadFromFileCommand.AddArgument("filename", + "The file you wish to load from, the file extension determines the loader to be used. Supported extensions include: " + + supportedFileExtensions, "String"); + + Command saveToFileCommand = + new Command("save", CommandIntentions.COMMAND_NON_HAZARDOUS, InterfaceSaveFile, "Saves the current heightmap to a specified file."); + saveToFileCommand.AddArgument("filename", + "The destination filename for your heightmap, the file extension determines the format to save in. Supported extensions include: " + + supportedFileExtensions, "String"); + + Command loadFromTileCommand = + new Command("load-tile", CommandIntentions.COMMAND_HAZARDOUS, InterfaceLoadTileFile, "Loads a terrain from a section of a larger file."); + loadFromTileCommand.AddArgument("filename", + "The file you wish to load from, the file extension determines the loader to be used. Supported extensions include: " + + supportedFileExtensions, "String"); + loadFromTileCommand.AddArgument("file width", "The width of the file in tiles", "Integer"); + loadFromTileCommand.AddArgument("file height", "The height of the file in tiles", "Integer"); + loadFromTileCommand.AddArgument("minimum X tile", "The X region coordinate of the first section on the file", + "Integer"); + loadFromTileCommand.AddArgument("minimum Y tile", "The Y region coordinate of the first section on the file", + "Integer"); + + // Terrain adjustments + Command fillRegionCommand = + new Command("fill", CommandIntentions.COMMAND_HAZARDOUS, InterfaceFillTerrain, "Fills the current heightmap with a specified value."); + fillRegionCommand.AddArgument("value", "The numeric value of the height you wish to set your region to.", + "Double"); + + Command elevateCommand = + new Command("elevate", CommandIntentions.COMMAND_HAZARDOUS, InterfaceElevateTerrain, "Raises the current heightmap by the specified amount."); + elevateCommand.AddArgument("amount", "The amount of height to add to the terrain in meters.", "Double"); + + Command lowerCommand = + new Command("lower", CommandIntentions.COMMAND_HAZARDOUS, InterfaceLowerTerrain, "Lowers the current heightmap by the specified amount."); + lowerCommand.AddArgument("amount", "The amount of height to remove from the terrain in meters.", "Double"); + + Command multiplyCommand = + new Command("multiply", CommandIntentions.COMMAND_HAZARDOUS, InterfaceMultiplyTerrain, "Multiplies the heightmap by the value specified."); + multiplyCommand.AddArgument("value", "The value to multiply the heightmap by.", "Double"); + + Command bakeRegionCommand = + new Command("bake", CommandIntentions.COMMAND_HAZARDOUS, InterfaceBakeTerrain, "Saves the current terrain into the regions revert map."); + Command revertRegionCommand = + new Command("revert", CommandIntentions.COMMAND_HAZARDOUS, InterfaceRevertTerrain, "Loads the revert map terrain into the regions heightmap."); + + Command flipCommand = + new Command("flip", CommandIntentions.COMMAND_HAZARDOUS, InterfaceFlipTerrain, "Flips the current terrain about the X or Y axis"); + flipCommand.AddArgument("direction", "[x|y] the direction to flip the terrain in", "String"); + + // Debug + Command showDebugStatsCommand = + new Command("stats", CommandIntentions.COMMAND_STATISTICAL, InterfaceShowDebugStats, + "Shows some information about the regions heightmap for debugging purposes."); + + Command experimentalBrushesCommand = + new Command("newbrushes", CommandIntentions.COMMAND_HAZARDOUS, InterfaceEnableExperimentalBrushes, + "Enables experimental brushes which replace the standard terrain brushes. WARNING: This is a debug setting and may be removed at any time."); + experimentalBrushesCommand.AddArgument("Enabled?", "true / false - Enable new brushes", "Boolean"); + + //Plugins + Command pluginRunCommand = + new Command("effect", CommandIntentions.COMMAND_HAZARDOUS, InterfaceRunPluginEffect, "Runs a specified plugin effect"); + pluginRunCommand.AddArgument("name", "The plugin effect you wish to run, or 'list' to see all plugins", "String"); + + m_commander.RegisterCommand("load", loadFromFileCommand); + m_commander.RegisterCommand("load-tile", loadFromTileCommand); + m_commander.RegisterCommand("save", saveToFileCommand); + m_commander.RegisterCommand("fill", fillRegionCommand); + m_commander.RegisterCommand("elevate", elevateCommand); + m_commander.RegisterCommand("lower", lowerCommand); + m_commander.RegisterCommand("multiply", multiplyCommand); + m_commander.RegisterCommand("bake", bakeRegionCommand); + m_commander.RegisterCommand("revert", revertRegionCommand); + m_commander.RegisterCommand("newbrushes", experimentalBrushesCommand); + m_commander.RegisterCommand("stats", showDebugStatsCommand); + m_commander.RegisterCommand("effect", pluginRunCommand); + m_commander.RegisterCommand("flip", flipCommand); + + // Add this to our scene so scripts can call these functions + m_scene.RegisterModuleCommander(m_commander); + } + + #endregion + } +} diff --git a/OpenSim/Region/CoreModules/World/Terrain/Tests/TerrainTest.cs b/OpenSim/Region/CoreModules/World/Terrain/Tests/TerrainTest.cs new file mode 100644 index 0000000..e7f92d7 --- /dev/null +++ b/OpenSim/Region/CoreModules/World/Terrain/Tests/TerrainTest.cs @@ -0,0 +1,118 @@ +/* + * Copyright (c) Contributors, http://opensimulator.org/ + * See CONTRIBUTORS.TXT for a full list of copyright holders. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of the OpenSim Project nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +using System; +using NUnit.Framework; +using OpenSim.Region.Framework.Scenes; +using OpenSim.Region.CoreModules.World.Terrain.PaintBrushes; + +namespace OpenSim.Region.CoreModules.World.Terrain.Tests +{ + [TestFixture] + public class TerrainTest + { + [Test] + public void BrushTest() + { + bool[,] allowMask = new bool[256, 256]; + int x; + int y; + for (x=0; x<128; x++) + { + for (y=0; y<256; y++) + { + allowMask[x,y] = true; + } + } + + // + // Test RaiseSphere + // + TerrainChannel map = new TerrainChannel(256, 256); + ITerrainPaintableEffect effect = new RaiseSphere(); + + effect.PaintEffect(map, allowMask, 128.0, 128.0, -1.0, 2, 0.1); + Assert.That(map[127, 128] > 0.0, "Raise brush should raising value at this point (127,128)."); + Assert.That(map[124, 128] > 0.0, "Raise brush should raising value at this point (124,128)."); + Assert.That(map[123, 128] == 0.0, "Raise brush should not change value at this point (123,128)."); + Assert.That(map[128, 128] == 0.0, "Raise brush should not change value at this point (128,128)."); + Assert.That(map[0, 128] == 0.0, "Raise brush should not change value at this point (0,128)."); + + // + // Test LowerSphere + // + map = new TerrainChannel(256, 256); + for (x=0; x= 0.0, "Lower should not lowering value below 0.0 at this point (127,128)."); + Assert.That(map[127, 128] == 0.0, "Lower brush should lowering value to 0.0 at this point (127,128)."); + Assert.That(map[124, 128] < 1.0, "Lower brush should lowering value at this point (124,128)."); + Assert.That(map[123, 128] == 1.0, "Lower brush should not change value at this point (123,128)."); + Assert.That(map[128, 128] == 1.0, "Lower brush should not change value at this point (128,128)."); + Assert.That(map[0, 128] == 1.0, "Lower brush should not change value at this point (0,128)."); + } + + [Test] + public void TerrainChannelTest() + { + TerrainChannel x = new TerrainChannel(256, 256); + Assert.That(x[0, 0] == 0.0, "Terrain not initialising correctly."); + + x[0, 0] = 1.0; + Assert.That(x[0, 0] == 1.0, "Terrain not setting values correctly."); + + x[0, 0] = 0; + x[0, 0] += 5.0; + x[0, 0] -= 1.0; + Assert.That(x[0, 0] == 4.0, "Terrain addition/subtraction error."); + + x[0, 0] = Math.PI; + double[,] doublesExport = x.GetDoubles(); + Assert.That(doublesExport[0, 0] == Math.PI, "Export to double[,] array not working correctly."); + + x[0, 0] = 1.0; + float[] floatsExport = x.GetFloatsSerialised(); + Assert.That(floatsExport[0] == 1.0f, "Export to float[] not working correctly."); + + x[0, 0] = 1.0; + Assert.That(x.Tainted(0, 0), "Terrain channel tainting not working correctly."); + Assert.That(!x.Tainted(0, 0), "Terrain channel tainting not working correctly."); + + TerrainChannel y = x.Copy(); + Assert.That(!ReferenceEquals(x, y), "Terrain copy not duplicating correctly."); + Assert.That(!ReferenceEquals(x.GetDoubles(), y.GetDoubles()), "Terrain array not duplicating correctly."); + } + } +} -- cgit v1.1