OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
CookieCutter.cs
Go to the documentation of this file.
1 /*
2  * Copyright (c) Contributors, http://opensimulator.org/
3  * See CONTRIBUTORS.TXT for a full list of copyright holders.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the OpenSimulator Project nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 using System;
28 using OpenSim.Region.CoreModules.World.Terrain.PaintBrushes;
29 using OpenSim.Region.Framework.Interfaces;
30 using OpenSim.Region.Framework.Scenes;
31 using log4net;
32 using System.Reflection;
33 
34 namespace OpenSim.Region.CoreModules.World.Terrain.Effects
35 {
36  internal class CookieCutter : ITerrainEffect
37  {
38  private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
39 
40  #region ITerrainEffect Members
41 
42  public void RunEffect(ITerrainChannel map)
43  {
44  ITerrainPaintableEffect eroder = new WeatherSphere();
45 
46  bool[,] cliffMask = new bool[map.Width,map.Height];
47  bool[,] channelMask = new bool[map.Width,map.Height];
48  bool[,] smoothMask = new bool[map.Width,map.Height];
49  bool[,] allowMask = new bool[map.Width,map.Height];
50 
51  m_log.Info("S1");
52 
53  // Step one, generate rough mask
54  int x, y;
55  for (x = 0; x < map.Width; x++)
56  {
57  for (y = 0; y < map.Height; y++)
58  {
59  m_log.Info(".");
60  smoothMask[x, y] = true;
61  allowMask[x,y] = true;
62 
63  // Start underwater
64  map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 3, 0.25) * 5;
65  // Add a little height. (terrain should now be above water, mostly.)
66  map[x, y] += 20;
67 
68  const int channelsX = 4;
69  int channelWidth = (map.Width / channelsX / 4);
70  const int channelsY = 4;
71  int channelHeight = (map.Height / channelsY / 4);
72 
73  SetLowerChannel(map, cliffMask, channelMask, x, y, channelsX, channelWidth, map.Width, x);
74  SetLowerChannel(map, cliffMask, channelMask, x, y, channelsY, channelHeight, map.Height, y);
75  }
76  }
77 
78  m_log.Info("S2");
79  //smooth.FloodEffect(map, smoothMask, 4.0);
80 
81  m_log.Info("S3");
82  for (x = 0; x < map.Width; x++)
83  {
84  for (y = 0; y < map.Height; y++)
85  {
86  if (cliffMask[x, y])
87  eroder.PaintEffect(map, allowMask, x, y, -1, 4, 0.1,0,map.Width - 1,0,map.Height - 1);
88  }
89  }
90 
91  for (x = 0; x < map.Width; x += 2)
92  {
93  for (y = 0; y < map.Height; y += 2)
94  {
95  if (map[x, y] < 0.1)
96  map[x, y] = 0.1;
97  if (map[x, y] > 256)
98  map[x, y] = 256;
99  }
100  }
101  //smooth.FloodEffect(map, smoothMask, 4.0);
102  }
103 
104  #endregion
105 
106  private static void SetLowerChannel(ITerrainChannel map, bool[,] cliffMask, bool[,] channelMask, int x, int y, int numChannels, int channelWidth,
107  int mapSize, int rp)
108  {
109  for (int i = 0; i < numChannels; i++)
110  {
111  double distanceToLine = Math.Abs(rp - ((mapSize / numChannels) * i));
112 
113  if (distanceToLine < channelWidth)
114  {
115  if (channelMask[x, y])
116  return;
117 
118  // Remove channels
119  map[x, y] -= 10;
120  channelMask[x, y] = true;
121  }
122  if (distanceToLine < 1)
123  {
124  cliffMask[x, y] = true;
125  }
126  }
127  }
128  }
129 }