PageRenderTime 69ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/OpenSim/Region/Framework/Scenes/SceneObjectGroup.cs

https://bitbucket.org/KyanhaLLC/opensim
C# | 3641 lines | 2323 code | 506 blank | 812 comment | 455 complexity | bf1bf34707346d156ea424dfa591c268 MD5 | raw file
Possible License(s): Unlicense, MIT, BSD-3-Clause
  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 OpenSimulator 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.ComponentModel;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Threading;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using OpenMetaverse;
  37. using OpenMetaverse.Packets;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Physics.Manager;
  41. using OpenSim.Region.Framework.Scenes.Serialization;
  42. namespace OpenSim.Region.Framework.Scenes
  43. {
  44. [Flags]
  45. public enum scriptEvents
  46. {
  47. None = 0,
  48. attach = 1,
  49. collision = 16,
  50. collision_end = 32,
  51. collision_start = 64,
  52. control = 128,
  53. dataserver = 256,
  54. email = 512,
  55. http_response = 1024,
  56. land_collision = 2048,
  57. land_collision_end = 4096,
  58. land_collision_start = 8192,
  59. at_target = 16384,
  60. at_rot_target = 16777216,
  61. listen = 32768,
  62. money = 65536,
  63. moving_end = 131072,
  64. moving_start = 262144,
  65. not_at_rot_target = 524288,
  66. not_at_target = 1048576,
  67. remote_data = 8388608,
  68. run_time_permissions = 268435456,
  69. state_entry = 1073741824,
  70. state_exit = 2,
  71. timer = 4,
  72. touch = 8,
  73. touch_end = 536870912,
  74. touch_start = 2097152,
  75. object_rez = 4194304
  76. }
  77. struct scriptPosTarget
  78. {
  79. public Vector3 targetPos;
  80. public float tolerance;
  81. public uint handle;
  82. }
  83. struct scriptRotTarget
  84. {
  85. public Quaternion targetRot;
  86. public float tolerance;
  87. public uint handle;
  88. }
  89. public delegate void PrimCountTaintedDelegate();
  90. /// <summary>
  91. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  92. /// (often known as prims), one of which is considered the root part.
  93. /// </summary>
  94. public partial class SceneObjectGroup : EntityBase, ISceneObject
  95. {
  96. // Axis selection bitmask used by SetAxisRotation()
  97. // Just happen to be the same bits used by llSetStatus() and defined in ScriptBaseClass.
  98. public enum axisSelect : int
  99. {
  100. STATUS_ROTATE_X = 0x002,
  101. STATUS_ROTATE_Y = 0x004,
  102. STATUS_ROTATE_Z = 0x008,
  103. }
  104. // private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  105. /// <summary>
  106. /// Signal whether the non-inventory attributes of any prims in the group have changed
  107. /// since the group's last persistent backup
  108. /// </summary>
  109. private bool m_hasGroupChanged = false;
  110. private long timeFirstChanged;
  111. private long timeLastChanged;
  112. /// <summary>
  113. /// This indicates whether the object has changed such that it needs to be repersisted to permenant storage
  114. /// (the database).
  115. /// </summary>
  116. /// <remarks>
  117. /// Ultimately, this should be managed such that region modules can change it at the end of a set of operations
  118. /// so that either all changes are preserved or none at all. However, currently, a large amount of internal
  119. /// code will set this anyway when some object properties are changed.
  120. /// </remarks>
  121. public bool HasGroupChanged
  122. {
  123. set
  124. {
  125. if (value)
  126. {
  127. timeLastChanged = DateTime.Now.Ticks;
  128. if (!m_hasGroupChanged)
  129. timeFirstChanged = DateTime.Now.Ticks;
  130. }
  131. m_hasGroupChanged = value;
  132. // m_log.DebugFormat(
  133. // "[SCENE OBJECT GROUP]: HasGroupChanged set to {0} for {1} {2}", m_hasGroupChanged, Name, LocalId);
  134. }
  135. get { return m_hasGroupChanged; }
  136. }
  137. /// <summary>
  138. /// Has the group changed due to an unlink operation? We record this in order to optimize deletion, since
  139. /// an unlinked group currently has to be persisted to the database before we can perform an unlink operation.
  140. /// </summary>
  141. public bool HasGroupChangedDueToDelink { get; private set; }
  142. private bool isTimeToPersist()
  143. {
  144. if (IsSelected || IsDeleted || IsAttachment)
  145. return false;
  146. if (!m_hasGroupChanged)
  147. return false;
  148. if (m_scene.ShuttingDown)
  149. return true;
  150. long currentTime = DateTime.Now.Ticks;
  151. if (currentTime - timeLastChanged > m_scene.m_dontPersistBefore || currentTime - timeFirstChanged > m_scene.m_persistAfter)
  152. return true;
  153. return false;
  154. }
  155. /// <summary>
  156. /// Is this scene object acting as an attachment?
  157. /// </summary>
  158. public bool IsAttachment { get; set; }
  159. /// <summary>
  160. /// The avatar to which this scene object is attached.
  161. /// </summary>
  162. /// <remarks>
  163. /// If we're not attached to an avatar then this is UUID.Zero
  164. /// </remarks>
  165. public UUID AttachedAvatar { get; set; }
  166. /// <summary>
  167. /// Attachment point of this scene object to an avatar.
  168. /// </summary>
  169. /// <remarks>
  170. /// 0 if we're not attached to anything
  171. /// </remarks>
  172. public uint AttachmentPoint
  173. {
  174. get
  175. {
  176. return m_rootPart.Shape.State;
  177. }
  178. set
  179. {
  180. IsAttachment = value != 0;
  181. m_rootPart.Shape.State = (byte)value;
  182. }
  183. }
  184. /// <summary>
  185. /// If this scene object has an attachment point then indicate whether there is a point where
  186. /// attachments are perceivable by avatars other than the avatar to which this object is attached.
  187. /// </summary>
  188. /// <remarks>
  189. /// HUDs are not perceivable by other avatars.
  190. /// </remarks>
  191. public bool HasPrivateAttachmentPoint
  192. {
  193. get
  194. {
  195. return AttachmentPoint >= (uint)OpenMetaverse.AttachmentPoint.HUDCenter2
  196. && AttachmentPoint <= (uint)OpenMetaverse.AttachmentPoint.HUDBottomRight;
  197. }
  198. }
  199. public void ClearPartAttachmentData()
  200. {
  201. AttachmentPoint = 0;
  202. // Even though we don't use child part state parameters for attachments any more, we still need to set
  203. // these to zero since having them non-zero in rezzed scene objects will crash some clients. Even if
  204. // we store them correctly, scene objects that we receive from elsewhere might not.
  205. foreach (SceneObjectPart part in Parts)
  206. part.Shape.State = 0;
  207. }
  208. /// <summary>
  209. /// Is this scene object phantom?
  210. /// </summary>
  211. /// <remarks>
  212. /// Updating must currently take place through UpdatePrimFlags()
  213. /// </remarks>
  214. public bool IsPhantom
  215. {
  216. get { return (RootPart.Flags & PrimFlags.Phantom) != 0; }
  217. }
  218. /// <summary>
  219. /// Does this scene object use physics?
  220. /// </summary>
  221. /// <remarks>
  222. /// Updating must currently take place through UpdatePrimFlags()
  223. /// </remarks>
  224. public bool UsesPhysics
  225. {
  226. get { return (RootPart.Flags & PrimFlags.Physics) != 0; }
  227. }
  228. /// <summary>
  229. /// Is this scene object temporary?
  230. /// </summary>
  231. /// <remarks>
  232. /// Updating must currently take place through UpdatePrimFlags()
  233. /// </remarks>
  234. public bool IsTemporary
  235. {
  236. get { return (RootPart.Flags & PrimFlags.TemporaryOnRez) != 0; }
  237. }
  238. public bool IsVolumeDetect
  239. {
  240. get { return RootPart.VolumeDetectActive; }
  241. }
  242. private Vector3 lastPhysGroupPos;
  243. private Quaternion lastPhysGroupRot;
  244. private bool m_isBackedUp;
  245. protected MapAndArray<UUID, SceneObjectPart> m_parts = new MapAndArray<UUID, SceneObjectPart>();
  246. protected ulong m_regionHandle;
  247. protected SceneObjectPart m_rootPart;
  248. // private Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  249. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  250. private Dictionary<uint, scriptRotTarget> m_rotTargets = new Dictionary<uint, scriptRotTarget>();
  251. private bool m_scriptListens_atTarget;
  252. private bool m_scriptListens_notAtTarget;
  253. private bool m_scriptListens_atRotTarget;
  254. private bool m_scriptListens_notAtRotTarget;
  255. internal Dictionary<UUID, string> m_savedScriptState;
  256. #region Properties
  257. /// <summary>
  258. /// The name of an object grouping is always the same as its root part
  259. /// </summary>
  260. public override string Name
  261. {
  262. get { return RootPart.Name; }
  263. set { RootPart.Name = value; }
  264. }
  265. public string Description
  266. {
  267. get { return RootPart.Description; }
  268. set { RootPart.Description = value; }
  269. }
  270. /// <summary>
  271. /// Added because the Parcel code seems to use it
  272. /// but not sure a object should have this
  273. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  274. /// think really there should be a list (or whatever) in each scenepresence
  275. /// saying what prim(s) that user has selected.
  276. /// </summary>
  277. protected bool m_isSelected = false;
  278. /// <summary>
  279. /// Number of prims in this group
  280. /// </summary>
  281. public int PrimCount
  282. {
  283. get { return m_parts.Count; }
  284. }
  285. public Quaternion GroupRotation
  286. {
  287. get { return m_rootPart.RotationOffset; }
  288. }
  289. public Vector3 GroupScale
  290. {
  291. get
  292. {
  293. Vector3 minScale = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionSize);
  294. Vector3 maxScale = Vector3.Zero;
  295. Vector3 finalScale = new Vector3(0.5f, 0.5f, 0.5f);
  296. SceneObjectPart[] parts = m_parts.GetArray();
  297. for (int i = 0; i < parts.Length; i++)
  298. {
  299. SceneObjectPart part = parts[i];
  300. Vector3 partscale = part.Scale;
  301. Vector3 partoffset = part.OffsetPosition;
  302. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  303. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.Y + partoffset.Y : minScale.Y;
  304. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.Z + partoffset.Z : minScale.Z;
  305. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  306. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  307. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  308. }
  309. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  310. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  311. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  312. return finalScale;
  313. }
  314. }
  315. public UUID GroupID
  316. {
  317. get { return m_rootPart.GroupID; }
  318. set { m_rootPart.GroupID = value; }
  319. }
  320. public SceneObjectPart[] Parts
  321. {
  322. get { return m_parts.GetArray(); }
  323. }
  324. public bool ContainsPart(UUID partID)
  325. {
  326. return m_parts.ContainsKey(partID);
  327. }
  328. /// <summary>
  329. /// Does this group contain the given part?
  330. /// should be able to remove these methods once we have a entity index in scene
  331. /// </summary>
  332. /// <param name="localID"></param>
  333. /// <returns></returns>
  334. public bool ContainsPart(uint localID)
  335. {
  336. SceneObjectPart[] parts = m_parts.GetArray();
  337. for (int i = 0; i < parts.Length; i++)
  338. {
  339. if (parts[i].LocalId == localID)
  340. return true;
  341. }
  342. return false;
  343. }
  344. /// <value>
  345. /// The root part of this scene object
  346. /// </value>
  347. public SceneObjectPart RootPart
  348. {
  349. get { return m_rootPart; }
  350. }
  351. public ulong RegionHandle
  352. {
  353. get { return m_regionHandle; }
  354. set
  355. {
  356. m_regionHandle = value;
  357. SceneObjectPart[] parts = m_parts.GetArray();
  358. for (int i = 0; i < parts.Length; i++)
  359. parts[i].RegionHandle = value;
  360. }
  361. }
  362. /// <summary>
  363. /// Check both the attachment property and the relevant properties of the underlying root part.
  364. /// </summary>
  365. /// <remarks>
  366. /// This is necessary in some cases, particularly when a scene object has just crossed into a region and doesn't
  367. /// have the IsAttachment property yet checked.
  368. ///
  369. /// FIXME: However, this should be fixed so that this property
  370. /// propertly reflects the underlying status.
  371. /// </remarks>
  372. /// <returns></returns>
  373. public bool IsAttachmentCheckFull()
  374. {
  375. return (IsAttachment || (m_rootPart.Shape.PCode == 9 && m_rootPart.Shape.State != 0));
  376. }
  377. /// <summary>
  378. /// The absolute position of this scene object in the scene
  379. /// </summary>
  380. public override Vector3 AbsolutePosition
  381. {
  382. get { return m_rootPart.GroupPosition; }
  383. set
  384. {
  385. Vector3 val = value;
  386. if (Scene != null)
  387. {
  388. if (
  389. // (Scene.TestBorderCross(val - Vector3.UnitX, Cardinals.E)
  390. // || Scene.TestBorderCross(val + Vector3.UnitX, Cardinals.W)
  391. // || Scene.TestBorderCross(val - Vector3.UnitY, Cardinals.N)
  392. // || Scene.TestBorderCross(val + Vector3.UnitY, Cardinals.S))
  393. // Experimental change for better border crossings.
  394. // The commented out original lines above would, it seems, trigger
  395. // a border crossing a little early or late depending on which
  396. // direction the object was moving.
  397. (Scene.TestBorderCross(val, Cardinals.E)
  398. || Scene.TestBorderCross(val, Cardinals.W)
  399. || Scene.TestBorderCross(val, Cardinals.N)
  400. || Scene.TestBorderCross(val, Cardinals.S))
  401. && !IsAttachmentCheckFull() && (!Scene.LoadingPrims))
  402. {
  403. m_scene.CrossPrimGroupIntoNewRegion(val, this, true);
  404. }
  405. }
  406. if (RootPart.GetStatusSandbox())
  407. {
  408. if (Util.GetDistanceTo(RootPart.StatusSandboxPos, value) > 10)
  409. {
  410. RootPart.ScriptSetPhysicsStatus(false);
  411. if (Scene != null)
  412. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  413. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  414. return;
  415. }
  416. }
  417. // Restuff the new GroupPosition into each SOP of the linkset.
  418. // This has the affect of resetting and tainting the physics actors.
  419. SceneObjectPart[] parts = m_parts.GetArray();
  420. for (int i = 0; i < parts.Length; i++)
  421. parts[i].GroupPosition = val;
  422. //if (m_rootPart.PhysActor != null)
  423. //{
  424. //m_rootPart.PhysActor.Position =
  425. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  426. //m_rootPart.GroupPosition.Z);
  427. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  428. //}
  429. if (Scene != null)
  430. Scene.EventManager.TriggerParcelPrimCountTainted();
  431. }
  432. }
  433. public override uint LocalId
  434. {
  435. get { return m_rootPart.LocalId; }
  436. set { m_rootPart.LocalId = value; }
  437. }
  438. public override UUID UUID
  439. {
  440. get { return m_rootPart.UUID; }
  441. set
  442. {
  443. lock (m_parts.SyncRoot)
  444. {
  445. m_parts.Remove(m_rootPart.UUID);
  446. m_rootPart.UUID = value;
  447. m_parts.Add(value, m_rootPart);
  448. }
  449. }
  450. }
  451. public UUID LastOwnerID
  452. {
  453. get { return m_rootPart.LastOwnerID; }
  454. set { m_rootPart.LastOwnerID = value; }
  455. }
  456. public UUID OwnerID
  457. {
  458. get { return m_rootPart.OwnerID; }
  459. set { m_rootPart.OwnerID = value; }
  460. }
  461. public float Damage
  462. {
  463. get { return m_rootPart.Damage; }
  464. set { m_rootPart.Damage = value; }
  465. }
  466. public Color Color
  467. {
  468. get { return m_rootPart.Color; }
  469. set { m_rootPart.Color = value; }
  470. }
  471. public string Text
  472. {
  473. get {
  474. string returnstr = m_rootPart.Text;
  475. if (returnstr.Length > 255)
  476. {
  477. returnstr = returnstr.Substring(0, 255);
  478. }
  479. return returnstr;
  480. }
  481. set { m_rootPart.Text = value; }
  482. }
  483. protected virtual bool InSceneBackup
  484. {
  485. get { return true; }
  486. }
  487. public bool IsSelected
  488. {
  489. get { return m_isSelected; }
  490. set
  491. {
  492. m_isSelected = value;
  493. // Tell physics engine that group is selected
  494. PhysicsActor pa = m_rootPart.PhysActor;
  495. if (pa != null)
  496. {
  497. pa.Selected = value;
  498. // Pass it on to the children.
  499. SceneObjectPart[] parts = m_parts.GetArray();
  500. for (int i = 0; i < parts.Length; i++)
  501. {
  502. SceneObjectPart child = parts[i];
  503. PhysicsActor childPa = child.PhysActor;
  504. if (childPa != null)
  505. childPa.Selected = value;
  506. }
  507. }
  508. }
  509. }
  510. private SceneObjectPart m_PlaySoundMasterPrim = null;
  511. public SceneObjectPart PlaySoundMasterPrim
  512. {
  513. get { return m_PlaySoundMasterPrim; }
  514. set { m_PlaySoundMasterPrim = value; }
  515. }
  516. private List<SceneObjectPart> m_PlaySoundSlavePrims = new List<SceneObjectPart>();
  517. public List<SceneObjectPart> PlaySoundSlavePrims
  518. {
  519. get { return m_PlaySoundSlavePrims; }
  520. set { m_PlaySoundSlavePrims = value; }
  521. }
  522. private SceneObjectPart m_LoopSoundMasterPrim = null;
  523. public SceneObjectPart LoopSoundMasterPrim
  524. {
  525. get { return m_LoopSoundMasterPrim; }
  526. set { m_LoopSoundMasterPrim = value; }
  527. }
  528. private List<SceneObjectPart> m_LoopSoundSlavePrims = new List<SceneObjectPart>();
  529. public List<SceneObjectPart> LoopSoundSlavePrims
  530. {
  531. get { return m_LoopSoundSlavePrims; }
  532. set { m_LoopSoundSlavePrims = value; }
  533. }
  534. /// <summary>
  535. /// The UUID for the region this object is in.
  536. /// </summary>
  537. public UUID RegionUUID
  538. {
  539. get
  540. {
  541. if (m_scene != null)
  542. {
  543. return m_scene.RegionInfo.RegionID;
  544. }
  545. return UUID.Zero;
  546. }
  547. }
  548. /// <summary>
  549. /// The item ID that this object was rezzed from, if applicable.
  550. /// </summary>
  551. /// <remarks>
  552. /// If not applicable will be UUID.Zero
  553. /// </remarks>
  554. public UUID FromItemID { get; set; }
  555. /// <summary>
  556. /// Refers to the SceneObjectPart.UUID property of the object that this object was rezzed from, if applicable.
  557. /// </summary>
  558. /// <remarks>
  559. /// If not applicable will be UUID.Zero
  560. /// </remarks>
  561. public UUID FromPartID { get; set; }
  562. /// <summary>
  563. /// The folder ID that this object was rezzed from, if applicable.
  564. /// </summary>
  565. /// <remarks>
  566. /// If not applicable will be UUID.Zero
  567. /// </remarks>
  568. public UUID FromFolderID { get; set; }
  569. /// <summary>
  570. /// IDs of all avatars sat on this scene object.
  571. /// </summary>
  572. /// <remarks>
  573. /// We need this so that we can maintain a linkset wide ordering of avatars sat on different parts.
  574. /// This must be locked before it is read or written.
  575. /// SceneObjectPart sitting avatar add/remove code also locks on this object to avoid race conditions.
  576. /// No avatar should appear more than once in this list.
  577. /// Do not manipulate this list directly - use the Add/Remove sitting avatar methods on SceneObjectPart.
  578. /// </remarks>
  579. protected internal List<UUID> m_sittingAvatars = new List<UUID>();
  580. #endregion
  581. // ~SceneObjectGroup()
  582. // {
  583. // //m_log.DebugFormat("[SCENE OBJECT GROUP]: Destructor called for {0}, local id {1}", Name, LocalId);
  584. // Console.WriteLine("Destructor called for {0}, local id {1}", Name, LocalId);
  585. // }
  586. #region Constructors
  587. /// <summary>
  588. /// Constructor
  589. /// </summary>
  590. public SceneObjectGroup()
  591. {
  592. }
  593. /// <summary>
  594. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  595. /// The original SceneObjectPart will be used rather than a copy, preserving
  596. /// its existing localID and UUID.
  597. /// </summary>
  598. /// <param name='part'>Root part for this scene object.</param>
  599. public SceneObjectGroup(SceneObjectPart part) : this()
  600. {
  601. SetRootPart(part);
  602. }
  603. /// <summary>
  604. /// Constructor. This object is added to the scene later via AttachToScene()
  605. /// </summary>
  606. public SceneObjectGroup(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  607. :this(new SceneObjectPart(ownerID, shape, pos, rot, Vector3.Zero))
  608. {
  609. }
  610. /// <summary>
  611. /// Constructor.
  612. /// </summary>
  613. public SceneObjectGroup(UUID ownerID, Vector3 pos, PrimitiveBaseShape shape)
  614. : this(ownerID, pos, Quaternion.Identity, shape)
  615. {
  616. }
  617. public void LoadScriptState(XmlDocument doc)
  618. {
  619. XmlNodeList nodes = doc.GetElementsByTagName("SavedScriptState");
  620. if (nodes.Count > 0)
  621. {
  622. if (m_savedScriptState == null)
  623. m_savedScriptState = new Dictionary<UUID, string>();
  624. foreach (XmlNode node in nodes)
  625. {
  626. if (node.Attributes["UUID"] != null)
  627. {
  628. UUID itemid = new UUID(node.Attributes["UUID"].Value);
  629. if (itemid != UUID.Zero)
  630. m_savedScriptState[itemid] = node.InnerXml;
  631. }
  632. }
  633. }
  634. }
  635. /// <summary>
  636. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  637. /// </summary>
  638. public virtual void AttachToBackup()
  639. {
  640. if (InSceneBackup)
  641. {
  642. //m_log.DebugFormat(
  643. // "[SCENE OBJECT GROUP]: Attaching object {0} {1} to scene presistence sweep", Name, UUID);
  644. if (!m_isBackedUp)
  645. m_scene.EventManager.OnBackup += ProcessBackup;
  646. m_isBackedUp = true;
  647. }
  648. }
  649. /// <summary>
  650. /// Attach this object to a scene. It will also now appear to agents.
  651. /// </summary>
  652. /// <param name="scene"></param>
  653. public void AttachToScene(Scene scene)
  654. {
  655. m_scene = scene;
  656. RegionHandle = m_scene.RegionInfo.RegionHandle;
  657. if (m_rootPart.Shape.PCode != 9 || m_rootPart.Shape.State == 0)
  658. m_rootPart.ParentID = 0;
  659. if (m_rootPart.LocalId == 0)
  660. m_rootPart.LocalId = m_scene.AllocateLocalId();
  661. SceneObjectPart[] parts = m_parts.GetArray();
  662. for (int i = 0; i < parts.Length; i++)
  663. {
  664. SceneObjectPart part = parts[i];
  665. if (Object.ReferenceEquals(part, m_rootPart))
  666. continue;
  667. if (part.LocalId == 0)
  668. part.LocalId = m_scene.AllocateLocalId();
  669. part.ParentID = m_rootPart.LocalId;
  670. //m_log.DebugFormat("[SCENE]: Given local id {0} to part {1}, linknum {2}, parent {3} {4}", part.LocalId, part.UUID, part.LinkNum, part.ParentID, part.ParentUUID);
  671. }
  672. ApplyPhysics();
  673. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  674. // for the same object with very different properties. The caller must schedule the update.
  675. //ScheduleGroupForFullUpdate();
  676. }
  677. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  678. {
  679. // We got a request from the inner_scene to raytrace along the Ray hRay
  680. // We're going to check all of the prim in this group for intersection with the ray
  681. // If we get a result, we're going to find the closest result to the origin of the ray
  682. // and send back the intersection information back to the innerscene.
  683. EntityIntersection result = new EntityIntersection();
  684. SceneObjectPart[] parts = m_parts.GetArray();
  685. for (int i = 0; i < parts.Length; i++)
  686. {
  687. SceneObjectPart part = parts[i];
  688. // Temporary commented to stop compiler warning
  689. //Vector3 partPosition =
  690. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  691. Quaternion parentrotation = GroupRotation;
  692. // Telling the prim to raytrace.
  693. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  694. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation, frontFacesOnly, faceCenters);
  695. // This may need to be updated to the maximum draw distance possible..
  696. // We might (and probably will) be checking for prim creation from other sims
  697. // when the camera crosses the border.
  698. float idist = Constants.RegionSize;
  699. if (inter.HitTF)
  700. {
  701. // We need to find the closest prim to return to the testcaller along the ray
  702. if (inter.distance < idist)
  703. {
  704. result.HitTF = true;
  705. result.ipoint = inter.ipoint;
  706. result.obj = part;
  707. result.normal = inter.normal;
  708. result.distance = inter.distance;
  709. }
  710. }
  711. }
  712. return result;
  713. }
  714. /// <summary>
  715. /// Gets a vector representing the size of the bounding box containing all the prims in the group
  716. /// Treats all prims as rectangular, so no shape (cut etc) is taken into account
  717. /// offsetHeight is the offset in the Z axis from the centre of the bounding box to the centre of the root prim
  718. /// </summary>
  719. /// <returns></returns>
  720. public void GetAxisAlignedBoundingBoxRaw(out float minX, out float maxX, out float minY, out float maxY, out float minZ, out float maxZ)
  721. {
  722. maxX = -256f;
  723. maxY = -256f;
  724. maxZ = -256f;
  725. minX = 256f;
  726. minY = 256f;
  727. minZ = 8192f;
  728. SceneObjectPart[] parts = m_parts.GetArray();
  729. for (int i = 0; i < parts.Length; i++)
  730. {
  731. SceneObjectPart part = parts[i];
  732. Vector3 worldPos = part.GetWorldPosition();
  733. Vector3 offset = worldPos - AbsolutePosition;
  734. Quaternion worldRot;
  735. if (part.ParentID == 0)
  736. worldRot = part.RotationOffset;
  737. else
  738. worldRot = part.GetWorldRotation();
  739. Vector3 frontTopLeft;
  740. Vector3 frontTopRight;
  741. Vector3 frontBottomLeft;
  742. Vector3 frontBottomRight;
  743. Vector3 backTopLeft;
  744. Vector3 backTopRight;
  745. Vector3 backBottomLeft;
  746. Vector3 backBottomRight;
  747. Vector3 orig = Vector3.Zero;
  748. frontTopLeft.X = orig.X - (part.Scale.X / 2);
  749. frontTopLeft.Y = orig.Y - (part.Scale.Y / 2);
  750. frontTopLeft.Z = orig.Z + (part.Scale.Z / 2);
  751. frontTopRight.X = orig.X - (part.Scale.X / 2);
  752. frontTopRight.Y = orig.Y + (part.Scale.Y / 2);
  753. frontTopRight.Z = orig.Z + (part.Scale.Z / 2);
  754. frontBottomLeft.X = orig.X - (part.Scale.X / 2);
  755. frontBottomLeft.Y = orig.Y - (part.Scale.Y / 2);
  756. frontBottomLeft.Z = orig.Z - (part.Scale.Z / 2);
  757. frontBottomRight.X = orig.X - (part.Scale.X / 2);
  758. frontBottomRight.Y = orig.Y + (part.Scale.Y / 2);
  759. frontBottomRight.Z = orig.Z - (part.Scale.Z / 2);
  760. backTopLeft.X = orig.X + (part.Scale.X / 2);
  761. backTopLeft.Y = orig.Y - (part.Scale.Y / 2);
  762. backTopLeft.Z = orig.Z + (part.Scale.Z / 2);
  763. backTopRight.X = orig.X + (part.Scale.X / 2);
  764. backTopRight.Y = orig.Y + (part.Scale.Y / 2);
  765. backTopRight.Z = orig.Z + (part.Scale.Z / 2);
  766. backBottomLeft.X = orig.X + (part.Scale.X / 2);
  767. backBottomLeft.Y = orig.Y - (part.Scale.Y / 2);
  768. backBottomLeft.Z = orig.Z - (part.Scale.Z / 2);
  769. backBottomRight.X = orig.X + (part.Scale.X / 2);
  770. backBottomRight.Y = orig.Y + (part.Scale.Y / 2);
  771. backBottomRight.Z = orig.Z - (part.Scale.Z / 2);
  772. frontTopLeft = frontTopLeft * worldRot;
  773. frontTopRight = frontTopRight * worldRot;
  774. frontBottomLeft = frontBottomLeft * worldRot;
  775. frontBottomRight = frontBottomRight * worldRot;
  776. backBottomLeft = backBottomLeft * worldRot;
  777. backBottomRight = backBottomRight * worldRot;
  778. backTopLeft = backTopLeft * worldRot;
  779. backTopRight = backTopRight * worldRot;
  780. frontTopLeft += offset;
  781. frontTopRight += offset;
  782. frontBottomLeft += offset;
  783. frontBottomRight += offset;
  784. backBottomLeft += offset;
  785. backBottomRight += offset;
  786. backTopLeft += offset;
  787. backTopRight += offset;
  788. if (frontTopRight.X > maxX)
  789. maxX = frontTopRight.X;
  790. if (frontTopLeft.X > maxX)
  791. maxX = frontTopLeft.X;
  792. if (frontBottomRight.X > maxX)
  793. maxX = frontBottomRight.X;
  794. if (frontBottomLeft.X > maxX)
  795. maxX = frontBottomLeft.X;
  796. if (backTopRight.X > maxX)
  797. maxX = backTopRight.X;
  798. if (backTopLeft.X > maxX)
  799. maxX = backTopLeft.X;
  800. if (backBottomRight.X > maxX)
  801. maxX = backBottomRight.X;
  802. if (backBottomLeft.X > maxX)
  803. maxX = backBottomLeft.X;
  804. if (frontTopRight.X < minX)
  805. minX = frontTopRight.X;
  806. if (frontTopLeft.X < minX)
  807. minX = frontTopLeft.X;
  808. if (frontBottomRight.X < minX)
  809. minX = frontBottomRight.X;
  810. if (frontBottomLeft.X < minX)
  811. minX = frontBottomLeft.X;
  812. if (backTopRight.X < minX)
  813. minX = backTopRight.X;
  814. if (backTopLeft.X < minX)
  815. minX = backTopLeft.X;
  816. if (backBottomRight.X < minX)
  817. minX = backBottomRight.X;
  818. if (backBottomLeft.X < minX)
  819. minX = backBottomLeft.X;
  820. //
  821. if (frontTopRight.Y > maxY)
  822. maxY = frontTopRight.Y;
  823. if (frontTopLeft.Y > maxY)
  824. maxY = frontTopLeft.Y;
  825. if (frontBottomRight.Y > maxY)
  826. maxY = frontBottomRight.Y;
  827. if (frontBottomLeft.Y > maxY)
  828. maxY = frontBottomLeft.Y;
  829. if (backTopRight.Y > maxY)
  830. maxY = backTopRight.Y;
  831. if (backTopLeft.Y > maxY)
  832. maxY = backTopLeft.Y;
  833. if (backBottomRight.Y > maxY)
  834. maxY = backBottomRight.Y;
  835. if (backBottomLeft.Y > maxY)
  836. maxY = backBottomLeft.Y;
  837. if (frontTopRight.Y < minY)
  838. minY = frontTopRight.Y;
  839. if (frontTopLeft.Y < minY)
  840. minY = frontTopLeft.Y;
  841. if (frontBottomRight.Y < minY)
  842. minY = frontBottomRight.Y;
  843. if (frontBottomLeft.Y < minY)
  844. minY = frontBottomLeft.Y;
  845. if (backTopRight.Y < minY)
  846. minY = backTopRight.Y;
  847. if (backTopLeft.Y < minY)
  848. minY = backTopLeft.Y;
  849. if (backBottomRight.Y < minY)
  850. minY = backBottomRight.Y;
  851. if (backBottomLeft.Y < minY)
  852. minY = backBottomLeft.Y;
  853. //
  854. if (frontTopRight.Z > maxZ)
  855. maxZ = frontTopRight.Z;
  856. if (frontTopLeft.Z > maxZ)
  857. maxZ = frontTopLeft.Z;
  858. if (frontBottomRight.Z > maxZ)
  859. maxZ = frontBottomRight.Z;
  860. if (frontBottomLeft.Z > maxZ)
  861. maxZ = frontBottomLeft.Z;
  862. if (backTopRight.Z > maxZ)
  863. maxZ = backTopRight.Z;
  864. if (backTopLeft.Z > maxZ)
  865. maxZ = backTopLeft.Z;
  866. if (backBottomRight.Z > maxZ)
  867. maxZ = backBottomRight.Z;
  868. if (backBottomLeft.Z > maxZ)
  869. maxZ = backBottomLeft.Z;
  870. if (frontTopRight.Z < minZ)
  871. minZ = frontTopRight.Z;
  872. if (frontTopLeft.Z < minZ)
  873. minZ = frontTopLeft.Z;
  874. if (frontBottomRight.Z < minZ)
  875. minZ = frontBottomRight.Z;
  876. if (frontBottomLeft.Z < minZ)
  877. minZ = frontBottomLeft.Z;
  878. if (backTopRight.Z < minZ)
  879. minZ = backTopRight.Z;
  880. if (backTopLeft.Z < minZ)
  881. minZ = backTopLeft.Z;
  882. if (backBottomRight.Z < minZ)
  883. minZ = backBottomRight.Z;
  884. if (backBottomLeft.Z < minZ)
  885. minZ = backBottomLeft.Z;
  886. }
  887. }
  888. public Vector3 GetAxisAlignedBoundingBox(out float offsetHeight)
  889. {
  890. float minX;
  891. float maxX;
  892. float minY;
  893. float maxY;
  894. float minZ;
  895. float maxZ;
  896. GetAxisAlignedBoundingBoxRaw(out minX, out maxX, out minY, out maxY, out minZ, out maxZ);
  897. Vector3 boundingBox = new Vector3(maxX - minX, maxY - minY, maxZ - minZ);
  898. offsetHeight = 0;
  899. float lower = (minZ * -1);
  900. if (lower > maxZ)
  901. {
  902. offsetHeight = lower - (boundingBox.Z / 2);
  903. }
  904. else if (maxZ > lower)
  905. {
  906. offsetHeight = maxZ - (boundingBox.Z / 2);
  907. offsetHeight *= -1;
  908. }
  909. // m_log.InfoFormat("BoundingBox is {0} , {1} , {2} ", boundingBox.X, boundingBox.Y, boundingBox.Z);
  910. return boundingBox;
  911. }
  912. #endregion
  913. public void SaveScriptedState(XmlTextWriter writer)
  914. {
  915. XmlDocument doc = new XmlDocument();
  916. Dictionary<UUID,string> states = new Dictionary<UUID,string>();
  917. SceneObjectPart[] parts = m_parts.GetArray();
  918. for (int i = 0; i < parts.Length; i++)
  919. {
  920. Dictionary<UUID, string> pstates = parts[i].Inventory.GetScriptStates();
  921. foreach (KeyValuePair<UUID, string> kvp in pstates)
  922. states.Add(kvp.Key, kvp.Value);
  923. }
  924. if (states.Count > 0)
  925. {
  926. // Now generate the necessary XML wrappings
  927. writer.WriteStartElement(String.Empty, "GroupScriptStates", String.Empty);
  928. foreach (UUID itemid in states.Keys)
  929. {
  930. doc.LoadXml(states[itemid]);
  931. writer.WriteStartElement(String.Empty, "SavedScriptState", String.Empty);
  932. writer.WriteAttributeString(String.Empty, "UUID", String.Empty, itemid.ToString());
  933. writer.WriteRaw(doc.DocumentElement.OuterXml); // Writes ScriptState element
  934. writer.WriteEndElement(); // End of SavedScriptState
  935. }
  936. writer.WriteEndElement(); // End of GroupScriptStates
  937. }
  938. }
  939. /// <summary>
  940. ///
  941. /// </summary>
  942. /// <param name="part"></param>
  943. private void SetPartAsNonRoot(SceneObjectPart part)
  944. {
  945. part.ParentID = m_rootPart.LocalId;
  946. part.ClearUndoState();
  947. }
  948. public ushort GetTimeDilation()
  949. {
  950. return Utils.FloatToUInt16(m_scene.TimeDilation, 0.0f, 1.0f);
  951. }
  952. /// <summary>
  953. /// Set a part to act as the root part for this scene object
  954. /// </summary>
  955. /// <param name="part"></param>
  956. public void SetRootPart(SceneObjectPart part)
  957. {
  958. if (part == null)
  959. throw new ArgumentNullException("Cannot give SceneObjectGroup a null root SceneObjectPart");
  960. part.SetParent(this);
  961. m_rootPart = part;
  962. if (!IsAttachment)
  963. part.ParentID = 0;
  964. part.LinkNum = 0;
  965. m_parts.Add(m_rootPart.UUID, m_rootPart);
  966. }
  967. /// <summary>
  968. /// Add a new part to this scene object. The part must already be correctly configured.
  969. /// </summary>
  970. /// <param name="part"></param>
  971. public void AddPart(SceneObjectPart part)
  972. {
  973. part.SetParent(this);
  974. part.LinkNum = m_parts.Add(part.UUID, part);
  975. if (part.LinkNum == 2)
  976. RootPart.LinkNum = 1;
  977. }
  978. /// <summary>
  979. /// Make sure that every non root part has the proper parent root part local id
  980. /// </summary>
  981. private void UpdateParentIDs()
  982. {
  983. SceneObjectPart[] parts = m_parts.GetArray();
  984. for (int i = 0; i < parts.Length; i++)
  985. {
  986. SceneObjectPart part = parts[i];
  987. if (part.UUID != m_rootPart.UUID)
  988. part.ParentID = m_rootPart.LocalId;
  989. }
  990. }
  991. public void RegenerateFullIDs()
  992. {
  993. SceneObjectPart[] parts = m_parts.GetArray();
  994. for (int i = 0; i < parts.Length; i++)
  995. parts[i].UUID = UUID.Random();
  996. }
  997. // justincc: I don't believe this hack is needed any longer, especially since the physics
  998. // parts of set AbsolutePosition were already commented out. By changing HasGroupChanged to false
  999. // this method was preventing proper reload of scene objects.
  1000. // dahlia: I had to uncomment it, without it meshing was failing on some prims and objects
  1001. // at region startup
  1002. // teravus: After this was removed from the linking algorithm, Linked prims no longer collided
  1003. // properly when non-physical if they havn't been moved. This breaks ALL builds.
  1004. // see: http://opensimulator.org/mantis/view.php?id=3108
  1005. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  1006. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  1007. // unmoved prims! As soon as you move a Prim/group, it will collide properly because Absolute
  1008. // Position has been set!
  1009. public void ResetChildPrimPhysicsPositions()
  1010. {
  1011. // Setting this SOG's absolute position also loops through and sets the positions
  1012. // of the SOP's in this SOG's linkset. This has the side affect of making sure
  1013. // the physics world matches the simulated world.
  1014. AbsolutePosition = AbsolutePosition; // could someone in the know please explain how this works?
  1015. // teravus: AbsolutePosition is NOT a normal property!
  1016. // the code in the getter of AbsolutePosition is significantly different then the code in the setter!
  1017. // jhurliman: Then why is it a property instead of two methods?
  1018. }
  1019. public UUID GetPartsFullID(uint localID)
  1020. {
  1021. SceneObjectPart part = GetPart(localID);
  1022. if (part != null)
  1023. {
  1024. return part.UUID;
  1025. }
  1026. return UUID.Zero;
  1027. }
  1028. public void ObjectGrabHandler(uint localId, Vector3 offsetPos, IClientAPI remoteClient)
  1029. {
  1030. if (m_rootPart.LocalId == localId)
  1031. {
  1032. OnGrabGroup(offsetPos, remoteClient);
  1033. }
  1034. else
  1035. {
  1036. SceneObjectPart part = GetPart(localId);
  1037. OnGrabPart(part, offsetPos, remoteClient);
  1038. }
  1039. }
  1040. public virtual void OnGrabPart(SceneObjectPart part, Vector3 offsetPos, IClientAPI remoteClient)
  1041. {
  1042. // m_log.DebugFormat(
  1043. // "[SCENE OBJECT GROUP]: Processing OnGrabPart for {0} on {1} {2}, offsetPos {3}",
  1044. // remoteClient.Name, part.Name, part.LocalId, offsetPos);
  1045. part.StoreUndoState();
  1046. part.OnGrab(offsetPos, remoteClient);
  1047. }
  1048. public virtual void OnGrabGroup(Vector3 offsetPos, IClientAPI remoteClient)
  1049. {
  1050. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  1051. }
  1052. /// <summary>
  1053. /// Delete this group from its scene.
  1054. /// </summary>
  1055. ///
  1056. /// This only handles the in-world consequences of deletion (e.g. any avatars sitting on it are forcibly stood
  1057. /// up and all avatars receive notification of its removal. Removal of the scene object from database backup
  1058. /// must be handled by the caller.
  1059. ///
  1060. /// <param name="silent">If true then deletion is not broadcast to clients</param>
  1061. public void DeleteGroupFromScene(bool silent)
  1062. {
  1063. SceneObjectPart[] parts = m_parts.GetArray();
  1064. for (int i = 0; i < parts.Length; i++)
  1065. {
  1066. SceneObjectPart part = parts[i];
  1067. Scene.ForEachRootScenePresence(delegate(ScenePresence avatar)
  1068. {
  1069. if (avatar.ParentID == LocalId)
  1070. avatar.StandUp();
  1071. if (!silent)
  1072. {
  1073. part.ClearUpdateSchedule();
  1074. if (part == m_rootPart)
  1075. {
  1076. if (!IsAttachment
  1077. || AttachedAvatar == avatar.ControllingClient.AgentId
  1078. || !HasPrivateAttachmentPoint)
  1079. avatar.ControllingClient.SendKillObject(m_regionHandle, new List<uint> { part.LocalId });
  1080. }
  1081. }
  1082. });
  1083. }
  1084. }
  1085. public void AddScriptLPS(int count)
  1086. {
  1087. m_scene.SceneGraph.AddToScriptLPS(count);
  1088. }
  1089. public void AddActiveScriptCount(int count)
  1090. {
  1091. SceneGraph d = m_scene.SceneGraph;
  1092. d.AddActiveScripts(count);
  1093. }
  1094. public void aggregateScriptEvents()
  1095. {
  1096. PrimFlags objectflagupdate = (PrimFlags)RootPart.GetEffectiveObjectFlags();
  1097. scriptEvents aggregateScriptEvents = 0;
  1098. SceneObjectPart[] parts = m_parts.GetArray();
  1099. for (int i = 0; i < parts.Length; i++)
  1100. {
  1101. SceneObjectPart part = parts[i];
  1102. if (part == null)
  1103. continue;
  1104. if (part != RootPart)
  1105. part.Flags = objectflagupdate;
  1106. aggregateScriptEvents |= part.AggregateScriptEvents;
  1107. }
  1108. m_scriptListens_atTarget = ((aggregateScriptEvents & scriptEvents.at_target) != 0);
  1109. m_scriptListens_notAtTarget = ((aggregateScriptEvents & scriptEvents.not_at_target) != 0);
  1110. if (!m_scriptListens_atTarget && !m_scriptListens_notAtTarget)
  1111. {
  1112. lock (m_targets)
  1113. m_targets.Clear();
  1114. m_scene.RemoveGroupTarget(this);
  1115. }
  1116. m_scriptListens_atRotTarget = ((aggregateScriptEvents & scriptEvents.at_rot_target) != 0);
  1117. m_scriptListens_notAtRotTarget = ((aggregateScriptEvents & scriptEvents.not_at_rot_target) != 0);
  1118. if (!m_scriptListens_atRotTarget && !m_scriptListens_notAtRotTarget)
  1119. {
  1120. lock (m_rotTargets)
  1121. m_rotTargets.Clear();
  1122. m_scene.RemoveGroupTarget(this);
  1123. }
  1124. ScheduleGroupForFullUpdate();
  1125. }
  1126. public void SetText(string text, Vector3 color, double alpha)
  1127. {
  1128. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  1129. (int) (color.X * 0xff),
  1130. (int) (color.Y * 0xff),
  1131. (int) (color.Z * 0xff));
  1132. Text = text;
  1133. HasGroupChanged = true;
  1134. m_rootPart.ScheduleFullUpdate();
  1135. }
  1136. /// <summary>
  1137. /// Apply physics to this group
  1138. /// </summary>
  1139. public void ApplyPhysics()
  1140. {
  1141. // Apply physics to the root prim
  1142. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive);
  1143. // Apply physics to child prims
  1144. SceneObjectPart[] parts = m_parts.GetArray();
  1145. if (parts.Length > 1)
  1146. {
  1147. for (int i = 0; i < parts.Length; i++)
  1148. {
  1149. SceneObjectPart part = parts[i];
  1150. if (part.LocalId != m_rootPart.LocalId)
  1151. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), part.VolumeDetectActive);
  1152. }
  1153. // Hack to get the physics scene geometries in the right spot
  1154. ResetChildPrimPhysicsPositions();
  1155. }
  1156. }
  1157. public void SetOwnerId(UUID userId)
  1158. {
  1159. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  1160. }
  1161. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  1162. {
  1163. SceneObjectPart[] parts = m_parts.GetArray();
  1164. for (int i = 0; i < parts.Length; i++)
  1165. whatToDo(parts[i]);
  1166. }
  1167. #region Events
  1168. /// <summary>
  1169. /// Processes backup.
  1170. /// </summary>
  1171. /// <param name="datastore"></param>
  1172. public virtual void ProcessBackup(ISimulationDataService datastore, bool forcedBackup)
  1173. {
  1174. if (!m_isBackedUp)
  1175. {
  1176. // m_log.DebugFormat(
  1177. // "[WATER WARS]: Ignoring backup of {0} {1} since object is not marked to be backed up", Name, UUID);
  1178. return;
  1179. }
  1180. if (IsDeleted || UUID == UUID.Zero)
  1181. {
  1182. // m_log.DebugFormat(
  1183. // "[WATER WARS]: Ignoring backup of {0} {1} since object is marked as already deleted", Name, UUID);
  1184. return;
  1185. }
  1186. // Since this is the top of the section of call stack for backing up a particular scene object, don't let
  1187. // any exception propogate upwards.
  1188. try
  1189. {
  1190. if (!m_scene.ShuttingDown) // if shutting down then there will be nothing to handle the return so leave till next restart
  1191. {
  1192. ILandObject parcel = m_scene.LandChannel.GetLandObject(
  1193. m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y);
  1194. if (parcel != null && parcel.LandData != null &&
  1195. parcel.LandData.OtherCleanTime != 0)
  1196. {
  1197. if (parcel.LandData.OwnerID != OwnerID &&
  1198. (parcel.LandData.GroupID != GroupID ||
  1199. parcel.LandData.GroupID == UUID.Zero))
  1200. {
  1201. if ((DateTime.UtcNow - RootPart.Rezzed).TotalMinutes >
  1202. parcel.LandData.OtherCleanTime)
  1203. {
  1204. DetachFromBackup();
  1205. m_log.DebugFormat(
  1206. "[SCENE OBJECT GROUP]: Returning object {0} due to parcel autoreturn",
  1207. RootPart.UUID);
  1208. m_scene.AddReturn(OwnerID == GroupID ? LastOwnerID : OwnerID, Name, AbsolutePosition, "parcel autoreturn");
  1209. m_scene.DeRezObjects(null, new List<uint>() { RootPart.LocalId }, UUID.Zero,
  1210. DeRezAction.Return, UUID.Zero);
  1211. return;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. if (m_scene.UseBackup && HasGroupChanged)
  1217. {
  1218. // don't backup while it's selected or you're asking for changes mid stream.
  1219. if (isTimeToPersist() || forcedBackup)
  1220. {
  1221. // m_log.DebugFormat(
  1222. // "[SCENE]: Storing {0}, {1} in {2}",
  1223. // Name, UUID, m_scene.RegionInfo.RegionName);
  1224. SceneObjectGroup backup_group = Copy(false);
  1225. backup_group.RootPart.Velocity = RootPart.Velocity;
  1226. backup_group.RootPart.Acceleration = RootPart.Acceleration;
  1227. backup_group.RootPart.AngularVelocity = RootPart.AngularVelocity;
  1228. backup_group.RootPart.ParticleSystem = RootPart.ParticleSystem;
  1229. HasGroupChanged = false;
  1230. HasGroupChangedDueToDelink = false;
  1231. m_scene.EventManager.TriggerOnSceneObjectPreSave(backup_group, this);
  1232. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  1233. backup_group.ForEachPart(delegate(SceneObjectPart part)
  1234. {
  1235. part.Inventory.ProcessInventoryBackup(datastore);
  1236. });
  1237. backup_group = null;
  1238. }
  1239. // else
  1240. // {
  1241. // m_log.DebugFormat(
  1242. // "[SCENE]: Did not update persistence of object {0} {1}, selected = {2}",
  1243. // Name, UUID, IsSelected);
  1244. // }
  1245. }
  1246. }
  1247. catch (Exception e)
  1248. {
  1249. m_log.ErrorFormat(
  1250. "[SCENE]: Storing of {0}, {1} in {2} failed with exception {3}{4}",
  1251. Name, UUID, m_scene.RegionInfo.RegionName, e.Message, e.StackTrace);
  1252. }
  1253. }
  1254. #endregion
  1255. /// <summary>
  1256. /// Send the parts of this SOG to a single client
  1257. /// </summary>
  1258. /// <remarks>
  1259. /// Used when the client initially connects and when client sends RequestPrim packet
  1260. /// </remarks>
  1261. /// <param name="remoteClient"></param>
  1262. public void SendFullUpdateToClient(IClientAPI remoteClient)
  1263. {
  1264. RootPart.SendFullUpdate(remoteClient);
  1265. SceneObjectPart[] parts = m_parts.GetArray();
  1266. for (int i = 0; i < parts.Length; i++)
  1267. {
  1268. SceneObjectPart part = parts[i];
  1269. if (part != RootPart)
  1270. part.SendFullUpdate(remoteClient);
  1271. }
  1272. }
  1273. #region Copying
  1274. /// <summary>
  1275. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1276. /// </summary>
  1277. /// <param name="userExposed">True if the duplicate will immediately be in the scene, false otherwise</param>
  1278. /// <returns></returns>
  1279. public SceneObjectGroup Copy(bool userExposed)
  1280. {
  1281. SceneObjectGroup dupe = (SceneObjectGroup)MemberwiseClone();
  1282. dupe.m_isBackedUp = false;
  1283. dupe.m_parts = new MapAndArray<OpenMetaverse.UUID, SceneObjectPart>();
  1284. // Warning, The following code related to previousAttachmentStatus is needed so that clones of
  1285. // attachments do not bordercross while they're being duplicated. This is hacktastic!
  1286. // Normally, setting AbsolutePosition will bordercross a prim if it's outside the region!
  1287. // unless IsAttachment is true!, so to prevent border crossing, we save it's attachment state
  1288. // (which should be false anyway) set it as an Attachment and then set it's Absolute Position,
  1289. // then restore it's attachment state
  1290. // This is only necessary when userExposed is false!
  1291. bool previousAttachmentStatus = dupe.IsAttachment;
  1292. if (!userExposed)
  1293. dupe.IsAttachment = true;
  1294. dupe.AbsolutePosition = new Vector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  1295. if (!userExposed)
  1296. {
  1297. dupe.IsAttachment = previousAttachmentStatus;
  1298. }
  1299. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  1300. dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
  1301. if (userExposed)
  1302. dupe.m_rootPart.TrimPermissions();
  1303. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.GetArray());
  1304. partList.Sort(delegate(SceneObjectPart p1, SceneObjectPart p2)
  1305. {
  1306. return p1.LinkNum.CompareTo(p2.LinkNum);
  1307. }
  1308. );
  1309. foreach (SceneObjectPart part in partList)
  1310. {
  1311. SceneObjectPart newPart;
  1312. if (part.UUID != m_rootPart.UUID)
  1313. {
  1314. newPart = dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  1315. newPart.LinkNum = part.LinkNum;
  1316. }
  1317. else
  1318. {
  1319. newPart = dupe.m_rootPart;
  1320. }
  1321. // Need to duplicate the physics actor as well
  1322. PhysicsActor originalPartPa = part.PhysActor;
  1323. if (originalPartPa != null && userExposed)
  1324. {
  1325. PrimitiveBaseShape pbs = newPart.Shape;
  1326. newPart.PhysActor
  1327. = m_scene.PhysicsScene.AddPrimShape(
  1328. string.Format("{0}/{1}", newPart.Name, newPart.UUID),
  1329. pbs,
  1330. newPart.AbsolutePosition,
  1331. newPart.Scale,
  1332. newPart.RotationOffset,
  1333. originalPartPa.IsPhysical,
  1334. newPart.LocalId);
  1335. newPart.DoPhysicsPropertyUpdate(originalPartPa.IsPhysical, true);
  1336. }
  1337. }
  1338. if (userExposed)
  1339. {
  1340. dupe.UpdateParentIDs();
  1341. dupe.HasGroupChanged = true;
  1342. dupe.AttachToBackup();
  1343. ScheduleGroupForFullUpdate();
  1344. }
  1345. return dupe;
  1346. }
  1347. /// <summary>
  1348. /// Copy the given part as the root part of this scene object.
  1349. /// </summary>
  1350. /// <param name="part"></param>
  1351. /// <param name="cAgentID"></param>
  1352. /// <param name="cGroupID"></param>
  1353. public void CopyRootPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1354. {
  1355. SetRootPart(part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, 0, userExposed));
  1356. }
  1357. public void ScriptSetPhysicsStatus(bool usePhysics)
  1358. {
  1359. UpdatePrimFlags(RootPart.LocalId, usePhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  1360. }
  1361. public void ScriptSetTemporaryStatus(bool makeTemporary)
  1362. {
  1363. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, makeTemporary, IsPhantom, IsVolumeDetect);
  1364. }
  1365. public void ScriptSetPhantomStatus(bool makePhantom)
  1366. {
  1367. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, IsTemporary, makePhantom, IsVolumeDetect);
  1368. }
  1369. public void ScriptSetVolumeDetect(bool makeVolumeDetect)
  1370. {
  1371. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, IsTemporary, IsPhantom, makeVolumeDetect);
  1372. /*
  1373. ScriptSetPhantomStatus(false); // What ever it was before, now it's not phantom anymore
  1374. if (PhysActor != null) // Should always be the case now
  1375. {
  1376. PhysActor.SetVolumeDetect(param);
  1377. }
  1378. if (param != 0)
  1379. AddFlag(PrimFlags.Phantom);
  1380. ScheduleFullUpdate();
  1381. */
  1382. }
  1383. public void applyImpulse(Vector3 impulse)
  1384. {
  1385. if (IsAttachment)
  1386. {
  1387. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1388. if (avatar != null)
  1389. {
  1390. avatar.PushForce(impulse);
  1391. }
  1392. }
  1393. else
  1394. {
  1395. PhysicsActor pa = RootPart.PhysActor;
  1396. if (pa != null)
  1397. {
  1398. pa.AddForce(impulse, true);
  1399. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1400. }
  1401. }
  1402. }
  1403. public void applyAngularImpulse(Vector3 impulse)
  1404. {
  1405. PhysicsActor pa = RootPart.PhysActor;
  1406. if (pa != null)
  1407. {
  1408. if (!IsAttachment)
  1409. {
  1410. pa.AddAngularForce(impulse, true);
  1411. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1412. }
  1413. }
  1414. }
  1415. public void setAngularImpulse(Vector3 impulse)
  1416. {
  1417. PhysicsActor pa = RootPart.PhysActor;
  1418. if (pa != null)
  1419. {
  1420. if (!IsAttachment)
  1421. {
  1422. pa.Torque = impulse;
  1423. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1424. }
  1425. }
  1426. }
  1427. public Vector3 GetTorque()
  1428. {
  1429. PhysicsActor pa = RootPart.PhysActor;
  1430. if (pa != null)
  1431. {
  1432. if (!IsAttachment)
  1433. {
  1434. Vector3 torque = pa.Torque;
  1435. return torque;
  1436. }
  1437. }
  1438. return Vector3.Zero;
  1439. }
  1440. public void moveToTarget(Vector3 target, float tau)
  1441. {
  1442. if (IsAttachment)
  1443. {
  1444. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1445. if (avatar != null)
  1446. {
  1447. avatar.MoveToTarget(target, false, false);
  1448. }
  1449. }
  1450. else
  1451. {
  1452. PhysicsActor pa = RootPart.PhysActor;
  1453. if (pa != null)
  1454. {
  1455. pa.PIDTarget = target;
  1456. pa.PIDTau = tau;
  1457. pa.PIDActive = true;
  1458. }
  1459. }
  1460. }
  1461. public void stopMoveToTarget()
  1462. {
  1463. PhysicsActor pa = RootPart.PhysActor;
  1464. if (pa != null)
  1465. pa.PIDActive = false;
  1466. }
  1467. /// <summary>
  1468. /// Uses a PID to attempt to clamp the object on the Z axis at the given height over tau seconds.
  1469. /// </summary>
  1470. /// <param name="height">Height to hover. Height of zero disables hover.</param>
  1471. /// <param name="hoverType">Determines what the height is relative to </param>
  1472. /// <param name="tau">Number of seconds over which to reach target</param>
  1473. public void SetHoverHeight(float height, PIDHoverType hoverType, float tau)
  1474. {
  1475. PhysicsActor pa = RootPart.PhysActor;
  1476. if (pa != null)
  1477. {
  1478. if (height != 0f)
  1479. {
  1480. pa.PIDHoverHeight = height;
  1481. pa.PIDHoverType = hoverType;
  1482. pa.PIDTau = tau;
  1483. pa.PIDHoverActive = true;
  1484. }
  1485. else
  1486. {
  1487. pa.PIDHoverActive = false;
  1488. }
  1489. }
  1490. }
  1491. /// <summary>
  1492. /// Set the owner of the root part.
  1493. /// </summary>
  1494. /// <param name="part"></param>
  1495. /// <param name="cAgentID"></param>
  1496. /// <param name="cGroupID"></param>
  1497. public void SetRootPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1498. {
  1499. part.LastOwnerID = part.OwnerID;
  1500. part.OwnerID = cAgentID;
  1501. part.GroupID = cGroupID;
  1502. if (part.OwnerID != cAgentID)
  1503. {
  1504. // Apply Next Owner Permissions if we're not bypassing permissions
  1505. if (!m_scene.Permissions.BypassPermissions())
  1506. ApplyNextOwnerPermissions();
  1507. }
  1508. part.ScheduleFullUpdate();
  1509. }
  1510. /// <summary>
  1511. /// Make a copy of the given part.
  1512. /// </summary>
  1513. /// <param name="part"></param>
  1514. /// <param name="cAgentID"></param>
  1515. /// <param name="cGroupID"></param>
  1516. public SceneObjectPart CopyPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1517. {
  1518. SceneObjectPart newPart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed);
  1519. AddPart(newPart);
  1520. SetPartAsNonRoot(newPart);
  1521. return newPart;
  1522. }
  1523. /// <summary>
  1524. /// Reset the UUIDs for all the prims that make up this group.
  1525. /// </summary>
  1526. /// <remarks>
  1527. /// This is called by methods which want to add a new group to an existing scene, in order
  1528. /// to ensure that there are no clashes with groups already present.
  1529. /// </remarks>
  1530. public void ResetIDs()
  1531. {
  1532. lock (m_parts.SyncRoot)
  1533. {
  1534. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.GetArray());
  1535. m_parts.Clear();
  1536. foreach (SceneObjectPart part in partsList)
  1537. {
  1538. part.ResetIDs(part.LinkNum); // Don't change link nums
  1539. m_parts.Add(part.UUID, part);
  1540. }
  1541. }
  1542. }
  1543. /// <summary>
  1544. ///
  1545. /// </summary>
  1546. /// <param name="part"></param>
  1547. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, UUID AgentID, uint RequestFlags)
  1548. {
  1549. remoteClient.SendObjectPropertiesFamilyData(RootPart, RequestFlags);
  1550. // remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.OwnerID, RootPart.GroupID, RootPart.BaseMask,
  1551. // RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  1552. // RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  1553. // RootPart.CreatorID, RootPart.Name, RootPart.Description);
  1554. }
  1555. public void SetPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1556. {
  1557. part.OwnerID = cAgentID;
  1558. part.GroupID = cGroupID;
  1559. }
  1560. #endregion
  1561. public override void Update()
  1562. {
  1563. // Check that the group was not deleted before the scheduled update
  1564. // FIXME: This is merely a temporary measure to reduce the incidence of failure when
  1565. // an object has been deleted from a scene before update was processed.
  1566. // A more fundamental overhaul of the update mechanism is required to eliminate all
  1567. // the race conditions.
  1568. if (IsDeleted)
  1569. return;
  1570. // Even temporary objects take part in physics (e.g. temp-on-rez bullets)
  1571. //if ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)
  1572. // return;
  1573. // If we somehow got here to updating the SOG and its root part is not scheduled for update,
  1574. // check to see if the physical position or rotation warrant an update.
  1575. if (m_rootPart.UpdateFlag == UpdateRequired.NONE)
  1576. {
  1577. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1578. if (UsePhysics && !AbsolutePosition.ApproxEquals(lastPhysGroupPos, 0.02f))
  1579. {
  1580. m_rootPart.UpdateFlag = UpdateRequired.TERSE;
  1581. lastPhysGroupPos = AbsolutePosition;
  1582. }
  1583. if (UsePhysics && !GroupRotation.ApproxEquals(lastPhysGroupRot, 0.1f))
  1584. {
  1585. m_rootPart.UpdateFlag = UpdateRequired.TERSE;
  1586. lastPhysGroupRot = GroupRotation;
  1587. }
  1588. }
  1589. SceneObjectPart[] parts = m_parts.GetArray();
  1590. for (int i = 0; i < parts.Length; i++)
  1591. {
  1592. SceneObjectPart part = parts[i];
  1593. if (!IsSelected)
  1594. part.UpdateLookAt();
  1595. part.SendScheduledUpdates();
  1596. }
  1597. }
  1598. /// <summary>
  1599. /// Schedule a full update for this scene object to all interested viewers.
  1600. /// </summary>
  1601. /// <remarks>
  1602. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  1603. /// so that either all changes are sent at once. However, currently, a large amount of internal
  1604. /// code will set this anyway when some object properties are changed.
  1605. /// </remarks>
  1606. public void ScheduleGroupForFullUpdate()
  1607. {
  1608. // if (IsAttachment)
  1609. // m_log.DebugFormat("[SOG]: Scheduling full update for {0} {1}", Name, LocalId);
  1610. checkAtTargets();
  1611. RootPart.ScheduleFullUpdate();
  1612. SceneObjectPart[] parts = m_parts.GetArray();
  1613. for (int i = 0; i < parts.Length; i++)
  1614. {
  1615. SceneObjectPart part = parts[i];
  1616. if (part != RootPart)
  1617. part.ScheduleFullUpdate();
  1618. }
  1619. }
  1620. /// <summary>
  1621. /// Schedule a terse update for this scene object to all interested viewers.
  1622. /// </summary>
  1623. /// <remarks>
  1624. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  1625. /// so that either all changes are sent at once. However, currently, a large amount of internal
  1626. /// code will set this anyway when some object properties are changed.
  1627. /// </remarks>
  1628. public void ScheduleGroupForTerseUpdate()
  1629. {
  1630. // m_log.DebugFormat("[SOG]: Scheduling terse update for {0} {1}", Name, UUID);
  1631. SceneObjectPart[] parts = m_parts.GetArray();
  1632. for (int i = 0; i < parts.Length; i++)
  1633. parts[i].ScheduleTerseUpdate();
  1634. }
  1635. /// <summary>
  1636. /// Immediately send a full update for this scene object.
  1637. /// </summary>
  1638. public void SendGroupFullUpdate()
  1639. {
  1640. if (IsDeleted)
  1641. return;
  1642. // m_log.DebugFormat("[SOG]: Sending immediate full group update for {0} {1}", Name, UUID);
  1643. RootPart.SendFullUpdateToAllClients();
  1644. SceneObjectPart[] parts = m_parts.GetArray();
  1645. for (int i = 0; i < parts.Length; i++)
  1646. {
  1647. SceneObjectPart part = parts[i];
  1648. if (part != RootPart)
  1649. part.SendFullUpdateToAllClients();
  1650. }
  1651. }
  1652. /// <summary>
  1653. /// Immediately send an update for this scene object's root prim only.
  1654. /// This is for updates regarding the object as a whole, and none of its parts in particular.
  1655. /// Note: this may not be used by opensim (it probably should) but it's used by
  1656. /// external modules.
  1657. /// </summary>
  1658. public void SendGroupRootTerseUpdate()
  1659. {
  1660. if (IsDeleted)
  1661. return;
  1662. RootPart.SendTerseUpdateToAllClients();
  1663. }
  1664. public void QueueForUpdateCheck()
  1665. {
  1666. if (m_scene == null) // Need to check here as it's null during object creation
  1667. return;
  1668. m_scene.SceneGraph.AddToUpdateList(this);
  1669. }
  1670. /// <summary>
  1671. /// Immediately send a terse update for this scene object.
  1672. /// </summary>
  1673. public void SendGroupTerseUpdate()
  1674. {
  1675. if (IsDeleted)
  1676. return;
  1677. SceneObjectPart[] parts = m_parts.GetArray();
  1678. for (int i = 0; i < parts.Length; i++)
  1679. parts[i].SendTerseUpdateToAllClients();
  1680. }
  1681. /// <summary>
  1682. /// Send metadata about the root prim (name, description, sale price, permissions, etc.) to a client.
  1683. /// </summary>
  1684. /// <param name="client"></param>
  1685. public void SendPropertiesToClient(IClientAPI client)
  1686. {
  1687. m_rootPart.SendPropertiesToClient(client);
  1688. }
  1689. #region SceneGroupPart Methods
  1690. /// <summary>
  1691. /// Get the child part by LinkNum
  1692. /// </summary>
  1693. /// <param name="linknum"></param>
  1694. /// <returns>null if no child part with that linknum or child part</returns>
  1695. public SceneObjectPart GetLinkNumPart(int linknum)
  1696. {
  1697. SceneObjectPart[] parts = m_parts.GetArray();
  1698. for (int i = 0; i < parts.Length; i++)
  1699. {
  1700. if (parts[i].LinkNum == linknum)
  1701. return parts[i];
  1702. }
  1703. return null;
  1704. }
  1705. /// <summary>
  1706. /// Get a part with a given UUID
  1707. /// </summary>
  1708. /// <param name="primID"></param>
  1709. /// <returns>null if a part with the primID was not found</returns>
  1710. public SceneObjectPart GetPart(UUID primID)
  1711. {
  1712. SceneObjectPart childPart;
  1713. m_parts.TryGetValue(primID, out childPart);
  1714. return childPart;
  1715. }
  1716. /// <summary>
  1717. /// Get a part with a given local ID
  1718. /// </summary>
  1719. /// <param name="localID"></param>
  1720. /// <returns>null if a part with the local ID was not found</returns>
  1721. public SceneObjectPart GetPart(uint localID)
  1722. {
  1723. SceneObjectPart[] parts = m_parts.GetArray();
  1724. for (int i = 0; i < parts.Length; i++)
  1725. {
  1726. if (parts[i].LocalId == localID)
  1727. return parts[i];
  1728. }
  1729. return null;
  1730. }
  1731. #endregion
  1732. #region Packet Handlers
  1733. /// <summary>
  1734. /// Link the prims in a given group to this group
  1735. /// </summary>
  1736. /// <remarks>
  1737. /// Do not call this method directly - use Scene.LinkObjects() instead to avoid races between threads.
  1738. /// FIXME: There are places where scripts call these methods directly without locking. This is a potential race condition.
  1739. /// </remarks>
  1740. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1741. public void LinkToGroup(SceneObjectGroup objectGroup)
  1742. {
  1743. LinkToGroup(objectGroup, false);
  1744. }
  1745. // Link an existing group to this group.
  1746. // The group being linked need not be a linkset -- it can have just one prim.
  1747. public void LinkToGroup(SceneObjectGroup objectGroup, bool insert)
  1748. {
  1749. // m_log.DebugFormat(
  1750. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1751. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1752. // Linking to ourselves is not a valid operation.
  1753. if (objectGroup == this)
  1754. return;
  1755. // If the configured linkset capacity is greater than zero,
  1756. // and the new linkset would have a prim count higher than this
  1757. // value, do not link it.
  1758. if (m_scene.m_linksetCapacity > 0 &&
  1759. (PrimCount + objectGroup.PrimCount) >
  1760. m_scene.m_linksetCapacity)
  1761. {
  1762. m_log.DebugFormat(
  1763. "[SCENE OBJECT GROUP]: Cannot link group with root" +
  1764. " part {0}, {1} ({2} prims) to group with root part" +
  1765. " {3}, {4} ({5} prims) because the new linkset" +
  1766. " would exceed the configured maximum of {6}",
  1767. objectGroup.RootPart.Name, objectGroup.RootPart.UUID,
  1768. objectGroup.PrimCount, RootPart.Name, RootPart.UUID,
  1769. PrimCount, m_scene.m_linksetCapacity);
  1770. return;
  1771. }
  1772. // 'linkPart' == the root of the group being linked into this group
  1773. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1774. // physics flags from group to be applied to linked parts
  1775. bool grpusephys = UsesPhysics;
  1776. bool grptemporary = IsTemporary;
  1777. // Remember where the group being linked thought it was
  1778. Vector3 oldGroupPosition = linkPart.GroupPosition;
  1779. Quaternion oldRootRotation = linkPart.RotationOffset;
  1780. // A linked SOP remembers its location and rotation relative to the root of a group.
  1781. // Convert the root of the group being linked to be relative to the
  1782. // root of the group being linked to.
  1783. // Note: Some of the assignments have complex side effects.
  1784. // First move the new group's root SOP's position to be relative to ours
  1785. // (radams1: Not sure if the multiple setting of OffsetPosition is required. If not,
  1786. // this code can be reordered to have a more logical flow.)
  1787. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1788. // Assign the new parent to the root of the old group
  1789. linkPart.ParentID = m_rootPart.LocalId;
  1790. // Now that it's a child, it's group position is our root position
  1791. linkPart.GroupPosition = AbsolutePosition;
  1792. Vector3 axPos = linkPart.OffsetPosition;
  1793. // Rotate the linking root SOP's position to be relative to the new root prim
  1794. Quaternion parentRot = m_rootPart.RotationOffset;
  1795. axPos *= Quaternion.Inverse(parentRot);
  1796. linkPart.OffsetPosition = axPos;
  1797. // Make the linking root SOP's rotation relative to the new root prim
  1798. Quaternion oldRot = linkPart.RotationOffset;
  1799. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  1800. linkPart.RotationOffset = newRot;
  1801. // If there is only one SOP in a SOG, the LinkNum is zero. I.e., not a linkset.
  1802. // Now that we know this SOG has at least two SOPs in it, the new root
  1803. // SOP becomes the first in the linkset.
  1804. if (m_rootPart.LinkNum == 0)
  1805. m_rootPart.LinkNum = 1;
  1806. lock (m_parts.SyncRoot)
  1807. {
  1808. // Calculate the new link number for the old root SOP
  1809. int linkNum;
  1810. if (insert)
  1811. {
  1812. linkNum = 2;
  1813. foreach (SceneObjectPart part in Parts)
  1814. {
  1815. if (part.LinkNum > 1)
  1816. part.LinkNum++;
  1817. }
  1818. }
  1819. else
  1820. {
  1821. linkNum = PrimCount + 1;
  1822. }
  1823. // Add the old root SOP as a part in our group's list
  1824. m_parts.Add(linkPart.UUID, linkPart);
  1825. linkPart.SetParent(this);
  1826. linkPart.CreateSelected = true;
  1827. // let physics know preserve part volume dtc messy since UpdatePrimFlags doesn't look to parent changes for now
  1828. linkPart.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (linkPart.Flags & PrimFlags.Phantom) != 0), linkPart.VolumeDetectActive);
  1829. // If the added SOP is physical, also tell the physics engine about the link relationship.
  1830. if (linkPart.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  1831. {
  1832. linkPart.PhysActor.link(m_rootPart.PhysActor);
  1833. this.Scene.PhysicsScene.AddPhysicsActorTaint(linkPart.PhysActor);
  1834. }
  1835. linkPart.LinkNum = linkNum++;
  1836. // Get a list of the SOP's in the old group in order of their linknum's.
  1837. SceneObjectPart[] ogParts = objectGroup.Parts;
  1838. Array.Sort(ogParts, delegate(SceneObjectPart a, SceneObjectPart b)
  1839. {
  1840. return a.LinkNum - b.LinkNum;
  1841. });
  1842. // Add each of the SOP's from the old linkset to our linkset
  1843. for (int i = 0; i < ogParts.Length; i++)
  1844. {
  1845. SceneObjectPart part = ogParts[i];
  1846. if (part.UUID != objectGroup.m_rootPart.UUID)
  1847. {
  1848. LinkNonRootPart(part, oldGroupPosition, oldRootRotation, linkNum++);
  1849. // Update the physics flags for the newly added SOP
  1850. // (Is this necessary? LinkNonRootPart() has already called UpdatePrimFlags but with different flags!??)
  1851. part.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (part.Flags & PrimFlags.Phantom) != 0), part.VolumeDetectActive);
  1852. // If the added SOP is physical, also tell the physics engine about the link relationship.
  1853. if (part.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  1854. {
  1855. part.PhysActor.link(m_rootPart.PhysActor);
  1856. this.Scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1857. }
  1858. }
  1859. part.ClearUndoState();
  1860. }
  1861. }
  1862. // Now that we've aquired all of the old SOG's parts, remove the old SOG from the scene.
  1863. m_scene.UnlinkSceneObject(objectGroup, true);
  1864. objectGroup.IsDeleted = true;
  1865. objectGroup.m_parts.Clear();
  1866. // Can't do this yet since backup still makes use of the root part without any synchronization
  1867. // objectGroup.m_rootPart = null;
  1868. // If linking prims with different permissions, fix them
  1869. AdjustChildPrimPermissions();
  1870. AttachToBackup();
  1871. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  1872. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  1873. // unmoved prims!
  1874. ResetChildPrimPhysicsPositions();
  1875. //HasGroupChanged = true;
  1876. //ScheduleGroupForFullUpdate();
  1877. }
  1878. /// <summary>
  1879. /// Delink the given prim from this group. The delinked prim is established as
  1880. /// an independent SceneObjectGroup.
  1881. /// </summary>
  1882. /// <remarks>
  1883. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  1884. /// condition. But currently there is no
  1885. /// alternative method that does take a lonk to delink a single prim.
  1886. /// </remarks>
  1887. /// <param name="partID"></param>
  1888. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  1889. public SceneObjectGroup DelinkFromGroup(uint partID)
  1890. {
  1891. return DelinkFromGroup(partID, true);
  1892. }
  1893. /// <summary>
  1894. /// Delink the given prim from this group. The delinked prim is established as
  1895. /// an independent SceneObjectGroup.
  1896. /// </summary>
  1897. /// <remarks>
  1898. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  1899. /// condition. But currently there is no
  1900. /// alternative method that does take a lonk to delink a single prim.
  1901. /// </remarks>
  1902. /// <param name="partID"></param>
  1903. /// <param name="sendEvents"></param>
  1904. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  1905. public SceneObjectGroup DelinkFromGroup(uint partID, bool sendEvents)
  1906. {
  1907. SceneObjectPart linkPart = GetPart(partID);
  1908. if (linkPart != null)
  1909. {
  1910. return DelinkFromGroup(linkPart, sendEvents);
  1911. }
  1912. else
  1913. {
  1914. m_log.WarnFormat("[SCENE OBJECT GROUP]: " +
  1915. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  1916. partID, LocalId, UUID);
  1917. return null;
  1918. }
  1919. }
  1920. /// <summary>
  1921. /// Delink the given prim from this group. The delinked prim is established as
  1922. /// an independent SceneObjectGroup.
  1923. /// </summary>
  1924. /// <remarks>
  1925. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  1926. /// condition. But currently there is no
  1927. /// alternative method that does take a lock to delink a single prim.
  1928. /// </remarks>
  1929. /// <param name="partID"></param>
  1930. /// <param name="sendEvents"></param>
  1931. /// <returns>The object group of the newly delinked prim.</returns>
  1932. public SceneObjectGroup DelinkFromGroup(SceneObjectPart linkPart, bool sendEvents)
  1933. {
  1934. // m_log.DebugFormat(
  1935. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  1936. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  1937. linkPart.ClearUndoState();
  1938. Vector3 worldPos = linkPart.GetWorldPosition();
  1939. Quaternion worldRot = linkPart.GetWorldRotation();
  1940. // Remove the part from this object
  1941. lock (m_parts.SyncRoot)
  1942. {
  1943. m_parts.Remove(linkPart.UUID);
  1944. SceneObjectPart[] parts = m_parts.GetArray();
  1945. // Rejigger the linknum's of the remaining SOP's to fill any gap
  1946. if (parts.Length == 1 && RootPart != null)
  1947. {
  1948. // Single prim left
  1949. RootPart.LinkNum = 0;
  1950. }
  1951. else
  1952. {
  1953. for (int i = 0; i < parts.Length; i++)
  1954. {
  1955. SceneObjectPart part = parts[i];
  1956. if (part.LinkNum > linkPart.LinkNum)
  1957. part.LinkNum--;
  1958. }
  1959. }
  1960. }
  1961. linkPart.ParentID = 0;
  1962. linkPart.LinkNum = 0;
  1963. PhysicsActor linkPartPa = linkPart.PhysActor;
  1964. // Remove the SOP from the physical scene.
  1965. // If the new SOG is physical, it is re-created later.
  1966. // (There is a problem here in that we have not yet told the physics
  1967. // engine about the delink. Someday, linksets should be made first
  1968. // class objects in the physics engine interface).
  1969. if (linkPartPa != null)
  1970. m_scene.PhysicsScene.RemovePrim(linkPartPa);
  1971. // We need to reset the child part's position
  1972. // ready for life as a separate object after being a part of another object
  1973. /* This commented out code seems to recompute what GetWorldPosition already does.
  1974. * Replace with a call to GetWorldPosition (before unlinking)
  1975. Quaternion parentRot = m_rootPart.RotationOffset;
  1976. Vector3 axPos = linkPart.OffsetPosition;
  1977. axPos *= parentRot;
  1978. linkPart.OffsetPosition = new Vector3(axPos.X, axPos.Y, axPos.Z);
  1979. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  1980. linkPart.OffsetPosition = new Vector3(0, 0, 0);
  1981. */
  1982. linkPart.GroupPosition = worldPos;
  1983. linkPart.OffsetPosition = Vector3.Zero;
  1984. linkPart.RotationOffset = worldRot;
  1985. // Create a new SOG to go around this unlinked and unattached SOP
  1986. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  1987. m_scene.AddNewSceneObject(objectGroup, true);
  1988. if (sendEvents)
  1989. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  1990. linkPart.Rezzed = RootPart.Rezzed;
  1991. // When we delete a group, we currently have to force persist to the database if the object id has changed
  1992. // (since delete works by deleting all rows which have a given object id)
  1993. objectGroup.HasGroupChangedDueToDelink = true;
  1994. return objectGroup;
  1995. }
  1996. /// <summary>
  1997. /// Stop this object from being persisted over server restarts.
  1998. /// </summary>
  1999. /// <param name="objectGroup"></param>
  2000. public virtual void DetachFromBackup()
  2001. {
  2002. if (m_isBackedUp && Scene != null)
  2003. m_scene.EventManager.OnBackup -= ProcessBackup;
  2004. m_isBackedUp = false;
  2005. }
  2006. // This links an SOP from a previous linkset into my linkset.
  2007. // The trick is that the SOP's position and rotation are relative to the old root SOP's
  2008. // so we are passed in the position and rotation of the old linkset so this can
  2009. // unjigger this SOP's position and rotation from the previous linkset and
  2010. // then make them relative to my linkset root.
  2011. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation, int linkNum)
  2012. {
  2013. Quaternion parentRot = oldGroupRotation;
  2014. Quaternion oldRot = part.RotationOffset;
  2015. // Move our position to not be relative to the old parent
  2016. Vector3 axPos = part.OffsetPosition;
  2017. axPos *= parentRot;
  2018. part.OffsetPosition = axPos;
  2019. part.GroupPosition = oldGroupPosition + part.OffsetPosition;
  2020. part.OffsetPosition = Vector3.Zero;
  2021. // Compution our rotation to be not relative to the old parent
  2022. Quaternion worldRot = parentRot * oldRot;
  2023. part.RotationOffset = worldRot;
  2024. // Add this SOP to our linkset
  2025. part.SetParent(this);
  2026. part.ParentID = m_rootPart.LocalId;
  2027. m_parts.Add(part.UUID, part);
  2028. part.LinkNum = linkNum;
  2029. // Compute the new position of this SOP relative to the group position
  2030. part.OffsetPosition = part.GroupPosition - AbsolutePosition;
  2031. // (radams1 20120711: I don't know why part.OffsetPosition is set multiple times.
  2032. // It would have the affect of setting the physics engine position multiple
  2033. // times. In theory, that is not necessary but I don't have a good linkset
  2034. // test to know that cleaning up this code wouldn't break things.)
  2035. // Rotate the relative position by the rotation of the group
  2036. Quaternion rootRotation = m_rootPart.RotationOffset;
  2037. Vector3 pos = part.OffsetPosition;
  2038. pos *= Quaternion.Inverse(rootRotation);
  2039. part.OffsetPosition = pos;
  2040. // Compute the SOP's rotation relative to the rotation of the group.
  2041. parentRot = m_rootPart.RotationOffset;
  2042. oldRot = part.RotationOffset;
  2043. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  2044. part.RotationOffset = newRot;
  2045. // Since this SOP's state has changed, push those changes into the physics engine
  2046. // and the simulator.
  2047. part.UpdatePrimFlags(UsesPhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  2048. }
  2049. /// <summary>
  2050. /// If object is physical, apply force to move it around
  2051. /// If object is not physical, just put it at the resulting location
  2052. /// </summary>
  2053. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  2054. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  2055. /// <param name="remoteClient"></param>
  2056. public void GrabMovement(Vector3 offset, Vector3 pos, IClientAPI remoteClient)
  2057. {
  2058. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2059. {
  2060. PhysicsActor pa = m_rootPart.PhysActor;
  2061. if (pa != null)
  2062. {
  2063. if (pa.IsPhysical)
  2064. {
  2065. if (!m_rootPart.BlockGrab)
  2066. {
  2067. Vector3 llmoveforce = pos - AbsolutePosition;
  2068. Vector3 grabforce = llmoveforce;
  2069. grabforce = (grabforce / 10) * pa.Mass;
  2070. pa.AddForce(grabforce, true);
  2071. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. //NonPhysicalGrabMovement(pos);
  2077. }
  2078. }
  2079. else
  2080. {
  2081. //NonPhysicalGrabMovement(pos);
  2082. }
  2083. }
  2084. }
  2085. public void NonPhysicalGrabMovement(Vector3 pos)
  2086. {
  2087. AbsolutePosition = pos;
  2088. m_rootPart.SendTerseUpdateToAllClients();
  2089. }
  2090. /// <summary>
  2091. /// If object is physical, prepare for spinning torques (set flag to save old orientation)
  2092. /// </summary>
  2093. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  2094. /// <param name="remoteClient"></param>
  2095. public void SpinStart(IClientAPI remoteClient)
  2096. {
  2097. if (m_scene.EventManager.TriggerGroupSpinStart(UUID))
  2098. {
  2099. PhysicsActor pa = m_rootPart.PhysActor;
  2100. if (pa != null)
  2101. {
  2102. if (pa.IsPhysical)
  2103. {
  2104. m_rootPart.IsWaitingForFirstSpinUpdatePacket = true;
  2105. }
  2106. }
  2107. }
  2108. }
  2109. /// <summary>
  2110. /// If object is physical, apply torque to spin it around
  2111. /// </summary>
  2112. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  2113. /// <param name="remoteClient"></param>
  2114. public void SpinMovement(Quaternion newOrientation, IClientAPI remoteClient)
  2115. {
  2116. // The incoming newOrientation, sent by the client, "seems" to be the
  2117. // desired target orientation. This needs further verification; in particular,
  2118. // one would expect that the initial incoming newOrientation should be
  2119. // fairly close to the original prim's physical orientation,
  2120. // m_rootPart.PhysActor.Orientation. This however does not seem to be the
  2121. // case (might just be an issue with different quaternions representing the
  2122. // same rotation, or it might be a coordinate system issue).
  2123. //
  2124. // Since it's not clear what the relationship is between the PhysActor.Orientation
  2125. // and the incoming orientations sent by the client, we take an alternative approach
  2126. // of calculating the delta rotation between the orientations being sent by the
  2127. // client. (Since a spin is invoked by ctrl+shift+drag in the client, we expect
  2128. // a steady stream of several new orientations coming in from the client.)
  2129. // This ensures that the delta rotations are being calculated from self-consistent
  2130. // pairs of old/new rotations. Given the delta rotation, we apply a torque around
  2131. // the delta rotation axis, scaled by the object mass times an arbitrary scaling
  2132. // factor (to ensure the resulting torque is not "too strong" or "too weak").
  2133. //
  2134. // Ideally we need to calculate (probably iteratively) the exact torque or series
  2135. // of torques needed to arrive exactly at the destination orientation. However, since
  2136. // it is not yet clear how to map the destination orientation (provided by the viewer)
  2137. // into PhysActor orientations (needed by the physics engine), we omit this step.
  2138. // This means that the resulting torque will at least be in the correct direction,
  2139. // but it will result in over-shoot or under-shoot of the target orientation.
  2140. // For the end user, this means that ctrl+shift+drag can be used for relative,
  2141. // but not absolute, adjustments of orientation for physical prims.
  2142. if (m_scene.EventManager.TriggerGroupSpin(UUID, newOrientation))
  2143. {
  2144. PhysicsActor pa = m_rootPart.PhysActor;
  2145. if (pa != null)
  2146. {
  2147. if (pa.IsPhysical)
  2148. {
  2149. if (m_rootPart.IsWaitingForFirstSpinUpdatePacket)
  2150. {
  2151. // first time initialization of "old" orientation for calculation of delta rotations
  2152. m_rootPart.SpinOldOrientation = newOrientation;
  2153. m_rootPart.IsWaitingForFirstSpinUpdatePacket = false;
  2154. }
  2155. else
  2156. {
  2157. // save and update old orientation
  2158. Quaternion old = m_rootPart.SpinOldOrientation;
  2159. m_rootPart.SpinOldOrientation = newOrientation;
  2160. //m_log.Error("[SCENE OBJECT GROUP]: Old orientation is " + old);
  2161. //m_log.Error("[SCENE OBJECT GROUP]: Incoming new orientation is " + newOrientation);
  2162. // compute difference between previous old rotation and new incoming rotation
  2163. Quaternion minimalRotationFromQ1ToQ2 = Quaternion.Inverse(old) * newOrientation;
  2164. float rotationAngle;
  2165. Vector3 rotationAxis;
  2166. minimalRotationFromQ1ToQ2.GetAxisAngle(out rotationAxis, out rotationAngle);
  2167. rotationAxis.Normalize();
  2168. //m_log.Error("SCENE OBJECT GROUP]: rotation axis is " + rotationAxis);
  2169. Vector3 spinforce = new Vector3(rotationAxis.X, rotationAxis.Y, rotationAxis.Z);
  2170. spinforce = (spinforce/8) * pa.Mass; // 8 is an arbitrary torque scaling factor
  2171. pa.AddAngularForce(spinforce,true);
  2172. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2173. }
  2174. }
  2175. else
  2176. {
  2177. //NonPhysicalSpinMovement(pos);
  2178. }
  2179. }
  2180. else
  2181. {
  2182. //NonPhysicalSpinMovement(pos);
  2183. }
  2184. }
  2185. }
  2186. /// <summary>
  2187. /// Set the name of a prim
  2188. /// </summary>
  2189. /// <param name="name"></param>
  2190. /// <param name="localID"></param>
  2191. public void SetPartName(string name, uint localID)
  2192. {
  2193. SceneObjectPart part = GetPart(localID);
  2194. if (part != null)
  2195. {
  2196. part.Name = name;
  2197. }
  2198. }
  2199. public void SetPartDescription(string des, uint localID)
  2200. {
  2201. SceneObjectPart part = GetPart(localID);
  2202. if (part != null)
  2203. {
  2204. part.Description = des;
  2205. }
  2206. }
  2207. public void SetPartText(string text, uint localID)
  2208. {
  2209. SceneObjectPart part = GetPart(localID);
  2210. if (part != null)
  2211. {
  2212. part.SetText(text);
  2213. }
  2214. }
  2215. public void SetPartText(string text, UUID partID)
  2216. {
  2217. SceneObjectPart part = GetPart(partID);
  2218. if (part != null)
  2219. {
  2220. part.SetText(text);
  2221. }
  2222. }
  2223. public string GetPartName(uint localID)
  2224. {
  2225. SceneObjectPart part = GetPart(localID);
  2226. if (part != null)
  2227. {
  2228. return part.Name;
  2229. }
  2230. return String.Empty;
  2231. }
  2232. public string GetPartDescription(uint localID)
  2233. {
  2234. SceneObjectPart part = GetPart(localID);
  2235. if (part != null)
  2236. {
  2237. return part.Description;
  2238. }
  2239. return String.Empty;
  2240. }
  2241. /// <summary>
  2242. /// Update prim flags for this group.
  2243. /// </summary>
  2244. /// <param name="localID"></param>
  2245. /// <param name="UsePhysics"></param>
  2246. /// <param name="SetTemporary"></param>
  2247. /// <param name="SetPhantom"></param>
  2248. /// <param name="SetVolumeDetect"></param>
  2249. public void UpdatePrimFlags(uint localID, bool UsePhysics, bool SetTemporary, bool SetPhantom, bool SetVolumeDetect)
  2250. {
  2251. SceneObjectPart selectionPart = GetPart(localID);
  2252. if (SetTemporary && Scene != null)
  2253. {
  2254. DetachFromBackup();
  2255. // Remove from database and parcel prim count
  2256. //
  2257. m_scene.DeleteFromStorage(UUID);
  2258. m_scene.EventManager.TriggerParcelPrimCountTainted();
  2259. }
  2260. if (selectionPart != null)
  2261. {
  2262. SceneObjectPart[] parts = m_parts.GetArray();
  2263. if (Scene != null)
  2264. {
  2265. for (int i = 0; i < parts.Length; i++)
  2266. {
  2267. SceneObjectPart part = parts[i];
  2268. if (part.Scale.X > m_scene.m_maxPhys ||
  2269. part.Scale.Y > m_scene.m_maxPhys ||
  2270. part.Scale.Z > m_scene.m_maxPhys )
  2271. {
  2272. UsePhysics = false; // Reset physics
  2273. break;
  2274. }
  2275. }
  2276. }
  2277. for (int i = 0; i < parts.Length; i++)
  2278. parts[i].UpdatePrimFlags(UsePhysics, SetTemporary, SetPhantom, SetVolumeDetect);
  2279. }
  2280. }
  2281. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  2282. {
  2283. SceneObjectPart part = GetPart(localID);
  2284. if (part != null)
  2285. {
  2286. part.UpdateExtraParam(type, inUse, data);
  2287. }
  2288. }
  2289. /// <summary>
  2290. /// Update the texture entry for this part
  2291. /// </summary>
  2292. /// <param name="localID"></param>
  2293. /// <param name="textureEntry"></param>
  2294. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  2295. {
  2296. SceneObjectPart part = GetPart(localID);
  2297. if (part != null)
  2298. {
  2299. part.UpdateTextureEntry(textureEntry);
  2300. }
  2301. }
  2302. public void AdjustChildPrimPermissions()
  2303. {
  2304. ForEachPart(part =>
  2305. {
  2306. if (part != RootPart)
  2307. part.ClonePermissions(RootPart);
  2308. });
  2309. }
  2310. public void UpdatePermissions(UUID AgentID, byte field, uint localID,
  2311. uint mask, byte addRemTF)
  2312. {
  2313. RootPart.UpdatePermissions(AgentID, field, localID, mask, addRemTF);
  2314. AdjustChildPrimPermissions();
  2315. HasGroupChanged = true;
  2316. // Send the group's properties to all clients once all parts are updated
  2317. IClientAPI client;
  2318. if (Scene.TryGetClient(AgentID, out client))
  2319. SendPropertiesToClient(client);
  2320. }
  2321. #endregion
  2322. #region Shape
  2323. /// <summary>
  2324. ///
  2325. /// </summary>
  2326. /// <param name="shapeBlock"></param>
  2327. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  2328. {
  2329. SceneObjectPart part = GetPart(localID);
  2330. if (part != null)
  2331. {
  2332. part.UpdateShape(shapeBlock);
  2333. PhysicsActor pa = m_rootPart.PhysActor;
  2334. if (pa != null)
  2335. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2336. }
  2337. }
  2338. #endregion
  2339. #region Resize
  2340. /// <summary>
  2341. /// Resize the entire group of prims.
  2342. /// </summary>
  2343. /// <param name="scale"></param>
  2344. public void GroupResize(Vector3 scale)
  2345. {
  2346. // m_log.DebugFormat(
  2347. // "[SCENE OBJECT GROUP]: Group resizing {0} {1} from {2} to {3}", Name, LocalId, RootPart.Scale, scale);
  2348. PhysicsActor pa = m_rootPart.PhysActor;
  2349. RootPart.StoreUndoState(true);
  2350. if (Scene != null)
  2351. {
  2352. scale.X = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.X));
  2353. scale.Y = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.Y));
  2354. scale.Z = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.Z));
  2355. if (pa != null && pa.IsPhysical)
  2356. {
  2357. scale.X = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.X));
  2358. scale.Y = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.Y));
  2359. scale.Z = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.Z));
  2360. }
  2361. }
  2362. float x = (scale.X / RootPart.Scale.X);
  2363. float y = (scale.Y / RootPart.Scale.Y);
  2364. float z = (scale.Z / RootPart.Scale.Z);
  2365. SceneObjectPart[] parts = m_parts.GetArray();
  2366. if (Scene != null & (x > 1.0f || y > 1.0f || z > 1.0f))
  2367. {
  2368. for (int i = 0; i < parts.Length; i++)
  2369. {
  2370. SceneObjectPart obPart = parts[i];
  2371. if (obPart.UUID != m_rootPart.UUID)
  2372. {
  2373. // obPart.IgnoreUndoUpdate = true;
  2374. Vector3 oldSize = new Vector3(obPart.Scale);
  2375. float f = 1.0f;
  2376. float a = 1.0f;
  2377. if (pa != null && pa.IsPhysical)
  2378. {
  2379. if (oldSize.X * x > Scene.m_maxPhys)
  2380. {
  2381. f = m_scene.m_maxPhys / oldSize.X;
  2382. a = f / x;
  2383. x *= a;
  2384. y *= a;
  2385. z *= a;
  2386. }
  2387. else if (oldSize.X * x < Scene.m_minPhys)
  2388. {
  2389. f = m_scene.m_minPhys / oldSize.X;
  2390. a = f / x;
  2391. x *= a;
  2392. y *= a;
  2393. z *= a;
  2394. }
  2395. if (oldSize.Y * y > Scene.m_maxPhys)
  2396. {
  2397. f = m_scene.m_maxPhys / oldSize.Y;
  2398. a = f / y;
  2399. x *= a;
  2400. y *= a;
  2401. z *= a;
  2402. }
  2403. else if (oldSize.Y * y < Scene.m_minPhys)
  2404. {
  2405. f = m_scene.m_minPhys / oldSize.Y;
  2406. a = f / y;
  2407. x *= a;
  2408. y *= a;
  2409. z *= a;
  2410. }
  2411. if (oldSize.Z * z > Scene.m_maxPhys)
  2412. {
  2413. f = m_scene.m_maxPhys / oldSize.Z;
  2414. a = f / z;
  2415. x *= a;
  2416. y *= a;
  2417. z *= a;
  2418. }
  2419. else if (oldSize.Z * z < Scene.m_minPhys)
  2420. {
  2421. f = m_scene.m_minPhys / oldSize.Z;
  2422. a = f / z;
  2423. x *= a;
  2424. y *= a;
  2425. z *= a;
  2426. }
  2427. }
  2428. else
  2429. {
  2430. if (oldSize.X * x > Scene.m_maxNonphys)
  2431. {
  2432. f = m_scene.m_maxNonphys / oldSize.X;
  2433. a = f / x;
  2434. x *= a;
  2435. y *= a;
  2436. z *= a;
  2437. }
  2438. else if (oldSize.X * x < Scene.m_minNonphys)
  2439. {
  2440. f = m_scene.m_minNonphys / oldSize.X;
  2441. a = f / x;
  2442. x *= a;
  2443. y *= a;
  2444. z *= a;
  2445. }
  2446. if (oldSize.Y * y > Scene.m_maxNonphys)
  2447. {
  2448. f = m_scene.m_maxNonphys / oldSize.Y;
  2449. a = f / y;
  2450. x *= a;
  2451. y *= a;
  2452. z *= a;
  2453. }
  2454. else if (oldSize.Y * y < Scene.m_minNonphys)
  2455. {
  2456. f = m_scene.m_minNonphys / oldSize.Y;
  2457. a = f / y;
  2458. x *= a;
  2459. y *= a;
  2460. z *= a;
  2461. }
  2462. if (oldSize.Z * z > Scene.m_maxNonphys)
  2463. {
  2464. f = m_scene.m_maxNonphys / oldSize.Z;
  2465. a = f / z;
  2466. x *= a;
  2467. y *= a;
  2468. z *= a;
  2469. }
  2470. else if (oldSize.Z * z < Scene.m_minNonphys)
  2471. {
  2472. f = m_scene.m_minNonphys / oldSize.Z;
  2473. a = f / z;
  2474. x *= a;
  2475. y *= a;
  2476. z *= a;
  2477. }
  2478. }
  2479. // obPart.IgnoreUndoUpdate = false;
  2480. }
  2481. }
  2482. }
  2483. Vector3 prevScale = RootPart.Scale;
  2484. prevScale.X *= x;
  2485. prevScale.Y *= y;
  2486. prevScale.Z *= z;
  2487. // RootPart.IgnoreUndoUpdate = true;
  2488. RootPart.Resize(prevScale);
  2489. // RootPart.IgnoreUndoUpdate = false;
  2490. for (int i = 0; i < parts.Length; i++)
  2491. {
  2492. SceneObjectPart obPart = parts[i];
  2493. if (obPart.UUID != m_rootPart.UUID)
  2494. {
  2495. obPart.IgnoreUndoUpdate = true;
  2496. Vector3 currentpos = new Vector3(obPart.OffsetPosition);
  2497. currentpos.X *= x;
  2498. currentpos.Y *= y;
  2499. currentpos.Z *= z;
  2500. Vector3 newSize = new Vector3(obPart.Scale);
  2501. newSize.X *= x;
  2502. newSize.Y *= y;
  2503. newSize.Z *= z;
  2504. obPart.Resize(newSize);
  2505. obPart.UpdateOffSet(currentpos);
  2506. obPart.IgnoreUndoUpdate = false;
  2507. }
  2508. // obPart.IgnoreUndoUpdate = false;
  2509. // obPart.StoreUndoState();
  2510. }
  2511. // m_log.DebugFormat(
  2512. // "[SCENE OBJECT GROUP]: Finished group resizing {0} {1} to {2}", Name, LocalId, RootPart.Scale);
  2513. }
  2514. #endregion
  2515. #region Position
  2516. /// <summary>
  2517. /// Move this scene object
  2518. /// </summary>
  2519. /// <param name="pos"></param>
  2520. public void UpdateGroupPosition(Vector3 pos)
  2521. {
  2522. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Updating group position on {0} {1} to {2}", Name, LocalId, pos);
  2523. RootPart.StoreUndoState(true);
  2524. // SceneObjectPart[] parts = m_parts.GetArray();
  2525. // for (int i = 0; i < parts.Length; i++)
  2526. // parts[i].StoreUndoState();
  2527. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2528. {
  2529. if (IsAttachment)
  2530. {
  2531. m_rootPart.AttachedPos = pos;
  2532. }
  2533. if (RootPart.GetStatusSandbox())
  2534. {
  2535. if (Util.GetDistanceTo(RootPart.StatusSandboxPos, pos) > 10)
  2536. {
  2537. RootPart.ScriptSetPhysicsStatus(false);
  2538. pos = AbsolutePosition;
  2539. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  2540. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  2541. }
  2542. }
  2543. AbsolutePosition = pos;
  2544. HasGroupChanged = true;
  2545. }
  2546. //we need to do a terse update even if the move wasn't allowed
  2547. // so that the position is reset in the client (the object snaps back)
  2548. RootPart.ScheduleTerseUpdate();
  2549. }
  2550. /// <summary>
  2551. /// Update the position of a single part of this scene object
  2552. /// </summary>
  2553. /// <param name="pos"></param>
  2554. /// <param name="localID"></param>
  2555. public void UpdateSinglePosition(Vector3 pos, uint localID)
  2556. {
  2557. SceneObjectPart part = GetPart(localID);
  2558. // SceneObjectPart[] parts = m_parts.GetArray();
  2559. // for (int i = 0; i < parts.Length; i++)
  2560. // parts[i].StoreUndoState();
  2561. if (part != null)
  2562. {
  2563. // m_log.DebugFormat(
  2564. // "[SCENE OBJECT GROUP]: Updating single position of {0} {1} to {2}", part.Name, part.LocalId, pos);
  2565. part.StoreUndoState(false);
  2566. part.IgnoreUndoUpdate = true;
  2567. if (part.UUID == m_rootPart.UUID)
  2568. {
  2569. UpdateRootPosition(pos);
  2570. }
  2571. else
  2572. {
  2573. part.UpdateOffSet(pos);
  2574. }
  2575. HasGroupChanged = true;
  2576. part.IgnoreUndoUpdate = false;
  2577. }
  2578. }
  2579. /// <summary>
  2580. /// Update just the root prim position in a linkset
  2581. /// </summary>
  2582. /// <param name="pos"></param>
  2583. public void UpdateRootPosition(Vector3 pos)
  2584. {
  2585. // m_log.DebugFormat(
  2586. // "[SCENE OBJECT GROUP]: Updating root position of {0} {1} to {2}", Name, LocalId, pos);
  2587. // SceneObjectPart[] parts = m_parts.GetArray();
  2588. // for (int i = 0; i < parts.Length; i++)
  2589. // parts[i].StoreUndoState();
  2590. Vector3 newPos = new Vector3(pos.X, pos.Y, pos.Z);
  2591. Vector3 oldPos =
  2592. new Vector3(AbsolutePosition.X + m_rootPart.OffsetPosition.X,
  2593. AbsolutePosition.Y + m_rootPart.OffsetPosition.Y,
  2594. AbsolutePosition.Z + m_rootPart.OffsetPosition.Z);
  2595. Vector3 diff = oldPos - newPos;
  2596. Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
  2597. Quaternion partRotation = m_rootPart.RotationOffset;
  2598. axDiff *= Quaternion.Inverse(partRotation);
  2599. diff = axDiff;
  2600. SceneObjectPart[] parts = m_parts.GetArray();
  2601. for (int i = 0; i < parts.Length; i++)
  2602. {
  2603. SceneObjectPart obPart = parts[i];
  2604. if (obPart.UUID != m_rootPart.UUID)
  2605. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  2606. }
  2607. AbsolutePosition = newPos;
  2608. HasGroupChanged = true;
  2609. ScheduleGroupForTerseUpdate();
  2610. }
  2611. #endregion
  2612. #region Rotation
  2613. /// <summary>
  2614. /// Update the rotation of the group.
  2615. /// </summary>
  2616. /// <param name="rot"></param>
  2617. public void UpdateGroupRotationR(Quaternion rot)
  2618. {
  2619. // m_log.DebugFormat(
  2620. // "[SCENE OBJECT GROUP]: Updating group rotation R of {0} {1} to {2}", Name, LocalId, rot);
  2621. // SceneObjectPart[] parts = m_parts.GetArray();
  2622. // for (int i = 0; i < parts.Length; i++)
  2623. // parts[i].StoreUndoState();
  2624. m_rootPart.StoreUndoState(true);
  2625. m_rootPart.UpdateRotation(rot);
  2626. PhysicsActor actor = m_rootPart.PhysActor;
  2627. if (actor != null)
  2628. {
  2629. actor.Orientation = m_rootPart.RotationOffset;
  2630. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  2631. }
  2632. HasGroupChanged = true;
  2633. ScheduleGroupForTerseUpdate();
  2634. }
  2635. /// <summary>
  2636. /// Update the position and rotation of a group simultaneously.
  2637. /// </summary>
  2638. /// <param name="pos"></param>
  2639. /// <param name="rot"></param>
  2640. public void UpdateGroupRotationPR(Vector3 pos, Quaternion rot)
  2641. {
  2642. // m_log.DebugFormat(
  2643. // "[SCENE OBJECT GROUP]: Updating group rotation PR of {0} {1} to {2}", Name, LocalId, rot);
  2644. // SceneObjectPart[] parts = m_parts.GetArray();
  2645. // for (int i = 0; i < parts.Length; i++)
  2646. // parts[i].StoreUndoState();
  2647. RootPart.StoreUndoState(true);
  2648. RootPart.IgnoreUndoUpdate = true;
  2649. m_rootPart.UpdateRotation(rot);
  2650. PhysicsActor actor = m_rootPart.PhysActor;
  2651. if (actor != null)
  2652. {
  2653. actor.Orientation = m_rootPart.RotationOffset;
  2654. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  2655. }
  2656. if (IsAttachment)
  2657. {
  2658. m_rootPart.AttachedPos = pos;
  2659. }
  2660. AbsolutePosition = pos;
  2661. HasGroupChanged = true;
  2662. ScheduleGroupForTerseUpdate();
  2663. RootPart.IgnoreUndoUpdate = false;
  2664. }
  2665. /// <summary>
  2666. /// Update the rotation of a single prim within the group.
  2667. /// </summary>
  2668. /// <param name="rot"></param>
  2669. /// <param name="localID"></param>
  2670. public void UpdateSingleRotation(Quaternion rot, uint localID)
  2671. {
  2672. SceneObjectPart part = GetPart(localID);
  2673. SceneObjectPart[] parts = m_parts.GetArray();
  2674. for (int i = 0; i < parts.Length; i++)
  2675. parts[i].StoreUndoState();
  2676. if (part != null)
  2677. {
  2678. // m_log.DebugFormat(
  2679. // "[SCENE OBJECT GROUP]: Updating single rotation of {0} {1} to {2}", part.Name, part.LocalId, rot);
  2680. if (part.UUID == m_rootPart.UUID)
  2681. {
  2682. UpdateRootRotation(rot);
  2683. }
  2684. else
  2685. {
  2686. part.UpdateRotation(rot);
  2687. }
  2688. }
  2689. }
  2690. /// <summary>
  2691. /// Update the position and rotation simultaneously of a single prim within the group.
  2692. /// </summary>
  2693. /// <param name="rot"></param>
  2694. /// <param name="localID"></param>
  2695. public void UpdateSingleRotation(Quaternion rot, Vector3 pos, uint localID)
  2696. {
  2697. SceneObjectPart part = GetPart(localID);
  2698. if (part != null)
  2699. {
  2700. // m_log.DebugFormat(
  2701. // "[SCENE OBJECT GROUP]: Updating single position and rotation of {0} {1} to {2}",
  2702. // part.Name, part.LocalId, rot);
  2703. part.StoreUndoState();
  2704. part.IgnoreUndoUpdate = true;
  2705. if (part.UUID == m_rootPart.UUID)
  2706. {
  2707. UpdateRootRotation(rot);
  2708. AbsolutePosition = pos;
  2709. }
  2710. else
  2711. {
  2712. part.UpdateRotation(rot);
  2713. part.OffsetPosition = pos;
  2714. }
  2715. part.IgnoreUndoUpdate = false;
  2716. }
  2717. }
  2718. /// <summary>
  2719. /// Update the rotation of just the root prim of a linkset.
  2720. /// </summary>
  2721. /// <param name="rot"></param>
  2722. public void UpdateRootRotation(Quaternion rot)
  2723. {
  2724. // m_log.DebugFormat(
  2725. // "[SCENE OBJECT GROUP]: Updating root rotation of {0} {1} to {2}",
  2726. // Name, LocalId, rot);
  2727. Quaternion axRot = rot;
  2728. Quaternion oldParentRot = m_rootPart.RotationOffset;
  2729. m_rootPart.StoreUndoState();
  2730. m_rootPart.UpdateRotation(rot);
  2731. PhysicsActor pa = m_rootPart.PhysActor;
  2732. if (pa != null)
  2733. {
  2734. pa.Orientation = m_rootPart.RotationOffset;
  2735. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2736. }
  2737. SceneObjectPart[] parts = m_parts.GetArray();
  2738. for (int i = 0; i < parts.Length; i++)
  2739. {
  2740. SceneObjectPart prim = parts[i];
  2741. if (prim.UUID != m_rootPart.UUID)
  2742. {
  2743. prim.IgnoreUndoUpdate = true;
  2744. Vector3 axPos = prim.OffsetPosition;
  2745. axPos *= oldParentRot;
  2746. axPos *= Quaternion.Inverse(axRot);
  2747. prim.OffsetPosition = axPos;
  2748. Quaternion primsRot = prim.RotationOffset;
  2749. Quaternion newRot = oldParentRot * primsRot;
  2750. newRot = Quaternion.Inverse(axRot) * newRot;
  2751. prim.RotationOffset = newRot;
  2752. prim.ScheduleTerseUpdate();
  2753. prim.IgnoreUndoUpdate = false;
  2754. }
  2755. }
  2756. // for (int i = 0; i < parts.Length; i++)
  2757. // {
  2758. // SceneObjectPart childpart = parts[i];
  2759. // if (childpart != m_rootPart)
  2760. // {
  2761. //// childpart.IgnoreUndoUpdate = false;
  2762. //// childpart.StoreUndoState();
  2763. // }
  2764. // }
  2765. m_rootPart.ScheduleTerseUpdate();
  2766. // m_log.DebugFormat(
  2767. // "[SCENE OBJECT GROUP]: Updated root rotation of {0} {1} to {2}",
  2768. // Name, LocalId, rot);
  2769. }
  2770. #endregion
  2771. internal void SetAxisRotation(int axis, int rotate10)
  2772. {
  2773. bool setX = false;
  2774. bool setY = false;
  2775. bool setZ = false;
  2776. int xaxis = 2;
  2777. int yaxis = 4;
  2778. int zaxis = 8;
  2779. setX = ((axis & xaxis) != 0) ? true : false;
  2780. setY = ((axis & yaxis) != 0) ? true : false;
  2781. setZ = ((axis & zaxis) != 0) ? true : false;
  2782. float setval = (rotate10 > 0) ? 1f : 0f;
  2783. if (setX)
  2784. RootPart.RotationAxis.X = setval;
  2785. if (setY)
  2786. RootPart.RotationAxis.Y = setval;
  2787. if (setZ)
  2788. RootPart.RotationAxis.Z = setval;
  2789. if (setX || setY || setZ)
  2790. RootPart.SetPhysicsAxisRotation();
  2791. }
  2792. public int registerRotTargetWaypoint(Quaternion target, float tolerance)
  2793. {
  2794. scriptRotTarget waypoint = new scriptRotTarget();
  2795. waypoint.targetRot = target;
  2796. waypoint.tolerance = tolerance;
  2797. uint handle = m_scene.AllocateLocalId();
  2798. waypoint.handle = handle;
  2799. lock (m_rotTargets)
  2800. {
  2801. m_rotTargets.Add(handle, waypoint);
  2802. }
  2803. m_scene.AddGroupTarget(this);
  2804. return (int)handle;
  2805. }
  2806. public void unregisterRotTargetWaypoint(int handle)
  2807. {
  2808. lock (m_targets)
  2809. {
  2810. m_rotTargets.Remove((uint)handle);
  2811. if (m_targets.Count == 0)
  2812. m_scene.RemoveGroupTarget(this);
  2813. }
  2814. }
  2815. public int registerTargetWaypoint(Vector3 target, float tolerance)
  2816. {
  2817. scriptPosTarget waypoint = new scriptPosTarget();
  2818. waypoint.targetPos = target;
  2819. waypoint.tolerance = tolerance;
  2820. uint handle = m_scene.AllocateLocalId();
  2821. waypoint.handle = handle;
  2822. lock (m_targets)
  2823. {
  2824. m_targets.Add(handle, waypoint);
  2825. }
  2826. m_scene.AddGroupTarget(this);
  2827. return (int)handle;
  2828. }
  2829. public void unregisterTargetWaypoint(int handle)
  2830. {
  2831. lock (m_targets)
  2832. {
  2833. m_targets.Remove((uint)handle);
  2834. if (m_targets.Count == 0)
  2835. m_scene.RemoveGroupTarget(this);
  2836. }
  2837. }
  2838. public void checkAtTargets()
  2839. {
  2840. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  2841. {
  2842. if (m_targets.Count > 0)
  2843. {
  2844. bool at_target = false;
  2845. //Vector3 targetPos;
  2846. //uint targetHandle;
  2847. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  2848. lock (m_targets)
  2849. {
  2850. foreach (uint idx in m_targets.Keys)
  2851. {
  2852. scriptPosTarget target = m_targets[idx];
  2853. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  2854. {
  2855. // trigger at_target
  2856. if (m_scriptListens_atTarget)
  2857. {
  2858. at_target = true;
  2859. scriptPosTarget att = new scriptPosTarget();
  2860. att.targetPos = target.targetPos;
  2861. att.tolerance = target.tolerance;
  2862. att.handle = target.handle;
  2863. atTargets.Add(idx, att);
  2864. }
  2865. }
  2866. }
  2867. }
  2868. if (atTargets.Count > 0)
  2869. {
  2870. SceneObjectPart[] parts = m_parts.GetArray();
  2871. uint[] localids = new uint[parts.Length];
  2872. for (int i = 0; i < parts.Length; i++)
  2873. localids[i] = parts[i].LocalId;
  2874. for (int ctr = 0; ctr < localids.Length; ctr++)
  2875. {
  2876. foreach (uint target in atTargets.Keys)
  2877. {
  2878. scriptPosTarget att = atTargets[target];
  2879. m_scene.EventManager.TriggerAtTargetEvent(
  2880. localids[ctr], att.handle, att.targetPos, m_rootPart.GroupPosition);
  2881. }
  2882. }
  2883. return;
  2884. }
  2885. if (m_scriptListens_notAtTarget && !at_target)
  2886. {
  2887. //trigger not_at_target
  2888. SceneObjectPart[] parts = m_parts.GetArray();
  2889. uint[] localids = new uint[parts.Length];
  2890. for (int i = 0; i < parts.Length; i++)
  2891. localids[i] = parts[i].LocalId;
  2892. for (int ctr = 0; ctr < localids.Length; ctr++)
  2893. {
  2894. m_scene.EventManager.TriggerNotAtTargetEvent(localids[ctr]);
  2895. }
  2896. }
  2897. }
  2898. }
  2899. if (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget)
  2900. {
  2901. if (m_rotTargets.Count > 0)
  2902. {
  2903. bool at_Rottarget = false;
  2904. Dictionary<uint, scriptRotTarget> atRotTargets = new Dictionary<uint, scriptRotTarget>();
  2905. lock (m_rotTargets)
  2906. {
  2907. foreach (uint idx in m_rotTargets.Keys)
  2908. {
  2909. scriptRotTarget target = m_rotTargets[idx];
  2910. double angle
  2911. = Math.Acos(
  2912. target.targetRot.X * m_rootPart.RotationOffset.X
  2913. + target.targetRot.Y * m_rootPart.RotationOffset.Y
  2914. + target.targetRot.Z * m_rootPart.RotationOffset.Z
  2915. + target.targetRot.W * m_rootPart.RotationOffset.W)
  2916. * 2;
  2917. if (angle < 0) angle = -angle;
  2918. if (angle > Math.PI) angle = (Math.PI * 2 - angle);
  2919. if (angle <= target.tolerance)
  2920. {
  2921. // trigger at_rot_target
  2922. if (m_scriptListens_atRotTarget)
  2923. {
  2924. at_Rottarget = true;
  2925. scriptRotTarget att = new scriptRotTarget();
  2926. att.targetRot = target.targetRot;
  2927. att.tolerance = target.tolerance;
  2928. att.handle = target.handle;
  2929. atRotTargets.Add(idx, att);
  2930. }
  2931. }
  2932. }
  2933. }
  2934. if (atRotTargets.Count > 0)
  2935. {
  2936. SceneObjectPart[] parts = m_parts.GetArray();
  2937. uint[] localids = new uint[parts.Length];
  2938. for (int i = 0; i < parts.Length; i++)
  2939. localids[i] = parts[i].LocalId;
  2940. for (int ctr = 0; ctr < localids.Length; ctr++)
  2941. {
  2942. foreach (uint target in atRotTargets.Keys)
  2943. {
  2944. scriptRotTarget att = atRotTargets[target];
  2945. m_scene.EventManager.TriggerAtRotTargetEvent(
  2946. localids[ctr], att.handle, att.targetRot, m_rootPart.RotationOffset);
  2947. }
  2948. }
  2949. return;
  2950. }
  2951. if (m_scriptListens_notAtRotTarget && !at_Rottarget)
  2952. {
  2953. //trigger not_at_target
  2954. SceneObjectPart[] parts = m_parts.GetArray();
  2955. uint[] localids = new uint[parts.Length];
  2956. for (int i = 0; i < parts.Length; i++)
  2957. localids[i] = parts[i].LocalId;
  2958. for (int ctr = 0; ctr < localids.Length; ctr++)
  2959. {
  2960. m_scene.EventManager.TriggerNotAtRotTargetEvent(localids[ctr]);
  2961. }
  2962. }
  2963. }
  2964. }
  2965. }
  2966. public float GetMass()
  2967. {
  2968. float retmass = 0f;
  2969. SceneObjectPart[] parts = m_parts.GetArray();
  2970. for (int i = 0; i < parts.Length; i++)
  2971. retmass += parts[i].GetMass();
  2972. return retmass;
  2973. }
  2974. /// <summary>
  2975. /// If the object is a sculpt/mesh, retrieve the mesh data for each part and reinsert it into each shape so that
  2976. /// the physics engine can use it.
  2977. /// </summary>
  2978. /// <remarks>
  2979. /// When the physics engine has finished with it, the sculpt data is discarded to save memory.
  2980. /// </remarks>
  2981. /*
  2982. public void CheckSculptAndLoad()
  2983. {
  2984. if (IsDeleted)
  2985. return;
  2986. if ((RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Phantom) != 0)
  2987. return;
  2988. // m_log.Debug("Processing CheckSculptAndLoad for {0} {1}", Name, LocalId);
  2989. SceneObjectPart[] parts = m_parts.GetArray();
  2990. for (int i = 0; i < parts.Length; i++)
  2991. parts[i].CheckSculptAndLoad();
  2992. }
  2993. */
  2994. /// <summary>
  2995. /// Set the user group to which this scene object belongs.
  2996. /// </summary>
  2997. /// <param name="GroupID"></param>
  2998. /// <param name="client"></param>
  2999. public void SetGroup(UUID GroupID, IClientAPI client)
  3000. {
  3001. SceneObjectPart[] parts = m_parts.GetArray();
  3002. for (int i = 0; i < parts.Length; i++)
  3003. {
  3004. SceneObjectPart part = parts[i];
  3005. part.SetGroup(GroupID, client);
  3006. part.Inventory.ChangeInventoryGroup(GroupID);
  3007. }
  3008. HasGroupChanged = true;
  3009. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  3010. // for the same object with very different properties. The caller must schedule the update.
  3011. //ScheduleGroupForFullUpdate();
  3012. }
  3013. public void TriggerScriptChangedEvent(Changed val)
  3014. {
  3015. SceneObjectPart[] parts = m_parts.GetArray();
  3016. for (int i = 0; i < parts.Length; i++)
  3017. parts[i].TriggerScriptChangedEvent(val);
  3018. }
  3019. /// <summary>
  3020. /// Returns a count of the number of scripts in this groups parts.
  3021. /// </summary>
  3022. public int ScriptCount()
  3023. {
  3024. int count = 0;
  3025. SceneObjectPart[] parts = m_parts.GetArray();
  3026. for (int i = 0; i < parts.Length; i++)
  3027. count += parts[i].Inventory.ScriptCount();
  3028. return count;
  3029. }
  3030. /// <summary>
  3031. /// A float the value is a representative execution time in milliseconds of all scripts in the link set.
  3032. /// </summary>
  3033. public float ScriptExecutionTime()
  3034. {
  3035. IScriptModule[] engines = Scene.RequestModuleInterfaces<IScriptModule>();
  3036. if (engines.Length == 0) // No engine at all
  3037. return 0.0f;
  3038. float time = 0.0f;
  3039. // get all the scripts in all parts
  3040. SceneObjectPart[] parts = m_parts.GetArray();
  3041. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>();
  3042. for (int i = 0; i < parts.Length; i++)
  3043. {
  3044. scripts.AddRange(parts[i].Inventory.GetInventoryItems(InventoryType.LSL));
  3045. }
  3046. // extract the UUIDs
  3047. List<UUID> ids = new List<UUID>(scripts.Count);
  3048. foreach (TaskInventoryItem script in scripts)
  3049. {
  3050. if (!ids.Contains(script.ItemID))
  3051. {
  3052. ids.Add(script.ItemID);
  3053. }
  3054. }
  3055. // Offer the list of script UUIDs to each engine found and accumulate the time
  3056. foreach (IScriptModule e in engines)
  3057. {
  3058. if (e != null)
  3059. {
  3060. time += e.GetScriptExecutionTime(ids);
  3061. }
  3062. }
  3063. return time;
  3064. }
  3065. /// <summary>
  3066. /// Returns a count of the number of running scripts in this groups parts.
  3067. /// </summary>
  3068. public int RunningScriptCount()
  3069. {
  3070. int count = 0;
  3071. SceneObjectPart[] parts = m_parts.GetArray();
  3072. for (int i = 0; i < parts.Length; i++)
  3073. count += parts[i].Inventory.RunningScriptCount();
  3074. return count;
  3075. }
  3076. /// <summary>
  3077. /// Get a copy of the list of sitting avatars on all prims of this object.
  3078. /// </summary>
  3079. /// <remarks>
  3080. /// This is sorted by the order in which avatars sat down. If an avatar stands up then all avatars that sat
  3081. /// down after it move one place down the list.
  3082. /// </remarks>
  3083. /// <returns>A list of the sitting avatars. Returns an empty list if there are no sitting avatars.</returns>
  3084. public List<UUID> GetSittingAvatars()
  3085. {
  3086. lock (m_sittingAvatars)
  3087. return new List<UUID>(m_sittingAvatars);
  3088. }
  3089. /// <summary>
  3090. /// Gets the number of sitting avatars.
  3091. /// </summary>
  3092. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  3093. /// <returns></returns>
  3094. public int GetSittingAvatarsCount()
  3095. {
  3096. lock (m_sittingAvatars)
  3097. return m_sittingAvatars.Count;
  3098. }
  3099. public override string ToString()
  3100. {
  3101. return String.Format("{0} {1} ({2})", Name, UUID, AbsolutePosition);
  3102. }
  3103. #region ISceneObject
  3104. public virtual ISceneObject CloneForNewScene()
  3105. {
  3106. SceneObjectGroup sog = Copy(false);
  3107. sog.IsDeleted = false;
  3108. return sog;
  3109. }
  3110. public virtual string ToXml2()
  3111. {
  3112. return SceneObjectSerializer.ToXml2Format(this);
  3113. }
  3114. public virtual string ExtraToXmlString()
  3115. {
  3116. return "<ExtraFromItemID>" + FromItemID.ToString() + "</ExtraFromItemID>";
  3117. }
  3118. public virtual void ExtraFromXmlString(string xmlstr)
  3119. {
  3120. string id = xmlstr.Substring(xmlstr.IndexOf("<ExtraFromItemID>"));
  3121. id = xmlstr.Replace("<ExtraFromItemID>", "");
  3122. id = id.Replace("</ExtraFromItemID>", "");
  3123. UUID uuid = UUID.Zero;
  3124. UUID.TryParse(id, out uuid);
  3125. FromItemID = uuid;
  3126. }
  3127. #endregion
  3128. }
  3129. }