1
0

SceneGraph.cs 75 KB

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