SunModule.cs 7.5 KB

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