1
0

SceneGraph.cs 75 KB

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