WindModule.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 OpenMetaverse;
  30. using Nini.Config;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Environment.Interfaces;
  33. using OpenSim.Region.Environment.Scenes;
  34. namespace OpenSim.Region.Environment.Modules
  35. {
  36. public class WindModule : IWindModule
  37. {
  38. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  39. private int m_frame = 0;
  40. private int m_frame_mod = 150;
  41. private Random rndnums = new Random(System.Environment.TickCount);
  42. private Scene m_scene = null;
  43. private bool ready = false;
  44. private Vector2[] windSpeeds = new Vector2[16 * 16];
  45. private Dictionary<UUID, ulong> m_rootAgents = new Dictionary<UUID, ulong>();
  46. // Current time in elpased seconds since Jan 1st 1970
  47. public void Initialise(Scene scene, IConfigSource config)
  48. {
  49. m_log.Debug("[WIND] Initializing");
  50. m_scene = scene;
  51. m_frame = 0;
  52. // Align ticks with Second Life
  53. // Just in case they don't have the stanzas
  54. try
  55. {
  56. }
  57. catch (Exception e)
  58. {
  59. m_log.Debug("[WIND] Configuration access failed, using defaults. Reason: " + e.Message);
  60. }
  61. scene.EventManager.OnFrame += WindUpdate;
  62. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  63. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  64. scene.EventManager.OnClientClosed += ClientLoggedOut;
  65. scene.RegisterModuleInterface<IWindModule>(this);
  66. GenWindPos();
  67. ready = true;
  68. }
  69. public void PostInitialise()
  70. {
  71. }
  72. public void Close()
  73. {
  74. ready = false;
  75. // Remove our hooks
  76. m_scene.EventManager.OnFrame -= WindUpdate;
  77. // m_scene.EventManager.OnNewClient -= SunToClient;
  78. m_scene.EventManager.OnMakeChildAgent -= MakeChildAgent;
  79. m_scene.EventManager.OnAvatarEnteringNewParcel -= AvatarEnteringParcel;
  80. m_scene.EventManager.OnClientClosed -= ClientLoggedOut;
  81. }
  82. public string Name
  83. {
  84. get { return "WindModule"; }
  85. }
  86. public bool IsSharedModule
  87. {
  88. get { return false; }
  89. }
  90. public Vector2[] WindSpeeds
  91. {
  92. get { return windSpeeds; }
  93. }
  94. public void WindToClient(IClientAPI client)
  95. {
  96. if (ready)
  97. {
  98. //if (!sunFixed)
  99. //GenWindPos(); // Generate shared values once
  100. client.SendWindData(windSpeeds);
  101. m_log.Debug("[WIND] Initial update for new client");
  102. }
  103. }
  104. public void WindUpdate()
  105. {
  106. if (((m_frame++ % m_frame_mod) != 0) || !ready)
  107. {
  108. return;
  109. }
  110. //m_log.Debug("[WIND]:Regenerating...");
  111. GenWindPos(); // Generate shared values once
  112. //int spotxp = 0;
  113. //int spotyp = 0;
  114. //int spotxm = 0;
  115. //int spotym = 0;
  116. List<ScenePresence> avatars = m_scene.GetAvatars();
  117. foreach (ScenePresence avatar in avatars)
  118. {
  119. if (!avatar.IsChildAgent)
  120. {
  121. avatar.ControllingClient.SendWindData(windSpeeds);
  122. }
  123. }
  124. // set estate settings for region access to sun position
  125. //m_scene.RegionInfo.RegionSettings.SunVector = Position;
  126. //m_scene.RegionInfo.EstateSettings.sunHour = GetLindenEstateHourFromCurrentTime();
  127. }
  128. public void ForceWindUpdateToAllClients()
  129. {
  130. GenWindPos(); // Generate shared values once
  131. List<ScenePresence> avatars = m_scene.GetAvatars();
  132. foreach (ScenePresence avatar in avatars)
  133. {
  134. if (!avatar.IsChildAgent)
  135. avatar.ControllingClient.SendWindData(windSpeeds);
  136. }
  137. // set estate settings for region access to sun position
  138. //m_scene.RegionInfo.RegionSettings.SunVector = Position;
  139. //m_scene.RegionInfo.RegionSettings.SunPosition = GetLindenEstateHourFromCurrentTime();
  140. }
  141. /// <summary>
  142. /// Calculate the sun's orbital position and its velocity.
  143. /// </summary>
  144. private void GenWindPos()
  145. {
  146. for (int y = 0; y < 16; y++)
  147. {
  148. for (int x = 0; x < 16; x++)
  149. {
  150. windSpeeds[y * 16 + x].X = (float)(rndnums.NextDouble() * 2d - 1d);
  151. windSpeeds[y * 16 + x].Y = (float)(rndnums.NextDouble() * 2d - 1d);
  152. }
  153. }
  154. }
  155. private void ClientLoggedOut(UUID AgentId)
  156. {
  157. lock (m_rootAgents)
  158. {
  159. if (m_rootAgents.ContainsKey(AgentId))
  160. {
  161. m_rootAgents.Remove(AgentId);
  162. m_log.Info("[WIND]: Removing " + AgentId + ". Agent logged out.");
  163. }
  164. }
  165. }
  166. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
  167. {
  168. lock (m_rootAgents)
  169. {
  170. if (m_rootAgents.ContainsKey(avatar.UUID))
  171. {
  172. m_rootAgents[avatar.UUID] = avatar.RegionHandle;
  173. }
  174. else
  175. {
  176. m_rootAgents.Add(avatar.UUID, avatar.RegionHandle);
  177. WindToClient(avatar.ControllingClient);
  178. }
  179. }
  180. //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
  181. }
  182. private void MakeChildAgent(ScenePresence avatar)
  183. {
  184. lock (m_rootAgents)
  185. {
  186. if (m_rootAgents.ContainsKey(avatar.UUID))
  187. {
  188. if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
  189. {
  190. m_rootAgents.Remove(avatar.UUID);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }