SceneGraph.cs 69 KB

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