HillPlanter.cs 9.6 KB

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