TreePopulatorModule.cs 9.6 KB

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