SceneObjectGroup.cs 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944
  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 System.Xml.Serialization;
  33. using OpenMetaverse;
  34. using OpenMetaverse.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 = 16,
  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 Vector3 targetPos;
  75. public float tolerance;
  76. }
  77. public delegate void PrimCountTaintedDelegate();
  78. /// <summary>
  79. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  80. /// (often known as prims), one of which is considered the root part.
  81. /// </summary>
  82. public partial class SceneObjectGroup : EntityBase
  83. {
  84. // private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  85. /// <summary>
  86. /// Signal whether the non-inventory attributes of any prims in the group have changed
  87. /// since the group's last persistent backup
  88. /// </summary>
  89. private bool m_hasGroupChanged = false;
  90. private long timeFirstChanged;
  91. private long timeLastChanged;
  92. public bool HasGroupChanged
  93. {
  94. set
  95. {
  96. if (value)
  97. {
  98. timeLastChanged = DateTime.Now.Ticks;
  99. if (!m_hasGroupChanged)
  100. timeFirstChanged = DateTime.Now.Ticks;
  101. }
  102. m_hasGroupChanged = value;
  103. }
  104. get { return m_hasGroupChanged; }
  105. }
  106. private bool isTimeToPersist()
  107. {
  108. if (IsSelected || IsDeleted || IsAttachment)
  109. return false;
  110. if (!m_hasGroupChanged)
  111. return false;
  112. if (m_scene.ShuttingDown)
  113. return true;
  114. long currentTime = DateTime.Now.Ticks;
  115. if (currentTime - timeLastChanged > m_scene.m_dontPersistBefore || currentTime - timeFirstChanged > m_scene.m_persistAfter)
  116. return true;
  117. return false;
  118. }
  119. /// <value>
  120. /// Is this scene object acting as an attachment?
  121. ///
  122. /// We return false if the group has already been deleted.
  123. ///
  124. /// TODO: At the moment set must be done on the part itself. There may be a case for doing it here since I
  125. /// presume either all or no parts in a linkset can be part of an attachment (in which
  126. /// case the value would get proprogated down into all the descendent parts).
  127. /// </value>
  128. public bool IsAttachment
  129. {
  130. get
  131. {
  132. if (!IsDeleted)
  133. return m_rootPart.IsAttachment;
  134. return false;
  135. }
  136. }
  137. public float scriptScore = 0f;
  138. private Vector3 lastPhysGroupPos;
  139. private Quaternion lastPhysGroupRot;
  140. private bool m_isBackedUp = false;
  141. /// <summary>
  142. /// The constituent parts of this group
  143. /// </summary>
  144. protected Dictionary<UUID, SceneObjectPart> m_parts = new Dictionary<UUID, SceneObjectPart>();
  145. protected ulong m_regionHandle;
  146. protected SceneObjectPart m_rootPart;
  147. // private Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  148. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  149. private bool m_scriptListens_atTarget = false;
  150. private bool m_scriptListens_notAtTarget = false;
  151. #region Properties
  152. /// <summary>
  153. /// The name of an object grouping is always the same as its root part
  154. /// </summary>
  155. public override string Name
  156. {
  157. get {
  158. if (RootPart == null)
  159. return "";
  160. return RootPart.Name;
  161. }
  162. set { RootPart.Name = value; }
  163. }
  164. /// <summary>
  165. /// Added because the Parcel code seems to use it
  166. /// but not sure a object should have this
  167. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  168. /// think really there should be a list (or whatever) in each scenepresence
  169. /// saying what prim(s) that user has selected.
  170. /// </summary>
  171. protected bool m_isSelected = false;
  172. /// <summary>
  173. /// Number of prims in this group
  174. /// </summary>
  175. public int PrimCount
  176. {
  177. get { return m_parts.Count; }
  178. }
  179. public Quaternion GroupRotation
  180. {
  181. get { return m_rootPart.RotationOffset; }
  182. }
  183. public UUID GroupID
  184. {
  185. get { return m_rootPart.GroupID; }
  186. set { m_rootPart.GroupID = value; }
  187. }
  188. public Dictionary<UUID, SceneObjectPart> Children
  189. {
  190. get { return m_parts; }
  191. set { m_parts = value; }
  192. }
  193. /// <value>
  194. /// The root part of this scene object
  195. /// </value>
  196. public SceneObjectPart RootPart
  197. {
  198. get { return m_rootPart; }
  199. }
  200. public ulong RegionHandle
  201. {
  202. get { return m_regionHandle; }
  203. set
  204. {
  205. m_regionHandle = value;
  206. lock (m_parts)
  207. {
  208. foreach (SceneObjectPart part in m_parts.Values)
  209. {
  210. part.RegionHandle = m_regionHandle;
  211. }
  212. }
  213. }
  214. }
  215. /// <summary>
  216. /// The absolute position of this scene object in the scene
  217. /// </summary>
  218. public override Vector3 AbsolutePosition
  219. {
  220. get
  221. {
  222. if (m_rootPart == null)
  223. {
  224. throw new NullReferenceException(
  225. string.Format("[SCENE OBJECT GROUP]: Object {0} has no root part.", m_uuid));
  226. }
  227. return m_rootPart.GroupPosition;
  228. }
  229. set
  230. {
  231. Vector3 val = value;
  232. if ((val.X > 257f || val.X < -1f || val.Y > 257f || val.Y < -1f) && !IsAttachment)
  233. {
  234. m_scene.CrossPrimGroupIntoNewRegion(val, this, true);
  235. }
  236. lock (m_parts)
  237. {
  238. foreach (SceneObjectPart part in m_parts.Values)
  239. {
  240. part.GroupPosition = val;
  241. }
  242. }
  243. //if (m_rootPart.PhysActor != null)
  244. //{
  245. //m_rootPart.PhysActor.Position =
  246. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  247. //m_rootPart.GroupPosition.Z);
  248. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  249. //}
  250. }
  251. }
  252. public override uint LocalId
  253. {
  254. get
  255. {
  256. if (m_rootPart == null)
  257. {
  258. m_log.Error("[SCENE OBJECT GROUP]: Unable to find the rootpart for a LocalId Request!");
  259. return 0;
  260. }
  261. return m_rootPart.LocalId;
  262. }
  263. set { m_rootPart.LocalId = value; }
  264. }
  265. public override UUID UUID
  266. {
  267. get {
  268. if (m_rootPart == null)
  269. {
  270. m_log.Error("Got a null rootpart while requesting UUID. Called from: ", new Exception());
  271. return UUID.Zero;
  272. }
  273. else return m_rootPart.UUID;
  274. }
  275. set { m_rootPart.UUID = value; }
  276. }
  277. public UUID OwnerID
  278. {
  279. get
  280. {
  281. if (m_rootPart == null)
  282. return UUID.Zero;
  283. return m_rootPart.OwnerID;
  284. }
  285. set { m_rootPart.OwnerID = value; }
  286. }
  287. public Color Color
  288. {
  289. get { return m_rootPart.Color; }
  290. set { m_rootPart.Color = value; }
  291. }
  292. public string Text
  293. {
  294. get {
  295. string returnstr = m_rootPart.Text;
  296. if (returnstr.Length > 255)
  297. {
  298. returnstr = returnstr.Substring(0, 255);
  299. }
  300. return returnstr;
  301. }
  302. set { m_rootPart.Text = value; }
  303. }
  304. protected virtual bool InSceneBackup
  305. {
  306. get { return true; }
  307. }
  308. public bool IsSelected
  309. {
  310. get { return m_isSelected; }
  311. set
  312. {
  313. m_isSelected = value;
  314. // Tell physics engine that group is selected
  315. if (m_rootPart != null && m_rootPart.PhysActor != null)
  316. {
  317. m_rootPart.PhysActor.Selected = value;
  318. // Pass it on to the children.
  319. foreach (SceneObjectPart child in Children.Values)
  320. {
  321. if (child.PhysActor != null)
  322. {
  323. child.PhysActor.Selected = value;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. // The UUID for the Region this Object is in.
  330. public UUID RegionUUID
  331. {
  332. get
  333. {
  334. if (m_scene != null)
  335. {
  336. return m_scene.RegionInfo.RegionID;
  337. }
  338. return UUID.Zero;
  339. }
  340. }
  341. #endregion
  342. #region Constructors
  343. /// <summary>
  344. /// Constructor
  345. /// </summary>
  346. public SceneObjectGroup()
  347. {
  348. }
  349. /// <summary>
  350. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  351. /// The original SceneObjectPart will be used rather than a copy, preserving
  352. /// its existing localID and UUID.
  353. /// </summary>
  354. public SceneObjectGroup(SceneObjectPart part)
  355. {
  356. SetRootPart(part);
  357. }
  358. public SceneObjectGroup(string xmlData, bool isOriginalXmlFormat)
  359. : this(UUID.Zero, xmlData, isOriginalXmlFormat)
  360. {
  361. }
  362. /// <summary>
  363. /// Create an object using serialized data in OpenSim's original xml format.
  364. /// </summary>
  365. /// <param name="fromUserInventoryItemID">
  366. /// If applicable, the user inventory item id from which this object was rezzed. If not applicable then this
  367. /// should be UUID.Zero
  368. /// </param>
  369. /// <param name="xmlData"></param>
  370. /// <param name="isOriginalXmlFormat">
  371. /// This parameter only exists to separate the two different xml constructors. In the future, versions should
  372. /// be specified within the xml itself.
  373. /// </param>
  374. public SceneObjectGroup(UUID fromUserInventoryItemID, string xmlData, bool isOriginalXmlFormat)
  375. {
  376. if (!isOriginalXmlFormat)
  377. throw new Exception("This constructor must specify the xml is in OpenSim's original format");
  378. // libomv.types changes UUID to Guid
  379. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  380. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  381. // Handle Nested <UUID><UUID> property
  382. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  383. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  384. StringReader sr = new StringReader(xmlData);
  385. XmlTextReader reader = new XmlTextReader(sr);
  386. try
  387. {
  388. reader.Read();
  389. reader.ReadStartElement("SceneObjectGroup");
  390. reader.ReadStartElement("RootPart");
  391. SetRootPart(SceneObjectPart.FromXml(fromUserInventoryItemID, reader));
  392. reader.ReadEndElement();
  393. while (reader.Read())
  394. {
  395. switch (reader.NodeType)
  396. {
  397. case XmlNodeType.Element:
  398. if (reader.Name == "Part")
  399. {
  400. reader.Read();
  401. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  402. // We reset the link number in order to make sure that the persisted linkset order is
  403. int linkNum = part.LinkNum;
  404. AddPart(part);
  405. part.LinkNum = linkNum;
  406. part.TrimPermissions();
  407. part.StoreUndoState();
  408. }
  409. break;
  410. case XmlNodeType.EndElement:
  411. break;
  412. }
  413. }
  414. }
  415. catch (XmlException e)
  416. {
  417. m_log.ErrorFormat("[SCENE]: Deserialization of xml failed with {0}. xml was {1}", e, xmlData);
  418. }
  419. reader.Close();
  420. sr.Close();
  421. }
  422. /// <summary>
  423. /// Create an object using serialized data in OpenSim's xml2 format.
  424. /// </summary>
  425. public SceneObjectGroup(string xmlData)
  426. {
  427. SetFromXml(xmlData);
  428. }
  429. protected void SetFromXml(string xmlData)
  430. {
  431. // libomv.types changes UUID to Guid
  432. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  433. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  434. // Handle Nested <UUID><UUID> property
  435. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  436. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  437. StringReader sr = new StringReader(xmlData);
  438. XmlTextReader reader = new XmlTextReader(sr);
  439. reader.Read();
  440. reader.ReadStartElement("SceneObjectGroup");
  441. SetRootPart(CreatePartFromXml(reader));
  442. reader.Read();
  443. bool more = true;
  444. while (more)
  445. {
  446. switch (reader.NodeType)
  447. {
  448. case XmlNodeType.Element:
  449. if (reader.Name == "SceneObjectPart")
  450. {
  451. SceneObjectPart part = CreatePartFromXml(reader);
  452. AddPart(part);
  453. part.StoreUndoState();
  454. }
  455. else
  456. {
  457. Console.WriteLine("found unexpected element: " + reader.Name);
  458. reader.Read();
  459. }
  460. break;
  461. case XmlNodeType.EndElement:
  462. reader.Read();
  463. break;
  464. }
  465. more = !reader.EOF;
  466. }
  467. reader.Close();
  468. sr.Close();
  469. }
  470. protected virtual SceneObjectPart CreatePartFromXml(XmlTextReader reader)
  471. {
  472. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  473. return part;
  474. }
  475. /// <summary>
  476. /// Constructor. This object is added to the scene later via AttachToScene()
  477. /// </summary>
  478. public SceneObjectGroup(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  479. {
  480. Vector3 rootOffset = new Vector3(0, 0, 0);
  481. SetRootPart(new SceneObjectPart(ownerID, shape, pos, rot, rootOffset));
  482. }
  483. /// <summary>
  484. /// Constructor.
  485. /// </summary>
  486. public SceneObjectGroup(UUID ownerID, Vector3 pos, PrimitiveBaseShape shape)
  487. : this(ownerID, pos, Quaternion.Identity, shape)
  488. {
  489. }
  490. public void SetFromAssetID(UUID AssetId)
  491. {
  492. lock (m_parts)
  493. {
  494. foreach (SceneObjectPart part in m_parts.Values)
  495. {
  496. part.FromAssetID = AssetId;
  497. }
  498. }
  499. }
  500. public UUID GetFromAssetID()
  501. {
  502. if (m_rootPart != null)
  503. {
  504. return m_rootPart.FromAssetID;
  505. }
  506. return UUID.Zero;
  507. }
  508. /// <summary>
  509. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  510. /// </summary>
  511. public void AttachToBackup()
  512. {
  513. if (InSceneBackup)
  514. {
  515. //m_log.DebugFormat(
  516. // "[SCENE OBJECT GROUP]: Attaching object {0} {1} to scene presistence sweep", Name, UUID);
  517. if (!m_isBackedUp)
  518. m_scene.EventManager.OnBackup += ProcessBackup;
  519. m_isBackedUp = true;
  520. }
  521. }
  522. /// <summary>
  523. /// Attach this object to a scene. It will also now appear to agents.
  524. /// </summary>
  525. /// <param name="scene"></param>
  526. public void AttachToScene(Scene scene)
  527. {
  528. m_scene = scene;
  529. RegionHandle = m_scene.RegionInfo.RegionHandle;
  530. if (m_rootPart.Shape.PCode != 9 || m_rootPart.Shape.State == 0)
  531. m_rootPart.ParentID = 0;
  532. if (m_rootPart.LocalId==0)
  533. m_rootPart.LocalId = m_scene.AllocateLocalId();
  534. // No need to lock here since the object isn't yet in a scene
  535. foreach (SceneObjectPart part in m_parts.Values)
  536. {
  537. if (Object.ReferenceEquals(part, m_rootPart))
  538. continue;
  539. if (part.LocalId==0)
  540. part.LocalId = m_scene.AllocateLocalId();
  541. part.ParentID = m_rootPart.LocalId;
  542. //m_log.DebugFormat("[SCENE]: Given local id {0} to part {1}, linknum {2}, parent {3} {4}", part.LocalId, part.UUID, part.LinkNum, part.ParentID, part.ParentUUID);
  543. }
  544. ApplyPhysics(m_scene.m_physicalPrim);
  545. ScheduleGroupForFullUpdate();
  546. }
  547. public Vector3 GroupScale()
  548. {
  549. Vector3 minScale = new Vector3(Constants.RegionSize,Constants.RegionSize,Constants.RegionSize);
  550. Vector3 maxScale = new Vector3(0f,0f,0f);
  551. Vector3 finalScale = new Vector3(0.5f, 0.5f, 0.5f);
  552. lock (m_parts)
  553. {
  554. foreach (SceneObjectPart part in m_parts.Values)
  555. {
  556. Vector3 partscale = part.Scale;
  557. Vector3 partoffset = part.OffsetPosition;
  558. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  559. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.X + partoffset.Y : minScale.Y;
  560. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.X + partoffset.Z : minScale.Z;
  561. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  562. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  563. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  564. }
  565. }
  566. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  567. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  568. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  569. return finalScale;
  570. }
  571. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  572. {
  573. // We got a request from the inner_scene to raytrace along the Ray hRay
  574. // We're going to check all of the prim in this group for intersection with the ray
  575. // If we get a result, we're going to find the closest result to the origin of the ray
  576. // and send back the intersection information back to the innerscene.
  577. EntityIntersection returnresult = new EntityIntersection();
  578. lock (m_parts)
  579. {
  580. foreach (SceneObjectPart part in m_parts.Values)
  581. {
  582. // Temporary commented to stop compiler warning
  583. //Vector3 partPosition =
  584. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  585. Quaternion parentrotation = GroupRotation;
  586. // Telling the prim to raytrace.
  587. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  588. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation,frontFacesOnly, faceCenters);
  589. // This may need to be updated to the maximum draw distance possible..
  590. // We might (and probably will) be checking for prim creation from other sims
  591. // when the camera crosses the border.
  592. float idist = Constants.RegionSize;
  593. if (inter.HitTF)
  594. {
  595. // We need to find the closest prim to return to the testcaller along the ray
  596. if (inter.distance < idist)
  597. {
  598. returnresult.HitTF = true;
  599. returnresult.ipoint = inter.ipoint;
  600. returnresult.obj = part;
  601. returnresult.normal = inter.normal;
  602. returnresult.distance = inter.distance;
  603. }
  604. }
  605. }
  606. }
  607. return returnresult;
  608. }
  609. #endregion
  610. public string ToXmlString()
  611. {
  612. using (StringWriter sw = new StringWriter())
  613. {
  614. using (XmlTextWriter writer = new XmlTextWriter(sw))
  615. {
  616. ToXml(writer);
  617. }
  618. return sw.ToString();
  619. }
  620. }
  621. public void ToXml(XmlTextWriter writer)
  622. {
  623. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  624. writer.WriteStartElement(String.Empty, "RootPart", String.Empty);
  625. m_rootPart.ToXml(writer);
  626. writer.WriteEndElement();
  627. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  628. lock (m_parts)
  629. {
  630. foreach (SceneObjectPart part in m_parts.Values)
  631. {
  632. if (part.UUID != m_rootPart.UUID)
  633. {
  634. writer.WriteStartElement(String.Empty, "Part", String.Empty);
  635. part.ToXml(writer);
  636. writer.WriteEndElement();
  637. }
  638. }
  639. }
  640. writer.WriteEndElement();
  641. writer.WriteEndElement();
  642. }
  643. public string ToXmlString2()
  644. {
  645. using (StringWriter sw = new StringWriter())
  646. {
  647. using (XmlTextWriter writer = new XmlTextWriter(sw))
  648. {
  649. ToXml2(writer);
  650. }
  651. return sw.ToString();
  652. }
  653. }
  654. public void ToXml2(XmlTextWriter writer)
  655. {
  656. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  657. m_rootPart.ToXml(writer);
  658. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  659. lock (m_parts)
  660. {
  661. foreach (SceneObjectPart part in m_parts.Values)
  662. {
  663. if (part.UUID != m_rootPart.UUID)
  664. {
  665. part.ToXml(writer);
  666. }
  667. }
  668. }
  669. writer.WriteEndElement();
  670. writer.WriteEndElement();
  671. }
  672. /// <summary>
  673. /// Attach this scene object to the given avatar.
  674. /// </summary>
  675. /// <param name="agentID"></param>
  676. /// <param name="attachmentpoint"></param>
  677. /// <param name="AttachOffset"></param>
  678. public void AttachToAgent(UUID agentID, uint attachmentpoint, Vector3 AttachOffset, bool silent)
  679. {
  680. ScenePresence avatar = m_scene.GetScenePresence(agentID);
  681. if (avatar != null)
  682. {
  683. // don't attach attachments to child agents
  684. if (avatar.IsChildAgent) return;
  685. DetachFromBackup();
  686. // Remove from database and parcel prim count
  687. //
  688. m_scene.DeleteFromStorage(UUID);
  689. m_scene.EventManager.TriggerParcelPrimCountTainted();
  690. m_rootPart.AttachedAvatar = agentID;
  691. if (m_rootPart.PhysActor != null)
  692. {
  693. m_scene.PhysicsScene.RemovePrim(m_rootPart.PhysActor);
  694. m_rootPart.PhysActor = null;
  695. }
  696. AbsolutePosition = AttachOffset;
  697. m_rootPart.AttachedPos = AttachOffset;
  698. m_rootPart.IsAttachment = true;
  699. m_rootPart.SetParentLocalId(avatar.LocalId);
  700. SetAttachmentPoint(Convert.ToByte(attachmentpoint));
  701. avatar.AddAttachment(this);
  702. if (!silent)
  703. {
  704. // Killing it here will cause the client to deselect it
  705. // It then reappears on the avatar, deselected
  706. // through the full update below
  707. //
  708. if (IsSelected)
  709. {
  710. m_scene.SendKillObject(m_rootPart.LocalId);
  711. }
  712. IsSelected = false; // fudge....
  713. ScheduleGroupForFullUpdate();
  714. }
  715. }
  716. }
  717. public byte GetAttachmentPoint()
  718. {
  719. if (m_rootPart != null)
  720. {
  721. return m_rootPart.Shape.State;
  722. }
  723. return (byte)0;
  724. }
  725. public void ClearPartAttachmentData()
  726. {
  727. SetAttachmentPoint((Byte)0);
  728. }
  729. public void DetachToGround()
  730. {
  731. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.AttachedAvatar);
  732. if (avatar == null)
  733. return;
  734. avatar.RemoveAttachment(this);
  735. Vector3 detachedpos = new Vector3(127f,127f,127f);
  736. if (avatar == null)
  737. return;
  738. detachedpos = avatar.AbsolutePosition;
  739. AbsolutePosition = detachedpos;
  740. m_rootPart.AttachedAvatar = UUID.Zero;
  741. m_rootPart.SetParentLocalId(0);
  742. SetAttachmentPoint((byte)0);
  743. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  744. HasGroupChanged = true;
  745. RootPart.Rezzed = DateTime.Now;
  746. RootPart.RemFlag(PrimFlags.TemporaryOnRez);
  747. AttachToBackup();
  748. m_scene.EventManager.TriggerParcelPrimCountTainted();
  749. m_rootPart.ScheduleFullUpdate();
  750. m_rootPart.ClearUndoState();
  751. }
  752. public void DetachToInventoryPrep()
  753. {
  754. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.AttachedAvatar);
  755. //Vector3 detachedpos = new Vector3(127f, 127f, 127f);
  756. if (avatar != null)
  757. {
  758. //detachedpos = avatar.AbsolutePosition;
  759. avatar.RemoveAttachment(this);
  760. }
  761. m_rootPart.AttachedAvatar = UUID.Zero;
  762. m_rootPart.SetParentLocalId(0);
  763. //m_rootPart.SetAttachmentPoint((byte)0);
  764. m_rootPart.IsAttachment = false;
  765. AbsolutePosition = m_rootPart.AttachedPos;
  766. //m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  767. //AttachToBackup();
  768. //m_rootPart.ScheduleFullUpdate();
  769. }
  770. /// <summary>
  771. ///
  772. /// </summary>
  773. /// <param name="part"></param>
  774. private void SetPartAsNonRoot(SceneObjectPart part)
  775. {
  776. part.ParentID = m_rootPart.LocalId;
  777. part.ClearUndoState();
  778. }
  779. public override void UpdateMovement()
  780. {
  781. lock (m_parts)
  782. {
  783. foreach (SceneObjectPart part in m_parts.Values)
  784. {
  785. part.UpdateMovement();
  786. }
  787. }
  788. }
  789. public float GetTimeDilation()
  790. {
  791. return m_scene.TimeDilation;
  792. }
  793. /// <summary>
  794. /// Added as a way for the storage provider to reset the scene,
  795. /// most likely a better way to do this sort of thing but for now...
  796. /// </summary>
  797. /// <param name="scene"></param>
  798. public void SetScene(Scene scene)
  799. {
  800. m_scene = scene;
  801. }
  802. /// <summary>
  803. /// Set a part to act as the root part for this scene object
  804. /// </summary>
  805. /// <param name="part"></param>
  806. public void SetRootPart(SceneObjectPart part)
  807. {
  808. part.SetParent(this);
  809. m_rootPart = part;
  810. if (!IsAttachment)
  811. part.ParentID = 0;
  812. part.LinkNum = 0;
  813. // No locking required since the SOG should not be in the scene yet - one can't change root parts after
  814. // the scene object has been attached to the scene
  815. m_parts.Add(m_rootPart.UUID, m_rootPart);
  816. }
  817. /// <summary>
  818. /// Add a new part to this scene object. The part must already be correctly configured.
  819. /// </summary>
  820. /// <param name="part"></param>
  821. public void AddPart(SceneObjectPart part)
  822. {
  823. lock (m_parts)
  824. {
  825. part.SetParent(this);
  826. m_parts.Add(part.UUID, part);
  827. part.LinkNum = m_parts.Count;
  828. if (part.LinkNum == 2 && RootPart != null)
  829. RootPart.LinkNum = 1;
  830. }
  831. }
  832. /// <summary>
  833. /// Make sure that every non root part has the proper parent root part local id
  834. /// </summary>
  835. private void UpdateParentIDs()
  836. {
  837. lock (m_parts)
  838. {
  839. foreach (SceneObjectPart part in m_parts.Values)
  840. {
  841. if (part.UUID != m_rootPart.UUID)
  842. {
  843. part.ParentID = m_rootPart.LocalId;
  844. }
  845. }
  846. }
  847. }
  848. public void RegenerateFullIDs()
  849. {
  850. lock (m_parts)
  851. {
  852. foreach (SceneObjectPart part in m_parts.Values)
  853. {
  854. part.UUID = UUID.Random();
  855. }
  856. }
  857. }
  858. // helper provided for parts.
  859. public int GetSceneMaxUndo()
  860. {
  861. if (m_scene != null)
  862. return m_scene.MaxUndoCount;
  863. return 5;
  864. }
  865. // justincc: I don't believe this hack is needed any longer, especially since the physics
  866. // parts of set AbsolutePosition were already commented out. By changing HasGroupChanged to false
  867. // this method was preventing proper reload of scene objects.
  868. // dahlia: I had to uncomment it, without it meshing was failing on some prims and objects
  869. // at region startup
  870. public void ResetChildPrimPhysicsPositions()
  871. {
  872. AbsolutePosition = AbsolutePosition; // could someone in the know please explain how this works?
  873. }
  874. public UUID GetPartsFullID(uint localID)
  875. {
  876. SceneObjectPart part = GetChildPart(localID);
  877. if (part != null)
  878. {
  879. return part.UUID;
  880. }
  881. return UUID.Zero;
  882. }
  883. public void ObjectGrabHandler(uint localId, Vector3 offsetPos, IClientAPI remoteClient)
  884. {
  885. if (m_rootPart.LocalId == localId)
  886. {
  887. OnGrabGroup(offsetPos, remoteClient);
  888. }
  889. else
  890. {
  891. SceneObjectPart part = GetChildPart(localId);
  892. OnGrabPart(part, offsetPos, remoteClient);
  893. }
  894. }
  895. public virtual void OnGrabPart(SceneObjectPart part, Vector3 offsetPos, IClientAPI remoteClient)
  896. {
  897. part.StoreUndoState();
  898. part.OnGrab(offsetPos, remoteClient);
  899. }
  900. public virtual void OnGrabGroup(Vector3 offsetPos, IClientAPI remoteClient)
  901. {
  902. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  903. }
  904. /// <summary>
  905. /// Delete this group from its scene and tell all the scene presences about that deletion.
  906. /// </summary>
  907. public void DeleteGroup(bool silent)
  908. {
  909. // We need to keep track of this state in case this group is still queued for backup.
  910. m_isDeleted = true;
  911. DetachFromBackup();
  912. lock (m_parts)
  913. {
  914. foreach (SceneObjectPart part in m_parts.Values)
  915. {
  916. // part.Inventory.RemoveScriptInstances();
  917. List<ScenePresence> avatars = Scene.GetScenePresences();
  918. for (int i = 0; i < avatars.Count; i++)
  919. {
  920. if (avatars[i].ParentID == LocalId)
  921. {
  922. avatars[i].StandUp();
  923. }
  924. if (!silent)
  925. {
  926. if (m_rootPart != null && part == m_rootPart)
  927. avatars[i].ControllingClient.SendKillObject(m_regionHandle, part.LocalId);
  928. }
  929. }
  930. }
  931. }
  932. }
  933. public void AddScriptLPS(int count)
  934. {
  935. if (scriptScore + count >= float.MaxValue - count)
  936. scriptScore = 0;
  937. scriptScore += (float)count;
  938. SceneGraph d = m_scene.m_sceneGraph;
  939. d.AddToScriptLPS(count);
  940. }
  941. public void AddActiveScriptCount(int count)
  942. {
  943. SceneGraph d = m_scene.m_sceneGraph;
  944. d.AddActiveScripts(count);
  945. }
  946. public void aggregateScriptEvents()
  947. {
  948. uint objectflagupdate=(uint)RootPart.GetEffectiveObjectFlags();
  949. scriptEvents aggregateScriptEvents=0;
  950. lock (m_parts)
  951. {
  952. foreach (SceneObjectPart part in m_parts.Values)
  953. {
  954. if (part == null)
  955. continue;
  956. if (part != RootPart)
  957. part.ObjectFlags = objectflagupdate;
  958. aggregateScriptEvents |= part.AggregateScriptEvents;
  959. }
  960. }
  961. if ((aggregateScriptEvents & scriptEvents.at_target) != 0)
  962. {
  963. m_scriptListens_atTarget = true;
  964. }
  965. else
  966. {
  967. m_scriptListens_atTarget = false;
  968. }
  969. if ((aggregateScriptEvents & scriptEvents.not_at_target) != 0)
  970. {
  971. m_scriptListens_notAtTarget = true;
  972. }
  973. else
  974. {
  975. m_scriptListens_notAtTarget = false;
  976. }
  977. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  978. {
  979. }
  980. else
  981. {
  982. lock (m_targets)
  983. m_targets.Clear();
  984. }
  985. ScheduleGroupForFullUpdate();
  986. }
  987. public override void SetText(string text, Vector3 color, double alpha)
  988. {
  989. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  990. (int) (color.X * 0xff),
  991. (int) (color.Y * 0xff),
  992. (int) (color.Z * 0xff));
  993. Text = text;
  994. HasGroupChanged = true;
  995. m_rootPart.ScheduleFullUpdate();
  996. }
  997. /// <summary>
  998. /// Apply physics to this group
  999. /// </summary>
  1000. /// <param name="m_physicalPrim"></param>
  1001. public void ApplyPhysics(bool m_physicalPrim)
  1002. {
  1003. lock (m_parts)
  1004. {
  1005. if (m_parts.Count > 1)
  1006. {
  1007. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  1008. foreach (SceneObjectPart part in m_parts.Values)
  1009. {
  1010. if (part.LocalId != m_rootPart.LocalId)
  1011. {
  1012. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  1013. }
  1014. }
  1015. // Hack to get the physics scene geometries in the right spot
  1016. ResetChildPrimPhysicsPositions();
  1017. }
  1018. else
  1019. {
  1020. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  1021. }
  1022. }
  1023. }
  1024. public void SetOwnerId(UUID userId)
  1025. {
  1026. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  1027. }
  1028. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  1029. {
  1030. lock (m_parts)
  1031. {
  1032. foreach (SceneObjectPart part in m_parts.Values)
  1033. {
  1034. whatToDo(part);
  1035. }
  1036. }
  1037. }
  1038. #region Events
  1039. /// <summary>
  1040. /// Processes backup.
  1041. /// </summary>
  1042. /// <param name="datastore"></param>
  1043. public void ProcessBackup(IRegionDataStore datastore)
  1044. {
  1045. if (!m_isBackedUp)
  1046. return;
  1047. // Since this is the top of the section of call stack for backing up a particular scene object, don't let
  1048. // any exception propogate upwards.
  1049. if (IsDeleted || UUID == UUID.Zero)
  1050. {
  1051. // DetachFromBackup();
  1052. return;
  1053. }
  1054. try
  1055. {
  1056. if (!m_scene.ShuttingDown) // if shutting down then there will be nothing to handle the return so leave till next restart
  1057. {
  1058. ILandObject parcel = m_scene.LandChannel.GetLandObject(
  1059. m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y);
  1060. if (parcel != null && parcel.landData != null &&
  1061. parcel.landData.OtherCleanTime != 0)
  1062. {
  1063. if (parcel.landData.OwnerID != OwnerID &&
  1064. (parcel.landData.GroupID != GroupID ||
  1065. parcel.landData.GroupID == UUID.Zero))
  1066. {
  1067. if ((DateTime.Now - RootPart.Rezzed).TotalMinutes >
  1068. parcel.landData.OtherCleanTime)
  1069. {
  1070. DetachFromBackup();
  1071. m_log.InfoFormat("[SCENE]: Returning object {0} due to parcel auto return", RootPart.UUID.ToString());
  1072. m_scene.AddReturn(OwnerID, Name, AbsolutePosition, "parcel auto return");
  1073. m_scene.DeRezObject(null, RootPart.LocalId,
  1074. RootPart.GroupID, DeRezAction.Return, UUID.Zero);
  1075. return;
  1076. }
  1077. }
  1078. }
  1079. }
  1080. if (HasGroupChanged)
  1081. {
  1082. // don't backup while it's selected or you're asking for changes mid stream.
  1083. if (isTimeToPersist())
  1084. {
  1085. m_log.DebugFormat(
  1086. "[SCENE]: Storing {0}, {1} in {2}",
  1087. Name, UUID, m_scene.RegionInfo.RegionName);
  1088. SceneObjectGroup backup_group = Copy(OwnerID, GroupID, false);
  1089. backup_group.RootPart.Velocity = RootPart.Velocity;
  1090. backup_group.RootPart.Acceleration = RootPart.Acceleration;
  1091. backup_group.RootPart.AngularVelocity = RootPart.AngularVelocity;
  1092. backup_group.RootPart.ParticleSystem = RootPart.ParticleSystem;
  1093. HasGroupChanged = false;
  1094. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  1095. backup_group.ForEachPart(delegate(SceneObjectPart part) { part.Inventory.ProcessInventoryBackup(datastore); });
  1096. backup_group = null;
  1097. }
  1098. // else
  1099. // {
  1100. // m_log.DebugFormat(
  1101. // "[SCENE]: Did not update persistence of object {0} {1}, selected = {2}",
  1102. // Name, UUID, IsSelected);
  1103. // }
  1104. }
  1105. }
  1106. catch (Exception e)
  1107. {
  1108. m_log.ErrorFormat(
  1109. "[SCENE]: Storing of {0}, {1} in {2} failed with exception {3}\n\t{4}",
  1110. Name, UUID, m_scene.RegionInfo.RegionName, e, e.StackTrace);
  1111. }
  1112. }
  1113. #endregion
  1114. #region Client Updating
  1115. public void SendFullUpdateToClient(IClientAPI remoteClient)
  1116. {
  1117. SendPartFullUpdate(remoteClient, RootPart, m_scene.Permissions.GenerateClientFlags(remoteClient.AgentId, RootPart.UUID));
  1118. lock (m_parts)
  1119. {
  1120. foreach (SceneObjectPart part in m_parts.Values)
  1121. {
  1122. if (part != RootPart)
  1123. SendPartFullUpdate(remoteClient, part, m_scene.Permissions.GenerateClientFlags(remoteClient.AgentId, part.UUID));
  1124. }
  1125. }
  1126. }
  1127. /// <summary>
  1128. /// Send a full update to the client for the given part
  1129. /// </summary>
  1130. /// <param name="remoteClient"></param>
  1131. /// <param name="part"></param>
  1132. internal void SendPartFullUpdate(IClientAPI remoteClient, SceneObjectPart part, uint clientFlags)
  1133. {
  1134. if (m_rootPart != null && m_rootPart.UUID == part.UUID)
  1135. {
  1136. if (IsAttachment)
  1137. {
  1138. part.SendFullUpdateToClient(remoteClient, m_rootPart.AttachedPos, clientFlags);
  1139. }
  1140. else
  1141. {
  1142. part.SendFullUpdateToClient(remoteClient, AbsolutePosition, clientFlags);
  1143. }
  1144. }
  1145. else
  1146. {
  1147. part.SendFullUpdateToClient(remoteClient, clientFlags);
  1148. }
  1149. }
  1150. #endregion
  1151. #region Copying
  1152. /// <summary>
  1153. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1154. /// </summary>
  1155. /// <returns></returns>
  1156. public SceneObjectGroup Copy(UUID cAgentID, UUID cGroupID, bool userExposed)
  1157. {
  1158. SceneObjectGroup dupe = (SceneObjectGroup) MemberwiseClone();
  1159. dupe.m_isBackedUp = false;
  1160. dupe.m_parts = new Dictionary<UUID, SceneObjectPart>();
  1161. dupe.m_parts.Clear();
  1162. //dupe.OwnerID = AgentID;
  1163. //dupe.GroupID = GroupID;
  1164. dupe.AbsolutePosition = new Vector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  1165. dupe.m_scene = m_scene;
  1166. dupe.m_regionHandle = m_regionHandle;
  1167. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  1168. dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
  1169. if (userExposed)
  1170. dupe.m_rootPart.TrimPermissions();
  1171. /// may need to create a new Physics actor.
  1172. if (dupe.RootPart.PhysActor != null && userExposed)
  1173. {
  1174. PrimitiveBaseShape pbs = dupe.RootPart.Shape;
  1175. dupe.RootPart.PhysActor = m_scene.PhysicsScene.AddPrimShape(
  1176. dupe.RootPart.Name,
  1177. pbs,
  1178. new PhysicsVector(dupe.RootPart.AbsolutePosition.X, dupe.RootPart.AbsolutePosition.Y, dupe.RootPart.AbsolutePosition.Z),
  1179. new PhysicsVector(dupe.RootPart.Scale.X, dupe.RootPart.Scale.Y, dupe.RootPart.Scale.Z),
  1180. dupe.RootPart.RotationOffset,
  1181. dupe.RootPart.PhysActor.IsPhysical);
  1182. dupe.RootPart.PhysActor.LocalID = dupe.RootPart.LocalId;
  1183. dupe.RootPart.DoPhysicsPropertyUpdate(dupe.RootPart.PhysActor.IsPhysical, true);
  1184. }
  1185. // Now we've made a copy that replaces this one, we need to
  1186. // switch the owner to the person who did the copying
  1187. // Second Life copies an object and duplicates the first one in it's place
  1188. // So, we have to make a copy of this one, set it in it's place then set the owner on this one
  1189. if (userExposed)
  1190. {
  1191. SetRootPartOwner(m_rootPart, cAgentID, cGroupID);
  1192. m_rootPart.ScheduleFullUpdate();
  1193. }
  1194. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.Values);
  1195. partList.Sort(delegate(SceneObjectPart p1, SceneObjectPart p2)
  1196. {
  1197. return p1.LinkNum.CompareTo(p2.LinkNum);
  1198. }
  1199. );
  1200. foreach (SceneObjectPart part in partList)
  1201. {
  1202. if (part.UUID != m_rootPart.UUID)
  1203. {
  1204. SceneObjectPart newPart =
  1205. dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  1206. newPart.LinkNum = part.LinkNum;
  1207. if (userExposed)
  1208. {
  1209. SetPartOwner(newPart, cAgentID, cGroupID);
  1210. newPart.ScheduleFullUpdate();
  1211. }
  1212. }
  1213. }
  1214. if (userExposed)
  1215. {
  1216. dupe.UpdateParentIDs();
  1217. dupe.HasGroupChanged = true;
  1218. dupe.AttachToBackup();
  1219. ScheduleGroupForFullUpdate();
  1220. }
  1221. return dupe;
  1222. }
  1223. /// <summary>
  1224. ///
  1225. /// </summary>
  1226. /// <param name="part"></param>
  1227. /// <param name="cAgentID"></param>
  1228. /// <param name="cGroupID"></param>
  1229. public void CopyRootPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1230. {
  1231. SetRootPart(part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed));
  1232. }
  1233. public void ScriptSetPhysicsStatus(bool UsePhysics)
  1234. {
  1235. bool IsTemporary = ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0);
  1236. bool IsPhantom = ((RootPart.Flags & PrimFlags.Phantom) != 0);
  1237. UpdatePrimFlags(RootPart.LocalId, UsePhysics, IsTemporary, IsPhantom);
  1238. }
  1239. public void ScriptSetTemporaryStatus(bool TemporaryStatus)
  1240. {
  1241. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1242. bool IsPhantom = ((RootPart.Flags & PrimFlags.Phantom) != 0);
  1243. UpdatePrimFlags(RootPart.LocalId, UsePhysics, TemporaryStatus, IsPhantom);
  1244. }
  1245. public void ScriptSetPhantomStatus(bool PhantomStatus)
  1246. {
  1247. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1248. bool IsTemporary = ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0);
  1249. UpdatePrimFlags(RootPart.LocalId, UsePhysics, IsTemporary, PhantomStatus);
  1250. }
  1251. public void applyImpulse(PhysicsVector impulse)
  1252. {
  1253. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1254. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1255. // Make sure we don't do that!
  1256. SceneObjectPart rootpart = m_rootPart;
  1257. if (rootpart != null)
  1258. {
  1259. if (rootpart.PhysActor != null)
  1260. {
  1261. if (IsAttachment)
  1262. {
  1263. ScenePresence avatar = m_scene.GetScenePresence(rootpart.AttachedAvatar);
  1264. if (avatar != null)
  1265. {
  1266. avatar.PushForce(impulse);
  1267. }
  1268. }
  1269. else
  1270. {
  1271. rootpart.PhysActor.AddForce(impulse,true);
  1272. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1273. }
  1274. }
  1275. }
  1276. }
  1277. public void applyAngularImpulse(PhysicsVector impulse)
  1278. {
  1279. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1280. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1281. // Make sure we don't do that!
  1282. SceneObjectPart rootpart = m_rootPart;
  1283. if (rootpart != null)
  1284. {
  1285. if (rootpart.PhysActor != null)
  1286. {
  1287. if (!IsAttachment)
  1288. {
  1289. rootpart.PhysActor.AddAngularForce(impulse, true);
  1290. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1291. }
  1292. }
  1293. }
  1294. }
  1295. public void setAngularImpulse(PhysicsVector impulse)
  1296. {
  1297. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1298. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1299. // Make sure we don't do that!
  1300. SceneObjectPart rootpart = m_rootPart;
  1301. if (rootpart != null)
  1302. {
  1303. if (rootpart.PhysActor != null)
  1304. {
  1305. if (!IsAttachment)
  1306. {
  1307. rootpart.PhysActor.Torque = impulse;
  1308. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1309. }
  1310. }
  1311. }
  1312. }
  1313. public Vector3 GetTorque()
  1314. {
  1315. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1316. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1317. // Make sure we don't do that!
  1318. SceneObjectPart rootpart = m_rootPart;
  1319. if (rootpart != null)
  1320. {
  1321. if (rootpart.PhysActor != null)
  1322. {
  1323. if (!IsAttachment)
  1324. {
  1325. PhysicsVector torque = rootpart.PhysActor.Torque;
  1326. return new Vector3(torque.X, torque.Y, torque.Z);
  1327. }
  1328. }
  1329. }
  1330. return Vector3.Zero;
  1331. }
  1332. public void moveToTarget(Vector3 target, float tau)
  1333. {
  1334. SceneObjectPart rootpart = m_rootPart;
  1335. if (rootpart != null)
  1336. {
  1337. if (rootpart.PhysActor != null)
  1338. {
  1339. rootpart.PhysActor.PIDTarget = new PhysicsVector(target.X, target.Y, target.Z);
  1340. rootpart.PhysActor.PIDTau = tau;
  1341. rootpart.PhysActor.PIDActive = true;
  1342. }
  1343. }
  1344. }
  1345. public void stopMoveToTarget()
  1346. {
  1347. SceneObjectPart rootpart = m_rootPart;
  1348. if (rootpart != null)
  1349. {
  1350. if (rootpart.PhysActor != null)
  1351. {
  1352. rootpart.PhysActor.PIDActive = false;
  1353. }
  1354. }
  1355. }
  1356. /// <summary>
  1357. /// Set the owner of the root part.
  1358. /// </summary>
  1359. /// <param name="part"></param>
  1360. /// <param name="cAgentID"></param>
  1361. /// <param name="cGroupID"></param>
  1362. public void SetRootPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1363. {
  1364. part.LastOwnerID = part.OwnerID;
  1365. part.OwnerID = cAgentID;
  1366. part.GroupID = cGroupID;
  1367. if (part.OwnerID != cAgentID)
  1368. {
  1369. // Apply Next Owner Permissions if we're not bypassing permissions
  1370. if (!m_scene.Permissions.BypassPermissions())
  1371. ApplyNextOwnerPermissions();
  1372. }
  1373. part.ScheduleFullUpdate();
  1374. }
  1375. /// <summary>
  1376. /// Make a copy of the given part.
  1377. /// </summary>
  1378. /// <param name="part"></param>
  1379. /// <param name="cAgentID"></param>
  1380. /// <param name="cGroupID"></param>
  1381. public SceneObjectPart CopyPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1382. {
  1383. SceneObjectPart newPart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed);
  1384. newPart.SetParent(this);
  1385. lock (m_parts)
  1386. {
  1387. m_parts.Add(newPart.UUID, newPart);
  1388. }
  1389. SetPartAsNonRoot(newPart);
  1390. return newPart;
  1391. }
  1392. /// <summary>
  1393. /// Reset the UUIDs for all the prims that make up this group.
  1394. ///
  1395. /// This is called by methods which want to add a new group to an existing scene, in order
  1396. /// to ensure that there are no clashes with groups already present.
  1397. /// </summary>
  1398. public void ResetIDs()
  1399. {
  1400. // As this is only ever called for prims which are not currently part of the scene (and hence
  1401. // not accessible by clients), there should be no need to lock
  1402. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.Values);
  1403. m_parts.Clear();
  1404. foreach (SceneObjectPart part in partsList)
  1405. {
  1406. part.ResetIDs(part.LinkNum); // Don't change link nums
  1407. m_parts.Add(part.UUID, part);
  1408. }
  1409. }
  1410. /// <summary>
  1411. ///
  1412. /// </summary>
  1413. /// <param name="part"></param>
  1414. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, UUID AgentID, uint RequestFlags)
  1415. {
  1416. remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.ObjectOwner, RootPart.GroupID, RootPart.BaseMask,
  1417. RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  1418. RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  1419. RootPart.CreatorID, RootPart.Name, RootPart.Description);
  1420. }
  1421. public void SetPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1422. {
  1423. part.OwnerID = cAgentID;
  1424. part.GroupID = cGroupID;
  1425. }
  1426. #endregion
  1427. #region Scheduling
  1428. public override void Update()
  1429. {
  1430. // Check that the group was not deleted before the scheduled update
  1431. // FIXME: This is merely a temporary measure to reduce the incidence of failure when
  1432. // an object has been deleted from a scene before update was processed.
  1433. // A more fundamental overhaul of the update mechanism is required to eliminate all
  1434. // the race conditions.
  1435. if (m_isDeleted)
  1436. return;
  1437. // This is what happens when an orphanced link set child prim's
  1438. // group was queued when it was linked
  1439. //
  1440. if (m_rootPart == null)
  1441. return;
  1442. lock (m_parts)
  1443. {
  1444. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1445. //if (IsAttachment)
  1446. //{
  1447. //foreach (SceneObjectPart part in m_parts.Values)
  1448. //{
  1449. //part.SendScheduledUpdates();
  1450. //}
  1451. //return;
  1452. //}
  1453. if ((Util.GetDistanceTo(lastPhysGroupPos, AbsolutePosition) > 0.02) && UsePhysics)
  1454. {
  1455. m_rootPart.UpdateFlag = 1;
  1456. lastPhysGroupPos = AbsolutePosition;
  1457. }
  1458. //foreach (SceneObjectPart part in m_parts.Values)
  1459. //{
  1460. //if (part.UpdateFlag == 0) part.UpdateFlag = 1;
  1461. //}
  1462. checkAtTargets();
  1463. if (((Math.Abs(lastPhysGroupRot.W - GroupRotation.W) > 0.1)
  1464. || (Math.Abs(lastPhysGroupRot.X - GroupRotation.X) > 0.1)
  1465. || (Math.Abs(lastPhysGroupRot.Y - GroupRotation.Y) > 0.1)
  1466. || (Math.Abs(lastPhysGroupRot.Z - GroupRotation.Z) > 0.1))
  1467. && UsePhysics)
  1468. {
  1469. m_rootPart.UpdateFlag = 1;
  1470. lastPhysGroupRot = GroupRotation;
  1471. }
  1472. foreach (SceneObjectPart part in m_parts.Values)
  1473. {
  1474. part.SendScheduledUpdates();
  1475. }
  1476. }
  1477. }
  1478. public void ScheduleFullUpdateToAvatar(ScenePresence presence)
  1479. {
  1480. RootPart.AddFullUpdateToAvatar(presence);
  1481. lock (m_parts)
  1482. {
  1483. foreach (SceneObjectPart part in m_parts.Values)
  1484. {
  1485. if (part != RootPart)
  1486. part.AddFullUpdateToAvatar(presence);
  1487. }
  1488. }
  1489. }
  1490. public void ScheduleTerseUpdateToAvatar(ScenePresence presence)
  1491. {
  1492. lock (m_parts)
  1493. {
  1494. foreach (SceneObjectPart part in m_parts.Values)
  1495. {
  1496. part.AddTerseUpdateToAvatar(presence);
  1497. }
  1498. }
  1499. }
  1500. /// <summary>
  1501. /// Schedule a full update for this scene object
  1502. /// </summary>
  1503. public void ScheduleGroupForFullUpdate()
  1504. {
  1505. checkAtTargets();
  1506. RootPart.ScheduleFullUpdate();
  1507. lock (m_parts)
  1508. {
  1509. foreach (SceneObjectPart part in m_parts.Values)
  1510. {
  1511. if (part != RootPart)
  1512. part.ScheduleFullUpdate();
  1513. }
  1514. }
  1515. }
  1516. /// <summary>
  1517. /// Schedule a terse update for this scene object
  1518. /// </summary>
  1519. public void ScheduleGroupForTerseUpdate()
  1520. {
  1521. lock (m_parts)
  1522. {
  1523. foreach (SceneObjectPart part in m_parts.Values)
  1524. {
  1525. part.ScheduleTerseUpdate();
  1526. }
  1527. }
  1528. }
  1529. /// <summary>
  1530. /// Immediately send a full update for this scene object.
  1531. /// </summary>
  1532. public void SendGroupFullUpdate()
  1533. {
  1534. if (IsDeleted)
  1535. return;
  1536. RootPart.SendFullUpdateToAllClients();
  1537. lock (m_parts)
  1538. {
  1539. foreach (SceneObjectPart part in m_parts.Values)
  1540. {
  1541. if (part != RootPart)
  1542. part.SendFullUpdateToAllClients();
  1543. }
  1544. }
  1545. }
  1546. public void QueueForUpdateCheck()
  1547. {
  1548. if (m_scene == null) // Need to check here as it's null during object creation
  1549. return;
  1550. m_scene.m_sceneGraph.AddToUpdateList(this);
  1551. }
  1552. /// <summary>
  1553. /// Immediately send a terse update for this scene object.
  1554. /// </summary>
  1555. public void SendGroupTerseUpdate()
  1556. {
  1557. if (IsDeleted)
  1558. return;
  1559. lock (m_parts)
  1560. {
  1561. foreach (SceneObjectPart part in m_parts.Values)
  1562. {
  1563. part.SendTerseUpdateToAllClients();
  1564. }
  1565. }
  1566. }
  1567. #endregion
  1568. #region SceneGroupPart Methods
  1569. /// <summary>
  1570. /// Get the child part by LinkNum
  1571. /// </summary>
  1572. /// <param name="linknum"></param>
  1573. /// <returns>null if no child part with that linknum or child part</returns>
  1574. public SceneObjectPart GetLinkNumPart(int linknum)
  1575. {
  1576. lock (m_parts)
  1577. {
  1578. foreach (SceneObjectPart part in m_parts.Values)
  1579. {
  1580. if (part.LinkNum == linknum)
  1581. {
  1582. return part;
  1583. }
  1584. }
  1585. }
  1586. return null;
  1587. }
  1588. /// <summary>
  1589. /// Get a child part with a given UUID
  1590. /// </summary>
  1591. /// <param name="primID"></param>
  1592. /// <returns>null if a child part with the primID was not found</returns>
  1593. public SceneObjectPart GetChildPart(UUID primID)
  1594. {
  1595. SceneObjectPart childPart = null;
  1596. if (m_parts.ContainsKey(primID))
  1597. {
  1598. childPart = m_parts[primID];
  1599. }
  1600. return childPart;
  1601. }
  1602. /// <summary>
  1603. /// Get a child part with a given local ID
  1604. /// </summary>
  1605. /// <param name="localID"></param>
  1606. /// <returns>null if a child part with the local ID was not found</returns>
  1607. public SceneObjectPart GetChildPart(uint localID)
  1608. {
  1609. //m_log.DebugFormat("Entered looking for {0}", localID);
  1610. lock (m_parts)
  1611. {
  1612. foreach (SceneObjectPart part in m_parts.Values)
  1613. {
  1614. //m_log.DebugFormat("Found {0}", part.LocalId);
  1615. if (part.LocalId == localID)
  1616. {
  1617. return part;
  1618. }
  1619. }
  1620. }
  1621. return null;
  1622. }
  1623. /// <summary>
  1624. /// Does this group contain the child prim
  1625. /// should be able to remove these methods once we have a entity index in scene
  1626. /// </summary>
  1627. /// <param name="primID"></param>
  1628. /// <returns></returns>
  1629. public bool HasChildPrim(UUID primID)
  1630. {
  1631. if (m_parts.ContainsKey(primID))
  1632. {
  1633. return true;
  1634. }
  1635. return false;
  1636. }
  1637. /// <summary>
  1638. /// Does this group contain the child prim
  1639. /// should be able to remove these methods once we have a entity index in scene
  1640. /// </summary>
  1641. /// <param name="localID"></param>
  1642. /// <returns></returns>
  1643. public bool HasChildPrim(uint localID)
  1644. {
  1645. //m_log.DebugFormat("Entered HasChildPrim looking for {0}", localID);
  1646. lock (m_parts)
  1647. {
  1648. foreach (SceneObjectPart part in m_parts.Values)
  1649. {
  1650. //m_log.DebugFormat("Found {0}", part.LocalId);
  1651. if (part.LocalId == localID)
  1652. {
  1653. return true;
  1654. }
  1655. }
  1656. }
  1657. return false;
  1658. }
  1659. #endregion
  1660. #region Packet Handlers
  1661. /// <summary>
  1662. /// Link the prims in a given group to this group
  1663. /// </summary>
  1664. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1665. public void LinkToGroup(SceneObjectGroup objectGroup)
  1666. {
  1667. if (objectGroup.RootPart.UpdateFlag > 0)
  1668. {
  1669. // I've never actually seen this happen, though I think it's theoretically possible
  1670. m_log.WarnFormat(
  1671. "[SCENE OBJECT GROUP]: Aborted linking {0}, {1} to {2}, {3} as it has yet to finish delinking",
  1672. objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1673. return;
  1674. }
  1675. // m_log.DebugFormat(
  1676. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1677. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1678. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1679. Vector3 oldGroupPosition = linkPart.GroupPosition;
  1680. Quaternion oldRootRotation = linkPart.RotationOffset;
  1681. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1682. linkPart.GroupPosition = AbsolutePosition;
  1683. Vector3 axPos = linkPart.OffsetPosition;
  1684. Quaternion parentRot = m_rootPart.RotationOffset;
  1685. axPos *= Quaternion.Inverse(parentRot);
  1686. linkPart.OffsetPosition = axPos;
  1687. Quaternion oldRot = linkPart.RotationOffset;
  1688. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  1689. linkPart.RotationOffset = newRot;
  1690. linkPart.ParentID = m_rootPart.LocalId;
  1691. if (m_rootPart.LinkNum == 0)
  1692. m_rootPart.LinkNum = 1;
  1693. lock (m_parts)
  1694. {
  1695. m_parts.Add(linkPart.UUID, linkPart);
  1696. // Insert in terms of link numbers, the new links
  1697. // before the current ones (with the exception of
  1698. // the root prim. Shuffle the old ones up
  1699. foreach (KeyValuePair<UUID, SceneObjectPart> kvp in m_parts)
  1700. {
  1701. if (kvp.Value.LinkNum != 1)
  1702. {
  1703. // Don't update root prim link number
  1704. kvp.Value.LinkNum += objectGroup.PrimCount;
  1705. }
  1706. }
  1707. linkPart.LinkNum = 2;
  1708. linkPart.SetParent(this);
  1709. linkPart.AddFlag(PrimFlags.CreateSelected);
  1710. //if (linkPart.PhysActor != null)
  1711. //{
  1712. // m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1713. //linkPart.PhysActor = null;
  1714. //}
  1715. //TODO: rest of parts
  1716. int linkNum = 3;
  1717. foreach (SceneObjectPart part in objectGroup.Children.Values)
  1718. {
  1719. if (part.UUID != objectGroup.m_rootPart.UUID)
  1720. {
  1721. LinkNonRootPart(part, oldGroupPosition, oldRootRotation, linkNum++);
  1722. }
  1723. part.ClearUndoState();
  1724. }
  1725. }
  1726. m_scene.UnlinkSceneObject(objectGroup.UUID, true);
  1727. // objectGroup.Children.Clear();
  1728. // objectGroup.m_rootPart = null;
  1729. // TODO Deleting the original group object may cause problems later on if they have already
  1730. // made it into the update queue. However, sending out updates for those parts is now
  1731. // spurious, so it would be good not to send them at some point.
  1732. // The traffic caused is always going to be pretty minor, so it's not high priority
  1733. //objectGroup.DeleteGroup();
  1734. AttachToBackup();
  1735. HasGroupChanged = true;
  1736. ScheduleGroupForFullUpdate();
  1737. }
  1738. /// <summary>
  1739. /// Delink the given prim from this group. The delinked prim is established as
  1740. /// an independent SceneObjectGroup.
  1741. /// </summary>
  1742. /// <param name="partID"></param>
  1743. public void DelinkFromGroup(uint partID)
  1744. {
  1745. DelinkFromGroup(partID, true);
  1746. }
  1747. /// <summary>
  1748. /// Delink the given prim from this group. The delinked prim is established as
  1749. /// an independent SceneObjectGroup.
  1750. /// </summary>
  1751. /// <param name="partID"></param>
  1752. /// <param name="sendEvents"></param>
  1753. public void DelinkFromGroup(uint partID, bool sendEvents)
  1754. {
  1755. SceneObjectPart linkPart = GetChildPart(partID);
  1756. if (null != linkPart)
  1757. {
  1758. linkPart.ClearUndoState();
  1759. // m_log.DebugFormat(
  1760. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  1761. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  1762. Quaternion worldRot = linkPart.GetWorldRotation();
  1763. // Remove the part from this object
  1764. lock (m_parts)
  1765. {
  1766. m_parts.Remove(linkPart.UUID);
  1767. }
  1768. if (m_parts.Count == 1 && RootPart != null) //Single prim is left
  1769. RootPart.LinkNum = 0;
  1770. else
  1771. {
  1772. foreach (SceneObjectPart p in m_parts.Values)
  1773. {
  1774. if (p.LinkNum > linkPart.LinkNum)
  1775. p.LinkNum--;
  1776. }
  1777. }
  1778. linkPart.ParentID = 0;
  1779. linkPart.LinkNum = 0;
  1780. if (linkPart.PhysActor != null)
  1781. {
  1782. m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1783. }
  1784. // We need to reset the child part's position
  1785. // ready for life as a separate object after being a part of another object
  1786. Quaternion parentRot = m_rootPart.RotationOffset;
  1787. Vector3 axPos = linkPart.OffsetPosition;
  1788. axPos *= parentRot;
  1789. linkPart.OffsetPosition = new Vector3(axPos.X, axPos.Y, axPos.Z);
  1790. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  1791. linkPart.OffsetPosition = new Vector3(0, 0, 0);
  1792. linkPart.RotationOffset = worldRot;
  1793. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  1794. m_scene.AddNewSceneObject(objectGroup, true);
  1795. if (sendEvents)
  1796. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  1797. linkPart.Rezzed = RootPart.Rezzed;
  1798. HasGroupChanged = true;
  1799. ScheduleGroupForFullUpdate();
  1800. }
  1801. else
  1802. {
  1803. m_log.InfoFormat("[SCENE OBJECT GROUP]: " +
  1804. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  1805. partID, LocalId, UUID);
  1806. }
  1807. }
  1808. /// <summary>
  1809. /// Stop this object from being persisted over server restarts.
  1810. /// </summary>
  1811. /// <param name="objectGroup"></param>
  1812. public void DetachFromBackup()
  1813. {
  1814. if (m_isBackedUp)
  1815. m_scene.EventManager.OnBackup -= ProcessBackup;
  1816. m_isBackedUp = false;
  1817. }
  1818. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation, int linkNum)
  1819. {
  1820. part.SetParent(this);
  1821. part.ParentID = m_rootPart.LocalId;
  1822. // Caller locks m_parts for us
  1823. m_parts.Add(part.UUID, part);
  1824. part.LinkNum = linkNum;
  1825. Vector3 oldPos = part.OffsetPosition;
  1826. oldPos *= oldGroupRotation;
  1827. oldPos += oldGroupPosition;
  1828. Vector3 oldAbsolutePosition = oldPos;
  1829. part.OffsetPosition = oldAbsolutePosition - AbsolutePosition;
  1830. Quaternion rootRotation = m_rootPart.RotationOffset;
  1831. Vector3 pos = part.OffsetPosition;
  1832. pos *= Quaternion.Inverse(rootRotation);
  1833. part.OffsetPosition = pos;
  1834. Quaternion partRotation = part.RotationOffset;
  1835. partRotation *= oldGroupRotation;
  1836. partRotation *= Quaternion.Inverse(rootRotation);
  1837. part.RotationOffset = partRotation;
  1838. }
  1839. /// <summary>
  1840. /// If object is physical, apply force to move it around
  1841. /// If object is not physical, just put it at the resulting location
  1842. /// </summary>
  1843. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  1844. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  1845. /// <param name="remoteClient"></param>
  1846. public void GrabMovement(Vector3 offset, Vector3 pos, IClientAPI remoteClient)
  1847. {
  1848. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  1849. {
  1850. if (m_rootPart.PhysActor != null)
  1851. {
  1852. if (m_rootPart.PhysActor.IsPhysical)
  1853. {
  1854. Vector3 llmoveforce = pos - AbsolutePosition;
  1855. PhysicsVector grabforce = new PhysicsVector(llmoveforce.X, llmoveforce.Y, llmoveforce.Z);
  1856. grabforce = (grabforce / 10) * m_rootPart.PhysActor.Mass;
  1857. m_rootPart.PhysActor.AddForce(grabforce,true);
  1858. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1859. }
  1860. else
  1861. {
  1862. //NonPhysicalGrabMovement(pos);
  1863. }
  1864. }
  1865. else
  1866. {
  1867. //NonPhysicalGrabMovement(pos);
  1868. }
  1869. }
  1870. }
  1871. public void NonPhysicalGrabMovement(Vector3 pos)
  1872. {
  1873. AbsolutePosition = pos;
  1874. m_rootPart.SendTerseUpdateToAllClients();
  1875. }
  1876. /// <summary>
  1877. /// Return metadata about a prim (name, description, sale price, etc.)
  1878. /// </summary>
  1879. /// <param name="client"></param>
  1880. public void GetProperties(IClientAPI client)
  1881. {
  1882. m_rootPart.GetProperties(client);
  1883. }
  1884. /// <summary>
  1885. /// Set the name of a prim
  1886. /// </summary>
  1887. /// <param name="name"></param>
  1888. /// <param name="localID"></param>
  1889. public void SetPartName(string name, uint localID)
  1890. {
  1891. SceneObjectPart part = GetChildPart(localID);
  1892. if (part != null)
  1893. {
  1894. part.Name = name;
  1895. }
  1896. }
  1897. public void SetPartDescription(string des, uint localID)
  1898. {
  1899. SceneObjectPart part = GetChildPart(localID);
  1900. if (part != null)
  1901. {
  1902. part.Description = des;
  1903. }
  1904. }
  1905. public void SetPartText(string text, uint localID)
  1906. {
  1907. SceneObjectPart part = GetChildPart(localID);
  1908. if (part != null)
  1909. {
  1910. part.SetText(text);
  1911. }
  1912. }
  1913. public void SetPartText(string text, UUID partID)
  1914. {
  1915. SceneObjectPart part = GetChildPart(partID);
  1916. if (part != null)
  1917. {
  1918. part.SetText(text);
  1919. }
  1920. }
  1921. public string GetPartName(uint localID)
  1922. {
  1923. SceneObjectPart part = GetChildPart(localID);
  1924. if (part != null)
  1925. {
  1926. return part.Name;
  1927. }
  1928. return String.Empty;
  1929. }
  1930. public string GetPartDescription(uint localID)
  1931. {
  1932. SceneObjectPart part = GetChildPart(localID);
  1933. if (part != null)
  1934. {
  1935. return part.Description;
  1936. }
  1937. return String.Empty;
  1938. }
  1939. /// <summary>
  1940. /// Update prim flags for this group.
  1941. /// </summary>
  1942. /// <param name="localID"></param>
  1943. /// <param name="type"></param>
  1944. /// <param name="inUse"></param>
  1945. /// <param name="data"></param>
  1946. public void UpdatePrimFlags(uint localID, bool UsePhysics, bool IsTemporary, bool IsPhantom)
  1947. {
  1948. SceneObjectPart selectionPart = GetChildPart(localID);
  1949. if (IsTemporary)
  1950. {
  1951. DetachFromBackup();
  1952. // Remove from database and parcel prim count
  1953. //
  1954. m_scene.DeleteFromStorage(UUID);
  1955. m_scene.EventManager.TriggerParcelPrimCountTainted();
  1956. }
  1957. if (selectionPart != null)
  1958. {
  1959. lock (m_parts)
  1960. {
  1961. foreach (SceneObjectPart part in m_parts.Values)
  1962. {
  1963. if (part.Scale.X > 10.0 || part.Scale.Y > 10.0 || part.Scale.Z > 10.0)
  1964. {
  1965. UsePhysics = false; // Reset physics
  1966. break;
  1967. }
  1968. }
  1969. foreach (SceneObjectPart part in m_parts.Values)
  1970. {
  1971. part.UpdatePrimFlags(UsePhysics, IsTemporary, IsPhantom);
  1972. }
  1973. }
  1974. }
  1975. }
  1976. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  1977. {
  1978. SceneObjectPart part = GetChildPart(localID);
  1979. if (part != null)
  1980. {
  1981. part.UpdateExtraParam(type, inUse, data);
  1982. }
  1983. }
  1984. /// <summary>
  1985. /// Get the parts of this scene object
  1986. /// </summary>
  1987. /// <returns></returns>
  1988. public SceneObjectPart[] GetParts()
  1989. {
  1990. int numParts = Children.Count;
  1991. SceneObjectPart[] partArray = new SceneObjectPart[numParts];
  1992. Children.Values.CopyTo(partArray, 0);
  1993. return partArray;
  1994. }
  1995. /// <summary>
  1996. /// Update the texture entry for this part
  1997. /// </summary>
  1998. /// <param name="localID"></param>
  1999. /// <param name="textureEntry"></param>
  2000. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  2001. {
  2002. SceneObjectPart part = GetChildPart(localID);
  2003. if (part != null)
  2004. {
  2005. part.UpdateTextureEntry(textureEntry);
  2006. }
  2007. }
  2008. public void UpdatePermissions(UUID AgentID, byte field, uint localID,
  2009. uint mask, byte addRemTF)
  2010. {
  2011. foreach (SceneObjectPart part in m_parts.Values)
  2012. part.UpdatePermissions(AgentID, field, localID, mask,
  2013. addRemTF);
  2014. HasGroupChanged = true;
  2015. }
  2016. #endregion
  2017. #region Shape
  2018. /// <summary>
  2019. ///
  2020. /// </summary>
  2021. /// <param name="shapeBlock"></param>
  2022. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  2023. {
  2024. SceneObjectPart part = GetChildPart(localID);
  2025. if (part != null)
  2026. {
  2027. part.UpdateShape(shapeBlock);
  2028. if (part.PhysActor != null)
  2029. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2030. }
  2031. }
  2032. #endregion
  2033. #region Resize
  2034. /// <summary>
  2035. /// Resize the given part
  2036. /// </summary>
  2037. /// <param name="scale"></param>
  2038. /// <param name="localID"></param>
  2039. public void Resize(Vector3 scale, uint localID)
  2040. {
  2041. if (scale.X > m_scene.m_maxNonphys)
  2042. scale.X = m_scene.m_maxNonphys;
  2043. if (scale.Y > m_scene.m_maxNonphys)
  2044. scale.Y = m_scene.m_maxNonphys;
  2045. if (scale.Z > m_scene.m_maxNonphys)
  2046. scale.Z = m_scene.m_maxNonphys;
  2047. SceneObjectPart part = GetChildPart(localID);
  2048. if (part != null)
  2049. {
  2050. part.Resize(scale);
  2051. if (part.PhysActor != null)
  2052. {
  2053. if (part.PhysActor.IsPhysical)
  2054. {
  2055. if (scale.X > m_scene.m_maxPhys)
  2056. scale.X = m_scene.m_maxPhys;
  2057. if (scale.Y > m_scene.m_maxPhys)
  2058. scale.Y = m_scene.m_maxPhys;
  2059. if (scale.Z > m_scene.m_maxPhys)
  2060. scale.Z = m_scene.m_maxPhys;
  2061. }
  2062. part.PhysActor.Size =
  2063. new PhysicsVector(scale.X, scale.Y, scale.Z);
  2064. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2065. }
  2066. //if (part.UUID != m_rootPart.UUID)
  2067. HasGroupChanged = true;
  2068. ScheduleGroupForFullUpdate();
  2069. //if (part.UUID == m_rootPart.UUID)
  2070. //{
  2071. //if (m_rootPart.PhysActor != null)
  2072. //{
  2073. //m_rootPart.PhysActor.Size =
  2074. //new PhysicsVector(m_rootPart.Scale.X, m_rootPart.Scale.Y, m_rootPart.Scale.Z);
  2075. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2076. //}
  2077. //}
  2078. }
  2079. }
  2080. public void GroupResize(Vector3 scale, uint localID)
  2081. {
  2082. SceneObjectPart part = GetChildPart(localID);
  2083. if (part != null)
  2084. {
  2085. if (scale.X > m_scene.m_maxNonphys)
  2086. scale.X = m_scene.m_maxNonphys;
  2087. if (scale.Y > m_scene.m_maxNonphys)
  2088. scale.Y = m_scene.m_maxNonphys;
  2089. if (scale.Z > m_scene.m_maxNonphys)
  2090. scale.Z = m_scene.m_maxNonphys;
  2091. if (part.PhysActor != null && part.PhysActor.IsPhysical)
  2092. {
  2093. if (scale.X > m_scene.m_maxPhys)
  2094. scale.X = m_scene.m_maxPhys;
  2095. if (scale.Y > m_scene.m_maxPhys)
  2096. scale.Y = m_scene.m_maxPhys;
  2097. if (scale.Z > m_scene.m_maxPhys)
  2098. scale.Z = m_scene.m_maxPhys;
  2099. }
  2100. float x = (scale.X / part.Scale.X);
  2101. float y = (scale.Y / part.Scale.Y);
  2102. float z = (scale.Z / part.Scale.Z);
  2103. lock (m_parts)
  2104. {
  2105. if (x > 1.0f || y > 1.0f || z > 1.0f)
  2106. {
  2107. foreach (SceneObjectPart obPart in m_parts.Values)
  2108. {
  2109. if (obPart.UUID != m_rootPart.UUID)
  2110. {
  2111. Vector3 oldSize = new Vector3(obPart.Scale);
  2112. float f = 1.0f;
  2113. float a = 1.0f;
  2114. if (part.PhysActor != null && part.PhysActor.IsPhysical)
  2115. {
  2116. if (oldSize.X*x > m_scene.m_maxPhys)
  2117. {
  2118. f = m_scene.m_maxPhys / oldSize.X;
  2119. a = f / x;
  2120. x *= a;
  2121. y *= a;
  2122. z *= a;
  2123. }
  2124. if (oldSize.Y*y > m_scene.m_maxPhys)
  2125. {
  2126. f = m_scene.m_maxPhys / oldSize.Y;
  2127. a = f / y;
  2128. x *= a;
  2129. y *= a;
  2130. z *= a;
  2131. }
  2132. if (oldSize.Z*z > m_scene.m_maxPhys)
  2133. {
  2134. f = m_scene.m_maxPhys / oldSize.Z;
  2135. a = f / z;
  2136. x *= a;
  2137. y *= a;
  2138. z *= a;
  2139. }
  2140. }
  2141. else
  2142. {
  2143. if (oldSize.X*x > m_scene.m_maxNonphys)
  2144. {
  2145. f = m_scene.m_maxNonphys / oldSize.X;
  2146. a = f / x;
  2147. x *= a;
  2148. y *= a;
  2149. z *= a;
  2150. }
  2151. if (oldSize.Y*y > m_scene.m_maxNonphys)
  2152. {
  2153. f = m_scene.m_maxNonphys / oldSize.Y;
  2154. a = f / y;
  2155. x *= a;
  2156. y *= a;
  2157. z *= a;
  2158. }
  2159. if (oldSize.Z*z > m_scene.m_maxNonphys)
  2160. {
  2161. f = m_scene.m_maxNonphys / oldSize.Z;
  2162. a = f / z;
  2163. x *= a;
  2164. y *= a;
  2165. z *= a;
  2166. }
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. Vector3 prevScale = part.Scale;
  2173. prevScale.X *= x;
  2174. prevScale.Y *= y;
  2175. prevScale.Z *= z;
  2176. part.Resize(prevScale);
  2177. lock (m_parts)
  2178. {
  2179. foreach (SceneObjectPart obPart in m_parts.Values)
  2180. {
  2181. if (obPart.UUID != m_rootPart.UUID)
  2182. {
  2183. Vector3 currentpos = new Vector3(obPart.OffsetPosition);
  2184. currentpos.X *= x;
  2185. currentpos.Y *= y;
  2186. currentpos.Z *= z;
  2187. Vector3 newSize = new Vector3(obPart.Scale);
  2188. newSize.X *= x;
  2189. newSize.Y *= y;
  2190. newSize.Z *= z;
  2191. obPart.Resize(newSize);
  2192. obPart.UpdateOffSet(currentpos);
  2193. }
  2194. }
  2195. }
  2196. if (part.PhysActor != null)
  2197. {
  2198. part.PhysActor.Size =
  2199. new PhysicsVector(prevScale.X, prevScale.Y, prevScale.Z);
  2200. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2201. }
  2202. HasGroupChanged = true;
  2203. ScheduleGroupForTerseUpdate();
  2204. }
  2205. }
  2206. #endregion
  2207. #region Position
  2208. /// <summary>
  2209. /// Move this scene object
  2210. /// </summary>
  2211. /// <param name="pos"></param>
  2212. public void UpdateGroupPosition(Vector3 pos)
  2213. {
  2214. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2215. {
  2216. if (IsAttachment)
  2217. {
  2218. m_rootPart.AttachedPos = pos;
  2219. }
  2220. AbsolutePosition = pos;
  2221. HasGroupChanged = true;
  2222. }
  2223. //we need to do a terse update even if the move wasn't allowed
  2224. // so that the position is reset in the client (the object snaps back)
  2225. ScheduleGroupForTerseUpdate();
  2226. }
  2227. /// <summary>
  2228. /// Update the position of a single part of this scene object
  2229. /// </summary>
  2230. /// <param name="pos"></param>
  2231. /// <param name="localID"></param>
  2232. public void UpdateSinglePosition(Vector3 pos, uint localID)
  2233. {
  2234. SceneObjectPart part = GetChildPart(localID);
  2235. if (part != null)
  2236. {
  2237. if (part.UUID == m_rootPart.UUID)
  2238. {
  2239. UpdateRootPosition(pos);
  2240. }
  2241. else
  2242. {
  2243. part.UpdateOffSet(pos);
  2244. }
  2245. HasGroupChanged = true;
  2246. }
  2247. }
  2248. /// <summary>
  2249. ///
  2250. /// </summary>
  2251. /// <param name="pos"></param>
  2252. private void UpdateRootPosition(Vector3 pos)
  2253. {
  2254. Vector3 newPos = new Vector3(pos.X, pos.Y, pos.Z);
  2255. Vector3 oldPos =
  2256. new Vector3(AbsolutePosition.X + m_rootPart.OffsetPosition.X,
  2257. AbsolutePosition.Y + m_rootPart.OffsetPosition.Y,
  2258. AbsolutePosition.Z + m_rootPart.OffsetPosition.Z);
  2259. Vector3 diff = oldPos - newPos;
  2260. Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
  2261. Quaternion partRotation = m_rootPart.RotationOffset;
  2262. axDiff *= Quaternion.Inverse(partRotation);
  2263. diff = axDiff;
  2264. lock (m_parts)
  2265. {
  2266. foreach (SceneObjectPart obPart in m_parts.Values)
  2267. {
  2268. if (obPart.UUID != m_rootPart.UUID)
  2269. {
  2270. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  2271. }
  2272. }
  2273. }
  2274. AbsolutePosition = newPos;
  2275. HasGroupChanged = true;
  2276. ScheduleGroupForTerseUpdate();
  2277. }
  2278. public void OffsetForNewRegion(Vector3 offset)
  2279. {
  2280. m_rootPart.GroupPosition = offset;
  2281. }
  2282. #endregion
  2283. #region Rotation
  2284. /// <summary>
  2285. ///
  2286. /// </summary>
  2287. /// <param name="rot"></param>
  2288. public void UpdateGroupRotation(Quaternion rot)
  2289. {
  2290. m_rootPart.UpdateRotation(rot);
  2291. if (m_rootPart.PhysActor != null)
  2292. {
  2293. m_rootPart.PhysActor.Orientation = m_rootPart.RotationOffset;
  2294. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2295. }
  2296. HasGroupChanged = true;
  2297. ScheduleGroupForTerseUpdate();
  2298. }
  2299. /// <summary>
  2300. ///
  2301. /// </summary>
  2302. /// <param name="pos"></param>
  2303. /// <param name="rot"></param>
  2304. public void UpdateGroupRotation(Vector3 pos, Quaternion rot)
  2305. {
  2306. m_rootPart.UpdateRotation(rot);
  2307. if (m_rootPart.PhysActor != null)
  2308. {
  2309. m_rootPart.PhysActor.Orientation = m_rootPart.RotationOffset;
  2310. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2311. }
  2312. AbsolutePosition = pos;
  2313. HasGroupChanged = true;
  2314. ScheduleGroupForTerseUpdate();
  2315. }
  2316. /// <summary>
  2317. ///
  2318. /// </summary>
  2319. /// <param name="rot"></param>
  2320. /// <param name="localID"></param>
  2321. public void UpdateSingleRotation(Quaternion rot, uint localID)
  2322. {
  2323. SceneObjectPart part = GetChildPart(localID);
  2324. if (part != null)
  2325. {
  2326. if (part.UUID == m_rootPart.UUID)
  2327. {
  2328. UpdateRootRotation(rot);
  2329. }
  2330. else
  2331. {
  2332. part.UpdateRotation(rot);
  2333. }
  2334. }
  2335. }
  2336. /// <summary>
  2337. ///
  2338. /// </summary>
  2339. /// <param name="rot"></param>
  2340. private void UpdateRootRotation(Quaternion rot)
  2341. {
  2342. Quaternion axRot = rot;
  2343. Quaternion oldParentRot = m_rootPart.RotationOffset;
  2344. m_rootPart.UpdateRotation(rot);
  2345. if (m_rootPart.PhysActor != null)
  2346. {
  2347. m_rootPart.PhysActor.Orientation = m_rootPart.RotationOffset;
  2348. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2349. }
  2350. lock (m_parts)
  2351. {
  2352. foreach (SceneObjectPart prim in m_parts.Values)
  2353. {
  2354. if (prim.UUID != m_rootPart.UUID)
  2355. {
  2356. Vector3 axPos = prim.OffsetPosition;
  2357. axPos *= oldParentRot;
  2358. axPos *= Quaternion.Inverse(axRot);
  2359. prim.OffsetPosition = axPos;
  2360. Quaternion primsRot = prim.RotationOffset;
  2361. Quaternion newRot = oldParentRot * primsRot;
  2362. newRot *= Quaternion.Inverse(axRot);
  2363. prim.RotationOffset = newRot;
  2364. prim.ScheduleTerseUpdate();
  2365. }
  2366. }
  2367. }
  2368. m_rootPart.ScheduleTerseUpdate();
  2369. }
  2370. #endregion
  2371. internal void SetAxisRotation(int axis, int rotate10)
  2372. {
  2373. bool setX = false;
  2374. bool setY = false;
  2375. bool setZ = false;
  2376. int xaxis = 2;
  2377. int yaxis = 4;
  2378. int zaxis = 8;
  2379. if (m_rootPart != null)
  2380. {
  2381. setX = ((axis & xaxis) != 0) ? true : false;
  2382. setY = ((axis & yaxis) != 0) ? true : false;
  2383. setZ = ((axis & zaxis) != 0) ? true : false;
  2384. float setval = (rotate10 > 0) ? 1f : 0f;
  2385. if (setX)
  2386. m_rootPart.RotationAxis.X = setval;
  2387. if (setY)
  2388. m_rootPart.RotationAxis.Y = setval;
  2389. if (setZ)
  2390. m_rootPart.RotationAxis.Z = setval;
  2391. if (setX || setY || setZ)
  2392. {
  2393. m_rootPart.SetPhysicsAxisRotation();
  2394. }
  2395. }
  2396. }
  2397. public int registerTargetWaypoint(Vector3 target, float tolerance)
  2398. {
  2399. scriptPosTarget waypoint = new scriptPosTarget();
  2400. waypoint.targetPos = target;
  2401. waypoint.tolerance = tolerance;
  2402. uint handle = m_scene.AllocateLocalId();
  2403. lock (m_targets)
  2404. {
  2405. m_targets.Add(handle, waypoint);
  2406. }
  2407. return (int)handle;
  2408. }
  2409. public void unregisterTargetWaypoint(int handle)
  2410. {
  2411. lock (m_targets)
  2412. {
  2413. if (m_targets.ContainsKey((uint)handle))
  2414. m_targets.Remove((uint)handle);
  2415. }
  2416. }
  2417. private void checkAtTargets()
  2418. {
  2419. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  2420. {
  2421. if (m_targets.Count > 0)
  2422. {
  2423. bool at_target = false;
  2424. //Vector3 targetPos;
  2425. //uint targetHandle;
  2426. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  2427. lock (m_targets)
  2428. {
  2429. foreach (uint idx in m_targets.Keys)
  2430. {
  2431. scriptPosTarget target = m_targets[idx];
  2432. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  2433. {
  2434. // trigger at_target
  2435. if (m_scriptListens_atTarget)
  2436. {
  2437. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2438. // to avoid deadlocking the sim.
  2439. at_target = true;
  2440. scriptPosTarget att = new scriptPosTarget();
  2441. att.targetPos = target.targetPos;
  2442. att.tolerance = (float)idx;
  2443. atTargets.Add(idx, att);
  2444. }
  2445. }
  2446. }
  2447. }
  2448. if (atTargets.Count > 0)
  2449. {
  2450. uint[] localids = new uint[0];
  2451. lock (m_parts)
  2452. {
  2453. localids = new uint[m_parts.Count];
  2454. int cntr = 0;
  2455. foreach (SceneObjectPart part in m_parts.Values)
  2456. {
  2457. localids[cntr] = part.LocalId;
  2458. cntr++;
  2459. }
  2460. }
  2461. for (int ctr = 0; ctr < localids.Length; ctr++)
  2462. {
  2463. foreach (uint target in atTargets.Keys)
  2464. {
  2465. scriptPosTarget att = atTargets[target];
  2466. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2467. // to avoid deadlocking the sim.
  2468. m_scene.TriggerAtTargetEvent(localids[ctr], (uint)att.tolerance, att.targetPos, m_rootPart.GroupPosition);
  2469. }
  2470. }
  2471. return;
  2472. }
  2473. if (m_scriptListens_notAtTarget && !at_target)
  2474. {
  2475. //trigger not_at_target
  2476. uint[] localids = new uint[0];
  2477. lock (m_parts)
  2478. {
  2479. localids = new uint[m_parts.Count];
  2480. int cntr = 0;
  2481. foreach (SceneObjectPart part in m_parts.Values)
  2482. {
  2483. localids[cntr] = part.LocalId;
  2484. cntr++;
  2485. }
  2486. }
  2487. for (int ctr = 0; ctr < localids.Length; ctr++)
  2488. {
  2489. m_scene.TriggerNotAtTargetEvent(localids[ctr]);
  2490. }
  2491. }
  2492. }
  2493. }
  2494. }
  2495. public float GetMass()
  2496. {
  2497. float retmass = 0f;
  2498. lock (m_parts)
  2499. {
  2500. foreach (SceneObjectPart part in m_parts.Values)
  2501. {
  2502. retmass += part.GetMass();
  2503. }
  2504. }
  2505. return retmass;
  2506. }
  2507. public void CheckSculptAndLoad()
  2508. {
  2509. lock (m_parts)
  2510. {
  2511. if (!IsDeleted)
  2512. {
  2513. if ((RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Phantom) == 0)
  2514. {
  2515. foreach (SceneObjectPart part in m_parts.Values)
  2516. {
  2517. if (part.Shape.SculptEntry && part.Shape.SculptTexture != UUID.Zero)
  2518. {
  2519. m_scene.AssetCache.GetAsset(part.Shape.SculptTexture, part.SculptTextureCallback, true);
  2520. }
  2521. }
  2522. }
  2523. }
  2524. }
  2525. }
  2526. /// <summary>
  2527. /// Set the user group to which this scene object belongs.
  2528. /// </summary>
  2529. /// <param name="GroupID"></param>
  2530. /// <param name="client"></param>
  2531. public void SetGroup(UUID GroupID, IClientAPI client)
  2532. {
  2533. lock (m_parts)
  2534. {
  2535. foreach (SceneObjectPart part in m_parts.Values)
  2536. {
  2537. part.SetGroup(GroupID, client);
  2538. part.Inventory.ChangeInventoryGroup(GroupID);
  2539. }
  2540. HasGroupChanged = true;
  2541. }
  2542. ScheduleGroupForFullUpdate();
  2543. }
  2544. public void TriggerScriptChangedEvent(Changed val)
  2545. {
  2546. foreach (SceneObjectPart part in Children.Values)
  2547. {
  2548. part.TriggerScriptChangedEvent(val);
  2549. }
  2550. }
  2551. public override string ToString()
  2552. {
  2553. return String.Format("{0} {1} ({2})", Name, UUID, AbsolutePosition);
  2554. }
  2555. public void SetAttachmentPoint(byte point)
  2556. {
  2557. lock (m_parts)
  2558. {
  2559. foreach (SceneObjectPart part in m_parts.Values)
  2560. part.SetAttachmentPoint(point);
  2561. }
  2562. }
  2563. }
  2564. }