SceneGraph.cs 75 KB

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