TreePopulatorModule.cs 9.3 KB

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