SceneObjectGroup.cs 193 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391
  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.Diagnostics;
  33. using System.Linq;
  34. using System.Threading;
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Packets;
  39. using OpenSim.Framework;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.PhysicsModules.SharedBase;
  42. using OpenSim.Region.Framework.Scenes.Serialization;
  43. using PermissionMask = OpenSim.Framework.PermissionMask;
  44. using OpenSim.Services.Interfaces;
  45. namespace OpenSim.Region.Framework.Scenes
  46. {
  47. [Flags]
  48. public enum scriptEvents
  49. {
  50. None = 0,
  51. attach = 1,
  52. collision = 16,
  53. collision_end = 32,
  54. collision_start = 64,
  55. control = 128,
  56. dataserver = 256,
  57. email = 512,
  58. http_response = 1024,
  59. land_collision = 2048,
  60. land_collision_end = 4096,
  61. land_collision_start = 8192,
  62. at_target = 16384,
  63. at_rot_target = 16777216,
  64. listen = 32768,
  65. money = 65536,
  66. moving_end = 131072,
  67. moving_start = 262144,
  68. not_at_rot_target = 524288,
  69. not_at_target = 1048576,
  70. remote_data = 8388608,
  71. run_time_permissions = 268435456,
  72. state_entry = 1073741824,
  73. state_exit = 2,
  74. timer = 4,
  75. touch = 8,
  76. touch_end = 536870912,
  77. touch_start = 2097152,
  78. transaction_result = 33554432,
  79. object_rez = 4194304,
  80. anytouch = touch | touch_end | touch_start
  81. }
  82. public struct scriptPosTarget
  83. {
  84. public Vector3 targetPos;
  85. public float tolerance;
  86. public uint handle;
  87. }
  88. public struct scriptRotTarget
  89. {
  90. public Quaternion targetRot;
  91. public float tolerance;
  92. public uint handle;
  93. }
  94. public delegate void PrimCountTaintedDelegate();
  95. /// <summary>
  96. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  97. /// (often known as prims), one of which is considered the root part.
  98. /// </summary>
  99. public partial class SceneObjectGroup : EntityBase, ISceneObject
  100. {
  101. // Axis selection bitmask used by SetAxisRotation()
  102. // Just happen to be the same bits used by llSetStatus() and defined in ScriptBaseClass.
  103. public enum axisSelect : int
  104. {
  105. STATUS_ROTATE_X = 0x002,
  106. STATUS_ROTATE_Y = 0x004,
  107. STATUS_ROTATE_Z = 0x008,
  108. NOT_STATUS_ROTATE_X = 0xFD,
  109. NOT_STATUS_ROTATE_Y = 0xFB,
  110. NOT_STATUS_ROTATE_Z = 0xF7
  111. }
  112. // private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  113. public bool IsViewerCachable
  114. {
  115. get
  116. {
  117. // needs more exclusion ?
  118. return(Backup && !IsTemporary && !inTransit && !IsSelected && !UsesPhysics && !IsAttachmentCheckFull() &&
  119. !RootPart.Shape.MeshFlagEntry && // animations are not sent correctly for now
  120. RootPart.KeyframeMotion == null &&
  121. (DateTime.UtcNow.Ticks - timeLastChanged > 36000000000) && //36000000000 is one hour
  122. RootPart.Velocity.LengthSquared() < 1e8f && // should not be needed
  123. RootPart.Acceleration.LengthSquared() < 1e4f // should not be needed
  124. );
  125. }
  126. }
  127. /// <summary>
  128. /// Signal whether the non-inventory attributes of any prims in the group have changed
  129. /// since the group's last persistent backup
  130. /// </summary>
  131. private bool m_hasGroupChanged = false;
  132. private long timeFirstChanged = 0;
  133. private long timeLastChanged = 0;
  134. private long m_maxPersistTime = 0;
  135. private long m_minPersistTime = 0;
  136. public int PseudoCRC;
  137. /// <summary>
  138. /// This indicates whether the object has changed such that it needs to be repersisted to permenant storage
  139. /// (the database).
  140. /// </summary>
  141. /// <remarks>
  142. /// Ultimately, this should be managed such that region modules can change it at the end of a set of operations
  143. /// so that either all changes are preserved or none at all. However, currently, a large amount of internal
  144. /// code will set this anyway when some object properties are changed.
  145. /// </remarks>
  146. public bool HasGroupChanged
  147. {
  148. set
  149. {
  150. if (value)
  151. {
  152. if (Backup)
  153. m_scene.SceneGraph.FireChangeBackup(this);
  154. PseudoCRC = (int)(DateTime.UtcNow.Ticks); ;
  155. timeLastChanged = DateTime.UtcNow.Ticks;
  156. if (!m_hasGroupChanged)
  157. timeFirstChanged = timeLastChanged;
  158. if (m_rootPart != null && m_scene != null)
  159. {
  160. if (m_scene.GetRootAgentCount() == 0)
  161. {
  162. //If the region is empty, this change has been made by an automated process
  163. //and thus we delay the persist time by a random amount between 1.5 and 2.5.
  164. float factor = 2.0f;
  165. m_maxPersistTime = (long)(m_scene.m_persistAfter * factor);
  166. m_minPersistTime = (long)(m_scene.m_dontPersistBefore * factor);
  167. }
  168. else
  169. {
  170. m_maxPersistTime = m_scene.m_persistAfter;
  171. m_minPersistTime = m_scene.m_dontPersistBefore;
  172. }
  173. }
  174. }
  175. m_hasGroupChanged = value;
  176. // m_log.DebugFormat(
  177. // "[SCENE OBJECT GROUP]: HasGroupChanged set to {0} for {1} {2}", m_hasGroupChanged, Name, LocalId);
  178. }
  179. get { return m_hasGroupChanged; }
  180. }
  181. private bool m_groupContainsForeignPrims = false;
  182. /// <summary>
  183. /// Whether the group contains prims that came from a different group. This happens when
  184. /// linking or delinking groups. The implication is that until the group is persisted,
  185. /// the prims in the database still use the old SceneGroupID. That's a problem if the group
  186. /// is deleted, because we delete groups by searching for prims by their SceneGroupID.
  187. /// </summary>
  188. public bool GroupContainsForeignPrims
  189. {
  190. private set
  191. {
  192. m_groupContainsForeignPrims = value;
  193. if (m_groupContainsForeignPrims)
  194. HasGroupChanged = true;
  195. }
  196. get { return m_groupContainsForeignPrims; }
  197. }
  198. public bool HasGroupChangedDueToDelink { get; set; }
  199. private bool isTimeToPersist()
  200. {
  201. if (IsSelected || IsDeleted || IsAttachment)
  202. return false;
  203. if (!m_hasGroupChanged)
  204. return false;
  205. if (m_scene.ShuttingDown)
  206. return true;
  207. if (m_minPersistTime == 0 || m_maxPersistTime == 0)
  208. {
  209. m_maxPersistTime = m_scene.m_persistAfter;
  210. m_minPersistTime = m_scene.m_dontPersistBefore;
  211. }
  212. long currentTime = DateTime.UtcNow.Ticks;
  213. if (timeLastChanged == 0) timeLastChanged = currentTime;
  214. if (timeFirstChanged == 0) timeFirstChanged = currentTime;
  215. if (currentTime - timeLastChanged > m_minPersistTime || currentTime - timeFirstChanged > m_maxPersistTime)
  216. return true;
  217. return false;
  218. }
  219. /// <summary>
  220. /// Is this scene object acting as an attachment?
  221. /// </summary>
  222. public bool IsAttachment { get; set; }
  223. /// <summary>
  224. /// The avatar to which this scene object is attached.
  225. /// </summary>
  226. /// <remarks>
  227. /// If we're not attached to an avatar then this is UUID.Zero
  228. /// </remarks>
  229. public UUID AttachedAvatar { get; set; }
  230. /// <summary>
  231. /// Attachment point of this scene object to an avatar.
  232. /// </summary>
  233. /// <remarks>
  234. /// 0 if we're not attached to anything
  235. /// </remarks>
  236. public uint AttachmentPoint
  237. {
  238. get
  239. {
  240. return m_rootPart.Shape.State;
  241. }
  242. set
  243. {
  244. IsAttachment = value != 0;
  245. m_rootPart.Shape.State = (byte)value;
  246. }
  247. }
  248. /// <summary>
  249. /// If this scene object has an attachment point then indicate whether there is a point where
  250. /// attachments are perceivable by avatars other than the avatar to which this object is attached.
  251. /// </summary>
  252. /// <remarks>
  253. /// HUDs are not perceivable by other avatars.
  254. /// </remarks>
  255. public bool HasPrivateAttachmentPoint
  256. {
  257. get
  258. {
  259. return AttachmentPoint >= (uint)OpenMetaverse.AttachmentPoint.HUDCenter2
  260. && AttachmentPoint <= (uint)OpenMetaverse.AttachmentPoint.HUDBottomRight;
  261. }
  262. }
  263. public void ClearPartAttachmentData()
  264. {
  265. AttachmentPoint = 0;
  266. // Don't zap trees
  267. if (RootPart.Shape.PCode == (byte)PCode.Tree ||
  268. RootPart.Shape.PCode == (byte)PCode.NewTree)
  269. return;
  270. // Even though we don't use child part state parameters for attachments any more, we still need to set
  271. // these to zero since having them non-zero in rezzed scene objects will crash some clients. Even if
  272. // we store them correctly, scene objects that we receive from elsewhere might not.
  273. foreach (SceneObjectPart part in Parts)
  274. part.Shape.State = 0;
  275. }
  276. /// <summary>
  277. /// Is this scene object phantom?
  278. /// </summary>
  279. /// <remarks>
  280. /// Updating must currently take place through UpdatePrimFlags()
  281. /// </remarks>
  282. public bool IsPhantom
  283. {
  284. get { return (RootPart.Flags & PrimFlags.Phantom) != 0; }
  285. }
  286. /// <summary>
  287. /// Does this scene object use physics?
  288. /// </summary>
  289. /// <remarks>
  290. /// Updating must currently take place through UpdatePrimFlags()
  291. /// </remarks>
  292. public bool UsesPhysics
  293. {
  294. get { return (RootPart.Flags & PrimFlags.Physics) != 0; }
  295. }
  296. /// <summary>
  297. /// Is this scene object temporary?
  298. /// </summary>
  299. /// <remarks>
  300. /// Updating must currently take place through UpdatePrimFlags()
  301. /// </remarks>
  302. public bool IsTemporary
  303. {
  304. get { return (RootPart.Flags & PrimFlags.TemporaryOnRez) != 0; }
  305. }
  306. public bool IsVolumeDetect
  307. {
  308. get { return RootPart.VolumeDetectActive; }
  309. }
  310. /// <summary>
  311. /// Is this entity set to be saved in persistent storage?
  312. /// </summary>
  313. public bool Backup { get; private set; }
  314. protected MapAndArray<UUID, SceneObjectPart> m_parts = new MapAndArray<UUID, SceneObjectPart>();
  315. protected ulong m_regionHandle;
  316. protected SceneObjectPart m_rootPart;
  317. // private Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  318. private SortedDictionary<uint, scriptPosTarget> m_targets = new SortedDictionary<uint, scriptPosTarget>();
  319. private SortedDictionary<uint, scriptRotTarget> m_rotTargets = new SortedDictionary<uint, scriptRotTarget>();
  320. public SortedDictionary<uint, scriptPosTarget> AtTargets
  321. {
  322. get { return m_targets; }
  323. }
  324. public SortedDictionary<uint, scriptRotTarget> RotTargets
  325. {
  326. get { return m_rotTargets; }
  327. }
  328. private bool m_scriptListens_atTarget;
  329. private bool m_scriptListens_notAtTarget;
  330. private bool m_scriptListens_atRotTarget;
  331. private bool m_scriptListens_notAtRotTarget;
  332. public bool m_dupeInProgress = false;
  333. internal Dictionary<UUID, string> m_savedScriptState;
  334. public UUID MonitoringObject { get; set; }
  335. #region Properties
  336. /// <summary>
  337. /// The name of an object grouping is always the same as its root part
  338. /// </summary>
  339. public override string Name
  340. {
  341. get { return RootPart.Name; }
  342. set { RootPart.Name = value; }
  343. }
  344. public string Description
  345. {
  346. get { return RootPart.Description; }
  347. set { RootPart.Description = value; }
  348. }
  349. /// <summary>
  350. /// Added because the Parcel code seems to use it
  351. /// but not sure a object should have this
  352. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  353. /// think really there should be a list (or whatever) in each scenepresence
  354. /// saying what prim(s) that user has selected.
  355. /// </summary>
  356. protected bool m_isSelected = false;
  357. /// <summary>
  358. /// Number of prims in this group
  359. /// </summary>
  360. public int PrimCount
  361. {
  362. get { return m_parts.Count; }
  363. }
  364. // protected Quaternion m_rotation = Quaternion.Identity;
  365. //
  366. // public virtual Quaternion Rotation
  367. // {
  368. // get { return m_rotation; }
  369. // set {
  370. // m_rotation = value;
  371. // }
  372. // }
  373. public Quaternion GroupRotation
  374. {
  375. get { return m_rootPart.RotationOffset; }
  376. }
  377. public Vector3 GroupScale
  378. {
  379. get
  380. {
  381. Vector3 finalScale = new Vector3();
  382. SceneObjectPart[] parts = m_parts.GetArray();
  383. SceneObjectPart part;
  384. Vector3 partscale;
  385. float ftmp;
  386. float minScaleX = float.MaxValue;
  387. float minScaleY = float.MaxValue;
  388. float minScaleZ = float.MaxValue;
  389. float maxScaleX = 0f;
  390. float maxScaleY = 0f;
  391. float maxScaleZ = 0f;
  392. for (int i = 0; i < parts.Length; i++)
  393. {
  394. part = parts[i];
  395. partscale = part.Scale + part.OffsetPosition;
  396. ftmp = partscale.X;
  397. if (ftmp < minScaleX)
  398. minScaleX = ftmp;
  399. if (ftmp > maxScaleX)
  400. maxScaleX = ftmp;
  401. ftmp = partscale.Y;
  402. if (ftmp < minScaleY)
  403. minScaleY = ftmp;
  404. if (ftmp > maxScaleY)
  405. maxScaleY = ftmp;
  406. ftmp = partscale.Z;
  407. if (ftmp < minScaleZ)
  408. minScaleZ = ftmp;
  409. if (ftmp > maxScaleZ)
  410. maxScaleZ = ftmp;
  411. }
  412. finalScale.X = (minScaleX > maxScaleX) ? minScaleX : maxScaleX;
  413. finalScale.Y = (minScaleY > maxScaleY) ? minScaleY : maxScaleY;
  414. finalScale.Z = (minScaleZ > maxScaleZ) ? minScaleZ : maxScaleZ;
  415. return finalScale;
  416. }
  417. }
  418. public UUID GroupID
  419. {
  420. get { return m_rootPart.GroupID; }
  421. set { m_rootPart.GroupID = value; }
  422. }
  423. public SceneObjectPart[] Parts
  424. {
  425. get { return m_parts.GetArray(); }
  426. }
  427. public bool ContainsPart(UUID partID)
  428. {
  429. return m_parts.ContainsKey(partID);
  430. }
  431. /// <summary>
  432. /// Does this group contain the given part?
  433. /// should be able to remove these methods once we have a entity index in scene
  434. /// </summary>
  435. /// <param name="localID"></param>
  436. /// <returns></returns>
  437. public bool ContainsPart(uint localID)
  438. {
  439. SceneObjectPart[] parts = m_parts.GetArray();
  440. for (int i = 0; i < parts.Length; i++)
  441. {
  442. if (parts[i].LocalId == localID)
  443. return true;
  444. }
  445. return false;
  446. }
  447. /// <value>
  448. /// The root part of this scene object
  449. /// </value>
  450. public SceneObjectPart RootPart
  451. {
  452. get { return m_rootPart; }
  453. }
  454. public ulong RegionHandle
  455. {
  456. get { return m_regionHandle; }
  457. set
  458. {
  459. m_regionHandle = value;
  460. SceneObjectPart[] parts = m_parts.GetArray();
  461. for (int i = 0; i < parts.Length; i++)
  462. parts[i].RegionHandle = value;
  463. }
  464. }
  465. /// <summary>
  466. /// Check both the attachment property and the relevant properties of the underlying root part.
  467. /// </summary>
  468. /// <remarks>
  469. /// This is necessary in some cases, particularly when a scene object has just crossed into a region and doesn't
  470. /// have the IsAttachment property yet checked.
  471. ///
  472. /// FIXME: However, this should be fixed so that this property
  473. /// propertly reflects the underlying status.
  474. /// </remarks>
  475. /// <returns></returns>
  476. public bool IsAttachmentCheckFull()
  477. {
  478. return (IsAttachment ||
  479. (m_rootPart.Shape.PCode == (byte)PCodeEnum.Primitive && m_rootPart.Shape.State != 0));
  480. }
  481. private struct avtocrossInfo
  482. {
  483. public ScenePresence av;
  484. public uint ParentID;
  485. }
  486. public bool inTransit = false;
  487. private delegate SceneObjectGroup SOGCrossDelegate(SceneObjectGroup sog,Vector3 pos, TeleportObjectData tpData);
  488. /// <summary>
  489. /// The absolute position of this scene object in the scene
  490. /// </summary>
  491. public override Vector3 AbsolutePosition
  492. {
  493. get { return m_rootPart.GroupPosition; }
  494. set
  495. {
  496. Vector3 val = value;
  497. if (Scene != null
  498. && !Scene.PositionIsInCurrentRegion(val)
  499. && !IsAttachmentCheckFull()
  500. && !Scene.LoadingPrims
  501. && !Scene.DisableObjectTransfer
  502. )
  503. {
  504. if (!inTransit)
  505. {
  506. inTransit = true;
  507. SOGCrossDelegate d = CrossAsync;
  508. d.BeginInvoke(this, val, null, CrossAsyncCompleted, d);
  509. }
  510. return;
  511. }
  512. if (RootPart.GetStatusSandbox())
  513. {
  514. if (Vector3.DistanceSquared(RootPart.StatusSandboxPos, value) > 100)
  515. {
  516. RootPart.ScriptSetPhysicsStatus(false);
  517. if (Scene != null)
  518. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  519. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  520. return;
  521. }
  522. }
  523. bool triggerScriptEvent = m_rootPart.GroupPosition != val;
  524. if (m_dupeInProgress || IsDeleted)
  525. triggerScriptEvent = false;
  526. m_rootPart.GroupPosition = val;
  527. // Restuff the new GroupPosition into each child SOP of the linkset.
  528. // this is needed because physics may not have linksets but just loose SOPs in world
  529. SceneObjectPart[] parts = m_parts.GetArray();
  530. foreach (SceneObjectPart part in parts)
  531. {
  532. if (part != m_rootPart)
  533. part.GroupPosition = val;
  534. }
  535. foreach (ScenePresence av in m_sittingAvatars)
  536. {
  537. av.sitSOGmoved();
  538. }
  539. // now that position is changed tell it to scripts
  540. if (triggerScriptEvent)
  541. {
  542. foreach (SceneObjectPart part in parts)
  543. {
  544. part.TriggerScriptChangedEvent(Changed.POSITION);
  545. }
  546. }
  547. if (Scene != null)
  548. Scene.EventManager.TriggerParcelPrimCountTainted();
  549. }
  550. }
  551. private SceneObjectGroup CrossAsync(SceneObjectGroup sog, Vector3 val, TeleportObjectData tpdata)
  552. {
  553. Scene sogScene = sog.m_scene;
  554. SceneObjectPart root = sog.RootPart;
  555. bool isTeleport = tpdata != null;
  556. if(!isTeleport)
  557. {
  558. if (root.DIE_AT_EDGE)
  559. {
  560. try
  561. {
  562. sogScene.DeleteSceneObject(sog, false);
  563. }
  564. catch (Exception)
  565. {
  566. m_log.Warn("[SCENE]: exception when trying to remove the prim that crossed the border.");
  567. }
  568. return sog;
  569. }
  570. if (root.RETURN_AT_EDGE)
  571. {
  572. // We remove the object here
  573. try
  574. {
  575. List<uint> localIDs = new List<uint>();
  576. localIDs.Add(root.LocalId);
  577. sogScene.AddReturn(sog.OwnerID, sog.Name, sog.AbsolutePosition,
  578. "Returned at region cross");
  579. sogScene.DeRezObjects(null, localIDs, UUID.Zero, DeRezAction.Return, UUID.Zero, false);
  580. }
  581. catch (Exception)
  582. {
  583. m_log.Warn("[SCENE]: exception when trying to return the prim that crossed the border.");
  584. }
  585. return sog;
  586. }
  587. }
  588. // if(!m_scene.IsRunning)
  589. // return sog;
  590. if (root.KeyframeMotion != null)
  591. root.KeyframeMotion.StartCrossingCheck();
  592. if(root.PhysActor != null)
  593. root.PhysActor.CrossingStart();
  594. IEntityTransferModule entityTransfer = sogScene.RequestModuleInterface<IEntityTransferModule>();
  595. if (entityTransfer == null)
  596. return sog;
  597. Vector3 newpos = Vector3.Zero;
  598. OpenSim.Services.Interfaces.GridRegion destination = null;
  599. destination = entityTransfer.GetObjectDestination(sog, val, out newpos);
  600. if (destination == null)
  601. return sog;
  602. if (sog.m_sittingAvatars.Count == 0)
  603. {
  604. entityTransfer.CrossPrimGroupIntoNewRegion(destination, newpos, sog, !isTeleport, true);
  605. return sog;
  606. }
  607. string reason = String.Empty;
  608. EntityTransferContext ctx = new EntityTransferContext();
  609. Vector3 curPos = root.GroupPosition;
  610. foreach (ScenePresence av in sog.m_sittingAvatars)
  611. {
  612. // We need to cross these agents. First, let's find
  613. // out if any of them can't cross for some reason.
  614. // We have to deny the crossing entirely if any
  615. // of them are banned. Alternatively, we could
  616. // unsit banned agents....
  617. // We set the avatar position as being the object
  618. // position to get the region to send to
  619. if(av.IsNPC)
  620. continue;
  621. if(av.IsInTransit)
  622. return sog;
  623. if(!entityTransfer.checkAgentAccessToRegion(av, destination, newpos, ctx, out reason))
  624. return sog;
  625. m_log.DebugFormat("[SCENE OBJECT]: Avatar {0} needs to be crossed to {1}", av.Name, destination.RegionName);
  626. }
  627. // We unparent the SP quietly so that it won't
  628. // be made to stand up
  629. List<avtocrossInfo> avsToCross = new List<avtocrossInfo>();
  630. List<ScenePresence> avsToCrossFar = new List<ScenePresence>();
  631. ulong destHandle = destination.RegionHandle;
  632. List<ScenePresence> sittingAvatars = GetSittingAvatars();
  633. foreach (ScenePresence av in sittingAvatars)
  634. {
  635. byte cflags = 1;
  636. avtocrossInfo avinfo = new avtocrossInfo();
  637. SceneObjectPart parentPart = sogScene.GetSceneObjectPart(av.ParentID);
  638. if (parentPart != null)
  639. {
  640. av.ParentUUID = parentPart.UUID;
  641. if(parentPart.SitTargetAvatar == av.UUID)
  642. cflags = 7; // low 3 bits set
  643. else
  644. cflags = 3;
  645. }
  646. if(!av.knowsNeighbourRegion(destHandle))
  647. cflags |= 8;
  648. // 1 is crossing
  649. // 2 is sitting
  650. // 4 is sitting at sittarget
  651. // 8 far crossing
  652. avinfo.av = av;
  653. avinfo.ParentID = av.ParentID;
  654. avsToCross.Add(avinfo);
  655. if(!av.knowsNeighbourRegion(destHandle))
  656. {
  657. cflags |= 8;
  658. avsToCrossFar.Add(av);
  659. }
  660. if(av.IsNPC)
  661. av.m_crossingFlags = 0;
  662. else
  663. av.m_crossingFlags = cflags;
  664. av.PrevSitOffset = av.OffsetPosition;
  665. av.ParentID = 0;
  666. }
  667. Vector3 vel = root.Velocity;
  668. Vector3 avel = root.AngularVelocity;
  669. Vector3 acc = root.Acceleration;
  670. Quaternion ori = root.RotationOffset;
  671. if(isTeleport)
  672. {
  673. root.Stop();
  674. sogScene.ForEachScenePresence(delegate(ScenePresence av)
  675. {
  676. av.ControllingClient.SendEntityUpdate(root,PrimUpdateFlags.SendInTransit);
  677. av.ControllingClient.SendEntityTerseUpdateImmediate(root);
  678. });
  679. root.Velocity = tpdata.vel;
  680. root.AngularVelocity = tpdata.avel;
  681. root.Acceleration = tpdata.acc;
  682. root.RotationOffset = tpdata.ori;
  683. }
  684. if (entityTransfer.CrossPrimGroupIntoNewRegion(destination, newpos, sog, true, false))
  685. {
  686. if(isTeleport)
  687. {
  688. sogScene.ForEachScenePresence(delegate(ScenePresence oav)
  689. {
  690. if(sittingAvatars.Contains(oav))
  691. return;
  692. if(oav.knowsNeighbourRegion(destHandle))
  693. return;
  694. oav.ControllingClient.SendEntityUpdate(root, PrimUpdateFlags.Kill);
  695. foreach (ScenePresence sav in sittingAvatars)
  696. {
  697. sav.SendKillTo(oav);
  698. }
  699. });
  700. }
  701. bool crossedfar = false;
  702. foreach (ScenePresence av in avsToCrossFar)
  703. {
  704. if(entityTransfer.CrossAgentCreateFarChild(av,destination, newpos, ctx))
  705. crossedfar = true;
  706. else
  707. av.m_crossingFlags = 0;
  708. }
  709. if(crossedfar)
  710. Thread.Sleep(1000);
  711. foreach (avtocrossInfo avinfo in avsToCross)
  712. {
  713. ScenePresence av = avinfo.av;
  714. av.IsInLocalTransit = true;
  715. av.IsInTransit = true;
  716. m_log.DebugFormat("[SCENE OBJECT]: Crossing avatar {0} to {1}", av.Name, val);
  717. if(av.m_crossingFlags > 0)
  718. entityTransfer.CrossAgentToNewRegionAsync(av, newpos, destination, false, ctx);
  719. if (av.IsChildAgent)
  720. {
  721. // avatar crossed do some extra cleanup
  722. if (av.ParentUUID != UUID.Zero)
  723. {
  724. av.ClearControls();
  725. av.ParentPart = null;
  726. }
  727. av.ParentUUID = UUID.Zero;
  728. av.ParentPart = null;
  729. // In any case
  730. av.IsInTransit = false;
  731. av.m_crossingFlags = 0;
  732. m_log.DebugFormat("[SCENE OBJECT]: Crossing agent {0} {1} completed.", av.Firstname, av.Lastname);
  733. }
  734. else
  735. {
  736. // avatar cross failed we need do dedicated standUp
  737. // part of it was done at CrossAgentToNewRegionAsync
  738. // so for now just remove the sog controls
  739. // this may need extra care
  740. av.UnRegisterSeatControls(sog.UUID);
  741. av.ParentUUID = UUID.Zero;
  742. av.ParentPart = null;
  743. Vector3 oldp = curPos;
  744. oldp.X = Util.Clamp<float>(oldp.X, 0.5f, sog.m_scene.RegionInfo.RegionSizeX - 0.5f);
  745. oldp.Y = Util.Clamp<float>(oldp.Y, 0.5f, sog.m_scene.RegionInfo.RegionSizeY - 0.5f);
  746. av.AbsolutePosition = oldp;
  747. av.m_crossingFlags = 0;
  748. av.sitAnimation = "SIT";
  749. av.IsInTransit = false;
  750. if(av.Animator!= null)
  751. av.Animator.SetMovementAnimations("STAND");
  752. av.AddToPhysicalScene(false);
  753. sogScene.ForEachScenePresence(delegate(ScenePresence oav)
  754. {
  755. if(sittingAvatars.Contains(oav))
  756. return;
  757. if(oav.knowsNeighbourRegion(destHandle))
  758. av.SendAvatarDataToAgent(oav);
  759. else
  760. {
  761. av.SendAvatarDataToAgent(oav);
  762. av.SendAppearanceToAgent(oav);
  763. if (av.Animator != null)
  764. av.Animator.SendAnimPackToClient(oav.ControllingClient);
  765. av.SendAttachmentsToAgentNF(oav); // not ok
  766. }
  767. });
  768. m_log.DebugFormat("[SCENE OBJECT]: Crossing agent {0} {1} failed.", av.Firstname, av.Lastname);
  769. }
  770. }
  771. if(crossedfar)
  772. {
  773. Thread.Sleep(10000);
  774. foreach (ScenePresence av in avsToCrossFar)
  775. {
  776. if(av.IsChildAgent)
  777. {
  778. av.Scene.CloseAgent(av.UUID, false);
  779. }
  780. else
  781. av.RemoveNeighbourRegion(destHandle);
  782. }
  783. }
  784. avsToCrossFar.Clear();
  785. avsToCross.Clear();
  786. sog.RemoveScriptInstances(true);
  787. sog.Clear();
  788. return sog;
  789. }
  790. else
  791. {
  792. if(isTeleport)
  793. {
  794. if((tpdata.flags & OSTPOBJ_STOPONFAIL) == 0)
  795. {
  796. root.Velocity = vel;
  797. root.AngularVelocity = avel;
  798. root.Acceleration = acc;
  799. }
  800. root.RotationOffset = ori;
  801. }
  802. foreach (avtocrossInfo avinfo in avsToCross)
  803. {
  804. ScenePresence av = avinfo.av;
  805. av.ParentUUID = UUID.Zero;
  806. av.ParentID = avinfo.ParentID;
  807. av.m_crossingFlags = 0;
  808. }
  809. }
  810. avsToCross.Clear();
  811. return sog;
  812. }
  813. public void CrossAsyncCompleted(IAsyncResult iar)
  814. {
  815. SOGCrossDelegate icon = (SOGCrossDelegate)iar.AsyncState;
  816. SceneObjectGroup sog = icon.EndInvoke(iar);
  817. if (!sog.IsDeleted)
  818. {
  819. SceneObjectPart rootp = sog.m_rootPart;
  820. Vector3 oldp = rootp.GroupPosition;
  821. oldp.X = Util.Clamp<float>(oldp.X, 0.5f, sog.m_scene.RegionInfo.RegionSizeX - 0.5f);
  822. oldp.Y = Util.Clamp<float>(oldp.Y, 0.5f, sog.m_scene.RegionInfo.RegionSizeY - 0.5f);
  823. rootp.GroupPosition = oldp;
  824. rootp.Stop();
  825. SceneObjectPart[] parts = sog.m_parts.GetArray();
  826. foreach (SceneObjectPart part in parts)
  827. {
  828. if (part != rootp)
  829. part.GroupPosition = oldp;
  830. }
  831. foreach (ScenePresence av in sog.m_sittingAvatars)
  832. {
  833. av.sitSOGmoved();
  834. }
  835. if (sog.m_rootPart.KeyframeMotion != null)
  836. sog.m_rootPart.KeyframeMotion.CrossingFailure();
  837. if (sog.RootPart.PhysActor != null)
  838. sog.RootPart.PhysActor.CrossingFailure();
  839. sog.inTransit = false;
  840. AttachToBackup();
  841. sog.ScheduleGroupForFullAnimUpdate();
  842. }
  843. }
  844. private class TeleportObjectData
  845. {
  846. public int flags;
  847. public Vector3 vel;
  848. public Vector3 avel;
  849. public Vector3 acc;
  850. public Quaternion ori;
  851. public UUID sourceID;
  852. }
  853. // copy from LSL_constants.cs
  854. const int OSTPOBJ_STOPATTARGET = 0x1; // stops at destination
  855. const int OSTPOBJ_STOPONFAIL = 0x2; // stops at start if tp fails
  856. const int OSTPOBJ_SETROT = 0x4; // the rotation is the final rotation, otherwise is a added rotation
  857. public int TeleportObject(UUID sourceID, Vector3 targetPosition, Quaternion rotation, int flags)
  858. {
  859. if(inTransit || IsDeleted || IsAttachmentCheckFull() || IsSelected || Scene == null)
  860. return -1;
  861. inTransit = true;
  862. PhysicsActor pa = RootPart.PhysActor;
  863. if(pa == null || RootPart.KeyframeMotion != null /*|| m_sittingAvatars.Count == 0*/)
  864. {
  865. inTransit = false;
  866. return -1;
  867. }
  868. bool stop = (flags & OSTPOBJ_STOPATTARGET) != 0;
  869. bool setrot = (flags & OSTPOBJ_SETROT) != 0;
  870. rotation.Normalize();
  871. Quaternion currentRot = RootPart.RotationOffset;
  872. if(setrot)
  873. rotation = Quaternion.Conjugate(currentRot) * rotation;
  874. bool dorot = setrot | (Math.Abs(rotation.W) < 0.99999);
  875. Vector3 vel = Vector3.Zero;
  876. Vector3 avel = Vector3.Zero;
  877. Vector3 acc = Vector3.Zero;
  878. if(!stop)
  879. {
  880. vel = RootPart.Velocity;
  881. avel = RootPart.AngularVelocity;
  882. acc = RootPart.Acceleration;
  883. }
  884. Quaternion ori = RootPart.RotationOffset;
  885. if(dorot)
  886. {
  887. if(!stop)
  888. {
  889. vel *= rotation;
  890. avel *= rotation;
  891. acc *= rotation;
  892. }
  893. ori *= rotation;
  894. }
  895. if(Scene.PositionIsInCurrentRegion(targetPosition))
  896. {
  897. if(Scene.InTeleportTargetsCoolDown(UUID, sourceID, 1.0))
  898. {
  899. inTransit = false;
  900. return -2;
  901. }
  902. Vector3 curPos = AbsolutePosition;
  903. ILandObject curLand = Scene.LandChannel.GetLandObject(curPos.X, curPos.Y);
  904. float posX = targetPosition.X;
  905. float posY = targetPosition.Y;
  906. ILandObject land = Scene.LandChannel.GetLandObject(posX, posY);
  907. if(land != null && land != curLand)
  908. {
  909. if(!Scene.Permissions.CanObjectEnterWithScripts(this, land))
  910. {
  911. inTransit = false;
  912. return -3;
  913. }
  914. UUID agentID;
  915. foreach (ScenePresence av in m_sittingAvatars)
  916. {
  917. agentID = av.UUID;
  918. if(land.IsRestrictedFromLand(agentID) || land.IsBannedFromLand(agentID))
  919. {
  920. inTransit = false;
  921. return -4;
  922. }
  923. }
  924. }
  925. RootPart.Velocity = vel;
  926. RootPart.AngularVelocity = avel;
  927. RootPart.Acceleration = acc;
  928. RootPart.RotationOffset = ori;
  929. Vector3 s = RootPart.Scale * RootPart.RotationOffset;
  930. float h = Scene.GetGroundHeight(posX, posY) + 0.5f * (float)Math.Abs(s.Z) + 0.01f;
  931. if(targetPosition.Z < h)
  932. targetPosition.Z = h;
  933. inTransit = false;
  934. AbsolutePosition = targetPosition;
  935. RootPart.ScheduleTerseUpdate();
  936. return 1;
  937. }
  938. if(Scene.InTeleportTargetsCoolDown(UUID, sourceID, 20.0))
  939. {
  940. inTransit = false;
  941. return -1;
  942. }
  943. TeleportObjectData tdata = new TeleportObjectData();
  944. tdata.flags = flags;
  945. tdata.vel = vel;
  946. tdata.avel = avel;
  947. tdata.acc = acc;
  948. tdata.ori = ori;
  949. tdata.sourceID = sourceID;
  950. SOGCrossDelegate d = CrossAsync;
  951. d.BeginInvoke(this, targetPosition, tdata, CrossAsyncCompleted, d);
  952. return 0;
  953. }
  954. public override Vector3 Velocity
  955. {
  956. get { return RootPart.Velocity; }
  957. set { RootPart.Velocity = value; }
  958. }
  959. public override uint LocalId
  960. {
  961. get { return m_rootPart.LocalId; }
  962. set { m_rootPart.LocalId = value; }
  963. }
  964. public override UUID UUID
  965. {
  966. get { return m_rootPart.UUID; }
  967. set
  968. {
  969. lock (m_parts.SyncRoot)
  970. {
  971. m_parts.Remove(m_rootPart.UUID);
  972. m_rootPart.UUID = value;
  973. m_parts.Add(value, m_rootPart);
  974. }
  975. }
  976. }
  977. public UUID LastOwnerID
  978. {
  979. get { return m_rootPart.LastOwnerID; }
  980. set { m_rootPart.LastOwnerID = value; }
  981. }
  982. public UUID RezzerID
  983. {
  984. get { return m_rootPart.RezzerID; }
  985. set { m_rootPart.RezzerID = value; }
  986. }
  987. public UUID OwnerID
  988. {
  989. get { return m_rootPart.OwnerID; }
  990. set { m_rootPart.OwnerID = value; }
  991. }
  992. public float Damage
  993. {
  994. get { return m_rootPart.Damage; }
  995. set { m_rootPart.Damage = value; }
  996. }
  997. public Color Color
  998. {
  999. get { return m_rootPart.Color; }
  1000. set { m_rootPart.Color = value; }
  1001. }
  1002. public string Text
  1003. {
  1004. get {
  1005. string returnstr = m_rootPart.Text;
  1006. if (returnstr.Length > 255)
  1007. {
  1008. returnstr = returnstr.Substring(0, 255);
  1009. }
  1010. return returnstr;
  1011. }
  1012. set { m_rootPart.Text = value; }
  1013. }
  1014. /// <summary>
  1015. /// If set to true then the scene object can be backed up in principle, though this will only actually occur
  1016. /// if Backup is set. If false then the scene object will never be backed up, Backup will always be false.
  1017. /// </summary>
  1018. protected virtual bool CanBeBackedUp
  1019. {
  1020. get { return true; }
  1021. }
  1022. public bool IsSelected
  1023. {
  1024. get { return m_isSelected; }
  1025. set
  1026. {
  1027. m_isSelected = value;
  1028. // Tell physics engine that group is selected
  1029. // this is not right
  1030. // but ode engines should only really need to know about root part
  1031. // so they can put entire object simulation on hold and not colliding
  1032. // keep as was for now
  1033. PhysicsActor pa = m_rootPart.PhysActor;
  1034. if (pa != null)
  1035. {
  1036. pa.Selected = value;
  1037. // Pass it on to the children.
  1038. SceneObjectPart[] parts = m_parts.GetArray();
  1039. for (int i = 0; i < parts.Length; i++)
  1040. {
  1041. SceneObjectPart child = parts[i];
  1042. PhysicsActor childPa = child.PhysActor;
  1043. if (childPa != null)
  1044. childPa.Selected = value;
  1045. }
  1046. }
  1047. if (RootPart.KeyframeMotion != null)
  1048. RootPart.KeyframeMotion.Selected = value;
  1049. }
  1050. }
  1051. public void PartSelectChanged(bool partSelect)
  1052. {
  1053. // any part selected makes group selected
  1054. if (m_isSelected == partSelect)
  1055. return;
  1056. if (partSelect)
  1057. {
  1058. IsSelected = partSelect;
  1059. // if (!IsAttachment)
  1060. // ScheduleGroupForFullUpdate();
  1061. }
  1062. else
  1063. {
  1064. // bad bad bad 2 heavy for large linksets
  1065. // since viewer does send lot of (un)selects
  1066. // this needs to be replaced by a specific list or count ?
  1067. // but that will require extra code in several places
  1068. SceneObjectPart[] parts = m_parts.GetArray();
  1069. for (int i = 0; i < parts.Length; i++)
  1070. {
  1071. SceneObjectPart part = parts[i];
  1072. if (part.IsSelected)
  1073. return;
  1074. }
  1075. IsSelected = partSelect;
  1076. // if (!IsAttachment)
  1077. // {
  1078. // ScheduleGroupForFullUpdate();
  1079. // }
  1080. }
  1081. }
  1082. private double m_lastCollisionSoundMS;
  1083. /// <summary>
  1084. /// The UUID for the region this object is in.
  1085. /// </summary>
  1086. public UUID RegionUUID
  1087. {
  1088. get
  1089. {
  1090. if (m_scene != null)
  1091. {
  1092. return m_scene.RegionInfo.RegionID;
  1093. }
  1094. return UUID.Zero;
  1095. }
  1096. }
  1097. /// <summary>
  1098. /// The item ID that this object was rezzed from, if applicable.
  1099. /// </summary>
  1100. /// <remarks>
  1101. /// If not applicable will be UUID.Zero
  1102. /// </remarks>
  1103. public UUID FromItemID { get; set; }
  1104. /// <summary>
  1105. /// Refers to the SceneObjectPart.UUID property of the object that this object was rezzed from, if applicable.
  1106. /// </summary>
  1107. /// <remarks>
  1108. /// If not applicable will be UUID.Zero
  1109. /// </remarks>
  1110. /// obsolete use RezzerID
  1111. public UUID FromPartID
  1112. {
  1113. get { return RezzerID; }
  1114. set {RezzerID = value; }
  1115. }
  1116. /// <summary>
  1117. /// The folder ID that this object was rezzed from, if applicable.
  1118. /// </summary>
  1119. /// <remarks>
  1120. /// If not applicable will be UUID.Zero
  1121. /// </remarks>
  1122. public UUID FromFolderID { get; set; }
  1123. /// <summary>
  1124. /// If true then grabs are blocked no matter what the individual part BlockGrab setting.
  1125. /// </summary>
  1126. /// <value><c>true</c> if block grab override; otherwise, <c>false</c>.</value>
  1127. public bool BlockGrabOverride { get; set; }
  1128. /// <summary>
  1129. /// IDs of all avatars sat on this scene object.
  1130. /// </summary>
  1131. /// <remarks>
  1132. /// We need this so that we can maintain a linkset wide ordering of avatars sat on different parts.
  1133. /// This must be locked before it is read or written.
  1134. /// SceneObjectPart sitting avatar add/remove code also locks on this object to avoid race conditions.
  1135. /// No avatar should appear more than once in this list.
  1136. /// Do not manipulate this list directly - use the Add/Remove sitting avatar methods on SceneObjectPart.
  1137. /// </remarks>
  1138. protected internal List<ScenePresence> m_sittingAvatars = new List<ScenePresence>();
  1139. #endregion
  1140. // ~SceneObjectGroup()
  1141. // {
  1142. // //m_log.DebugFormat("[SCENE OBJECT GROUP]: Destructor called for {0}, local id {1}", Name, LocalId);
  1143. // Console.WriteLine("Destructor called for {0}, local id {1}", Name, LocalId);
  1144. // }
  1145. #region Constructors
  1146. /// <summary>
  1147. /// Constructor
  1148. /// </summary>
  1149. public SceneObjectGroup()
  1150. {
  1151. m_lastCollisionSoundMS = Util.GetTimeStampMS() + 1000.0;
  1152. PseudoCRC = (int)(DateTime.UtcNow.Ticks);
  1153. }
  1154. /// <summary>
  1155. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  1156. /// The original SceneObjectPart will be used rather than a copy, preserving
  1157. /// its existing localID and UUID.
  1158. /// </summary>
  1159. /// <param name='part'>Root part for this scene object.</param>
  1160. public SceneObjectGroup(SceneObjectPart part) : this()
  1161. {
  1162. SetRootPart(part);
  1163. }
  1164. /// <summary>
  1165. /// Constructor. This object is added to the scene later via AttachToScene()
  1166. /// </summary>
  1167. public SceneObjectGroup(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  1168. {
  1169. SetRootPart(new SceneObjectPart(ownerID, shape, pos, rot, Vector3.Zero));
  1170. }
  1171. /// <summary>
  1172. /// Constructor.
  1173. /// </summary>
  1174. public SceneObjectGroup(UUID ownerID, Vector3 pos, PrimitiveBaseShape shape)
  1175. : this(ownerID, pos, Quaternion.Identity, shape)
  1176. {
  1177. }
  1178. public void LoadScriptState(XmlDocument doc)
  1179. {
  1180. XmlNodeList nodes = doc.GetElementsByTagName("SavedScriptState");
  1181. if (nodes.Count > 0)
  1182. {
  1183. if (m_savedScriptState == null)
  1184. m_savedScriptState = new Dictionary<UUID, string>();
  1185. foreach (XmlNode node in nodes)
  1186. {
  1187. if (node.Attributes["UUID"] != null)
  1188. {
  1189. UUID itemid = new UUID(node.Attributes["UUID"].Value);
  1190. if (itemid != UUID.Zero)
  1191. m_savedScriptState[itemid] = node.InnerXml;
  1192. }
  1193. }
  1194. }
  1195. }
  1196. public void LoadScriptState(XmlReader reader)
  1197. {
  1198. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Looking for script state for {0}", Name);
  1199. while (true)
  1200. {
  1201. if (reader.Name == "SavedScriptState" && reader.NodeType == XmlNodeType.Element)
  1202. {
  1203. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Loading script state for {0}", Name);
  1204. if (m_savedScriptState == null)
  1205. m_savedScriptState = new Dictionary<UUID, string>();
  1206. string uuid = reader.GetAttribute("UUID");
  1207. // Even if there is no UUID attribute for some strange reason, we must always read the inner XML
  1208. // so we don't continually keep checking the same SavedScriptedState element.
  1209. string innerXml = reader.ReadInnerXml();
  1210. if (uuid != null)
  1211. {
  1212. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Found state for item ID {0} in object {1}", uuid, Name);
  1213. UUID itemid = new UUID(uuid);
  1214. if (itemid != UUID.Zero)
  1215. m_savedScriptState[itemid] = innerXml;
  1216. }
  1217. else
  1218. {
  1219. m_log.WarnFormat("[SCENE OBJECT GROUP]: SavedScriptState element had no UUID in object {0} id: {1}", Name,UUID);
  1220. }
  1221. }
  1222. else
  1223. {
  1224. if (!reader.Read())
  1225. break;
  1226. }
  1227. }
  1228. }
  1229. /// <summary>
  1230. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  1231. /// </summary>
  1232. public virtual void AttachToBackup()
  1233. {
  1234. if (IsAttachment) return;
  1235. m_scene.SceneGraph.FireAttachToBackup(this);
  1236. // if (InSceneBackup)
  1237. {
  1238. // m_log.DebugFormat(
  1239. // "[SCENE OBJECT GROUP]: Attaching object {0} {1} to scene presistence sweep", Name, UUID);
  1240. if (!Backup)
  1241. m_scene.EventManager.OnBackup += ProcessBackup;
  1242. Backup = true;
  1243. }
  1244. }
  1245. /// <summary>
  1246. /// Attach this object to a scene. It will also now appear to agents.
  1247. /// </summary>
  1248. /// <param name="scene"></param>
  1249. public void AttachToScene(Scene scene)
  1250. {
  1251. m_scene = scene;
  1252. RegionHandle = m_scene.RegionInfo.RegionHandle;
  1253. if (m_rootPart.Shape.PCode != 9 || m_rootPart.Shape.State == 0)
  1254. m_rootPart.ParentID = 0;
  1255. if (m_rootPart.LocalId == 0)
  1256. m_rootPart.LocalId = m_scene.AllocateLocalId();
  1257. SceneObjectPart[] parts = m_parts.GetArray();
  1258. SceneObjectPart part;
  1259. for (int i = 0; i < parts.Length; i++)
  1260. {
  1261. part = parts[i];
  1262. if (part.KeyframeMotion != null)
  1263. {
  1264. part.KeyframeMotion.UpdateSceneObject(this);
  1265. }
  1266. if (Object.ReferenceEquals(part, m_rootPart))
  1267. continue;
  1268. if (part.LocalId == 0)
  1269. part.LocalId = m_scene.AllocateLocalId();
  1270. part.ParentID = m_rootPart.LocalId;
  1271. //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);
  1272. }
  1273. ApplyPhysics();
  1274. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  1275. // for the same object with very different properties. The caller must schedule the update.
  1276. //ScheduleGroupForFullUpdate();
  1277. }
  1278. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  1279. {
  1280. // We got a request from the inner_scene to raytrace along the Ray hRay
  1281. // We're going to check all of the prim in this group for intersection with the ray
  1282. // If we get a result, we're going to find the closest result to the origin of the ray
  1283. // and send back the intersection information back to the innerscene.
  1284. EntityIntersection result = new EntityIntersection();
  1285. SceneObjectPart[] parts = m_parts.GetArray();
  1286. // Find closest hit here
  1287. float idist = float.MaxValue;
  1288. for (int i = 0; i < parts.Length; i++)
  1289. {
  1290. SceneObjectPart part = parts[i];
  1291. // Temporary commented to stop compiler warning
  1292. //Vector3 partPosition =
  1293. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  1294. Quaternion parentrotation = GroupRotation;
  1295. // Telling the prim to raytrace.
  1296. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  1297. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation, frontFacesOnly, faceCenters);
  1298. if (inter.HitTF)
  1299. {
  1300. // We need to find the closest prim to return to the testcaller along the ray
  1301. if (inter.distance < idist)
  1302. {
  1303. result.HitTF = true;
  1304. result.ipoint = inter.ipoint;
  1305. result.obj = part;
  1306. result.normal = inter.normal;
  1307. result.distance = inter.distance;
  1308. idist = inter.distance;
  1309. }
  1310. }
  1311. }
  1312. return result;
  1313. }
  1314. public void GetBoundingBox(out float minX, out float maxX, out float minY, out float maxY, out float minZ, out float maxZ)
  1315. {
  1316. uint rootid = RootPart.LocalId;
  1317. Vector3 scale = RootPart.Scale * 0.5f;
  1318. minX = -scale.X;
  1319. maxX = scale.X;
  1320. minY = -scale.Y;
  1321. maxY = scale.Y;
  1322. minZ = -scale.Z;
  1323. maxZ = scale.Z;
  1324. SceneObjectPart[] parts = m_parts.GetArray();
  1325. SceneObjectPart part;
  1326. for (int i = 0; i < parts.Length; ++i)
  1327. {
  1328. part = parts[i];
  1329. if(part.LocalId == rootid)
  1330. continue;
  1331. Vector3 offset = part.OffsetPosition;
  1332. scale = part.Scale * 0.5f;
  1333. Matrix4 m = Matrix4.CreateFromQuaternion(part.RotationOffset);
  1334. Vector3 a = m.AtAxis;
  1335. a.X = Math.Abs(a.X);
  1336. a.Y = Math.Abs(a.Y);
  1337. a.Z = Math.Abs(a.Z);
  1338. float tmpS = Vector3.Dot(a, scale);
  1339. float tmp = offset.X - tmpS;
  1340. if (tmp < minX)
  1341. minX = tmp;
  1342. tmp = offset.X + tmpS;
  1343. if (tmp > maxX)
  1344. maxX = tmp;
  1345. a = m.LeftAxis;
  1346. a.X = Math.Abs(a.X);
  1347. a.Y = Math.Abs(a.Y);
  1348. a.Z = Math.Abs(a.Z);
  1349. tmpS = Vector3.Dot(a, scale);
  1350. tmp = offset.Y - tmpS;
  1351. if (tmp < minY)
  1352. minY = tmp;
  1353. tmp = offset.Y + tmpS;
  1354. if (tmp > maxY)
  1355. maxY = tmp;
  1356. a = m.UpAxis;
  1357. a.X = Math.Abs(a.X);
  1358. a.Y = Math.Abs(a.Y);
  1359. a.Z = Math.Abs(a.Z);
  1360. tmpS = Vector3.Dot(a, scale);
  1361. tmp = offset.Z - tmpS;
  1362. if (tmp < minZ)
  1363. minZ = tmp;
  1364. tmp = offset.Z + tmpS;
  1365. if (tmp > maxZ)
  1366. maxZ = tmp;
  1367. }
  1368. }
  1369. /// <summary>
  1370. /// Gets a vector representing the size of the bounding box containing all the prims in the group
  1371. /// Treats all prims as rectangular, so no shape (cut etc) is taken into account
  1372. /// offsetHeight is the offset in the Z axis from the centre of the bounding box to the centre of the root prim
  1373. /// </summary>
  1374. /// <returns></returns>
  1375. public void GetAxisAlignedBoundingBoxRaw(out float minX, out float maxX, out float minY, out float maxY, out float minZ, out float maxZ)
  1376. {
  1377. maxX = float.MinValue;
  1378. maxY = float.MinValue;
  1379. maxZ = float.MinValue;
  1380. minX = float.MaxValue;
  1381. minY = float.MaxValue;
  1382. minZ = float.MaxValue;
  1383. Vector3 absPos = AbsolutePosition;
  1384. SceneObjectPart[] parts = m_parts.GetArray();
  1385. SceneObjectPart part;
  1386. for(int i = 0; i< parts.Length; ++i)
  1387. {
  1388. part = parts[i];
  1389. Vector3 offset = part.GetWorldPosition() - absPos;
  1390. Vector3 scale = part.Scale * 0.5f;
  1391. Matrix4 m = Matrix4.CreateFromQuaternion(part.GetWorldRotation());
  1392. Vector3 a = m.AtAxis;
  1393. a.X = Math.Abs(a.X);
  1394. a.Y = Math.Abs(a.Y);
  1395. a.Z = Math.Abs(a.Z);
  1396. float tmpS = Vector3.Dot(a, scale);
  1397. float tmp = offset.X - tmpS;
  1398. if (tmp < minX)
  1399. minX = tmp;
  1400. tmp = offset.X + tmpS;
  1401. if (tmp > maxX)
  1402. maxX = tmp;
  1403. a = m.LeftAxis;
  1404. a.X = Math.Abs(a.X);
  1405. a.Y = Math.Abs(a.Y);
  1406. a.Z = Math.Abs(a.Z);
  1407. tmpS = Vector3.Dot(a, scale);
  1408. tmp = offset.Y - tmpS;
  1409. if (tmp < minY)
  1410. minY = tmp;
  1411. tmp = offset.Y + tmpS;
  1412. if (tmp > maxY)
  1413. maxY = tmp;
  1414. a = m.UpAxis;
  1415. a.X = Math.Abs(a.X);
  1416. a.Y = Math.Abs(a.Y);
  1417. a.Z = Math.Abs(a.Z);
  1418. tmpS = Vector3.Dot(a, scale);
  1419. tmp = offset.Z - tmpS;
  1420. if (tmp < minZ)
  1421. minZ = tmp;
  1422. tmp = offset.Z + tmpS;
  1423. if (tmp > maxZ)
  1424. maxZ = tmp;
  1425. }
  1426. }
  1427. public Vector3 GetAxisAlignedBoundingBox(out float offsetHeight)
  1428. {
  1429. float minX;
  1430. float maxX;
  1431. float minY;
  1432. float maxY;
  1433. float minZ;
  1434. float maxZ;
  1435. GetAxisAlignedBoundingBoxRaw(out minX, out maxX, out minY, out maxY, out minZ, out maxZ);
  1436. Vector3 boundingBox = new Vector3(maxX - minX, maxY - minY, maxZ - minZ);
  1437. offsetHeight = 0;
  1438. float lower = (minZ * -1);
  1439. if (lower > maxZ)
  1440. {
  1441. offsetHeight = lower - (boundingBox.Z / 2);
  1442. }
  1443. else if (maxZ > lower)
  1444. {
  1445. offsetHeight = maxZ - (boundingBox.Z / 2);
  1446. offsetHeight *= -1;
  1447. }
  1448. // m_log.InfoFormat("BoundingBox is {0} , {1} , {2} ", boundingBox.X, boundingBox.Y, boundingBox.Z);
  1449. return boundingBox;
  1450. }
  1451. #endregion
  1452. private float? m_boundsRadius = null;
  1453. public void InvalidBoundsRadius()
  1454. {
  1455. m_boundsRadius = null;
  1456. }
  1457. private Vector3 m_boundsCenter;
  1458. private Vector3 m_LastCenterOffset;
  1459. private Vector3 last_boundsRot = new Vector3(-10, -10, -10);
  1460. public Vector3 getCenterOffset()
  1461. {
  1462. // math is done in GetBoundsRadius();
  1463. if(m_boundsRadius == null)
  1464. GetBoundsRadius();
  1465. Quaternion rot = m_rootPart.RotationOffset;
  1466. if (last_boundsRot.X != rot.X ||
  1467. last_boundsRot.Y != rot.Y ||
  1468. last_boundsRot.Z != rot.Z)
  1469. {
  1470. m_LastCenterOffset = m_boundsCenter * rot;
  1471. last_boundsRot.X = rot.X;
  1472. last_boundsRot.Y = rot.Y;
  1473. last_boundsRot.Z = rot.Z;
  1474. }
  1475. return m_rootPart.GroupPosition + m_LastCenterOffset;
  1476. }
  1477. private float m_areaFactor;
  1478. public float getAreaFactor()
  1479. {
  1480. // math is done in GetBoundsRadius();
  1481. if(m_boundsRadius == null)
  1482. GetBoundsRadius();
  1483. return m_areaFactor;
  1484. }
  1485. public float GetBoundsRadius()
  1486. {
  1487. // this may need more threading work
  1488. if(m_boundsRadius == null)
  1489. {
  1490. float res = 0;
  1491. float areaF = 0;
  1492. float partR;
  1493. Vector3 offset = Vector3.Zero;
  1494. SceneObjectPart p;
  1495. SceneObjectPart[] parts = m_parts.GetArray();
  1496. int nparts = parts.Length;
  1497. for (int i = 0; i < nparts; i++)
  1498. {
  1499. p = parts[i];
  1500. partR = 0.5f * p.Scale.Length();
  1501. if(p != RootPart)
  1502. {
  1503. partR += p.OffsetPosition.Length();
  1504. offset += p.OffsetPosition;
  1505. }
  1506. if(partR > res)
  1507. res = partR;
  1508. if(p.maxSimpleArea() > areaF)
  1509. areaF = p.maxSimpleArea();
  1510. }
  1511. if(parts.Length > 1)
  1512. {
  1513. offset /= parts.Length; // basicly geometric center
  1514. }
  1515. areaF = 0.5f / areaF; // scale it
  1516. areaF = Util.Clamp(areaF, 0.05f, 100f); // clamp it
  1517. m_areaFactor = (float)Math.Sqrt(areaF);
  1518. m_boundsCenter = offset;
  1519. m_boundsRadius = res;
  1520. return res;
  1521. }
  1522. return m_boundsRadius.Value;
  1523. }
  1524. public void GetResourcesCosts(SceneObjectPart apart,
  1525. out float linksetResCost, out float linksetPhysCost, out float partCost, out float partPhysCost)
  1526. {
  1527. // this information may need to be cached
  1528. float cost;
  1529. float tmpcost;
  1530. bool ComplexCost = false;
  1531. SceneObjectPart p;
  1532. SceneObjectPart[] parts = m_parts.GetArray();
  1533. int nparts = parts.Length;
  1534. for (int i = 0; i < nparts; i++)
  1535. {
  1536. p = parts[i];
  1537. if (p.UsesComplexCost)
  1538. {
  1539. ComplexCost = true;
  1540. break;
  1541. }
  1542. }
  1543. if (ComplexCost)
  1544. {
  1545. linksetResCost = 0;
  1546. linksetPhysCost = 0;
  1547. partCost = 0;
  1548. partPhysCost = 0;
  1549. for (int i = 0; i < nparts; i++)
  1550. {
  1551. p = parts[i];
  1552. cost = p.StreamingCost;
  1553. tmpcost = p.SimulationCost;
  1554. if (tmpcost > cost)
  1555. cost = tmpcost;
  1556. tmpcost = p.PhysicsCost;
  1557. if (tmpcost > cost)
  1558. cost = tmpcost;
  1559. linksetPhysCost += tmpcost;
  1560. linksetResCost += cost;
  1561. if (p == apart)
  1562. {
  1563. partCost = cost;
  1564. partPhysCost = tmpcost;
  1565. }
  1566. }
  1567. }
  1568. else
  1569. {
  1570. partPhysCost = 1.0f;
  1571. partCost = 1.0f;
  1572. linksetResCost = (float)nparts;
  1573. linksetPhysCost = linksetResCost;
  1574. }
  1575. }
  1576. public void GetSelectedCosts(out float PhysCost, out float StreamCost, out float SimulCost)
  1577. {
  1578. SceneObjectPart p;
  1579. SceneObjectPart[] parts = m_parts.GetArray();
  1580. int nparts = parts.Length;
  1581. PhysCost = 0;
  1582. StreamCost = 0;
  1583. SimulCost = 0;
  1584. for (int i = 0; i < nparts; i++)
  1585. {
  1586. p = parts[i];
  1587. StreamCost += p.StreamingCost;
  1588. SimulCost += p.SimulationCost;
  1589. PhysCost += p.PhysicsCost;
  1590. }
  1591. }
  1592. public void SaveScriptedState(XmlTextWriter writer)
  1593. {
  1594. SaveScriptedState(writer, false);
  1595. }
  1596. public void SaveScriptedState(XmlTextWriter writer, bool oldIDs)
  1597. {
  1598. XmlDocument doc = new XmlDocument();
  1599. Dictionary<UUID,string> states = new Dictionary<UUID,string>();
  1600. SceneObjectPart[] parts = m_parts.GetArray();
  1601. for (int i = 0; i < parts.Length; i++)
  1602. {
  1603. Dictionary<UUID, string> pstates = parts[i].Inventory.GetScriptStates(oldIDs);
  1604. foreach (KeyValuePair<UUID, string> kvp in pstates)
  1605. states[kvp.Key] = kvp.Value;
  1606. }
  1607. if (states.Count > 0)
  1608. {
  1609. // Now generate the necessary XML wrappings
  1610. writer.WriteStartElement(String.Empty, "GroupScriptStates", String.Empty);
  1611. foreach (UUID itemid in states.Keys)
  1612. {
  1613. doc.LoadXml(states[itemid]);
  1614. writer.WriteStartElement(String.Empty, "SavedScriptState", String.Empty);
  1615. writer.WriteAttributeString(String.Empty, "UUID", String.Empty, itemid.ToString());
  1616. writer.WriteRaw(doc.DocumentElement.OuterXml); // Writes ScriptState element
  1617. writer.WriteEndElement(); // End of SavedScriptState
  1618. }
  1619. writer.WriteEndElement(); // End of GroupScriptStates
  1620. }
  1621. }
  1622. public byte GetAttachmentPoint()
  1623. {
  1624. return m_rootPart.Shape.State;
  1625. }
  1626. public void DetachToGround()
  1627. {
  1628. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1629. if (avatar == null)
  1630. return;
  1631. m_rootPart.Shape.LastAttachPoint = m_rootPart.Shape.State;
  1632. m_rootPart.AttachedPos = m_rootPart.OffsetPosition;
  1633. avatar.RemoveAttachment(this);
  1634. Vector3 detachedpos = new Vector3(127f,127f,127f);
  1635. if (avatar == null)
  1636. return;
  1637. detachedpos = avatar.AbsolutePosition;
  1638. FromItemID = UUID.Zero;
  1639. AbsolutePosition = detachedpos;
  1640. AttachedAvatar = UUID.Zero;
  1641. //SceneObjectPart[] parts = m_parts.GetArray();
  1642. //for (int i = 0; i < parts.Length; i++)
  1643. // parts[i].AttachedAvatar = UUID.Zero;
  1644. m_rootPart.SetParentLocalId(0);
  1645. AttachmentPoint = (byte)0;
  1646. // must check if buildind should be true or false here
  1647. // m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive,false);
  1648. ApplyPhysics();
  1649. HasGroupChanged = true;
  1650. RootPart.Rezzed = DateTime.Now;
  1651. RootPart.RemFlag(PrimFlags.TemporaryOnRez);
  1652. AttachToBackup();
  1653. m_scene.EventManager.TriggerParcelPrimCountTainted();
  1654. m_rootPart.ScheduleFullUpdate();
  1655. m_rootPart.ClearUndoState();
  1656. }
  1657. public void DetachToInventoryPrep()
  1658. {
  1659. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1660. //Vector3 detachedpos = new Vector3(127f, 127f, 127f);
  1661. if (avatar != null)
  1662. {
  1663. //detachedpos = avatar.AbsolutePosition;
  1664. avatar.RemoveAttachment(this);
  1665. }
  1666. AttachedAvatar = UUID.Zero;
  1667. /*SceneObjectPart[] parts = m_parts.GetArray();
  1668. for (int i = 0; i < parts.Length; i++)
  1669. parts[i].AttachedAvatar = UUID.Zero;*/
  1670. m_rootPart.SetParentLocalId(0);
  1671. //m_rootPart.SetAttachmentPoint((byte)0);
  1672. IsAttachment = false;
  1673. AbsolutePosition = m_rootPart.AttachedPos;
  1674. //m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  1675. //AttachToBackup();
  1676. //m_rootPart.ScheduleFullUpdate();
  1677. }
  1678. /// <summary>
  1679. ///
  1680. /// </summary>
  1681. /// <param name="part"></param>
  1682. private void SetPartAsNonRoot(SceneObjectPart part)
  1683. {
  1684. part.ParentID = m_rootPart.LocalId;
  1685. part.ClearUndoState();
  1686. }
  1687. /// <summary>
  1688. /// Set a part to act as the root part for this scene object
  1689. /// </summary>
  1690. /// <param name="part"></param>
  1691. public void SetRootPart(SceneObjectPart part)
  1692. {
  1693. if (part == null)
  1694. throw new ArgumentNullException("Cannot give SceneObjectGroup a null root SceneObjectPart");
  1695. part.SetParent(this);
  1696. m_rootPart = part;
  1697. if (!IsAttachment)
  1698. part.ParentID = 0;
  1699. part.LinkNum = 0;
  1700. m_parts.Add(m_rootPart.UUID, m_rootPart);
  1701. }
  1702. /// <summary>
  1703. /// Add a new part to this scene object. The part must already be correctly configured.
  1704. /// </summary>
  1705. /// <param name="part"></param>
  1706. public void AddPart(SceneObjectPart part)
  1707. {
  1708. part.SetParent(this);
  1709. m_parts.Add(part.UUID, part);
  1710. part.LinkNum = m_parts.Count;
  1711. if (part.LinkNum == 2)
  1712. RootPart.LinkNum = 1;
  1713. InvalidatePartsLinkMaps();
  1714. }
  1715. /// <summary>
  1716. /// Make sure that every non root part has the proper parent root part local id
  1717. /// </summary>
  1718. private void UpdateParentIDs()
  1719. {
  1720. SceneObjectPart[] parts = m_parts.GetArray();
  1721. for (int i = 0; i < parts.Length; i++)
  1722. {
  1723. SceneObjectPart part = parts[i];
  1724. if (part.UUID != m_rootPart.UUID)
  1725. part.ParentID = m_rootPart.LocalId;
  1726. }
  1727. }
  1728. public void RegenerateFullIDs()
  1729. {
  1730. SceneObjectPart[] parts = m_parts.GetArray();
  1731. for (int i = 0; i < parts.Length; i++)
  1732. parts[i].UUID = UUID.Random();
  1733. }
  1734. // helper provided for parts.
  1735. public int GetSceneMaxUndo()
  1736. {
  1737. if (m_scene != null)
  1738. return m_scene.MaxUndoCount;
  1739. return 5;
  1740. }
  1741. // justincc: I don't believe this hack is needed any longer, especially since the physics
  1742. // parts of set AbsolutePosition were already commented out. By changing HasGroupChanged to false
  1743. // this method was preventing proper reload of scene objects.
  1744. // dahlia: I had to uncomment it, without it meshing was failing on some prims and objects
  1745. // at region startup
  1746. // teravus: After this was removed from the linking algorithm, Linked prims no longer collided
  1747. // properly when non-physical if they havn't been moved. This breaks ALL builds.
  1748. // see: http://opensimulator.org/mantis/view.php?id=3108
  1749. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  1750. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  1751. // unmoved prims! As soon as you move a Prim/group, it will collide properly because Absolute
  1752. // Position has been set!
  1753. public void ResetChildPrimPhysicsPositions()
  1754. {
  1755. // Setting this SOG's absolute position also loops through and sets the positions
  1756. // of the SOP's in this SOG's linkset. This has the side affect of making sure
  1757. // the physics world matches the simulated world.
  1758. // AbsolutePosition = AbsolutePosition; // could someone in the know please explain how this works?
  1759. // teravus: AbsolutePosition is NOT a normal property!
  1760. // the code in the getter of AbsolutePosition is significantly different then the code in the setter!
  1761. // jhurliman: Then why is it a property instead of two methods?
  1762. // do only what is supposed to do
  1763. Vector3 groupPosition = m_rootPart.GroupPosition;
  1764. SceneObjectPart[] parts = m_parts.GetArray();
  1765. foreach (SceneObjectPart part in parts)
  1766. {
  1767. if (part != m_rootPart)
  1768. part.GroupPosition = groupPosition;
  1769. }
  1770. }
  1771. public UUID GetPartsFullID(uint localID)
  1772. {
  1773. SceneObjectPart part = GetPart(localID);
  1774. if (part != null)
  1775. {
  1776. return part.UUID;
  1777. }
  1778. return UUID.Zero;
  1779. }
  1780. public void ObjectGrabHandler(uint localId, Vector3 offsetPos, IClientAPI remoteClient)
  1781. {
  1782. if (m_rootPart.LocalId == localId)
  1783. {
  1784. if((RootPart.ScriptEvents & scriptEvents.anytouch) != 0)
  1785. lastTouchTime = Util.GetTimeStampMS();
  1786. OnGrabGroup(offsetPos, remoteClient);
  1787. }
  1788. else
  1789. {
  1790. SceneObjectPart part = GetPart(localId);
  1791. if (((part.ScriptEvents & scriptEvents.anytouch) != 0) || (RootPart.ScriptEvents & scriptEvents.anytouch) != 0)
  1792. lastTouchTime = Util.GetTimeStampMS();
  1793. OnGrabPart(part, offsetPos, remoteClient);
  1794. }
  1795. }
  1796. public virtual void OnGrabPart(SceneObjectPart part, Vector3 offsetPos, IClientAPI remoteClient)
  1797. {
  1798. // m_log.DebugFormat(
  1799. // "[SCENE OBJECT GROUP]: Processing OnGrabPart for {0} on {1} {2}, offsetPos {3}",
  1800. // remoteClient.Name, part.Name, part.LocalId, offsetPos);
  1801. // part.StoreUndoState();
  1802. part.OnGrab(offsetPos, remoteClient);
  1803. }
  1804. public virtual void OnGrabGroup(Vector3 offsetPos, IClientAPI remoteClient)
  1805. {
  1806. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  1807. }
  1808. /// <summary>
  1809. /// Delete this group from its scene.
  1810. /// </summary>
  1811. /// <remarks>
  1812. /// This only handles the in-world consequences of deletion (e.g. any avatars sitting on it are forcibly stood
  1813. /// up and all avatars receive notification of its removal. Removal of the scene object from database backup
  1814. /// must be handled by the caller.
  1815. /// </remarks>
  1816. /// <param name="silent">If true then deletion is not broadcast to clients</param>
  1817. public void DeleteGroupFromScene(bool silent)
  1818. {
  1819. // We need to keep track of this state in case this group is still queued for backup.
  1820. IsDeleted = true;
  1821. DetachFromBackup();
  1822. if(Scene == null) // should not happen unless restart/shutdown ?
  1823. return;
  1824. SceneObjectPart[] parts = m_parts.GetArray();
  1825. for (int i = 0; i < parts.Length; i++)
  1826. {
  1827. SceneObjectPart part = parts[i];
  1828. Scene.ForEachScenePresence(delegate(ScenePresence avatar)
  1829. {
  1830. if (!avatar.IsChildAgent && avatar.ParentID == part.LocalId && avatar.ParentUUID == UUID.Zero)
  1831. avatar.StandUp();
  1832. if (!silent)
  1833. {
  1834. part.ClearUpdateSchedule();
  1835. if (part == m_rootPart)
  1836. {
  1837. if (!IsAttachment
  1838. || AttachedAvatar == avatar.ControllingClient.AgentId
  1839. || !HasPrivateAttachmentPoint)
  1840. {
  1841. // Send a kill object immediately
  1842. avatar.ControllingClient.SendKillObject(new List<uint> { part.LocalId });
  1843. //direct enqueue another delayed kill
  1844. avatar.ControllingClient.SendEntityUpdate(part,PrimUpdateFlags.Kill);
  1845. }
  1846. }
  1847. }
  1848. });
  1849. }
  1850. }
  1851. public void AddScriptLPS(int count)
  1852. {
  1853. m_scene.SceneGraph.AddToScriptLPS(count);
  1854. }
  1855. public void AddActiveScriptCount(int count)
  1856. {
  1857. SceneGraph d = m_scene.SceneGraph;
  1858. d.AddActiveScripts(count);
  1859. }
  1860. private const scriptEvents PhysicsNeeedSubsEvents = (
  1861. scriptEvents.collision | scriptEvents.collision_start | scriptEvents.collision_end |
  1862. scriptEvents.land_collision | scriptEvents.land_collision_start | scriptEvents.land_collision_end);
  1863. private scriptEvents lastRootPartPhysEvents = 0;
  1864. public void aggregateScriptEvents()
  1865. {
  1866. PrimFlags objectflagupdate = (PrimFlags)RootPart.GetEffectiveObjectFlags();
  1867. scriptEvents aggregateScriptEvents = 0;
  1868. SceneObjectPart[] parts = m_parts.GetArray();
  1869. for (int i = 0; i < parts.Length; i++)
  1870. {
  1871. SceneObjectPart part = parts[i];
  1872. if (part == null)
  1873. continue;
  1874. if (part != RootPart)
  1875. part.Flags = objectflagupdate;
  1876. aggregateScriptEvents |= part.AggregateScriptEvents;
  1877. }
  1878. m_scriptListens_atTarget = ((aggregateScriptEvents & scriptEvents.at_target) != 0);
  1879. m_scriptListens_notAtTarget = ((aggregateScriptEvents & scriptEvents.not_at_target) != 0);
  1880. if (!m_scriptListens_atTarget && !m_scriptListens_notAtTarget)
  1881. {
  1882. lock (m_targets)
  1883. m_targets.Clear();
  1884. m_scene.RemoveGroupTarget(this);
  1885. }
  1886. m_scriptListens_atRotTarget = ((aggregateScriptEvents & scriptEvents.at_rot_target) != 0);
  1887. m_scriptListens_notAtRotTarget = ((aggregateScriptEvents & scriptEvents.not_at_rot_target) != 0);
  1888. if (!m_scriptListens_atRotTarget && !m_scriptListens_notAtRotTarget)
  1889. {
  1890. lock (m_rotTargets)
  1891. m_rotTargets.Clear();
  1892. m_scene.RemoveGroupTarget(this);
  1893. }
  1894. scriptEvents rootPartPhysEvents = RootPart.AggregateScriptEvents;
  1895. rootPartPhysEvents &= PhysicsNeeedSubsEvents;
  1896. if (rootPartPhysEvents != lastRootPartPhysEvents)
  1897. {
  1898. lastRootPartPhysEvents = rootPartPhysEvents;
  1899. for (int i = 0; i < parts.Length; i++)
  1900. {
  1901. SceneObjectPart part = parts[i];
  1902. if (part == null)
  1903. continue;
  1904. part.UpdatePhysicsSubscribedEvents();
  1905. }
  1906. }
  1907. ScheduleGroupForFullUpdate();
  1908. }
  1909. public void SetText(string text, Vector3 color, double alpha)
  1910. {
  1911. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  1912. (int) (color.X * 0xff),
  1913. (int) (color.Y * 0xff),
  1914. (int) (color.Z * 0xff));
  1915. Text = text;
  1916. HasGroupChanged = true;
  1917. m_rootPart.ScheduleFullUpdate();
  1918. }
  1919. /// <summary>
  1920. /// Apply physics to this group
  1921. /// </summary>
  1922. public void ApplyPhysics()
  1923. {
  1924. SceneObjectPart[] parts = m_parts.GetArray();
  1925. if (parts.Length > 1)
  1926. {
  1927. ResetChildPrimPhysicsPositions();
  1928. // Apply physics to the root prim
  1929. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive, true);
  1930. for (int i = 0; i < parts.Length; i++)
  1931. {
  1932. SceneObjectPart part = parts[i];
  1933. if (part.LocalId != m_rootPart.LocalId)
  1934. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), part.VolumeDetectActive, true);
  1935. }
  1936. // Hack to get the physics scene geometries in the right spot
  1937. // ResetChildPrimPhysicsPositions();
  1938. if (m_rootPart.PhysActor != null)
  1939. m_rootPart.PhysActor.Building = false;
  1940. }
  1941. else
  1942. {
  1943. // Apply physics to the root prim
  1944. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive, false);
  1945. }
  1946. }
  1947. public void SetOwnerId(UUID userId)
  1948. {
  1949. ForEachPart(delegate(SceneObjectPart part)
  1950. {
  1951. if (part.OwnerID != userId)
  1952. {
  1953. if(part.GroupID != part.OwnerID)
  1954. part.LastOwnerID = part.OwnerID;
  1955. part.OwnerID = userId;
  1956. }
  1957. });
  1958. }
  1959. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  1960. {
  1961. SceneObjectPart[] parts = m_parts.GetArray();
  1962. for (int i = 0; i < parts.Length; i++)
  1963. whatToDo(parts[i]);
  1964. }
  1965. #region Events
  1966. /// <summary>
  1967. /// Processes backup.
  1968. /// </summary>
  1969. /// <param name="datastore"></param>
  1970. public virtual void ProcessBackup(ISimulationDataService datastore, bool forcedBackup)
  1971. {
  1972. if (!Backup)
  1973. {
  1974. // m_log.DebugFormat(
  1975. // "[WATER WARS]: Ignoring backup of {0} {1} since object is not marked to be backed up", Name, UUID);
  1976. return;
  1977. }
  1978. if (IsDeleted || inTransit || UUID == UUID.Zero)
  1979. {
  1980. // m_log.DebugFormat(
  1981. // "[WATER WARS]: Ignoring backup of {0} {1} since object is marked as already deleted", Name, UUID);
  1982. return;
  1983. }
  1984. if ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)
  1985. return;
  1986. // Since this is the top of the section of call stack for backing up a particular scene object, don't let
  1987. // any exception propogate upwards.
  1988. try
  1989. {
  1990. // if shutting down then there will be nothing to handle the return so leave till next restart
  1991. if (!m_scene.ShuttingDown &&
  1992. m_scene.LoginsEnabled && // We're starting up or doing maintenance, don't mess with things
  1993. !m_scene.LoadingPrims) // Land may not be valid yet
  1994. {
  1995. ILandObject parcel = m_scene.LandChannel.GetLandObject(
  1996. m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y);
  1997. if (parcel != null && parcel.LandData != null &&
  1998. parcel.LandData.OtherCleanTime != 0)
  1999. {
  2000. if (parcel.LandData.OwnerID != OwnerID &&
  2001. (parcel.LandData.GroupID != GroupID ||
  2002. parcel.LandData.GroupID == UUID.Zero))
  2003. {
  2004. if ((DateTime.UtcNow - RootPart.Rezzed).TotalMinutes >
  2005. parcel.LandData.OtherCleanTime)
  2006. {
  2007. // don't autoreturn if we have a sitting avatar
  2008. // mantis 7828 (but none the provided patchs)
  2009. if(GetSittingAvatarsCount() > 0)
  2010. {
  2011. // do not respect npcs
  2012. List<ScenePresence> sitters = GetSittingAvatars();
  2013. foreach(ScenePresence sp in sitters)
  2014. {
  2015. if(!sp.IsDeleted && !sp.IsNPC && sp.IsSatOnObject)
  2016. return;
  2017. }
  2018. }
  2019. DetachFromBackup();
  2020. m_log.DebugFormat(
  2021. "[SCENE OBJECT GROUP]: Returning object {0} due to parcel autoreturn",
  2022. RootPart.UUID);
  2023. m_scene.AddReturn(OwnerID == GroupID ? LastOwnerID : OwnerID, Name, AbsolutePosition, "parcel autoreturn");
  2024. m_scene.DeRezObjects(null, new List<uint>() { RootPart.LocalId }, UUID.Zero,
  2025. DeRezAction.Return, UUID.Zero, false);
  2026. return;
  2027. }
  2028. }
  2029. }
  2030. }
  2031. if (m_scene.UseBackup && HasGroupChanged)
  2032. {
  2033. // don't backup while it's selected or you're asking for changes mid stream.
  2034. if (isTimeToPersist() || forcedBackup)
  2035. {
  2036. if (RootPart.Shape.PCode == 9 && RootPart.Shape.State != 0)
  2037. {
  2038. RootPart.Shape.LastAttachPoint = RootPart.Shape.State;
  2039. RootPart.Shape.State = 0;
  2040. ScheduleGroupForFullUpdate();
  2041. }
  2042. SceneObjectGroup backup_group = Copy(false);
  2043. backup_group.RootPart.Velocity = RootPart.Velocity;
  2044. backup_group.RootPart.Acceleration = RootPart.Acceleration;
  2045. backup_group.RootPart.AngularVelocity = RootPart.AngularVelocity;
  2046. HasGroupChanged = false;
  2047. GroupContainsForeignPrims = false;
  2048. m_scene.EventManager.TriggerOnSceneObjectPreSave(backup_group, this);
  2049. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  2050. backup_group.ForEachPart(delegate(SceneObjectPart part)
  2051. {
  2052. part.Inventory.ProcessInventoryBackup(datastore);
  2053. if(part.KeyframeMotion != null)
  2054. {
  2055. part.KeyframeMotion.Delete();
  2056. part.KeyframeMotion = null;
  2057. }
  2058. });
  2059. backup_group.Clear();
  2060. backup_group = null;
  2061. }
  2062. }
  2063. }
  2064. catch (Exception e)
  2065. {
  2066. m_log.ErrorFormat(
  2067. "[SCENE]: Storing of {0}, {1} in {2} failed with exception {3}{4}",
  2068. Name, UUID, m_scene.RegionInfo.RegionName, e.Message, e.StackTrace);
  2069. }
  2070. }
  2071. #endregion
  2072. /// <summary>
  2073. /// Send the parts of this SOG to a single client
  2074. /// </summary>
  2075. /// <remarks>
  2076. /// Used when the client initially connects and when client sends RequestPrim packet
  2077. /// </remarks>
  2078. /// <param name="remoteClient"></param>
  2079. public void SendFullAnimUpdateToClient(IClientAPI remoteClient)
  2080. {
  2081. PrimUpdateFlags update = PrimUpdateFlags.FullUpdate;
  2082. if (RootPart.Shape.MeshFlagEntry)
  2083. update = PrimUpdateFlags.FullUpdatewithAnim;
  2084. RootPart.SendUpdate(remoteClient, update);
  2085. SceneObjectPart[] parts = m_parts.GetArray();
  2086. for (int i = 0; i < parts.Length; i++)
  2087. {
  2088. SceneObjectPart part = parts[i];
  2089. if (part != RootPart)
  2090. part.SendUpdate(remoteClient, update);
  2091. }
  2092. }
  2093. public void SendUpdateProbes(IClientAPI remoteClient)
  2094. {
  2095. PrimUpdateFlags update = PrimUpdateFlags.UpdateProbe;
  2096. RootPart.SendUpdate(remoteClient, update);
  2097. SceneObjectPart[] parts = m_parts.GetArray();
  2098. for (int i = 0; i < parts.Length; i++)
  2099. {
  2100. SceneObjectPart part = parts[i];
  2101. if (part != RootPart)
  2102. part.SendUpdate(remoteClient, update);
  2103. }
  2104. }
  2105. #region Copying
  2106. /// <summary>
  2107. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  2108. /// </summary>
  2109. /// <param name="userExposed">True if the duplicate will immediately be in the scene, false otherwise</param>
  2110. /// <returns></returns>
  2111. public SceneObjectGroup Copy(bool userExposed)
  2112. {
  2113. m_dupeInProgress = true;
  2114. SceneObjectGroup dupe = (SceneObjectGroup)MemberwiseClone();
  2115. dupe.m_parts = new MapAndArray<OpenMetaverse.UUID, SceneObjectPart>();
  2116. // a copy isnt backedup
  2117. dupe.Backup = false;
  2118. dupe.InvalidBoundsRadius();
  2119. // a copy is not in transit hopefully
  2120. dupe.inTransit = false;
  2121. // new group as no sitting avatars
  2122. dupe.m_sittingAvatars = new List<ScenePresence>();
  2123. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  2124. dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
  2125. if (userExposed)
  2126. dupe.m_rootPart.TrimPermissions();
  2127. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.GetArray());
  2128. partList.Sort(delegate(SceneObjectPart p1, SceneObjectPart p2)
  2129. {
  2130. return p1.LinkNum.CompareTo(p2.LinkNum);
  2131. }
  2132. );
  2133. foreach (SceneObjectPart part in partList)
  2134. {
  2135. SceneObjectPart newPart;
  2136. if (part.UUID != m_rootPart.UUID)
  2137. {
  2138. newPart = dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  2139. newPart.LinkNum = part.LinkNum;
  2140. // if (userExposed)
  2141. newPart.ParentID = dupe.m_rootPart.LocalId;
  2142. }
  2143. else
  2144. {
  2145. newPart = dupe.m_rootPart;
  2146. }
  2147. if (userExposed)
  2148. newPart.ApplyPhysics((uint)newPart.Flags,newPart.VolumeDetectActive,true);
  2149. // copy keyframemotion
  2150. if (part.KeyframeMotion != null)
  2151. newPart.KeyframeMotion = part.KeyframeMotion.Copy(dupe);
  2152. }
  2153. if (userExposed)
  2154. {
  2155. if (dupe.m_rootPart.PhysActor != null)
  2156. dupe.m_rootPart.PhysActor.Building = false; // tell physics to finish building
  2157. dupe.InvalidateDeepEffectivePerms();
  2158. dupe.HasGroupChanged = true;
  2159. dupe.AttachToBackup();
  2160. dupe.ScheduleGroupForFullAnimUpdate();
  2161. }
  2162. dupe.InvalidatePartsLinkMaps();
  2163. dupe.PseudoCRC = (int)(DateTime.UtcNow.Ticks);
  2164. m_dupeInProgress = false;
  2165. return dupe;
  2166. }
  2167. /// <summary>
  2168. /// Copy the given part as the root part of this scene object.
  2169. /// </summary>
  2170. /// <param name="part"></param>
  2171. /// <param name="cAgentID"></param>
  2172. /// <param name="cGroupID"></param>
  2173. public void CopyRootPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  2174. {
  2175. SceneObjectPart newpart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, 0, userExposed);
  2176. // SceneObjectPart newpart = part.Copy(part.LocalId, OwnerID, GroupID, 0, userExposed);
  2177. // newpart.LocalId = m_scene.AllocateLocalId();
  2178. SetRootPart(newpart);
  2179. if (userExposed)
  2180. RootPart.Velocity = Vector3.Zero; // In case source is moving
  2181. }
  2182. public void ScriptSetPhysicsStatus(bool usePhysics)
  2183. {
  2184. if (usePhysics)
  2185. {
  2186. if (RootPart.KeyframeMotion != null)
  2187. RootPart.KeyframeMotion.Stop();
  2188. RootPart.KeyframeMotion = null;
  2189. }
  2190. UpdateFlags(usePhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  2191. }
  2192. public void ScriptSetTemporaryStatus(bool makeTemporary)
  2193. {
  2194. UpdateFlags(UsesPhysics, makeTemporary, IsPhantom, IsVolumeDetect);
  2195. }
  2196. public void ScriptSetPhantomStatus(bool makePhantom)
  2197. {
  2198. UpdateFlags(UsesPhysics, IsTemporary, makePhantom, IsVolumeDetect);
  2199. }
  2200. public void ScriptSetVolumeDetect(bool makeVolumeDetect)
  2201. {
  2202. UpdateFlags(UsesPhysics, IsTemporary, IsPhantom, makeVolumeDetect);
  2203. }
  2204. public void applyImpulse(Vector3 impulse)
  2205. {
  2206. if (IsAttachment)
  2207. {
  2208. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  2209. if (avatar != null)
  2210. {
  2211. avatar.PushForce(impulse);
  2212. }
  2213. }
  2214. else
  2215. {
  2216. PhysicsActor pa = RootPart.PhysActor;
  2217. if (pa != null)
  2218. {
  2219. // false to be applied as a impulse
  2220. pa.AddForce(impulse, false);
  2221. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2222. }
  2223. }
  2224. }
  2225. public void ApplyAngularImpulse(Vector3 impulse)
  2226. {
  2227. PhysicsActor pa = RootPart.PhysActor;
  2228. if (pa != null)
  2229. {
  2230. if (!IsAttachment)
  2231. {
  2232. // false to be applied as a impulse
  2233. pa.AddAngularForce(impulse, false);
  2234. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2235. }
  2236. }
  2237. }
  2238. public Vector3 GetTorque()
  2239. {
  2240. return RootPart.Torque;
  2241. }
  2242. // This is used by both Double-Click Auto-Pilot and llMoveToTarget() in an attached object
  2243. public void MoveToTarget(Vector3 target, float tau)
  2244. {
  2245. if(tau > 0)
  2246. {
  2247. if (IsAttachment)
  2248. {
  2249. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  2250. if (avatar != null && !avatar.IsSatOnObject)
  2251. avatar.MoveToTarget(target, false, false, tau);
  2252. }
  2253. else
  2254. {
  2255. PhysicsActor pa = RootPart.PhysActor;
  2256. if (pa != null)
  2257. {
  2258. pa.PIDTarget = target;
  2259. pa.PIDTau = tau;
  2260. pa.PIDActive = true;
  2261. }
  2262. }
  2263. }
  2264. else
  2265. StopMoveToTarget();
  2266. }
  2267. public void StopMoveToTarget()
  2268. {
  2269. if (IsAttachment)
  2270. {
  2271. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  2272. if (avatar != null)
  2273. {
  2274. avatar.ResetMoveToTarget();
  2275. }
  2276. }
  2277. else
  2278. {
  2279. PhysicsActor pa = RootPart.PhysActor;
  2280. if (pa != null)
  2281. pa.PIDActive = false;
  2282. RootPart.ScheduleTerseUpdate(); // send a stop information
  2283. }
  2284. }
  2285. public void RotLookAt(Quaternion target, float strength, float damping)
  2286. {
  2287. if(IsDeleted)
  2288. return;
  2289. // non physical is handle in LSL api
  2290. if(!UsesPhysics || IsAttachment)
  2291. return;
  2292. SceneObjectPart rootpart = m_rootPart;
  2293. if (rootpart != null)
  2294. {
  2295. /* physics still doesnt suport this
  2296. if (rootpart.PhysActor != null)
  2297. {
  2298. rootpart.PhysActor.APIDTarget = new Quaternion(target.X, target.Y, target.Z, target.W);
  2299. rootpart.PhysActor.APIDStrength = strength;
  2300. rootpart.PhysActor.APIDDamping = damping;
  2301. rootpart.PhysActor.APIDActive = true;
  2302. }
  2303. */
  2304. // so do it in rootpart
  2305. rootpart.RotLookAt(target, strength, damping);
  2306. }
  2307. }
  2308. public void StartLookAt(Quaternion target, float strength, float damping)
  2309. {
  2310. if(IsDeleted)
  2311. return;
  2312. // non physical is done by LSL APi
  2313. if(!UsesPhysics || IsAttachment)
  2314. return;
  2315. if (m_rootPart != null)
  2316. m_rootPart.RotLookAt(target, strength, damping);
  2317. }
  2318. public void StopLookAt()
  2319. {
  2320. SceneObjectPart rootpart = m_rootPart;
  2321. if (rootpart != null)
  2322. {
  2323. if (rootpart.PhysActor != null)
  2324. {
  2325. rootpart.PhysActor.APIDActive = false;
  2326. }
  2327. rootpart.StopLookAt();
  2328. }
  2329. }
  2330. /// <summary>
  2331. /// Uses a PID to attempt to clamp the object on the Z axis at the given height over tau seconds.
  2332. /// </summary>
  2333. /// <param name="height">Height to hover. Height of zero disables hover.</param>
  2334. /// <param name="hoverType">Determines what the height is relative to </param>
  2335. /// <param name="tau">Number of seconds over which to reach target</param>
  2336. public void SetHoverHeight(float height, PIDHoverType hoverType, float tau)
  2337. {
  2338. PhysicsActor pa = null;
  2339. if(IsAttachment)
  2340. {
  2341. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  2342. if (avatar != null)
  2343. pa = avatar.PhysicsActor;
  2344. }
  2345. else
  2346. pa = RootPart.PhysActor;
  2347. if (pa != null)
  2348. {
  2349. if (height != 0f)
  2350. {
  2351. pa.PIDHoverHeight = height;
  2352. pa.PIDHoverType = hoverType;
  2353. pa.PIDHoverTau = tau;
  2354. pa.PIDHoverActive = true;
  2355. }
  2356. else
  2357. {
  2358. pa.PIDHoverActive = false;
  2359. }
  2360. }
  2361. }
  2362. /// <summary>
  2363. /// Set the owner of all linkset.
  2364. /// </summary>
  2365. /// <param name="cAgentID"></param>
  2366. /// <param name="cGroupID"></param>
  2367. public void SetOwner(UUID cAgentID, UUID cGroupID)
  2368. {
  2369. SceneObjectPart rpart = RootPart;
  2370. UUID oldowner = rpart.OwnerID;
  2371. ForEachPart(delegate(SceneObjectPart part)
  2372. {
  2373. if(part.GroupID != part.OwnerID)
  2374. part.LastOwnerID = part.OwnerID;
  2375. part.OwnerID = cAgentID;
  2376. part.GroupID = cGroupID;
  2377. });
  2378. if (oldowner != cAgentID)
  2379. {
  2380. // Apply Next Owner Permissions if we're not bypassing permissions
  2381. if (!m_scene.Permissions.BypassPermissions())
  2382. {
  2383. ApplyNextOwnerPermissions();
  2384. InvalidateEffectivePerms();
  2385. }
  2386. }
  2387. PseudoCRC = (int)(DateTime.UtcNow.Ticks);
  2388. rpart.ScheduleFullUpdate();
  2389. }
  2390. /// <summary>
  2391. /// Make a copy of the given part.
  2392. /// </summary>
  2393. /// <param name="part"></param>
  2394. /// <param name="cAgentID"></param>
  2395. /// <param name="cGroupID"></param>
  2396. public SceneObjectPart CopyPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  2397. {
  2398. SceneObjectPart newPart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed);
  2399. // SceneObjectPart newPart = part.Copy(part.LocalId, OwnerID, GroupID, m_parts.Count, userExposed);
  2400. // newPart.LocalId = m_scene.AllocateLocalId();
  2401. AddPart(newPart);
  2402. SetPartAsNonRoot(newPart);
  2403. return newPart;
  2404. }
  2405. /// <summary>
  2406. /// Reset the UUIDs for all the prims that make up this group.
  2407. /// </summary>
  2408. /// <remarks>
  2409. /// This is called by methods which want to add a new group to an existing scene, in order
  2410. /// to ensure that there are no clashes with groups already present.
  2411. /// </remarks>
  2412. public void ResetIDs()
  2413. {
  2414. lock (m_parts.SyncRoot)
  2415. {
  2416. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.GetArray());
  2417. m_parts.Clear();
  2418. foreach (SceneObjectPart part in partsList)
  2419. {
  2420. part.ResetIDs(part.LinkNum); // Don't change link nums
  2421. m_parts.Add(part.UUID, part);
  2422. }
  2423. PseudoCRC = (int)(DateTime.UtcNow.Ticks);
  2424. }
  2425. }
  2426. /// <summary>
  2427. ///
  2428. /// </summary>
  2429. /// <param name="part"></param>
  2430. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, UUID AgentID, uint RequestFlags)
  2431. {
  2432. remoteClient.SendObjectPropertiesFamilyData(RootPart, RequestFlags);
  2433. // remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.OwnerID, RootPart.GroupID, RootPart.BaseMask,
  2434. // RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  2435. // RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  2436. // RootPart.CreatorID, RootPart.Name, RootPart.Description);
  2437. }
  2438. public void SetPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  2439. {
  2440. part.OwnerID = cAgentID;
  2441. part.GroupID = cGroupID;
  2442. }
  2443. #endregion
  2444. public override void Update()
  2445. {
  2446. // Check that the group was not deleted before the scheduled update
  2447. // FIXME: This is merely a temporary measure to reduce the incidence of failure when
  2448. // an object has been deleted from a scene before update was processed.
  2449. // A more fundamental overhaul of the update mechanism is required to eliminate all
  2450. // the race conditions.
  2451. if (IsDeleted || inTransit)
  2452. return;
  2453. if (IsAttachment)
  2454. {
  2455. ScenePresence sp = m_scene.GetScenePresence(AttachedAvatar);
  2456. if (sp != null)
  2457. sp.SendAttachmentScheduleUpdate(this);
  2458. return;
  2459. }
  2460. // while physics doesn't suports LookAt, we do it in RootPart
  2461. if (!IsSelected)
  2462. RootPart.UpdateLookAt();
  2463. double now = Util.GetTimeStampMS();
  2464. RootPart.SendScheduledUpdates(now);
  2465. SceneObjectPart[] parts = m_parts.GetArray();
  2466. for (int i = 0; i < parts.Length; i++)
  2467. {
  2468. SceneObjectPart part = parts[i];
  2469. if(part != RootPart)
  2470. part.SendScheduledUpdates(now);
  2471. }
  2472. }
  2473. /// <summary>
  2474. /// Schedule a full update for this scene object to all interested viewers.
  2475. /// </summary>
  2476. /// <remarks>
  2477. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  2478. /// so that either all changes are sent at once. However, currently, a large amount of internal
  2479. /// code will set this anyway when some object properties are changed.
  2480. /// </remarks>
  2481. public void ScheduleGroupForFullUpdate()
  2482. {
  2483. // if (IsAttachment)
  2484. // m_log.DebugFormat("[SOG]: Scheduling full update for {0} {1}", Name, LocalId);
  2485. checkAtTargets();
  2486. if (Scene.GetNumberOfClients() == 0)
  2487. return;
  2488. RootPart.ScheduleFullUpdate();
  2489. SceneObjectPart[] parts = m_parts.GetArray();
  2490. for (int i = 0; i < parts.Length; i++)
  2491. {
  2492. SceneObjectPart part = parts[i];
  2493. if (part != RootPart)
  2494. part.ScheduleFullUpdate();
  2495. }
  2496. }
  2497. public void ScheduleGroupForFullAnimUpdate()
  2498. {
  2499. // if (IsAttachment)
  2500. // m_log.DebugFormat("[SOG]: Scheduling full update for {0} {1}", Name, LocalId);
  2501. checkAtTargets();
  2502. if (Scene.GetNumberOfClients() == 0)
  2503. return;
  2504. SceneObjectPart[] parts = m_parts.GetArray();
  2505. if (!RootPart.Shape.MeshFlagEntry)
  2506. {
  2507. RootPart.ScheduleFullUpdate();
  2508. for (int i = 0; i < parts.Length; i++)
  2509. {
  2510. SceneObjectPart part = parts[i];
  2511. if (part != RootPart)
  2512. part.ScheduleFullUpdate();
  2513. }
  2514. return;
  2515. }
  2516. RootPart.ScheduleFullAnimUpdate();
  2517. for (int i = 0; i < parts.Length; i++)
  2518. {
  2519. SceneObjectPart part = parts[i];
  2520. if (part != RootPart)
  2521. part.ScheduleFullAnimUpdate();
  2522. }
  2523. }
  2524. /// <summary>
  2525. /// Schedule a terse update for this scene object to all interested viewers.
  2526. /// </summary>
  2527. /// <remarks>
  2528. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  2529. /// so that either all changes are sent at once. However, currently, a large amount of internal
  2530. /// code will set this anyway when some object properties are changed.
  2531. /// </remarks>
  2532. public void ScheduleGroupForTerseUpdate()
  2533. {
  2534. // m_log.DebugFormat("[SOG]: Scheduling terse update for {0} {1}", Name, UUID);
  2535. SceneObjectPart[] parts = m_parts.GetArray();
  2536. for (int i = 0; i < parts.Length; i++)
  2537. parts[i].ScheduleTerseUpdate();
  2538. }
  2539. /// <summary>
  2540. /// Immediately send an update for this scene object's root prim only.
  2541. /// This is for updates regarding the object as a whole, and none of its parts in particular.
  2542. /// Note: this may not be used by opensim (it probably should) but it's used by
  2543. /// external modules.
  2544. /// </summary>
  2545. public void SendGroupRootTerseUpdate()
  2546. {
  2547. if (IsDeleted || inTransit)
  2548. return;
  2549. RootPart.SendTerseUpdateToAllClients();
  2550. }
  2551. public void QueueForUpdateCheck()
  2552. {
  2553. if (m_scene == null) // Need to check here as it's null during object creation
  2554. return;
  2555. m_scene.SceneGraph.AddToUpdateList(this);
  2556. }
  2557. /// <summary>
  2558. /// Immediately send a terse update for this scene object.
  2559. /// </summary>
  2560. public void SendGroupTerseUpdate()
  2561. {
  2562. if (IsDeleted || inTransit)
  2563. return;
  2564. if (IsAttachment)
  2565. {
  2566. ScenePresence sp = m_scene.GetScenePresence(AttachedAvatar);
  2567. if (sp != null)
  2568. {
  2569. sp.SendAttachmentUpdate(this, PrimUpdateFlags.TerseUpdate);
  2570. return;
  2571. }
  2572. }
  2573. SceneObjectPart[] parts = m_parts.GetArray();
  2574. for (int i = 0; i < parts.Length; i++)
  2575. parts[i].SendTerseUpdateToAllClientsInternal();
  2576. }
  2577. /// <summary>
  2578. /// Send metadata about the root prim (name, description, sale price, permissions, etc.) to a client.
  2579. /// </summary>
  2580. /// <param name="client"></param>
  2581. public void SendPropertiesToClient(IClientAPI client)
  2582. {
  2583. m_rootPart.SendPropertiesToClient(client);
  2584. }
  2585. #region SceneGroupPart Methods
  2586. /// <summary>
  2587. /// Get the child part by LinkNum
  2588. /// </summary>
  2589. /// <param name="linknum"></param>
  2590. /// <returns>null if no child part with that linknum or child part</returns>
  2591. public SceneObjectPart GetLinkNumPart(int linknum)
  2592. {
  2593. SceneObjectPart[] parts = m_parts.GetArray();
  2594. for (int i = 0; i < parts.Length; i++)
  2595. {
  2596. if (parts[i].LinkNum == linknum)
  2597. return parts[i];
  2598. }
  2599. return null;
  2600. }
  2601. /// <summary>
  2602. /// Get a part with a given UUID
  2603. /// </summary>
  2604. /// <param name="primID"></param>
  2605. /// <returns>null if a part with the primID was not found</returns>
  2606. public SceneObjectPart GetPart(UUID primID)
  2607. {
  2608. SceneObjectPart childPart;
  2609. m_parts.TryGetValue(primID, out childPart);
  2610. return childPart;
  2611. }
  2612. /// <summary>
  2613. /// Get a part with a given local ID
  2614. /// </summary>
  2615. /// <param name="localID"></param>
  2616. /// <returns>null if a part with the local ID was not found</returns>
  2617. public SceneObjectPart GetPart(uint localID)
  2618. {
  2619. SceneObjectPart sop = m_scene.GetSceneObjectPart(localID);
  2620. if(sop.ParentGroup.Equals(this))
  2621. return sop;
  2622. return null;
  2623. }
  2624. #endregion
  2625. #region Packet Handlers
  2626. /// <summary>
  2627. /// Link the prims in a given group to this group
  2628. /// </summary>
  2629. /// <remarks>
  2630. /// Do not call this method directly - use Scene.LinkObjects() instead to avoid races between threads.
  2631. /// FIXME: There are places where scripts call these methods directly without locking. This is a potential race condition.
  2632. /// </remarks>
  2633. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  2634. public void LinkToGroup(SceneObjectGroup objectGroup)
  2635. {
  2636. LinkToGroup(objectGroup, false);
  2637. }
  2638. // Link an existing group to this group.
  2639. // The group being linked need not be a linkset -- it can have just one prim.
  2640. public void LinkToGroup(SceneObjectGroup objectGroup, bool insert)
  2641. {
  2642. // m_log.DebugFormat(
  2643. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  2644. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  2645. // Linking to ourselves is not a valid operation.
  2646. if (objectGroup == this)
  2647. return;
  2648. // If the configured linkset capacity is greater than zero,
  2649. // and the new linkset would have a prim count higher than this
  2650. // value, do not link it.
  2651. if (m_scene.m_linksetCapacity > 0 &&
  2652. (PrimCount + objectGroup.PrimCount) >
  2653. m_scene.m_linksetCapacity)
  2654. {
  2655. m_log.DebugFormat(
  2656. "[SCENE OBJECT GROUP]: Cannot link group with root" +
  2657. " part {0}, {1} ({2} prims) to group with root part" +
  2658. " {3}, {4} ({5} prims) because the new linkset" +
  2659. " would exceed the configured maximum of {6}",
  2660. objectGroup.RootPart.Name, objectGroup.RootPart.UUID,
  2661. objectGroup.PrimCount, RootPart.Name, RootPart.UUID,
  2662. PrimCount, m_scene.m_linksetCapacity);
  2663. return;
  2664. }
  2665. // physical prims count limit
  2666. // not very eficient :(
  2667. if (UsesPhysics && m_scene.m_linksetPhysCapacity > 0 && (PrimCount + objectGroup.PrimCount) >
  2668. m_scene.m_linksetPhysCapacity)
  2669. {
  2670. int cntr = 0;
  2671. foreach (SceneObjectPart part in Parts)
  2672. {
  2673. if (part.PhysicsShapeType != (byte)PhysicsShapeType.None)
  2674. cntr++;
  2675. }
  2676. foreach (SceneObjectPart part in objectGroup.Parts)
  2677. {
  2678. if (part.PhysicsShapeType != (byte)PhysicsShapeType.None)
  2679. cntr++;
  2680. }
  2681. if (cntr > m_scene.m_linksetPhysCapacity)
  2682. {
  2683. // cancel physics
  2684. RootPart.Flags &= ~PrimFlags.Physics;
  2685. ApplyPhysics();
  2686. }
  2687. }
  2688. // 'linkPart' == the root of the group being linked into this group
  2689. SceneObjectPart linkPart = objectGroup.m_rootPart;
  2690. if (m_rootPart.PhysActor != null)
  2691. m_rootPart.PhysActor.Building = true;
  2692. if (linkPart.PhysActor != null)
  2693. linkPart.PhysActor.Building = true;
  2694. // physics flags from group to be applied to linked parts
  2695. bool grpusephys = UsesPhysics;
  2696. bool grptemporary = IsTemporary;
  2697. // Remember where the group being linked thought it was
  2698. Vector3 oldGroupPosition = linkPart.GroupPosition;
  2699. Quaternion oldRootRotation = linkPart.RotationOffset;
  2700. // A linked SOP remembers its location and rotation relative to the root of a group.
  2701. // Convert the root of the group being linked to be relative to the
  2702. // root of the group being linked to.
  2703. // Note: Some of the assignments have complex side effects.
  2704. // First move the new group's root SOP's position to be relative to ours
  2705. // (radams1: Not sure if the multiple setting of OffsetPosition is required. If not,
  2706. // this code can be reordered to have a more logical flow.)
  2707. linkPart.setOffsetPosition(linkPart.GroupPosition - AbsolutePosition);
  2708. // Assign the new parent to the root of the old group
  2709. linkPart.ParentID = m_rootPart.LocalId;
  2710. // Now that it's a child, it's group position is our root position
  2711. linkPart.setGroupPosition(AbsolutePosition);
  2712. // Rotate the linking root SOP's position to be relative to the new root prim
  2713. Quaternion parentRot = m_rootPart.RotationOffset;
  2714. // Make the linking root SOP's rotation relative to the new root prim
  2715. Quaternion oldRot = linkPart.RotationOffset;
  2716. Quaternion newRot = Quaternion.Conjugate(parentRot) * oldRot;
  2717. linkPart.setRotationOffset(newRot);
  2718. Vector3 axPos = linkPart.OffsetPosition;
  2719. axPos *= Quaternion.Conjugate(parentRot);
  2720. linkPart.OffsetPosition = axPos;
  2721. // If there is only one SOP in a SOG, the LinkNum is zero. I.e., not a linkset.
  2722. // Now that we know this SOG has at least two SOPs in it, the new root
  2723. // SOP becomes the first in the linkset.
  2724. if (m_rootPart.LinkNum == 0)
  2725. m_rootPart.LinkNum = 1;
  2726. lock (m_parts.SyncRoot)
  2727. {
  2728. // Calculate the new link number for the old root SOP
  2729. int linkNum;
  2730. if (insert)
  2731. {
  2732. linkNum = 2;
  2733. int insertSize = objectGroup.PrimCount;
  2734. foreach (SceneObjectPart part in Parts)
  2735. {
  2736. if (part.LinkNum > 1)
  2737. part.LinkNum += insertSize;
  2738. }
  2739. }
  2740. else
  2741. {
  2742. linkNum = PrimCount + 1;
  2743. }
  2744. // Add the old root SOP as a part in our group's list
  2745. m_parts.Add(linkPart.UUID, linkPart);
  2746. linkPart.SetParent(this);
  2747. linkPart.CreateSelected = true;
  2748. linkPart.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (linkPart.Flags & PrimFlags.Phantom) != 0), linkPart.VolumeDetectActive || RootPart.VolumeDetectActive, true);
  2749. // If the added SOP is physical, also tell the physics engine about the link relationship.
  2750. if (linkPart.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  2751. {
  2752. linkPart.PhysActor.link(m_rootPart.PhysActor);
  2753. this.Scene.PhysicsScene.AddPhysicsActorTaint(linkPart.PhysActor);
  2754. }
  2755. linkPart.LinkNum = linkNum++;
  2756. linkPart.UpdatePrimFlags(UsesPhysics, IsTemporary, IsPhantom, IsVolumeDetect, false);
  2757. // Get a list of the SOP's in the source group in order of their linknum's.
  2758. SceneObjectPart[] ogParts = objectGroup.Parts;
  2759. Array.Sort(ogParts, delegate(SceneObjectPart a, SceneObjectPart b)
  2760. {
  2761. return a.LinkNum - b.LinkNum;
  2762. });
  2763. // Add each of the SOP's from the source linkset to our linkset
  2764. for (int i = 0; i < ogParts.Length; i++)
  2765. {
  2766. SceneObjectPart part = ogParts[i];
  2767. if (part.UUID != objectGroup.m_rootPart.UUID)
  2768. {
  2769. LinkNonRootPart(part, oldGroupPosition, oldRootRotation, linkNum++);
  2770. // Update the physics flags for the newly added SOP
  2771. // (Is this necessary? LinkNonRootPart() has already called UpdatePrimFlags but with different flags!??)
  2772. part.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (part.Flags & PrimFlags.Phantom) != 0), part.VolumeDetectActive, true);
  2773. // If the added SOP is physical, also tell the physics engine about the link relationship.
  2774. if (part.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  2775. {
  2776. part.PhysActor.link(m_rootPart.PhysActor);
  2777. this.Scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2778. }
  2779. }
  2780. part.ClearUndoState();
  2781. }
  2782. }
  2783. // Now that we've aquired all of the old SOG's parts, remove the old SOG from the scene.
  2784. m_scene.UnlinkSceneObject(objectGroup, true);
  2785. objectGroup.IsDeleted = true;
  2786. objectGroup.m_parts.Clear();
  2787. // Can't do this yet since backup still makes use of the root part without any synchronization
  2788. // objectGroup.m_rootPart = null;
  2789. // If linking prims with different permissions, fix them
  2790. AdjustChildPrimPermissions(false);
  2791. GroupContainsForeignPrims = true;
  2792. AttachToBackup();
  2793. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  2794. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  2795. // unmoved prims!
  2796. ResetChildPrimPhysicsPositions();
  2797. InvalidBoundsRadius();
  2798. InvalidatePartsLinkMaps();
  2799. if (m_rootPart.PhysActor != null)
  2800. m_rootPart.PhysActor.Building = false;
  2801. //HasGroupChanged = true;
  2802. //ScheduleGroupForFullUpdate();
  2803. }
  2804. /// <summary>
  2805. /// Delink the given prim from this group. The delinked prim is established as
  2806. /// an independent SceneObjectGroup.
  2807. /// </summary>
  2808. /// <remarks>
  2809. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  2810. /// condition. But currently there is no
  2811. /// alternative method that does take a lonk to delink a single prim.
  2812. /// </remarks>
  2813. /// <param name="partID"></param>
  2814. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  2815. public SceneObjectGroup DelinkFromGroup(uint partID)
  2816. {
  2817. return DelinkFromGroup(partID, true);
  2818. }
  2819. /// <summary>
  2820. /// Delink the given prim from this group. The delinked prim is established as
  2821. /// an independent SceneObjectGroup.
  2822. /// </summary>
  2823. /// <remarks>
  2824. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  2825. /// condition. But currently there is no
  2826. /// alternative method that does take a lonk to delink a single prim.
  2827. /// </remarks>
  2828. /// <param name="partID"></param>
  2829. /// <param name="sendEvents"></param>
  2830. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  2831. public SceneObjectGroup DelinkFromGroup(uint partID, bool sendEvents)
  2832. {
  2833. SceneObjectPart linkPart = GetPart(partID);
  2834. if (linkPart != null)
  2835. {
  2836. return DelinkFromGroup(linkPart, sendEvents);
  2837. }
  2838. else
  2839. {
  2840. m_log.WarnFormat("[SCENE OBJECT GROUP]: " +
  2841. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  2842. partID, LocalId, UUID);
  2843. return null;
  2844. }
  2845. }
  2846. /// <summary>
  2847. /// Delink the given prim from this group. The delinked prim is established as
  2848. /// an independent SceneObjectGroup.
  2849. /// </summary>
  2850. /// <remarks>
  2851. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  2852. /// condition. But currently there is no
  2853. /// alternative method that does take a lock to delink a single prim.
  2854. /// </remarks>
  2855. /// <param name="partID"></param>
  2856. /// <param name="sendEvents"></param>
  2857. /// <returns>The object group of the newly delinked prim.</returns>
  2858. public SceneObjectGroup DelinkFromGroup(SceneObjectPart linkPart, bool sendEvents)
  2859. {
  2860. // m_log.DebugFormat(
  2861. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  2862. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  2863. if (m_rootPart.PhysActor != null)
  2864. m_rootPart.PhysActor.Building = true;
  2865. linkPart.ClearUndoState();
  2866. Vector3 worldPos = linkPart.GetWorldPosition();
  2867. Quaternion worldRot = linkPart.GetWorldRotation();
  2868. // Remove the part from this object
  2869. lock (m_parts.SyncRoot)
  2870. {
  2871. m_parts.Remove(linkPart.UUID);
  2872. SceneObjectPart[] parts = m_parts.GetArray();
  2873. // Rejigger the linknum's of the remaining SOP's to fill any gap
  2874. if (parts.Length == 1 && RootPart != null)
  2875. {
  2876. // Single prim left
  2877. RootPart.LinkNum = 0;
  2878. }
  2879. else
  2880. {
  2881. for (int i = 0; i < parts.Length; i++)
  2882. {
  2883. SceneObjectPart part = parts[i];
  2884. if (part.LinkNum > linkPart.LinkNum)
  2885. part.LinkNum--;
  2886. }
  2887. }
  2888. }
  2889. linkPart.ParentID = 0;
  2890. linkPart.LinkNum = 0;
  2891. PhysicsActor linkPartPa = linkPart.PhysActor;
  2892. // Remove the SOP from the physical scene.
  2893. // If the new SOG is physical, it is re-created later.
  2894. // (There is a problem here in that we have not yet told the physics
  2895. // engine about the delink. Someday, linksets should be made first
  2896. // class objects in the physics engine interface).
  2897. if (linkPartPa != null)
  2898. {
  2899. m_scene.PhysicsScene.RemovePrim(linkPartPa);
  2900. linkPart.PhysActor = null;
  2901. }
  2902. // We need to reset the child part's position
  2903. // ready for life as a separate object after being a part of another object
  2904. /* This commented out code seems to recompute what GetWorldPosition already does.
  2905. * Replace with a call to GetWorldPosition (before unlinking)
  2906. Quaternion parentRot = m_rootPart.RotationOffset;
  2907. Vector3 axPos = linkPart.OffsetPosition;
  2908. axPos *= parentRot;
  2909. linkPart.OffsetPosition = new Vector3(axPos.X, axPos.Y, axPos.Z);
  2910. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  2911. linkPart.OffsetPosition = new Vector3(0, 0, 0);
  2912. */
  2913. linkPart.setGroupPosition(worldPos);
  2914. linkPart.setOffsetPosition(Vector3.Zero);
  2915. linkPart.setRotationOffset(worldRot);
  2916. // Create a new SOG to go around this unlinked and unattached SOP
  2917. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  2918. m_scene.AddNewSceneObject(objectGroup, true);
  2919. linkPart.Rezzed = RootPart.Rezzed;
  2920. // When we delete a group, we currently have to force persist to the database if the object id has changed
  2921. // (since delete works by deleting all rows which have a given object id)
  2922. // this is as it seems to be in sl now
  2923. if(linkPart.PhysicsShapeType == (byte)PhysShapeType.none)
  2924. linkPart.PhysicsShapeType = linkPart.DefaultPhysicsShapeType(); // root prims can't have type none for now
  2925. if (m_rootPart.PhysActor != null)
  2926. m_rootPart.PhysActor.Building = false;
  2927. objectGroup.HasGroupChangedDueToDelink = true;
  2928. InvalidBoundsRadius();
  2929. InvalidatePartsLinkMaps();
  2930. objectGroup.InvalidateEffectivePerms();
  2931. if (sendEvents)
  2932. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  2933. return objectGroup;
  2934. }
  2935. /* working on it
  2936. public void DelinkFromGroup(List<SceneObjectPart> linkParts, bool sendEvents)
  2937. {
  2938. // m_log.DebugFormat(
  2939. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  2940. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  2941. if(PrimCount == 1)
  2942. return;
  2943. if (m_rootPart.PhysActor != null)
  2944. m_rootPart.PhysActor.Building = true;
  2945. bool unlinkroot = false;
  2946. foreach(SceneObjectPart linkPart in linkParts)
  2947. {
  2948. // first we only remove child parts
  2949. if(linkPart.LocalId == m_rootPart.LocalId)
  2950. {
  2951. unlinkroot = true;
  2952. continue;
  2953. }
  2954. lock (m_parts.SyncRoot)
  2955. if(!m_parts.Remove(linkPart.UUID))
  2956. continue;
  2957. linkPart.ClearUndoState();
  2958. Vector3 worldPos = linkPart.GetWorldPosition();
  2959. Quaternion worldRot = linkPart.GetWorldRotation();
  2960. linkPart.ParentID = 0;
  2961. linkPart.LinkNum = 0;
  2962. PhysicsActor linkPartPa = linkPart.PhysActor;
  2963. // Remove the SOP from the physical scene.
  2964. // If the new SOG is physical, it is re-created later.
  2965. // (There is a problem here in that we have not yet told the physics
  2966. // engine about the delink. Someday, linksets should be made first
  2967. // class objects in the physics engine interface).
  2968. if (linkPartPa != null)
  2969. {
  2970. m_scene.PhysicsScene.RemovePrim(linkPartPa);
  2971. linkPart.PhysActor = null;
  2972. }
  2973. linkPart.setGroupPosition(worldPos);
  2974. linkPart.setOffsetPosition(Vector3.Zero);
  2975. linkPart.setRotationOffset(worldRot);
  2976. // Create a new SOG to go around this unlinked and unattached SOP
  2977. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  2978. m_scene.AddNewSceneObject(objectGroup, true);
  2979. linkPart.Rezzed = RootPart.Rezzed;
  2980. // this is as it seems to be in sl now
  2981. if(linkPart.PhysicsShapeType == (byte)PhysShapeType.none)
  2982. linkPart.PhysicsShapeType = linkPart.DefaultPhysicsShapeType(); // root prims can't have type none for now
  2983. objectGroup.HasGroupChangedDueToDelink = true;
  2984. if (sendEvents)
  2985. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  2986. }
  2987. if(unlinkroot)
  2988. {
  2989. //TODO
  2990. }
  2991. lock (m_parts.SyncRoot)
  2992. {
  2993. SceneObjectPart[] parts = m_parts.GetArray();
  2994. if (parts.Length == 1)
  2995. {
  2996. // Single prim left
  2997. m_rootPart.LinkNum = 0;
  2998. }
  2999. else
  3000. {
  3001. m_rootPart.LinkNum = 1;
  3002. int linknum = 2;
  3003. for (int i = 1; i < parts.Length; i++)
  3004. parts[i].LinkNum = linknum++;
  3005. }
  3006. }
  3007. InvalidBoundsRadius();
  3008. if (m_rootPart.PhysActor != null)
  3009. m_rootPart.PhysActor.Building = false;
  3010. // When we delete a group, we currently have to force persist to the database if the object id has changed
  3011. // (since delete works by deleting all rows which have a given object id)
  3012. Scene.SimulationDataService.RemoveObject(UUID, Scene.RegionInfo.RegionID);
  3013. HasGroupChangedDueToDelink = true;
  3014. TriggerScriptChangedEvent(Changed.LINK);
  3015. return;
  3016. }
  3017. */
  3018. /// <summary>
  3019. /// Stop this object from being persisted over server restarts.
  3020. /// </summary>
  3021. /// <param name="objectGroup"></param>
  3022. public virtual void DetachFromBackup()
  3023. {
  3024. if (m_scene != null)
  3025. {
  3026. m_scene.SceneGraph.FireDetachFromBackup(this);
  3027. if (Backup)
  3028. m_scene.EventManager.OnBackup -= ProcessBackup;
  3029. }
  3030. Backup = false;
  3031. }
  3032. // This links an SOP from a previous linkset into my linkset.
  3033. // The trick is that the SOP's position and rotation are relative to the old root SOP's
  3034. // so we are passed in the position and rotation of the old linkset so this can
  3035. // unjigger this SOP's position and rotation from the previous linkset and
  3036. // then make them relative to my linkset root.
  3037. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation, int linkNum)
  3038. {
  3039. Quaternion parentRot = oldGroupRotation;
  3040. Quaternion oldRot = part.RotationOffset;
  3041. // Move our position in world
  3042. Vector3 axPos = part.OffsetPosition;
  3043. axPos *= parentRot;
  3044. Vector3 newPos = oldGroupPosition + axPos;
  3045. part.setGroupPosition(newPos);
  3046. part.setOffsetPosition(Vector3.Zero);
  3047. // Compution our rotation in world
  3048. Quaternion worldRot = parentRot * oldRot;
  3049. part.RotationOffset = worldRot;
  3050. // Add this SOP to our linkset
  3051. part.SetParent(this);
  3052. part.ParentID = m_rootPart.LocalId;
  3053. m_parts.Add(part.UUID, part);
  3054. part.LinkNum = linkNum;
  3055. // Compute the new position of this SOP relative to the group position
  3056. part.setOffsetPosition(newPos - AbsolutePosition);
  3057. // (radams1 20120711: I don't know why part.OffsetPosition is set multiple times.
  3058. // It would have the affect of setting the physics engine position multiple
  3059. // times. In theory, that is not necessary but I don't have a good linkset
  3060. // test to know that cleaning up this code wouldn't break things.)
  3061. // Compute the SOP's rotation relative to the rotation of the group.
  3062. parentRot = m_rootPart.RotationOffset;
  3063. oldRot = part.RotationOffset;
  3064. Quaternion newRot = Quaternion.Conjugate(parentRot) * worldRot;
  3065. part.setRotationOffset(newRot);
  3066. Vector3 pos = part.OffsetPosition;
  3067. pos *= Quaternion.Conjugate(parentRot);
  3068. part.OffsetPosition = pos; // update position and orientation on physics also
  3069. // Since this SOP's state has changed, push those changes into the physics engine
  3070. // and the simulator.
  3071. // done on caller
  3072. // part.UpdatePrimFlags(UsesPhysics, IsTemporary, IsPhantom, IsVolumeDetect, false);
  3073. }
  3074. double lastTouchTime = 0;
  3075. /// <summary>
  3076. /// If object is physical, apply force to move it around
  3077. /// If object is not physical, just put it at the resulting location
  3078. /// </summary>
  3079. /// <param name="partID">Part ID to check for grab</param>
  3080. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  3081. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  3082. /// <param name="remoteClient"></param>
  3083. public void GrabMovement(UUID partID, Vector3 offset, Vector3 pos, IClientAPI remoteClienth)
  3084. {
  3085. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  3086. {
  3087. if (BlockGrabOverride)
  3088. return;
  3089. SceneObjectPart part = GetPart(partID);
  3090. if (part == null)
  3091. return;
  3092. if (part.BlockGrab)
  3093. return;
  3094. PhysicsActor pa = m_rootPart.PhysActor;
  3095. if (pa != null && pa.IsPhysical)
  3096. {
  3097. // empirically convert distance diference to a impulse
  3098. Vector3 grabforce = pos - AbsolutePosition;
  3099. grabforce = grabforce * (pa.Mass * 0.1f);
  3100. pa.AddForce(grabforce, false);
  3101. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  3102. }
  3103. else
  3104. {
  3105. if(IsAttachment)
  3106. return;
  3107. // block movement if there was a touch at start
  3108. double now = Util.GetTimeStampMS();
  3109. if (now - lastTouchTime < 250)
  3110. {
  3111. lastTouchTime = now;
  3112. return;
  3113. }
  3114. // a touch or pass may had become active ??
  3115. if (((part.ScriptEvents & scriptEvents.anytouch) != 0) || (RootPart.ScriptEvents & scriptEvents.anytouch) != 0)
  3116. {
  3117. lastTouchTime = now;
  3118. return;
  3119. }
  3120. lastTouchTime = 0;
  3121. UpdateGroupPosition(pos);
  3122. }
  3123. }
  3124. }
  3125. /// <summary>
  3126. /// If object is physical, prepare for spinning torques (set flag to save old orientation)
  3127. /// </summary>
  3128. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  3129. /// <param name="remoteClient"></param>
  3130. public void SpinStart(IClientAPI remoteClient)
  3131. {
  3132. if (BlockGrabOverride || m_rootPart.BlockGrab)
  3133. return;
  3134. if (m_scene.EventManager.TriggerGroupSpinStart(UUID))
  3135. {
  3136. PhysicsActor pa = m_rootPart.PhysActor;
  3137. if (pa != null)
  3138. {
  3139. if (pa.IsPhysical)
  3140. {
  3141. m_rootPart.IsWaitingForFirstSpinUpdatePacket = true;
  3142. }
  3143. }
  3144. }
  3145. }
  3146. /// <summary>
  3147. /// If object is physical, apply torque to spin it around
  3148. /// </summary>
  3149. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  3150. /// <param name="remoteClient"></param>
  3151. public void SpinMovement(Quaternion newOrientation, IClientAPI remoteClient)
  3152. {
  3153. // The incoming newOrientation, sent by the client, "seems" to be the
  3154. // desired target orientation. This needs further verification; in particular,
  3155. // one would expect that the initial incoming newOrientation should be
  3156. // fairly close to the original prim's physical orientation,
  3157. // m_rootPart.PhysActor.Orientation. This however does not seem to be the
  3158. // case (might just be an issue with different quaternions representing the
  3159. // same rotation, or it might be a coordinate system issue).
  3160. //
  3161. // Since it's not clear what the relationship is between the PhysActor.Orientation
  3162. // and the incoming orientations sent by the client, we take an alternative approach
  3163. // of calculating the delta rotation between the orientations being sent by the
  3164. // client. (Since a spin is invoked by ctrl+shift+drag in the client, we expect
  3165. // a steady stream of several new orientations coming in from the client.)
  3166. // This ensures that the delta rotations are being calculated from self-consistent
  3167. // pairs of old/new rotations. Given the delta rotation, we apply a torque around
  3168. // the delta rotation axis, scaled by the object mass times an arbitrary scaling
  3169. // factor (to ensure the resulting torque is not "too strong" or "too weak").
  3170. //
  3171. // Ideally we need to calculate (probably iteratively) the exact torque or series
  3172. // of torques needed to arrive exactly at the destination orientation. However, since
  3173. // it is not yet clear how to map the destination orientation (provided by the viewer)
  3174. // into PhysActor orientations (needed by the physics engine), we omit this step.
  3175. // This means that the resulting torque will at least be in the correct direction,
  3176. // but it will result in over-shoot or under-shoot of the target orientation.
  3177. // For the end user, this means that ctrl+shift+drag can be used for relative,
  3178. // but not absolute, adjustments of orientation for physical prims.
  3179. if (BlockGrabOverride || m_rootPart.BlockGrab)
  3180. return;
  3181. if (m_scene.EventManager.TriggerGroupSpin(UUID, newOrientation))
  3182. {
  3183. PhysicsActor pa = m_rootPart.PhysActor;
  3184. if (pa != null && pa.IsPhysical)
  3185. {
  3186. if (m_rootPart.IsWaitingForFirstSpinUpdatePacket)
  3187. {
  3188. // first time initialization of "old" orientation for calculation of delta rotations
  3189. m_rootPart.SpinOldOrientation = newOrientation;
  3190. m_rootPart.IsWaitingForFirstSpinUpdatePacket = false;
  3191. }
  3192. else
  3193. {
  3194. // save and update old orientation
  3195. Quaternion old = m_rootPart.SpinOldOrientation;
  3196. m_rootPart.SpinOldOrientation = newOrientation;
  3197. //m_log.Error("[SCENE OBJECT GROUP]: Old orientation is " + old);
  3198. //m_log.Error("[SCENE OBJECT GROUP]: Incoming new orientation is " + newOrientation);
  3199. // compute difference between previous old rotation and new incoming rotation
  3200. Quaternion minimalRotationFromQ1ToQ2 = newOrientation * Quaternion.Inverse(old);
  3201. float rotationAngle;
  3202. Vector3 spinforce;
  3203. minimalRotationFromQ1ToQ2.GetAxisAngle(out spinforce, out rotationAngle);
  3204. if(Math.Abs(rotationAngle)< 0.001)
  3205. return;
  3206. spinforce.Normalize();
  3207. //m_log.Error("SCENE OBJECT GROUP]: rotation axis is " + rotationAxis);
  3208. if(rotationAngle > 0)
  3209. spinforce = spinforce * pa.Mass * 0.1f; // 0.1 is an arbitrary torque scaling factor
  3210. else
  3211. spinforce = spinforce * pa.Mass * -0.1f; // 0.1 is an arbitrary torque scaling
  3212. pa.AddAngularForce(spinforce,true);
  3213. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  3214. }
  3215. }
  3216. else
  3217. {
  3218. NonPhysicalSpinMovement(newOrientation);
  3219. }
  3220. }
  3221. }
  3222. /// <summary>
  3223. /// Apply rotation for spinning non-physical linksets (Ctrl+Shift+Drag)
  3224. /// As with dragging, scripted objects must be blocked from spinning
  3225. /// </summary>
  3226. /// <param name="newOrientation">New Rotation</param>
  3227. private void NonPhysicalSpinMovement(Quaternion newOrientation)
  3228. {
  3229. if(!IsAttachment && ScriptCount() == 0)
  3230. UpdateGroupRotationR(newOrientation);
  3231. }
  3232. /// <summary>
  3233. /// Set the name of a prim
  3234. /// </summary>
  3235. /// <param name="name"></param>
  3236. /// <param name="localID"></param>
  3237. public void SetPartName(string name, uint localID)
  3238. {
  3239. SceneObjectPart part = GetPart(localID);
  3240. if (part != null)
  3241. {
  3242. part.Name = name;
  3243. }
  3244. }
  3245. public void SetPartDescription(string des, uint localID)
  3246. {
  3247. SceneObjectPart part = GetPart(localID);
  3248. if (part != null)
  3249. {
  3250. part.Description = des;
  3251. }
  3252. }
  3253. public void SetPartText(string text, uint localID)
  3254. {
  3255. SceneObjectPart part = GetPart(localID);
  3256. if (part != null)
  3257. {
  3258. part.SetText(text);
  3259. }
  3260. }
  3261. public void SetPartText(string text, UUID partID)
  3262. {
  3263. SceneObjectPart part = GetPart(partID);
  3264. if (part != null)
  3265. {
  3266. part.SetText(text);
  3267. }
  3268. }
  3269. public string GetPartName(uint localID)
  3270. {
  3271. SceneObjectPart part = GetPart(localID);
  3272. if (part != null)
  3273. {
  3274. return part.Name;
  3275. }
  3276. return String.Empty;
  3277. }
  3278. public string GetPartDescription(uint localID)
  3279. {
  3280. SceneObjectPart part = GetPart(localID);
  3281. if (part != null)
  3282. {
  3283. return part.Description;
  3284. }
  3285. return String.Empty;
  3286. }
  3287. /// <summary>
  3288. /// Update prim flags for this group.
  3289. /// </summary>
  3290. /// <param name="localID"></param>
  3291. /// <param name="UsePhysics"></param>
  3292. /// <param name="SetTemporary"></param>
  3293. /// <param name="SetPhantom"></param>
  3294. /// <param name="SetVolumeDetect"></param>
  3295. public void UpdateFlags(bool UsePhysics, bool SetTemporary, bool SetPhantom, bool SetVolumeDetect)
  3296. {
  3297. if (m_scene == null || IsDeleted)
  3298. return;
  3299. HasGroupChanged = true;
  3300. if (SetTemporary)
  3301. {
  3302. DetachFromBackup();
  3303. // Remove from database and parcel prim count
  3304. //
  3305. m_scene.DeleteFromStorage(UUID);
  3306. }
  3307. else if (!Backup)
  3308. {
  3309. // Previously been temporary now switching back so make it
  3310. // available for persisting again
  3311. AttachToBackup();
  3312. }
  3313. SceneObjectPart[] parts = m_parts.GetArray();
  3314. if (UsePhysics)
  3315. {
  3316. int maxprims = m_scene.m_linksetPhysCapacity;
  3317. bool checkShape = (maxprims > 0 &&
  3318. parts.Length > maxprims);
  3319. for (int i = 0; i < parts.Length; i++)
  3320. {
  3321. SceneObjectPart part = parts[i];
  3322. if(part.PhysicsShapeType == (byte)PhysicsShapeType.None)
  3323. continue; // assuming root type was checked elsewhere
  3324. if (checkShape)
  3325. {
  3326. if (--maxprims < 0)
  3327. {
  3328. UsePhysics = false;
  3329. break;
  3330. }
  3331. }
  3332. if (part.Scale.X > m_scene.m_maxPhys ||
  3333. part.Scale.Y > m_scene.m_maxPhys ||
  3334. part.Scale.Z > m_scene.m_maxPhys )
  3335. {
  3336. UsePhysics = false; // Reset physics
  3337. break;
  3338. }
  3339. }
  3340. }
  3341. if (parts.Length > 1)
  3342. {
  3343. m_rootPart.UpdatePrimFlags(UsePhysics, SetTemporary, SetPhantom, SetVolumeDetect, true);
  3344. for (int i = 0; i < parts.Length; i++)
  3345. {
  3346. if (parts[i].UUID != m_rootPart.UUID)
  3347. parts[i].UpdatePrimFlags(UsePhysics, SetTemporary, SetPhantom, SetVolumeDetect, true);
  3348. }
  3349. if (m_rootPart.PhysActor != null)
  3350. m_rootPart.PhysActor.Building = false;
  3351. }
  3352. else
  3353. m_rootPart.UpdatePrimFlags(UsePhysics, SetTemporary, SetPhantom, SetVolumeDetect, false);
  3354. m_scene.EventManager.TriggerParcelPrimCountTainted();
  3355. }
  3356. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  3357. {
  3358. SceneObjectPart part = GetPart(localID);
  3359. if (part != null)
  3360. {
  3361. part.UpdateExtraParam(type, inUse, data);
  3362. }
  3363. }
  3364. /// <summary>
  3365. /// Gets the number of parts
  3366. /// </summary>
  3367. /// <returns></returns>
  3368. public int GetPartCount()
  3369. {
  3370. return Parts.Count();
  3371. }
  3372. public void AdjustChildPrimPermissions(bool forceTaskInventoryPermissive)
  3373. {
  3374. uint newOwnerMask = (uint)(PermissionMask.All | PermissionMask.Export) & 0xfffffff0; // Mask folded bits
  3375. uint foldedPerms = RootPart.OwnerMask & (uint)PermissionMask.FoldedMask;
  3376. ForEachPart(part =>
  3377. {
  3378. newOwnerMask &= part.BaseMask;
  3379. if (part != RootPart)
  3380. part.ClonePermissions(RootPart);
  3381. if (forceTaskInventoryPermissive)
  3382. part.Inventory.ApplyGodPermissions(part.BaseMask);
  3383. });
  3384. uint lockMask = ~(uint)(PermissionMask.Move);
  3385. uint lockBit = RootPart.OwnerMask & (uint)(PermissionMask.Move);
  3386. RootPart.OwnerMask = (RootPart.OwnerMask & lockBit) | ((newOwnerMask | foldedPerms) & lockMask);
  3387. // m_log.DebugFormat(
  3388. // "[SCENE OBJECT GROUP]: RootPart.OwnerMask now {0} for {1} in {2}",
  3389. // (OpenMetaverse.PermissionMask)RootPart.OwnerMask, Name, Scene.Name);
  3390. InvalidateEffectivePerms();
  3391. RootPart.ScheduleFullUpdate();
  3392. }
  3393. public void UpdatePermissions(UUID AgentID, byte field, uint localID,
  3394. uint mask, byte addRemTF)
  3395. {
  3396. RootPart.UpdatePermissions(AgentID, field, localID, mask, addRemTF);
  3397. bool god = Scene.Permissions.IsGod(AgentID);
  3398. if (field == 1 && god)
  3399. {
  3400. ForEachPart(part =>
  3401. {
  3402. part.BaseMask = RootPart.BaseMask;
  3403. });
  3404. }
  3405. AdjustChildPrimPermissions(false);
  3406. if (field == 1 && god) // Base mask was set. Update all child part inventories
  3407. {
  3408. foreach (SceneObjectPart part in Parts)
  3409. part.Inventory.ApplyGodPermissions(RootPart.BaseMask);
  3410. InvalidateEffectivePerms();
  3411. }
  3412. HasGroupChanged = true;
  3413. // Send the group's properties to all clients once all parts are updated
  3414. IClientAPI client;
  3415. if (Scene.TryGetClient(AgentID, out client))
  3416. SendPropertiesToClient(client);
  3417. }
  3418. #endregion
  3419. #region Shape
  3420. /// <summary>
  3421. ///
  3422. /// </summary>
  3423. /// <param name="shapeBlock"></param>
  3424. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  3425. {
  3426. SceneObjectPart part = GetPart(localID);
  3427. if (part != null)
  3428. {
  3429. part.UpdateShape(shapeBlock);
  3430. PhysicsActor pa = m_rootPart.PhysActor;
  3431. if (pa != null)
  3432. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  3433. }
  3434. InvalidBoundsRadius();
  3435. }
  3436. #endregion
  3437. #region Resize
  3438. /// <summary>
  3439. /// Resize the entire group of prims.
  3440. /// </summary>
  3441. /// <param name="scale"></param>
  3442. public void GroupResize(Vector3 scale)
  3443. {
  3444. // m_log.DebugFormat(
  3445. // "[SCENE OBJECT GROUP]: Group resizing {0} {1} from {2} to {3}", Name, LocalId, RootPart.Scale, scale);
  3446. if (Scene == null)
  3447. return;
  3448. PhysicsActor pa = m_rootPart.PhysActor;
  3449. float minsize = Scene.m_minNonphys;
  3450. float maxsize = Scene.m_maxNonphys;
  3451. if (pa != null && pa.IsPhysical)
  3452. {
  3453. minsize = Scene.m_minPhys;
  3454. maxsize = Scene.m_maxPhys;
  3455. }
  3456. scale.X = Util.Clamp(scale.X, minsize, maxsize);
  3457. scale.Y = Util.Clamp(scale.Y, minsize, maxsize);
  3458. scale.Z = Util.Clamp(scale.Z, minsize, maxsize);
  3459. // requested scaling factors
  3460. float x = (scale.X / RootPart.Scale.X);
  3461. float y = (scale.Y / RootPart.Scale.Y);
  3462. float z = (scale.Z / RootPart.Scale.Z);
  3463. SceneObjectPart[] parts = m_parts.GetArray();
  3464. // fix scaling factors so parts don't violate dimensions
  3465. for(int i = 0;i < parts.Length;i++)
  3466. {
  3467. SceneObjectPart obPart = parts[i];
  3468. if(obPart.UUID != m_rootPart.UUID)
  3469. {
  3470. Vector3 oldSize = new Vector3(obPart.Scale);
  3471. float f = 1.0f;
  3472. float a = 1.0f;
  3473. if(oldSize.X * x > maxsize)
  3474. {
  3475. f = maxsize / oldSize.X;
  3476. a = f / x;
  3477. x *= a;
  3478. y *= a;
  3479. z *= a;
  3480. }
  3481. else if(oldSize.X * x < minsize)
  3482. {
  3483. f = minsize / oldSize.X;
  3484. a = f / x;
  3485. x *= a;
  3486. y *= a;
  3487. z *= a;
  3488. }
  3489. if(oldSize.Y * y > maxsize)
  3490. {
  3491. f = maxsize / oldSize.Y;
  3492. a = f / y;
  3493. x *= a;
  3494. y *= a;
  3495. z *= a;
  3496. }
  3497. else if(oldSize.Y * y < minsize)
  3498. {
  3499. f = minsize / oldSize.Y;
  3500. a = f / y;
  3501. x *= a;
  3502. y *= a;
  3503. z *= a;
  3504. }
  3505. if(oldSize.Z * z > maxsize)
  3506. {
  3507. f = maxsize / oldSize.Z;
  3508. a = f / z;
  3509. x *= a;
  3510. y *= a;
  3511. z *= a;
  3512. }
  3513. else if(oldSize.Z * z < minsize)
  3514. {
  3515. f = minsize / oldSize.Z;
  3516. a = f / z;
  3517. x *= a;
  3518. y *= a;
  3519. z *= a;
  3520. }
  3521. }
  3522. }
  3523. Vector3 rootScale = RootPart.Scale;
  3524. rootScale.X *= x;
  3525. rootScale.Y *= y;
  3526. rootScale.Z *= z;
  3527. RootPart.Scale = rootScale;
  3528. for (int i = 0; i < parts.Length; i++)
  3529. {
  3530. SceneObjectPart obPart = parts[i];
  3531. if (obPart.UUID != m_rootPart.UUID)
  3532. {
  3533. Vector3 currentpos = new Vector3(obPart.OffsetPosition);
  3534. currentpos.X *= x;
  3535. currentpos.Y *= y;
  3536. currentpos.Z *= z;
  3537. Vector3 newSize = new Vector3(obPart.Scale);
  3538. newSize.X *= x;
  3539. newSize.Y *= y;
  3540. newSize.Z *= z;
  3541. obPart.Scale = newSize;
  3542. obPart.UpdateOffSet(currentpos);
  3543. }
  3544. }
  3545. InvalidBoundsRadius();
  3546. HasGroupChanged = true;
  3547. m_rootPart.TriggerScriptChangedEvent(Changed.SCALE);
  3548. ScheduleGroupForFullUpdate();
  3549. }
  3550. public bool GroupResize(double fscale)
  3551. {
  3552. // m_log.DebugFormat(
  3553. // "[SCENE OBJECT GROUP]: Group resizing {0} {1} from {2} to {3}", Name, LocalId, RootPart.Scale, fscale);
  3554. if (Scene == null || IsDeleted || inTransit || fscale < 0)
  3555. return false;
  3556. // ignore lsl restrictions. let them be done a LSL
  3557. PhysicsActor pa = m_rootPart.PhysActor;
  3558. if(RootPart.KeyframeMotion != null)
  3559. RootPart.KeyframeMotion.Suspend();
  3560. float minsize = Scene.m_minNonphys;
  3561. float maxsize = Scene.m_maxNonphys;
  3562. // assuming physics is more restrictive
  3563. if (pa != null && pa.IsPhysical)
  3564. {
  3565. minsize = Scene.m_minPhys;
  3566. maxsize = Scene.m_maxPhys;
  3567. }
  3568. SceneObjectPart[] parts = m_parts.GetArray();
  3569. float tmp;
  3570. // check scaling factor so parts don't violate dimensions
  3571. for(int i = 0; i < parts.Length; i++)
  3572. {
  3573. SceneObjectPart obPart = parts[i];
  3574. Vector3 oldSize = new Vector3(obPart.Scale);
  3575. tmp = (float)(oldSize.X * fscale);
  3576. if(tmp > maxsize)
  3577. return false;
  3578. if(tmp < minsize)
  3579. return false;
  3580. tmp = (float)(oldSize.Y * fscale);
  3581. if(tmp > maxsize)
  3582. return false;
  3583. if(tmp < minsize)
  3584. return false;
  3585. tmp = (float)(oldSize.Z * fscale);
  3586. if(tmp > maxsize)
  3587. return false;
  3588. if(tmp < minsize)
  3589. return false;
  3590. }
  3591. Vector3 newSize = RootPart.Scale;
  3592. newSize.X = (float)(newSize.X * fscale);
  3593. newSize.Y = (float)(newSize.Y * fscale);
  3594. newSize.Z = (float)(newSize.Z * fscale);
  3595. if(pa != null)
  3596. pa.Building = true;
  3597. RootPart.Scale = newSize;
  3598. Vector3 currentpos;
  3599. for (int i = 0; i < parts.Length; i++)
  3600. {
  3601. SceneObjectPart obPart = parts[i];
  3602. if (obPart.UUID != m_rootPart.UUID)
  3603. {
  3604. currentpos = obPart.OffsetPosition;
  3605. currentpos.X = (float)(currentpos.X * fscale);
  3606. currentpos.Y = (float)(currentpos.Y * fscale);
  3607. currentpos.Z = (float)(currentpos.Z * fscale);
  3608. newSize = obPart.Scale;
  3609. newSize.X = (float)(newSize.X * fscale);
  3610. newSize.Y = (float)(newSize.Y * fscale);
  3611. newSize.Z = (float)(newSize.Z * fscale);
  3612. obPart.Scale = newSize;
  3613. obPart.UpdateOffSet(currentpos);
  3614. }
  3615. }
  3616. if(pa != null)
  3617. pa.Building = false;
  3618. InvalidBoundsRadius();
  3619. HasGroupChanged = true;
  3620. m_rootPart.TriggerScriptChangedEvent(Changed.SCALE);
  3621. ScheduleGroupForFullUpdate();
  3622. if(RootPart.KeyframeMotion != null)
  3623. RootPart.KeyframeMotion.Resume();
  3624. return true;
  3625. }
  3626. public float GetMaxGroupResizeScale()
  3627. {
  3628. if (Scene == null || IsDeleted || inTransit)
  3629. return 1.0f;
  3630. float maxsize = Scene.m_maxNonphys;
  3631. PhysicsActor pa = m_rootPart.PhysActor;
  3632. // assuming physics is more restrictive
  3633. if (pa != null && pa.IsPhysical)
  3634. maxsize = Scene.m_maxPhys;
  3635. SceneObjectPart[] parts = m_parts.GetArray();
  3636. float larger = float.MinValue;
  3637. for(int i = 0; i < parts.Length; i++)
  3638. {
  3639. SceneObjectPart obPart = parts[i];
  3640. Vector3 oldSize = new Vector3(obPart.Scale);
  3641. if(larger < oldSize.X)
  3642. larger = oldSize.X;
  3643. if(larger < oldSize.Y)
  3644. larger = oldSize.Y;
  3645. if(larger < oldSize.Z)
  3646. larger = oldSize.Z;
  3647. }
  3648. if(larger >= maxsize)
  3649. return 1.0f;
  3650. larger += 1e-3f;
  3651. float fscale = maxsize / larger;
  3652. return fscale;
  3653. }
  3654. public float GetMinGroupResizeScale()
  3655. {
  3656. if (Scene == null || IsDeleted || inTransit)
  3657. return 1.0f;
  3658. float minsize = Scene.m_minNonphys;
  3659. PhysicsActor pa = m_rootPart.PhysActor;
  3660. // assuming physics is more restrictive
  3661. if (pa != null && pa.IsPhysical)
  3662. minsize = Scene.m_minPhys;
  3663. SceneObjectPart[] parts = m_parts.GetArray();
  3664. float smaller = float.MaxValue;
  3665. for(int i = 0; i < parts.Length; i++)
  3666. {
  3667. SceneObjectPart obPart = parts[i];
  3668. Vector3 oldSize = new Vector3(obPart.Scale);
  3669. if(smaller > oldSize.X)
  3670. smaller = oldSize.X;
  3671. if(smaller > oldSize.Y)
  3672. smaller = oldSize.Y;
  3673. if(smaller > oldSize.Z)
  3674. smaller = oldSize.Z;
  3675. }
  3676. if(smaller <= minsize)
  3677. return 1.0f;
  3678. if(smaller > 2e-3f)
  3679. smaller -= 1e-3f;
  3680. float fscale = minsize / smaller;
  3681. if(fscale < 1e-8f)
  3682. fscale = 1e-8f;
  3683. return fscale;
  3684. }
  3685. #endregion
  3686. #region Position
  3687. /// <summary>
  3688. /// Move this scene object
  3689. /// </summary>
  3690. /// <param name="pos"></param>
  3691. public void UpdateGroupPosition(Vector3 pos)
  3692. {
  3693. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  3694. {
  3695. if (IsAttachment)
  3696. {
  3697. m_rootPart.AttachedPos = pos;
  3698. }
  3699. if (RootPart.GetStatusSandbox())
  3700. {
  3701. if (Vector3.DistanceSquared(RootPart.StatusSandboxPos, pos) > 100)
  3702. {
  3703. RootPart.ScriptSetPhysicsStatus(false);
  3704. pos = AbsolutePosition;
  3705. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  3706. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  3707. }
  3708. }
  3709. AbsolutePosition = pos;
  3710. HasGroupChanged = true;
  3711. }
  3712. //we need to do a terse update even if the move wasn't allowed
  3713. // so that the position is reset in the client (the object snaps back)
  3714. RootPart.ScheduleTerseUpdate();
  3715. }
  3716. /// <summary>
  3717. /// Update the position of a single part of this scene object
  3718. /// </summary>
  3719. /// <param name="pos"></param>
  3720. /// <param name="localID"></param>
  3721. ///
  3722. public void UpdateSinglePosition(Vector3 pos, uint localID)
  3723. {
  3724. SceneObjectPart part = GetPart(localID);
  3725. if (part != null)
  3726. {
  3727. // unlock parts position change
  3728. if (m_rootPart.PhysActor != null)
  3729. m_rootPart.PhysActor.Building = true;
  3730. if (part.UUID == m_rootPart.UUID)
  3731. {
  3732. UpdateRootPosition(pos);
  3733. }
  3734. else
  3735. {
  3736. part.UpdateOffSet(pos);
  3737. }
  3738. if (m_rootPart.PhysActor != null)
  3739. m_rootPart.PhysActor.Building = false;
  3740. HasGroupChanged = true;
  3741. }
  3742. }
  3743. /// <summary>
  3744. /// Update just the root prim position in a linkset
  3745. /// </summary>
  3746. /// <param name="newPos"></param>
  3747. public void UpdateRootPosition(Vector3 newPos)
  3748. {
  3749. // needs to be called with phys building true
  3750. Vector3 oldPos;
  3751. if (IsAttachment)
  3752. oldPos = m_rootPart.AttachedPos + m_rootPart.OffsetPosition; // OffsetPosition should always be 0 in an attachments's root prim
  3753. else
  3754. oldPos = AbsolutePosition + m_rootPart.OffsetPosition;
  3755. Vector3 diff = oldPos - newPos;
  3756. Quaternion partRotation = m_rootPart.RotationOffset;
  3757. diff *= Quaternion.Inverse(partRotation);
  3758. SceneObjectPart[] parts = m_parts.GetArray();
  3759. for (int i = 0; i < parts.Length; i++)
  3760. {
  3761. SceneObjectPart obPart = parts[i];
  3762. if (obPart.UUID != m_rootPart.UUID)
  3763. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  3764. }
  3765. AbsolutePosition = newPos;
  3766. if (IsAttachment)
  3767. m_rootPart.AttachedPos = newPos;
  3768. HasGroupChanged = true;
  3769. if (m_rootPart.Undoing)
  3770. {
  3771. ScheduleGroupForFullUpdate();
  3772. }
  3773. else
  3774. {
  3775. ScheduleGroupForTerseUpdate();
  3776. }
  3777. }
  3778. #endregion
  3779. #region Rotation
  3780. /// <summary>
  3781. /// Update the rotation of the group.
  3782. /// </summary>
  3783. /// <param name="rot"></param>
  3784. public void UpdateGroupRotationR(Quaternion rot)
  3785. {
  3786. m_rootPart.UpdateRotation(rot);
  3787. /* this is done by rootpart RotationOffset set called by UpdateRotation
  3788. PhysicsActor actor = m_rootPart.PhysActor;
  3789. if (actor != null)
  3790. {
  3791. actor.Orientation = m_rootPart.RotationOffset;
  3792. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  3793. }
  3794. */
  3795. HasGroupChanged = true;
  3796. ScheduleGroupForTerseUpdate();
  3797. }
  3798. /// <summary>
  3799. /// Update the position and rotation of a group simultaneously.
  3800. /// </summary>
  3801. /// <param name="pos"></param>
  3802. /// <param name="rot"></param>
  3803. public void UpdateGroupRotationPR(Vector3 pos, Quaternion rot)
  3804. {
  3805. m_rootPart.UpdateRotation(rot);
  3806. PhysicsActor actor = m_rootPart.PhysActor;
  3807. if (actor != null)
  3808. {
  3809. actor.Orientation = m_rootPart.RotationOffset;
  3810. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  3811. }
  3812. if (IsAttachment)
  3813. {
  3814. m_rootPart.AttachedPos = pos;
  3815. }
  3816. AbsolutePosition = pos;
  3817. HasGroupChanged = true;
  3818. ScheduleGroupForTerseUpdate();
  3819. }
  3820. /// <summary>
  3821. /// Update the rotation of a single prim within the group.
  3822. /// </summary>
  3823. /// <param name="rot"></param>
  3824. /// <param name="localID"></param>
  3825. public void UpdateSingleRotation(Quaternion rot, uint localID)
  3826. {
  3827. SceneObjectPart part = GetPart(localID);
  3828. SceneObjectPart[] parts = m_parts.GetArray();
  3829. if (part != null)
  3830. {
  3831. if (m_rootPart.PhysActor != null)
  3832. m_rootPart.PhysActor.Building = true;
  3833. if (part.UUID == m_rootPart.UUID)
  3834. {
  3835. UpdateRootRotation(rot);
  3836. }
  3837. else
  3838. {
  3839. part.UpdateRotation(rot);
  3840. }
  3841. if (m_rootPart.PhysActor != null)
  3842. m_rootPart.PhysActor.Building = false;
  3843. }
  3844. }
  3845. /// <summary>
  3846. /// Update the position and rotation simultaneously of a single prim within the group.
  3847. /// </summary>
  3848. /// <param name="rot"></param>
  3849. /// <param name="localID"></param>
  3850. public void UpdateSingleRotation(Quaternion rot, Vector3 pos, uint localID)
  3851. {
  3852. SceneObjectPart part = GetPart(localID);
  3853. if (part != null)
  3854. {
  3855. if (m_rootPart.PhysActor != null)
  3856. m_rootPart.PhysActor.Building = true;
  3857. if (part.UUID == m_rootPart.UUID)
  3858. {
  3859. UpdateRootRotation(rot);
  3860. AbsolutePosition = pos;
  3861. }
  3862. else
  3863. {
  3864. part.UpdateRotation(rot);
  3865. part.OffsetPosition = pos;
  3866. }
  3867. if (m_rootPart.PhysActor != null)
  3868. m_rootPart.PhysActor.Building = false;
  3869. }
  3870. }
  3871. /// <summary>
  3872. /// Update the rotation of just the root prim of a linkset.
  3873. /// </summary>
  3874. /// <param name="rot"></param>
  3875. public void UpdateRootRotation(Quaternion rot)
  3876. {
  3877. // needs to be called with phys building true
  3878. Quaternion axRot = rot;
  3879. Quaternion oldParentRot = m_rootPart.RotationOffset;
  3880. //Don't use UpdateRotation because it schedules an update prematurely
  3881. m_rootPart.RotationOffset = rot;
  3882. PhysicsActor pa = m_rootPart.PhysActor;
  3883. if (pa != null)
  3884. {
  3885. pa.Orientation = m_rootPart.RotationOffset;
  3886. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  3887. }
  3888. SceneObjectPart[] parts = m_parts.GetArray();
  3889. for (int i = 0; i < parts.Length; i++)
  3890. {
  3891. SceneObjectPart prim = parts[i];
  3892. if (prim.UUID != m_rootPart.UUID)
  3893. {
  3894. Quaternion NewRot = oldParentRot * prim.RotationOffset;
  3895. NewRot = Quaternion.Inverse(axRot) * NewRot;
  3896. prim.RotationOffset = NewRot;
  3897. Vector3 axPos = prim.OffsetPosition;
  3898. axPos *= oldParentRot;
  3899. axPos *= Quaternion.Inverse(axRot);
  3900. prim.OffsetPosition = axPos;
  3901. }
  3902. }
  3903. HasGroupChanged = true;
  3904. ScheduleGroupForFullUpdate();
  3905. }
  3906. private enum updatetype :int
  3907. {
  3908. none = 0,
  3909. partterse = 1,
  3910. partfull = 2,
  3911. groupterse = 3,
  3912. groupfull = 4
  3913. }
  3914. public void doChangeObject(SceneObjectPart part, ObjectChangeData data)
  3915. {
  3916. // TODO this still as excessive *.Schedule*Update()s
  3917. if (part != null && part.ParentGroup != null)
  3918. {
  3919. ObjectChangeType change = data.change;
  3920. bool togroup = ((change & ObjectChangeType.Group) != 0);
  3921. // bool uniform = ((what & ObjectChangeType.UniformScale) != 0); not in use
  3922. SceneObjectGroup group = part.ParentGroup;
  3923. PhysicsActor pha = group.RootPart.PhysActor;
  3924. updatetype updateType = updatetype.none;
  3925. if (togroup)
  3926. {
  3927. // related to group
  3928. if ((change & (ObjectChangeType.Rotation | ObjectChangeType.Position)) != 0)
  3929. {
  3930. if ((change & ObjectChangeType.Rotation) != 0)
  3931. {
  3932. group.RootPart.UpdateRotation(data.rotation);
  3933. updateType = updatetype.none;
  3934. }
  3935. if ((change & ObjectChangeType.Position) != 0)
  3936. {
  3937. if (IsAttachment || m_scene.Permissions.CanObjectEntry(group, false, data.position))
  3938. UpdateGroupPosition(data.position);
  3939. updateType = updatetype.groupterse;
  3940. }
  3941. else
  3942. // ugly rotation update of all parts
  3943. {
  3944. group.ResetChildPrimPhysicsPositions();
  3945. }
  3946. }
  3947. if ((change & ObjectChangeType.Scale) != 0)
  3948. {
  3949. if (pha != null)
  3950. pha.Building = true;
  3951. group.GroupResize(data.scale);
  3952. updateType = updatetype.none;
  3953. if (pha != null)
  3954. pha.Building = false;
  3955. }
  3956. }
  3957. else
  3958. {
  3959. // related to single prim in a link-set ( ie group)
  3960. if (pha != null)
  3961. pha.Building = true;
  3962. // root part is special
  3963. // parts offset positions or rotations need to change also
  3964. if (part == group.RootPart)
  3965. {
  3966. if ((change & ObjectChangeType.Rotation) != 0)
  3967. group.UpdateRootRotation(data.rotation);
  3968. if ((change & ObjectChangeType.Position) != 0)
  3969. group.UpdateRootPosition(data.position);
  3970. if ((change & ObjectChangeType.Scale) != 0)
  3971. part.Resize(data.scale);
  3972. }
  3973. else
  3974. {
  3975. if ((change & ObjectChangeType.Position) != 0)
  3976. {
  3977. part.OffsetPosition = data.position;
  3978. updateType = updatetype.partterse;
  3979. }
  3980. if ((change & ObjectChangeType.Rotation) != 0)
  3981. {
  3982. part.UpdateRotation(data.rotation);
  3983. updateType = updatetype.none;
  3984. }
  3985. if ((change & ObjectChangeType.Scale) != 0)
  3986. {
  3987. part.Resize(data.scale);
  3988. updateType = updatetype.none;
  3989. }
  3990. }
  3991. if (pha != null)
  3992. pha.Building = false;
  3993. }
  3994. if (updateType != updatetype.none)
  3995. {
  3996. group.HasGroupChanged = true;
  3997. switch (updateType)
  3998. {
  3999. case updatetype.partterse:
  4000. part.ScheduleTerseUpdate();
  4001. break;
  4002. case updatetype.partfull:
  4003. part.ScheduleFullUpdate();
  4004. break;
  4005. case updatetype.groupterse:
  4006. group.ScheduleGroupForTerseUpdate();
  4007. break;
  4008. case updatetype.groupfull:
  4009. group.ScheduleGroupForFullUpdate();
  4010. break;
  4011. default:
  4012. break;
  4013. }
  4014. }
  4015. }
  4016. }
  4017. #endregion
  4018. internal void SetAxisRotation(int axis, int rotate10)
  4019. {
  4020. bool setX = ((axis & (int)SceneObjectGroup.axisSelect.STATUS_ROTATE_X) != 0);
  4021. bool setY = ((axis & (int)SceneObjectGroup.axisSelect.STATUS_ROTATE_Y) != 0);
  4022. bool setZ = ((axis & (int)SceneObjectGroup.axisSelect.STATUS_ROTATE_Z) != 0);
  4023. if (setX || setY || setZ)
  4024. {
  4025. bool lockaxis = (rotate10 == 0); // zero means axis locked
  4026. byte locks = RootPart.RotationAxisLocks;
  4027. if (setX)
  4028. {
  4029. if(lockaxis)
  4030. locks |= (byte)SceneObjectGroup.axisSelect.STATUS_ROTATE_X;
  4031. else
  4032. locks &= (byte)SceneObjectGroup.axisSelect.NOT_STATUS_ROTATE_X;
  4033. }
  4034. if (setY)
  4035. {
  4036. if(lockaxis)
  4037. locks |= (byte)SceneObjectGroup.axisSelect.STATUS_ROTATE_Y;
  4038. else
  4039. locks &= (byte)SceneObjectGroup.axisSelect.NOT_STATUS_ROTATE_Y;
  4040. }
  4041. if (setZ)
  4042. {
  4043. if(lockaxis)
  4044. locks |= (byte)SceneObjectGroup.axisSelect.STATUS_ROTATE_Z;
  4045. else
  4046. locks &= (byte)SceneObjectGroup.axisSelect.NOT_STATUS_ROTATE_Z;
  4047. }
  4048. RootPart.RotationAxisLocks = locks;
  4049. RootPart.SetPhysicsAxisRotation();
  4050. }
  4051. }
  4052. public int GetAxisRotation(int axis)
  4053. {
  4054. byte rotAxislocks = RootPart.RotationAxisLocks;
  4055. // if multiple return the one with higher id
  4056. if (axis == (int)SceneObjectGroup.axisSelect.STATUS_ROTATE_Z)
  4057. return (rotAxislocks & (byte)SceneObjectGroup.axisSelect.STATUS_ROTATE_Z) == 0 ? 1:0;
  4058. if (axis == (int)SceneObjectGroup.axisSelect.STATUS_ROTATE_Y)
  4059. return (rotAxislocks & (byte)SceneObjectGroup.axisSelect.STATUS_ROTATE_Y) == 0 ? 1:0;
  4060. if (axis == (int)SceneObjectGroup.axisSelect.STATUS_ROTATE_X)
  4061. return (rotAxislocks & (byte)SceneObjectGroup.axisSelect.STATUS_ROTATE_X) == 0 ? 1:0;
  4062. return 0;
  4063. }
  4064. public int registerRotTargetWaypoint(Quaternion target, float tolerance)
  4065. {
  4066. scriptRotTarget waypoint = new scriptRotTarget();
  4067. waypoint.targetRot = target;
  4068. waypoint.tolerance = tolerance;
  4069. uint handle = m_scene.AllocateLocalId();
  4070. waypoint.handle = handle;
  4071. lock (m_rotTargets)
  4072. {
  4073. if (m_rotTargets.Count >= 8)
  4074. m_rotTargets.Remove(m_rotTargets.ElementAt(0).Key);
  4075. m_rotTargets.Add(handle, waypoint);
  4076. }
  4077. m_scene.AddGroupTarget(this);
  4078. return (int)handle;
  4079. }
  4080. public void unregisterRotTargetWaypoint(int handle)
  4081. {
  4082. lock (m_targets)
  4083. {
  4084. m_rotTargets.Remove((uint)handle);
  4085. if (m_targets.Count == 0)
  4086. m_scene.RemoveGroupTarget(this);
  4087. }
  4088. }
  4089. public int registerTargetWaypoint(Vector3 target, float tolerance)
  4090. {
  4091. scriptPosTarget waypoint = new scriptPosTarget();
  4092. waypoint.targetPos = target;
  4093. waypoint.tolerance = tolerance;
  4094. uint handle = m_scene.AllocateLocalId();
  4095. waypoint.handle = handle;
  4096. lock (m_targets)
  4097. {
  4098. if (m_targets.Count >= 8)
  4099. m_targets.Remove(m_targets.ElementAt(0).Key);
  4100. m_targets.Add(handle, waypoint);
  4101. }
  4102. m_scene.AddGroupTarget(this);
  4103. return (int)handle;
  4104. }
  4105. public void unregisterTargetWaypoint(int handle)
  4106. {
  4107. lock (m_targets)
  4108. {
  4109. m_targets.Remove((uint)handle);
  4110. if (m_targets.Count == 0)
  4111. m_scene.RemoveGroupTarget(this);
  4112. }
  4113. }
  4114. public void checkAtTargets()
  4115. {
  4116. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  4117. {
  4118. if (m_targets.Count > 0)
  4119. {
  4120. bool at_target = false;
  4121. //Vector3 targetPos;
  4122. //uint targetHandle;
  4123. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  4124. lock (m_targets)
  4125. {
  4126. foreach (uint idx in m_targets.Keys)
  4127. {
  4128. scriptPosTarget target = m_targets[idx];
  4129. if (Vector3.DistanceSquared(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance * target.tolerance)
  4130. {
  4131. at_target = true;
  4132. // trigger at_target
  4133. if (m_scriptListens_atTarget)
  4134. {
  4135. scriptPosTarget att = new scriptPosTarget();
  4136. att.targetPos = target.targetPos;
  4137. att.tolerance = target.tolerance;
  4138. att.handle = target.handle;
  4139. atTargets.Add(idx, att);
  4140. }
  4141. }
  4142. }
  4143. }
  4144. if (atTargets.Count > 0)
  4145. {
  4146. SceneObjectPart[] parts = m_parts.GetArray();
  4147. uint[] localids = new uint[parts.Length];
  4148. for (int i = 0; i < parts.Length; i++)
  4149. localids[i] = parts[i].LocalId;
  4150. for (int ctr = 0; ctr < localids.Length; ctr++)
  4151. {
  4152. foreach (uint target in atTargets.Keys)
  4153. {
  4154. scriptPosTarget att = atTargets[target];
  4155. m_scene.EventManager.TriggerAtTargetEvent(
  4156. localids[ctr], att.handle, att.targetPos, m_rootPart.GroupPosition);
  4157. }
  4158. }
  4159. return;
  4160. }
  4161. if (m_scriptListens_notAtTarget && !at_target)
  4162. {
  4163. //trigger not_at_target
  4164. SceneObjectPart[] parts = m_parts.GetArray();
  4165. uint[] localids = new uint[parts.Length];
  4166. for (int i = 0; i < parts.Length; i++)
  4167. localids[i] = parts[i].LocalId;
  4168. for (int ctr = 0; ctr < localids.Length; ctr++)
  4169. {
  4170. m_scene.EventManager.TriggerNotAtTargetEvent(localids[ctr]);
  4171. }
  4172. }
  4173. }
  4174. }
  4175. if (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget)
  4176. {
  4177. if (m_rotTargets.Count > 0)
  4178. {
  4179. bool at_Rottarget = false;
  4180. Dictionary<uint, scriptRotTarget> atRotTargets = new Dictionary<uint, scriptRotTarget>();
  4181. lock (m_rotTargets)
  4182. {
  4183. foreach (uint idx in m_rotTargets.Keys)
  4184. {
  4185. scriptRotTarget target = m_rotTargets[idx];
  4186. double angle
  4187. = Math.Acos(
  4188. target.targetRot.X * m_rootPart.RotationOffset.X
  4189. + target.targetRot.Y * m_rootPart.RotationOffset.Y
  4190. + target.targetRot.Z * m_rootPart.RotationOffset.Z
  4191. + target.targetRot.W * m_rootPart.RotationOffset.W)
  4192. * 2;
  4193. if (angle < 0) angle = -angle;
  4194. if (angle > Math.PI) angle = (Math.PI * 2 - angle);
  4195. if (angle <= target.tolerance)
  4196. {
  4197. // trigger at_rot_target
  4198. if (m_scriptListens_atRotTarget)
  4199. {
  4200. at_Rottarget = true;
  4201. scriptRotTarget att = new scriptRotTarget();
  4202. att.targetRot = target.targetRot;
  4203. att.tolerance = target.tolerance;
  4204. att.handle = target.handle;
  4205. atRotTargets.Add(idx, att);
  4206. }
  4207. }
  4208. }
  4209. }
  4210. if (atRotTargets.Count > 0)
  4211. {
  4212. SceneObjectPart[] parts = m_parts.GetArray();
  4213. uint[] localids = new uint[parts.Length];
  4214. for (int i = 0; i < parts.Length; i++)
  4215. localids[i] = parts[i].LocalId;
  4216. for (int ctr = 0; ctr < localids.Length; ctr++)
  4217. {
  4218. foreach (uint target in atRotTargets.Keys)
  4219. {
  4220. scriptRotTarget att = atRotTargets[target];
  4221. m_scene.EventManager.TriggerAtRotTargetEvent(
  4222. localids[ctr], att.handle, att.targetRot, m_rootPart.RotationOffset);
  4223. }
  4224. }
  4225. return;
  4226. }
  4227. if (m_scriptListens_notAtRotTarget && !at_Rottarget)
  4228. {
  4229. //trigger not_at_target
  4230. SceneObjectPart[] parts = m_parts.GetArray();
  4231. uint[] localids = new uint[parts.Length];
  4232. for (int i = 0; i < parts.Length; i++)
  4233. localids[i] = parts[i].LocalId;
  4234. for (int ctr = 0; ctr < localids.Length; ctr++)
  4235. {
  4236. m_scene.EventManager.TriggerNotAtRotTargetEvent(localids[ctr]);
  4237. }
  4238. }
  4239. }
  4240. }
  4241. }
  4242. public Vector3 GetGeometricCenter()
  4243. {
  4244. // this is not real geometric center but a average of positions relative to root prim acording to
  4245. // http://wiki.secondlife.com/wiki/llGetGeometricCenter
  4246. // ignoring tortured prims details since sl also seems to ignore
  4247. // so no real use in doing it on physics
  4248. Vector3 gc = Vector3.Zero;
  4249. SceneObjectPart[] parts = m_parts.GetArray();
  4250. int nparts = parts.Length;
  4251. if (nparts < 2)
  4252. return gc;
  4253. // average all parts positions
  4254. for (int i = 0; i < nparts; i++)
  4255. {
  4256. if (parts[i] != RootPart)
  4257. gc += parts[i].OffsetPosition;
  4258. }
  4259. gc /= nparts;
  4260. return gc;
  4261. }
  4262. public float GetMass()
  4263. {
  4264. float retmass = 0f;
  4265. SceneObjectPart[] parts = m_parts.GetArray();
  4266. for (int i = 0; i < parts.Length; i++)
  4267. retmass += parts[i].GetMass();
  4268. return retmass;
  4269. }
  4270. // center of mass of full object
  4271. public Vector3 GetCenterOfMass()
  4272. {
  4273. PhysicsActor pa = RootPart.PhysActor;
  4274. if(((RootPart.Flags & PrimFlags.Physics) !=0) && pa !=null)
  4275. {
  4276. // physics knows better about center of mass of physical prims
  4277. Vector3 tmp = pa.CenterOfMass;
  4278. return tmp;
  4279. }
  4280. Vector3 Ptot = Vector3.Zero;
  4281. float totmass = 0f;
  4282. float m;
  4283. SceneObjectPart[] parts = m_parts.GetArray();
  4284. for (int i = 0; i < parts.Length; i++)
  4285. {
  4286. m = parts[i].GetMass();
  4287. Ptot += parts[i].GetPartCenterOfMass() * m;
  4288. totmass += m;
  4289. }
  4290. if (totmass == 0)
  4291. totmass = 0;
  4292. else
  4293. totmass = 1 / totmass;
  4294. Ptot *= totmass;
  4295. return Ptot;
  4296. }
  4297. public void GetInertiaData(out float TotalMass, out Vector3 CenterOfMass, out Vector3 Inertia, out Vector4 aux )
  4298. {
  4299. PhysicsActor pa = RootPart.PhysActor;
  4300. if(((RootPart.Flags & PrimFlags.Physics) !=0) && pa !=null)
  4301. {
  4302. PhysicsInertiaData inertia;
  4303. inertia = pa.GetInertiaData();
  4304. TotalMass = inertia.TotalMass;
  4305. CenterOfMass = inertia.CenterOfMass;
  4306. Inertia = inertia.Inertia;
  4307. aux = inertia.InertiaRotation;
  4308. return;
  4309. }
  4310. TotalMass = GetMass();
  4311. CenterOfMass = GetCenterOfMass() - AbsolutePosition;
  4312. CenterOfMass *= Quaternion.Conjugate(RootPart.RotationOffset);
  4313. Inertia = Vector3.Zero;
  4314. aux = Vector4.Zero;
  4315. }
  4316. public void SetInertiaData(float TotalMass, Vector3 CenterOfMass, Vector3 Inertia, Vector4 aux )
  4317. {
  4318. PhysicsInertiaData inertia = new PhysicsInertiaData();
  4319. inertia.TotalMass = TotalMass;
  4320. inertia.CenterOfMass = CenterOfMass;
  4321. inertia.Inertia = Inertia;
  4322. inertia.InertiaRotation = aux;
  4323. if(TotalMass < 0)
  4324. RootPart.PhysicsInertia = null;
  4325. else
  4326. RootPart.PhysicsInertia = new PhysicsInertiaData(inertia);
  4327. PhysicsActor pa = RootPart.PhysActor;
  4328. if(pa !=null)
  4329. pa.SetInertiaData(inertia);
  4330. }
  4331. /// <summary>
  4332. /// Set the user group to which this scene object belongs.
  4333. /// </summary>
  4334. /// <param name="GroupID"></param>
  4335. /// <param name="client"></param>
  4336. public void SetGroup(UUID GroupID, IClientAPI client)
  4337. {
  4338. SceneObjectPart[] parts = m_parts.GetArray();
  4339. for (int i = 0; i < parts.Length; i++)
  4340. {
  4341. SceneObjectPart part = parts[i];
  4342. part.SetGroup(GroupID, client);
  4343. part.Inventory.ChangeInventoryGroup(GroupID);
  4344. }
  4345. HasGroupChanged = true;
  4346. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  4347. // for the same object with very different properties. The caller must schedule the update.
  4348. //ScheduleGroupForFullUpdate();
  4349. }
  4350. public void TriggerScriptChangedEvent(Changed val)
  4351. {
  4352. SceneObjectPart[] parts = m_parts.GetArray();
  4353. for (int i = 0; i < parts.Length; i++)
  4354. parts[i].TriggerScriptChangedEvent(val);
  4355. }
  4356. /// <summary>
  4357. /// Returns a count of the number of scripts in this groups parts.
  4358. /// </summary>
  4359. public int ScriptCount()
  4360. {
  4361. int count = 0;
  4362. SceneObjectPart[] parts = m_parts.GetArray();
  4363. for (int i = 0; i < parts.Length; i++)
  4364. count += parts[i].Inventory.ScriptCount();
  4365. return count;
  4366. }
  4367. /// <summary>
  4368. /// A float the value is a representative execution time in milliseconds of all scripts in the link set.
  4369. /// </summary>
  4370. public float ScriptExecutionTime()
  4371. {
  4372. IScriptModule[] engines = Scene.RequestModuleInterfaces<IScriptModule>();
  4373. if (engines.Length == 0) // No engine at all
  4374. return 0.0f;
  4375. float time = 0.0f;
  4376. // get all the scripts in all parts
  4377. SceneObjectPart[] parts = m_parts.GetArray();
  4378. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>();
  4379. for (int i = 0; i < parts.Length; i++)
  4380. {
  4381. scripts.AddRange(parts[i].Inventory.GetInventoryItems(InventoryType.LSL));
  4382. }
  4383. // extract the UUIDs
  4384. List<UUID> ids = new List<UUID>(scripts.Count);
  4385. foreach (TaskInventoryItem script in scripts)
  4386. {
  4387. if (!ids.Contains(script.ItemID))
  4388. {
  4389. ids.Add(script.ItemID);
  4390. }
  4391. }
  4392. // Offer the list of script UUIDs to each engine found and accumulate the time
  4393. foreach (IScriptModule e in engines)
  4394. {
  4395. if (e != null)
  4396. {
  4397. time += e.GetScriptExecutionTime(ids);
  4398. }
  4399. }
  4400. return time;
  4401. }
  4402. /// <summary>
  4403. /// Returns a count of the number of running scripts in this groups parts.
  4404. /// </summary>
  4405. public int RunningScriptCount()
  4406. {
  4407. int count = 0;
  4408. SceneObjectPart[] parts = m_parts.GetArray();
  4409. for (int i = 0; i < parts.Length; i++)
  4410. count += parts[i].Inventory.RunningScriptCount();
  4411. return count;
  4412. }
  4413. /// <summary>
  4414. /// Get a copy of the list of sitting avatars on all prims of this object.
  4415. /// </summary>
  4416. /// <remarks>
  4417. /// This is sorted by the order in which avatars sat down. If an avatar stands up then all avatars that sat
  4418. /// down after it move one place down the list.
  4419. /// </remarks>
  4420. /// <returns>A list of the sitting avatars. Returns an empty list if there are no sitting avatars.</returns>
  4421. public List<ScenePresence> GetSittingAvatars()
  4422. {
  4423. lock (m_sittingAvatars)
  4424. return new List<ScenePresence>(m_sittingAvatars);
  4425. }
  4426. /// <summary>
  4427. /// Gets the number of sitting avatars.
  4428. /// </summary>
  4429. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  4430. /// <returns></returns>
  4431. public int GetSittingAvatarsCount()
  4432. {
  4433. lock (m_sittingAvatars)
  4434. return m_sittingAvatars.Count;
  4435. }
  4436. public override string ToString()
  4437. {
  4438. return String.Format("{0} {1} ({2})", Name, UUID, AbsolutePosition);
  4439. }
  4440. #region ISceneObject
  4441. public virtual ISceneObject CloneForNewScene()
  4442. {
  4443. SceneObjectGroup sog = Copy(false);
  4444. sog.IsDeleted = false;
  4445. return sog;
  4446. }
  4447. public virtual string ToXml2()
  4448. {
  4449. return SceneObjectSerializer.ToXml2Format(this);
  4450. }
  4451. public virtual string ExtraToXmlString()
  4452. {
  4453. return "<ExtraFromItemID>" + FromItemID.ToString() + "</ExtraFromItemID>";
  4454. }
  4455. public virtual void ExtraFromXmlString(string xmlstr)
  4456. {
  4457. string id = xmlstr.Substring(xmlstr.IndexOf("<ExtraFromItemID>"));
  4458. id = xmlstr.Replace("<ExtraFromItemID>", "");
  4459. id = id.Replace("</ExtraFromItemID>", "");
  4460. UUID uuid = UUID.Zero;
  4461. UUID.TryParse(id, out uuid);
  4462. FromItemID = uuid;
  4463. }
  4464. public void ResetOwnerChangeFlag()
  4465. {
  4466. ForEachPart(delegate(SceneObjectPart part)
  4467. {
  4468. part.ResetOwnerChangeFlag();
  4469. });
  4470. InvalidateEffectivePerms();
  4471. }
  4472. // clear some references to easy cg
  4473. public void Clear()
  4474. {
  4475. m_parts.Clear();
  4476. m_sittingAvatars.Clear();
  4477. // m_rootPart = null;
  4478. m_targets.Clear();
  4479. m_partsNameToLinkMap.Clear();
  4480. }
  4481. private Dictionary<string,int> m_partsNameToLinkMap = new Dictionary<string, int>();
  4482. private string GetLinkNumber_lastname;
  4483. private int GetLinkNumber_lastnumber;
  4484. public int GetLinkNumber(string name)
  4485. {
  4486. if(String.IsNullOrEmpty(name) || name == "Object" || name == "Primitive")
  4487. return -1;
  4488. lock(m_partsNameToLinkMap)
  4489. {
  4490. if (name == GetLinkNumber_lastname)
  4491. return GetLinkNumber_lastnumber;
  4492. if (m_partsNameToLinkMap.Count == 0)
  4493. {
  4494. GetLinkNumber_lastname = String.Empty;
  4495. GetLinkNumber_lastnumber = -1;
  4496. SceneObjectPart[] parts = m_parts.GetArray();
  4497. for (int i = 0; i < parts.Length; i++)
  4498. {
  4499. string s = parts[i].Name;
  4500. if(String.IsNullOrEmpty(s) || s == "Object" || s == "Primitive")
  4501. continue;
  4502. if(m_partsNameToLinkMap.ContainsKey(s))
  4503. {
  4504. int ol = parts[i].LinkNum;
  4505. if(ol < m_partsNameToLinkMap[s])
  4506. m_partsNameToLinkMap[s] = ol;
  4507. }
  4508. else
  4509. m_partsNameToLinkMap[s] = parts[i].LinkNum;
  4510. }
  4511. }
  4512. if(m_partsNameToLinkMap.ContainsKey(name))
  4513. {
  4514. GetLinkNumber_lastname = name;
  4515. GetLinkNumber_lastnumber = m_partsNameToLinkMap[name];
  4516. return GetLinkNumber_lastnumber;
  4517. }
  4518. }
  4519. if(m_sittingAvatars.Count > 0)
  4520. {
  4521. int j = m_parts.Count + 1;
  4522. ScenePresence[] avs = m_sittingAvatars.ToArray();
  4523. for (int i = 0; i < avs.Length; i++, j++)
  4524. {
  4525. if (avs[i].Name == name)
  4526. {
  4527. GetLinkNumber_lastname = name;
  4528. GetLinkNumber_lastnumber = j;
  4529. return j;
  4530. }
  4531. }
  4532. }
  4533. return -1;
  4534. }
  4535. public void InvalidatePartsLinkMaps(bool all = true)
  4536. {
  4537. lock(m_partsNameToLinkMap)
  4538. {
  4539. if(all)
  4540. m_partsNameToLinkMap.Clear();
  4541. GetLinkNumber_lastname = String.Empty;
  4542. GetLinkNumber_lastnumber = -1;
  4543. }
  4544. }
  4545. public bool CollisionSoundThrottled(int collisionSoundType)
  4546. {
  4547. double time = m_lastCollisionSoundMS;
  4548. // m_lastCollisionSoundMS = Util.GetTimeStampMS();
  4549. // time = m_lastCollisionSoundMS - time;
  4550. double now = Util.GetTimeStampMS();
  4551. time = now - time;
  4552. switch (collisionSoundType)
  4553. {
  4554. case 0: // default sounds
  4555. case 2: // default sounds with volume set by script
  4556. if(time < 300.0)
  4557. return true;
  4558. break;
  4559. case 1: // selected sound
  4560. if(time < 200.0)
  4561. return true;
  4562. break;
  4563. default:
  4564. break;
  4565. }
  4566. m_lastCollisionSoundMS = now;
  4567. return false;
  4568. }
  4569. #endregion
  4570. }
  4571. }