SceneGraph.cs 76 KB

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