/Aurora/Modules/World/Terrain/PaintBrushes/OlsenSphere.cs

https://bitbucket.org/VirtualReality/async-sim-testing-depracated · C# · 242 lines · 167 code · 43 blank · 32 comment · 21 complexity · f258b4cb4498f6418f46a1ec85183842 MD5 · raw file

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