SceneObjectPart.cs 175 KB

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