PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/OpenSim/Region/Framework/Scenes/SceneGraph.cs

https://bitbucket.org/VirtualReality/taiga
C# | 1952 lines | 1260 code | 236 blank | 456 comment | 228 complexity | 3a5d82e7eb645804ec9b97ce391ee359 MD5 | raw file
  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.Threading;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using OpenMetaverse.Packets;
  33. using log4net;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Scenes.Types;
  36. using OpenSim.Region.Physics.Manager;
  37. namespace OpenSim.Region.Framework.Scenes
  38. {
  39. public delegate void PhysicsCrash();
  40. public delegate void ObjectDuplicateDelegate(EntityBase original, EntityBase clone);
  41. public delegate void ObjectCreateDelegate(EntityBase obj);
  42. public delegate void ObjectDeleteDelegate(EntityBase obj);
  43. /// <summary>
  44. /// This class used to be called InnerScene and may not yet truly be a SceneGraph. The non scene graph components
  45. /// should be migrated out over time.
  46. /// </summary>
  47. public class SceneGraph
  48. {
  49. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  50. #region Events
  51. protected internal event PhysicsCrash UnRecoverableError;
  52. private PhysicsCrash handlerPhysicsCrash = null;
  53. public event ObjectDuplicateDelegate OnObjectDuplicate;
  54. public event ObjectCreateDelegate OnObjectCreate;
  55. public event ObjectDeleteDelegate OnObjectRemove;
  56. #endregion
  57. #region Fields
  58. protected Dictionary<UUID, ScenePresence> m_scenePresences = new Dictionary<UUID, ScenePresence>();
  59. protected ScenePresence[] m_scenePresenceArray = new ScenePresence[0];
  60. // SceneObjects is not currently populated or used.
  61. //public Dictionary<UUID, SceneObjectGroup> SceneObjects;
  62. protected internal EntityManager Entities = new EntityManager();
  63. // protected internal Dictionary<UUID, EntityBase> Entities = new Dictionary<UUID, EntityBase>();
  64. protected internal Dictionary<UUID, ScenePresence> RestorePresences = new Dictionary<UUID, ScenePresence>();
  65. protected internal BasicQuadTreeNode QuadTree;
  66. protected RegionInfo m_regInfo;
  67. protected Scene m_parentScene;
  68. protected Dictionary<UUID, SceneObjectGroup> m_updateList = new Dictionary<UUID, SceneObjectGroup>();
  69. protected int m_numRootAgents = 0;
  70. protected int m_numPrim = 0;
  71. protected int m_numChildAgents = 0;
  72. protected int m_physicalPrim = 0;
  73. protected int m_activeScripts = 0;
  74. protected int m_scriptLPS = 0;
  75. protected internal object m_syncRoot = new object();
  76. protected internal PhysicsScene _PhyScene;
  77. protected internal Dictionary<uint, SceneObjectGroup> SceneObjectGroupsByLocalID = new Dictionary<uint, SceneObjectGroup>();
  78. protected internal Dictionary<UUID, SceneObjectGroup> SceneObjectGroupsByFullID = new Dictionary<UUID, SceneObjectGroup>();
  79. private readonly Object m_dictionary_lock = new Object();
  80. private Object m_updateLock = new Object();
  81. #endregion
  82. protected internal SceneGraph(Scene parent, RegionInfo regInfo)
  83. {
  84. m_parentScene = parent;
  85. m_regInfo = regInfo;
  86. QuadTree = new BasicQuadTreeNode(null, "/0/", 0, 0, (short)Constants.RegionSize, (short)Constants.RegionSize);
  87. QuadTree.Subdivide();
  88. QuadTree.Subdivide();
  89. }
  90. public PhysicsScene PhysicsScene
  91. {
  92. get { return _PhyScene; }
  93. set
  94. {
  95. // If we're not doing the initial set
  96. // Then we've got to remove the previous
  97. // event handler
  98. if (_PhyScene != null)
  99. _PhyScene.OnPhysicsCrash -= physicsBasedCrash;
  100. _PhyScene = value;
  101. if (_PhyScene != null)
  102. _PhyScene.OnPhysicsCrash += physicsBasedCrash;
  103. }
  104. }
  105. protected internal void Close()
  106. {
  107. lock (m_scenePresences)
  108. {
  109. m_scenePresences.Clear();
  110. m_scenePresenceArray = new ScenePresence[0];
  111. }
  112. lock (m_dictionary_lock)
  113. {
  114. SceneObjectGroupsByFullID.Clear();
  115. SceneObjectGroupsByLocalID.Clear();
  116. }
  117. Entities.Clear();
  118. }
  119. #region Update Methods
  120. protected internal void UpdatePreparePhysics()
  121. {
  122. // If we are using a threaded physics engine
  123. // grab the latest scene from the engine before
  124. // trying to process it.
  125. // PhysX does this (runs in the background).
  126. if (_PhyScene.IsThreaded)
  127. {
  128. _PhyScene.GetResults();
  129. }
  130. }
  131. protected internal void UpdatePresences()
  132. {
  133. ScenePresence[] updateScenePresences = GetScenePresences();
  134. for (int i = 0; i < updateScenePresences.Length; i++)
  135. updateScenePresences[i].Update();
  136. }
  137. protected internal float UpdatePhysics(double elapsed)
  138. {
  139. lock (m_syncRoot)
  140. {
  141. // Here is where the Scene calls the PhysicsScene. This is a one-way
  142. // interaction; the PhysicsScene cannot access the calling Scene directly.
  143. // But with joints, we want a PhysicsActor to be able to influence a
  144. // non-physics SceneObjectPart. In particular, a PhysicsActor that is connected
  145. // with a joint should be able to move the SceneObjectPart which is the visual
  146. // representation of that joint (for editing and serialization purposes).
  147. // However the PhysicsActor normally cannot directly influence anything outside
  148. // of the PhysicsScene, and the non-physical SceneObjectPart which represents
  149. // the joint in the Scene does not exist in the PhysicsScene.
  150. //
  151. // To solve this, we have an event in the PhysicsScene that is fired when a joint
  152. // has changed position (because one of its associated PhysicsActors has changed
  153. // position).
  154. //
  155. // Therefore, JointMoved and JointDeactivated events will be fired as a result of the following Simulate().
  156. return _PhyScene.Simulate((float)elapsed);
  157. }
  158. }
  159. protected internal void UpdateScenePresenceMovement()
  160. {
  161. ScenePresence[] moveEntities = GetScenePresences();
  162. for (int i = 0; i < moveEntities.Length; i++)
  163. moveEntities[i].UpdateMovement();
  164. }
  165. #endregion
  166. #region Entity Methods
  167. /// <summary>
  168. /// Add an object into the scene that has come from storage
  169. /// </summary>
  170. /// <param name="sceneObject"></param>
  171. /// <param name="attachToBackup">
  172. /// If true, changes to the object will be reflected in its persisted data
  173. /// If false, the persisted data will not be changed even if the object in the scene is changed
  174. /// </param>
  175. /// <param name="alreadyPersisted">
  176. /// If true, we won't persist this object until it changes
  177. /// If false, we'll persist this object immediately
  178. /// </param>
  179. /// <returns>
  180. /// true if the object was added, false if an object with the same uuid was already in the scene
  181. /// </returns>
  182. protected internal bool AddRestoredSceneObject(
  183. SceneObjectGroup sceneObject, bool attachToBackup, bool alreadyPersisted)
  184. {
  185. if (!alreadyPersisted)
  186. {
  187. sceneObject.ForceInventoryPersistence();
  188. sceneObject.HasGroupChanged = true;
  189. }
  190. return AddSceneObject(sceneObject, attachToBackup, true);
  191. }
  192. /// <summary>
  193. /// Add a newly created object to the scene. This will both update the scene, and send information about the
  194. /// new object to all clients interested in the scene.
  195. /// </summary>
  196. /// <param name="sceneObject"></param>
  197. /// <param name="attachToBackup">
  198. /// If true, the object is made persistent into the scene.
  199. /// If false, the object will not persist over server restarts
  200. /// </param>
  201. /// <returns>
  202. /// true if the object was added, false if an object with the same uuid was already in the scene
  203. /// </returns>
  204. protected internal bool AddNewSceneObject(SceneObjectGroup sceneObject, bool attachToBackup, bool sendClientUpdates)
  205. {
  206. // Ensure that we persist this new scene object
  207. sceneObject.HasGroupChanged = true;
  208. return AddSceneObject(sceneObject, attachToBackup, sendClientUpdates);
  209. }
  210. /// <summary>
  211. /// Add an object to the scene. This will both update the scene, and send information about the
  212. /// new object to all clients interested in the scene.
  213. /// </summary>
  214. /// <param name="sceneObject"></param>
  215. /// <param name="attachToBackup">
  216. /// If true, the object is made persistent into the scene.
  217. /// If false, the object will not persist over server restarts
  218. /// </param>
  219. /// <param name="sendClientUpdates">
  220. /// If true, updates for the new scene object are sent to all viewers in range.
  221. /// If false, it is left to the caller to schedule the update
  222. /// </param>
  223. /// <returns>
  224. /// true if the object was added, false if an object with the same uuid was already in the scene
  225. /// </returns>
  226. protected bool AddSceneObject(SceneObjectGroup sceneObject, bool attachToBackup, bool sendClientUpdates)
  227. {
  228. if (sceneObject == null || sceneObject.RootPart == null || sceneObject.RootPart.UUID == UUID.Zero)
  229. return false;
  230. bool alreadyExisted = false;
  231. if (m_parentScene.m_clampPrimSize)
  232. {
  233. foreach (SceneObjectPart part in sceneObject.Children.Values)
  234. {
  235. Vector3 scale = part.Shape.Scale;
  236. if (scale.X > m_parentScene.m_maxNonphys)
  237. scale.X = m_parentScene.m_maxNonphys;
  238. if (scale.Y > m_parentScene.m_maxNonphys)
  239. scale.Y = m_parentScene.m_maxNonphys;
  240. if (scale.Z > m_parentScene.m_maxNonphys)
  241. scale.Z = m_parentScene.m_maxNonphys;
  242. part.Shape.Scale = scale;
  243. }
  244. }
  245. sceneObject.AttachToScene(m_parentScene);
  246. if (sendClientUpdates)
  247. sceneObject.ScheduleGroupForFullUpdate();
  248. lock (sceneObject)
  249. {
  250. if (!Entities.ContainsKey(sceneObject.UUID))
  251. {
  252. Entities.Add(sceneObject);
  253. m_numPrim += sceneObject.Children.Count;
  254. if (attachToBackup)
  255. sceneObject.AttachToBackup();
  256. if (OnObjectCreate != null)
  257. OnObjectCreate(sceneObject);
  258. lock (m_dictionary_lock)
  259. {
  260. SceneObjectGroupsByFullID[sceneObject.UUID] = sceneObject;
  261. SceneObjectGroupsByLocalID[sceneObject.LocalId] = sceneObject;
  262. foreach (SceneObjectPart part in sceneObject.Children.Values)
  263. {
  264. SceneObjectGroupsByFullID[part.UUID] = sceneObject;
  265. SceneObjectGroupsByLocalID[part.LocalId] = sceneObject;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. alreadyExisted = true;
  272. }
  273. }
  274. return alreadyExisted;
  275. }
  276. /// <summary>
  277. /// Delete an object from the scene
  278. /// </summary>
  279. /// <returns>true if the object was deleted, false if there was no object to delete</returns>
  280. public bool DeleteSceneObject(UUID uuid, bool resultOfObjectLinked)
  281. {
  282. if (Entities.ContainsKey(uuid))
  283. {
  284. if (!resultOfObjectLinked)
  285. {
  286. m_numPrim -= ((SceneObjectGroup) Entities[uuid]).Children.Count;
  287. if ((((SceneObjectGroup)Entities[uuid]).RootPart.Flags & PrimFlags.Physics) == PrimFlags.Physics)
  288. {
  289. RemovePhysicalPrim(((SceneObjectGroup)Entities[uuid]).Children.Count);
  290. }
  291. }
  292. if (OnObjectRemove != null)
  293. OnObjectRemove(Entities[uuid]);
  294. lock (m_dictionary_lock)
  295. {
  296. SceneObjectGroupsByFullID.Remove(uuid);
  297. SceneObjectGroupsByLocalID.Remove(((SceneObjectGroup)Entities[uuid]).LocalId);
  298. }
  299. Entities.Remove(uuid);
  300. //SceneObjectGroup part;
  301. //((part.RootPart.Flags & PrimFlags.Physics) == PrimFlags.Physics)
  302. return true;
  303. }
  304. return false;
  305. }
  306. /// <summary>
  307. /// Add an object to the list of prims to process on the next update
  308. /// </summary>
  309. /// <param name="obj">
  310. /// A <see cref="SceneObjectGroup"/>
  311. /// </param>
  312. protected internal void AddToUpdateList(SceneObjectGroup obj)
  313. {
  314. lock (m_updateList)
  315. {
  316. m_updateList[obj.UUID] = obj;
  317. }
  318. }
  319. /// <summary>
  320. /// Process all pending updates
  321. /// </summary>
  322. protected internal void UpdateObjectGroups()
  323. {
  324. if (!Monitor.TryEnter(m_updateLock))
  325. return;
  326. List<SceneObjectGroup> updates;
  327. // Some updates add more updates to the updateList.
  328. // Get the current list of updates and clear the list before iterating
  329. lock (m_updateList)
  330. {
  331. updates = new List<SceneObjectGroup>(m_updateList.Values);
  332. m_updateList.Clear();
  333. }
  334. // Go through all updates
  335. for (int i = 0; i < updates.Count; i++)
  336. {
  337. SceneObjectGroup sog = updates[i];
  338. // Don't abort the whole update if one entity happens to give us an exception.
  339. try
  340. {
  341. sog.Update();
  342. }
  343. catch (Exception e)
  344. {
  345. m_log.ErrorFormat(
  346. "[INNER SCENE]: Failed to update {0}, {1} - {2}", sog.Name, sog.UUID, e);
  347. }
  348. }
  349. Monitor.Exit(m_updateLock);
  350. }
  351. protected internal void AddPhysicalPrim(int number)
  352. {
  353. m_physicalPrim++;
  354. }
  355. protected internal void RemovePhysicalPrim(int number)
  356. {
  357. m_physicalPrim--;
  358. }
  359. protected internal void AddToScriptLPS(int number)
  360. {
  361. m_scriptLPS += number;
  362. }
  363. protected internal void AddActiveScripts(int number)
  364. {
  365. m_activeScripts += number;
  366. }
  367. public void DropObject(uint objectLocalID, IClientAPI remoteClient)
  368. {
  369. SceneObjectGroup group = GetGroupByPrim(objectLocalID);
  370. if (group != null)
  371. {
  372. m_parentScene.DetachSingleAttachmentToGround(group.UUID, remoteClient);
  373. }
  374. }
  375. protected internal void DetachObject(uint objectLocalID, IClientAPI remoteClient)
  376. {
  377. SceneObjectGroup group = GetGroupByPrim(objectLocalID);
  378. if (group != null)
  379. {
  380. //group.DetachToGround();
  381. m_parentScene.DetachSingleAttachmentToInv(group.GetFromItemID(), remoteClient);
  382. }
  383. }
  384. protected internal void HandleUndo(IClientAPI remoteClient, UUID primId)
  385. {
  386. if (primId != UUID.Zero)
  387. {
  388. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primId);
  389. if (part != null)
  390. part.Undo();
  391. }
  392. }
  393. protected internal void HandleRedo(IClientAPI remoteClient, UUID primId)
  394. {
  395. if (primId != UUID.Zero)
  396. {
  397. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primId);
  398. if (part != null)
  399. part.Redo();
  400. }
  401. }
  402. protected internal void HandleObjectGroupUpdate(
  403. IClientAPI remoteClient, UUID GroupID, uint objectLocalID, UUID Garbage)
  404. {
  405. SceneObjectGroup group = GetGroupByPrim(objectLocalID);
  406. if (group != null)
  407. {
  408. if (group.OwnerID == remoteClient.AgentId)
  409. group.SetGroup(GroupID, remoteClient);
  410. }
  411. }
  412. /// <summary>
  413. /// Event Handling routine for Attach Object
  414. /// </summary>
  415. /// <param name="remoteClient"></param>
  416. /// <param name="objectLocalID"></param>
  417. /// <param name="AttachmentPt"></param>
  418. /// <param name="rot"></param>
  419. protected internal void AttachObject(IClientAPI remoteClient, uint objectLocalID, uint AttachmentPt, Quaternion rot, bool silent)
  420. {
  421. // If we can't take it, we can't attach it!
  422. SceneObjectPart part = m_parentScene.GetSceneObjectPart(objectLocalID);
  423. if (part == null)
  424. return;
  425. if (!m_parentScene.Permissions.CanTakeObject(part.UUID, remoteClient.AgentId))
  426. return;
  427. // Calls attach with a Zero position
  428. if (AttachObject(remoteClient, objectLocalID, AttachmentPt, rot, Vector3.Zero, false))
  429. {
  430. m_parentScene.SendAttachEvent(objectLocalID, part.ParentGroup.GetFromItemID(), remoteClient.AgentId);
  431. // Save avatar attachment information
  432. ScenePresence presence;
  433. if (m_parentScene.AvatarFactory != null && m_parentScene.TryGetAvatar(remoteClient.AgentId, out presence))
  434. {
  435. m_log.Info(
  436. "[SCENE]: Saving avatar attachment. AgentID: " + remoteClient.AgentId
  437. + ", AttachmentPoint: " + AttachmentPt);
  438. m_parentScene.AvatarFactory.UpdateDatabase(remoteClient.AgentId, presence.Appearance);
  439. }
  440. }
  441. }
  442. /// <summary>
  443. /// Rez an attachment
  444. /// </summary>
  445. /// <param name="remoteClient"></param>
  446. /// <param name="itemID"></param>
  447. /// <param name="AttachmentPt"></param>
  448. /// <returns>The scene object that was attached. Null if the scene object could not be found</returns>
  449. public SceneObjectGroup RezSingleAttachment(IClientAPI remoteClient, UUID itemID, uint AttachmentPt)
  450. {
  451. SceneObjectGroup objatt = m_parentScene.RezObject(remoteClient,
  452. itemID, Vector3.Zero, Vector3.Zero, UUID.Zero, (byte)1, true,
  453. false, false, remoteClient.AgentId, true);
  454. // m_log.DebugFormat(
  455. // "[SCENE GRAPH]: Retrieved single object {0} for attachment to {1} on point {2}",
  456. // objatt.Name, remoteClient.Name, AttachmentPt);
  457. if (objatt != null)
  458. {
  459. bool tainted = false;
  460. if (AttachmentPt != 0 && AttachmentPt != objatt.GetAttachmentPoint())
  461. tainted = true;
  462. AttachObject(remoteClient, objatt.LocalId, AttachmentPt, Quaternion.Identity, objatt.AbsolutePosition, false);
  463. //objatt.ScheduleGroupForFullUpdate();
  464. if (tainted)
  465. objatt.HasGroupChanged = true;
  466. // Fire after attach, so we don't get messy perms dialogs
  467. // 3 == AttachedRez
  468. objatt.CreateScriptInstances(0, true, m_parentScene.DefaultScriptEngine, 3);
  469. // Do this last so that event listeners have access to all the effects of the attachment
  470. m_parentScene.EventManager.TriggerOnAttach(objatt.LocalId, itemID, remoteClient.AgentId);
  471. }
  472. else
  473. {
  474. m_log.WarnFormat(
  475. "[SCENE GRAPH]: Could not retrieve item {0} for attaching to avatar {1} at point {2}",
  476. itemID, remoteClient.Name, AttachmentPt);
  477. }
  478. return objatt;
  479. }
  480. // What makes this method odd and unique is it tries to detach using an UUID.... Yay for standards.
  481. // To LocalId or UUID, *THAT* is the question. How now Brown UUID??
  482. public void DetachSingleAttachmentToInv(UUID itemID, IClientAPI remoteClient)
  483. {
  484. if (itemID == UUID.Zero) // If this happened, someone made a mistake....
  485. return;
  486. // We can NOT use the dictionries here, as we are looking
  487. // for an entity by the fromAssetID, which is NOT the prim UUID
  488. //
  489. List<EntityBase> detachEntities = GetEntities();
  490. SceneObjectGroup group;
  491. foreach (EntityBase entity in detachEntities)
  492. {
  493. if (entity is SceneObjectGroup)
  494. {
  495. group = (SceneObjectGroup)entity;
  496. if (group.GetFromItemID() == itemID)
  497. {
  498. m_parentScene.SendAttachEvent(group.LocalId, itemID, UUID.Zero);
  499. group.DetachToInventoryPrep();
  500. m_log.Debug("[DETACH]: Saving attachpoint: " +
  501. ((uint)group.GetAttachmentPoint()).ToString());
  502. m_parentScene.UpdateKnownItem(remoteClient, group,
  503. group.GetFromItemID(), group.OwnerID);
  504. m_parentScene.DeleteSceneObject(group, false);
  505. return;
  506. }
  507. }
  508. }
  509. }
  510. /// <summary>
  511. /// Attach a scene object to an avatar.
  512. /// </summary>
  513. /// <param name="remoteClient"></param>
  514. /// <param name="objectLocalID"></param>
  515. /// <param name="AttachmentPt"></param>
  516. /// <param name="rot"></param>
  517. /// <param name="attachPos"></param>
  518. /// <param name="silent"></param>
  519. /// <returns>true if the attachment was successful, false otherwise</returns>
  520. protected internal bool AttachObject(
  521. IClientAPI remoteClient, uint objectLocalID, uint AttachmentPt, Quaternion rot, Vector3 attachPos, bool silent)
  522. {
  523. SceneObjectGroup group = GetGroupByPrim(objectLocalID);
  524. if (group != null)
  525. {
  526. if (m_parentScene.Permissions.CanTakeObject(group.UUID, remoteClient.AgentId))
  527. {
  528. // If the attachment point isn't the same as the one previously used
  529. // set it's offset position = 0 so that it appears on the attachment point
  530. // and not in a weird location somewhere unknown.
  531. if (AttachmentPt != 0 && AttachmentPt != (uint)group.GetAttachmentPoint())
  532. {
  533. attachPos = Vector3.Zero;
  534. }
  535. // AttachmentPt 0 means the client chose to 'wear' the attachment.
  536. if (AttachmentPt == 0)
  537. {
  538. // Check object for stored attachment point
  539. AttachmentPt = (uint)group.GetAttachmentPoint();
  540. }
  541. // if we still didn't find a suitable attachment point.......
  542. if (AttachmentPt == 0)
  543. {
  544. // Stick it on left hand with Zero Offset from the attachment point.
  545. AttachmentPt = (uint)AttachmentPoint.LeftHand;
  546. attachPos = Vector3.Zero;
  547. }
  548. group.SetAttachmentPoint((byte)AttachmentPt);
  549. group.AbsolutePosition = attachPos;
  550. // Saves and gets itemID
  551. UUID itemId;
  552. if (group.GetFromItemID() == UUID.Zero)
  553. {
  554. m_parentScene.attachObjectAssetStore(remoteClient, group, remoteClient.AgentId, out itemId);
  555. }
  556. else
  557. {
  558. itemId = group.GetFromItemID();
  559. }
  560. m_parentScene.AttachObject(remoteClient, AttachmentPt, itemId, group);
  561. group.AttachToAgent(remoteClient.AgentId, AttachmentPt, attachPos, silent);
  562. // In case it is later dropped again, don't let
  563. // it get cleaned up
  564. //
  565. group.RootPart.RemFlag(PrimFlags.TemporaryOnRez);
  566. group.HasGroupChanged = false;
  567. }
  568. else
  569. {
  570. remoteClient.SendAgentAlertMessage("You don't have sufficient permissions to attach this object", false);
  571. return false;
  572. }
  573. }
  574. else
  575. {
  576. m_log.DebugFormat("[SCENE GRAPH]: AttachObject found no such scene object {0}", objectLocalID);
  577. return false;
  578. }
  579. return true;
  580. }
  581. protected internal ScenePresence CreateAndAddChildScenePresence(IClientAPI client, AvatarAppearance appearance)
  582. {
  583. ScenePresence newAvatar = null;
  584. newAvatar = new ScenePresence(client, m_parentScene, m_regInfo, appearance);
  585. newAvatar.IsChildAgent = true;
  586. AddScenePresence(newAvatar);
  587. return newAvatar;
  588. }
  589. /// <summary>
  590. /// Add a presence to the scene
  591. /// </summary>
  592. /// <param name="presence"></param>
  593. protected internal void AddScenePresence(ScenePresence presence)
  594. {
  595. bool child = presence.IsChildAgent;
  596. if (child)
  597. {
  598. m_numChildAgents++;
  599. }
  600. else
  601. {
  602. m_numRootAgents++;
  603. presence.AddToPhysicalScene(false);
  604. }
  605. Entities[presence.UUID] = presence;
  606. lock (m_scenePresences)
  607. {
  608. if (!m_scenePresences.ContainsKey(presence.UUID))
  609. {
  610. m_scenePresences.Add(presence.UUID, presence);
  611. // Create a new array of ScenePresence references
  612. int oldLength = m_scenePresenceArray.Length;
  613. ScenePresence[] newArray = new ScenePresence[oldLength + 1];
  614. Array.Copy(m_scenePresenceArray, newArray, oldLength);
  615. newArray[oldLength] = presence;
  616. m_scenePresenceArray = newArray;
  617. }
  618. else
  619. {
  620. m_scenePresences[presence.UUID] = presence;
  621. // Do a linear search through the array of ScenePresence references
  622. // and update the modified entry
  623. for (int i = 0; i < m_scenePresenceArray.Length; i++)
  624. {
  625. if (m_scenePresenceArray[i].UUID == presence.UUID)
  626. {
  627. m_scenePresenceArray[i] = presence;
  628. break;
  629. }
  630. }
  631. }
  632. }
  633. }
  634. /// <summary>
  635. /// Remove a presence from the scene
  636. /// </summary>
  637. protected internal void RemoveScenePresence(UUID agentID)
  638. {
  639. if (!Entities.Remove(agentID))
  640. {
  641. m_log.WarnFormat(
  642. "[SCENE] Tried to remove non-existent scene presence with agent ID {0} from scene Entities list",
  643. agentID);
  644. }
  645. lock (m_scenePresences)
  646. {
  647. if (m_scenePresences.Remove(agentID))
  648. {
  649. // Copy all of the elements from the previous array
  650. // into the new array except the removed element
  651. int oldLength = m_scenePresenceArray.Length;
  652. ScenePresence[] newArray = new ScenePresence[oldLength - 1];
  653. int j = 0;
  654. for (int i = 0; i < m_scenePresenceArray.Length; i++)
  655. {
  656. ScenePresence presence = m_scenePresenceArray[i];
  657. if (presence.UUID != agentID)
  658. {
  659. newArray[j] = presence;
  660. ++j;
  661. }
  662. }
  663. m_scenePresenceArray = newArray;
  664. }
  665. else
  666. {
  667. m_log.WarnFormat("[SCENE] Tried to remove non-existent scene presence with agent ID {0} from scene ScenePresences list", agentID);
  668. }
  669. }
  670. }
  671. protected internal void SwapRootChildAgent(bool direction_RC_CR_T_F)
  672. {
  673. if (direction_RC_CR_T_F)
  674. {
  675. m_numRootAgents--;
  676. m_numChildAgents++;
  677. }
  678. else
  679. {
  680. m_numChildAgents--;
  681. m_numRootAgents++;
  682. }
  683. }
  684. public void removeUserCount(bool TypeRCTF)
  685. {
  686. if (TypeRCTF)
  687. {
  688. m_numRootAgents--;
  689. }
  690. else
  691. {
  692. m_numChildAgents--;
  693. }
  694. }
  695. public void RecalculateStats()
  696. {
  697. ScenePresence[] presences = GetScenePresences();
  698. int rootcount = 0;
  699. int childcount = 0;
  700. for (int i = 0; i < presences.Length; i++)
  701. {
  702. ScenePresence user = presences[i];
  703. if (user.IsChildAgent)
  704. ++childcount;
  705. else
  706. ++rootcount;
  707. }
  708. m_numRootAgents = rootcount;
  709. m_numChildAgents = childcount;
  710. }
  711. public int GetChildAgentCount()
  712. {
  713. // some network situations come in where child agents get closed twice.
  714. if (m_numChildAgents < 0)
  715. {
  716. m_numChildAgents = 0;
  717. }
  718. return m_numChildAgents;
  719. }
  720. public int GetRootAgentCount()
  721. {
  722. return m_numRootAgents;
  723. }
  724. public int GetTotalObjectsCount()
  725. {
  726. return m_numPrim;
  727. }
  728. public int GetActiveObjectsCount()
  729. {
  730. return m_physicalPrim;
  731. }
  732. public int GetActiveScriptsCount()
  733. {
  734. return m_activeScripts;
  735. }
  736. public int GetScriptLPS()
  737. {
  738. int returnval = m_scriptLPS;
  739. m_scriptLPS = 0;
  740. return returnval;
  741. }
  742. #endregion
  743. #region Get Methods
  744. /// <summary>
  745. /// Request a List of all scene presences in this scene. This is a new list, so no
  746. /// locking is required to iterate over it.
  747. /// </summary>
  748. /// <returns></returns>
  749. protected internal ScenePresence[] GetScenePresences()
  750. {
  751. return m_scenePresenceArray;
  752. }
  753. protected internal List<ScenePresence> GetAvatars()
  754. {
  755. List<ScenePresence> result =
  756. GetScenePresences(delegate(ScenePresence scenePresence) { return !scenePresence.IsChildAgent; });
  757. return result;
  758. }
  759. /// <summary>
  760. /// Get the controlling client for the given avatar, if there is one.
  761. ///
  762. /// FIXME: The only user of the method right now is Caps.cs, in order to resolve a client API since it can't
  763. /// use the ScenePresence. This could be better solved in a number of ways - we could establish an
  764. /// OpenSim.Framework.IScenePresence, or move the caps code into a region package (which might be the more
  765. /// suitable solution).
  766. /// </summary>
  767. /// <param name="agentId"></param>
  768. /// <returns>null if either the avatar wasn't in the scene, or
  769. /// they do not have a controlling client</returns>
  770. /// <remarks>this used to be protected internal, but that
  771. /// prevents CapabilitiesModule from accessing it</remarks>
  772. public IClientAPI GetControllingClient(UUID agentId)
  773. {
  774. ScenePresence presence = GetScenePresence(agentId);
  775. if (presence != null)
  776. {
  777. return presence.ControllingClient;
  778. }
  779. return null;
  780. }
  781. /// <summary>
  782. /// Request a filtered list of m_scenePresences in this World
  783. /// </summary>
  784. /// <returns></returns>
  785. protected internal List<ScenePresence> GetScenePresences(FilterAvatarList filter)
  786. {
  787. // No locking of scene presences here since we're passing back a list...
  788. List<ScenePresence> result = new List<ScenePresence>();
  789. ScenePresence[] scenePresences = GetScenePresences();
  790. for (int i = 0; i < scenePresences.Length; i++)
  791. {
  792. ScenePresence avatar = scenePresences[i];
  793. if (filter(avatar))
  794. result.Add(avatar);
  795. }
  796. return result;
  797. }
  798. /// <summary>
  799. /// Request a scene presence by UUID
  800. /// </summary>
  801. /// <param name="avatarID"></param>
  802. /// <returns>null if the agent was not found</returns>
  803. protected internal ScenePresence GetScenePresence(UUID agentID)
  804. {
  805. ScenePresence sp;
  806. lock (m_scenePresences)
  807. {
  808. m_scenePresences.TryGetValue(agentID, out sp);
  809. }
  810. return sp;
  811. }
  812. /// <summary>
  813. /// Get a scene object group that contains the prim with the given local id
  814. /// </summary>
  815. /// <param name="localID"></param>
  816. /// <returns>null if no scene object group containing that prim is found</returns>
  817. public SceneObjectGroup GetGroupByPrim(uint localID)
  818. {
  819. if (Entities.ContainsKey(localID))
  820. return Entities[localID] as SceneObjectGroup;
  821. //m_log.DebugFormat("Entered GetGroupByPrim with localID {0}", localID);
  822. SceneObjectGroup sog;
  823. lock (SceneObjectGroupsByLocalID)
  824. {
  825. if (SceneObjectGroupsByLocalID.TryGetValue(localID, out sog))
  826. {
  827. return sog;
  828. }
  829. }
  830. List<EntityBase> EntityList = GetEntities();
  831. foreach (EntityBase ent in EntityList)
  832. {
  833. //m_log.DebugFormat("Looking at entity {0}", ent.UUID);
  834. if (ent is SceneObjectGroup)
  835. {
  836. if (((SceneObjectGroup)ent).HasChildPrim(localID))
  837. {
  838. sog = (SceneObjectGroup)ent;
  839. lock (SceneObjectGroupsByLocalID)
  840. {
  841. SceneObjectGroupsByLocalID[localID] = sog;
  842. }
  843. return sog;
  844. }
  845. }
  846. }
  847. return null;
  848. }
  849. /// <summary>
  850. /// Get a scene object group that contains the prim with the given uuid
  851. /// </summary>
  852. /// <param name="fullID"></param>
  853. /// <returns>null if no scene object group containing that prim is found</returns>
  854. private SceneObjectGroup GetGroupByPrim(UUID fullID)
  855. {
  856. SceneObjectGroup sog;
  857. lock (SceneObjectGroupsByFullID)
  858. {
  859. if (SceneObjectGroupsByFullID.TryGetValue(fullID, out sog))
  860. {
  861. return sog;
  862. }
  863. }
  864. List<EntityBase> EntityList = GetEntities();
  865. foreach (EntityBase ent in EntityList)
  866. {
  867. if (ent is SceneObjectGroup)
  868. {
  869. if (((SceneObjectGroup)ent).HasChildPrim(fullID))
  870. {
  871. sog = (SceneObjectGroup)ent;
  872. lock (SceneObjectGroupsByFullID)
  873. {
  874. SceneObjectGroupsByFullID[fullID] = sog;
  875. }
  876. return sog;
  877. }
  878. }
  879. }
  880. return null;
  881. }
  882. protected internal EntityIntersection GetClosestIntersectingPrim(Ray hray, bool frontFacesOnly, bool faceCenters)
  883. {
  884. // Primitive Ray Tracing
  885. float closestDistance = 280f;
  886. EntityIntersection result = new EntityIntersection();
  887. List<EntityBase> EntityList = GetEntities();
  888. foreach (EntityBase ent in EntityList)
  889. {
  890. if (ent is SceneObjectGroup)
  891. {
  892. SceneObjectGroup reportingG = (SceneObjectGroup)ent;
  893. EntityIntersection inter = reportingG.TestIntersection(hray, frontFacesOnly, faceCenters);
  894. if (inter.HitTF && inter.distance < closestDistance)
  895. {
  896. closestDistance = inter.distance;
  897. result = inter;
  898. }
  899. }
  900. }
  901. return result;
  902. }
  903. /// <summary>
  904. /// Get a part contained in this scene.
  905. /// </summary>
  906. /// <param name="localID"></param>
  907. /// <returns>null if the part was not found</returns>
  908. protected internal SceneObjectPart GetSceneObjectPart(uint localID)
  909. {
  910. SceneObjectGroup group = GetGroupByPrim(localID);
  911. if (group == null)
  912. return null;
  913. return group.GetChildPart(localID);
  914. }
  915. /// <summary>
  916. /// Get a named prim contained in this scene (will return the first
  917. /// found, if there are more than one prim with the same name)
  918. /// </summary>
  919. /// <param name="name"></param>
  920. /// <returns>null if the part was not found</returns>
  921. protected internal SceneObjectPart GetSceneObjectPart(string name)
  922. {
  923. List<EntityBase> EntityList = GetEntities();
  924. // FIXME: use a dictionary here
  925. foreach (EntityBase ent in EntityList)
  926. {
  927. if (ent is SceneObjectGroup)
  928. {
  929. foreach (SceneObjectPart p in ((SceneObjectGroup) ent).GetParts())
  930. {
  931. if (p.Name == name)
  932. {
  933. return p;
  934. }
  935. }
  936. }
  937. }
  938. return null;
  939. }
  940. /// <summary>
  941. /// Get a part contained in this scene.
  942. /// </summary>
  943. /// <param name="fullID"></param>
  944. /// <returns>null if the part was not found</returns>
  945. protected internal SceneObjectPart GetSceneObjectPart(UUID fullID)
  946. {
  947. SceneObjectGroup group = GetGroupByPrim(fullID);
  948. if (group == null)
  949. return null;
  950. return group.GetChildPart(fullID);
  951. }
  952. protected internal bool TryGetAvatar(UUID avatarId, out ScenePresence avatar)
  953. {
  954. lock (m_scenePresences)
  955. return m_scenePresences.TryGetValue(avatarId, out avatar);
  956. }
  957. protected internal bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  958. {
  959. ScenePresence[] presences = GetScenePresences();
  960. for (int i = 0; i < presences.Length; i++)
  961. {
  962. ScenePresence presence = presences[i];
  963. if (!presence.IsChildAgent)
  964. {
  965. if (String.Compare(avatarName, presence.ControllingClient.Name, true) == 0)
  966. {
  967. avatar = presence;
  968. return true;
  969. }
  970. }
  971. }
  972. avatar = null;
  973. return false;
  974. }
  975. /// <summary>
  976. /// Returns a list of the entities in the scene. This is a new list so no locking is required to iterate over
  977. /// it
  978. /// </summary>
  979. /// <returns></returns>
  980. protected internal List<EntityBase> GetEntities()
  981. {
  982. return Entities.GetEntities();
  983. }
  984. public Dictionary<uint, float> GetTopScripts()
  985. {
  986. Dictionary<uint, float> topScripts = new Dictionary<uint, float>();
  987. List<EntityBase> EntityList = GetEntities();
  988. int limit = 0;
  989. foreach (EntityBase ent in EntityList)
  990. {
  991. if (ent is SceneObjectGroup)
  992. {
  993. SceneObjectGroup grp = (SceneObjectGroup)ent;
  994. if ((grp.RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Scripted) != 0)
  995. {
  996. if (grp.scriptScore >= 0.01)
  997. {
  998. topScripts.Add(grp.LocalId, grp.scriptScore);
  999. limit++;
  1000. if (limit >= 100)
  1001. {
  1002. break;
  1003. }
  1004. }
  1005. grp.scriptScore = 0;
  1006. }
  1007. }
  1008. }
  1009. return topScripts;
  1010. }
  1011. #endregion
  1012. #region Other Methods
  1013. protected internal void physicsBasedCrash()
  1014. {
  1015. handlerPhysicsCrash = UnRecoverableError;
  1016. if (handlerPhysicsCrash != null)
  1017. {
  1018. handlerPhysicsCrash();
  1019. }
  1020. }
  1021. protected internal UUID ConvertLocalIDToFullID(uint localID)
  1022. {
  1023. SceneObjectGroup group = GetGroupByPrim(localID);
  1024. if (group != null)
  1025. return group.GetPartsFullID(localID);
  1026. else
  1027. return UUID.Zero;
  1028. }
  1029. protected internal void ForEachSOG(Action<SceneObjectGroup> action)
  1030. {
  1031. List<SceneObjectGroup> objlist = new List<SceneObjectGroup>(SceneObjectGroupsByFullID.Values);
  1032. foreach (SceneObjectGroup obj in objlist)
  1033. {
  1034. try
  1035. {
  1036. action(obj);
  1037. }
  1038. catch (Exception e)
  1039. {
  1040. // Catch it and move on. This includes situations where splist has inconsistent info
  1041. m_log.WarnFormat("[SCENE]: Problem processing action in ForEachSOG: ", e.Message);
  1042. }
  1043. }
  1044. }
  1045. #endregion
  1046. #region Client Event handlers
  1047. /// <summary>
  1048. ///
  1049. /// </summary>
  1050. /// <param name="localID"></param>
  1051. /// <param name="scale"></param>
  1052. /// <param name="remoteClient"></param>
  1053. protected internal void UpdatePrimScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  1054. {
  1055. SceneObjectGroup group = GetGroupByPrim(localID);
  1056. if (group != null)
  1057. {
  1058. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1059. {
  1060. group.Resize(scale, localID);
  1061. }
  1062. }
  1063. }
  1064. protected internal void UpdatePrimGroupScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  1065. {
  1066. SceneObjectGroup group = GetGroupByPrim(localID);
  1067. if (group != null)
  1068. {
  1069. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1070. {
  1071. group.GroupResize(scale, localID);
  1072. }
  1073. }
  1074. }
  1075. /// <summary>
  1076. /// This handles the nifty little tool tip that you get when you drag your mouse over an object
  1077. /// Send to the Object Group to process. We don't know enough to service the request
  1078. /// </summary>
  1079. /// <param name="remoteClient"></param>
  1080. /// <param name="AgentID"></param>
  1081. /// <param name="RequestFlags"></param>
  1082. /// <param name="ObjectID"></param>
  1083. protected internal void RequestObjectPropertiesFamily(
  1084. IClientAPI remoteClient, UUID AgentID, uint RequestFlags, UUID ObjectID)
  1085. {
  1086. SceneObjectGroup group = GetGroupByPrim(ObjectID);
  1087. if (group != null)
  1088. {
  1089. group.ServiceObjectPropertiesFamilyRequest(remoteClient, AgentID, RequestFlags);
  1090. }
  1091. }
  1092. /// <summary>
  1093. ///
  1094. /// </summary>
  1095. /// <param name="localID"></param>
  1096. /// <param name="rot"></param>
  1097. /// <param name="remoteClient"></param>
  1098. protected internal void UpdatePrimSingleRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1099. {
  1100. SceneObjectGroup group = GetGroupByPrim(localID);
  1101. if (group != null)
  1102. {
  1103. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1104. {
  1105. group.UpdateSingleRotation(rot, localID);
  1106. }
  1107. }
  1108. }
  1109. /// <summary>
  1110. ///
  1111. /// </summary>
  1112. /// <param name="localID"></param>
  1113. /// <param name="rot"></param>
  1114. /// <param name="remoteClient"></param>
  1115. protected internal void UpdatePrimSingleRotationPosition(uint localID, Quaternion rot, Vector3 pos, IClientAPI remoteClient)
  1116. {
  1117. SceneObjectGroup group = GetGroupByPrim(localID);
  1118. if (group != null)
  1119. {
  1120. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1121. {
  1122. group.UpdateSingleRotation(rot,pos, localID);
  1123. }
  1124. }
  1125. }
  1126. /// <summary>
  1127. ///
  1128. /// </summary>
  1129. /// <param name="localID"></param>
  1130. /// <param name="rot"></param>
  1131. /// <param name="remoteClient"></param>
  1132. protected internal void UpdatePrimRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1133. {
  1134. SceneObjectGroup group = GetGroupByPrim(localID);
  1135. if (group != null)
  1136. {
  1137. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1138. {
  1139. group.UpdateGroupRotationR(rot);
  1140. }
  1141. }
  1142. }
  1143. /// <summary>
  1144. ///
  1145. /// </summary>
  1146. /// <param name="localID"></param>
  1147. /// <param name="pos"></param>
  1148. /// <param name="rot"></param>
  1149. /// <param name="remoteClient"></param>
  1150. protected internal void UpdatePrimRotation(uint localID, Vector3 pos, Quaternion rot, IClientAPI remoteClient)
  1151. {
  1152. SceneObjectGroup group = GetGroupByPrim(localID);
  1153. if (group != null)
  1154. {
  1155. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1156. {
  1157. group.UpdateGroupRotationPR(pos, rot);
  1158. }
  1159. }
  1160. }
  1161. /// <summary>
  1162. /// Update the position of the given part
  1163. /// </summary>
  1164. /// <param name="localID"></param>
  1165. /// <param name="pos"></param>
  1166. /// <param name="remoteClient"></param>
  1167. protected internal void UpdatePrimSinglePosition(uint localID, Vector3 pos, IClientAPI remoteClient)
  1168. {
  1169. SceneObjectGroup group = GetGroupByPrim(localID);
  1170. if (group != null)
  1171. {
  1172. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId) || group.IsAttachment)
  1173. {
  1174. group.UpdateSinglePosition(pos, localID);
  1175. }
  1176. }
  1177. }
  1178. /// <summary>
  1179. /// Update the position of the given part
  1180. /// </summary>
  1181. /// <param name="localID"></param>
  1182. /// <param name="pos"></param>
  1183. /// <param name="remoteClient"></param>
  1184. protected internal void UpdatePrimPosition(uint localID, Vector3 pos, IClientAPI remoteClient)
  1185. {
  1186. SceneObjectGroup group = GetGroupByPrim(localID);
  1187. if (group != null)
  1188. {
  1189. // Vector3 oldPos = group.AbsolutePosition;
  1190. if (group.IsAttachment || (group.RootPart.Shape.PCode == 9 && group.RootPart.Shape.State != 0))
  1191. {
  1192. // If this is an attachment, then we need to save the modified
  1193. // object back into the avatar's inventory. First we save the
  1194. // attachment point information, then we update the relative
  1195. // positioning (which caused this method to get driven in the
  1196. // first place. Then we have to mark the object as NOT an
  1197. // attachment. This is necessary in order to correctly save
  1198. // and retrieve GroupPosition information for the attachment.
  1199. // Then we save the asset back into the appropriate inventory
  1200. // entry. Finally, we restore the object's attachment status.
  1201. byte attachmentPoint = group.GetAttachmentPoint();
  1202. group.UpdateGroupPosition(pos);
  1203. group.RootPart.IsAttachment = false;
  1204. group.AbsolutePosition = group.RootPart.AttachedPos;
  1205. m_parentScene.UpdateKnownItem(remoteClient, group, group.GetFromItemID(), group.OwnerID);
  1206. group.SetAttachmentPoint(attachmentPoint);
  1207. }
  1208. else
  1209. {
  1210. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId) && m_parentScene.Permissions.CanObjectEntry(group.UUID, false, pos))
  1211. {
  1212. group.UpdateGroupPosition(pos);
  1213. }
  1214. }
  1215. }
  1216. }
  1217. /// <summary>
  1218. ///
  1219. /// </summary>
  1220. /// <param name="localID"></param>
  1221. /// <param name="texture"></param>
  1222. /// <param name="remoteClient"></param>
  1223. protected internal void UpdatePrimTexture(uint localID, byte[] texture, IClientAPI remoteClient)
  1224. {
  1225. SceneObjectGroup group = GetGroupByPrim(localID);
  1226. if (group != null)
  1227. {
  1228. if (m_parentScene.Permissions.CanEditObject(group.UUID,remoteClient.AgentId))
  1229. {
  1230. group.UpdateTextureEntry(localID, texture);
  1231. }
  1232. }
  1233. }
  1234. /// <summary>
  1235. ///
  1236. /// </summary>
  1237. /// <param name="localID"></param>
  1238. /// <param name="packet"></param>
  1239. /// <param name="remoteClient"></param>
  1240. /// This routine seems to get called when a user changes object settings in the viewer.
  1241. /// If some one can confirm that, please change the comment according.
  1242. protected internal void UpdatePrimFlags(uint localID, bool UsePhysics, bool IsTemporary, bool IsPhantom, IClientAPI remoteClient)
  1243. {
  1244. SceneObjectGroup group = GetGroupByPrim(localID);
  1245. if (group != null)
  1246. {
  1247. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1248. {
  1249. group.UpdatePrimFlags(localID, UsePhysics, IsTemporary, IsPhantom, false); // VolumeDetect can't be set via UI and will always be off when a change is made there
  1250. }
  1251. }
  1252. }
  1253. /// <summary>
  1254. /// Move the given object
  1255. /// </summary>
  1256. /// <param name="objectID"></param>
  1257. /// <param name="offset"></param>
  1258. /// <param name="pos"></param>
  1259. /// <param name="remoteClient"></param>
  1260. protected internal void MoveObject(UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  1261. {
  1262. SceneObjectGroup group = GetGroupByPrim(objectID);
  1263. if (group != null)
  1264. {
  1265. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  1266. {
  1267. group.GrabMovement(offset, pos, remoteClient);
  1268. }
  1269. // This is outside the above permissions condition
  1270. // so that if the object is locked the client moving the object
  1271. // get's it's position on the simulator even if it was the same as before
  1272. // This keeps the moving user's client in sync with the rest of the world.
  1273. group.SendGroupTerseUpdate();
  1274. }
  1275. }
  1276. /// <summary>
  1277. /// Start spinning the given object
  1278. /// </summary>
  1279. /// <param name="objectID"></param>
  1280. /// <param name="rotation"></param>
  1281. /// <param name="remoteClient"></param>
  1282. protected internal void SpinStart(UUID objectID, IClientAPI remoteClient)
  1283. {
  1284. SceneObjectGroup group = GetGroupByPrim(objectID);
  1285. if (group != null)
  1286. {
  1287. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  1288. {
  1289. group.SpinStart(remoteClient);
  1290. }
  1291. }
  1292. }
  1293. /// <summary>
  1294. /// Spin the given object
  1295. /// </summary>
  1296. /// <param name="objectID"></param>
  1297. /// <param name="rotation"></param>
  1298. /// <param name="remoteClient"></param>
  1299. protected internal void SpinObject(UUID objectID, Quaternion rotation, IClientAPI remoteClient)
  1300. {
  1301. SceneObjectGroup group = GetGroupByPrim(objectID);
  1302. if (group != null)
  1303. {
  1304. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  1305. {
  1306. group.SpinMovement(rotation, remoteClient);
  1307. }
  1308. // This is outside the above permissions condition
  1309. // so that if the object is locked the client moving the object
  1310. // get's it's position on the simulator even if it was the same as before
  1311. // This keeps the moving user's client in sync with the rest of the world.
  1312. group.SendGroupTerseUpdate();
  1313. }
  1314. }
  1315. /// <summary>
  1316. ///
  1317. /// </summary>
  1318. /// <param name="primLocalID"></param>
  1319. /// <param name="description"></param>
  1320. protected internal void PrimName(IClientAPI remoteClient, uint primLocalID, string name)
  1321. {
  1322. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1323. if (group != null)
  1324. {
  1325. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1326. {
  1327. group.SetPartName(Util.CleanString(name), primLocalID);
  1328. group.HasGroupChanged = true;
  1329. }
  1330. }
  1331. }
  1332. /// <summary>
  1333. ///
  1334. /// </summary>
  1335. /// <param name="primLocalID"></param>
  1336. /// <param name="description"></param>
  1337. protected internal void PrimDescription(IClientAPI remoteClient, uint primLocalID, string description)
  1338. {
  1339. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1340. if (group != null)
  1341. {
  1342. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1343. {
  1344. group.SetPartDescription(Util.CleanString(description), primLocalID);
  1345. group.HasGroupChanged = true;
  1346. }
  1347. }
  1348. }
  1349. protected internal void PrimClickAction(IClientAPI remoteClient, uint primLocalID, string clickAction)
  1350. {
  1351. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1352. if (group != null)
  1353. {
  1354. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1355. {
  1356. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primLocalID);
  1357. part.ClickAction = Convert.ToByte(clickAction);
  1358. group.HasGroupChanged = true;
  1359. }
  1360. }
  1361. }
  1362. protected internal void PrimMaterial(IClientAPI remoteClient, uint primLocalID, string material)
  1363. {
  1364. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1365. if (group != null)
  1366. {
  1367. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1368. {
  1369. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primLocalID);
  1370. part.Material = Convert.ToByte(material);
  1371. group.HasGroupChanged = true;
  1372. }
  1373. }
  1374. }
  1375. protected internal void UpdateExtraParam(UUID agentID, uint primLocalID, ushort type, bool inUse, byte[] data)
  1376. {
  1377. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1378. if (group != null)
  1379. {
  1380. if (m_parentScene.Permissions.CanEditObject(group.UUID,agentID))
  1381. {
  1382. group.UpdateExtraParam(primLocalID, type, inUse, data);
  1383. }
  1384. }
  1385. }
  1386. /// <summary>
  1387. ///
  1388. /// </summary>
  1389. /// <param name="primLocalID"></param>
  1390. /// <param name="shapeBlock"></param>
  1391. protected internal void UpdatePrimShape(UUID agentID, uint primLocalID, UpdateShapeArgs shapeBlock)
  1392. {
  1393. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1394. if (group != null)
  1395. {
  1396. if (m_parentScene.Permissions.CanEditObject(group.GetPartsFullID(primLocalID), agentID))
  1397. {
  1398. ObjectShapePacket.ObjectDataBlock shapeData = new ObjectShapePacket.ObjectDataBlock();
  1399. shapeData.ObjectLocalID = shapeBlock.ObjectLocalID;
  1400. shapeData.PathBegin = shapeBlock.PathBegin;
  1401. shapeData.PathCurve = shapeBlock.PathCurve;
  1402. shapeData.PathEnd = shapeBlock.PathEnd;
  1403. shapeData.PathRadiusOffset = shapeBlock.PathRadiusOffset;
  1404. shapeData.PathRevolutions = shapeBlock.PathRevolutions;
  1405. shapeData.PathScaleX = shapeBlock.PathScaleX;
  1406. shapeData.PathScaleY = shapeBlock.PathScaleY;
  1407. shapeData.PathShearX = shapeBlock.PathShearX;
  1408. shapeData.PathShearY = shapeBlock.PathShearY;
  1409. shapeData.PathSkew = shapeBlock.PathSkew;
  1410. shapeData.PathTaperX = shapeBlock.PathTaperX;
  1411. shapeData.PathTaperY = shapeBlock.PathTaperY;
  1412. shapeData.PathTwist = shapeBlock.PathTwist;
  1413. shapeData.PathTwistBegin = shapeBlock.PathTwistBegin;
  1414. shapeData.ProfileBegin = shapeBlock.ProfileBegin;
  1415. shapeData.ProfileCurve = shapeBlock.ProfileCurve;
  1416. shapeData.ProfileEnd = shapeBlock.ProfileEnd;
  1417. shapeData.ProfileHollow = shapeBlock.ProfileHollow;
  1418. group.UpdateShape(shapeData, primLocalID);
  1419. }
  1420. }
  1421. }
  1422. /// <summary>
  1423. /// Initial method invoked when we receive a link objects request from the client.
  1424. /// </summary>
  1425. /// <param name="client"></param>
  1426. /// <param name="parentPrim"></param>
  1427. /// <param name="childPrims"></param>
  1428. protected internal void LinkObjects(IClientAPI client, uint parentPrimId, List<uint> childPrimIds)
  1429. {
  1430. Monitor.Enter(m_updateLock);
  1431. try
  1432. {
  1433. SceneObjectGroup parentGroup = GetGroupByPrim(parentPrimId);
  1434. List<SceneObjectGroup> childGroups = new List<SceneObjectGroup>();
  1435. if (parentGroup != null)
  1436. {
  1437. // We do this in reverse to get the link order of the prims correct
  1438. for (int i = childPrimIds.Count - 1; i >= 0; i--)
  1439. {
  1440. SceneObjectGroup child = GetGroupByPrim(childPrimIds[i]);
  1441. if (child != null)
  1442. {
  1443. // Make sure no child prim is set for sale
  1444. // So that, on delink, no prims are unwittingly
  1445. // left for sale and sold off
  1446. child.RootPart.ObjectSaleType = 0;
  1447. child.RootPart.SalePrice = 10;
  1448. childGroups.Add(child);
  1449. }
  1450. }
  1451. }
  1452. else
  1453. {
  1454. return; // parent is null so not in this region
  1455. }
  1456. foreach (SceneObjectGroup child in childGroups)
  1457. {
  1458. parentGroup.LinkToGroup(child);
  1459. // this is here so physics gets updated!
  1460. // Don't remove! Bad juju! Stay away! or fix physics!
  1461. child.AbsolutePosition = child.AbsolutePosition;
  1462. }
  1463. // We need to explicitly resend the newly link prim's object properties since no other actions
  1464. // occur on link to invoke this elsewhere (such as object selection)
  1465. parentGroup.RootPart.AddFlag(PrimFlags.CreateSelected);
  1466. parentGroup.TriggerScriptChangedEvent(Changed.LINK);
  1467. parentGroup.HasGroupChanged = true;
  1468. parentGroup.ScheduleGroupForFullUpdate();
  1469. // if (client != null)
  1470. // {
  1471. // parentGroup.GetProperties(client);
  1472. // }
  1473. // else
  1474. // {
  1475. // foreach (ScenePresence p in GetScenePresences())
  1476. // {
  1477. // parentGroup.GetProperties(p.ControllingClient);
  1478. // }
  1479. // }
  1480. }
  1481. finally
  1482. {
  1483. Monitor.Exit(m_updateLock);
  1484. }
  1485. }
  1486. /// <summary>
  1487. /// Delink a linkset
  1488. /// </summary>
  1489. /// <param name="prims"></param>
  1490. protected internal void DelinkObjects(List<uint> primIds)
  1491. {
  1492. DelinkObjects(primIds, true);
  1493. }
  1494. protected internal void DelinkObjects(List<uint> primIds, bool sendEvents)
  1495. {
  1496. Monitor.Enter(m_updateLock);
  1497. try
  1498. {
  1499. List<SceneObjectPart> childParts = new List<SceneObjectPart>();
  1500. List<SceneObjectPart> rootParts = new List<SceneObjectPart>();
  1501. List<SceneObjectGroup> affectedGroups = new List<SceneObjectGroup>();
  1502. // Look them all up in one go, since that is comparatively expensive
  1503. //
  1504. foreach (uint primID in primIds)
  1505. {
  1506. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primID);
  1507. if (part != null)
  1508. {
  1509. if (part.ParentGroup.Children.Count != 1) // Skip single
  1510. {
  1511. if (part.LinkNum < 2) // Root
  1512. rootParts.Add(part);
  1513. else
  1514. childParts.Add(part);
  1515. SceneObjectGroup group = part.ParentGroup;
  1516. if (!affectedGroups.Contains(group))
  1517. affectedGroups.Add(group);
  1518. }
  1519. }
  1520. else
  1521. {
  1522. m_log.ErrorFormat("Viewer requested unlink of nonexistent part {0}", primID);
  1523. }
  1524. }
  1525. foreach (SceneObjectPart child in childParts)
  1526. {
  1527. // Unlink all child parts from their groups
  1528. //
  1529. child.ParentGroup.DelinkFromGroup(child, sendEvents);
  1530. }
  1531. foreach (SceneObjectPart root in rootParts)
  1532. {
  1533. // In most cases, this will run only one time, and the prim
  1534. // will be a solo prim
  1535. // However, editing linked parts and unlinking may be different
  1536. //
  1537. SceneObjectGroup group = root.ParentGroup;
  1538. List<SceneObjectPart> newSet = new List<SceneObjectPart>(group.Children.Values);
  1539. int numChildren = group.Children.Count;
  1540. // If there are prims left in a link set, but the root is
  1541. // slated for unlink, we need to do this
  1542. //
  1543. if (numChildren != 1)
  1544. {
  1545. // Unlink the remaining set
  1546. //
  1547. bool sendEventsToRemainder = true;
  1548. if (numChildren > 1)
  1549. sendEventsToRemainder = false;
  1550. foreach (SceneObjectPart p in newSet)
  1551. {
  1552. if (p != group.RootPart)
  1553. group.DelinkFromGroup(p, sendEventsToRemainder);
  1554. }
  1555. // If there is more than one prim remaining, we
  1556. // need to re-link
  1557. //
  1558. if (numChildren > 2)
  1559. {
  1560. // Remove old root
  1561. //
  1562. if (newSet.Contains(root))
  1563. newSet.Remove(root);
  1564. // Preserve link ordering
  1565. //
  1566. newSet.Sort(delegate (SceneObjectPart a, SceneObjectPart b)
  1567. {
  1568. return a.LinkNum.CompareTo(b.LinkNum);
  1569. });
  1570. // Determine new root
  1571. //
  1572. SceneObjectPart newRoot = newSet[0];
  1573. newSet.RemoveAt(0);
  1574. List<uint> linkIDs = new List<uint>();
  1575. foreach (SceneObjectPart newChild in newSet)
  1576. {
  1577. newChild.UpdateFlag = 0;
  1578. linkIDs.Add(newChild.LocalId);
  1579. }
  1580. LinkObjects(null, newRoot.LocalId, linkIDs);
  1581. if (!affectedGroups.Contains(newRoot.ParentGroup))
  1582. affectedGroups.Add(newRoot.ParentGroup);
  1583. }
  1584. }
  1585. }
  1586. // Finally, trigger events in the roots
  1587. //
  1588. foreach (SceneObjectGroup g in affectedGroups)
  1589. {
  1590. g.TriggerScriptChangedEvent(Changed.LINK);
  1591. g.HasGroupChanged = true; // Persist
  1592. g.ScheduleGroupForFullUpdate();
  1593. }
  1594. }
  1595. finally
  1596. {
  1597. Monitor.Exit(m_updateLock);
  1598. }
  1599. }
  1600. protected internal void MakeObjectSearchable(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  1601. {
  1602. UUID user = remoteClient.AgentId;
  1603. UUID objid = UUID.Zero;
  1604. SceneObjectPart obj = null;
  1605. List<EntityBase> EntityList = GetEntities();
  1606. foreach (EntityBase ent in EntityList)
  1607. {
  1608. if (ent is SceneObjectGroup)
  1609. {
  1610. foreach (KeyValuePair<UUID, SceneObjectPart> subent in ((SceneObjectGroup)ent).Children)
  1611. {
  1612. if (subent.Value.LocalId == localID)
  1613. {
  1614. objid = subent.Key;
  1615. obj = subent.Value;
  1616. }
  1617. }
  1618. }
  1619. }
  1620. //Protip: In my day, we didn't call them searchable objects, we called them limited point-to-point joints
  1621. //aka ObjectFlags.JointWheel = IncludeInSearch
  1622. //Permissions model: Object can be REMOVED from search IFF:
  1623. // * User owns object
  1624. //use CanEditObject
  1625. //Object can be ADDED to search IFF:
  1626. // * User owns object
  1627. // * Asset/DRM permission bit "modify" is enabled
  1628. //use CanEditObjectPosition
  1629. // libomv will complain about PrimFlags.JointWheel being
  1630. // deprecated, so we
  1631. #pragma warning disable 0612
  1632. if (IncludeInSearch && m_parentScene.Permissions.CanEditObject(objid, user))
  1633. {
  1634. obj.ParentGroup.RootPart.AddFlag(PrimFlags.JointWheel);
  1635. obj.ParentGroup.HasGroupChanged = true;
  1636. }
  1637. else if (!IncludeInSearch && m_parentScene.Permissions.CanMoveObject(objid,user))
  1638. {
  1639. obj.ParentGroup.RootPart.RemFlag(PrimFlags.JointWheel);
  1640. obj.ParentGroup.HasGroupChanged = true;
  1641. }
  1642. #pragma warning restore 0612
  1643. }
  1644. /// <summary>
  1645. /// Duplicate the given object, Fire and Forget, No rotation, no return wrapper
  1646. /// </summary>
  1647. /// <param name="originalPrim"></param>
  1648. /// <param name="offset"></param>
  1649. /// <param name="flags"></param>
  1650. protected internal void DuplicateObject(uint originalPrim, Vector3 offset, uint flags, UUID AgentID, UUID GroupID)
  1651. {
  1652. //m_log.DebugFormat("[SCENE]: Duplication of object {0} at offset {1} requested by agent {2}", originalPrim, offset, AgentID);
  1653. // SceneObjectGroup dupe = DuplicateObject(originalPrim, offset, flags, AgentID, GroupID, Quaternion.Zero);
  1654. DuplicateObject(originalPrim, offset, flags, AgentID, GroupID, Quaternion.Identity);
  1655. }
  1656. /// <summary>
  1657. /// Duplicate the given object.
  1658. /// </summary>
  1659. /// <param name="originalPrim"></param>
  1660. /// <param name="offset"></param>
  1661. /// <param name="flags"></param>
  1662. protected internal SceneObjectGroup DuplicateObject(uint originalPrimID, Vector3 offset, uint flags, UUID AgentID, UUID GroupID, Quaternion rot)
  1663. {
  1664. //m_log.DebugFormat("[SCENE]: Duplication of object {0} at offset {1} requested by agent {2}", originalPrim, offset, AgentID);
  1665. SceneObjectGroup original = GetGroupByPrim(originalPrimID);
  1666. if (original != null)
  1667. {
  1668. if (m_parentScene.Permissions.CanDuplicateObject(original.Children.Count, original.UUID, AgentID, original.AbsolutePosition))
  1669. {
  1670. SceneObjectGroup copy = original.Copy(AgentID, GroupID, true);
  1671. copy.AbsolutePosition = copy.AbsolutePosition + offset;
  1672. Entities.Add(copy);
  1673. // Since we copy from a source group that is in selected
  1674. // state, but the copy is shown deselected in the viewer,
  1675. // We need to clear the selection flag here, else that
  1676. // prim never gets persisted at all. The client doesn't
  1677. // think it's selected, so it will never send a deselect...
  1678. copy.IsSelected = false;
  1679. m_numPrim += copy.Children.Count;
  1680. if (rot != Quaternion.Identity)
  1681. {
  1682. copy.UpdateGroupRotationR(rot);
  1683. }
  1684. copy.CreateScriptInstances(0, false, m_parentScene.DefaultScriptEngine, 0);
  1685. copy.HasGroupChanged = true;
  1686. copy.ScheduleGroupForFullUpdate();
  1687. // required for physics to update it's position
  1688. copy.AbsolutePosition = copy.AbsolutePosition;
  1689. if (OnObjectDuplicate != null)
  1690. OnObjectDuplicate(original, copy);
  1691. return copy;
  1692. }
  1693. }
  1694. else
  1695. {
  1696. m_log.WarnFormat("[SCENE]: Attempted to duplicate nonexistant prim id {0}", GroupID);
  1697. }
  1698. return null;
  1699. }
  1700. /// <summary>
  1701. /// Calculates the distance between two Vector3s
  1702. /// </summary>
  1703. /// <param name="v1"></param>
  1704. /// <param name="v2"></param>
  1705. /// <returns></returns>
  1706. protected internal float Vector3Distance(Vector3 v1, Vector3 v2)
  1707. {
  1708. // We don't really need the double floating point precision...
  1709. // so casting it to a single
  1710. return
  1711. (float)
  1712. Math.Sqrt((v1.X - v2.X) * (v1.X - v2.X) + (v1.Y - v2.Y) * (v1.Y - v2.Y) + (v1.Z - v2.Z) * (v1.Z - v2.Z));
  1713. }
  1714. #endregion
  1715. }
  1716. }