VegetationModule.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Reflection;
  29. using log4net;
  30. using Nini.Config;
  31. using OpenMetaverse;
  32. using OpenSim.Framework;
  33. using OpenSim.Framework.Communications;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes;
  37. namespace OpenSim.Region.Environment.Modules.Avatar.Vegetation
  38. {
  39. public class VegetationModule : IRegionModule, IVegetationModule
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. protected Scene m_scene;
  43. protected static readonly PCode[] creationCapabilities = new PCode[] { PCode.Grass, PCode.NewTree, PCode.Tree };
  44. public PCode[] CreationCapabilities { get { return creationCapabilities; } }
  45. public void Initialise(Scene scene, IConfigSource source)
  46. {
  47. m_scene = scene;
  48. m_scene.RegisterModuleInterface<IVegetationModule>(this);
  49. }
  50. public void PostInitialise() {}
  51. public void Close() {}
  52. public string Name { get { return "Vegetation Module"; } }
  53. public bool IsSharedModule { get { return false; } }
  54. public SceneObjectGroup AddTree(
  55. UUID uuid, UUID groupID, Vector3 scale, Quaternion rotation, Vector3 position, Tree treeType, bool newTree)
  56. {
  57. PrimitiveBaseShape treeShape = new PrimitiveBaseShape();
  58. treeShape.PathCurve = 16;
  59. treeShape.PathEnd = 49900;
  60. treeShape.PCode = newTree ? (byte)PCode.NewTree : (byte)PCode.Tree;
  61. treeShape.Scale = scale;
  62. treeShape.State = (byte)treeType;
  63. return m_scene.AddNewPrim(uuid, groupID, position, rotation, treeShape);
  64. }
  65. public SceneObjectGroup CreateEntity(
  66. UUID ownerID, UUID groupID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  67. {
  68. if (Array.IndexOf(creationCapabilities, (PCode)shape.PCode) < 0)
  69. {
  70. m_log.DebugFormat("[VEGETATION]: PCode {0} not handled by {1}", shape.PCode, Name);
  71. return null;
  72. }
  73. SceneObjectGroup sceneObject = new SceneObjectGroup(ownerID, pos, rot, shape);
  74. SceneObjectPart rootPart = sceneObject.GetChildPart(sceneObject.UUID);
  75. // if grass or tree, make phantom
  76. //rootPart.TrimPermissions();
  77. rootPart.AddFlag(PrimFlags.Phantom);
  78. if (rootPart.Shape.PCode != (byte)PCode.Grass)
  79. AdaptTree(ref shape);
  80. m_scene.AddNewSceneObject(sceneObject, true);
  81. sceneObject.SetGroup(groupID, null);
  82. return sceneObject;
  83. }
  84. protected void AdaptTree(ref PrimitiveBaseShape tree)
  85. {
  86. // Tree size has to be adapted depending on its type
  87. switch ((Tree)tree.State)
  88. {
  89. case Tree.Cypress1:
  90. case Tree.Cypress2:
  91. tree.Scale = new Vector3(4, 4, 10);
  92. break;
  93. // case... other tree types
  94. // tree.Scale = new Vector3(?, ?, ?);
  95. // break;
  96. default:
  97. tree.Scale = new Vector3(4, 4, 4);
  98. break;
  99. }
  100. }
  101. }
  102. }