TerrainData.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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. if (SizeX == Constants.RegionSize && SizeY == Constants.RegionSize)
  179. {
  180. DBRevisionCode = (int)DBTerrainRevision.Legacy256;
  181. blob = ToLegacyTerrainSerialization();
  182. ret = true;
  183. }
  184. else
  185. {
  186. DBRevisionCode = (int)DBTerrainRevision.Variable2DGzip;
  187. // DBRevisionCode = (int)DBTerrainRevision.Variable2D;
  188. blob = ToCompressedTerrainSerializationV2DGzip();
  189. // blob = ToCompressedTerrainSerializationV2D();
  190. ret = true;
  191. }
  192. return ret;
  193. }
  194. // TerrainData.CompressionFactor
  195. private float m_compressionFactor = 100.0f;
  196. public override float CompressionFactor { get { return m_compressionFactor; } }
  197. // TerrainData.GetCompressedMap
  198. public override float[] GetCompressedMap()
  199. {
  200. float[] newMap = new float[SizeX * SizeY];
  201. int ind = 0;
  202. for (int xx = 0; xx < SizeX; xx++)
  203. for (int yy = 0; yy < SizeY; yy++)
  204. newMap[ind++] = m_heightmap[xx, yy];
  205. return newMap;
  206. }
  207. // TerrainData.Clone
  208. public override TerrainData Clone()
  209. {
  210. HeightmapTerrainData ret = new HeightmapTerrainData(SizeX, SizeY, SizeZ);
  211. ret.m_heightmap = (float[,])this.m_heightmap.Clone();
  212. return ret;
  213. }
  214. // TerrainData.GetFloatsSerialized
  215. // This one dimensional version is ordered so height = map[y*sizeX+x];
  216. // DEPRECATED: don't use this function as it does not retain the dimensions of the terrain
  217. // and the caller will probably do the wrong thing if the terrain is not the legacy 256x256.
  218. public override float[] GetFloatsSerialized()
  219. {
  220. int points = SizeX * SizeY;
  221. float[] heights = new float[points];
  222. int idx = 0;
  223. for (int jj = 0; jj < SizeY; jj++)
  224. for (int ii = 0; ii < SizeX; ii++)
  225. {
  226. heights[idx++] = m_heightmap[ii, jj];
  227. }
  228. return heights;
  229. }
  230. // TerrainData.GetDoubles
  231. public override double[,] GetDoubles()
  232. {
  233. double[,] ret = new double[SizeX, SizeY];
  234. for (int xx = 0; xx < SizeX; xx++)
  235. for (int yy = 0; yy < SizeY; yy++)
  236. ret[xx, yy] = (double)m_heightmap[xx, yy];
  237. return ret;
  238. }
  239. // =============================================================
  240. private float[,] m_heightmap;
  241. // Remember subregions of the heightmap that has changed.
  242. private bool[,] m_taint;
  243. // that is coded as the float height times the compression factor (usually '100'
  244. // to make for two decimal points).
  245. public short ToCompressedHeightshort(float pHeight)
  246. {
  247. // clamp into valid range
  248. pHeight *= CompressionFactor;
  249. if (pHeight < short.MinValue)
  250. return short.MinValue;
  251. else if (pHeight > short.MaxValue)
  252. return short.MaxValue;
  253. return (short)pHeight;
  254. }
  255. public ushort ToCompressedHeightushort(float pHeight)
  256. {
  257. // clamp into valid range
  258. pHeight *= CompressionFactor;
  259. if (pHeight < ushort.MinValue)
  260. return ushort.MinValue;
  261. else if (pHeight > ushort.MaxValue)
  262. return ushort.MaxValue;
  263. return (ushort)pHeight;
  264. }
  265. public float FromCompressedHeight(short pHeight)
  266. {
  267. return ((float)pHeight) / CompressionFactor;
  268. }
  269. public float FromCompressedHeight(ushort pHeight)
  270. {
  271. return ((float)pHeight) / CompressionFactor;
  272. }
  273. // To keep with the legacy theme, create an instance of this class based on the
  274. // way terrain used to be passed around.
  275. public HeightmapTerrainData(double[,] pTerrain)
  276. {
  277. SizeX = pTerrain.GetLength(0);
  278. SizeY = pTerrain.GetLength(1);
  279. SizeZ = (int)Constants.RegionHeight;
  280. m_compressionFactor = 100.0f;
  281. m_heightmap = new float[SizeX, SizeY];
  282. for (int ii = 0; ii < SizeX; ii++)
  283. {
  284. for (int jj = 0; jj < SizeY; jj++)
  285. {
  286. m_heightmap[ii, jj] = (float)pTerrain[ii, jj];
  287. }
  288. }
  289. // m_log.DebugFormat("{0} new by doubles. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  290. m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
  291. ClearTaint();
  292. }
  293. // Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
  294. public HeightmapTerrainData(int pX, int pY, int pZ)
  295. {
  296. SizeX = pX;
  297. SizeY = pY;
  298. SizeZ = pZ;
  299. m_compressionFactor = 100.0f;
  300. m_heightmap = new float[SizeX, SizeY];
  301. m_taint = new bool[SizeX / Constants.TerrainPatchSize, SizeY / Constants.TerrainPatchSize];
  302. // m_log.DebugFormat("{0} new by dimensions. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  303. ClearTaint();
  304. ClearLand(0f);
  305. }
  306. public HeightmapTerrainData(float[] cmap, float pCompressionFactor, int pX, int pY, int pZ)
  307. : this(pX, pY, pZ)
  308. {
  309. m_compressionFactor = pCompressionFactor;
  310. int ind = 0;
  311. for (int xx = 0; xx < SizeX; xx++)
  312. for (int yy = 0; yy < SizeY; yy++)
  313. m_heightmap[xx, yy] = cmap[ind++];
  314. // m_log.DebugFormat("{0} new by compressed map. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
  315. }
  316. // Create a heighmap from a database blob
  317. public HeightmapTerrainData(int pSizeX, int pSizeY, int pSizeZ, int pFormatCode, byte[] pBlob)
  318. : this(pSizeX, pSizeY, pSizeZ)
  319. {
  320. switch ((DBTerrainRevision)pFormatCode)
  321. {
  322. case DBTerrainRevision.Variable2DGzip:
  323. FromCompressedTerrainSerializationV2DGZip(pBlob);
  324. m_log.DebugFormat("{0} HeightmapTerrainData create from Variable2DGzip serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  325. break;
  326. case DBTerrainRevision.Variable2D:
  327. FromCompressedTerrainSerializationV2D(pBlob);
  328. m_log.DebugFormat("{0} HeightmapTerrainData create from Variable2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  329. break;
  330. case DBTerrainRevision.Compressed2D:
  331. FromCompressedTerrainSerialization2D(pBlob);
  332. m_log.DebugFormat("{0} HeightmapTerrainData create from Compressed2D serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  333. break;
  334. default:
  335. FromLegacyTerrainSerialization(pBlob);
  336. m_log.DebugFormat("{0} HeightmapTerrainData create from legacy serialization. Size=<{1},{2}>", LogHeader, SizeX, SizeY);
  337. break;
  338. }
  339. }
  340. // Just create an array of doubles. Presumes the caller implicitly knows the size.
  341. public Array ToLegacyTerrainSerialization()
  342. {
  343. Array ret = null;
  344. using (MemoryStream str = new MemoryStream((int)Constants.RegionSize * (int)Constants.RegionSize * sizeof(double)))
  345. {
  346. using (BinaryWriter bw = new BinaryWriter(str))
  347. {
  348. for (int xx = 0; xx < Constants.RegionSize; xx++)
  349. {
  350. for (int yy = 0; yy < Constants.RegionSize; yy++)
  351. {
  352. double height = this[xx, yy];
  353. if (height == 0.0)
  354. height = double.Epsilon;
  355. bw.Write(height);
  356. }
  357. }
  358. }
  359. ret = str.ToArray();
  360. }
  361. return ret;
  362. }
  363. // Presumes the caller implicitly knows the size.
  364. public void FromLegacyTerrainSerialization(byte[] pBlob)
  365. {
  366. // In case database info doesn't match real terrain size, initialize the whole terrain.
  367. ClearLand();
  368. try
  369. {
  370. using (MemoryStream mstr = new MemoryStream(pBlob))
  371. {
  372. using (BinaryReader br = new BinaryReader(mstr))
  373. {
  374. for (int xx = 0; xx < (int)Constants.RegionSize; xx++)
  375. {
  376. for (int yy = 0; yy < (int)Constants.RegionSize; yy++)
  377. {
  378. float val = (float)br.ReadDouble();
  379. if (xx < SizeX && yy < SizeY)
  380. m_heightmap[xx, yy] = val;
  381. }
  382. }
  383. }
  384. }
  385. }
  386. catch
  387. {
  388. ClearLand();
  389. }
  390. ClearTaint();
  391. }
  392. // stores as variable2D
  393. // int32 sizeX
  394. // int32 sizeY
  395. // float[,] array
  396. public Array ToCompressedTerrainSerializationV2D()
  397. {
  398. Array ret = null;
  399. try
  400. {
  401. using (MemoryStream str = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float))))
  402. {
  403. using (BinaryWriter bw = new BinaryWriter(str))
  404. {
  405. bw.Write((Int32)SizeX);
  406. bw.Write((Int32)SizeY);
  407. for (int yy = 0; yy < SizeY; yy++)
  408. for (int xx = 0; xx < SizeX; xx++)
  409. {
  410. // reduce to 1cm resolution
  411. float val = (float)Math.Round(m_heightmap[xx, yy],2,MidpointRounding.ToEven);
  412. bw.Write(val);
  413. }
  414. }
  415. ret = str.ToArray();
  416. }
  417. }
  418. catch
  419. {
  420. }
  421. m_log.DebugFormat("{0} V2D {1} bytes",
  422. LogHeader, ret.Length);
  423. return ret;
  424. }
  425. // as above with Gzip compression
  426. public Array ToCompressedTerrainSerializationV2DGzip()
  427. {
  428. Array ret = null;
  429. try
  430. {
  431. using (MemoryStream inp = new MemoryStream((2 * sizeof(Int32)) + (SizeX * SizeY * sizeof(float))))
  432. {
  433. using (BinaryWriter bw = new BinaryWriter(inp))
  434. {
  435. bw.Write((Int32)SizeX);
  436. bw.Write((Int32)SizeY);
  437. for (int yy = 0; yy < SizeY; yy++)
  438. for (int xx = 0; xx < SizeX; xx++)
  439. {
  440. bw.Write((float)m_heightmap[xx, yy]);
  441. }
  442. bw.Flush();
  443. inp.Seek(0, SeekOrigin.Begin);
  444. using (MemoryStream outputStream = new MemoryStream())
  445. {
  446. using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
  447. {
  448. inp.CopyStream(compressionStream, int.MaxValue);
  449. compressionStream.Close();
  450. ret = outputStream.ToArray();
  451. }
  452. }
  453. }
  454. }
  455. }
  456. catch
  457. {
  458. }
  459. m_log.DebugFormat("{0} V2DGzip {1} bytes",
  460. LogHeader, ret.Length);
  461. return ret;
  462. }
  463. // Initialize heightmap from blob consisting of:
  464. // int32, int32, int32, int32, int16[]
  465. // where the first int32 is format code, next two int32s are the X and y of heightmap data and
  466. // the forth int is the compression factor for the following int16s
  467. // This is just sets heightmap info. The actual size of the region was set on this instance's
  468. // creation and any heights not initialized by theis blob are set to the default height.
  469. public void FromCompressedTerrainSerialization2D(byte[] pBlob)
  470. {
  471. Int32 hmFormatCode, hmSizeX, hmSizeY, hmCompressionFactor;
  472. using (MemoryStream mstr = new MemoryStream(pBlob))
  473. {
  474. using (BinaryReader br = new BinaryReader(mstr))
  475. {
  476. hmFormatCode = br.ReadInt32();
  477. hmSizeX = br.ReadInt32();
  478. hmSizeY = br.ReadInt32();
  479. hmCompressionFactor = br.ReadInt32();
  480. m_compressionFactor = hmCompressionFactor;
  481. // In case database info doesn't match real terrain size, initialize the whole terrain.
  482. ClearLand();
  483. for (int yy = 0; yy < hmSizeY; yy++)
  484. {
  485. for (int xx = 0; xx < hmSizeX; xx++)
  486. {
  487. float val = FromCompressedHeight(br.ReadInt16());
  488. if (xx < SizeX && yy < SizeY)
  489. m_heightmap[xx, yy] = val;
  490. }
  491. }
  492. }
  493. ClearTaint();
  494. m_log.DebugFormat("{0} Read (compressed2D) heightmap. Heightmap size=<{1},{2}>. Region size=<{3},{4}>. CompFact={5}",
  495. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY, hmCompressionFactor);
  496. }
  497. }
  498. // Initialize heightmap from blob consisting of:
  499. // int32, int32, int32, float[]
  500. // where the first int32 is format code, next two int32s are the X and y of heightmap data
  501. // This is just sets heightmap info. The actual size of the region was set on this instance's
  502. // creation and any heights not initialized by theis blob are set to the default height.
  503. public void FromCompressedTerrainSerializationV2D(byte[] pBlob)
  504. {
  505. Int32 hmSizeX, hmSizeY;
  506. try
  507. {
  508. using (MemoryStream mstr = new MemoryStream(pBlob))
  509. {
  510. using (BinaryReader br = new BinaryReader(mstr))
  511. {
  512. hmSizeX = br.ReadInt32();
  513. hmSizeY = br.ReadInt32();
  514. // In case database info doesn't match real terrain size, initialize the whole terrain.
  515. ClearLand();
  516. for (int yy = 0; yy < hmSizeY; yy++)
  517. {
  518. for (int xx = 0; xx < hmSizeX; xx++)
  519. {
  520. float val = br.ReadSingle();
  521. if (xx < SizeX && yy < SizeY)
  522. m_heightmap[xx, yy] = val;
  523. }
  524. }
  525. }
  526. }
  527. }
  528. catch (Exception e)
  529. {
  530. ClearTaint();
  531. m_log.ErrorFormat("{0} 2D error: {1} - terrain may be damaged",
  532. LogHeader, e.Message);
  533. return;
  534. }
  535. ClearTaint();
  536. m_log.DebugFormat("{0} V2D Heightmap size=<{1},{2}>. Region size=<{3},{4}>",
  537. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY);
  538. }
  539. // as above but Gzip compressed
  540. public void FromCompressedTerrainSerializationV2DGZip(byte[] pBlob)
  541. {
  542. m_log.InfoFormat("{0} VD2Gzip {1} bytes input",
  543. LogHeader, pBlob.Length);
  544. Int32 hmSizeX, hmSizeY;
  545. try
  546. {
  547. using (MemoryStream outputStream = new MemoryStream())
  548. {
  549. using (MemoryStream inputStream = new MemoryStream(pBlob))
  550. {
  551. using (GZipStream decompressionStream = new GZipStream(inputStream, CompressionMode.Decompress))
  552. {
  553. decompressionStream.Flush();
  554. decompressionStream.CopyTo(outputStream);
  555. }
  556. }
  557. outputStream.Seek(0, SeekOrigin.Begin);
  558. using (BinaryReader br = new BinaryReader(outputStream))
  559. {
  560. hmSizeX = br.ReadInt32();
  561. hmSizeY = br.ReadInt32();
  562. // In case database info doesn't match real terrain size, initialize the whole terrain.
  563. ClearLand();
  564. for (int yy = 0; yy < hmSizeY; yy++)
  565. {
  566. for (int xx = 0; xx < hmSizeX; xx++)
  567. {
  568. float val = br.ReadSingle();
  569. if (xx < SizeX && yy < SizeY)
  570. m_heightmap[xx, yy] = val;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. catch( Exception e)
  577. {
  578. ClearTaint();
  579. m_log.ErrorFormat("{0} V2DGzip error: {1} - terrain may be damaged",
  580. LogHeader, e.Message);
  581. return;
  582. }
  583. ClearTaint();
  584. m_log.DebugFormat("{0} V2DGzip. Heightmap size=<{1},{2}>. Region size=<{3},{4}>",
  585. LogHeader, hmSizeX, hmSizeY, SizeX, SizeY);
  586. }
  587. }
  588. }