SceneObjectGroup.cs 149 KB

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