TerrainChannel.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 OpenSim.Framework;
  28. using OpenSim.Region.Environment.Interfaces;
  29. namespace OpenSim.Region.Environment.Modules.World.Terrain
  30. {
  31. /// <summary>
  32. /// A new version of the old Channel class, simplified
  33. /// </summary>
  34. public class TerrainChannel : ITerrainChannel
  35. {
  36. private readonly bool[,] taint;
  37. private double[,] map;
  38. public TerrainChannel()
  39. {
  40. map = new double[Constants.RegionSize,Constants.RegionSize];
  41. taint = new bool[Constants.RegionSize / 16,Constants.RegionSize / 16];
  42. int x;
  43. for (x = 0; x < Constants.RegionSize; x++)
  44. {
  45. int y;
  46. for (y = 0; y < Constants.RegionSize; y++)
  47. {
  48. map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 3, 0.25) * 10;
  49. double spherFac = TerrainUtil.SphericalFactor(x, y, Constants.RegionSize / 2, Constants.RegionSize / 2, 50) * 0.01;
  50. if (map[x, y] < spherFac)
  51. {
  52. map[x, y] = spherFac;
  53. }
  54. }
  55. }
  56. }
  57. public TerrainChannel(double[,] import)
  58. {
  59. map = import;
  60. taint = new bool[import.GetLength(0),import.GetLength(1)];
  61. }
  62. public TerrainChannel(bool createMap)
  63. {
  64. if (createMap)
  65. {
  66. map = new double[Constants.RegionSize,Constants.RegionSize];
  67. taint = new bool[Constants.RegionSize / 16,Constants.RegionSize / 16];
  68. }
  69. }
  70. public TerrainChannel(int w, int h)
  71. {
  72. map = new double[w,h];
  73. taint = new bool[w / 16,h / 16];
  74. }
  75. #region ITerrainChannel Members
  76. public int Width
  77. {
  78. get { return map.GetLength(0); }
  79. }
  80. public int Height
  81. {
  82. get { return map.GetLength(1); }
  83. }
  84. public ITerrainChannel MakeCopy()
  85. {
  86. TerrainChannel copy = new TerrainChannel(false);
  87. copy.map = (double[,]) map.Clone();
  88. return copy;
  89. }
  90. public float[] GetFloatsSerialised()
  91. {
  92. float[] heights = new float[Width * Height];
  93. int i;
  94. for (i = 0; i < Width * Height; i++)
  95. {
  96. heights[i] = (float) map[i % Width, i / Width];
  97. }
  98. return heights;
  99. }
  100. public double[,] GetDoubles()
  101. {
  102. return map;
  103. }
  104. public double this[int x, int y]
  105. {
  106. get { return map[x, y]; }
  107. set
  108. {
  109. if (map[x, y] != value)
  110. {
  111. taint[x / 16, y / 16] = true;
  112. map[x, y] = value;
  113. }
  114. }
  115. }
  116. public bool Tainted(int x, int y)
  117. {
  118. if (taint[x / 16, y / 16])
  119. {
  120. taint[x / 16, y / 16] = false;
  121. return true;
  122. }
  123. else
  124. {
  125. return false;
  126. }
  127. }
  128. #endregion
  129. public TerrainChannel Copy()
  130. {
  131. TerrainChannel copy = new TerrainChannel(false);
  132. copy.map = (double[,]) map.Clone();
  133. return copy;
  134. }
  135. }
  136. }