TreePopulatorModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. namespace OpenSim.Region.OptionalModules.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 bool m_active_trees = false;
  49. private List<UUID> m_trees;
  50. Timer CalculateTrees;
  51. #region IRegionModule Members
  52. public void Initialise(Scene scene, IConfigSource config)
  53. {
  54. try
  55. {
  56. m_tree_density = config.Configs["Trees"].GetDouble("tree_density", m_tree_density);
  57. m_active_trees = config.Configs["Trees"].GetBoolean("active_trees", m_active_trees);
  58. }
  59. catch (Exception)
  60. {
  61. }
  62. m_trees = new List<UUID>();
  63. m_scene = scene;
  64. m_scene.EventManager.OnPluginConsole += EventManager_OnPluginConsole;
  65. if (m_active_trees)
  66. activeizeTreeze(true);
  67. m_log.Debug("[TREES]: Initialised tree module");
  68. }
  69. public void PostInitialise()
  70. {
  71. }
  72. public void Close()
  73. {
  74. }
  75. public string Name
  76. {
  77. get { return "TreePopulatorModule"; }
  78. }
  79. public bool IsSharedModule
  80. {
  81. get { return false; }
  82. }
  83. #endregion
  84. private void EventManager_OnPluginConsole(string[] args)
  85. {
  86. if (args.Length == 1)
  87. {
  88. if (args[0] == "tree")
  89. {
  90. UUID uuid = m_scene.RegionInfo.EstateSettings.EstateOwner;
  91. if (uuid == UUID.Zero)
  92. uuid = m_scene.RegionInfo.MasterAvatarAssignedUUID;
  93. m_log.Debug("[TREES]: New tree planting");
  94. CreateTree(uuid, new Vector3(128.0f, 128.0f, 0.0f));
  95. }
  96. }
  97. if (args.Length == 2 || args.Length == 3)
  98. {
  99. if (args[1] == "active")
  100. {
  101. if (args.Length >= 3)
  102. {
  103. if (args[2] == "true" && !m_active_trees)
  104. {
  105. m_active_trees = true;
  106. activeizeTreeze(m_active_trees);
  107. m_log.Info("[TREES]: Activizing Trees");
  108. }
  109. if (args[2] == "false" && m_active_trees)
  110. {
  111. m_active_trees = false;
  112. activeizeTreeze(m_active_trees);
  113. m_log.Info("[TREES]: Trees no longer Active, for now...");
  114. }
  115. }
  116. else
  117. {
  118. m_log.Info("[TREES]: When setting the tree module active via the console, you must specify true or false");
  119. }
  120. }
  121. }
  122. }
  123. private void activeizeTreeze(bool activeYN)
  124. {
  125. if (activeYN)
  126. {
  127. CalculateTrees = new Timer(m_tree_updates);
  128. CalculateTrees.Elapsed += CalculateTrees_Elapsed;
  129. CalculateTrees.Start();
  130. }
  131. else
  132. {
  133. CalculateTrees.Stop();
  134. }
  135. }
  136. private void growTrees()
  137. {
  138. foreach (UUID tree in m_trees)
  139. {
  140. if (m_scene.Entities.ContainsKey(tree))
  141. {
  142. SceneObjectPart s_tree = ((SceneObjectGroup) m_scene.Entities[tree]).RootPart;
  143. // 100 seconds to grow 1m
  144. s_tree.Scale += new Vector3(0.1f, 0.1f, 0.1f);
  145. s_tree.SendFullUpdateToAllClients();
  146. //s_tree.ScheduleTerseUpdate();
  147. }
  148. else
  149. {
  150. m_trees.Remove(tree);
  151. }
  152. }
  153. }
  154. private void seedTrees()
  155. {
  156. foreach (UUID tree in m_trees)
  157. {
  158. if (m_scene.Entities.ContainsKey(tree))
  159. {
  160. SceneObjectPart s_tree = ((SceneObjectGroup) m_scene.Entities[tree]).RootPart;
  161. if (s_tree.Scale.X > 0.5)
  162. {
  163. if (Util.RandomClass.NextDouble() > 0.75)
  164. {
  165. SpawnChild(s_tree);
  166. }
  167. }
  168. }
  169. else
  170. {
  171. m_trees.Remove(tree);
  172. }
  173. }
  174. }
  175. private void killTrees()
  176. {
  177. foreach (UUID tree in m_trees)
  178. {
  179. double killLikelyhood = 0.0;
  180. if (m_scene.Entities.ContainsKey(tree))
  181. {
  182. SceneObjectPart selectedTree = ((SceneObjectGroup) m_scene.Entities[tree]).RootPart;
  183. double selectedTreeScale = Math.Sqrt(Math.Pow(selectedTree.Scale.X, 2) +
  184. Math.Pow(selectedTree.Scale.Y, 2) +
  185. Math.Pow(selectedTree.Scale.Z, 2));
  186. foreach (UUID picktree in m_trees)
  187. {
  188. if (picktree != tree)
  189. {
  190. SceneObjectPart pickedTree = ((SceneObjectGroup) m_scene.Entities[picktree]).RootPart;
  191. double pickedTreeScale = Math.Sqrt(Math.Pow(pickedTree.Scale.X, 2) +
  192. Math.Pow(pickedTree.Scale.Y, 2) +
  193. Math.Pow(pickedTree.Scale.Z, 2));
  194. double pickedTreeDistance = Math.Sqrt(Math.Pow(Math.Abs(pickedTree.AbsolutePosition.X - selectedTree.AbsolutePosition.X), 2) +
  195. Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Y - selectedTree.AbsolutePosition.Y), 2) +
  196. Math.Pow(Math.Abs(pickedTree.AbsolutePosition.Z - selectedTree.AbsolutePosition.Z), 2));
  197. killLikelyhood += (selectedTreeScale / (pickedTreeScale * pickedTreeDistance)) * 0.1;
  198. }
  199. }
  200. if (Util.RandomClass.NextDouble() < killLikelyhood)
  201. {
  202. m_scene.DeleteSceneObject(selectedTree.ParentGroup, false);
  203. m_trees.Remove(selectedTree.ParentGroup.UUID);
  204. m_scene.ForEachClient(delegate(IClientAPI controller)
  205. {
  206. controller.SendKillObject(m_scene.RegionInfo.RegionHandle,
  207. selectedTree.LocalId);
  208. });
  209. break;
  210. }
  211. selectedTree.SetText(killLikelyhood.ToString(), new Vector3(1.0f, 1.0f, 1.0f), 1.0);
  212. }
  213. else
  214. {
  215. m_trees.Remove(tree);
  216. }
  217. }
  218. }
  219. private void SpawnChild(SceneObjectPart s_tree)
  220. {
  221. Vector3 position = new Vector3();
  222. position.X = s_tree.AbsolutePosition.X + (1 * (-1 * Util.RandomClass.Next(1)));
  223. if (position.X > 255)
  224. position.X = 255;
  225. if (position.X < 0)
  226. position.X = 0;
  227. position.Y = s_tree.AbsolutePosition.Y + (1 * (-1 * Util.RandomClass.Next(1)));
  228. if (position.Y > 255)
  229. position.Y = 255;
  230. if (position.Y < 0)
  231. position.Y = 0;
  232. double randX = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
  233. double randY = ((Util.RandomClass.NextDouble() * 2.0) - 1.0) * (s_tree.Scale.X * 3);
  234. position.X += (float) randX;
  235. position.Y += (float) randY;
  236. UUID uuid = m_scene.RegionInfo.EstateSettings.EstateOwner;
  237. if (uuid == UUID.Zero)
  238. uuid = m_scene.RegionInfo.MasterAvatarAssignedUUID;
  239. CreateTree(uuid, position);
  240. }
  241. private void CreateTree(UUID uuid, Vector3 position)
  242. {
  243. position.Z = (float) m_scene.Heightmap[(int) position.X, (int) position.Y];
  244. IVegetationModule module = m_scene.RequestModuleInterface<IVegetationModule>();
  245. if (null == module)
  246. return;
  247. SceneObjectGroup tree
  248. = module.AddTree(
  249. uuid, UUID.Zero, new Vector3(0.1f, 0.1f, 0.1f), Quaternion.Identity, position, Tree.Cypress1, false);
  250. m_trees.Add(tree.UUID);
  251. tree.SendGroupFullUpdate();
  252. }
  253. private void CalculateTrees_Elapsed(object sender, ElapsedEventArgs e)
  254. {
  255. growTrees();
  256. seedTrees();
  257. killTrees();
  258. }
  259. }
  260. }