CookieCutter.cs 5.1 KB

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