aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs
diff options
context:
space:
mode:
authorDr Scofield2009-02-10 13:10:57 +0000
committerDr Scofield2009-02-10 13:10:57 +0000
commit180be7de07014aa33bc6066f12a0819b731c1c9d (patch)
tree3aa13af3cda4b808fa9453655875327699b61311 /OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs
parentStopgap measure: To use gridlaunch, or GUI, start opensim with (diff)
downloadopensim-SC_OLD-180be7de07014aa33bc6066f12a0819b731c1c9d.zip
opensim-SC_OLD-180be7de07014aa33bc6066f12a0819b731c1c9d.tar.gz
opensim-SC_OLD-180be7de07014aa33bc6066f12a0819b731c1c9d.tar.bz2
opensim-SC_OLD-180be7de07014aa33bc6066f12a0819b731c1c9d.tar.xz
this is step 2 of 2 of the OpenSim.Region.Environment refactor.
NOTHING has been deleted or moved off to forge at this point. what has happened is that OpenSim.Region.Environment.Modules has been split in two: - OpenSim.Region.CoreModules: all those modules that are either directly or indirectly referenced from other OpenSim packages, or that provide functionality that the OpenSim developer community considers core functionality: CoreModules/Agent/AssetTransaction CoreModules/Agent/Capabilities CoreModules/Agent/TextureDownload CoreModules/Agent/TextureSender CoreModules/Agent/TextureSender/Tests CoreModules/Agent/Xfer CoreModules/Avatar/AvatarFactory CoreModules/Avatar/Chat/ChatModule CoreModules/Avatar/Combat CoreModules/Avatar/Currency/SampleMoney CoreModules/Avatar/Dialog CoreModules/Avatar/Friends CoreModules/Avatar/Gestures CoreModules/Avatar/Groups CoreModules/Avatar/InstantMessage CoreModules/Avatar/Inventory CoreModules/Avatar/Inventory/Archiver CoreModules/Avatar/Inventory/Transfer CoreModules/Avatar/Lure CoreModules/Avatar/ObjectCaps CoreModules/Avatar/Profiles CoreModules/Communications/Local CoreModules/Communications/REST CoreModules/Framework/EventQueue CoreModules/Framework/InterfaceCommander CoreModules/Hypergrid CoreModules/InterGrid CoreModules/Scripting/DynamicTexture CoreModules/Scripting/EMailModules CoreModules/Scripting/HttpRequest CoreModules/Scripting/LoadImageURL CoreModules/Scripting/VectorRender CoreModules/Scripting/WorldComm CoreModules/Scripting/XMLRPC CoreModules/World/Archiver CoreModules/World/Archiver/Tests CoreModules/World/Estate CoreModules/World/Land CoreModules/World/Permissions CoreModules/World/Serialiser CoreModules/World/Sound CoreModules/World/Sun CoreModules/World/Terrain CoreModules/World/Terrain/DefaultEffects CoreModules/World/Terrain/DefaultEffects/bin CoreModules/World/Terrain/DefaultEffects/bin/Debug CoreModules/World/Terrain/Effects CoreModules/World/Terrain/FileLoaders CoreModules/World/Terrain/FloodBrushes CoreModules/World/Terrain/PaintBrushes CoreModules/World/Terrain/Tests CoreModules/World/Vegetation CoreModules/World/Wind CoreModules/World/WorldMap - OpenSim.Region.OptionalModules: all those modules that are not core modules: OptionalModules/Avatar/Chat/IRC-stuff OptionalModules/Avatar/Concierge OptionalModules/Avatar/Voice/AsterixVoice OptionalModules/Avatar/Voice/SIPVoice OptionalModules/ContentManagementSystem OptionalModules/Grid/Interregion OptionalModules/Python OptionalModules/SvnSerialiser OptionalModules/World/NPC OptionalModules/World/TreePopulator
Diffstat (limited to 'OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs')
-rw-r--r--OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs434
1 files changed, 0 insertions, 434 deletions
diff --git a/OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs b/OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs
deleted file mode 100644
index ee96d58..0000000
--- a/OpenSim/Region/Environment/Modules/World/Sun/SunModule.cs
+++ /dev/null
@@ -1,434 +0,0 @@
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
28using System;
29using System.Collections.Generic;
30using OpenMetaverse;
31using Nini.Config;
32using OpenSim.Framework;
33using OpenSim.Region.Framework.Interfaces;
34using OpenSim.Region.Framework.Scenes;
35
36namespace OpenSim.Region.Environment.Modules
37{
38 public class SunModule : IRegionModule
39 {
40
41 private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
42
43 private const double SeasonalTilt = 0.03 * Math.PI; // A daily shift of approximately 1.7188 degrees
44 private const double AverageTilt = -0.25 * Math.PI; // A 45 degree tilt
45 private const double SunCycle = 2.0D * Math.PI; // A perfect circle measured in radians
46 private const double SeasonalCycle = 2.0D * Math.PI; // Ditto
47
48 //
49 // Per Region Values
50 //
51
52 private bool ready = false;
53
54 // Configurable values
55 private string m_mode = "SL";
56 private int m_frame_mod = 0;
57 private double m_day_length = 0;
58 private int m_year_length = 0;
59 private double m_day_night = 0;
60 // private double m_longitude = 0;
61 // private double m_latitude = 0;
62 // Configurable defaults Defaults close to SL
63 private string d_mode = "SL";
64 private int d_frame_mod = 100; // Every 10 seconds (actually less)
65 private double d_day_length = 4; // A VW day is 4 RW hours long
66 private int d_year_length = 60; // There are 60 VW days in a VW year
67 private double d_day_night = 0.45; // axis offset: ratio of light-to-dark, approx 1:3
68 // private double d_longitude = -73.53;
69 // private double d_latitude = 41.29;
70
71 // Frame counter
72 private uint m_frame = 0;
73
74 // Cached Scene reference
75 private Scene m_scene = null;
76
77 // Calculated Once in the lifetime of a region
78 private long TicksToEpoch; // Elapsed time for 1/1/1970
79 private uint SecondsPerSunCycle; // Length of a virtual day in RW seconds
80 private uint SecondsPerYear; // Length of a virtual year in RW seconds
81 private double SunSpeed; // Rate of passage in radians/second
82 private double SeasonSpeed; // Rate of change for seasonal effects
83 // private double HoursToRadians; // Rate of change for seasonal effects
84 private long TicksOffset = 0; // seconds offset from UTC
85 // Calculated every update
86 private float OrbitalPosition; // Orbital placement at a point in time
87 private double HorizonShift; // Axis offset to skew day and night
88 private double TotalDistanceTravelled; // Distance since beginning of time (in radians)
89 private double SeasonalOffset; // Seaonal variation of tilt
90 private float Magnitude; // Normal tilt
91 // private double VWTimeRatio; // VW time as a ratio of real time
92
93 // Working values
94 private Vector3 Position = Vector3.Zero;
95 private Vector3 Velocity = Vector3.Zero;
96 private Quaternion Tilt = new Quaternion(1.0f, 0.0f, 0.0f, 0.0f);
97
98 private long LindenHourOffset = 0;
99 private bool sunFixed = false;
100
101 private Dictionary<UUID, ulong> m_rootAgents = new Dictionary<UUID, ulong>();
102
103 // Current time in elapsed seconds since Jan 1st 1970
104 private ulong CurrentTime
105 {
106 get {
107 return (ulong)(((System.DateTime.Now.Ticks) - TicksToEpoch + TicksOffset + LindenHourOffset)/10000000);
108 }
109 }
110
111 private float GetLindenEstateHourFromCurrentTime()
112 {
113 float ticksleftover = ((float)CurrentTime) % ((float)SecondsPerSunCycle);
114
115 float hour = (24 * (ticksleftover / SecondsPerSunCycle)) + 6;
116
117 return hour;
118 }
119
120 private void SetTimeByLindenHour(float LindenHour)
121 {
122 // Linden hour is 24 hours with a 6 hour offset. 6-30
123
124 if (LindenHour - 6 == 0)
125 {
126 LindenHourOffset = 0;
127 return;
128 }
129
130 // Remove LindenHourOffset to calculate it from LocalTime
131 float ticksleftover = ((float)(((long)(CurrentTime * 10000000) - (long)LindenHourOffset)/ 10000000) % ((float)SecondsPerSunCycle));
132 float hour = (24 * (ticksleftover / SecondsPerSunCycle));
133
134 float offsethours = 0;
135
136 if (LindenHour - 6 > hour)
137 {
138 offsethours = hour + ((LindenHour-6) - hour);
139 }
140 else
141 {
142 offsethours = hour - (hour - (LindenHour - 6));
143 }
144 //m_log.Debug("[OFFSET]: " + hour + " - " + LindenHour + " - " + offsethours.ToString());
145
146 LindenHourOffset = (long)((float)offsethours * (36000000000/m_day_length));
147 m_log.Debug("[SUN]: Directive from the Estate Tools to set the sun phase to LindenHour " + GetLindenEstateHourFromCurrentTime().ToString());
148 }
149
150 // Called immediately after the module is loaded for a given region
151 // i.e. Immediately after instance creation.
152 public void Initialise(Scene scene, IConfigSource config)
153 {
154 m_scene = scene;
155
156 m_frame = 0;
157
158 TimeZone local = TimeZone.CurrentTimeZone;
159 TicksOffset = local.GetUtcOffset(local.ToLocalTime(DateTime.Now)).Ticks;
160 m_log.Debug("[SUN]: localtime offset is " + TicksOffset);
161
162 // Align ticks with Second Life
163
164 TicksToEpoch = new System.DateTime(1970,1,1).Ticks;
165
166 // Just in case they don't have the stanzas
167 try
168 {
169 // Mode: determines how the sun is handled
170 m_mode = config.Configs["Sun"].GetString("mode", d_mode);
171 // Mode: determines how the sun is handled
172 // m_latitude = config.Configs["Sun"].GetDouble("latitude", d_latitude);
173 // Mode: determines how the sun is handled
174 // m_longitude = config.Configs["Sun"].GetDouble("longitude", d_longitude);
175 // Year length in days
176 m_year_length = config.Configs["Sun"].GetInt("year_length", d_year_length);
177 // Day length in decimal hours
178 m_day_length = config.Configs["Sun"].GetDouble("day_length", d_day_length);
179 // Day to Night Ratio
180 m_day_night = config.Configs["Sun"].GetDouble("day_night_offset", d_day_night);
181 // Update frequency in frames
182 m_frame_mod = config.Configs["Sun"].GetInt("update_interval", d_frame_mod);
183 }
184 catch (Exception e)
185 {
186 m_log.Debug("[SUN]: Configuration access failed, using defaults. Reason: "+e.Message);
187 m_mode = d_mode;
188 m_year_length = d_year_length;
189 m_day_length = d_day_length;
190 m_day_night = d_day_night;
191 m_frame_mod = d_frame_mod;
192 // m_latitude = d_latitude;
193 // m_longitude = d_longitude;
194 }
195
196 switch (m_mode)
197 {
198 case "T1":
199 default:
200 case "SL":
201 // Time taken to complete a cycle (day and season)
202
203 SecondsPerSunCycle = (uint) (m_day_length * 60 * 60);
204 SecondsPerYear = (uint) (SecondsPerSunCycle*m_year_length);
205
206 // Ration of real-to-virtual time
207
208 // VWTimeRatio = 24/m_day_length;
209
210 // Speed of rotation needed to complete a cycle in the
211 // designated period (day and season)
212
213 SunSpeed = SunCycle/SecondsPerSunCycle;
214 SeasonSpeed = SeasonalCycle/SecondsPerYear;
215
216 // Horizon translation
217
218 HorizonShift = m_day_night; // Z axis translation
219 // HoursToRadians = (SunCycle/24)*VWTimeRatio;
220
221 // Insert our event handling hooks
222
223 scene.EventManager.OnFrame += SunUpdate;
224 scene.EventManager.OnMakeChildAgent += MakeChildAgent;
225 scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
226 scene.EventManager.OnClientClosed += ClientLoggedOut;
227 scene.EventManager.OnEstateToolsTimeUpdate += EstateToolsTimeUpdate;
228 scene.EventManager.OnGetSunLindenHour += GetLindenEstateHourFromCurrentTime;
229
230 ready = true;
231
232 m_log.Debug("[SUN]: Mode is "+m_mode);
233 m_log.Debug("[SUN]: Initialization completed. Day is "+SecondsPerSunCycle+" seconds, and year is "+m_year_length+" days");
234 m_log.Debug("[SUN]: Axis offset is "+m_day_night);
235 m_log.Debug("[SUN]: Positional data updated every "+m_frame_mod+" frames");
236
237 break;
238 }
239 }
240
241 public void PostInitialise()
242 {
243 }
244
245 public void Close()
246 {
247 ready = false;
248
249 // Remove our hooks
250 m_scene.EventManager.OnFrame -= SunUpdate;
251 m_scene.EventManager.OnMakeChildAgent -= MakeChildAgent;
252 m_scene.EventManager.OnAvatarEnteringNewParcel -= AvatarEnteringParcel;
253 m_scene.EventManager.OnClientClosed -= ClientLoggedOut;
254 m_scene.EventManager.OnEstateToolsTimeUpdate -= EstateToolsTimeUpdate;
255 m_scene.EventManager.OnGetSunLindenHour -= GetLindenEstateHourFromCurrentTime;
256 }
257
258 public string Name
259 {
260 get { return "SunModule"; }
261 }
262
263 public bool IsSharedModule
264 {
265 get { return false; }
266 }
267
268 public void SunToClient(IClientAPI client)
269 {
270 if (m_mode != "T1")
271 {
272 if (ready)
273 {
274 if (!sunFixed)
275 GenSunPos(); // Generate shared values once
276 client.SendSunPos(Position, Velocity, CurrentTime, SecondsPerSunCycle, SecondsPerYear, OrbitalPosition);
277 }
278 }
279 }
280
281 public void SunUpdate()
282 {
283 if (((m_frame++%m_frame_mod) != 0) || !ready || sunFixed)
284 {
285 return;
286 }
287
288 GenSunPos(); // Generate shared values once
289
290 List<ScenePresence> avatars = m_scene.GetAvatars();
291 foreach (ScenePresence avatar in avatars)
292 {
293 if (!avatar.IsChildAgent)
294 avatar.ControllingClient.SendSunPos(Position, Velocity, CurrentTime, SecondsPerSunCycle, SecondsPerYear, OrbitalPosition);
295 }
296
297 // set estate settings for region access to sun position
298 m_scene.RegionInfo.RegionSettings.SunVector = Position;
299 //m_scene.RegionInfo.EstateSettings.sunHour = GetLindenEstateHourFromCurrentTime();
300 }
301
302 public void ForceSunUpdateToAllClients()
303 {
304 GenSunPos(); // Generate shared values once
305
306 List<ScenePresence> avatars = m_scene.GetAvatars();
307 foreach (ScenePresence avatar in avatars)
308 {
309 if (!avatar.IsChildAgent)
310 avatar.ControllingClient.SendSunPos(Position, Velocity, CurrentTime, SecondsPerSunCycle, SecondsPerYear, OrbitalPosition);
311 }
312
313 // set estate settings for region access to sun position
314 m_scene.RegionInfo.RegionSettings.SunVector = Position;
315 m_scene.RegionInfo.RegionSettings.SunPosition = GetLindenEstateHourFromCurrentTime();
316 }
317
318 /// <summary>
319 /// Calculate the sun's orbital position and its velocity.
320 /// </summary>
321 private void GenSunPos()
322 {
323 TotalDistanceTravelled = SunSpeed * CurrentTime; // distance measured in radians
324 OrbitalPosition = (float) (TotalDistanceTravelled%SunCycle); // position measured in radians
325
326 // TotalDistanceTravelled += HoursToRadians-(0.25*Math.PI)*Math.Cos(HoursToRadians)-OrbitalPosition;
327 // OrbitalPosition = (float) (TotalDistanceTravelled%SunCycle);
328
329 SeasonalOffset = SeasonSpeed * CurrentTime; // Present season determined as total radians travelled around season cycle
330
331 Tilt.W = (float) (AverageTilt + (SeasonalTilt*Math.Sin(SeasonalOffset))); // Calculate seasonal orbital N/S tilt
332
333 // m_log.Debug("[SUN] Total distance travelled = "+TotalDistanceTravelled+", present position = "+OrbitalPosition+".");
334 // m_log.Debug("[SUN] Total seasonal progress = "+SeasonalOffset+", present tilt = "+Tilt.W+".");
335
336 // The sun rotates about the Z axis
337
338 Position.X = (float) Math.Cos(-TotalDistanceTravelled);
339 Position.Y = (float) Math.Sin(-TotalDistanceTravelled);
340 Position.Z = 0;
341
342 // For interest we rotate it slightly about the X access.
343 // Celestial tilt is a value that ranges .025
344
345 Position *= Tilt;
346
347 // Finally we shift the axis so that more of the
348 // circle is above the horizon than below. This
349 // makes the nights shorter than the days.
350
351 Position.Z = Position.Z + (float) HorizonShift;
352 Position = Vector3.Normalize(Position);
353
354 // m_log.Debug("[SUN] Position("+Position.X+","+Position.Y+","+Position.Z+")");
355
356 Velocity.X = 0;
357 Velocity.Y = 0;
358 Velocity.Z = (float) SunSpeed;
359
360 // Correct angular velocity to reflect the seasonal rotation
361
362 Magnitude = Position.Length();
363 if (sunFixed)
364 {
365 Velocity.X = 0;
366 Velocity.Y = 0;
367 Velocity.Z = 0;
368 return;
369 }
370
371 Velocity = (Velocity * Tilt) * (1.0f / Magnitude);
372
373 // m_log.Debug("[SUN] Velocity("+Velocity.X+","+Velocity.Y+","+Velocity.Z+")");
374 }
375
376 private void ClientLoggedOut(UUID AgentId)
377 {
378 lock (m_rootAgents)
379 {
380 if (m_rootAgents.ContainsKey(AgentId))
381 {
382 m_rootAgents.Remove(AgentId);
383 }
384 }
385 }
386
387 private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
388 {
389 lock (m_rootAgents)
390 {
391 if (m_rootAgents.ContainsKey(avatar.UUID))
392 {
393 m_rootAgents[avatar.UUID] = avatar.RegionHandle;
394 }
395 else
396 {
397 m_rootAgents.Add(avatar.UUID, avatar.RegionHandle);
398 SunToClient(avatar.ControllingClient);
399 }
400 }
401 //m_log.Info("[FRIEND]: " + avatar.Name + " status:" + (!avatar.IsChildAgent).ToString());
402 }
403
404 private void MakeChildAgent(ScenePresence avatar)
405 {
406 lock (m_rootAgents)
407 {
408 if (m_rootAgents.ContainsKey(avatar.UUID))
409 {
410 if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
411 {
412 m_rootAgents.Remove(avatar.UUID);
413 }
414 }
415 }
416 }
417
418 public void EstateToolsTimeUpdate(ulong regionHandle, bool FixedTime, bool useEstateTime, float LindenHour)
419 {
420 if (m_scene.RegionInfo.RegionHandle == regionHandle)
421 {
422 SetTimeByLindenHour(LindenHour);
423
424 //if (useEstateTime)
425 //LindenHourOffset = 0;
426
427 ForceSunUpdateToAllClients();
428 sunFixed = FixedTime;
429 if (sunFixed)
430 GenSunPos();
431 }
432 }
433 }
434}