aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region
diff options
context:
space:
mode:
authorAdam Frisby2008-02-14 12:27:24 +0000
committerAdam Frisby2008-02-14 12:27:24 +0000
commit3c22da9efc77b2ec133a789c5162650d2c3bf6d5 (patch)
treed30a26add215fcf90b6b473f7e3485a025b56806 /OpenSim/Region
parent* Fixed #564, errors in friend sql (diff)
downloadopensim-SC_OLD-3c22da9efc77b2ec133a789c5162650d2c3bf6d5.zip
opensim-SC_OLD-3c22da9efc77b2ec133a789c5162650d2c3bf6d5.tar.gz
opensim-SC_OLD-3c22da9efc77b2ec133a789c5162650d2c3bf6d5.tar.bz2
opensim-SC_OLD-3c22da9efc77b2ec133a789c5162650d2c3bf6d5.tar.xz
* Tree Populator Module, use "script tree" to make a growing tree in your sim.
Diffstat (limited to 'OpenSim/Region')
-rw-r--r--OpenSim/Region/Environment/Modules/TreePopulatorModule.cs246
1 files changed, 246 insertions, 0 deletions
diff --git a/OpenSim/Region/Environment/Modules/TreePopulatorModule.cs b/OpenSim/Region/Environment/Modules/TreePopulatorModule.cs
new file mode 100644
index 0000000..9c556c1
--- /dev/null
+++ b/OpenSim/Region/Environment/Modules/TreePopulatorModule.cs
@@ -0,0 +1,246 @@
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
29using System;
30using System.Collections.Generic;
31using libsecondlife;
32using Nini.Config;
33using OpenSim.Framework;
34using OpenSim.Framework.Console;
35using OpenSim.Region.Environment.Interfaces;
36using OpenSim.Region.Environment.Scenes;
37
38namespace OpenSim.Region.Environment.Modules
39{
40 /// <summary>
41 /// Version 2.0 - Very hacky compared to the original. Will fix original and release as 0.3 later.
42 /// </summary>
43 public class TreePopulatorModule : IRegionModule
44 {
45 private Scene m_scene;
46 private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
47
48 private List<LLUUID> m_trees;
49
50 public double m_tree_density = 50.0; // Aim for this many per region
51 public double m_tree_updates = 1000.0; // MS between updates
52
53 public void Initialise(Scene scene, IConfigSource config)
54 {
55 try
56 {
57 m_tree_density = config.Configs["Trees"].GetDouble("tree_density", m_tree_density);
58 }
59 catch (Exception)
60 { }
61
62 m_trees = new List<LLUUID>();
63 m_scene = scene;
64
65 m_scene.EventManager.OnPluginConsole += new EventManager.OnPluginConsoleDelegate(EventManager_OnPluginConsole);
66
67 System.Timers.Timer CalculateTrees = new System.Timers.Timer(m_tree_updates);
68 CalculateTrees.Elapsed += new System.Timers.ElapsedEventHandler(CalculateTrees_Elapsed);
69 CalculateTrees.Start();
70 m_log.Debug("[TREES]: Initialised tree module");
71 }
72
73 void EventManager_OnPluginConsole(string[] args)
74 {
75 if (args[0] == "tree")
76 {
77 m_log.Debug("[TREES]: New tree planting");
78 CreateTree(new LLVector3(128.0f, 128.0f, 0.0f));
79 }
80 }
81
82 void growTrees()
83 {
84 foreach (LLUUID tree in m_trees)
85 {
86 if (m_scene.Entities.ContainsKey(tree))
87 {
88 SceneObjectPart s_tree = ((SceneObjectGroup)m_scene.Entities[tree]).RootPart;
89
90 // 100 seconds to grow 1m
91 s_tree.Scale += new LLVector3(0.1f, 0.1f, 0.1f);
92 s_tree.SendFullUpdateToAllClients();
93 //s_tree.ScheduleTerseUpdate();
94 }
95 else
96 {
97 m_trees.Remove(tree);
98 }
99 }
100 }
101
102 void seedTrees()
103 {
104 foreach (LLUUID tree in m_trees)
105 {
106 if (m_scene.Entities.ContainsKey(tree))
107 {
108 SceneObjectPart s_tree = ((SceneObjectGroup)m_scene.Entities[tree]).RootPart;
109
110 if (s_tree.Scale.X > 0.5)
111 {
112 if (Util.RandomClass.NextDouble() > 0.75)
113 {
114 SpawnChild(s_tree);
115 }
116 }
117
118 }
119 else
120 {
121 m_trees.Remove(tree);
122 }
123 }
124 }
125
126 void killTrees()
127 {
128 foreach (LLUUID tree in m_trees)
129 {
130 double killLikelyhood = 0.0;
131
132 if (m_scene.Entities.ContainsKey(tree))
133 {
134 SceneObjectPart selectedTree = ((SceneObjectGroup)m_scene.Entities[tree]).RootPart;
135 double selectedTreeScale = Math.Sqrt(Math.Pow(selectedTree.Scale.X, 2) +
136 Math.Pow(selectedTree.Scale.Y, 2) +
137 Math.Pow(selectedTree.Scale.Z, 2));
138
139 foreach (LLUUID picktree in m_trees)
140 {
141 if (picktree != tree)
142 {
143 SceneObjectPart pickedTree = ((SceneObjectGroup)m_scene.Entities[picktree]).RootPart;
144
145 double pickedTreeScale = Math.Sqrt(Math.Pow(pickedTree.Scale.X, 2) +
146 Math.Pow(pickedTree.Scale.Y, 2) +
147 Math.Pow(pickedTree.Scale.Z, 2));
148
149 double pickedTreeDistance = Math.Sqrt(Math.Pow(Math.Abs(pickedTree.AbsolutePosition.X - selectedTree.AbsolutePosition.X), 2) +
150 Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Y - selectedTree.AbsolutePosition.Y), 2) +
151 Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Z - selectedTree.AbsolutePosition.Z), 2));
152
153 killLikelyhood += (selectedTreeScale / (pickedTreeScale * pickedTreeDistance)) * 0.1;
154 }
155 }
156
157 if (Util.RandomClass.NextDouble() < killLikelyhood)
158 {
159 m_scene.RemoveEntity(selectedTree.ParentGroup);
160 m_trees.Remove(selectedTree.ParentGroup.UUID);
161
162 m_scene.ForEachClient(delegate(IClientAPI controller)
163 {
164 controller.SendKillObject(m_scene.RegionInfo.RegionHandle,
165 selectedTree.LocalID);
166 });
167
168 break;
169 }
170 else
171 {
172 selectedTree.SetText(killLikelyhood.ToString(), new Axiom.Math.Vector3(1.0f, 1.0f, 1.0f), 1.0);
173 }
174 }
175 else
176 {
177 m_trees.Remove(tree);
178 }
179 }
180 }
181
182 private void SpawnChild(SceneObjectPart s_tree)
183 {
184 LLVector3 position = new LLVector3();
185
186 position.X = s_tree.AbsolutePosition.X + (1 * (-1 * Util.RandomClass.Next(1)));
187 if (position.X > 255)
188 position.X = 255;
189 if (position.X < 0)
190 position.X = 0;
191 position.Y = s_tree.AbsolutePosition.Y + (1 * (-1 * Util.RandomClass.Next(1)));
192 if (position.Y > 255)
193 position.Y = 255;
194 if (position.Y < 0)
195 position.Y = 0;
196
197 double randX = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
198 double randY = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
199
200 position.X += (float)randX;
201 position.Y += (float)randY;
202
203 CreateTree(position);
204 }
205
206 private void CreateTree(LLVector3 position)
207 {
208 position.Z = (float)m_scene.Terrain.heightmap.Get((int)position.X, (int)position.Y);
209
210 SceneObjectGroup tree =
211 m_scene.AddTree(new LLVector3(0.1f, 0.1f, 0.1f),
212 LLQuaternion.Identity,
213 position,
214 Tree.Cypress1,
215 false);
216
217 m_trees.Add(tree.UUID);
218 tree.SendGroupFullUpdate();
219 }
220
221 void CalculateTrees_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
222 {
223 growTrees();
224 seedTrees();
225 killTrees();
226 }
227
228 public void PostInitialise()
229 {
230 }
231
232 public void Close()
233 {
234 }
235
236 public string Name
237 {
238 get { return "TreePopulatorModule"; }
239 }
240
241 public bool IsSharedModule
242 {
243 get { return false; }
244 }
245 }
246} \ No newline at end of file