CloudModule.cs 7.5 KB

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