TerrainChannel.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 OpenSim.Framework;
  28. using OpenSim.Region.Framework.Interfaces;
  29. using System;
  30. using System.Text;
  31. using System.Xml;
  32. using System.IO;
  33. using System.Xml.Serialization;
  34. namespace OpenSim.Region.Framework.Scenes
  35. {
  36. /// <summary>
  37. /// A new version of the old Channel class, simplified
  38. /// </summary>
  39. public class TerrainChannel : ITerrainChannel
  40. {
  41. private readonly bool[,] taint;
  42. private double[,] map;
  43. public TerrainChannel()
  44. {
  45. map = new double[Constants.RegionSize, Constants.RegionSize];
  46. taint = new bool[Constants.RegionSize / 16,Constants.RegionSize / 16];
  47. int x;
  48. for (x = 0; x < Constants.RegionSize; x++)
  49. {
  50. int y;
  51. for (y = 0; y < Constants.RegionSize; y++)
  52. {
  53. map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 2, 0.125) * 10;
  54. double spherFacA = TerrainUtil.SphericalFactor(x, y, Constants.RegionSize / 2.0, Constants.RegionSize / 2.0, 50) * 0.01;
  55. double spherFacB = TerrainUtil.SphericalFactor(x, y, Constants.RegionSize / 2.0, Constants.RegionSize / 2.0, 100) * 0.001;
  56. if (map[x, y] < spherFacA)
  57. map[x, y] = spherFacA;
  58. if (map[x, y] < spherFacB)
  59. map[x, y] = spherFacB;
  60. }
  61. }
  62. }
  63. public TerrainChannel(double[,] import)
  64. {
  65. map = import;
  66. taint = new bool[import.GetLength(0),import.GetLength(1)];
  67. }
  68. public TerrainChannel(bool createMap)
  69. {
  70. if (createMap)
  71. {
  72. map = new double[Constants.RegionSize,Constants.RegionSize];
  73. taint = new bool[Constants.RegionSize / 16,Constants.RegionSize / 16];
  74. }
  75. }
  76. public TerrainChannel(int w, int h)
  77. {
  78. map = new double[w,h];
  79. taint = new bool[w / 16,h / 16];
  80. }
  81. #region ITerrainChannel Members
  82. public int Width
  83. {
  84. get { return map.GetLength(0); }
  85. }
  86. public int Height
  87. {
  88. get { return map.GetLength(1); }
  89. }
  90. public ITerrainChannel MakeCopy()
  91. {
  92. TerrainChannel copy = new TerrainChannel(false);
  93. copy.map = (double[,]) map.Clone();
  94. return copy;
  95. }
  96. public float[] GetFloatsSerialised()
  97. {
  98. // Move the member variables into local variables, calling
  99. // member variables 256*256 times gets expensive
  100. int w = Width;
  101. int h = Height;
  102. float[] heights = new float[w * h];
  103. int i, j; // map coordinates
  104. int idx = 0; // index into serialized array
  105. for (i = 0; i < h; i++)
  106. {
  107. for (j = 0; j < w; j++)
  108. {
  109. heights[idx++] = (float)map[j, i];
  110. }
  111. }
  112. return heights;
  113. }
  114. public double[,] GetDoubles()
  115. {
  116. return map;
  117. }
  118. public double this[int x, int y]
  119. {
  120. get { return map[x, y]; }
  121. set
  122. {
  123. // Will "fix" terrain hole problems. Although not fantastically.
  124. if (Double.IsNaN(value) || Double.IsInfinity(value))
  125. return;
  126. if (map[x, y] != value)
  127. {
  128. taint[x / 16, y / 16] = true;
  129. map[x, y] = value;
  130. }
  131. }
  132. }
  133. public bool Tainted(int x, int y)
  134. {
  135. if (taint[x / 16, y / 16])
  136. {
  137. taint[x / 16, y / 16] = false;
  138. return true;
  139. }
  140. return false;
  141. }
  142. #endregion
  143. public TerrainChannel Copy()
  144. {
  145. TerrainChannel copy = new TerrainChannel(false);
  146. copy.map = (double[,]) map.Clone();
  147. return copy;
  148. }
  149. public string SaveToXmlString()
  150. {
  151. XmlWriterSettings settings = new XmlWriterSettings();
  152. settings.Encoding = Encoding.UTF8;
  153. using (StringWriter sw = new StringWriter())
  154. {
  155. using (XmlWriter writer = XmlWriter.Create(sw, settings))
  156. {
  157. WriteXml(writer);
  158. }
  159. string output = sw.ToString();
  160. return output;
  161. }
  162. }
  163. private void WriteXml(XmlWriter writer)
  164. {
  165. writer.WriteStartElement(String.Empty, "TerrainMap", String.Empty);
  166. ToXml(writer);
  167. writer.WriteEndElement();
  168. }
  169. public void LoadFromXmlString(string data)
  170. {
  171. StringReader sr = new StringReader(data);
  172. XmlTextReader reader = new XmlTextReader(sr);
  173. reader.Read();
  174. ReadXml(reader);
  175. reader.Close();
  176. sr.Close();
  177. }
  178. private void ReadXml(XmlReader reader)
  179. {
  180. reader.ReadStartElement("TerrainMap");
  181. FromXml(reader);
  182. }
  183. private void ToXml(XmlWriter xmlWriter)
  184. {
  185. float[] mapData = GetFloatsSerialised();
  186. byte[] buffer = new byte[mapData.Length * 4];
  187. for (int i = 0; i < mapData.Length; i++)
  188. {
  189. byte[] value = BitConverter.GetBytes(mapData[i]);
  190. Array.Copy(value, 0, buffer, (i * 4), 4);
  191. }
  192. XmlSerializer serializer = new XmlSerializer(typeof(byte[]));
  193. serializer.Serialize(xmlWriter, buffer);
  194. }
  195. private void FromXml(XmlReader xmlReader)
  196. {
  197. XmlSerializer serializer = new XmlSerializer(typeof(byte[]));
  198. byte[] dataArray = (byte[])serializer.Deserialize(xmlReader);
  199. int index = 0;
  200. for (int y = 0; y < Height; y++)
  201. {
  202. for (int x = 0; x < Width; x++)
  203. {
  204. float value;
  205. value = BitConverter.ToSingle(dataArray, index);
  206. index += 4;
  207. this[x, y] = (double)value;
  208. }
  209. }
  210. }
  211. }
  212. }