SceneGraph.cs 64 KB

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