SceneGraph.cs 69 KB

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