TreePopulatorModule.cs 9.2 KB

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