HillPlanter.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. namespace libTerrain
  29. {
  30. partial class Channel
  31. {
  32. /// <summary>
  33. /// Generates a series of spheres which are then either max()'d or added together. Inspired by suggestion from jh.
  34. /// </summary>
  35. /// <remarks>3-Clause BSD Licensed</remarks>
  36. /// <param name="number">The number of hills to generate</param>
  37. /// <param name="scale_min">The minimum size of each hill</param>
  38. /// <param name="scale_range">The maximum size of each hill</param>
  39. /// <param name="island">Whether to bias hills towards the center of the map</param>
  40. /// <param name="additive">Whether to add hills together or to pick the largest value</param>
  41. /// <param name="noisy">Generates hill-shaped noise instead of consistent hills</param>
  42. public void HillsSpheres(int number, double scale_min, double scale_range, bool island, bool additive,
  43. bool noisy)
  44. {
  45. SetDiff();
  46. Random random = new Random(seed);
  47. int x, y;
  48. int i;
  49. for (i = 0; i < number; i++)
  50. {
  51. double rx = Math.Min(255.0, random.NextDouble()*w);
  52. double ry = Math.Min(255.0, random.NextDouble()*h);
  53. double rand = random.NextDouble();
  54. if (island)
  55. {
  56. // Move everything towards the center
  57. rx -= w/2;
  58. rx /= 2;
  59. rx += w/2;
  60. ry -= h/2;
  61. ry /= 2;
  62. ry += h/2;
  63. }
  64. for (x = 0; x < w; x++)
  65. {
  66. for (y = 0; y < h; y++)
  67. {
  68. if (noisy)
  69. rand = random.NextDouble();
  70. double z = (scale_min + (scale_range*rand));
  71. z *= z;
  72. z -= ((x - rx)*(x - rx)) + ((y - ry)*(y - ry));
  73. if (z < 0)
  74. z = 0;
  75. if (additive)
  76. {
  77. map[x, y] += z;
  78. }
  79. else
  80. {
  81. map[x, y] = Math.Max(map[x, y], z);
  82. }
  83. }
  84. }
  85. }
  86. Normalise();
  87. }
  88. /// <summary>
  89. /// Generates a series of cones which are then either max()'d or added together. Inspired by suggestion from jh.
  90. /// </summary>
  91. /// <remarks>3-Clause BSD Licensed</remarks>
  92. /// <param name="number">The number of hills to generate</param>
  93. /// <param name="scale_min">The minimum size of each hill</param>
  94. /// <param name="scale_range">The maximum size of each hill</param>
  95. /// <param name="island">Whether to bias hills towards the center of the map</param>
  96. /// <param name="additive">Whether to add hills together or to pick the largest value</param>
  97. /// <param name="noisy">Generates hill-shaped noise instead of consistent hills</param>
  98. public void HillsCones(int number, double scale_min, double scale_range, bool island, bool additive, bool noisy)
  99. {
  100. SetDiff();
  101. Random random = new Random(seed);
  102. int x, y;
  103. int i;
  104. for (i = 0; i < number; i++)
  105. {
  106. double rx = Math.Min(255.0, random.NextDouble()*w);
  107. double ry = Math.Min(255.0, random.NextDouble()*h);
  108. double rand = random.NextDouble();
  109. if (island)
  110. {
  111. // Move everything towards the center
  112. rx -= w/2;
  113. rx /= 2;
  114. rx += w/2;
  115. ry -= h/2;
  116. ry /= 2;
  117. ry += h/2;
  118. }
  119. for (x = 0; x < w; x++)
  120. {
  121. for (y = 0; y < h; y++)
  122. {
  123. if (noisy)
  124. rand = random.NextDouble();
  125. double z = (scale_min + (scale_range*rand));
  126. z -= Math.Sqrt(((x - rx)*(x - rx)) + ((y - ry)*(y - ry)));
  127. if (z < 0)
  128. z = 0;
  129. if (additive)
  130. {
  131. map[x, y] += z;
  132. }
  133. else
  134. {
  135. map[x, y] = Math.Max(map[x, y], z);
  136. }
  137. }
  138. }
  139. }
  140. Normalise();
  141. }
  142. public void HillsBlocks(int number, double scale_min, double scale_range, bool island, bool additive, bool noisy)
  143. {
  144. SetDiff();
  145. Random random = new Random(seed);
  146. int x, y;
  147. int i;
  148. for (i = 0; i < number; i++)
  149. {
  150. double rx = Math.Min(255.0, random.NextDouble()*w);
  151. double ry = Math.Min(255.0, random.NextDouble()*h);
  152. double rand = random.NextDouble();
  153. if (island)
  154. {
  155. // Move everything towards the center
  156. rx -= w/2;
  157. rx /= 2;
  158. rx += w/2;
  159. ry -= h/2;
  160. ry /= 2;
  161. ry += h/2;
  162. }
  163. for (x = 0; x < w; x++)
  164. {
  165. for (y = 0; y < h; y++)
  166. {
  167. if (noisy)
  168. rand = random.NextDouble();
  169. double z = (scale_min + (scale_range*rand));
  170. z -= Math.Abs(x - rx) + Math.Abs(y - ry);
  171. //z -= Math.Sqrt(((x - rx) * (x - rx)) + ((y - ry) * (y - ry)));
  172. if (z < 0)
  173. z = 0;
  174. if (additive)
  175. {
  176. map[x, y] += z;
  177. }
  178. else
  179. {
  180. map[x, y] = Math.Max(map[x, y], z);
  181. }
  182. }
  183. }
  184. }
  185. Normalise();
  186. }
  187. public void HillsSquared(int number, double scale_min, double scale_range, bool island, bool additive,
  188. bool noisy)
  189. {
  190. SetDiff();
  191. Random random = new Random(seed);
  192. int x, y;
  193. int i;
  194. for (i = 0; i < number; i++)
  195. {
  196. double rx = Math.Min(255.0, random.NextDouble()*w);
  197. double ry = Math.Min(255.0, random.NextDouble()*h);
  198. double rand = random.NextDouble();
  199. if (island)
  200. {
  201. // Move everything towards the center
  202. rx -= w/2;
  203. rx /= 2;
  204. rx += w/2;
  205. ry -= h/2;
  206. ry /= 2;
  207. ry += h/2;
  208. }
  209. for (x = 0; x < w; x++)
  210. {
  211. for (y = 0; y < h; y++)
  212. {
  213. if (noisy)
  214. rand = random.NextDouble();
  215. double z = (scale_min + (scale_range*rand));
  216. z *= z*z*z;
  217. double dx = Math.Abs(x - rx);
  218. double dy = Math.Abs(y - ry);
  219. z -= (dx*dx*dx*dx) + (dy*dy*dy*dy);
  220. if (z < 0)
  221. z = 0;
  222. if (additive)
  223. {
  224. map[x, y] += z;
  225. }
  226. else
  227. {
  228. map[x, y] = Math.Max(map[x, y], z);
  229. }
  230. }
  231. }
  232. }
  233. Normalise();
  234. }
  235. }
  236. }