HydraulicErosion.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. public void HydraulicErosion(Channel rain, double evaporation, double solubility, int frequency, int rounds)
  34. {
  35. SetDiff();
  36. Channel water = new Channel(w, h);
  37. Channel sediment = new Channel(w, h);
  38. Channel terrain = this;
  39. Channel waterFlow = new Channel(w, h);
  40. NeighbourSystem type = NeighbourSystem.Moore;
  41. int NEIGHBOUR_ME = 4;
  42. int NEIGHBOUR_MAX = type == NeighbourSystem.Moore ? 9 : 5;
  43. for (int i = 0; i < rounds; i++)
  44. {
  45. water += rain;
  46. sediment = terrain*water;
  47. terrain -= sediment;
  48. for (int x = 1; x < w - 1; x++)
  49. {
  50. for (int y = 1; y < h - 1; y++)
  51. {
  52. double[] heights = new double[NEIGHBOUR_MAX];
  53. double[] diffs = new double[NEIGHBOUR_MAX];
  54. double heightCenter = map[x, y];
  55. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  56. {
  57. if (j != NEIGHBOUR_ME)
  58. {
  59. int[] coords = Neighbours(type, j);
  60. coords[0] += x;
  61. coords[1] += y;
  62. heights[j] = map[coords[0], coords[1]] + water.map[coords[0], coords[1]] +
  63. sediment.map[coords[0], coords[1]];
  64. diffs[j] = heightCenter - heights[j];
  65. }
  66. }
  67. double totalHeight = 0;
  68. double totalHeightDiff = 0;
  69. int totalCellsCounted = 1;
  70. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  71. {
  72. if (j != NEIGHBOUR_ME)
  73. {
  74. if (diffs[j] > 0)
  75. {
  76. totalHeight += heights[j];
  77. totalHeightDiff += diffs[j];
  78. totalCellsCounted++;
  79. }
  80. }
  81. }
  82. if (totalCellsCounted == 1)
  83. continue;
  84. double averageHeight = totalHeight/totalCellsCounted;
  85. double waterAmount = Math.Min(water.map[x, y], heightCenter - averageHeight);
  86. // TODO: Check this.
  87. waterFlow.map[x, y] += waterFlow.map[x, y] - waterAmount;
  88. double totalInverseDiff = waterAmount/totalHeightDiff;
  89. for (int j = 0; j < NEIGHBOUR_MAX; j++)
  90. {
  91. if (j != NEIGHBOUR_ME)
  92. {
  93. int[] coords = Neighbours(type, j);
  94. coords[0] += x;
  95. coords[1] += y;
  96. if (diffs[j] > 0)
  97. {
  98. waterFlow.SetWrap(coords[0], coords[1],
  99. waterFlow.map[coords[0], coords[1]] + diffs[j]*totalInverseDiff);
  100. }
  101. }
  102. }
  103. }
  104. }
  105. water += waterFlow;
  106. waterFlow.Fill(0);
  107. water *= evaporation;
  108. for (int x = 0; x < w; x++)
  109. {
  110. for (int y = 0; y < h; y++)
  111. {
  112. double deposition = sediment.map[x, y] - water.map[x, y]*solubility;
  113. if (deposition > 0)
  114. {
  115. sediment.map[x, y] -= deposition;
  116. terrain.map[x, y] += deposition;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }