SceneGraph.cs 62 KB

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