CloudModule.cs 8.8 KB

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