SceneGraph.cs 75 KB

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