SceneGraph.cs 69 KB

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