OlsenSphere.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 System;
  28. using OpenSim.Region.Framework.Interfaces;
  29. using OpenSim.Region.Framework.Scenes;
  30. namespace OpenSim.Region.CoreModules.World.Terrain.PaintBrushes
  31. {
  32. /// <summary>
  33. /// Speed-Optimised Hybrid Erosion Brush
  34. ///
  35. /// As per Jacob Olsen's Paper
  36. /// http://www.oddlabs.com/download/terrain_generation.pdf
  37. /// </summary>
  38. public class OlsenSphere : ITerrainPaintableEffect
  39. {
  40. private const double nConst = 1024.0;
  41. private const NeighbourSystem type = NeighbourSystem.Moore;
  42. #region Supporting Functions
  43. private static int[] Neighbours(NeighbourSystem neighbourType, int index)
  44. {
  45. int[] coord = new int[2];
  46. index++;
  47. switch (neighbourType)
  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. private enum NeighbourSystem
  123. {
  124. Moore,
  125. VonNeumann
  126. } ;
  127. #endregion
  128. #region ITerrainPaintableEffect Members
  129. public void PaintEffect(ITerrainChannel map, bool[,] mask, double rx, double ry, double rz,
  130. double strength, double duration, int startX, int endX, int startY, int endY)
  131. {
  132. strength = TerrainUtil.MetersToSphericalStrength(strength);
  133. int x, y;
  134. for (x = startX; x <= endX; x++)
  135. {
  136. for (y = startY; y <= endY; y++)
  137. {
  138. if (!mask[x, y])
  139. continue;
  140. double z = TerrainUtil.SphericalFactor(x, y, rx, ry, strength);
  141. if (z > 0) // add in non-zero amount
  142. {
  143. const int NEIGHBOUR_ME = 4;
  144. const int NEIGHBOUR_MAX = 9;
  145. double max = Double.MinValue;
  146. int loc = 0;
  147. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  148. {
  149. if (j != NEIGHBOUR_ME)
  150. {
  151. int[] coords = Neighbours(type, j);
  152. coords[0] += x;
  153. coords[1] += y;
  154. if (coords[0] > map.Width - 1)
  155. continue;
  156. if (coords[1] > map.Height - 1)
  157. continue;
  158. if (coords[0] < 0)
  159. continue;
  160. if (coords[1] < 0)
  161. continue;
  162. double cellmax = map[x, y] - map[coords[0], coords[1]];
  163. if (cellmax > max)
  164. {
  165. max = cellmax;
  166. loc = j;
  167. }
  168. }
  169. }
  170. double T = nConst / ((map.Width + map.Height) / 2.0);
  171. // Apply results
  172. if (0 < max && max <= T)
  173. {
  174. int[] maxCoords = Neighbours(type, loc);
  175. double heightDelta = 0.5 * max * z * duration;
  176. map[x, y] -= heightDelta;
  177. map[x + maxCoords[0], y + maxCoords[1]] += heightDelta;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. #endregion
  184. }
  185. }