TerrainChannel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.IO;
  29. using System.Text;
  30. using System.Reflection;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using OpenSim.Data;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using log4net;
  37. namespace OpenSim.Region.Framework.Scenes
  38. {
  39. /// <summary>
  40. /// A new version of the old Channel class, simplified
  41. /// </summary>
  42. public class TerrainChannel : ITerrainChannel
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private static string LogHeader = "[TERRAIN CHANNEL]";
  46. protected TerrainData m_terrainData;
  47. public int Width { get { return m_terrainData.SizeX; } } // X dimension
  48. // Unfortunately, for historical reasons, in this module 'Width' is X and 'Height' is Y
  49. public int Height { get { return m_terrainData.SizeY; } } // Y dimension
  50. public int Altitude { get { return m_terrainData.SizeZ; } } // Y dimension
  51. // Default, not-often-used builder
  52. public TerrainChannel()
  53. {
  54. m_terrainData = new HeightmapTerrainData((int)Constants.RegionSize, (int)Constants.RegionSize, (int)Constants.RegionHeight);
  55. FlatLand();
  56. // PinHeadIsland();
  57. }
  58. // Create terrain of given size
  59. public TerrainChannel(int pX, int pY)
  60. {
  61. m_terrainData = new HeightmapTerrainData(pX, pY, (int)Constants.RegionHeight);
  62. }
  63. // Create terrain of specified size and initialize with specified terrain.
  64. // TODO: join this with the terrain initializers.
  65. public TerrainChannel(String type, int pX, int pY, int pZ)
  66. {
  67. m_terrainData = new HeightmapTerrainData(pX, pY, pZ);
  68. if (type.Equals("flat"))
  69. FlatLand();
  70. else
  71. PinHeadIsland();
  72. }
  73. // Create channel passed a heightmap and expected dimensions of the region.
  74. // The heightmap might not fit the passed size so accomodations must be made.
  75. public TerrainChannel(double[,] pM, int pSizeX, int pSizeY, int pAltitude)
  76. {
  77. int hmSizeX = pM.GetLength(0);
  78. int hmSizeY = pM.GetLength(1);
  79. m_terrainData = new HeightmapTerrainData(pSizeX, pSizeY, pAltitude);
  80. for (int xx = 0; xx < pSizeX; xx++)
  81. for (int yy = 0; yy < pSizeY; yy++)
  82. if (xx > hmSizeX || yy > hmSizeY)
  83. m_terrainData[xx, yy] = TerrainData.DefaultTerrainHeight;
  84. else
  85. m_terrainData[xx, yy] = (float)pM[xx, yy];
  86. }
  87. public TerrainChannel(TerrainData pTerrData)
  88. {
  89. m_terrainData = pTerrData;
  90. }
  91. #region ITerrainChannel Members
  92. // ITerrainChannel.MakeCopy()
  93. public ITerrainChannel MakeCopy()
  94. {
  95. return this.Copy();
  96. }
  97. // ITerrainChannel.GetTerrainData()
  98. public TerrainData GetTerrainData()
  99. {
  100. return m_terrainData;
  101. }
  102. // ITerrainChannel.GetFloatsSerialized()
  103. // This one dimensional version is ordered so height = map[y*sizeX+x];
  104. // DEPRECATED: don't use this function as it does not retain the dimensions of the terrain
  105. // and the caller will probably do the wrong thing if the terrain is not the legacy 256x256.
  106. public float[] GetFloatsSerialised()
  107. {
  108. int points = Width * Height;
  109. float[] heights = new float[points];
  110. int idx = 0;
  111. for (int jj = 0; jj < Height; jj++)
  112. for (int ii = 0; ii < Width; ii++)
  113. {
  114. heights[idx++] = m_terrainData[ii, jj];
  115. }
  116. return heights;
  117. }
  118. // ITerrainChannel.GetDoubles()
  119. public double[,] GetDoubles()
  120. {
  121. double[,] heights = new double[Width, Height];
  122. int idx = 0; // index into serialized array
  123. for (int ii = 0; ii < Width; ii++)
  124. {
  125. for (int jj = 0; jj < Height; jj++)
  126. {
  127. heights[ii, jj] = (double)m_terrainData[ii, jj];
  128. idx++;
  129. }
  130. }
  131. return heights;
  132. }
  133. // ITerrainChannel.this[x,y]
  134. public double this[int x, int y]
  135. {
  136. get {
  137. if (x < 0 || x >= Width || y < 0 || y >= Height)
  138. return 0;
  139. return (double)m_terrainData[x, y];
  140. }
  141. set
  142. {
  143. if (Double.IsNaN(value) || Double.IsInfinity(value))
  144. return;
  145. m_terrainData[x, y] = (float)value;
  146. }
  147. }
  148. // ITerrainChannel.GetHieghtAtXYZ(x, y, z)
  149. public float GetHeightAtXYZ(float x, float y, float z)
  150. {
  151. if (x < 0 || x >= Width || y < 0 || y >= Height)
  152. return 0;
  153. return m_terrainData[(int)x, (int)y];
  154. }
  155. // ITerrainChannel.Tainted()
  156. public bool Tainted(int x, int y)
  157. {
  158. return m_terrainData.IsTaintedAt(x, y);
  159. }
  160. // ITerrainChannel.SaveToXmlString()
  161. public string SaveToXmlString()
  162. {
  163. XmlWriterSettings settings = new XmlWriterSettings();
  164. settings.Encoding = Util.UTF8;
  165. using (StringWriter sw = new StringWriter())
  166. {
  167. using (XmlWriter writer = XmlWriter.Create(sw, settings))
  168. {
  169. WriteXml(writer);
  170. }
  171. string output = sw.ToString();
  172. return output;
  173. }
  174. }
  175. // ITerrainChannel.LoadFromXmlString()
  176. public void LoadFromXmlString(string data)
  177. {
  178. StringReader sr = new StringReader(data);
  179. XmlTextReader reader = new XmlTextReader(sr);
  180. reader.Read();
  181. ReadXml(reader);
  182. reader.Close();
  183. sr.Close();
  184. }
  185. #endregion
  186. public TerrainChannel Copy()
  187. {
  188. TerrainChannel copy = new TerrainChannel();
  189. copy.m_terrainData = m_terrainData.Clone();
  190. return copy;
  191. }
  192. private void WriteXml(XmlWriter writer)
  193. {
  194. if (Width == Constants.RegionSize && Height == Constants.RegionSize)
  195. {
  196. // Downward compatibility for legacy region terrain maps.
  197. // If region is exactly legacy size, return the old format XML.
  198. writer.WriteStartElement(String.Empty, "TerrainMap", String.Empty);
  199. ToXml(writer);
  200. writer.WriteEndElement();
  201. }
  202. else
  203. {
  204. // New format XML that includes width and length.
  205. writer.WriteStartElement(String.Empty, "TerrainMap2", String.Empty);
  206. ToXml2(writer);
  207. writer.WriteEndElement();
  208. }
  209. }
  210. private void ReadXml(XmlReader reader)
  211. {
  212. // Check the first element. If legacy element, use the legacy reader.
  213. if (reader.IsStartElement("TerrainMap"))
  214. {
  215. reader.ReadStartElement("TerrainMap");
  216. FromXml(reader);
  217. }
  218. else
  219. {
  220. reader.ReadStartElement("TerrainMap2");
  221. FromXml2(reader);
  222. }
  223. }
  224. // Write legacy terrain map. Presumed to be 256x256 of data encoded as floats in a byte array.
  225. private void ToXml(XmlWriter xmlWriter)
  226. {
  227. float[] mapData = GetFloatsSerialised();
  228. byte[] buffer = new byte[mapData.Length * 4];
  229. for (int i = 0; i < mapData.Length; i++)
  230. {
  231. byte[] value = BitConverter.GetBytes(mapData[i]);
  232. Array.Copy(value, 0, buffer, (i * 4), 4);
  233. }
  234. XmlSerializer serializer = new XmlSerializer(typeof(byte[]));
  235. serializer.Serialize(xmlWriter, buffer);
  236. }
  237. // Read legacy terrain map. Presumed to be 256x256 of data encoded as floats in a byte array.
  238. private void FromXml(XmlReader xmlReader)
  239. {
  240. XmlSerializer serializer = new XmlSerializer(typeof(byte[]));
  241. byte[] dataArray = (byte[])serializer.Deserialize(xmlReader);
  242. int index = 0;
  243. m_terrainData = new HeightmapTerrainData(Height, Width, (int)Constants.RegionHeight);
  244. for (int y = 0; y < Height; y++)
  245. {
  246. for (int x = 0; x < Width; x++)
  247. {
  248. float value;
  249. value = BitConverter.ToSingle(dataArray, index);
  250. index += 4;
  251. this[x, y] = (double)value;
  252. }
  253. }
  254. }
  255. private class TerrainChannelXMLPackage
  256. {
  257. public int Version;
  258. public int SizeX;
  259. public int SizeY;
  260. public int SizeZ;
  261. public float CompressionFactor;
  262. public short[] Map;
  263. public TerrainChannelXMLPackage(int pX, int pY, int pZ, float pCompressionFactor, short[] pMap)
  264. {
  265. Version = 1;
  266. SizeX = pX;
  267. SizeY = pY;
  268. SizeZ = pZ;
  269. CompressionFactor = pCompressionFactor;
  270. Map = pMap;
  271. }
  272. }
  273. // New terrain serialization format that includes the width and length.
  274. private void ToXml2(XmlWriter xmlWriter)
  275. {
  276. TerrainChannelXMLPackage package = new TerrainChannelXMLPackage(Width, Height, Altitude, m_terrainData.CompressionFactor,
  277. m_terrainData.GetCompressedMap());
  278. XmlSerializer serializer = new XmlSerializer(typeof(TerrainChannelXMLPackage));
  279. serializer.Serialize(xmlWriter, package);
  280. }
  281. // New terrain serialization format that includes the width and length.
  282. private void FromXml2(XmlReader xmlReader)
  283. {
  284. XmlSerializer serializer = new XmlSerializer(typeof(TerrainChannelXMLPackage));
  285. TerrainChannelXMLPackage package = (TerrainChannelXMLPackage)serializer.Deserialize(xmlReader);
  286. m_terrainData = new HeightmapTerrainData(package.Map, package.CompressionFactor, package.SizeX, package.SizeY, package.SizeZ);
  287. }
  288. // Fill the heightmap with the center bump terrain
  289. private void PinHeadIsland()
  290. {
  291. for (int x = 0; x < Width; x++)
  292. {
  293. for (int y = 0; y < Height; y++)
  294. {
  295. m_terrainData[x, y] = (float)TerrainUtil.PerlinNoise2D(x, y, 2, 0.125) * 10;
  296. float spherFacA = (float)(TerrainUtil.SphericalFactor(x, y, m_terrainData.SizeX / 2.0, m_terrainData.SizeY / 2.0, 50) * 0.01d);
  297. float spherFacB = (float)(TerrainUtil.SphericalFactor(x, y, m_terrainData.SizeX / 2.0, m_terrainData.SizeY / 2.0, 100) * 0.001d);
  298. if (m_terrainData[x, y]< spherFacA)
  299. m_terrainData[x, y]= spherFacA;
  300. if (m_terrainData[x, y]< spherFacB)
  301. m_terrainData[x, y] = spherFacB;
  302. }
  303. }
  304. }
  305. private void FlatLand()
  306. {
  307. m_terrainData.ClearLand();
  308. }
  309. }
  310. }