Hills.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace OpenSim.Terrain.BasicTerrain
  5. {
  6. static class Hills
  7. {
  8. /// <summary>
  9. /// Generates a series of spheres which are then either max()'d or added together. Inspired by suggestion from jh.
  10. /// </summary>
  11. /// <remarks>3-Clause BSD Licensed</remarks>
  12. /// <param name="number">The number of hills to generate</param>
  13. /// <param name="scale_min">The minimum size of each hill</param>
  14. /// <param name="scale_range">The maximum size of each hill</param>
  15. /// <param name="island">Whether to bias hills towards the center of the map</param>
  16. /// <param name="additive">Whether to add hills together or to pick the largest value</param>
  17. /// <param name="noisy">Generates hill-shaped noise instead of consistent hills</param>
  18. public static void hillsSpheres(float[,] map,int seed, int number, double scale_min, double scale_range, bool island, bool additive, bool noisy)
  19. {
  20. Random random = new Random(seed);
  21. int w = map.GetLength(0);
  22. int h = map.GetLength(1);
  23. int x, y;
  24. int i;
  25. for (i = 0; i < number; i++)
  26. {
  27. double rx = Math.Min(255.0, random.NextDouble() * w);
  28. double ry = Math.Min(255.0, random.NextDouble() * h);
  29. double rand = random.NextDouble();
  30. if (island)
  31. {
  32. // Move everything towards the center
  33. rx -= w / 2;
  34. rx /= 2;
  35. rx += w / 2;
  36. ry -= h / 2;
  37. ry /= 2;
  38. ry += h / 2;
  39. }
  40. for (x = 0; x < w; x++)
  41. {
  42. for (y = 0; y < h; y++)
  43. {
  44. if (noisy)
  45. rand = random.NextDouble();
  46. double z = (scale_min + (scale_range * rand));
  47. z *= z;
  48. z -= ((x - rx) * (x - rx)) + ((y - ry) * (y - ry));
  49. if (z < 0)
  50. z = 0;
  51. if (additive)
  52. {
  53. map[x, y] += (float)z;
  54. }
  55. else
  56. {
  57. map[x, y] = (float)Math.Max(map[x, y], z);
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }