CloudModule.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 System.Collections.Generic;
  29. using Nini.Config;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Framework.Interfaces;
  33. using OpenSim.Region.Framework.Scenes;
  34. namespace OpenSim.Region.CoreModules
  35. {
  36. public class CloudModule : ICloudModule
  37. {
  38. // private static readonly log4net.ILog m_log
  39. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  40. private uint m_frame = 0;
  41. private int m_frameUpdateRate = 1000;
  42. private Random m_rndnums = new Random(Environment.TickCount);
  43. private Scene m_scene = null;
  44. private bool m_ready = false;
  45. private bool m_enabled = false;
  46. private float m_cloudDensity = 1.0F;
  47. private float[] cloudCover = new float[16 * 16];
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. IConfig cloudConfig = config.Configs["Cloud"];
  51. if (cloudConfig != null)
  52. {
  53. m_enabled = cloudConfig.GetBoolean("enabled", false);
  54. m_cloudDensity = cloudConfig.GetFloat("density", 0.5F);
  55. m_frameUpdateRate = cloudConfig.GetInt("cloud_update_rate", 1000);
  56. }
  57. if (m_enabled)
  58. {
  59. m_scene = scene;
  60. scene.EventManager.OnNewClient += CloudsToClient;
  61. scene.RegisterModuleInterface<ICloudModule>(this);
  62. scene.EventManager.OnFrame += CloudUpdate;
  63. GenerateCloudCover();
  64. m_ready = true;
  65. }
  66. }
  67. public void PostInitialise()
  68. {
  69. }
  70. public void Close()
  71. {
  72. if (m_enabled)
  73. {
  74. m_ready = false;
  75. // Remove our hooks
  76. m_scene.EventManager.OnNewClient -= CloudsToClient;
  77. m_scene.EventManager.OnFrame -= CloudUpdate;
  78. }
  79. }
  80. public string Name
  81. {
  82. get { return "CloudModule"; }
  83. }
  84. public bool IsSharedModule
  85. {
  86. get { return false; }
  87. }
  88. public float CloudCover(int x, int y, int z)
  89. {
  90. float cover = 0f;
  91. x /= 16;
  92. y /= 16;
  93. if (x < 0) x = 0;
  94. if (x > 15) x = 15;
  95. if (y < 0) y = 0;
  96. if (y > 15) y = 15;
  97. if (cloudCover != null)
  98. {
  99. cover = cloudCover[y * 16 + x];
  100. }
  101. return cover;
  102. }
  103. private void UpdateCloudCover()
  104. {
  105. float[] newCover = new float[16 * 16];
  106. int rowAbove = new int();
  107. int rowBelow = new int();
  108. int columnLeft = new int();
  109. int columnRight = new int();
  110. for (int x = 0; x < 16; x++)
  111. {
  112. if (x == 0)
  113. {
  114. columnRight = x + 1;
  115. columnLeft = 15;
  116. }
  117. else if (x == 15)
  118. {
  119. columnRight = 0;
  120. columnLeft = x - 1;
  121. }
  122. else
  123. {
  124. columnRight = x + 1;
  125. columnLeft = x - 1;
  126. }
  127. for (int y = 0; y< 16; y++)
  128. {
  129. if (y == 0)
  130. {
  131. rowAbove = y + 1;
  132. rowBelow = 15;
  133. }
  134. else if (y == 15)
  135. {
  136. rowAbove = 0;
  137. rowBelow = y - 1;
  138. }
  139. else
  140. {
  141. rowAbove = y + 1;
  142. rowBelow = y - 1;
  143. }
  144. float neighborAverage = (cloudCover[rowBelow * 16 + columnLeft] +
  145. cloudCover[y * 16 + columnLeft] +
  146. cloudCover[rowAbove * 16 + columnLeft] +
  147. cloudCover[rowBelow * 16 + x] +
  148. cloudCover[rowAbove * 16 + x] +
  149. cloudCover[rowBelow * 16 + columnRight] +
  150. cloudCover[y * 16 + columnRight] +
  151. cloudCover[rowAbove * 16 + columnRight] +
  152. cloudCover[y * 16 + x]) / 9;
  153. newCover[y * 16 + x] = ((neighborAverage / m_cloudDensity) + 0.175f) % 1.0f;
  154. newCover[y * 16 + x] *= m_cloudDensity;
  155. }
  156. }
  157. Array.Copy(newCover, cloudCover, 16 * 16);
  158. }
  159. private void CloudUpdate()
  160. {
  161. if (((m_frame++ % m_frameUpdateRate) != 0) || !m_ready || (m_cloudDensity == 0))
  162. {
  163. return;
  164. }
  165. UpdateCloudCover();
  166. }
  167. public void CloudsToClient(IClientAPI client)
  168. {
  169. if (m_ready)
  170. {
  171. client.SendCloudData(cloudCover);
  172. }
  173. }
  174. /// <summary>
  175. /// Calculate the cloud cover over the region.
  176. /// </summary>
  177. private void GenerateCloudCover()
  178. {
  179. for (int y = 0; y < 16; y++)
  180. {
  181. for (int x = 0; x < 16; x++)
  182. {
  183. cloudCover[y * 16 + x] = (float)(m_rndnums.NextDouble()); // 0 to 1
  184. cloudCover[y * 16 + x] *= m_cloudDensity;
  185. }
  186. }
  187. }
  188. }
  189. }