1
0

SceneGraph.cs 76 KB

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