1
0

SunModule.cs 7.5 KB

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