Terrain.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 OpenSim 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.IO;
  29. using System.Reflection;
  30. using OpenSim.Framework;
  31. using log4net;
  32. using OpenMetaverse;
  33. namespace OpenSim.Data.NHibernate
  34. {
  35. public class Terrain
  36. {
  37. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  38. private double[,] map;
  39. private UUID regionID;
  40. public Terrain(UUID Region, double[,] array)
  41. {
  42. map = array;
  43. regionID = Region;
  44. }
  45. public Terrain()
  46. {
  47. map = new double[Constants.RegionSize, Constants.RegionSize];
  48. map.Initialize();
  49. regionID = UUID.Zero;
  50. }
  51. public UUID RegionID
  52. {
  53. get { return regionID; }
  54. set { regionID = value; }
  55. }
  56. public byte[] MapData
  57. {
  58. get { return serializeTerrain(map); }
  59. set { map = parseTerrain(value); }
  60. }
  61. public double[,] Doubles
  62. {
  63. get {return map;}
  64. set {map = value;}
  65. }
  66. private static double[,] parseTerrain(byte[] data)
  67. {
  68. double[,] terret = new double[256,256];
  69. terret.Initialize();
  70. MemoryStream str = new MemoryStream(data);
  71. BinaryReader br = new BinaryReader(str);
  72. try {
  73. for (int x = 0; x < 256; x++)
  74. {
  75. for (int y = 0; y < 256; y++)
  76. {
  77. terret[x, y] = br.ReadDouble();
  78. }
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. m_log.Error("Issue parsing Map", e);
  84. }
  85. return terret;
  86. }
  87. private static byte[] serializeTerrain(double[,] val)
  88. {
  89. MemoryStream str = new MemoryStream((int)(65536 * sizeof (double)));
  90. BinaryWriter bw = new BinaryWriter(str);
  91. // TODO: COMPATIBILITY - Add byte-order conversions
  92. for (int x = 0; x < 256; x++)
  93. {
  94. for (int y = 0; y < 256; y++)
  95. {
  96. double height = val[x, y];
  97. if (height <= 0.0)
  98. height = double.Epsilon;
  99. bw.Write(height);
  100. }
  101. }
  102. return str.ToArray();
  103. }
  104. }
  105. }