WindModule.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 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 WindModule : IWindModule
  37. {
  38. // private static readonly log4net.ILog m_log
  39. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  40. private int m_frame = 0;
  41. private int m_frame_mod = 150;
  42. private Random rndnums = new Random(Environment.TickCount);
  43. private Scene m_scene = null;
  44. private bool ready = false;
  45. // Simplified windSpeeds based on the fact that the client protocal tracks at a resolution of 16m
  46. private Vector2[] windSpeeds = new Vector2[16 * 16];
  47. private Dictionary<UUID, ulong> m_rootAgents = new Dictionary<UUID, ulong>();
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. m_scene = scene;
  51. m_frame = 0;
  52. scene.EventManager.OnFrame += WindUpdate;
  53. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  54. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  55. scene.EventManager.OnClientClosed += ClientLoggedOut;
  56. scene.RegisterModuleInterface<IWindModule>(this);
  57. GenWindPos();
  58. ready = true;
  59. }
  60. public void PostInitialise()
  61. {
  62. }
  63. public void Close()
  64. {
  65. ready = false;
  66. // Remove our hooks
  67. m_scene.EventManager.OnFrame -= WindUpdate;
  68. // m_scene.EventManager.OnNewClient -= SunToClient;
  69. m_scene.EventManager.OnMakeChildAgent -= MakeChildAgent;
  70. m_scene.EventManager.OnAvatarEnteringNewParcel -= AvatarEnteringParcel;
  71. m_scene.EventManager.OnClientClosed -= ClientLoggedOut;
  72. }
  73. public string Name
  74. {
  75. get { return "WindModule"; }
  76. }
  77. public bool IsSharedModule
  78. {
  79. get { return false; }
  80. }
  81. /// <summary>
  82. /// Retrieve the wind speed at the given region coordinate. This
  83. /// implimentation ignores Z.
  84. /// </summary>
  85. /// <param name="x">0...255</param>
  86. /// <param name="y">0...255</param>
  87. /// <returns></returns>
  88. public Vector3 WindSpeed(int x, int y, int z)
  89. {
  90. Vector3 windVector = new Vector3(0.0f, 0.0f, 0.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 (windSpeeds != null)
  98. {
  99. windVector.X = windSpeeds[y * 16 + x].X;
  100. windVector.Y = windSpeeds[y * 16 + x].Y;
  101. }
  102. return windVector;
  103. }
  104. public void WindToClient(IClientAPI client)
  105. {
  106. if (ready)
  107. {
  108. //if (!sunFixed)
  109. //GenWindPos(); // Generate shared values once
  110. client.SendWindData(windSpeeds);
  111. }
  112. }
  113. public void WindUpdate()
  114. {
  115. if (((m_frame++ % m_frame_mod) != 0) || !ready)
  116. {
  117. return;
  118. }
  119. //m_log.Debug("[WIND]:Regenerating...");
  120. GenWindPos(); // Generate shared values once
  121. //int spotxp = 0;
  122. //int spotyp = 0;
  123. //int spotxm = 0;
  124. //int spotym = 0;
  125. List<ScenePresence> avatars = m_scene.GetAvatars();
  126. foreach (ScenePresence avatar in avatars)
  127. {
  128. if (!avatar.IsChildAgent)
  129. {
  130. avatar.ControllingClient.SendWindData(windSpeeds);
  131. }
  132. }
  133. // set estate settings for region access to sun position
  134. //m_scene.RegionInfo.RegionSettings.SunVector = Position;
  135. //m_scene.RegionInfo.EstateSettings.sunHour = GetLindenEstateHourFromCurrentTime();
  136. }
  137. public void ForceWindUpdateToAllClients()
  138. {
  139. GenWindPos(); // Generate shared values once
  140. List<ScenePresence> avatars = m_scene.GetAvatars();
  141. foreach (ScenePresence avatar in avatars)
  142. {
  143. if (!avatar.IsChildAgent)
  144. avatar.ControllingClient.SendWindData(windSpeeds);
  145. }
  146. // set estate settings for region access to sun position
  147. //m_scene.RegionInfo.RegionSettings.SunVector = Position;
  148. //m_scene.RegionInfo.RegionSettings.SunPosition = GetLindenEstateHourFromCurrentTime();
  149. }
  150. /// <summary>
  151. /// Calculate the sun's orbital position and its velocity.
  152. /// </summary>
  153. private void GenWindPos()
  154. {
  155. for (int y = 0; y < 16; y++)
  156. {
  157. for (int x = 0; x < 16; x++)
  158. {
  159. windSpeeds[y * 16 + x].X = (float)(rndnums.NextDouble() * 2d - 1d);
  160. windSpeeds[y * 16 + x].Y = (float)(rndnums.NextDouble() * 2d - 1d);
  161. }
  162. }
  163. }
  164. private void ClientLoggedOut(UUID AgentId)
  165. {
  166. lock (m_rootAgents)
  167. {
  168. if (m_rootAgents.ContainsKey(AgentId))
  169. {
  170. m_rootAgents.Remove(AgentId);
  171. }
  172. }
  173. }
  174. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
  175. {
  176. lock (m_rootAgents)
  177. {
  178. if (m_rootAgents.ContainsKey(avatar.UUID))
  179. {
  180. m_rootAgents[avatar.UUID] = avatar.RegionHandle;
  181. }
  182. else
  183. {
  184. m_rootAgents.Add(avatar.UUID, avatar.RegionHandle);
  185. WindToClient(avatar.ControllingClient);
  186. }
  187. }
  188. //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
  189. }
  190. private void MakeChildAgent(ScenePresence avatar)
  191. {
  192. lock (m_rootAgents)
  193. {
  194. if (m_rootAgents.ContainsKey(avatar.UUID))
  195. {
  196. if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
  197. {
  198. m_rootAgents.Remove(avatar.UUID);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }