SceneGraph.cs 70 KB

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