SceneGraph.cs 65 KB

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