aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/OptionalModules/World/TreePopulator/TreePopulatorModule.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/OptionalModules/World/TreePopulator/TreePopulatorModule.cs')
-rw-r--r--OpenSim/Region/OptionalModules/World/TreePopulator/TreePopulatorModule.cs304
1 files changed, 304 insertions, 0 deletions
diff --git a/OpenSim/Region/OptionalModules/World/TreePopulator/TreePopulatorModule.cs b/OpenSim/Region/OptionalModules/World/TreePopulator/TreePopulatorModule.cs
new file mode 100644
index 0000000..94ebcac
--- /dev/null
+++ b/OpenSim/Region/OptionalModules/World/TreePopulator/TreePopulatorModule.cs
@@ -0,0 +1,304 @@
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 System.Reflection;
31using System.Timers;
32using OpenMetaverse;
33using log4net;
34using Nini.Config;
35using OpenSim.Framework;
36using OpenSim.Region.Framework.Interfaces;
37using OpenSim.Region.Framework.Scenes;
38
39namespace OpenSim.Region.OptionalModules.World.TreePopulator
40{
41 /// <summary>
42 /// Version 2.0 - Very hacky compared to the original. Will fix original and release as 0.3 later.
43 /// </summary>
44 public class TreePopulatorModule : IRegionModule
45 {
46 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
47 private Scene m_scene;
48
49 public double m_tree_density = 50.0; // Aim for this many per region
50 public double m_tree_updates = 1000.0; // MS between updates
51 private bool m_active_trees = false;
52 private List<UUID> m_trees;
53 Timer CalculateTrees;
54
55 #region IRegionModule Members
56
57 public void Initialise(Scene scene, IConfigSource config)
58 {
59 try
60 {
61 m_tree_density = config.Configs["Trees"].GetDouble("tree_density", m_tree_density);
62 m_active_trees = config.Configs["Trees"].GetBoolean("active_trees", m_active_trees);
63 }
64 catch (Exception)
65 {
66 }
67
68 m_trees = new List<UUID>();
69 m_scene = scene;
70
71 m_scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
72
73 if (m_active_trees)
74 activeizeTreeze(true);
75
76 m_log.Debug("[TREES]: Initialised tree module");
77 }
78
79 public void PostInitialise()
80 {
81 }
82
83 public void Close()
84 {
85 }
86
87 public string Name
88 {
89 get { return "TreePopulatorModule"; }
90 }
91
92 public bool IsSharedModule
93 {
94 get { return false; }
95 }
96
97 #endregion
98
99 private void EventManager_OnPluginConsole(string[] args)
100 {
101 if (args.Length == 1)
102 {
103 if (args[0] == "tree")
104 {
105 UUID uuid = m_scene.RegionInfo.EstateSettings.EstateOwner;
106 if (uuid == UUID.Zero)
107 uuid = m_scene.RegionInfo.MasterAvatarAssignedUUID;
108 m_log.Debug("[TREES]: New tree planting");
109 CreateTree(uuid, new Vector3(128.0f, 128.0f, 0.0f));
110
111 }
112 }
113
114 if (args.Length == 2 || args.Length == 3)
115 {
116 if (args[1] == "active")
117 {
118 if (args.Length >= 3)
119 {
120 if (args[2] == "true" && !m_active_trees)
121 {
122 m_active_trees = true;
123 activeizeTreeze(m_active_trees);
124 m_log.Info("[TREES]: Activizing Trees");
125 }
126 if (args[2] == "false" && m_active_trees)
127 {
128 m_active_trees = false;
129 activeizeTreeze(m_active_trees);
130 m_log.Info("[TREES]: Trees no longer Active, for now...");
131 }
132 }
133 else
134 {
135 m_log.Info("[TREES]: When setting the tree module active via the console, you must specify true or false");
136 }
137 }
138 }
139
140 }
141
142 private void activeizeTreeze(bool activeYN)
143 {
144 if (activeYN)
145 {
146 CalculateTrees = new Timer(m_tree_updates);
147 CalculateTrees.Elapsed += CalculateTrees_Elapsed;
148 CalculateTrees.Start();
149 }
150 else
151 {
152 CalculateTrees.Stop();
153 }
154 }
155
156 private void growTrees()
157 {
158 foreach (UUID tree in m_trees)
159 {
160 if (m_scene.Entities.ContainsKey(tree))
161 {
162 SceneObjectPart s_tree = ((SceneObjectGroup) m_scene.Entities[tree]).RootPart;
163
164 // 100 seconds to grow 1m
165 s_tree.Scale += new Vector3(0.1f, 0.1f, 0.1f);
166 s_tree.SendFullUpdateToAllClients();
167 //s_tree.ScheduleTerseUpdate();
168 }
169 else
170 {
171 m_trees.Remove(tree);
172 }
173 }
174 }
175
176 private void seedTrees()
177 {
178 foreach (UUID tree in m_trees)
179 {
180 if (m_scene.Entities.ContainsKey(tree))
181 {
182 SceneObjectPart s_tree = ((SceneObjectGroup) m_scene.Entities[tree]).RootPart;
183
184 if (s_tree.Scale.X > 0.5)
185 {
186 if (Util.RandomClass.NextDouble() > 0.75)
187 {
188 SpawnChild(s_tree);
189 }
190 }
191 }
192 else
193 {
194 m_trees.Remove(tree);
195 }
196 }
197 }
198
199 private void killTrees()
200 {
201 foreach (UUID tree in m_trees)
202 {
203 double killLikelyhood = 0.0;
204
205 if (m_scene.Entities.ContainsKey(tree))
206 {
207 SceneObjectPart selectedTree = ((SceneObjectGroup) m_scene.Entities[tree]).RootPart;
208 double selectedTreeScale = Math.Sqrt(Math.Pow(selectedTree.Scale.X, 2) +
209 Math.Pow(selectedTree.Scale.Y, 2) +
210 Math.Pow(selectedTree.Scale.Z, 2));
211
212 foreach (UUID picktree in m_trees)
213 {
214 if (picktree != tree)
215 {
216 SceneObjectPart pickedTree = ((SceneObjectGroup) m_scene.Entities[picktree]).RootPart;
217
218 double pickedTreeScale = Math.Sqrt(Math.Pow(pickedTree.Scale.X, 2) +
219 Math.Pow(pickedTree.Scale.Y, 2) +
220 Math.Pow(pickedTree.Scale.Z, 2));
221
222 double pickedTreeDistance = Math.Sqrt(Math.Pow(Math.Abs(pickedTree.AbsolutePosition.X - selectedTree.AbsolutePosition.X), 2) +
223 Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Y - selectedTree.AbsolutePosition.Y), 2) +
224 Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Z - selectedTree.AbsolutePosition.Z), 2));
225
226 killLikelyhood += (selectedTreeScale / (pickedTreeScale * pickedTreeDistance)) * 0.1;
227 }
228 }
229
230 if (Util.RandomClass.NextDouble() < killLikelyhood)
231 {
232 m_scene.DeleteSceneObject(selectedTree.ParentGroup, false);
233 m_trees.Remove(selectedTree.ParentGroup.UUID);
234
235 m_scene.ForEachClient(delegate(IClientAPI controller)
236 {
237 controller.SendKillObject(m_scene.RegionInfo.RegionHandle,
238 selectedTree.LocalId);
239 });
240
241 break;
242 }
243 selectedTree.SetText(killLikelyhood.ToString(), new Vector3(1.0f, 1.0f, 1.0f), 1.0);
244 }
245 else
246 {
247 m_trees.Remove(tree);
248 }
249 }
250 }
251
252 private void SpawnChild(SceneObjectPart s_tree)
253 {
254 Vector3 position = new Vector3();
255
256 position.X = s_tree.AbsolutePosition.X + (1 * (-1 * Util.RandomClass.Next(1)));
257 if (position.X > 255)
258 position.X = 255;
259 if (position.X < 0)
260 position.X = 0;
261 position.Y = s_tree.AbsolutePosition.Y + (1 * (-1 * Util.RandomClass.Next(1)));
262 if (position.Y > 255)
263 position.Y = 255;
264 if (position.Y < 0)
265 position.Y = 0;
266
267 double randX = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
268 double randY = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
269
270 position.X += (float) randX;
271 position.Y += (float) randY;
272
273 UUID uuid = m_scene.RegionInfo.EstateSettings.EstateOwner;
274 if (uuid == UUID.Zero)
275 uuid = m_scene.RegionInfo.MasterAvatarAssignedUUID;
276
277 CreateTree(uuid, position);
278 }
279
280 private void CreateTree(UUID uuid, Vector3 position)
281 {
282 position.Z = (float) m_scene.Heightmap[(int) position.X, (int) position.Y];
283
284 IVegetationModule module = m_scene.RequestModuleInterface<IVegetationModule>();
285
286 if (null == module)
287 return;
288
289 SceneObjectGroup tree
290 = module.AddTree(
291 uuid, UUID.Zero, new Vector3(0.1f, 0.1f, 0.1f), Quaternion.Identity, position, Tree.Cypress1, false);
292
293 m_trees.Add(tree.UUID);
294 tree.SendGroupFullUpdate();
295 }
296
297 private void CalculateTrees_Elapsed(object sender, ElapsedEventArgs e)
298 {
299 growTrees();
300 seedTrees();
301 killTrees();
302 }
303 }
304}