SceneGraph.cs 76 KB

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