SunModule.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 libsecondlife;
  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.World.Sun
  35. {
  36. public class SunModule : IRegionModule
  37. {
  38. //private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  39. private const int m_default_frame = 100;
  40. private const double m_real_day = 24.0;
  41. private double m_day_length;
  42. private int m_dilation;
  43. private int m_frame;
  44. private int m_frame_mod;
  45. private Scene m_scene;
  46. private long m_start;
  47. #region IRegionModule Members
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. m_start = DateTime.Now.Ticks;
  51. m_frame = 0;
  52. // Just in case they don't have the stanzas
  53. try
  54. {
  55. m_day_length = config.Configs["Sun"].GetDouble("day_length", m_real_day);
  56. m_frame_mod = config.Configs["Sun"].GetInt("frame_rate", m_default_frame);
  57. }
  58. catch (Exception)
  59. {
  60. m_day_length = m_real_day;
  61. m_frame_mod = m_default_frame;
  62. }
  63. m_dilation = (int) (m_real_day / m_day_length);
  64. m_scene = scene;
  65. scene.EventManager.OnFrame += SunUpdate;
  66. scene.EventManager.OnNewClient += SunToClient;
  67. }
  68. public void PostInitialise()
  69. {
  70. }
  71. public void Close()
  72. {
  73. }
  74. public string Name
  75. {
  76. get { return "SunModule"; }
  77. }
  78. public bool IsSharedModule
  79. {
  80. get { return false; }
  81. }
  82. #endregion
  83. public void SunToClient(IClientAPI client)
  84. {
  85. client.SendSunPos(SunPos(HourOfTheDay()), new LLVector3(0, 0.0f, 10.0f));
  86. }
  87. public void SunUpdate()
  88. {
  89. if (m_frame < m_frame_mod)
  90. {
  91. m_frame++;
  92. return;
  93. }
  94. // m_log.InfoFormat("[SUN]: I've got an update {0} => {1}", m_scene.RegionsInfo.RegionName, HourOfTheDay());
  95. List<ScenePresence> avatars = m_scene.GetAvatars();
  96. foreach (ScenePresence avatar in avatars)
  97. {
  98. avatar.ControllingClient.SendSunPos(SunPos(HourOfTheDay()), new LLVector3(0, 0.0f, 10.0f));
  99. }
  100. // set estate settings for region access to sun position
  101. m_scene.RegionInfo.EstateSettings.sunPosition = SunPos(HourOfTheDay());
  102. m_frame = 0;
  103. }
  104. // Hour of the Day figures out the hour of the day as a float.
  105. // The intent here is that we seed hour of the day with real
  106. // time when the simulator starts, then run time forward
  107. // faster based on time dilation factor. This means that
  108. // ticks don't get out of hand
  109. private double HourOfTheDay()
  110. {
  111. long m_addticks = (DateTime.Now.Ticks - m_start) * m_dilation;
  112. DateTime dt = new DateTime(m_start + m_addticks);
  113. return (double) dt.Hour + ((double) dt.Minute / 60.0);
  114. }
  115. private LLVector3 SunPos(double hour)
  116. {
  117. // now we have our radian position
  118. double rad = (hour / m_real_day) * 2 * Math.PI - (Math.PI / 2.0);
  119. double z = Math.Sin(rad);
  120. double x = Math.Cos(rad);
  121. return new LLVector3((float) x, 0f, (float) z);
  122. }
  123. // TODO: clear this out. This is here so that I remember to
  124. // figure out if we need those other packet fields that I've
  125. // left out so far
  126. //
  127. // public void SendViewerTime(int phase)
  128. // {
  129. // Console.WriteLine("SunPhase: {0}", phase);
  130. // SimulatorViewerTimeMessagePacket viewertime = new SimulatorViewerTimeMessagePacket();
  131. // //viewertime.TimeInfo.SecPerDay = 86400;
  132. // // viewertime.TimeInfo.SecPerYear = 31536000;
  133. // viewertime.TimeInfo.SecPerDay = 1000;
  134. // viewertime.TimeInfo.SecPerYear = 365000;
  135. // viewertime.TimeInfo.SunPhase = 1;
  136. // int sunPhase = (phase + 2)/2;
  137. // if ((sunPhase < 6) || (sunPhase > 36))
  138. // {
  139. // viewertime.TimeInfo.SunDirection = new LLVector3(0f, 0.8f, -0.8f);
  140. // Console.WriteLine("sending night");
  141. // }
  142. // else
  143. // {
  144. // if (sunPhase < 12)
  145. // {
  146. // sunPhase = 12;
  147. // }
  148. // sunPhase = sunPhase - 12;
  149. //
  150. // float yValue = 0.1f*(sunPhase);
  151. // Console.WriteLine("Computed SunPhase: {0}, yValue: {1}", sunPhase, yValue);
  152. // if (yValue > 1.2f)
  153. // {
  154. // yValue = yValue - 1.2f;
  155. // }
  156. // if (yValue > 1)
  157. // {
  158. // yValue = 1;
  159. // }
  160. // if (yValue < 0)
  161. // {
  162. // yValue = 0;
  163. // }
  164. // if (sunPhase < 14)
  165. // {
  166. // yValue = 1 - yValue;
  167. // }
  168. // if (sunPhase < 12)
  169. // {
  170. // yValue *= -1;
  171. // }
  172. // viewertime.TimeInfo.SunDirection = new LLVector3(0f, yValue, 0.3f);
  173. // Console.WriteLine("sending sun update " + yValue);
  174. // }
  175. // viewertime.TimeInfo.SunAngVelocity = new LLVector3(0, 0.0f, 10.0f);
  176. // viewertime.TimeInfo.UsecSinceStart = (ulong) Util.UnixTimeSinceEpoch();
  177. // // OutPacket(viewertime);
  178. // }
  179. }
  180. }