aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/world/HeightmapGenHills.cs
diff options
context:
space:
mode:
authorjhurliman2007-03-07 05:09:18 +0000
committerjhurliman2007-03-07 05:09:18 +0000
commit0b6f8a02a7d0927031bf812429770d6ecc5f483a (patch)
tree6795f255e730e5cdfcf79191a0fd64734951d691 /src/world/HeightmapGenHills.cs
parentNEVER COMMIT WITHOUT A TEST COMPILE! (diff)
downloadopensim-SC_OLD-0b6f8a02a7d0927031bf812429770d6ecc5f483a.zip
opensim-SC_OLD-0b6f8a02a7d0927031bf812429770d6ecc5f483a.tar.gz
opensim-SC_OLD-0b6f8a02a7d0927031bf812429770d6ecc5f483a.tar.bz2
opensim-SC_OLD-0b6f8a02a7d0927031bf812429770d6ecc5f483a.tar.xz
* Updating libsecondlife.dll to the latest version with a working LayerData function
* Reformatting some source files * Adding a try/catch sanity check around a phoning home check to osgrid.org * Updating the MSVC project file * Converted LayerData generation to use the functions built in to libsecondlife * Removing unused BitPack.cs and TerrainDecoder.cs files * Added a basic terrain generator (hills algorithm)
Diffstat (limited to 'src/world/HeightmapGenHills.cs')
-rw-r--r--src/world/HeightmapGenHills.cs122
1 files changed, 122 insertions, 0 deletions
diff --git a/src/world/HeightmapGenHills.cs b/src/world/HeightmapGenHills.cs
new file mode 100644
index 0000000..12af005
--- /dev/null
+++ b/src/world/HeightmapGenHills.cs
@@ -0,0 +1,122 @@
1using System;
2
3namespace libsecondlife
4{
5 public class HeightmapGenHills
6 {
7 private Random Rand = new Random();
8 private int NumHills;
9 private float HillMin;
10 private float HillMax;
11 private bool Island;
12 private float[] heightmap;
13
14 public float[] GenerateHeightmap(int numHills, float hillMin, float hillMax, bool island)
15 {
16 NumHills = numHills;
17 HillMin = hillMin;
18 HillMax = hillMax;
19 Island = island;
20
21 heightmap = new float[256 * 256];
22
23 for (int i = 0; i < numHills; i++)
24 {
25 AddHill();
26 }
27
28 Normalize();
29
30 return heightmap;
31 }
32
33 private void AddHill()
34 {
35 float x, y;
36 float radius = RandomRange(HillMin, HillMax);
37
38 if (Island)
39 {
40 // Which direction from the center of the map the hill is placed
41 float theta = RandomRange(0, 6.28f);
42
43 // How far from the center of the map to place the hill. The radius
44 // is subtracted from the range to prevent any part of the hill from
45 // reaching the edge of the map
46 float distance = RandomRange(radius / 2.0f, 128.0f - radius);
47
48 x = 128.0f + (float)Math.Cos(theta) * distance;
49 y = 128.0f + (float)Math.Sin(theta) * distance;
50 }
51 else
52 {
53 x = RandomRange(-radius, 256.0f + radius);
54 y = RandomRange(-radius, 256.0f + radius);
55 }
56
57 float radiusSq = radius * radius;
58 float distSq;
59 float height;
60
61 int xMin = (int)(x - radius) - 1;
62 int xMax = (int)(x + radius) + 1;
63 if (xMin < 0) xMin = 0;
64 if (xMax > 255) xMax = 255;
65
66 int yMin = (int)(y - radius) - 1;
67 int yMax = (int)(y + radius) + 1;
68 if (yMin < 0) yMin = 0;
69 if (yMax > 255) yMax = 255;
70
71 // Loop through each affected cell and determine the height at that point
72 for (int v = yMin; v <= yMax; ++v)
73 {
74 float fv = (float)v;
75
76 for (int h = xMin; h <= xMax; ++h)
77 {
78 float fh = (float)h;
79
80 // Determine how far from the center of this hill this point is
81 distSq = (x - fh) * (x - fh) + (y - fv) * (y - fv);
82 height = radiusSq - distSq;
83
84 // Don't add negative hill values
85 if (height > 0.0f) heightmap[h + v * 256] += height;
86 }
87 }
88 }
89
90 private void Normalize()
91 {
92 float min = heightmap[0];
93 float max = heightmap[0];
94
95 for (int x = 0; x < 256; x++)
96 {
97 for (int y = 0; y < 256; y++)
98 {
99 if (heightmap[x + y * 256] < min) min = heightmap[x + y * 256];
100 if (heightmap[x + y * 256] > max) max = heightmap[x + y * 256];
101 }
102 }
103
104 // Avoid a rare divide by zero
105 if (min != max)
106 {
107 for (int x = 0; x < 256; x++)
108 {
109 for (int y = 0; y < 256; y++)
110 {
111 heightmap[x + y * 256] = ((heightmap[x + y * 256] - min) / (max - min)) * (HillMax - HillMin);
112 }
113 }
114 }
115 }
116
117 private float RandomRange(float min, float max)
118 {
119 return (float)Rand.NextDouble() * (max - min) + min;
120 }
121 }
122}