diff options
author | Adam Frisby | 2007-07-11 08:02:47 +0000 |
---|---|---|
committer | Adam Frisby | 2007-07-11 08:02:47 +0000 |
commit | 5c7ffdde0b9642a42e8f5987e06eb01220ff7776 (patch) | |
tree | 4b825dc642cb6eb9a060e54bf8d69288fbee4904 /Common/OpenSim.Framework/HeightMapGenHills.cs | |
parent | Who would have known that the only way of specifying utf-8 without preamble, ... (diff) | |
download | opensim-SC_OLD-5c7ffdde0b9642a42e8f5987e06eb01220ff7776.zip opensim-SC_OLD-5c7ffdde0b9642a42e8f5987e06eb01220ff7776.tar.gz opensim-SC_OLD-5c7ffdde0b9642a42e8f5987e06eb01220ff7776.tar.bz2 opensim-SC_OLD-5c7ffdde0b9642a42e8f5987e06eb01220ff7776.tar.xz |
* Wiping trunk in prep for Sugilite
Diffstat (limited to 'Common/OpenSim.Framework/HeightMapGenHills.cs')
-rw-r--r-- | Common/OpenSim.Framework/HeightMapGenHills.cs | 150 |
1 files changed, 0 insertions, 150 deletions
diff --git a/Common/OpenSim.Framework/HeightMapGenHills.cs b/Common/OpenSim.Framework/HeightMapGenHills.cs deleted file mode 100644 index 9777b82..0000000 --- a/Common/OpenSim.Framework/HeightMapGenHills.cs +++ /dev/null | |||
@@ -1,150 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://www.openmetaverse.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 | |||
29 | using System; | ||
30 | |||
31 | namespace OpenSim.Framework.Terrain | ||
32 | { | ||
33 | public class HeightmapGenHills | ||
34 | { | ||
35 | private Random Rand = new Random(); | ||
36 | private int NumHills; | ||
37 | private float HillMin; | ||
38 | private float HillMax; | ||
39 | private bool Island; | ||
40 | private float[] heightmap; | ||
41 | |||
42 | public float[] GenerateHeightmap(int numHills, float hillMin, float hillMax, bool island) | ||
43 | { | ||
44 | NumHills = numHills; | ||
45 | HillMin = hillMin; | ||
46 | HillMax = hillMax; | ||
47 | Island = island; | ||
48 | |||
49 | heightmap = new float[256 * 256]; | ||
50 | |||
51 | for (int i = 0; i < numHills; i++) | ||
52 | { | ||
53 | AddHill(); | ||
54 | } | ||
55 | |||
56 | Normalize(); | ||
57 | |||
58 | return heightmap; | ||
59 | } | ||
60 | |||
61 | private void AddHill() | ||
62 | { | ||
63 | float x, y; | ||
64 | float radius = RandomRange(HillMin, HillMax); | ||
65 | |||
66 | if (Island) | ||
67 | { | ||
68 | // Which direction from the center of the map the hill is placed | ||
69 | float theta = RandomRange(0, 6.28f); | ||
70 | |||
71 | // How far from the center of the map to place the hill. The radius | ||
72 | // is subtracted from the range to prevent any part of the hill from | ||
73 | // reaching the edge of the map | ||
74 | float distance = RandomRange(radius / 2.0f, 128.0f - radius); | ||
75 | |||
76 | x = 128.0f + (float)Math.Cos(theta) * distance; | ||
77 | y = 128.0f + (float)Math.Sin(theta) * distance; | ||
78 | } | ||
79 | else | ||
80 | { | ||
81 | x = RandomRange(-radius, 256.0f + radius); | ||
82 | y = RandomRange(-radius, 256.0f + radius); | ||
83 | } | ||
84 | |||
85 | float radiusSq = radius * radius; | ||
86 | float distSq; | ||
87 | float height; | ||
88 | |||
89 | int xMin = (int)(x - radius) - 1; | ||
90 | int xMax = (int)(x + radius) + 1; | ||
91 | if (xMin < 0) xMin = 0; | ||
92 | if (xMax > 255) xMax = 255; | ||
93 | |||
94 | int yMin = (int)(y - radius) - 1; | ||
95 | int yMax = (int)(y + radius) + 1; | ||
96 | if (yMin < 0) yMin = 0; | ||
97 | if (yMax > 255) yMax = 255; | ||
98 | |||
99 | // Loop through each affected cell and determine the height at that point | ||
100 | for (int v = yMin; v <= yMax; ++v) | ||
101 | { | ||
102 | float fv = (float)v; | ||
103 | |||
104 | for (int h = xMin; h <= xMax; ++h) | ||
105 | { | ||
106 | float fh = (float)h; | ||
107 | |||
108 | // Determine how far from the center of this hill this point is | ||
109 | distSq = (x - fh) * (x - fh) + (y - fv) * (y - fv); | ||
110 | height = radiusSq - distSq; | ||
111 | |||
112 | // Don't add negative hill values | ||
113 | if (height > 0.0f) heightmap[h + v * 256] += height; | ||
114 | } | ||
115 | } | ||
116 | } | ||
117 | |||
118 | private void Normalize() | ||
119 | { | ||
120 | float min = heightmap[0]; | ||
121 | float max = heightmap[0]; | ||
122 | |||
123 | for (int x = 0; x < 256; x++) | ||
124 | { | ||
125 | for (int y = 0; y < 256; y++) | ||
126 | { | ||
127 | if (heightmap[x + y * 256] < min) min = heightmap[x + y * 256]; | ||
128 | if (heightmap[x + y * 256] > max) max = heightmap[x + y * 256]; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | // Avoid a rare divide by zero | ||
133 | if (min != max) | ||
134 | { | ||
135 | for (int x = 0; x < 256; x++) | ||
136 | { | ||
137 | for (int y = 0; y < 256; y++) | ||
138 | { | ||
139 | heightmap[x + y * 256] = ((heightmap[x + y * 256] - min) / (max - min)) * (HillMax - HillMin); | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | } | ||
144 | |||
145 | private float RandomRange(float min, float max) | ||
146 | { | ||
147 | return (float)Rand.NextDouble() * (max - min) + min; | ||
148 | } | ||
149 | } | ||
150 | } | ||