1
0

TerrainData.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 OpenSimulator 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. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.IO.Compression;
  31. using System.Reflection;
  32. using OpenMetaverse;
  33. using log4net;
  34. namespace OpenSim.Framework
  35. {
  36. public abstract class TerrainData
  37. {
  38. // Terrain always is a square
  39. public int SizeX { get; protected set; }
  40. public int SizeY { get; protected set; }
  41. public int SizeZ { get; protected set; }
  42. // A height used when the user doesn't specify anything
  43. public const float DefaultTerrainHeight = 21f;
  44. public abstract float this[int x, int y] { get; set; }
  45. // Someday terrain will have caves
  46. // at most holes :p
  47. public abstract float this[int x, int y, int z] { get; set; }
  48. public abstract bool IsTaintedAt(int xx, int yy);
  49. public abstract bool IsTaintedAt(int xx, int yy, bool clearOnTest);
  50. public abstract void TaintAllTerrain();
  51. public abstract void ClearTaint();
  52. public abstract void ClearLand();
  53. public abstract void ClearLand(float height);
  54. // Return a representation of this terrain for storing as a blob in the database.
  55. // Returns 'true' to say blob was stored in the 'out' locations.
  56. public abstract bool GetDatabaseBlob(out int DBFormatRevisionCode, out Array blob);
  57. // Given a revision code and a blob from the database, create and return the right type of TerrainData.
  58. // The sizes passed are the expected size of the region. The database info will be used to
  59. // initialize the heightmap of that sized region with as much data is in the blob.
  60. // Return created TerrainData or 'null' if unsuccessful.
  61. public static TerrainData CreateFromDatabaseBlobFactory(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
  62. {
  63. // For the moment, there is only one implementation class
  64. return new HeightmapTerrainData(pSizeX, pSizeY, pSizeZ, pFormatCode, pBlob);
  65. }
  66. // return a special compressed representation of the heightmap in ushort
  67. public abstract float[] GetCompressedMap();
  68. public abstract float CompressionFactor { get; }
  69. public abstract float[] GetFloatsSerialized();
  70. public abstract double[,] GetDoubles();
  71. public abstract TerrainData Clone();
  72. }
  73. // The terrain is stored in the database as a blob with a 'revision' field.
  74. // Some implementations of terrain storage would fill the revision field with
  75. // the time the terrain was stored. When real revisions were added and this
  76. // feature removed, that left some old entries with the time in the revision
  77. // field.
  78. // Thus, if revision is greater than 'RevisionHigh' then terrain db entry is
  79. // left over and it is presumed to be 'Legacy256'.
  80. // Numbers are arbitrary and are chosen to to reduce possible mis-interpretation.
  81. // If a revision does not match any of these, it is assumed to be Legacy256.
  82. public enum DBTerrainRevision
  83. {
  84. // Terrain is 'double[256,256]'
  85. Legacy256 = 11,
  86. // Terrain is 'int32, int32, float[,]' where the ints are X and Y dimensions
  87. // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
  88. Variable2D = 22,
  89. Variable2DGzip = 23,
  90. // Terrain is 'int32, int32, int32, int16[]' where the ints are X and Y dimensions
  91. // and third int is the 'compression factor'. The heights are compressed as
  92. // "ushort compressedHeight = (ushort)(height * compressionFactor);"
  93. // The dimensions are presumed to be multiples of 16 and, more likely, multiples of 256.
  94. Compressed2D = 27,
  95. // A revision that is not listed above or any revision greater than this value is 'Legacy256'.
  96. RevisionHigh = 1234
  97. }
  98. // Version of terrain that is a heightmap.
  99. // This should really be 'LLOptimizedHeightmapTerrainData' as it includes knowledge
  100. // of 'patches' which are 16x16 terrain areas which can be sent separately to the viewer.
  101. // The heighmap is kept as an array of ushorts. The ushort values are converted to
  102. // and from floats by TerrainCompressionFactor.
  103. public class HeightmapTerrainData : TerrainData
  104. {
  105. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  106. private static string LogHeader = "[HEIGHTMAP TERRAIN DATA]";
  107. // TerrainData.this[x, y]
  108. public override float this[int x, int y]
  109. {
  110. get { return m_heightmap[x, y]; }
  111. set
  112. {
  113. if (m_heightmap[x, y] != value)
  114. {
  115. m_heightmap[x, y] = value;
  116. m_taint[x / Constants.TerrainPatchSize, y / Constants.TerrainPatchSize] = true;
  117. }
  118. }
  119. }
  120. // TerrainData.this[x, y, z]
  121. public override float this[int x, int y, int z]
  122. {
  123. get { return this[x, y]; }
  124. set { this[x, y] = value; }
  125. }
  126. // TerrainData.ClearTaint
  127. public override void ClearTaint()
  128. {
  129. SetAllTaint(false);
  130. }
  131. // TerrainData.TaintAllTerrain
  132. public override void TaintAllTerrain()
  133. {
  134. SetAllTaint(true);
  135. }
  136. private void SetAllTaint(bool setting)
  137. {
  138. for (int ii = 0; ii < m_taint.GetLength(0); ii++)
  139. for (int jj = 0; jj < m_taint.GetLength(1); jj++)
  140. m_taint[ii, jj] = setting;
  141. }
  142. // TerrainData.ClearLand
  143. public override void ClearLand()
  144. {
  145. ClearLand(DefaultTerrainHeight);
  146. }
  147. // TerrainData.ClearLand(float)
  148. public override void ClearLand(float pHeight)
  149. {
  150. for (int xx = 0; xx < SizeX; xx++)
  151. for (int yy = 0; yy < SizeY; yy++)
  152. m_heightmap[xx, yy] = pHeight;
  153. }
  154. // Return 'true' of the patch that contains these region coordinates has been modified.
  155. // Note that checking the taint clears it.
  156. // There is existing code that relies on this feature.
  157. public override bool IsTaintedAt(int xx, int yy, bool clearOnTest)
  158. {
  159. int tx = xx / Constants.TerrainPatchSize;
  160. int ty = yy / Constants.TerrainPatchSize;
  161. bool ret = m_taint[tx, ty];
  162. if (ret && clearOnTest)
  163. m_taint[tx, ty] = false;
  164. return ret;
  165. }
  166. // Old form that clears the taint flag when we check it.
  167. // ubit: this dangerus naming should be only check without clear
  168. // keeping for old modules outthere
  169. public override bool IsTaintedAt(int xx, int yy)
  170. {
  171. return IsTaintedAt(xx, yy, true /* clearOnTest */);
  172. }
  173. // TerrainData.GetDatabaseBlob
  174. // The user wants something to store in the database.
  175. public override bool GetDatabaseBlob(out int DBRevisionCode, out Array blob)
  176. {
  177. bool ret = false;
  178. /* save all as Variable2DGzip
  179. if (SizeX == Constants.RegionSize && SizeY == Constants.RegionSize)
  180. {
  181. DBRevisionCode = (int)DBTerrainRevision.Legacy256;
  182. blob = ToLegacyTerrainSerialization();
  183. ret = true;
  184. }
  185. else
  186. {
  187. */
  188. DBRevisionCode = (int)DBTerrainRevision.Variable2DGzip;
  189. // DBRevisionCode = (int)DBTerrainRevision.Variable2D;
  190. blob = ToCompressedTerrainSerializationV2DGzip();
  191. // blob = ToCompressedTerrainSerializationV2D();
  192. ret = true;
  193. // }
  194. return ret;
  195. }
  196. // TerrainData.CompressionFactor
  197. private float m_compressionFactor = 100.0f;
  198. public override float CompressionFactor { get { return m_compressionFactor; } }
  199. // TerrainData.GetCompressedMap
  200. public override float[] GetCompressedMap()
  201. {
  202. float[] newMap = new float[SizeX * SizeY];
  203. int ind = 0;
  204. for (int xx = 0; xx < SizeX; xx++)
  205. for (int yy = 0; yy < SizeY; yy++)
  206. newMap[ind++] = m_heightmap[xx, yy];
  207. return newMap;
  208. }
  209. // TerrainData.Clone
  210. public override TerrainData Clone()
  211. {
  212. HeightmapTerrainData ret = new HeightmapTerrainData(SizeX, SizeY, SizeZ);
  213. ret.m_heightmap = (float[,])this.m_heightmap.Clone();
  214. return ret;
  215. }
  216. // TerrainData.GetFloatsSerialized
  217. // This one dimensional version is ordered so height = map[y*sizeX+x];
  218. // DEPRECATED: don't use this function as it does not retain the dimensions of the terrain
  219. // and the caller will probably do the wrong thing if the terrain is not the legacy 256x256.
  220. public override float[] GetFloatsSerialized()
  221. {
  222. int points = SizeX * SizeY;
  223. float[] heights = new float[points];
  224. int idx = 0;
  225. for (int jj = 0; jj < SizeY; jj++)
  226. for (int ii = 0; ii < SizeX; ii++)
  227. {
  228. heights[idx++] = m_heightmap[ii, jj];
  229. }
  230. return heights;
  231. }
  232. // TerrainData.GetDoubles
  233. public override double[,] GetDoubles()
  234. {
  235. double[,] ret = new double[SizeX, SizeY];
  236. for (int xx = 0; xx < SizeX; xx++)
  237. for (int yy = 0; yy < SizeY; yy++)
  238. ret[xx, yy] = (double)m_heightmap[xx, yy];
  239. return ret;
  240. }
  241. // =============================================================
  242. private float[,] m_heightmap;
  243. // Remember subregions of the heightmap that has changed.
  244. private bool[,] m_taint;
  245. // that is coded as the float height times the compression factor (usually '100'
  246. // to make for two decimal points).
  247. public short ToCompressedHeightshort(float pHeight)
  248. {
  249. // clamp into valid range
  250. pHeight *= CompressionFactor;
  251. if (pHeight < short.MinValue)
  252. return short.MinValue;
  253. else if (pHeight > short.MaxValue)
  254. return short.MaxValue;
  255. return (short)pHeight;
  256. }
  257. public ushort ToCompressedHeightushort(float pHeight)
  258. {
  259. // clamp into valid range
  260. pHeight *= CompressionFactor;
  261. if (pHeight < ushort.MinValue)
  262. return ushort.MinValue;
  263. else if (pHeight > ushort.MaxValue)
  264. return ushort.MaxValue;
  265. return (ushort)pHeight;
  266. }
  267. public float FromCompressedHeight(short pHeight)
  268. {
  269. return ((float)pHeight) / CompressionFactor;
  270. }
  271. public float FromCompressedHeight(ushort pHeight)
  272. {
  273. return ((float)pHeight) / CompressionFactor;
  274. }
  275. // To keep with the legacy theme, create an instance of this class based on the
  276. // way terrain used to be passed around.
  277. public HeightmapTerrainData(double[,] pTerrain)
  278. {
  279. SizeX = pTerrain.GetLength(0);
  280. SizeY = pTerrain.GetLength(1);
  281. SizeZ = (int)Constants.RegionHeight;
  282. m_compressionFactor = 100.0f;
  283. m_heightmap = new float[SizeX, SizeY];
  284. for (int ii = 0; ii < SizeX; ii++)
  285. {
  286. for (int jj = 0; jj < SizeY; jj++)
  287. {
  288. m_heightmap[ii, jj] = (float)pTerrain[ii, jj];
  289. }
  290. }
  291. // m_log.DebugFormat("{0} new by doubles. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  292. m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
  293. ClearTaint();
  294. }
  295. // Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
  296. public HeightmapTerrainData(int pX, int pY, int pZ)
  297. {
  298. SizeX = pX;
  299. SizeY = pY;
  300. SizeZ = pZ;
  301. m_compressionFactor = 100.0f;
  302. m_heightmap = new float[SizeX, SizeY];
  303. m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
  304. // m_log.DebugFormat("{0} new by dimensions. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  305. ClearTaint();
  306. ClearLand(0f);
  307. }
  308. public HeightmapTerrainData(float[] cmap, float pCompressionFactor, int pX, int pY, int pZ)
  309. : this(pX, pY, pZ)
  310. {
  311. m_compressionFactor = pCompressionFactor;
  312. int ind = 0;
  313. for (int xx = 0; xx < SizeX; xx++)
  314. for (int yy = 0; yy < SizeY; yy++)
  315. m_heightmap[xx, yy] = cmap[ind++];
  316. // m_log.DebugFormat("{0} new by compressed map. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  317. }
  318. // Create a heighmap from a database blob
  319. public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
  320. : this(pSizeX, pSizeY, pSizeZ)
  321. {
  322. switch ((DBTerrainRevision)pFormatCode)
  323. {
  324. case DBTerrainRevision.Variable2DGzip:
  325. FromCompressedTerrainSerializationV2DGZip(pBlob);
  326. m_log.DebugFormat("{0} HeightmapTerrainData create from Variable2DGzip serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  327. break;
  328. case DBTerrainRevision.Variable2D:
  329. FromCompressedTerrainSerializationV2D(pBlob);
  330. m_log.DebugFormat("{0} HeightmapTerrainData create from Variable2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  331. break;
  332. case DBTerrainRevision.Compressed2D:
  333. FromCompressedTerrainSerialization2D(pBlob);
  334. m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  335. break;
  336. default:
  337. FromLegacyTerrainSerialization(pBlob);
  338. m_log.DebugFormat("{0} HeightmapTerrainData create from legacy serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  339. break;
  340. }
  341. }
  342. // Just create an array of doubles. Presumes the caller implicitly knows the size.
  343. public Array ToLegacyTerrainSerialization()
  344. {
  345. Array ret = null;
  346. using (MemoryStream str = new MemoryStream((int)Constants.RegionSize * (int)Constants.RegionSize * sizeof(double)))
  347. {
  348. using (BinaryWriter bw = new BinaryWriter(str))
  349. {
  350. for (int xx = 0; xx < Constants.RegionSize; xx++)
  351. {
  352. for (int yy = 0; yy < Constants.RegionSize; yy++)
  353. {
  354. double height = this[xx, yy];
  355. if (height == 0.0)
  356. height = double.Epsilon;
  357. bw.Write(height);
  358. }
  359. }
  360. }
  361. ret = str.ToArray();
  362. }
  363. return ret;
  364. }
  365. // Presumes the caller implicitly knows the size.
  366. public void FromLegacyTerrainSerialization(byte[] pBlob)
  367. {
  368. // In case database info doesn't match real terrain size, initialize the whole terrain.
  369. ClearLand();
  370. try
  371. {
  372. using (MemoryStream mstr = new MemoryStream(pBlob))
  373. {
  374. using (BinaryReader br = new BinaryReader(mstr))
  375. {
  376. for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
  377. {
  378. for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
  379. {
  380. float val = (float)br.ReadDouble();
  381. if (xx < SizeX && yy < SizeY)
  382. m_heightmap[xx, yy] = val;
  383. }
  384. }
  385. }
  386. }
  387. }
  388. catch
  389. {
  390. ClearLand();
  391. }
  392. ClearTaint();
  393. }
  394. // stores as variable2D
  395. // int32 sizeX
  396. // int32 sizeY
  397. // float[,] array
  398. public Array ToCompressedTerrainSerializationV2D()
  399. {
  400. Array ret = null;
  401. try
  402. {
  403. using (MemoryStream str = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float))))
  404. {
  405. using (BinaryWriter bw = new BinaryWriter(str))
  406. {
  407. bw.Write((Int32)SizeX);
  408. bw.Write((Int32)SizeY);
  409. for (int yy = 0; yy < SizeY; yy++)
  410. for (int xx = 0; xx < SizeX; xx++)
  411. {
  412. // reduce to 1cm resolution
  413. float val = (float)Math.Round(m_heightmap[xx, yy],2,MidpointRounding.ToEven);
  414. bw.Write(val);
  415. }
  416. }
  417. ret = str.ToArray();
  418. }
  419. }
  420. catch
  421. {
  422. }
  423. m_log.DebugFormat("{0} V2D {1} bytes",
  424. LogHeader, ret.Length);
  425. return ret;
  426. }
  427. // as above with Gzip compression
  428. public Array ToCompressedTerrainSerializationV2DGzip()
  429. {
  430. Array ret = null;
  431. try
  432. {
  433. using (MemoryStream inp = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float))))
  434. {
  435. using (BinaryWriter bw = new BinaryWriter(inp))
  436. {
  437. bw.Write((Int32)SizeX);
  438. bw.Write((Int32)SizeY);
  439. for (int yy = 0; yy < SizeY; yy++)
  440. for (int xx = 0; xx < SizeX; xx++)
  441. {
  442. bw.Write((float)m_heightmap[xx, yy]);
  443. }
  444. bw.Flush();
  445. inp.Seek(0, SeekOrigin.Begin);
  446. using (MemoryStream outputStream = new MemoryStream())
  447. {
  448. using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
  449. {
  450. inp.CopyStream(compressionStream, int.MaxValue);
  451. compressionStream.Close();
  452. ret = outputStream.ToArray();
  453. }
  454. }
  455. }
  456. }
  457. }
  458. catch
  459. {
  460. }
  461. m_log.DebugFormat("{0} V2DGzip {1} bytes",
  462. LogHeader, ret.Length);
  463. return ret;
  464. }
  465. // Initialize heightmap from blob consisting of:
  466. // int32, int32, int32, int32, int16[]
  467. // where the first int32 is format code, next two int32s are the X and y of heightmap data and
  468. // the forth int is the compression factor for the following int16s
  469. // This is just sets heightmap info. The actual size of the region was set on this instance's
  470. // creation and any heights not initialized by theis blob are set to the default height.
  471. public void FromCompressedTerrainSerialization2D(byte[] pBlob)
  472. {
  473. Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
  474. using (MemoryStream mstr = new MemoryStream(pBlob))
  475. {
  476. using (BinaryReader br = new BinaryReader(mstr))
  477. {
  478. hmFormatCode = br.ReadInt32();
  479. hmSizeX = br.ReadInt32();
  480. hmSizeY = br.ReadInt32();
  481. hmCompressionFactor = br.ReadInt32();
  482. m_compressionFactor = hmCompressionFactor;
  483. // In case database info doesn't match real terrain size, initialize the whole terrain.
  484. ClearLand();
  485. for (int yy = 0; yy < hmSizeY; yy++)
  486. {
  487. for (int xx = 0; xx < hmSizeX; xx++)
  488. {
  489. float val = FromCompressedHeight(br.ReadInt16());
  490. if (xx < SizeX && yy < SizeY)
  491. m_heightmap[xx, yy] = val;
  492. }
  493. }
  494. }
  495. ClearTaint();
  496. m_log.DebugFormat("{0} Read (compressed2D) heightmap. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
  497. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
  498. }
  499. }
  500. // Initialize heightmap from blob consisting of:
  501. // int32, int32, int32, float[]
  502. // where the first int32 is format code, next two int32s are the X and y of heightmap data
  503. // This is just sets heightmap info. The actual size of the region was set on this instance's
  504. // creation and any heights not initialized by theis blob are set to the default height.
  505. public void FromCompressedTerrainSerializationV2D(byte[] pBlob)
  506. {
  507. Int32 hmSizeX, hmSizeY;
  508. try
  509. {
  510. using (MemoryStream mstr = new MemoryStream(pBlob))
  511. {
  512. using (BinaryReader br = new BinaryReader(mstr))
  513. {
  514. hmSizeX = br.ReadInt32();
  515. hmSizeY = br.ReadInt32();
  516. // In case database info doesn't match real terrain size, initialize the whole terrain.
  517. ClearLand();
  518. for (int yy = 0; yy < hmSizeY; yy++)
  519. {
  520. for (int xx = 0; xx < hmSizeX; xx++)
  521. {
  522. float val = br.ReadSingle();
  523. if (xx < SizeX && yy < SizeY)
  524. m_heightmap[xx, yy] = val;
  525. }
  526. }
  527. }
  528. }
  529. }
  530. catch (Exception e)
  531. {
  532. ClearTaint();
  533. m_log.ErrorFormat("{0} 2D error: {1} - terrain may be damaged",
  534. LogHeader, e.Message);
  535. return;
  536. }
  537. ClearTaint();
  538. m_log.DebugFormat("{0} V2D Heightmap size=<{1},{2}>. Region size=<{3},{4}>",
  539. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY);
  540. }
  541. // as above but Gzip compressed
  542. public void FromCompressedTerrainSerializationV2DGZip(byte[] pBlob)
  543. {
  544. m_log.InfoFormat("{0} VD2Gzip {1} bytes input",
  545. LogHeader, pBlob.Length);
  546. Int32 hmSizeX, hmSizeY;
  547. try
  548. {
  549. using (MemoryStream outputStream = new MemoryStream())
  550. {
  551. using (MemoryStream inputStream = new MemoryStream(pBlob))
  552. {
  553. using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
  554. {
  555. decompressionStream.Flush();
  556. decompressionStream.CopyTo(outputStream);
  557. }
  558. }
  559. outputStream.Seek(0, SeekOrigin.Begin);
  560. using (BinaryReader br = new BinaryReader(outputStream))
  561. {
  562. hmSizeX = br.ReadInt32();
  563. hmSizeY = br.ReadInt32();
  564. // In case database info doesn't match real terrain size, initialize the whole terrain.
  565. ClearLand();
  566. for (int yy = 0; yy < hmSizeY; yy++)
  567. {
  568. for (int xx = 0; xx < hmSizeX; xx++)
  569. {
  570. float val = br.ReadSingle();
  571. if (xx < SizeX && yy < SizeY)
  572. m_heightmap[xx, yy] = val;
  573. }
  574. }
  575. }
  576. }
  577. }
  578. catch( Exception e)
  579. {
  580. ClearTaint();
  581. m_log.ErrorFormat("{0} V2DGzip error: {1} - terrain may be damaged",
  582. LogHeader, e.Message);
  583. return;
  584. }
  585. ClearTaint();
  586. m_log.DebugFormat("{0} V2DGzip. Heightmap size=<{1},{2}>. Region size=<{3},{4}>",
  587. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY);
  588. }
  589. }
  590. }