MetaEntity.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. #region Header
  28. // MetaEntity.cs
  29. // User: bongiojp
  30. //
  31. // TODO:
  32. // Create a physics manager to the meta object if there isn't one or the object knows of no scene but the user wants physics enabled.
  33. #endregion Header
  34. using System;
  35. using System.Collections.Generic;
  36. using System.Drawing;
  37. using OpenMetaverse;
  38. using Nini.Config;
  39. using OpenSim.Framework;
  40. using OpenSim.Region.Environment.Interfaces;
  41. using OpenSim.Region.Environment.Scenes;
  42. using OpenSim.Region.Physics.Manager;
  43. using log4net;
  44. namespace OpenSim.Region.Environment.Modules.ContentManagement
  45. {
  46. public class MetaEntity
  47. {
  48. #region Constants
  49. public const float INVISIBLE = .95f;
  50. // Settings for transparency of metaentity
  51. public const float NONE = 0f;
  52. public const float TRANSLUCENT = .5f;
  53. #endregion Constants
  54. #region Static Fields
  55. protected static readonly ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  56. #endregion Static Fields
  57. #region Fields
  58. protected SceneObjectGroup m_Entity = null; // The scene object group that represents this meta entity.
  59. protected uint m_metaLocalid;
  60. #endregion Fields
  61. #region Constructors
  62. public MetaEntity()
  63. {
  64. }
  65. /// <summary>
  66. /// Makes a new meta entity by copying the given scene object group.
  67. /// The physics boolean is just a stub right now.
  68. /// </summary>
  69. public MetaEntity(SceneObjectGroup orig, bool physics)
  70. {
  71. m_Entity = orig.Copy(orig.RootPart.OwnerID, orig.RootPart.GroupID, false);
  72. Initialize(physics);
  73. }
  74. /// <summary>
  75. /// Takes an XML description of a scene object group and converts it to a meta entity.
  76. /// </summary>
  77. public MetaEntity(string objectXML, Scene scene, bool physics)
  78. {
  79. m_Entity = new SceneObjectGroup(objectXML);
  80. m_Entity.SetScene(scene);
  81. Initialize(physics);
  82. }
  83. #endregion Constructors
  84. #region Public Properties
  85. public Dictionary<UUID, SceneObjectPart> Children
  86. {
  87. get { return m_Entity.Children; }
  88. set { m_Entity.Children = value; }
  89. }
  90. public uint LocalId
  91. {
  92. get { return m_Entity.LocalId; }
  93. set { m_Entity.LocalId = value; }
  94. }
  95. public SceneObjectGroup ObjectGroup
  96. {
  97. get { return m_Entity; }
  98. }
  99. public int PrimCount
  100. {
  101. get { return m_Entity.PrimCount; }
  102. }
  103. public SceneObjectPart RootPart
  104. {
  105. get { return m_Entity.RootPart; }
  106. }
  107. public Scene Scene
  108. {
  109. get { return m_Entity.Scene; }
  110. }
  111. public UUID UUID
  112. {
  113. get { return m_Entity.UUID; }
  114. set { m_Entity.UUID = value; }
  115. }
  116. #endregion Public Properties
  117. #region Protected Methods
  118. // The metaentity objectgroup must have unique localids as well as unique uuids.
  119. // localids are used by the client to refer to parts.
  120. // uuids are sent to the client and back to the server to identify parts on the server side.
  121. /// <summary>
  122. /// Changes localids and uuids of m_Entity.
  123. /// </summary>
  124. protected void Initialize(bool physics)
  125. {
  126. //make new uuids
  127. Dictionary<UUID, SceneObjectPart> parts = new Dictionary<UUID, SceneObjectPart>();
  128. foreach (SceneObjectPart part in m_Entity.Children.Values)
  129. {
  130. part.ResetIDs(part.LinkNum);
  131. parts.Add(part.UUID, part);
  132. }
  133. //finalize
  134. m_Entity.RootPart.PhysActor = null;
  135. m_Entity.Children = parts;
  136. }
  137. #endregion Protected Methods
  138. #region Public Methods
  139. /// <summary>
  140. /// Hides the metaentity from a single client.
  141. /// </summary>
  142. public virtual void Hide(IClientAPI client)
  143. {
  144. //This deletes the group without removing from any databases.
  145. //This is important because we are not IN any database.
  146. //m_Entity.FakeDeleteGroup();
  147. foreach (SceneObjectPart part in m_Entity.Children.Values)
  148. client.SendKillObject(m_Entity.RegionHandle, part.LocalId);
  149. }
  150. /// <summary>
  151. /// Sends a kill object message to all clients, effectively "hiding" the metaentity even though it's still on the server.
  152. /// </summary>
  153. public virtual void HideFromAll()
  154. {
  155. foreach (SceneObjectPart part in m_Entity.Children.Values)
  156. m_Entity.Scene.ClientManager.ForEachClient(delegate(IClientAPI controller)
  157. { controller.SendKillObject(m_Entity.RegionHandle, part.LocalId); }
  158. );
  159. }
  160. public void SendFullUpdate(IClientAPI client)
  161. {
  162. // Not sure what clientFlags should be but 0 seems to work
  163. SendFullUpdate(client, 0);
  164. }
  165. public void SendFullUpdate(IClientAPI client, uint clientFlags)
  166. {
  167. m_Entity.SendFullUpdateToClient(client);
  168. }
  169. public void SendFullUpdateToAll()
  170. {
  171. m_Entity.Scene.ClientManager.ForEachClient(delegate(IClientAPI controller)
  172. { m_Entity.SendFullUpdateToClient(controller); }
  173. );
  174. }
  175. /// <summary>
  176. /// Makes a single SceneObjectPart see through.
  177. /// </summary>
  178. /// <param name="part">
  179. /// A <see cref="SceneObjectPart"/>
  180. /// The part to make see through
  181. /// </param>
  182. /// <param name="transparencyAmount">
  183. /// A <see cref="System.Single"/>
  184. /// The degree of transparency to imbue the part with, 0f being solid and .95f being invisible.
  185. /// </param>
  186. public static void SetPartTransparency(SceneObjectPart part, float transparencyAmount)
  187. {
  188. Primitive.TextureEntry tex = null;
  189. Color4 texcolor;
  190. try
  191. {
  192. tex = part.Shape.Textures;
  193. texcolor = new Color4();
  194. }
  195. catch(Exception)
  196. {
  197. //m_log.ErrorFormat("[Content Management]: Exception thrown while accessing textures of scene object: " + e);
  198. return;
  199. }
  200. for (uint i = 0; i < tex.FaceTextures.Length; i++)
  201. {
  202. try {
  203. if (tex.FaceTextures[i] != null)
  204. {
  205. texcolor = tex.FaceTextures[i].RGBA;
  206. texcolor.A = transparencyAmount;
  207. tex.FaceTextures[i].RGBA = texcolor;
  208. }
  209. }
  210. catch (Exception)
  211. {
  212. //m_log.ErrorFormat("[Content Management]: Exception thrown while accessing different face textures of object: " + e);
  213. continue;
  214. }
  215. }
  216. try {
  217. texcolor = tex.DefaultTexture.RGBA;
  218. texcolor.A = transparencyAmount;
  219. tex.DefaultTexture.RGBA = texcolor;
  220. part.Shape.TextureEntry = tex.ToBytes();
  221. }
  222. catch (Exception)
  223. {
  224. //m_log.Info("[Content Management]: Exception thrown while accessing default face texture of object: " + e);
  225. }
  226. }
  227. #endregion Public Methods
  228. }
  229. }