SceneGraph.cs 79 KB

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