SceneObjectPart.cs 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922
  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.Collections.Generic;
  29. using System.Drawing;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Runtime.Serialization;
  33. using System.Security.Permissions;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using log4net;
  37. using OpenMetaverse;
  38. using OpenMetaverse.Packets;
  39. using OpenSim.Framework;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes.Scripting;
  42. using OpenSim.Region.Framework.Scenes.Serialization;
  43. using OpenSim.Region.Physics.Manager;
  44. namespace OpenSim.Region.Framework.Scenes
  45. {
  46. #region Enumerations
  47. [Flags]
  48. public enum Changed : uint
  49. {
  50. INVENTORY = 1,
  51. COLOR = 2,
  52. SHAPE = 4,
  53. SCALE = 8,
  54. TEXTURE = 16,
  55. LINK = 32,
  56. ALLOWED_DROP = 64,
  57. OWNER = 128,
  58. REGION = 256,
  59. TELEPORT = 512,
  60. REGION_RESTART = 1024,
  61. MEDIA = 2048,
  62. ANIMATION = 16384
  63. }
  64. // I don't really know where to put this except here.
  65. // Can't access the OpenSim.Region.ScriptEngine.Common.LSL_BaseClass.Changed constants
  66. [Flags]
  67. public enum ExtraParamType
  68. {
  69. Something1 = 1,
  70. Something2 = 2,
  71. Something3 = 4,
  72. Something4 = 8,
  73. Flexible = 16,
  74. Light = 32,
  75. Sculpt = 48,
  76. Something5 = 64,
  77. Something6 = 128
  78. }
  79. [Flags]
  80. public enum TextureAnimFlags : byte
  81. {
  82. NONE = 0x00,
  83. ANIM_ON = 0x01,
  84. LOOP = 0x02,
  85. REVERSE = 0x04,
  86. PING_PONG = 0x08,
  87. SMOOTH = 0x10,
  88. ROTATE = 0x20,
  89. SCALE = 0x40
  90. }
  91. public enum PrimType : int
  92. {
  93. BOX = 0,
  94. CYLINDER = 1,
  95. PRISM = 2,
  96. SPHERE = 3,
  97. TORUS = 4,
  98. TUBE = 5,
  99. RING = 6,
  100. SCULPT = 7
  101. }
  102. public enum UpdateRequired : byte
  103. {
  104. NONE = 0,
  105. TERSE = 1,
  106. FULL = 2
  107. }
  108. #endregion Enumerations
  109. public class SceneObjectPart : IScriptHost, ISceneEntity
  110. {
  111. /// <value>
  112. /// Denote all sides of the prim
  113. /// </value>
  114. public const int ALL_SIDES = -1;
  115. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  116. /// <value>
  117. /// Is this sop a root part?
  118. /// </value>
  119. public bool IsRoot
  120. {
  121. get { return ParentGroup.RootPart == this; }
  122. }
  123. /// <summary>
  124. /// Is an explicit sit target set for this part?
  125. /// </summary>
  126. public bool IsSitTargetSet
  127. {
  128. get
  129. {
  130. return
  131. !(SitTargetPosition == Vector3.Zero
  132. && (SitTargetOrientation == Quaternion.Identity // Valid Zero Rotation quaternion
  133. || SitTargetOrientation.X == 0f && SitTargetOrientation.Y == 0f && SitTargetOrientation.Z == 1f && SitTargetOrientation.W == 0f // W-Z Mapping was invalid at one point
  134. || SitTargetOrientation.X == 0f && SitTargetOrientation.Y == 0f && SitTargetOrientation.Z == 0f && SitTargetOrientation.W == 0f)); // Invalid Quaternion
  135. }
  136. }
  137. #region Fields
  138. public bool AllowedDrop;
  139. public bool DIE_AT_EDGE;
  140. public bool RETURN_AT_EDGE;
  141. public bool BlockGrab;
  142. public bool StatusSandbox;
  143. public Vector3 StatusSandboxPos;
  144. [XmlIgnore]
  145. public int[] PayPrice = {-2,-2,-2,-2,-2};
  146. [XmlIgnore]
  147. /// <summary>
  148. /// The representation of this part in the physics scene.
  149. /// </summary>
  150. /// <remarks>
  151. /// If you use this property more than once in a section of code then you must take a reference and use that.
  152. /// If another thread is simultaneously turning physics off on this part then this refernece could become
  153. /// null at any time.
  154. /// </remarks>
  155. public PhysicsActor PhysActor { get; set; }
  156. //Xantor 20080528 Sound stuff:
  157. // Note: This isn't persisted in the database right now, as the fields for that aren't just there yet.
  158. // Not a big problem as long as the script that sets it remains in the prim on startup.
  159. // for SL compatibility it should be persisted though (set sound / displaytext / particlesystem, kill script)
  160. public UUID Sound;
  161. public byte SoundFlags;
  162. public double SoundGain;
  163. public double SoundRadius;
  164. public uint TimeStampFull;
  165. public uint TimeStampLastActivity; // Will be used for AutoReturn
  166. public uint TimeStampTerse;
  167. public int STATUS_ROTATE_X;
  168. public int STATUS_ROTATE_Y;
  169. public int STATUS_ROTATE_Z;
  170. private Dictionary<int, string> m_CollisionFilter = new Dictionary<int, string>();
  171. /// <value>
  172. /// The UUID of the user inventory item from which this object was rezzed if this is a root part.
  173. /// If UUID.Zero then either this is not a root part or there is no connection with a user inventory item.
  174. /// </value>
  175. private UUID m_fromUserInventoryItemID;
  176. public UUID FromUserInventoryItemID
  177. {
  178. get { return m_fromUserInventoryItemID; }
  179. set { m_fromUserInventoryItemID = value; }
  180. }
  181. public scriptEvents AggregateScriptEvents;
  182. public Vector3 AttachedPos;
  183. public Vector3 RotationAxis = Vector3.One;
  184. public bool VolumeDetectActive; // XmlIgnore set to avoid problems with persistance until I come to care for this
  185. // Certainly this must be a persistant setting finally
  186. public bool IsWaitingForFirstSpinUpdatePacket;
  187. public Quaternion SpinOldOrientation = Quaternion.Identity;
  188. protected int m_APIDIterations = 0;
  189. protected Quaternion m_APIDTarget = Quaternion.Identity;
  190. protected float m_APIDDamp = 0;
  191. protected float m_APIDStrength = 0;
  192. /// <summary>
  193. /// This part's inventory
  194. /// </summary>
  195. public IEntityInventory Inventory
  196. {
  197. get { return m_inventory; }
  198. }
  199. protected SceneObjectPartInventory m_inventory;
  200. public bool Undoing;
  201. public bool IgnoreUndoUpdate = false;
  202. public PrimFlags LocalFlags;
  203. private float m_damage = -1.0f;
  204. private byte[] m_TextureAnimation;
  205. private byte m_clickAction;
  206. private Color m_color = Color.Black;
  207. private readonly List<uint> m_lastColliders = new List<uint>();
  208. private int m_linkNum;
  209. private int m_scriptAccessPin;
  210. private readonly Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  211. private string m_sitName = String.Empty;
  212. private Quaternion m_sitTargetOrientation = Quaternion.Identity;
  213. private Vector3 m_sitTargetPosition;
  214. private string m_sitAnimation = "SIT";
  215. private string m_text = String.Empty;
  216. private string m_touchName = String.Empty;
  217. private readonly Stack<UndoState> m_undo = new Stack<UndoState>(5);
  218. private readonly Stack<UndoState> m_redo = new Stack<UndoState>(5);
  219. private bool m_passTouches;
  220. protected Vector3 m_acceleration;
  221. protected Vector3 m_angularVelocity;
  222. //unkown if this will be kept, added as a way of removing the group position from the group class
  223. protected Vector3 m_groupPosition;
  224. protected uint m_localId;
  225. protected Material m_material = OpenMetaverse.Material.Wood;
  226. protected string m_name;
  227. protected Vector3 m_offsetPosition;
  228. protected SceneObjectGroup m_parentGroup;
  229. protected byte[] m_particleSystem = Utils.EmptyBytes;
  230. protected ulong m_regionHandle;
  231. protected Quaternion m_rotationOffset = Quaternion.Identity;
  232. protected PrimitiveBaseShape m_shape;
  233. protected UUID m_uuid;
  234. protected Vector3 m_velocity;
  235. protected Vector3 m_lastPosition;
  236. protected Quaternion m_lastRotation;
  237. protected Vector3 m_lastVelocity;
  238. protected Vector3 m_lastAcceleration;
  239. protected Vector3 m_lastAngularVelocity;
  240. protected int m_lastTerseSent;
  241. /// <summary>
  242. /// Stores media texture data
  243. /// </summary>
  244. protected string m_mediaUrl;
  245. // TODO: Those have to be changed into persistent properties at some later point,
  246. // or sit-camera on vehicles will break on sim-crossing.
  247. private Vector3 m_cameraEyeOffset;
  248. private Vector3 m_cameraAtOffset;
  249. private bool m_forceMouselook;
  250. // TODO: Collision sound should have default.
  251. private UUID m_collisionSound;
  252. private float m_collisionSoundVolume;
  253. #endregion Fields
  254. // ~SceneObjectPart()
  255. // {
  256. // Console.WriteLine(
  257. // "[SCENE OBJECT PART]: Destructor called for {0}, local id {1}, parent {2} {3}",
  258. // Name, LocalId, ParentGroup.Name, ParentGroup.LocalId);
  259. // m_log.DebugFormat(
  260. // "[SCENE OBJECT PART]: Destructor called for {0}, local id {1}, parent {2} {3}",
  261. // Name, LocalId, ParentGroup.Name, ParentGroup.LocalId);
  262. // }
  263. #region Constructors
  264. /// <summary>
  265. /// No arg constructor called by region restore db code
  266. /// </summary>
  267. public SceneObjectPart()
  268. {
  269. m_TextureAnimation = Utils.EmptyBytes;
  270. m_particleSystem = Utils.EmptyBytes;
  271. Rezzed = DateTime.UtcNow;
  272. Description = String.Empty;
  273. // Prims currently only contain a single folder (Contents). From looking at the Second Life protocol,
  274. // this appears to have the same UUID (!) as the prim. If this isn't the case, one can't drag items from
  275. // the prim into an agent inventory (Linden client reports that the "Object not found for drop" in its log
  276. m_inventory = new SceneObjectPartInventory(this);
  277. }
  278. /// <summary>
  279. /// Create a completely new SceneObjectPart (prim). This will need to be added separately to a SceneObjectGroup
  280. /// </summary>
  281. /// <param name="ownerID"></param>
  282. /// <param name="shape"></param>
  283. /// <param name="position"></param>
  284. /// <param name="rotationOffset"></param>
  285. /// <param name="offsetPosition"></param>
  286. public SceneObjectPart(
  287. UUID ownerID, PrimitiveBaseShape shape, Vector3 groupPosition,
  288. Quaternion rotationOffset, Vector3 offsetPosition) : this()
  289. {
  290. m_name = "Primitive";
  291. CreationDate = (int)Utils.DateTimeToUnixTime(Rezzed);
  292. LastOwnerID = CreatorID = OwnerID = ownerID;
  293. UUID = UUID.Random();
  294. Shape = shape;
  295. OwnershipCost = 0;
  296. ObjectSaleType = 0;
  297. SalePrice = 0;
  298. Category = 0;
  299. GroupPosition = groupPosition;
  300. OffsetPosition = offsetPosition;
  301. RotationOffset = rotationOffset;
  302. Velocity = Vector3.Zero;
  303. AngularVelocity = Vector3.Zero;
  304. Acceleration = Vector3.Zero;
  305. Flags = 0;
  306. CreateSelected = true;
  307. TrimPermissions();
  308. }
  309. #endregion Constructors
  310. #region XML Schema
  311. private UUID _lastOwnerID;
  312. private UUID _ownerID;
  313. private UUID _groupID;
  314. private int _ownershipCost;
  315. private byte _objectSaleType;
  316. private int _salePrice;
  317. private uint _category;
  318. private Int32 _creationDate;
  319. private uint _parentID = 0;
  320. private uint _baseMask = (uint)PermissionMask.All;
  321. private uint _ownerMask = (uint)PermissionMask.All;
  322. private uint _groupMask = (uint)PermissionMask.None;
  323. private uint _everyoneMask = (uint)PermissionMask.None;
  324. private uint _nextOwnerMask = (uint)PermissionMask.All;
  325. private PrimFlags _flags = PrimFlags.None;
  326. private DateTime m_expires;
  327. private DateTime m_rezzed;
  328. private bool m_createSelected = false;
  329. private UUID _creatorID;
  330. public UUID CreatorID
  331. {
  332. get { return _creatorID; }
  333. set { _creatorID = value; }
  334. }
  335. private string m_creatorData = string.Empty;
  336. /// <summary>
  337. /// Data about the creator in the form home_url;name
  338. /// </summary>
  339. public string CreatorData
  340. {
  341. get { return m_creatorData; }
  342. set { m_creatorData = value; }
  343. }
  344. /// <summary>
  345. /// Used by the DB layer to retrieve / store the entire user identification.
  346. /// The identification can either be a simple UUID or a string of the form
  347. /// uuid[;home_url[;name]]
  348. /// </summary>
  349. public string CreatorIdentification
  350. {
  351. get
  352. {
  353. if (CreatorData != null && CreatorData != string.Empty)
  354. return CreatorID.ToString() + ';' + CreatorData;
  355. else
  356. return CreatorID.ToString();
  357. }
  358. set
  359. {
  360. if ((value == null) || (value != null && value == string.Empty))
  361. {
  362. CreatorData = string.Empty;
  363. return;
  364. }
  365. if (!value.Contains(";")) // plain UUID
  366. {
  367. UUID uuid = UUID.Zero;
  368. UUID.TryParse(value, out uuid);
  369. CreatorID = uuid;
  370. }
  371. else // <uuid>[;<endpoint>[;name]]
  372. {
  373. string name = "Unknown User";
  374. string[] parts = value.Split(';');
  375. if (parts.Length >= 1)
  376. {
  377. UUID uuid = UUID.Zero;
  378. UUID.TryParse(parts[0], out uuid);
  379. CreatorID = uuid;
  380. }
  381. if (parts.Length >= 2)
  382. CreatorData = parts[1];
  383. if (parts.Length >= 3)
  384. name = parts[2];
  385. CreatorData += ';' + name;
  386. }
  387. }
  388. }
  389. /// <summary>
  390. /// A relic from when we we thought that prims contained folder objects. In
  391. /// reality, prim == folder
  392. /// Exposing this is not particularly good, but it's one of the least evils at the moment to see
  393. /// folder id from prim inventory item data, since it's not (yet) actually stored with the prim.
  394. /// </summary>
  395. public UUID FolderID
  396. {
  397. get { return UUID; }
  398. set { } // Don't allow assignment, or legacy prims wil b0rk - but we need the setter for legacy serialization.
  399. }
  400. /// <value>
  401. /// Access should be via Inventory directly - this property temporarily remains for xml serialization purposes
  402. /// </value>
  403. public uint InventorySerial
  404. {
  405. get { return m_inventory.Serial; }
  406. set { m_inventory.Serial = value; }
  407. }
  408. /// <value>
  409. /// Access should be via Inventory directly - this property temporarily remains for xml serialization purposes
  410. /// </value>
  411. public TaskInventoryDictionary TaskInventory
  412. {
  413. get { return m_inventory.Items; }
  414. set { m_inventory.Items = value; }
  415. }
  416. /// <summary>
  417. /// This is idential to the Flags property, except that the returned value is uint rather than PrimFlags
  418. /// </summary>
  419. [Obsolete("Use Flags property instead")]
  420. public uint ObjectFlags
  421. {
  422. get { return (uint)Flags; }
  423. set { Flags = (PrimFlags)value; }
  424. }
  425. public UUID UUID
  426. {
  427. get { return m_uuid; }
  428. set
  429. {
  430. m_uuid = value;
  431. // This is necessary so that TaskInventoryItem parent ids correctly reference the new uuid of this part
  432. if (Inventory != null)
  433. Inventory.ResetObjectID();
  434. }
  435. }
  436. public uint LocalId
  437. {
  438. get { return m_localId; }
  439. set
  440. {
  441. m_localId = value;
  442. // m_log.DebugFormat("[SCENE OBJECT PART]: Set part {0} to local id {1}", Name, m_localId);
  443. }
  444. }
  445. public virtual string Name
  446. {
  447. get { return m_name; }
  448. set
  449. {
  450. m_name = value;
  451. PhysicsActor pa = PhysActor;
  452. if (pa != null)
  453. pa.SOPName = value;
  454. }
  455. }
  456. public byte Material
  457. {
  458. get { return (byte) m_material; }
  459. set
  460. {
  461. m_material = (Material)value;
  462. PhysicsActor pa = PhysActor;
  463. if (pa != null)
  464. pa.SetMaterial((int)value);
  465. }
  466. }
  467. public bool PassTouches
  468. {
  469. get { return m_passTouches; }
  470. set
  471. {
  472. m_passTouches = value;
  473. if (ParentGroup != null)
  474. ParentGroup.HasGroupChanged = true;
  475. }
  476. }
  477. public Dictionary<int, string> CollisionFilter
  478. {
  479. get { return m_CollisionFilter; }
  480. set
  481. {
  482. m_CollisionFilter = value;
  483. }
  484. }
  485. protected Quaternion APIDTarget
  486. {
  487. get { return m_APIDTarget; }
  488. set { m_APIDTarget = value; }
  489. }
  490. protected float APIDDamp
  491. {
  492. get { return m_APIDDamp; }
  493. set { m_APIDDamp = value; }
  494. }
  495. protected float APIDStrength
  496. {
  497. get { return m_APIDStrength; }
  498. set { m_APIDStrength = value; }
  499. }
  500. public ulong RegionHandle
  501. {
  502. get { return m_regionHandle; }
  503. set { m_regionHandle = value; }
  504. }
  505. public int ScriptAccessPin
  506. {
  507. get { return m_scriptAccessPin; }
  508. set { m_scriptAccessPin = (int)value; }
  509. }
  510. private SceneObjectPart m_PlaySoundMasterPrim = null;
  511. public SceneObjectPart PlaySoundMasterPrim
  512. {
  513. get { return m_PlaySoundMasterPrim; }
  514. set { m_PlaySoundMasterPrim = value; }
  515. }
  516. private List<SceneObjectPart> m_PlaySoundSlavePrims = new List<SceneObjectPart>();
  517. public List<SceneObjectPart> PlaySoundSlavePrims
  518. {
  519. get { return m_PlaySoundSlavePrims; }
  520. set { m_PlaySoundSlavePrims = value; }
  521. }
  522. private SceneObjectPart m_LoopSoundMasterPrim = null;
  523. public SceneObjectPart LoopSoundMasterPrim
  524. {
  525. get { return m_LoopSoundMasterPrim; }
  526. set { m_LoopSoundMasterPrim = value; }
  527. }
  528. private List<SceneObjectPart> m_LoopSoundSlavePrims = new List<SceneObjectPart>();
  529. public List<SceneObjectPart> LoopSoundSlavePrims
  530. {
  531. get { return m_LoopSoundSlavePrims; }
  532. set { m_LoopSoundSlavePrims = value; }
  533. }
  534. public Byte[] TextureAnimation
  535. {
  536. get { return m_TextureAnimation; }
  537. set { m_TextureAnimation = value; }
  538. }
  539. public Byte[] ParticleSystem
  540. {
  541. get { return m_particleSystem; }
  542. set { m_particleSystem = value; }
  543. }
  544. public DateTime Expires
  545. {
  546. get { return m_expires; }
  547. set { m_expires = value; }
  548. }
  549. public DateTime Rezzed
  550. {
  551. get { return m_rezzed; }
  552. set { m_rezzed = value; }
  553. }
  554. public float Damage
  555. {
  556. get { return m_damage; }
  557. set { m_damage = value; }
  558. }
  559. /// <summary>
  560. /// The position of the entire group that this prim belongs to.
  561. /// </summary>
  562. public Vector3 GroupPosition
  563. {
  564. get
  565. {
  566. // If this is a linkset, we don't want the physics engine mucking up our group position here.
  567. PhysicsActor actor = PhysActor;
  568. if (actor != null && ParentID == 0)
  569. m_groupPosition = actor.Position;
  570. if (ParentGroup.IsAttachment)
  571. {
  572. ScenePresence sp = ParentGroup.Scene.GetScenePresence(ParentGroup.AttachedAvatar);
  573. if (sp != null)
  574. return sp.AbsolutePosition;
  575. }
  576. return m_groupPosition;
  577. }
  578. set
  579. {
  580. m_groupPosition = value;
  581. PhysicsActor actor = PhysActor;
  582. if (actor != null)
  583. {
  584. try
  585. {
  586. // Root prim actually goes at Position
  587. if (ParentID == 0)
  588. {
  589. actor.Position = value;
  590. }
  591. else
  592. {
  593. // To move the child prim in respect to the group position and rotation we have to calculate
  594. actor.Position = GetWorldPosition();
  595. actor.Orientation = GetWorldRotation();
  596. }
  597. // Tell the physics engines that this prim changed.
  598. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(actor);
  599. }
  600. catch (Exception e)
  601. {
  602. m_log.Error("[SCENEOBJECTPART]: GROUP POSITION. " + e.Message);
  603. }
  604. }
  605. // TODO if we decide to do sitting in a more SL compatible way (multiple avatars per prim), this has to be fixed, too
  606. if (SitTargetAvatar != UUID.Zero)
  607. {
  608. ScenePresence avatar;
  609. if (ParentGroup.Scene.TryGetScenePresence(SitTargetAvatar, out avatar))
  610. {
  611. avatar.ParentPosition = GetWorldPosition();
  612. }
  613. }
  614. }
  615. }
  616. public Vector3 OffsetPosition
  617. {
  618. get { return m_offsetPosition; }
  619. set
  620. {
  621. // StoreUndoState();
  622. m_offsetPosition = value;
  623. if (ParentGroup != null && !ParentGroup.IsDeleted)
  624. {
  625. PhysicsActor actor = PhysActor;
  626. if (ParentID != 0 && actor != null)
  627. {
  628. actor.Position = GetWorldPosition();
  629. actor.Orientation = GetWorldRotation();
  630. // Tell the physics engines that this prim changed.
  631. if (ParentGroup.Scene != null)
  632. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(actor);
  633. }
  634. }
  635. }
  636. }
  637. public Vector3 RelativePosition
  638. {
  639. get
  640. {
  641. if (IsRoot)
  642. {
  643. if (ParentGroup.IsAttachment)
  644. return AttachedPos;
  645. else
  646. return AbsolutePosition;
  647. }
  648. else
  649. {
  650. return OffsetPosition;
  651. }
  652. }
  653. }
  654. public Quaternion RotationOffset
  655. {
  656. get
  657. {
  658. // We don't want the physics engine mucking up the rotations in a linkset
  659. PhysicsActor actor = PhysActor;
  660. if (ParentID == 0 && (Shape.PCode != 9 || Shape.State == 0) && actor != null)
  661. {
  662. if (actor.Orientation.X != 0f || actor.Orientation.Y != 0f
  663. || actor.Orientation.Z != 0f || actor.Orientation.W != 0f)
  664. {
  665. m_rotationOffset = actor.Orientation;
  666. }
  667. }
  668. // float roll, pitch, yaw = 0;
  669. // m_rotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  670. //
  671. // m_log.DebugFormat(
  672. // "[SCENE OBJECT PART]: Got euler {0} for RotationOffset on {1} {2}",
  673. // new Vector3(roll, pitch, yaw), Name, LocalId);
  674. return m_rotationOffset;
  675. }
  676. set
  677. {
  678. StoreUndoState();
  679. m_rotationOffset = value;
  680. PhysicsActor actor = PhysActor;
  681. if (actor != null)
  682. {
  683. try
  684. {
  685. // Root prim gets value directly
  686. if (ParentID == 0)
  687. {
  688. actor.Orientation = value;
  689. //m_log.Info("[PART]: RO1:" + actor.Orientation.ToString());
  690. }
  691. else
  692. {
  693. // Child prim we have to calculate it's world rotationwel
  694. Quaternion resultingrotation = GetWorldRotation();
  695. actor.Orientation = resultingrotation;
  696. //m_log.Info("[PART]: RO2:" + actor.Orientation.ToString());
  697. }
  698. if (ParentGroup != null)
  699. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(actor);
  700. //}
  701. }
  702. catch (Exception ex)
  703. {
  704. m_log.Error("[SCENEOBJECTPART]: ROTATIONOFFSET" + ex.Message);
  705. }
  706. }
  707. // float roll, pitch, yaw = 0;
  708. // m_rotationOffset.GetEulerAngles(out roll, out pitch, out yaw);
  709. //
  710. // m_log.DebugFormat(
  711. // "[SCENE OBJECT PART]: Set euler {0} for RotationOffset on {1} {2}",
  712. // new Vector3(roll, pitch, yaw), Name, LocalId);
  713. }
  714. }
  715. /// <summary></summary>
  716. public Vector3 Velocity
  717. {
  718. get
  719. {
  720. PhysicsActor actor = PhysActor;
  721. if (actor != null)
  722. {
  723. if (actor.IsPhysical)
  724. {
  725. m_velocity = actor.Velocity;
  726. }
  727. }
  728. return m_velocity;
  729. }
  730. set
  731. {
  732. m_velocity = value;
  733. PhysicsActor actor = PhysActor;
  734. if (actor != null)
  735. {
  736. if (actor.IsPhysical)
  737. {
  738. actor.Velocity = value;
  739. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(actor);
  740. }
  741. }
  742. }
  743. }
  744. /// <summary>Update angular velocity and schedule terse update.</summary>
  745. public void UpdateAngularVelocity(Vector3 avel)
  746. {
  747. AngularVelocity = avel;
  748. ScheduleTerseUpdate();
  749. ParentGroup.HasGroupChanged = true;
  750. }
  751. /// <summary>Get or set angular velocity. Does not schedule update.</summary>
  752. public Vector3 AngularVelocity
  753. {
  754. get
  755. {
  756. PhysicsActor actor = PhysActor;
  757. if ((actor != null) && actor.IsPhysical)
  758. {
  759. m_angularVelocity = actor.RotationalVelocity;
  760. }
  761. return m_angularVelocity;
  762. }
  763. set { m_angularVelocity = value; }
  764. }
  765. /// <summary></summary>
  766. public Vector3 Acceleration
  767. {
  768. get { return m_acceleration; }
  769. set { m_acceleration = value; }
  770. }
  771. public string Description { get; set; }
  772. /// <value>
  773. /// Text color.
  774. /// </value>
  775. public Color Color
  776. {
  777. get { return m_color; }
  778. set { m_color = value; }
  779. }
  780. public string Text
  781. {
  782. get
  783. {
  784. if (m_text.Length > 255)
  785. return m_text.Substring(0, 254);
  786. return m_text;
  787. }
  788. set { m_text = value; }
  789. }
  790. public string SitName
  791. {
  792. get { return m_sitName; }
  793. set { m_sitName = value; }
  794. }
  795. public string TouchName
  796. {
  797. get { return m_touchName; }
  798. set { m_touchName = value; }
  799. }
  800. public int LinkNum
  801. {
  802. get { return m_linkNum; }
  803. set
  804. {
  805. // if (ParentGroup != null)
  806. // {
  807. // m_log.DebugFormat(
  808. // "[SCENE OBJECT PART]: Setting linknum of {0}@{1} to {2} from {3}",
  809. // Name, AbsolutePosition, value, m_linkNum);
  810. // Util.PrintCallStack();
  811. // }
  812. m_linkNum = value;
  813. }
  814. }
  815. public byte ClickAction
  816. {
  817. get { return m_clickAction; }
  818. set
  819. {
  820. m_clickAction = value;
  821. }
  822. }
  823. public PrimitiveBaseShape Shape
  824. {
  825. get { return m_shape; }
  826. set { m_shape = value;}
  827. }
  828. /// <summary>
  829. /// Change the scale of this part.
  830. /// </summary>
  831. public Vector3 Scale
  832. {
  833. get { return m_shape.Scale; }
  834. set
  835. {
  836. if (m_shape != null)
  837. {
  838. StoreUndoState();
  839. m_shape.Scale = value;
  840. PhysicsActor actor = PhysActor;
  841. if (actor != null)
  842. {
  843. if (ParentGroup.Scene != null)
  844. {
  845. if (ParentGroup.Scene.PhysicsScene != null)
  846. {
  847. actor.Size = m_shape.Scale;
  848. if (Shape.SculptEntry)
  849. CheckSculptAndLoad();
  850. else
  851. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(actor);
  852. }
  853. }
  854. }
  855. }
  856. TriggerScriptChangedEvent(Changed.SCALE);
  857. }
  858. }
  859. public UpdateRequired UpdateFlag { get; set; }
  860. /// <summary>
  861. /// Used for media on a prim.
  862. /// </summary>
  863. /// Do not change this value directly - always do it through an IMoapModule.
  864. public string MediaUrl
  865. {
  866. get
  867. {
  868. return m_mediaUrl;
  869. }
  870. set
  871. {
  872. m_mediaUrl = value;
  873. if (ParentGroup != null)
  874. ParentGroup.HasGroupChanged = true;
  875. }
  876. }
  877. public bool CreateSelected
  878. {
  879. get { return m_createSelected; }
  880. set
  881. {
  882. // m_log.DebugFormat("[SOP]: Setting CreateSelected to {0} for {1} {2}", value, Name, UUID);
  883. m_createSelected = value;
  884. }
  885. }
  886. #endregion
  887. //---------------
  888. #region Public Properties with only Get
  889. public Vector3 AbsolutePosition
  890. {
  891. get
  892. {
  893. if (ParentGroup.IsAttachment)
  894. return GroupPosition;
  895. return m_offsetPosition + m_groupPosition;
  896. }
  897. }
  898. public SceneObjectGroup ParentGroup
  899. {
  900. get { return m_parentGroup; }
  901. private set { m_parentGroup = value; }
  902. }
  903. public scriptEvents ScriptEvents
  904. {
  905. get { return AggregateScriptEvents; }
  906. }
  907. public Quaternion SitTargetOrientation
  908. {
  909. get { return m_sitTargetOrientation; }
  910. set
  911. {
  912. m_sitTargetOrientation = value;
  913. // m_log.DebugFormat("[SCENE OBJECT PART]: Set sit target orientation {0} for {1} {2}", m_sitTargetOrientation, Name, LocalId);
  914. }
  915. }
  916. public Vector3 SitTargetPosition
  917. {
  918. get { return m_sitTargetPosition; }
  919. set
  920. {
  921. m_sitTargetPosition = value;
  922. // m_log.DebugFormat("[SCENE OBJECT PART]: Set sit target position to {0} for {1} {2}", m_sitTargetPosition, Name, LocalId);
  923. }
  924. }
  925. // This sort of sucks, but I'm adding these in to make some of
  926. // the mappings more consistant.
  927. public Vector3 SitTargetPositionLL
  928. {
  929. get { return new Vector3(m_sitTargetPosition.X, m_sitTargetPosition.Y,m_sitTargetPosition.Z); }
  930. set { m_sitTargetPosition = value; }
  931. }
  932. public Quaternion SitTargetOrientationLL
  933. {
  934. get
  935. {
  936. return new Quaternion(
  937. m_sitTargetOrientation.X,
  938. m_sitTargetOrientation.Y,
  939. m_sitTargetOrientation.Z,
  940. m_sitTargetOrientation.W
  941. );
  942. }
  943. set { m_sitTargetOrientation = new Quaternion(value.X, value.Y, value.Z, value.W); }
  944. }
  945. public bool Stopped
  946. {
  947. get {
  948. double threshold = 0.02;
  949. return (Math.Abs(Velocity.X) < threshold &&
  950. Math.Abs(Velocity.Y) < threshold &&
  951. Math.Abs(Velocity.Z) < threshold &&
  952. Math.Abs(AngularVelocity.X) < threshold &&
  953. Math.Abs(AngularVelocity.Y) < threshold &&
  954. Math.Abs(AngularVelocity.Z) < threshold);
  955. }
  956. }
  957. public uint ParentID
  958. {
  959. get { return _parentID; }
  960. set { _parentID = value; }
  961. }
  962. public int CreationDate
  963. {
  964. get { return _creationDate; }
  965. set { _creationDate = value; }
  966. }
  967. public uint Category
  968. {
  969. get { return _category; }
  970. set { _category = value; }
  971. }
  972. public int SalePrice
  973. {
  974. get { return _salePrice; }
  975. set { _salePrice = value; }
  976. }
  977. public byte ObjectSaleType
  978. {
  979. get { return _objectSaleType; }
  980. set { _objectSaleType = value; }
  981. }
  982. public int OwnershipCost
  983. {
  984. get { return _ownershipCost; }
  985. set { _ownershipCost = value; }
  986. }
  987. public UUID GroupID
  988. {
  989. get { return _groupID; }
  990. set { _groupID = value; }
  991. }
  992. public UUID OwnerID
  993. {
  994. get { return _ownerID; }
  995. set { _ownerID = value; }
  996. }
  997. public UUID LastOwnerID
  998. {
  999. get { return _lastOwnerID; }
  1000. set { _lastOwnerID = value; }
  1001. }
  1002. public uint BaseMask
  1003. {
  1004. get { return _baseMask; }
  1005. set { _baseMask = value; }
  1006. }
  1007. public uint OwnerMask
  1008. {
  1009. get { return _ownerMask; }
  1010. set { _ownerMask = value; }
  1011. }
  1012. public uint GroupMask
  1013. {
  1014. get { return _groupMask; }
  1015. set { _groupMask = value; }
  1016. }
  1017. public uint EveryoneMask
  1018. {
  1019. get { return _everyoneMask; }
  1020. set { _everyoneMask = value; }
  1021. }
  1022. public uint NextOwnerMask
  1023. {
  1024. get { return _nextOwnerMask; }
  1025. set { _nextOwnerMask = value; }
  1026. }
  1027. /// <summary>
  1028. /// Property flags. See OpenMetaverse.PrimFlags
  1029. /// </summary>
  1030. /// <remarks>
  1031. /// Example properties are PrimFlags.Phantom and PrimFlags.DieAtEdge
  1032. /// </remarks>
  1033. public PrimFlags Flags
  1034. {
  1035. get { return _flags; }
  1036. set
  1037. {
  1038. // m_log.DebugFormat("[SOP]: Setting flags for {0} {1} to {2}", UUID, Name, value);
  1039. _flags = value;
  1040. }
  1041. }
  1042. /// <summary>
  1043. /// ID of the avatar that is sat on us if we have a sit target. If there is no such avatar then is UUID.Zero
  1044. /// </summary>
  1045. public UUID SitTargetAvatar { get; set; }
  1046. /// <summary>
  1047. /// IDs of all avatars start on this object part.
  1048. /// </summary>
  1049. /// <remarks>
  1050. /// We need to track this so that we can stop sat upon prims from being attached.
  1051. /// </remarks>
  1052. /// <value>
  1053. /// null if there are no sitting avatars. This is to save us create a hashset for every prim in a scene.
  1054. /// </value>
  1055. private HashSet<UUID> m_sittingAvatars;
  1056. public virtual UUID RegionID
  1057. {
  1058. get
  1059. {
  1060. if (ParentGroup.Scene != null)
  1061. return ParentGroup.Scene.RegionInfo.RegionID;
  1062. else
  1063. return UUID.Zero;
  1064. }
  1065. set {} // read only
  1066. }
  1067. private UUID _parentUUID = UUID.Zero;
  1068. public UUID ParentUUID
  1069. {
  1070. get
  1071. {
  1072. if (ParentGroup != null)
  1073. _parentUUID = ParentGroup.UUID;
  1074. return _parentUUID;
  1075. }
  1076. set { _parentUUID = value; }
  1077. }
  1078. public string SitAnimation
  1079. {
  1080. get { return m_sitAnimation; }
  1081. set { m_sitAnimation = value; }
  1082. }
  1083. public UUID CollisionSound
  1084. {
  1085. get { return m_collisionSound; }
  1086. set
  1087. {
  1088. m_collisionSound = value;
  1089. aggregateScriptEvents();
  1090. }
  1091. }
  1092. public float CollisionSoundVolume
  1093. {
  1094. get { return m_collisionSoundVolume; }
  1095. set { m_collisionSoundVolume = value; }
  1096. }
  1097. #endregion Public Properties with only Get
  1098. private uint ApplyMask(uint val, bool set, uint mask)
  1099. {
  1100. if (set)
  1101. {
  1102. return val |= mask;
  1103. }
  1104. else
  1105. {
  1106. return val &= ~mask;
  1107. }
  1108. }
  1109. /// <summary>
  1110. /// Clear all pending updates of parts to clients
  1111. /// </summary>
  1112. public void ClearUpdateSchedule()
  1113. {
  1114. UpdateFlag = UpdateRequired.NONE;
  1115. }
  1116. /// <summary>
  1117. /// Send this part's properties (name, description, inventory serial, base mask, etc.) to a client
  1118. /// </summary>
  1119. /// <param name="client"></param>
  1120. public void SendPropertiesToClient(IClientAPI client)
  1121. {
  1122. client.SendObjectPropertiesReply(this);
  1123. }
  1124. // TODO: unused:
  1125. // private void handleTimerAccounting(uint localID, double interval)
  1126. // {
  1127. // if (localID == LocalId)
  1128. // {
  1129. // float sec = (float)interval;
  1130. // if (m_parentGroup != null)
  1131. // {
  1132. // if (sec == 0)
  1133. // {
  1134. // if (m_parentGroup.scriptScore + 0.001f >= float.MaxValue - 0.001)
  1135. // m_parentGroup.scriptScore = 0;
  1136. //
  1137. // m_parentGroup.scriptScore += 0.001f;
  1138. // return;
  1139. // }
  1140. //
  1141. // if (m_parentGroup.scriptScore + (0.001f / sec) >= float.MaxValue - (0.001f / sec))
  1142. // m_parentGroup.scriptScore = 0;
  1143. // m_parentGroup.scriptScore += (0.001f / sec);
  1144. // }
  1145. // }
  1146. // }
  1147. #region Public Methods
  1148. public void ResetExpire()
  1149. {
  1150. Expires = DateTime.Now + new TimeSpan(600000000);
  1151. }
  1152. public void AddFlag(PrimFlags flag)
  1153. {
  1154. // PrimFlags prevflag = Flags;
  1155. if ((Flags & flag) == 0)
  1156. {
  1157. //m_log.Debug("Adding flag: " + ((PrimFlags) flag).ToString());
  1158. Flags |= flag;
  1159. if (flag == PrimFlags.TemporaryOnRez)
  1160. ResetExpire();
  1161. }
  1162. // m_log.Debug("Aprev: " + prevflag.ToString() + " curr: " + Flags.ToString());
  1163. }
  1164. public void AddNewParticleSystem(Primitive.ParticleSystem pSystem)
  1165. {
  1166. m_particleSystem = pSystem.GetBytes();
  1167. }
  1168. public void RemoveParticleSystem()
  1169. {
  1170. m_particleSystem = new byte[0];
  1171. }
  1172. public void AddTextureAnimation(Primitive.TextureAnimation pTexAnim)
  1173. {
  1174. byte[] data = new byte[16];
  1175. int pos = 0;
  1176. // The flags don't like conversion from uint to byte, so we have to do
  1177. // it the crappy way. See the above function :(
  1178. data[pos] = ConvertScriptUintToByte((uint)pTexAnim.Flags); pos++;
  1179. data[pos] = (byte)pTexAnim.Face; pos++;
  1180. data[pos] = (byte)pTexAnim.SizeX; pos++;
  1181. data[pos] = (byte)pTexAnim.SizeY; pos++;
  1182. Utils.FloatToBytes(pTexAnim.Start).CopyTo(data, pos);
  1183. Utils.FloatToBytes(pTexAnim.Length).CopyTo(data, pos + 4);
  1184. Utils.FloatToBytes(pTexAnim.Rate).CopyTo(data, pos + 8);
  1185. m_TextureAnimation = data;
  1186. }
  1187. public void AdjustSoundGain(double volume)
  1188. {
  1189. if (volume > 1)
  1190. volume = 1;
  1191. if (volume < 0)
  1192. volume = 0;
  1193. ParentGroup.Scene.ForEachRootClient(delegate(IClientAPI client)
  1194. {
  1195. client.SendAttachedSoundGainChange(UUID, (float)volume);
  1196. });
  1197. }
  1198. /// <summary>
  1199. /// hook to the physics scene to apply impulse
  1200. /// This is sent up to the group, which then finds the root prim
  1201. /// and applies the force on the root prim of the group
  1202. /// </summary>
  1203. /// <param name="impulsei">Vector force</param>
  1204. /// <param name="localGlobalTF">true for the local frame, false for the global frame</param>
  1205. public void ApplyImpulse(Vector3 impulsei, bool localGlobalTF)
  1206. {
  1207. Vector3 impulse = impulsei;
  1208. if (localGlobalTF)
  1209. {
  1210. Quaternion grot = GetWorldRotation();
  1211. Quaternion AXgrot = grot;
  1212. Vector3 AXimpulsei = impulsei;
  1213. Vector3 newimpulse = AXimpulsei * AXgrot;
  1214. impulse = newimpulse;
  1215. }
  1216. if (ParentGroup != null)
  1217. {
  1218. ParentGroup.applyImpulse(impulse);
  1219. }
  1220. }
  1221. /// <summary>
  1222. /// hook to the physics scene to apply angular impulse
  1223. /// This is sent up to the group, which then finds the root prim
  1224. /// and applies the force on the root prim of the group
  1225. /// </summary>
  1226. /// <param name="impulsei">Vector force</param>
  1227. /// <param name="localGlobalTF">true for the local frame, false for the global frame</param>
  1228. public void ApplyAngularImpulse(Vector3 impulsei, bool localGlobalTF)
  1229. {
  1230. Vector3 impulse = impulsei;
  1231. if (localGlobalTF)
  1232. {
  1233. Quaternion grot = GetWorldRotation();
  1234. Quaternion AXgrot = grot;
  1235. Vector3 AXimpulsei = impulsei;
  1236. Vector3 newimpulse = AXimpulsei * AXgrot;
  1237. impulse = newimpulse;
  1238. }
  1239. ParentGroup.applyAngularImpulse(impulse);
  1240. }
  1241. /// <summary>
  1242. /// hook to the physics scene to apply angular impulse
  1243. /// This is sent up to the group, which then finds the root prim
  1244. /// and applies the force on the root prim of the group
  1245. /// </summary>
  1246. /// <param name="impulsei">Vector force</param>
  1247. /// <param name="localGlobalTF">true for the local frame, false for the global frame</param>
  1248. public void SetAngularImpulse(Vector3 impulsei, bool localGlobalTF)
  1249. {
  1250. Vector3 impulse = impulsei;
  1251. if (localGlobalTF)
  1252. {
  1253. Quaternion grot = GetWorldRotation();
  1254. Quaternion AXgrot = grot;
  1255. Vector3 AXimpulsei = impulsei;
  1256. Vector3 newimpulse = AXimpulsei * AXgrot;
  1257. impulse = newimpulse;
  1258. }
  1259. ParentGroup.setAngularImpulse(impulse);
  1260. }
  1261. /// <summary>
  1262. /// Apply physics to this part.
  1263. /// </summary>
  1264. /// <param name="rootObjectFlags"></param>
  1265. /// <param name="VolumeDetectActive"></param>
  1266. public void ApplyPhysics(uint rootObjectFlags, bool VolumeDetectActive)
  1267. {
  1268. if (!ParentGroup.Scene.CollidablePrims)
  1269. return;
  1270. // m_log.DebugFormat(
  1271. // "[SCENE OBJECT PART]: Applying physics to {0} {1}, m_physicalPrim {2}",
  1272. // Name, LocalId, UUID, m_physicalPrim);
  1273. bool isPhysical = (rootObjectFlags & (uint) PrimFlags.Physics) != 0;
  1274. bool isPhantom = (rootObjectFlags & (uint) PrimFlags.Phantom) != 0;
  1275. if (IsJoint())
  1276. {
  1277. DoPhysicsPropertyUpdate(isPhysical, true);
  1278. }
  1279. else
  1280. {
  1281. // Special case for VolumeDetection: If VolumeDetection is set, the phantom flag is locally ignored
  1282. if (VolumeDetectActive)
  1283. isPhantom = false;
  1284. // The only time the physics scene shouldn't know about the prim is if it's phantom or an attachment, which is phantom by definition
  1285. // or flexible
  1286. if (!isPhantom && !ParentGroup.IsAttachment && !(Shape.PathCurve == (byte)Extrusion.Flexible))
  1287. {
  1288. // Added clarification.. since A rigid body is an object that you can kick around, etc.
  1289. bool rigidBody = isPhysical && !isPhantom;
  1290. PhysicsActor pa = AddToPhysics(rigidBody);
  1291. if (pa != null)
  1292. pa.SetVolumeDetect(VolumeDetectActive ? 1 : 0);
  1293. }
  1294. }
  1295. }
  1296. public byte ConvertScriptUintToByte(uint indata)
  1297. {
  1298. byte outdata = (byte)TextureAnimFlags.NONE;
  1299. if ((indata & 1) != 0) outdata |= (byte)TextureAnimFlags.ANIM_ON;
  1300. if ((indata & 2) != 0) outdata |= (byte)TextureAnimFlags.LOOP;
  1301. if ((indata & 4) != 0) outdata |= (byte)TextureAnimFlags.REVERSE;
  1302. if ((indata & 8) != 0) outdata |= (byte)TextureAnimFlags.PING_PONG;
  1303. if ((indata & 16) != 0) outdata |= (byte)TextureAnimFlags.SMOOTH;
  1304. if ((indata & 32) != 0) outdata |= (byte)TextureAnimFlags.ROTATE;
  1305. if ((indata & 64) != 0) outdata |= (byte)TextureAnimFlags.SCALE;
  1306. return outdata;
  1307. }
  1308. /// <summary>
  1309. /// Duplicates this part.
  1310. /// </summary>
  1311. /// <param name="localID"></param>
  1312. /// <param name="AgentID"></param>
  1313. /// <param name="GroupID"></param>
  1314. /// <param name="linkNum"></param>
  1315. /// <param name="userExposed">True if the duplicate will immediately be in the scene, false otherwise</param>
  1316. /// <returns></returns>
  1317. public SceneObjectPart Copy(uint localID, UUID AgentID, UUID GroupID, int linkNum, bool userExposed)
  1318. {
  1319. SceneObjectPart dupe = (SceneObjectPart)MemberwiseClone();
  1320. dupe.m_shape = m_shape.Copy();
  1321. dupe.m_regionHandle = m_regionHandle;
  1322. if (userExposed)
  1323. dupe.UUID = UUID.Random();
  1324. //memberwiseclone means it also clones the physics actor reference
  1325. // This will make physical prim 'bounce' if not set to null.
  1326. if (!userExposed)
  1327. dupe.PhysActor = null;
  1328. dupe.OwnerID = AgentID;
  1329. dupe.GroupID = GroupID;
  1330. dupe.GroupPosition = GroupPosition;
  1331. dupe.OffsetPosition = OffsetPosition;
  1332. dupe.RotationOffset = RotationOffset;
  1333. dupe.Velocity = Velocity;
  1334. dupe.Acceleration = Acceleration;
  1335. dupe.AngularVelocity = AngularVelocity;
  1336. dupe.Flags = Flags;
  1337. dupe.OwnershipCost = OwnershipCost;
  1338. dupe.ObjectSaleType = ObjectSaleType;
  1339. dupe.SalePrice = SalePrice;
  1340. dupe.Category = Category;
  1341. dupe.m_rezzed = m_rezzed;
  1342. dupe.m_inventory = new SceneObjectPartInventory(dupe);
  1343. dupe.m_inventory.Items = (TaskInventoryDictionary)m_inventory.Items.Clone();
  1344. if (userExposed)
  1345. {
  1346. dupe.ResetIDs(linkNum);
  1347. dupe.m_inventory.HasInventoryChanged = true;
  1348. }
  1349. else
  1350. {
  1351. dupe.m_inventory.HasInventoryChanged = m_inventory.HasInventoryChanged;
  1352. }
  1353. // Move afterwards ResetIDs as it clears the localID
  1354. dupe.LocalId = localID;
  1355. // This may be wrong... it might have to be applied in SceneObjectGroup to the object that's being duplicated.
  1356. dupe.LastOwnerID = OwnerID;
  1357. byte[] extraP = new byte[Shape.ExtraParams.Length];
  1358. Array.Copy(Shape.ExtraParams, extraP, extraP.Length);
  1359. dupe.Shape.ExtraParams = extraP;
  1360. if (userExposed)
  1361. {
  1362. if (dupe.m_shape.SculptEntry && dupe.m_shape.SculptTexture != UUID.Zero)
  1363. {
  1364. ParentGroup.Scene.AssetService.Get(
  1365. dupe.m_shape.SculptTexture.ToString(), dupe, dupe.AssetReceived);
  1366. }
  1367. bool UsePhysics = ((dupe.Flags & PrimFlags.Physics) != 0);
  1368. dupe.DoPhysicsPropertyUpdate(UsePhysics, true);
  1369. }
  1370. ParentGroup.Scene.EventManager.TriggerOnSceneObjectPartCopy(dupe, this, userExposed);
  1371. // m_log.DebugFormat("[SCENE OBJECT PART]: Clone of {0} {1} finished", Name, UUID);
  1372. return dupe;
  1373. }
  1374. /// <summary>
  1375. /// Called back by asynchronous asset fetch.
  1376. /// </summary>
  1377. /// <param name="id">ID of asset received</param>
  1378. /// <param name="sender">Register</param>
  1379. /// <param name="asset"></param>
  1380. protected void AssetReceived(string id, Object sender, AssetBase asset)
  1381. {
  1382. if (asset != null)
  1383. SculptTextureCallback(asset);
  1384. else
  1385. m_log.WarnFormat(
  1386. "[SCENE OBJECT PART]: Part {0} {1} requested mesh/sculpt data for asset id {2} from asset service but received no data",
  1387. Name, UUID, id);
  1388. }
  1389. /// <summary>
  1390. /// Do a physics property update for a NINJA joint.
  1391. /// </summary>
  1392. /// <param name="UsePhysics"></param>
  1393. /// <param name="isNew"></param>
  1394. protected void DoPhysicsPropertyUpdateForNinjaJoint(bool UsePhysics, bool isNew)
  1395. {
  1396. if (UsePhysics)
  1397. {
  1398. // by turning a joint proxy object physical, we cause creation of a joint in the ODE scene.
  1399. // note that, as a special case, joints have no bodies or geoms in the physics scene, even though they are physical.
  1400. PhysicsJointType jointType;
  1401. if (IsHingeJoint())
  1402. {
  1403. jointType = PhysicsJointType.Hinge;
  1404. }
  1405. else if (IsBallJoint())
  1406. {
  1407. jointType = PhysicsJointType.Ball;
  1408. }
  1409. else
  1410. {
  1411. jointType = PhysicsJointType.Ball;
  1412. }
  1413. List<string> bodyNames = new List<string>();
  1414. string RawParams = Description;
  1415. string[] jointParams = RawParams.Split(" ".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries);
  1416. string trackedBodyName = null;
  1417. if (jointParams.Length >= 2)
  1418. {
  1419. for (int iBodyName = 0; iBodyName < 2; iBodyName++)
  1420. {
  1421. string bodyName = jointParams[iBodyName];
  1422. bodyNames.Add(bodyName);
  1423. if (bodyName != "NULL")
  1424. {
  1425. if (trackedBodyName == null)
  1426. {
  1427. trackedBodyName = bodyName;
  1428. }
  1429. }
  1430. }
  1431. }
  1432. SceneObjectPart trackedBody = ParentGroup.Scene.GetSceneObjectPart(trackedBodyName); // FIXME: causes a sequential lookup
  1433. Quaternion localRotation = Quaternion.Identity;
  1434. if (trackedBody != null)
  1435. {
  1436. localRotation = Quaternion.Inverse(trackedBody.RotationOffset) * this.RotationOffset;
  1437. }
  1438. else
  1439. {
  1440. // error, output it below
  1441. }
  1442. PhysicsJoint joint;
  1443. joint = ParentGroup.Scene.PhysicsScene.RequestJointCreation(Name, jointType,
  1444. AbsolutePosition,
  1445. this.RotationOffset,
  1446. Description,
  1447. bodyNames,
  1448. trackedBodyName,
  1449. localRotation);
  1450. if (trackedBody == null)
  1451. {
  1452. ParentGroup.Scene.jointErrorMessage(joint, "warning: tracked body name not found! joint location will not be updated properly. joint: " + Name);
  1453. }
  1454. }
  1455. else
  1456. {
  1457. if (isNew)
  1458. {
  1459. // if the joint proxy is new, and it is not physical, do nothing. There is no joint in ODE to
  1460. // delete, and if we try to delete it, due to asynchronous processing, the deletion request
  1461. // will get processed later at an indeterminate time, which could cancel a later-arriving
  1462. // joint creation request.
  1463. }
  1464. else
  1465. {
  1466. // here we turn off the joint object, so remove the joint from the physics scene
  1467. ParentGroup.Scene.PhysicsScene.RequestJointDeletion(Name); // FIXME: what if the name changed?
  1468. // make sure client isn't interpolating the joint proxy object
  1469. Velocity = Vector3.Zero;
  1470. AngularVelocity = Vector3.Zero;
  1471. Acceleration = Vector3.Zero;
  1472. }
  1473. }
  1474. }
  1475. /// <summary>
  1476. /// Do a physics propery update for this part.
  1477. /// </summary>
  1478. /// <param name="UsePhysics"></param>
  1479. /// <param name="isNew"></param>
  1480. public void DoPhysicsPropertyUpdate(bool UsePhysics, bool isNew)
  1481. {
  1482. if (ParentGroup.Scene == null)
  1483. return;
  1484. if (!ParentGroup.Scene.PhysicalPrims && UsePhysics)
  1485. return;
  1486. if (IsJoint())
  1487. {
  1488. DoPhysicsPropertyUpdateForNinjaJoint(UsePhysics, isNew);
  1489. }
  1490. else
  1491. {
  1492. PhysicsActor pa = PhysActor;
  1493. if (pa != null)
  1494. {
  1495. if (UsePhysics != pa.IsPhysical || isNew)
  1496. {
  1497. if (pa.IsPhysical) // implies UsePhysics==false for this block
  1498. {
  1499. if (!isNew)
  1500. ParentGroup.Scene.RemovePhysicalPrim(1);
  1501. pa.OnRequestTerseUpdate -= PhysicsRequestingTerseUpdate;
  1502. pa.OnOutOfBounds -= PhysicsOutOfBounds;
  1503. pa.delink();
  1504. if (ParentGroup.Scene.PhysicsScene.SupportsNINJAJoints && (!isNew))
  1505. {
  1506. // destroy all joints connected to this now deactivated body
  1507. ParentGroup.Scene.PhysicsScene.RemoveAllJointsConnectedToActorThreadLocked(pa);
  1508. }
  1509. // stop client-side interpolation of all joint proxy objects that have just been deleted
  1510. // this is done because RemoveAllJointsConnectedToActor invokes the OnJointDeactivated callback,
  1511. // which stops client-side interpolation of deactivated joint proxy objects.
  1512. }
  1513. if (!UsePhysics && !isNew)
  1514. {
  1515. // reset velocity to 0 on physics switch-off. Without that, the client thinks the
  1516. // prim still has velocity and continues to interpolate its position along the old
  1517. // velocity-vector.
  1518. Velocity = new Vector3(0, 0, 0);
  1519. Acceleration = new Vector3(0, 0, 0);
  1520. AngularVelocity = new Vector3(0, 0, 0);
  1521. //RotationalVelocity = new Vector3(0, 0, 0);
  1522. }
  1523. pa.IsPhysical = UsePhysics;
  1524. // If we're not what we're supposed to be in the physics scene, recreate ourselves.
  1525. //m_parentGroup.Scene.PhysicsScene.RemovePrim(PhysActor);
  1526. /// that's not wholesome. Had to make Scene public
  1527. //PhysActor = null;
  1528. if ((Flags & PrimFlags.Phantom) == 0)
  1529. {
  1530. if (UsePhysics)
  1531. {
  1532. ParentGroup.Scene.AddPhysicalPrim(1);
  1533. pa.OnRequestTerseUpdate += PhysicsRequestingTerseUpdate;
  1534. pa.OnOutOfBounds += PhysicsOutOfBounds;
  1535. if (ParentID != 0 && ParentID != LocalId)
  1536. {
  1537. PhysicsActor parentPa = ParentGroup.RootPart.PhysActor;
  1538. if (parentPa != null)
  1539. {
  1540. pa.link(parentPa);
  1541. }
  1542. }
  1543. }
  1544. }
  1545. }
  1546. // If this part is a sculpt then delay the physics update until we've asynchronously loaded the
  1547. // mesh data.
  1548. if (Shape.SculptEntry)
  1549. CheckSculptAndLoad();
  1550. else
  1551. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1552. }
  1553. }
  1554. }
  1555. /// <summary>
  1556. /// Restore this part from the serialized xml representation.
  1557. /// </summary>
  1558. /// <param name="xmlReader"></param>
  1559. /// <returns></returns>
  1560. public static SceneObjectPart FromXml(XmlTextReader xmlReader)
  1561. {
  1562. SceneObjectPart part = SceneObjectSerializer.Xml2ToSOP(xmlReader);
  1563. // for tempOnRez objects, we have to fix the Expire date.
  1564. if ((part.Flags & PrimFlags.TemporaryOnRez) != 0)
  1565. part.ResetExpire();
  1566. return part;
  1567. }
  1568. public bool GetDieAtEdge()
  1569. {
  1570. if (ParentGroup.IsDeleted)
  1571. return false;
  1572. return ParentGroup.RootPart.DIE_AT_EDGE;
  1573. }
  1574. public bool GetReturnAtEdge()
  1575. {
  1576. if (ParentGroup.IsDeleted)
  1577. return false;
  1578. return ParentGroup.RootPart.RETURN_AT_EDGE;
  1579. }
  1580. public void SetReturnAtEdge(bool p)
  1581. {
  1582. if (ParentGroup.IsDeleted)
  1583. return;
  1584. ParentGroup.RootPart.RETURN_AT_EDGE = p;
  1585. }
  1586. public bool GetBlockGrab()
  1587. {
  1588. if (ParentGroup.IsDeleted)
  1589. return false;
  1590. return ParentGroup.RootPart.BlockGrab;
  1591. }
  1592. public void SetBlockGrab(bool p)
  1593. {
  1594. if (ParentGroup.IsDeleted)
  1595. return;
  1596. ParentGroup.RootPart.BlockGrab = p;
  1597. }
  1598. public void SetStatusSandbox(bool p)
  1599. {
  1600. if (ParentGroup.IsDeleted)
  1601. return;
  1602. StatusSandboxPos = ParentGroup.RootPart.AbsolutePosition;
  1603. ParentGroup.RootPart.StatusSandbox = p;
  1604. }
  1605. public bool GetStatusSandbox()
  1606. {
  1607. if (ParentGroup.IsDeleted)
  1608. return false;
  1609. return ParentGroup.RootPart.StatusSandbox;
  1610. }
  1611. public int GetAxisRotation(int axis)
  1612. {
  1613. //Cannot use ScriptBaseClass constants as no referance to it currently.
  1614. if (axis == 2)//STATUS_ROTATE_X
  1615. return STATUS_ROTATE_X;
  1616. if (axis == 4)//STATUS_ROTATE_Y
  1617. return STATUS_ROTATE_Y;
  1618. if (axis == 8)//STATUS_ROTATE_Z
  1619. return STATUS_ROTATE_Z;
  1620. return 0;
  1621. }
  1622. public double GetDistanceTo(Vector3 a, Vector3 b)
  1623. {
  1624. float dx = a.X - b.X;
  1625. float dy = a.Y - b.Y;
  1626. float dz = a.Z - b.Z;
  1627. return Math.Sqrt(dx * dx + dy * dy + dz * dz);
  1628. }
  1629. public uint GetEffectiveObjectFlags()
  1630. {
  1631. // Commenting this section of code out since it doesn't actually do anything, as enums are handled by
  1632. // value rather than reference
  1633. // PrimFlags f = _flags;
  1634. // if (m_parentGroup == null || m_parentGroup.RootPart == this)
  1635. // f &= ~(PrimFlags.Touch | PrimFlags.Money);
  1636. return (uint)Flags | (uint)LocalFlags;
  1637. }
  1638. public Vector3 GetGeometricCenter()
  1639. {
  1640. PhysicsActor pa = PhysActor;
  1641. if (pa != null)
  1642. return new Vector3(pa.CenterOfMass.X, pa.CenterOfMass.Y, pa.CenterOfMass.Z);
  1643. else
  1644. return new Vector3(0, 0, 0);
  1645. }
  1646. public float GetMass()
  1647. {
  1648. PhysicsActor pa = PhysActor;
  1649. if (pa != null)
  1650. return pa.Mass;
  1651. else
  1652. return 0;
  1653. }
  1654. public Vector3 GetForce()
  1655. {
  1656. PhysicsActor pa = PhysActor;
  1657. if (pa != null)
  1658. return pa.Force;
  1659. else
  1660. return Vector3.Zero;
  1661. }
  1662. /// <summary>
  1663. /// Method for a prim to get it's world position from the group.
  1664. /// </summary>
  1665. /// <remarks>
  1666. /// Remember, the Group Position simply gives the position of the group itself
  1667. /// </remarks>
  1668. /// <returns>A Linked Child Prim objects position in world</returns>
  1669. public Vector3 GetWorldPosition()
  1670. {
  1671. Quaternion parentRot = ParentGroup.RootPart.RotationOffset;
  1672. Vector3 axPos = OffsetPosition;
  1673. axPos *= parentRot;
  1674. Vector3 translationOffsetPosition = axPos;
  1675. if(_parentID == 0)
  1676. return GroupPosition;
  1677. else
  1678. return ParentGroup.AbsolutePosition + translationOffsetPosition;
  1679. }
  1680. /// <summary>
  1681. /// Gets the rotation of this prim offset by the group rotation
  1682. /// </summary>
  1683. /// <returns></returns>
  1684. public Quaternion GetWorldRotation()
  1685. {
  1686. Quaternion newRot;
  1687. if (this.LinkNum == 0 || this.LinkNum == 1)
  1688. {
  1689. newRot = RotationOffset;
  1690. }
  1691. else
  1692. {
  1693. Quaternion parentRot = ParentGroup.RootPart.RotationOffset;
  1694. Quaternion oldRot = RotationOffset;
  1695. newRot = parentRot * oldRot;
  1696. }
  1697. return newRot;
  1698. }
  1699. public void MoveToTarget(Vector3 target, float tau)
  1700. {
  1701. if (tau > 0)
  1702. {
  1703. ParentGroup.moveToTarget(target, tau);
  1704. }
  1705. else
  1706. {
  1707. StopMoveToTarget();
  1708. }
  1709. }
  1710. /// <summary>
  1711. /// Uses a PID to attempt to clamp the object on the Z axis at the given height over tau seconds.
  1712. /// </summary>
  1713. /// <param name="height">Height to hover. Height of zero disables hover.</param>
  1714. /// <param name="hoverType">Determines what the height is relative to </param>
  1715. /// <param name="tau">Number of seconds over which to reach target</param>
  1716. public void SetHoverHeight(float height, PIDHoverType hoverType, float tau)
  1717. {
  1718. ParentGroup.SetHoverHeight(height, hoverType, tau);
  1719. }
  1720. public void StopHover()
  1721. {
  1722. ParentGroup.SetHoverHeight(0f, PIDHoverType.Ground, 0f);
  1723. }
  1724. public virtual void OnGrab(Vector3 offsetPos, IClientAPI remoteClient)
  1725. {
  1726. }
  1727. public void PhysicsCollision(EventArgs e)
  1728. {
  1729. // m_log.DebugFormat("Invoking PhysicsCollision on {0} {1} {2}", Name, LocalId, UUID);
  1730. // single threaded here
  1731. CollisionEventUpdate a = (CollisionEventUpdate)e;
  1732. Dictionary<uint, ContactPoint> collissionswith = a.m_objCollisionList;
  1733. List<uint> thisHitColliders = new List<uint>();
  1734. List<uint> endedColliders = new List<uint>();
  1735. List<uint> startedColliders = new List<uint>();
  1736. // calculate things that started colliding this time
  1737. // and build up list of colliders this time
  1738. foreach (uint localid in collissionswith.Keys)
  1739. {
  1740. thisHitColliders.Add(localid);
  1741. if (!m_lastColliders.Contains(localid))
  1742. {
  1743. startedColliders.Add(localid);
  1744. }
  1745. //m_log.Debug("[OBJECT]: Collided with:" + localid.ToString() + " at depth of: " + collissionswith[localid].ToString());
  1746. }
  1747. // calculate things that ended colliding
  1748. foreach (uint localID in m_lastColliders)
  1749. {
  1750. if (!thisHitColliders.Contains(localID))
  1751. {
  1752. endedColliders.Add(localID);
  1753. }
  1754. }
  1755. //add the items that started colliding this time to the last colliders list.
  1756. foreach (uint localID in startedColliders)
  1757. {
  1758. m_lastColliders.Add(localID);
  1759. }
  1760. // remove things that ended colliding from the last colliders list
  1761. foreach (uint localID in endedColliders)
  1762. {
  1763. m_lastColliders.Remove(localID);
  1764. }
  1765. if (ParentGroup.IsDeleted)
  1766. return;
  1767. // play the sound.
  1768. if (startedColliders.Count > 0 && CollisionSound != UUID.Zero && CollisionSoundVolume > 0.0f)
  1769. {
  1770. SendSound(CollisionSound.ToString(), CollisionSoundVolume, true, (byte)0, 0, false, false);
  1771. }
  1772. if ((ParentGroup.RootPart.ScriptEvents & scriptEvents.collision_start) != 0)
  1773. {
  1774. // do event notification
  1775. if (startedColliders.Count > 0)
  1776. {
  1777. ColliderArgs StartCollidingMessage = new ColliderArgs();
  1778. List<DetectedObject> colliding = new List<DetectedObject>();
  1779. foreach (uint localId in startedColliders)
  1780. {
  1781. if (localId == 0)
  1782. continue;
  1783. if (ParentGroup.Scene == null)
  1784. return;
  1785. SceneObjectPart obj = ParentGroup.Scene.GetSceneObjectPart(localId);
  1786. string data = "";
  1787. if (obj != null)
  1788. {
  1789. if (ParentGroup.RootPart.CollisionFilter.ContainsValue(obj.UUID.ToString())
  1790. || ParentGroup.RootPart.CollisionFilter.ContainsValue(obj.Name))
  1791. {
  1792. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  1793. //If it is 1, it is to accept ONLY collisions from this object
  1794. if (found)
  1795. {
  1796. DetectedObject detobj = new DetectedObject();
  1797. detobj.keyUUID = obj.UUID;
  1798. detobj.nameStr = obj.Name;
  1799. detobj.ownerUUID = obj.OwnerID;
  1800. detobj.posVector = obj.AbsolutePosition;
  1801. detobj.rotQuat = obj.GetWorldRotation();
  1802. detobj.velVector = obj.Velocity;
  1803. detobj.colliderType = 0;
  1804. detobj.groupUUID = obj.GroupID;
  1805. colliding.Add(detobj);
  1806. }
  1807. //If it is 0, it is to not accept collisions from this object
  1808. else
  1809. {
  1810. }
  1811. }
  1812. else
  1813. {
  1814. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  1815. //If it is 1, it is to accept ONLY collisions from this object, so this other object will not work
  1816. if (!found)
  1817. {
  1818. DetectedObject detobj = new DetectedObject();
  1819. detobj.keyUUID = obj.UUID;
  1820. detobj.nameStr = obj.Name;
  1821. detobj.ownerUUID = obj.OwnerID;
  1822. detobj.posVector = obj.AbsolutePosition;
  1823. detobj.rotQuat = obj.GetWorldRotation();
  1824. detobj.velVector = obj.Velocity;
  1825. detobj.colliderType = 0;
  1826. detobj.groupUUID = obj.GroupID;
  1827. colliding.Add(detobj);
  1828. }
  1829. }
  1830. }
  1831. else
  1832. {
  1833. ParentGroup.Scene.ForEachRootScenePresence(delegate(ScenePresence av)
  1834. {
  1835. if (av.LocalId == localId)
  1836. {
  1837. if (ParentGroup.RootPart.CollisionFilter.ContainsValue(av.UUID.ToString())
  1838. || ParentGroup.RootPart.CollisionFilter.ContainsValue(av.Name))
  1839. {
  1840. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  1841. //If it is 1, it is to accept ONLY collisions from this avatar
  1842. if (found)
  1843. {
  1844. DetectedObject detobj = new DetectedObject();
  1845. detobj.keyUUID = av.UUID;
  1846. detobj.nameStr = av.ControllingClient.Name;
  1847. detobj.ownerUUID = av.UUID;
  1848. detobj.posVector = av.AbsolutePosition;
  1849. detobj.rotQuat = av.Rotation;
  1850. detobj.velVector = av.Velocity;
  1851. detobj.colliderType = 0;
  1852. detobj.groupUUID = av.ControllingClient.ActiveGroupId;
  1853. colliding.Add(detobj);
  1854. }
  1855. //If it is 0, it is to not accept collisions from this avatar
  1856. else
  1857. {
  1858. }
  1859. }
  1860. else
  1861. {
  1862. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  1863. //If it is 1, it is to accept ONLY collisions from this avatar, so this other avatar will not work
  1864. if (!found)
  1865. {
  1866. DetectedObject detobj = new DetectedObject();
  1867. detobj.keyUUID = av.UUID;
  1868. detobj.nameStr = av.ControllingClient.Name;
  1869. detobj.ownerUUID = av.UUID;
  1870. detobj.posVector = av.AbsolutePosition;
  1871. detobj.rotQuat = av.Rotation;
  1872. detobj.velVector = av.Velocity;
  1873. detobj.colliderType = 0;
  1874. detobj.groupUUID = av.ControllingClient.ActiveGroupId;
  1875. colliding.Add(detobj);
  1876. }
  1877. }
  1878. }
  1879. });
  1880. }
  1881. }
  1882. if (colliding.Count > 0)
  1883. {
  1884. StartCollidingMessage.Colliders = colliding;
  1885. if (ParentGroup.Scene == null)
  1886. return;
  1887. // if (m_parentGroup.PassCollision == true)
  1888. // {
  1889. // //TODO: Add pass to root prim!
  1890. // }
  1891. ParentGroup.Scene.EventManager.TriggerScriptCollidingStart(LocalId, StartCollidingMessage);
  1892. }
  1893. }
  1894. }
  1895. if ((ParentGroup.RootPart.ScriptEvents & scriptEvents.collision) != 0)
  1896. {
  1897. if (m_lastColliders.Count > 0)
  1898. {
  1899. ColliderArgs CollidingMessage = new ColliderArgs();
  1900. List<DetectedObject> colliding = new List<DetectedObject>();
  1901. foreach (uint localId in m_lastColliders)
  1902. {
  1903. // always running this check because if the user deletes the object it would return a null reference.
  1904. if (localId == 0)
  1905. continue;
  1906. if (ParentGroup.Scene == null)
  1907. return;
  1908. SceneObjectPart obj = ParentGroup.Scene.GetSceneObjectPart(localId);
  1909. string data = "";
  1910. if (obj != null)
  1911. {
  1912. if (ParentGroup.RootPart.CollisionFilter.ContainsValue(obj.UUID.ToString())
  1913. || ParentGroup.RootPart.CollisionFilter.ContainsValue(obj.Name))
  1914. {
  1915. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1,out data);
  1916. //If it is 1, it is to accept ONLY collisions from this object
  1917. if (found)
  1918. {
  1919. DetectedObject detobj = new DetectedObject();
  1920. detobj.keyUUID = obj.UUID;
  1921. detobj.nameStr = obj.Name;
  1922. detobj.ownerUUID = obj.OwnerID;
  1923. detobj.posVector = obj.AbsolutePosition;
  1924. detobj.rotQuat = obj.GetWorldRotation();
  1925. detobj.velVector = obj.Velocity;
  1926. detobj.colliderType = 0;
  1927. detobj.groupUUID = obj.GroupID;
  1928. colliding.Add(detobj);
  1929. }
  1930. //If it is 0, it is to not accept collisions from this object
  1931. else
  1932. {
  1933. }
  1934. }
  1935. else
  1936. {
  1937. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1,out data);
  1938. //If it is 1, it is to accept ONLY collisions from this object, so this other object will not work
  1939. if (!found)
  1940. {
  1941. DetectedObject detobj = new DetectedObject();
  1942. detobj.keyUUID = obj.UUID;
  1943. detobj.nameStr = obj.Name;
  1944. detobj.ownerUUID = obj.OwnerID;
  1945. detobj.posVector = obj.AbsolutePosition;
  1946. detobj.rotQuat = obj.GetWorldRotation();
  1947. detobj.velVector = obj.Velocity;
  1948. detobj.colliderType = 0;
  1949. detobj.groupUUID = obj.GroupID;
  1950. colliding.Add(detobj);
  1951. }
  1952. }
  1953. }
  1954. else
  1955. {
  1956. ParentGroup.Scene.ForEachRootScenePresence(delegate(ScenePresence av)
  1957. {
  1958. if (av.LocalId == localId)
  1959. {
  1960. if (ParentGroup.RootPart.CollisionFilter.ContainsValue(av.UUID.ToString())
  1961. || ParentGroup.RootPart.CollisionFilter.ContainsValue(av.Name))
  1962. {
  1963. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  1964. //If it is 1, it is to accept ONLY collisions from this avatar
  1965. if (found)
  1966. {
  1967. DetectedObject detobj = new DetectedObject();
  1968. detobj.keyUUID = av.UUID;
  1969. detobj.nameStr = av.ControllingClient.Name;
  1970. detobj.ownerUUID = av.UUID;
  1971. detobj.posVector = av.AbsolutePosition;
  1972. detobj.rotQuat = av.Rotation;
  1973. detobj.velVector = av.Velocity;
  1974. detobj.colliderType = 0;
  1975. detobj.groupUUID = av.ControllingClient.ActiveGroupId;
  1976. colliding.Add(detobj);
  1977. }
  1978. //If it is 0, it is to not accept collisions from this avatar
  1979. else
  1980. {
  1981. }
  1982. }
  1983. else
  1984. {
  1985. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  1986. //If it is 1, it is to accept ONLY collisions from this avatar, so this other avatar will not work
  1987. if (!found)
  1988. {
  1989. DetectedObject detobj = new DetectedObject();
  1990. detobj.keyUUID = av.UUID;
  1991. detobj.nameStr = av.ControllingClient.Name;
  1992. detobj.ownerUUID = av.UUID;
  1993. detobj.posVector = av.AbsolutePosition;
  1994. detobj.rotQuat = av.Rotation;
  1995. detobj.velVector = av.Velocity;
  1996. detobj.colliderType = 0;
  1997. detobj.groupUUID = av.ControllingClient.ActiveGroupId;
  1998. colliding.Add(detobj);
  1999. }
  2000. }
  2001. }
  2002. });
  2003. }
  2004. }
  2005. if (colliding.Count > 0)
  2006. {
  2007. CollidingMessage.Colliders = colliding;
  2008. if (ParentGroup.Scene == null)
  2009. return;
  2010. ParentGroup.Scene.EventManager.TriggerScriptColliding(LocalId, CollidingMessage);
  2011. }
  2012. }
  2013. }
  2014. if ((ParentGroup.RootPart.ScriptEvents & scriptEvents.collision_end) != 0)
  2015. {
  2016. if (endedColliders.Count > 0)
  2017. {
  2018. ColliderArgs EndCollidingMessage = new ColliderArgs();
  2019. List<DetectedObject> colliding = new List<DetectedObject>();
  2020. foreach (uint localId in endedColliders)
  2021. {
  2022. if (localId == 0)
  2023. continue;
  2024. if (ParentGroup.Scene == null)
  2025. return;
  2026. SceneObjectPart obj = ParentGroup.Scene.GetSceneObjectPart(localId);
  2027. string data = "";
  2028. if (obj != null)
  2029. {
  2030. if (ParentGroup.RootPart.CollisionFilter.ContainsValue(obj.UUID.ToString()) || ParentGroup.RootPart.CollisionFilter.ContainsValue(obj.Name))
  2031. {
  2032. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1,out data);
  2033. //If it is 1, it is to accept ONLY collisions from this object
  2034. if (found)
  2035. {
  2036. DetectedObject detobj = new DetectedObject();
  2037. detobj.keyUUID = obj.UUID;
  2038. detobj.nameStr = obj.Name;
  2039. detobj.ownerUUID = obj.OwnerID;
  2040. detobj.posVector = obj.AbsolutePosition;
  2041. detobj.rotQuat = obj.GetWorldRotation();
  2042. detobj.velVector = obj.Velocity;
  2043. detobj.colliderType = 0;
  2044. detobj.groupUUID = obj.GroupID;
  2045. colliding.Add(detobj);
  2046. }
  2047. //If it is 0, it is to not accept collisions from this object
  2048. else
  2049. {
  2050. }
  2051. }
  2052. else
  2053. {
  2054. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1,out data);
  2055. //If it is 1, it is to accept ONLY collisions from this object, so this other object will not work
  2056. if (!found)
  2057. {
  2058. DetectedObject detobj = new DetectedObject();
  2059. detobj.keyUUID = obj.UUID;
  2060. detobj.nameStr = obj.Name;
  2061. detobj.ownerUUID = obj.OwnerID;
  2062. detobj.posVector = obj.AbsolutePosition;
  2063. detobj.rotQuat = obj.GetWorldRotation();
  2064. detobj.velVector = obj.Velocity;
  2065. detobj.colliderType = 0;
  2066. detobj.groupUUID = obj.GroupID;
  2067. colliding.Add(detobj);
  2068. }
  2069. }
  2070. }
  2071. else
  2072. {
  2073. ParentGroup.Scene.ForEachRootScenePresence(delegate(ScenePresence av)
  2074. {
  2075. if (av.LocalId == localId)
  2076. {
  2077. if (ParentGroup.RootPart.CollisionFilter.ContainsValue(av.UUID.ToString())
  2078. || ParentGroup.RootPart.CollisionFilter.ContainsValue(av.Name))
  2079. {
  2080. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  2081. //If it is 1, it is to accept ONLY collisions from this avatar
  2082. if (found)
  2083. {
  2084. DetectedObject detobj = new DetectedObject();
  2085. detobj.keyUUID = av.UUID;
  2086. detobj.nameStr = av.ControllingClient.Name;
  2087. detobj.ownerUUID = av.UUID;
  2088. detobj.posVector = av.AbsolutePosition;
  2089. detobj.rotQuat = av.Rotation;
  2090. detobj.velVector = av.Velocity;
  2091. detobj.colliderType = 0;
  2092. detobj.groupUUID = av.ControllingClient.ActiveGroupId;
  2093. colliding.Add(detobj);
  2094. }
  2095. //If it is 0, it is to not accept collisions from this avatar
  2096. else
  2097. {
  2098. }
  2099. }
  2100. else
  2101. {
  2102. bool found = ParentGroup.RootPart.CollisionFilter.TryGetValue(1, out data);
  2103. //If it is 1, it is to accept ONLY collisions from this avatar, so this other avatar will not work
  2104. if (!found)
  2105. {
  2106. DetectedObject detobj = new DetectedObject();
  2107. detobj.keyUUID = av.UUID;
  2108. detobj.nameStr = av.ControllingClient.Name;
  2109. detobj.ownerUUID = av.UUID;
  2110. detobj.posVector = av.AbsolutePosition;
  2111. detobj.rotQuat = av.Rotation;
  2112. detobj.velVector = av.Velocity;
  2113. detobj.colliderType = 0;
  2114. detobj.groupUUID = av.ControllingClient.ActiveGroupId;
  2115. colliding.Add(detobj);
  2116. }
  2117. }
  2118. }
  2119. });
  2120. }
  2121. }
  2122. if (colliding.Count > 0)
  2123. {
  2124. EndCollidingMessage.Colliders = colliding;
  2125. if (ParentGroup.Scene == null)
  2126. return;
  2127. ParentGroup.Scene.EventManager.TriggerScriptCollidingEnd(LocalId, EndCollidingMessage);
  2128. }
  2129. }
  2130. }
  2131. if ((ParentGroup.RootPart.ScriptEvents & scriptEvents.land_collision_start) != 0)
  2132. {
  2133. if (startedColliders.Count > 0)
  2134. {
  2135. ColliderArgs LandStartCollidingMessage = new ColliderArgs();
  2136. List<DetectedObject> colliding = new List<DetectedObject>();
  2137. foreach (uint localId in startedColliders)
  2138. {
  2139. if (localId == 0)
  2140. {
  2141. //Hope that all is left is ground!
  2142. DetectedObject detobj = new DetectedObject();
  2143. detobj.keyUUID = UUID.Zero;
  2144. detobj.nameStr = "";
  2145. detobj.ownerUUID = UUID.Zero;
  2146. detobj.posVector = ParentGroup.RootPart.AbsolutePosition;
  2147. detobj.rotQuat = Quaternion.Identity;
  2148. detobj.velVector = Vector3.Zero;
  2149. detobj.colliderType = 0;
  2150. detobj.groupUUID = UUID.Zero;
  2151. colliding.Add(detobj);
  2152. }
  2153. }
  2154. if (colliding.Count > 0)
  2155. {
  2156. LandStartCollidingMessage.Colliders = colliding;
  2157. if (ParentGroup.Scene == null)
  2158. return;
  2159. ParentGroup.Scene.EventManager.TriggerScriptLandCollidingStart(LocalId, LandStartCollidingMessage);
  2160. }
  2161. }
  2162. }
  2163. if ((ParentGroup.RootPart.ScriptEvents & scriptEvents.land_collision) != 0)
  2164. {
  2165. if (m_lastColliders.Count > 0)
  2166. {
  2167. ColliderArgs LandCollidingMessage = new ColliderArgs();
  2168. List<DetectedObject> colliding = new List<DetectedObject>();
  2169. foreach (uint localId in startedColliders)
  2170. {
  2171. if (localId == 0)
  2172. {
  2173. //Hope that all is left is ground!
  2174. DetectedObject detobj = new DetectedObject();
  2175. detobj.keyUUID = UUID.Zero;
  2176. detobj.nameStr = "";
  2177. detobj.ownerUUID = UUID.Zero;
  2178. detobj.posVector = ParentGroup.RootPart.AbsolutePosition;
  2179. detobj.rotQuat = Quaternion.Identity;
  2180. detobj.velVector = Vector3.Zero;
  2181. detobj.colliderType = 0;
  2182. detobj.groupUUID = UUID.Zero;
  2183. colliding.Add(detobj);
  2184. }
  2185. }
  2186. if (colliding.Count > 0)
  2187. {
  2188. LandCollidingMessage.Colliders = colliding;
  2189. if (ParentGroup.Scene == null)
  2190. return;
  2191. ParentGroup.Scene.EventManager.TriggerScriptLandColliding(LocalId, LandCollidingMessage);
  2192. }
  2193. }
  2194. }
  2195. if ((ParentGroup.RootPart.ScriptEvents & scriptEvents.land_collision_end) != 0)
  2196. {
  2197. if (endedColliders.Count > 0)
  2198. {
  2199. ColliderArgs LandEndCollidingMessage = new ColliderArgs();
  2200. List<DetectedObject> colliding = new List<DetectedObject>();
  2201. foreach (uint localId in startedColliders)
  2202. {
  2203. if (localId == 0)
  2204. {
  2205. //Hope that all is left is ground!
  2206. DetectedObject detobj = new DetectedObject();
  2207. detobj.keyUUID = UUID.Zero;
  2208. detobj.nameStr = "";
  2209. detobj.ownerUUID = UUID.Zero;
  2210. detobj.posVector = ParentGroup.RootPart.AbsolutePosition;
  2211. detobj.rotQuat = Quaternion.Identity;
  2212. detobj.velVector = Vector3.Zero;
  2213. detobj.colliderType = 0;
  2214. detobj.groupUUID = UUID.Zero;
  2215. colliding.Add(detobj);
  2216. }
  2217. }
  2218. if (colliding.Count > 0)
  2219. {
  2220. LandEndCollidingMessage.Colliders = colliding;
  2221. if (ParentGroup.Scene == null)
  2222. return;
  2223. ParentGroup.Scene.EventManager.TriggerScriptLandCollidingEnd(LocalId, LandEndCollidingMessage);
  2224. }
  2225. }
  2226. }
  2227. }
  2228. public void PhysicsOutOfBounds(Vector3 pos)
  2229. {
  2230. m_log.Error("[PHYSICS]: Physical Object went out of bounds.");
  2231. RemFlag(PrimFlags.Physics);
  2232. DoPhysicsPropertyUpdate(false, true);
  2233. //ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(PhysActor);
  2234. }
  2235. public void PhysicsRequestingTerseUpdate()
  2236. {
  2237. PhysicsActor pa = PhysActor;
  2238. if (pa != null)
  2239. {
  2240. Vector3 newpos = new Vector3(pa.Position.GetBytes(), 0);
  2241. if (ParentGroup.Scene.TestBorderCross(newpos, Cardinals.N)
  2242. | ParentGroup.Scene.TestBorderCross(newpos, Cardinals.S)
  2243. | ParentGroup.Scene.TestBorderCross(newpos, Cardinals.E)
  2244. | ParentGroup.Scene.TestBorderCross(newpos, Cardinals.W))
  2245. {
  2246. ParentGroup.AbsolutePosition = newpos;
  2247. return;
  2248. }
  2249. //ParentGroup.RootPart.m_groupPosition = newpos;
  2250. }
  2251. ScheduleTerseUpdate();
  2252. }
  2253. public void PreloadSound(string sound)
  2254. {
  2255. // UUID ownerID = OwnerID;
  2256. UUID objectID = ParentGroup.RootPart.UUID;
  2257. UUID soundID = UUID.Zero;
  2258. if (!UUID.TryParse(sound, out soundID))
  2259. {
  2260. //Trys to fetch sound id from prim's inventory.
  2261. //Prim's inventory doesn't support non script items yet
  2262. lock (TaskInventory)
  2263. {
  2264. foreach (KeyValuePair<UUID, TaskInventoryItem> item in TaskInventory)
  2265. {
  2266. if (item.Value.Name == sound)
  2267. {
  2268. soundID = item.Value.ItemID;
  2269. break;
  2270. }
  2271. }
  2272. }
  2273. }
  2274. ParentGroup.Scene.ForEachRootScenePresence(delegate(ScenePresence sp)
  2275. {
  2276. if (!(Util.GetDistanceTo(sp.AbsolutePosition, AbsolutePosition) >= 100))
  2277. sp.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
  2278. });
  2279. }
  2280. public void RemFlag(PrimFlags flag)
  2281. {
  2282. // PrimFlags prevflag = Flags;
  2283. if ((Flags & flag) != 0)
  2284. {
  2285. //m_log.Debug("Removing flag: " + ((PrimFlags)flag).ToString());
  2286. Flags &= ~flag;
  2287. }
  2288. //m_log.Debug("prev: " + prevflag.ToString() + " curr: " + Flags.ToString());
  2289. //ScheduleFullUpdate();
  2290. }
  2291. public void RemoveScriptEvents(UUID scriptid)
  2292. {
  2293. lock (m_scriptEvents)
  2294. {
  2295. if (m_scriptEvents.ContainsKey(scriptid))
  2296. {
  2297. scriptEvents oldparts = scriptEvents.None;
  2298. oldparts = (scriptEvents) m_scriptEvents[scriptid];
  2299. // remove values from aggregated script events
  2300. AggregateScriptEvents &= ~oldparts;
  2301. m_scriptEvents.Remove(scriptid);
  2302. aggregateScriptEvents();
  2303. }
  2304. }
  2305. }
  2306. /// <summary>
  2307. /// Reset UUIDs for this part. This involves generate this part's own UUID and
  2308. /// generating new UUIDs for all the items in the inventory.
  2309. /// </summary>
  2310. /// <param name="linkNum">Link number for the part</param>
  2311. public void ResetIDs(int linkNum)
  2312. {
  2313. UUID = UUID.Random();
  2314. LinkNum = linkNum;
  2315. LocalId = 0;
  2316. Inventory.ResetInventoryIDs();
  2317. }
  2318. /// <summary>
  2319. /// Set the scale of this part.
  2320. /// </summary>
  2321. /// <remarks>
  2322. /// Unlike the scale property, this checks the new size against scene limits and schedules a full property
  2323. /// update to viewers.
  2324. /// </remarks>
  2325. /// <param name="scale"></param>
  2326. public void Resize(Vector3 scale)
  2327. {
  2328. scale.X = Math.Min(scale.X, ParentGroup.Scene.m_maxNonphys);
  2329. scale.Y = Math.Min(scale.Y, ParentGroup.Scene.m_maxNonphys);
  2330. scale.Z = Math.Min(scale.Z, ParentGroup.Scene.m_maxNonphys);
  2331. PhysicsActor pa = PhysActor;
  2332. if (pa != null && pa.IsPhysical)
  2333. {
  2334. scale.X = Math.Min(scale.X, ParentGroup.Scene.m_maxPhys);
  2335. scale.Y = Math.Min(scale.Y, ParentGroup.Scene.m_maxPhys);
  2336. scale.Z = Math.Min(scale.Z, ParentGroup.Scene.m_maxPhys);
  2337. }
  2338. // m_log.DebugFormat("[SCENE OBJECT PART]: Resizing {0} {1} to {2}", Name, LocalId, scale);
  2339. Scale = scale;
  2340. ParentGroup.HasGroupChanged = true;
  2341. ScheduleFullUpdate();
  2342. }
  2343. public void RotLookAt(Quaternion target, float strength, float damping)
  2344. {
  2345. if (ParentGroup.IsAttachment)
  2346. {
  2347. /*
  2348. ScenePresence avatar = m_scene.GetScenePresence(rootpart.AttachedAvatar);
  2349. if (avatar != null)
  2350. {
  2351. Rotate the Av?
  2352. } */
  2353. }
  2354. else
  2355. {
  2356. APIDDamp = damping;
  2357. APIDStrength = strength;
  2358. APIDTarget = target;
  2359. if (APIDStrength <= 0)
  2360. {
  2361. m_log.WarnFormat("[SceneObjectPart] Invalid rotation strength {0}",APIDStrength);
  2362. return;
  2363. }
  2364. m_APIDIterations = 1 + (int)(Math.PI * APIDStrength);
  2365. }
  2366. // Necessary to get the lookat deltas applied
  2367. ParentGroup.QueueForUpdateCheck();
  2368. }
  2369. public void StartLookAt(Quaternion target, float strength, float damping)
  2370. {
  2371. RotLookAt(target,strength,damping);
  2372. }
  2373. public void StopLookAt()
  2374. {
  2375. APIDTarget = Quaternion.Identity;
  2376. }
  2377. /// <summary>
  2378. /// Schedules this prim for a full update
  2379. /// </summary>
  2380. public void ScheduleFullUpdate()
  2381. {
  2382. // m_log.DebugFormat("[SCENE OBJECT PART]: Scheduling full update for {0} {1}", Name, LocalId);
  2383. if (ParentGroup == null)
  2384. return;
  2385. ParentGroup.QueueForUpdateCheck();
  2386. int timeNow = Util.UnixTimeSinceEpoch();
  2387. // If multiple updates are scheduled on the same second, we still need to perform all of them
  2388. // So we'll force the issue by bumping up the timestamp so that later processing sees these need
  2389. // to be performed.
  2390. if (timeNow <= TimeStampFull)
  2391. {
  2392. TimeStampFull += 1;
  2393. }
  2394. else
  2395. {
  2396. TimeStampFull = (uint)timeNow;
  2397. }
  2398. UpdateFlag = UpdateRequired.FULL;
  2399. // m_log.DebugFormat(
  2400. // "[SCENE OBJECT PART]: Scheduling full update for {0}, {1} at {2}",
  2401. // UUID, Name, TimeStampFull);
  2402. if (ParentGroup.Scene != null)
  2403. ParentGroup.Scene.EventManager.TriggerSceneObjectPartUpdated(this);
  2404. }
  2405. /// <summary>
  2406. /// Schedule a terse update for this prim. Terse updates only send position,
  2407. /// rotation, velocity, rotational velocity and shape information.
  2408. /// </summary>
  2409. public void ScheduleTerseUpdate()
  2410. {
  2411. if (ParentGroup == null)
  2412. return;
  2413. // This was pulled from SceneViewer. Attachments always receive full updates.
  2414. // I could not verify if this is a requirement but this maintains existing behavior
  2415. if (ParentGroup.IsAttachment)
  2416. {
  2417. ScheduleFullUpdate();
  2418. return;
  2419. }
  2420. if (UpdateFlag == UpdateRequired.NONE)
  2421. {
  2422. ParentGroup.HasGroupChanged = true;
  2423. ParentGroup.QueueForUpdateCheck();
  2424. TimeStampTerse = (uint) Util.UnixTimeSinceEpoch();
  2425. UpdateFlag = UpdateRequired.TERSE;
  2426. // m_log.DebugFormat(
  2427. // "[SCENE OBJECT PART]: Scheduling terse update for {0}, {1} at {2}",
  2428. // UUID, Name, TimeStampTerse);
  2429. }
  2430. if (ParentGroup.Scene != null)
  2431. ParentGroup.Scene.EventManager.TriggerSceneObjectPartUpdated(this);
  2432. }
  2433. public void ScriptSetPhysicsStatus(bool UsePhysics)
  2434. {
  2435. ParentGroup.ScriptSetPhysicsStatus(UsePhysics);
  2436. }
  2437. /// <summary>
  2438. /// Set sculpt and mesh data, and tell the physics engine to process the change.
  2439. /// </summary>
  2440. /// <param name="texture">The mesh itself.</param>
  2441. public void SculptTextureCallback(AssetBase texture)
  2442. {
  2443. if (m_shape.SculptEntry)
  2444. {
  2445. // commented out for sculpt map caching test - null could mean a cached sculpt map has been found
  2446. //if (texture != null)
  2447. {
  2448. if (texture != null)
  2449. {
  2450. // m_log.DebugFormat(
  2451. // "[SCENE OBJECT PART]: Setting sculpt data for {0} on SculptTextureCallback()", Name);
  2452. m_shape.SculptData = texture.Data;
  2453. }
  2454. PhysicsActor pa = PhysActor;
  2455. if (pa != null)
  2456. {
  2457. // Update the physics actor with the new loaded sculpt data and set the taint signal.
  2458. pa.Shape = m_shape;
  2459. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2460. }
  2461. }
  2462. }
  2463. }
  2464. /// <summary>
  2465. /// Send a full update to the client for the given part
  2466. /// </summary>
  2467. /// <param name="remoteClient"></param>
  2468. protected internal void SendFullUpdate(IClientAPI remoteClient)
  2469. {
  2470. if (ParentGroup == null)
  2471. return;
  2472. // m_log.DebugFormat(
  2473. // "[SOG]: Sendinging part full update to {0} for {1} {2}", remoteClient.Name, part.Name, part.LocalId);
  2474. if (IsRoot)
  2475. {
  2476. if (ParentGroup.IsAttachment)
  2477. {
  2478. SendFullUpdateToClient(remoteClient, AttachedPos);
  2479. }
  2480. else
  2481. {
  2482. SendFullUpdateToClient(remoteClient, AbsolutePosition);
  2483. }
  2484. }
  2485. else
  2486. {
  2487. SendFullUpdateToClient(remoteClient);
  2488. }
  2489. }
  2490. /// <summary>
  2491. /// Send a full update for this part to all clients.
  2492. /// </summary>
  2493. public void SendFullUpdateToAllClients()
  2494. {
  2495. if (ParentGroup == null)
  2496. return;
  2497. ParentGroup.Scene.ForEachScenePresence(delegate(ScenePresence avatar)
  2498. {
  2499. SendFullUpdate(avatar.ControllingClient);
  2500. });
  2501. }
  2502. /// <summary>
  2503. /// Sends a full update to the client
  2504. /// </summary>
  2505. /// <param name="remoteClient"></param>
  2506. public void SendFullUpdateToClient(IClientAPI remoteClient)
  2507. {
  2508. SendFullUpdateToClient(remoteClient, OffsetPosition);
  2509. }
  2510. /// <summary>
  2511. /// Sends a full update to the client
  2512. /// </summary>
  2513. /// <param name="remoteClient"></param>
  2514. /// <param name="lPos"></param>
  2515. public void SendFullUpdateToClient(IClientAPI remoteClient, Vector3 lPos)
  2516. {
  2517. if (ParentGroup == null)
  2518. return;
  2519. // Suppress full updates during attachment editing
  2520. //
  2521. if (ParentGroup.IsSelected && ParentGroup.IsAttachment)
  2522. return;
  2523. if (ParentGroup.IsDeleted)
  2524. return;
  2525. if (ParentGroup.IsAttachment
  2526. && ParentGroup.AttachedAvatar != remoteClient.AgentId
  2527. && ParentGroup.HasPrivateAttachmentPoint)
  2528. return;
  2529. if (remoteClient.AgentId == OwnerID)
  2530. {
  2531. if ((Flags & PrimFlags.CreateSelected) != 0)
  2532. Flags &= ~PrimFlags.CreateSelected;
  2533. }
  2534. //bool isattachment = IsAttachment;
  2535. //if (LocalId != ParentGroup.RootPart.LocalId)
  2536. //isattachment = ParentGroup.RootPart.IsAttachment;
  2537. remoteClient.SendEntityUpdate(this, PrimUpdateFlags.FullUpdate);
  2538. ParentGroup.Scene.StatsReporter.AddObjectUpdates(1);
  2539. }
  2540. /// <summary>
  2541. /// Tell all the prims which have had updates scheduled
  2542. /// </summary>
  2543. public void SendScheduledUpdates()
  2544. {
  2545. const float ROTATION_TOLERANCE = 0.01f;
  2546. const float VELOCITY_TOLERANCE = 0.001f;
  2547. const float POSITION_TOLERANCE = 0.05f;
  2548. const int TIME_MS_TOLERANCE = 3000;
  2549. switch (UpdateFlag)
  2550. {
  2551. case UpdateRequired.TERSE:
  2552. {
  2553. ClearUpdateSchedule();
  2554. // Throw away duplicate or insignificant updates
  2555. if (!RotationOffset.ApproxEquals(m_lastRotation, ROTATION_TOLERANCE) ||
  2556. !Acceleration.Equals(m_lastAcceleration) ||
  2557. !Velocity.ApproxEquals(m_lastVelocity, VELOCITY_TOLERANCE) ||
  2558. Velocity.ApproxEquals(Vector3.Zero, VELOCITY_TOLERANCE) ||
  2559. !AngularVelocity.ApproxEquals(m_lastAngularVelocity, VELOCITY_TOLERANCE) ||
  2560. !OffsetPosition.ApproxEquals(m_lastPosition, POSITION_TOLERANCE) ||
  2561. Environment.TickCount - m_lastTerseSent > TIME_MS_TOLERANCE)
  2562. {
  2563. SendTerseUpdateToAllClients();
  2564. // Update the "last" values
  2565. m_lastPosition = OffsetPosition;
  2566. m_lastRotation = RotationOffset;
  2567. m_lastVelocity = Velocity;
  2568. m_lastAcceleration = Acceleration;
  2569. m_lastAngularVelocity = AngularVelocity;
  2570. m_lastTerseSent = Environment.TickCount;
  2571. }
  2572. break;
  2573. }
  2574. case UpdateRequired.FULL:
  2575. {
  2576. ClearUpdateSchedule();
  2577. SendFullUpdateToAllClients();
  2578. break;
  2579. }
  2580. }
  2581. }
  2582. /// <summary>
  2583. /// Trigger or play an attached sound in this part's inventory.
  2584. /// </summary>
  2585. /// <param name="sound"></param>
  2586. /// <param name="volume"></param>
  2587. /// <param name="triggered"></param>
  2588. /// <param name="flags"></param>
  2589. public void SendSound(string sound, double volume, bool triggered, byte flags, float radius, bool useMaster, bool isMaster)
  2590. {
  2591. if (volume > 1)
  2592. volume = 1;
  2593. if (volume < 0)
  2594. volume = 0;
  2595. UUID ownerID = OwnerID;
  2596. UUID objectID = ParentGroup.RootPart.UUID;
  2597. UUID parentID = ParentGroup.UUID;
  2598. UUID soundID = UUID.Zero;
  2599. Vector3 position = AbsolutePosition; // region local
  2600. ulong regionHandle = ParentGroup.Scene.RegionInfo.RegionHandle;
  2601. if (!UUID.TryParse(sound, out soundID))
  2602. {
  2603. // search sound file from inventory
  2604. lock (TaskInventory)
  2605. {
  2606. foreach (KeyValuePair<UUID, TaskInventoryItem> item in TaskInventory)
  2607. {
  2608. if (item.Value.Name == sound && item.Value.Type == (int)AssetType.Sound)
  2609. {
  2610. soundID = item.Value.ItemID;
  2611. break;
  2612. }
  2613. }
  2614. }
  2615. }
  2616. if (soundID == UUID.Zero)
  2617. return;
  2618. ISoundModule soundModule = ParentGroup.Scene.RequestModuleInterface<ISoundModule>();
  2619. if (soundModule != null)
  2620. {
  2621. if (useMaster)
  2622. {
  2623. if (isMaster)
  2624. {
  2625. if (triggered)
  2626. soundModule.TriggerSound(soundID, ownerID, objectID, parentID, volume, position, regionHandle, radius);
  2627. else
  2628. soundModule.PlayAttachedSound(soundID, ownerID, objectID, volume, position, flags, radius);
  2629. ParentGroup.PlaySoundMasterPrim = this;
  2630. ownerID = OwnerID;
  2631. objectID = ParentGroup.RootPart.UUID;
  2632. parentID = ParentGroup.UUID;
  2633. position = AbsolutePosition; // region local
  2634. regionHandle = ParentGroup.Scene.RegionInfo.RegionHandle;
  2635. if (triggered)
  2636. soundModule.TriggerSound(soundID, ownerID, objectID, parentID, volume, position, regionHandle, radius);
  2637. else
  2638. soundModule.PlayAttachedSound(soundID, ownerID, objectID, volume, position, flags, radius);
  2639. foreach (SceneObjectPart prim in ParentGroup.PlaySoundSlavePrims)
  2640. {
  2641. ownerID = prim.OwnerID;
  2642. objectID = prim.ParentGroup.RootPart.UUID;
  2643. parentID = prim.ParentGroup.UUID;
  2644. position = prim.AbsolutePosition; // region local
  2645. regionHandle = prim.ParentGroup.Scene.RegionInfo.RegionHandle;
  2646. if (triggered)
  2647. soundModule.TriggerSound(soundID, ownerID, objectID, parentID, volume, position, regionHandle, radius);
  2648. else
  2649. soundModule.PlayAttachedSound(soundID, ownerID, objectID, volume, position, flags, radius);
  2650. }
  2651. ParentGroup.PlaySoundSlavePrims.Clear();
  2652. ParentGroup.PlaySoundMasterPrim = null;
  2653. }
  2654. else
  2655. {
  2656. ParentGroup.PlaySoundSlavePrims.Add(this);
  2657. }
  2658. }
  2659. else
  2660. {
  2661. if (triggered)
  2662. soundModule.TriggerSound(soundID, ownerID, objectID, parentID, volume, position, regionHandle, radius);
  2663. else
  2664. soundModule.PlayAttachedSound(soundID, ownerID, objectID, volume, position, flags, radius);
  2665. }
  2666. }
  2667. }
  2668. /// <summary>
  2669. /// Send a terse update to all clients
  2670. /// </summary>
  2671. public void SendTerseUpdateToAllClients()
  2672. {
  2673. ParentGroup.Scene.ForEachClient(delegate(IClientAPI client)
  2674. {
  2675. SendTerseUpdateToClient(client);
  2676. });
  2677. }
  2678. public void SetAxisRotation(int axis, int rotate)
  2679. {
  2680. ParentGroup.SetAxisRotation(axis, rotate);
  2681. //Cannot use ScriptBaseClass constants as no referance to it currently.
  2682. if (axis == 2)//STATUS_ROTATE_X
  2683. STATUS_ROTATE_X = rotate;
  2684. if (axis == 4)//STATUS_ROTATE_Y
  2685. STATUS_ROTATE_Y = rotate;
  2686. if (axis == 8)//STATUS_ROTATE_Z
  2687. STATUS_ROTATE_Z = rotate;
  2688. }
  2689. public void SetBuoyancy(float fvalue)
  2690. {
  2691. PhysicsActor pa = PhysActor;
  2692. if (pa != null)
  2693. pa.Buoyancy = fvalue;
  2694. }
  2695. public void SetDieAtEdge(bool p)
  2696. {
  2697. if (ParentGroup.IsDeleted)
  2698. return;
  2699. ParentGroup.RootPart.DIE_AT_EDGE = p;
  2700. }
  2701. public void SetFloatOnWater(int floatYN)
  2702. {
  2703. PhysicsActor pa = PhysActor;
  2704. if (pa != null)
  2705. pa.FloatOnWater = floatYN == 1;
  2706. }
  2707. public void SetForce(Vector3 force)
  2708. {
  2709. PhysicsActor pa = PhysActor;
  2710. if (pa != null)
  2711. pa.Force = force;
  2712. }
  2713. public void SetVehicleType(int type)
  2714. {
  2715. PhysicsActor pa = PhysActor;
  2716. if (pa != null)
  2717. pa.VehicleType = type;
  2718. }
  2719. public void SetVehicleFloatParam(int param, float value)
  2720. {
  2721. PhysicsActor pa = PhysActor;
  2722. if (pa != null)
  2723. pa.VehicleFloatParam(param, value);
  2724. }
  2725. public void SetVehicleVectorParam(int param, Vector3 value)
  2726. {
  2727. PhysicsActor pa = PhysActor;
  2728. if (pa != null)
  2729. pa.VehicleVectorParam(param, value);
  2730. }
  2731. public void SetVehicleRotationParam(int param, Quaternion rotation)
  2732. {
  2733. PhysicsActor pa = PhysActor;
  2734. if (pa != null)
  2735. pa.VehicleRotationParam(param, rotation);
  2736. }
  2737. /// <summary>
  2738. /// Set the color of prim faces
  2739. /// </summary>
  2740. /// <param name="color"></param>
  2741. /// <param name="face"></param>
  2742. public void SetFaceColor(Vector3 color, int face)
  2743. {
  2744. // The only way to get a deep copy/ If we don't do this, we can
  2745. // mever detect color changes further down.
  2746. Byte[] buf = Shape.Textures.GetBytes();
  2747. Primitive.TextureEntry tex = new Primitive.TextureEntry(buf, 0, buf.Length);
  2748. Color4 texcolor;
  2749. if (face >= 0 && face < GetNumberOfSides())
  2750. {
  2751. texcolor = tex.CreateFace((uint)face).RGBA;
  2752. texcolor.R = Util.Clip((float)color.X, 0.0f, 1.0f);
  2753. texcolor.G = Util.Clip((float)color.Y, 0.0f, 1.0f);
  2754. texcolor.B = Util.Clip((float)color.Z, 0.0f, 1.0f);
  2755. tex.FaceTextures[face].RGBA = texcolor;
  2756. UpdateTextureEntry(tex.GetBytes());
  2757. return;
  2758. }
  2759. else if (face == ALL_SIDES)
  2760. {
  2761. for (uint i = 0; i < GetNumberOfSides(); i++)
  2762. {
  2763. if (tex.FaceTextures[i] != null)
  2764. {
  2765. texcolor = tex.FaceTextures[i].RGBA;
  2766. texcolor.R = Util.Clip((float)color.X, 0.0f, 1.0f);
  2767. texcolor.G = Util.Clip((float)color.Y, 0.0f, 1.0f);
  2768. texcolor.B = Util.Clip((float)color.Z, 0.0f, 1.0f);
  2769. tex.FaceTextures[i].RGBA = texcolor;
  2770. }
  2771. texcolor = tex.DefaultTexture.RGBA;
  2772. texcolor.R = Util.Clip((float)color.X, 0.0f, 1.0f);
  2773. texcolor.G = Util.Clip((float)color.Y, 0.0f, 1.0f);
  2774. texcolor.B = Util.Clip((float)color.Z, 0.0f, 1.0f);
  2775. tex.DefaultTexture.RGBA = texcolor;
  2776. }
  2777. UpdateTextureEntry(tex.GetBytes());
  2778. return;
  2779. }
  2780. }
  2781. /// <summary>
  2782. /// Get the number of sides that this part has.
  2783. /// </summary>
  2784. /// <returns></returns>
  2785. public int GetNumberOfSides()
  2786. {
  2787. int ret = 0;
  2788. bool hasCut;
  2789. bool hasHollow;
  2790. bool hasDimple;
  2791. bool hasProfileCut;
  2792. PrimType primType = GetPrimType();
  2793. HasCutHollowDimpleProfileCut(primType, Shape, out hasCut, out hasHollow, out hasDimple, out hasProfileCut);
  2794. switch (primType)
  2795. {
  2796. case PrimType.BOX:
  2797. ret = 6;
  2798. if (hasCut) ret += 2;
  2799. if (hasHollow) ret += 1;
  2800. break;
  2801. case PrimType.CYLINDER:
  2802. ret = 3;
  2803. if (hasCut) ret += 2;
  2804. if (hasHollow) ret += 1;
  2805. break;
  2806. case PrimType.PRISM:
  2807. ret = 5;
  2808. if (hasCut) ret += 2;
  2809. if (hasHollow) ret += 1;
  2810. break;
  2811. case PrimType.SPHERE:
  2812. ret = 1;
  2813. if (hasCut) ret += 2;
  2814. if (hasDimple) ret += 2;
  2815. if (hasHollow) ret += 1;
  2816. break;
  2817. case PrimType.TORUS:
  2818. ret = 1;
  2819. if (hasCut) ret += 2;
  2820. if (hasProfileCut) ret += 2;
  2821. if (hasHollow) ret += 1;
  2822. break;
  2823. case PrimType.TUBE:
  2824. ret = 4;
  2825. if (hasCut) ret += 2;
  2826. if (hasProfileCut) ret += 2;
  2827. if (hasHollow) ret += 1;
  2828. break;
  2829. case PrimType.RING:
  2830. ret = 3;
  2831. if (hasCut) ret += 2;
  2832. if (hasProfileCut) ret += 2;
  2833. if (hasHollow) ret += 1;
  2834. break;
  2835. case PrimType.SCULPT:
  2836. // Special mesh handling
  2837. if (Shape.SculptType == (byte)SculptType.Mesh)
  2838. ret = 8; // if it's a mesh then max 8 faces
  2839. else
  2840. ret = 1; // if it's a sculpt then max 1 face
  2841. break;
  2842. }
  2843. return ret;
  2844. }
  2845. /// <summary>
  2846. /// Tell us what type this prim is
  2847. /// </summary>
  2848. /// <param name="primShape"></param>
  2849. /// <returns></returns>
  2850. public PrimType GetPrimType()
  2851. {
  2852. if (Shape.SculptEntry)
  2853. return PrimType.SCULPT;
  2854. if ((Shape.ProfileCurve & 0x07) == (byte)ProfileShape.Square)
  2855. {
  2856. if (Shape.PathCurve == (byte)Extrusion.Straight)
  2857. return PrimType.BOX;
  2858. else if (Shape.PathCurve == (byte)Extrusion.Curve1)
  2859. return PrimType.TUBE;
  2860. }
  2861. else if ((Shape.ProfileCurve & 0x07) == (byte)ProfileShape.Circle)
  2862. {
  2863. if (Shape.PathCurve == (byte)Extrusion.Straight)
  2864. return PrimType.CYLINDER;
  2865. // ProfileCurve seems to combine hole shape and profile curve so we need to only compare against the lower 3 bits
  2866. else if (Shape.PathCurve == (byte)Extrusion.Curve1)
  2867. return PrimType.TORUS;
  2868. }
  2869. else if ((Shape.ProfileCurve & 0x07) == (byte)ProfileShape.HalfCircle)
  2870. {
  2871. if (Shape.PathCurve == (byte)Extrusion.Curve1 || Shape.PathCurve == (byte)Extrusion.Curve2)
  2872. return PrimType.SPHERE;
  2873. }
  2874. else if ((Shape.ProfileCurve & 0x07) == (byte)ProfileShape.EquilateralTriangle)
  2875. {
  2876. if (Shape.PathCurve == (byte)Extrusion.Straight)
  2877. return PrimType.PRISM;
  2878. else if (Shape.PathCurve == (byte)Extrusion.Curve1)
  2879. return PrimType.RING;
  2880. }
  2881. return PrimType.BOX;
  2882. }
  2883. /// <summary>
  2884. /// Tell us if this object has cut, hollow, dimple, and other factors affecting the number of faces
  2885. /// </summary>
  2886. /// <param name="primType"></param>
  2887. /// <param name="shape"></param>
  2888. /// <param name="hasCut"></param>
  2889. /// <param name="hasHollow"></param>
  2890. /// <param name="hasDimple"></param>
  2891. /// <param name="hasProfileCut"></param>
  2892. protected static void HasCutHollowDimpleProfileCut(PrimType primType, PrimitiveBaseShape shape, out bool hasCut, out bool hasHollow,
  2893. out bool hasDimple, out bool hasProfileCut)
  2894. {
  2895. if (primType == PrimType.BOX
  2896. ||
  2897. primType == PrimType.CYLINDER
  2898. ||
  2899. primType == PrimType.PRISM)
  2900. hasCut = (shape.ProfileBegin > 0) || (shape.ProfileEnd > 0);
  2901. else
  2902. hasCut = (shape.PathBegin > 0) || (shape.PathEnd > 0);
  2903. hasHollow = shape.ProfileHollow > 0;
  2904. hasDimple = (shape.ProfileBegin > 0) || (shape.ProfileEnd > 0); // taken from llSetPrimitiveParms
  2905. hasProfileCut = hasDimple; // is it the same thing?
  2906. }
  2907. public void SetVehicleFlags(int param, bool remove)
  2908. {
  2909. PhysicsActor pa = PhysActor;
  2910. if (pa != null)
  2911. pa.VehicleFlags(param, remove);
  2912. }
  2913. public void SetGroup(UUID groupID, IClientAPI client)
  2914. {
  2915. // Scene.AddNewPrims() calls with client == null so can't use this.
  2916. // m_log.DebugFormat(
  2917. // "[SCENE OBJECT PART]: Setting group for {0} to {1} for {2}",
  2918. // Name, groupID, OwnerID);
  2919. GroupID = groupID;
  2920. if (client != null)
  2921. SendPropertiesToClient(client);
  2922. UpdateFlag = UpdateRequired.FULL;
  2923. }
  2924. /// <summary>
  2925. /// Set the parent group of this prim.
  2926. /// </summary>
  2927. public void SetParent(SceneObjectGroup parent)
  2928. {
  2929. ParentGroup = parent;
  2930. }
  2931. // Use this for attachments! LocalID should be avatar's localid
  2932. public void SetParentLocalId(uint localID)
  2933. {
  2934. ParentID = localID;
  2935. }
  2936. public void SetPhysicsAxisRotation()
  2937. {
  2938. PhysicsActor pa = PhysActor;
  2939. if (pa != null)
  2940. {
  2941. pa.LockAngularMotion(RotationAxis);
  2942. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2943. }
  2944. }
  2945. /// <summary>
  2946. /// Set the events that this part will pass on to listeners.
  2947. /// </summary>
  2948. /// <param name="scriptid"></param>
  2949. /// <param name="events"></param>
  2950. public void SetScriptEvents(UUID scriptid, int events)
  2951. {
  2952. // scriptEvents oldparts;
  2953. lock (m_scriptEvents)
  2954. {
  2955. if (m_scriptEvents.ContainsKey(scriptid))
  2956. {
  2957. // oldparts = m_scriptEvents[scriptid];
  2958. // remove values from aggregated script events
  2959. if (m_scriptEvents[scriptid] == (scriptEvents) events)
  2960. return;
  2961. m_scriptEvents[scriptid] = (scriptEvents) events;
  2962. }
  2963. else
  2964. {
  2965. m_scriptEvents.Add(scriptid, (scriptEvents) events);
  2966. }
  2967. }
  2968. aggregateScriptEvents();
  2969. }
  2970. /// <summary>
  2971. /// Set the text displayed for this part.
  2972. /// </summary>
  2973. /// <param name="text"></param>
  2974. public void SetText(string text)
  2975. {
  2976. Text = text;
  2977. if (ParentGroup != null)
  2978. {
  2979. ParentGroup.HasGroupChanged = true;
  2980. ScheduleFullUpdate();
  2981. }
  2982. }
  2983. /// <summary>
  2984. /// Set the text displayed for this part.
  2985. /// </summary>
  2986. /// <param name="text"></param>
  2987. /// <param name="color"></param>
  2988. /// <param name="alpha"></param>
  2989. public void SetText(string text, Vector3 color, double alpha)
  2990. {
  2991. Color = Color.FromArgb((int) (alpha*0xff),
  2992. (int) (color.X*0xff),
  2993. (int) (color.Y*0xff),
  2994. (int) (color.Z*0xff));
  2995. SetText(text);
  2996. }
  2997. public void StopMoveToTarget()
  2998. {
  2999. ParentGroup.stopMoveToTarget();
  3000. ParentGroup.ScheduleGroupForTerseUpdate();
  3001. //ParentGroup.ScheduleGroupForFullUpdate();
  3002. }
  3003. public void StoreUndoState()
  3004. {
  3005. StoreUndoState(false);
  3006. }
  3007. public void StoreUndoState(bool forGroup)
  3008. {
  3009. if (!Undoing)
  3010. {
  3011. if (!IgnoreUndoUpdate)
  3012. {
  3013. if (ParentGroup != null)
  3014. {
  3015. lock (m_undo)
  3016. {
  3017. if (m_undo.Count > 0)
  3018. {
  3019. UndoState last = m_undo.Peek();
  3020. if (last != null)
  3021. {
  3022. // TODO: May need to fix for group comparison
  3023. if (last.Compare(this))
  3024. {
  3025. // m_log.DebugFormat(
  3026. // "[SCENE OBJECT PART]: Not storing undo for {0} {1} since current state is same as last undo state, initial stack size {2}",
  3027. // Name, LocalId, m_undo.Count);
  3028. return;
  3029. }
  3030. }
  3031. }
  3032. // m_log.DebugFormat(
  3033. // "[SCENE OBJECT PART]: Storing undo state for {0} {1}, forGroup {2}, initial stack size {3}",
  3034. // Name, LocalId, forGroup, m_undo.Count);
  3035. if (ParentGroup.GetSceneMaxUndo() > 0)
  3036. {
  3037. UndoState nUndo = new UndoState(this, forGroup);
  3038. m_undo.Push(nUndo);
  3039. if (m_redo.Count > 0)
  3040. m_redo.Clear();
  3041. // m_log.DebugFormat(
  3042. // "[SCENE OBJECT PART]: Stored undo state for {0} {1}, forGroup {2}, stack size now {3}",
  3043. // Name, LocalId, forGroup, m_undo.Count);
  3044. }
  3045. }
  3046. }
  3047. }
  3048. // else
  3049. // {
  3050. // m_log.DebugFormat("[SCENE OBJECT PART]: Ignoring undo store for {0} {1}", Name, LocalId);
  3051. // }
  3052. }
  3053. // else
  3054. // {
  3055. // m_log.DebugFormat(
  3056. // "[SCENE OBJECT PART]: Ignoring undo store for {0} {1} since already undoing", Name, LocalId);
  3057. // }
  3058. }
  3059. /// <summary>
  3060. /// Return number of undos on the stack. Here temporarily pending a refactor.
  3061. /// </summary>
  3062. public int UndoCount
  3063. {
  3064. get
  3065. {
  3066. lock (m_undo)
  3067. return m_undo.Count;
  3068. }
  3069. }
  3070. public void Undo()
  3071. {
  3072. lock (m_undo)
  3073. {
  3074. // m_log.DebugFormat(
  3075. // "[SCENE OBJECT PART]: Handling undo request for {0} {1}, stack size {2}",
  3076. // Name, LocalId, m_undo.Count);
  3077. if (m_undo.Count > 0)
  3078. {
  3079. UndoState goback = m_undo.Pop();
  3080. if (goback != null)
  3081. {
  3082. UndoState nUndo = null;
  3083. if (ParentGroup.GetSceneMaxUndo() > 0)
  3084. {
  3085. nUndo = new UndoState(this, goback.ForGroup);
  3086. }
  3087. goback.PlaybackState(this);
  3088. if (nUndo != null)
  3089. m_redo.Push(nUndo);
  3090. }
  3091. }
  3092. // m_log.DebugFormat(
  3093. // "[SCENE OBJECT PART]: Handled undo request for {0} {1}, stack size now {2}",
  3094. // Name, LocalId, m_undo.Count);
  3095. }
  3096. }
  3097. public void Redo()
  3098. {
  3099. lock (m_undo)
  3100. {
  3101. // m_log.DebugFormat(
  3102. // "[SCENE OBJECT PART]: Handling redo request for {0} {1}, stack size {2}",
  3103. // Name, LocalId, m_redo.Count);
  3104. if (m_redo.Count > 0)
  3105. {
  3106. UndoState gofwd = m_redo.Pop();
  3107. if (gofwd != null)
  3108. {
  3109. if (ParentGroup.GetSceneMaxUndo() > 0)
  3110. {
  3111. UndoState nUndo = new UndoState(this, gofwd.ForGroup);
  3112. m_undo.Push(nUndo);
  3113. }
  3114. gofwd.PlayfwdState(this);
  3115. }
  3116. // m_log.DebugFormat(
  3117. // "[SCENE OBJECT PART]: Handled redo request for {0} {1}, stack size now {2}",
  3118. // Name, LocalId, m_redo.Count);
  3119. }
  3120. }
  3121. }
  3122. public void ClearUndoState()
  3123. {
  3124. // m_log.DebugFormat("[SCENE OBJECT PART]: Clearing undo and redo stacks in {0} {1}", Name, LocalId);
  3125. lock (m_undo)
  3126. {
  3127. m_undo.Clear();
  3128. m_redo.Clear();
  3129. }
  3130. }
  3131. public EntityIntersection TestIntersection(Ray iray, Quaternion parentrot)
  3132. {
  3133. // In this case we're using a sphere with a radius of the largest dimension of the prim
  3134. // TODO: Change to take shape into account
  3135. EntityIntersection result = new EntityIntersection();
  3136. Vector3 vAbsolutePosition = AbsolutePosition;
  3137. Vector3 vScale = Scale;
  3138. Vector3 rOrigin = iray.Origin;
  3139. Vector3 rDirection = iray.Direction;
  3140. //rDirection = rDirection.Normalize();
  3141. // Buidling the first part of the Quadratic equation
  3142. Vector3 r2ndDirection = rDirection*rDirection;
  3143. float itestPart1 = r2ndDirection.X + r2ndDirection.Y + r2ndDirection.Z;
  3144. // Buidling the second part of the Quadratic equation
  3145. Vector3 tmVal2 = rOrigin - vAbsolutePosition;
  3146. Vector3 r2Direction = rDirection*2.0f;
  3147. Vector3 tmVal3 = r2Direction*tmVal2;
  3148. float itestPart2 = tmVal3.X + tmVal3.Y + tmVal3.Z;
  3149. // Buidling the third part of the Quadratic equation
  3150. Vector3 tmVal4 = rOrigin*rOrigin;
  3151. Vector3 tmVal5 = vAbsolutePosition*vAbsolutePosition;
  3152. Vector3 tmVal6 = vAbsolutePosition*rOrigin;
  3153. // Set Radius to the largest dimension of the prim
  3154. float radius = 0f;
  3155. if (vScale.X > radius)
  3156. radius = vScale.X;
  3157. if (vScale.Y > radius)
  3158. radius = vScale.Y;
  3159. if (vScale.Z > radius)
  3160. radius = vScale.Z;
  3161. // the second part of this is the default prim size
  3162. // once we factor in the aabb of the prim we're adding we can
  3163. // change this to;
  3164. // radius = (radius / 2) - 0.01f;
  3165. //
  3166. radius = (radius / 2) + (0.5f / 2) - 0.1f;
  3167. //radius = radius;
  3168. float itestPart3 = tmVal4.X + tmVal4.Y + tmVal4.Z + tmVal5.X + tmVal5.Y + tmVal5.Z -
  3169. (2.0f*(tmVal6.X + tmVal6.Y + tmVal6.Z + (radius*radius)));
  3170. // Yuk Quadradrics.. Solve first
  3171. float rootsqr = (itestPart2*itestPart2) - (4.0f*itestPart1*itestPart3);
  3172. if (rootsqr < 0.0f)
  3173. {
  3174. // No intersection
  3175. return result;
  3176. }
  3177. float root = ((-itestPart2) - (float) Math.Sqrt((double) rootsqr))/(itestPart1*2.0f);
  3178. if (root < 0.0f)
  3179. {
  3180. // perform second quadratic root solution
  3181. root = ((-itestPart2) + (float) Math.Sqrt((double) rootsqr))/(itestPart1*2.0f);
  3182. // is there any intersection?
  3183. if (root < 0.0f)
  3184. {
  3185. // nope, no intersection
  3186. return result;
  3187. }
  3188. }
  3189. // We got an intersection. putting together an EntityIntersection object with the
  3190. // intersection information
  3191. Vector3 ipoint =
  3192. new Vector3(iray.Origin.X + (iray.Direction.X*root), iray.Origin.Y + (iray.Direction.Y*root),
  3193. iray.Origin.Z + (iray.Direction.Z*root));
  3194. result.HitTF = true;
  3195. result.ipoint = ipoint;
  3196. // Normal is calculated by the difference and then normalizing the result
  3197. Vector3 normalpart = ipoint - vAbsolutePosition;
  3198. result.normal = normalpart / normalpart.Length();
  3199. // It's funny how the Vector3 object has a Distance function, but the Axiom.Math object doesn't.
  3200. // I can write a function to do it.. but I like the fact that this one is Static.
  3201. Vector3 distanceConvert1 = new Vector3(iray.Origin.X, iray.Origin.Y, iray.Origin.Z);
  3202. Vector3 distanceConvert2 = new Vector3(ipoint.X, ipoint.Y, ipoint.Z);
  3203. float distance = (float) Util.GetDistanceTo(distanceConvert1, distanceConvert2);
  3204. result.distance = distance;
  3205. return result;
  3206. }
  3207. public EntityIntersection TestIntersectionOBB(Ray iray, Quaternion parentrot, bool frontFacesOnly, bool faceCenters)
  3208. {
  3209. // In this case we're using a rectangular prism, which has 6 faces and therefore 6 planes
  3210. // This breaks down into the ray---> plane equation.
  3211. // TODO: Change to take shape into account
  3212. Vector3[] vertexes = new Vector3[8];
  3213. // float[] distance = new float[6];
  3214. Vector3[] FaceA = new Vector3[6]; // vertex A for Facei
  3215. Vector3[] FaceB = new Vector3[6]; // vertex B for Facei
  3216. Vector3[] FaceC = new Vector3[6]; // vertex C for Facei
  3217. Vector3[] FaceD = new Vector3[6]; // vertex D for Facei
  3218. Vector3[] normals = new Vector3[6]; // Normal for Facei
  3219. Vector3[] AAfacenormals = new Vector3[6]; // Axis Aligned face normals
  3220. AAfacenormals[0] = new Vector3(1, 0, 0);
  3221. AAfacenormals[1] = new Vector3(0, 1, 0);
  3222. AAfacenormals[2] = new Vector3(-1, 0, 0);
  3223. AAfacenormals[3] = new Vector3(0, -1, 0);
  3224. AAfacenormals[4] = new Vector3(0, 0, 1);
  3225. AAfacenormals[5] = new Vector3(0, 0, -1);
  3226. Vector3 AmBa = new Vector3(0, 0, 0); // Vertex A - Vertex B
  3227. Vector3 AmBb = new Vector3(0, 0, 0); // Vertex B - Vertex C
  3228. Vector3 cross = new Vector3();
  3229. Vector3 pos = GetWorldPosition();
  3230. Quaternion rot = GetWorldRotation();
  3231. // Variables prefixed with AX are Axiom.Math copies of the LL variety.
  3232. Quaternion AXrot = rot;
  3233. AXrot.Normalize();
  3234. Vector3 AXpos = pos;
  3235. // tScale is the offset to derive the vertex based on the scale.
  3236. // it's different for each vertex because we've got to rotate it
  3237. // to get the world position of the vertex to produce the Oriented Bounding Box
  3238. Vector3 tScale = Vector3.Zero;
  3239. Vector3 AXscale = new Vector3(m_shape.Scale.X * 0.5f, m_shape.Scale.Y * 0.5f, m_shape.Scale.Z * 0.5f);
  3240. //Vector3 pScale = (AXscale) - (AXrot.Inverse() * (AXscale));
  3241. //Vector3 nScale = (AXscale * -1) - (AXrot.Inverse() * (AXscale * -1));
  3242. // rScale is the rotated offset to find a vertex based on the scale and the world rotation.
  3243. Vector3 rScale = new Vector3();
  3244. // Get Vertexes for Faces Stick them into ABCD for each Face
  3245. // Form: Face<vertex>[face] that corresponds to the below diagram
  3246. #region ABCD Face Vertex Map Comment Diagram
  3247. // A _________ B
  3248. // | |
  3249. // | 4 top |
  3250. // |_________|
  3251. // C D
  3252. // A _________ B
  3253. // | Back |
  3254. // | 3 |
  3255. // |_________|
  3256. // C D
  3257. // A _________ B B _________ A
  3258. // | Left | | Right |
  3259. // | 0 | | 2 |
  3260. // |_________| |_________|
  3261. // C D D C
  3262. // A _________ B
  3263. // | Front |
  3264. // | 1 |
  3265. // |_________|
  3266. // C D
  3267. // C _________ D
  3268. // | |
  3269. // | 5 bot |
  3270. // |_________|
  3271. // A B
  3272. #endregion
  3273. #region Plane Decomposition of Oriented Bounding Box
  3274. tScale = new Vector3(AXscale.X, -AXscale.Y, AXscale.Z);
  3275. rScale = tScale * AXrot;
  3276. vertexes[0] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3277. // vertexes[0].X = pos.X + vertexes[0].X;
  3278. //vertexes[0].Y = pos.Y + vertexes[0].Y;
  3279. //vertexes[0].Z = pos.Z + vertexes[0].Z;
  3280. FaceA[0] = vertexes[0];
  3281. FaceB[3] = vertexes[0];
  3282. FaceA[4] = vertexes[0];
  3283. tScale = AXscale;
  3284. rScale = tScale * AXrot;
  3285. vertexes[1] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3286. // vertexes[1].X = pos.X + vertexes[1].X;
  3287. // vertexes[1].Y = pos.Y + vertexes[1].Y;
  3288. //vertexes[1].Z = pos.Z + vertexes[1].Z;
  3289. FaceB[0] = vertexes[1];
  3290. FaceA[1] = vertexes[1];
  3291. FaceC[4] = vertexes[1];
  3292. tScale = new Vector3(AXscale.X, -AXscale.Y, -AXscale.Z);
  3293. rScale = tScale * AXrot;
  3294. vertexes[2] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3295. //vertexes[2].X = pos.X + vertexes[2].X;
  3296. //vertexes[2].Y = pos.Y + vertexes[2].Y;
  3297. //vertexes[2].Z = pos.Z + vertexes[2].Z;
  3298. FaceC[0] = vertexes[2];
  3299. FaceD[3] = vertexes[2];
  3300. FaceC[5] = vertexes[2];
  3301. tScale = new Vector3(AXscale.X, AXscale.Y, -AXscale.Z);
  3302. rScale = tScale * AXrot;
  3303. vertexes[3] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3304. //vertexes[3].X = pos.X + vertexes[3].X;
  3305. // vertexes[3].Y = pos.Y + vertexes[3].Y;
  3306. // vertexes[3].Z = pos.Z + vertexes[3].Z;
  3307. FaceD[0] = vertexes[3];
  3308. FaceC[1] = vertexes[3];
  3309. FaceA[5] = vertexes[3];
  3310. tScale = new Vector3(-AXscale.X, AXscale.Y, AXscale.Z);
  3311. rScale = tScale * AXrot;
  3312. vertexes[4] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3313. // vertexes[4].X = pos.X + vertexes[4].X;
  3314. // vertexes[4].Y = pos.Y + vertexes[4].Y;
  3315. // vertexes[4].Z = pos.Z + vertexes[4].Z;
  3316. FaceB[1] = vertexes[4];
  3317. FaceA[2] = vertexes[4];
  3318. FaceD[4] = vertexes[4];
  3319. tScale = new Vector3(-AXscale.X, AXscale.Y, -AXscale.Z);
  3320. rScale = tScale * AXrot;
  3321. vertexes[5] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3322. // vertexes[5].X = pos.X + vertexes[5].X;
  3323. // vertexes[5].Y = pos.Y + vertexes[5].Y;
  3324. // vertexes[5].Z = pos.Z + vertexes[5].Z;
  3325. FaceD[1] = vertexes[5];
  3326. FaceC[2] = vertexes[5];
  3327. FaceB[5] = vertexes[5];
  3328. tScale = new Vector3(-AXscale.X, -AXscale.Y, AXscale.Z);
  3329. rScale = tScale * AXrot;
  3330. vertexes[6] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3331. // vertexes[6].X = pos.X + vertexes[6].X;
  3332. // vertexes[6].Y = pos.Y + vertexes[6].Y;
  3333. // vertexes[6].Z = pos.Z + vertexes[6].Z;
  3334. FaceB[2] = vertexes[6];
  3335. FaceA[3] = vertexes[6];
  3336. FaceB[4] = vertexes[6];
  3337. tScale = new Vector3(-AXscale.X, -AXscale.Y, -AXscale.Z);
  3338. rScale = tScale * AXrot;
  3339. vertexes[7] = (new Vector3((pos.X + rScale.X), (pos.Y + rScale.Y), (pos.Z + rScale.Z)));
  3340. // vertexes[7].X = pos.X + vertexes[7].X;
  3341. // vertexes[7].Y = pos.Y + vertexes[7].Y;
  3342. // vertexes[7].Z = pos.Z + vertexes[7].Z;
  3343. FaceD[2] = vertexes[7];
  3344. FaceC[3] = vertexes[7];
  3345. FaceD[5] = vertexes[7];
  3346. #endregion
  3347. // Get our plane normals
  3348. for (int i = 0; i < 6; i++)
  3349. {
  3350. //m_log.Info("[FACECALCULATION]: FaceA[" + i + "]=" + FaceA[i] + " FaceB[" + i + "]=" + FaceB[i] + " FaceC[" + i + "]=" + FaceC[i] + " FaceD[" + i + "]=" + FaceD[i]);
  3351. // Our Plane direction
  3352. AmBa = FaceA[i] - FaceB[i];
  3353. AmBb = FaceB[i] - FaceC[i];
  3354. cross = Vector3.Cross(AmBb, AmBa);
  3355. // normalize the cross product to get the normal.
  3356. normals[i] = cross / cross.Length();
  3357. //m_log.Info("[NORMALS]: normals[ " + i + "]" + normals[i].ToString());
  3358. //distance[i] = (normals[i].X * AmBa.X + normals[i].Y * AmBa.Y + normals[i].Z * AmBa.Z) * -1;
  3359. }
  3360. EntityIntersection result = new EntityIntersection();
  3361. result.distance = 1024;
  3362. float c = 0;
  3363. float a = 0;
  3364. float d = 0;
  3365. Vector3 q = new Vector3();
  3366. #region OBB Version 2 Experiment
  3367. //float fmin = 999999;
  3368. //float fmax = -999999;
  3369. //float s = 0;
  3370. //for (int i=0;i<6;i++)
  3371. //{
  3372. //s = iray.Direction.Dot(normals[i]);
  3373. //d = normals[i].Dot(FaceB[i]);
  3374. //if (s == 0)
  3375. //{
  3376. //if (iray.Origin.Dot(normals[i]) > d)
  3377. //{
  3378. //return result;
  3379. //}
  3380. // else
  3381. //{
  3382. //continue;
  3383. //}
  3384. //}
  3385. //a = (d - iray.Origin.Dot(normals[i])) / s;
  3386. //if (iray.Direction.Dot(normals[i]) < 0)
  3387. //{
  3388. //if (a > fmax)
  3389. //{
  3390. //if (a > fmin)
  3391. //{
  3392. //return result;
  3393. //}
  3394. //fmax = a;
  3395. //}
  3396. //}
  3397. //else
  3398. //{
  3399. //if (a < fmin)
  3400. //{
  3401. //if (a < 0 || a < fmax)
  3402. //{
  3403. //return result;
  3404. //}
  3405. //fmin = a;
  3406. //}
  3407. //}
  3408. //}
  3409. //if (fmax > 0)
  3410. // a= fmax;
  3411. //else
  3412. // a=fmin;
  3413. //q = iray.Origin + a * iray.Direction;
  3414. #endregion
  3415. // Loop over faces (6 of them)
  3416. for (int i = 0; i < 6; i++)
  3417. {
  3418. AmBa = FaceA[i] - FaceB[i];
  3419. AmBb = FaceB[i] - FaceC[i];
  3420. d = Vector3.Dot(normals[i], FaceB[i]);
  3421. //if (faceCenters)
  3422. //{
  3423. // c = normals[i].Dot(normals[i]);
  3424. //}
  3425. //else
  3426. //{
  3427. c = Vector3.Dot(iray.Direction, normals[i]);
  3428. //}
  3429. if (c == 0)
  3430. continue;
  3431. a = (d - Vector3.Dot(iray.Origin, normals[i])) / c;
  3432. if (a < 0)
  3433. continue;
  3434. // If the normal is pointing outside the object
  3435. if (Vector3.Dot(iray.Direction, normals[i]) < 0 || !frontFacesOnly)
  3436. {
  3437. //if (faceCenters)
  3438. //{ //(FaceA[i] + FaceB[i] + FaceC[1] + FaceD[i]) / 4f;
  3439. // q = iray.Origin + a * normals[i];
  3440. //}
  3441. //else
  3442. //{
  3443. q = iray.Origin + iray.Direction * a;
  3444. //}
  3445. float distance2 = (float)GetDistanceTo(q, AXpos);
  3446. // Is this the closest hit to the object's origin?
  3447. //if (faceCenters)
  3448. //{
  3449. // distance2 = (float)GetDistanceTo(q, iray.Origin);
  3450. //}
  3451. if (distance2 < result.distance)
  3452. {
  3453. result.distance = distance2;
  3454. result.HitTF = true;
  3455. result.ipoint = q;
  3456. //m_log.Info("[FACE]:" + i.ToString());
  3457. //m_log.Info("[POINT]: " + q.ToString());
  3458. //m_log.Info("[DIST]: " + distance2.ToString());
  3459. if (faceCenters)
  3460. {
  3461. result.normal = AAfacenormals[i] * AXrot;
  3462. Vector3 scaleComponent = AAfacenormals[i];
  3463. float ScaleOffset = 0.5f;
  3464. if (scaleComponent.X != 0) ScaleOffset = AXscale.X;
  3465. if (scaleComponent.Y != 0) ScaleOffset = AXscale.Y;
  3466. if (scaleComponent.Z != 0) ScaleOffset = AXscale.Z;
  3467. ScaleOffset = Math.Abs(ScaleOffset);
  3468. Vector3 offset = result.normal * ScaleOffset;
  3469. result.ipoint = AXpos + offset;
  3470. ///pos = (intersectionpoint + offset);
  3471. }
  3472. else
  3473. {
  3474. result.normal = normals[i];
  3475. }
  3476. result.AAfaceNormal = AAfacenormals[i];
  3477. }
  3478. }
  3479. }
  3480. return result;
  3481. }
  3482. /// <summary>
  3483. /// Serialize this part to xml.
  3484. /// </summary>
  3485. /// <param name="xmlWriter"></param>
  3486. public void ToXml(XmlTextWriter xmlWriter)
  3487. {
  3488. SceneObjectSerializer.SOPToXml2(xmlWriter, this, new Dictionary<string, object>());
  3489. }
  3490. public void TriggerScriptChangedEvent(Changed val)
  3491. {
  3492. if (ParentGroup != null && ParentGroup.Scene != null)
  3493. ParentGroup.Scene.EventManager.TriggerOnScriptChangedEvent(LocalId, (uint)val);
  3494. }
  3495. public void TrimPermissions()
  3496. {
  3497. BaseMask &= (uint)PermissionMask.All;
  3498. OwnerMask &= (uint)PermissionMask.All;
  3499. GroupMask &= (uint)PermissionMask.All;
  3500. EveryoneMask &= (uint)PermissionMask.All;
  3501. NextOwnerMask &= (uint)PermissionMask.All;
  3502. }
  3503. public void UpdateExtraParam(ushort type, bool inUse, byte[] data)
  3504. {
  3505. m_shape.ReadInUpdateExtraParam(type, inUse, data);
  3506. if (type == 0x30)
  3507. {
  3508. if (m_shape.SculptEntry && m_shape.SculptTexture != UUID.Zero)
  3509. {
  3510. ParentGroup.Scene.AssetService.Get(m_shape.SculptTexture.ToString(), this, AssetReceived);
  3511. }
  3512. }
  3513. if (ParentGroup != null)
  3514. {
  3515. ParentGroup.HasGroupChanged = true;
  3516. ScheduleFullUpdate();
  3517. }
  3518. }
  3519. public void UpdateGroupPosition(Vector3 pos)
  3520. {
  3521. if ((pos.X != GroupPosition.X) ||
  3522. (pos.Y != GroupPosition.Y) ||
  3523. (pos.Z != GroupPosition.Z))
  3524. {
  3525. Vector3 newPos = new Vector3(pos.X, pos.Y, pos.Z);
  3526. GroupPosition = newPos;
  3527. ScheduleTerseUpdate();
  3528. }
  3529. }
  3530. /// <summary>
  3531. ///
  3532. /// </summary>
  3533. /// <param name="pos"></param>
  3534. public void UpdateOffSet(Vector3 pos)
  3535. {
  3536. if ((pos.X != OffsetPosition.X) ||
  3537. (pos.Y != OffsetPosition.Y) ||
  3538. (pos.Z != OffsetPosition.Z))
  3539. {
  3540. Vector3 newPos = new Vector3(pos.X, pos.Y, pos.Z);
  3541. if (ParentGroup.RootPart.GetStatusSandbox())
  3542. {
  3543. if (Util.GetDistanceTo(ParentGroup.RootPart.StatusSandboxPos, newPos) > 10)
  3544. {
  3545. ParentGroup.RootPart.ScriptSetPhysicsStatus(false);
  3546. newPos = OffsetPosition;
  3547. ParentGroup.Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  3548. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, ParentGroup.RootPart.AbsolutePosition, Name, UUID, false);
  3549. }
  3550. }
  3551. OffsetPosition = newPos;
  3552. ScheduleTerseUpdate();
  3553. }
  3554. }
  3555. /// <summary>
  3556. /// Update permissions on the SOP. Should only be called from SOG.UpdatePermissions because the SOG
  3557. /// will handle the client notifications once all of its parts are updated.
  3558. /// </summary>
  3559. /// <param name="AgentID"></param>
  3560. /// <param name="field"></param>
  3561. /// <param name="localID"></param>
  3562. /// <param name="mask"></param>
  3563. /// <param name="addRemTF"></param>
  3564. public void UpdatePermissions(UUID AgentID, byte field, uint localID, uint mask, byte addRemTF)
  3565. {
  3566. bool set = addRemTF == 1;
  3567. bool god = ParentGroup.Scene.Permissions.IsGod(AgentID);
  3568. uint baseMask = BaseMask;
  3569. if (god)
  3570. baseMask = 0x7ffffff0;
  3571. // Are we the owner?
  3572. if ((AgentID == OwnerID) || god)
  3573. {
  3574. switch (field)
  3575. {
  3576. case 1:
  3577. if (god)
  3578. {
  3579. BaseMask = ApplyMask(BaseMask, set, mask);
  3580. Inventory.ApplyGodPermissions(_baseMask);
  3581. }
  3582. break;
  3583. case 2:
  3584. OwnerMask = ApplyMask(OwnerMask, set, mask) &
  3585. baseMask;
  3586. break;
  3587. case 4:
  3588. GroupMask = ApplyMask(GroupMask, set, mask) &
  3589. baseMask;
  3590. break;
  3591. case 8:
  3592. EveryoneMask = ApplyMask(EveryoneMask, set, mask) &
  3593. baseMask;
  3594. break;
  3595. case 16:
  3596. NextOwnerMask = ApplyMask(NextOwnerMask, set, mask) &
  3597. baseMask;
  3598. // Prevent the client from creating no mod, no copy
  3599. // objects
  3600. if ((NextOwnerMask & (uint)PermissionMask.Copy) == 0)
  3601. NextOwnerMask |= (uint)PermissionMask.Transfer;
  3602. NextOwnerMask |= (uint)PermissionMask.Move;
  3603. break;
  3604. }
  3605. SendFullUpdateToAllClients();
  3606. }
  3607. }
  3608. public bool IsHingeJoint()
  3609. {
  3610. // For now, we use the NINJA naming scheme for identifying joints.
  3611. // In the future, we can support other joint specification schemes such as a
  3612. // custom checkbox in the viewer GUI.
  3613. if (ParentGroup.Scene != null && ParentGroup.Scene.PhysicsScene.SupportsNINJAJoints)
  3614. {
  3615. string hingeString = "hingejoint";
  3616. return (Name.Length >= hingeString.Length && Name.Substring(0, hingeString.Length) == hingeString);
  3617. }
  3618. else
  3619. {
  3620. return false;
  3621. }
  3622. }
  3623. public bool IsBallJoint()
  3624. {
  3625. // For now, we use the NINJA naming scheme for identifying joints.
  3626. // In the future, we can support other joint specification schemes such as a
  3627. // custom checkbox in the viewer GUI.
  3628. if (ParentGroup.Scene != null && ParentGroup.Scene.PhysicsScene.SupportsNINJAJoints)
  3629. {
  3630. string ballString = "balljoint";
  3631. return (Name.Length >= ballString.Length && Name.Substring(0, ballString.Length) == ballString);
  3632. }
  3633. else
  3634. {
  3635. return false;
  3636. }
  3637. }
  3638. public bool IsJoint()
  3639. {
  3640. // For now, we use the NINJA naming scheme for identifying joints.
  3641. // In the future, we can support other joint specification schemes such as a
  3642. // custom checkbox in the viewer GUI.
  3643. if (ParentGroup.Scene != null && ParentGroup.Scene.PhysicsScene.SupportsNINJAJoints)
  3644. {
  3645. return IsHingeJoint() || IsBallJoint();
  3646. }
  3647. else
  3648. {
  3649. return false;
  3650. }
  3651. }
  3652. /// <summary>
  3653. /// Update the flags on this prim. This covers properties such as phantom, physics and temporary.
  3654. /// </summary>
  3655. /// <param name="UsePhysics"></param>
  3656. /// <param name="SetTemporary"></param>
  3657. /// <param name="SetPhantom"></param>
  3658. /// <param name="SetVD"></param>
  3659. public void UpdatePrimFlags(bool UsePhysics, bool SetTemporary, bool SetPhantom, bool SetVD)
  3660. {
  3661. bool wasUsingPhysics = ((Flags & PrimFlags.Physics) != 0);
  3662. bool wasTemporary = ((Flags & PrimFlags.TemporaryOnRez) != 0);
  3663. bool wasPhantom = ((Flags & PrimFlags.Phantom) != 0);
  3664. bool wasVD = VolumeDetectActive;
  3665. if ((UsePhysics == wasUsingPhysics) && (wasTemporary == SetTemporary) && (wasPhantom == SetPhantom) && (SetVD == wasVD))
  3666. return;
  3667. PhysicsActor pa = PhysActor;
  3668. // Special cases for VD. VD can only be called from a script
  3669. // and can't be combined with changes to other states. So we can rely
  3670. // that...
  3671. // ... if VD is changed, all others are not.
  3672. // ... if one of the others is changed, VD is not.
  3673. if (SetVD) // VD is active, special logic applies
  3674. {
  3675. // State machine logic for VolumeDetect
  3676. // More logic below
  3677. bool phanReset = (SetPhantom != wasPhantom) && !SetPhantom;
  3678. if (phanReset) // Phantom changes from on to off switch VD off too
  3679. {
  3680. SetVD = false; // Switch it of for the course of this routine
  3681. VolumeDetectActive = false; // and also permanently
  3682. if (pa != null)
  3683. pa.SetVolumeDetect(0); // Let physics know about it too
  3684. }
  3685. else
  3686. {
  3687. // If volumedetect is active we don't want phantom to be applied.
  3688. // If this is a new call to VD out of the state "phantom"
  3689. // this will also cause the prim to be visible to physics
  3690. SetPhantom = false;
  3691. }
  3692. }
  3693. if (UsePhysics && IsJoint())
  3694. {
  3695. SetPhantom = true;
  3696. }
  3697. if (UsePhysics)
  3698. {
  3699. AddFlag(PrimFlags.Physics);
  3700. if (!wasUsingPhysics)
  3701. {
  3702. DoPhysicsPropertyUpdate(UsePhysics, false);
  3703. if (!ParentGroup.IsDeleted)
  3704. {
  3705. if (LocalId == ParentGroup.RootPart.LocalId)
  3706. {
  3707. ParentGroup.CheckSculptAndLoad();
  3708. }
  3709. }
  3710. }
  3711. }
  3712. else
  3713. {
  3714. RemFlag(PrimFlags.Physics);
  3715. if (wasUsingPhysics)
  3716. {
  3717. DoPhysicsPropertyUpdate(UsePhysics, false);
  3718. }
  3719. }
  3720. if (SetPhantom
  3721. || ParentGroup.IsAttachment
  3722. || (Shape.PathCurve == (byte)Extrusion.Flexible)) // note: this may have been changed above in the case of joints
  3723. {
  3724. AddFlag(PrimFlags.Phantom);
  3725. if (PhysActor != null)
  3726. {
  3727. RemoveFromPhysics();
  3728. pa = null;
  3729. }
  3730. }
  3731. else // Not phantom
  3732. {
  3733. RemFlag(PrimFlags.Phantom);
  3734. if (ParentGroup.Scene == null)
  3735. return;
  3736. if (ParentGroup.Scene.CollidablePrims && pa == null)
  3737. {
  3738. pa = AddToPhysics(UsePhysics);
  3739. if (pa != null)
  3740. {
  3741. pa.SetMaterial(Material);
  3742. DoPhysicsPropertyUpdate(UsePhysics, true);
  3743. if (!ParentGroup.IsDeleted)
  3744. {
  3745. if (LocalId == ParentGroup.RootPart.LocalId)
  3746. {
  3747. ParentGroup.CheckSculptAndLoad();
  3748. }
  3749. }
  3750. if (
  3751. ((AggregateScriptEvents & scriptEvents.collision) != 0) ||
  3752. ((AggregateScriptEvents & scriptEvents.collision_end) != 0) ||
  3753. ((AggregateScriptEvents & scriptEvents.collision_start) != 0) ||
  3754. ((AggregateScriptEvents & scriptEvents.land_collision_start) != 0) ||
  3755. ((AggregateScriptEvents & scriptEvents.land_collision) != 0) ||
  3756. ((AggregateScriptEvents & scriptEvents.land_collision_end) != 0) ||
  3757. (CollisionSound != UUID.Zero)
  3758. )
  3759. {
  3760. pa.OnCollisionUpdate += PhysicsCollision;
  3761. pa.SubscribeEvents(1000);
  3762. }
  3763. }
  3764. }
  3765. else // it already has a physical representation
  3766. {
  3767. DoPhysicsPropertyUpdate(UsePhysics, false); // Update physical status. If it's phantom this will remove the prim
  3768. if (!ParentGroup.IsDeleted)
  3769. {
  3770. if (LocalId == ParentGroup.RootPart.LocalId)
  3771. {
  3772. ParentGroup.CheckSculptAndLoad();
  3773. }
  3774. }
  3775. }
  3776. }
  3777. if (SetVD)
  3778. {
  3779. // If the above logic worked (this is urgent candidate to unit tests!)
  3780. // we now have a physicsactor.
  3781. // Defensive programming calls for a check here.
  3782. // Better would be throwing an exception that could be catched by a unit test as the internal
  3783. // logic should make sure, this Physactor is always here.
  3784. if (pa != null)
  3785. {
  3786. pa.SetVolumeDetect(1);
  3787. AddFlag(PrimFlags.Phantom); // We set this flag also if VD is active
  3788. VolumeDetectActive = true;
  3789. }
  3790. }
  3791. else
  3792. {
  3793. // Remove VolumeDetect in any case. Note, it's safe to call SetVolumeDetect as often as you like
  3794. // (mumbles, well, at least if you have infinte CPU powers :-))
  3795. if (pa != null)
  3796. pa.SetVolumeDetect(0);
  3797. VolumeDetectActive = false;
  3798. }
  3799. if (SetTemporary)
  3800. {
  3801. AddFlag(PrimFlags.TemporaryOnRez);
  3802. }
  3803. else
  3804. {
  3805. RemFlag(PrimFlags.TemporaryOnRez);
  3806. }
  3807. // m_log.Debug("Update: PHY:" + UsePhysics.ToString() + ", T:" + IsTemporary.ToString() + ", PHA:" + IsPhantom.ToString() + " S:" + CastsShadows.ToString());
  3808. if (ParentGroup != null)
  3809. {
  3810. ParentGroup.HasGroupChanged = true;
  3811. ScheduleFullUpdate();
  3812. }
  3813. // m_log.DebugFormat("[SCENE OBJECT PART]: Updated PrimFlags on {0} {1} to {2}", Name, LocalId, Flags);
  3814. }
  3815. /// <summary>
  3816. /// Adds this part to the physics scene.
  3817. /// </summary>
  3818. /// <remarks>This method also sets the PhysActor property.</remarks>
  3819. /// <param name="rigidBody">Add this prim with a rigid body.</param>
  3820. /// <returns>
  3821. /// The physics actor. null if there was a failure.
  3822. /// </returns>
  3823. private PhysicsActor AddToPhysics(bool rigidBody)
  3824. {
  3825. PhysicsActor pa;
  3826. try
  3827. {
  3828. pa = ParentGroup.Scene.PhysicsScene.AddPrimShape(
  3829. string.Format("{0}/{1}", Name, UUID),
  3830. Shape,
  3831. AbsolutePosition,
  3832. Scale,
  3833. RotationOffset,
  3834. rigidBody,
  3835. m_localId);
  3836. }
  3837. catch
  3838. {
  3839. m_log.ErrorFormat("[SCENE]: caught exception meshing object {0}. Object set to phantom.", m_uuid);
  3840. pa = null;
  3841. }
  3842. // FIXME: Ideally we wouldn't set the property here to reduce situations where threads changing physical
  3843. // properties can stop on each other. However, DoPhysicsPropertyUpdate() currently relies on PhysActor
  3844. // being set.
  3845. PhysActor = pa;
  3846. // Basic Physics can also return null as well as an exception catch.
  3847. if (pa != null)
  3848. {
  3849. pa.SOPName = this.Name; // save object into the PhysActor so ODE internals know the joint/body info
  3850. pa.SetMaterial(Material);
  3851. DoPhysicsPropertyUpdate(rigidBody, true);
  3852. }
  3853. return pa;
  3854. }
  3855. /// <summary>
  3856. /// This removes the part from the physics scene.
  3857. /// </summary>
  3858. /// <remarks>
  3859. /// This isn't the same as turning off physical, since even without being physical the prim has a physics
  3860. /// representation for collision detection. Rather, this would be used in situations such as making a prim
  3861. /// phantom.
  3862. /// </remarks>
  3863. public void RemoveFromPhysics()
  3864. {
  3865. ParentGroup.Scene.PhysicsScene.RemovePrim(PhysActor);
  3866. PhysActor = null;
  3867. }
  3868. /// <summary>
  3869. /// This updates the part's rotation and sends out an update to clients if necessary.
  3870. /// </summary>
  3871. /// <param name="rot"></param>
  3872. public void UpdateRotation(Quaternion rot)
  3873. {
  3874. if (rot != RotationOffset)
  3875. {
  3876. RotationOffset = rot;
  3877. if (ParentGroup != null)
  3878. {
  3879. ParentGroup.HasGroupChanged = true;
  3880. ScheduleTerseUpdate();
  3881. }
  3882. }
  3883. }
  3884. /// <summary>
  3885. /// Update the shape of this part.
  3886. /// </summary>
  3887. /// <param name="shapeBlock"></param>
  3888. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock)
  3889. {
  3890. m_shape.PathBegin = shapeBlock.PathBegin;
  3891. m_shape.PathEnd = shapeBlock.PathEnd;
  3892. m_shape.PathScaleX = shapeBlock.PathScaleX;
  3893. m_shape.PathScaleY = shapeBlock.PathScaleY;
  3894. m_shape.PathShearX = shapeBlock.PathShearX;
  3895. m_shape.PathShearY = shapeBlock.PathShearY;
  3896. m_shape.PathSkew = shapeBlock.PathSkew;
  3897. m_shape.ProfileBegin = shapeBlock.ProfileBegin;
  3898. m_shape.ProfileEnd = shapeBlock.ProfileEnd;
  3899. m_shape.PathCurve = shapeBlock.PathCurve;
  3900. m_shape.ProfileCurve = shapeBlock.ProfileCurve;
  3901. m_shape.ProfileHollow = shapeBlock.ProfileHollow;
  3902. m_shape.PathRadiusOffset = shapeBlock.PathRadiusOffset;
  3903. m_shape.PathRevolutions = shapeBlock.PathRevolutions;
  3904. m_shape.PathTaperX = shapeBlock.PathTaperX;
  3905. m_shape.PathTaperY = shapeBlock.PathTaperY;
  3906. m_shape.PathTwist = shapeBlock.PathTwist;
  3907. m_shape.PathTwistBegin = shapeBlock.PathTwistBegin;
  3908. PhysicsActor pa = PhysActor;
  3909. if (pa != null)
  3910. {
  3911. pa.Shape = m_shape;
  3912. ParentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(pa);
  3913. }
  3914. // This is what makes vehicle trailers work
  3915. // A script in a child prim re-issues
  3916. // llSetPrimitiveParams(PRIM_TYPE) every few seconds. That
  3917. // prevents autoreturn. This is not well known. It also works
  3918. // in SL.
  3919. //
  3920. if (ParentGroup.RootPart != this)
  3921. ParentGroup.RootPart.Rezzed = DateTime.UtcNow;
  3922. ParentGroup.HasGroupChanged = true;
  3923. TriggerScriptChangedEvent(Changed.SHAPE);
  3924. ScheduleFullUpdate();
  3925. }
  3926. /// <summary>
  3927. /// If the part is a sculpt/mesh, retrieve the mesh data and reinsert it into the shape so that the physics
  3928. /// engine can use it.
  3929. /// </summary>
  3930. /// <remarks>
  3931. /// When the physics engine has finished with it, the sculpt data is discarded to save memory.
  3932. /// </remarks>
  3933. public void CheckSculptAndLoad()
  3934. {
  3935. // m_log.DebugFormat("Processing CheckSculptAndLoad for {0} {1}", Name, LocalId);
  3936. if (ParentGroup.IsDeleted)
  3937. return;
  3938. if ((ParentGroup.RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Phantom) != 0)
  3939. return;
  3940. if (Shape.SculptEntry && Shape.SculptTexture != UUID.Zero)
  3941. {
  3942. // check if a previously decoded sculpt map has been cached
  3943. // We don't read the file here - the meshmerizer will do that later.
  3944. // TODO: Could we simplify the meshmerizer code by reading and setting the data here?
  3945. if (File.Exists(System.IO.Path.Combine("j2kDecodeCache", "smap_" + Shape.SculptTexture.ToString())))
  3946. {
  3947. SculptTextureCallback(null);
  3948. }
  3949. else
  3950. {
  3951. ParentGroup.Scene.AssetService.Get(Shape.SculptTexture.ToString(), this, AssetReceived);
  3952. }
  3953. }
  3954. }
  3955. /// <summary>
  3956. /// Update the texture entry for this part.
  3957. /// </summary>
  3958. /// <param name="serializedTextureEntry"></param>
  3959. public void UpdateTextureEntry(byte[] serializedTextureEntry)
  3960. {
  3961. UpdateTextureEntry(new Primitive.TextureEntry(serializedTextureEntry, 0, serializedTextureEntry.Length));
  3962. }
  3963. /// <summary>
  3964. /// Update the texture entry for this part.
  3965. /// </summary>
  3966. /// <param name="newTex"></param>
  3967. public void UpdateTextureEntry(Primitive.TextureEntry newTex)
  3968. {
  3969. Primitive.TextureEntry oldTex = Shape.Textures;
  3970. Changed changeFlags = 0;
  3971. for (int i = 0 ; i < GetNumberOfSides(); i++)
  3972. {
  3973. Primitive.TextureEntryFace newFace = newTex.DefaultTexture;
  3974. Primitive.TextureEntryFace oldFace = oldTex.DefaultTexture;
  3975. if (oldTex.FaceTextures[i] != null)
  3976. oldFace = oldTex.FaceTextures[i];
  3977. if (newTex.FaceTextures[i] != null)
  3978. newFace = newTex.FaceTextures[i];
  3979. Color4 oldRGBA = oldFace.RGBA;
  3980. Color4 newRGBA = newFace.RGBA;
  3981. if (oldRGBA.R != newRGBA.R ||
  3982. oldRGBA.G != newRGBA.G ||
  3983. oldRGBA.B != newRGBA.B ||
  3984. oldRGBA.A != newRGBA.A)
  3985. changeFlags |= Changed.COLOR;
  3986. if (oldFace.TextureID != newFace.TextureID)
  3987. changeFlags |= Changed.TEXTURE;
  3988. // Max change, skip the rest of testing
  3989. if (changeFlags == (Changed.TEXTURE | Changed.COLOR))
  3990. break;
  3991. }
  3992. m_shape.TextureEntry = newTex.GetBytes();
  3993. if (changeFlags != 0)
  3994. TriggerScriptChangedEvent(changeFlags);
  3995. UpdateFlag = UpdateRequired.FULL;
  3996. ParentGroup.HasGroupChanged = true;
  3997. //This is madness..
  3998. //ParentGroup.ScheduleGroupForFullUpdate();
  3999. //This is sparta
  4000. ScheduleFullUpdate();
  4001. }
  4002. public void aggregateScriptEvents()
  4003. {
  4004. AggregateScriptEvents = 0;
  4005. // Aggregate script events
  4006. lock (m_scriptEvents)
  4007. {
  4008. foreach (scriptEvents s in m_scriptEvents.Values)
  4009. {
  4010. AggregateScriptEvents |= s;
  4011. }
  4012. }
  4013. uint objectflagupdate = 0;
  4014. if (
  4015. ((AggregateScriptEvents & scriptEvents.touch) != 0) ||
  4016. ((AggregateScriptEvents & scriptEvents.touch_end) != 0) ||
  4017. ((AggregateScriptEvents & scriptEvents.touch_start) != 0)
  4018. )
  4019. {
  4020. objectflagupdate |= (uint) PrimFlags.Touch;
  4021. }
  4022. if ((AggregateScriptEvents & scriptEvents.money) != 0)
  4023. {
  4024. objectflagupdate |= (uint) PrimFlags.Money;
  4025. }
  4026. if (AllowedDrop)
  4027. {
  4028. objectflagupdate |= (uint) PrimFlags.AllowInventoryDrop;
  4029. }
  4030. PhysicsActor pa = PhysActor;
  4031. if (
  4032. ((AggregateScriptEvents & scriptEvents.collision) != 0) ||
  4033. ((AggregateScriptEvents & scriptEvents.collision_end) != 0) ||
  4034. ((AggregateScriptEvents & scriptEvents.collision_start) != 0) ||
  4035. ((AggregateScriptEvents & scriptEvents.land_collision_start) != 0) ||
  4036. ((AggregateScriptEvents & scriptEvents.land_collision) != 0) ||
  4037. ((AggregateScriptEvents & scriptEvents.land_collision_end) != 0) ||
  4038. (CollisionSound != UUID.Zero)
  4039. )
  4040. {
  4041. // subscribe to physics updates.
  4042. if (pa != null)
  4043. {
  4044. pa.OnCollisionUpdate += PhysicsCollision;
  4045. pa.SubscribeEvents(1000);
  4046. }
  4047. }
  4048. else
  4049. {
  4050. if (pa != null)
  4051. {
  4052. pa.UnSubscribeEvents();
  4053. pa.OnCollisionUpdate -= PhysicsCollision;
  4054. }
  4055. }
  4056. //if ((GetEffectiveObjectFlags() & (uint)PrimFlags.Scripted) != 0)
  4057. //{
  4058. // ParentGroup.Scene.EventManager.OnScriptTimerEvent += handleTimerAccounting;
  4059. //}
  4060. //else
  4061. //{
  4062. // ParentGroup.Scene.EventManager.OnScriptTimerEvent -= handleTimerAccounting;
  4063. //}
  4064. LocalFlags = (PrimFlags)objectflagupdate;
  4065. if (ParentGroup != null && ParentGroup.RootPart == this)
  4066. {
  4067. ParentGroup.aggregateScriptEvents();
  4068. }
  4069. else
  4070. {
  4071. // m_log.DebugFormat(
  4072. // "[SCENE OBJECT PART]: Scheduling part {0} {1} for full update in aggregateScriptEvents()", Name, LocalId);
  4073. ScheduleFullUpdate();
  4074. }
  4075. }
  4076. public void SetCameraAtOffset(Vector3 v)
  4077. {
  4078. m_cameraAtOffset = v;
  4079. }
  4080. public void SetCameraEyeOffset(Vector3 v)
  4081. {
  4082. m_cameraEyeOffset = v;
  4083. }
  4084. public void SetForceMouselook(bool force)
  4085. {
  4086. m_forceMouselook = force;
  4087. }
  4088. public Vector3 GetCameraAtOffset()
  4089. {
  4090. return m_cameraAtOffset;
  4091. }
  4092. public Vector3 GetCameraEyeOffset()
  4093. {
  4094. return m_cameraEyeOffset;
  4095. }
  4096. public bool GetForceMouselook()
  4097. {
  4098. return m_forceMouselook;
  4099. }
  4100. public override string ToString()
  4101. {
  4102. return String.Format("{0} {1} (parent {2}))", Name, UUID, ParentGroup);
  4103. }
  4104. #endregion Public Methods
  4105. public void SendTerseUpdateToClient(IClientAPI remoteClient)
  4106. {
  4107. if (ParentGroup.IsDeleted)
  4108. return;
  4109. if (ParentGroup.IsAttachment
  4110. && (ParentGroup.RootPart != this
  4111. || ParentGroup.AttachedAvatar != remoteClient.AgentId && ParentGroup.HasPrivateAttachmentPoint))
  4112. return;
  4113. // Causes this thread to dig into the Client Thread Data.
  4114. // Remember your locking here!
  4115. remoteClient.SendEntityUpdate(
  4116. this,
  4117. PrimUpdateFlags.Position | PrimUpdateFlags.Rotation | PrimUpdateFlags.Velocity
  4118. | PrimUpdateFlags.Acceleration | PrimUpdateFlags.AngularVelocity);
  4119. ParentGroup.Scene.StatsReporter.AddObjectUpdates(1);
  4120. }
  4121. public void AddScriptLPS(int count)
  4122. {
  4123. ParentGroup.AddScriptLPS(count);
  4124. }
  4125. public void ApplyNextOwnerPermissions()
  4126. {
  4127. BaseMask &= NextOwnerMask;
  4128. OwnerMask &= NextOwnerMask;
  4129. EveryoneMask &= NextOwnerMask;
  4130. Inventory.ApplyNextOwnerPermissions();
  4131. }
  4132. public void UpdateLookAt()
  4133. {
  4134. try
  4135. {
  4136. if (APIDTarget != Quaternion.Identity)
  4137. {
  4138. if (m_APIDIterations <= 1)
  4139. {
  4140. UpdateRotation(APIDTarget);
  4141. APIDTarget = Quaternion.Identity;
  4142. return;
  4143. }
  4144. Quaternion rot = Quaternion.Slerp(RotationOffset,APIDTarget,1.0f/(float)m_APIDIterations);
  4145. UpdateRotation(rot);
  4146. m_APIDIterations--;
  4147. // This ensures that we'll check this object on the next iteration
  4148. ParentGroup.QueueForUpdateCheck();
  4149. }
  4150. }
  4151. catch (Exception ex)
  4152. {
  4153. m_log.Error("[Physics] " + ex);
  4154. }
  4155. }
  4156. public Color4 GetTextColor()
  4157. {
  4158. Color color = Color;
  4159. return new Color4(color.R, color.G, color.B, (byte)(0xFF - color.A));
  4160. }
  4161. /// <summary>
  4162. /// Record an avatar sitting on this part.
  4163. /// </summary>
  4164. /// <remarks>This is called for all the sitting avatars whether there is a sit target set or not.</remarks>
  4165. /// <returns>
  4166. /// true if the avatar was not already recorded, false otherwise.
  4167. /// </returns>
  4168. /// <param name='avatarId'></param>
  4169. protected internal bool AddSittingAvatar(UUID avatarId)
  4170. {
  4171. if (IsSitTargetSet && SitTargetAvatar == UUID.Zero)
  4172. SitTargetAvatar = avatarId;
  4173. HashSet<UUID> sittingAvatars = m_sittingAvatars;
  4174. if (sittingAvatars == null)
  4175. sittingAvatars = new HashSet<UUID>();
  4176. lock (sittingAvatars)
  4177. {
  4178. m_sittingAvatars = sittingAvatars;
  4179. return m_sittingAvatars.Add(avatarId);
  4180. }
  4181. }
  4182. /// <summary>
  4183. /// Remove an avatar recorded as sitting on this part.
  4184. /// </summary>
  4185. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  4186. /// <returns>
  4187. /// true if the avatar was present and removed, false if it was not present.
  4188. /// </returns>
  4189. /// <param name='avatarId'></param>
  4190. protected internal bool RemoveSittingAvatar(UUID avatarId)
  4191. {
  4192. if (SitTargetAvatar == avatarId)
  4193. SitTargetAvatar = UUID.Zero;
  4194. HashSet<UUID> sittingAvatars = m_sittingAvatars;
  4195. // This can occur under a race condition where another thread
  4196. if (sittingAvatars == null)
  4197. return false;
  4198. lock (sittingAvatars)
  4199. {
  4200. if (sittingAvatars.Remove(avatarId))
  4201. {
  4202. if (sittingAvatars.Count == 0)
  4203. m_sittingAvatars = null;
  4204. return true;
  4205. }
  4206. }
  4207. return false;
  4208. }
  4209. /// <summary>
  4210. /// Get a copy of the list of sitting avatars.
  4211. /// </summary>
  4212. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  4213. /// <returns>A hashset of the sitting avatars. Returns null if there are no sitting avatars.</returns>
  4214. public HashSet<UUID> GetSittingAvatars()
  4215. {
  4216. HashSet<UUID> sittingAvatars = m_sittingAvatars;
  4217. if (sittingAvatars == null)
  4218. {
  4219. return null;
  4220. }
  4221. else
  4222. {
  4223. lock (sittingAvatars)
  4224. return new HashSet<UUID>(sittingAvatars);
  4225. }
  4226. }
  4227. /// <summary>
  4228. /// Gets the number of sitting avatars.
  4229. /// </summary>
  4230. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  4231. /// <returns></returns>
  4232. public int GetSittingAvatarsCount()
  4233. {
  4234. HashSet<UUID> sittingAvatars = m_sittingAvatars;
  4235. if (sittingAvatars == null)
  4236. return 0;
  4237. lock (sittingAvatars)
  4238. return sittingAvatars.Count;
  4239. }
  4240. }
  4241. }