CookieCutter.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. using OpenSim.Region.Environment.Interfaces;
  29. using OpenSim.Region.Environment.Modules.World.Terrain.PaintBrushes;
  30. namespace OpenSim.Region.Environment.Modules.World.Terrain.Effects
  31. {
  32. internal class CookieCutter : ITerrainEffect
  33. {
  34. #region ITerrainEffect Members
  35. public void RunEffect(ITerrainChannel map)
  36. {
  37. ITerrainPaintableEffect eroder = new WeatherSphere();
  38. bool[,] cliffMask = new bool[map.Width,map.Height];
  39. bool[,] channelMask = new bool[map.Width,map.Height];
  40. bool[,] smoothMask = new bool[map.Width,map.Height];
  41. bool[,] allowMask = new bool[map.Width,map.Height];
  42. Console.WriteLine("S1");
  43. // Step one, generate rough mask
  44. int x, y;
  45. for (x = 0; x < map.Width; x++)
  46. {
  47. for (y = 0; y < map.Height; y++)
  48. {
  49. Console.Write(".");
  50. smoothMask[x, y] = true;
  51. allowMask[x,y] = true;
  52. // Start underwater
  53. map[x, y] = TerrainUtil.PerlinNoise2D(x, y, 3, 0.25) * 5;
  54. // Add a little height. (terrain should now be above water, mostly.)
  55. map[x, y] += 20;
  56. const int channelsX = 4;
  57. int channelWidth = (map.Width / channelsX / 4);
  58. const int channelsY = 4;
  59. int channelHeight = (map.Height / channelsY / 4);
  60. SetLowerChannel(map, cliffMask, channelMask, x, y, channelsX, channelWidth, map.Width, x);
  61. SetLowerChannel(map, cliffMask, channelMask, x, y, channelsY, channelHeight, map.Height, y);
  62. }
  63. }
  64. Console.WriteLine("S2");
  65. //smooth.FloodEffect(map, smoothMask, 4.0);
  66. Console.WriteLine("S3");
  67. for (x = 0; x < map.Width; x++)
  68. {
  69. for (y = 0; y < map.Height; y++)
  70. {
  71. if (cliffMask[x, y])
  72. eroder.PaintEffect(map, allowMask, x, y, -1, 4, 0.1);
  73. }
  74. }
  75. for (x = 0; x < map.Width; x += 2)
  76. {
  77. for (y = 0; y < map.Height; y += 2)
  78. {
  79. if (map[x, y] < 0.1)
  80. map[x, y] = 0.1;
  81. if (map[x, y] > 256)
  82. map[x, y] = 256;
  83. }
  84. }
  85. //smooth.FloodEffect(map, smoothMask, 4.0);
  86. }
  87. #endregion
  88. private static void SetLowerChannel(ITerrainChannel map, bool[,] cliffMask, bool[,] channelMask, int x, int y, int numChannels, int channelWidth,
  89. int mapSize, int rp)
  90. {
  91. for (int i = 0; i < numChannels; i++)
  92. {
  93. double distanceToLine = Math.Abs(rp - ((mapSize / numChannels) * i));
  94. if (distanceToLine < channelWidth)
  95. {
  96. if (channelMask[x, y])
  97. return;
  98. // Remove channels
  99. map[x, y] -= 10;
  100. channelMask[x, y] = true;
  101. }
  102. if (distanceToLine < 1)
  103. {
  104. cliffMask[x, y] = true;
  105. }
  106. }
  107. }
  108. }
  109. }