OlsenSphere.cs 7.5 KB

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