shapeBase.cpp 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/shapeBase.h"
  24. #include "core/dnet.h"
  25. #include "sfx/sfxSystem.h"
  26. #include "sfx/sfxSource.h"
  27. #include "sfx/sfxTrack.h"
  28. #include "sfx/sfxDescription.h"
  29. #include "T3D/sfx/sfx3DWorld.h"
  30. #include "T3D/gameBase/gameConnection.h"
  31. #include "console/consoleTypes.h"
  32. #include "console/engineAPI.h"
  33. #include "core/stream/bitStream.h"
  34. #include "ts/tsPartInstance.h"
  35. #include "ts/tsShapeInstance.h"
  36. #include "ts/tsMaterialList.h"
  37. #include "scene/sceneManager.h"
  38. #include "scene/sceneRenderState.h"
  39. #include "scene/sceneObjectLightingPlugin.h"
  40. #include "T3D/fx/explosion.h"
  41. #include "T3D/fx/cameraFXMgr.h"
  42. #include "environment/waterBlock.h"
  43. #include "T3D/debris.h"
  44. #include "T3D/physicalZone.h"
  45. #include "T3D/containerQuery.h"
  46. #include "math/mathUtils.h"
  47. #include "math/mMatrix.h"
  48. #include "math/mTransform.h"
  49. #include "math/mRandom.h"
  50. #include "platform/profiler.h"
  51. #include "gfx/gfxCubemap.h"
  52. #include "gfx/gfxDrawUtil.h"
  53. #include "gfx/gfxTransformSaver.h"
  54. #include "renderInstance/renderPassManager.h"
  55. #include "collision/earlyOutPolyList.h"
  56. #include "core/resourceManager.h"
  57. #include "scene/reflectionManager.h"
  58. #include "gfx/sim/cubemapData.h"
  59. #include "materials/materialManager.h"
  60. #include "materials/materialFeatureTypes.h"
  61. #include "renderInstance/renderOcclusionMgr.h"
  62. #include "core/stream/fileStream.h"
  63. IMPLEMENT_CO_DATABLOCK_V1(ShapeBaseData);
  64. ConsoleDocClass( ShapeBaseData,
  65. "@brief Defines properties for a ShapeBase object.\n\n"
  66. "@see ShapeBase\n"
  67. "@ingroup gameObjects\n"
  68. );
  69. IMPLEMENT_CALLBACK( ShapeBaseData, onEnabled, void, ( ShapeBase* obj, const char* lastState ), ( obj, lastState ),
  70. "@brief Called when the object damage state changes to Enabled.\n\n"
  71. "@param obj The ShapeBase object\n"
  72. "@param lastState The previous damage state\n" );
  73. IMPLEMENT_CALLBACK( ShapeBaseData, onDisabled, void, ( ShapeBase* obj, const char* lastState ), ( obj, lastState ),
  74. "@brief Called when the object damage state changes to Disabled.\n\n"
  75. "@param obj The ShapeBase object\n"
  76. "@param lastState The previous damage state\n" );
  77. IMPLEMENT_CALLBACK( ShapeBaseData, onDestroyed, void, ( ShapeBase* obj, const char* lastState ), ( obj, lastState ),
  78. "@brief Called when the object damage state changes to Destroyed.\n\n"
  79. "@param obj The ShapeBase object\n"
  80. "@param lastState The previous damage state\n" );
  81. IMPLEMENT_CALLBACK( ShapeBaseData, onImpact, void, ( ShapeBase* obj, SceneObject *collObj, VectorF vec, F32 len ), ( obj, collObj, vec, len ),
  82. "@brief Called when we collide with another object beyond some impact speed.\n\n"
  83. "The Player class makes use of this callback when a collision speed is more than PlayerData::minImpactSpeed.\n"
  84. "@param obj The ShapeBase object\n"
  85. "@param collObj The object we collided with\n"
  86. "@param vec Collision impact vector\n"
  87. "@param len Length of the impact vector\n" );
  88. IMPLEMENT_CALLBACK( ShapeBaseData, onCollision, void, ( ShapeBase* obj, SceneObject *collObj, VectorF vec, F32 len ), ( obj, collObj, vec, len ),
  89. "@brief Called when we collide with another object.\n\n"
  90. "@param obj The ShapeBase object\n"
  91. "@param collObj The object we collided with\n"
  92. "@param vec Collision impact vector\n"
  93. "@param len Length of the impact vector\n" );
  94. IMPLEMENT_CALLBACK( ShapeBaseData, onDamage, void, ( ShapeBase* obj, F32 delta ), ( obj, delta ),
  95. "@brief Called when the object is damaged.\n\n"
  96. "@param obj The ShapeBase object\n"
  97. "@param obj The ShapeBase object\n"
  98. "@param delta The amount of damage received." );
  99. IMPLEMENT_CALLBACK( ShapeBaseData, onTrigger, void, ( ShapeBase* obj, S32 index, bool state ), ( obj, index, state ),
  100. "@brief Called when a move trigger input changes state.\n\n"
  101. "@param obj The ShapeBase object\n"
  102. "@param index Index of the trigger that changed\n"
  103. "@param state New state of the trigger\n" );
  104. IMPLEMENT_CALLBACK(ShapeBaseData, onEndSequence, void, (ShapeBase* obj, S32 slot, const char* name), (obj, slot, name),
  105. "@brief Called when a thread playing a non-cyclic sequence reaches the end of the "
  106. "sequence.\n\n"
  107. "@param obj The ShapeBase object\n"
  108. "@param slot Thread slot that finished playing\n"
  109. "@param name Thread name that finished playing\n");
  110. IMPLEMENT_CALLBACK( ShapeBaseData, onForceUncloak, void, ( ShapeBase* obj, const char* reason ), ( obj, reason ),
  111. "@brief Called when the object is forced to uncloak.\n\n"
  112. "@param obj The ShapeBase object\n"
  113. "@param reason String describing why the object was uncloaked\n" );
  114. //----------------------------------------------------------------------------
  115. // Timeout for non-looping sounds on a channel
  116. static SimTime sAudioTimeout = 500;
  117. F32 ShapeBase::sWhiteoutDec = 0.007f;
  118. F32 ShapeBase::sDamageFlashDec = 0.02f;
  119. F32 ShapeBase::sFullCorrectionDistance = 0.5f;
  120. F32 ShapeBase::sCloakSpeed = 0.5;
  121. U32 ShapeBase::sLastRenderFrame = 0;
  122. static const char *sDamageStateName[] =
  123. {
  124. // Index by enum ShapeBase::DamageState
  125. "Enabled",
  126. "Disabled",
  127. "Destroyed"
  128. };
  129. //----------------------------------------------------------------------------
  130. ShapeBaseData::ShapeBaseData()
  131. : shadowEnable( false ),
  132. shadowSize( 128 ),
  133. shadowMaxVisibleDistance( 80.0f ),
  134. shadowProjectionDistance( 10.0f ),
  135. shadowSphereAdjust( 1.0f ),
  136. shapeName( StringTable->insert("") ),
  137. cloakTexName( StringTable->insert("") ),
  138. mass( 1.0f ),
  139. drag( 0.0f ),
  140. density( 1.0f ),
  141. maxEnergy( 0.0f ),
  142. maxDamage( 1.0f ),
  143. disabledLevel( 1.0f ),
  144. destroyedLevel( 1.0f ),
  145. repairRate( 0.0033f ),
  146. eyeNode( -1 ),
  147. earNode( -1 ),
  148. cameraNode( -1 ),
  149. damageSequence( -1 ),
  150. hulkSequence( -1 ),
  151. cameraMaxDist( 0.0f ),
  152. cameraMinDist( 0.2f ),
  153. cameraDefaultFov( 75.0f ),
  154. cameraMinFov( 5.0f ),
  155. cameraMaxFov( 120.f ),
  156. cameraCanBank( false ),
  157. mountedImagesBank( false ),
  158. isInvincible( false ),
  159. renderWhenDestroyed( true ),
  160. debris( NULL ),
  161. debrisID( 0 ),
  162. debrisShapeName( StringTable->insert("") ),
  163. explosion( NULL ),
  164. explosionID( 0 ),
  165. underwaterExplosion( NULL ),
  166. underwaterExplosionID( 0 ),
  167. firstPersonOnly( false ),
  168. useEyePoint( false ),
  169. cubeDescId( 0 ),
  170. reflectorDesc( NULL ),
  171. observeThroughObject( false ),
  172. computeCRC( false ),
  173. inheritEnergyFromMount( false ),
  174. mCRC( 0 ),
  175. debrisDetail( -1 )
  176. {
  177. dMemset( mountPointNode, -1, sizeof( S32 ) * SceneObject::NumMountPoints );
  178. }
  179. struct ShapeBaseDataProto
  180. {
  181. F32 mass;
  182. F32 drag;
  183. F32 density;
  184. F32 maxEnergy;
  185. F32 cameraMaxDist;
  186. F32 cameraMinDist;
  187. F32 cameraDefaultFov;
  188. F32 cameraMinFov;
  189. F32 cameraMaxFov;
  190. ShapeBaseDataProto()
  191. {
  192. mass = 1;
  193. drag = 0;
  194. density = 1;
  195. maxEnergy = 0;
  196. cameraMaxDist = 0;
  197. cameraMinDist = 0.2f;
  198. cameraDefaultFov = 75.f;
  199. cameraMinFov = 5.0f;
  200. cameraMaxFov = 120.f;
  201. }
  202. };
  203. static ShapeBaseDataProto gShapeBaseDataProto;
  204. ShapeBaseData::~ShapeBaseData()
  205. {
  206. }
  207. bool ShapeBaseData::preload(bool server, String &errorStr)
  208. {
  209. if (!Parent::preload(server, errorStr))
  210. return false;
  211. bool shapeError = false;
  212. // Resolve objects transmitted from server
  213. if (!server) {
  214. if( !explosion && explosionID != 0 )
  215. {
  216. if( Sim::findObject( explosionID, explosion ) == false)
  217. {
  218. Con::errorf( ConsoleLogEntry::General, "ShapeBaseData::preload: Invalid packet, bad datablockId(explosion): 0x%x", explosionID );
  219. }
  220. AssertFatal(!(explosion && ((explosionID < DataBlockObjectIdFirst) || (explosionID > DataBlockObjectIdLast))),
  221. "ShapeBaseData::preload: invalid explosion data");
  222. }
  223. if( !underwaterExplosion && underwaterExplosionID != 0 )
  224. {
  225. if( Sim::findObject( underwaterExplosionID, underwaterExplosion ) == false)
  226. {
  227. Con::errorf( ConsoleLogEntry::General, "ShapeBaseData::preload: Invalid packet, bad datablockId(underwaterExplosion): 0x%x", underwaterExplosionID );
  228. }
  229. AssertFatal(!(underwaterExplosion && ((underwaterExplosionID < DataBlockObjectIdFirst) || (underwaterExplosionID > DataBlockObjectIdLast))),
  230. "ShapeBaseData::preload: invalid underwaterExplosion data");
  231. }
  232. if( !debris && debrisID != 0 )
  233. {
  234. Sim::findObject( debrisID, debris );
  235. AssertFatal(!(debris && ((debrisID < DataBlockObjectIdFirst) || (debrisID > DataBlockObjectIdLast))),
  236. "ShapeBaseData::preload: invalid debris data");
  237. }
  238. if( debrisShapeName && debrisShapeName[0] != '\0' && !bool(debrisShape) )
  239. {
  240. debrisShape = ResourceManager::get().load(debrisShapeName);
  241. if( bool(debrisShape) == false )
  242. {
  243. errorStr = String::ToString("ShapeBaseData::load: Couldn't load shape \"%s\"", debrisShapeName);
  244. return false;
  245. }
  246. else
  247. {
  248. if(!server && !debrisShape->preloadMaterialList(debrisShape.getPath()) && NetConnection::filesWereDownloaded())
  249. shapeError = true;
  250. TSShapeInstance* pDummy = new TSShapeInstance(debrisShape, !server);
  251. delete pDummy;
  252. }
  253. }
  254. }
  255. //
  256. if (shapeName && shapeName[0]) {
  257. S32 i;
  258. // Resolve shapename
  259. mShape = ResourceManager::get().load(shapeName);
  260. if (bool(mShape) == false)
  261. {
  262. errorStr = String::ToString("ShapeBaseData: Couldn't load shape \"%s\"",shapeName);
  263. return false;
  264. }
  265. if(!server && !mShape->preloadMaterialList(mShape.getPath()) && NetConnection::filesWereDownloaded())
  266. shapeError = true;
  267. if(computeCRC)
  268. {
  269. Con::printf("Validation required for shape: %s", shapeName);
  270. Torque::FS::FileNodeRef fileRef = Torque::FS::GetFileNode(mShape.getPath());
  271. if (!fileRef)
  272. {
  273. errorStr = String::ToString("ShapeBaseData: Couldn't load shape \"%s\"",shapeName);
  274. return false;
  275. }
  276. if(server)
  277. mCRC = fileRef->getChecksum();
  278. else if(mCRC != fileRef->getChecksum())
  279. {
  280. errorStr = String::ToString("Shape \"%s\" does not match version on server.",shapeName);
  281. return false;
  282. }
  283. }
  284. // Resolve details and camera node indexes.
  285. static const String sCollisionStr( "collision-" );
  286. for (i = 0; i < mShape->details.size(); i++)
  287. {
  288. const String &name = mShape->names[mShape->details[i].nameIndex];
  289. if (name.compare( sCollisionStr, sCollisionStr.length(), String::NoCase ) == 0)
  290. {
  291. collisionDetails.push_back(i);
  292. collisionBounds.increment();
  293. mShape->computeBounds(collisionDetails.last(), collisionBounds.last());
  294. mShape->getAccelerator(collisionDetails.last());
  295. if (!mShape->bounds.isContained(collisionBounds.last()))
  296. {
  297. Con::warnf("Warning: shape %s collision detail %d (Collision-%d) bounds exceed that of shape.", shapeName, collisionDetails.size() - 1, collisionDetails.last());
  298. collisionBounds.last() = mShape->bounds;
  299. }
  300. else if (collisionBounds.last().isValidBox() == false)
  301. {
  302. Con::errorf("Error: shape %s-collision detail %d (Collision-%d) bounds box invalid!", shapeName, collisionDetails.size() - 1, collisionDetails.last());
  303. collisionBounds.last() = mShape->bounds;
  304. }
  305. // The way LOS works is that it will check to see if there is a LOS detail that matches
  306. // the the collision detail + 1 + MaxCollisionShapes (this variable name should change in
  307. // the future). If it can't find a matching LOS it will simply use the collision instead.
  308. // We check for any "unmatched" LOS's further down
  309. LOSDetails.increment();
  310. String buff = String::ToString("LOS-%d", i + 1 + MaxCollisionShapes);
  311. U32 los = mShape->findDetail(buff);
  312. if (los == -1)
  313. LOSDetails.last() = i;
  314. else
  315. LOSDetails.last() = los;
  316. }
  317. }
  318. // Snag any "unmatched" LOS details
  319. static const String sLOSStr( "LOS-" );
  320. for (i = 0; i < mShape->details.size(); i++)
  321. {
  322. const String &name = mShape->names[mShape->details[i].nameIndex];
  323. if (name.compare( sLOSStr, sLOSStr.length(), String::NoCase ) == 0)
  324. {
  325. // See if we already have this LOS
  326. bool found = false;
  327. for (U32 j = 0; j < LOSDetails.size(); j++)
  328. {
  329. if (LOSDetails[j] == i)
  330. {
  331. found = true;
  332. break;
  333. }
  334. }
  335. if (!found)
  336. LOSDetails.push_back(i);
  337. }
  338. }
  339. debrisDetail = mShape->findDetail("Debris-17");
  340. eyeNode = mShape->findNode("eye");
  341. earNode = mShape->findNode( "ear" );
  342. if( earNode == -1 )
  343. earNode = eyeNode;
  344. cameraNode = mShape->findNode("cam");
  345. if (cameraNode == -1)
  346. cameraNode = eyeNode;
  347. // Resolve mount point node indexes
  348. for (i = 0; i < SceneObject::NumMountPoints; i++) {
  349. char fullName[256];
  350. dSprintf(fullName,sizeof(fullName),"mount%d",i);
  351. mountPointNode[i] = mShape->findNode(fullName);
  352. }
  353. // find the AIRepairNode - hardcoded to be the last node in the array...
  354. mountPointNode[AIRepairNode] = mShape->findNode("AIRepairNode");
  355. //
  356. hulkSequence = mShape->findSequence("Visibility");
  357. damageSequence = mShape->findSequence("Damage");
  358. //
  359. F32 w = mShape->bounds.len_y() / 2;
  360. if (cameraMaxDist < w)
  361. cameraMaxDist = w;
  362. }
  363. if(!server)
  364. {
  365. /*
  366. // grab all the hud images
  367. for(U32 i = 0; i < NumHudRenderImages; i++)
  368. {
  369. if(hudImageNameFriendly[i] && hudImageNameFriendly[i][0])
  370. hudImageFriendly[i] = TextureHandle(hudImageNameFriendly[i], BitmapTexture);
  371. if(hudImageNameEnemy[i] && hudImageNameEnemy[i][0])
  372. hudImageEnemy[i] = TextureHandle(hudImageNameEnemy[i], BitmapTexture);
  373. }
  374. */
  375. }
  376. // Resolve CubeReflectorDesc.
  377. if ( cubeDescName.isNotEmpty() )
  378. {
  379. Sim::findObject( cubeDescName, reflectorDesc );
  380. }
  381. else if( cubeDescId > 0 )
  382. {
  383. Sim::findObject( cubeDescId, reflectorDesc );
  384. }
  385. return !shapeError;
  386. }
  387. bool ShapeBaseData::_setMass( void* object, const char* index, const char* data )
  388. {
  389. ShapeBaseData* shape = reinterpret_cast< ShapeBaseData* >( object );
  390. F32 mass = dAtof(data);
  391. if (mass <= 0)
  392. mass = 0.01f;
  393. shape->mass = mass;
  394. return false;
  395. }
  396. void ShapeBaseData::initPersistFields()
  397. {
  398. addGroup( "Shadows" );
  399. addField( "shadowEnable", TypeBool, Offset(shadowEnable, ShapeBaseData),
  400. "Enable shadows for this shape (currently unused, shadows are always enabled)." );
  401. addField( "shadowSize", TypeS32, Offset(shadowSize, ShapeBaseData),
  402. "Size of the projected shadow texture (must be power of 2)." );
  403. addField( "shadowMaxVisibleDistance", TypeF32, Offset(shadowMaxVisibleDistance, ShapeBaseData),
  404. "Maximum distance at which shadow is visible (currently unused)." );
  405. addField( "shadowProjectionDistance", TypeF32, Offset(shadowProjectionDistance, ShapeBaseData),
  406. "Maximum height above ground to project shadow. If the object is higher "
  407. "than this no shadow will be rendered." );
  408. addField( "shadowSphereAdjust", TypeF32, Offset(shadowSphereAdjust, ShapeBaseData),
  409. "Scalar applied to the radius of spot shadows (initial radius is based "
  410. "on the shape bounds but can be adjusted with this field)." );
  411. endGroup( "Shadows" );
  412. addGroup( "Render" );
  413. addField( "shapeFile", TypeShapeFilename, Offset(shapeName, ShapeBaseData),
  414. "The DTS or DAE model to use for this object." );
  415. endGroup( "Render" );
  416. addGroup( "Destruction", "Parameters related to the destruction effects of this object." );
  417. addField( "explosion", TYPEID< ExplosionData >(), Offset(explosion, ShapeBaseData),
  418. "%Explosion to generate when this shape is blown up." );
  419. addField( "underwaterExplosion", TYPEID< ExplosionData >(), Offset(underwaterExplosion, ShapeBaseData),
  420. "%Explosion to generate when this shape is blown up underwater." );
  421. addField( "debris", TYPEID< DebrisData >(), Offset(debris, ShapeBaseData),
  422. "%Debris to generate when this shape is blown up." );
  423. addField( "renderWhenDestroyed", TypeBool, Offset(renderWhenDestroyed, ShapeBaseData),
  424. "Whether to render the shape when it is in the \"Destroyed\" damage state." );
  425. addField( "debrisShapeName", TypeShapeFilename, Offset(debrisShapeName, ShapeBaseData),
  426. "The DTS or DAE model to use for auto-generated breakups. @note may not be functional." );
  427. endGroup( "Destruction" );
  428. addGroup( "Physics" );
  429. addProtectedField("mass", TypeF32, Offset(mass, ShapeBaseData), &_setMass, &defaultProtectedGetFn, "Shape mass.\nUsed in simulation of moving objects.\n" );
  430. addField( "drag", TypeF32, Offset(drag, ShapeBaseData),
  431. "Drag factor.\nReduces velocity of moving objects." );
  432. addField( "density", TypeF32, Offset(density, ShapeBaseData),
  433. "Shape density.\nUsed when computing buoyancy when in water.\n" );
  434. endGroup( "Physics" );
  435. addGroup( "Damage/Energy" );
  436. addField( "maxEnergy", TypeF32, Offset(maxEnergy, ShapeBaseData),
  437. "Maximum energy level for this object." );
  438. addField( "maxDamage", TypeF32, Offset(maxDamage, ShapeBaseData),
  439. "Maximum damage level for this object." );
  440. addField( "disabledLevel", TypeF32, Offset(disabledLevel, ShapeBaseData),
  441. "Damage level above which the object is disabled.\n"
  442. "Currently unused." );
  443. addField( "destroyedLevel", TypeF32, Offset(destroyedLevel, ShapeBaseData),
  444. "Damage level above which the object is destroyed.\n"
  445. "When the damage level increases above this value, the object damage "
  446. "state is set to \"Destroyed\"." );
  447. addField( "repairRate", TypeF32, Offset(repairRate, ShapeBaseData),
  448. "Rate at which damage is repaired in damage units/tick.\n"
  449. "This value is subtracted from the damage level until it reaches 0." );
  450. addField( "inheritEnergyFromMount", TypeBool, Offset(inheritEnergyFromMount, ShapeBaseData),
  451. "Flag controlling whether to manage our own energy level, or to use "
  452. "the energy level of the object we are mounted to." );
  453. addField( "isInvincible", TypeBool, Offset(isInvincible, ShapeBaseData),
  454. "Invincible flag; when invincible, the object cannot be damaged or "
  455. "repaired." );
  456. endGroup( "Damage/Energy" );
  457. addGroup( "Camera", "The settings used by the shape when it is the camera." );
  458. addField( "cameraMaxDist", TypeF32, Offset(cameraMaxDist, ShapeBaseData),
  459. "The maximum distance from the camera to the object.\n"
  460. "Used when computing a custom camera transform for this object.\n\n"
  461. "@see observeThroughObject" );
  462. addField( "cameraMinDist", TypeF32, Offset(cameraMinDist, ShapeBaseData),
  463. "The minimum distance from the camera to the object.\n"
  464. "Used when computing a custom camera transform for this object.\n\n"
  465. "@see observeThroughObject" );
  466. addField( "cameraDefaultFov", TypeF32, Offset(cameraDefaultFov, ShapeBaseData),
  467. "The default camera vertical FOV in degrees." );
  468. addField( "cameraMinFov", TypeF32, Offset(cameraMinFov, ShapeBaseData),
  469. "The minimum camera vertical FOV allowed in degrees." );
  470. addField( "cameraMaxFov", TypeF32, Offset(cameraMaxFov, ShapeBaseData),
  471. "The maximum camera vertical FOV allowed in degrees." );
  472. addField( "cameraCanBank", TypeBool, Offset(cameraCanBank, ShapeBaseData),
  473. "If the derrived class supports it, allow the camera to bank." );
  474. addField( "mountedImagesBank", TypeBool, Offset(mountedImagesBank, ShapeBaseData),
  475. "Do mounted images bank along with the camera?" );
  476. addField( "firstPersonOnly", TypeBool, Offset(firstPersonOnly, ShapeBaseData),
  477. "Flag controlling whether the view from this object is first person "
  478. "only." );
  479. addField( "useEyePoint", TypeBool, Offset(useEyePoint, ShapeBaseData),
  480. "Flag controlling whether the client uses this object's eye point to "
  481. "view from." );
  482. addField( "observeThroughObject", TypeBool, Offset(observeThroughObject, ShapeBaseData),
  483. "Observe this object through its camera transform and default fov.\n"
  484. "If true, when this object is the camera it can provide a custom camera "
  485. "transform and FOV (instead of the default eye transform)." );
  486. endGroup("Camera");
  487. addGroup( "Misc" );
  488. addField( "computeCRC", TypeBool, Offset(computeCRC, ShapeBaseData),
  489. "If true, verify that the CRC of the client's shape model matches the "
  490. "server's CRC for the shape model when loaded by the client." );
  491. endGroup( "Misc" );
  492. addGroup( "Reflection" );
  493. addField( "cubeReflectorDesc", TypeRealString, Offset( cubeDescName, ShapeBaseData ),
  494. "References a ReflectorDesc datablock that defines performance and quality properties for dynamic reflections.\n");
  495. //addField( "reflectMaxRateMs", TypeS32, Offset( reflectMaxRateMs, ShapeBaseData ), "reflection will not be updated more frequently than this" );
  496. //addField( "reflectMaxDist", TypeF32, Offset( reflectMaxDist, ShapeBaseData ), "distance at which reflection is never updated" );
  497. //addField( "reflectMinDist", TypeF32, Offset( reflectMinDist, ShapeBaseData ), "distance at which reflection is always updated" );
  498. //addField( "reflectDetailAdjust", TypeF32, Offset( reflectDetailAdjust, ShapeBaseData ), "scale up or down the detail level for objects rendered in a reflection" );
  499. endGroup( "Reflection" );
  500. Parent::initPersistFields();
  501. }
  502. DefineEngineMethod( ShapeBaseData, checkDeployPos, bool, ( TransformF txfm ),,
  503. "@brief Check if there is the space at the given transform is free to spawn into.\n\n"
  504. "The shape's bounding box volume is used to check for collisions at the given world "
  505. "transform. Only interior and static objects are checked for collision.\n"
  506. "@param txfm Deploy transform to check\n"
  507. "@return True if the space is free, false if there is already something in "
  508. "the way.\n"
  509. "@note This is a server side only check, and is not actually limited to spawning.\n")
  510. {
  511. if (bool(object->mShape) == false)
  512. return false;
  513. MatrixF mat = txfm.getMatrix();
  514. Box3F objBox = object->mShape->bounds;
  515. Point3F boxCenter = (objBox.minExtents + objBox.maxExtents) * 0.5f;
  516. objBox.minExtents = boxCenter + (objBox.minExtents - boxCenter) * 0.9f;
  517. objBox.maxExtents = boxCenter + (objBox.maxExtents - boxCenter) * 0.9f;
  518. Box3F wBox = objBox;
  519. mat.mul(wBox);
  520. EarlyOutPolyList polyList;
  521. polyList.mNormal.set(0,0,0);
  522. polyList.mPlaneList.clear();
  523. polyList.mPlaneList.setSize(6);
  524. polyList.mPlaneList[0].set(objBox.minExtents,VectorF(-1,0,0));
  525. polyList.mPlaneList[1].set(objBox.maxExtents,VectorF(0,1,0));
  526. polyList.mPlaneList[2].set(objBox.maxExtents,VectorF(1,0,0));
  527. polyList.mPlaneList[3].set(objBox.minExtents,VectorF(0,-1,0));
  528. polyList.mPlaneList[4].set(objBox.minExtents,VectorF(0,0,-1));
  529. polyList.mPlaneList[5].set(objBox.maxExtents,VectorF(0,0,1));
  530. for (U32 i = 0; i < 6; i++)
  531. {
  532. PlaneF temp;
  533. mTransformPlane(mat, Point3F(1, 1, 1), polyList.mPlaneList[i], &temp);
  534. polyList.mPlaneList[i] = temp;
  535. }
  536. if (gServerContainer.buildPolyList(PLC_Collision, wBox, StaticShapeObjectType, &polyList))
  537. return false;
  538. return true;
  539. }
  540. DefineEngineMethod(ShapeBaseData, getDeployTransform, TransformF, ( Point3F pos, Point3F normal ),,
  541. "@brief Helper method to get a transform from a position and vector (suitable for use with setTransform).\n\n"
  542. "@param pos Desired transform position\n"
  543. "@param normal Vector of desired direction\n"
  544. "@return The deploy transform\n" )
  545. {
  546. normal.normalize();
  547. VectorF xAxis;
  548. if( mFabs(normal.z) > mFabs(normal.x) && mFabs(normal.z) > mFabs(normal.y))
  549. mCross( VectorF( 0, 1, 0 ), normal, &xAxis );
  550. else
  551. mCross( VectorF( 0, 0, 1 ), normal, &xAxis );
  552. VectorF yAxis;
  553. mCross( normal, xAxis, &yAxis );
  554. MatrixF testMat(true);
  555. testMat.setColumn( 0, xAxis );
  556. testMat.setColumn( 1, yAxis );
  557. testMat.setColumn( 2, normal );
  558. testMat.setPosition( pos );
  559. return testMat;
  560. }
  561. void ShapeBaseData::packData(BitStream* stream)
  562. {
  563. Parent::packData(stream);
  564. if(stream->writeFlag(computeCRC))
  565. stream->write(mCRC);
  566. stream->writeFlag(shadowEnable);
  567. stream->write(shadowSize);
  568. stream->write(shadowMaxVisibleDistance);
  569. stream->write(shadowProjectionDistance);
  570. stream->write(shadowSphereAdjust);
  571. stream->writeString(shapeName);
  572. stream->writeString(cloakTexName);
  573. if(stream->writeFlag(mass != gShapeBaseDataProto.mass))
  574. stream->write(mass);
  575. if(stream->writeFlag(drag != gShapeBaseDataProto.drag))
  576. stream->write(drag);
  577. if(stream->writeFlag(density != gShapeBaseDataProto.density))
  578. stream->write(density);
  579. if(stream->writeFlag(maxEnergy != gShapeBaseDataProto.maxEnergy))
  580. stream->write(maxEnergy);
  581. if(stream->writeFlag(cameraMaxDist != gShapeBaseDataProto.cameraMaxDist))
  582. stream->write(cameraMaxDist);
  583. if(stream->writeFlag(cameraMinDist != gShapeBaseDataProto.cameraMinDist))
  584. stream->write(cameraMinDist);
  585. cameraDefaultFov = mClampF(cameraDefaultFov, cameraMinFov, cameraMaxFov);
  586. if(stream->writeFlag(cameraDefaultFov != gShapeBaseDataProto.cameraDefaultFov))
  587. stream->write(cameraDefaultFov);
  588. if(stream->writeFlag(cameraMinFov != gShapeBaseDataProto.cameraMinFov))
  589. stream->write(cameraMinFov);
  590. if(stream->writeFlag(cameraMaxFov != gShapeBaseDataProto.cameraMaxFov))
  591. stream->write(cameraMaxFov);
  592. stream->writeFlag(cameraCanBank);
  593. stream->writeFlag(mountedImagesBank);
  594. stream->writeString( debrisShapeName );
  595. stream->writeFlag(observeThroughObject);
  596. if( stream->writeFlag( debris != NULL ) )
  597. {
  598. stream->writeRangedU32(packed? SimObjectId((uintptr_t)debris):
  599. debris->getId(),DataBlockObjectIdFirst,DataBlockObjectIdLast);
  600. }
  601. stream->writeFlag(isInvincible);
  602. stream->writeFlag(renderWhenDestroyed);
  603. if( stream->writeFlag( explosion != NULL ) )
  604. {
  605. stream->writeRangedU32( explosion->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  606. }
  607. if( stream->writeFlag( underwaterExplosion != NULL ) )
  608. {
  609. stream->writeRangedU32( underwaterExplosion->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  610. }
  611. stream->writeFlag(inheritEnergyFromMount);
  612. stream->writeFlag(firstPersonOnly);
  613. stream->writeFlag(useEyePoint);
  614. if( stream->writeFlag( reflectorDesc != NULL ) )
  615. {
  616. stream->writeRangedU32( reflectorDesc->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  617. }
  618. //stream->write(reflectPriority);
  619. //stream->write(reflectMaxRateMs);
  620. //stream->write(reflectMinDist);
  621. //stream->write(reflectMaxDist);
  622. //stream->write(reflectDetailAdjust);
  623. }
  624. void ShapeBaseData::unpackData(BitStream* stream)
  625. {
  626. Parent::unpackData(stream);
  627. computeCRC = stream->readFlag();
  628. if(computeCRC)
  629. stream->read(&mCRC);
  630. shadowEnable = stream->readFlag();
  631. stream->read(&shadowSize);
  632. stream->read(&shadowMaxVisibleDistance);
  633. stream->read(&shadowProjectionDistance);
  634. stream->read(&shadowSphereAdjust);
  635. shapeName = stream->readSTString();
  636. cloakTexName = stream->readSTString();
  637. if(stream->readFlag())
  638. stream->read(&mass);
  639. else
  640. mass = gShapeBaseDataProto.mass;
  641. if(stream->readFlag())
  642. stream->read(&drag);
  643. else
  644. drag = gShapeBaseDataProto.drag;
  645. if(stream->readFlag())
  646. stream->read(&density);
  647. else
  648. density = gShapeBaseDataProto.density;
  649. if(stream->readFlag())
  650. stream->read(&maxEnergy);
  651. else
  652. maxEnergy = gShapeBaseDataProto.maxEnergy;
  653. if(stream->readFlag())
  654. stream->read(&cameraMaxDist);
  655. else
  656. cameraMaxDist = gShapeBaseDataProto.cameraMaxDist;
  657. if(stream->readFlag())
  658. stream->read(&cameraMinDist);
  659. else
  660. cameraMinDist = gShapeBaseDataProto.cameraMinDist;
  661. if(stream->readFlag())
  662. stream->read(&cameraDefaultFov);
  663. else
  664. cameraDefaultFov = gShapeBaseDataProto.cameraDefaultFov;
  665. if(stream->readFlag())
  666. stream->read(&cameraMinFov);
  667. else
  668. cameraMinFov = gShapeBaseDataProto.cameraMinFov;
  669. if(stream->readFlag())
  670. stream->read(&cameraMaxFov);
  671. else
  672. cameraMaxFov = gShapeBaseDataProto.cameraMaxFov;
  673. cameraCanBank = stream->readFlag();
  674. mountedImagesBank = stream->readFlag();
  675. debrisShapeName = stream->readSTString();
  676. observeThroughObject = stream->readFlag();
  677. if( stream->readFlag() )
  678. {
  679. debrisID = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  680. }
  681. isInvincible = stream->readFlag();
  682. renderWhenDestroyed = stream->readFlag();
  683. if( stream->readFlag() )
  684. {
  685. explosionID = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  686. }
  687. if( stream->readFlag() )
  688. {
  689. underwaterExplosionID = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  690. }
  691. inheritEnergyFromMount = stream->readFlag();
  692. firstPersonOnly = stream->readFlag();
  693. useEyePoint = stream->readFlag();
  694. if( stream->readFlag() )
  695. {
  696. cubeDescId = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  697. }
  698. //stream->read(&reflectPriority);
  699. //stream->read(&reflectMaxRateMs);
  700. //stream->read(&reflectMinDist);
  701. //stream->read(&reflectMaxDist);
  702. //stream->read(&reflectDetailAdjust);
  703. }
  704. //----------------------------------------------------------------------------
  705. //----------------------------------------------------------------------------
  706. Chunker<ShapeBase::CollisionTimeout> sTimeoutChunker;
  707. ShapeBase::CollisionTimeout* ShapeBase::sFreeTimeoutList = 0;
  708. //----------------------------------------------------------------------------
  709. IMPLEMENT_CO_NETOBJECT_V1(ShapeBase);
  710. ConsoleDocClass( ShapeBase,
  711. "@ingroup gameObjects"
  712. );
  713. IMPLEMENT_CALLBACK( ShapeBase, validateCameraFov, F32, (F32 fov), (fov),
  714. "@brief Called on the server when the client has requested a FOV change.\n\n"
  715. "When the client requests that its field of view should be changed (because "
  716. "they want to use a sniper scope, for example) this new FOV needs to be validated "
  717. "by the server. This method is called if it exists (it is optional) to validate "
  718. "the requested FOV, and modify it if necessary. This could be as simple as checking "
  719. "that the FOV falls within a correct range, to making sure that the FOV matches the "
  720. "capabilities of the current weapon.\n\n"
  721. "Following this method, ShapeBase ensures that the given FOV still falls within "
  722. "the datablock's cameraMinFov and cameraMaxFov. If that is good enough for your "
  723. "purposes, then you do not need to define the validateCameraFov() callback for "
  724. "your ShapeBase.\n\n"
  725. "@param fov The FOV that has been requested by the client.\n"
  726. "@return The FOV as validated by the server.\n\n"
  727. "@see ShapeBaseData\n\n");
  728. ShapeBase::ShapeBase()
  729. : mDrag( 0.0f ),
  730. mBuoyancy( 0.0f ),
  731. mWaterCoverage( 0.0f ),
  732. mLiquidHeight( 0.0f ),
  733. mControllingObject( NULL ),
  734. mGravityMod( 1.0f ),
  735. mAppliedForce( Point3F::Zero ),
  736. mTimeoutList( NULL ),
  737. mDataBlock( NULL ),
  738. mShapeInstance( NULL ),
  739. mEnergy( 0.0f ),
  740. mRechargeRate( 0.0f ),
  741. mDamage( 0.0f ),
  742. mRepairRate( 0.0f ),
  743. mRepairReserve( 0.0f ),
  744. mDamageState( Enabled ),
  745. mDamageThread( NULL ),
  746. mHulkThread( NULL ),
  747. mLastRenderFrame( 0 ),
  748. mLastRenderDistance( 0.0f ),
  749. mCloaked( false ),
  750. mCloakLevel( 0.0f ),
  751. mDamageFlash( 0.0f ),
  752. mWhiteOut( 0.0f ),
  753. mIsControlled( false ),
  754. mConvexList( new Convex ),
  755. mCameraFov( 90.0f ),
  756. mFadeOut( true ),
  757. mFading( false ),
  758. mFadeVal( 1.0f ),
  759. mFadeTime( 1.0f ),
  760. mFadeElapsedTime( 0.0f ),
  761. mFadeDelay( 0.0f ),
  762. mFlipFadeVal( false ),
  763. damageDir( 0.0f, 0.0f, 1.0f ),
  764. mShapeBaseMount( NULL ),
  765. mMass( 1.0f ),
  766. mOneOverMass( 1.0f ),
  767. mMoveMotion( false ),
  768. mIsAiControlled( false )
  769. {
  770. mTypeMask |= ShapeBaseObjectType | LightObjectType;
  771. S32 i;
  772. for (i = 0; i < MaxSoundThreads; i++) {
  773. mSoundThread[i].play = false;
  774. mSoundThread[i].profile = 0;
  775. mSoundThread[i].sound = 0;
  776. }
  777. for (i = 0; i < MaxScriptThreads; i++) {
  778. mScriptThread[i].sequence = -1;
  779. mScriptThread[i].thread = 0;
  780. mScriptThread[i].state = Thread::Stop;
  781. mScriptThread[i].atEnd = false;
  782. mScriptThread[i].timescale = 1.f;
  783. mScriptThread[i].position = -1.f;
  784. }
  785. for (i = 0; i < MaxTriggerKeys; i++)
  786. mTrigger[i] = false;
  787. }
  788. ShapeBase::~ShapeBase()
  789. {
  790. SAFE_DELETE( mConvexList );
  791. AssertFatal(mMount.link == 0,"ShapeBase::~ShapeBase: An object is still mounted");
  792. if( mShapeInstance && (mShapeInstance->getDebrisRefCount() == 0) )
  793. {
  794. delete mShapeInstance;
  795. mShapeInstance = NULL;
  796. }
  797. CollisionTimeout* ptr = mTimeoutList;
  798. while (ptr) {
  799. CollisionTimeout* cur = ptr;
  800. ptr = ptr->next;
  801. cur->next = sFreeTimeoutList;
  802. sFreeTimeoutList = cur;
  803. }
  804. }
  805. void ShapeBase::initPersistFields()
  806. {
  807. addProtectedField( "skin", TypeRealString, Offset(mAppliedSkinName, ShapeBase), &_setFieldSkin, &_getFieldSkin,
  808. "@brief The skin applied to the shape.\n\n"
  809. "'Skinning' the shape effectively renames the material targets, allowing "
  810. "different materials to be used on different instances of the same model. "
  811. "Using getSkinName() and setSkinName() is equivalent to reading and "
  812. "writing the skin field directly.\n\n"
  813. "Any material targets that start with the old skin name have that part "
  814. "of the name replaced with the new skin name. The initial old skin name is "
  815. "\"base\". For example, if a new skin of \"blue\" was applied to a model "
  816. "that had material targets <i>base_body</i> and <i>face</i>, the new targets "
  817. "would be <i>blue_body</i> and <i>face</i>. Note that <i>face</i> was not "
  818. "renamed since it did not start with the old skin name of \"base\".\n\n"
  819. "To support models that do not use the default \"base\" naming convention, "
  820. "you can also specify the part of the name to replace in the skin field "
  821. "itself. For example, if a model had a material target called <i>shapemat</i>, "
  822. "we could apply a new skin \"shape=blue\", and the material target would be "
  823. "renamed to <i>bluemat</i> (note \"shape\" has been replaced with \"blue\").\n\n"
  824. "Multiple skin updates can also be applied at the same time by separating "
  825. "them with a semicolon. For example: \"base=blue;face=happy_face\".\n\n"
  826. "Material targets are only renamed if an existing Material maps to that "
  827. "name, or if there is a diffuse texture in the model folder with the same "
  828. "name as the new target.\n\n" );
  829. addField( "isAiControlled", TypeBool, Offset(mIsAiControlled, ShapeBase),
  830. "@brief Is this object AI controlled.\n\n"
  831. "If True then this object is considered AI controlled and not player controlled.\n" );
  832. Parent::initPersistFields();
  833. }
  834. bool ShapeBase::_setFieldSkin( void *object, const char *index, const char *data )
  835. {
  836. ShapeBase* shape = static_cast<ShapeBase*>( object );
  837. if ( shape )
  838. shape->setSkinName( data );
  839. return false;
  840. }
  841. const char *ShapeBase::_getFieldSkin( void *object, const char *data )
  842. {
  843. ShapeBase* shape = static_cast<ShapeBase*>( object );
  844. return shape ? shape->getSkinName() : "";
  845. }
  846. //----------------------------------------------------------------------------
  847. bool ShapeBase::onAdd()
  848. {
  849. if( !Parent::onAdd() )
  850. return false;
  851. if( !mDataBlock )
  852. {
  853. Con::errorf( "ShapeBase::onAdd - no datablock on shape %i:%s (%s)",
  854. getId(), getClassName(), getName() );
  855. return false;
  856. }
  857. // Resolve sounds that arrived in the initial update
  858. S32 i;
  859. for (i = 0; i < MaxSoundThreads; i++)
  860. updateAudioState(mSoundThread[i]);
  861. for (i = 0; i < MaxScriptThreads; i++)
  862. {
  863. Thread& st = mScriptThread[i];
  864. if(st.thread)
  865. updateThread(st);
  866. }
  867. /*
  868. if(mDataBlock->cloakTexName != StringTable->insert(""))
  869. mCloakTexture = TextureHandle(mDataBlock->cloakTexName, MeshTexture, false);
  870. */
  871. return true;
  872. }
  873. void ShapeBase::onRemove()
  874. {
  875. mConvexList->nukeList();
  876. Parent::onRemove();
  877. // Stop any running sounds on the client
  878. if (isGhost())
  879. for (S32 i = 0; i < MaxSoundThreads; i++)
  880. stopAudio(i);
  881. if ( isClientObject() )
  882. {
  883. mCubeReflector.unregisterReflector();
  884. }
  885. }
  886. void ShapeBase::onSceneRemove()
  887. {
  888. mConvexList->nukeList();
  889. Parent::onSceneRemove();
  890. }
  891. bool ShapeBase::onNewDataBlock( GameBaseData *dptr, bool reload )
  892. {
  893. ShapeBaseData *prevDB = dynamic_cast<ShapeBaseData*>( mDataBlock );
  894. bool isInitialDataBlock = ( mDataBlock == 0 );
  895. if ( Parent::onNewDataBlock( dptr, reload ) == false )
  896. return false;
  897. mDataBlock = dynamic_cast<ShapeBaseData*>(dptr);
  898. if (!mDataBlock)
  899. return false;
  900. setMaskBits(DamageMask);
  901. mDamageThread = 0;
  902. mHulkThread = 0;
  903. // Even if loadShape succeeds, there may not actually be
  904. // a shape assigned to this object.
  905. if (bool(mDataBlock->mShape)) {
  906. delete mShapeInstance;
  907. mShapeInstance = new TSShapeInstance(mDataBlock->mShape, isClientObject());
  908. if (isClientObject())
  909. mShapeInstance->cloneMaterialList();
  910. mObjBox = mDataBlock->mShape->bounds;
  911. resetWorldBox();
  912. // Set the initial mesh hidden state.
  913. mMeshHidden.setSize( mDataBlock->mShape->objects.size() );
  914. mMeshHidden.clear();
  915. // Initialize the threads
  916. for (U32 i = 0; i < MaxScriptThreads; i++) {
  917. Thread& st = mScriptThread[i];
  918. if (st.sequence != -1) {
  919. // TG: Need to see about suppressing non-cyclic sounds
  920. // if the sequences were activated before the object was
  921. // ghosted.
  922. // TG: Cyclic animations need to have a random pos if
  923. // they were started before the object was ghosted.
  924. // If there was something running on the old shape, the thread
  925. // needs to be reset. Otherwise we assume that it's been
  926. // initialized either by the constructor or from the server.
  927. bool reset = st.thread != 0;
  928. st.thread = 0;
  929. // New datablock/shape may not actually HAVE this sequence.
  930. // In that case stop playing it.
  931. AssertFatal( prevDB != NULL, "ShapeBase::onNewDataBlock - how did you have a sequence playing without a prior datablock?" );
  932. const TSShape *prevShape = prevDB->mShape;
  933. const TSShape::Sequence &prevSeq = prevShape->sequences[st.sequence];
  934. const String &prevSeqName = prevShape->names[prevSeq.nameIndex];
  935. st.sequence = mDataBlock->mShape->findSequence( prevSeqName );
  936. if ( st.sequence != -1 )
  937. {
  938. setThreadSequence( i, st.sequence, reset );
  939. }
  940. }
  941. }
  942. if (mDataBlock->damageSequence != -1) {
  943. mDamageThread = mShapeInstance->addThread();
  944. mShapeInstance->setSequence(mDamageThread,
  945. mDataBlock->damageSequence,0);
  946. }
  947. if (mDataBlock->hulkSequence != -1) {
  948. mHulkThread = mShapeInstance->addThread();
  949. mShapeInstance->setSequence(mHulkThread,
  950. mDataBlock->hulkSequence,0);
  951. }
  952. if( isGhost() )
  953. {
  954. // Reapply the current skin
  955. mAppliedSkinName = "";
  956. reSkin();
  957. }
  958. }
  959. //
  960. mEnergy = 0;
  961. mDamage = 0;
  962. mDamageState = Enabled;
  963. mRepairReserve = 0;
  964. updateMass();
  965. updateDamageLevel();
  966. updateDamageState();
  967. mDrag = mDataBlock->drag;
  968. mCameraFov = mDataBlock->cameraDefaultFov;
  969. updateMass();
  970. if( !isInitialDataBlock && mLightPlugin )
  971. mLightPlugin->reset();
  972. if ( isClientObject() )
  973. {
  974. mCubeReflector.unregisterReflector();
  975. if ( mDataBlock->reflectorDesc )
  976. mCubeReflector.registerReflector( this, mDataBlock->reflectorDesc );
  977. }
  978. return true;
  979. }
  980. void ShapeBase::onDeleteNotify( SimObject *obj )
  981. {
  982. if ( obj == mCurrentWaterObject )
  983. mCurrentWaterObject = NULL;
  984. Parent::onDeleteNotify( obj );
  985. }
  986. void ShapeBase::onImpact(SceneObject* obj, VectorF vec)
  987. {
  988. if (!isGhost())
  989. mDataBlock->onImpact_callback( this, obj, vec, vec.len() );
  990. }
  991. void ShapeBase::onImpact(VectorF vec)
  992. {
  993. if (!isGhost())
  994. mDataBlock->onImpact_callback( this, NULL, vec, vec.len() );
  995. }
  996. //----------------------------------------------------------------------------
  997. void ShapeBase::processTick(const Move* move)
  998. {
  999. PROFILE_SCOPE( ShapeBase_ProcessTick );
  1000. // Energy management
  1001. if (mDamageState == Enabled && mDataBlock->inheritEnergyFromMount == false) {
  1002. F32 store = mEnergy;
  1003. mEnergy += mRechargeRate;
  1004. if (mEnergy > mDataBlock->maxEnergy)
  1005. mEnergy = mDataBlock->maxEnergy;
  1006. else
  1007. if (mEnergy < 0)
  1008. mEnergy = 0;
  1009. // Virtual setEnergyLevel is used here by some derived classes to
  1010. // decide whether they really want to set the energy mask bit.
  1011. if (mEnergy != store)
  1012. setEnergyLevel(mEnergy);
  1013. }
  1014. // Repair management
  1015. if (mDataBlock->isInvincible == false)
  1016. {
  1017. F32 store = mDamage;
  1018. mDamage -= mRepairRate;
  1019. mDamage = mClampF(mDamage, 0.f, mDataBlock->maxDamage);
  1020. if (mRepairReserve > mDamage)
  1021. mRepairReserve = mDamage;
  1022. if (mRepairReserve > 0.0)
  1023. {
  1024. F32 rate = getMin(mDataBlock->repairRate, mRepairReserve);
  1025. mDamage -= rate;
  1026. mRepairReserve -= rate;
  1027. }
  1028. if (store != mDamage)
  1029. {
  1030. updateDamageLevel();
  1031. if (isServerObject()) {
  1032. setMaskBits(DamageMask);
  1033. mDataBlock->onDamage_callback( this, mDamage - store );
  1034. }
  1035. }
  1036. }
  1037. if (isServerObject()) {
  1038. // Server only...
  1039. advanceThreads(TickSec);
  1040. updateServerAudio();
  1041. // update wet state
  1042. setImageWetState(0, mWaterCoverage > 0.4); // more than 40 percent covered
  1043. // update motion state
  1044. mMoveMotion = false;
  1045. if (move && (move->x || move->y || move->z))
  1046. {
  1047. mMoveMotion = true;
  1048. }
  1049. for (S32 i = 0; i < MaxMountedImages; i++)
  1050. {
  1051. setImageMotionState(i, mMoveMotion);
  1052. }
  1053. if(mFading)
  1054. {
  1055. F32 dt = TickMs / 1000.0f;
  1056. F32 newFadeET = mFadeElapsedTime + dt;
  1057. if(mFadeElapsedTime < mFadeDelay && newFadeET >= mFadeDelay)
  1058. setMaskBits(CloakMask);
  1059. mFadeElapsedTime = newFadeET;
  1060. if(mFadeElapsedTime > mFadeTime + mFadeDelay)
  1061. {
  1062. mFadeVal = F32(!mFadeOut);
  1063. mFading = false;
  1064. }
  1065. }
  1066. }
  1067. // Advance images
  1068. if (isServerObject())
  1069. {
  1070. for (S32 i = 0; i < MaxMountedImages; i++)
  1071. {
  1072. if (mMountedImageList[i].dataBlock)
  1073. updateImageState(i, TickSec);
  1074. }
  1075. }
  1076. // Call script on trigger state changes
  1077. if (move && mDataBlock && isServerObject())
  1078. {
  1079. for (S32 i = 0; i < MaxTriggerKeys; i++)
  1080. {
  1081. if (move->trigger[i] != mTrigger[i])
  1082. {
  1083. mTrigger[i] = move->trigger[i];
  1084. mDataBlock->onTrigger_callback( this, i, move->trigger[i] );
  1085. }
  1086. }
  1087. }
  1088. // Update the damage flash and the whiteout
  1089. //
  1090. if (mDamageFlash > 0.0)
  1091. {
  1092. mDamageFlash -= sDamageFlashDec;
  1093. if (mDamageFlash <= 0.0)
  1094. mDamageFlash = 0.0;
  1095. }
  1096. if (mWhiteOut > 0.0)
  1097. {
  1098. mWhiteOut -= sWhiteoutDec;
  1099. if (mWhiteOut <= 0.0)
  1100. mWhiteOut = 0.0;
  1101. }
  1102. }
  1103. void ShapeBase::advanceTime(F32 dt)
  1104. {
  1105. // On the client, the shape threads and images are
  1106. // advanced at framerate.
  1107. advanceThreads(dt);
  1108. updateAudioPos();
  1109. for (S32 i = 0; i < MaxMountedImages; i++)
  1110. if (mMountedImageList[i].dataBlock)
  1111. {
  1112. updateImageState(i, dt);
  1113. updateImageAnimation(i, dt);
  1114. }
  1115. // Cloaking
  1116. if (mCloaked && mCloakLevel != 1.0) {
  1117. if (sCloakSpeed <= 0.0f)
  1118. {
  1119. // Instantaneous
  1120. mCloakLevel = 1.0;
  1121. }
  1122. else
  1123. {
  1124. // Over time determined by sCloakSpeed
  1125. mCloakLevel += dt / sCloakSpeed;
  1126. if (mCloakLevel >= 1.0)
  1127. mCloakLevel = 1.0;
  1128. }
  1129. } else if (!mCloaked && mCloakLevel != 0.0) {
  1130. if (sCloakSpeed <= 0.0f)
  1131. {
  1132. // Instantaneous
  1133. mCloakLevel = 0.0;
  1134. }
  1135. else
  1136. {
  1137. // Over time determined by sCloakSpeed
  1138. mCloakLevel -= dt / sCloakSpeed;
  1139. if (mCloakLevel <= 0.0)
  1140. mCloakLevel = 0.0;
  1141. }
  1142. }
  1143. if(mFading)
  1144. {
  1145. mFadeElapsedTime += dt;
  1146. if(mFadeElapsedTime > mFadeTime)
  1147. {
  1148. mFadeVal = F32(!mFadeOut);
  1149. mFading = false;
  1150. }
  1151. else
  1152. {
  1153. mFadeVal = mFadeElapsedTime / mFadeTime;
  1154. if(mFadeOut)
  1155. mFadeVal = 1 - mFadeVal;
  1156. }
  1157. }
  1158. }
  1159. void ShapeBase::setControllingClient( GameConnection* client )
  1160. {
  1161. if( isGhost() && gSFX3DWorld )
  1162. {
  1163. if( gSFX3DWorld->getListener() == this && !client && getControllingClient() && getControllingClient()->isConnectionToServer() )
  1164. {
  1165. // We are the current listener and are no longer a controller object on the
  1166. // connection, so clear our listener status.
  1167. gSFX3DWorld->setListener( NULL );
  1168. }
  1169. else if( client && client->isConnectionToServer() && !getControllingObject() )
  1170. {
  1171. // We're on the local client and not controlled by another object, so make
  1172. // us the current SFX listener.
  1173. gSFX3DWorld->setListener( this );
  1174. }
  1175. }
  1176. Parent::setControllingClient( client );
  1177. // Update all of the mounted images' shapes so they may
  1178. // optimize their animation threads.
  1179. for (U32 i=0; i<MaxMountedImages; ++i)
  1180. {
  1181. MountedImage& image = mMountedImageList[i];
  1182. image.updateDoAnimateAllShapes( this );
  1183. }
  1184. }
  1185. void ShapeBase::setControllingObject(ShapeBase* obj)
  1186. {
  1187. if (obj) {
  1188. setProcessTick(false);
  1189. // Even though we don't processTick, we still need to
  1190. // process after the controller in case anyone is mounted
  1191. // on this object.
  1192. processAfter(obj);
  1193. }
  1194. else {
  1195. setProcessTick(true);
  1196. clearProcessAfter();
  1197. // Catch the case of the controlling object actually
  1198. // mounted on this object.
  1199. if (mControllingObject->mMount.object == this)
  1200. mControllingObject->processAfter(this);
  1201. }
  1202. mControllingObject = obj;
  1203. }
  1204. ShapeBase* ShapeBase::getControlObject()
  1205. {
  1206. return 0;
  1207. }
  1208. void ShapeBase::setControlObject(ShapeBase*)
  1209. {
  1210. }
  1211. bool ShapeBase::isFirstPerson() const
  1212. {
  1213. // Always first person as far as the server is concerned.
  1214. if (!isGhost())
  1215. return true;
  1216. if (const GameConnection* con = getControllingClient())
  1217. return con->getControlObject() == this && con->isFirstPerson();
  1218. return false;
  1219. }
  1220. bool ShapeBase::isValidCameraFov(F32 fov)
  1221. {
  1222. return((fov >= mDataBlock->cameraMinFov) && (fov <= mDataBlock->cameraMaxFov));
  1223. }
  1224. void ShapeBase::setCameraFov(F32 fov)
  1225. {
  1226. // On server allow for script side checking
  1227. if ( !isGhost() && isMethod( "validateCameraFov" ) )
  1228. {
  1229. fov = validateCameraFov_callback( fov );
  1230. }
  1231. mCameraFov = mClampF(fov, mDataBlock->cameraMinFov, mDataBlock->cameraMaxFov);
  1232. }
  1233. void ShapeBase::onCameraScopeQuery(NetConnection *cr, CameraScopeQuery * query)
  1234. {
  1235. // update the camera query
  1236. query->camera = this;
  1237. if(GameConnection * con = dynamic_cast<GameConnection*>(cr))
  1238. {
  1239. // get the fov from the connection (in deg)
  1240. F32 fov;
  1241. if (con->getControlCameraFov(&fov))
  1242. {
  1243. query->fov = mDegToRad(fov/2);
  1244. query->sinFov = mSin(query->fov);
  1245. query->cosFov = mCos(query->fov);
  1246. }
  1247. }
  1248. // use eye rather than camera transform (good enough and faster)
  1249. MatrixF eyeTransform;
  1250. getEyeTransform(&eyeTransform);
  1251. eyeTransform.getColumn(3, &query->pos);
  1252. eyeTransform.getColumn(1, &query->orientation);
  1253. // Get the visible distance.
  1254. if (getSceneManager() != NULL)
  1255. query->visibleDistance = getSceneManager()->getVisibleDistance();
  1256. Parent::onCameraScopeQuery( cr, query );
  1257. }
  1258. //----------------------------------------------------------------------------
  1259. F32 ShapeBase::getEnergyLevel()
  1260. {
  1261. if ( mDataBlock->inheritEnergyFromMount && mShapeBaseMount )
  1262. return mShapeBaseMount->getEnergyLevel();
  1263. return mEnergy;
  1264. }
  1265. F32 ShapeBase::getEnergyValue()
  1266. {
  1267. if ( mDataBlock->inheritEnergyFromMount && mShapeBaseMount )
  1268. {
  1269. F32 maxEnergy = mShapeBaseMount->mDataBlock->maxEnergy;
  1270. if ( maxEnergy > 0.f )
  1271. return (mShapeBaseMount->getEnergyLevel() / maxEnergy);
  1272. }
  1273. else
  1274. {
  1275. F32 maxEnergy = mDataBlock->maxEnergy;
  1276. if ( maxEnergy > 0.f )
  1277. return (mEnergy / mDataBlock->maxEnergy);
  1278. }
  1279. return 0.0f;
  1280. }
  1281. void ShapeBase::setEnergyLevel(F32 energy)
  1282. {
  1283. if (mDataBlock->inheritEnergyFromMount == false || !mShapeBaseMount) {
  1284. if (mDamageState == Enabled) {
  1285. mEnergy = (energy > mDataBlock->maxEnergy)?
  1286. mDataBlock->maxEnergy: (energy < 0)? 0: energy;
  1287. }
  1288. } else {
  1289. // Pass the set onto whatever we're mounted to...
  1290. if ( mShapeBaseMount )
  1291. {
  1292. mShapeBaseMount->setEnergyLevel(energy);
  1293. }
  1294. }
  1295. }
  1296. void ShapeBase::setDamageLevel(F32 damage)
  1297. {
  1298. if (!mDataBlock->isInvincible) {
  1299. F32 store = mDamage;
  1300. mDamage = mClampF(damage, 0.f, mDataBlock->maxDamage);
  1301. if (store != mDamage) {
  1302. updateDamageLevel();
  1303. if (isServerObject()) {
  1304. setMaskBits(DamageMask);
  1305. mDataBlock->onDamage_callback( this, mDamage - store );
  1306. }
  1307. }
  1308. }
  1309. }
  1310. void ShapeBase::updateContainer()
  1311. {
  1312. PROFILE_SCOPE( ShapeBase_updateContainer );
  1313. // Update container drag and buoyancy properties
  1314. // Set default values.
  1315. mDrag = mDataBlock->drag;
  1316. mBuoyancy = 0.0f;
  1317. mGravityMod = 1.0;
  1318. mAppliedForce.set(0,0,0);
  1319. ContainerQueryInfo info;
  1320. info.box = getWorldBox();
  1321. info.mass = getMass();
  1322. mContainer->findObjects(info.box, WaterObjectType|PhysicalZoneObjectType,findRouter,&info);
  1323. mWaterCoverage = info.waterCoverage;
  1324. mLiquidType = info.liquidType;
  1325. mLiquidHeight = info.waterHeight;
  1326. setCurrentWaterObject( info.waterObject );
  1327. // This value might be useful as a datablock value,
  1328. // This is what allows the player to stand in shallow water (below this coverage)
  1329. // without jiggling from buoyancy
  1330. if (mWaterCoverage >= 0.25f)
  1331. {
  1332. // water viscosity is used as drag for in water.
  1333. // ShapeBaseData drag is used for drag outside of water.
  1334. // Combine these two components to calculate this ShapeBase object's
  1335. // current drag.
  1336. mDrag = ( info.waterCoverage * info.waterViscosity ) +
  1337. ( 1.0f - info.waterCoverage ) * mDataBlock->drag;
  1338. mBuoyancy = (info.waterDensity / mDataBlock->density) * info.waterCoverage;
  1339. }
  1340. mAppliedForce = info.appliedForce;
  1341. mGravityMod = info.gravityScale;
  1342. //Con::printf( "WaterCoverage: %f", mWaterCoverage );
  1343. //Con::printf( "Drag: %f", mDrag );
  1344. }
  1345. //----------------------------------------------------------------------------
  1346. void ShapeBase::applyRepair(F32 amount)
  1347. {
  1348. // Repair increases the repair reserve
  1349. if (amount > 0 && ((mRepairReserve += amount) > mDamage))
  1350. mRepairReserve = mDamage;
  1351. }
  1352. void ShapeBase::applyDamage(F32 amount)
  1353. {
  1354. if (amount > 0)
  1355. setDamageLevel(mDamage + amount);
  1356. }
  1357. F32 ShapeBase::getDamageValue()
  1358. {
  1359. // Return a 0-1 damage value.
  1360. return mDamage / mDataBlock->maxDamage;
  1361. }
  1362. F32 ShapeBase::getMaxDamage()
  1363. {
  1364. return mDataBlock->maxDamage;
  1365. }
  1366. void ShapeBase::updateDamageLevel()
  1367. {
  1368. if (mDamageThread) {
  1369. // mDamage is already 0-1 on the client
  1370. if (mDamage >= mDataBlock->destroyedLevel) {
  1371. if (getDamageState() == Destroyed)
  1372. mShapeInstance->setPos(mDamageThread, 0);
  1373. else
  1374. mShapeInstance->setPos(mDamageThread, 1);
  1375. } else {
  1376. mShapeInstance->setPos(mDamageThread, mDamage / mDataBlock->destroyedLevel);
  1377. }
  1378. }
  1379. }
  1380. //----------------------------------------------------------------------------
  1381. void ShapeBase::setDamageState(DamageState state)
  1382. {
  1383. if (mDamageState == state)
  1384. return;
  1385. bool invokeCallback = false;
  1386. const char* lastState = 0;
  1387. if (!isGhost()) {
  1388. if (state != getDamageState())
  1389. setMaskBits(DamageMask);
  1390. lastState = getDamageStateName();
  1391. switch (state) {
  1392. case Destroyed: {
  1393. if (mDamageState == Enabled)
  1394. {
  1395. setDamageState(Disabled);
  1396. // It is possible that the setDamageState() call above has already
  1397. // upgraded our state to Destroyed. If that is the case, no need
  1398. // to continue.
  1399. if (mDamageState == state)
  1400. return;
  1401. }
  1402. invokeCallback = true;
  1403. break;
  1404. }
  1405. case Disabled:
  1406. if (mDamageState == Enabled)
  1407. invokeCallback = true;
  1408. break;
  1409. case Enabled:
  1410. invokeCallback = true;
  1411. break;
  1412. default:
  1413. AssertFatal(false, "Invalid damage state");
  1414. break;
  1415. }
  1416. }
  1417. mDamageState = state;
  1418. if (mDamageState != Enabled) {
  1419. mRepairReserve = 0;
  1420. mEnergy = 0;
  1421. }
  1422. if (invokeCallback) {
  1423. // Like to call the scripts after the state has been intialize.
  1424. // This should only end up being called on the server.
  1425. switch (state) {
  1426. case Destroyed:
  1427. mDataBlock->onDestroyed_callback( this, lastState );
  1428. break;
  1429. case Disabled:
  1430. mDataBlock->onDisabled_callback( this, lastState );
  1431. break;
  1432. case Enabled:
  1433. mDataBlock->onEnabled_callback( this, lastState );
  1434. break;
  1435. default:
  1436. break;
  1437. }
  1438. }
  1439. updateDamageState();
  1440. updateDamageLevel();
  1441. }
  1442. bool ShapeBase::setDamageState(const char* state)
  1443. {
  1444. for (S32 i = 0; i < NumDamageStates; i++)
  1445. if (!dStricmp(state,sDamageStateName[i])) {
  1446. setDamageState(DamageState(i));
  1447. return true;
  1448. }
  1449. return false;
  1450. }
  1451. const char* ShapeBase::getDamageStateName()
  1452. {
  1453. return sDamageStateName[mDamageState];
  1454. }
  1455. void ShapeBase::updateDamageState()
  1456. {
  1457. if (mHulkThread) {
  1458. F32 pos = (mDamageState == Destroyed) ? 1.0f : 0.0f;
  1459. if (mShapeInstance->getPos(mHulkThread) != pos) {
  1460. mShapeInstance->setPos(mHulkThread,pos);
  1461. if (isClientObject())
  1462. mShapeInstance->animate();
  1463. }
  1464. }
  1465. }
  1466. //----------------------------------------------------------------------------
  1467. void ShapeBase::blowUp()
  1468. {
  1469. Point3F center;
  1470. mObjBox.getCenter(&center);
  1471. center += getPosition();
  1472. MatrixF trans = getTransform();
  1473. trans.setPosition( center );
  1474. // explode
  1475. Explosion* pExplosion = NULL;
  1476. if( pointInWater( (Point3F &)center ) && mDataBlock->underwaterExplosion )
  1477. {
  1478. pExplosion = new Explosion;
  1479. pExplosion->onNewDataBlock(mDataBlock->underwaterExplosion, false);
  1480. }
  1481. else
  1482. {
  1483. if (mDataBlock->explosion)
  1484. {
  1485. pExplosion = new Explosion;
  1486. pExplosion->onNewDataBlock(mDataBlock->explosion, false);
  1487. }
  1488. }
  1489. if( pExplosion )
  1490. {
  1491. pExplosion->setTransform(trans);
  1492. pExplosion->setInitialState(center, damageDir);
  1493. if (pExplosion->registerObject() == false)
  1494. {
  1495. Con::errorf(ConsoleLogEntry::General, "ShapeBase(%s)::explode: couldn't register explosion",
  1496. mDataBlock->getName() );
  1497. delete pExplosion;
  1498. pExplosion = NULL;
  1499. }
  1500. }
  1501. TSShapeInstance *debShape = NULL;
  1502. if( mDataBlock->debrisShape == NULL )
  1503. {
  1504. return;
  1505. }
  1506. else
  1507. {
  1508. debShape = new TSShapeInstance( mDataBlock->debrisShape, true);
  1509. }
  1510. Vector< TSPartInstance * > partList;
  1511. TSPartInstance::breakShape( debShape, 0, partList, NULL, NULL, 0 );
  1512. if( !mDataBlock->debris )
  1513. {
  1514. mDataBlock->debris = new DebrisData;
  1515. }
  1516. // cycle through partlist and create debris pieces
  1517. for( U32 i=0; i<partList.size(); i++ )
  1518. {
  1519. //Point3F axis( 0.0, 0.0, 1.0 );
  1520. Point3F randomDir = MathUtils::randomDir( damageDir, 0, 50 );
  1521. Debris *debris = new Debris;
  1522. debris->setPartInstance( partList[i] );
  1523. debris->init( center, randomDir );
  1524. debris->onNewDataBlock( mDataBlock->debris, false );
  1525. debris->setTransform( trans );
  1526. if( !debris->registerObject() )
  1527. {
  1528. Con::warnf( ConsoleLogEntry::General, "Could not register debris for class: %s", mDataBlock->getName() );
  1529. delete debris;
  1530. debris = NULL;
  1531. }
  1532. else
  1533. {
  1534. debShape->incDebrisRefCount();
  1535. }
  1536. }
  1537. damageDir.set(0, 0, 1);
  1538. }
  1539. //----------------------------------------------------------------------------
  1540. void ShapeBase::onMount( SceneObject *obj, S32 node )
  1541. {
  1542. mConvexList->nukeList();
  1543. // Are we mounting to a ShapeBase object?
  1544. mShapeBaseMount = dynamic_cast<ShapeBase*>( obj );
  1545. Parent::onMount( obj, node );
  1546. }
  1547. void ShapeBase::onUnmount( SceneObject *obj, S32 node )
  1548. {
  1549. Parent::onUnmount( obj, node );
  1550. mShapeBaseMount = NULL;
  1551. }
  1552. Point3F ShapeBase::getAIRepairPoint()
  1553. {
  1554. if (mDataBlock->mountPointNode[ShapeBaseData::AIRepairNode] < 0)
  1555. return Point3F(0, 0, 0);
  1556. MatrixF xf(true);
  1557. getMountTransform( ShapeBaseData::AIRepairNode, MatrixF::Identity, &xf );
  1558. Point3F pos(0, 0, 0);
  1559. xf.getColumn(3,&pos);
  1560. return pos;
  1561. }
  1562. //----------------------------------------------------------------------------
  1563. void ShapeBase::getEyeTransform(MatrixF* mat)
  1564. {
  1565. getEyeBaseTransform(mat, true);
  1566. }
  1567. void ShapeBase::getEyeBaseTransform(MatrixF* mat, bool includeBank)
  1568. {
  1569. // Returns eye to world space transform
  1570. S32 eyeNode = mDataBlock->eyeNode;
  1571. if (eyeNode != -1)
  1572. mat->mul(getTransform(), mShapeInstance->mNodeTransforms[eyeNode]);
  1573. else
  1574. *mat = getTransform();
  1575. }
  1576. void ShapeBase::getRenderEyeTransform(MatrixF* mat)
  1577. {
  1578. getRenderEyeBaseTransform(mat, true);
  1579. }
  1580. void ShapeBase::getRenderEyeBaseTransform(MatrixF* mat, bool includeBank)
  1581. {
  1582. // Returns eye to world space transform
  1583. S32 eyeNode = mDataBlock->eyeNode;
  1584. if (eyeNode != -1)
  1585. mat->mul(getRenderTransform(), mShapeInstance->mNodeTransforms[eyeNode]);
  1586. else
  1587. *mat = getRenderTransform();
  1588. }
  1589. void ShapeBase::getCameraTransform(F32* pos,MatrixF* mat)
  1590. {
  1591. // Returns camera to world space transform
  1592. // Handles first person / third person camera position
  1593. if (isServerObject() && mShapeInstance)
  1594. mShapeInstance->animateNodeSubtrees(true);
  1595. if (*pos != 0)
  1596. {
  1597. F32 min,max;
  1598. Point3F offset;
  1599. MatrixF eye,rot;
  1600. getCameraParameters(&min,&max,&offset,&rot);
  1601. getRenderEyeTransform(&eye);
  1602. mat->mul(eye,rot);
  1603. // Use the eye transform to orient the camera
  1604. VectorF vp,vec;
  1605. vp.x = vp.z = 0;
  1606. vp.y = -(max - min) * *pos;
  1607. eye.mulV(vp,&vec);
  1608. VectorF minVec;
  1609. vp.y = -min;
  1610. eye.mulV( vp, &minVec );
  1611. // Use the camera node's pos.
  1612. Point3F osp,sp;
  1613. if (mDataBlock->cameraNode != -1) {
  1614. mShapeInstance->mNodeTransforms[mDataBlock->cameraNode].getColumn(3,&osp);
  1615. // Scale the camera position before applying the transform
  1616. const Point3F& scale = getScale();
  1617. osp.convolve( scale );
  1618. getRenderTransform().mulP(osp,&sp);
  1619. }
  1620. else
  1621. getRenderTransform().getColumn(3,&sp);
  1622. // Make sure we don't extend the camera into anything solid
  1623. Point3F ep = sp + minVec + vec + offset;
  1624. disableCollision();
  1625. if (isMounted())
  1626. getObjectMount()->disableCollision();
  1627. RayInfo collision;
  1628. if( mContainer && mContainer->castRay(sp, ep,
  1629. (0xFFFFFFFF & ~(WaterObjectType |
  1630. GameBaseObjectType |
  1631. TriggerObjectType |
  1632. DefaultObjectType)),
  1633. &collision) == true) {
  1634. F32 vecLenSq = vec.lenSquared();
  1635. F32 adj = (-mDot(vec, collision.normal) / vecLenSq) * 0.1;
  1636. F32 newPos = getMax(0.0f, collision.t - adj);
  1637. if (newPos == 0.0f)
  1638. eye.getColumn(3,&ep);
  1639. else
  1640. ep = sp + offset + (vec * newPos);
  1641. }
  1642. mat->setColumn(3,ep);
  1643. if (isMounted())
  1644. getObjectMount()->enableCollision();
  1645. enableCollision();
  1646. }
  1647. else
  1648. {
  1649. getRenderEyeTransform(mat);
  1650. }
  1651. // Apply Camera FX.
  1652. mat->mul( gCamFXMgr.getTrans() );
  1653. }
  1654. void ShapeBase::getCameraParameters(F32 *min,F32* max,Point3F* off,MatrixF* rot)
  1655. {
  1656. *min = mDataBlock->cameraMinDist;
  1657. *max = mDataBlock->cameraMaxDist;
  1658. off->set(0,0,0);
  1659. rot->identity();
  1660. }
  1661. //----------------------------------------------------------------------------
  1662. F32 ShapeBase::getDamageFlash() const
  1663. {
  1664. return mDamageFlash;
  1665. }
  1666. void ShapeBase::setDamageFlash(const F32 flash)
  1667. {
  1668. mDamageFlash = flash;
  1669. if (mDamageFlash < 0.0)
  1670. mDamageFlash = 0;
  1671. else if (mDamageFlash > 1.0)
  1672. mDamageFlash = 1.0;
  1673. }
  1674. //----------------------------------------------------------------------------
  1675. F32 ShapeBase::getWhiteOut() const
  1676. {
  1677. return mWhiteOut;
  1678. }
  1679. void ShapeBase::setWhiteOut(const F32 flash)
  1680. {
  1681. mWhiteOut = flash;
  1682. if (mWhiteOut < 0.0)
  1683. mWhiteOut = 0;
  1684. else if (mWhiteOut > 1.5)
  1685. mWhiteOut = 1.5;
  1686. }
  1687. //----------------------------------------------------------------------------
  1688. bool ShapeBase::onlyFirstPerson() const
  1689. {
  1690. return mDataBlock->firstPersonOnly;
  1691. }
  1692. bool ShapeBase::useObjsEyePoint() const
  1693. {
  1694. return mDataBlock->useEyePoint;
  1695. }
  1696. //----------------------------------------------------------------------------
  1697. void ShapeBase::setVelocity(const VectorF&)
  1698. {
  1699. }
  1700. void ShapeBase::applyImpulse(const Point3F&,const VectorF&)
  1701. {
  1702. }
  1703. //----------------------------------------------------------------------------
  1704. void ShapeBase::playAudio(U32 slot,SFXTrack* profile)
  1705. {
  1706. AssertFatal( slot < MaxSoundThreads, "ShapeBase::playAudio() bad slot index" );
  1707. Sound& st = mSoundThread[slot];
  1708. if( profile && ( !st.play || st.profile != profile ) )
  1709. {
  1710. setMaskBits(SoundMaskN << slot);
  1711. st.play = true;
  1712. st.profile = profile;
  1713. updateAudioState(st);
  1714. }
  1715. }
  1716. void ShapeBase::stopAudio(U32 slot)
  1717. {
  1718. AssertFatal( slot < MaxSoundThreads, "ShapeBase::stopAudio() bad slot index" );
  1719. Sound& st = mSoundThread[slot];
  1720. if ( st.play )
  1721. {
  1722. st.play = false;
  1723. setMaskBits(SoundMaskN << slot);
  1724. updateAudioState(st);
  1725. }
  1726. }
  1727. void ShapeBase::updateServerAudio()
  1728. {
  1729. // Timeout non-looping sounds
  1730. for (S32 i = 0; i < MaxSoundThreads; i++) {
  1731. Sound& st = mSoundThread[i];
  1732. if (st.play && st.timeout && st.timeout < Sim::getCurrentTime()) {
  1733. clearMaskBits(SoundMaskN << i);
  1734. st.play = false;
  1735. }
  1736. }
  1737. }
  1738. void ShapeBase::updateAudioState(Sound& st)
  1739. {
  1740. SFX_DELETE( st.sound );
  1741. if ( st.play && st.profile )
  1742. {
  1743. if ( isGhost() )
  1744. {
  1745. if ( Sim::findObject( SimObjectId((uintptr_t)st.profile), st.profile ) )
  1746. {
  1747. st.sound = SFX->createSource( st.profile, &getTransform() );
  1748. if ( st.sound )
  1749. st.sound->play();
  1750. }
  1751. else
  1752. st.play = false;
  1753. }
  1754. else
  1755. {
  1756. // Non-looping sounds timeout on the server
  1757. st.timeout = 0;
  1758. if ( !st.profile->getDescription()->mIsLooping )
  1759. st.timeout = Sim::getCurrentTime() + sAudioTimeout;
  1760. }
  1761. }
  1762. else
  1763. st.play = false;
  1764. }
  1765. void ShapeBase::updateAudioPos()
  1766. {
  1767. for (S32 i = 0; i < MaxSoundThreads; i++)
  1768. {
  1769. SFXSource* source = mSoundThread[i].sound;
  1770. if ( source )
  1771. source->setTransform( getTransform() );
  1772. }
  1773. }
  1774. //----------------------------------------------------------------------------
  1775. const char *ShapeBase::getThreadSequenceName( U32 slot )
  1776. {
  1777. Thread& st = mScriptThread[slot];
  1778. if ( st.sequence == -1 )
  1779. {
  1780. // Invalid Animation.
  1781. return "";
  1782. }
  1783. // Name Index
  1784. const U32 nameIndex = getShape()->sequences[st.sequence].nameIndex;
  1785. // Return Name.
  1786. return getShape()->getName( nameIndex );
  1787. }
  1788. bool ShapeBase::setThreadSequence(U32 slot, S32 seq, bool reset)
  1789. {
  1790. Thread& st = mScriptThread[slot];
  1791. if (st.thread && st.sequence == seq && st.state == Thread::Play)
  1792. return true;
  1793. // Handle a -1 sequence, as this may be set when a thread has been destroyed.
  1794. if(seq == -1)
  1795. return true;
  1796. if (seq < MaxSequenceIndex) {
  1797. setMaskBits(ThreadMaskN << slot);
  1798. st.sequence = seq;
  1799. if (reset) {
  1800. st.state = Thread::Play;
  1801. st.atEnd = false;
  1802. st.timescale = 1.f;
  1803. st.position = 0.f;
  1804. }
  1805. if (mShapeInstance) {
  1806. if (!st.thread)
  1807. st.thread = mShapeInstance->addThread();
  1808. mShapeInstance->setSequence(st.thread,seq,st.position);
  1809. updateThread(st);
  1810. }
  1811. return true;
  1812. }
  1813. return false;
  1814. }
  1815. void ShapeBase::updateThread(Thread& st)
  1816. {
  1817. switch (st.state)
  1818. {
  1819. case Thread::Stop:
  1820. {
  1821. mShapeInstance->setTimeScale( st.thread, 1.f );
  1822. mShapeInstance->setPos( st.thread, ( st.timescale > 0.f ) ? 1.0f : 0.0f );
  1823. } // Drop through to pause state
  1824. case Thread::Pause:
  1825. {
  1826. mShapeInstance->setTimeScale( st.thread, 0.f );
  1827. } break;
  1828. case Thread::Play:
  1829. {
  1830. if (st.atEnd)
  1831. {
  1832. mShapeInstance->setTimeScale(st.thread,1);
  1833. mShapeInstance->setPos( st.thread, ( st.timescale > 0.f ) ? 1.0f : 0.0f );
  1834. mShapeInstance->setTimeScale(st.thread,0);
  1835. st.state = Thread::Stop;
  1836. }
  1837. else
  1838. {
  1839. if ( st.position != -1.f )
  1840. {
  1841. mShapeInstance->setTimeScale( st.thread, 1.f );
  1842. mShapeInstance->setPos( st.thread, st.position );
  1843. }
  1844. mShapeInstance->setTimeScale(st.thread, st.timescale );
  1845. }
  1846. } break;
  1847. case Thread::Destroy:
  1848. {
  1849. st.atEnd = true;
  1850. st.sequence = -1;
  1851. if(st.thread)
  1852. {
  1853. mShapeInstance->destroyThread(st.thread);
  1854. st.thread = 0;
  1855. }
  1856. } break;
  1857. }
  1858. }
  1859. bool ShapeBase::stopThread(U32 slot)
  1860. {
  1861. Thread& st = mScriptThread[slot];
  1862. if (st.sequence != -1 && st.state != Thread::Stop) {
  1863. setMaskBits(ThreadMaskN << slot);
  1864. st.state = Thread::Stop;
  1865. updateThread(st);
  1866. return true;
  1867. }
  1868. return false;
  1869. }
  1870. bool ShapeBase::destroyThread(U32 slot)
  1871. {
  1872. Thread& st = mScriptThread[slot];
  1873. if (st.sequence != -1 && st.state != Thread::Destroy) {
  1874. setMaskBits(ThreadMaskN << slot);
  1875. st.state = Thread::Destroy;
  1876. updateThread(st);
  1877. return true;
  1878. }
  1879. return false;
  1880. }
  1881. bool ShapeBase::pauseThread(U32 slot)
  1882. {
  1883. Thread& st = mScriptThread[slot];
  1884. if (st.sequence != -1 && st.state != Thread::Pause) {
  1885. setMaskBits(ThreadMaskN << slot);
  1886. st.state = Thread::Pause;
  1887. updateThread(st);
  1888. return true;
  1889. }
  1890. return false;
  1891. }
  1892. bool ShapeBase::playThread(U32 slot)
  1893. {
  1894. Thread& st = mScriptThread[slot];
  1895. if (st.sequence != -1 && st.state != Thread::Play) {
  1896. setMaskBits(ThreadMaskN << slot);
  1897. st.state = Thread::Play;
  1898. updateThread(st);
  1899. return true;
  1900. }
  1901. return false;
  1902. }
  1903. bool ShapeBase::setThreadPosition( U32 slot, F32 pos )
  1904. {
  1905. Thread& st = mScriptThread[slot];
  1906. if (st.sequence != -1)
  1907. {
  1908. setMaskBits(ThreadMaskN << slot);
  1909. st.position = pos;
  1910. st.atEnd = false;
  1911. updateThread(st);
  1912. return true;
  1913. }
  1914. return false;
  1915. }
  1916. bool ShapeBase::setThreadDir(U32 slot,bool forward)
  1917. {
  1918. Thread& st = mScriptThread[slot];
  1919. if (st.sequence != -1)
  1920. {
  1921. if ( ( st.timescale >= 0.f ) != forward )
  1922. {
  1923. setMaskBits(ThreadMaskN << slot);
  1924. st.timescale *= -1.f ;
  1925. st.atEnd = false;
  1926. updateThread(st);
  1927. }
  1928. return true;
  1929. }
  1930. return false;
  1931. }
  1932. bool ShapeBase::setThreadTimeScale( U32 slot, F32 timeScale )
  1933. {
  1934. Thread& st = mScriptThread[slot];
  1935. if (st.sequence != -1)
  1936. {
  1937. if (st.timescale != timeScale)
  1938. {
  1939. setMaskBits(ThreadMaskN << slot);
  1940. st.timescale = timeScale;
  1941. updateThread(st);
  1942. }
  1943. return true;
  1944. }
  1945. return false;
  1946. }
  1947. void ShapeBase::advanceThreads(F32 dt)
  1948. {
  1949. for (U32 i = 0; i < MaxScriptThreads; i++) {
  1950. Thread& st = mScriptThread[i];
  1951. if (st.thread) {
  1952. if (!mShapeInstance->getShape()->sequences[st.sequence].isCyclic() && !st.atEnd &&
  1953. ( ( st.timescale > 0.f )? mShapeInstance->getPos(st.thread) >= 1.0:
  1954. mShapeInstance->getPos(st.thread) <= 0)) {
  1955. st.atEnd = true;
  1956. updateThread(st);
  1957. if (!isGhost()) {
  1958. mDataBlock->onEndSequence_callback(this, i, this->getThreadSequenceName(i));
  1959. }
  1960. }
  1961. // Make sure the thread is still valid after the call to onEndSequence_callback().
  1962. // Someone could have called destroyThread() while in there.
  1963. if(st.thread)
  1964. {
  1965. mShapeInstance->advanceTime(dt,st.thread);
  1966. st.position = mShapeInstance->getPos(st.thread);
  1967. }
  1968. }
  1969. }
  1970. }
  1971. //----------------------------------------------------------------------------
  1972. /// Emit particles on the given emitter, if we're within triggerHeight above some static surface with a
  1973. /// material that has 'showDust' set to true. The particles will have a lifetime of 'numMilliseconds'
  1974. /// and be emitted at the given offset from the contact point having a direction of 'axis'.
  1975. void ShapeBase::emitDust( ParticleEmitter* emitter, F32 triggerHeight, const Point3F& offset, U32 numMilliseconds, const Point3F& axis )
  1976. {
  1977. if( !emitter )
  1978. return;
  1979. Point3F startPos = getPosition();
  1980. Point3F endPos = startPos + Point3F( 0.0, 0.0, - triggerHeight );
  1981. RayInfo rayInfo;
  1982. if( getContainer()->castRay( startPos, endPos, STATIC_COLLISION_TYPEMASK, &rayInfo ) )
  1983. {
  1984. Material* material = ( rayInfo.material ? dynamic_cast< Material* >( rayInfo.material->getMaterial() ) : 0 );
  1985. if( material && material->mShowDust )
  1986. {
  1987. ColorF colorList[ ParticleData::PDC_NUM_KEYS ];
  1988. for( U32 x = 0; x < getMin( Material::NUM_EFFECT_COLOR_STAGES, ParticleData::PDC_NUM_KEYS ); ++ x )
  1989. colorList[ x ] = material->mEffectColor[ x ];
  1990. for( U32 x = Material::NUM_EFFECT_COLOR_STAGES; x < ParticleData::PDC_NUM_KEYS; ++ x )
  1991. colorList[ x ].set( 1.0, 1.0, 1.0, 0.0 );
  1992. emitter->setColors( colorList );
  1993. Point3F contactPoint = rayInfo.point + offset;
  1994. emitter->emitParticles( contactPoint, true, ( axis == Point3F::Zero ? rayInfo.normal : axis ),
  1995. getVelocity(), numMilliseconds );
  1996. }
  1997. }
  1998. }
  1999. //----------------------------------------------------------------------------
  2000. TSShape const* ShapeBase::getShape()
  2001. {
  2002. return mShapeInstance? mShapeInstance->getShape(): 0;
  2003. }
  2004. void ShapeBase::prepRenderImage( SceneRenderState *state )
  2005. {
  2006. _prepRenderImage( state, true, true );
  2007. }
  2008. void ShapeBase::_prepRenderImage( SceneRenderState *state,
  2009. bool renderSelf,
  2010. bool renderMountedImages )
  2011. {
  2012. PROFILE_SCOPE( ShapeBase_PrepRenderImage );
  2013. //if ( mIsCubemapUpdate )
  2014. // return false;
  2015. if( ( getDamageState() == Destroyed ) && ( !mDataBlock->renderWhenDestroyed ) )
  2016. return;
  2017. // We don't need to render if all the meshes are forced hidden.
  2018. if ( mMeshHidden.getSize() > 0 && mMeshHidden.testAll() )
  2019. return;
  2020. // If we're rendering shadows don't render the mounted
  2021. // images unless the shape is also rendered.
  2022. if ( state->isShadowPass() && !renderSelf )
  2023. return;
  2024. // If we're currently rendering our own reflection we
  2025. // don't want to render ourselves into it.
  2026. if ( mCubeReflector.isRendering() )
  2027. return;
  2028. // We force all the shapes to use the highest detail
  2029. // if we're the control object or mounted.
  2030. bool forceHighestDetail = false;
  2031. {
  2032. GameConnection *con = GameConnection::getConnectionToServer();
  2033. ShapeBase *co = NULL;
  2034. if(con && ( (co = dynamic_cast<ShapeBase*>(con->getControlObject())) != NULL) )
  2035. {
  2036. if(co == this || co->getObjectMount() == this)
  2037. forceHighestDetail = true;
  2038. }
  2039. }
  2040. mLastRenderFrame = sLastRenderFrame;
  2041. // get shape detail...we might not even need to be drawn
  2042. Point3F cameraOffset = getWorldBox().getClosestPoint( state->getDiffuseCameraPosition() ) - state->getDiffuseCameraPosition();
  2043. F32 dist = cameraOffset.len();
  2044. if (dist < 0.01f)
  2045. dist = 0.01f;
  2046. F32 invScale = (1.0f/getMax(getMax(mObjScale.x,mObjScale.y),mObjScale.z));
  2047. if (mShapeInstance)
  2048. {
  2049. if ( forceHighestDetail )
  2050. mShapeInstance->setCurrentDetail( 0 );
  2051. else
  2052. mShapeInstance->setDetailFromDistance( state, dist * invScale );
  2053. mShapeInstance->animate();
  2054. }
  2055. if ( ( mShapeInstance && mShapeInstance->getCurrentDetail() < 0 ) ||
  2056. ( !mShapeInstance && !gShowBoundingBox ) )
  2057. {
  2058. // no, don't draw anything
  2059. return;
  2060. }
  2061. if( renderMountedImages )
  2062. {
  2063. for (U32 i = 0; i < MaxMountedImages; i++)
  2064. {
  2065. MountedImage& image = mMountedImageList[i];
  2066. U32 imageShapeIndex = getImageShapeIndex(image);
  2067. if (image.dataBlock && image.shapeInstance[imageShapeIndex])
  2068. {
  2069. // Select detail levels on mounted items but... always
  2070. // draw the control object's mounted images in high detail.
  2071. if ( forceHighestDetail )
  2072. image.shapeInstance[imageShapeIndex]->setCurrentDetail( 0 );
  2073. else
  2074. image.shapeInstance[imageShapeIndex]->setDetailFromDistance( state, dist * invScale );
  2075. if (!mIsZero( (1.0f - mCloakLevel) * mFadeVal))
  2076. {
  2077. prepBatchRender( state, i );
  2078. // Debug rendering of the mounted shape bounds.
  2079. if ( gShowBoundingBox )
  2080. {
  2081. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  2082. ri->renderDelegate.bind( this, &ShapeBase::_renderBoundingBox );
  2083. ri->objectIndex = i;
  2084. ri->type = RenderPassManager::RIT_Editor;
  2085. state->getRenderPass()->addInst( ri );
  2086. }
  2087. }
  2088. }
  2089. }
  2090. }
  2091. // Debug rendering of the shape bounding box.
  2092. if ( gShowBoundingBox )
  2093. {
  2094. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  2095. ri->renderDelegate.bind( this, &ShapeBase::_renderBoundingBox );
  2096. ri->objectIndex = -1;
  2097. ri->type = RenderPassManager::RIT_Editor;
  2098. state->getRenderPass()->addInst( ri );
  2099. }
  2100. if ( mShapeInstance && renderSelf )
  2101. prepBatchRender( state, -1 );
  2102. calcClassRenderData();
  2103. }
  2104. //----------------------------------------------------------------------------
  2105. // prepBatchRender
  2106. //----------------------------------------------------------------------------
  2107. void ShapeBase::prepBatchRender(SceneRenderState* state, S32 mountedImageIndex )
  2108. {
  2109. // CHANGES IN HERE SHOULD BE DUPLICATED IN TSSTATIC!
  2110. GFXTransformSaver saver;
  2111. // Set up our TS render state.
  2112. TSRenderState rdata;
  2113. rdata.setSceneState( state );
  2114. if ( mCubeReflector.isEnabled() )
  2115. rdata.setCubemap( mCubeReflector.getCubemap() );
  2116. rdata.setFadeOverride( (1.0f - mCloakLevel) * mFadeVal );
  2117. // We might have some forward lit materials
  2118. // so pass down a query to gather lights.
  2119. LightQuery query;
  2120. query.init( getWorldSphere() );
  2121. rdata.setLightQuery( &query );
  2122. if( mountedImageIndex != -1 )
  2123. {
  2124. MountedImage& image = mMountedImageList[mountedImageIndex];
  2125. if( image.dataBlock && image.shapeInstance )
  2126. {
  2127. renderMountedImage( mountedImageIndex, rdata, state );
  2128. }
  2129. }
  2130. else
  2131. {
  2132. MatrixF mat = getRenderTransform();
  2133. mat.scale( mObjScale );
  2134. GFX->setWorldMatrix( mat );
  2135. if ( state->isDiffusePass() && mCubeReflector.isEnabled() && mCubeReflector.getOcclusionQuery() )
  2136. {
  2137. RenderPassManager *pass = state->getRenderPass();
  2138. OccluderRenderInst *ri = pass->allocInst<OccluderRenderInst>();
  2139. ri->type = RenderPassManager::RIT_Occluder;
  2140. ri->query = mCubeReflector.getOcclusionQuery();
  2141. mObjToWorld.mulP( mObjBox.getCenter(), &ri->position );
  2142. ri->scale.set( mObjBox.getExtents() );
  2143. ri->orientation = pass->allocUniqueXform( mObjToWorld );
  2144. ri->isSphere = false;
  2145. state->getRenderPass()->addInst( ri );
  2146. }
  2147. mShapeInstance->animate();
  2148. mShapeInstance->render( rdata );
  2149. }
  2150. }
  2151. void ShapeBase::renderMountedImage( U32 imageSlot, TSRenderState &rstate, SceneRenderState *state )
  2152. {
  2153. GFX->pushWorldMatrix();
  2154. MatrixF mat;
  2155. getRenderImageTransform(imageSlot, &mat, rstate.getSceneState()->isShadowPass());
  2156. GFX->setWorldMatrix( mat );
  2157. MountedImage& image = mMountedImageList[imageSlot];
  2158. U32 imageShapeIndex = getImageShapeIndex(image);
  2159. image.shapeInstance[imageShapeIndex]->animate();
  2160. image.shapeInstance[imageShapeIndex]->render( rstate );
  2161. GFX->popWorldMatrix();
  2162. }
  2163. void ShapeBase::_renderBoundingBox( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  2164. {
  2165. // If we got an override mat then this some
  2166. // special rendering pass... skip out of it.
  2167. if ( overrideMat )
  2168. return;
  2169. GFXStateBlockDesc desc;
  2170. desc.setZReadWrite( true, false );
  2171. desc.setBlend( true );
  2172. desc.fillMode = GFXFillWireframe;
  2173. GFXDrawUtil *drawer = GFX->getDrawUtil();
  2174. if ( ri->objectIndex != -1 )
  2175. {
  2176. MountedImage &image = mMountedImageList[ ri->objectIndex ];
  2177. if ( image.shapeInstance )
  2178. {
  2179. MatrixF mat;
  2180. getRenderImageTransform( ri->objectIndex, &mat );
  2181. const Box3F &objBox = image.shapeInstance[getImageShapeIndex(image)]->getShape()->bounds;
  2182. drawer->drawCube( desc, objBox, ColorI( 255, 255, 255 ), &mat );
  2183. }
  2184. }
  2185. else
  2186. drawer->drawCube( desc, mObjBox, ColorI( 255, 255, 255 ), &mRenderObjToWorld );
  2187. }
  2188. bool ShapeBase::castRay(const Point3F &start, const Point3F &end, RayInfo* info)
  2189. {
  2190. if (mShapeInstance)
  2191. {
  2192. RayInfo shortest;
  2193. shortest.t = 1e8;
  2194. info->object = NULL;
  2195. for (U32 i = 0; i < mDataBlock->LOSDetails.size(); i++)
  2196. {
  2197. mShapeInstance->animate(mDataBlock->LOSDetails[i]);
  2198. if (mShapeInstance->castRay(start, end, info, mDataBlock->LOSDetails[i]))
  2199. {
  2200. info->object = this;
  2201. if (info->t < shortest.t)
  2202. shortest = *info;
  2203. }
  2204. }
  2205. if (info->object == this)
  2206. {
  2207. // Copy out the shortest time...
  2208. *info = shortest;
  2209. return true;
  2210. }
  2211. }
  2212. return false;
  2213. }
  2214. bool ShapeBase::castRayRendered(const Point3F &start, const Point3F &end, RayInfo* info)
  2215. {
  2216. if (mShapeInstance)
  2217. {
  2218. RayInfo localInfo;
  2219. mShapeInstance->animate();
  2220. bool res = mShapeInstance->castRayRendered(start, end, &localInfo, mShapeInstance->getCurrentDetail());
  2221. if (res)
  2222. {
  2223. *info = localInfo;
  2224. info->object = this;
  2225. return true;
  2226. }
  2227. }
  2228. return false;
  2229. }
  2230. //----------------------------------------------------------------------------
  2231. bool ShapeBase::buildPolyList(PolyListContext context, AbstractPolyList* polyList, const Box3F &, const SphereF &)
  2232. {
  2233. if ( !mShapeInstance )
  2234. return false;
  2235. polyList->setTransform(&mObjToWorld, mObjScale);
  2236. polyList->setObject(this);
  2237. if ( context == PLC_Selection )
  2238. {
  2239. mShapeInstance->animate();
  2240. mShapeInstance->buildPolyList(polyList,mShapeInstance->getCurrentDetail());
  2241. return true;
  2242. }
  2243. else if ( context == PLC_Export )
  2244. {
  2245. // Try to call on the client so we can export materials
  2246. ShapeBase* exportObj = this;
  2247. if ( isServerObject() && getClientObject() )
  2248. exportObj = dynamic_cast<ShapeBase*>(getClientObject());
  2249. S32 dl = 0;
  2250. exportObj->mShapeInstance->animate();
  2251. exportObj->mShapeInstance->buildPolyList(polyList, dl);
  2252. return true;
  2253. }
  2254. else
  2255. {
  2256. bool ret = false;
  2257. for (U32 i = 0; i < mDataBlock->collisionDetails.size(); i++)
  2258. {
  2259. mShapeInstance->buildPolyList(polyList,mDataBlock->collisionDetails[i]);
  2260. ret = true;
  2261. }
  2262. return ret;
  2263. }
  2264. }
  2265. void ShapeBase::buildConvex(const Box3F& box, Convex* convex)
  2266. {
  2267. if (mShapeInstance == NULL)
  2268. return;
  2269. // These should really come out of a pool
  2270. mConvexList->collectGarbage();
  2271. Box3F realBox = box;
  2272. mWorldToObj.mul(realBox);
  2273. realBox.minExtents.convolveInverse(mObjScale);
  2274. realBox.maxExtents.convolveInverse(mObjScale);
  2275. if (realBox.isOverlapped(getObjBox()) == false)
  2276. return;
  2277. for (U32 i = 0; i < mDataBlock->collisionDetails.size(); i++)
  2278. {
  2279. Box3F newbox = mDataBlock->collisionBounds[i];
  2280. newbox.minExtents.convolve(mObjScale);
  2281. newbox.maxExtents.convolve(mObjScale);
  2282. mObjToWorld.mul(newbox);
  2283. if (box.isOverlapped(newbox) == false)
  2284. continue;
  2285. // See if this hull exists in the working set already...
  2286. Convex* cc = 0;
  2287. CollisionWorkingList& wl = convex->getWorkingList();
  2288. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext) {
  2289. if (itr->mConvex->getType() == ShapeBaseConvexType &&
  2290. (static_cast<ShapeBaseConvex*>(itr->mConvex)->pShapeBase == this &&
  2291. static_cast<ShapeBaseConvex*>(itr->mConvex)->hullId == i)) {
  2292. cc = itr->mConvex;
  2293. break;
  2294. }
  2295. }
  2296. if (cc)
  2297. continue;
  2298. // Create a new convex.
  2299. ShapeBaseConvex* cp = new ShapeBaseConvex;
  2300. mConvexList->registerObject(cp);
  2301. convex->addToWorkingList(cp);
  2302. cp->mObject = this;
  2303. cp->pShapeBase = this;
  2304. cp->hullId = i;
  2305. cp->box = mDataBlock->collisionBounds[i];
  2306. cp->transform = 0;
  2307. cp->findNodeTransform();
  2308. }
  2309. }
  2310. //----------------------------------------------------------------------------
  2311. void ShapeBase::queueCollision( SceneObject *obj, const VectorF &vec)
  2312. {
  2313. // Add object to list of collisions.
  2314. SimTime time = Sim::getCurrentTime();
  2315. S32 num = obj->getId();
  2316. CollisionTimeout** adr = &mTimeoutList;
  2317. CollisionTimeout* ptr = mTimeoutList;
  2318. while (ptr) {
  2319. if (ptr->objectNumber == num) {
  2320. if (ptr->expireTime < time) {
  2321. ptr->expireTime = time + CollisionTimeoutValue;
  2322. ptr->object = obj;
  2323. ptr->vector = vec;
  2324. }
  2325. return;
  2326. }
  2327. // Recover expired entries
  2328. if (ptr->expireTime < time) {
  2329. CollisionTimeout* cur = ptr;
  2330. *adr = ptr->next;
  2331. ptr = ptr->next;
  2332. cur->next = sFreeTimeoutList;
  2333. sFreeTimeoutList = cur;
  2334. }
  2335. else {
  2336. adr = &ptr->next;
  2337. ptr = ptr->next;
  2338. }
  2339. }
  2340. // New entry for the object
  2341. if (sFreeTimeoutList != NULL)
  2342. {
  2343. ptr = sFreeTimeoutList;
  2344. sFreeTimeoutList = ptr->next;
  2345. ptr->next = NULL;
  2346. }
  2347. else
  2348. {
  2349. ptr = sTimeoutChunker.alloc();
  2350. }
  2351. ptr->object = obj;
  2352. ptr->objectNumber = obj->getId();
  2353. ptr->vector = vec;
  2354. ptr->expireTime = time + CollisionTimeoutValue;
  2355. ptr->next = mTimeoutList;
  2356. mTimeoutList = ptr;
  2357. }
  2358. void ShapeBase::notifyCollision()
  2359. {
  2360. // Notify all the objects that were just stamped during the queueing
  2361. // process.
  2362. SimTime expireTime = Sim::getCurrentTime() + CollisionTimeoutValue;
  2363. for (CollisionTimeout* ptr = mTimeoutList; ptr; ptr = ptr->next)
  2364. {
  2365. if (ptr->expireTime == expireTime && ptr->object)
  2366. {
  2367. SimObjectPtr<SceneObject> safePtr(ptr->object);
  2368. SimObjectPtr<ShapeBase> safeThis(this);
  2369. onCollision(ptr->object,ptr->vector);
  2370. ptr->object = 0;
  2371. if(!bool(safeThis))
  2372. return;
  2373. if(bool(safePtr))
  2374. safePtr->onCollision(this,ptr->vector);
  2375. if(!bool(safeThis))
  2376. return;
  2377. }
  2378. }
  2379. }
  2380. void ShapeBase::onCollision( SceneObject *object, const VectorF &vec )
  2381. {
  2382. if (!isGhost())
  2383. mDataBlock->onCollision_callback( this, object, vec, vec.len() );
  2384. }
  2385. //--------------------------------------------------------------------------
  2386. bool ShapeBase::pointInWater( Point3F &point )
  2387. {
  2388. if ( mCurrentWaterObject == NULL )
  2389. return false;
  2390. return mCurrentWaterObject->isUnderwater( point );
  2391. }
  2392. //----------------------------------------------------------------------------
  2393. void ShapeBase::writePacketData(GameConnection *connection, BitStream *stream)
  2394. {
  2395. Parent::writePacketData(connection, stream);
  2396. stream->write(getEnergyLevel());
  2397. stream->write(mRechargeRate);
  2398. }
  2399. void ShapeBase::readPacketData(GameConnection *connection, BitStream *stream)
  2400. {
  2401. Parent::readPacketData(connection, stream);
  2402. F32 energy;
  2403. stream->read(&energy);
  2404. setEnergyLevel(energy);
  2405. stream->read(&mRechargeRate);
  2406. }
  2407. F32 ShapeBase::getUpdatePriority(CameraScopeQuery *camInfo, U32 updateMask, S32 updateSkips)
  2408. {
  2409. // If it's the scope object, must be high priority
  2410. if (camInfo->camera == this) {
  2411. // Most priorities are between 0 and 1, so this
  2412. // should be something larger.
  2413. return 10.0f;
  2414. }
  2415. if( camInfo->camera )
  2416. {
  2417. ShapeBase* camera = dynamic_cast< ShapeBase* >( camInfo->camera );
  2418. // see if the camera is mounted to this...
  2419. // if it is, this should have a high priority
  2420. if( camera && camera->getObjectMount() == this)
  2421. return 10.0f;
  2422. }
  2423. return Parent::getUpdatePriority(camInfo, updateMask, updateSkips);
  2424. }
  2425. U32 ShapeBase::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  2426. {
  2427. U32 retMask = Parent::packUpdate(con, mask, stream);
  2428. if (mask & InitialUpdateMask) {
  2429. // mask off sounds that aren't playing
  2430. S32 i;
  2431. for (i = 0; i < MaxSoundThreads; i++)
  2432. if (!mSoundThread[i].play)
  2433. mask &= ~(SoundMaskN << i);
  2434. // mask off threads that aren't running
  2435. for (i = 0; i < MaxScriptThreads; i++)
  2436. if (mScriptThread[i].sequence == -1)
  2437. mask &= ~(ThreadMaskN << i);
  2438. // mask off images that aren't updated
  2439. for(i = 0; i < MaxMountedImages; i++)
  2440. if(!mMountedImageList[i].dataBlock)
  2441. mask &= ~(ImageMaskN << i);
  2442. }
  2443. if(!stream->writeFlag(mask & (NameMask | DamageMask | SoundMask | MeshHiddenMask |
  2444. ThreadMask | ImageMask | CloakMask | SkinMask)))
  2445. return retMask;
  2446. if (stream->writeFlag(mask & DamageMask)) {
  2447. stream->writeFloat(mClampF(mDamage / mDataBlock->maxDamage, 0.f, 1.f), DamageLevelBits);
  2448. stream->writeInt(mDamageState,NumDamageStateBits);
  2449. stream->writeNormalVector( damageDir, 8 );
  2450. }
  2451. if (stream->writeFlag(mask & ThreadMask)) {
  2452. for (S32 i = 0; i < MaxScriptThreads; i++) {
  2453. Thread& st = mScriptThread[i];
  2454. if (stream->writeFlag( (st.sequence != -1 || st.state == Thread::Destroy) && (mask & (ThreadMaskN << i)) ) ) {
  2455. stream->writeInt(st.sequence,ThreadSequenceBits);
  2456. stream->writeInt(st.state,2);
  2457. stream->write(st.timescale);
  2458. stream->write(st.position);
  2459. stream->writeFlag(st.atEnd);
  2460. }
  2461. }
  2462. }
  2463. if (stream->writeFlag(mask & SoundMask)) {
  2464. for (S32 i = 0; i < MaxSoundThreads; i++) {
  2465. Sound& st = mSoundThread[i];
  2466. if (stream->writeFlag(mask & (SoundMaskN << i)))
  2467. if (stream->writeFlag(st.play))
  2468. stream->writeRangedU32(st.profile->getId(),DataBlockObjectIdFirst,
  2469. DataBlockObjectIdLast);
  2470. }
  2471. }
  2472. if (stream->writeFlag(mask & ImageMask)) {
  2473. for (S32 i = 0; i < MaxMountedImages; i++)
  2474. if (stream->writeFlag(mask & (ImageMaskN << i))) {
  2475. MountedImage& image = mMountedImageList[i];
  2476. if (stream->writeFlag(image.dataBlock))
  2477. stream->writeInt(image.dataBlock->getId() - DataBlockObjectIdFirst,
  2478. DataBlockObjectIdBitSize);
  2479. con->packNetStringHandleU(stream, image.skinNameHandle);
  2480. con->packNetStringHandleU(stream, image.scriptAnimPrefix);
  2481. // Used to force the 1st person rendering on the client. This is required
  2482. // as this object could be ghosted to the client prior to its controlling client
  2483. // being set. Therefore there is a network tick when the object is in limbo...
  2484. stream->writeFlag(image.dataBlock && image.dataBlock->animateAllShapes && getControllingClient() == con);
  2485. stream->writeFlag(image.wet);
  2486. stream->writeFlag(image.motion);
  2487. stream->writeFlag(image.ammo);
  2488. stream->writeFlag(image.loaded);
  2489. stream->writeFlag(image.target);
  2490. stream->writeFlag(image.triggerDown);
  2491. stream->writeFlag(image.altTriggerDown);
  2492. for (U32 j=0; j<ShapeBaseImageData::MaxGenericTriggers; ++j)
  2493. {
  2494. stream->writeFlag(image.genericTrigger[j]);
  2495. }
  2496. stream->writeInt(image.fireCount,3);
  2497. stream->writeInt(image.altFireCount,3);
  2498. stream->writeInt(image.reloadCount,3);
  2499. stream->writeFlag(isImageFiring(i));
  2500. stream->writeFlag(isImageAltFiring(i));
  2501. stream->writeFlag(isImageReloading(i));
  2502. }
  2503. }
  2504. // Group some of the uncommon stuff together.
  2505. if (stream->writeFlag(mask & (NameMask | CloakMask | SkinMask | MeshHiddenMask ))) {
  2506. if (stream->writeFlag(mask & CloakMask))
  2507. {
  2508. // cloaking
  2509. stream->writeFlag( mCloaked );
  2510. // piggyback control update
  2511. stream->writeFlag(bool(getControllingClient()));
  2512. // fading
  2513. if(stream->writeFlag(mFading && mFadeElapsedTime >= mFadeDelay)) {
  2514. stream->writeFlag(mFadeOut);
  2515. stream->write(mFadeTime);
  2516. }
  2517. else
  2518. stream->writeFlag(mFadeVal == 1.0f);
  2519. }
  2520. if (stream->writeFlag(mask & NameMask)) {
  2521. con->packNetStringHandleU(stream, mShapeNameHandle);
  2522. }
  2523. if ( stream->writeFlag( mask & MeshHiddenMask ) )
  2524. stream->writeBits( mMeshHidden );
  2525. if (stream->writeFlag(mask & SkinMask))
  2526. con->packNetStringHandleU(stream, mSkinNameHandle);
  2527. }
  2528. return retMask;
  2529. }
  2530. void ShapeBase::unpackUpdate(NetConnection *con, BitStream *stream)
  2531. {
  2532. Parent::unpackUpdate(con, stream);
  2533. mLastRenderFrame = sLastRenderFrame; // make sure we get a process after the event...
  2534. if(!stream->readFlag())
  2535. return;
  2536. if (stream->readFlag()) {
  2537. mDamage = mClampF(stream->readFloat(DamageLevelBits) * mDataBlock->maxDamage, 0.f, mDataBlock->maxDamage);
  2538. DamageState prevState = mDamageState;
  2539. mDamageState = DamageState(stream->readInt(NumDamageStateBits));
  2540. stream->readNormalVector( &damageDir, 8 );
  2541. if (prevState != Destroyed && mDamageState == Destroyed && isProperlyAdded())
  2542. blowUp();
  2543. updateDamageLevel();
  2544. updateDamageState();
  2545. }
  2546. if (stream->readFlag()) {
  2547. for (S32 i = 0; i < MaxScriptThreads; i++) {
  2548. if (stream->readFlag()) {
  2549. Thread& st = mScriptThread[i];
  2550. U32 seq = stream->readInt(ThreadSequenceBits);
  2551. st.state = Thread::State(stream->readInt(2));
  2552. stream->read( &st.timescale );
  2553. stream->read( &st.position );
  2554. st.atEnd = stream->readFlag();
  2555. if (st.sequence != seq && st.state != Thread::Destroy)
  2556. setThreadSequence(i,seq,false);
  2557. else
  2558. updateThread(st);
  2559. }
  2560. }
  2561. }
  2562. if ( stream->readFlag() )
  2563. {
  2564. for ( S32 i = 0; i < MaxSoundThreads; i++ )
  2565. {
  2566. if ( stream->readFlag() )
  2567. {
  2568. Sound& st = mSoundThread[i];
  2569. st.play = stream->readFlag();
  2570. if ( st.play )
  2571. {
  2572. st.profile = (SFXTrack*) stream->readRangedU32( DataBlockObjectIdFirst,
  2573. DataBlockObjectIdLast );
  2574. }
  2575. if ( isProperlyAdded() )
  2576. updateAudioState( st );
  2577. }
  2578. }
  2579. }
  2580. // Mounted Images
  2581. if (stream->readFlag()) {
  2582. for (S32 i = 0; i < MaxMountedImages; i++) {
  2583. if (stream->readFlag()) {
  2584. MountedImage& image = mMountedImageList[i];
  2585. ShapeBaseImageData* imageData = 0;
  2586. if (stream->readFlag()) {
  2587. SimObjectId id = stream->readInt(DataBlockObjectIdBitSize) +
  2588. DataBlockObjectIdFirst;
  2589. if (!Sim::findObject(id,imageData)) {
  2590. con->setLastError("Invalid packet (mounted images).");
  2591. return;
  2592. }
  2593. }
  2594. NetStringHandle skinDesiredNameHandle = con->unpackNetStringHandleU(stream);
  2595. NetStringHandle scriptDesiredAnimPrefix = con->unpackNetStringHandleU(stream);
  2596. image.forceAnimateAllShapes = stream->readFlag();
  2597. image.wet = stream->readFlag();
  2598. image.motion = stream->readFlag();
  2599. image.ammo = stream->readFlag();
  2600. image.loaded = stream->readFlag();
  2601. image.target = stream->readFlag();
  2602. image.triggerDown = stream->readFlag();
  2603. image.altTriggerDown = stream->readFlag();
  2604. for (U32 j=0; j<ShapeBaseImageData::MaxGenericTriggers; ++j)
  2605. {
  2606. image.genericTrigger[j] = stream->readFlag();
  2607. }
  2608. S32 count = stream->readInt(3);
  2609. S32 altCount = stream->readInt(3);
  2610. S32 reloadCount = stream->readInt(3);
  2611. bool datablockChange = image.dataBlock != imageData;
  2612. if (datablockChange || (image.skinNameHandle != skinDesiredNameHandle))
  2613. {
  2614. MountedImage& image = mMountedImageList[i];
  2615. image.scriptAnimPrefix = scriptDesiredAnimPrefix;
  2616. setImage( i, imageData,
  2617. skinDesiredNameHandle, image.loaded,
  2618. image.ammo, image.triggerDown, image.altTriggerDown,
  2619. image.motion, image.genericTrigger[0], image.genericTrigger[1], image.genericTrigger[2], image.genericTrigger[3],
  2620. image.target);
  2621. }
  2622. if (!datablockChange && image.scriptAnimPrefix != scriptDesiredAnimPrefix)
  2623. {
  2624. // We don't have a new image, but we do have a new script anim prefix to work with.
  2625. // Notify the image of this change.
  2626. MountedImage& image = mMountedImageList[i];
  2627. image.scriptAnimPrefix = scriptDesiredAnimPrefix;
  2628. updateAnimThread(i, getImageShapeIndex(image));
  2629. }
  2630. bool isFiring = stream->readFlag();
  2631. bool isAltFiring = stream->readFlag();
  2632. bool isReloading = stream->readFlag();
  2633. if (isProperlyAdded()) {
  2634. // Normal processing
  2635. bool processFiring = false;
  2636. if (count != image.fireCount)
  2637. {
  2638. image.fireCount = count;
  2639. setImageState(i,getImageFireState(i),true);
  2640. processFiring = true;
  2641. }
  2642. else if (altCount != image.altFireCount)
  2643. {
  2644. image.altFireCount = altCount;
  2645. setImageState(i,getImageAltFireState(i),true);
  2646. processFiring = true;
  2647. }
  2648. else if (reloadCount != image.reloadCount)
  2649. {
  2650. image.reloadCount = reloadCount;
  2651. setImageState(i,getImageReloadState(i),true);
  2652. }
  2653. if (processFiring && imageData)
  2654. {
  2655. if ( imageData->lightType == ShapeBaseImageData::WeaponFireLight )
  2656. image.lightStart = Sim::getCurrentTime();
  2657. }
  2658. updateImageState(i,0);
  2659. }
  2660. else
  2661. {
  2662. if(imageData)
  2663. {
  2664. // Initial state
  2665. image.fireCount = count;
  2666. image.altFireCount = altCount;
  2667. image.reloadCount = reloadCount;
  2668. if (isFiring)
  2669. setImageState(i,getImageFireState(i),true);
  2670. else if (isAltFiring)
  2671. setImageState(i,getImageAltFireState(i),true);
  2672. else if (isReloading)
  2673. setImageState(i,getImageReloadState(i),true);
  2674. }
  2675. }
  2676. }
  2677. }
  2678. }
  2679. if (stream->readFlag())
  2680. {
  2681. if(stream->readFlag()) // CloakMask and control
  2682. {
  2683. // Read cloaking state.
  2684. setCloakedState(stream->readFlag());
  2685. mIsControlled = stream->readFlag();
  2686. if (( mFading = stream->readFlag()) == true) {
  2687. mFadeOut = stream->readFlag();
  2688. if(mFadeOut)
  2689. mFadeVal = 1.0f;
  2690. else
  2691. mFadeVal = 0;
  2692. stream->read(&mFadeTime);
  2693. mFadeDelay = 0;
  2694. mFadeElapsedTime = 0;
  2695. }
  2696. else
  2697. mFadeVal = F32(stream->readFlag());
  2698. }
  2699. if (stream->readFlag()) { // NameMask
  2700. mShapeNameHandle = con->unpackNetStringHandleU(stream);
  2701. }
  2702. if ( stream->readFlag() ) // MeshHiddenMask
  2703. {
  2704. stream->readBits( &mMeshHidden );
  2705. _updateHiddenMeshes();
  2706. }
  2707. if (stream->readFlag()) // SkinMask
  2708. {
  2709. NetStringHandle skinDesiredNameHandle = con->unpackNetStringHandleU(stream);;
  2710. if (mSkinNameHandle != skinDesiredNameHandle)
  2711. {
  2712. mSkinNameHandle = skinDesiredNameHandle;
  2713. reSkin();
  2714. }
  2715. }
  2716. }
  2717. }
  2718. //--------------------------------------------------------------------------
  2719. void ShapeBase::forceUncloak(const char * reason)
  2720. {
  2721. AssertFatal(isServerObject(), "ShapeBase::forceUncloak: server only call");
  2722. if(!mCloaked)
  2723. return;
  2724. mDataBlock->onForceUncloak_callback( this, reason ? reason : "" );
  2725. }
  2726. void ShapeBase::setCloakedState(bool cloaked)
  2727. {
  2728. if (cloaked == mCloaked)
  2729. return;
  2730. if (isServerObject())
  2731. setMaskBits(CloakMask);
  2732. // Have to do this for the client, if we are ghosted over in the initial
  2733. // packet as cloaked, we set the state immediately to the extreme
  2734. if (isProperlyAdded() == false) {
  2735. mCloaked = cloaked;
  2736. if (mCloaked)
  2737. mCloakLevel = 1.0;
  2738. else
  2739. mCloakLevel = 0.0;
  2740. } else {
  2741. mCloaked = cloaked;
  2742. }
  2743. }
  2744. //--------------------------------------------------------------------------
  2745. void ShapeBase::setHidden( bool hidden )
  2746. {
  2747. if( hidden != isHidden() )
  2748. {
  2749. Parent::setHidden( hidden );
  2750. if( hidden )
  2751. setProcessTick( false );
  2752. else
  2753. setProcessTick( true );
  2754. }
  2755. }
  2756. //--------------------------------------------------------------------------
  2757. void ShapeBaseConvex::findNodeTransform()
  2758. {
  2759. S32 dl = pShapeBase->mDataBlock->collisionDetails[hullId];
  2760. TSShapeInstance* si = pShapeBase->getShapeInstance();
  2761. TSShape* shape = si->getShape();
  2762. const TSShape::Detail* detail = &shape->details[dl];
  2763. const S32 subs = detail->subShapeNum;
  2764. const S32 start = shape->subShapeFirstObject[subs];
  2765. const S32 end = start + shape->subShapeNumObjects[subs];
  2766. // Find the first object that contains a mesh for this
  2767. // detail level. There should only be one mesh per
  2768. // collision detail level.
  2769. for (S32 i = start; i < end; i++)
  2770. {
  2771. const TSShape::Object* obj = &shape->objects[i];
  2772. if (obj->numMeshes && detail->objectDetailNum < obj->numMeshes)
  2773. {
  2774. nodeTransform = &si->mNodeTransforms[obj->nodeIndex];
  2775. return;
  2776. }
  2777. }
  2778. return;
  2779. }
  2780. const MatrixF& ShapeBaseConvex::getTransform() const
  2781. {
  2782. // If the transform isn't specified, it's assumed to be the
  2783. // origin of the shape.
  2784. const MatrixF& omat = (transform != 0)? *transform: mObject->getTransform();
  2785. // Multiply on the mesh shape offset
  2786. // tg: Returning this static here is not really a good idea, but
  2787. // all this Convex code needs to be re-organized.
  2788. if (nodeTransform) {
  2789. static MatrixF mat;
  2790. mat.mul(omat,*nodeTransform);
  2791. return mat;
  2792. }
  2793. return omat;
  2794. }
  2795. Box3F ShapeBaseConvex::getBoundingBox() const
  2796. {
  2797. const MatrixF& omat = (transform != 0)? *transform: mObject->getTransform();
  2798. return getBoundingBox(omat, mObject->getScale());
  2799. }
  2800. Box3F ShapeBaseConvex::getBoundingBox(const MatrixF& mat, const Point3F& scale) const
  2801. {
  2802. Box3F newBox = box;
  2803. newBox.minExtents.convolve(scale);
  2804. newBox.maxExtents.convolve(scale);
  2805. mat.mul(newBox);
  2806. return newBox;
  2807. }
  2808. Point3F ShapeBaseConvex::support(const VectorF& v) const
  2809. {
  2810. TSShape::ConvexHullAccelerator* pAccel =
  2811. pShapeBase->mShapeInstance->getShape()->getAccelerator(pShapeBase->mDataBlock->collisionDetails[hullId]);
  2812. AssertFatal(pAccel != NULL, "Error, no accel!");
  2813. F32 currMaxDP = mDot(pAccel->vertexList[0], v);
  2814. U32 index = 0;
  2815. for (U32 i = 1; i < pAccel->numVerts; i++) {
  2816. F32 dp = mDot(pAccel->vertexList[i], v);
  2817. if (dp > currMaxDP) {
  2818. currMaxDP = dp;
  2819. index = i;
  2820. }
  2821. }
  2822. return pAccel->vertexList[index];
  2823. }
  2824. void ShapeBaseConvex::getFeatures(const MatrixF& mat, const VectorF& n, ConvexFeature* cf)
  2825. {
  2826. cf->material = 0;
  2827. cf->object = mObject;
  2828. TSShape::ConvexHullAccelerator* pAccel =
  2829. pShapeBase->mShapeInstance->getShape()->getAccelerator(pShapeBase->mDataBlock->collisionDetails[hullId]);
  2830. AssertFatal(pAccel != NULL, "Error, no accel!");
  2831. F32 currMaxDP = mDot(pAccel->vertexList[0], n);
  2832. U32 index = 0;
  2833. U32 i;
  2834. for (i = 1; i < pAccel->numVerts; i++) {
  2835. F32 dp = mDot(pAccel->vertexList[i], n);
  2836. if (dp > currMaxDP) {
  2837. currMaxDP = dp;
  2838. index = i;
  2839. }
  2840. }
  2841. const U8* emitString = pAccel->emitStrings[index];
  2842. U32 currPos = 0;
  2843. U32 numVerts = emitString[currPos++];
  2844. for (i = 0; i < numVerts; i++) {
  2845. cf->mVertexList.increment();
  2846. U32 index = emitString[currPos++];
  2847. mat.mulP(pAccel->vertexList[index], &cf->mVertexList.last());
  2848. }
  2849. U32 numEdges = emitString[currPos++];
  2850. for (i = 0; i < numEdges; i++) {
  2851. U32 ev0 = emitString[currPos++];
  2852. U32 ev1 = emitString[currPos++];
  2853. cf->mEdgeList.increment();
  2854. cf->mEdgeList.last().vertex[0] = ev0;
  2855. cf->mEdgeList.last().vertex[1] = ev1;
  2856. }
  2857. U32 numFaces = emitString[currPos++];
  2858. for (i = 0; i < numFaces; i++) {
  2859. cf->mFaceList.increment();
  2860. U32 plane = emitString[currPos++];
  2861. mat.mulV(pAccel->normalList[plane], &cf->mFaceList.last().normal);
  2862. for (U32 j = 0; j < 3; j++)
  2863. cf->mFaceList.last().vertex[j] = emitString[currPos++];
  2864. }
  2865. }
  2866. void ShapeBaseConvex::getPolyList(AbstractPolyList* list)
  2867. {
  2868. list->setTransform(&pShapeBase->getTransform(), pShapeBase->getScale());
  2869. list->setObject(pShapeBase);
  2870. pShapeBase->mShapeInstance->animate(pShapeBase->mDataBlock->collisionDetails[hullId]);
  2871. pShapeBase->mShapeInstance->buildPolyList(list,pShapeBase->mDataBlock->collisionDetails[hullId]);
  2872. }
  2873. //--------------------------------------------------------------------------
  2874. bool ShapeBase::isInvincible()
  2875. {
  2876. if( mDataBlock )
  2877. {
  2878. return mDataBlock->isInvincible;
  2879. }
  2880. return false;
  2881. }
  2882. void ShapeBase::startFade( F32 fadeTime, F32 fadeDelay, bool fadeOut )
  2883. {
  2884. setMaskBits(CloakMask);
  2885. mFadeElapsedTime = 0;
  2886. mFading = true;
  2887. if(fadeDelay < 0)
  2888. fadeDelay = 0;
  2889. if(fadeTime < 0)
  2890. fadeTime = 0;
  2891. mFadeTime = fadeTime;
  2892. mFadeDelay = fadeDelay;
  2893. mFadeOut = fadeOut;
  2894. mFadeVal = F32(mFadeOut);
  2895. }
  2896. //--------------------------------------------------------------------------
  2897. void ShapeBase::setShapeName(const char* name)
  2898. {
  2899. if (!isGhost()) {
  2900. if (name[0] != '\0') {
  2901. // Use tags for better network performance
  2902. // Should be a tag, but we'll convert to one if it isn't.
  2903. if (name[0] == StringTagPrefixByte)
  2904. mShapeNameHandle = NetStringHandle(U32(dAtoi(name + 1)));
  2905. else
  2906. mShapeNameHandle = NetStringHandle(name);
  2907. }
  2908. else {
  2909. mShapeNameHandle = NetStringHandle();
  2910. }
  2911. setMaskBits(NameMask);
  2912. }
  2913. }
  2914. void ShapeBase::setSkinName(const char* name)
  2915. {
  2916. if (!isGhost()) {
  2917. if (name[0] != '\0') {
  2918. // Use tags for better network performance
  2919. // Should be a tag, but we'll convert to one if it isn't.
  2920. if (name[0] == StringTagPrefixByte)
  2921. mSkinNameHandle = NetStringHandle(U32(dAtoi(name + 1)));
  2922. else
  2923. mSkinNameHandle = NetStringHandle(name);
  2924. }
  2925. else
  2926. mSkinNameHandle = NetStringHandle();
  2927. setMaskBits(SkinMask);
  2928. }
  2929. }
  2930. //----------------------------------------------------------------------------
  2931. void ShapeBase::reSkin()
  2932. {
  2933. if ( isGhost() && mShapeInstance && mSkinNameHandle.isValidString() )
  2934. {
  2935. Vector<String> skins;
  2936. String(mSkinNameHandle.getString()).split( ";", skins );
  2937. for (S32 i = 0; i < skins.size(); i++)
  2938. {
  2939. String oldSkin( mAppliedSkinName.c_str() );
  2940. String newSkin( skins[i] );
  2941. // Check if the skin handle contains an explicit "old" base string. This
  2942. // allows all models to support skinning, even if they don't follow the
  2943. // "base_xxx" material naming convention.
  2944. S32 split = newSkin.find( '=' ); // "old=new" format skin?
  2945. if ( split != String::NPos )
  2946. {
  2947. oldSkin = newSkin.substr( 0, split );
  2948. newSkin = newSkin.erase( 0, split+1 );
  2949. }
  2950. mShapeInstance->reSkin( newSkin, oldSkin );
  2951. mAppliedSkinName = newSkin;
  2952. }
  2953. }
  2954. }
  2955. void ShapeBase::setCurrentWaterObject( WaterObject *obj )
  2956. {
  2957. if ( obj )
  2958. deleteNotify( obj );
  2959. if ( mCurrentWaterObject )
  2960. clearNotify( mCurrentWaterObject );
  2961. mCurrentWaterObject = obj;
  2962. }
  2963. //--------------------------------------------------------------------------
  2964. //----------------------------------------------------------------------------
  2965. DefineEngineMethod( ShapeBase, setHidden, void, ( bool show ),,
  2966. "@brief Add or remove this object from the scene.\n\n"
  2967. "When removed from the scene, the object will not be processed or rendered.\n"
  2968. "@param show False to hide the object, true to re-show it\n\n" )
  2969. {
  2970. object->setHidden( show );
  2971. }
  2972. DefineEngineMethod( ShapeBase, isHidden, bool, (),,
  2973. "Check if the object is hidden.\n"
  2974. "@return true if the object is hidden, false if visible.\n\n" )
  2975. {
  2976. return object->isHidden();
  2977. }
  2978. //----------------------------------------------------------------------------
  2979. DefineEngineMethod( ShapeBase, playAudio, bool, ( S32 slot, SFXTrack* track ),,
  2980. "@brief Attach a sound to this shape and start playing it.\n\n"
  2981. "@param slot Audio slot index for the sound (valid range is 0 - 3)\n" // 3 = ShapeBase::MaxSoundThreads-1
  2982. "@param track SFXTrack to play\n"
  2983. "@return true if the sound was attached successfully, false if failed\n\n"
  2984. "@see stopAudio()\n")
  2985. {
  2986. if (track && slot >= 0 && slot < ShapeBase::MaxSoundThreads) {
  2987. object->playAudio(slot,track);
  2988. return true;
  2989. }
  2990. return false;
  2991. }
  2992. DefineEngineMethod( ShapeBase, stopAudio, bool, ( S32 slot ),,
  2993. "@brief Stop a sound started with playAudio.\n\n"
  2994. "@param slot audio slot index (started with playAudio)\n"
  2995. "@return true if the sound was stopped successfully, false if failed\n\n"
  2996. "@see playAudio()\n")
  2997. {
  2998. if (slot >= 0 && slot < ShapeBase::MaxSoundThreads) {
  2999. object->stopAudio(slot);
  3000. return true;
  3001. }
  3002. return false;
  3003. }
  3004. //----------------------------------------------------------------------------
  3005. DefineEngineMethod( ShapeBase, playThread, bool, ( S32 slot, const char* name ), ( "" ),
  3006. "@brief Start a new animation thread, or restart one that has been paused or "
  3007. "stopped.\n\n"
  3008. "@param slot thread slot to play. Valid range is 0 - 3)\n" // 3 = ShapeBase::MaxScriptThreads-1
  3009. "@param name name of the animation sequence to play in this slot. If not "
  3010. "specified, the paused or stopped thread in this slot will be resumed.\n"
  3011. "@return true if successful, false if failed\n\n"
  3012. "@tsexample\n"
  3013. "%obj.playThread( 0, \"ambient\" ); // Play the ambient sequence in slot 0\n"
  3014. "%obj.setThreadTimeScale( 0, 0.5 ); // Play at half-speed\n"
  3015. "%obj.pauseThread( 0 ); // Pause the sequence\n"
  3016. "%obj.playThread( 0 ); // Resume playback\n"
  3017. "%obj.playThread( 0, \"spin\" ); // Replace the sequence in slot 0\n"
  3018. "@endtsexample\n"
  3019. "@see pauseThread()\n"
  3020. "@see stopThread()\n"
  3021. "@see setThreadDir()\n"
  3022. "@see setThreadTimeScale()\n"
  3023. "@see destroyThread()\n")
  3024. {
  3025. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3026. if (!dStrEqual(name, "")) {
  3027. if (object->getShape()) {
  3028. S32 seq = object->getShape()->findSequence(name);
  3029. if (seq != -1 && object->setThreadSequence(slot,seq))
  3030. return true;
  3031. }
  3032. }
  3033. else
  3034. if (object->playThread(slot))
  3035. return true;
  3036. }
  3037. return false;
  3038. }
  3039. DefineEngineMethod( ShapeBase, setThreadDir, bool, ( S32 slot, bool fwd ),,
  3040. "@brief Set the playback direction of an animation thread.\n\n"
  3041. "@param slot thread slot to modify\n"
  3042. "@param fwd true to play the animation forwards, false to play backwards\n"
  3043. "@return true if successful, false if failed\n\n"
  3044. "@see playThread()\n" )
  3045. {
  3046. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3047. if (object->setThreadDir(slot,fwd))
  3048. return true;
  3049. }
  3050. return false;
  3051. }
  3052. DefineEngineMethod( ShapeBase, setThreadTimeScale, bool, ( S32 slot, F32 scale ),,
  3053. "@brief Set the playback time scale of an animation thread.\n\n"
  3054. "@param slot thread slot to modify\n"
  3055. "@param scale new thread time scale (1=normal speed, 0.5=half speed etc)\n"
  3056. "@return true if successful, false if failed\n\n"
  3057. "@see playThread\n" )
  3058. {
  3059. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3060. if (object->setThreadTimeScale(slot,scale))
  3061. return true;
  3062. }
  3063. return false;
  3064. }
  3065. DefineEngineMethod( ShapeBase, setThreadPosition, bool, ( S32 slot, F32 pos ),,
  3066. "@brief Set the position within an animation thread.\n\n"
  3067. "@param slot thread slot to modify\n"
  3068. "@param pos position within thread\n"
  3069. "@return true if successful, false if failed\n\n"
  3070. "@see playThread\n" )
  3071. {
  3072. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3073. if (object->setThreadPosition(slot,pos))
  3074. return true;
  3075. }
  3076. return false;
  3077. }
  3078. DefineEngineMethod( ShapeBase, stopThread, bool, ( S32 slot ),,
  3079. "@brief Stop an animation thread.\n\n"
  3080. "If restarted using playThread, the animation "
  3081. "will start from the beginning again.\n"
  3082. "@param slot thread slot to stop\n"
  3083. "@return true if successful, false if failed\n\n"
  3084. "@see playThread\n" )
  3085. {
  3086. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3087. if (object->stopThread(slot))
  3088. return true;
  3089. }
  3090. return false;
  3091. }
  3092. DefineEngineMethod( ShapeBase, destroyThread, bool, ( S32 slot ),,
  3093. "@brief Destroy an animation thread, which prevents it from playing.\n\n"
  3094. "@param slot thread slot to destroy\n"
  3095. "@return true if successful, false if failed\n\n"
  3096. "@see playThread\n" )
  3097. {
  3098. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3099. if (object->destroyThread(slot))
  3100. return true;
  3101. }
  3102. return false;
  3103. }
  3104. DefineEngineMethod( ShapeBase, pauseThread, bool, ( S32 slot ),,
  3105. "@brief Pause an animation thread.\n\n"
  3106. "If restarted using playThread, the animation "
  3107. "will resume from the paused position.\n"
  3108. "@param slot thread slot to stop\n"
  3109. "@return true if successful, false if failed\n\n"
  3110. "@see playThread\n" )
  3111. {
  3112. if (slot >= 0 && slot < ShapeBase::MaxScriptThreads) {
  3113. if (object->pauseThread(slot))
  3114. return true;
  3115. }
  3116. return false;
  3117. }
  3118. //----------------------------------------------------------------------------
  3119. DefineEngineMethod( ShapeBase, mountImage, bool,
  3120. ( ShapeBaseImageData* image, S32 slot, bool loaded, const char* skinTag ), ( true, "" ),
  3121. "@brief Mount a new Image.\n\n"
  3122. "@param image the Image to mount\n"
  3123. "@param slot Image slot to mount into (valid range is 0 - 3)\n"
  3124. "@param loaded initial loaded state for the Image\n"
  3125. "@param skinTag tagged string to reskin the mounted Image\n"
  3126. "@return true if successful, false if failed\n\n"
  3127. "@tsexample\n"
  3128. "%player.mountImage( PistolImage, 1 );\n"
  3129. "%player.mountImage( CrossbowImage, 0, false );\n"
  3130. "%player.mountImage( RocketLauncherImage, 0, true, 'blue' );\n"
  3131. "@endtsexample\n"
  3132. "@see unmountImage()\n"
  3133. "@see getMountedImage()\n"
  3134. "@see getPendingImage()\n"
  3135. "@see isImageMounted()\n")
  3136. {
  3137. if (image && slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3138. NetStringHandle team;
  3139. if (skinTag[0] == StringTagPrefixByte)
  3140. team = NetStringHandle(U32(dAtoi(skinTag+1)));
  3141. return object->mountImage( image, slot, loaded, team );
  3142. }
  3143. return false;
  3144. }
  3145. DefineEngineMethod( ShapeBase, unmountImage, bool, ( S32 slot ),,
  3146. "@brief Unmount the mounted Image in the specified slot.\n\n"
  3147. "@param slot Image slot to unmount\n"
  3148. "@return true if successful, false if failed\n\n"
  3149. "@see mountImage()\n")
  3150. {
  3151. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3152. return object->unmountImage(slot);
  3153. return false;
  3154. }
  3155. DefineEngineMethod( ShapeBase, getMountedImage, S32, ( S32 slot ),,
  3156. "@brief Get the Image mounted in the specified slot.\n\n"
  3157. "@param slot Image slot to query\n"
  3158. "@return ID of the ShapeBaseImageData datablock mounted in the slot, or 0 "
  3159. "if no Image is mounted there.\n\n" )
  3160. {
  3161. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3162. if (ShapeBaseImageData* data = object->getMountedImage(slot))
  3163. return data->getId();
  3164. return 0;
  3165. }
  3166. DefineEngineMethod( ShapeBase, getPendingImage, S32, ( S32 slot ),,
  3167. "@brief Get the Image that will be mounted next in the specified slot.\n\n"
  3168. "Calling mountImage when an Image is already mounted does one of two things: "
  3169. "<ol><li>Mount the new Image immediately, the old Image is discarded and "
  3170. "whatever state it was in is ignored.</li>"
  3171. "<li>If the current Image state does not allow Image changes, the new "
  3172. "Image is marked as pending, and will not be mounted until the current "
  3173. "state completes. eg. if the user changes weapons, you may wish to ensure "
  3174. "that the current weapon firing state plays to completion first.</li></ol>\n"
  3175. "This command retrieves the ID of the pending Image (2nd case above).\n"
  3176. "@param slot Image slot to query\n"
  3177. "@return ID of the pending ShapeBaseImageData datablock, or 0 if none.\n\n" )
  3178. {
  3179. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3180. if (ShapeBaseImageData* data = object->getPendingImage(slot))
  3181. return data->getId();
  3182. return 0;
  3183. }
  3184. DefineEngineMethod( ShapeBase, isImageFiring, bool, ( S32 slot ),,
  3185. "@brief Check if the current Image state is firing.\n\n"
  3186. "@param slot Image slot to query\n"
  3187. "@return true if the current Image state in this slot has the 'stateFire' flag set.\n" )
  3188. {
  3189. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3190. return object->isImageFiring(slot);
  3191. return false;
  3192. }
  3193. DefineEngineMethod( ShapeBase, isImageMounted, bool, ( ShapeBaseImageData* image ),,
  3194. "@brief Check if the given datablock is mounted to any slot on this object.\n\n"
  3195. "@param image ShapeBaseImageData datablock to query\n"
  3196. "@return true if the Image is mounted to any slot, false otherwise.\n\n" )
  3197. {
  3198. return (image && object->isImageMounted(image));
  3199. }
  3200. DefineEngineMethod( ShapeBase, getMountSlot, S32, ( ShapeBaseImageData* image ),,
  3201. "@brief Get the first slot the given datablock is mounted to on this object.\n\n"
  3202. "@param image ShapeBaseImageData datablock to query\n"
  3203. "@return index of the first slot the Image is mounted in, or -1 if the Image "
  3204. "is not mounted in any slot on this object.\n\n" )
  3205. {
  3206. return image ? object->getMountSlot(image) : -1;
  3207. }
  3208. DefineEngineMethod( ShapeBase, getImageSkinTag, S32, ( S32 slot ),,
  3209. "@brief Get the skin tag ID for the Image mounted in the specified slot.\n\n"
  3210. "@param slot Image slot to query\n"
  3211. "@return the skinTag value passed to mountImage when the image was "
  3212. "mounted\n\n" )
  3213. {
  3214. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3215. return object->getImageSkinTag(slot).getIndex();
  3216. return -1;
  3217. }
  3218. DefineEngineMethod( ShapeBase, getImageState, const char*, ( S32 slot ),,
  3219. "@brief Get the name of the current state of the Image in the specified slot.\n\n"
  3220. "@param slot Image slot to query\n"
  3221. "@return name of the current Image state, or \"Error\" if slot is invalid\n\n" )
  3222. {
  3223. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3224. return object->getImageState(slot);
  3225. return "Error";
  3226. }
  3227. DefineEngineMethod( ShapeBase, hasImageState, bool, ( S32 slot, const char* state ),,
  3228. "@brief Check if the given state exists on the mounted Image.\n\n"
  3229. "@param slot Image slot to query\n"
  3230. "@param state Image state to check for\n"
  3231. "@return true if the Image has the requested state defined.\n\n" )
  3232. {
  3233. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3234. return object->hasImageState(slot, state);
  3235. return false;
  3236. }
  3237. DefineEngineMethod( ShapeBase, getImageTrigger, bool, ( S32 slot ),,
  3238. "@brief Get the trigger state of the Image mounted in the specified slot.\n\n"
  3239. "@param slot Image slot to query\n"
  3240. "@return the Image's current trigger state\n\n" )
  3241. {
  3242. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3243. return object->getImageTriggerState(slot);
  3244. return false;
  3245. }
  3246. DefineEngineMethod( ShapeBase, setImageTrigger, bool, ( S32 slot, bool state ),,
  3247. "@brief Set the trigger state of the Image mounted in the specified slot.\n\n"
  3248. "@param slot Image slot to modify\n"
  3249. "@param state new trigger state for the Image\n"
  3250. "@return the Image's new trigger state\n\n" )
  3251. {
  3252. if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3253. object->setImageTriggerState(slot,state);
  3254. return object->getImageTriggerState(slot);
  3255. }
  3256. return false;
  3257. }
  3258. DefineEngineMethod( ShapeBase, getImageGenericTrigger, bool, ( S32 slot, S32 trigger ),,
  3259. "@brief Get the generic trigger state of the Image mounted in the specified slot.\n\n"
  3260. "@param slot Image slot to query\n"
  3261. "@param trigger Generic trigger number\n"
  3262. "@return the Image's current generic trigger state\n\n" )
  3263. {
  3264. if (slot >= 0 && slot < ShapeBase::MaxMountedImages && trigger >= 0 && trigger < ShapeBaseImageData::MaxGenericTriggers)
  3265. return object->getImageGenericTriggerState(slot, trigger);
  3266. return false;
  3267. }
  3268. DefineEngineMethod( ShapeBase, setImageGenericTrigger, S32, ( S32 slot, S32 trigger, bool state ),,
  3269. "@brief Set the generic trigger state of the Image mounted in the specified slot.\n\n"
  3270. "@param slot Image slot to modify\n"
  3271. "@param trigger Generic trigger number\n"
  3272. "@param state new generic trigger state for the Image\n"
  3273. "@return the Image's new generic trigger state or -1 if there was a problem.\n\n" )
  3274. {
  3275. if (slot >= 0 && slot < ShapeBase::MaxMountedImages && trigger >= 0 && trigger < ShapeBaseImageData::MaxGenericTriggers) {
  3276. object->setImageGenericTriggerState(slot,trigger,state);
  3277. return object->getImageGenericTriggerState(slot,trigger);
  3278. }
  3279. return -1;
  3280. }
  3281. DefineEngineMethod( ShapeBase, getImageAltTrigger, bool, ( S32 slot ),,
  3282. "@brief Get the alt trigger state of the Image mounted in the specified slot.\n\n"
  3283. "@param slot Image slot to query\n"
  3284. "@return the Image's current alt trigger state\n\n" )
  3285. {
  3286. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3287. return object->getImageAltTriggerState(slot);
  3288. return false;
  3289. }
  3290. DefineEngineMethod( ShapeBase, setImageAltTrigger, bool, ( S32 slot, bool state ),,
  3291. "@brief Set the alt trigger state of the Image mounted in the specified slot.\n\n"
  3292. "@param slot Image slot to modify\n"
  3293. "@param state new alt trigger state for the Image\n"
  3294. "@return the Image's new alt trigger state\n\n" )
  3295. {
  3296. if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3297. object->setImageAltTriggerState(slot,state);
  3298. return object->getImageAltTriggerState(slot);
  3299. }
  3300. return false;
  3301. }
  3302. DefineEngineMethod( ShapeBase, getImageAmmo, bool, ( S32 slot ),,
  3303. "@brief Get the ammo state of the Image mounted in the specified slot.\n\n"
  3304. "@param slot Image slot to query\n"
  3305. "@return the Image's current ammo state\n\n" )
  3306. {
  3307. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3308. return object->getImageAmmoState(slot);
  3309. return false;
  3310. }
  3311. DefineEngineMethod( ShapeBase, setImageAmmo, bool, ( S32 slot, bool state ),,
  3312. "@brief Set the ammo state of the Image mounted in the specified slot.\n\n"
  3313. "@param slot Image slot to modify\n"
  3314. "@param state new ammo state for the Image\n"
  3315. "@return the Image's new ammo state\n\n" )
  3316. {
  3317. if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3318. object->setImageAmmoState(slot,state);
  3319. return state;
  3320. }
  3321. return false;
  3322. }
  3323. DefineEngineMethod( ShapeBase, getImageLoaded, bool, ( S32 slot ),,
  3324. "@brief Get the loaded state of the Image mounted in the specified slot.\n\n"
  3325. "@param slot Image slot to query\n"
  3326. "@return the Image's current loaded state\n\n" )
  3327. {
  3328. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3329. return object->getImageLoadedState(slot);
  3330. return false;
  3331. }
  3332. DefineEngineMethod( ShapeBase, setImageLoaded, bool, ( S32 slot, bool state ),,
  3333. "@brief Set the loaded state of the Image mounted in the specified slot.\n\n"
  3334. "@param slot Image slot to modify\n"
  3335. "@param state new loaded state for the Image\n"
  3336. "@return the Image's new loaded state\n\n" )
  3337. {
  3338. if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3339. object->setImageLoadedState(slot, state);
  3340. return state;
  3341. }
  3342. return false;
  3343. }
  3344. DefineEngineMethod( ShapeBase, getImageTarget, bool, ( S32 slot ),,
  3345. "@brief Get the target state of the Image mounted in the specified slot.\n\n"
  3346. "@param slot Image slot to query\n"
  3347. "@return the Image's current target state\n\n" )
  3348. {
  3349. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3350. return object->getImageTargetState(slot);
  3351. return false;
  3352. }
  3353. DefineEngineMethod( ShapeBase, setImageTarget, bool, ( S32 slot, bool state ),,
  3354. "@brief Set the target state of the Image mounted in the specified slot.\n\n"
  3355. "@param slot Image slot to modify\n"
  3356. "@param state new target state for the Image\n"
  3357. "@return the Image's new target state\n\n" )
  3358. {
  3359. if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3360. object->setImageTargetState(slot,state);
  3361. return state;
  3362. }
  3363. return false;
  3364. }
  3365. DefineEngineMethod( ShapeBase, getImageScriptAnimPrefix, const char*, ( S32 slot ),,
  3366. "@brief Get the script animation prefix of the Image mounted in the specified slot.\n\n"
  3367. "@param slot Image slot to query\n"
  3368. "@return the Image's current script animation prefix\n\n" )
  3369. {
  3370. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3371. return object->getImageScriptAnimPrefix(slot).getString();
  3372. return "";
  3373. }
  3374. DefineEngineMethod( ShapeBase, setImageScriptAnimPrefix, void, ( S32 slot, const char* prefix ),,
  3375. "@brief Set the script animation prefix for the Image mounted in the specified slot.\n\n"
  3376. "This is used to further modify the prefix used when deciding which animation sequence to "
  3377. "play while this image is mounted.\n"
  3378. "@param slot Image slot to modify\n"
  3379. "@param prefix The prefix applied to the image\n" )
  3380. {
  3381. if (slot >= 0 && slot < ShapeBase::MaxMountedImages) {
  3382. NetStringHandle prefixHandle;
  3383. if (prefix[0] == StringTagPrefixByte)
  3384. prefixHandle = NetStringHandle(U32(dAtoi(prefix+1)));
  3385. object->setImageScriptAnimPrefix(slot, prefixHandle);
  3386. }
  3387. }
  3388. DefineEngineMethod( ShapeBase, getMuzzleVector, VectorF, ( S32 slot ),,
  3389. "@brief Get the muzzle vector of the Image mounted in the specified slot.\n\n"
  3390. "If the Image shape contains a node called 'muzzlePoint', then the muzzle "
  3391. "vector is the forward direction vector of that node's transform in world "
  3392. "space. If no such node is specified, the slot's mount node is used "
  3393. "instead.\n\n"
  3394. "If the correctMuzzleVector flag (correctMuzzleVectorTP in 3rd person) "
  3395. "is set in the Image, the muzzle vector is computed to point at whatever "
  3396. "object is right in front of the object's 'eye' node.\n"
  3397. "@param slot Image slot to query\n"
  3398. "@return the muzzle vector, or \"0 1 0\" if the slot is invalid\n\n" )
  3399. {
  3400. VectorF vec(0, 1, 0);
  3401. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3402. object->getMuzzleVector(slot, &vec);
  3403. return vec;
  3404. }
  3405. DefineEngineMethod( ShapeBase, getMuzzlePoint, Point3F, ( S32 slot ),,
  3406. "@brief Get the muzzle position of the Image mounted in the specified slot.\n\n"
  3407. "If the Image shape contains a node called 'muzzlePoint', then the muzzle "
  3408. "position is the position of that node in world space. If no such node "
  3409. "is specified, the slot's mount node is used instead.\n"
  3410. "@param slot Image slot to query\n"
  3411. "@return the muzzle position, or \"0 0 0\" if the slot is invalid\n\n" )
  3412. {
  3413. Point3F pos(0, 0, 0);
  3414. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3415. object->getMuzzlePoint(slot, &pos);
  3416. return pos;
  3417. }
  3418. DefineEngineMethod( ShapeBase, getSlotTransform, TransformF, ( S32 slot ),,
  3419. "@brief Get the world transform of the specified mount slot.\n\n"
  3420. "@param slot Image slot to query\n"
  3421. "@return the mount transform\n\n" )
  3422. {
  3423. MatrixF xf(true);
  3424. if (slot >= 0 && slot < ShapeBase::MaxMountedImages)
  3425. object->getMountTransform( slot, MatrixF::Identity, &xf );
  3426. return xf;
  3427. }
  3428. //----------------------------------------------------------------------------
  3429. DefineEngineMethod( ShapeBase, getAIRepairPoint, Point3F, (),,
  3430. "@brief Get the position at which the AI should stand to repair things.\n\n"
  3431. "If the shape defines a node called \"AIRepairNode\", this method will "
  3432. "return the current world position of that node, otherwise \"0 0 0\".\n"
  3433. "@return the AI repair position\n\n" )
  3434. {
  3435. return object->getAIRepairPoint();
  3436. }
  3437. DefineEngineMethod( ShapeBase, getVelocity, VectorF, (),,
  3438. "@brief Get the object's current velocity.\n\n"
  3439. "@return the current velocity\n\n" )
  3440. {
  3441. return object->getVelocity();
  3442. }
  3443. DefineEngineMethod( ShapeBase, setVelocity, bool, ( Point3F vel ),,
  3444. "@brief Set the object's velocity.\n\n"
  3445. "@param vel new velocity for the object\n"
  3446. "@return true\n\n" )
  3447. {
  3448. object->setVelocity( vel );
  3449. return true;
  3450. }
  3451. DefineEngineMethod( ShapeBase, applyImpulse, bool, ( Point3F pos, Point3F vec ),,
  3452. "@brief Apply an impulse to the object.\n\n"
  3453. "@param pos world position of the impulse\n"
  3454. "@param vec impulse momentum (velocity * mass)\n"
  3455. "@return true\n\n" )
  3456. {
  3457. object->applyImpulse( pos, vec );
  3458. return true;
  3459. }
  3460. DefineEngineMethod( ShapeBase, getEyeVector, VectorF, (),,
  3461. "@brief Get the forward direction of the 'eye' for this object.\n\n"
  3462. "If the object model has a node called 'eye', this method will return that "
  3463. "node's current forward direction vector, otherwise it will return the "
  3464. "object's current forward direction vector.\n"
  3465. "@return the eye vector for this object\n"
  3466. "@see getEyePoint\n"
  3467. "@see getEyeTransform\n" )
  3468. {
  3469. MatrixF mat;
  3470. object->getEyeTransform(&mat);
  3471. return mat.getForwardVector();
  3472. }
  3473. DefineEngineMethod( ShapeBase, getEyePoint, Point3F, (),,
  3474. "@brief Get the position of the 'eye' for this object.\n\n"
  3475. "If the object model has a node called 'eye', this method will return that "
  3476. "node's current world position, otherwise it will return the object's current "
  3477. "world position.\n"
  3478. "@return the eye position for this object\n"
  3479. "@see getEyeVector\n"
  3480. "@see getEyeTransform\n" )
  3481. {
  3482. MatrixF mat;
  3483. object->getEyeTransform(&mat);
  3484. return mat.getPosition();
  3485. }
  3486. DefineEngineMethod( ShapeBase, getEyeTransform, TransformF, (),,
  3487. "@brief Get the 'eye' transform for this object.\n\n"
  3488. "If the object model has a node called 'eye', this method will return that "
  3489. "node's current transform, otherwise it will return the object's current "
  3490. "transform.\n"
  3491. "@return the eye transform for this object\n"
  3492. "@see getEyeVector\n"
  3493. "@see getEyePoint\n" )
  3494. {
  3495. MatrixF mat;
  3496. object->getEyeTransform(&mat);
  3497. return mat;
  3498. }
  3499. DefineEngineMethod( ShapeBase, getLookAtPoint, const char*, ( F32 distance, S32 typeMask ), ( 2000, 0xFFFFFFFF ),
  3500. "@brief Get the world position this object is looking at.\n\n"
  3501. "Casts a ray from the eye and returns information about what the ray hits.\n"
  3502. "@param distance maximum distance of the raycast\n"
  3503. "@param typeMask typeMask of objects to include for raycast collision testing\n"
  3504. "@return look-at information as \"Object HitX HitY HitZ [Material]\" or empty string for no hit\n\n"
  3505. "@tsexample\n"
  3506. "%lookat = %obj.getLookAtPoint();\n"
  3507. "echo( \"Looking at: \" @ getWords( %lookat, 1, 3 ) );\n"
  3508. "@endtsexample\n" )
  3509. {
  3510. MatrixF mat;
  3511. object->getEyeTransform( &mat );
  3512. // Get eye vector.
  3513. VectorF eyeVector;
  3514. mat.getColumn( 1, &eyeVector );
  3515. // Get eye position.
  3516. VectorF eyePos;
  3517. mat.getColumn( 3, &eyePos );
  3518. // Make sure the eye vector covers the distance.
  3519. eyeVector *= distance;
  3520. // Do a container search.
  3521. VectorF start = eyePos;
  3522. VectorF end = eyePos + eyeVector;
  3523. RayInfo ri;
  3524. if( !gServerContainer.castRay( start, end, typeMask, &ri ) || !ri.object )
  3525. return ""; // No hit.
  3526. // Gather hit info.
  3527. enum { BUFFER_SIZE = 256 };
  3528. char* buffer = Con::getReturnBuffer( BUFFER_SIZE );
  3529. if( ri.material )
  3530. dSprintf( buffer, BUFFER_SIZE, "%u %f %f %f %u",
  3531. ri.object->getId(),
  3532. ri.point.x,
  3533. ri.point.y,
  3534. ri.point.z,
  3535. ri.material->getMaterial()->getId() );
  3536. else
  3537. dSprintf( buffer, BUFFER_SIZE, "%u %f %f %f",
  3538. ri.object->getId(),
  3539. ri.point.x,
  3540. ri.point.y,
  3541. ri.point.z );
  3542. return buffer;
  3543. }
  3544. DefineEngineMethod( ShapeBase, setEnergyLevel, void, ( F32 level ),,
  3545. "@brief Set this object's current energy level.\n\n"
  3546. "@param level new energy level\n"
  3547. "@see getEnergyLevel()\n"
  3548. "@see getEnergyPercent()\n")
  3549. {
  3550. object->setEnergyLevel( level );
  3551. }
  3552. DefineEngineMethod( ShapeBase, getEnergyLevel, F32, (),,
  3553. "@brief Get the object's current energy level.\n\n"
  3554. "@return energy level\n"
  3555. "@see setEnergyLevel()\n")
  3556. {
  3557. return object->getEnergyLevel();
  3558. }
  3559. DefineEngineMethod( ShapeBase, getEnergyPercent, F32, (),,
  3560. "@brief Get the object's current energy level as a percentage of maxEnergy.\n\n"
  3561. "@return energyLevel / datablock.maxEnergy\n"
  3562. "@see setEnergyLevel()\n")
  3563. {
  3564. return object->getEnergyValue();
  3565. }
  3566. DefineEngineMethod( ShapeBase, setDamageLevel, void, ( F32 level ),,
  3567. "@brief Set the object's current damage level.\n\n"
  3568. "@param level new damage level\n"
  3569. "@see getDamageLevel()\n"
  3570. "@see getDamagePercent()\n")
  3571. {
  3572. object->setDamageLevel( level );
  3573. }
  3574. DefineEngineMethod( ShapeBase, getDamageLevel, F32, (),,
  3575. "@brief Get the object's current damage level.\n\n"
  3576. "@return damage level\n"
  3577. "@see setDamageLevel()\n")
  3578. {
  3579. return object->getDamageLevel();
  3580. }
  3581. DefineEngineMethod( ShapeBase, getDamagePercent, F32, (),,
  3582. "@brief Get the object's current damage level as a percentage of maxDamage.\n\n"
  3583. "@return damageLevel / datablock.maxDamage\n"
  3584. "@see setDamageLevel()\n")
  3585. {
  3586. return object->getDamageValue();
  3587. }
  3588. DefineEngineMethod(ShapeBase, getMaxDamage, F32, (),,
  3589. "Get the object's maxDamage level.\n"
  3590. "@return datablock.maxDamage\n")
  3591. {
  3592. return object->getMaxDamage();
  3593. }
  3594. DefineEngineMethod( ShapeBase, setDamageState, bool, ( const char* state ),,
  3595. "@brief Set the object's damage state.\n\n"
  3596. "@param state should be one of \"Enabled\", \"Disabled\", \"Destroyed\"\n"
  3597. "@return true if successful, false if failed\n"
  3598. "@see getDamageState()\n")
  3599. {
  3600. return object->setDamageState( state );
  3601. }
  3602. DefineEngineMethod( ShapeBase, getDamageState, const char*, (),,
  3603. "@brief Get the object's damage state.\n\n"
  3604. "@return the damage state; one of \"Enabled\", \"Disabled\", \"Destroyed\"\n"
  3605. "@see setDamageState()\n")
  3606. {
  3607. return object->getDamageStateName();
  3608. }
  3609. DefineEngineMethod( ShapeBase, isDestroyed, bool, (),,
  3610. "@brief Check if the object is in the Destroyed damage state.\n\n"
  3611. "@return true if damage state is \"Destroyed\", false if not\n"
  3612. "@see isDisabled()\n"
  3613. "@see isEnabled()\n")
  3614. {
  3615. return object->isDestroyed();
  3616. }
  3617. DefineEngineMethod( ShapeBase, isDisabled, bool, (),,
  3618. "@brief Check if the object is in the Disabled or Destroyed damage state.\n\n"
  3619. "@return true if damage state is not \"Enabled\", false if it is\n"
  3620. "@see isDestroyed()\n"
  3621. "@see isEnabled()\n")
  3622. {
  3623. return object->getDamageState() != ShapeBase::Enabled;
  3624. }
  3625. DefineEngineMethod( ShapeBase, isEnabled, bool, (),,
  3626. "@brief Check if the object is in the Enabled damage state.\n\n"
  3627. "@return true if damage state is \"Enabled\", false if not\n"
  3628. "@see isDestroyed()\n"
  3629. "@see isDisabled()\n")
  3630. {
  3631. return object->getDamageState() == ShapeBase::Enabled;
  3632. }
  3633. DefineEngineMethod(ShapeBase, blowUp, void, (),, "@brief Explodes an object into pieces.")
  3634. {
  3635. object->blowUp();
  3636. }
  3637. DefineEngineMethod( ShapeBase, applyDamage, void, ( F32 amount ),,
  3638. "@brief Increment the current damage level by the specified amount.\n\n"
  3639. "@param amount value to add to current damage level\n" )
  3640. {
  3641. object->applyDamage( amount );
  3642. }
  3643. DefineEngineMethod( ShapeBase, applyRepair, void, ( F32 amount ),,
  3644. "@brief Repair damage by the specified amount.\n\n"
  3645. "Note that the damage level is only reduced by repairRate per tick, so it may "
  3646. "take several ticks for the total repair to complete.\n"
  3647. "@param amount total repair value (subtracted from damage level over time)\n" )
  3648. {
  3649. object->applyRepair( amount );
  3650. }
  3651. DefineEngineMethod( ShapeBase, setRepairRate, void, ( F32 rate ),,
  3652. "@brief Set amount to repair damage by each tick.\n\n"
  3653. "Note that this value is separate to the repairRate field in ShapeBaseData. "
  3654. "This value will be subtracted from the damage level each tick, whereas the "
  3655. "ShapeBaseData field limits how much of the applyRepair value is subtracted "
  3656. "each tick. Both repair types can be active at the same time.\n"
  3657. "@param rate value to subtract from damage level each tick (must be > 0)\n"
  3658. "@see getRepairRate()\n")
  3659. {
  3660. if(rate < 0)
  3661. rate = 0;
  3662. object->setRepairRate( rate );
  3663. }
  3664. DefineEngineMethod( ShapeBase, getRepairRate, F32, (),,
  3665. "@brief Get the per-tick repair amount.\n\n"
  3666. "@return the current value to be subtracted from damage level each tick\n"
  3667. "@see setRepairRate\n" )
  3668. {
  3669. return object->getRepairRate();
  3670. }
  3671. DefineEngineMethod( ShapeBase, setRechargeRate, void, ( F32 rate ),,
  3672. "@brief Set the recharge rate.\n\n"
  3673. "The recharge rate is added to the object's current energy level each tick, "
  3674. "up to the maxEnergy level set in the ShapeBaseData datablock.\n"
  3675. "@param rate the recharge rate (per tick)\n"
  3676. "@see getRechargeRate()\n")
  3677. {
  3678. object->setRechargeRate( rate );
  3679. }
  3680. DefineEngineMethod( ShapeBase, getRechargeRate, F32, (),,
  3681. "@brief Get the current recharge rate.\n\n"
  3682. "@return the recharge rate (per tick)\n"
  3683. "@see setRechargeRate()\n")
  3684. {
  3685. return object->getRechargeRate();
  3686. }
  3687. DefineEngineMethod( ShapeBase, getControllingClient, S32, (),,
  3688. "@brief Get the client (if any) that controls this object.\n\n"
  3689. "The controlling client is the one that will send moves to us to act on.\n"
  3690. "@return the ID of the controlling GameConnection, or 0 if this object is not "
  3691. "controlled by any client.\n"
  3692. "@see GameConnection\n")
  3693. {
  3694. if (GameConnection* con = object->getControllingClient())
  3695. return con->getId();
  3696. return 0;
  3697. }
  3698. DefineEngineMethod( ShapeBase, getControllingObject, S32, (),,
  3699. "@brief Get the object (if any) that controls this object.\n\n"
  3700. "@return the ID of the controlling ShapeBase object, or 0 if this object is "
  3701. "not controlled by another object.\n" )
  3702. {
  3703. if (ShapeBase* con = object->getControllingObject())
  3704. return con->getId();
  3705. return 0;
  3706. }
  3707. DefineEngineMethod( ShapeBase, canCloak, bool, (),,
  3708. "@brief Check if this object can cloak.\n\n"
  3709. "@return true\n"
  3710. "@note Not implemented as it always returns true.")
  3711. {
  3712. return true;
  3713. }
  3714. DefineEngineMethod( ShapeBase, setCloaked, void, ( bool cloak ),,
  3715. "@brief Set the cloaked state of this object.\n\n"
  3716. "When an object is cloaked it is not rendered.\n"
  3717. "@param cloak true to cloak the object, false to uncloak\n"
  3718. "@see isCloaked()\n")
  3719. {
  3720. if (object->isServerObject())
  3721. object->setCloakedState( cloak );
  3722. }
  3723. DefineEngineMethod( ShapeBase, isCloaked, bool, (),,
  3724. "@brief Check if this object is cloaked.\n\n"
  3725. "@return true if cloaked, false if not\n"
  3726. "@see setCloaked()\n")
  3727. {
  3728. return object->getCloakedState();
  3729. }
  3730. DefineEngineMethod( ShapeBase, setDamageFlash, void, ( F32 level ),,
  3731. "@brief Set the damage flash level.\n\n"
  3732. "Damage flash may be used as a postfx effect to flash the screen when the "
  3733. "client is damaged.\n"
  3734. "@note Relies on the flash postFx.\n"
  3735. "@param level flash level (0-1)\n"
  3736. "@see getDamageFlash()\n")
  3737. {
  3738. if (object->isServerObject())
  3739. object->setDamageFlash( level );
  3740. }
  3741. DefineEngineMethod( ShapeBase, getDamageFlash, F32, (),,
  3742. "@brief Get the damage flash level.\n\n"
  3743. "@return flash level\n"
  3744. "@see setDamageFlash\n" )
  3745. {
  3746. return object->getDamageFlash();
  3747. }
  3748. DefineEngineMethod( ShapeBase, setWhiteOut, void, ( F32 level ),,
  3749. "@brief Set the white-out level.\n\n"
  3750. "White-out may be used as a postfx effect to brighten the screen in response "
  3751. "to a game event.\n"
  3752. "@note Relies on the flash postFx.\n"
  3753. "@param level flash level (0-1)\n"
  3754. "@see getWhiteOut()\n")
  3755. {
  3756. if (object->isServerObject())
  3757. object->setWhiteOut( level );
  3758. }
  3759. DefineEngineMethod( ShapeBase, getWhiteOut, F32, (),,
  3760. "@brief Get the white-out level.\n\n"
  3761. "@return white-out level\n"
  3762. "@see setWhiteOut\n" )
  3763. {
  3764. return object->getWhiteOut();
  3765. }
  3766. DefineEngineMethod( ShapeBase, getDefaultCameraFov, F32, (),,
  3767. "@brief Returns the default vertical field of view in degrees for this object if used as a camera.\n\n"
  3768. "@return Default FOV\n" )
  3769. {
  3770. if (object->isServerObject())
  3771. return object->getDefaultCameraFov();
  3772. return 0.0;
  3773. }
  3774. DefineEngineMethod( ShapeBase, getCameraFov, F32, (),,
  3775. "@brief Returns the vertical field of view in degrees for this object if used as a camera.\n\n"
  3776. "@return current FOV as defined in ShapeBaseData::cameraDefaultFov\n" )
  3777. {
  3778. if (object->isServerObject())
  3779. return object->getCameraFov();
  3780. return 0.0;
  3781. }
  3782. DefineEngineMethod( ShapeBase, setCameraFov, void, ( F32 fov ),,
  3783. "@brief Set the vertical field of view in degrees for this object if used as a camera.\n\n"
  3784. "@param fov new FOV value\n" )
  3785. {
  3786. if (object->isServerObject())
  3787. object->setCameraFov( fov );
  3788. }
  3789. DefineEngineMethod( ShapeBase, startFade, void, ( S32 time, S32 delay, bool fadeOut ),,
  3790. "@brief Fade the object in or out without removing it from the scene.\n\n"
  3791. "A faded out object is still in the scene and can still be collided with, "
  3792. "so if you want to disable collisions for this shape after it fades out "
  3793. "use setHidden to temporarily remove this shape from the scene.\n"
  3794. "@note Items have the ability to light their surroundings. When an Item with "
  3795. "an active light is fading out, the light it emits is correspondingly "
  3796. "reduced until it goes out. Likewise, when the item fades in, the light is "
  3797. "turned-up till it reaches it's normal brightntess.\n"
  3798. "@param time duration of the fade effect in ms\n"
  3799. "@param delay delay in ms before the fade effect begins\n"
  3800. "@param fadeOut true to fade-out to invisible, false to fade-in to full visibility\n" )
  3801. {
  3802. object->startFade( (F32)time / (F32)1000.0, delay / 1000.0, fadeOut );
  3803. }
  3804. DefineEngineMethod( ShapeBase, setDamageVector, void, ( Point3F vec ),,
  3805. "@brief Set the damage direction vector.\n\n"
  3806. "Currently this is only used to initialise the explosion if this object "
  3807. "is blown up.\n"
  3808. "@param vec damage direction vector\n\n"
  3809. "@tsexample\n"
  3810. "%obj.setDamageVector( \"0 0 1\" );\n"
  3811. "@endtsexample\n" )
  3812. {
  3813. vec.normalize();
  3814. object->setDamageDir( vec );
  3815. }
  3816. DefineEngineMethod( ShapeBase, setShapeName, void, ( const char* name ),,
  3817. "@brief Set the name of this shape.\n\n"
  3818. "@note This is the name of the shape object that is sent to the client, "
  3819. "not the DTS or DAE model filename.\n"
  3820. "@param name new name for the shape\n\n"
  3821. "@see getShapeName()\n")
  3822. {
  3823. object->setShapeName( name );
  3824. }
  3825. DefineEngineMethod( ShapeBase, getShapeName, const char*, (),,
  3826. "@brief Get the name of the shape.\n\n"
  3827. "@note This is the name of the shape object that is sent to the client, "
  3828. "not the DTS or DAE model filename.\n"
  3829. "@return the name of the shape\n\n"
  3830. "@see setShapeName()\n")
  3831. {
  3832. return object->getShapeName();
  3833. }
  3834. DefineEngineMethod( ShapeBase, setSkinName, void, ( const char* name ),,
  3835. "@brief Apply a new skin to this shape.\n\n"
  3836. "'Skinning' the shape effectively renames the material targets, allowing "
  3837. "different materials to be used on different instances of the same model.\n\n"
  3838. "@param name name of the skin to apply\n\n"
  3839. "@see skin\n"
  3840. "@see getSkinName()\n")
  3841. {
  3842. object->setSkinName( name );
  3843. }
  3844. DefineEngineMethod( ShapeBase, getSkinName, const char*, (),,
  3845. "@brief Get the name of the skin applied to this shape.\n\n"
  3846. "@return the name of the skin\n\n"
  3847. "@see skin\n"
  3848. "@see setSkinName()\n")
  3849. {
  3850. return object->getSkinName();
  3851. }
  3852. //----------------------------------------------------------------------------
  3853. void ShapeBase::consoleInit()
  3854. {
  3855. Con::addVariable("SB::DFDec", TypeF32, &sDamageFlashDec, "Speed to reduce the damage flash effect per tick.\n\n"
  3856. "@see ShapeBase::setDamageFlash()\n"
  3857. "@see ShapeBase::getDamageFlash()\n"
  3858. "@note Relies on the flash postFx.\n"
  3859. "@ingroup gameObjects\n");
  3860. Con::addVariable("SB::WODec", TypeF32, &sWhiteoutDec, "Speed to reduce the whiteout effect per tick.\n\n"
  3861. "@see ShapeBase::setWhiteOut()\n"
  3862. "@see ShapeBase::getWhiteOut"
  3863. "@note Relies on the flash postFx.\n"
  3864. "@ingroup gameObjects\n");
  3865. Con::addVariable("SB::FullCorrectionDistance", TypeF32, &sFullCorrectionDistance,
  3866. "@brief Distance at which a weapon's muzzle vector is fully corrected to match where the player is looking.\n\n"
  3867. "When a weapon image has correctMuzzleVector set and the Player is in 1st person, the muzzle vector from the "
  3868. "weapon is modified to match where the player is looking. Beyond the FullCorrectionDistance the muzzle vector "
  3869. "is always corrected. Between FullCorrectionDistance and the player, the weapon's muzzle vector is adjusted so that "
  3870. "the closer the aim point is to the player, the closer the muzzle vector is to the true (non-corrected) one.\n"
  3871. "@ingroup gameObjects\n");
  3872. Con::addVariable("SB::CloakSpeed", TypeF32, &sCloakSpeed,
  3873. "@brief Time to cloak, in seconds.\n\n"
  3874. "@ingroup gameObjects\n");
  3875. }
  3876. void ShapeBase::_updateHiddenMeshes()
  3877. {
  3878. if ( !mShapeInstance )
  3879. return;
  3880. // This may happen at some point in the future... lets
  3881. // detect it so that it can be fixed at that time.
  3882. AssertFatal( mMeshHidden.getSize() == mShapeInstance->mMeshObjects.size(),
  3883. "ShapeBase::_updateMeshVisibility() - Mesh visibility size mismatch!" );
  3884. for ( U32 i = 0; i < mMeshHidden.getSize(); i++ )
  3885. setMeshHidden( i, mMeshHidden.test( i ) );
  3886. }
  3887. void ShapeBase::setMeshHidden( const char *meshName, bool forceHidden )
  3888. {
  3889. setMeshHidden( mDataBlock->mShape->findObject( meshName ), forceHidden );
  3890. }
  3891. void ShapeBase::setMeshHidden( S32 meshIndex, bool forceHidden )
  3892. {
  3893. if ( meshIndex == -1 || meshIndex >= mMeshHidden.getSize() )
  3894. return;
  3895. if ( forceHidden )
  3896. mMeshHidden.set( meshIndex );
  3897. else
  3898. mMeshHidden.clear( meshIndex );
  3899. if ( mShapeInstance )
  3900. mShapeInstance->setMeshForceHidden( meshIndex, forceHidden );
  3901. setMaskBits( MeshHiddenMask );
  3902. }
  3903. void ShapeBase::setAllMeshesHidden( bool forceHidden )
  3904. {
  3905. if ( forceHidden )
  3906. mMeshHidden.set();
  3907. else
  3908. mMeshHidden.clear();
  3909. if ( mShapeInstance )
  3910. {
  3911. for ( U32 i = 0; i < mMeshHidden.getSize(); i++ )
  3912. mShapeInstance->setMeshForceHidden( i, forceHidden );
  3913. }
  3914. setMaskBits( MeshHiddenMask );
  3915. }
  3916. DefineEngineMethod( ShapeBase, setAllMeshesHidden, void, ( bool hide ),,
  3917. "@brief Set the hidden state on all the shape meshes.\n\n"
  3918. "This allows you to hide all meshes in the shape, for example, and then only "
  3919. "enable a few.\n"
  3920. "@param hide new hidden state for all meshes\n\n" )
  3921. {
  3922. object->setAllMeshesHidden( hide );
  3923. }
  3924. DefineEngineMethod( ShapeBase, setMeshHidden, void, ( const char* name, bool hide ),,
  3925. "@brief Set the hidden state on the named shape mesh.\n\n"
  3926. "@param name name of the mesh to hide/show\n"
  3927. "@param hide new hidden state for the mesh\n\n" )
  3928. {
  3929. object->setMeshHidden( name, hide );
  3930. }
  3931. // Some development-handy functions
  3932. #ifndef TORQUE_SHIPPING
  3933. void ShapeBase::dumpMeshVisibility()
  3934. {
  3935. if ( !mShapeInstance )
  3936. return;
  3937. const Vector<TSShapeInstance::MeshObjectInstance> &meshes = mShapeInstance->mMeshObjects;
  3938. for ( U32 i = 0; i < meshes.size(); i++)
  3939. {
  3940. const TSShapeInstance::MeshObjectInstance &mesh = meshes[i];
  3941. const String &meshName = mDataBlock->mShape->getMeshName( i );
  3942. Con::printf( "%d - %s - forceHidden = %s, visibility = %f",
  3943. i,
  3944. meshName.c_str(),
  3945. mesh.forceHidden ? "true" : "false",
  3946. mesh.visible );
  3947. }
  3948. }
  3949. DefineEngineMethod( ShapeBase, dumpMeshVisibility, void, (),,
  3950. "@brief Print a list of visible and hidden meshes in the shape to the console "
  3951. "for debugging purposes.\n\n"
  3952. "@note Only in a SHIPPING build.\n")
  3953. {
  3954. object->dumpMeshVisibility();
  3955. }
  3956. #endif // #ifndef TORQUE_SHIPPING
  3957. //------------------------------------------------------------------------
  3958. //These functions are duplicated in tsStatic and shapeBase.
  3959. //They each function a little differently; but achieve the same purpose of gathering
  3960. //target names/counts without polluting simObject.
  3961. DefineEngineMethod( ShapeBase, getTargetName, const char*, ( S32 index ),,
  3962. "@brief Get the name of the indexed shape material.\n\n"
  3963. "@param index index of the material to get (valid range is 0 - getTargetCount()-1).\n"
  3964. "@return the name of the indexed material.\n\n"
  3965. "@see getTargetCount()\n")
  3966. {
  3967. ShapeBase *obj = dynamic_cast< ShapeBase* > ( object );
  3968. if(obj)
  3969. {
  3970. // Try to use the client object (so we get the reskinned targets in the Material Editor)
  3971. if ((ShapeBase*)obj->getClientObject())
  3972. obj = (ShapeBase*)obj->getClientObject();
  3973. return obj->getShapeInstance()->getTargetName(index);
  3974. }
  3975. return "";
  3976. }
  3977. DefineEngineMethod( ShapeBase, getTargetCount, S32, (),,
  3978. "@brief Get the number of materials in the shape.\n\n"
  3979. "@return the number of materials in the shape.\n\n"
  3980. "@see getTargetName()\n")
  3981. {
  3982. ShapeBase *obj = dynamic_cast< ShapeBase* > ( object );
  3983. if(obj)
  3984. {
  3985. // Try to use the client object (so we get the reskinned targets in the Material Editor)
  3986. if ((ShapeBase*)obj->getClientObject())
  3987. obj = (ShapeBase*)obj->getClientObject();
  3988. return obj->getShapeInstance()->getTargetCount();
  3989. }
  3990. return -1;
  3991. }
  3992. DefineEngineMethod( ShapeBase, changeMaterial, void, ( const char* mapTo, Material* oldMat, Material* newMat ),,
  3993. "@brief Change one of the materials on the shape.\n\n"
  3994. "This method changes materials per mapTo with others. The material that "
  3995. "is being replaced is mapped to unmapped_mat as a part of this transition.\n"
  3996. "@note Warning, right now this only sort of works. It doesn't do a live "
  3997. "update like it should.\n"
  3998. "@param mapTo the name of the material target to remap (from getTargetName)\n"
  3999. "@param oldMat the old Material that was mapped \n"
  4000. "@param newMat the new Material to map\n\n"
  4001. "@tsexample\n"
  4002. "// remap the first material in the shape\n"
  4003. "%mapTo = %obj.getTargetName( 0 );\n"
  4004. "%obj.changeMaterial( %mapTo, 0, MyMaterial );\n"
  4005. "@endtsexample\n" )
  4006. {
  4007. // if no valid new material, theres no reason for doing this
  4008. if( !newMat )
  4009. {
  4010. Con::errorf("ShapeBase::changeMaterial failed: New material does not exist!");
  4011. return;
  4012. }
  4013. // initilize server/client versions
  4014. ShapeBase *serverObj = object;
  4015. ShapeBase *clientObj = dynamic_cast< ShapeBase* > ( object->getClientObject() );
  4016. // Check the mapTo name exists for this shape
  4017. S32 matIndex = serverObj->getShape()->materialList->getMaterialNameList().find_next(String(mapTo));
  4018. if (matIndex < 0)
  4019. {
  4020. Con::errorf("ShapeBase::changeMaterial failed: Invalid mapTo name '%s'", mapTo);
  4021. return;
  4022. }
  4023. // Lets remap the old material off, so as to let room for our current material room to claim its spot
  4024. if( oldMat )
  4025. oldMat->mMapTo = String("unmapped_mat");
  4026. newMat->mMapTo = mapTo;
  4027. // Map the material by name in the matmgr
  4028. MATMGR->mapMaterial( mapTo, newMat->getName() );
  4029. // Replace instances with the new material being traded in. For ShapeBase
  4030. // class we have to update the server/client objects separately so both
  4031. // represent our changes
  4032. delete serverObj->getShape()->materialList->mMatInstList[matIndex];
  4033. serverObj->getShape()->materialList->mMatInstList[matIndex] = newMat->createMatInstance();
  4034. if (clientObj)
  4035. {
  4036. delete clientObj->getShape()->materialList->mMatInstList[matIndex];
  4037. clientObj->getShape()->materialList->mMatInstList[matIndex] = newMat->createMatInstance();
  4038. }
  4039. // Finish up preparing the material instances for rendering
  4040. const GFXVertexFormat *flags = getGFXVertexFormat<GFXVertexPNTTB>();
  4041. FeatureSet features = MATMGR->getDefaultFeatures();
  4042. serverObj->getShape()->materialList->getMaterialInst(matIndex)->init( features, flags );
  4043. if (clientObj)
  4044. clientObj->getShapeInstance()->mMaterialList->getMaterialInst(matIndex)->init( features, flags );
  4045. }
  4046. DefineEngineMethod( ShapeBase, getModelFile, const char *, (),,
  4047. "@brief Get the model filename used by this shape.\n\n"
  4048. "@return the shape filename\n\n" )
  4049. {
  4050. GameBaseData * datablock = object->getDataBlock();
  4051. if( !datablock )
  4052. return String::EmptyString;
  4053. const char *fieldName = StringTable->insert( String("shapeFile") );
  4054. return datablock->getDataField( fieldName, NULL );
  4055. }