SceneGraph.cs 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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.Environment.Types;
  35. using OpenSim.Region.Physics.Manager;
  36. namespace OpenSim.Region.Environment.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. protected internal 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();
  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 they do not have a controlling client</returns>
  674. protected internal IClientAPI GetControllingClient(UUID agentId)
  675. {
  676. ScenePresence presence = GetScenePresence(agentId);
  677. if (presence != null)
  678. {
  679. return presence.ControllingClient;
  680. }
  681. return null;
  682. }
  683. /// <summary>
  684. /// Request a filtered list of m_scenePresences in this World
  685. /// </summary>
  686. /// <returns></returns>
  687. protected internal List<ScenePresence> GetScenePresences(FilterAvatarList filter)
  688. {
  689. // No locking of scene presences here since we're passing back a list...
  690. List<ScenePresence> result = new List<ScenePresence>();
  691. List<ScenePresence> ScenePresencesList = GetScenePresences();
  692. foreach (ScenePresence avatar in ScenePresencesList)
  693. {
  694. if (filter(avatar))
  695. {
  696. result.Add(avatar);
  697. }
  698. }
  699. return result;
  700. }
  701. /// <summary>
  702. /// Request a scene presence by UUID
  703. /// </summary>
  704. /// <param name="avatarID"></param>
  705. /// <returns>null if the agent was not found</returns>
  706. protected internal ScenePresence GetScenePresence(UUID agentID)
  707. {
  708. ScenePresence sp;
  709. lock (ScenePresences)
  710. {
  711. ScenePresences.TryGetValue(agentID, out sp);
  712. }
  713. return sp;
  714. }
  715. /// <summary>
  716. /// Get a scene object group that contains the prim with the given local id
  717. /// </summary>
  718. /// <param name="localID"></param>
  719. /// <returns>null if no scene object group containing that prim is found</returns>
  720. private SceneObjectGroup GetGroupByPrim(uint localID)
  721. {
  722. //m_log.DebugFormat("Entered GetGroupByPrim with localID {0}", localID);
  723. List<EntityBase> EntityList = GetEntities();
  724. foreach (EntityBase ent in EntityList)
  725. {
  726. //m_log.DebugFormat("Looking at entity {0}", ent.UUID);
  727. if (ent is SceneObjectGroup)
  728. {
  729. if (((SceneObjectGroup)ent).HasChildPrim(localID))
  730. return (SceneObjectGroup)ent;
  731. }
  732. }
  733. return null;
  734. }
  735. /// <summary>
  736. /// Get a scene object group that contains the prim with the given uuid
  737. /// </summary>
  738. /// <param name="fullID"></param>
  739. /// <returns>null if no scene object group containing that prim is found</returns>
  740. private SceneObjectGroup GetGroupByPrim(UUID fullID)
  741. {
  742. List<EntityBase> EntityList = GetEntities();
  743. foreach (EntityBase ent in EntityList)
  744. {
  745. if (ent is SceneObjectGroup)
  746. {
  747. if (((SceneObjectGroup)ent).HasChildPrim(fullID))
  748. return (SceneObjectGroup)ent;
  749. }
  750. }
  751. return null;
  752. }
  753. protected internal EntityIntersection GetClosestIntersectingPrim(Ray hray, bool frontFacesOnly, bool faceCenters)
  754. {
  755. // Primitive Ray Tracing
  756. float closestDistance = 280f;
  757. EntityIntersection returnResult = new EntityIntersection();
  758. List<EntityBase> EntityList = GetEntities();
  759. foreach (EntityBase ent in EntityList)
  760. {
  761. if (ent is SceneObjectGroup)
  762. {
  763. SceneObjectGroup reportingG = (SceneObjectGroup)ent;
  764. EntityIntersection result = reportingG.TestIntersection(hray, frontFacesOnly, faceCenters);
  765. if (result.HitTF)
  766. {
  767. if (result.distance < closestDistance)
  768. {
  769. closestDistance = result.distance;
  770. returnResult = result;
  771. }
  772. }
  773. }
  774. }
  775. return returnResult;
  776. }
  777. /// <summary>
  778. /// Get a part contained in this scene.
  779. /// </summary>
  780. /// <param name="localID"></param>
  781. /// <returns>null if the part was not found</returns>
  782. protected internal SceneObjectPart GetSceneObjectPart(uint localID)
  783. {
  784. SceneObjectGroup group = GetGroupByPrim(localID);
  785. if (group != null)
  786. return group.GetChildPart(localID);
  787. else
  788. return null;
  789. }
  790. /// <summary>
  791. /// Get a named prim contained in this scene (will return the first
  792. /// found, if there are more than one prim with the same name)
  793. /// </summary>
  794. /// <param name="name"></param>
  795. /// <returns>null if the part was not found</returns>
  796. protected internal SceneObjectPart GetSceneObjectPart(string name)
  797. {
  798. List<EntityBase> EntityList = GetEntities();
  799. // FIXME: use a dictionary here
  800. foreach (EntityBase ent in EntityList)
  801. {
  802. if (ent is SceneObjectGroup)
  803. {
  804. foreach (SceneObjectPart p in ((SceneObjectGroup) ent).GetParts())
  805. {
  806. if (p.Name==name)
  807. {
  808. return p;
  809. }
  810. }
  811. }
  812. }
  813. return null;
  814. }
  815. /// <summary>
  816. /// Get a part contained in this scene.
  817. /// </summary>
  818. /// <param name="fullID"></param>
  819. /// <returns>null if the part was not found</returns>
  820. protected internal SceneObjectPart GetSceneObjectPart(UUID fullID)
  821. {
  822. SceneObjectGroup group = GetGroupByPrim(fullID);
  823. if (group != null)
  824. return group.GetChildPart(fullID);
  825. else
  826. return null;
  827. }
  828. protected internal bool TryGetAvatar(UUID avatarId, out ScenePresence avatar)
  829. {
  830. ScenePresence presence;
  831. lock (ScenePresences)
  832. {
  833. if (ScenePresences.TryGetValue(avatarId, out presence))
  834. {
  835. avatar = presence;
  836. return true;
  837. //if (!presence.IsChildAgent)
  838. //{
  839. // avatar = presence;
  840. // return true;
  841. //}
  842. //else
  843. //{
  844. // m_log.WarnFormat(
  845. // "[INNER SCENE]: Requested avatar {0} could not be found in scene {1} since it is only registered as a child agent!",
  846. // avatarId, m_parentScene.RegionInfo.RegionName);
  847. //}
  848. }
  849. }
  850. avatar = null;
  851. return false;
  852. }
  853. protected internal bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
  854. {
  855. lock (ScenePresences)
  856. {
  857. foreach (ScenePresence presence in ScenePresences.Values)
  858. {
  859. if (!presence.IsChildAgent)
  860. {
  861. string name = presence.ControllingClient.Name;
  862. if (String.Compare(avatarName, name, true) == 0)
  863. {
  864. avatar = presence;
  865. return true;
  866. }
  867. }
  868. }
  869. }
  870. avatar = null;
  871. return false;
  872. }
  873. /// <summary>
  874. /// Returns a list of the entities in the scene. This is a new list so no locking is required to iterate over
  875. /// it
  876. /// </summary>
  877. /// <returns></returns>
  878. protected internal List<EntityBase> GetEntities()
  879. {
  880. return Entities.GetEntities();
  881. }
  882. protected internal Dictionary<uint, float> GetTopScripts()
  883. {
  884. Dictionary<uint, float> topScripts = new Dictionary<uint, float>();
  885. List<EntityBase> EntityList = GetEntities();
  886. int limit = 0;
  887. foreach (EntityBase ent in EntityList)
  888. {
  889. if (ent is SceneObjectGroup)
  890. {
  891. SceneObjectGroup grp = (SceneObjectGroup)ent;
  892. if ((grp.RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Scripted) != 0)
  893. {
  894. if (grp.scriptScore >= 0.01)
  895. {
  896. topScripts.Add(grp.LocalId, grp.scriptScore);
  897. limit++;
  898. if (limit >= 100)
  899. {
  900. break;
  901. }
  902. }
  903. grp.scriptScore = 0;
  904. }
  905. }
  906. }
  907. return topScripts;
  908. }
  909. #endregion
  910. #region Other Methods
  911. protected internal void physicsBasedCrash()
  912. {
  913. handlerPhysicsCrash = UnRecoverableError;
  914. if (handlerPhysicsCrash != null)
  915. {
  916. handlerPhysicsCrash();
  917. }
  918. }
  919. protected internal UUID ConvertLocalIDToFullID(uint localID)
  920. {
  921. SceneObjectGroup group = GetGroupByPrim(localID);
  922. if (group != null)
  923. return group.GetPartsFullID(localID);
  924. else
  925. return UUID.Zero;
  926. }
  927. protected internal void ForEachClient(Action<IClientAPI> action)
  928. {
  929. List<ScenePresence> splist = GetScenePresences();
  930. foreach (ScenePresence presence in splist)
  931. {
  932. try
  933. {
  934. action(presence.ControllingClient);
  935. }
  936. catch (Exception e)
  937. {
  938. // Catch it and move on. This includes situations where splist has inconsistent info
  939. m_log.WarnFormat("[SCENE]: Problem processing action in ForEachClient: ", e.Message);
  940. }
  941. }
  942. }
  943. #endregion
  944. #region Client Event handlers
  945. /// <summary>
  946. ///
  947. /// </summary>
  948. /// <param name="localID"></param>
  949. /// <param name="scale"></param>
  950. /// <param name="remoteClient"></param>
  951. protected internal void UpdatePrimScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  952. {
  953. SceneObjectGroup group = GetGroupByPrim(localID);
  954. if (group != null)
  955. {
  956. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  957. {
  958. group.Resize(scale, localID);
  959. }
  960. }
  961. }
  962. protected internal void UpdatePrimGroupScale(uint localID, Vector3 scale, IClientAPI remoteClient)
  963. {
  964. SceneObjectGroup group = GetGroupByPrim(localID);
  965. if (group != null)
  966. {
  967. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  968. {
  969. group.GroupResize(scale, localID);
  970. }
  971. }
  972. }
  973. /// <summary>
  974. /// This handles the nifty little tool tip that you get when you drag your mouse over an object
  975. /// Send to the Object Group to process. We don't know enough to service the request
  976. /// </summary>
  977. /// <param name="remoteClient"></param>
  978. /// <param name="AgentID"></param>
  979. /// <param name="RequestFlags"></param>
  980. /// <param name="ObjectID"></param>
  981. protected internal void RequestObjectPropertiesFamily(
  982. IClientAPI remoteClient, UUID AgentID, uint RequestFlags, UUID ObjectID)
  983. {
  984. SceneObjectGroup group = GetGroupByPrim(ObjectID);
  985. if (group != null)
  986. {
  987. group.ServiceObjectPropertiesFamilyRequest(remoteClient, AgentID, RequestFlags);
  988. }
  989. }
  990. /// <summary>
  991. ///
  992. /// </summary>
  993. /// <param name="localID"></param>
  994. /// <param name="rot"></param>
  995. /// <param name="remoteClient"></param>
  996. protected internal void UpdatePrimSingleRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  997. {
  998. SceneObjectGroup group = GetGroupByPrim(localID);
  999. if (group != null)
  1000. {
  1001. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1002. {
  1003. group.UpdateSingleRotation(rot, localID);
  1004. }
  1005. }
  1006. }
  1007. /// <summary>
  1008. ///
  1009. /// </summary>
  1010. /// <param name="localID"></param>
  1011. /// <param name="rot"></param>
  1012. /// <param name="remoteClient"></param>
  1013. protected internal void UpdatePrimRotation(uint localID, Quaternion rot, IClientAPI remoteClient)
  1014. {
  1015. SceneObjectGroup group = GetGroupByPrim(localID);
  1016. if (group != null)
  1017. {
  1018. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1019. {
  1020. group.UpdateGroupRotation(rot);
  1021. }
  1022. }
  1023. }
  1024. /// <summary>
  1025. ///
  1026. /// </summary>
  1027. /// <param name="localID"></param>
  1028. /// <param name="pos"></param>
  1029. /// <param name="rot"></param>
  1030. /// <param name="remoteClient"></param>
  1031. protected internal void UpdatePrimRotation(uint localID, Vector3 pos, Quaternion rot, IClientAPI remoteClient)
  1032. {
  1033. SceneObjectGroup group = GetGroupByPrim(localID);
  1034. if (group != null)
  1035. {
  1036. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))
  1037. {
  1038. group.UpdateGroupRotation(pos, rot);
  1039. }
  1040. }
  1041. }
  1042. /// <summary>
  1043. /// Update the position of the given part
  1044. /// </summary>
  1045. /// <param name="localID"></param>
  1046. /// <param name="pos"></param>
  1047. /// <param name="remoteClient"></param>
  1048. protected internal void UpdatePrimSinglePosition(uint localID, Vector3 pos, IClientAPI remoteClient)
  1049. {
  1050. SceneObjectGroup group = GetGroupByPrim(localID);
  1051. if (group != null)
  1052. {
  1053. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId) || group.IsAttachment)
  1054. {
  1055. group.UpdateSinglePosition(pos, localID);
  1056. }
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// Update the position of the given part
  1061. /// </summary>
  1062. /// <param name="localID"></param>
  1063. /// <param name="pos"></param>
  1064. /// <param name="remoteClient"></param>
  1065. protected internal void UpdatePrimPosition(uint localID, Vector3 pos, IClientAPI remoteClient)
  1066. {
  1067. SceneObjectGroup group = GetGroupByPrim(localID);
  1068. if (group != null)
  1069. {
  1070. // Vector3 oldPos = group.AbsolutePosition;
  1071. if (group.IsAttachment || (group.RootPart.Shape.PCode == 9 && group.RootPart.Shape.State != 0))
  1072. {
  1073. group.UpdateGroupPosition(pos);
  1074. }
  1075. else
  1076. {
  1077. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId) && m_parentScene.Permissions.CanObjectEntry(group.UUID, false, pos))
  1078. {
  1079. group.UpdateGroupPosition(pos);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. /// <summary>
  1085. ///
  1086. /// </summary>
  1087. /// <param name="localID"></param>
  1088. /// <param name="texture"></param>
  1089. /// <param name="remoteClient"></param>
  1090. protected internal void UpdatePrimTexture(uint localID, byte[] texture, IClientAPI remoteClient)
  1091. {
  1092. SceneObjectGroup group = GetGroupByPrim(localID);
  1093. if (group != null)
  1094. {
  1095. if (m_parentScene.Permissions.CanEditObject(group.UUID,remoteClient.AgentId))
  1096. {
  1097. group.UpdateTextureEntry(localID, texture);
  1098. }
  1099. }
  1100. }
  1101. /// <summary>
  1102. ///
  1103. /// </summary>
  1104. /// <param name="localID"></param>
  1105. /// <param name="packet"></param>
  1106. /// <param name="remoteClient"></param>
  1107. /// This routine seems to get called when a user changes object settings in the viewer.
  1108. /// If some one can confirm that, please change the comment according.
  1109. protected internal void UpdatePrimFlags(uint localID, bool UsePhysics, bool IsTemporary, bool IsPhantom, IClientAPI remoteClient)
  1110. {
  1111. SceneObjectGroup group = GetGroupByPrim(localID);
  1112. if (group != null)
  1113. {
  1114. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1115. {
  1116. 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
  1117. }
  1118. }
  1119. }
  1120. /// <summary>
  1121. /// Move the given object
  1122. /// </summary>
  1123. /// <param name="objectID"></param>
  1124. /// <param name="offset"></param>
  1125. /// <param name="pos"></param>
  1126. /// <param name="remoteClient"></param>
  1127. protected internal void MoveObject(UUID objectID, Vector3 offset, Vector3 pos, IClientAPI remoteClient, List<SurfaceTouchEventArgs> surfaceArgs)
  1128. {
  1129. SceneObjectGroup group = GetGroupByPrim(objectID);
  1130. if (group != null)
  1131. {
  1132. if (m_parentScene.Permissions.CanMoveObject(group.UUID, remoteClient.AgentId))// && PermissionsMngr.)
  1133. {
  1134. group.GrabMovement(offset, pos, remoteClient);
  1135. }
  1136. // This is outside the above permissions condition
  1137. // so that if the object is locked the client moving the object
  1138. // get's it's position on the simulator even if it was the same as before
  1139. // This keeps the moving user's client in sync with the rest of the world.
  1140. group.SendGroupTerseUpdate();
  1141. }
  1142. }
  1143. /// <summary>
  1144. ///
  1145. /// </summary>
  1146. /// <param name="primLocalID"></param>
  1147. /// <param name="description"></param>
  1148. protected internal void PrimName(IClientAPI remoteClient, uint primLocalID, string name)
  1149. {
  1150. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1151. if (group != null)
  1152. {
  1153. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1154. {
  1155. group.SetPartName(Util.CleanString(name), primLocalID);
  1156. group.HasGroupChanged = true;
  1157. }
  1158. }
  1159. }
  1160. /// <summary>
  1161. ///
  1162. /// </summary>
  1163. /// <param name="primLocalID"></param>
  1164. /// <param name="description"></param>
  1165. protected internal void PrimDescription(IClientAPI remoteClient, uint primLocalID, string description)
  1166. {
  1167. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1168. if (group != null)
  1169. {
  1170. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1171. {
  1172. group.SetPartDescription(Util.CleanString(description), primLocalID);
  1173. group.HasGroupChanged = true;
  1174. }
  1175. }
  1176. }
  1177. protected internal void PrimClickAction(IClientAPI remoteClient, uint primLocalID, string clickAction)
  1178. {
  1179. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1180. if (group != null)
  1181. {
  1182. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1183. {
  1184. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primLocalID);
  1185. part.ClickAction = Convert.ToByte(clickAction);
  1186. group.HasGroupChanged = true;
  1187. }
  1188. }
  1189. }
  1190. protected internal void PrimMaterial(IClientAPI remoteClient, uint primLocalID, string material)
  1191. {
  1192. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1193. if (group != null)
  1194. {
  1195. if (m_parentScene.Permissions.CanEditObject(group.UUID, remoteClient.AgentId))
  1196. {
  1197. SceneObjectPart part = m_parentScene.GetSceneObjectPart(primLocalID);
  1198. part.Material = Convert.ToByte(material);
  1199. group.HasGroupChanged = true;
  1200. }
  1201. }
  1202. }
  1203. protected internal void UpdateExtraParam(UUID agentID, uint primLocalID, ushort type, bool inUse, byte[] data)
  1204. {
  1205. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1206. if (group != null)
  1207. {
  1208. if (m_parentScene.Permissions.CanEditObject(group.UUID,agentID))
  1209. {
  1210. group.UpdateExtraParam(primLocalID, type, inUse, data);
  1211. }
  1212. }
  1213. }
  1214. /// <summary>
  1215. ///
  1216. /// </summary>
  1217. /// <param name="primLocalID"></param>
  1218. /// <param name="shapeBlock"></param>
  1219. protected internal void UpdatePrimShape(UUID agentID, uint primLocalID, UpdateShapeArgs shapeBlock)
  1220. {
  1221. SceneObjectGroup group = GetGroupByPrim(primLocalID);
  1222. if (group != null)
  1223. {
  1224. if (m_parentScene.Permissions.CanEditObject(group.GetPartsFullID(primLocalID), agentID))
  1225. {
  1226. ObjectShapePacket.ObjectDataBlock shapeData = new ObjectShapePacket.ObjectDataBlock();
  1227. shapeData.ObjectLocalID = shapeBlock.ObjectLocalID;
  1228. shapeData.PathBegin = shapeBlock.PathBegin;
  1229. shapeData.PathCurve = shapeBlock.PathCurve;
  1230. shapeData.PathEnd = shapeBlock.PathEnd;
  1231. shapeData.PathRadiusOffset = shapeBlock.PathRadiusOffset;
  1232. shapeData.PathRevolutions = shapeBlock.PathRevolutions;
  1233. shapeData.PathScaleX = shapeBlock.PathScaleX;
  1234. shapeData.PathScaleY = shapeBlock.PathScaleY;
  1235. shapeData.PathShearX = shapeBlock.PathShearX;
  1236. shapeData.PathShearY = shapeBlock.PathShearY;
  1237. shapeData.PathSkew = shapeBlock.PathSkew;
  1238. shapeData.PathTaperX = shapeBlock.PathTaperX;
  1239. shapeData.PathTaperY = shapeBlock.PathTaperY;
  1240. shapeData.PathTwist = shapeBlock.PathTwist;
  1241. shapeData.PathTwistBegin = shapeBlock.PathTwistBegin;
  1242. shapeData.ProfileBegin = shapeBlock.ProfileBegin;
  1243. shapeData.ProfileCurve = shapeBlock.ProfileCurve;
  1244. shapeData.ProfileEnd = shapeBlock.ProfileEnd;
  1245. shapeData.ProfileHollow = shapeBlock.ProfileHollow;
  1246. group.UpdateShape(shapeData, primLocalID);
  1247. }
  1248. }
  1249. }
  1250. /// <summary>
  1251. /// Initial method invoked when we receive a link objects request from the client.
  1252. /// </summary>
  1253. /// <param name="client"></param>
  1254. /// <param name="parentPrim"></param>
  1255. /// <param name="childPrims"></param>
  1256. protected internal void LinkObjects(IClientAPI client, uint parentPrim, List<uint> childPrims)
  1257. {
  1258. List<EntityBase> EntityList = GetEntities();
  1259. SceneObjectGroup parenPrim = null;
  1260. foreach (EntityBase ent in EntityList)
  1261. {
  1262. if (ent is SceneObjectGroup)
  1263. {
  1264. if (((SceneObjectGroup)ent).LocalId == parentPrim)
  1265. {
  1266. parenPrim = (SceneObjectGroup)ent;
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. List<SceneObjectGroup> children = new List<SceneObjectGroup>();
  1272. if (parenPrim != null)
  1273. {
  1274. // We do this in reverse to get the link order of the prims correct
  1275. for (int i = childPrims.Count - 1; i >= 0; i--)
  1276. {
  1277. foreach (EntityBase ent in EntityList)
  1278. {
  1279. if (ent is SceneObjectGroup)
  1280. {
  1281. if (((SceneObjectGroup)ent).LocalId == childPrims[i])
  1282. {
  1283. // Make sure no child prim is set for sale
  1284. // So that, on delink, no prims are unwittingly
  1285. // left for sale and sold off
  1286. ((SceneObjectGroup)ent).RootPart.ObjectSaleType = 0;
  1287. ((SceneObjectGroup)ent).RootPart.SalePrice = 10;
  1288. children.Add((SceneObjectGroup)ent);
  1289. }
  1290. }
  1291. }
  1292. }
  1293. }
  1294. else
  1295. {
  1296. return; // parent is null so not in this region
  1297. }
  1298. foreach (SceneObjectGroup sceneObj in children)
  1299. {
  1300. parenPrim.LinkToGroup(sceneObj);
  1301. // this is here so physics gets updated!
  1302. // Don't remove! Bad juju! Stay away! or fix physics!
  1303. sceneObj.AbsolutePosition = sceneObj.AbsolutePosition;
  1304. }
  1305. // We need to explicitly resend the newly link prim's object properties since no other actions
  1306. // occur on link to invoke this elsewhere (such as object selection)
  1307. parenPrim.RootPart.AddFlag(PrimFlags.CreateSelected);
  1308. parenPrim.TriggerScriptChangedEvent(Changed.LINK);
  1309. if (client != null)
  1310. parenPrim.GetProperties(client);
  1311. else
  1312. {
  1313. foreach (ScenePresence p in GetScenePresences())
  1314. {
  1315. parenPrim.GetProperties(p.ControllingClient);
  1316. }
  1317. }
  1318. }
  1319. /// <summary>
  1320. /// Delink a linkset
  1321. /// </summary>
  1322. /// <param name="prims"></param>
  1323. protected internal void DelinkObjects(List<uint> primIds)
  1324. {
  1325. DelinkObjects(primIds, true);
  1326. }
  1327. protected internal void DelinkObjects(List<uint> primIds, bool sendEvents)
  1328. {
  1329. SceneObjectGroup parenPrim = null;
  1330. // Need a list of the SceneObjectGroup local ids
  1331. // XXX I'm anticipating that building this dictionary once is more efficient than
  1332. // repeated scanning of the Entity.Values for a large number of primIds. However, it might
  1333. // be more efficient yet to keep this dictionary permanently on hand.
  1334. Dictionary<uint, SceneObjectGroup> sceneObjects = new Dictionary<uint, SceneObjectGroup>();
  1335. List<EntityBase> EntityList = GetEntities();
  1336. foreach (EntityBase ent in EntityList)
  1337. {
  1338. if (ent is SceneObjectGroup)
  1339. {
  1340. SceneObjectGroup obj = (SceneObjectGroup)ent;
  1341. // Nasty one. Can't unlink anything in the sim
  1342. // If a duplicate local ID sneaks in
  1343. // So, check it here!
  1344. //
  1345. if (!sceneObjects.ContainsKey(obj.LocalId))
  1346. sceneObjects.Add(obj.LocalId, obj);
  1347. }
  1348. }
  1349. // Find the root prim among the prim ids we've been given
  1350. for (int i = 0; i < primIds.Count; i++)
  1351. {
  1352. if (sceneObjects.ContainsKey(primIds[i]))
  1353. {
  1354. parenPrim = sceneObjects[primIds[i]];
  1355. primIds.RemoveAt(i);
  1356. break;
  1357. }
  1358. }
  1359. if (parenPrim != null)
  1360. {
  1361. foreach (uint childPrimId in primIds)
  1362. {
  1363. parenPrim.DelinkFromGroup(childPrimId, sendEvents);
  1364. }
  1365. if (parenPrim.Children.Count == 1)
  1366. {
  1367. // The link set has been completely torn down
  1368. // This is the case if you select a link set and delink
  1369. //
  1370. parenPrim.RootPart.LinkNum = 0;
  1371. if (sendEvents)
  1372. parenPrim.TriggerScriptChangedEvent(Changed.LINK);
  1373. }
  1374. else
  1375. {
  1376. // The link set has prims remaining. This path is taken
  1377. // when a subset of a link set's prims are selected
  1378. // and the root prim is part of that selection
  1379. //
  1380. List<SceneObjectPart> parts = new List<SceneObjectPart>(parenPrim.Children.Values);
  1381. List<uint> unlink_ids = new List<uint>();
  1382. foreach (SceneObjectPart unlink_part in parts)
  1383. unlink_ids.Add(unlink_part.LocalId);
  1384. // Tear down the remaining link set
  1385. //
  1386. if (unlink_ids.Count == 2)
  1387. {
  1388. DelinkObjects(unlink_ids, true);
  1389. return;
  1390. }
  1391. DelinkObjects(unlink_ids, false);
  1392. // Send event to root prim, then we're done with it
  1393. parenPrim.TriggerScriptChangedEvent(Changed.LINK);
  1394. unlink_ids.Remove(parenPrim.RootPart.LocalId);
  1395. foreach (uint localId in unlink_ids)
  1396. {
  1397. SceneObjectPart nr = GetSceneObjectPart(localId);
  1398. nr.UpdateFlag = 0;
  1399. }
  1400. uint newRoot = unlink_ids[0];
  1401. unlink_ids.Remove(newRoot);
  1402. LinkObjects(null, newRoot, unlink_ids);
  1403. }
  1404. }
  1405. else
  1406. {
  1407. // The selected prims were all child prims. Edit linked parts
  1408. // without the root prim selected will get us here
  1409. //
  1410. List<SceneObjectGroup> parents = new List<SceneObjectGroup>();
  1411. // If the first scan failed, we need to do a /deep/ scan of the linkages. This is /really/ slow
  1412. // We know that this is not the root prim now essentially, so we don't have to worry about remapping
  1413. // which one is the root prim
  1414. bool delinkedSomething = false;
  1415. for (int i = 0; i < primIds.Count; i++)
  1416. {
  1417. foreach (SceneObjectGroup grp in sceneObjects.Values)
  1418. {
  1419. SceneObjectPart gPart = grp.GetChildPart(primIds[i]);
  1420. if (gPart != null)
  1421. {
  1422. grp.DelinkFromGroup(primIds[i]);
  1423. delinkedSomething = true;
  1424. if (!parents.Contains(grp))
  1425. parents.Add(grp);
  1426. }
  1427. }
  1428. }
  1429. if (!delinkedSomething)
  1430. {
  1431. m_log.InfoFormat("[SCENE]: " +
  1432. "DelinkObjects(): Could not find a root prim out of {0} as given to a delink request!",
  1433. primIds);
  1434. }
  1435. else
  1436. {
  1437. foreach (SceneObjectGroup g in parents)
  1438. {
  1439. g.TriggerScriptChangedEvent(Changed.LINK);
  1440. }
  1441. }
  1442. }
  1443. }
  1444. protected internal void MakeObjectSearchable(IClientAPI remoteClient, bool IncludeInSearch, uint localID)
  1445. {
  1446. UUID user = remoteClient.AgentId;
  1447. UUID objid = UUID.Zero;
  1448. SceneObjectPart obj = null;
  1449. List<EntityBase> EntityList = GetEntities();
  1450. foreach (EntityBase ent in EntityList)
  1451. {
  1452. if (ent is SceneObjectGroup)
  1453. {
  1454. foreach (KeyValuePair<UUID, SceneObjectPart> subent in ((SceneObjectGroup)ent).Children)
  1455. {
  1456. if (subent.Value.LocalId == localID)
  1457. {
  1458. objid = subent.Key;
  1459. obj = subent.Value;
  1460. }
  1461. }
  1462. }
  1463. }
  1464. //Protip: In my day, we didn't call them searchable objects, we called them limited point-to-point joints
  1465. //aka ObjectFlags.JointWheel = IncludeInSearch
  1466. //Permissions model: Object can be REMOVED from search IFF:
  1467. // * User owns object
  1468. //use CanEditObject
  1469. //Object can be ADDED to search IFF:
  1470. // * User owns object
  1471. // * Asset/DRM permission bit "modify" is enabled
  1472. //use CanEditObjectPosition
  1473. // libomv will complain about PrimFlags.JointWheel being
  1474. // deprecated, so we
  1475. #pragma warning disable 0612
  1476. if (IncludeInSearch && m_parentScene.Permissions.CanEditObject(objid, user))
  1477. {
  1478. obj.ParentGroup.RootPart.AddFlag(PrimFlags.JointWheel);
  1479. obj.ParentGroup.HasGroupChanged = true;
  1480. }
  1481. else if (!IncludeInSearch && m_parentScene.Permissions.CanMoveObject(objid,user))
  1482. {
  1483. obj.ParentGroup.RootPart.RemFlag(PrimFlags.JointWheel);
  1484. obj.ParentGroup.HasGroupChanged = true;
  1485. }
  1486. #pragma warning restore 0612
  1487. }
  1488. /// <summary>
  1489. /// Duplicate the given object, Fire and Forget, No rotation, no return wrapper
  1490. /// </summary>
  1491. /// <param name="originalPrim"></param>
  1492. /// <param name="offset"></param>
  1493. /// <param name="flags"></param>
  1494. protected internal void DuplicateObject(uint originalPrim, Vector3 offset, uint flags, UUID AgentID, UUID GroupID)
  1495. {
  1496. //m_log.DebugFormat("[SCENE]: Duplication of object {0} at offset {1} requested by agent {2}", originalPrim, offset, AgentID);
  1497. // SceneObjectGroup dupe = DuplicateObject(originalPrim, offset, flags, AgentID, GroupID, Quaternion.Zero);
  1498. DuplicateObject(originalPrim, offset, flags, AgentID, GroupID, Quaternion.Identity);
  1499. }
  1500. /// <summary>
  1501. /// Duplicate the given object.
  1502. /// </summary>
  1503. /// <param name="originalPrim"></param>
  1504. /// <param name="offset"></param>
  1505. /// <param name="flags"></param>
  1506. protected internal SceneObjectGroup DuplicateObject(uint originalPrim, Vector3 offset, uint flags, UUID AgentID, UUID GroupID, Quaternion rot)
  1507. {
  1508. //m_log.DebugFormat("[SCENE]: Duplication of object {0} at offset {1} requested by agent {2}", originalPrim, offset, AgentID);
  1509. List<EntityBase> EntityList = GetEntities();
  1510. SceneObjectGroup originPrim = null;
  1511. foreach (EntityBase ent in EntityList)
  1512. {
  1513. if (ent is SceneObjectGroup)
  1514. {
  1515. if (((SceneObjectGroup)ent).LocalId == originalPrim)
  1516. {
  1517. originPrim = (SceneObjectGroup)ent;
  1518. break;
  1519. }
  1520. }
  1521. }
  1522. if (originPrim != null)
  1523. {
  1524. if (m_parentScene.Permissions.CanDuplicateObject(originPrim.Children.Count, originPrim.UUID, AgentID, originPrim.AbsolutePosition))
  1525. {
  1526. SceneObjectGroup copy = originPrim.Copy(AgentID, GroupID, true);
  1527. copy.AbsolutePosition = copy.AbsolutePosition + offset;
  1528. Entities.Add(copy);
  1529. // Since we copy from a source group that is in selected
  1530. // state, but the copy is shown deselected in the viewer,
  1531. // We need to clear the selection flag here, else that
  1532. // prim never gets persisted at all. The client doesn't
  1533. // think it's selected, so it will never send a deselect...
  1534. copy.IsSelected = false;
  1535. m_numPrim += copy.Children.Count;
  1536. if (rot != Quaternion.Identity)
  1537. {
  1538. copy.UpdateGroupRotation(rot);
  1539. }
  1540. copy.CreateScriptInstances(0, false, m_parentScene.DefaultScriptEngine, 0);
  1541. copy.HasGroupChanged = true;
  1542. copy.ScheduleGroupForFullUpdate();
  1543. // required for physics to update it's position
  1544. copy.AbsolutePosition = copy.AbsolutePosition;
  1545. if (OnObjectDuplicate != null)
  1546. OnObjectDuplicate(originPrim, copy);
  1547. return copy;
  1548. }
  1549. }
  1550. else
  1551. {
  1552. m_log.WarnFormat("[SCENE]: Attempted to duplicate nonexistant prim id {0}", GroupID);
  1553. }
  1554. return null;
  1555. }
  1556. /// <summary>
  1557. /// Calculates the distance between two Vector3s
  1558. /// </summary>
  1559. /// <param name="v1"></param>
  1560. /// <param name="v2"></param>
  1561. /// <returns></returns>
  1562. protected internal float Vector3Distance(Vector3 v1, Vector3 v2)
  1563. {
  1564. // We don't really need the double floating point precision...
  1565. // so casting it to a single
  1566. return
  1567. (float)
  1568. 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));
  1569. }
  1570. #endregion
  1571. }
  1572. }