TreePopulatorModule.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Console;
  34. using OpenSim.Region.Environment.Interfaces;
  35. using OpenSim.Region.Environment.Scenes;
  36. namespace OpenSim.Region.Environment.Modules
  37. {
  38. /// <summary>
  39. /// Version 2.0 - Very hacky compared to the original. Will fix original and release as 0.3 later.
  40. /// </summary>
  41. public class TreePopulatorModule : IRegionModule
  42. {
  43. private Scene m_scene;
  44. private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  45. private List<LLUUID> m_trees;
  46. public double m_tree_density = 50.0; // Aim for this many per region
  47. public double m_tree_updates = 1000.0; // MS between updates
  48. public void Initialise(Scene scene, IConfigSource config)
  49. {
  50. try
  51. {
  52. m_tree_density = config.Configs["Trees"].GetDouble("tree_density", m_tree_density);
  53. }
  54. catch (Exception)
  55. { }
  56. m_trees = new List<LLUUID>();
  57. m_scene = scene;
  58. m_scene.EventManager.OnPluginConsole += new EventManager.OnPluginConsoleDelegate(EventManager_OnPluginConsole);
  59. System.Timers.Timer CalculateTrees = new System.Timers.Timer(m_tree_updates);
  60. CalculateTrees.Elapsed += new System.Timers.ElapsedEventHandler(CalculateTrees_Elapsed);
  61. CalculateTrees.Start();
  62. m_log.Debug("[TREES]: Initialised tree module");
  63. }
  64. void EventManager_OnPluginConsole(string[] args)
  65. {
  66. if (args[0] == "tree")
  67. {
  68. m_log.Debug("[TREES]: New tree planting");
  69. CreateTree(new LLVector3(128.0f, 128.0f, 0.0f));
  70. }
  71. }
  72. void growTrees()
  73. {
  74. foreach (LLUUID tree in m_trees)
  75. {
  76. if (m_scene.Entities.ContainsKey(tree))
  77. {
  78. SceneObjectPart s_tree = ((SceneObjectGroup)m_scene.Entities[tree]).RootPart;
  79. // 100 seconds to grow 1m
  80. s_tree.Scale += new LLVector3(0.1f, 0.1f, 0.1f);
  81. s_tree.SendFullUpdateToAllClients();
  82. //s_tree.ScheduleTerseUpdate();
  83. }
  84. else
  85. {
  86. m_trees.Remove(tree);
  87. }
  88. }
  89. }
  90. void seedTrees()
  91. {
  92. foreach (LLUUID tree in m_trees)
  93. {
  94. if (m_scene.Entities.ContainsKey(tree))
  95. {
  96. SceneObjectPart s_tree = ((SceneObjectGroup)m_scene.Entities[tree]).RootPart;
  97. if (s_tree.Scale.X > 0.5)
  98. {
  99. if (Util.RandomClass.NextDouble() > 0.75)
  100. {
  101. SpawnChild(s_tree);
  102. }
  103. }
  104. }
  105. else
  106. {
  107. m_trees.Remove(tree);
  108. }
  109. }
  110. }
  111. void killTrees()
  112. {
  113. foreach (LLUUID tree in m_trees)
  114. {
  115. double killLikelyhood = 0.0;
  116. if (m_scene.Entities.ContainsKey(tree))
  117. {
  118. SceneObjectPart selectedTree = ((SceneObjectGroup)m_scene.Entities[tree]).RootPart;
  119. double selectedTreeScale = Math.Sqrt(Math.Pow(selectedTree.Scale.X, 2) +
  120. Math.Pow(selectedTree.Scale.Y, 2) +
  121. Math.Pow(selectedTree.Scale.Z, 2));
  122. foreach (LLUUID picktree in m_trees)
  123. {
  124. if (picktree != tree)
  125. {
  126. SceneObjectPart pickedTree = ((SceneObjectGroup)m_scene.Entities[picktree]).RootPart;
  127. double pickedTreeScale = Math.Sqrt(Math.Pow(pickedTree.Scale.X, 2) +
  128. Math.Pow(pickedTree.Scale.Y, 2) +
  129. Math.Pow(pickedTree.Scale.Z, 2));
  130. double pickedTreeDistance = Math.Sqrt(Math.Pow(Math.Abs(pickedTree.AbsolutePosition.X - selectedTree.AbsolutePosition.X), 2) +
  131. Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Y - selectedTree.AbsolutePosition.Y), 2) +
  132. Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Z - selectedTree.AbsolutePosition.Z), 2));
  133. killLikelyhood += (selectedTreeScale / (pickedTreeScale * pickedTreeDistance)) * 0.1;
  134. }
  135. }
  136. if (Util.RandomClass.NextDouble() < killLikelyhood)
  137. {
  138. m_scene.RemoveEntity(selectedTree.ParentGroup);
  139. m_trees.Remove(selectedTree.ParentGroup.UUID);
  140. m_scene.ForEachClient(delegate(IClientAPI controller)
  141. {
  142. controller.SendKillObject(m_scene.RegionInfo.RegionHandle,
  143. selectedTree.LocalID);
  144. });
  145. break;
  146. }
  147. else
  148. {
  149. selectedTree.SetText(killLikelyhood.ToString(), new Axiom.Math.Vector3(1.0f, 1.0f, 1.0f), 1.0);
  150. }
  151. }
  152. else
  153. {
  154. m_trees.Remove(tree);
  155. }
  156. }
  157. }
  158. private void SpawnChild(SceneObjectPart s_tree)
  159. {
  160. LLVector3 position = new LLVector3();
  161. position.X = s_tree.AbsolutePosition.X + (1 * (-1 * Util.RandomClass.Next(1)));
  162. if (position.X > 255)
  163. position.X = 255;
  164. if (position.X < 0)
  165. position.X = 0;
  166. position.Y = s_tree.AbsolutePosition.Y + (1 * (-1 * Util.RandomClass.Next(1)));
  167. if (position.Y > 255)
  168. position.Y = 255;
  169. if (position.Y < 0)
  170. position.Y = 0;
  171. double randX = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
  172. double randY = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
  173. position.X += (float)randX;
  174. position.Y += (float)randY;
  175. CreateTree(position);
  176. }
  177. private void CreateTree(LLVector3 position)
  178. {
  179. position.Z = (float)m_scene.Terrain.heightmap.Get((int)position.X, (int)position.Y);
  180. SceneObjectGroup tree =
  181. m_scene.AddTree(new LLVector3(0.1f, 0.1f, 0.1f),
  182. LLQuaternion.Identity,
  183. position,
  184. Tree.Cypress1,
  185. false);
  186. m_trees.Add(tree.UUID);
  187. tree.SendGroupFullUpdate();
  188. }
  189. void CalculateTrees_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  190. {
  191. growTrees();
  192. seedTrees();
  193. killTrees();
  194. }
  195. public void PostInitialise()
  196. {
  197. }
  198. public void Close()
  199. {
  200. }
  201. public string Name
  202. {
  203. get { return "TreePopulatorModule"; }
  204. }
  205. public bool IsSharedModule
  206. {
  207. get { return false; }
  208. }
  209. }
  210. }