HeightMapGenHills.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) OpenSim project, http://sim.opensecondlife.org/
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of the <organization> nor the
  12. * names of its contributors may be used to endorse or promote products
  13. * derived from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY <copyright holder> ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *
  26. */
  27. using System;
  28. namespace OpenSim.Framework.Terrain
  29. {
  30. public class HeightmapGenHills
  31. {
  32. private Random Rand = new Random();
  33. private int NumHills;
  34. private float HillMin;
  35. private float HillMax;
  36. private bool Island;
  37. private float[] heightmap;
  38. public float[] GenerateHeightmap(int numHills, float hillMin, float hillMax, bool island)
  39. {
  40. NumHills = numHills;
  41. HillMin = hillMin;
  42. HillMax = hillMax;
  43. Island = island;
  44. heightmap = new float[256 * 256];
  45. for (int i = 0; i < numHills; i++)
  46. {
  47. AddHill();
  48. }
  49. Normalize();
  50. return heightmap;
  51. }
  52. private void AddHill()
  53. {
  54. float x, y;
  55. float radius = RandomRange(HillMin, HillMax);
  56. if (Island)
  57. {
  58. // Which direction from the center of the map the hill is placed
  59. float theta = RandomRange(0, 6.28f);
  60. // How far from the center of the map to place the hill. The radius
  61. // is subtracted from the range to prevent any part of the hill from
  62. // reaching the edge of the map
  63. float distance = RandomRange(radius / 2.0f, 128.0f - radius);
  64. x = 128.0f + (float)Math.Cos(theta) * distance;
  65. y = 128.0f + (float)Math.Sin(theta) * distance;
  66. }
  67. else
  68. {
  69. x = RandomRange(-radius, 256.0f + radius);
  70. y = RandomRange(-radius, 256.0f + radius);
  71. }
  72. float radiusSq = radius * radius;
  73. float distSq;
  74. float height;
  75. int xMin = (int)(x - radius) - 1;
  76. int xMax = (int)(x + radius) + 1;
  77. if (xMin < 0) xMin = 0;
  78. if (xMax > 255) xMax = 255;
  79. int yMin = (int)(y - radius) - 1;
  80. int yMax = (int)(y + radius) + 1;
  81. if (yMin < 0) yMin = 0;
  82. if (yMax > 255) yMax = 255;
  83. // Loop through each affected cell and determine the height at that point
  84. for (int v = yMin; v <= yMax; ++v)
  85. {
  86. float fv = (float)v;
  87. for (int h = xMin; h <= xMax; ++h)
  88. {
  89. float fh = (float)h;
  90. // Determine how far from the center of this hill this point is
  91. distSq = (x - fh) * (x - fh) + (y - fv) * (y - fv);
  92. height = radiusSq - distSq;
  93. // Don't add negative hill values
  94. if (height > 0.0f) heightmap[h + v * 256] += height;
  95. }
  96. }
  97. }
  98. private void Normalize()
  99. {
  100. float min = heightmap[0];
  101. float max = heightmap[0];
  102. for (int x = 0; x < 256; x++)
  103. {
  104. for (int y = 0; y < 256; y++)
  105. {
  106. if (heightmap[x + y * 256] < min) min = heightmap[x + y * 256];
  107. if (heightmap[x + y * 256] > max) max = heightmap[x + y * 256];
  108. }
  109. }
  110. // Avoid a rare divide by zero
  111. if (min != max)
  112. {
  113. for (int x = 0; x < 256; x++)
  114. {
  115. for (int y = 0; y < 256; y++)
  116. {
  117. heightmap[x + y * 256] = ((heightmap[x + y * 256] - min) / (max - min)) * (HillMax - HillMin);
  118. }
  119. }
  120. }
  121. }
  122. private float RandomRange(float min, float max)
  123. {
  124. return (float)Rand.NextDouble() * (max - min) + min;
  125. }
  126. }
  127. }