WindModule.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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
  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(System.Environment.TickCount);
  43. private Scene m_scene = null;
  44. private bool ready = false;
  45. private Vector2[] windSpeeds = new Vector2[16 * 16];
  46. private Dictionary<UUID, ulong> m_rootAgents = new Dictionary<UUID, ulong>();
  47. public void Initialise(Scene scene, IConfigSource config)
  48. {
  49. m_scene = scene;
  50. m_frame = 0;
  51. scene.EventManager.OnFrame += WindUpdate;
  52. scene.EventManager.OnMakeChildAgent += MakeChildAgent;
  53. scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
  54. scene.EventManager.OnClientClosed += ClientLoggedOut;
  55. scene.RegisterModuleInterface<IWindModule>(this);
  56. GenWindPos();
  57. ready = true;
  58. }
  59. public void PostInitialise()
  60. {
  61. }
  62. public void Close()
  63. {
  64. ready = false;
  65. // Remove our hooks
  66. m_scene.EventManager.OnFrame -= WindUpdate;
  67. // m_scene.EventManager.OnNewClient -= SunToClient;
  68. m_scene.EventManager.OnMakeChildAgent -= MakeChildAgent;
  69. m_scene.EventManager.OnAvatarEnteringNewParcel -= AvatarEnteringParcel;
  70. m_scene.EventManager.OnClientClosed -= ClientLoggedOut;
  71. }
  72. public string Name
  73. {
  74. get { return "WindModule"; }
  75. }
  76. public bool IsSharedModule
  77. {
  78. get { return false; }
  79. }
  80. public Vector2[] WindSpeeds
  81. {
  82. get { return windSpeeds; }
  83. }
  84. public void WindToClient(IClientAPI client)
  85. {
  86. if (ready)
  87. {
  88. //if (!sunFixed)
  89. //GenWindPos(); // Generate shared values once
  90. client.SendWindData(windSpeeds);
  91. }
  92. }
  93. public void WindUpdate()
  94. {
  95. if (((m_frame++ % m_frame_mod) != 0) || !ready)
  96. {
  97. return;
  98. }
  99. //m_log.Debug("[WIND]:Regenerating...");
  100. GenWindPos(); // Generate shared values once
  101. //int spotxp = 0;
  102. //int spotyp = 0;
  103. //int spotxm = 0;
  104. //int spotym = 0;
  105. List<ScenePresence> avatars = m_scene.GetAvatars();
  106. foreach (ScenePresence avatar in avatars)
  107. {
  108. if (!avatar.IsChildAgent)
  109. {
  110. avatar.ControllingClient.SendWindData(windSpeeds);
  111. }
  112. }
  113. // set estate settings for region access to sun position
  114. //m_scene.RegionInfo.RegionSettings.SunVector = Position;
  115. //m_scene.RegionInfo.EstateSettings.sunHour = GetLindenEstateHourFromCurrentTime();
  116. }
  117. public void ForceWindUpdateToAllClients()
  118. {
  119. GenWindPos(); // Generate shared values once
  120. List<ScenePresence> avatars = m_scene.GetAvatars();
  121. foreach (ScenePresence avatar in avatars)
  122. {
  123. if (!avatar.IsChildAgent)
  124. avatar.ControllingClient.SendWindData(windSpeeds);
  125. }
  126. // set estate settings for region access to sun position
  127. //m_scene.RegionInfo.RegionSettings.SunVector = Position;
  128. //m_scene.RegionInfo.RegionSettings.SunPosition = GetLindenEstateHourFromCurrentTime();
  129. }
  130. /// <summary>
  131. /// Calculate the sun's orbital position and its velocity.
  132. /// </summary>
  133. private void GenWindPos()
  134. {
  135. for (int y = 0; y < 16; y++)
  136. {
  137. for (int x = 0; x < 16; x++)
  138. {
  139. windSpeeds[y * 16 + x].X = (float)(rndnums.NextDouble() * 2d - 1d);
  140. windSpeeds[y * 16 + x].Y = (float)(rndnums.NextDouble() * 2d - 1d);
  141. }
  142. }
  143. }
  144. private void ClientLoggedOut(UUID AgentId)
  145. {
  146. lock (m_rootAgents)
  147. {
  148. if (m_rootAgents.ContainsKey(AgentId))
  149. {
  150. m_rootAgents.Remove(AgentId);
  151. }
  152. }
  153. }
  154. private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
  155. {
  156. lock (m_rootAgents)
  157. {
  158. if (m_rootAgents.ContainsKey(avatar.UUID))
  159. {
  160. m_rootAgents[avatar.UUID] = avatar.RegionHandle;
  161. }
  162. else
  163. {
  164. m_rootAgents.Add(avatar.UUID, avatar.RegionHandle);
  165. WindToClient(avatar.ControllingClient);
  166. }
  167. }
  168. //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
  169. }
  170. private void MakeChildAgent(ScenePresence avatar)
  171. {
  172. lock (m_rootAgents)
  173. {
  174. if (m_rootAgents.ContainsKey(avatar.UUID))
  175. {
  176. if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
  177. {
  178. m_rootAgents.Remove(avatar.UUID);
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }