diff options
Diffstat (limited to 'OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/OlsenSphere.cs')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Terrain/PaintBrushes/OlsenSphere.cs | 223 |
1 files changed, 223 insertions, 0 deletions
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 @@ | |||
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 OpenSim 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 | |||
28 | using System; | ||
29 | using OpenSim.Region.Framework.Interfaces; | ||
30 | using OpenSim.Region.Framework.Scenes; | ||
31 | |||
32 | namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes | ||
33 | { | ||
34 | /// <summary> | ||
35 | /// Speed-Optimised Hybrid Erosion Brush | ||
36 | /// | ||
37 | /// As per Jacob Olsen's Paper | ||
38 | /// http://www.oddlabs.com/download/terrain_generation.pdf | ||
39 | /// </summary> | ||
40 | public class OlsenSphere : ITerrainPaintableEffect | ||
41 | { | ||
42 | private const double nConst = 1024.0; | ||
43 | private const NeighbourSystem type = NeighbourSystem.Moore; | ||
44 | |||
45 | #region Supporting Functions | ||
46 | |||
47 | private static int[] Neighbours(NeighbourSystem neighbourType, int index) | ||
48 | { | ||
49 | int[] coord = new int[2]; | ||
50 | |||
51 | index++; | ||
52 | |||
53 | switch (neighbourType) | ||
54 | { | ||
55 | case NeighbourSystem.Moore: | ||
56 | switch (index) | ||
57 | { | ||
58 | case 1: | ||
59 | coord[0] = -1; | ||
60 | coord[1] = -1; | ||
61 | break; | ||
62 | |||
63 | case 2: | ||
64 | coord[0] = -0; | ||
65 | coord[1] = -1; | ||
66 | break; | ||
67 | |||
68 | case 3: | ||
69 | coord[0] = +1; | ||
70 | coord[1] = -1; | ||
71 | break; | ||
72 | |||
73 | case 4: | ||
74 | coord[0] = -1; | ||
75 | coord[1] = -0; | ||
76 | break; | ||
77 | |||
78 | case 5: | ||
79 | coord[0] = -0; | ||
80 | coord[1] = -0; | ||
81 | break; | ||
82 | |||
83 | case 6: | ||
84 | coord[0] = +1; | ||
85 | coord[1] = -0; | ||
86 | break; | ||
87 | |||
88 | case 7: | ||
89 | coord[0] = -1; | ||
90 | coord[1] = +1; | ||
91 | break; | ||
92 | |||
93 | case 8: | ||
94 | coord[0] = -0; | ||
95 | coord[1] = +1; | ||
96 | break; | ||
97 | |||
98 | case 9: | ||
99 | coord[0] = +1; | ||
100 | coord[1] = +1; | ||
101 | break; | ||
102 | |||
103 | default: | ||
104 | break; | ||
105 | } | ||
106 | break; | ||
107 | |||
108 | case NeighbourSystem.VonNeumann: | ||
109 | switch (index) | ||
110 | { | ||
111 | case 1: | ||
112 | coord[0] = 0; | ||
113 | coord[1] = -1; | ||
114 | break; | ||
115 | |||
116 | case 2: | ||
117 | coord[0] = -1; | ||
118 | coord[1] = 0; | ||
119 | break; | ||
120 | |||
121 | case 3: | ||
122 | coord[0] = +1; | ||
123 | coord[1] = 0; | ||
124 | break; | ||
125 | |||
126 | case 4: | ||
127 | coord[0] = 0; | ||
128 | coord[1] = +1; | ||
129 | break; | ||
130 | |||
131 | case 5: | ||
132 | coord[0] = -0; | ||
133 | coord[1] = -0; | ||
134 | break; | ||
135 | |||
136 | default: | ||
137 | break; | ||
138 | } | ||
139 | break; | ||
140 | } | ||
141 | |||
142 | return coord; | ||
143 | } | ||
144 | |||
145 | private enum NeighbourSystem | ||
146 | { | ||
147 | Moore, | ||
148 | VonNeumann | ||
149 | } ; | ||
150 | |||
151 | #endregion | ||
152 | |||
153 | #region ITerrainPaintableEffect Members | ||
154 | |||
155 | public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration) | ||
156 | { | ||
157 | strength = TerrainUtil.MetersToSphericalStrength(strength); | ||
158 | |||
159 | int x; | ||
160 | |||
161 | for (x = 0; x < map.Width; x++) | ||
162 | { | ||
163 | int y; | ||
164 | for (y = 0; y < map.Height; y++) | ||
165 | { | ||
166 | if (!mask[x,y]) | ||
167 | continue; | ||
168 | |||
169 | double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength); | ||
170 | |||
171 | if (z > 0) // add in non-zero amount | ||
172 | { | ||
173 | const int NEIGHBOUR_ME = 4; | ||
174 | const int NEIGHBOUR_MAX = 9; | ||
175 | |||
176 | double max = Double.MinValue; | ||
177 | int loc = 0; | ||
178 | |||
179 | |||
180 | for (int j = 0; j < NEIGHBOUR_MAX; j++) | ||
181 | { | ||
182 | if (j != NEIGHBOUR_ME) | ||
183 | { | ||
184 | int[] coords = Neighbours(type, j); | ||
185 | |||
186 | coords[0] += x; | ||
187 | coords[1] += y; | ||
188 | |||
189 | if (coords[0] > map.Width - 1) | ||
190 | continue; | ||
191 | if (coords[1] > map.Height - 1) | ||
192 | continue; | ||
193 | if (coords[0] < 0) | ||
194 | continue; | ||
195 | if (coords[1] < 0) | ||
196 | continue; | ||
197 | |||
198 | double cellmax = map[x, y] - map[coords[0], coords[1]]; | ||
199 | if (cellmax > max) | ||
200 | { | ||
201 | max = cellmax; | ||
202 | loc = j; | ||
203 | } | ||
204 | } | ||
205 | } | ||
206 | |||
207 | double T = nConst / ((map.Width + map.Height) / 2.0); | ||
208 | // Apply results | ||
209 | if (0 < max && max <= T) | ||
210 | { | ||
211 | int[] maxCoords = Neighbours(type, loc); | ||
212 | double heightDelta = 0.5 * max * z * duration; | ||
213 | map[x, y] -= heightDelta; | ||
214 | map[x + maxCoords[0], y + maxCoords[1]] += heightDelta; | ||
215 | } | ||
216 | } | ||
217 | } | ||
218 | } | ||
219 | } | ||
220 | |||
221 | #endregion | ||
222 | } | ||
223 | } | ||