diff options
Diffstat (limited to 'OpenSim')
-rw-r--r-- | OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs | 51 |
1 files changed, 23 insertions, 28 deletions
diff --git a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs index b5c7d33..7d5adf9 100644 --- a/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs +++ b/OpenSim/Region/CoreModules/World/Terrain/FileLoaders/Terragen.cs | |||
@@ -65,7 +65,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
65 | bool eof = false; | 65 | bool eof = false; |
66 | 66 | ||
67 | int fileXPoints = 0; | 67 | int fileXPoints = 0; |
68 | // int fileYPoints = 0; | 68 | int fileYPoints = 0; |
69 | 69 | ||
70 | // Terragen file | 70 | // Terragen file |
71 | while (eof == false) | 71 | while (eof == false) |
@@ -75,7 +75,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
75 | { | 75 | { |
76 | case "SIZE": | 76 | case "SIZE": |
77 | fileXPoints = bs.ReadInt16() + 1; | 77 | fileXPoints = bs.ReadInt16() + 1; |
78 | // fileYPoints = fileXPoints; | 78 | fileYPoints = fileXPoints; |
79 | bs.ReadInt16(); | 79 | bs.ReadInt16(); |
80 | break; | 80 | break; |
81 | case "XPTS": | 81 | case "XPTS": |
@@ -83,8 +83,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
83 | bs.ReadInt16(); | 83 | bs.ReadInt16(); |
84 | break; | 84 | break; |
85 | case "YPTS": | 85 | case "YPTS": |
86 | // fileYPoints = bs.ReadInt16(); | 86 | fileYPoints = bs.ReadInt16(); |
87 | bs.ReadInt16(); | ||
88 | bs.ReadInt16(); | 87 | bs.ReadInt16(); |
89 | break; | 88 | break; |
90 | case "ALTW": | 89 | case "ALTW": |
@@ -138,7 +137,6 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
138 | bs.ReadInt16(); | 137 | bs.ReadInt16(); |
139 | } | 138 | } |
140 | 139 | ||
141 | |||
142 | break; | 140 | break; |
143 | default: | 141 | default: |
144 | bs.ReadInt32(); | 142 | bs.ReadInt32(); |
@@ -154,10 +152,11 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
154 | 152 | ||
155 | public ITerrainChannel LoadStream(Stream s) | 153 | public ITerrainChannel LoadStream(Stream s) |
156 | { | 154 | { |
157 | 155 | // Set to default size | |
158 | int w = (int)Constants.RegionSize; | 156 | int w = (int)Constants.RegionSize; |
159 | int h = (int)Constants.RegionSize; | 157 | int h = (int)Constants.RegionSize; |
160 | 158 | ||
159 | // create a dummy channel (in case data is bad) | ||
161 | TerrainChannel retval = new TerrainChannel(w, h); | 160 | TerrainChannel retval = new TerrainChannel(w, h); |
162 | 161 | ||
163 | BinaryReader bs = new BinaryReader(s); | 162 | BinaryReader bs = new BinaryReader(s); |
@@ -165,8 +164,6 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
165 | bool eof = false; | 164 | bool eof = false; |
166 | if (Encoding.ASCII.GetString(bs.ReadBytes(16)) == "TERRAGENTERRAIN ") | 165 | if (Encoding.ASCII.GetString(bs.ReadBytes(16)) == "TERRAGENTERRAIN ") |
167 | { | 166 | { |
168 | // int fileWidth = w; | ||
169 | // int fileHeight = h; | ||
170 | 167 | ||
171 | // Terragen file | 168 | // Terragen file |
172 | while (eof == false) | 169 | while (eof == false) |
@@ -175,31 +172,29 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
175 | switch (tmp) | 172 | switch (tmp) |
176 | { | 173 | { |
177 | case "SIZE": | 174 | case "SIZE": |
178 | // int sztmp = bs.ReadInt16() + 1; | 175 | w = bs.ReadInt16() + 1; |
179 | // fileWidth = sztmp; | 176 | h = w; |
180 | // fileHeight = sztmp; | ||
181 | bs.ReadInt16(); | ||
182 | bs.ReadInt16(); | 177 | bs.ReadInt16(); |
183 | break; | 178 | break; |
184 | case "XPTS": | 179 | case "XPTS": |
185 | // fileWidth = bs.ReadInt16(); | 180 | w = bs.ReadInt16(); |
186 | bs.ReadInt16(); | ||
187 | bs.ReadInt16(); | 181 | bs.ReadInt16(); |
188 | break; | 182 | break; |
189 | case "YPTS": | 183 | case "YPTS": |
190 | // fileHeight = bs.ReadInt16(); | 184 | h = bs.ReadInt16(); |
191 | bs.ReadInt16(); | ||
192 | bs.ReadInt16(); | 185 | bs.ReadInt16(); |
193 | break; | 186 | break; |
194 | case "ALTW": | 187 | case "ALTW": |
195 | eof = true; | 188 | eof = true; |
196 | Int16 heightScale = bs.ReadInt16(); | 189 | // create new channel of proper size (now that we know it) |
197 | Int16 baseHeight = bs.ReadInt16(); | 190 | retval = new TerrainChannel(w, h); |
191 | double heightScale = (double)bs.ReadInt16() / 65536.0; | ||
192 | double baseHeight = (double)bs.ReadInt16(); | ||
198 | for (int y = 0; y < h; y++) | 193 | for (int y = 0; y < h; y++) |
199 | { | 194 | { |
200 | for (int x = 0; x < w; x++) | 195 | for (int x = 0; x < w; x++) |
201 | { | 196 | { |
202 | retval[x, y] = baseHeight + bs.ReadInt16() * (double)heightScale / 65536.0; | 197 | retval[x, y] = baseHeight + (double)bs.ReadInt16() * heightScale; |
203 | } | 198 | } |
204 | } | 199 | } |
205 | break; | 200 | break; |
@@ -209,9 +204,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
209 | } | 204 | } |
210 | } | 205 | } |
211 | } | 206 | } |
212 | |||
213 | bs.Close(); | 207 | bs.Close(); |
214 | |||
215 | return retval; | 208 | return retval; |
216 | } | 209 | } |
217 | 210 | ||
@@ -257,17 +250,17 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
257 | bs.Write(enc.GetBytes("TERRAGENTERRAIN ")); | 250 | bs.Write(enc.GetBytes("TERRAGENTERRAIN ")); |
258 | 251 | ||
259 | bs.Write(enc.GetBytes("SIZE")); | 252 | bs.Write(enc.GetBytes("SIZE")); |
260 | bs.Write(Convert.ToInt16(Constants.RegionSize)); | 253 | bs.Write(Convert.ToInt16(map.Width)); |
261 | bs.Write(Convert.ToInt16(0)); // necessary padding | 254 | bs.Write(Convert.ToInt16(0)); // necessary padding |
262 | 255 | ||
263 | //The XPTS and YPTS chunks are not needed for square regions | 256 | //The XPTS and YPTS chunks are not needed for square regions |
264 | //but L3DT won't load the terrain file properly without them. | 257 | //but L3DT won't load the terrain file properly without them. |
265 | bs.Write(enc.GetBytes("XPTS")); | 258 | bs.Write(enc.GetBytes("XPTS")); |
266 | bs.Write(Convert.ToInt16(Constants.RegionSize)); | 259 | bs.Write(Convert.ToInt16(map.Width)); |
267 | bs.Write(Convert.ToInt16(0)); // necessary padding | 260 | bs.Write(Convert.ToInt16(0)); // necessary padding |
268 | 261 | ||
269 | bs.Write(enc.GetBytes("YPTS")); | 262 | bs.Write(enc.GetBytes("YPTS")); |
270 | bs.Write(Convert.ToInt16(Constants.RegionSize)); | 263 | bs.Write(Convert.ToInt16(map.Height)); |
271 | bs.Write(Convert.ToInt16(0)); // necessary padding | 264 | bs.Write(Convert.ToInt16(0)); // necessary padding |
272 | 265 | ||
273 | bs.Write(enc.GetBytes("SCAL")); | 266 | bs.Write(enc.GetBytes("SCAL")); |
@@ -282,12 +275,14 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
282 | bs.Write(enc.GetBytes("ALTW")); | 275 | bs.Write(enc.GetBytes("ALTW")); |
283 | bs.Write(Convert.ToInt16(horizontalScale)); // range between max and min | 276 | bs.Write(Convert.ToInt16(horizontalScale)); // range between max and min |
284 | bs.Write(Convert.ToInt16(baseHeight)); // base height or mid point | 277 | bs.Write(Convert.ToInt16(baseHeight)); // base height or mid point |
285 | 278 | ||
279 | double factor = 65536.0 / horizontalScale; // avoid computing this on each iteration | ||
280 | |||
286 | for (int y = 0; y < map.Height; y++) | 281 | for (int y = 0; y < map.Height; y++) |
287 | { | 282 | { |
288 | for (int x = 0; x < map.Width; x++) | 283 | for (int x = 0; x < map.Width; x++) |
289 | { | 284 | { |
290 | float elevation = (float)((map[x,y] - baseHeight) * 65536 ) / (float)horizontalScale; // see LoadStream for inverse | 285 | float elevation = (float)((map[x,y] - baseHeight) * factor); // see LoadStream for inverse |
291 | 286 | ||
292 | // clamp rounding issues | 287 | // clamp rounding issues |
293 | if (elevation > Int16.MaxValue) | 288 | if (elevation > Int16.MaxValue) |
@@ -299,7 +294,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
299 | } | 294 | } |
300 | } | 295 | } |
301 | 296 | ||
302 | //This is only necessary for older versions of Terragen. | 297 | //This is necessary for older versions of Terragen. |
303 | bs.Write(enc.GetBytes("EOF ")); | 298 | bs.Write(enc.GetBytes("EOF ")); |
304 | 299 | ||
305 | bs.Close(); | 300 | bs.Close(); |
@@ -343,7 +338,7 @@ namespace OpenSim.Region.CoreModules.World.Terrain.FileLoaders | |||
343 | if (BitConverter.IsLittleEndian == false) | 338 | if (BitConverter.IsLittleEndian == false) |
344 | { | 339 | { |
345 | byte[] tmp = new byte[4]; | 340 | byte[] tmp = new byte[4]; |
346 | for (int i = 0; i < 4; i++) | 341 | for (int i = 3; i >= 0; i--) |
347 | { | 342 | { |
348 | tmp[i] = retVal[3 - i]; | 343 | tmp[i] = retVal[3 - i]; |
349 | } | 344 | } |