SceneGraph.cs 66 KB

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