SceneObjectGroup.cs 202 KB

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