TerrainChannel.cs 4.8 KB

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