TerrainChannel.cs 8.4 KB

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