31 using OpenSim.Region.Framework.Interfaces;
32 using OpenSim.Region.Framework.Scenes;
33 using OpenSim.Framework;
42 internal class Terragen : ITerrainLoader
44 #region ITerrainLoader Members
48 FileInfo file =
new FileInfo(filename);
49 FileStream s = file.Open(FileMode.Open, FileAccess.Read);
57 public ITerrainChannel LoadFile(
string filename,
int offsetX,
int offsetY,
int fileWidth,
int fileHeight,
int sectionWidth,
int sectionHeight)
61 FileInfo file =
new FileInfo(filename);
62 FileStream s = file.Open(FileMode.Open, FileAccess.Read);
63 BinaryReader bs =
new BinaryReader(s);
73 string tmp = Encoding.ASCII.GetString(bs.ReadBytes(4));
77 fileXPoints = bs.ReadInt16() + 1;
78 fileYPoints = fileXPoints;
82 fileXPoints = bs.ReadInt16();
86 fileYPoints = bs.ReadInt16();
91 Int16 heightScale = bs.ReadInt16();
92 Int16 baseHeight = bs.ReadInt16();
94 int currFileYOffset = 0;
98 while (currFileYOffset < offsetY)
101 int heightsToRead = sectionHeight * fileXPoints;
102 bs.ReadBytes(heightsToRead * 2);
106 for (
int y = 0; y < sectionHeight; y++)
108 int currFileXOffset = 0;
113 while (currFileXOffset < offsetX)
115 bs.ReadBytes(sectionWidth * 2);
120 for (
int x = 0; x < sectionWidth; x++)
123 retval[x, y] = baseHeight + bs.ReadInt16() * (
double)heightScale / 65536.0;
130 while (currFileXOffset < fileWidth)
133 bs.ReadBytes(sectionWidth * 2);
157 int h = (
int)Constants.RegionSize;
162 BinaryReader bs =
new BinaryReader(s);
165 if (Encoding.ASCII.GetString(bs.ReadBytes(16)) ==
"TERRAGENTERRAIN ")
171 string tmp = Encoding.ASCII.GetString(bs.ReadBytes(4));
175 w = bs.ReadInt16() + 1;
191 double heightScale = (double)bs.ReadInt16() / 65536.0;
192 double baseHeight = (double)bs.ReadInt16();
193 for (
int y = 0; y < h; y++)
195 for (
int x = 0; x < w; x++)
197 retval[x, y] = baseHeight + (double)bs.ReadInt16() * heightScale;
213 FileInfo file =
new FileInfo(filename);
214 FileStream s = file.Open(FileMode.Create, FileAccess.Write);
222 BinaryWriter bs =
new BinaryWriter(stream);
225 double heightMax = map[0,0];
226 double heightMin = map[0,0];
228 for (
int y = 0; y < map.Height; y++)
230 for (
int x = 0; x < map.Width; x++)
232 double current = map[x,y];
233 if (heightMax < current)
235 if (heightMin > current)
240 double baseHeight = Math.Floor( (heightMax + heightMin) / 2d );
242 double horizontalScale = Math.Ceiling((heightMax - heightMin));
245 if (horizontalScale < 0.01d)
246 horizontalScale = 0.01d;
248 Encoding enc = Encoding.ASCII;
250 bs.Write(enc.GetBytes(
"TERRAGENTERRAIN "));
252 bs.Write(enc.GetBytes(
"SIZE"));
253 bs.Write(Convert.ToInt16(map.Width));
254 bs.Write(Convert.ToInt16(0));
258 bs.Write(enc.GetBytes(
"XPTS"));
259 bs.Write(Convert.ToInt16(map.Width));
260 bs.Write(Convert.ToInt16(0));
262 bs.Write(enc.GetBytes(
"YPTS"));
263 bs.Write(Convert.ToInt16(map.Height));
264 bs.Write(Convert.ToInt16(0));
266 bs.Write(enc.GetBytes(
"SCAL"));
267 bs.Write(ToLittleEndian(1f));
268 bs.Write(ToLittleEndian(1f));
269 bs.Write(ToLittleEndian(1f));
275 bs.Write(enc.GetBytes(
"ALTW"));
276 bs.Write(Convert.ToInt16(horizontalScale));
277 bs.Write(Convert.ToInt16(baseHeight));
279 double factor = 65536.0 / horizontalScale;
281 for (
int y = 0; y < map.Height; y++)
283 for (
int x = 0; x < map.Width; x++)
285 float elevation = (float)((map[x,y] - baseHeight) * factor);
288 if (elevation > Int16.MaxValue)
289 elevation = Int16.MaxValue;
290 else if (elevation < Int16.MinValue)
291 elevation = Int16.MinValue;
293 bs.Write(Convert.ToInt16(elevation));
298 bs.Write(enc.GetBytes(
"EOF "));
303 public string FileExtension
305 get {
return ".ter"; }
308 public virtual void SaveFile(
ITerrainChannel m_channel,
string filename,
309 int offsetX,
int offsetY,
310 int fileWidth,
int fileHeight,
311 int regionSizeX,
int regionSizeY)
313 throw new System.Exception(
"Not Implemented");
318 public override string ToString()
324 public bool SupportsTileSave()
335 private byte[] ToLittleEndian(
float number)
337 byte[] retVal = BitConverter.GetBytes(number);
338 if (BitConverter.IsLittleEndian ==
false)
340 byte[] tmp =
new byte[4];
341 for (
int i = 3; i >= 0; i--)
343 tmp[i] = retVal[3 - i];
A new version of the old Channel class, simplified