WeatherSphere.cs 7.1 KB

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