WeatherSphere.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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.Region.Framework.Interfaces;
  28. using OpenSim.Region.Framework.Scenes;
  29. namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes
  30. {
  31. /// <summary>
  32. /// Thermal Weathering Paint Brush
  33. /// </summary>
  34. public class WeatherSphere : ITerrainPaintableEffect
  35. {
  36. private const double talus = 0.2;
  37. private const NeighbourSystem type = NeighbourSystem.Moore;
  38. #region Supporting Functions
  39. private static int[] Neighbours(NeighbourSystem neighbourType, int index)
  40. {
  41. int[] coord = new int[2];
  42. index++;
  43. switch (neighbourType)
  44. {
  45. case NeighbourSystem.Moore:
  46. switch (index)
  47. {
  48. case 1:
  49. coord[0] = -1;
  50. coord[1] = -1;
  51. break;
  52. case 2:
  53. coord[0] = -0;
  54. coord[1] = -1;
  55. break;
  56. case 3:
  57. coord[0] = +1;
  58. coord[1] = -1;
  59. break;
  60. case 4:
  61. coord[0] = -1;
  62. coord[1] = -0;
  63. break;
  64. case 5:
  65. coord[0] = -0;
  66. coord[1] = -0;
  67. break;
  68. case 6:
  69. coord[0] = +1;
  70. coord[1] = -0;
  71. break;
  72. case 7:
  73. coord[0] = -1;
  74. coord[1] = +1;
  75. break;
  76. case 8:
  77. coord[0] = -0;
  78. coord[1] = +1;
  79. break;
  80. case 9:
  81. coord[0] = +1;
  82. coord[1] = +1;
  83. break;
  84. default:
  85. break;
  86. }
  87. break;
  88. case NeighbourSystem.VonNeumann:
  89. switch (index)
  90. {
  91. case 1:
  92. coord[0] = 0;
  93. coord[1] = -1;
  94. break;
  95. case 2:
  96. coord[0] = -1;
  97. coord[1] = 0;
  98. break;
  99. case 3:
  100. coord[0] = +1;
  101. coord[1] = 0;
  102. break;
  103. case 4:
  104. coord[0] = 0;
  105. coord[1] = +1;
  106. break;
  107. case 5:
  108. coord[0] = -0;
  109. coord[1] = -0;
  110. break;
  111. default:
  112. break;
  113. }
  114. break;
  115. }
  116. return coord;
  117. }
  118. private enum NeighbourSystem
  119. {
  120. Moore,
  121. VonNeumann
  122. } ;
  123. #endregion
  124. #region ITerrainPaintableEffect Members
  125. public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz, double strength, double duration)
  126. {
  127. strength = TerrainUtil.MetersToSphericalStrength(strength);
  128. int x;
  129. for (x = 0; x < map.Width; x++)
  130. {
  131. int y;
  132. for (y = 0; y < map.Height; y++)
  133. {
  134. if (!mask[x,y])
  135. continue;
  136. double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength);
  137. if (z > 0) // add in non-zero amount
  138. {
  139. const int NEIGHBOUR_ME = 4;
  140. const int NEIGHBOUR_MAX = 9;
  141. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  142. {
  143. if (j != NEIGHBOUR_ME)
  144. {
  145. int[] coords = Neighbours(type, j);
  146. coords[0] += x;
  147. coords[1] += y;
  148. if (coords[0] > map.Width - 1)
  149. continue;
  150. if (coords[1] > map.Height - 1)
  151. continue;
  152. if (coords[0] < 0)
  153. continue;
  154. if (coords[1] < 0)
  155. continue;
  156. double heightF = map[x, y];
  157. double target = map[coords[0], coords[1]];
  158. if (target > heightF + talus)
  159. {
  160. double calc = duration * ((target - heightF) - talus) * z;
  161. heightF += calc;
  162. target -= calc;
  163. }
  164. map[x, y] = heightF;
  165. map[coords[0], coords[1]] = target;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. }
  172. #endregion
  173. }
  174. }