HydraulicErosion.cs 5.7 KB

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