aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/CoreModules
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/CoreModules')
-rw-r--r--OpenSim/Region/CoreModules/World/Cloud/CloudModule.cs190
1 files changed, 190 insertions, 0 deletions
diff --git a/OpenSim/Region/CoreModules/World/Cloud/CloudModule.cs b/OpenSim/Region/CoreModules/World/Cloud/CloudModule.cs
new file mode 100644
index 0000000..e164f08
--- /dev/null
+++ b/OpenSim/Region/CoreModules/World/Cloud/CloudModule.cs
@@ -0,0 +1,190 @@
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 Nini.Config;
31using OpenMetaverse;
32using OpenSim.Framework;
33using OpenSim.Region.Framework.Interfaces;
34using OpenSim.Region.Framework.Scenes;
35
36namespace OpenSim.Region.CoreModules
37{
38 public class CloudModule : ICloudModule
39 {
40 private static readonly log4net.ILog m_log
41 = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
42
43 private Random m_rndnums = new Random(Environment.TickCount);
44 private Scene m_scene = null;
45 private bool m_ready = false;
46 private bool m_enabled = false;
47 private float m_cloudDensity = 1.0F;
48 private float[] cloudCover = new float[16 * 16];
49
50 private Dictionary<UUID, ulong> m_rootAgents = new Dictionary<UUID, ulong>();
51
52 public void Initialise(Scene scene, IConfigSource config)
53 {
54 IConfig cloudConfig = config.Configs["Cloud"];
55
56 if (cloudConfig != null)
57 {
58 m_enabled = cloudConfig.GetBoolean("enabled", false);
59 m_cloudDensity = cloudConfig.GetFloat("density", 0.5F);
60 }
61
62 if (m_enabled)
63 {
64
65 m_scene = scene;
66
67 scene.EventManager.OnMakeChildAgent += MakeChildAgent;
68 scene.EventManager.OnAvatarEnteringNewParcel += AvatarEnteringParcel;
69 scene.EventManager.OnClientClosed += ClientLoggedOut;
70 scene.RegisterModuleInterface<ICloudModule>(this);
71
72 GenerateCloudCover();
73
74 m_ready = true;
75
76 }
77
78 }
79
80 public void PostInitialise()
81 {
82 }
83
84 public void Close()
85 {
86 if (m_enabled)
87 {
88 m_ready = false;
89 // Remove our hooks
90 m_scene.EventManager.OnMakeChildAgent -= MakeChildAgent;
91 m_scene.EventManager.OnAvatarEnteringNewParcel -= AvatarEnteringParcel;
92 m_scene.EventManager.OnClientClosed -= ClientLoggedOut;
93 }
94 }
95
96 public string Name
97 {
98 get { return "CloudModule"; }
99 }
100
101 public bool IsSharedModule
102 {
103 get { return false; }
104 }
105
106
107 public float CloudCover(int x, int y, int z)
108 {
109 float cover = 0f;
110 x /= 16;
111 y /= 16;
112 if (x < 0) x = 0;
113 if (x > 15) x = 15;
114 if (y < 0) y = 0;
115 if (y > 15) y = 15;
116
117 if (cloudCover != null)
118 {
119 cover = cloudCover[y * 16 + x];
120 }
121
122 return cover;
123 }
124
125 public void CloudsToClient(IClientAPI client)
126 {
127 if (m_ready)
128 {
129 client.SendCloudData(cloudCover);
130 }
131 }
132
133
134 /// <summary>
135 /// Calculate the cloud cover over the region.
136 /// </summary>
137 private void GenerateCloudCover()
138 {
139 for (int y = 0; y < 16; y++)
140 {
141 for (int x = 0; x < 16; x++)
142 {
143 cloudCover[y * 16 + x] = (float)(m_rndnums.NextDouble()); // 0 to 1
144 cloudCover[y * 16 + x] *= m_cloudDensity;
145 }
146 }
147 }
148
149 private void ClientLoggedOut(UUID AgentId)
150 {
151 lock (m_rootAgents)
152 {
153 if (m_rootAgents.ContainsKey(AgentId))
154 {
155 m_rootAgents.Remove(AgentId);
156 }
157 }
158 }
159
160 private void AvatarEnteringParcel(ScenePresence avatar, int localLandID, UUID regionID)
161 {
162 lock (m_rootAgents)
163 {
164 if (m_rootAgents.ContainsKey(avatar.UUID))
165 {
166 m_rootAgents[avatar.UUID] = avatar.RegionHandle;
167 }
168 else
169 {
170 m_rootAgents.Add(avatar.UUID, avatar.RegionHandle);
171 CloudsToClient(avatar.ControllingClient);
172 }
173 }
174 }
175
176 private void MakeChildAgent(ScenePresence avatar)
177 {
178 lock (m_rootAgents)
179 {
180 if (m_rootAgents.ContainsKey(avatar.UUID))
181 {
182 if (m_rootAgents[avatar.UUID] == avatar.RegionHandle)
183 {
184 m_rootAgents.Remove(avatar.UUID);
185 }
186 }
187 }
188 }
189 }
190}