SceneObjectGroup.cs 146 KB

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