SceneObjectGroup.cs 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530
  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.Drawing;
  30. using System.IO;
  31. using System.Xml;
  32. using Axiom.Math;
  33. using libsecondlife;
  34. using libsecondlife.Packets;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Physics.Manager;
  38. namespace OpenSim.Region.Environment.Scenes
  39. {
  40. [Flags]
  41. public enum scriptEvents
  42. {
  43. None = 0,
  44. attach = 1,
  45. collision = 15,
  46. collision_end = 32,
  47. collision_start = 64,
  48. control = 128,
  49. dataserver = 256,
  50. email = 512,
  51. http_response = 1024,
  52. land_collision = 2048,
  53. land_collision_end = 4096,
  54. land_collision_start = 8192,
  55. at_target = 16384,
  56. listen = 32768,
  57. money = 65536,
  58. moving_end = 131072,
  59. moving_start = 262144,
  60. not_at_rot_target = 524288,
  61. not_at_target = 1048576,
  62. remote_data = 8388608,
  63. run_time_permissions = 268435456,
  64. state_entry = 1073741824,
  65. state_exit = 2,
  66. timer = 4,
  67. touch = 8,
  68. touch_end = 536870912,
  69. touch_start = 2097152,
  70. object_rez = 4194304
  71. }
  72. struct scriptPosTarget
  73. {
  74. public LLVector3 targetPos;
  75. public float tolerance;
  76. }
  77. public delegate void PrimCountTaintedDelegate();
  78. public partial class SceneObjectGroup : EntityBase
  79. {
  80. private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  81. /// <summary>
  82. /// Signal whether the non-inventory attributes of any prims in the group have changed
  83. /// since the group's last persistent backup
  84. /// </summary>
  85. public bool HasGroupChanged = false;
  86. private LLVector3 lastPhysGroupPos;
  87. private LLQuaternion lastPhysGroupRot;
  88. /// <summary>
  89. /// The constituent parts of this group
  90. /// </summary>
  91. protected Dictionary<LLUUID, SceneObjectPart> m_parts = new Dictionary<LLUUID, SceneObjectPart>();
  92. protected ulong m_regionHandle;
  93. protected SceneObjectPart m_rootPart;
  94. private Dictionary<LLUUID, scriptEvents> m_scriptEvents = new Dictionary<LLUUID, scriptEvents>();
  95. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  96. private bool m_scriptListens_atTarget = false;
  97. private bool m_scriptListens_notAtTarget = false;
  98. #region Properties
  99. /// <summary>
  100. /// Added because the Parcel code seems to use it
  101. /// but not sure a object should have this
  102. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  103. /// think really there should be a list (or whatever) in each scenepresence
  104. /// saying what prim(s) that user has selected.
  105. /// </summary>
  106. protected bool m_isSelected = false;
  107. /// <summary>
  108. ///
  109. /// </summary>
  110. public int PrimCount
  111. {
  112. get { return m_parts.Count; }
  113. }
  114. public LLQuaternion GroupRotation
  115. {
  116. get { return m_rootPart.RotationOffset; }
  117. }
  118. public LLUUID GroupID
  119. {
  120. get { return m_rootPart.GroupID; }
  121. set { m_rootPart.GroupID = value; }
  122. }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. public LLVector3 GroupCentrePoint
  127. {
  128. get { return new LLVector3(0, 0, 0); }
  129. }
  130. public Dictionary<LLUUID, SceneObjectPart> Children
  131. {
  132. get { return m_parts; }
  133. set { m_parts = value; }
  134. }
  135. public SceneObjectPart RootPart
  136. {
  137. get { return m_rootPart; }
  138. set { m_rootPart = value; }
  139. }
  140. public ulong RegionHandle
  141. {
  142. get { return m_regionHandle; }
  143. set
  144. {
  145. m_regionHandle = value;
  146. lock (m_parts)
  147. {
  148. foreach (SceneObjectPart part in m_parts.Values)
  149. {
  150. part.RegionHandle = m_regionHandle;
  151. }
  152. }
  153. }
  154. }
  155. public override LLVector3 AbsolutePosition
  156. {
  157. get
  158. {
  159. if (m_rootPart == null)
  160. {
  161. throw new NullReferenceException(
  162. string.Format("[SCENE OBJECT GROUP]: Object {0} has no root part.", m_uuid));
  163. }
  164. return m_rootPart.GroupPosition;
  165. }
  166. set
  167. {
  168. LLVector3 val = value;
  169. if ((val.X > 257f || val.X < -1f || val.Y > 257f || val.Y < -1f) && !m_rootPart.m_IsAttachment)
  170. {
  171. m_scene.CrossPrimGroupIntoNewRegion(val, this);
  172. }
  173. lock (m_parts)
  174. {
  175. foreach (SceneObjectPart part in m_parts.Values)
  176. {
  177. part.GroupPosition = val;
  178. }
  179. }
  180. //if (m_rootPart.PhysActor != null)
  181. //{
  182. //m_rootPart.PhysActor.Position =
  183. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  184. //m_rootPart.GroupPosition.Z);
  185. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  186. //}
  187. }
  188. }
  189. public override uint LocalId
  190. {
  191. get
  192. {
  193. if (m_rootPart == null)
  194. {
  195. m_log.Error("[SCENE OBJECT GROUP]: Unable to find the rootpart for a LocalId Request!");
  196. return 0;
  197. }
  198. return m_rootPart.LocalId;
  199. }
  200. set { m_rootPart.LocalId = value; }
  201. }
  202. public override LLUUID UUID
  203. {
  204. get { return m_rootPart.UUID; }
  205. set { m_rootPart.UUID = value; }
  206. }
  207. public LLUUID OwnerID
  208. {
  209. get
  210. {
  211. if (m_rootPart == null)
  212. return LLUUID.Zero;
  213. return m_rootPart.OwnerID;
  214. }
  215. set { m_rootPart.OwnerID = value; }
  216. }
  217. public Color Color
  218. {
  219. get { return m_rootPart.Color; }
  220. set { m_rootPart.Color = value; }
  221. }
  222. public string Text
  223. {
  224. get { return m_rootPart.Text; }
  225. set { m_rootPart.Text = value; }
  226. }
  227. protected virtual bool InSceneBackup
  228. {
  229. get { return true; }
  230. }
  231. public bool IsSelected
  232. {
  233. get { return m_isSelected; }
  234. set
  235. {
  236. m_isSelected = value;
  237. // Tell physics engine that group is selected
  238. if (m_rootPart.PhysActor != null)
  239. {
  240. m_rootPart.PhysActor.Selected = value;
  241. // Pass it on to the children.
  242. foreach (SceneObjectPart child in Children.Values)
  243. {
  244. if (child.PhysActor != null)
  245. {
  246. child.PhysActor.Selected = value;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. // The UUID for the Region this Object is in.
  253. public LLUUID RegionUUID
  254. {
  255. get
  256. {
  257. if (m_scene != null)
  258. {
  259. return m_scene.RegionInfo.RegionID;
  260. }
  261. return LLUUID.Zero;
  262. }
  263. }
  264. #endregion
  265. #region Constructors
  266. /// <summary>
  267. ///
  268. /// </summary>
  269. public SceneObjectGroup()
  270. {
  271. }
  272. /// <summary>
  273. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  274. /// The original SceneObjectPart will be used rather than a copy, preserving
  275. /// its existing localID and UUID.
  276. /// </summary>
  277. public SceneObjectGroup(Scene scene, ulong regionHandle, SceneObjectPart part)
  278. {
  279. m_scene = scene;
  280. part.SetParent(this);
  281. part.ParentID = 0;
  282. part.LinkNum = 0;
  283. m_parts.Add(part.UUID, part);
  284. SetPartAsRoot(part);
  285. RegionHandle = regionHandle;
  286. AttachToBackup();
  287. ApplyPhysics(scene.m_physicalPrim);
  288. ScheduleGroupForFullUpdate();
  289. }
  290. /// <summary>
  291. /// Restore the object from its serialized xml representation.
  292. /// </summary>
  293. public SceneObjectGroup(Scene scene, ulong regionHandle, string xmlData)
  294. {
  295. m_scene = scene;
  296. m_regionHandle = regionHandle;
  297. StringReader sr = new StringReader(xmlData);
  298. XmlTextReader reader = new XmlTextReader(sr);
  299. try
  300. {
  301. reader.Read();
  302. reader.ReadStartElement("SceneObjectGroup");
  303. reader.ReadStartElement("RootPart");
  304. m_rootPart = SceneObjectPart.FromXml(reader);
  305. AddPart(m_rootPart);
  306. m_log.DebugFormat("[SCENE OBJECT GROUP]: Current node {0}", reader.Name);
  307. reader.ReadEndElement();
  308. while (reader.Read())
  309. {
  310. switch (reader.NodeType)
  311. {
  312. case XmlNodeType.Element:
  313. if (reader.Name == "Part")
  314. {
  315. reader.Read();
  316. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  317. part.LocalId = m_scene.PrimIDAllocate();
  318. AddPart(part);
  319. part.RegionHandle = m_regionHandle;
  320. part.TrimPermissions();
  321. part.StoreUndoState();
  322. }
  323. break;
  324. case XmlNodeType.EndElement:
  325. break;
  326. }
  327. }
  328. }
  329. catch (XmlException)
  330. {
  331. m_log.ErrorFormat("[SCENE OBJECT GROUP]: Deserialization of following xml failed, {0}", xmlData);
  332. // Let's see if carrying on does anything for us
  333. }
  334. reader.Close();
  335. sr.Close();
  336. m_rootPart.LocalId = m_scene.PrimIDAllocate();
  337. m_rootPart.ParentID = 0;
  338. m_rootPart.RegionHandle = m_regionHandle;
  339. UpdateParentIDs();
  340. AttachToBackup();
  341. ApplyPhysics(scene.m_physicalPrim);
  342. ScheduleGroupForFullUpdate();
  343. }
  344. /// <summary>
  345. ///
  346. /// </summary>
  347. public SceneObjectGroup(string xmlData)
  348. {
  349. StringReader sr = new StringReader(xmlData);
  350. XmlTextReader reader = new XmlTextReader(sr);
  351. reader.Read();
  352. reader.ReadStartElement("SceneObjectGroup");
  353. m_rootPart = SceneObjectPart.FromXml(reader);
  354. m_rootPart.SetParent(this);
  355. m_parts.Add(m_rootPart.UUID, m_rootPart);
  356. m_rootPart.ParentID = 0;
  357. m_rootPart.LinkNum = 0;
  358. reader.Read();
  359. bool more = true;
  360. while (more)
  361. {
  362. switch (reader.NodeType)
  363. {
  364. case XmlNodeType.Element:
  365. if (reader.Name == "SceneObjectPart")
  366. {
  367. SceneObjectPart Part = SceneObjectPart.FromXml(reader);
  368. AddPart(Part);
  369. Part.StoreUndoState();
  370. }
  371. else
  372. {
  373. Console.WriteLine("found unexpected element: " + reader.Name);
  374. reader.Read();
  375. }
  376. break;
  377. case XmlNodeType.EndElement:
  378. reader.Read();
  379. break;
  380. }
  381. more = !reader.EOF;
  382. }
  383. reader.Close();
  384. sr.Close();
  385. UpdateParentIDs();
  386. }
  387. /// <summary>
  388. ///
  389. /// </summary>
  390. public SceneObjectGroup(Scene scene, ulong regionHandle, LLUUID ownerID, uint localID, LLVector3 pos,
  391. LLQuaternion rot, PrimitiveBaseShape shape)
  392. {
  393. m_regionHandle = regionHandle;
  394. m_scene = scene;
  395. // this.Pos = pos;
  396. LLVector3 rootOffset = new LLVector3(0, 0, 0);
  397. SceneObjectPart newPart =
  398. new SceneObjectPart(m_regionHandle, this, ownerID, localID, shape, pos, rot, rootOffset);
  399. newPart.LinkNum = m_parts.Count;
  400. m_parts.Add(newPart.UUID, newPart);
  401. SetPartAsRoot(newPart);
  402. AttachToBackup();
  403. //ApplyPhysics(scene.m_physicalPrim);
  404. }
  405. /// <summary>
  406. ///
  407. /// </summary>
  408. public SceneObjectGroup(Scene scene, ulong regionHandle, LLUUID ownerID, uint localID, LLVector3 pos,
  409. PrimitiveBaseShape shape)
  410. : this(scene, regionHandle, ownerID, localID, pos, LLQuaternion.Identity, shape)
  411. {
  412. }
  413. public void SetFromAssetID(LLUUID AssetId)
  414. {
  415. lock (m_parts)
  416. {
  417. foreach (SceneObjectPart part in m_parts.Values)
  418. {
  419. part.fromAssetID = AssetId;
  420. }
  421. }
  422. }
  423. public LLUUID GetFromAssetID()
  424. {
  425. if (m_rootPart != null)
  426. {
  427. return m_rootPart.fromAssetID;
  428. }
  429. return LLUUID.Zero;
  430. }
  431. /// <summary>
  432. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  433. /// </summary>
  434. private void AttachToBackup()
  435. {
  436. if (InSceneBackup)
  437. {
  438. m_scene.EventManager.OnBackup += ProcessBackup;
  439. }
  440. }
  441. public LLVector3 GroupScale()
  442. {
  443. LLVector3 minScale = new LLVector3(Constants.RegionSize,Constants.RegionSize,Constants.RegionSize);
  444. LLVector3 maxScale = new LLVector3(0f,0f,0f);
  445. LLVector3 finalScale = new LLVector3(0.5f, 0.5f, 0.5f);
  446. lock (m_parts)
  447. {
  448. foreach (SceneObjectPart part in m_parts.Values)
  449. {
  450. LLVector3 partscale = part.Scale;
  451. LLVector3 partoffset = part.OffsetPosition;
  452. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  453. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.X + partoffset.Y : minScale.Y;
  454. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.X + partoffset.Z : minScale.Z;
  455. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  456. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  457. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  458. }
  459. }
  460. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  461. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  462. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  463. return finalScale;
  464. }
  465. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  466. {
  467. // We got a request from the inner_scene to raytrace along the Ray hRay
  468. // We're going to check all of the prim in this group for intersection with the ray
  469. // If we get a result, we're going to find the closest result to the origin of the ray
  470. // and send back the intersection information back to the innerscene.
  471. EntityIntersection returnresult = new EntityIntersection();
  472. lock (m_parts)
  473. {
  474. foreach (SceneObjectPart part in m_parts.Values)
  475. {
  476. // Temporary commented to stop compiler warning
  477. //Vector3 partPosition =
  478. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  479. Quaternion parentrotation =
  480. new Quaternion(GroupRotation.W, GroupRotation.X, GroupRotation.Y, GroupRotation.Z);
  481. // Telling the prim to raytrace.
  482. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  483. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation,frontFacesOnly, faceCenters);
  484. // This may need to be updated to the maximum draw distance possible..
  485. // We might (and probably will) be checking for prim creation from other sims
  486. // when the camera crosses the border.
  487. float idist = Constants.RegionSize;
  488. if (inter.HitTF)
  489. {
  490. // We need to find the closest prim to return to the testcaller along the ray
  491. if (inter.distance < idist)
  492. {
  493. returnresult.HitTF = true;
  494. returnresult.ipoint = inter.ipoint;
  495. returnresult.obj = part;
  496. returnresult.normal = inter.normal;
  497. returnresult.distance = inter.distance;
  498. }
  499. }
  500. }
  501. }
  502. return returnresult;
  503. }
  504. #endregion
  505. public event PrimCountTaintedDelegate OnPrimCountTainted;
  506. public string ToXmlString()
  507. {
  508. using (StringWriter sw = new StringWriter())
  509. {
  510. using (XmlTextWriter writer = new XmlTextWriter(sw))
  511. {
  512. ToXml(writer);
  513. }
  514. return sw.ToString();
  515. }
  516. }
  517. public void ToXml(XmlTextWriter writer)
  518. {
  519. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  520. writer.WriteStartElement(String.Empty, "RootPart", String.Empty);
  521. m_rootPart.ToXml(writer);
  522. writer.WriteEndElement();
  523. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  524. lock (m_parts)
  525. {
  526. foreach (SceneObjectPart part in m_parts.Values)
  527. {
  528. if (part.UUID != m_rootPart.UUID)
  529. {
  530. writer.WriteStartElement(String.Empty, "Part", String.Empty);
  531. part.ToXml(writer);
  532. writer.WriteEndElement();
  533. }
  534. }
  535. }
  536. writer.WriteEndElement();
  537. writer.WriteEndElement();
  538. }
  539. public string ToXmlString2()
  540. {
  541. using (StringWriter sw = new StringWriter())
  542. {
  543. using (XmlTextWriter writer = new XmlTextWriter(sw))
  544. {
  545. ToXml2(writer);
  546. }
  547. return sw.ToString();
  548. }
  549. }
  550. public void ToXml2(XmlTextWriter writer)
  551. {
  552. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  553. m_rootPart.ToXml(writer);
  554. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  555. lock (m_parts)
  556. {
  557. foreach (SceneObjectPart part in m_parts.Values)
  558. {
  559. if (part.UUID != m_rootPart.UUID)
  560. {
  561. part.ToXml(writer);
  562. }
  563. }
  564. }
  565. writer.WriteEndElement();
  566. writer.WriteEndElement();
  567. }
  568. /// <summary>
  569. ///
  570. /// </summary>
  571. /// <param name="part"></param>
  572. private void SetPartAsRoot(SceneObjectPart part)
  573. {
  574. m_rootPart = part;
  575. }
  576. public void AttachToAgent(LLUUID agentID, uint attachmentpoint, LLVector3 AttachOffset)
  577. {
  578. ScenePresence avatar = m_scene.GetScenePresence(agentID);
  579. if (avatar != null)
  580. {
  581. DetachFromBackup(this);
  582. m_rootPart.m_attachedAvatar = agentID;
  583. if (m_rootPart.PhysActor != null)
  584. {
  585. m_scene.PhysicsScene.RemovePrim(m_rootPart.PhysActor);
  586. m_rootPart.PhysActor = null;
  587. }
  588. AbsolutePosition = AttachOffset;
  589. m_rootPart.m_attachedPos = AttachOffset;
  590. m_rootPart.m_IsAttachment = true;
  591. m_rootPart.SetParentLocalId(avatar.LocalId);
  592. lock (m_parts)
  593. {
  594. foreach (SceneObjectPart part in m_parts.Values)
  595. {
  596. part.SetAttachmentPoint(attachmentpoint);
  597. }
  598. }
  599. avatar.AddAttachment(this);
  600. m_rootPart.ScheduleFullUpdate();
  601. }
  602. }
  603. public byte GetAttachmentPoint()
  604. {
  605. if (m_rootPart != null)
  606. {
  607. return m_rootPart.Shape.State;
  608. }
  609. return (byte)0;
  610. }
  611. public void ClearPartAttachmentData()
  612. {
  613. foreach (SceneObjectPart part in m_parts.Values)
  614. {
  615. part.SetAttachmentPoint((Byte)0);
  616. }
  617. }
  618. public void DetachToGround()
  619. {
  620. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.m_attachedAvatar);
  621. LLVector3 detachedpos = new LLVector3(127f,127f,127f);
  622. if (avatar != null)
  623. {
  624. detachedpos = avatar.AbsolutePosition;
  625. avatar.RemoveAttachment(this);
  626. }
  627. AbsolutePosition = detachedpos;
  628. m_rootPart.m_attachedAvatar = LLUUID.Zero;
  629. m_rootPart.SetParentLocalId(0);
  630. m_rootPart.SetAttachmentPoint((byte)0);
  631. m_rootPart.m_IsAttachment = false;
  632. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  633. AttachToBackup();
  634. m_rootPart.ScheduleFullUpdate();
  635. m_rootPart.ClearUndoState();
  636. }
  637. public void DetachToInventoryPrep()
  638. {
  639. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.m_attachedAvatar);
  640. //LLVector3 detachedpos = new LLVector3(127f, 127f, 127f);
  641. if (avatar != null)
  642. {
  643. //detachedpos = avatar.AbsolutePosition;
  644. avatar.RemoveAttachment(this);
  645. }
  646. m_rootPart.m_attachedAvatar = LLUUID.Zero;
  647. m_rootPart.SetParentLocalId(0);
  648. //m_rootPart.SetAttachmentPoint((byte)0);
  649. m_rootPart.m_IsAttachment = false;
  650. AbsolutePosition = m_rootPart.m_attachedPos;
  651. //m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  652. //AttachToBackup();
  653. //m_rootPart.ScheduleFullUpdate();
  654. }
  655. /// <summary>
  656. ///
  657. /// </summary>
  658. /// <param name="part"></param>
  659. private void SetPartAsNonRoot(SceneObjectPart part)
  660. {
  661. part.ParentID = m_rootPart.LocalId;
  662. part.ClearUndoState();
  663. }
  664. /// <summary>
  665. ///
  666. /// </summary>
  667. /// <returns></returns>
  668. public List<ScenePresence> GetScenePresences()
  669. {
  670. return m_scene.GetScenePresences();
  671. }
  672. public override void UpdateMovement()
  673. {
  674. lock (m_parts)
  675. {
  676. foreach (SceneObjectPart part in m_parts.Values)
  677. {
  678. part.UpdateMovement();
  679. }
  680. }
  681. }
  682. public float GetTimeDilation()
  683. {
  684. return m_scene.TimeDilation;
  685. }
  686. /// <summary>
  687. /// Added as a way for the storage provider to reset the scene,
  688. /// most likely a better way to do this sort of thing but for now...
  689. /// </summary>
  690. /// <param name="scene"></param>
  691. public void SetScene(Scene scene)
  692. {
  693. m_scene = scene;
  694. AttachToBackup();
  695. }
  696. /// <summary>
  697. ///
  698. /// </summary>
  699. /// <param name="part"></param>
  700. public void AddPart(SceneObjectPart part)
  701. {
  702. lock (m_parts)
  703. {
  704. part.SetParent(this);
  705. part.LinkNum = m_parts.Count;
  706. try
  707. {
  708. m_parts.Add(part.UUID, part);
  709. }
  710. catch (Exception e)
  711. {
  712. m_log.Error("Failed to add scened object part", e);
  713. }
  714. }
  715. }
  716. /// <summary>
  717. ///
  718. /// </summary>
  719. public void UpdateParentIDs()
  720. {
  721. lock (m_parts)
  722. {
  723. foreach (SceneObjectPart part in m_parts.Values)
  724. {
  725. if (part.UUID != m_rootPart.UUID)
  726. {
  727. part.ParentID = m_rootPart.LocalId;
  728. }
  729. }
  730. }
  731. }
  732. public void RegenerateFullIDs()
  733. {
  734. lock (m_parts)
  735. {
  736. foreach (SceneObjectPart part in m_parts.Values)
  737. {
  738. part.UUID = LLUUID.Random();
  739. }
  740. }
  741. }
  742. // helper provided for parts.
  743. public int GetSceneMaxUndo()
  744. {
  745. if (m_scene != null)
  746. return m_scene.MaxUndoCount;
  747. return 5;
  748. }
  749. public void ResetChildPrimPhysicsPositions()
  750. {
  751. AbsolutePosition = AbsolutePosition;
  752. HasGroupChanged = false;
  753. }
  754. public LLUUID GetPartsFullID(uint localID)
  755. {
  756. SceneObjectPart part = GetChildPart(localID);
  757. if (part != null)
  758. {
  759. return part.UUID;
  760. }
  761. return null;
  762. }
  763. public void ObjectGrabHandler(uint localId, LLVector3 offsetPos, IClientAPI remoteClient)
  764. {
  765. if (m_rootPart.LocalId == localId)
  766. {
  767. OnGrabGroup(offsetPos, remoteClient);
  768. }
  769. else
  770. {
  771. SceneObjectPart part = GetChildPart(localId);
  772. OnGrabPart(part, offsetPos, remoteClient);
  773. }
  774. }
  775. public virtual void OnGrabPart(SceneObjectPart part, LLVector3 offsetPos, IClientAPI remoteClient)
  776. {
  777. part.StoreUndoState();
  778. part.OnGrab(offsetPos, remoteClient);
  779. }
  780. public virtual void OnGrabGroup(LLVector3 offsetPos, IClientAPI remoteClient)
  781. {
  782. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  783. }
  784. /// <summary>
  785. /// Completely delete this group and tell all the scene presences about that deletion.
  786. /// </summary>
  787. public void DeleteGroup()
  788. {
  789. DetachFromBackup(this);
  790. lock (m_parts)
  791. {
  792. foreach (SceneObjectPart part in m_parts.Values)
  793. {
  794. List<ScenePresence> avatars = GetScenePresences();
  795. for (int i = 0; i < avatars.Count; i++)
  796. {
  797. if (avatars[i].ParentID == LocalId)
  798. {
  799. avatars[i].StandUp();
  800. }
  801. avatars[i].ControllingClient.SendKillObject(m_regionHandle, part.LocalId);
  802. }
  803. }
  804. }
  805. }
  806. /// <summary>
  807. /// Delete all the parts in this group.
  808. /// </summary>
  809. public void DeleteParts()
  810. {
  811. lock (m_parts)
  812. {
  813. foreach (SceneObjectPart part in m_parts.Values)
  814. {
  815. part.StopScripts();
  816. }
  817. m_rootPart = null;
  818. m_parts.Clear();
  819. }
  820. }
  821. public void AddScriptLPS(int count)
  822. {
  823. InnerScene d = m_scene.m_innerScene;
  824. d.AddToScriptLPS(count);
  825. }
  826. public void AddActiveScriptCount(int count)
  827. {
  828. InnerScene d = m_scene.m_innerScene;
  829. d.AddActiveScripts(count);
  830. }
  831. public void aggregateScriptEvents()
  832. {
  833. uint objectflagupdate=(uint)RootPart.GetEffectiveObjectFlags();
  834. scriptEvents aggregateScriptEvents=0;
  835. lock (m_parts)
  836. {
  837. foreach (SceneObjectPart part in m_parts.Values)
  838. {
  839. if(part == null)
  840. continue;
  841. if(part != RootPart)
  842. part.ObjectFlags = objectflagupdate;
  843. aggregateScriptEvents |= part.m_aggregateScriptEvents;
  844. }
  845. }
  846. if ((aggregateScriptEvents & scriptEvents.at_target) != 0)
  847. {
  848. m_scriptListens_atTarget = true;
  849. }
  850. else
  851. {
  852. m_scriptListens_atTarget = false;
  853. }
  854. if ((aggregateScriptEvents & scriptEvents.not_at_target) != 0)
  855. {
  856. m_scriptListens_notAtTarget = true;
  857. }
  858. else
  859. {
  860. m_scriptListens_notAtTarget = false;
  861. }
  862. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  863. {
  864. }
  865. else
  866. {
  867. lock (m_targets)
  868. m_targets.Clear();
  869. }
  870. ScheduleGroupForFullUpdate();
  871. }
  872. public override void SetText(string text, Vector3 color, double alpha)
  873. {
  874. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  875. (int) (color.x * 0xff),
  876. (int) (color.y * 0xff),
  877. (int) (color.z * 0xff));
  878. Text = text;
  879. m_rootPart.ScheduleFullUpdate();
  880. }
  881. public void ApplyPhysics(bool m_physicalPrim)
  882. {
  883. lock (m_parts)
  884. {
  885. if (m_parts.Count > 1)
  886. {
  887. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  888. foreach (SceneObjectPart part in m_parts.Values)
  889. {
  890. if (part.LocalId != m_rootPart.LocalId)
  891. {
  892. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  893. }
  894. // Hack to get the physics scene geometries in the right spot
  895. ResetChildPrimPhysicsPositions();
  896. }
  897. }
  898. else
  899. {
  900. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  901. }
  902. }
  903. }
  904. public void SetOwnerId(LLUUID userId)
  905. {
  906. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  907. }
  908. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  909. {
  910. lock (m_parts)
  911. {
  912. foreach (SceneObjectPart part in m_parts.Values)
  913. {
  914. whatToDo(part);
  915. }
  916. }
  917. }
  918. #region Events
  919. /// <summary>
  920. ///
  921. /// </summary>
  922. public void TriggerTainted()
  923. {
  924. handlerPrimCountTainted = OnPrimCountTainted;
  925. if (handlerPrimCountTainted != null)
  926. {
  927. handlerPrimCountTainted();
  928. }
  929. }
  930. /// <summary>
  931. /// Processes backup
  932. /// </summary>
  933. /// <param name="datastore"></param>
  934. public void ProcessBackup(IRegionDataStore datastore)
  935. {
  936. if (HasGroupChanged)
  937. {
  938. datastore.StoreObject(this, m_scene.RegionInfo.RegionID);
  939. HasGroupChanged = false;
  940. }
  941. ForEachPart(delegate(SceneObjectPart part) { part.ProcessInventoryBackup(datastore); });
  942. }
  943. #endregion
  944. #region Client Updating
  945. public void SendFullUpdateToClient(IClientAPI remoteClient, uint clientFlags)
  946. {
  947. lock (m_parts)
  948. {
  949. foreach (SceneObjectPart part in m_parts.Values)
  950. {
  951. SendPartFullUpdate(remoteClient, part, clientFlags);
  952. }
  953. }
  954. }
  955. /// <summary>
  956. /// Send a full update to the client for the given part
  957. /// </summary>
  958. /// <param name="remoteClient"></param>
  959. /// <param name="part"></param>
  960. internal void SendPartFullUpdate(IClientAPI remoteClient, SceneObjectPart part, uint clientFlags)
  961. {
  962. if (m_rootPart.UUID == part.UUID)
  963. {
  964. if (m_rootPart.m_IsAttachment)
  965. {
  966. part.SendFullUpdateToClient(remoteClient, m_rootPart.m_attachedPos, clientFlags);
  967. }
  968. else
  969. {
  970. part.SendFullUpdateToClient(remoteClient, AbsolutePosition, clientFlags);
  971. }
  972. }
  973. else
  974. {
  975. part.SendFullUpdateToClient(remoteClient, clientFlags);
  976. }
  977. }
  978. /// <summary>
  979. /// Send a terse update to the client for the given part
  980. /// </summary>
  981. /// <param name="remoteClient"></param>
  982. /// <param name="part"></param>
  983. internal void SendPartTerseUpdate(IClientAPI remoteClient, SceneObjectPart part)
  984. {
  985. if (m_rootPart.UUID == part.UUID)
  986. {
  987. if (m_rootPart.m_IsAttachment)
  988. {
  989. part.SendTerseUpdateToClient(remoteClient, m_rootPart.m_attachedPos);
  990. }
  991. else
  992. {
  993. part.SendTerseUpdateToClient(remoteClient, AbsolutePosition);
  994. }
  995. }
  996. else
  997. {
  998. part.SendTerseUpdateToClient(remoteClient);
  999. }
  1000. }
  1001. #endregion
  1002. #region Copying
  1003. /// <summary>
  1004. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1005. /// </summary>
  1006. /// <returns></returns>
  1007. public SceneObjectGroup Copy(LLUUID cAgentID, LLUUID cGroupID)
  1008. {
  1009. SceneObjectGroup dupe = (SceneObjectGroup) MemberwiseClone();
  1010. dupe.m_parts = new Dictionary<LLUUID, SceneObjectPart>();
  1011. dupe.m_parts.Clear();
  1012. //dupe.OwnerID = AgentID;
  1013. //dupe.GroupID = GroupID;
  1014. dupe.AbsolutePosition = new LLVector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  1015. dupe.m_scene = m_scene;
  1016. dupe.m_regionHandle = m_regionHandle;
  1017. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID);
  1018. dupe.m_rootPart.TrimPermissions();
  1019. /// may need to create a new Physics actor.
  1020. if (dupe.RootPart.PhysActor != null)
  1021. {
  1022. PrimitiveBaseShape pbs = dupe.RootPart.Shape;
  1023. dupe.RootPart.PhysActor = m_scene.PhysicsScene.AddPrimShape(
  1024. dupe.RootPart.Name,
  1025. pbs,
  1026. new PhysicsVector(dupe.RootPart.AbsolutePosition.X, dupe.RootPart.AbsolutePosition.Y,
  1027. dupe.RootPart.AbsolutePosition.Z),
  1028. new PhysicsVector(dupe.RootPart.Scale.X, dupe.RootPart.Scale.Y, dupe.RootPart.Scale.Z),
  1029. new Quaternion(dupe.RootPart.RotationOffset.W, dupe.RootPart.RotationOffset.X,
  1030. dupe.RootPart.RotationOffset.Y, dupe.RootPart.RotationOffset.Z),
  1031. dupe.RootPart.PhysActor.IsPhysical);
  1032. dupe.RootPart.PhysActor.LocalID = dupe.RootPart.LocalId;
  1033. dupe.RootPart.DoPhysicsPropertyUpdate(dupe.RootPart.PhysActor.IsPhysical, true);
  1034. }
  1035. // Now we've made a copy that replaces this one, we need to
  1036. // switch the owner to the person who did the copying
  1037. // Second Life copies an object and duplicates the first one in it's place
  1038. // So, we have to make a copy of this one, set it in it's place then set the owner on this one
  1039. SetRootPartOwner(m_rootPart, cAgentID, cGroupID);
  1040. m_rootPart.ScheduleFullUpdate();
  1041. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.Values);
  1042. foreach (SceneObjectPart part in partList)
  1043. {
  1044. if (part.UUID != m_rootPart.UUID)
  1045. {
  1046. dupe.CopyPart(part, OwnerID, GroupID);
  1047. SetPartOwner(part, cAgentID, cGroupID);
  1048. part.ScheduleFullUpdate();
  1049. }
  1050. }
  1051. dupe.UpdateParentIDs();
  1052. dupe.AttachToBackup();
  1053. ScheduleGroupForFullUpdate();
  1054. return dupe;
  1055. }
  1056. /// <summary>
  1057. ///
  1058. /// </summary>
  1059. /// <param name="part"></param>
  1060. /// <param name="cAgentID"></param>
  1061. /// <param name="cGroupID"></param>
  1062. public void CopyRootPart(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID)
  1063. {
  1064. SceneObjectPart newPart = part.Copy(m_scene.PrimIDAllocate(), OwnerID, GroupID, m_parts.Count);
  1065. newPart.SetParent(this);
  1066. lock (m_parts)
  1067. {
  1068. m_parts.Add(newPart.UUID, newPart);
  1069. }
  1070. SetPartAsRoot(newPart);
  1071. }
  1072. public void ScriptSetPhysicsStatus(bool UsePhysics)
  1073. {
  1074. if (m_scene.m_physicalPrim)
  1075. {
  1076. lock (m_parts)
  1077. {
  1078. foreach (SceneObjectPart part in m_parts.Values)
  1079. {
  1080. if (UsePhysics)
  1081. part.AddFlag(LLObject.ObjectFlags.Physics);
  1082. else
  1083. part.RemFlag(LLObject.ObjectFlags.Physics);
  1084. part.DoPhysicsPropertyUpdate(UsePhysics, false);
  1085. IsSelected = false;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. public void ScriptSetPhantomStatus(bool PhantomStatus)
  1091. {
  1092. lock (m_parts)
  1093. {
  1094. foreach (SceneObjectPart part in m_parts.Values)
  1095. {
  1096. if (PhantomStatus)
  1097. {
  1098. part.AddFlag(LLObject.ObjectFlags.Phantom);
  1099. if (part.PhysActor != null)
  1100. {
  1101. m_scene.PhysicsScene.RemovePrim(part.PhysActor);
  1102. }
  1103. }
  1104. else
  1105. {
  1106. part.RemFlag(LLObject.ObjectFlags.Phantom);
  1107. if ((part.GetEffectiveObjectFlags() & (int) LLObject.ObjectFlags.Physics) != 0)
  1108. {
  1109. part.DoPhysicsPropertyUpdate(true, false);
  1110. }
  1111. }
  1112. }
  1113. }
  1114. }
  1115. public void applyImpulse(PhysicsVector impulse)
  1116. {
  1117. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1118. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1119. // Make sure we don't do that!
  1120. SceneObjectPart rootpart = m_rootPart;
  1121. if (rootpart != null)
  1122. {
  1123. if (rootpart.PhysActor != null)
  1124. {
  1125. rootpart.PhysActor.AddForce(impulse);
  1126. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1127. }
  1128. }
  1129. }
  1130. public void moveToTarget(LLVector3 target, float tau)
  1131. {
  1132. SceneObjectPart rootpart = m_rootPart;
  1133. if (rootpart != null)
  1134. {
  1135. if (rootpart.PhysActor != null)
  1136. {
  1137. rootpart.PhysActor.PIDTarget = new PhysicsVector(target.X, target.Y, target.Z);
  1138. rootpart.PhysActor.PIDTau = tau;
  1139. rootpart.PhysActor.PIDActive = true;
  1140. }
  1141. }
  1142. }
  1143. public void stopMoveToTarget()
  1144. {
  1145. SceneObjectPart rootpart = m_rootPart;
  1146. if (rootpart != null)
  1147. {
  1148. rootpart.PhysActor.PIDActive = false;
  1149. }
  1150. }
  1151. public void SetRootPartOwner(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID)
  1152. {
  1153. part.LastOwnerID = part.OwnerID;
  1154. part.OwnerID = cAgentID;
  1155. part.GroupID = cGroupID;
  1156. if (part.OwnerID != cAgentID)
  1157. {
  1158. // Apply Next Owner Permissions if we're not bypassing permissions
  1159. if (!m_scene.PermissionsMngr.BypassPermissions)
  1160. m_rootPart.ApplyNextOwnerPermissions();
  1161. }
  1162. part.ScheduleFullUpdate();
  1163. }
  1164. /// <summary>
  1165. ///
  1166. /// </summary>
  1167. /// <param name="part"></param>
  1168. /// <param name="cAgentID"></param>
  1169. /// <param name="cGroupID"></param>
  1170. public void CopyPart(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID)
  1171. {
  1172. SceneObjectPart newPart = part.Copy(m_scene.PrimIDAllocate(), OwnerID, GroupID, m_parts.Count);
  1173. newPart.SetParent(this);
  1174. lock (m_parts)
  1175. {
  1176. m_parts.Add(newPart.UUID, newPart);
  1177. }
  1178. SetPartAsNonRoot(newPart);
  1179. }
  1180. /// <summary>
  1181. /// Reset the LLUUIDs for all the prims that make up this group.
  1182. ///
  1183. /// This is called by methods which want to add a new group to an existing scene, in order
  1184. /// to ensure that there are no clashes with groups already present.
  1185. /// </summary>
  1186. public void ResetIDs()
  1187. {
  1188. // As this is only ever called for prims which are not currently part of the scene (and hence
  1189. // not accessible by clients), there should be no need to lock
  1190. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.Values);
  1191. m_parts.Clear();
  1192. foreach (SceneObjectPart part in partsList)
  1193. {
  1194. part.ResetIDs(m_parts.Count);
  1195. m_parts.Add(part.UUID, part);
  1196. }
  1197. }
  1198. /// <summary>
  1199. ///
  1200. /// </summary>
  1201. /// <param name="part"></param>
  1202. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, LLUUID AgentID, uint RequestFlags)
  1203. {
  1204. //RootPart.ServiceObjectPropertiesFamilyRequest(remoteClient, AgentID, RequestFlags);
  1205. ObjectPropertiesFamilyPacket objPropFamilyPack = (ObjectPropertiesFamilyPacket) PacketPool.Instance.GetPacket(PacketType.ObjectPropertiesFamily);
  1206. // TODO: don't create new blocks if recycling an old packet
  1207. ObjectPropertiesFamilyPacket.ObjectDataBlock objPropDB = new ObjectPropertiesFamilyPacket.ObjectDataBlock();
  1208. objPropDB.RequestFlags = RequestFlags;
  1209. objPropDB.ObjectID = RootPart.UUID;
  1210. objPropDB.OwnerID = RootPart.ObjectOwner;
  1211. objPropDB.GroupID = RootPart.GroupID;
  1212. objPropDB.BaseMask = RootPart.BaseMask;
  1213. objPropDB.OwnerMask = RootPart.OwnerMask;
  1214. objPropDB.GroupMask = RootPart.GroupMask;
  1215. objPropDB.EveryoneMask = RootPart.EveryoneMask;
  1216. objPropDB.NextOwnerMask = RootPart.NextOwnerMask;
  1217. // TODO: More properties are needed in SceneObjectPart!
  1218. objPropDB.OwnershipCost = RootPart.OwnershipCost;
  1219. objPropDB.SaleType = RootPart.ObjectSaleType;
  1220. objPropDB.SalePrice = RootPart.SalePrice;
  1221. objPropDB.Category = RootPart.Category;
  1222. objPropDB.LastOwnerID = RootPart.CreatorID;
  1223. objPropDB.Name = Helpers.StringToField(RootPart.Name);
  1224. objPropDB.Description = Helpers.StringToField(RootPart.Description);
  1225. objPropFamilyPack.ObjectData = objPropDB;
  1226. remoteClient.OutPacket(objPropFamilyPack, ThrottleOutPacketType.Task);
  1227. }
  1228. public void SetPartOwner(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID)
  1229. {
  1230. part.OwnerID = cAgentID;
  1231. part.GroupID = cGroupID;
  1232. }
  1233. #endregion
  1234. #region Scheduling
  1235. /// <summary>
  1236. /// Examine this object's parts to see if they've changed sufficiently to warrant an update
  1237. /// </summary>
  1238. public override void Update()
  1239. {
  1240. lock (m_parts)
  1241. {
  1242. //if (m_rootPart.m_IsAttachment)
  1243. //{
  1244. //foreach (SceneObjectPart part in m_parts.Values)
  1245. //{
  1246. //part.SendScheduledUpdates();
  1247. //}
  1248. //return;
  1249. //}
  1250. if (Util.GetDistanceTo(lastPhysGroupPos, AbsolutePosition) > 0.02)
  1251. {
  1252. m_rootPart.UpdateFlag = 1;
  1253. lastPhysGroupPos = AbsolutePosition;
  1254. }
  1255. //foreach (SceneObjectPart part in m_parts.Values)
  1256. //{
  1257. //if (part.UpdateFlag == 0) part.UpdateFlag = 1;
  1258. //}
  1259. checkAtTargets();
  1260. if ((Math.Abs(lastPhysGroupRot.W - GroupRotation.W) > 0.1)
  1261. || (Math.Abs(lastPhysGroupRot.X - GroupRotation.X) > 0.1)
  1262. || (Math.Abs(lastPhysGroupRot.Y - GroupRotation.Y) > 0.1)
  1263. || (Math.Abs(lastPhysGroupRot.Z - GroupRotation.Z) > 0.1))
  1264. {
  1265. m_rootPart.UpdateFlag = 1;
  1266. lastPhysGroupRot = GroupRotation;
  1267. }
  1268. foreach (SceneObjectPart part in m_parts.Values)
  1269. {
  1270. part.SendScheduledUpdates();
  1271. }
  1272. }
  1273. }
  1274. public void ScheduleFullUpdateToAvatar(ScenePresence presence)
  1275. {
  1276. lock (m_parts)
  1277. {
  1278. foreach (SceneObjectPart part in m_parts.Values)
  1279. {
  1280. part.AddFullUpdateToAvatar(presence);
  1281. }
  1282. }
  1283. }
  1284. public void ScheduleTerseUpdateToAvatar(ScenePresence presence)
  1285. {
  1286. lock (m_parts)
  1287. {
  1288. foreach (SceneObjectPart part in m_parts.Values)
  1289. {
  1290. part.AddTerseUpdateToAvatar(presence);
  1291. }
  1292. }
  1293. }
  1294. /// <summary>
  1295. /// Schedule a full update for every part in this object
  1296. /// </summary>
  1297. public void ScheduleGroupForFullUpdate()
  1298. {
  1299. HasGroupChanged = true;
  1300. checkAtTargets();
  1301. lock (m_parts)
  1302. {
  1303. foreach (SceneObjectPart part in m_parts.Values)
  1304. {
  1305. part.ScheduleFullUpdate();
  1306. }
  1307. }
  1308. }
  1309. /// <summary>
  1310. ///
  1311. /// </summary>
  1312. public void ScheduleGroupForTerseUpdate()
  1313. {
  1314. HasGroupChanged = true;
  1315. lock (m_parts)
  1316. {
  1317. foreach (SceneObjectPart part in m_parts.Values)
  1318. {
  1319. part.ScheduleTerseUpdate();
  1320. }
  1321. }
  1322. }
  1323. /// <summary>
  1324. ///
  1325. /// </summary>
  1326. public void SendGroupFullUpdate()
  1327. {
  1328. HasGroupChanged = true;
  1329. lock (m_parts)
  1330. {
  1331. foreach (SceneObjectPart part in m_parts.Values)
  1332. {
  1333. part.SendFullUpdateToAllClients();
  1334. }
  1335. }
  1336. }
  1337. public void QueueForUpdateCheck()
  1338. {
  1339. m_scene.m_innerScene.AddToUpdateList(this);
  1340. }
  1341. /// <summary>
  1342. ///
  1343. /// </summary>
  1344. public void SendGroupTerseUpdate()
  1345. {
  1346. HasGroupChanged = true;
  1347. lock (m_parts)
  1348. {
  1349. foreach (SceneObjectPart part in m_parts.Values)
  1350. {
  1351. part.SendTerseUpdateToAllClients();
  1352. }
  1353. }
  1354. }
  1355. #endregion
  1356. #region SceneGroupPart Methods
  1357. /// <summary>
  1358. /// Get the child part by LinkNum
  1359. /// </summary>
  1360. /// <param name="linknum"></param>
  1361. /// <returns>null if no child part with that linknum or child part</returns>
  1362. public SceneObjectPart GetLinkNumPart(int linknum)
  1363. {
  1364. lock (m_parts)
  1365. {
  1366. foreach (SceneObjectPart part in m_parts.Values)
  1367. {
  1368. if (part.LinkNum == linknum)
  1369. {
  1370. return part;
  1371. }
  1372. }
  1373. }
  1374. return null;
  1375. }
  1376. /// <summary>
  1377. /// Get a child part with a given UUID
  1378. /// </summary>
  1379. /// <param name="primID"></param>
  1380. /// <returns>null if a child part with the primID was not found</returns>
  1381. public SceneObjectPart GetChildPart(LLUUID primID)
  1382. {
  1383. SceneObjectPart childPart = null;
  1384. if (m_parts.ContainsKey(primID))
  1385. {
  1386. childPart = m_parts[primID];
  1387. }
  1388. return childPart;
  1389. }
  1390. /// <summary>
  1391. /// Get a child part with a given local ID
  1392. /// </summary>
  1393. /// <param name="localID"></param>
  1394. /// <returns>null if a child part with the local ID was not found</returns>
  1395. public SceneObjectPart GetChildPart(uint localID)
  1396. {
  1397. lock (m_parts)
  1398. {
  1399. foreach (SceneObjectPart part in m_parts.Values)
  1400. {
  1401. if (part.LocalId == localID)
  1402. {
  1403. return part;
  1404. }
  1405. }
  1406. }
  1407. return null;
  1408. }
  1409. /// <summary>
  1410. /// Does this group contain the child prim
  1411. /// should be able to remove these methods once we have a entity index in scene
  1412. /// </summary>
  1413. /// <param name="primID"></param>
  1414. /// <returns></returns>
  1415. public bool HasChildPrim(LLUUID primID)
  1416. {
  1417. if (m_parts.ContainsKey(primID))
  1418. {
  1419. return true;
  1420. }
  1421. return false;
  1422. }
  1423. /// <summary>
  1424. /// Does this group contain the child prim
  1425. /// should be able to remove these methods once we have a entity index in scene
  1426. /// </summary>
  1427. /// <param name="localID"></param>
  1428. /// <returns></returns>
  1429. public bool HasChildPrim(uint localID)
  1430. {
  1431. lock (m_parts)
  1432. {
  1433. foreach (SceneObjectPart part in m_parts.Values)
  1434. {
  1435. if (part.LocalId == localID)
  1436. {
  1437. return true;
  1438. }
  1439. }
  1440. }
  1441. return false;
  1442. }
  1443. #endregion
  1444. #region Packet Handlers
  1445. /// <summary>
  1446. /// Link the prims in a given group to this group
  1447. /// </summary>
  1448. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1449. public void LinkToGroup(SceneObjectGroup objectGroup)
  1450. {
  1451. if (objectGroup.RootPart.UpdateFlag > 0)
  1452. {
  1453. // I've never actually seen this happen, though I think it's theoretically possible
  1454. m_log.WarnFormat(
  1455. "[SCENE OBJECT GROUP]: Aborted linking {0}, {1} to {2}, {3} as it has yet to finish delinking",
  1456. objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1457. return;
  1458. }
  1459. // m_log.DebugFormat(
  1460. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1461. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1462. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1463. Vector3 oldGroupPosition =
  1464. new Vector3(linkPart.GroupPosition.X, linkPart.GroupPosition.Y, linkPart.GroupPosition.Z);
  1465. Quaternion oldRootRotation =
  1466. new Quaternion(linkPart.RotationOffset.W, linkPart.RotationOffset.X, linkPart.RotationOffset.Y,
  1467. linkPart.RotationOffset.Z);
  1468. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1469. linkPart.GroupPosition = AbsolutePosition;
  1470. Vector3 axPos = new Vector3(linkPart.OffsetPosition.X, linkPart.OffsetPosition.Y, linkPart.OffsetPosition.Z);
  1471. Quaternion parentRot =
  1472. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1473. m_rootPart.RotationOffset.Z);
  1474. axPos = parentRot.Inverse() * axPos;
  1475. linkPart.OffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  1476. Quaternion oldRot =
  1477. new Quaternion(linkPart.RotationOffset.W, linkPart.RotationOffset.X, linkPart.RotationOffset.Y,
  1478. linkPart.RotationOffset.Z);
  1479. Quaternion newRot = parentRot.Inverse() * oldRot;
  1480. linkPart.RotationOffset = new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  1481. linkPart.ParentID = m_rootPart.LocalId;
  1482. linkPart.LinkNum = m_parts.Count;
  1483. lock (m_parts)
  1484. {
  1485. m_parts.Add(linkPart.UUID, linkPart);
  1486. }
  1487. linkPart.SetParent(this);
  1488. //if (linkPart.PhysActor != null)
  1489. //{
  1490. // m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1491. //linkPart.PhysActor = null;
  1492. //}
  1493. //TODO: rest of parts
  1494. foreach (SceneObjectPart part in objectGroup.Children.Values)
  1495. {
  1496. if (part.UUID != objectGroup.m_rootPart.UUID)
  1497. {
  1498. LinkNonRootPart(part, oldGroupPosition, oldRootRotation);
  1499. }
  1500. part.ClearUndoState();
  1501. }
  1502. DetachFromBackup(objectGroup);
  1503. m_scene.DeleteEntity(objectGroup.UUID);
  1504. // TODO Deleting the parts may cause problems later on if they have already
  1505. // made it into the update queue. However, sending out updates for those parts is now
  1506. // spurious, so it would be good not to send them at some point.
  1507. // The traffic caused is always going to be pretty minor, so it's not high priority
  1508. //objectGroup.DeleteParts();
  1509. ScheduleGroupForFullUpdate();
  1510. }
  1511. /// <summary>
  1512. /// Delink the given prim from this group. The delinked prim is established as
  1513. /// an independent SceneObjectGroup.
  1514. /// </summary>
  1515. /// <param name="partID"></param>
  1516. public void DelinkFromGroup(uint partID)
  1517. {
  1518. SceneObjectPart linkPart = GetChildPart(partID);
  1519. if (null != linkPart)
  1520. {
  1521. linkPart.ClearUndoState();
  1522. // m_log.DebugFormat(
  1523. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  1524. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  1525. LLQuaternion worldRot = linkPart.GetWorldRotation();
  1526. // Remove the part from this object
  1527. lock (m_parts)
  1528. {
  1529. m_parts.Remove(linkPart.UUID);
  1530. }
  1531. linkPart.ParentID = 0;
  1532. if (linkPart.PhysActor != null)
  1533. {
  1534. m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1535. }
  1536. // We need to reset the child part's position
  1537. // ready for life as a separate object after being a part of another object
  1538. Quaternion parentRot
  1539. = new Quaternion(
  1540. m_rootPart.RotationOffset.W,
  1541. m_rootPart.RotationOffset.X,
  1542. m_rootPart.RotationOffset.Y,
  1543. m_rootPart.RotationOffset.Z);
  1544. Vector3 axPos
  1545. = new Vector3(
  1546. linkPart.OffsetPosition.X,
  1547. linkPart.OffsetPosition.Y,
  1548. linkPart.OffsetPosition.Z);
  1549. axPos = parentRot * axPos;
  1550. linkPart.OffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  1551. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  1552. linkPart.OffsetPosition = new LLVector3(0, 0, 0);
  1553. linkPart.RotationOffset = worldRot;
  1554. // This chunk is probably unnecesary now - delete later on
  1555. /*
  1556. Quaternion oldRot
  1557. = new Quaternion(
  1558. linkPart.RotationOffset.W,
  1559. linkPart.RotationOffset.X,
  1560. linkPart.RotationOffset.Y,
  1561. linkPart.RotationOffset.Z);
  1562. Quaternion newRot = parentRot*oldRot;
  1563. linkPart.RotationOffset = new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  1564. */
  1565. // Add physics information back to delinked part if appropriate
  1566. // XXX This is messy and should be refactorable with the similar section in
  1567. // SceneObjectPart.UpdatePrimFlags()
  1568. //if (m_rootPart.PhysActor != null)
  1569. //{
  1570. //linkPart.PhysActor = m_scene.PhysicsScene.AddPrimShape(
  1571. //linkPart.Name,
  1572. //linkPart.Shape,
  1573. //new PhysicsVector(linkPart.AbsolutePosition.X, linkPart.AbsolutePosition.Y,
  1574. //linkPart.AbsolutePosition.Z),
  1575. //new PhysicsVector(linkPart.Scale.X, linkPart.Scale.Y, linkPart.Scale.Z),
  1576. //new Quaternion(linkPart.RotationOffset.W, linkPart.RotationOffset.X,
  1577. //linkPart.RotationOffset.Y, linkPart.RotationOffset.Z),
  1578. //m_rootPart.PhysActor.IsPhysical);
  1579. //m_rootPart.DoPhysicsPropertyUpdate(m_rootPart.PhysActor.IsPhysical, true);
  1580. //}
  1581. SceneObjectGroup objectGroup = new SceneObjectGroup(m_scene, m_regionHandle, linkPart);
  1582. m_scene.AddEntity(objectGroup);
  1583. ScheduleGroupForFullUpdate();
  1584. }
  1585. else
  1586. {
  1587. m_log.InfoFormat("[SCENE OBJECT GROUP]: " +
  1588. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  1589. partID, LocalId, UUID);
  1590. }
  1591. }
  1592. private void DetachFromBackup(SceneObjectGroup objectGroup)
  1593. {
  1594. m_scene.EventManager.OnBackup -= objectGroup.ProcessBackup;
  1595. }
  1596. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation)
  1597. {
  1598. part.SetParent(this);
  1599. part.ParentID = m_rootPart.LocalId;
  1600. part.LinkNum = m_parts.Count;
  1601. lock (m_parts)
  1602. {
  1603. m_parts.Add(part.UUID, part);
  1604. }
  1605. Vector3 axiomOldPos = new Vector3(part.OffsetPosition.X, part.OffsetPosition.Y, part.OffsetPosition.Z);
  1606. axiomOldPos = oldGroupRotation * axiomOldPos;
  1607. axiomOldPos += oldGroupPosition;
  1608. LLVector3 oldAbsolutePosition = new LLVector3(axiomOldPos.x, axiomOldPos.y, axiomOldPos.z);
  1609. part.OffsetPosition = oldAbsolutePosition - AbsolutePosition;
  1610. Quaternion axiomRootRotation =
  1611. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1612. m_rootPart.RotationOffset.Z);
  1613. Vector3 axiomPos = new Vector3(part.OffsetPosition.X, part.OffsetPosition.Y, part.OffsetPosition.Z);
  1614. axiomPos = axiomRootRotation.Inverse() * axiomPos;
  1615. part.OffsetPosition = new LLVector3(axiomPos.x, axiomPos.y, axiomPos.z);
  1616. Quaternion axiomPartRotation =
  1617. new Quaternion(part.RotationOffset.W, part.RotationOffset.X, part.RotationOffset.Y,
  1618. part.RotationOffset.Z);
  1619. axiomPartRotation = oldGroupRotation * axiomPartRotation;
  1620. axiomPartRotation = axiomRootRotation.Inverse() * axiomPartRotation;
  1621. part.RotationOffset =
  1622. new LLQuaternion(axiomPartRotation.x, axiomPartRotation.y, axiomPartRotation.z, axiomPartRotation.w);
  1623. }
  1624. /// <summary>
  1625. /// If object is physical, apply force to move it around
  1626. /// If object is not physical, just put it at the resulting location
  1627. /// </summary>
  1628. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  1629. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  1630. /// <param name="remoteClient"></param>
  1631. public void GrabMovement(LLVector3 offset, LLVector3 pos, IClientAPI remoteClient)
  1632. {
  1633. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  1634. {
  1635. if (m_rootPart.PhysActor != null)
  1636. {
  1637. if (m_rootPart.PhysActor.IsPhysical)
  1638. {
  1639. LLVector3 llmoveforce = pos - AbsolutePosition;
  1640. PhysicsVector grabforce = new PhysicsVector(llmoveforce.X, llmoveforce.Y, llmoveforce.Z);
  1641. grabforce = (grabforce / 10) * m_rootPart.PhysActor.Mass;
  1642. m_rootPart.PhysActor.AddForce(grabforce);
  1643. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1644. }
  1645. else
  1646. {
  1647. //NonPhysicalGrabMovement(pos);
  1648. }
  1649. }
  1650. else
  1651. {
  1652. //NonPhysicalGrabMovement(pos);
  1653. }
  1654. }
  1655. }
  1656. public void NonPhysicalGrabMovement(LLVector3 pos)
  1657. {
  1658. AbsolutePosition = pos;
  1659. m_rootPart.SendTerseUpdateToAllClients();
  1660. }
  1661. /// <summary>
  1662. /// Return metadata about a prim (name, description, sale price, etc.)
  1663. /// </summary>
  1664. /// <param name="client"></param>
  1665. public void GetProperties(IClientAPI client)
  1666. {
  1667. ObjectPropertiesPacket proper = (ObjectPropertiesPacket) PacketPool.Instance.GetPacket(PacketType.ObjectProperties);
  1668. // TODO: don't create new blocks if recycling an old packet
  1669. proper.ObjectData = new ObjectPropertiesPacket.ObjectDataBlock[1];
  1670. proper.ObjectData[0] = new ObjectPropertiesPacket.ObjectDataBlock();
  1671. proper.ObjectData[0].ItemID = LLUUID.Zero;
  1672. proper.ObjectData[0].CreationDate = (ulong) m_rootPart.CreationDate;
  1673. proper.ObjectData[0].CreatorID = m_rootPart.CreatorID;
  1674. proper.ObjectData[0].FolderID = LLUUID.Zero;
  1675. proper.ObjectData[0].FromTaskID = LLUUID.Zero;
  1676. proper.ObjectData[0].GroupID = LLUUID.Zero;
  1677. proper.ObjectData[0].InventorySerial = (short) m_rootPart.InventorySerial;
  1678. proper.ObjectData[0].LastOwnerID = m_rootPart.LastOwnerID;
  1679. // proper.ObjectData[0].LastOwnerID = LLUUID.Zero;
  1680. proper.ObjectData[0].ObjectID = UUID;
  1681. proper.ObjectData[0].OwnerID = m_rootPart.OwnerID;
  1682. proper.ObjectData[0].TouchName = Helpers.StringToField(m_rootPart.TouchName);
  1683. proper.ObjectData[0].TextureID = new byte[0];
  1684. proper.ObjectData[0].SitName = Helpers.StringToField(m_rootPart.SitName);
  1685. proper.ObjectData[0].Name = Helpers.StringToField(m_rootPart.Name);
  1686. proper.ObjectData[0].Description = Helpers.StringToField(m_rootPart.Description);
  1687. proper.ObjectData[0].OwnerMask = m_rootPart.OwnerMask;
  1688. proper.ObjectData[0].NextOwnerMask = m_rootPart.NextOwnerMask;
  1689. proper.ObjectData[0].GroupMask = m_rootPart.GroupMask;
  1690. proper.ObjectData[0].EveryoneMask = m_rootPart.EveryoneMask;
  1691. proper.ObjectData[0].BaseMask = m_rootPart.BaseMask;
  1692. // proper.ObjectData[0].AggregatePerms = 53;
  1693. // proper.ObjectData[0].AggregatePermTextures = 0;
  1694. // proper.ObjectData[0].AggregatePermTexturesOwner = 0;
  1695. client.OutPacket(proper, ThrottleOutPacketType.Task);
  1696. }
  1697. /// <summary>
  1698. /// Set the name of a prim
  1699. /// </summary>
  1700. /// <param name="name"></param>
  1701. /// <param name="localID"></param>
  1702. public void SetPartName(string name, uint localID)
  1703. {
  1704. SceneObjectPart part = GetChildPart(localID);
  1705. if (part != null)
  1706. {
  1707. part.Name = name;
  1708. }
  1709. }
  1710. public void SetPartDescription(string des, uint localID)
  1711. {
  1712. SceneObjectPart part = GetChildPart(localID);
  1713. if (part != null)
  1714. {
  1715. part.Description = des;
  1716. }
  1717. }
  1718. public void SetPartText(string text, uint localID)
  1719. {
  1720. SceneObjectPart part = GetChildPart(localID);
  1721. if (part != null)
  1722. {
  1723. part.SetText(text);
  1724. }
  1725. }
  1726. public void SetPartText(string text, LLUUID partID)
  1727. {
  1728. SceneObjectPart part = GetChildPart(partID);
  1729. if (part != null)
  1730. {
  1731. part.SetText(text);
  1732. }
  1733. }
  1734. public string GetPartName(uint localID)
  1735. {
  1736. SceneObjectPart part = GetChildPart(localID);
  1737. if (part != null)
  1738. {
  1739. return part.Name;
  1740. }
  1741. return String.Empty;
  1742. }
  1743. public string GetPartDescription(uint localID)
  1744. {
  1745. SceneObjectPart part = GetChildPart(localID);
  1746. if (part != null)
  1747. {
  1748. return part.Description;
  1749. }
  1750. return String.Empty;
  1751. }
  1752. /// <summary>
  1753. ///
  1754. /// </summary>
  1755. /// <param name="localID"></param>
  1756. /// <param name="type"></param>
  1757. /// <param name="inUse"></param>
  1758. /// <param name="data"></param>
  1759. ///
  1760. public void UpdatePrimFlags(uint localID, ushort type, bool inUse, byte[] data)
  1761. {
  1762. SceneObjectPart part = GetChildPart(localID);
  1763. if (part != null)
  1764. {
  1765. // If we have children
  1766. lock (m_parts)
  1767. {
  1768. if (m_parts.Count > 1)
  1769. {
  1770. foreach (SceneObjectPart parts in m_parts.Values)
  1771. {
  1772. parts.UpdatePrimFlags(type, inUse, data);
  1773. }
  1774. }
  1775. else
  1776. {
  1777. part.UpdatePrimFlags(type, inUse, data);
  1778. }
  1779. }
  1780. }
  1781. }
  1782. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  1783. {
  1784. SceneObjectPart part = GetChildPart(localID);
  1785. if (part != null)
  1786. {
  1787. part.UpdateExtraParam(type, inUse, data);
  1788. }
  1789. }
  1790. public SceneObjectPart[] GetParts()
  1791. {
  1792. int numParts = Children.Count;
  1793. SceneObjectPart[] partArray = new SceneObjectPart[numParts];
  1794. Children.Values.CopyTo(partArray, 0);
  1795. return partArray;
  1796. }
  1797. /// <summary>
  1798. ///
  1799. /// </summary>
  1800. /// <param name="localID"></param>
  1801. /// <param name="textureEntry"></param>
  1802. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  1803. {
  1804. SceneObjectPart part = GetChildPart(localID);
  1805. if (part != null)
  1806. {
  1807. part.UpdateTextureEntry(textureEntry);
  1808. }
  1809. }
  1810. public void UpdatePermissions(LLUUID AgentID, byte field, uint localID, uint mask, byte addRemTF)
  1811. {
  1812. SceneObjectPart updatePart = GetChildPart(localID);
  1813. updatePart.UpdatePermissions(AgentID, field, localID, mask, addRemTF);
  1814. HasGroupChanged = true;
  1815. }
  1816. #endregion
  1817. #region Shape
  1818. /// <summary>
  1819. ///
  1820. /// </summary>
  1821. /// <param name="shapeBlock"></param>
  1822. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  1823. {
  1824. SceneObjectPart part = GetChildPart(localID);
  1825. if (part != null)
  1826. {
  1827. part.UpdateShape(shapeBlock);
  1828. if (part.PhysActor != null)
  1829. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1830. }
  1831. }
  1832. #endregion
  1833. #region Resize
  1834. /// <summary>
  1835. ///
  1836. /// </summary>
  1837. /// <param name="scale"></param>
  1838. /// <param name="localID"></param>
  1839. public void Resize(LLVector3 scale, uint localID)
  1840. {
  1841. SceneObjectPart part = GetChildPart(localID);
  1842. if (part != null)
  1843. {
  1844. part.Resize(scale);
  1845. if (part.PhysActor != null)
  1846. {
  1847. part.PhysActor.Size =
  1848. new PhysicsVector(scale.X, scale.Y, scale.Z);
  1849. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1850. }
  1851. //if (part.UUID != m_rootPart.UUID)
  1852. ScheduleGroupForFullUpdate();
  1853. //if (part.UUID == m_rootPart.UUID)
  1854. //{
  1855. //if (m_rootPart.PhysActor != null)
  1856. //{
  1857. //m_rootPart.PhysActor.Size =
  1858. //new PhysicsVector(m_rootPart.Scale.X, m_rootPart.Scale.Y, m_rootPart.Scale.Z);
  1859. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1860. //}
  1861. //}
  1862. }
  1863. }
  1864. public void GroupResize(LLVector3 scale, uint localID)
  1865. {
  1866. SceneObjectPart part = GetChildPart(localID);
  1867. if (part != null)
  1868. {
  1869. float x = (scale.X / part.Scale.X);
  1870. float y = (scale.Y / part.Scale.Y);
  1871. float z = (scale.Z / part.Scale.Z);
  1872. part.Resize(scale);
  1873. lock (m_parts)
  1874. {
  1875. foreach (SceneObjectPart obPart in m_parts.Values)
  1876. {
  1877. if (obPart.UUID != m_rootPart.UUID)
  1878. {
  1879. LLVector3 currentpos = new LLVector3(obPart.OffsetPosition);
  1880. currentpos.X *= x;
  1881. currentpos.Y *= y;
  1882. currentpos.Z *= z;
  1883. LLVector3 newSize = new LLVector3(obPart.Scale);
  1884. newSize.X *= x;
  1885. newSize.Y *= y;
  1886. newSize.Z *= z;
  1887. obPart.Resize(newSize);
  1888. obPart.UpdateOffSet(currentpos);
  1889. }
  1890. }
  1891. }
  1892. if (part.PhysActor != null)
  1893. {
  1894. part.PhysActor.Size =
  1895. new PhysicsVector(scale.X, scale.Y, scale.Z);
  1896. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1897. }
  1898. ScheduleGroupForTerseUpdate();
  1899. }
  1900. }
  1901. #endregion
  1902. #region Position
  1903. /// <summary>
  1904. ///
  1905. /// </summary>
  1906. /// <param name="pos"></param>
  1907. public void UpdateGroupPosition(LLVector3 pos)
  1908. {
  1909. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  1910. {
  1911. if (m_rootPart.m_IsAttachment)
  1912. {
  1913. m_rootPart.m_attachedPos = pos;
  1914. }
  1915. AbsolutePosition = pos;
  1916. }
  1917. //we need to do a terse update even if the move wasn't allowed
  1918. // so that the position is reset in the client (the object snaps back)
  1919. ScheduleGroupForTerseUpdate();
  1920. }
  1921. /// <summary>
  1922. ///
  1923. /// </summary>
  1924. /// <param name="pos"></param>
  1925. /// <param name="localID"></param>
  1926. public void UpdateSinglePosition(LLVector3 pos, uint localID)
  1927. {
  1928. SceneObjectPart part = GetChildPart(localID);
  1929. if (part != null)
  1930. {
  1931. if (part.UUID == m_rootPart.UUID)
  1932. {
  1933. UpdateRootPosition(pos);
  1934. }
  1935. else
  1936. {
  1937. part.UpdateOffSet(pos);
  1938. }
  1939. }
  1940. }
  1941. /// <summary>
  1942. ///
  1943. /// </summary>
  1944. /// <param name="pos"></param>
  1945. private void UpdateRootPosition(LLVector3 pos)
  1946. {
  1947. LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);
  1948. LLVector3 oldPos =
  1949. new LLVector3(AbsolutePosition.X + m_rootPart.OffsetPosition.X,
  1950. AbsolutePosition.Y + m_rootPart.OffsetPosition.Y,
  1951. AbsolutePosition.Z + m_rootPart.OffsetPosition.Z);
  1952. LLVector3 diff = oldPos - newPos;
  1953. Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
  1954. Quaternion partRotation =
  1955. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1956. m_rootPart.RotationOffset.Z);
  1957. axDiff = partRotation.Inverse() * axDiff;
  1958. diff.X = axDiff.x;
  1959. diff.Y = axDiff.y;
  1960. diff.Z = axDiff.z;
  1961. lock (m_parts)
  1962. {
  1963. foreach (SceneObjectPart obPart in m_parts.Values)
  1964. {
  1965. if (obPart.UUID != m_rootPart.UUID)
  1966. {
  1967. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  1968. }
  1969. }
  1970. }
  1971. AbsolutePosition = newPos;
  1972. ScheduleGroupForTerseUpdate();
  1973. }
  1974. public void OffsetForNewRegion(LLVector3 offset)
  1975. {
  1976. m_rootPart.GroupPosition = offset;
  1977. }
  1978. #endregion
  1979. #region Rotation
  1980. /// <summary>
  1981. ///
  1982. /// </summary>
  1983. /// <param name="rot"></param>
  1984. public void UpdateGroupRotation(LLQuaternion rot)
  1985. {
  1986. m_rootPart.UpdateRotation(rot);
  1987. if (m_rootPart.PhysActor != null)
  1988. {
  1989. m_rootPart.PhysActor.Orientation =
  1990. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1991. m_rootPart.RotationOffset.Z);
  1992. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1993. }
  1994. ScheduleGroupForTerseUpdate();
  1995. }
  1996. /// <summary>
  1997. ///
  1998. /// </summary>
  1999. /// <param name="pos"></param>
  2000. /// <param name="rot"></param>
  2001. public void UpdateGroupRotation(LLVector3 pos, LLQuaternion rot)
  2002. {
  2003. m_rootPart.UpdateRotation(rot);
  2004. if (m_rootPart.PhysActor != null)
  2005. {
  2006. m_rootPart.PhysActor.Orientation =
  2007. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  2008. m_rootPart.RotationOffset.Z);
  2009. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2010. }
  2011. AbsolutePosition = pos;
  2012. ScheduleGroupForTerseUpdate();
  2013. }
  2014. /// <summary>
  2015. ///
  2016. /// </summary>
  2017. /// <param name="rot"></param>
  2018. /// <param name="localID"></param>
  2019. public void UpdateSingleRotation(LLQuaternion rot, uint localID)
  2020. {
  2021. SceneObjectPart part = GetChildPart(localID);
  2022. if (part != null)
  2023. {
  2024. if (part.UUID == m_rootPart.UUID)
  2025. {
  2026. UpdateRootRotation(rot);
  2027. }
  2028. else
  2029. {
  2030. part.UpdateRotation(rot);
  2031. }
  2032. }
  2033. }
  2034. /// <summary>
  2035. ///
  2036. /// </summary>
  2037. /// <param name="rot"></param>
  2038. private void UpdateRootRotation(LLQuaternion rot)
  2039. {
  2040. Quaternion axRot = new Quaternion(rot.W, rot.X, rot.Y, rot.Z);
  2041. Quaternion oldParentRot =
  2042. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  2043. m_rootPart.RotationOffset.Z);
  2044. m_rootPart.UpdateRotation(rot);
  2045. if (m_rootPart.PhysActor != null)
  2046. {
  2047. m_rootPart.PhysActor.Orientation =
  2048. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  2049. m_rootPart.RotationOffset.Z);
  2050. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2051. }
  2052. lock (m_parts)
  2053. {
  2054. foreach (SceneObjectPart prim in m_parts.Values)
  2055. {
  2056. if (prim.UUID != m_rootPart.UUID)
  2057. {
  2058. Vector3 axPos = new Vector3(prim.OffsetPosition.X, prim.OffsetPosition.Y, prim.OffsetPosition.Z);
  2059. axPos = oldParentRot * axPos;
  2060. axPos = axRot.Inverse() * axPos;
  2061. prim.OffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  2062. Quaternion primsRot =
  2063. new Quaternion(prim.RotationOffset.W, prim.RotationOffset.X, prim.RotationOffset.Y,
  2064. prim.RotationOffset.Z);
  2065. Quaternion newRot = oldParentRot * primsRot;
  2066. newRot = axRot.Inverse() * newRot;
  2067. prim.RotationOffset = new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  2068. prim.ScheduleTerseUpdate();
  2069. }
  2070. }
  2071. }
  2072. m_rootPart.ScheduleTerseUpdate();
  2073. }
  2074. #endregion
  2075. internal void SetAxisRotation(int axis, int rotate10)
  2076. {
  2077. bool setX = false;
  2078. bool setY = false;
  2079. bool setZ = false;
  2080. int xaxis = 2;
  2081. int yaxis = 4;
  2082. int zaxis = 8;
  2083. if (m_rootPart != null)
  2084. {
  2085. setX = ((axis & xaxis) != 0) ? true : false;
  2086. setY = ((axis & yaxis) != 0) ? true : false;
  2087. setZ = ((axis & zaxis) != 0) ? true : false;
  2088. float setval = (rotate10 > 0) ? 1f : 0f;
  2089. if (setX)
  2090. m_rootPart.m_rotationAxis.X = setval;
  2091. if (setY)
  2092. m_rootPart.m_rotationAxis.Y = setval;
  2093. if (setZ)
  2094. m_rootPart.m_rotationAxis.Z = setval;
  2095. if (setX || setY || setZ)
  2096. {
  2097. m_rootPart.SetPhysicsAxisRotation();
  2098. }
  2099. }
  2100. }
  2101. public int registerTargetWaypoint(LLVector3 target, float tolerance)
  2102. {
  2103. scriptPosTarget waypoint = new scriptPosTarget();
  2104. waypoint.targetPos = target;
  2105. waypoint.tolerance = tolerance;
  2106. uint handle = m_scene.PrimIDAllocate();
  2107. lock (m_targets)
  2108. {
  2109. m_targets.Add(handle, waypoint);
  2110. }
  2111. return (int)handle;
  2112. }
  2113. public void unregisterTargetWaypoint(int handle)
  2114. {
  2115. lock (m_targets)
  2116. {
  2117. if (m_targets.ContainsKey((uint)handle))
  2118. m_targets.Remove((uint)handle);
  2119. }
  2120. }
  2121. private void checkAtTargets()
  2122. {
  2123. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  2124. {
  2125. if (m_targets.Count > 0)
  2126. {
  2127. bool at_target = false;
  2128. //LLVector3 targetPos;
  2129. //uint targetHandle;
  2130. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  2131. lock (m_targets)
  2132. {
  2133. foreach (uint idx in m_targets.Keys)
  2134. {
  2135. scriptPosTarget target = m_targets[idx];
  2136. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  2137. {
  2138. // trigger at_target
  2139. if (m_scriptListens_atTarget)
  2140. {
  2141. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2142. // to avoid deadlocking the sim.
  2143. at_target = true;
  2144. scriptPosTarget att = new scriptPosTarget();
  2145. att.targetPos = target.targetPos;
  2146. att.tolerance = (float)idx;
  2147. atTargets.Add(idx, att);
  2148. }
  2149. }
  2150. }
  2151. }
  2152. if (atTargets.Count > 0)
  2153. {
  2154. uint[] localids = new uint[0];
  2155. lock (m_parts)
  2156. {
  2157. localids = new uint[m_parts.Count];
  2158. int cntr = 0;
  2159. foreach (SceneObjectPart part in m_parts.Values)
  2160. {
  2161. localids[cntr] = part.LocalId;
  2162. cntr++;
  2163. }
  2164. }
  2165. for (int ctr = 0; ctr < localids.Length; ctr++)
  2166. {
  2167. foreach (uint target in atTargets.Keys)
  2168. {
  2169. scriptPosTarget att = atTargets[target];
  2170. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2171. // to avoid deadlocking the sim.
  2172. m_scene.TriggerAtTargetEvent(localids[ctr], (uint)att.tolerance, att.targetPos, m_rootPart.GroupPosition);
  2173. }
  2174. }
  2175. return;
  2176. }
  2177. if (m_scriptListens_notAtTarget && !at_target)
  2178. {
  2179. //trigger not_at_target
  2180. uint[] localids = new uint[0];
  2181. lock (m_parts)
  2182. {
  2183. localids = new uint[m_parts.Count];
  2184. int cntr = 0;
  2185. foreach (SceneObjectPart part in m_parts.Values)
  2186. {
  2187. localids[cntr] = part.LocalId;
  2188. cntr++;
  2189. }
  2190. }
  2191. for (int ctr = 0; ctr < localids.Length; ctr++)
  2192. {
  2193. m_scene.TriggerNotAtTargetEvent(localids[ctr]);
  2194. }
  2195. }
  2196. }
  2197. }
  2198. }
  2199. }
  2200. }