WeatherSphere.cs 7.0 KB

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