SceneObjectGroup.cs 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.ComponentModel;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Threading;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using OpenMetaverse;
  37. using OpenMetaverse.Packets;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Physics.Manager;
  41. using OpenSim.Region.Framework.Scenes.Serialization;
  42. namespace OpenSim.Region.Framework.Scenes
  43. {
  44. [Flags]
  45. public enum scriptEvents
  46. {
  47. None = 0,
  48. attach = 1,
  49. collision = 16,
  50. collision_end = 32,
  51. collision_start = 64,
  52. control = 128,
  53. dataserver = 256,
  54. email = 512,
  55. http_response = 1024,
  56. land_collision = 2048,
  57. land_collision_end = 4096,
  58. land_collision_start = 8192,
  59. at_target = 16384,
  60. at_rot_target = 16777216,
  61. listen = 32768,
  62. money = 65536,
  63. moving_end = 131072,
  64. moving_start = 262144,
  65. not_at_rot_target = 524288,
  66. not_at_target = 1048576,
  67. remote_data = 8388608,
  68. run_time_permissions = 268435456,
  69. state_entry = 1073741824,
  70. state_exit = 2,
  71. timer = 4,
  72. touch = 8,
  73. touch_end = 536870912,
  74. touch_start = 2097152,
  75. object_rez = 4194304
  76. }
  77. struct scriptPosTarget
  78. {
  79. public Vector3 targetPos;
  80. public float tolerance;
  81. public uint handle;
  82. }
  83. struct scriptRotTarget
  84. {
  85. public Quaternion targetRot;
  86. public float tolerance;
  87. public uint handle;
  88. }
  89. public delegate void PrimCountTaintedDelegate();
  90. /// <summary>
  91. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  92. /// (often known as prims), one of which is considered the root part.
  93. /// </summary>
  94. public partial class SceneObjectGroup : EntityBase, ISceneObject
  95. {
  96. // private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  97. /// <summary>
  98. /// Signal whether the non-inventory attributes of any prims in the group have changed
  99. /// since the group's last persistent backup
  100. /// </summary>
  101. private bool m_hasGroupChanged = false;
  102. private long timeFirstChanged;
  103. private long timeLastChanged;
  104. /// <summary>
  105. /// This indicates whether the object has changed such that it needs to be repersisted to permenant storage
  106. /// (the database).
  107. /// </summary>
  108. /// <remarks>
  109. /// Ultimately, this should be managed such that region modules can change it at the end of a set of operations
  110. /// so that either all changes are preserved or none at all. However, currently, a large amount of internal
  111. /// code will set this anyway when some object properties are changed.
  112. /// </remarks>
  113. public bool HasGroupChanged
  114. {
  115. set
  116. {
  117. if (value)
  118. {
  119. timeLastChanged = DateTime.Now.Ticks;
  120. if (!m_hasGroupChanged)
  121. timeFirstChanged = DateTime.Now.Ticks;
  122. }
  123. m_hasGroupChanged = value;
  124. // m_log.DebugFormat(
  125. // "[SCENE OBJECT GROUP]: HasGroupChanged set to {0} for {1} {2}", m_hasGroupChanged, Name, LocalId);
  126. }
  127. get { return m_hasGroupChanged; }
  128. }
  129. /// <summary>
  130. /// Has the group changed due to an unlink operation? We record this in order to optimize deletion, since
  131. /// an unlinked group currently has to be persisted to the database before we can perform an unlink operation.
  132. /// </summary>
  133. public bool HasGroupChangedDueToDelink { get; private set; }
  134. private bool isTimeToPersist()
  135. {
  136. if (IsSelected || IsDeleted || IsAttachment)
  137. return false;
  138. if (!m_hasGroupChanged)
  139. return false;
  140. if (m_scene.ShuttingDown)
  141. return true;
  142. long currentTime = DateTime.Now.Ticks;
  143. if (currentTime - timeLastChanged > m_scene.m_dontPersistBefore || currentTime - timeFirstChanged > m_scene.m_persistAfter)
  144. return true;
  145. return false;
  146. }
  147. /// <summary>
  148. /// Is this scene object acting as an attachment?
  149. /// </summary>
  150. public bool IsAttachment { get; set; }
  151. /// <summary>
  152. /// The avatar to which this scene object is attached.
  153. /// </summary>
  154. /// <remarks>
  155. /// If we're not attached to an avatar then this is UUID.Zero
  156. /// </remarks>
  157. public UUID AttachedAvatar { get; set; }
  158. /// <summary>
  159. /// Attachment point of this scene object to an avatar.
  160. /// </summary>
  161. /// <remarks>
  162. /// 0 if we're not attached to anything
  163. /// </remarks>
  164. public uint AttachmentPoint
  165. {
  166. get
  167. {
  168. return m_rootPart.Shape.State;
  169. }
  170. set
  171. {
  172. IsAttachment = value != 0;
  173. m_rootPart.Shape.State = (byte)value;
  174. }
  175. }
  176. /// <summary>
  177. /// If this scene object has an attachment point then indicate whether there is a point where
  178. /// attachments are perceivable by avatars other than the avatar to which this object is attached.
  179. /// </summary>
  180. /// <remarks>
  181. /// HUDs are not perceivable by other avatars.
  182. /// </remarks>
  183. public bool HasPrivateAttachmentPoint
  184. {
  185. get
  186. {
  187. return AttachmentPoint >= (uint)OpenMetaverse.AttachmentPoint.HUDCenter2
  188. && AttachmentPoint <= (uint)OpenMetaverse.AttachmentPoint.HUDBottomRight;
  189. }
  190. }
  191. public void ClearPartAttachmentData()
  192. {
  193. AttachmentPoint = 0;
  194. // Even though we don't use child part state parameters for attachments any more, we still need to set
  195. // these to zero since having them non-zero in rezzed scene objects will crash some clients. Even if
  196. // we store them correctly, scene objects that we receive from elsewhere might not.
  197. foreach (SceneObjectPart part in Parts)
  198. part.Shape.State = 0;
  199. }
  200. /// <summary>
  201. /// Is this scene object phantom?
  202. /// </summary>
  203. /// <remarks>
  204. /// Updating must currently take place through UpdatePrimFlags()
  205. /// </remarks>
  206. public bool IsPhantom
  207. {
  208. get { return (RootPart.Flags & PrimFlags.Phantom) != 0; }
  209. }
  210. /// <summary>
  211. /// Does this scene object use physics?
  212. /// </summary>
  213. /// <remarks>
  214. /// Updating must currently take place through UpdatePrimFlags()
  215. /// </remarks>
  216. public bool UsesPhysics
  217. {
  218. get { return (RootPart.Flags & PrimFlags.Physics) != 0; }
  219. }
  220. /// <summary>
  221. /// Is this scene object temporary?
  222. /// </summary>
  223. /// <remarks>
  224. /// Updating must currently take place through UpdatePrimFlags()
  225. /// </remarks>
  226. public bool IsTemporary
  227. {
  228. get { return (RootPart.Flags & PrimFlags.TemporaryOnRez) != 0; }
  229. }
  230. public bool IsVolumeDetect
  231. {
  232. get { return RootPart.VolumeDetectActive; }
  233. }
  234. private Vector3 lastPhysGroupPos;
  235. private Quaternion lastPhysGroupRot;
  236. private bool m_isBackedUp;
  237. protected MapAndArray<UUID, SceneObjectPart> m_parts = new MapAndArray<UUID, SceneObjectPart>();
  238. protected ulong m_regionHandle;
  239. protected SceneObjectPart m_rootPart;
  240. // private Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  241. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  242. private Dictionary<uint, scriptRotTarget> m_rotTargets = new Dictionary<uint, scriptRotTarget>();
  243. private bool m_scriptListens_atTarget;
  244. private bool m_scriptListens_notAtTarget;
  245. private bool m_scriptListens_atRotTarget;
  246. private bool m_scriptListens_notAtRotTarget;
  247. internal Dictionary<UUID, string> m_savedScriptState;
  248. #region Properties
  249. /// <summary>
  250. /// The name of an object grouping is always the same as its root part
  251. /// </summary>
  252. public override string Name
  253. {
  254. get { return RootPart.Name; }
  255. set { RootPart.Name = value; }
  256. }
  257. public string Description
  258. {
  259. get { return RootPart.Description; }
  260. set { RootPart.Description = value; }
  261. }
  262. /// <summary>
  263. /// Added because the Parcel code seems to use it
  264. /// but not sure a object should have this
  265. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  266. /// think really there should be a list (or whatever) in each scenepresence
  267. /// saying what prim(s) that user has selected.
  268. /// </summary>
  269. protected bool m_isSelected = false;
  270. /// <summary>
  271. /// Number of prims in this group
  272. /// </summary>
  273. public int PrimCount
  274. {
  275. get { return m_parts.Count; }
  276. }
  277. public Quaternion GroupRotation
  278. {
  279. get { return m_rootPart.RotationOffset; }
  280. }
  281. public Vector3 GroupScale
  282. {
  283. get
  284. {
  285. Vector3 minScale = new Vector3(Constants.RegionSize, Constants.RegionSize, Constants.RegionSize);
  286. Vector3 maxScale = Vector3.Zero;
  287. Vector3 finalScale = new Vector3(0.5f, 0.5f, 0.5f);
  288. SceneObjectPart[] parts = m_parts.GetArray();
  289. for (int i = 0; i < parts.Length; i++)
  290. {
  291. SceneObjectPart part = parts[i];
  292. Vector3 partscale = part.Scale;
  293. Vector3 partoffset = part.OffsetPosition;
  294. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  295. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.Y + partoffset.Y : minScale.Y;
  296. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.Z + partoffset.Z : minScale.Z;
  297. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  298. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  299. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  300. }
  301. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  302. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  303. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  304. return finalScale;
  305. }
  306. }
  307. public UUID GroupID
  308. {
  309. get { return m_rootPart.GroupID; }
  310. set { m_rootPart.GroupID = value; }
  311. }
  312. public SceneObjectPart[] Parts
  313. {
  314. get { return m_parts.GetArray(); }
  315. }
  316. public bool ContainsPart(UUID partID)
  317. {
  318. return m_parts.ContainsKey(partID);
  319. }
  320. /// <summary>
  321. /// Does this group contain the given part?
  322. /// should be able to remove these methods once we have a entity index in scene
  323. /// </summary>
  324. /// <param name="localID"></param>
  325. /// <returns></returns>
  326. public bool ContainsPart(uint localID)
  327. {
  328. SceneObjectPart[] parts = m_parts.GetArray();
  329. for (int i = 0; i < parts.Length; i++)
  330. {
  331. if (parts[i].LocalId == localID)
  332. return true;
  333. }
  334. return false;
  335. }
  336. /// <value>
  337. /// The root part of this scene object
  338. /// </value>
  339. public SceneObjectPart RootPart
  340. {
  341. get { return m_rootPart; }
  342. }
  343. public ulong RegionHandle
  344. {
  345. get { return m_regionHandle; }
  346. set
  347. {
  348. m_regionHandle = value;
  349. SceneObjectPart[] parts = m_parts.GetArray();
  350. for (int i = 0; i < parts.Length; i++)
  351. parts[i].RegionHandle = value;
  352. }
  353. }
  354. /// <summary>
  355. /// Check both the attachment property and the relevant properties of the underlying root part.
  356. /// </summary>
  357. /// <remarks>
  358. /// This is necessary in some cases, particularly when a scene object has just crossed into a region and doesn't
  359. /// have the IsAttachment property yet checked.
  360. ///
  361. /// FIXME: However, this should be fixed so that this property
  362. /// propertly reflects the underlying status.
  363. /// </remarks>
  364. /// <returns></returns>
  365. public bool IsAttachmentCheckFull()
  366. {
  367. return (IsAttachment || (m_rootPart.Shape.PCode == 9 && m_rootPart.Shape.State != 0));
  368. }
  369. /// <summary>
  370. /// The absolute position of this scene object in the scene
  371. /// </summary>
  372. public override Vector3 AbsolutePosition
  373. {
  374. get { return m_rootPart.GroupPosition; }
  375. set
  376. {
  377. Vector3 val = value;
  378. if (Scene != null)
  379. {
  380. if ((Scene.TestBorderCross(val - Vector3.UnitX, Cardinals.E) || Scene.TestBorderCross(val + Vector3.UnitX, Cardinals.W)
  381. || Scene.TestBorderCross(val - Vector3.UnitY, Cardinals.N) || Scene.TestBorderCross(val + Vector3.UnitY, Cardinals.S))
  382. && !IsAttachmentCheckFull() && (!Scene.LoadingPrims))
  383. {
  384. m_scene.CrossPrimGroupIntoNewRegion(val, this, true);
  385. }
  386. }
  387. if (RootPart.GetStatusSandbox())
  388. {
  389. if (Util.GetDistanceTo(RootPart.StatusSandboxPos, value) > 10)
  390. {
  391. RootPart.ScriptSetPhysicsStatus(false);
  392. if (Scene != null)
  393. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  394. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  395. return;
  396. }
  397. }
  398. // Restuff the new GroupPosition into each SOP of the linkset.
  399. // This has the affect of resetting and tainting the physics actors.
  400. SceneObjectPart[] parts = m_parts.GetArray();
  401. for (int i = 0; i < parts.Length; i++)
  402. parts[i].GroupPosition = val;
  403. //if (m_rootPart.PhysActor != null)
  404. //{
  405. //m_rootPart.PhysActor.Position =
  406. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  407. //m_rootPart.GroupPosition.Z);
  408. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  409. //}
  410. if (Scene != null)
  411. Scene.EventManager.TriggerParcelPrimCountTainted();
  412. }
  413. }
  414. public override uint LocalId
  415. {
  416. get { return m_rootPart.LocalId; }
  417. set { m_rootPart.LocalId = value; }
  418. }
  419. public override UUID UUID
  420. {
  421. get { return m_rootPart.UUID; }
  422. set
  423. {
  424. lock (m_parts.SyncRoot)
  425. {
  426. m_parts.Remove(m_rootPart.UUID);
  427. m_rootPart.UUID = value;
  428. m_parts.Add(value, m_rootPart);
  429. }
  430. }
  431. }
  432. public UUID LastOwnerID
  433. {
  434. get { return m_rootPart.LastOwnerID; }
  435. set { m_rootPart.LastOwnerID = value; }
  436. }
  437. public UUID OwnerID
  438. {
  439. get { return m_rootPart.OwnerID; }
  440. set { m_rootPart.OwnerID = value; }
  441. }
  442. public float Damage
  443. {
  444. get { return m_rootPart.Damage; }
  445. set { m_rootPart.Damage = value; }
  446. }
  447. public Color Color
  448. {
  449. get { return m_rootPart.Color; }
  450. set { m_rootPart.Color = value; }
  451. }
  452. public string Text
  453. {
  454. get {
  455. string returnstr = m_rootPart.Text;
  456. if (returnstr.Length > 255)
  457. {
  458. returnstr = returnstr.Substring(0, 255);
  459. }
  460. return returnstr;
  461. }
  462. set { m_rootPart.Text = value; }
  463. }
  464. protected virtual bool InSceneBackup
  465. {
  466. get { return true; }
  467. }
  468. public bool IsSelected
  469. {
  470. get { return m_isSelected; }
  471. set
  472. {
  473. m_isSelected = value;
  474. // Tell physics engine that group is selected
  475. PhysicsActor pa = m_rootPart.PhysActor;
  476. if (pa != null)
  477. {
  478. pa.Selected = value;
  479. // Pass it on to the children.
  480. SceneObjectPart[] parts = m_parts.GetArray();
  481. for (int i = 0; i < parts.Length; i++)
  482. {
  483. SceneObjectPart child = parts[i];
  484. PhysicsActor childPa = child.PhysActor;
  485. if (childPa != null)
  486. childPa.Selected = value;
  487. }
  488. }
  489. }
  490. }
  491. private SceneObjectPart m_PlaySoundMasterPrim = null;
  492. public SceneObjectPart PlaySoundMasterPrim
  493. {
  494. get { return m_PlaySoundMasterPrim; }
  495. set { m_PlaySoundMasterPrim = value; }
  496. }
  497. private List<SceneObjectPart> m_PlaySoundSlavePrims = new List<SceneObjectPart>();
  498. public List<SceneObjectPart> PlaySoundSlavePrims
  499. {
  500. get { return m_PlaySoundSlavePrims; }
  501. set { m_PlaySoundSlavePrims = value; }
  502. }
  503. private SceneObjectPart m_LoopSoundMasterPrim = null;
  504. public SceneObjectPart LoopSoundMasterPrim
  505. {
  506. get { return m_LoopSoundMasterPrim; }
  507. set { m_LoopSoundMasterPrim = value; }
  508. }
  509. private List<SceneObjectPart> m_LoopSoundSlavePrims = new List<SceneObjectPart>();
  510. public List<SceneObjectPart> LoopSoundSlavePrims
  511. {
  512. get { return m_LoopSoundSlavePrims; }
  513. set { m_LoopSoundSlavePrims = value; }
  514. }
  515. /// <summary>
  516. /// The UUID for the region this object is in.
  517. /// </summary>
  518. public UUID RegionUUID
  519. {
  520. get
  521. {
  522. if (m_scene != null)
  523. {
  524. return m_scene.RegionInfo.RegionID;
  525. }
  526. return UUID.Zero;
  527. }
  528. }
  529. /// <summary>
  530. /// The item ID that this object was rezzed from, if applicable.
  531. /// </summary>
  532. /// <remarks>
  533. /// If not applicable will be UUID.Zero
  534. /// </remarks>
  535. public UUID FromItemID { get; set; }
  536. /// <summary>
  537. /// Refers to the SceneObjectPart.UUID property of the object that this object was rezzed from, if applicable.
  538. /// </summary>
  539. /// <remarks>
  540. /// If not applicable will be UUID.Zero
  541. /// </remarks>
  542. public UUID FromPartID { get; set; }
  543. /// <summary>
  544. /// The folder ID that this object was rezzed from, if applicable.
  545. /// </summary>
  546. /// <remarks>
  547. /// If not applicable will be UUID.Zero
  548. /// </remarks>
  549. public UUID FromFolderID { get; set; }
  550. #endregion
  551. // ~SceneObjectGroup()
  552. // {
  553. // //m_log.DebugFormat("[SCENE OBJECT GROUP]: Destructor called for {0}, local id {1}", Name, LocalId);
  554. // Console.WriteLine("Destructor called for {0}, local id {1}", Name, LocalId);
  555. // }
  556. #region Constructors
  557. /// <summary>
  558. /// Constructor
  559. /// </summary>
  560. public SceneObjectGroup()
  561. {
  562. }
  563. /// <summary>
  564. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  565. /// The original SceneObjectPart will be used rather than a copy, preserving
  566. /// its existing localID and UUID.
  567. /// </summary>
  568. /// <param name='part'>Root part for this scene object.</param>
  569. public SceneObjectGroup(SceneObjectPart part) : this()
  570. {
  571. SetRootPart(part);
  572. }
  573. /// <summary>
  574. /// Constructor. This object is added to the scene later via AttachToScene()
  575. /// </summary>
  576. public SceneObjectGroup(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  577. :this(new SceneObjectPart(ownerID, shape, pos, rot, Vector3.Zero))
  578. {
  579. }
  580. /// <summary>
  581. /// Constructor.
  582. /// </summary>
  583. public SceneObjectGroup(UUID ownerID, Vector3 pos, PrimitiveBaseShape shape)
  584. : this(ownerID, pos, Quaternion.Identity, shape)
  585. {
  586. }
  587. public void LoadScriptState(XmlDocument doc)
  588. {
  589. XmlNodeList nodes = doc.GetElementsByTagName("SavedScriptState");
  590. if (nodes.Count > 0)
  591. {
  592. if (m_savedScriptState == null)
  593. m_savedScriptState = new Dictionary<UUID, string>();
  594. foreach (XmlNode node in nodes)
  595. {
  596. if (node.Attributes["UUID"] != null)
  597. {
  598. UUID itemid = new UUID(node.Attributes["UUID"].Value);
  599. if (itemid != UUID.Zero)
  600. m_savedScriptState[itemid] = node.InnerXml;
  601. }
  602. }
  603. }
  604. }
  605. /// <summary>
  606. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  607. /// </summary>
  608. public virtual void AttachToBackup()
  609. {
  610. if (InSceneBackup)
  611. {
  612. //m_log.DebugFormat(
  613. // "[SCENE OBJECT GROUP]: Attaching object {0} {1} to scene presistence sweep", Name, UUID);
  614. if (!m_isBackedUp)
  615. m_scene.EventManager.OnBackup += ProcessBackup;
  616. m_isBackedUp = true;
  617. }
  618. }
  619. /// <summary>
  620. /// Attach this object to a scene. It will also now appear to agents.
  621. /// </summary>
  622. /// <param name="scene"></param>
  623. public void AttachToScene(Scene scene)
  624. {
  625. m_scene = scene;
  626. RegionHandle = m_scene.RegionInfo.RegionHandle;
  627. if (m_rootPart.Shape.PCode != 9 || m_rootPart.Shape.State == 0)
  628. m_rootPart.ParentID = 0;
  629. if (m_rootPart.LocalId == 0)
  630. m_rootPart.LocalId = m_scene.AllocateLocalId();
  631. SceneObjectPart[] parts = m_parts.GetArray();
  632. for (int i = 0; i < parts.Length; i++)
  633. {
  634. SceneObjectPart part = parts[i];
  635. if (Object.ReferenceEquals(part, m_rootPart))
  636. continue;
  637. if (part.LocalId == 0)
  638. part.LocalId = m_scene.AllocateLocalId();
  639. part.ParentID = m_rootPart.LocalId;
  640. //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);
  641. }
  642. ApplyPhysics();
  643. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  644. // for the same object with very different properties. The caller must schedule the update.
  645. //ScheduleGroupForFullUpdate();
  646. }
  647. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  648. {
  649. // We got a request from the inner_scene to raytrace along the Ray hRay
  650. // We're going to check all of the prim in this group for intersection with the ray
  651. // If we get a result, we're going to find the closest result to the origin of the ray
  652. // and send back the intersection information back to the innerscene.
  653. EntityIntersection result = new EntityIntersection();
  654. SceneObjectPart[] parts = m_parts.GetArray();
  655. for (int i = 0; i < parts.Length; i++)
  656. {
  657. SceneObjectPart part = parts[i];
  658. // Temporary commented to stop compiler warning
  659. //Vector3 partPosition =
  660. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  661. Quaternion parentrotation = GroupRotation;
  662. // Telling the prim to raytrace.
  663. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  664. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation, frontFacesOnly, faceCenters);
  665. // This may need to be updated to the maximum draw distance possible..
  666. // We might (and probably will) be checking for prim creation from other sims
  667. // when the camera crosses the border.
  668. float idist = Constants.RegionSize;
  669. if (inter.HitTF)
  670. {
  671. // We need to find the closest prim to return to the testcaller along the ray
  672. if (inter.distance < idist)
  673. {
  674. result.HitTF = true;
  675. result.ipoint = inter.ipoint;
  676. result.obj = part;
  677. result.normal = inter.normal;
  678. result.distance = inter.distance;
  679. }
  680. }
  681. }
  682. return result;
  683. }
  684. /// <summary>
  685. /// Gets a vector representing the size of the bounding box containing all the prims in the group
  686. /// Treats all prims as rectangular, so no shape (cut etc) is taken into account
  687. /// offsetHeight is the offset in the Z axis from the centre of the bounding box to the centre of the root prim
  688. /// </summary>
  689. /// <returns></returns>
  690. public void GetAxisAlignedBoundingBoxRaw(out float minX, out float maxX, out float minY, out float maxY, out float minZ, out float maxZ)
  691. {
  692. maxX = -256f;
  693. maxY = -256f;
  694. maxZ = -256f;
  695. minX = 256f;
  696. minY = 256f;
  697. minZ = 8192f;
  698. SceneObjectPart[] parts = m_parts.GetArray();
  699. for (int i = 0; i < parts.Length; i++)
  700. {
  701. SceneObjectPart part = parts[i];
  702. Vector3 worldPos = part.GetWorldPosition();
  703. Vector3 offset = worldPos - AbsolutePosition;
  704. Quaternion worldRot;
  705. if (part.ParentID == 0)
  706. worldRot = part.RotationOffset;
  707. else
  708. worldRot = part.GetWorldRotation();
  709. Vector3 frontTopLeft;
  710. Vector3 frontTopRight;
  711. Vector3 frontBottomLeft;
  712. Vector3 frontBottomRight;
  713. Vector3 backTopLeft;
  714. Vector3 backTopRight;
  715. Vector3 backBottomLeft;
  716. Vector3 backBottomRight;
  717. Vector3 orig = Vector3.Zero;
  718. frontTopLeft.X = orig.X - (part.Scale.X / 2);
  719. frontTopLeft.Y = orig.Y - (part.Scale.Y / 2);
  720. frontTopLeft.Z = orig.Z + (part.Scale.Z / 2);
  721. frontTopRight.X = orig.X - (part.Scale.X / 2);
  722. frontTopRight.Y = orig.Y + (part.Scale.Y / 2);
  723. frontTopRight.Z = orig.Z + (part.Scale.Z / 2);
  724. frontBottomLeft.X = orig.X - (part.Scale.X / 2);
  725. frontBottomLeft.Y = orig.Y - (part.Scale.Y / 2);
  726. frontBottomLeft.Z = orig.Z - (part.Scale.Z / 2);
  727. frontBottomRight.X = orig.X - (part.Scale.X / 2);
  728. frontBottomRight.Y = orig.Y + (part.Scale.Y / 2);
  729. frontBottomRight.Z = orig.Z - (part.Scale.Z / 2);
  730. backTopLeft.X = orig.X + (part.Scale.X / 2);
  731. backTopLeft.Y = orig.Y - (part.Scale.Y / 2);
  732. backTopLeft.Z = orig.Z + (part.Scale.Z / 2);
  733. backTopRight.X = orig.X + (part.Scale.X / 2);
  734. backTopRight.Y = orig.Y + (part.Scale.Y / 2);
  735. backTopRight.Z = orig.Z + (part.Scale.Z / 2);
  736. backBottomLeft.X = orig.X + (part.Scale.X / 2);
  737. backBottomLeft.Y = orig.Y - (part.Scale.Y / 2);
  738. backBottomLeft.Z = orig.Z - (part.Scale.Z / 2);
  739. backBottomRight.X = orig.X + (part.Scale.X / 2);
  740. backBottomRight.Y = orig.Y + (part.Scale.Y / 2);
  741. backBottomRight.Z = orig.Z - (part.Scale.Z / 2);
  742. frontTopLeft = frontTopLeft * worldRot;
  743. frontTopRight = frontTopRight * worldRot;
  744. frontBottomLeft = frontBottomLeft * worldRot;
  745. frontBottomRight = frontBottomRight * worldRot;
  746. backBottomLeft = backBottomLeft * worldRot;
  747. backBottomRight = backBottomRight * worldRot;
  748. backTopLeft = backTopLeft * worldRot;
  749. backTopRight = backTopRight * worldRot;
  750. frontTopLeft += offset;
  751. frontTopRight += offset;
  752. frontBottomLeft += offset;
  753. frontBottomRight += offset;
  754. backBottomLeft += offset;
  755. backBottomRight += offset;
  756. backTopLeft += offset;
  757. backTopRight += offset;
  758. if (frontTopRight.X > maxX)
  759. maxX = frontTopRight.X;
  760. if (frontTopLeft.X > maxX)
  761. maxX = frontTopLeft.X;
  762. if (frontBottomRight.X > maxX)
  763. maxX = frontBottomRight.X;
  764. if (frontBottomLeft.X > maxX)
  765. maxX = frontBottomLeft.X;
  766. if (backTopRight.X > maxX)
  767. maxX = backTopRight.X;
  768. if (backTopLeft.X > maxX)
  769. maxX = backTopLeft.X;
  770. if (backBottomRight.X > maxX)
  771. maxX = backBottomRight.X;
  772. if (backBottomLeft.X > maxX)
  773. maxX = backBottomLeft.X;
  774. if (frontTopRight.X < minX)
  775. minX = frontTopRight.X;
  776. if (frontTopLeft.X < minX)
  777. minX = frontTopLeft.X;
  778. if (frontBottomRight.X < minX)
  779. minX = frontBottomRight.X;
  780. if (frontBottomLeft.X < minX)
  781. minX = frontBottomLeft.X;
  782. if (backTopRight.X < minX)
  783. minX = backTopRight.X;
  784. if (backTopLeft.X < minX)
  785. minX = backTopLeft.X;
  786. if (backBottomRight.X < minX)
  787. minX = backBottomRight.X;
  788. if (backBottomLeft.X < minX)
  789. minX = backBottomLeft.X;
  790. //
  791. if (frontTopRight.Y > maxY)
  792. maxY = frontTopRight.Y;
  793. if (frontTopLeft.Y > maxY)
  794. maxY = frontTopLeft.Y;
  795. if (frontBottomRight.Y > maxY)
  796. maxY = frontBottomRight.Y;
  797. if (frontBottomLeft.Y > maxY)
  798. maxY = frontBottomLeft.Y;
  799. if (backTopRight.Y > maxY)
  800. maxY = backTopRight.Y;
  801. if (backTopLeft.Y > maxY)
  802. maxY = backTopLeft.Y;
  803. if (backBottomRight.Y > maxY)
  804. maxY = backBottomRight.Y;
  805. if (backBottomLeft.Y > maxY)
  806. maxY = backBottomLeft.Y;
  807. if (frontTopRight.Y < minY)
  808. minY = frontTopRight.Y;
  809. if (frontTopLeft.Y < minY)
  810. minY = frontTopLeft.Y;
  811. if (frontBottomRight.Y < minY)
  812. minY = frontBottomRight.Y;
  813. if (frontBottomLeft.Y < minY)
  814. minY = frontBottomLeft.Y;
  815. if (backTopRight.Y < minY)
  816. minY = backTopRight.Y;
  817. if (backTopLeft.Y < minY)
  818. minY = backTopLeft.Y;
  819. if (backBottomRight.Y < minY)
  820. minY = backBottomRight.Y;
  821. if (backBottomLeft.Y < minY)
  822. minY = backBottomLeft.Y;
  823. //
  824. if (frontTopRight.Z > maxZ)
  825. maxZ = frontTopRight.Z;
  826. if (frontTopLeft.Z > maxZ)
  827. maxZ = frontTopLeft.Z;
  828. if (frontBottomRight.Z > maxZ)
  829. maxZ = frontBottomRight.Z;
  830. if (frontBottomLeft.Z > maxZ)
  831. maxZ = frontBottomLeft.Z;
  832. if (backTopRight.Z > maxZ)
  833. maxZ = backTopRight.Z;
  834. if (backTopLeft.Z > maxZ)
  835. maxZ = backTopLeft.Z;
  836. if (backBottomRight.Z > maxZ)
  837. maxZ = backBottomRight.Z;
  838. if (backBottomLeft.Z > maxZ)
  839. maxZ = backBottomLeft.Z;
  840. if (frontTopRight.Z < minZ)
  841. minZ = frontTopRight.Z;
  842. if (frontTopLeft.Z < minZ)
  843. minZ = frontTopLeft.Z;
  844. if (frontBottomRight.Z < minZ)
  845. minZ = frontBottomRight.Z;
  846. if (frontBottomLeft.Z < minZ)
  847. minZ = frontBottomLeft.Z;
  848. if (backTopRight.Z < minZ)
  849. minZ = backTopRight.Z;
  850. if (backTopLeft.Z < minZ)
  851. minZ = backTopLeft.Z;
  852. if (backBottomRight.Z < minZ)
  853. minZ = backBottomRight.Z;
  854. if (backBottomLeft.Z < minZ)
  855. minZ = backBottomLeft.Z;
  856. }
  857. }
  858. public Vector3 GetAxisAlignedBoundingBox(out float offsetHeight)
  859. {
  860. float minX;
  861. float maxX;
  862. float minY;
  863. float maxY;
  864. float minZ;
  865. float maxZ;
  866. GetAxisAlignedBoundingBoxRaw(out minX, out maxX, out minY, out maxY, out minZ, out maxZ);
  867. Vector3 boundingBox = new Vector3(maxX - minX, maxY - minY, maxZ - minZ);
  868. offsetHeight = 0;
  869. float lower = (minZ * -1);
  870. if (lower > maxZ)
  871. {
  872. offsetHeight = lower - (boundingBox.Z / 2);
  873. }
  874. else if (maxZ > lower)
  875. {
  876. offsetHeight = maxZ - (boundingBox.Z / 2);
  877. offsetHeight *= -1;
  878. }
  879. // m_log.InfoFormat("BoundingBox is {0} , {1} , {2} ", boundingBox.X, boundingBox.Y, boundingBox.Z);
  880. return boundingBox;
  881. }
  882. #endregion
  883. public void SaveScriptedState(XmlTextWriter writer)
  884. {
  885. XmlDocument doc = new XmlDocument();
  886. Dictionary<UUID,string> states = new Dictionary<UUID,string>();
  887. SceneObjectPart[] parts = m_parts.GetArray();
  888. for (int i = 0; i < parts.Length; i++)
  889. {
  890. Dictionary<UUID, string> pstates = parts[i].Inventory.GetScriptStates();
  891. foreach (KeyValuePair<UUID, string> kvp in pstates)
  892. states.Add(kvp.Key, kvp.Value);
  893. }
  894. if (states.Count > 0)
  895. {
  896. // Now generate the necessary XML wrappings
  897. writer.WriteStartElement(String.Empty, "GroupScriptStates", String.Empty);
  898. foreach (UUID itemid in states.Keys)
  899. {
  900. doc.LoadXml(states[itemid]);
  901. writer.WriteStartElement(String.Empty, "SavedScriptState", String.Empty);
  902. writer.WriteAttributeString(String.Empty, "UUID", String.Empty, itemid.ToString());
  903. writer.WriteRaw(doc.DocumentElement.OuterXml); // Writes ScriptState element
  904. writer.WriteEndElement(); // End of SavedScriptState
  905. }
  906. writer.WriteEndElement(); // End of GroupScriptStates
  907. }
  908. }
  909. /// <summary>
  910. ///
  911. /// </summary>
  912. /// <param name="part"></param>
  913. private void SetPartAsNonRoot(SceneObjectPart part)
  914. {
  915. part.ParentID = m_rootPart.LocalId;
  916. part.ClearUndoState();
  917. }
  918. public ushort GetTimeDilation()
  919. {
  920. return Utils.FloatToUInt16(m_scene.TimeDilation, 0.0f, 1.0f);
  921. }
  922. /// <summary>
  923. /// Set a part to act as the root part for this scene object
  924. /// </summary>
  925. /// <param name="part"></param>
  926. public void SetRootPart(SceneObjectPart part)
  927. {
  928. if (part == null)
  929. throw new ArgumentNullException("Cannot give SceneObjectGroup a null root SceneObjectPart");
  930. part.SetParent(this);
  931. m_rootPart = part;
  932. if (!IsAttachment)
  933. part.ParentID = 0;
  934. part.LinkNum = 0;
  935. m_parts.Add(m_rootPart.UUID, m_rootPart);
  936. }
  937. /// <summary>
  938. /// Add a new part to this scene object. The part must already be correctly configured.
  939. /// </summary>
  940. /// <param name="part"></param>
  941. public void AddPart(SceneObjectPart part)
  942. {
  943. part.SetParent(this);
  944. part.LinkNum = m_parts.Add(part.UUID, part);
  945. if (part.LinkNum == 2)
  946. RootPart.LinkNum = 1;
  947. }
  948. /// <summary>
  949. /// Make sure that every non root part has the proper parent root part local id
  950. /// </summary>
  951. private void UpdateParentIDs()
  952. {
  953. SceneObjectPart[] parts = m_parts.GetArray();
  954. for (int i = 0; i < parts.Length; i++)
  955. {
  956. SceneObjectPart part = parts[i];
  957. if (part.UUID != m_rootPart.UUID)
  958. part.ParentID = m_rootPart.LocalId;
  959. }
  960. }
  961. public void RegenerateFullIDs()
  962. {
  963. SceneObjectPart[] parts = m_parts.GetArray();
  964. for (int i = 0; i < parts.Length; i++)
  965. parts[i].UUID = UUID.Random();
  966. }
  967. // justincc: I don't believe this hack is needed any longer, especially since the physics
  968. // parts of set AbsolutePosition were already commented out. By changing HasGroupChanged to false
  969. // this method was preventing proper reload of scene objects.
  970. // dahlia: I had to uncomment it, without it meshing was failing on some prims and objects
  971. // at region startup
  972. // teravus: After this was removed from the linking algorithm, Linked prims no longer collided
  973. // properly when non-physical if they havn't been moved. This breaks ALL builds.
  974. // see: http://opensimulator.org/mantis/view.php?id=3108
  975. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  976. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  977. // unmoved prims! As soon as you move a Prim/group, it will collide properly because Absolute
  978. // Position has been set!
  979. public void ResetChildPrimPhysicsPositions()
  980. {
  981. // Setting this SOG's absolute position also loops through and sets the positions
  982. // of the SOP's in this SOG's linkset. This has the side affect of making sure
  983. // the physics world matches the simulated world.
  984. AbsolutePosition = AbsolutePosition; // could someone in the know please explain how this works?
  985. // teravus: AbsolutePosition is NOT a normal property!
  986. // the code in the getter of AbsolutePosition is significantly different then the code in the setter!
  987. // jhurliman: Then why is it a property instead of two methods?
  988. }
  989. public UUID GetPartsFullID(uint localID)
  990. {
  991. SceneObjectPart part = GetPart(localID);
  992. if (part != null)
  993. {
  994. return part.UUID;
  995. }
  996. return UUID.Zero;
  997. }
  998. public void ObjectGrabHandler(uint localId, Vector3 offsetPos, IClientAPI remoteClient)
  999. {
  1000. if (m_rootPart.LocalId == localId)
  1001. {
  1002. OnGrabGroup(offsetPos, remoteClient);
  1003. }
  1004. else
  1005. {
  1006. SceneObjectPart part = GetPart(localId);
  1007. OnGrabPart(part, offsetPos, remoteClient);
  1008. }
  1009. }
  1010. public virtual void OnGrabPart(SceneObjectPart part, Vector3 offsetPos, IClientAPI remoteClient)
  1011. {
  1012. // m_log.DebugFormat(
  1013. // "[SCENE OBJECT GROUP]: Processing OnGrabPart for {0} on {1} {2}, offsetPos {3}",
  1014. // remoteClient.Name, part.Name, part.LocalId, offsetPos);
  1015. part.StoreUndoState();
  1016. part.OnGrab(offsetPos, remoteClient);
  1017. }
  1018. public virtual void OnGrabGroup(Vector3 offsetPos, IClientAPI remoteClient)
  1019. {
  1020. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  1021. }
  1022. /// <summary>
  1023. /// Delete this group from its scene.
  1024. /// </summary>
  1025. ///
  1026. /// This only handles the in-world consequences of deletion (e.g. any avatars sitting on it are forcibly stood
  1027. /// up and all avatars receive notification of its removal. Removal of the scene object from database backup
  1028. /// must be handled by the caller.
  1029. ///
  1030. /// <param name="silent">If true then deletion is not broadcast to clients</param>
  1031. public void DeleteGroupFromScene(bool silent)
  1032. {
  1033. SceneObjectPart[] parts = m_parts.GetArray();
  1034. for (int i = 0; i < parts.Length; i++)
  1035. {
  1036. SceneObjectPart part = parts[i];
  1037. Scene.ForEachRootScenePresence(delegate(ScenePresence avatar)
  1038. {
  1039. if (avatar.ParentID == LocalId)
  1040. avatar.StandUp();
  1041. if (!silent)
  1042. {
  1043. part.ClearUpdateSchedule();
  1044. if (part == m_rootPart)
  1045. {
  1046. if (!IsAttachment
  1047. || AttachedAvatar == avatar.ControllingClient.AgentId
  1048. || !HasPrivateAttachmentPoint)
  1049. avatar.ControllingClient.SendKillObject(m_regionHandle, new List<uint> { part.LocalId });
  1050. }
  1051. }
  1052. });
  1053. }
  1054. }
  1055. public void AddScriptLPS(int count)
  1056. {
  1057. m_scene.SceneGraph.AddToScriptLPS(count);
  1058. }
  1059. public void AddActiveScriptCount(int count)
  1060. {
  1061. SceneGraph d = m_scene.SceneGraph;
  1062. d.AddActiveScripts(count);
  1063. }
  1064. public void aggregateScriptEvents()
  1065. {
  1066. PrimFlags objectflagupdate = (PrimFlags)RootPart.GetEffectiveObjectFlags();
  1067. scriptEvents aggregateScriptEvents = 0;
  1068. SceneObjectPart[] parts = m_parts.GetArray();
  1069. for (int i = 0; i < parts.Length; i++)
  1070. {
  1071. SceneObjectPart part = parts[i];
  1072. if (part == null)
  1073. continue;
  1074. if (part != RootPart)
  1075. part.Flags = objectflagupdate;
  1076. aggregateScriptEvents |= part.AggregateScriptEvents;
  1077. }
  1078. m_scriptListens_atTarget = ((aggregateScriptEvents & scriptEvents.at_target) != 0);
  1079. m_scriptListens_notAtTarget = ((aggregateScriptEvents & scriptEvents.not_at_target) != 0);
  1080. if (!m_scriptListens_atTarget && !m_scriptListens_notAtTarget)
  1081. {
  1082. lock (m_targets)
  1083. m_targets.Clear();
  1084. m_scene.RemoveGroupTarget(this);
  1085. }
  1086. m_scriptListens_atRotTarget = ((aggregateScriptEvents & scriptEvents.at_rot_target) != 0);
  1087. m_scriptListens_notAtRotTarget = ((aggregateScriptEvents & scriptEvents.not_at_rot_target) != 0);
  1088. if (!m_scriptListens_atRotTarget && !m_scriptListens_notAtRotTarget)
  1089. {
  1090. lock (m_rotTargets)
  1091. m_rotTargets.Clear();
  1092. m_scene.RemoveGroupTarget(this);
  1093. }
  1094. ScheduleGroupForFullUpdate();
  1095. }
  1096. public void SetText(string text, Vector3 color, double alpha)
  1097. {
  1098. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  1099. (int) (color.X * 0xff),
  1100. (int) (color.Y * 0xff),
  1101. (int) (color.Z * 0xff));
  1102. Text = text;
  1103. HasGroupChanged = true;
  1104. m_rootPart.ScheduleFullUpdate();
  1105. }
  1106. /// <summary>
  1107. /// Apply physics to this group
  1108. /// </summary>
  1109. public void ApplyPhysics()
  1110. {
  1111. // Apply physics to the root prim
  1112. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive);
  1113. // Apply physics to child prims
  1114. SceneObjectPart[] parts = m_parts.GetArray();
  1115. if (parts.Length > 1)
  1116. {
  1117. for (int i = 0; i < parts.Length; i++)
  1118. {
  1119. SceneObjectPart part = parts[i];
  1120. if (part.LocalId != m_rootPart.LocalId)
  1121. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), part.VolumeDetectActive);
  1122. }
  1123. // Hack to get the physics scene geometries in the right spot
  1124. ResetChildPrimPhysicsPositions();
  1125. }
  1126. }
  1127. public void SetOwnerId(UUID userId)
  1128. {
  1129. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  1130. }
  1131. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  1132. {
  1133. SceneObjectPart[] parts = m_parts.GetArray();
  1134. for (int i = 0; i < parts.Length; i++)
  1135. whatToDo(parts[i]);
  1136. }
  1137. #region Events
  1138. /// <summary>
  1139. /// Processes backup.
  1140. /// </summary>
  1141. /// <param name="datastore"></param>
  1142. public virtual void ProcessBackup(ISimulationDataService datastore, bool forcedBackup)
  1143. {
  1144. if (!m_isBackedUp)
  1145. {
  1146. // m_log.DebugFormat(
  1147. // "[WATER WARS]: Ignoring backup of {0} {1} since object is not marked to be backed up", Name, UUID);
  1148. return;
  1149. }
  1150. if (IsDeleted || UUID == UUID.Zero)
  1151. {
  1152. // m_log.DebugFormat(
  1153. // "[WATER WARS]: Ignoring backup of {0} {1} since object is marked as already deleted", Name, UUID);
  1154. return;
  1155. }
  1156. // Since this is the top of the section of call stack for backing up a particular scene object, don't let
  1157. // any exception propogate upwards.
  1158. try
  1159. {
  1160. if (!m_scene.ShuttingDown) // if shutting down then there will be nothing to handle the return so leave till next restart
  1161. {
  1162. ILandObject parcel = m_scene.LandChannel.GetLandObject(
  1163. m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y);
  1164. if (parcel != null && parcel.LandData != null &&
  1165. parcel.LandData.OtherCleanTime != 0)
  1166. {
  1167. if (parcel.LandData.OwnerID != OwnerID &&
  1168. (parcel.LandData.GroupID != GroupID ||
  1169. parcel.LandData.GroupID == UUID.Zero))
  1170. {
  1171. if ((DateTime.UtcNow - RootPart.Rezzed).TotalMinutes >
  1172. parcel.LandData.OtherCleanTime)
  1173. {
  1174. DetachFromBackup();
  1175. m_log.DebugFormat(
  1176. "[SCENE OBJECT GROUP]: Returning object {0} due to parcel autoreturn",
  1177. RootPart.UUID);
  1178. m_scene.AddReturn(OwnerID == GroupID ? LastOwnerID : OwnerID, Name, AbsolutePosition, "parcel autoreturn");
  1179. m_scene.DeRezObjects(null, new List<uint>() { RootPart.LocalId }, UUID.Zero,
  1180. DeRezAction.Return, UUID.Zero);
  1181. return;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. if (m_scene.UseBackup && HasGroupChanged)
  1187. {
  1188. // don't backup while it's selected or you're asking for changes mid stream.
  1189. if (isTimeToPersist() || forcedBackup)
  1190. {
  1191. // m_log.DebugFormat(
  1192. // "[SCENE]: Storing {0}, {1} in {2}",
  1193. // Name, UUID, m_scene.RegionInfo.RegionName);
  1194. SceneObjectGroup backup_group = Copy(false);
  1195. backup_group.RootPart.Velocity = RootPart.Velocity;
  1196. backup_group.RootPart.Acceleration = RootPart.Acceleration;
  1197. backup_group.RootPart.AngularVelocity = RootPart.AngularVelocity;
  1198. backup_group.RootPart.ParticleSystem = RootPart.ParticleSystem;
  1199. HasGroupChanged = false;
  1200. HasGroupChangedDueToDelink = false;
  1201. m_scene.EventManager.TriggerOnSceneObjectPreSave(backup_group, this);
  1202. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  1203. backup_group.ForEachPart(delegate(SceneObjectPart part)
  1204. {
  1205. part.Inventory.ProcessInventoryBackup(datastore);
  1206. });
  1207. backup_group = null;
  1208. }
  1209. // else
  1210. // {
  1211. // m_log.DebugFormat(
  1212. // "[SCENE]: Did not update persistence of object {0} {1}, selected = {2}",
  1213. // Name, UUID, IsSelected);
  1214. // }
  1215. }
  1216. }
  1217. catch (Exception e)
  1218. {
  1219. m_log.ErrorFormat(
  1220. "[SCENE]: Storing of {0}, {1} in {2} failed with exception {3}{4}",
  1221. Name, UUID, m_scene.RegionInfo.RegionName, e.Message, e.StackTrace);
  1222. }
  1223. }
  1224. #endregion
  1225. /// <summary>
  1226. /// Send the parts of this SOG to a single client
  1227. /// </summary>
  1228. /// <remarks>
  1229. /// Used when the client initially connects and when client sends RequestPrim packet
  1230. /// </remarks>
  1231. /// <param name="remoteClient"></param>
  1232. public void SendFullUpdateToClient(IClientAPI remoteClient)
  1233. {
  1234. RootPart.SendFullUpdate(remoteClient);
  1235. SceneObjectPart[] parts = m_parts.GetArray();
  1236. for (int i = 0; i < parts.Length; i++)
  1237. {
  1238. SceneObjectPart part = parts[i];
  1239. if (part != RootPart)
  1240. part.SendFullUpdate(remoteClient);
  1241. }
  1242. }
  1243. #region Copying
  1244. /// <summary>
  1245. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1246. /// </summary>
  1247. /// <param name="userExposed">True if the duplicate will immediately be in the scene, false otherwise</param>
  1248. /// <returns></returns>
  1249. public SceneObjectGroup Copy(bool userExposed)
  1250. {
  1251. SceneObjectGroup dupe = (SceneObjectGroup)MemberwiseClone();
  1252. dupe.m_isBackedUp = false;
  1253. dupe.m_parts = new MapAndArray<OpenMetaverse.UUID, SceneObjectPart>();
  1254. // Warning, The following code related to previousAttachmentStatus is needed so that clones of
  1255. // attachments do not bordercross while they're being duplicated. This is hacktastic!
  1256. // Normally, setting AbsolutePosition will bordercross a prim if it's outside the region!
  1257. // unless IsAttachment is true!, so to prevent border crossing, we save it's attachment state
  1258. // (which should be false anyway) set it as an Attachment and then set it's Absolute Position,
  1259. // then restore it's attachment state
  1260. // This is only necessary when userExposed is false!
  1261. bool previousAttachmentStatus = dupe.IsAttachment;
  1262. if (!userExposed)
  1263. dupe.IsAttachment = true;
  1264. dupe.AbsolutePosition = new Vector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  1265. if (!userExposed)
  1266. {
  1267. dupe.IsAttachment = previousAttachmentStatus;
  1268. }
  1269. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  1270. dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
  1271. if (userExposed)
  1272. dupe.m_rootPart.TrimPermissions();
  1273. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.GetArray());
  1274. partList.Sort(delegate(SceneObjectPart p1, SceneObjectPart p2)
  1275. {
  1276. return p1.LinkNum.CompareTo(p2.LinkNum);
  1277. }
  1278. );
  1279. foreach (SceneObjectPart part in partList)
  1280. {
  1281. SceneObjectPart newPart;
  1282. if (part.UUID != m_rootPart.UUID)
  1283. {
  1284. newPart = dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  1285. newPart.LinkNum = part.LinkNum;
  1286. }
  1287. else
  1288. {
  1289. newPart = dupe.m_rootPart;
  1290. }
  1291. // Need to duplicate the physics actor as well
  1292. PhysicsActor originalPartPa = part.PhysActor;
  1293. if (originalPartPa != null && userExposed)
  1294. {
  1295. PrimitiveBaseShape pbs = newPart.Shape;
  1296. newPart.PhysActor
  1297. = m_scene.PhysicsScene.AddPrimShape(
  1298. string.Format("{0}/{1}", newPart.Name, newPart.UUID),
  1299. pbs,
  1300. newPart.AbsolutePosition,
  1301. newPart.Scale,
  1302. newPart.RotationOffset,
  1303. originalPartPa.IsPhysical,
  1304. newPart.LocalId);
  1305. newPart.DoPhysicsPropertyUpdate(originalPartPa.IsPhysical, true);
  1306. }
  1307. }
  1308. if (userExposed)
  1309. {
  1310. dupe.UpdateParentIDs();
  1311. dupe.HasGroupChanged = true;
  1312. dupe.AttachToBackup();
  1313. ScheduleGroupForFullUpdate();
  1314. }
  1315. return dupe;
  1316. }
  1317. /// <summary>
  1318. /// Copy the given part as the root part of this scene object.
  1319. /// </summary>
  1320. /// <param name="part"></param>
  1321. /// <param name="cAgentID"></param>
  1322. /// <param name="cGroupID"></param>
  1323. public void CopyRootPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1324. {
  1325. SetRootPart(part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, 0, userExposed));
  1326. }
  1327. public void ScriptSetPhysicsStatus(bool usePhysics)
  1328. {
  1329. UpdatePrimFlags(RootPart.LocalId, usePhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  1330. }
  1331. public void ScriptSetTemporaryStatus(bool makeTemporary)
  1332. {
  1333. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, makeTemporary, IsPhantom, IsVolumeDetect);
  1334. }
  1335. public void ScriptSetPhantomStatus(bool makePhantom)
  1336. {
  1337. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, IsTemporary, makePhantom, IsVolumeDetect);
  1338. }
  1339. public void ScriptSetVolumeDetect(bool makeVolumeDetect)
  1340. {
  1341. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, IsTemporary, IsPhantom, makeVolumeDetect);
  1342. /*
  1343. ScriptSetPhantomStatus(false); // What ever it was before, now it's not phantom anymore
  1344. if (PhysActor != null) // Should always be the case now
  1345. {
  1346. PhysActor.SetVolumeDetect(param);
  1347. }
  1348. if (param != 0)
  1349. AddFlag(PrimFlags.Phantom);
  1350. ScheduleFullUpdate();
  1351. */
  1352. }
  1353. public void applyImpulse(Vector3 impulse)
  1354. {
  1355. if (IsAttachment)
  1356. {
  1357. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1358. if (avatar != null)
  1359. {
  1360. avatar.PushForce(impulse);
  1361. }
  1362. }
  1363. else
  1364. {
  1365. PhysicsActor pa = RootPart.PhysActor;
  1366. if (pa != null)
  1367. {
  1368. pa.AddForce(impulse, true);
  1369. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1370. }
  1371. }
  1372. }
  1373. public void applyAngularImpulse(Vector3 impulse)
  1374. {
  1375. PhysicsActor pa = RootPart.PhysActor;
  1376. if (pa != null)
  1377. {
  1378. if (!IsAttachment)
  1379. {
  1380. pa.AddAngularForce(impulse, true);
  1381. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1382. }
  1383. }
  1384. }
  1385. public void setAngularImpulse(Vector3 impulse)
  1386. {
  1387. PhysicsActor pa = RootPart.PhysActor;
  1388. if (pa != null)
  1389. {
  1390. if (!IsAttachment)
  1391. {
  1392. pa.Torque = impulse;
  1393. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1394. }
  1395. }
  1396. }
  1397. public Vector3 GetTorque()
  1398. {
  1399. PhysicsActor pa = RootPart.PhysActor;
  1400. if (pa != null)
  1401. {
  1402. if (!IsAttachment)
  1403. {
  1404. Vector3 torque = pa.Torque;
  1405. return torque;
  1406. }
  1407. }
  1408. return Vector3.Zero;
  1409. }
  1410. public void moveToTarget(Vector3 target, float tau)
  1411. {
  1412. if (IsAttachment)
  1413. {
  1414. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1415. if (avatar != null)
  1416. {
  1417. avatar.MoveToTarget(target, false, false);
  1418. }
  1419. }
  1420. else
  1421. {
  1422. PhysicsActor pa = RootPart.PhysActor;
  1423. if (pa != null)
  1424. {
  1425. pa.PIDTarget = target;
  1426. pa.PIDTau = tau;
  1427. pa.PIDActive = true;
  1428. }
  1429. }
  1430. }
  1431. public void stopMoveToTarget()
  1432. {
  1433. PhysicsActor pa = RootPart.PhysActor;
  1434. if (pa != null)
  1435. pa.PIDActive = false;
  1436. }
  1437. /// <summary>
  1438. /// Uses a PID to attempt to clamp the object on the Z axis at the given height over tau seconds.
  1439. /// </summary>
  1440. /// <param name="height">Height to hover. Height of zero disables hover.</param>
  1441. /// <param name="hoverType">Determines what the height is relative to </param>
  1442. /// <param name="tau">Number of seconds over which to reach target</param>
  1443. public void SetHoverHeight(float height, PIDHoverType hoverType, float tau)
  1444. {
  1445. PhysicsActor pa = RootPart.PhysActor;
  1446. if (pa != null)
  1447. {
  1448. if (height != 0f)
  1449. {
  1450. pa.PIDHoverHeight = height;
  1451. pa.PIDHoverType = hoverType;
  1452. pa.PIDTau = tau;
  1453. pa.PIDHoverActive = true;
  1454. }
  1455. else
  1456. {
  1457. pa.PIDHoverActive = false;
  1458. }
  1459. }
  1460. }
  1461. /// <summary>
  1462. /// Set the owner of the root part.
  1463. /// </summary>
  1464. /// <param name="part"></param>
  1465. /// <param name="cAgentID"></param>
  1466. /// <param name="cGroupID"></param>
  1467. public void SetRootPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1468. {
  1469. part.LastOwnerID = part.OwnerID;
  1470. part.OwnerID = cAgentID;
  1471. part.GroupID = cGroupID;
  1472. if (part.OwnerID != cAgentID)
  1473. {
  1474. // Apply Next Owner Permissions if we're not bypassing permissions
  1475. if (!m_scene.Permissions.BypassPermissions())
  1476. ApplyNextOwnerPermissions();
  1477. }
  1478. part.ScheduleFullUpdate();
  1479. }
  1480. /// <summary>
  1481. /// Make a copy of the given part.
  1482. /// </summary>
  1483. /// <param name="part"></param>
  1484. /// <param name="cAgentID"></param>
  1485. /// <param name="cGroupID"></param>
  1486. public SceneObjectPart CopyPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1487. {
  1488. SceneObjectPart newPart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed);
  1489. AddPart(newPart);
  1490. SetPartAsNonRoot(newPart);
  1491. return newPart;
  1492. }
  1493. /// <summary>
  1494. /// Reset the UUIDs for all the prims that make up this group.
  1495. /// </summary>
  1496. /// <remarks>
  1497. /// This is called by methods which want to add a new group to an existing scene, in order
  1498. /// to ensure that there are no clashes with groups already present.
  1499. /// </remarks>
  1500. public void ResetIDs()
  1501. {
  1502. lock (m_parts.SyncRoot)
  1503. {
  1504. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.GetArray());
  1505. m_parts.Clear();
  1506. foreach (SceneObjectPart part in partsList)
  1507. {
  1508. part.ResetIDs(part.LinkNum); // Don't change link nums
  1509. m_parts.Add(part.UUID, part);
  1510. }
  1511. }
  1512. }
  1513. /// <summary>
  1514. ///
  1515. /// </summary>
  1516. /// <param name="part"></param>
  1517. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, UUID AgentID, uint RequestFlags)
  1518. {
  1519. remoteClient.SendObjectPropertiesFamilyData(RootPart, RequestFlags);
  1520. // remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.OwnerID, RootPart.GroupID, RootPart.BaseMask,
  1521. // RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  1522. // RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  1523. // RootPart.CreatorID, RootPart.Name, RootPart.Description);
  1524. }
  1525. public void SetPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1526. {
  1527. part.OwnerID = cAgentID;
  1528. part.GroupID = cGroupID;
  1529. }
  1530. #endregion
  1531. public override void Update()
  1532. {
  1533. // Check that the group was not deleted before the scheduled update
  1534. // FIXME: This is merely a temporary measure to reduce the incidence of failure when
  1535. // an object has been deleted from a scene before update was processed.
  1536. // A more fundamental overhaul of the update mechanism is required to eliminate all
  1537. // the race conditions.
  1538. if (IsDeleted)
  1539. return;
  1540. // Even temporary objects take part in physics (e.g. temp-on-rez bullets)
  1541. //if ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)
  1542. // return;
  1543. // If we somehow got here to updating the SOG and its root part is not scheduled for update,
  1544. // check to see if the physical position or rotation warrant an update.
  1545. if (m_rootPart.UpdateFlag == UpdateRequired.NONE)
  1546. {
  1547. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1548. if (UsePhysics && !AbsolutePosition.ApproxEquals(lastPhysGroupPos, 0.02f))
  1549. {
  1550. m_rootPart.UpdateFlag = UpdateRequired.TERSE;
  1551. lastPhysGroupPos = AbsolutePosition;
  1552. }
  1553. if (UsePhysics && !GroupRotation.ApproxEquals(lastPhysGroupRot, 0.1f))
  1554. {
  1555. m_rootPart.UpdateFlag = UpdateRequired.TERSE;
  1556. lastPhysGroupRot = GroupRotation;
  1557. }
  1558. }
  1559. SceneObjectPart[] parts = m_parts.GetArray();
  1560. for (int i = 0; i < parts.Length; i++)
  1561. {
  1562. SceneObjectPart part = parts[i];
  1563. if (!IsSelected)
  1564. part.UpdateLookAt();
  1565. part.SendScheduledUpdates();
  1566. }
  1567. }
  1568. /// <summary>
  1569. /// Schedule a full update for this scene object to all interested viewers.
  1570. /// </summary>
  1571. /// <remarks>
  1572. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  1573. /// so that either all changes are sent at once. However, currently, a large amount of internal
  1574. /// code will set this anyway when some object properties are changed.
  1575. /// </remarks>
  1576. public void ScheduleGroupForFullUpdate()
  1577. {
  1578. // if (IsAttachment)
  1579. // m_log.DebugFormat("[SOG]: Scheduling full update for {0} {1}", Name, LocalId);
  1580. checkAtTargets();
  1581. RootPart.ScheduleFullUpdate();
  1582. SceneObjectPart[] parts = m_parts.GetArray();
  1583. for (int i = 0; i < parts.Length; i++)
  1584. {
  1585. SceneObjectPart part = parts[i];
  1586. if (part != RootPart)
  1587. part.ScheduleFullUpdate();
  1588. }
  1589. }
  1590. /// <summary>
  1591. /// Schedule a terse update for this scene object to all interested viewers.
  1592. /// </summary>
  1593. /// <remarks>
  1594. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  1595. /// so that either all changes are sent at once. However, currently, a large amount of internal
  1596. /// code will set this anyway when some object properties are changed.
  1597. /// </remarks>
  1598. public void ScheduleGroupForTerseUpdate()
  1599. {
  1600. // m_log.DebugFormat("[SOG]: Scheduling terse update for {0} {1}", Name, UUID);
  1601. SceneObjectPart[] parts = m_parts.GetArray();
  1602. for (int i = 0; i < parts.Length; i++)
  1603. parts[i].ScheduleTerseUpdate();
  1604. }
  1605. /// <summary>
  1606. /// Immediately send a full update for this scene object.
  1607. /// </summary>
  1608. public void SendGroupFullUpdate()
  1609. {
  1610. if (IsDeleted)
  1611. return;
  1612. // m_log.DebugFormat("[SOG]: Sending immediate full group update for {0} {1}", Name, UUID);
  1613. RootPart.SendFullUpdateToAllClients();
  1614. SceneObjectPart[] parts = m_parts.GetArray();
  1615. for (int i = 0; i < parts.Length; i++)
  1616. {
  1617. SceneObjectPart part = parts[i];
  1618. if (part != RootPart)
  1619. part.SendFullUpdateToAllClients();
  1620. }
  1621. }
  1622. /// <summary>
  1623. /// Immediately send an update for this scene object's root prim only.
  1624. /// This is for updates regarding the object as a whole, and none of its parts in particular.
  1625. /// Note: this may not be used by opensim (it probably should) but it's used by
  1626. /// external modules.
  1627. /// </summary>
  1628. public void SendGroupRootTerseUpdate()
  1629. {
  1630. if (IsDeleted)
  1631. return;
  1632. RootPart.SendTerseUpdateToAllClients();
  1633. }
  1634. public void QueueForUpdateCheck()
  1635. {
  1636. if (m_scene == null) // Need to check here as it's null during object creation
  1637. return;
  1638. m_scene.SceneGraph.AddToUpdateList(this);
  1639. }
  1640. /// <summary>
  1641. /// Immediately send a terse update for this scene object.
  1642. /// </summary>
  1643. public void SendGroupTerseUpdate()
  1644. {
  1645. if (IsDeleted)
  1646. return;
  1647. SceneObjectPart[] parts = m_parts.GetArray();
  1648. for (int i = 0; i < parts.Length; i++)
  1649. parts[i].SendTerseUpdateToAllClients();
  1650. }
  1651. /// <summary>
  1652. /// Send metadata about the root prim (name, description, sale price, permissions, etc.) to a client.
  1653. /// </summary>
  1654. /// <param name="client"></param>
  1655. public void SendPropertiesToClient(IClientAPI client)
  1656. {
  1657. m_rootPart.SendPropertiesToClient(client);
  1658. }
  1659. #region SceneGroupPart Methods
  1660. /// <summary>
  1661. /// Get the child part by LinkNum
  1662. /// </summary>
  1663. /// <param name="linknum"></param>
  1664. /// <returns>null if no child part with that linknum or child part</returns>
  1665. public SceneObjectPart GetLinkNumPart(int linknum)
  1666. {
  1667. SceneObjectPart[] parts = m_parts.GetArray();
  1668. for (int i = 0; i < parts.Length; i++)
  1669. {
  1670. if (parts[i].LinkNum == linknum)
  1671. return parts[i];
  1672. }
  1673. return null;
  1674. }
  1675. /// <summary>
  1676. /// Get a part with a given UUID
  1677. /// </summary>
  1678. /// <param name="primID"></param>
  1679. /// <returns>null if a part with the primID was not found</returns>
  1680. public SceneObjectPart GetPart(UUID primID)
  1681. {
  1682. SceneObjectPart childPart;
  1683. m_parts.TryGetValue(primID, out childPart);
  1684. return childPart;
  1685. }
  1686. /// <summary>
  1687. /// Get a part with a given local ID
  1688. /// </summary>
  1689. /// <param name="localID"></param>
  1690. /// <returns>null if a part with the local ID was not found</returns>
  1691. public SceneObjectPart GetPart(uint localID)
  1692. {
  1693. SceneObjectPart[] parts = m_parts.GetArray();
  1694. for (int i = 0; i < parts.Length; i++)
  1695. {
  1696. if (parts[i].LocalId == localID)
  1697. return parts[i];
  1698. }
  1699. return null;
  1700. }
  1701. #endregion
  1702. #region Packet Handlers
  1703. /// <summary>
  1704. /// Link the prims in a given group to this group
  1705. /// </summary>
  1706. /// <remarks>
  1707. /// Do not call this method directly - use Scene.LinkObjects() instead to avoid races between threads.
  1708. /// FIXME: There are places where scripts call these methods directly without locking. This is a potential race condition.
  1709. /// </remarks>
  1710. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1711. public void LinkToGroup(SceneObjectGroup objectGroup)
  1712. {
  1713. LinkToGroup(objectGroup, false);
  1714. }
  1715. // Link an existing group to this group.
  1716. // The group being linked need not be a linkset -- it can have just one prim.
  1717. public void LinkToGroup(SceneObjectGroup objectGroup, bool insert)
  1718. {
  1719. // m_log.DebugFormat(
  1720. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1721. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1722. // Linking to ourselves is not a valid operation.
  1723. if (objectGroup == this)
  1724. return;
  1725. // If the configured linkset capacity is greater than zero,
  1726. // and the new linkset would have a prim count higher than this
  1727. // value, do not link it.
  1728. if (m_scene.m_linksetCapacity > 0 &&
  1729. (PrimCount + objectGroup.PrimCount) >
  1730. m_scene.m_linksetCapacity)
  1731. {
  1732. m_log.DebugFormat(
  1733. "[SCENE OBJECT GROUP]: Cannot link group with root" +
  1734. " part {0}, {1} ({2} prims) to group with root part" +
  1735. " {3}, {4} ({5} prims) because the new linkset" +
  1736. " would exceed the configured maximum of {6}",
  1737. objectGroup.RootPart.Name, objectGroup.RootPart.UUID,
  1738. objectGroup.PrimCount, RootPart.Name, RootPart.UUID,
  1739. PrimCount, m_scene.m_linksetCapacity);
  1740. return;
  1741. }
  1742. // 'linkPart' == the root of the group being linked into this group
  1743. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1744. // physics flags from group to be applied to linked parts
  1745. bool grpusephys = UsesPhysics;
  1746. bool grptemporary = IsTemporary;
  1747. // Remember where the group being linked thought it was
  1748. Vector3 oldGroupPosition = linkPart.GroupPosition;
  1749. Quaternion oldRootRotation = linkPart.RotationOffset;
  1750. // A linked SOP remembers its location and rotation relative to the root of a group.
  1751. // Convert the root of the group being linked to be relative to the
  1752. // root of the group being linked to.
  1753. // Note: Some of the assignments have complex side effects.
  1754. // First move the new group's root SOP's position to be relative to ours
  1755. // (radams1: Not sure if the multiple setting of OffsetPosition is required. If not,
  1756. // this code can be reordered to have a more logical flow.)
  1757. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1758. // Assign the new parent to the root of the old group
  1759. linkPart.ParentID = m_rootPart.LocalId;
  1760. // Now that it's a child, it's group position is our root position
  1761. linkPart.GroupPosition = AbsolutePosition;
  1762. Vector3 axPos = linkPart.OffsetPosition;
  1763. // Rotate the linking root SOP's position to be relative to the new root prim
  1764. Quaternion parentRot = m_rootPart.RotationOffset;
  1765. axPos *= Quaternion.Inverse(parentRot);
  1766. linkPart.OffsetPosition = axPos;
  1767. // Make the linking root SOP's rotation relative to the new root prim
  1768. Quaternion oldRot = linkPart.RotationOffset;
  1769. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  1770. linkPart.RotationOffset = newRot;
  1771. // If there is only one SOP in a SOG, the LinkNum is zero. I.e., not a linkset.
  1772. // Now that we know this SOG has at least two SOPs in it, the new root
  1773. // SOP becomes the first in the linkset.
  1774. if (m_rootPart.LinkNum == 0)
  1775. m_rootPart.LinkNum = 1;
  1776. lock (m_parts.SyncRoot)
  1777. {
  1778. // Calculate the new link number for the old root SOP
  1779. int linkNum;
  1780. if (insert)
  1781. {
  1782. linkNum = 2;
  1783. foreach (SceneObjectPart part in Parts)
  1784. {
  1785. if (part.LinkNum > 1)
  1786. part.LinkNum++;
  1787. }
  1788. }
  1789. else
  1790. {
  1791. linkNum = PrimCount + 1;
  1792. }
  1793. // Add the old root SOP as a part in our group's list
  1794. m_parts.Add(linkPart.UUID, linkPart);
  1795. linkPart.SetParent(this);
  1796. linkPart.CreateSelected = true;
  1797. // let physics know preserve part volume dtc messy since UpdatePrimFlags doesn't look to parent changes for now
  1798. linkPart.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (linkPart.Flags & PrimFlags.Phantom) != 0), linkPart.VolumeDetectActive);
  1799. // If the added SOP is physical, also tell the physics engine about the link relationship.
  1800. if (linkPart.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  1801. {
  1802. linkPart.PhysActor.link(m_rootPart.PhysActor);
  1803. this.Scene.PhysicsScene.AddPhysicsActorTaint(linkPart.PhysActor);
  1804. }
  1805. linkPart.LinkNum = linkNum++;
  1806. // Get a list of the SOP's in the old group in order of their linknum's.
  1807. SceneObjectPart[] ogParts = objectGroup.Parts;
  1808. Array.Sort(ogParts, delegate(SceneObjectPart a, SceneObjectPart b)
  1809. {
  1810. return a.LinkNum - b.LinkNum;
  1811. });
  1812. // Add each of the SOP's from the old linkset to our linkset
  1813. for (int i = 0; i < ogParts.Length; i++)
  1814. {
  1815. SceneObjectPart part = ogParts[i];
  1816. if (part.UUID != objectGroup.m_rootPart.UUID)
  1817. {
  1818. LinkNonRootPart(part, oldGroupPosition, oldRootRotation, linkNum++);
  1819. // Update the physics flags for the newly added SOP
  1820. // (Is this necessary? LinkNonRootPart() has already called UpdatePrimFlags but with different flags!??)
  1821. part.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (part.Flags & PrimFlags.Phantom) != 0), part.VolumeDetectActive);
  1822. // If the added SOP is physical, also tell the physics engine about the link relationship.
  1823. if (part.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  1824. {
  1825. part.PhysActor.link(m_rootPart.PhysActor);
  1826. this.Scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1827. }
  1828. }
  1829. part.ClearUndoState();
  1830. }
  1831. }
  1832. // Now that we've aquired all of the old SOG's parts, remove the old SOG from the scene.
  1833. m_scene.UnlinkSceneObject(objectGroup, true);
  1834. objectGroup.IsDeleted = true;
  1835. objectGroup.m_parts.Clear();
  1836. // Can't do this yet since backup still makes use of the root part without any synchronization
  1837. // objectGroup.m_rootPart = null;
  1838. // If linking prims with different permissions, fix them
  1839. AdjustChildPrimPermissions();
  1840. AttachToBackup();
  1841. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  1842. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  1843. // unmoved prims!
  1844. ResetChildPrimPhysicsPositions();
  1845. //HasGroupChanged = true;
  1846. //ScheduleGroupForFullUpdate();
  1847. }
  1848. /// <summary>
  1849. /// Delink the given prim from this group. The delinked prim is established as
  1850. /// an independent SceneObjectGroup.
  1851. /// </summary>
  1852. /// <remarks>
  1853. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  1854. /// condition. But currently there is no
  1855. /// alternative method that does take a lonk to delink a single prim.
  1856. /// </remarks>
  1857. /// <param name="partID"></param>
  1858. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  1859. public SceneObjectGroup DelinkFromGroup(uint partID)
  1860. {
  1861. return DelinkFromGroup(partID, true);
  1862. }
  1863. /// <summary>
  1864. /// Delink the given prim from this group. The delinked prim is established as
  1865. /// an independent SceneObjectGroup.
  1866. /// </summary>
  1867. /// <remarks>
  1868. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  1869. /// condition. But currently there is no
  1870. /// alternative method that does take a lonk to delink a single prim.
  1871. /// </remarks>
  1872. /// <param name="partID"></param>
  1873. /// <param name="sendEvents"></param>
  1874. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  1875. public SceneObjectGroup DelinkFromGroup(uint partID, bool sendEvents)
  1876. {
  1877. SceneObjectPart linkPart = GetPart(partID);
  1878. if (linkPart != null)
  1879. {
  1880. return DelinkFromGroup(linkPart, sendEvents);
  1881. }
  1882. else
  1883. {
  1884. m_log.WarnFormat("[SCENE OBJECT GROUP]: " +
  1885. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  1886. partID, LocalId, UUID);
  1887. return null;
  1888. }
  1889. }
  1890. /// <summary>
  1891. /// Delink the given prim from this group. The delinked prim is established as
  1892. /// an independent SceneObjectGroup.
  1893. /// </summary>
  1894. /// <remarks>
  1895. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  1896. /// condition. But currently there is no
  1897. /// alternative method that does take a lock to delink a single prim.
  1898. /// </remarks>
  1899. /// <param name="partID"></param>
  1900. /// <param name="sendEvents"></param>
  1901. /// <returns>The object group of the newly delinked prim.</returns>
  1902. public SceneObjectGroup DelinkFromGroup(SceneObjectPart linkPart, bool sendEvents)
  1903. {
  1904. // m_log.DebugFormat(
  1905. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  1906. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  1907. linkPart.ClearUndoState();
  1908. Vector3 worldPos = linkPart.GetWorldPosition();
  1909. Quaternion worldRot = linkPart.GetWorldRotation();
  1910. // Remove the part from this object
  1911. lock (m_parts.SyncRoot)
  1912. {
  1913. m_parts.Remove(linkPart.UUID);
  1914. SceneObjectPart[] parts = m_parts.GetArray();
  1915. // Rejigger the linknum's of the remaining SOP's to fill any gap
  1916. if (parts.Length == 1 && RootPart != null)
  1917. {
  1918. // Single prim left
  1919. RootPart.LinkNum = 0;
  1920. }
  1921. else
  1922. {
  1923. for (int i = 0; i < parts.Length; i++)
  1924. {
  1925. SceneObjectPart part = parts[i];
  1926. if (part.LinkNum > linkPart.LinkNum)
  1927. part.LinkNum--;
  1928. }
  1929. }
  1930. }
  1931. linkPart.ParentID = 0;
  1932. linkPart.LinkNum = 0;
  1933. PhysicsActor linkPartPa = linkPart.PhysActor;
  1934. // Remove the SOP from the physical scene.
  1935. // If the new SOG is physical, it is re-created later.
  1936. // (There is a problem here in that we have not yet told the physics
  1937. // engine about the delink. Someday, linksets should be made first
  1938. // class objects in the physics engine interface).
  1939. if (linkPartPa != null)
  1940. m_scene.PhysicsScene.RemovePrim(linkPartPa);
  1941. // We need to reset the child part's position
  1942. // ready for life as a separate object after being a part of another object
  1943. /* This commented out code seems to recompute what GetWorldPosition already does.
  1944. * Replace with a call to GetWorldPosition (before unlinking)
  1945. Quaternion parentRot = m_rootPart.RotationOffset;
  1946. Vector3 axPos = linkPart.OffsetPosition;
  1947. axPos *= parentRot;
  1948. linkPart.OffsetPosition = new Vector3(axPos.X, axPos.Y, axPos.Z);
  1949. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  1950. linkPart.OffsetPosition = new Vector3(0, 0, 0);
  1951. */
  1952. linkPart.GroupPosition = worldPos;
  1953. linkPart.OffsetPosition = Vector3.Zero;
  1954. linkPart.RotationOffset = worldRot;
  1955. // Create a new SOG to go around this unlinked and unattached SOP
  1956. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  1957. m_scene.AddNewSceneObject(objectGroup, true);
  1958. if (sendEvents)
  1959. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  1960. linkPart.Rezzed = RootPart.Rezzed;
  1961. // When we delete a group, we currently have to force persist to the database if the object id has changed
  1962. // (since delete works by deleting all rows which have a given object id)
  1963. objectGroup.HasGroupChangedDueToDelink = true;
  1964. return objectGroup;
  1965. }
  1966. /// <summary>
  1967. /// Stop this object from being persisted over server restarts.
  1968. /// </summary>
  1969. /// <param name="objectGroup"></param>
  1970. public virtual void DetachFromBackup()
  1971. {
  1972. if (m_isBackedUp && Scene != null)
  1973. m_scene.EventManager.OnBackup -= ProcessBackup;
  1974. m_isBackedUp = false;
  1975. }
  1976. // This links an SOP from a previous linkset into my linkset.
  1977. // The trick is that the SOP's position and rotation are relative to the old root SOP's
  1978. // so we are passed in the position and rotation of the old linkset so this can
  1979. // unjigger this SOP's position and rotation from the previous linkset and
  1980. // then make them relative to my linkset root.
  1981. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation, int linkNum)
  1982. {
  1983. Quaternion parentRot = oldGroupRotation;
  1984. Quaternion oldRot = part.RotationOffset;
  1985. // Move our position to not be relative to the old parent
  1986. Vector3 axPos = part.OffsetPosition;
  1987. axPos *= parentRot;
  1988. part.OffsetPosition = axPos;
  1989. part.GroupPosition = oldGroupPosition + part.OffsetPosition;
  1990. part.OffsetPosition = Vector3.Zero;
  1991. // Compution our rotation to be not relative to the old parent
  1992. Quaternion worldRot = parentRot * oldRot;
  1993. part.RotationOffset = worldRot;
  1994. // Add this SOP to our linkset
  1995. part.SetParent(this);
  1996. part.ParentID = m_rootPart.LocalId;
  1997. m_parts.Add(part.UUID, part);
  1998. part.LinkNum = linkNum;
  1999. // Compute the new position of this SOP relative to the group position
  2000. part.OffsetPosition = part.GroupPosition - AbsolutePosition;
  2001. // (radams1 20120711: I don't know why part.OffsetPosition is set multiple times.
  2002. // It would have the affect of setting the physics engine position multiple
  2003. // times. In theory, that is not necessary but I don't have a good linkset
  2004. // test to know that cleaning up this code wouldn't break things.)
  2005. // Rotate the relative position by the rotation of the group
  2006. Quaternion rootRotation = m_rootPart.RotationOffset;
  2007. Vector3 pos = part.OffsetPosition;
  2008. pos *= Quaternion.Inverse(rootRotation);
  2009. part.OffsetPosition = pos;
  2010. // Compute the SOP's rotation relative to the rotation of the group.
  2011. parentRot = m_rootPart.RotationOffset;
  2012. oldRot = part.RotationOffset;
  2013. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  2014. part.RotationOffset = newRot;
  2015. // Since this SOP's state has changed, push those changes into the physics engine
  2016. // and the simulator.
  2017. part.UpdatePrimFlags(UsesPhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  2018. }
  2019. /// <summary>
  2020. /// If object is physical, apply force to move it around
  2021. /// If object is not physical, just put it at the resulting location
  2022. /// </summary>
  2023. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  2024. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  2025. /// <param name="remoteClient"></param>
  2026. public void GrabMovement(Vector3 offset, Vector3 pos, IClientAPI remoteClient)
  2027. {
  2028. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2029. {
  2030. PhysicsActor pa = m_rootPart.PhysActor;
  2031. if (pa != null)
  2032. {
  2033. if (pa.IsPhysical)
  2034. {
  2035. if (!m_rootPart.BlockGrab)
  2036. {
  2037. Vector3 llmoveforce = pos - AbsolutePosition;
  2038. Vector3 grabforce = llmoveforce;
  2039. grabforce = (grabforce / 10) * pa.Mass;
  2040. pa.AddForce(grabforce, true);
  2041. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2042. }
  2043. }
  2044. else
  2045. {
  2046. //NonPhysicalGrabMovement(pos);
  2047. }
  2048. }
  2049. else
  2050. {
  2051. //NonPhysicalGrabMovement(pos);
  2052. }
  2053. }
  2054. }
  2055. public void NonPhysicalGrabMovement(Vector3 pos)
  2056. {
  2057. AbsolutePosition = pos;
  2058. m_rootPart.SendTerseUpdateToAllClients();
  2059. }
  2060. /// <summary>
  2061. /// If object is physical, prepare for spinning torques (set flag to save old orientation)
  2062. /// </summary>
  2063. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  2064. /// <param name="remoteClient"></param>
  2065. public void SpinStart(IClientAPI remoteClient)
  2066. {
  2067. if (m_scene.EventManager.TriggerGroupSpinStart(UUID))
  2068. {
  2069. PhysicsActor pa = m_rootPart.PhysActor;
  2070. if (pa != null)
  2071. {
  2072. if (pa.IsPhysical)
  2073. {
  2074. m_rootPart.IsWaitingForFirstSpinUpdatePacket = true;
  2075. }
  2076. }
  2077. }
  2078. }
  2079. /// <summary>
  2080. /// If object is physical, apply torque to spin it around
  2081. /// </summary>
  2082. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  2083. /// <param name="remoteClient"></param>
  2084. public void SpinMovement(Quaternion newOrientation, IClientAPI remoteClient)
  2085. {
  2086. // The incoming newOrientation, sent by the client, "seems" to be the
  2087. // desired target orientation. This needs further verification; in particular,
  2088. // one would expect that the initial incoming newOrientation should be
  2089. // fairly close to the original prim's physical orientation,
  2090. // m_rootPart.PhysActor.Orientation. This however does not seem to be the
  2091. // case (might just be an issue with different quaternions representing the
  2092. // same rotation, or it might be a coordinate system issue).
  2093. //
  2094. // Since it's not clear what the relationship is between the PhysActor.Orientation
  2095. // and the incoming orientations sent by the client, we take an alternative approach
  2096. // of calculating the delta rotation between the orientations being sent by the
  2097. // client. (Since a spin is invoked by ctrl+shift+drag in the client, we expect
  2098. // a steady stream of several new orientations coming in from the client.)
  2099. // This ensures that the delta rotations are being calculated from self-consistent
  2100. // pairs of old/new rotations. Given the delta rotation, we apply a torque around
  2101. // the delta rotation axis, scaled by the object mass times an arbitrary scaling
  2102. // factor (to ensure the resulting torque is not "too strong" or "too weak").
  2103. //
  2104. // Ideally we need to calculate (probably iteratively) the exact torque or series
  2105. // of torques needed to arrive exactly at the destination orientation. However, since
  2106. // it is not yet clear how to map the destination orientation (provided by the viewer)
  2107. // into PhysActor orientations (needed by the physics engine), we omit this step.
  2108. // This means that the resulting torque will at least be in the correct direction,
  2109. // but it will result in over-shoot or under-shoot of the target orientation.
  2110. // For the end user, this means that ctrl+shift+drag can be used for relative,
  2111. // but not absolute, adjustments of orientation for physical prims.
  2112. if (m_scene.EventManager.TriggerGroupSpin(UUID, newOrientation))
  2113. {
  2114. PhysicsActor pa = m_rootPart.PhysActor;
  2115. if (pa != null)
  2116. {
  2117. if (pa.IsPhysical)
  2118. {
  2119. if (m_rootPart.IsWaitingForFirstSpinUpdatePacket)
  2120. {
  2121. // first time initialization of "old" orientation for calculation of delta rotations
  2122. m_rootPart.SpinOldOrientation = newOrientation;
  2123. m_rootPart.IsWaitingForFirstSpinUpdatePacket = false;
  2124. }
  2125. else
  2126. {
  2127. // save and update old orientation
  2128. Quaternion old = m_rootPart.SpinOldOrientation;
  2129. m_rootPart.SpinOldOrientation = newOrientation;
  2130. //m_log.Error("[SCENE OBJECT GROUP]: Old orientation is " + old);
  2131. //m_log.Error("[SCENE OBJECT GROUP]: Incoming new orientation is " + newOrientation);
  2132. // compute difference between previous old rotation and new incoming rotation
  2133. Quaternion minimalRotationFromQ1ToQ2 = Quaternion.Inverse(old) * newOrientation;
  2134. float rotationAngle;
  2135. Vector3 rotationAxis;
  2136. minimalRotationFromQ1ToQ2.GetAxisAngle(out rotationAxis, out rotationAngle);
  2137. rotationAxis.Normalize();
  2138. //m_log.Error("SCENE OBJECT GROUP]: rotation axis is " + rotationAxis);
  2139. Vector3 spinforce = new Vector3(rotationAxis.X, rotationAxis.Y, rotationAxis.Z);
  2140. spinforce = (spinforce/8) * pa.Mass; // 8 is an arbitrary torque scaling factor
  2141. pa.AddAngularForce(spinforce,true);
  2142. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2143. }
  2144. }
  2145. else
  2146. {
  2147. //NonPhysicalSpinMovement(pos);
  2148. }
  2149. }
  2150. else
  2151. {
  2152. //NonPhysicalSpinMovement(pos);
  2153. }
  2154. }
  2155. }
  2156. /// <summary>
  2157. /// Set the name of a prim
  2158. /// </summary>
  2159. /// <param name="name"></param>
  2160. /// <param name="localID"></param>
  2161. public void SetPartName(string name, uint localID)
  2162. {
  2163. SceneObjectPart part = GetPart(localID);
  2164. if (part != null)
  2165. {
  2166. part.Name = name;
  2167. }
  2168. }
  2169. public void SetPartDescription(string des, uint localID)
  2170. {
  2171. SceneObjectPart part = GetPart(localID);
  2172. if (part != null)
  2173. {
  2174. part.Description = des;
  2175. }
  2176. }
  2177. public void SetPartText(string text, uint localID)
  2178. {
  2179. SceneObjectPart part = GetPart(localID);
  2180. if (part != null)
  2181. {
  2182. part.SetText(text);
  2183. }
  2184. }
  2185. public void SetPartText(string text, UUID partID)
  2186. {
  2187. SceneObjectPart part = GetPart(partID);
  2188. if (part != null)
  2189. {
  2190. part.SetText(text);
  2191. }
  2192. }
  2193. public string GetPartName(uint localID)
  2194. {
  2195. SceneObjectPart part = GetPart(localID);
  2196. if (part != null)
  2197. {
  2198. return part.Name;
  2199. }
  2200. return String.Empty;
  2201. }
  2202. public string GetPartDescription(uint localID)
  2203. {
  2204. SceneObjectPart part = GetPart(localID);
  2205. if (part != null)
  2206. {
  2207. return part.Description;
  2208. }
  2209. return String.Empty;
  2210. }
  2211. /// <summary>
  2212. /// Update prim flags for this group.
  2213. /// </summary>
  2214. /// <param name="localID"></param>
  2215. /// <param name="UsePhysics"></param>
  2216. /// <param name="SetTemporary"></param>
  2217. /// <param name="SetPhantom"></param>
  2218. /// <param name="SetVolumeDetect"></param>
  2219. public void UpdatePrimFlags(uint localID, bool UsePhysics, bool SetTemporary, bool SetPhantom, bool SetVolumeDetect)
  2220. {
  2221. SceneObjectPart selectionPart = GetPart(localID);
  2222. if (SetTemporary && Scene != null)
  2223. {
  2224. DetachFromBackup();
  2225. // Remove from database and parcel prim count
  2226. //
  2227. m_scene.DeleteFromStorage(UUID);
  2228. m_scene.EventManager.TriggerParcelPrimCountTainted();
  2229. }
  2230. if (selectionPart != null)
  2231. {
  2232. SceneObjectPart[] parts = m_parts.GetArray();
  2233. if (Scene != null)
  2234. {
  2235. for (int i = 0; i < parts.Length; i++)
  2236. {
  2237. SceneObjectPart part = parts[i];
  2238. if (part.Scale.X > m_scene.m_maxPhys ||
  2239. part.Scale.Y > m_scene.m_maxPhys ||
  2240. part.Scale.Z > m_scene.m_maxPhys )
  2241. {
  2242. UsePhysics = false; // Reset physics
  2243. break;
  2244. }
  2245. }
  2246. }
  2247. for (int i = 0; i < parts.Length; i++)
  2248. parts[i].UpdatePrimFlags(UsePhysics, SetTemporary, SetPhantom, SetVolumeDetect);
  2249. }
  2250. }
  2251. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  2252. {
  2253. SceneObjectPart part = GetPart(localID);
  2254. if (part != null)
  2255. {
  2256. part.UpdateExtraParam(type, inUse, data);
  2257. }
  2258. }
  2259. /// <summary>
  2260. /// Update the texture entry for this part
  2261. /// </summary>
  2262. /// <param name="localID"></param>
  2263. /// <param name="textureEntry"></param>
  2264. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  2265. {
  2266. SceneObjectPart part = GetPart(localID);
  2267. if (part != null)
  2268. {
  2269. part.UpdateTextureEntry(textureEntry);
  2270. }
  2271. }
  2272. public void AdjustChildPrimPermissions()
  2273. {
  2274. ForEachPart(part =>
  2275. {
  2276. if (part != RootPart)
  2277. part.ClonePermissions(RootPart);
  2278. });
  2279. }
  2280. public void UpdatePermissions(UUID AgentID, byte field, uint localID,
  2281. uint mask, byte addRemTF)
  2282. {
  2283. RootPart.UpdatePermissions(AgentID, field, localID, mask, addRemTF);
  2284. AdjustChildPrimPermissions();
  2285. HasGroupChanged = true;
  2286. // Send the group's properties to all clients once all parts are updated
  2287. IClientAPI client;
  2288. if (Scene.TryGetClient(AgentID, out client))
  2289. SendPropertiesToClient(client);
  2290. }
  2291. #endregion
  2292. #region Shape
  2293. /// <summary>
  2294. ///
  2295. /// </summary>
  2296. /// <param name="shapeBlock"></param>
  2297. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  2298. {
  2299. SceneObjectPart part = GetPart(localID);
  2300. if (part != null)
  2301. {
  2302. part.UpdateShape(shapeBlock);
  2303. PhysicsActor pa = m_rootPart.PhysActor;
  2304. if (pa != null)
  2305. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2306. }
  2307. }
  2308. #endregion
  2309. #region Resize
  2310. /// <summary>
  2311. /// Resize the entire group of prims.
  2312. /// </summary>
  2313. /// <param name="scale"></param>
  2314. public void GroupResize(Vector3 scale)
  2315. {
  2316. // m_log.DebugFormat(
  2317. // "[SCENE OBJECT GROUP]: Group resizing {0} {1} from {2} to {3}", Name, LocalId, RootPart.Scale, scale);
  2318. PhysicsActor pa = m_rootPart.PhysActor;
  2319. RootPart.StoreUndoState(true);
  2320. if (Scene != null)
  2321. {
  2322. scale.X = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.X));
  2323. scale.Y = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.Y));
  2324. scale.Z = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.Z));
  2325. if (pa != null && pa.IsPhysical)
  2326. {
  2327. scale.X = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.X));
  2328. scale.Y = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.Y));
  2329. scale.Z = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.Z));
  2330. }
  2331. }
  2332. float x = (scale.X / RootPart.Scale.X);
  2333. float y = (scale.Y / RootPart.Scale.Y);
  2334. float z = (scale.Z / RootPart.Scale.Z);
  2335. SceneObjectPart[] parts = m_parts.GetArray();
  2336. if (Scene != null & (x > 1.0f || y > 1.0f || z > 1.0f))
  2337. {
  2338. for (int i = 0; i < parts.Length; i++)
  2339. {
  2340. SceneObjectPart obPart = parts[i];
  2341. if (obPart.UUID != m_rootPart.UUID)
  2342. {
  2343. // obPart.IgnoreUndoUpdate = true;
  2344. Vector3 oldSize = new Vector3(obPart.Scale);
  2345. float f = 1.0f;
  2346. float a = 1.0f;
  2347. if (pa != null && pa.IsPhysical)
  2348. {
  2349. if (oldSize.X * x > Scene.m_maxPhys)
  2350. {
  2351. f = m_scene.m_maxPhys / oldSize.X;
  2352. a = f / x;
  2353. x *= a;
  2354. y *= a;
  2355. z *= a;
  2356. }
  2357. else if (oldSize.X * x < Scene.m_minPhys)
  2358. {
  2359. f = m_scene.m_minPhys / oldSize.X;
  2360. a = f / x;
  2361. x *= a;
  2362. y *= a;
  2363. z *= a;
  2364. }
  2365. if (oldSize.Y * y > Scene.m_maxPhys)
  2366. {
  2367. f = m_scene.m_maxPhys / oldSize.Y;
  2368. a = f / y;
  2369. x *= a;
  2370. y *= a;
  2371. z *= a;
  2372. }
  2373. else if (oldSize.Y * y < Scene.m_minPhys)
  2374. {
  2375. f = m_scene.m_minPhys / oldSize.Y;
  2376. a = f / y;
  2377. x *= a;
  2378. y *= a;
  2379. z *= a;
  2380. }
  2381. if (oldSize.Z * z > Scene.m_maxPhys)
  2382. {
  2383. f = m_scene.m_maxPhys / oldSize.Z;
  2384. a = f / z;
  2385. x *= a;
  2386. y *= a;
  2387. z *= a;
  2388. }
  2389. else if (oldSize.Z * z < Scene.m_minPhys)
  2390. {
  2391. f = m_scene.m_minPhys / oldSize.Z;
  2392. a = f / z;
  2393. x *= a;
  2394. y *= a;
  2395. z *= a;
  2396. }
  2397. }
  2398. else
  2399. {
  2400. if (oldSize.X * x > Scene.m_maxNonphys)
  2401. {
  2402. f = m_scene.m_maxNonphys / oldSize.X;
  2403. a = f / x;
  2404. x *= a;
  2405. y *= a;
  2406. z *= a;
  2407. }
  2408. else if (oldSize.X * x < Scene.m_minNonphys)
  2409. {
  2410. f = m_scene.m_minNonphys / oldSize.X;
  2411. a = f / x;
  2412. x *= a;
  2413. y *= a;
  2414. z *= a;
  2415. }
  2416. if (oldSize.Y * y > Scene.m_maxNonphys)
  2417. {
  2418. f = m_scene.m_maxNonphys / oldSize.Y;
  2419. a = f / y;
  2420. x *= a;
  2421. y *= a;
  2422. z *= a;
  2423. }
  2424. else if (oldSize.Y * y < Scene.m_minNonphys)
  2425. {
  2426. f = m_scene.m_minNonphys / oldSize.Y;
  2427. a = f / y;
  2428. x *= a;
  2429. y *= a;
  2430. z *= a;
  2431. }
  2432. if (oldSize.Z * z > Scene.m_maxNonphys)
  2433. {
  2434. f = m_scene.m_maxNonphys / oldSize.Z;
  2435. a = f / z;
  2436. x *= a;
  2437. y *= a;
  2438. z *= a;
  2439. }
  2440. else if (oldSize.Z * z < Scene.m_minNonphys)
  2441. {
  2442. f = m_scene.m_minNonphys / oldSize.Z;
  2443. a = f / z;
  2444. x *= a;
  2445. y *= a;
  2446. z *= a;
  2447. }
  2448. }
  2449. // obPart.IgnoreUndoUpdate = false;
  2450. }
  2451. }
  2452. }
  2453. Vector3 prevScale = RootPart.Scale;
  2454. prevScale.X *= x;
  2455. prevScale.Y *= y;
  2456. prevScale.Z *= z;
  2457. // RootPart.IgnoreUndoUpdate = true;
  2458. RootPart.Resize(prevScale);
  2459. // RootPart.IgnoreUndoUpdate = false;
  2460. for (int i = 0; i < parts.Length; i++)
  2461. {
  2462. SceneObjectPart obPart = parts[i];
  2463. if (obPart.UUID != m_rootPart.UUID)
  2464. {
  2465. obPart.IgnoreUndoUpdate = true;
  2466. Vector3 currentpos = new Vector3(obPart.OffsetPosition);
  2467. currentpos.X *= x;
  2468. currentpos.Y *= y;
  2469. currentpos.Z *= z;
  2470. Vector3 newSize = new Vector3(obPart.Scale);
  2471. newSize.X *= x;
  2472. newSize.Y *= y;
  2473. newSize.Z *= z;
  2474. obPart.Resize(newSize);
  2475. obPart.UpdateOffSet(currentpos);
  2476. obPart.IgnoreUndoUpdate = false;
  2477. }
  2478. // obPart.IgnoreUndoUpdate = false;
  2479. // obPart.StoreUndoState();
  2480. }
  2481. // m_log.DebugFormat(
  2482. // "[SCENE OBJECT GROUP]: Finished group resizing {0} {1} to {2}", Name, LocalId, RootPart.Scale);
  2483. }
  2484. #endregion
  2485. #region Position
  2486. /// <summary>
  2487. /// Move this scene object
  2488. /// </summary>
  2489. /// <param name="pos"></param>
  2490. public void UpdateGroupPosition(Vector3 pos)
  2491. {
  2492. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Updating group position on {0} {1} to {2}", Name, LocalId, pos);
  2493. RootPart.StoreUndoState(true);
  2494. // SceneObjectPart[] parts = m_parts.GetArray();
  2495. // for (int i = 0; i < parts.Length; i++)
  2496. // parts[i].StoreUndoState();
  2497. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2498. {
  2499. if (IsAttachment)
  2500. {
  2501. m_rootPart.AttachedPos = pos;
  2502. }
  2503. if (RootPart.GetStatusSandbox())
  2504. {
  2505. if (Util.GetDistanceTo(RootPart.StatusSandboxPos, pos) > 10)
  2506. {
  2507. RootPart.ScriptSetPhysicsStatus(false);
  2508. pos = AbsolutePosition;
  2509. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  2510. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  2511. }
  2512. }
  2513. AbsolutePosition = pos;
  2514. HasGroupChanged = true;
  2515. }
  2516. //we need to do a terse update even if the move wasn't allowed
  2517. // so that the position is reset in the client (the object snaps back)
  2518. RootPart.ScheduleTerseUpdate();
  2519. }
  2520. /// <summary>
  2521. /// Update the position of a single part of this scene object
  2522. /// </summary>
  2523. /// <param name="pos"></param>
  2524. /// <param name="localID"></param>
  2525. public void UpdateSinglePosition(Vector3 pos, uint localID)
  2526. {
  2527. SceneObjectPart part = GetPart(localID);
  2528. // SceneObjectPart[] parts = m_parts.GetArray();
  2529. // for (int i = 0; i < parts.Length; i++)
  2530. // parts[i].StoreUndoState();
  2531. if (part != null)
  2532. {
  2533. // m_log.DebugFormat(
  2534. // "[SCENE OBJECT GROUP]: Updating single position of {0} {1} to {2}", part.Name, part.LocalId, pos);
  2535. part.StoreUndoState(false);
  2536. part.IgnoreUndoUpdate = true;
  2537. if (part.UUID == m_rootPart.UUID)
  2538. {
  2539. UpdateRootPosition(pos);
  2540. }
  2541. else
  2542. {
  2543. part.UpdateOffSet(pos);
  2544. }
  2545. HasGroupChanged = true;
  2546. part.IgnoreUndoUpdate = false;
  2547. }
  2548. }
  2549. /// <summary>
  2550. /// Update just the root prim position in a linkset
  2551. /// </summary>
  2552. /// <param name="pos"></param>
  2553. public void UpdateRootPosition(Vector3 pos)
  2554. {
  2555. // m_log.DebugFormat(
  2556. // "[SCENE OBJECT GROUP]: Updating root position of {0} {1} to {2}", Name, LocalId, pos);
  2557. // SceneObjectPart[] parts = m_parts.GetArray();
  2558. // for (int i = 0; i < parts.Length; i++)
  2559. // parts[i].StoreUndoState();
  2560. Vector3 newPos = new Vector3(pos.X, pos.Y, pos.Z);
  2561. Vector3 oldPos =
  2562. new Vector3(AbsolutePosition.X + m_rootPart.OffsetPosition.X,
  2563. AbsolutePosition.Y + m_rootPart.OffsetPosition.Y,
  2564. AbsolutePosition.Z + m_rootPart.OffsetPosition.Z);
  2565. Vector3 diff = oldPos - newPos;
  2566. Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
  2567. Quaternion partRotation = m_rootPart.RotationOffset;
  2568. axDiff *= Quaternion.Inverse(partRotation);
  2569. diff = axDiff;
  2570. SceneObjectPart[] parts = m_parts.GetArray();
  2571. for (int i = 0; i < parts.Length; i++)
  2572. {
  2573. SceneObjectPart obPart = parts[i];
  2574. if (obPart.UUID != m_rootPart.UUID)
  2575. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  2576. }
  2577. AbsolutePosition = newPos;
  2578. HasGroupChanged = true;
  2579. ScheduleGroupForTerseUpdate();
  2580. }
  2581. #endregion
  2582. #region Rotation
  2583. /// <summary>
  2584. /// Update the rotation of the group.
  2585. /// </summary>
  2586. /// <param name="rot"></param>
  2587. public void UpdateGroupRotationR(Quaternion rot)
  2588. {
  2589. // m_log.DebugFormat(
  2590. // "[SCENE OBJECT GROUP]: Updating group rotation R of {0} {1} to {2}", Name, LocalId, rot);
  2591. // SceneObjectPart[] parts = m_parts.GetArray();
  2592. // for (int i = 0; i < parts.Length; i++)
  2593. // parts[i].StoreUndoState();
  2594. m_rootPart.StoreUndoState(true);
  2595. m_rootPart.UpdateRotation(rot);
  2596. PhysicsActor actor = m_rootPart.PhysActor;
  2597. if (actor != null)
  2598. {
  2599. actor.Orientation = m_rootPart.RotationOffset;
  2600. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  2601. }
  2602. HasGroupChanged = true;
  2603. ScheduleGroupForTerseUpdate();
  2604. }
  2605. /// <summary>
  2606. /// Update the position and rotation of a group simultaneously.
  2607. /// </summary>
  2608. /// <param name="pos"></param>
  2609. /// <param name="rot"></param>
  2610. public void UpdateGroupRotationPR(Vector3 pos, Quaternion rot)
  2611. {
  2612. // m_log.DebugFormat(
  2613. // "[SCENE OBJECT GROUP]: Updating group rotation PR of {0} {1} to {2}", Name, LocalId, rot);
  2614. // SceneObjectPart[] parts = m_parts.GetArray();
  2615. // for (int i = 0; i < parts.Length; i++)
  2616. // parts[i].StoreUndoState();
  2617. RootPart.StoreUndoState(true);
  2618. RootPart.IgnoreUndoUpdate = true;
  2619. m_rootPart.UpdateRotation(rot);
  2620. PhysicsActor actor = m_rootPart.PhysActor;
  2621. if (actor != null)
  2622. {
  2623. actor.Orientation = m_rootPart.RotationOffset;
  2624. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  2625. }
  2626. if (IsAttachment)
  2627. {
  2628. m_rootPart.AttachedPos = pos;
  2629. }
  2630. AbsolutePosition = pos;
  2631. HasGroupChanged = true;
  2632. ScheduleGroupForTerseUpdate();
  2633. RootPart.IgnoreUndoUpdate = false;
  2634. }
  2635. /// <summary>
  2636. /// Update the rotation of a single prim within the group.
  2637. /// </summary>
  2638. /// <param name="rot"></param>
  2639. /// <param name="localID"></param>
  2640. public void UpdateSingleRotation(Quaternion rot, uint localID)
  2641. {
  2642. SceneObjectPart part = GetPart(localID);
  2643. SceneObjectPart[] parts = m_parts.GetArray();
  2644. for (int i = 0; i < parts.Length; i++)
  2645. parts[i].StoreUndoState();
  2646. if (part != null)
  2647. {
  2648. // m_log.DebugFormat(
  2649. // "[SCENE OBJECT GROUP]: Updating single rotation of {0} {1} to {2}", part.Name, part.LocalId, rot);
  2650. if (part.UUID == m_rootPart.UUID)
  2651. {
  2652. UpdateRootRotation(rot);
  2653. }
  2654. else
  2655. {
  2656. part.UpdateRotation(rot);
  2657. }
  2658. }
  2659. }
  2660. /// <summary>
  2661. /// Update the position and rotation simultaneously of a single prim within the group.
  2662. /// </summary>
  2663. /// <param name="rot"></param>
  2664. /// <param name="localID"></param>
  2665. public void UpdateSingleRotation(Quaternion rot, Vector3 pos, uint localID)
  2666. {
  2667. SceneObjectPart part = GetPart(localID);
  2668. if (part != null)
  2669. {
  2670. // m_log.DebugFormat(
  2671. // "[SCENE OBJECT GROUP]: Updating single position and rotation of {0} {1} to {2}",
  2672. // part.Name, part.LocalId, rot);
  2673. part.StoreUndoState();
  2674. part.IgnoreUndoUpdate = true;
  2675. if (part.UUID == m_rootPart.UUID)
  2676. {
  2677. UpdateRootRotation(rot);
  2678. AbsolutePosition = pos;
  2679. }
  2680. else
  2681. {
  2682. part.UpdateRotation(rot);
  2683. part.OffsetPosition = pos;
  2684. }
  2685. part.IgnoreUndoUpdate = false;
  2686. }
  2687. }
  2688. /// <summary>
  2689. /// Update the rotation of just the root prim of a linkset.
  2690. /// </summary>
  2691. /// <param name="rot"></param>
  2692. public void UpdateRootRotation(Quaternion rot)
  2693. {
  2694. // m_log.DebugFormat(
  2695. // "[SCENE OBJECT GROUP]: Updating root rotation of {0} {1} to {2}",
  2696. // Name, LocalId, rot);
  2697. Quaternion axRot = rot;
  2698. Quaternion oldParentRot = m_rootPart.RotationOffset;
  2699. m_rootPart.StoreUndoState();
  2700. m_rootPart.UpdateRotation(rot);
  2701. PhysicsActor pa = m_rootPart.PhysActor;
  2702. if (pa != null)
  2703. {
  2704. pa.Orientation = m_rootPart.RotationOffset;
  2705. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2706. }
  2707. SceneObjectPart[] parts = m_parts.GetArray();
  2708. for (int i = 0; i < parts.Length; i++)
  2709. {
  2710. SceneObjectPart prim = parts[i];
  2711. if (prim.UUID != m_rootPart.UUID)
  2712. {
  2713. prim.IgnoreUndoUpdate = true;
  2714. Vector3 axPos = prim.OffsetPosition;
  2715. axPos *= oldParentRot;
  2716. axPos *= Quaternion.Inverse(axRot);
  2717. prim.OffsetPosition = axPos;
  2718. Quaternion primsRot = prim.RotationOffset;
  2719. Quaternion newRot = oldParentRot * primsRot;
  2720. newRot = Quaternion.Inverse(axRot) * newRot;
  2721. prim.RotationOffset = newRot;
  2722. prim.ScheduleTerseUpdate();
  2723. prim.IgnoreUndoUpdate = false;
  2724. }
  2725. }
  2726. // for (int i = 0; i < parts.Length; i++)
  2727. // {
  2728. // SceneObjectPart childpart = parts[i];
  2729. // if (childpart != m_rootPart)
  2730. // {
  2731. //// childpart.IgnoreUndoUpdate = false;
  2732. //// childpart.StoreUndoState();
  2733. // }
  2734. // }
  2735. m_rootPart.ScheduleTerseUpdate();
  2736. // m_log.DebugFormat(
  2737. // "[SCENE OBJECT GROUP]: Updated root rotation of {0} {1} to {2}",
  2738. // Name, LocalId, rot);
  2739. }
  2740. #endregion
  2741. internal void SetAxisRotation(int axis, int rotate10)
  2742. {
  2743. bool setX = false;
  2744. bool setY = false;
  2745. bool setZ = false;
  2746. int xaxis = 2;
  2747. int yaxis = 4;
  2748. int zaxis = 8;
  2749. setX = ((axis & xaxis) != 0) ? true : false;
  2750. setY = ((axis & yaxis) != 0) ? true : false;
  2751. setZ = ((axis & zaxis) != 0) ? true : false;
  2752. float setval = (rotate10 > 0) ? 1f : 0f;
  2753. if (setX)
  2754. RootPart.RotationAxis.X = setval;
  2755. if (setY)
  2756. RootPart.RotationAxis.Y = setval;
  2757. if (setZ)
  2758. RootPart.RotationAxis.Z = setval;
  2759. if (setX || setY || setZ)
  2760. RootPart.SetPhysicsAxisRotation();
  2761. }
  2762. public int registerRotTargetWaypoint(Quaternion target, float tolerance)
  2763. {
  2764. scriptRotTarget waypoint = new scriptRotTarget();
  2765. waypoint.targetRot = target;
  2766. waypoint.tolerance = tolerance;
  2767. uint handle = m_scene.AllocateLocalId();
  2768. waypoint.handle = handle;
  2769. lock (m_rotTargets)
  2770. {
  2771. m_rotTargets.Add(handle, waypoint);
  2772. }
  2773. m_scene.AddGroupTarget(this);
  2774. return (int)handle;
  2775. }
  2776. public void unregisterRotTargetWaypoint(int handle)
  2777. {
  2778. lock (m_targets)
  2779. {
  2780. m_rotTargets.Remove((uint)handle);
  2781. if (m_targets.Count == 0)
  2782. m_scene.RemoveGroupTarget(this);
  2783. }
  2784. }
  2785. public int registerTargetWaypoint(Vector3 target, float tolerance)
  2786. {
  2787. scriptPosTarget waypoint = new scriptPosTarget();
  2788. waypoint.targetPos = target;
  2789. waypoint.tolerance = tolerance;
  2790. uint handle = m_scene.AllocateLocalId();
  2791. waypoint.handle = handle;
  2792. lock (m_targets)
  2793. {
  2794. m_targets.Add(handle, waypoint);
  2795. }
  2796. m_scene.AddGroupTarget(this);
  2797. return (int)handle;
  2798. }
  2799. public void unregisterTargetWaypoint(int handle)
  2800. {
  2801. lock (m_targets)
  2802. {
  2803. m_targets.Remove((uint)handle);
  2804. if (m_targets.Count == 0)
  2805. m_scene.RemoveGroupTarget(this);
  2806. }
  2807. }
  2808. public void checkAtTargets()
  2809. {
  2810. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  2811. {
  2812. if (m_targets.Count > 0)
  2813. {
  2814. bool at_target = false;
  2815. //Vector3 targetPos;
  2816. //uint targetHandle;
  2817. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  2818. lock (m_targets)
  2819. {
  2820. foreach (uint idx in m_targets.Keys)
  2821. {
  2822. scriptPosTarget target = m_targets[idx];
  2823. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  2824. {
  2825. // trigger at_target
  2826. if (m_scriptListens_atTarget)
  2827. {
  2828. at_target = true;
  2829. scriptPosTarget att = new scriptPosTarget();
  2830. att.targetPos = target.targetPos;
  2831. att.tolerance = target.tolerance;
  2832. att.handle = target.handle;
  2833. atTargets.Add(idx, att);
  2834. }
  2835. }
  2836. }
  2837. }
  2838. if (atTargets.Count > 0)
  2839. {
  2840. SceneObjectPart[] parts = m_parts.GetArray();
  2841. uint[] localids = new uint[parts.Length];
  2842. for (int i = 0; i < parts.Length; i++)
  2843. localids[i] = parts[i].LocalId;
  2844. for (int ctr = 0; ctr < localids.Length; ctr++)
  2845. {
  2846. foreach (uint target in atTargets.Keys)
  2847. {
  2848. scriptPosTarget att = atTargets[target];
  2849. m_scene.EventManager.TriggerAtTargetEvent(
  2850. localids[ctr], att.handle, att.targetPos, m_rootPart.GroupPosition);
  2851. }
  2852. }
  2853. return;
  2854. }
  2855. if (m_scriptListens_notAtTarget && !at_target)
  2856. {
  2857. //trigger not_at_target
  2858. SceneObjectPart[] parts = m_parts.GetArray();
  2859. uint[] localids = new uint[parts.Length];
  2860. for (int i = 0; i < parts.Length; i++)
  2861. localids[i] = parts[i].LocalId;
  2862. for (int ctr = 0; ctr < localids.Length; ctr++)
  2863. {
  2864. m_scene.EventManager.TriggerNotAtTargetEvent(localids[ctr]);
  2865. }
  2866. }
  2867. }
  2868. }
  2869. if (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget)
  2870. {
  2871. if (m_rotTargets.Count > 0)
  2872. {
  2873. bool at_Rottarget = false;
  2874. Dictionary<uint, scriptRotTarget> atRotTargets = new Dictionary<uint, scriptRotTarget>();
  2875. lock (m_rotTargets)
  2876. {
  2877. foreach (uint idx in m_rotTargets.Keys)
  2878. {
  2879. scriptRotTarget target = m_rotTargets[idx];
  2880. double angle
  2881. = Math.Acos(
  2882. target.targetRot.X * m_rootPart.RotationOffset.X
  2883. + target.targetRot.Y * m_rootPart.RotationOffset.Y
  2884. + target.targetRot.Z * m_rootPart.RotationOffset.Z
  2885. + target.targetRot.W * m_rootPart.RotationOffset.W)
  2886. * 2;
  2887. if (angle < 0) angle = -angle;
  2888. if (angle > Math.PI) angle = (Math.PI * 2 - angle);
  2889. if (angle <= target.tolerance)
  2890. {
  2891. // trigger at_rot_target
  2892. if (m_scriptListens_atRotTarget)
  2893. {
  2894. at_Rottarget = true;
  2895. scriptRotTarget att = new scriptRotTarget();
  2896. att.targetRot = target.targetRot;
  2897. att.tolerance = target.tolerance;
  2898. att.handle = target.handle;
  2899. atRotTargets.Add(idx, att);
  2900. }
  2901. }
  2902. }
  2903. }
  2904. if (atRotTargets.Count > 0)
  2905. {
  2906. SceneObjectPart[] parts = m_parts.GetArray();
  2907. uint[] localids = new uint[parts.Length];
  2908. for (int i = 0; i < parts.Length; i++)
  2909. localids[i] = parts[i].LocalId;
  2910. for (int ctr = 0; ctr < localids.Length; ctr++)
  2911. {
  2912. foreach (uint target in atRotTargets.Keys)
  2913. {
  2914. scriptRotTarget att = atRotTargets[target];
  2915. m_scene.EventManager.TriggerAtRotTargetEvent(
  2916. localids[ctr], att.handle, att.targetRot, m_rootPart.RotationOffset);
  2917. }
  2918. }
  2919. return;
  2920. }
  2921. if (m_scriptListens_notAtRotTarget && !at_Rottarget)
  2922. {
  2923. //trigger not_at_target
  2924. SceneObjectPart[] parts = m_parts.GetArray();
  2925. uint[] localids = new uint[parts.Length];
  2926. for (int i = 0; i < parts.Length; i++)
  2927. localids[i] = parts[i].LocalId;
  2928. for (int ctr = 0; ctr < localids.Length; ctr++)
  2929. {
  2930. m_scene.EventManager.TriggerNotAtRotTargetEvent(localids[ctr]);
  2931. }
  2932. }
  2933. }
  2934. }
  2935. }
  2936. public float GetMass()
  2937. {
  2938. float retmass = 0f;
  2939. SceneObjectPart[] parts = m_parts.GetArray();
  2940. for (int i = 0; i < parts.Length; i++)
  2941. retmass += parts[i].GetMass();
  2942. return retmass;
  2943. }
  2944. /// <summary>
  2945. /// If the object is a sculpt/mesh, retrieve the mesh data for each part and reinsert it into each shape so that
  2946. /// the physics engine can use it.
  2947. /// </summary>
  2948. /// <remarks>
  2949. /// When the physics engine has finished with it, the sculpt data is discarded to save memory.
  2950. /// </remarks>
  2951. /*
  2952. public void CheckSculptAndLoad()
  2953. {
  2954. if (IsDeleted)
  2955. return;
  2956. if ((RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Phantom) != 0)
  2957. return;
  2958. // m_log.Debug("Processing CheckSculptAndLoad for {0} {1}", Name, LocalId);
  2959. SceneObjectPart[] parts = m_parts.GetArray();
  2960. for (int i = 0; i < parts.Length; i++)
  2961. parts[i].CheckSculptAndLoad();
  2962. }
  2963. */
  2964. /// <summary>
  2965. /// Set the user group to which this scene object belongs.
  2966. /// </summary>
  2967. /// <param name="GroupID"></param>
  2968. /// <param name="client"></param>
  2969. public void SetGroup(UUID GroupID, IClientAPI client)
  2970. {
  2971. SceneObjectPart[] parts = m_parts.GetArray();
  2972. for (int i = 0; i < parts.Length; i++)
  2973. {
  2974. SceneObjectPart part = parts[i];
  2975. part.SetGroup(GroupID, client);
  2976. part.Inventory.ChangeInventoryGroup(GroupID);
  2977. }
  2978. HasGroupChanged = true;
  2979. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  2980. // for the same object with very different properties. The caller must schedule the update.
  2981. //ScheduleGroupForFullUpdate();
  2982. }
  2983. public void TriggerScriptChangedEvent(Changed val)
  2984. {
  2985. SceneObjectPart[] parts = m_parts.GetArray();
  2986. for (int i = 0; i < parts.Length; i++)
  2987. parts[i].TriggerScriptChangedEvent(val);
  2988. }
  2989. /// <summary>
  2990. /// Returns a count of the number of scripts in this groups parts.
  2991. /// </summary>
  2992. public int ScriptCount()
  2993. {
  2994. int count = 0;
  2995. SceneObjectPart[] parts = m_parts.GetArray();
  2996. for (int i = 0; i < parts.Length; i++)
  2997. count += parts[i].Inventory.ScriptCount();
  2998. return count;
  2999. }
  3000. /// <summary>
  3001. /// A float the value is a representative execution time in milliseconds of all scripts in the link set.
  3002. /// </summary>
  3003. public float ScriptExecutionTime()
  3004. {
  3005. IScriptModule[] engines = Scene.RequestModuleInterfaces<IScriptModule>();
  3006. if (engines.Length == 0) // No engine at all
  3007. return 0.0f;
  3008. float time = 0.0f;
  3009. // get all the scripts in all parts
  3010. SceneObjectPart[] parts = m_parts.GetArray();
  3011. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>();
  3012. for (int i = 0; i < parts.Length; i++)
  3013. {
  3014. scripts.AddRange(parts[i].Inventory.GetInventoryItems(InventoryType.LSL));
  3015. }
  3016. // extract the UUIDs
  3017. List<UUID> ids = new List<UUID>(scripts.Count);
  3018. foreach (TaskInventoryItem script in scripts)
  3019. {
  3020. if (!ids.Contains(script.ItemID))
  3021. {
  3022. ids.Add(script.ItemID);
  3023. }
  3024. }
  3025. // Offer the list of script UUIDs to each engine found and accumulate the time
  3026. foreach (IScriptModule e in engines)
  3027. {
  3028. if (e != null)
  3029. {
  3030. time += e.GetScriptExecutionTime(ids);
  3031. }
  3032. }
  3033. return time;
  3034. }
  3035. /// <summary>
  3036. /// Returns a count of the number of running scripts in this groups parts.
  3037. /// </summary>
  3038. public int RunningScriptCount()
  3039. {
  3040. int count = 0;
  3041. SceneObjectPart[] parts = m_parts.GetArray();
  3042. for (int i = 0; i < parts.Length; i++)
  3043. count += parts[i].Inventory.RunningScriptCount();
  3044. return count;
  3045. }
  3046. /// <summary>
  3047. /// Gets the number of sitting avatars.
  3048. /// </summary>
  3049. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  3050. /// <returns></returns>
  3051. public int GetSittingAvatarsCount()
  3052. {
  3053. int count = 0;
  3054. Array.ForEach<SceneObjectPart>(m_parts.GetArray(), p => count += p.GetSittingAvatarsCount());
  3055. return count;
  3056. }
  3057. public override string ToString()
  3058. {
  3059. return String.Format("{0} {1} ({2})", Name, UUID, AbsolutePosition);
  3060. }
  3061. #region ISceneObject
  3062. public virtual ISceneObject CloneForNewScene()
  3063. {
  3064. SceneObjectGroup sog = Copy(false);
  3065. sog.IsDeleted = false;
  3066. return sog;
  3067. }
  3068. public virtual string ToXml2()
  3069. {
  3070. return SceneObjectSerializer.ToXml2Format(this);
  3071. }
  3072. public virtual string ExtraToXmlString()
  3073. {
  3074. return "<ExtraFromItemID>" + FromItemID.ToString() + "</ExtraFromItemID>";
  3075. }
  3076. public virtual void ExtraFromXmlString(string xmlstr)
  3077. {
  3078. string id = xmlstr.Substring(xmlstr.IndexOf("<ExtraFromItemID>"));
  3079. id = xmlstr.Replace("<ExtraFromItemID>", "");
  3080. id = id.Replace("</ExtraFromItemID>", "");
  3081. UUID uuid = UUID.Zero;
  3082. UUID.TryParse(id, out uuid);
  3083. FromItemID = uuid;
  3084. }
  3085. #endregion
  3086. }
  3087. }