Anim_Blend.cpp 118 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "../Game_local.h"
  23. static const char *channelNames[ ANIM_NumAnimChannels ] = {
  24. "all", "torso", "legs", "head", "eyelids"
  25. };
  26. /***********************************************************************
  27. idAnim
  28. ***********************************************************************/
  29. /*
  30. =====================
  31. idAnim::idAnim
  32. =====================
  33. */
  34. idAnim::idAnim() {
  35. modelDef = NULL;
  36. numAnims = 0;
  37. memset( anims, 0, sizeof( anims ) );
  38. memset( &flags, 0, sizeof( flags ) );
  39. }
  40. /*
  41. =====================
  42. idAnim::idAnim
  43. =====================
  44. */
  45. idAnim::idAnim( const idDeclModelDef *modelDef, const idAnim *anim ) {
  46. int i;
  47. this->modelDef = modelDef;
  48. numAnims = anim->numAnims;
  49. name = anim->name;
  50. realname = anim->realname;
  51. flags = anim->flags;
  52. memset( anims, 0, sizeof( anims ) );
  53. for( i = 0; i < numAnims; i++ ) {
  54. anims[ i ] = anim->anims[ i ];
  55. anims[ i ]->IncreaseRefs();
  56. }
  57. frameLookup.SetNum( anim->frameLookup.Num() );
  58. memcpy( frameLookup.Ptr(), anim->frameLookup.Ptr(), frameLookup.MemoryUsed() );
  59. frameCommands.SetNum( anim->frameCommands.Num() );
  60. for( i = 0; i < frameCommands.Num(); i++ ) {
  61. frameCommands[ i ] = anim->frameCommands[ i ];
  62. if ( anim->frameCommands[ i ].string ) {
  63. frameCommands[ i ].string = new idStr( *anim->frameCommands[ i ].string );
  64. }
  65. }
  66. }
  67. /*
  68. =====================
  69. idAnim::~idAnim
  70. =====================
  71. */
  72. idAnim::~idAnim() {
  73. int i;
  74. for( i = 0; i < numAnims; i++ ) {
  75. anims[ i ]->DecreaseRefs();
  76. }
  77. for( i = 0; i < frameCommands.Num(); i++ ) {
  78. delete frameCommands[ i ].string;
  79. }
  80. }
  81. /*
  82. =====================
  83. idAnim::SetAnim
  84. =====================
  85. */
  86. void idAnim::SetAnim( const idDeclModelDef *modelDef, const char *sourcename, const char *animname, int num, const idMD5Anim *md5anims[ ANIM_MaxSyncedAnims ] ) {
  87. int i;
  88. this->modelDef = modelDef;
  89. for( i = 0; i < numAnims; i++ ) {
  90. anims[ i ]->DecreaseRefs();
  91. anims[ i ] = NULL;
  92. }
  93. assert( ( num > 0 ) && ( num <= ANIM_MaxSyncedAnims ) );
  94. numAnims = num;
  95. realname = sourcename;
  96. name = animname;
  97. for( i = 0; i < num; i++ ) {
  98. anims[ i ] = md5anims[ i ];
  99. anims[ i ]->IncreaseRefs();
  100. }
  101. memset( &flags, 0, sizeof( flags ) );
  102. for( i = 0; i < frameCommands.Num(); i++ ) {
  103. delete frameCommands[ i ].string;
  104. }
  105. frameLookup.Clear();
  106. frameCommands.Clear();
  107. }
  108. /*
  109. =====================
  110. idAnim::Name
  111. =====================
  112. */
  113. const char *idAnim::Name( void ) const {
  114. return name;
  115. }
  116. /*
  117. =====================
  118. idAnim::FullName
  119. =====================
  120. */
  121. const char *idAnim::FullName( void ) const {
  122. return realname;
  123. }
  124. /*
  125. =====================
  126. idAnim::MD5Anim
  127. index 0 will never be NULL. Any anim >= NumAnims will return NULL.
  128. =====================
  129. */
  130. const idMD5Anim *idAnim::MD5Anim( int num ) const {
  131. if ( anims == NULL || anims[0] == NULL ) {
  132. return NULL;
  133. }
  134. return anims[ num ];
  135. }
  136. /*
  137. =====================
  138. idAnim::ModelDef
  139. =====================
  140. */
  141. const idDeclModelDef *idAnim::ModelDef( void ) const {
  142. return modelDef;
  143. }
  144. /*
  145. =====================
  146. idAnim::Length
  147. =====================
  148. */
  149. int idAnim::Length( void ) const {
  150. if ( !anims[ 0 ] ) {
  151. return 0;
  152. }
  153. return anims[ 0 ]->Length();
  154. }
  155. /*
  156. =====================
  157. idAnim::NumFrames
  158. =====================
  159. */
  160. int idAnim::NumFrames( void ) const {
  161. if ( !anims[ 0 ] ) {
  162. return 0;
  163. }
  164. return anims[ 0 ]->NumFrames();
  165. }
  166. /*
  167. =====================
  168. idAnim::NumAnims
  169. =====================
  170. */
  171. int idAnim::NumAnims( void ) const {
  172. return numAnims;
  173. }
  174. /*
  175. =====================
  176. idAnim::TotalMovementDelta
  177. =====================
  178. */
  179. const idVec3 &idAnim::TotalMovementDelta( void ) const {
  180. if ( !anims[ 0 ] ) {
  181. return vec3_zero;
  182. }
  183. return anims[ 0 ]->TotalMovementDelta();
  184. }
  185. /*
  186. =====================
  187. idAnim::GetOrigin
  188. =====================
  189. */
  190. bool idAnim::GetOrigin( idVec3 &offset, int animNum, int currentTime, int cyclecount ) const {
  191. if ( !anims[ animNum ] ) {
  192. offset.Zero();
  193. return false;
  194. }
  195. anims[ animNum ]->GetOrigin( offset, currentTime, cyclecount );
  196. return true;
  197. }
  198. /*
  199. =====================
  200. idAnim::GetOriginRotation
  201. =====================
  202. */
  203. bool idAnim::GetOriginRotation( idQuat &rotation, int animNum, int currentTime, int cyclecount ) const {
  204. if ( !anims[ animNum ] ) {
  205. rotation.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  206. return false;
  207. }
  208. anims[ animNum ]->GetOriginRotation( rotation, currentTime, cyclecount );
  209. return true;
  210. }
  211. /*
  212. =====================
  213. idAnim::GetBounds
  214. =====================
  215. */
  216. ID_INLINE bool idAnim::GetBounds( idBounds &bounds, int animNum, int currentTime, int cyclecount ) const {
  217. if ( !anims[ animNum ] ) {
  218. return false;
  219. }
  220. anims[ animNum ]->GetBounds( bounds, currentTime, cyclecount );
  221. return true;
  222. }
  223. /*
  224. =====================
  225. idAnim::AddFrameCommand
  226. Returns NULL if no error.
  227. =====================
  228. */
  229. const char *idAnim::AddFrameCommand( const idDeclModelDef *modelDef, int framenum, idLexer &src, const idDict *def ) {
  230. int i;
  231. int index;
  232. idStr text;
  233. idStr funcname;
  234. frameCommand_t fc;
  235. idToken token;
  236. const jointInfo_t *jointInfo;
  237. // make sure we're within bounds
  238. if ( ( framenum < 1 ) || ( framenum > anims[ 0 ]->NumFrames() ) ) {
  239. return va( "Frame %d out of range", framenum );
  240. }
  241. // frame numbers are 1 based in .def files, but 0 based internally
  242. framenum--;
  243. memset( &fc, 0, sizeof( fc ) );
  244. if( !src.ReadTokenOnLine( &token ) ) {
  245. return "Unexpected end of line";
  246. }
  247. if ( token == "call" ) {
  248. if( !src.ReadTokenOnLine( &token ) ) {
  249. return "Unexpected end of line";
  250. }
  251. fc.type = FC_SCRIPTFUNCTION;
  252. fc.function = gameLocal.program.FindFunction( token );
  253. if ( !fc.function ) {
  254. return va( "Function '%s' not found", token.c_str() );
  255. }
  256. } else if ( token == "object_call" ) {
  257. if( !src.ReadTokenOnLine( &token ) ) {
  258. return "Unexpected end of line";
  259. }
  260. fc.type = FC_SCRIPTFUNCTIONOBJECT;
  261. fc.string = new idStr( token );
  262. } else if ( token == "event" ) {
  263. if( !src.ReadTokenOnLine( &token ) ) {
  264. return "Unexpected end of line";
  265. }
  266. fc.type = FC_EVENTFUNCTION;
  267. const idEventDef *ev = idEventDef::FindEvent( token );
  268. if ( !ev ) {
  269. return va( "Event '%s' not found", token.c_str() );
  270. }
  271. if ( ev->GetNumArgs() != 0 ) {
  272. return va( "Event '%s' has arguments", token.c_str() );
  273. }
  274. fc.string = new idStr( token );
  275. } else if ( token == "sound" ) {
  276. if( !src.ReadTokenOnLine( &token ) ) {
  277. return "Unexpected end of line";
  278. }
  279. fc.type = FC_SOUND;
  280. if ( !token.Cmpn( "snd_", 4 ) ) {
  281. fc.string = new idStr( token );
  282. } else {
  283. fc.soundShader = declManager->FindSound( token );
  284. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  285. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  286. }
  287. }
  288. } else if ( token == "sound_voice" ) {
  289. if( !src.ReadTokenOnLine( &token ) ) {
  290. return "Unexpected end of line";
  291. }
  292. fc.type = FC_SOUND_VOICE;
  293. if ( !token.Cmpn( "snd_", 4 ) ) {
  294. fc.string = new idStr( token );
  295. } else {
  296. fc.soundShader = declManager->FindSound( token );
  297. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  298. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  299. }
  300. }
  301. } else if ( token == "sound_voice2" ) {
  302. if( !src.ReadTokenOnLine( &token ) ) {
  303. return "Unexpected end of line";
  304. }
  305. fc.type = FC_SOUND_VOICE2;
  306. if ( !token.Cmpn( "snd_", 4 ) ) {
  307. fc.string = new idStr( token );
  308. } else {
  309. fc.soundShader = declManager->FindSound( token );
  310. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  311. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  312. }
  313. }
  314. } else if ( token == "sound_body" ) {
  315. if( !src.ReadTokenOnLine( &token ) ) {
  316. return "Unexpected end of line";
  317. }
  318. fc.type = FC_SOUND_BODY;
  319. if ( !token.Cmpn( "snd_", 4 ) ) {
  320. fc.string = new idStr( token );
  321. } else {
  322. fc.soundShader = declManager->FindSound( token );
  323. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  324. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  325. }
  326. }
  327. } else if ( token == "sound_body2" ) {
  328. if( !src.ReadTokenOnLine( &token ) ) {
  329. return "Unexpected end of line";
  330. }
  331. fc.type = FC_SOUND_BODY2;
  332. if ( !token.Cmpn( "snd_", 4 ) ) {
  333. fc.string = new idStr( token );
  334. } else {
  335. fc.soundShader = declManager->FindSound( token );
  336. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  337. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  338. }
  339. }
  340. } else if ( token == "sound_body3" ) {
  341. if( !src.ReadTokenOnLine( &token ) ) {
  342. return "Unexpected end of line";
  343. }
  344. fc.type = FC_SOUND_BODY3;
  345. if ( !token.Cmpn( "snd_", 4 ) ) {
  346. fc.string = new idStr( token );
  347. } else {
  348. fc.soundShader = declManager->FindSound( token );
  349. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  350. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  351. }
  352. }
  353. } else if ( token == "sound_weapon" ) {
  354. if( !src.ReadTokenOnLine( &token ) ) {
  355. return "Unexpected end of line";
  356. }
  357. fc.type = FC_SOUND_WEAPON;
  358. if ( !token.Cmpn( "snd_", 4 ) ) {
  359. fc.string = new idStr( token );
  360. } else {
  361. fc.soundShader = declManager->FindSound( token );
  362. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  363. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  364. }
  365. }
  366. } else if ( token == "sound_global" ) {
  367. if( !src.ReadTokenOnLine( &token ) ) {
  368. return "Unexpected end of line";
  369. }
  370. fc.type = FC_SOUND_GLOBAL;
  371. if ( !token.Cmpn( "snd_", 4 ) ) {
  372. fc.string = new idStr( token );
  373. } else {
  374. fc.soundShader = declManager->FindSound( token );
  375. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  376. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  377. }
  378. }
  379. } else if ( token == "sound_item" ) {
  380. if( !src.ReadTokenOnLine( &token ) ) {
  381. return "Unexpected end of line";
  382. }
  383. fc.type = FC_SOUND_ITEM;
  384. if ( !token.Cmpn( "snd_", 4 ) ) {
  385. fc.string = new idStr( token );
  386. } else {
  387. fc.soundShader = declManager->FindSound( token );
  388. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  389. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  390. }
  391. }
  392. } else if ( token == "sound_chatter" ) {
  393. if( !src.ReadTokenOnLine( &token ) ) {
  394. return "Unexpected end of line";
  395. }
  396. fc.type = FC_SOUND_CHATTER;
  397. if ( !token.Cmpn( "snd_", 4 ) ) {
  398. fc.string = new idStr( token );
  399. } else {
  400. fc.soundShader = declManager->FindSound( token );
  401. if ( fc.soundShader->GetState() == DS_DEFAULTED ) {
  402. gameLocal.Warning( "Sound '%s' not found", token.c_str() );
  403. }
  404. }
  405. } else if ( token == "skin" ) {
  406. if( !src.ReadTokenOnLine( &token ) ) {
  407. return "Unexpected end of line";
  408. }
  409. fc.type = FC_SKIN;
  410. if ( token == "none" ) {
  411. fc.skin = NULL;
  412. } else {
  413. fc.skin = declManager->FindSkin( token );
  414. if ( !fc.skin ) {
  415. return va( "Skin '%s' not found", token.c_str() );
  416. }
  417. }
  418. } else if ( token == "fx" ) {
  419. if( !src.ReadTokenOnLine( &token ) ) {
  420. return "Unexpected end of line";
  421. }
  422. fc.type = FC_FX;
  423. if ( !declManager->FindType( DECL_FX, token.c_str() ) ) {
  424. return va( "fx '%s' not found", token.c_str() );
  425. }
  426. fc.string = new idStr( token );
  427. } else if ( token == "trigger" ) {
  428. if( !src.ReadTokenOnLine( &token ) ) {
  429. return "Unexpected end of line";
  430. }
  431. fc.type = FC_TRIGGER;
  432. fc.string = new idStr( token );
  433. } else if ( token == "triggerSmokeParticle" ) {
  434. if( !src.ReadTokenOnLine( &token ) ) {
  435. return "Unexpected end of line";
  436. }
  437. fc.type = FC_TRIGGER_SMOKE_PARTICLE;
  438. fc.string = new idStr( token );
  439. } else if ( token == "melee" ) {
  440. if( !src.ReadTokenOnLine( &token ) ) {
  441. return "Unexpected end of line";
  442. }
  443. fc.type = FC_MELEE;
  444. if ( !gameLocal.FindEntityDef( token.c_str(), false ) ) {
  445. return va( "Unknown entityDef '%s'", token.c_str() );
  446. }
  447. fc.string = new idStr( token );
  448. } else if ( token == "direct_damage" ) {
  449. if( !src.ReadTokenOnLine( &token ) ) {
  450. return "Unexpected end of line";
  451. }
  452. fc.type = FC_DIRECTDAMAGE;
  453. if ( !gameLocal.FindEntityDef( token.c_str(), false ) ) {
  454. return va( "Unknown entityDef '%s'", token.c_str() );
  455. }
  456. fc.string = new idStr( token );
  457. } else if ( token == "attack_begin" ) {
  458. if( !src.ReadTokenOnLine( &token ) ) {
  459. return "Unexpected end of line";
  460. }
  461. fc.type = FC_BEGINATTACK;
  462. if ( !gameLocal.FindEntityDef( token.c_str(), false ) ) {
  463. return va( "Unknown entityDef '%s'", token.c_str() );
  464. }
  465. fc.string = new idStr( token );
  466. } else if ( token == "attack_end" ) {
  467. fc.type = FC_ENDATTACK;
  468. } else if ( token == "muzzle_flash" ) {
  469. if( !src.ReadTokenOnLine( &token ) ) {
  470. return "Unexpected end of line";
  471. }
  472. if ( ( token != "" ) && !modelDef->FindJoint( token ) ) {
  473. return va( "Joint '%s' not found", token.c_str() );
  474. }
  475. fc.type = FC_MUZZLEFLASH;
  476. fc.string = new idStr( token );
  477. } else if ( token == "muzzle_flash" ) {
  478. fc.type = FC_MUZZLEFLASH;
  479. fc.string = new idStr( "" );
  480. } else if ( token == "create_missile" ) {
  481. if( !src.ReadTokenOnLine( &token ) ) {
  482. return "Unexpected end of line";
  483. }
  484. if ( !modelDef->FindJoint( token ) ) {
  485. return va( "Joint '%s' not found", token.c_str() );
  486. }
  487. fc.type = FC_CREATEMISSILE;
  488. fc.string = new idStr( token );
  489. } else if ( token == "launch_missile" ) {
  490. if( !src.ReadTokenOnLine( &token ) ) {
  491. return "Unexpected end of line";
  492. }
  493. if ( !modelDef->FindJoint( token ) ) {
  494. return va( "Joint '%s' not found", token.c_str() );
  495. }
  496. fc.type = FC_LAUNCHMISSILE;
  497. fc.string = new idStr( token );
  498. } else if ( token == "fire_missile_at_target" ) {
  499. if( !src.ReadTokenOnLine( &token ) ) {
  500. return "Unexpected end of line";
  501. }
  502. jointInfo = modelDef->FindJoint( token );
  503. if ( !jointInfo ) {
  504. return va( "Joint '%s' not found", token.c_str() );
  505. }
  506. if( !src.ReadTokenOnLine( &token ) ) {
  507. return "Unexpected end of line";
  508. }
  509. fc.type = FC_FIREMISSILEATTARGET;
  510. fc.string = new idStr( token );
  511. fc.index = jointInfo->num;
  512. } else if ( token == "footstep" ) {
  513. fc.type = FC_FOOTSTEP;
  514. } else if ( token == "leftfoot" ) {
  515. fc.type = FC_LEFTFOOT;
  516. } else if ( token == "rightfoot" ) {
  517. fc.type = FC_RIGHTFOOT;
  518. } else if ( token == "enableEyeFocus" ) {
  519. fc.type = FC_ENABLE_EYE_FOCUS;
  520. } else if ( token == "disableEyeFocus" ) {
  521. fc.type = FC_DISABLE_EYE_FOCUS;
  522. } else if ( token == "disableGravity" ) {
  523. fc.type = FC_DISABLE_GRAVITY;
  524. } else if ( token == "enableGravity" ) {
  525. fc.type = FC_ENABLE_GRAVITY;
  526. } else if ( token == "jump" ) {
  527. fc.type = FC_JUMP;
  528. } else if ( token == "enableClip" ) {
  529. fc.type = FC_ENABLE_CLIP;
  530. } else if ( token == "disableClip" ) {
  531. fc.type = FC_DISABLE_CLIP;
  532. } else if ( token == "enableWalkIK" ) {
  533. fc.type = FC_ENABLE_WALK_IK;
  534. } else if ( token == "disableWalkIK" ) {
  535. fc.type = FC_DISABLE_WALK_IK;
  536. } else if ( token == "enableLegIK" ) {
  537. if( !src.ReadTokenOnLine( &token ) ) {
  538. return "Unexpected end of line";
  539. }
  540. fc.type = FC_ENABLE_LEG_IK;
  541. fc.index = atoi( token );
  542. } else if ( token == "disableLegIK" ) {
  543. if( !src.ReadTokenOnLine( &token ) ) {
  544. return "Unexpected end of line";
  545. }
  546. fc.type = FC_DISABLE_LEG_IK;
  547. fc.index = atoi( token );
  548. } else if ( token == "recordDemo" ) {
  549. fc.type = FC_RECORDDEMO;
  550. if( src.ReadTokenOnLine( &token ) ) {
  551. fc.string = new idStr( token );
  552. }
  553. } else if ( token == "aviGame" ) {
  554. fc.type = FC_AVIGAME;
  555. if( src.ReadTokenOnLine( &token ) ) {
  556. fc.string = new idStr( token );
  557. }
  558. } else {
  559. return va( "Unknown command '%s'", token.c_str() );
  560. }
  561. // check if we've initialized the frame loopup table
  562. if ( !frameLookup.Num() ) {
  563. // we haven't, so allocate the table and initialize it
  564. frameLookup.SetGranularity( 1 );
  565. frameLookup.SetNum( anims[ 0 ]->NumFrames() );
  566. for( i = 0; i < frameLookup.Num(); i++ ) {
  567. frameLookup[ i ].num = 0;
  568. frameLookup[ i ].firstCommand = 0;
  569. }
  570. }
  571. // allocate space for a new command
  572. frameCommands.Alloc();
  573. // calculate the index of the new command
  574. index = frameLookup[ framenum ].firstCommand + frameLookup[ framenum ].num;
  575. // move all commands from our index onward up one to give us space for our new command
  576. for( i = frameCommands.Num() - 1; i > index; i-- ) {
  577. frameCommands[ i ] = frameCommands[ i - 1 ];
  578. }
  579. // fix the indices of any later frames to account for the inserted command
  580. for( i = framenum + 1; i < frameLookup.Num(); i++ ) {
  581. frameLookup[ i ].firstCommand++;
  582. }
  583. // store the new command
  584. frameCommands[ index ] = fc;
  585. // increase the number of commands on this frame
  586. frameLookup[ framenum ].num++;
  587. // return with no error
  588. return NULL;
  589. }
  590. /*
  591. =====================
  592. idAnim::CallFrameCommands
  593. =====================
  594. */
  595. void idAnim::CallFrameCommands( idEntity *ent, int from, int to ) const {
  596. int index;
  597. int end;
  598. int frame;
  599. int numframes;
  600. numframes = anims[ 0 ]->NumFrames();
  601. frame = from;
  602. while( frame != to ) {
  603. frame++;
  604. if ( frame >= numframes ) {
  605. frame = 0;
  606. }
  607. index = frameLookup[ frame ].firstCommand;
  608. end = index + frameLookup[ frame ].num;
  609. while( index < end ) {
  610. const frameCommand_t &command = frameCommands[ index++ ];
  611. switch( command.type ) {
  612. case FC_SCRIPTFUNCTION: {
  613. gameLocal.CallFrameCommand( ent, command.function );
  614. break;
  615. }
  616. case FC_SCRIPTFUNCTIONOBJECT: {
  617. gameLocal.CallObjectFrameCommand( ent, command.string->c_str() );
  618. break;
  619. }
  620. case FC_EVENTFUNCTION: {
  621. const idEventDef *ev = idEventDef::FindEvent( command.string->c_str() );
  622. ent->ProcessEvent( ev );
  623. break;
  624. }
  625. case FC_SOUND: {
  626. if ( !command.soundShader ) {
  627. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_ANY, 0, false, NULL ) ) {
  628. gameLocal.Warning( "Framecommand 'sound' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  629. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  630. }
  631. } else {
  632. ent->StartSoundShader( command.soundShader, SND_CHANNEL_ANY, 0, false, NULL );
  633. }
  634. break;
  635. }
  636. case FC_SOUND_VOICE: {
  637. if ( !command.soundShader ) {
  638. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_VOICE, 0, false, NULL ) ) {
  639. gameLocal.Warning( "Framecommand 'sound_voice' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  640. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  641. }
  642. } else {
  643. ent->StartSoundShader( command.soundShader, SND_CHANNEL_VOICE, 0, false, NULL );
  644. }
  645. break;
  646. }
  647. case FC_SOUND_VOICE2: {
  648. if ( !command.soundShader ) {
  649. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_VOICE2, 0, false, NULL ) ) {
  650. gameLocal.Warning( "Framecommand 'sound_voice2' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  651. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  652. }
  653. } else {
  654. ent->StartSoundShader( command.soundShader, SND_CHANNEL_VOICE2, 0, false, NULL );
  655. }
  656. break;
  657. }
  658. case FC_SOUND_BODY: {
  659. if ( !command.soundShader ) {
  660. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_BODY, 0, false, NULL ) ) {
  661. gameLocal.Warning( "Framecommand 'sound_body' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  662. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  663. }
  664. } else {
  665. ent->StartSoundShader( command.soundShader, SND_CHANNEL_BODY, 0, false, NULL );
  666. }
  667. break;
  668. }
  669. case FC_SOUND_BODY2: {
  670. if ( !command.soundShader ) {
  671. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_BODY2, 0, false, NULL ) ) {
  672. gameLocal.Warning( "Framecommand 'sound_body2' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  673. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  674. }
  675. } else {
  676. ent->StartSoundShader( command.soundShader, SND_CHANNEL_BODY2, 0, false, NULL );
  677. }
  678. break;
  679. }
  680. case FC_SOUND_BODY3: {
  681. if ( !command.soundShader ) {
  682. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_BODY3, 0, false, NULL ) ) {
  683. gameLocal.Warning( "Framecommand 'sound_body3' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  684. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  685. }
  686. } else {
  687. ent->StartSoundShader( command.soundShader, SND_CHANNEL_BODY3, 0, false, NULL );
  688. }
  689. break;
  690. }
  691. case FC_SOUND_WEAPON: {
  692. if ( !command.soundShader ) {
  693. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_WEAPON, 0, false, NULL ) ) {
  694. gameLocal.Warning( "Framecommand 'sound_weapon' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  695. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  696. }
  697. } else {
  698. ent->StartSoundShader( command.soundShader, SND_CHANNEL_WEAPON, 0, false, NULL );
  699. }
  700. break;
  701. }
  702. case FC_SOUND_GLOBAL: {
  703. if ( !command.soundShader ) {
  704. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_ANY, SSF_GLOBAL, false, NULL ) ) {
  705. gameLocal.Warning( "Framecommand 'sound_global' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  706. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  707. }
  708. } else {
  709. ent->StartSoundShader( command.soundShader, SND_CHANNEL_ANY, SSF_GLOBAL, false, NULL );
  710. }
  711. break;
  712. }
  713. case FC_SOUND_ITEM: {
  714. if ( !command.soundShader ) {
  715. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_ITEM, 0, false, NULL ) ) {
  716. gameLocal.Warning( "Framecommand 'sound_item' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  717. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  718. }
  719. } else {
  720. ent->StartSoundShader( command.soundShader, SND_CHANNEL_ITEM, 0, false, NULL );
  721. }
  722. break;
  723. }
  724. case FC_SOUND_CHATTER: {
  725. if ( ent->CanPlayChatterSounds() ) {
  726. if ( !command.soundShader ) {
  727. if ( !ent->StartSound( command.string->c_str(), SND_CHANNEL_VOICE, 0, false, NULL ) ) {
  728. gameLocal.Warning( "Framecommand 'sound_chatter' on entity '%s', anim '%s', frame %d: Could not find sound '%s'",
  729. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  730. }
  731. } else {
  732. ent->StartSoundShader( command.soundShader, SND_CHANNEL_VOICE, 0, false, NULL );
  733. }
  734. }
  735. break;
  736. }
  737. case FC_FX: {
  738. idEntityFx::StartFx( command.string->c_str(), NULL, NULL, ent, true );
  739. break;
  740. }
  741. case FC_SKIN: {
  742. ent->SetSkin( command.skin );
  743. break;
  744. }
  745. case FC_TRIGGER: {
  746. idEntity *target;
  747. target = gameLocal.FindEntity( command.string->c_str() );
  748. if ( target ) {
  749. target->Signal( SIG_TRIGGER );
  750. target->ProcessEvent( &EV_Activate, ent );
  751. target->TriggerGuis();
  752. } else {
  753. gameLocal.Warning( "Framecommand 'trigger' on entity '%s', anim '%s', frame %d: Could not find entity '%s'",
  754. ent->name.c_str(), FullName(), frame + 1, command.string->c_str() );
  755. }
  756. break;
  757. }
  758. case FC_TRIGGER_SMOKE_PARTICLE: {
  759. ent->ProcessEvent( &AI_TriggerParticles, command.string->c_str() );
  760. break;
  761. }
  762. case FC_MELEE: {
  763. ent->ProcessEvent( &AI_AttackMelee, command.string->c_str() );
  764. break;
  765. }
  766. case FC_DIRECTDAMAGE: {
  767. ent->ProcessEvent( &AI_DirectDamage, command.string->c_str() );
  768. break;
  769. }
  770. case FC_BEGINATTACK: {
  771. ent->ProcessEvent( &AI_BeginAttack, command.string->c_str() );
  772. break;
  773. }
  774. case FC_ENDATTACK: {
  775. ent->ProcessEvent( &AI_EndAttack );
  776. break;
  777. }
  778. case FC_MUZZLEFLASH: {
  779. ent->ProcessEvent( &AI_MuzzleFlash, command.string->c_str() );
  780. break;
  781. }
  782. case FC_CREATEMISSILE: {
  783. ent->ProcessEvent( &AI_CreateMissile, command.string->c_str() );
  784. break;
  785. }
  786. case FC_LAUNCHMISSILE: {
  787. ent->ProcessEvent( &AI_AttackMissile, command.string->c_str() );
  788. break;
  789. }
  790. case FC_FIREMISSILEATTARGET: {
  791. ent->ProcessEvent( &AI_FireMissileAtTarget, modelDef->GetJointName( command.index ), command.string->c_str() );
  792. break;
  793. }
  794. case FC_FOOTSTEP : {
  795. ent->ProcessEvent( &EV_Footstep );
  796. break;
  797. }
  798. case FC_LEFTFOOT: {
  799. ent->ProcessEvent( &EV_FootstepLeft );
  800. break;
  801. }
  802. case FC_RIGHTFOOT: {
  803. ent->ProcessEvent( &EV_FootstepRight );
  804. break;
  805. }
  806. case FC_ENABLE_EYE_FOCUS: {
  807. ent->ProcessEvent( &AI_EnableEyeFocus );
  808. break;
  809. }
  810. case FC_DISABLE_EYE_FOCUS: {
  811. ent->ProcessEvent( &AI_DisableEyeFocus );
  812. break;
  813. }
  814. case FC_DISABLE_GRAVITY: {
  815. ent->ProcessEvent( &AI_DisableGravity );
  816. break;
  817. }
  818. case FC_ENABLE_GRAVITY: {
  819. ent->ProcessEvent( &AI_EnableGravity );
  820. break;
  821. }
  822. case FC_JUMP: {
  823. ent->ProcessEvent( &AI_JumpFrame );
  824. break;
  825. }
  826. case FC_ENABLE_CLIP: {
  827. ent->ProcessEvent( &AI_EnableClip );
  828. break;
  829. }
  830. case FC_DISABLE_CLIP: {
  831. ent->ProcessEvent( &AI_DisableClip );
  832. break;
  833. }
  834. case FC_ENABLE_WALK_IK: {
  835. ent->ProcessEvent( &EV_EnableWalkIK );
  836. break;
  837. }
  838. case FC_DISABLE_WALK_IK: {
  839. ent->ProcessEvent( &EV_DisableWalkIK );
  840. break;
  841. }
  842. case FC_ENABLE_LEG_IK: {
  843. ent->ProcessEvent( &EV_EnableLegIK, command.index );
  844. break;
  845. }
  846. case FC_DISABLE_LEG_IK: {
  847. ent->ProcessEvent( &EV_DisableLegIK, command.index );
  848. break;
  849. }
  850. case FC_RECORDDEMO: {
  851. if ( command.string ) {
  852. cmdSystem->BufferCommandText( CMD_EXEC_NOW, va( "recordDemo %s", command.string->c_str() ) );
  853. } else {
  854. cmdSystem->BufferCommandText( CMD_EXEC_NOW, "stoprecording" );
  855. }
  856. break;
  857. }
  858. case FC_AVIGAME: {
  859. if ( command.string ) {
  860. cmdSystem->BufferCommandText( CMD_EXEC_NOW, va( "aviGame %s", command.string->c_str() ) );
  861. } else {
  862. cmdSystem->BufferCommandText( CMD_EXEC_NOW, "aviGame" );
  863. }
  864. break;
  865. }
  866. }
  867. }
  868. }
  869. }
  870. /*
  871. =====================
  872. idAnim::FindFrameForFrameCommand
  873. =====================
  874. */
  875. int idAnim::FindFrameForFrameCommand( frameCommandType_t framecommand, const frameCommand_t **command ) const {
  876. int frame;
  877. int index;
  878. int numframes;
  879. int end;
  880. if ( !frameCommands.Num() ) {
  881. return -1;
  882. }
  883. numframes = anims[ 0 ]->NumFrames();
  884. for( frame = 0; frame < numframes; frame++ ) {
  885. end = frameLookup[ frame ].firstCommand + frameLookup[ frame ].num;
  886. for( index = frameLookup[ frame ].firstCommand; index < end; index++ ) {
  887. if ( frameCommands[ index ].type == framecommand ) {
  888. if ( command ) {
  889. *command = &frameCommands[ index ];
  890. }
  891. return frame;
  892. }
  893. }
  894. }
  895. if ( command ) {
  896. *command = NULL;
  897. }
  898. return -1;
  899. }
  900. /*
  901. =====================
  902. idAnim::HasFrameCommands
  903. =====================
  904. */
  905. bool idAnim::HasFrameCommands( void ) const {
  906. if ( !frameCommands.Num() ) {
  907. return false;
  908. }
  909. return true;
  910. }
  911. /*
  912. =====================
  913. idAnim::SetAnimFlags
  914. =====================
  915. */
  916. void idAnim::SetAnimFlags( const animFlags_t &animflags ) {
  917. flags = animflags;
  918. }
  919. /*
  920. =====================
  921. idAnim::GetAnimFlags
  922. =====================
  923. */
  924. const animFlags_t &idAnim::GetAnimFlags( void ) const {
  925. return flags;
  926. }
  927. /***********************************************************************
  928. idAnimBlend
  929. ***********************************************************************/
  930. /*
  931. =====================
  932. idAnimBlend::idAnimBlend
  933. =====================
  934. */
  935. idAnimBlend::idAnimBlend( void ) {
  936. Reset( NULL );
  937. }
  938. /*
  939. =====================
  940. idAnimBlend::Save
  941. archives object for save game file
  942. =====================
  943. */
  944. void idAnimBlend::Save( idSaveGame *savefile ) const {
  945. int i;
  946. savefile->WriteInt( starttime );
  947. savefile->WriteInt( endtime );
  948. savefile->WriteInt( timeOffset );
  949. savefile->WriteFloat( rate );
  950. savefile->WriteInt( blendStartTime );
  951. savefile->WriteInt( blendDuration );
  952. savefile->WriteFloat( blendStartValue );
  953. savefile->WriteFloat( blendEndValue );
  954. for( i = 0; i < ANIM_MaxSyncedAnims; i++ ) {
  955. savefile->WriteFloat( animWeights[ i ] );
  956. }
  957. savefile->WriteShort( cycle );
  958. savefile->WriteShort( frame );
  959. savefile->WriteShort( animNum );
  960. savefile->WriteBool( allowMove );
  961. savefile->WriteBool( allowFrameCommands );
  962. }
  963. /*
  964. =====================
  965. idAnimBlend::Restore
  966. unarchives object from save game file
  967. =====================
  968. */
  969. void idAnimBlend::Restore( idRestoreGame *savefile, const idDeclModelDef *modelDef ) {
  970. int i;
  971. this->modelDef = modelDef;
  972. savefile->ReadInt( starttime );
  973. savefile->ReadInt( endtime );
  974. savefile->ReadInt( timeOffset );
  975. savefile->ReadFloat( rate );
  976. savefile->ReadInt( blendStartTime );
  977. savefile->ReadInt( blendDuration );
  978. savefile->ReadFloat( blendStartValue );
  979. savefile->ReadFloat( blendEndValue );
  980. for( i = 0; i < ANIM_MaxSyncedAnims; i++ ) {
  981. savefile->ReadFloat( animWeights[ i ] );
  982. }
  983. savefile->ReadShort( cycle );
  984. savefile->ReadShort( frame );
  985. savefile->ReadShort( animNum );
  986. if ( !modelDef ) {
  987. animNum = 0;
  988. } else if ( ( animNum < 0 ) || ( animNum > modelDef->NumAnims() ) ) {
  989. gameLocal.Warning( "Anim number %d out of range for model '%s' during save game", animNum, modelDef->GetModelName() );
  990. animNum = 0;
  991. }
  992. savefile->ReadBool( allowMove );
  993. savefile->ReadBool( allowFrameCommands );
  994. }
  995. /*
  996. =====================
  997. idAnimBlend::Reset
  998. =====================
  999. */
  1000. void idAnimBlend::Reset( const idDeclModelDef *_modelDef ) {
  1001. modelDef = _modelDef;
  1002. cycle = 1;
  1003. starttime = 0;
  1004. endtime = 0;
  1005. timeOffset = 0;
  1006. rate = 1.0f;
  1007. frame = 0;
  1008. allowMove = true;
  1009. allowFrameCommands = true;
  1010. animNum = 0;
  1011. memset( animWeights, 0, sizeof( animWeights ) );
  1012. blendStartValue = 0.0f;
  1013. blendEndValue = 0.0f;
  1014. blendStartTime = 0;
  1015. blendDuration = 0;
  1016. }
  1017. /*
  1018. =====================
  1019. idAnimBlend::FullName
  1020. =====================
  1021. */
  1022. const char *idAnimBlend::AnimFullName( void ) const {
  1023. const idAnim *anim = Anim();
  1024. if ( !anim ) {
  1025. return "";
  1026. }
  1027. return anim->FullName();
  1028. }
  1029. /*
  1030. =====================
  1031. idAnimBlend::AnimName
  1032. =====================
  1033. */
  1034. const char *idAnimBlend::AnimName( void ) const {
  1035. const idAnim *anim = Anim();
  1036. if ( !anim ) {
  1037. return "";
  1038. }
  1039. return anim->Name();
  1040. }
  1041. /*
  1042. =====================
  1043. idAnimBlend::NumFrames
  1044. =====================
  1045. */
  1046. int idAnimBlend::NumFrames( void ) const {
  1047. const idAnim *anim = Anim();
  1048. if ( !anim ) {
  1049. return 0;
  1050. }
  1051. return anim->NumFrames();
  1052. }
  1053. /*
  1054. =====================
  1055. idAnimBlend::Length
  1056. =====================
  1057. */
  1058. int idAnimBlend::Length( void ) const {
  1059. const idAnim *anim = Anim();
  1060. if ( !anim ) {
  1061. return 0;
  1062. }
  1063. return anim->Length();
  1064. }
  1065. /*
  1066. =====================
  1067. idAnimBlend::GetWeight
  1068. =====================
  1069. */
  1070. float idAnimBlend::GetWeight( int currentTime ) const {
  1071. int timeDelta;
  1072. float frac;
  1073. float w;
  1074. timeDelta = currentTime - blendStartTime;
  1075. if ( timeDelta <= 0 ) {
  1076. w = blendStartValue;
  1077. } else if ( timeDelta >= blendDuration ) {
  1078. w = blendEndValue;
  1079. } else {
  1080. frac = ( float )timeDelta / ( float )blendDuration;
  1081. w = blendStartValue + ( blendEndValue - blendStartValue ) * frac;
  1082. }
  1083. return w;
  1084. }
  1085. /*
  1086. =====================
  1087. idAnimBlend::GetFinalWeight
  1088. =====================
  1089. */
  1090. float idAnimBlend::GetFinalWeight( void ) const {
  1091. return blendEndValue;
  1092. }
  1093. /*
  1094. =====================
  1095. idAnimBlend::SetWeight
  1096. =====================
  1097. */
  1098. void idAnimBlend::SetWeight( float newweight, int currentTime, int blendTime ) {
  1099. blendStartValue = GetWeight( currentTime );
  1100. blendEndValue = newweight;
  1101. blendStartTime = currentTime - 1;
  1102. blendDuration = blendTime;
  1103. if ( !newweight ) {
  1104. endtime = currentTime + blendTime;
  1105. }
  1106. }
  1107. /*
  1108. =====================
  1109. idAnimBlend::NumSyncedAnims
  1110. =====================
  1111. */
  1112. int idAnimBlend::NumSyncedAnims( void ) const {
  1113. const idAnim *anim = Anim();
  1114. if ( !anim ) {
  1115. return 0;
  1116. }
  1117. return anim->NumAnims();
  1118. }
  1119. /*
  1120. =====================
  1121. idAnimBlend::SetSyncedAnimWeight
  1122. =====================
  1123. */
  1124. bool idAnimBlend::SetSyncedAnimWeight( int num, float weight ) {
  1125. const idAnim *anim = Anim();
  1126. if ( !anim ) {
  1127. return false;
  1128. }
  1129. if ( ( num < 0 ) || ( num > anim->NumAnims() ) ) {
  1130. return false;
  1131. }
  1132. animWeights[ num ] = weight;
  1133. return true;
  1134. }
  1135. /*
  1136. =====================
  1137. idAnimBlend::SetFrame
  1138. =====================
  1139. */
  1140. void idAnimBlend::SetFrame( const idDeclModelDef *modelDef, int _animNum, int _frame, int currentTime, int blendTime ) {
  1141. Reset( modelDef );
  1142. if ( !modelDef ) {
  1143. return;
  1144. }
  1145. const idAnim *_anim = modelDef->GetAnim( _animNum );
  1146. if ( !_anim ) {
  1147. return;
  1148. }
  1149. const idMD5Anim *md5anim = _anim->MD5Anim( 0 );
  1150. if ( modelDef->Joints().Num() != md5anim->NumJoints() ) {
  1151. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", modelDef->GetModelName(), md5anim->Name() );
  1152. return;
  1153. }
  1154. animNum = _animNum;
  1155. starttime = currentTime;
  1156. endtime = -1;
  1157. cycle = -1;
  1158. animWeights[ 0 ] = 1.0f;
  1159. frame = _frame;
  1160. // a frame of 0 means it's not a single frame blend, so we set it to frame + 1
  1161. if ( frame <= 0 ) {
  1162. frame = 1;
  1163. } else if ( frame > _anim->NumFrames() ) {
  1164. frame = _anim->NumFrames();
  1165. }
  1166. // set up blend
  1167. blendEndValue = 1.0f;
  1168. blendStartTime = currentTime - 1;
  1169. blendDuration = blendTime;
  1170. blendStartValue = 0.0f;
  1171. }
  1172. /*
  1173. =====================
  1174. idAnimBlend::CycleAnim
  1175. =====================
  1176. */
  1177. void idAnimBlend::CycleAnim( const idDeclModelDef *modelDef, int _animNum, int currentTime, int blendTime ) {
  1178. Reset( modelDef );
  1179. if ( !modelDef ) {
  1180. return;
  1181. }
  1182. const idAnim *_anim = modelDef->GetAnim( _animNum );
  1183. if ( !_anim ) {
  1184. return;
  1185. }
  1186. const idMD5Anim *md5anim = _anim->MD5Anim( 0 );
  1187. if ( modelDef->Joints().Num() != md5anim->NumJoints() ) {
  1188. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", modelDef->GetModelName(), md5anim->Name() );
  1189. return;
  1190. }
  1191. animNum = _animNum;
  1192. animWeights[ 0 ] = 1.0f;
  1193. endtime = -1;
  1194. cycle = -1;
  1195. if ( _anim->GetAnimFlags().random_cycle_start ) {
  1196. // start the animation at a random time so that characters don't walk in sync
  1197. starttime = currentTime - gameLocal.random.RandomFloat() * _anim->Length();
  1198. } else {
  1199. starttime = currentTime;
  1200. }
  1201. // set up blend
  1202. blendEndValue = 1.0f;
  1203. blendStartTime = currentTime - 1;
  1204. blendDuration = blendTime;
  1205. blendStartValue = 0.0f;
  1206. }
  1207. /*
  1208. =====================
  1209. idAnimBlend::PlayAnim
  1210. =====================
  1211. */
  1212. void idAnimBlend::PlayAnim( const idDeclModelDef *modelDef, int _animNum, int currentTime, int blendTime ) {
  1213. Reset( modelDef );
  1214. if ( !modelDef ) {
  1215. return;
  1216. }
  1217. const idAnim *_anim = modelDef->GetAnim( _animNum );
  1218. if ( !_anim ) {
  1219. return;
  1220. }
  1221. const idMD5Anim *md5anim = _anim->MD5Anim( 0 );
  1222. if ( modelDef->Joints().Num() != md5anim->NumJoints() ) {
  1223. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", modelDef->GetModelName(), md5anim->Name() );
  1224. return;
  1225. }
  1226. animNum = _animNum;
  1227. starttime = currentTime;
  1228. endtime = starttime + _anim->Length();
  1229. cycle = 1;
  1230. animWeights[ 0 ] = 1.0f;
  1231. // set up blend
  1232. blendEndValue = 1.0f;
  1233. blendStartTime = currentTime - 1;
  1234. blendDuration = blendTime;
  1235. blendStartValue = 0.0f;
  1236. }
  1237. /*
  1238. =====================
  1239. idAnimBlend::Clear
  1240. =====================
  1241. */
  1242. void idAnimBlend::Clear( int currentTime, int clearTime ) {
  1243. if ( !clearTime ) {
  1244. Reset( modelDef );
  1245. } else {
  1246. SetWeight( 0.0f, currentTime, clearTime );
  1247. }
  1248. }
  1249. /*
  1250. =====================
  1251. idAnimBlend::IsDone
  1252. =====================
  1253. */
  1254. bool idAnimBlend::IsDone( int currentTime ) const {
  1255. if ( !frame && ( endtime > 0 ) && ( currentTime >= endtime ) ) {
  1256. return true;
  1257. }
  1258. if ( ( blendEndValue <= 0.0f ) && ( currentTime >= ( blendStartTime + blendDuration ) ) ) {
  1259. return true;
  1260. }
  1261. return false;
  1262. }
  1263. /*
  1264. =====================
  1265. idAnimBlend::FrameHasChanged
  1266. =====================
  1267. */
  1268. bool idAnimBlend::FrameHasChanged( int currentTime ) const {
  1269. // if we don't have an anim, no change
  1270. if ( !animNum ) {
  1271. return false;
  1272. }
  1273. // if anim is done playing, no change
  1274. if ( ( endtime > 0 ) && ( currentTime > endtime ) ) {
  1275. return false;
  1276. }
  1277. // if our blend weight changes, we need to update
  1278. if ( ( currentTime < ( blendStartTime + blendDuration ) && ( blendStartValue != blendEndValue ) ) ) {
  1279. return true;
  1280. }
  1281. // if we're a single frame anim and this isn't the frame we started on, we don't need to update
  1282. if ( ( frame || ( NumFrames() == 1 ) ) && ( currentTime != starttime ) ) {
  1283. return false;
  1284. }
  1285. return true;
  1286. }
  1287. /*
  1288. =====================
  1289. idAnimBlend::GetCycleCount
  1290. =====================
  1291. */
  1292. int idAnimBlend::GetCycleCount( void ) const {
  1293. return cycle;
  1294. }
  1295. /*
  1296. =====================
  1297. idAnimBlend::SetCycleCount
  1298. =====================
  1299. */
  1300. void idAnimBlend::SetCycleCount( int count ) {
  1301. const idAnim *anim = Anim();
  1302. if ( !anim ) {
  1303. cycle = -1;
  1304. endtime = 0;
  1305. } else {
  1306. cycle = count;
  1307. if ( cycle < 0 ) {
  1308. cycle = -1;
  1309. endtime = -1;
  1310. } else if ( cycle == 0 ) {
  1311. cycle = 1;
  1312. // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion
  1313. if ( rate == 1.0f ) {
  1314. endtime = starttime - timeOffset + anim->Length();
  1315. } else if ( rate != 0.0f ) {
  1316. endtime = starttime - timeOffset + anim->Length() / rate;
  1317. } else {
  1318. endtime = -1;
  1319. }
  1320. } else {
  1321. // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion
  1322. if ( rate == 1.0f ) {
  1323. endtime = starttime - timeOffset + anim->Length() * cycle;
  1324. } else if ( rate != 0.0f ) {
  1325. endtime = starttime - timeOffset + ( anim->Length() * cycle ) / rate;
  1326. } else {
  1327. endtime = -1;
  1328. }
  1329. }
  1330. }
  1331. }
  1332. /*
  1333. =====================
  1334. idAnimBlend::SetPlaybackRate
  1335. =====================
  1336. */
  1337. void idAnimBlend::SetPlaybackRate( int currentTime, float newRate ) {
  1338. int animTime;
  1339. if ( rate == newRate ) {
  1340. return;
  1341. }
  1342. animTime = AnimTime( currentTime );
  1343. if ( newRate == 1.0f ) {
  1344. timeOffset = animTime - ( currentTime - starttime );
  1345. } else {
  1346. timeOffset = animTime - ( currentTime - starttime ) * newRate;
  1347. }
  1348. rate = newRate;
  1349. // update the anim endtime
  1350. SetCycleCount( cycle );
  1351. }
  1352. /*
  1353. =====================
  1354. idAnimBlend::GetPlaybackRate
  1355. =====================
  1356. */
  1357. float idAnimBlend::GetPlaybackRate( void ) const {
  1358. return rate;
  1359. }
  1360. /*
  1361. =====================
  1362. idAnimBlend::SetStartTime
  1363. =====================
  1364. */
  1365. void idAnimBlend::SetStartTime( int _startTime ) {
  1366. starttime = _startTime;
  1367. // update the anim endtime
  1368. SetCycleCount( cycle );
  1369. }
  1370. /*
  1371. =====================
  1372. idAnimBlend::GetStartTime
  1373. =====================
  1374. */
  1375. int idAnimBlend::GetStartTime( void ) const {
  1376. if ( !animNum ) {
  1377. return 0;
  1378. }
  1379. return starttime;
  1380. }
  1381. /*
  1382. =====================
  1383. idAnimBlend::GetEndTime
  1384. =====================
  1385. */
  1386. int idAnimBlend::GetEndTime( void ) const {
  1387. if ( !animNum ) {
  1388. return 0;
  1389. }
  1390. return endtime;
  1391. }
  1392. /*
  1393. =====================
  1394. idAnimBlend::PlayLength
  1395. =====================
  1396. */
  1397. int idAnimBlend::PlayLength( void ) const {
  1398. if ( !animNum ) {
  1399. return 0;
  1400. }
  1401. if ( endtime < 0 ) {
  1402. return -1;
  1403. }
  1404. return endtime - starttime + timeOffset;
  1405. }
  1406. /*
  1407. =====================
  1408. idAnimBlend::AllowMovement
  1409. =====================
  1410. */
  1411. void idAnimBlend::AllowMovement( bool allow ) {
  1412. allowMove = allow;
  1413. }
  1414. /*
  1415. =====================
  1416. idAnimBlend::AllowFrameCommands
  1417. =====================
  1418. */
  1419. void idAnimBlend::AllowFrameCommands( bool allow ) {
  1420. allowFrameCommands = allow;
  1421. }
  1422. /*
  1423. =====================
  1424. idAnimBlend::Anim
  1425. =====================
  1426. */
  1427. const idAnim *idAnimBlend::Anim( void ) const {
  1428. if ( !modelDef ) {
  1429. return NULL;
  1430. }
  1431. const idAnim *anim = modelDef->GetAnim( animNum );
  1432. return anim;
  1433. }
  1434. /*
  1435. =====================
  1436. idAnimBlend::AnimNum
  1437. =====================
  1438. */
  1439. int idAnimBlend::AnimNum( void ) const {
  1440. return animNum;
  1441. }
  1442. /*
  1443. =====================
  1444. idAnimBlend::AnimTime
  1445. =====================
  1446. */
  1447. int idAnimBlend::AnimTime( int currentTime ) const {
  1448. int time;
  1449. int length;
  1450. const idAnim *anim = Anim();
  1451. if ( anim ) {
  1452. if ( frame ) {
  1453. return FRAME2MS( frame - 1 );
  1454. }
  1455. // most of the time we're running at the original frame rate, so avoid the int-to-float-to-int conversion
  1456. if ( rate == 1.0f ) {
  1457. time = currentTime - starttime + timeOffset;
  1458. } else {
  1459. time = static_cast<int>( ( currentTime - starttime ) * rate ) + timeOffset;
  1460. }
  1461. // given enough time, we can easily wrap time around in our frame calculations, so
  1462. // keep cycling animations' time within the length of the anim.
  1463. length = anim->Length();
  1464. if ( ( cycle < 0 ) && ( length > 0 ) ) {
  1465. time %= length;
  1466. // time will wrap after 24 days (oh no!), resulting in negative results for the %.
  1467. // adding the length gives us the proper result.
  1468. if ( time < 0 ) {
  1469. time += length;
  1470. }
  1471. }
  1472. return time;
  1473. } else {
  1474. return 0;
  1475. }
  1476. }
  1477. /*
  1478. =====================
  1479. idAnimBlend::GetFrameNumber
  1480. =====================
  1481. */
  1482. int idAnimBlend::GetFrameNumber( int currentTime ) const {
  1483. const idMD5Anim *md5anim;
  1484. frameBlend_t frameinfo;
  1485. int animTime;
  1486. const idAnim *anim = Anim();
  1487. if ( !anim ) {
  1488. return 1;
  1489. }
  1490. if ( frame ) {
  1491. return frame;
  1492. }
  1493. md5anim = anim->MD5Anim( 0 );
  1494. animTime = AnimTime( currentTime );
  1495. md5anim->ConvertTimeToFrame( animTime, cycle, frameinfo );
  1496. return frameinfo.frame1 + 1;
  1497. }
  1498. /*
  1499. =====================
  1500. idAnimBlend::CallFrameCommands
  1501. =====================
  1502. */
  1503. void idAnimBlend::CallFrameCommands( idEntity *ent, int fromtime, int totime ) const {
  1504. const idMD5Anim *md5anim;
  1505. frameBlend_t frame1;
  1506. frameBlend_t frame2;
  1507. int fromFrameTime;
  1508. int toFrameTime;
  1509. if ( !allowFrameCommands || !ent || frame || ( ( endtime > 0 ) && ( fromtime > endtime ) ) ) {
  1510. return;
  1511. }
  1512. const idAnim *anim = Anim();
  1513. if ( !anim || !anim->HasFrameCommands() ) {
  1514. return;
  1515. }
  1516. if ( totime <= starttime ) {
  1517. // don't play until next frame or we'll play commands twice.
  1518. // this happens on the player sometimes.
  1519. return;
  1520. }
  1521. fromFrameTime = AnimTime( fromtime );
  1522. toFrameTime = AnimTime( totime );
  1523. if ( toFrameTime < fromFrameTime ) {
  1524. toFrameTime += anim->Length();
  1525. }
  1526. md5anim = anim->MD5Anim( 0 );
  1527. md5anim->ConvertTimeToFrame( fromFrameTime, cycle, frame1 );
  1528. md5anim->ConvertTimeToFrame( toFrameTime, cycle, frame2 );
  1529. if ( fromFrameTime <= 0 ) {
  1530. // make sure first frame is called
  1531. anim->CallFrameCommands( ent, -1, frame2.frame1 );
  1532. } else {
  1533. anim->CallFrameCommands( ent, frame1.frame1, frame2.frame1 );
  1534. }
  1535. }
  1536. /*
  1537. =====================
  1538. idAnimBlend::BlendAnim
  1539. =====================
  1540. */
  1541. bool idAnimBlend::BlendAnim( int currentTime, int channel, int numJoints, idJointQuat *blendFrame, float &blendWeight, bool removeOriginOffset, bool overrideBlend, bool printInfo ) const {
  1542. int i;
  1543. float lerp;
  1544. float mixWeight;
  1545. const idMD5Anim *md5anim;
  1546. idJointQuat *ptr;
  1547. frameBlend_t frametime;
  1548. idJointQuat *jointFrame;
  1549. idJointQuat *mixFrame;
  1550. int numAnims;
  1551. int time;
  1552. const idAnim *anim = Anim();
  1553. if ( !anim ) {
  1554. return false;
  1555. }
  1556. float weight = GetWeight( currentTime );
  1557. if ( blendWeight > 0.0f ) {
  1558. if ( ( endtime >= 0 ) && ( currentTime >= endtime ) ) {
  1559. return false;
  1560. }
  1561. if ( !weight ) {
  1562. return false;
  1563. }
  1564. if ( overrideBlend ) {
  1565. blendWeight = 1.0f - weight;
  1566. }
  1567. }
  1568. if ( ( channel == ANIMCHANNEL_ALL ) && !blendWeight ) {
  1569. // we don't need a temporary buffer, so just store it directly in the blend frame
  1570. jointFrame = blendFrame;
  1571. } else {
  1572. // allocate a temporary buffer to copy the joints from
  1573. jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  1574. }
  1575. time = AnimTime( currentTime );
  1576. numAnims = anim->NumAnims();
  1577. if ( numAnims == 1 ) {
  1578. md5anim = anim->MD5Anim( 0 );
  1579. if ( frame ) {
  1580. md5anim->GetSingleFrame( frame - 1, jointFrame, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1581. } else {
  1582. md5anim->ConvertTimeToFrame( time, cycle, frametime );
  1583. md5anim->GetInterpolatedFrame( frametime, jointFrame, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1584. }
  1585. } else {
  1586. //
  1587. // need to mix the multipoint anim together first
  1588. //
  1589. // allocate a temporary buffer to copy the joints to
  1590. mixFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  1591. if ( !frame ) {
  1592. anim->MD5Anim( 0 )->ConvertTimeToFrame( time, cycle, frametime );
  1593. }
  1594. ptr = jointFrame;
  1595. mixWeight = 0.0f;
  1596. for( i = 0; i < numAnims; i++ ) {
  1597. if ( animWeights[ i ] > 0.0f ) {
  1598. mixWeight += animWeights[ i ];
  1599. lerp = animWeights[ i ] / mixWeight;
  1600. md5anim = anim->MD5Anim( i );
  1601. if ( frame ) {
  1602. md5anim->GetSingleFrame( frame - 1, ptr, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1603. } else {
  1604. md5anim->GetInterpolatedFrame( frametime, ptr, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1605. }
  1606. // only blend after the first anim is mixed in
  1607. if ( ptr != jointFrame ) {
  1608. SIMDProcessor->BlendJoints( jointFrame, ptr, lerp, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1609. }
  1610. ptr = mixFrame;
  1611. }
  1612. }
  1613. if ( !mixWeight ) {
  1614. return false;
  1615. }
  1616. }
  1617. if ( removeOriginOffset ) {
  1618. if ( allowMove ) {
  1619. #ifdef VELOCITY_MOVE
  1620. jointFrame[ 0 ].t.x = 0.0f;
  1621. #else
  1622. jointFrame[ 0 ].t.Zero();
  1623. #endif
  1624. }
  1625. if ( anim->GetAnimFlags().anim_turn ) {
  1626. jointFrame[ 0 ].q.Set( -0.70710677f, 0.0f, 0.0f, 0.70710677f );
  1627. }
  1628. }
  1629. if ( !blendWeight ) {
  1630. blendWeight = weight;
  1631. if ( channel != ANIMCHANNEL_ALL ) {
  1632. const int *index = modelDef->GetChannelJoints( channel );
  1633. const int num = modelDef->NumJointsOnChannel( channel );
  1634. for( i = 0; i < num; i++ ) {
  1635. int j = index[i];
  1636. blendFrame[j].t = jointFrame[j].t;
  1637. blendFrame[j].q = jointFrame[j].q;
  1638. }
  1639. }
  1640. } else {
  1641. blendWeight += weight;
  1642. lerp = weight / blendWeight;
  1643. SIMDProcessor->BlendJoints( blendFrame, jointFrame, lerp, modelDef->GetChannelJoints( channel ), modelDef->NumJointsOnChannel( channel ) );
  1644. }
  1645. if ( printInfo ) {
  1646. if ( frame ) {
  1647. gameLocal.Printf( " %s: '%s', %d, %.2f%%\n", channelNames[ channel ], anim->FullName(), frame, weight * 100.0f );
  1648. } else {
  1649. gameLocal.Printf( " %s: '%s', %.3f, %.2f%%\n", channelNames[ channel ], anim->FullName(), ( float )frametime.frame1 + frametime.backlerp, weight * 100.0f );
  1650. }
  1651. }
  1652. return true;
  1653. }
  1654. /*
  1655. =====================
  1656. idAnimBlend::BlendOrigin
  1657. =====================
  1658. */
  1659. void idAnimBlend::BlendOrigin( int currentTime, idVec3 &blendPos, float &blendWeight, bool removeOriginOffset ) const {
  1660. float lerp;
  1661. idVec3 animpos;
  1662. idVec3 pos;
  1663. int time;
  1664. int num;
  1665. int i;
  1666. if ( frame || ( ( endtime > 0 ) && ( currentTime > endtime ) ) ) {
  1667. return;
  1668. }
  1669. const idAnim *anim = Anim();
  1670. if ( !anim ) {
  1671. return;
  1672. }
  1673. if ( allowMove && removeOriginOffset ) {
  1674. return;
  1675. }
  1676. float weight = GetWeight( currentTime );
  1677. if ( !weight ) {
  1678. return;
  1679. }
  1680. time = AnimTime( currentTime );
  1681. pos.Zero();
  1682. num = anim->NumAnims();
  1683. for( i = 0; i < num; i++ ) {
  1684. anim->GetOrigin( animpos, i, time, cycle );
  1685. pos += animpos * animWeights[ i ];
  1686. }
  1687. if ( !blendWeight ) {
  1688. blendPos = pos;
  1689. blendWeight = weight;
  1690. } else {
  1691. lerp = weight / ( blendWeight + weight );
  1692. blendPos += lerp * ( pos - blendPos );
  1693. blendWeight += weight;
  1694. }
  1695. }
  1696. /*
  1697. =====================
  1698. idAnimBlend::BlendDelta
  1699. =====================
  1700. */
  1701. void idAnimBlend::BlendDelta( int fromtime, int totime, idVec3 &blendDelta, float &blendWeight ) const {
  1702. idVec3 pos1;
  1703. idVec3 pos2;
  1704. idVec3 animpos;
  1705. idVec3 delta;
  1706. int time1;
  1707. int time2;
  1708. float lerp;
  1709. int num;
  1710. int i;
  1711. if ( frame || !allowMove || ( ( endtime > 0 ) && ( fromtime > endtime ) ) ) {
  1712. return;
  1713. }
  1714. const idAnim *anim = Anim();
  1715. if ( !anim ) {
  1716. return;
  1717. }
  1718. float weight = GetWeight( totime );
  1719. if ( !weight ) {
  1720. return;
  1721. }
  1722. time1 = AnimTime( fromtime );
  1723. time2 = AnimTime( totime );
  1724. if ( time2 < time1 ) {
  1725. time2 += anim->Length();
  1726. }
  1727. num = anim->NumAnims();
  1728. pos1.Zero();
  1729. pos2.Zero();
  1730. for( i = 0; i < num; i++ ) {
  1731. anim->GetOrigin( animpos, i, time1, cycle );
  1732. pos1 += animpos * animWeights[ i ];
  1733. anim->GetOrigin( animpos, i, time2, cycle );
  1734. pos2 += animpos * animWeights[ i ];
  1735. }
  1736. delta = pos2 - pos1;
  1737. if ( !blendWeight ) {
  1738. blendDelta = delta;
  1739. blendWeight = weight;
  1740. } else {
  1741. lerp = weight / ( blendWeight + weight );
  1742. blendDelta += lerp * ( delta - blendDelta );
  1743. blendWeight += weight;
  1744. }
  1745. }
  1746. /*
  1747. =====================
  1748. idAnimBlend::BlendDeltaRotation
  1749. =====================
  1750. */
  1751. void idAnimBlend::BlendDeltaRotation( int fromtime, int totime, idQuat &blendDelta, float &blendWeight ) const {
  1752. idQuat q1;
  1753. idQuat q2;
  1754. idQuat q3;
  1755. int time1;
  1756. int time2;
  1757. float lerp;
  1758. float mixWeight;
  1759. int num;
  1760. int i;
  1761. if ( frame || !allowMove || ( ( endtime > 0 ) && ( fromtime > endtime ) ) ) {
  1762. return;
  1763. }
  1764. const idAnim *anim = Anim();
  1765. if ( !anim || !anim->GetAnimFlags().anim_turn ) {
  1766. return;
  1767. }
  1768. float weight = GetWeight( totime );
  1769. if ( !weight ) {
  1770. return;
  1771. }
  1772. time1 = AnimTime( fromtime );
  1773. time2 = AnimTime( totime );
  1774. if ( time2 < time1 ) {
  1775. time2 += anim->Length();
  1776. }
  1777. q1.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  1778. q2.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  1779. mixWeight = 0.0f;
  1780. num = anim->NumAnims();
  1781. for( i = 0; i < num; i++ ) {
  1782. if ( animWeights[ i ] > 0.0f ) {
  1783. mixWeight += animWeights[ i ];
  1784. if ( animWeights[ i ] == mixWeight ) {
  1785. anim->GetOriginRotation( q1, i, time1, cycle );
  1786. anim->GetOriginRotation( q2, i, time2, cycle );
  1787. } else {
  1788. lerp = animWeights[ i ] / mixWeight;
  1789. anim->GetOriginRotation( q3, i, time1, cycle );
  1790. q1.Slerp( q1, q3, lerp );
  1791. anim->GetOriginRotation( q3, i, time2, cycle );
  1792. q2.Slerp( q1, q3, lerp );
  1793. }
  1794. }
  1795. }
  1796. q3 = q1.Inverse() * q2;
  1797. if ( !blendWeight ) {
  1798. blendDelta = q3;
  1799. blendWeight = weight;
  1800. } else {
  1801. lerp = weight / ( blendWeight + weight );
  1802. blendDelta.Slerp( blendDelta, q3, lerp );
  1803. blendWeight += weight;
  1804. }
  1805. }
  1806. /*
  1807. =====================
  1808. idAnimBlend::AddBounds
  1809. =====================
  1810. */
  1811. bool idAnimBlend::AddBounds( int currentTime, idBounds &bounds, bool removeOriginOffset ) const {
  1812. int i;
  1813. int num;
  1814. idBounds b;
  1815. int time;
  1816. idVec3 pos;
  1817. bool addorigin;
  1818. if ( ( endtime > 0 ) && ( currentTime > endtime ) ) {
  1819. return false;
  1820. }
  1821. const idAnim *anim = Anim();
  1822. if ( !anim ) {
  1823. return false;
  1824. }
  1825. float weight = GetWeight( currentTime );
  1826. if ( !weight ) {
  1827. return false;
  1828. }
  1829. time = AnimTime( currentTime );
  1830. num = anim->NumAnims();
  1831. addorigin = !allowMove || !removeOriginOffset;
  1832. for( i = 0; i < num; i++ ) {
  1833. if ( anim->GetBounds( b, i, time, cycle ) ) {
  1834. if ( addorigin ) {
  1835. anim->GetOrigin( pos, i, time, cycle );
  1836. b.TranslateSelf( pos );
  1837. }
  1838. bounds.AddBounds( b );
  1839. }
  1840. }
  1841. return true;
  1842. }
  1843. /***********************************************************************
  1844. idDeclModelDef
  1845. ***********************************************************************/
  1846. /*
  1847. =====================
  1848. idDeclModelDef::idDeclModelDef
  1849. =====================
  1850. */
  1851. idDeclModelDef::idDeclModelDef() {
  1852. modelHandle = NULL;
  1853. skin = NULL;
  1854. offset.Zero();
  1855. for ( int i = 0; i < ANIM_NumAnimChannels; i++ ) {
  1856. channelJoints[i].Clear();
  1857. }
  1858. }
  1859. /*
  1860. =====================
  1861. idDeclModelDef::~idDeclModelDef
  1862. =====================
  1863. */
  1864. idDeclModelDef::~idDeclModelDef() {
  1865. FreeData();
  1866. }
  1867. /*
  1868. =================
  1869. idDeclModelDef::Size
  1870. =================
  1871. */
  1872. size_t idDeclModelDef::Size( void ) const {
  1873. return sizeof( idDeclModelDef );
  1874. }
  1875. /*
  1876. =====================
  1877. idDeclModelDef::CopyDecl
  1878. =====================
  1879. */
  1880. void idDeclModelDef::CopyDecl( const idDeclModelDef *decl ) {
  1881. int i;
  1882. FreeData();
  1883. offset = decl->offset;
  1884. modelHandle = decl->modelHandle;
  1885. skin = decl->skin;
  1886. anims.SetNum( decl->anims.Num() );
  1887. for( i = 0; i < anims.Num(); i++ ) {
  1888. anims[ i ] = new idAnim( this, decl->anims[ i ] );
  1889. }
  1890. joints.SetNum( decl->joints.Num() );
  1891. memcpy( joints.Ptr(), decl->joints.Ptr(), decl->joints.Num() * sizeof( joints[0] ) );
  1892. jointParents.SetNum( decl->jointParents.Num() );
  1893. memcpy( jointParents.Ptr(), decl->jointParents.Ptr(), decl->jointParents.Num() * sizeof( jointParents[0] ) );
  1894. for ( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  1895. channelJoints[i] = decl->channelJoints[i];
  1896. }
  1897. }
  1898. /*
  1899. =====================
  1900. idDeclModelDef::FreeData
  1901. =====================
  1902. */
  1903. void idDeclModelDef::FreeData( void ) {
  1904. anims.DeleteContents( true );
  1905. joints.Clear();
  1906. jointParents.Clear();
  1907. modelHandle = NULL;
  1908. skin = NULL;
  1909. offset.Zero();
  1910. for ( int i = 0; i < ANIM_NumAnimChannels; i++ ) {
  1911. channelJoints[i].Clear();
  1912. }
  1913. }
  1914. /*
  1915. ================
  1916. idDeclModelDef::DefaultDefinition
  1917. ================
  1918. */
  1919. const char *idDeclModelDef::DefaultDefinition( void ) const {
  1920. return "{ }";
  1921. }
  1922. /*
  1923. ====================
  1924. idDeclModelDef::FindJoint
  1925. ====================
  1926. */
  1927. const jointInfo_t *idDeclModelDef::FindJoint( const char *name ) const {
  1928. int i;
  1929. const idMD5Joint *joint;
  1930. if ( !modelHandle ) {
  1931. return NULL;
  1932. }
  1933. joint = modelHandle->GetJoints();
  1934. for( i = 0; i < joints.Num(); i++, joint++ ) {
  1935. if ( !joint->name.Icmp( name ) ) {
  1936. return &joints[ i ];
  1937. }
  1938. }
  1939. return NULL;
  1940. }
  1941. /*
  1942. =====================
  1943. idDeclModelDef::ModelHandle
  1944. =====================
  1945. */
  1946. idRenderModel *idDeclModelDef::ModelHandle( void ) const {
  1947. return ( idRenderModel * )modelHandle;
  1948. }
  1949. /*
  1950. =====================
  1951. idDeclModelDef::GetJointList
  1952. =====================
  1953. */
  1954. void idDeclModelDef::GetJointList( const char *jointnames, idList<jointHandle_t> &jointList ) const {
  1955. const char *pos;
  1956. idStr jointname;
  1957. const jointInfo_t *joint;
  1958. const jointInfo_t *child;
  1959. int i;
  1960. int num;
  1961. bool getChildren;
  1962. bool subtract;
  1963. if ( !modelHandle ) {
  1964. return;
  1965. }
  1966. jointList.Clear();
  1967. num = modelHandle->NumJoints();
  1968. // scan through list of joints and add each to the joint list
  1969. pos = jointnames;
  1970. while( *pos ) {
  1971. // skip over whitespace
  1972. while( ( *pos != 0 ) && isspace( *pos ) ) {
  1973. pos++;
  1974. }
  1975. if ( !*pos ) {
  1976. // no more names
  1977. break;
  1978. }
  1979. // copy joint name
  1980. jointname = "";
  1981. if ( *pos == '-' ) {
  1982. subtract = true;
  1983. pos++;
  1984. } else {
  1985. subtract = false;
  1986. }
  1987. if ( *pos == '*' ) {
  1988. getChildren = true;
  1989. pos++;
  1990. } else {
  1991. getChildren = false;
  1992. }
  1993. while( ( *pos != 0 ) && !isspace( *pos ) ) {
  1994. jointname += *pos;
  1995. pos++;
  1996. }
  1997. joint = FindJoint( jointname );
  1998. if ( !joint ) {
  1999. gameLocal.Warning( "Unknown joint '%s' in '%s' for model '%s'", jointname.c_str(), jointnames, GetName() );
  2000. continue;
  2001. }
  2002. if ( !subtract ) {
  2003. jointList.AddUnique( joint->num );
  2004. } else {
  2005. jointList.Remove( joint->num );
  2006. }
  2007. if ( getChildren ) {
  2008. // include all joint's children
  2009. child = joint + 1;
  2010. for( i = joint->num + 1; i < num; i++, child++ ) {
  2011. // all children of the joint should follow it in the list.
  2012. // once we reach a joint without a parent or with a parent
  2013. // who is earlier in the list than the specified joint, then
  2014. // we've gone through all it's children.
  2015. if ( child->parentNum < joint->num ) {
  2016. break;
  2017. }
  2018. if ( !subtract ) {
  2019. jointList.AddUnique( child->num );
  2020. } else {
  2021. jointList.Remove( child->num );
  2022. }
  2023. }
  2024. }
  2025. }
  2026. }
  2027. /*
  2028. =====================
  2029. idDeclModelDef::Touch
  2030. =====================
  2031. */
  2032. void idDeclModelDef::Touch( void ) const {
  2033. if ( modelHandle ) {
  2034. renderModelManager->FindModel( modelHandle->Name() );
  2035. }
  2036. }
  2037. /*
  2038. =====================
  2039. idDeclModelDef::GetDefaultSkin
  2040. =====================
  2041. */
  2042. const idDeclSkin *idDeclModelDef::GetDefaultSkin( void ) const {
  2043. return skin;
  2044. }
  2045. /*
  2046. =====================
  2047. idDeclModelDef::GetDefaultPose
  2048. =====================
  2049. */
  2050. const idJointQuat *idDeclModelDef::GetDefaultPose( void ) const {
  2051. return modelHandle->GetDefaultPose();
  2052. }
  2053. /*
  2054. =====================
  2055. idDeclModelDef::SetupJoints
  2056. =====================
  2057. */
  2058. void idDeclModelDef::SetupJoints( int *numJoints, idJointMat **jointList, idBounds &frameBounds, bool removeOriginOffset ) const {
  2059. int num;
  2060. const idJointQuat *pose;
  2061. idJointMat *list;
  2062. if ( !modelHandle || modelHandle->IsDefaultModel() ) {
  2063. Mem_Free16( (*jointList) );
  2064. (*jointList) = NULL;
  2065. frameBounds.Clear();
  2066. return;
  2067. }
  2068. // get the number of joints
  2069. num = modelHandle->NumJoints();
  2070. if ( !num ) {
  2071. gameLocal.Error( "model '%s' has no joints", modelHandle->Name() );
  2072. }
  2073. // set up initial pose for model (with no pose, model is just a jumbled mess)
  2074. list = (idJointMat *) Mem_Alloc16( num * sizeof( list[0] ) );
  2075. pose = GetDefaultPose();
  2076. // convert the joint quaternions to joint matrices
  2077. SIMDProcessor->ConvertJointQuatsToJointMats( list, pose, joints.Num() );
  2078. // check if we offset the model by the origin joint
  2079. if ( removeOriginOffset ) {
  2080. #ifdef VELOCITY_MOVE
  2081. list[ 0 ].SetTranslation( idVec3( offset.x, offset.y + pose[0].t.y, offset.z + pose[0].t.z ) );
  2082. #else
  2083. list[ 0 ].SetTranslation( offset );
  2084. #endif
  2085. } else {
  2086. list[ 0 ].SetTranslation( pose[0].t + offset );
  2087. }
  2088. // transform the joint hierarchy
  2089. SIMDProcessor->TransformJoints( list, jointParents.Ptr(), 1, joints.Num() - 1 );
  2090. *numJoints = num;
  2091. *jointList = list;
  2092. // get the bounds of the default pose
  2093. frameBounds = modelHandle->Bounds( NULL );
  2094. }
  2095. /*
  2096. =====================
  2097. idDeclModelDef::ParseAnim
  2098. =====================
  2099. */
  2100. bool idDeclModelDef::ParseAnim( idLexer &src, int numDefaultAnims ) {
  2101. int i;
  2102. int len;
  2103. idAnim *anim;
  2104. const idMD5Anim *md5anims[ ANIM_MaxSyncedAnims ];
  2105. const idMD5Anim *md5anim;
  2106. idStr alias;
  2107. idToken realname;
  2108. idToken token;
  2109. int numAnims;
  2110. animFlags_t flags;
  2111. numAnims = 0;
  2112. memset( md5anims, 0, sizeof( md5anims ) );
  2113. if( !src.ReadToken( &realname ) ) {
  2114. src.Warning( "Unexpected end of file" );
  2115. MakeDefault();
  2116. return false;
  2117. }
  2118. alias = realname;
  2119. for( i = 0; i < anims.Num(); i++ ) {
  2120. if ( !strcmp( anims[ i ]->FullName(), realname ) ) {
  2121. break;
  2122. }
  2123. }
  2124. if ( ( i < anims.Num() ) && ( i >= numDefaultAnims ) ) {
  2125. src.Warning( "Duplicate anim '%s'", realname.c_str() );
  2126. MakeDefault();
  2127. return false;
  2128. }
  2129. if ( i < numDefaultAnims ) {
  2130. anim = anims[ i ];
  2131. } else {
  2132. // create the alias associated with this animation
  2133. anim = new idAnim();
  2134. anims.Append( anim );
  2135. }
  2136. // random anims end with a number. find the numeric suffix of the animation.
  2137. len = alias.Length();
  2138. for( i = len - 1; i > 0; i-- ) {
  2139. if ( !isdigit( alias[ i ] ) ) {
  2140. break;
  2141. }
  2142. }
  2143. // check for zero length name, or a purely numeric name
  2144. if ( i <= 0 ) {
  2145. src.Warning( "Invalid animation name '%s'", alias.c_str() );
  2146. MakeDefault();
  2147. return false;
  2148. }
  2149. // remove the numeric suffix
  2150. alias.CapLength( i + 1 );
  2151. // parse the anims from the string
  2152. do {
  2153. if( !src.ReadToken( &token ) ) {
  2154. src.Warning( "Unexpected end of file" );
  2155. MakeDefault();
  2156. return false;
  2157. }
  2158. // lookup the animation
  2159. md5anim = animationLib.GetAnim( token );
  2160. if ( !md5anim ) {
  2161. src.Warning( "Couldn't load anim '%s'", token.c_str() );
  2162. MakeDefault();
  2163. return false;
  2164. }
  2165. md5anim->CheckModelHierarchy( modelHandle );
  2166. if ( numAnims > 0 ) {
  2167. // make sure it's the same length as the other anims
  2168. if ( md5anim->Length() != md5anims[ 0 ]->Length() ) {
  2169. src.Warning( "Anim '%s' does not match length of anim '%s'", md5anim->Name(), md5anims[ 0 ]->Name() );
  2170. MakeDefault();
  2171. return false;
  2172. }
  2173. }
  2174. if ( numAnims >= ANIM_MaxSyncedAnims ) {
  2175. src.Warning( "Exceeded max synced anims (%d)", ANIM_MaxSyncedAnims );
  2176. MakeDefault();
  2177. return false;
  2178. }
  2179. // add it to our list
  2180. md5anims[ numAnims ] = md5anim;
  2181. numAnims++;
  2182. } while ( src.CheckTokenString( "," ) );
  2183. if ( !numAnims ) {
  2184. src.Warning( "No animation specified" );
  2185. MakeDefault();
  2186. return false;
  2187. }
  2188. anim->SetAnim( this, realname, alias, numAnims, md5anims );
  2189. memset( &flags, 0, sizeof( flags ) );
  2190. // parse any frame commands or animflags
  2191. if ( src.CheckTokenString( "{" ) ) {
  2192. while( 1 ) {
  2193. if( !src.ReadToken( &token ) ) {
  2194. src.Warning( "Unexpected end of file" );
  2195. MakeDefault();
  2196. return false;
  2197. }
  2198. if ( token == "}" ) {
  2199. break;
  2200. }else if ( token == "prevent_idle_override" ) {
  2201. flags.prevent_idle_override = true;
  2202. } else if ( token == "random_cycle_start" ) {
  2203. flags.random_cycle_start = true;
  2204. } else if ( token == "ai_no_turn" ) {
  2205. flags.ai_no_turn = true;
  2206. } else if ( token == "anim_turn" ) {
  2207. flags.anim_turn = true;
  2208. } else if ( token == "frame" ) {
  2209. // create a frame command
  2210. int framenum;
  2211. const char *err;
  2212. // make sure we don't have any line breaks while reading the frame command so the error line # will be correct
  2213. if ( !src.ReadTokenOnLine( &token ) ) {
  2214. src.Warning( "Missing frame # after 'frame'" );
  2215. MakeDefault();
  2216. return false;
  2217. }
  2218. if ( token.type == TT_PUNCTUATION && token == "-" ) {
  2219. src.Warning( "Invalid frame # after 'frame'" );
  2220. MakeDefault();
  2221. return false;
  2222. } else if ( token.type != TT_NUMBER || token.subtype == TT_FLOAT ) {
  2223. src.Error( "expected integer value, found '%s'", token.c_str() );
  2224. }
  2225. // get the frame number
  2226. framenum = token.GetIntValue();
  2227. // put the command on the specified frame of the animation
  2228. err = anim->AddFrameCommand( this, framenum, src, NULL );
  2229. if ( err ) {
  2230. src.Warning( "%s", err );
  2231. MakeDefault();
  2232. return false;
  2233. }
  2234. } else {
  2235. src.Warning( "Unknown command '%s'", token.c_str() );
  2236. MakeDefault();
  2237. return false;
  2238. }
  2239. }
  2240. }
  2241. // set the flags
  2242. anim->SetAnimFlags( flags );
  2243. return true;
  2244. }
  2245. /*
  2246. ================
  2247. idDeclModelDef::Parse
  2248. ================
  2249. */
  2250. bool idDeclModelDef::Parse( const char *text, const int textLength ) {
  2251. int i;
  2252. int num;
  2253. idStr filename;
  2254. idStr extension;
  2255. const idMD5Joint *md5joint;
  2256. const idMD5Joint *md5joints;
  2257. idLexer src;
  2258. idToken token;
  2259. idToken token2;
  2260. idStr jointnames;
  2261. int channel;
  2262. jointHandle_t jointnum;
  2263. idList<jointHandle_t> jointList;
  2264. int numDefaultAnims;
  2265. src.LoadMemory( text, textLength, GetFileName(), GetLineNum() );
  2266. src.SetFlags( DECL_LEXER_FLAGS );
  2267. src.SkipUntilString( "{" );
  2268. numDefaultAnims = 0;
  2269. while( 1 ) {
  2270. if ( !src.ReadToken( &token ) ) {
  2271. break;
  2272. }
  2273. if ( !token.Icmp( "}" ) ) {
  2274. break;
  2275. }
  2276. if ( token == "inherit" ) {
  2277. if( !src.ReadToken( &token2 ) ) {
  2278. src.Warning( "Unexpected end of file" );
  2279. MakeDefault();
  2280. return false;
  2281. }
  2282. const idDeclModelDef *copy = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, token2, false ) );
  2283. if ( !copy ) {
  2284. common->Warning( "Unknown model definition '%s'", token2.c_str() );
  2285. } else if ( copy->GetState() == DS_DEFAULTED ) {
  2286. common->Warning( "inherited model definition '%s' defaulted", token2.c_str() );
  2287. MakeDefault();
  2288. return false;
  2289. } else {
  2290. CopyDecl( copy );
  2291. numDefaultAnims = anims.Num();
  2292. }
  2293. } else if ( token == "skin" ) {
  2294. if( !src.ReadToken( &token2 ) ) {
  2295. src.Warning( "Unexpected end of file" );
  2296. MakeDefault();
  2297. return false;
  2298. }
  2299. skin = declManager->FindSkin( token2 );
  2300. if ( !skin ) {
  2301. src.Warning( "Skin '%s' not found", token2.c_str() );
  2302. MakeDefault();
  2303. return false;
  2304. }
  2305. } else if ( token == "mesh" ) {
  2306. if( !src.ReadToken( &token2 ) ) {
  2307. src.Warning( "Unexpected end of file" );
  2308. MakeDefault();
  2309. return false;
  2310. }
  2311. filename = token2;
  2312. filename.ExtractFileExtension( extension );
  2313. if ( extension != MD5_MESH_EXT ) {
  2314. src.Warning( "Invalid model for MD5 mesh" );
  2315. MakeDefault();
  2316. return false;
  2317. }
  2318. modelHandle = renderModelManager->FindModel( filename );
  2319. if ( !modelHandle ) {
  2320. src.Warning( "Model '%s' not found", filename.c_str() );
  2321. MakeDefault();
  2322. return false;
  2323. }
  2324. if ( modelHandle->IsDefaultModel() ) {
  2325. src.Warning( "Model '%s' defaulted", filename.c_str() );
  2326. MakeDefault();
  2327. return false;
  2328. }
  2329. // get the number of joints
  2330. num = modelHandle->NumJoints();
  2331. if ( !num ) {
  2332. src.Warning( "Model '%s' has no joints", filename.c_str() );
  2333. }
  2334. // set up the joint hierarchy
  2335. joints.SetGranularity( 1 );
  2336. joints.SetNum( num );
  2337. jointParents.SetNum( num );
  2338. channelJoints[0].SetNum( num );
  2339. md5joints = modelHandle->GetJoints();
  2340. md5joint = md5joints;
  2341. for( i = 0; i < num; i++, md5joint++ ) {
  2342. joints[i].channel = ANIMCHANNEL_ALL;
  2343. joints[i].num = static_cast<jointHandle_t>( i );
  2344. if ( md5joint->parent ) {
  2345. joints[i].parentNum = static_cast<jointHandle_t>( md5joint->parent - md5joints );
  2346. } else {
  2347. joints[i].parentNum = INVALID_JOINT;
  2348. }
  2349. jointParents[i] = joints[i].parentNum;
  2350. channelJoints[0][i] = i;
  2351. }
  2352. } else if ( token == "remove" ) {
  2353. // removes any anims whos name matches
  2354. if( !src.ReadToken( &token2 ) ) {
  2355. src.Warning( "Unexpected end of file" );
  2356. MakeDefault();
  2357. return false;
  2358. }
  2359. num = 0;
  2360. for( i = 0; i < anims.Num(); i++ ) {
  2361. if ( ( token2 == anims[ i ]->Name() ) || ( token2 == anims[ i ]->FullName() ) ) {
  2362. delete anims[ i ];
  2363. anims.RemoveIndex( i );
  2364. if ( i >= numDefaultAnims ) {
  2365. src.Warning( "Anim '%s' was not inherited. Anim should be removed from the model def.", token2.c_str() );
  2366. MakeDefault();
  2367. return false;
  2368. }
  2369. i--;
  2370. numDefaultAnims--;
  2371. num++;
  2372. continue;
  2373. }
  2374. }
  2375. if ( !num ) {
  2376. src.Warning( "Couldn't find anim '%s' to remove", token2.c_str() );
  2377. MakeDefault();
  2378. return false;
  2379. }
  2380. } else if ( token == "anim" ) {
  2381. if ( !modelHandle ) {
  2382. src.Warning( "Must specify mesh before defining anims" );
  2383. MakeDefault();
  2384. return false;
  2385. }
  2386. if ( !ParseAnim( src, numDefaultAnims ) ) {
  2387. MakeDefault();
  2388. return false;
  2389. }
  2390. } else if ( token == "offset" ) {
  2391. if ( !src.Parse1DMatrix( 3, offset.ToFloatPtr() ) ) {
  2392. src.Warning( "Expected vector following 'offset'" );
  2393. MakeDefault();
  2394. return false;
  2395. }
  2396. } else if ( token == "channel" ) {
  2397. if ( !modelHandle ) {
  2398. src.Warning( "Must specify mesh before defining channels" );
  2399. MakeDefault();
  2400. return false;
  2401. }
  2402. // set the channel for a group of joints
  2403. if( !src.ReadToken( &token2 ) ) {
  2404. src.Warning( "Unexpected end of file" );
  2405. MakeDefault();
  2406. return false;
  2407. }
  2408. if ( !src.CheckTokenString( "(" ) ) {
  2409. src.Warning( "Expected { after '%s'\n", token2.c_str() );
  2410. MakeDefault();
  2411. return false;
  2412. }
  2413. for( i = ANIMCHANNEL_ALL + 1; i < ANIM_NumAnimChannels; i++ ) {
  2414. if ( !idStr::Icmp( channelNames[ i ], token2 ) ) {
  2415. break;
  2416. }
  2417. }
  2418. if ( i >= ANIM_NumAnimChannels ) {
  2419. src.Warning( "Unknown channel '%s'", token2.c_str() );
  2420. MakeDefault();
  2421. return false;
  2422. }
  2423. channel = i;
  2424. jointnames = "";
  2425. while( !src.CheckTokenString( ")" ) ) {
  2426. if( !src.ReadToken( &token2 ) ) {
  2427. src.Warning( "Unexpected end of file" );
  2428. MakeDefault();
  2429. return false;
  2430. }
  2431. jointnames += token2;
  2432. if ( ( token2 != "*" ) && ( token2 != "-" ) ) {
  2433. jointnames += " ";
  2434. }
  2435. }
  2436. GetJointList( jointnames, jointList );
  2437. channelJoints[ channel ].SetNum( jointList.Num() );
  2438. for( num = i = 0; i < jointList.Num(); i++ ) {
  2439. jointnum = jointList[ i ];
  2440. if ( joints[ jointnum ].channel != ANIMCHANNEL_ALL ) {
  2441. src.Warning( "Joint '%s' assigned to multiple channels", modelHandle->GetJointName( jointnum ) );
  2442. continue;
  2443. }
  2444. joints[ jointnum ].channel = channel;
  2445. channelJoints[ channel ][ num++ ] = jointnum;
  2446. }
  2447. channelJoints[ channel ].SetNum( num );
  2448. } else {
  2449. src.Warning( "unknown token '%s'", token.c_str() );
  2450. MakeDefault();
  2451. return false;
  2452. }
  2453. }
  2454. // shrink the anim list down to save space
  2455. anims.SetGranularity( 1 );
  2456. anims.SetNum( anims.Num() );
  2457. return true;
  2458. }
  2459. /*
  2460. =====================
  2461. idDeclModelDef::HasAnim
  2462. =====================
  2463. */
  2464. bool idDeclModelDef::HasAnim( const char *name ) const {
  2465. int i;
  2466. // find any animations with same name
  2467. for( i = 0; i < anims.Num(); i++ ) {
  2468. if ( !strcmp( anims[ i ]->Name(), name ) ) {
  2469. return true;
  2470. }
  2471. }
  2472. return false;
  2473. }
  2474. /*
  2475. =====================
  2476. idDeclModelDef::NumAnims
  2477. =====================
  2478. */
  2479. int idDeclModelDef::NumAnims( void ) const {
  2480. return anims.Num() + 1;
  2481. }
  2482. /*
  2483. =====================
  2484. idDeclModelDef::GetSpecificAnim
  2485. Gets the exact anim for the name, without randomization.
  2486. =====================
  2487. */
  2488. int idDeclModelDef::GetSpecificAnim( const char *name ) const {
  2489. int i;
  2490. // find a specific animation
  2491. for( i = 0; i < anims.Num(); i++ ) {
  2492. if ( !strcmp( anims[ i ]->FullName(), name ) ) {
  2493. return i + 1;
  2494. }
  2495. }
  2496. // didn't find it
  2497. return 0;
  2498. }
  2499. /*
  2500. =====================
  2501. idDeclModelDef::GetAnim
  2502. =====================
  2503. */
  2504. const idAnim *idDeclModelDef::GetAnim( int index ) const {
  2505. if ( ( index < 1 ) || ( index > anims.Num() ) ) {
  2506. return NULL;
  2507. }
  2508. return anims[ index - 1 ];
  2509. }
  2510. /*
  2511. =====================
  2512. idDeclModelDef::GetAnim
  2513. =====================
  2514. */
  2515. int idDeclModelDef::GetAnim( const char *name ) const {
  2516. int i;
  2517. int which;
  2518. const int MAX_ANIMS = 64;
  2519. int animList[ MAX_ANIMS ];
  2520. int numAnims;
  2521. int len;
  2522. len = strlen( name );
  2523. if ( len && idStr::CharIsNumeric( name[ len - 1 ] ) ) {
  2524. // find a specific animation
  2525. return GetSpecificAnim( name );
  2526. }
  2527. // find all animations with same name
  2528. numAnims = 0;
  2529. for( i = 0; i < anims.Num(); i++ ) {
  2530. if ( !strcmp( anims[ i ]->Name(), name ) ) {
  2531. animList[ numAnims++ ] = i;
  2532. if ( numAnims >= MAX_ANIMS ) {
  2533. break;
  2534. }
  2535. }
  2536. }
  2537. if ( !numAnims ) {
  2538. return 0;
  2539. }
  2540. // get a random anim
  2541. //FIXME: don't access gameLocal here?
  2542. which = gameLocal.random.RandomInt( numAnims );
  2543. return animList[ which ] + 1;
  2544. }
  2545. /*
  2546. =====================
  2547. idDeclModelDef::GetSkin
  2548. =====================
  2549. */
  2550. const idDeclSkin *idDeclModelDef::GetSkin( void ) const {
  2551. return skin;
  2552. }
  2553. /*
  2554. =====================
  2555. idDeclModelDef::GetModelName
  2556. =====================
  2557. */
  2558. const char *idDeclModelDef::GetModelName( void ) const {
  2559. if ( modelHandle ) {
  2560. return modelHandle->Name();
  2561. } else {
  2562. return "";
  2563. }
  2564. }
  2565. /*
  2566. =====================
  2567. idDeclModelDef::Joints
  2568. =====================
  2569. */
  2570. const idList<jointInfo_t> &idDeclModelDef::Joints( void ) const {
  2571. return joints;
  2572. }
  2573. /*
  2574. =====================
  2575. idDeclModelDef::JointParents
  2576. =====================
  2577. */
  2578. const int * idDeclModelDef::JointParents( void ) const {
  2579. return jointParents.Ptr();
  2580. }
  2581. /*
  2582. =====================
  2583. idDeclModelDef::NumJoints
  2584. =====================
  2585. */
  2586. int idDeclModelDef::NumJoints( void ) const {
  2587. return joints.Num();
  2588. }
  2589. /*
  2590. =====================
  2591. idDeclModelDef::GetJoint
  2592. =====================
  2593. */
  2594. const jointInfo_t *idDeclModelDef::GetJoint( int jointHandle ) const {
  2595. if ( ( jointHandle < 0 ) || ( jointHandle > joints.Num() ) ) {
  2596. gameLocal.Error( "idDeclModelDef::GetJoint : joint handle out of range" );
  2597. }
  2598. return &joints[ jointHandle ];
  2599. }
  2600. /*
  2601. ====================
  2602. idDeclModelDef::GetJointName
  2603. ====================
  2604. */
  2605. const char *idDeclModelDef::GetJointName( int jointHandle ) const {
  2606. const idMD5Joint *joint;
  2607. if ( !modelHandle ) {
  2608. return NULL;
  2609. }
  2610. if ( ( jointHandle < 0 ) || ( jointHandle > joints.Num() ) ) {
  2611. gameLocal.Error( "idDeclModelDef::GetJointName : joint handle out of range" );
  2612. }
  2613. joint = modelHandle->GetJoints();
  2614. return joint[ jointHandle ].name.c_str();
  2615. }
  2616. /*
  2617. =====================
  2618. idDeclModelDef::NumJointsOnChannel
  2619. =====================
  2620. */
  2621. int idDeclModelDef::NumJointsOnChannel( int channel ) const {
  2622. if ( ( channel < 0 ) || ( channel >= ANIM_NumAnimChannels ) ) {
  2623. gameLocal.Error( "idDeclModelDef::NumJointsOnChannel : channel out of range" );
  2624. }
  2625. return channelJoints[ channel ].Num();
  2626. }
  2627. /*
  2628. =====================
  2629. idDeclModelDef::GetChannelJoints
  2630. =====================
  2631. */
  2632. const int * idDeclModelDef::GetChannelJoints( int channel ) const {
  2633. if ( ( channel < 0 ) || ( channel >= ANIM_NumAnimChannels ) ) {
  2634. gameLocal.Error( "idDeclModelDef::GetChannelJoints : channel out of range" );
  2635. }
  2636. return channelJoints[ channel ].Ptr();
  2637. }
  2638. /*
  2639. =====================
  2640. idDeclModelDef::GetVisualOffset
  2641. =====================
  2642. */
  2643. const idVec3 &idDeclModelDef::GetVisualOffset( void ) const {
  2644. return offset;
  2645. }
  2646. /***********************************************************************
  2647. idAnimator
  2648. ***********************************************************************/
  2649. /*
  2650. =====================
  2651. idAnimator::idAnimator
  2652. =====================
  2653. */
  2654. idAnimator::idAnimator() {
  2655. int i, j;
  2656. modelDef = NULL;
  2657. entity = NULL;
  2658. numJoints = 0;
  2659. joints = NULL;
  2660. lastTransformTime = -1;
  2661. stoppedAnimatingUpdate = false;
  2662. removeOriginOffset = false;
  2663. forceUpdate = false;
  2664. frameBounds.Clear();
  2665. AFPoseJoints.SetGranularity( 1 );
  2666. AFPoseJointMods.SetGranularity( 1 );
  2667. AFPoseJointFrame.SetGranularity( 1 );
  2668. ClearAFPose();
  2669. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2670. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2671. channels[ i ][ j ].Reset( NULL );
  2672. }
  2673. }
  2674. }
  2675. /*
  2676. =====================
  2677. idAnimator::~idAnimator
  2678. =====================
  2679. */
  2680. idAnimator::~idAnimator() {
  2681. FreeData();
  2682. }
  2683. /*
  2684. =====================
  2685. idAnimator::Allocated
  2686. =====================
  2687. */
  2688. size_t idAnimator::Allocated( void ) const {
  2689. size_t size;
  2690. size = jointMods.Allocated() + numJoints * sizeof( joints[0] ) + jointMods.Num() * sizeof( jointMods[ 0 ] ) + AFPoseJointMods.Allocated() + AFPoseJointFrame.Allocated() + AFPoseJoints.Allocated();
  2691. return size;
  2692. }
  2693. /*
  2694. =====================
  2695. idAnimator::Save
  2696. archives object for save game file
  2697. =====================
  2698. */
  2699. void idAnimator::Save( idSaveGame *savefile ) const {
  2700. int i;
  2701. int j;
  2702. savefile->WriteModelDef( modelDef );
  2703. savefile->WriteObject( entity );
  2704. savefile->WriteInt( jointMods.Num() );
  2705. for( i = 0; i < jointMods.Num(); i++ ) {
  2706. savefile->WriteInt( jointMods[ i ]->jointnum );
  2707. savefile->WriteMat3( jointMods[ i ]->mat );
  2708. savefile->WriteVec3( jointMods[ i ]->pos );
  2709. savefile->WriteInt( (int&)jointMods[ i ]->transform_pos );
  2710. savefile->WriteInt( (int&)jointMods[ i ]->transform_axis );
  2711. }
  2712. savefile->WriteInt( numJoints );
  2713. for ( i = 0; i < numJoints; i++ ) {
  2714. float *data = joints[i].ToFloatPtr();
  2715. for ( j = 0; j < 12; j++ ) {
  2716. savefile->WriteFloat( data[j] );
  2717. }
  2718. }
  2719. savefile->WriteInt( lastTransformTime );
  2720. savefile->WriteBool( stoppedAnimatingUpdate );
  2721. savefile->WriteBool( forceUpdate );
  2722. savefile->WriteBounds( frameBounds );
  2723. savefile->WriteFloat( AFPoseBlendWeight );
  2724. savefile->WriteInt( AFPoseJoints.Num() );
  2725. for ( i = 0; i < AFPoseJoints.Num(); i++ ) {
  2726. savefile->WriteInt( AFPoseJoints[i] );
  2727. }
  2728. savefile->WriteInt( AFPoseJointMods.Num() );
  2729. for ( i = 0; i < AFPoseJointMods.Num(); i++ ) {
  2730. savefile->WriteInt( (int&)AFPoseJointMods[i].mod );
  2731. savefile->WriteMat3( AFPoseJointMods[i].axis );
  2732. savefile->WriteVec3( AFPoseJointMods[i].origin );
  2733. }
  2734. savefile->WriteInt( AFPoseJointFrame.Num() );
  2735. for ( i = 0; i < AFPoseJointFrame.Num(); i++ ) {
  2736. savefile->WriteFloat( AFPoseJointFrame[i].q.x );
  2737. savefile->WriteFloat( AFPoseJointFrame[i].q.y );
  2738. savefile->WriteFloat( AFPoseJointFrame[i].q.z );
  2739. savefile->WriteFloat( AFPoseJointFrame[i].q.w );
  2740. savefile->WriteVec3( AFPoseJointFrame[i].t );
  2741. }
  2742. savefile->WriteBounds( AFPoseBounds );
  2743. savefile->WriteInt( AFPoseTime );
  2744. savefile->WriteBool( removeOriginOffset );
  2745. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2746. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2747. channels[ i ][ j ].Save( savefile );
  2748. }
  2749. }
  2750. }
  2751. /*
  2752. =====================
  2753. idAnimator::Restore
  2754. unarchives object from save game file
  2755. =====================
  2756. */
  2757. void idAnimator::Restore( idRestoreGame *savefile ) {
  2758. int i;
  2759. int j;
  2760. int num;
  2761. savefile->ReadModelDef( modelDef );
  2762. savefile->ReadObject( reinterpret_cast<idClass *&>( entity ) );
  2763. savefile->ReadInt( num );
  2764. jointMods.SetNum( num );
  2765. for( i = 0; i < num; i++ ) {
  2766. jointMods[ i ] = new jointMod_t;
  2767. savefile->ReadInt( (int&)jointMods[ i ]->jointnum );
  2768. savefile->ReadMat3( jointMods[ i ]->mat );
  2769. savefile->ReadVec3( jointMods[ i ]->pos );
  2770. savefile->ReadInt( (int&)jointMods[ i ]->transform_pos );
  2771. savefile->ReadInt( (int&)jointMods[ i ]->transform_axis );
  2772. }
  2773. savefile->ReadInt( numJoints );
  2774. joints = (idJointMat *) Mem_Alloc16( numJoints * sizeof( joints[0] ) );
  2775. for ( i = 0; i < numJoints; i++ ) {
  2776. float *data = joints[i].ToFloatPtr();
  2777. for ( j = 0; j < 12; j++ ) {
  2778. savefile->ReadFloat( data[j] );
  2779. }
  2780. }
  2781. savefile->ReadInt( lastTransformTime );
  2782. savefile->ReadBool( stoppedAnimatingUpdate );
  2783. savefile->ReadBool( forceUpdate );
  2784. savefile->ReadBounds( frameBounds );
  2785. savefile->ReadFloat( AFPoseBlendWeight );
  2786. savefile->ReadInt( num );
  2787. AFPoseJoints.SetGranularity( 1 );
  2788. AFPoseJoints.SetNum( num );
  2789. for ( i = 0; i < AFPoseJoints.Num(); i++ ) {
  2790. savefile->ReadInt( AFPoseJoints[i] );
  2791. }
  2792. savefile->ReadInt( num );
  2793. AFPoseJointMods.SetGranularity( 1 );
  2794. AFPoseJointMods.SetNum( num );
  2795. for ( i = 0; i < AFPoseJointMods.Num(); i++ ) {
  2796. savefile->ReadInt( (int&)AFPoseJointMods[i].mod );
  2797. savefile->ReadMat3( AFPoseJointMods[i].axis );
  2798. savefile->ReadVec3( AFPoseJointMods[i].origin );
  2799. }
  2800. savefile->ReadInt( num );
  2801. AFPoseJointFrame.SetGranularity( 1 );
  2802. AFPoseJointFrame.SetNum( num );
  2803. for ( i = 0; i < AFPoseJointFrame.Num(); i++ ) {
  2804. savefile->ReadFloat( AFPoseJointFrame[i].q.x );
  2805. savefile->ReadFloat( AFPoseJointFrame[i].q.y );
  2806. savefile->ReadFloat( AFPoseJointFrame[i].q.z );
  2807. savefile->ReadFloat( AFPoseJointFrame[i].q.w );
  2808. savefile->ReadVec3( AFPoseJointFrame[i].t );
  2809. }
  2810. savefile->ReadBounds( AFPoseBounds );
  2811. savefile->ReadInt( AFPoseTime );
  2812. savefile->ReadBool( removeOriginOffset );
  2813. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2814. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2815. channels[ i ][ j ].Restore( savefile, modelDef );
  2816. }
  2817. }
  2818. }
  2819. /*
  2820. =====================
  2821. idAnimator::FreeData
  2822. =====================
  2823. */
  2824. void idAnimator::FreeData( void ) {
  2825. int i, j;
  2826. if ( entity ) {
  2827. entity->BecomeInactive( TH_ANIMATE );
  2828. }
  2829. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2830. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2831. channels[ i ][ j ].Reset( NULL );
  2832. }
  2833. }
  2834. jointMods.DeleteContents( true );
  2835. Mem_Free16( joints );
  2836. joints = NULL;
  2837. numJoints = 0;
  2838. modelDef = NULL;
  2839. ForceUpdate();
  2840. }
  2841. /*
  2842. =====================
  2843. idAnimator::PushAnims
  2844. =====================
  2845. */
  2846. void idAnimator::PushAnims( int channelNum, int currentTime, int blendTime ) {
  2847. int i;
  2848. idAnimBlend *channel;
  2849. channel = channels[ channelNum ];
  2850. if ( !channel[ 0 ].GetWeight( currentTime ) || ( channel[ 0 ].starttime == currentTime ) ) {
  2851. return;
  2852. }
  2853. for( i = ANIM_MaxAnimsPerChannel - 1; i > 0; i-- ) {
  2854. channel[ i ] = channel[ i - 1 ];
  2855. }
  2856. channel[ 0 ].Reset( modelDef );
  2857. channel[ 1 ].Clear( currentTime, blendTime );
  2858. ForceUpdate();
  2859. }
  2860. /*
  2861. =====================
  2862. idAnimator::SetModel
  2863. =====================
  2864. */
  2865. idRenderModel *idAnimator::SetModel( const char *modelname ) {
  2866. int i, j;
  2867. FreeData();
  2868. // check if we're just clearing the model
  2869. if ( !modelname || !*modelname ) {
  2870. return NULL;
  2871. }
  2872. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  2873. if ( !modelDef ) {
  2874. return NULL;
  2875. }
  2876. idRenderModel *renderModel = modelDef->ModelHandle();
  2877. if ( !renderModel ) {
  2878. modelDef = NULL;
  2879. return NULL;
  2880. }
  2881. // make sure model hasn't been purged
  2882. modelDef->Touch();
  2883. modelDef->SetupJoints( &numJoints, &joints, frameBounds, removeOriginOffset );
  2884. modelDef->ModelHandle()->Reset();
  2885. // set the modelDef on all channels
  2886. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  2887. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++ ) {
  2888. channels[ i ][ j ].Reset( modelDef );
  2889. }
  2890. }
  2891. return modelDef->ModelHandle();
  2892. }
  2893. /*
  2894. =====================
  2895. idAnimator::Size
  2896. =====================
  2897. */
  2898. size_t idAnimator::Size( void ) const {
  2899. return sizeof( *this ) + Allocated();
  2900. }
  2901. /*
  2902. =====================
  2903. idAnimator::SetEntity
  2904. =====================
  2905. */
  2906. void idAnimator::SetEntity( idEntity *ent ) {
  2907. entity = ent;
  2908. }
  2909. /*
  2910. =====================
  2911. idAnimator::GetEntity
  2912. =====================
  2913. */
  2914. idEntity *idAnimator::GetEntity( void ) const {
  2915. return entity;
  2916. }
  2917. /*
  2918. =====================
  2919. idAnimator::RemoveOriginOffset
  2920. =====================
  2921. */
  2922. void idAnimator::RemoveOriginOffset( bool remove ) {
  2923. removeOriginOffset = remove;
  2924. }
  2925. /*
  2926. =====================
  2927. idAnimator::RemoveOrigin
  2928. =====================
  2929. */
  2930. bool idAnimator::RemoveOrigin( void ) const {
  2931. return removeOriginOffset;
  2932. }
  2933. /*
  2934. =====================
  2935. idAnimator::GetJointList
  2936. =====================
  2937. */
  2938. void idAnimator::GetJointList( const char *jointnames, idList<jointHandle_t> &jointList ) const {
  2939. if ( modelDef ) {
  2940. modelDef->GetJointList( jointnames, jointList );
  2941. }
  2942. }
  2943. /*
  2944. =====================
  2945. idAnimator::NumAnims
  2946. =====================
  2947. */
  2948. int idAnimator::NumAnims( void ) const {
  2949. if ( !modelDef ) {
  2950. return 0;
  2951. }
  2952. return modelDef->NumAnims();
  2953. }
  2954. /*
  2955. =====================
  2956. idAnimator::GetAnim
  2957. =====================
  2958. */
  2959. const idAnim *idAnimator::GetAnim( int index ) const {
  2960. if ( !modelDef ) {
  2961. return NULL;
  2962. }
  2963. return modelDef->GetAnim( index );
  2964. }
  2965. /*
  2966. =====================
  2967. idAnimator::GetAnim
  2968. =====================
  2969. */
  2970. int idAnimator::GetAnim( const char *name ) const {
  2971. if ( !modelDef ) {
  2972. return 0;
  2973. }
  2974. return modelDef->GetAnim( name );
  2975. }
  2976. /*
  2977. =====================
  2978. idAnimator::HasAnim
  2979. =====================
  2980. */
  2981. bool idAnimator::HasAnim( const char *name ) const {
  2982. if ( !modelDef ) {
  2983. return false;
  2984. }
  2985. return modelDef->HasAnim( name );
  2986. }
  2987. /*
  2988. =====================
  2989. idAnimator::NumJoints
  2990. =====================
  2991. */
  2992. int idAnimator::NumJoints( void ) const {
  2993. return numJoints;
  2994. }
  2995. /*
  2996. =====================
  2997. idAnimator::ModelHandle
  2998. =====================
  2999. */
  3000. idRenderModel *idAnimator::ModelHandle( void ) const {
  3001. if ( !modelDef ) {
  3002. return NULL;
  3003. }
  3004. return modelDef->ModelHandle();
  3005. }
  3006. /*
  3007. =====================
  3008. idAnimator::ModelDef
  3009. =====================
  3010. */
  3011. const idDeclModelDef *idAnimator::ModelDef( void ) const {
  3012. return modelDef;
  3013. }
  3014. /*
  3015. =====================
  3016. idAnimator::CurrentAnim
  3017. =====================
  3018. */
  3019. idAnimBlend *idAnimator::CurrentAnim( int channelNum ) {
  3020. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3021. gameLocal.Error( "idAnimator::CurrentAnim : channel out of range" );
  3022. }
  3023. return &channels[ channelNum ][ 0 ];
  3024. }
  3025. /*
  3026. =====================
  3027. idAnimator::Clear
  3028. =====================
  3029. */
  3030. void idAnimator::Clear( int channelNum, int currentTime, int cleartime ) {
  3031. int i;
  3032. idAnimBlend *blend;
  3033. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3034. gameLocal.Error( "idAnimator::Clear : channel out of range" );
  3035. }
  3036. blend = channels[ channelNum ];
  3037. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3038. blend->Clear( currentTime, cleartime );
  3039. }
  3040. ForceUpdate();
  3041. }
  3042. /*
  3043. =====================
  3044. idAnimator::SetFrame
  3045. =====================
  3046. */
  3047. void idAnimator::SetFrame( int channelNum, int animNum, int frame, int currentTime, int blendTime ) {
  3048. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3049. gameLocal.Error( "idAnimator::SetFrame : channel out of range" );
  3050. }
  3051. if ( !modelDef || !modelDef->GetAnim( animNum ) ) {
  3052. return;
  3053. }
  3054. PushAnims( channelNum, currentTime, blendTime );
  3055. channels[ channelNum ][ 0 ].SetFrame( modelDef, animNum, frame, currentTime, blendTime );
  3056. if ( entity ) {
  3057. entity->BecomeActive( TH_ANIMATE );
  3058. }
  3059. }
  3060. /*
  3061. =====================
  3062. idAnimator::CycleAnim
  3063. =====================
  3064. */
  3065. void idAnimator::CycleAnim( int channelNum, int animNum, int currentTime, int blendTime ) {
  3066. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3067. gameLocal.Error( "idAnimator::CycleAnim : channel out of range" );
  3068. }
  3069. if ( !modelDef || !modelDef->GetAnim( animNum ) ) {
  3070. return;
  3071. }
  3072. PushAnims( channelNum, currentTime, blendTime );
  3073. channels[ channelNum ][ 0 ].CycleAnim( modelDef, animNum, currentTime, blendTime );
  3074. if ( entity ) {
  3075. entity->BecomeActive( TH_ANIMATE );
  3076. }
  3077. }
  3078. /*
  3079. =====================
  3080. idAnimator::PlayAnim
  3081. =====================
  3082. */
  3083. void idAnimator::PlayAnim( int channelNum, int animNum, int currentTime, int blendTime ) {
  3084. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) ) {
  3085. gameLocal.Error( "idAnimator::PlayAnim : channel out of range" );
  3086. }
  3087. if ( !modelDef || !modelDef->GetAnim( animNum ) ) {
  3088. return;
  3089. }
  3090. PushAnims( channelNum, currentTime, blendTime );
  3091. channels[ channelNum ][ 0 ].PlayAnim( modelDef, animNum, currentTime, blendTime );
  3092. if ( entity ) {
  3093. entity->BecomeActive( TH_ANIMATE );
  3094. }
  3095. }
  3096. /*
  3097. =====================
  3098. idAnimator::SyncAnimChannels
  3099. =====================
  3100. */
  3101. void idAnimator::SyncAnimChannels( int channelNum, int fromChannelNum, int currentTime, int blendTime ) {
  3102. if ( ( channelNum < 0 ) || ( channelNum >= ANIM_NumAnimChannels ) || ( fromChannelNum < 0 ) || ( fromChannelNum >= ANIM_NumAnimChannels ) ) {
  3103. gameLocal.Error( "idAnimator::SyncToChannel : channel out of range" );
  3104. }
  3105. idAnimBlend &fromBlend = channels[ fromChannelNum ][ 0 ];
  3106. idAnimBlend &toBlend = channels[ channelNum ][ 0 ];
  3107. float weight = fromBlend.blendEndValue;
  3108. if ( ( fromBlend.Anim() != toBlend.Anim() ) || ( fromBlend.GetStartTime() != toBlend.GetStartTime() ) || ( fromBlend.GetEndTime() != toBlend.GetEndTime() ) ) {
  3109. PushAnims( channelNum, currentTime, blendTime );
  3110. toBlend = fromBlend;
  3111. toBlend.blendStartValue = 0.0f;
  3112. toBlend.blendEndValue = 0.0f;
  3113. }
  3114. toBlend.SetWeight( weight, currentTime - 1, blendTime );
  3115. // disable framecommands on the current channel so that commands aren't called twice
  3116. toBlend.AllowFrameCommands( false );
  3117. if ( entity ) {
  3118. entity->BecomeActive( TH_ANIMATE );
  3119. }
  3120. }
  3121. /*
  3122. =====================
  3123. idAnimator::SetJointPos
  3124. =====================
  3125. */
  3126. void idAnimator::SetJointPos( jointHandle_t jointnum, jointModTransform_t transform_type, const idVec3 &pos ) {
  3127. int i;
  3128. jointMod_t *jointMod;
  3129. if ( !modelDef || !modelDef->ModelHandle() || ( jointnum < 0 ) || ( jointnum >= numJoints ) ) {
  3130. return;
  3131. }
  3132. jointMod = NULL;
  3133. for( i = 0; i < jointMods.Num(); i++ ) {
  3134. if ( jointMods[ i ]->jointnum == jointnum ) {
  3135. jointMod = jointMods[ i ];
  3136. break;
  3137. } else if ( jointMods[ i ]->jointnum > jointnum ) {
  3138. break;
  3139. }
  3140. }
  3141. if ( !jointMod ) {
  3142. jointMod = new jointMod_t;
  3143. jointMod->jointnum = jointnum;
  3144. jointMod->mat.Identity();
  3145. jointMod->transform_axis = JOINTMOD_NONE;
  3146. jointMods.Insert( jointMod, i );
  3147. }
  3148. jointMod->pos = pos;
  3149. jointMod->transform_pos = transform_type;
  3150. if ( entity ) {
  3151. entity->BecomeActive( TH_ANIMATE );
  3152. }
  3153. ForceUpdate();
  3154. }
  3155. /*
  3156. =====================
  3157. idAnimator::SetJointAxis
  3158. =====================
  3159. */
  3160. void idAnimator::SetJointAxis( jointHandle_t jointnum, jointModTransform_t transform_type, const idMat3 &mat ) {
  3161. int i;
  3162. jointMod_t *jointMod;
  3163. if ( !modelDef || !modelDef->ModelHandle() || ( jointnum < 0 ) || ( jointnum >= numJoints ) ) {
  3164. return;
  3165. }
  3166. jointMod = NULL;
  3167. for( i = 0; i < jointMods.Num(); i++ ) {
  3168. if ( jointMods[ i ]->jointnum == jointnum ) {
  3169. jointMod = jointMods[ i ];
  3170. break;
  3171. } else if ( jointMods[ i ]->jointnum > jointnum ) {
  3172. break;
  3173. }
  3174. }
  3175. if ( !jointMod ) {
  3176. jointMod = new jointMod_t;
  3177. jointMod->jointnum = jointnum;
  3178. jointMod->pos.Zero();
  3179. jointMod->transform_pos = JOINTMOD_NONE;
  3180. jointMods.Insert( jointMod, i );
  3181. }
  3182. jointMod->mat = mat;
  3183. jointMod->transform_axis = transform_type;
  3184. if ( entity ) {
  3185. entity->BecomeActive( TH_ANIMATE );
  3186. }
  3187. ForceUpdate();
  3188. }
  3189. /*
  3190. =====================
  3191. idAnimator::ClearJoint
  3192. =====================
  3193. */
  3194. void idAnimator::ClearJoint( jointHandle_t jointnum ) {
  3195. int i;
  3196. if ( !modelDef || !modelDef->ModelHandle() || ( jointnum < 0 ) || ( jointnum >= numJoints ) ) {
  3197. return;
  3198. }
  3199. for( i = 0; i < jointMods.Num(); i++ ) {
  3200. if ( jointMods[ i ]->jointnum == jointnum ) {
  3201. delete jointMods[ i ];
  3202. jointMods.RemoveIndex( i );
  3203. ForceUpdate();
  3204. break;
  3205. } else if ( jointMods[ i ]->jointnum > jointnum ) {
  3206. break;
  3207. }
  3208. }
  3209. }
  3210. /*
  3211. =====================
  3212. idAnimator::ClearAllJoints
  3213. =====================
  3214. */
  3215. void idAnimator::ClearAllJoints( void ) {
  3216. if ( jointMods.Num() ) {
  3217. ForceUpdate();
  3218. }
  3219. jointMods.DeleteContents( true );
  3220. }
  3221. /*
  3222. =====================
  3223. idAnimator::ClearAllAnims
  3224. =====================
  3225. */
  3226. void idAnimator::ClearAllAnims( int currentTime, int cleartime ) {
  3227. int i;
  3228. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3229. Clear( i, currentTime, cleartime );
  3230. }
  3231. ClearAFPose();
  3232. ForceUpdate();
  3233. }
  3234. /*
  3235. ====================
  3236. idAnimator::GetDelta
  3237. ====================
  3238. */
  3239. void idAnimator::GetDelta( int fromtime, int totime, idVec3 &delta ) const {
  3240. int i;
  3241. const idAnimBlend *blend;
  3242. float blendWeight;
  3243. if ( !modelDef || !modelDef->ModelHandle() || ( fromtime == totime ) ) {
  3244. delta.Zero();
  3245. return;
  3246. }
  3247. delta.Zero();
  3248. blendWeight = 0.0f;
  3249. blend = channels[ ANIMCHANNEL_ALL ];
  3250. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3251. blend->BlendDelta( fromtime, totime, delta, blendWeight );
  3252. }
  3253. if ( modelDef->Joints()[ 0 ].channel ) {
  3254. blend = channels[ modelDef->Joints()[ 0 ].channel ];
  3255. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3256. blend->BlendDelta( fromtime, totime, delta, blendWeight );
  3257. }
  3258. }
  3259. }
  3260. /*
  3261. ====================
  3262. idAnimator::GetDeltaRotation
  3263. ====================
  3264. */
  3265. bool idAnimator::GetDeltaRotation( int fromtime, int totime, idMat3 &delta ) const {
  3266. int i;
  3267. const idAnimBlend *blend;
  3268. float blendWeight;
  3269. idQuat q;
  3270. if ( !modelDef || !modelDef->ModelHandle() || ( fromtime == totime ) ) {
  3271. delta.Identity();
  3272. return false;
  3273. }
  3274. q.Set( 0.0f, 0.0f, 0.0f, 1.0f );
  3275. blendWeight = 0.0f;
  3276. blend = channels[ ANIMCHANNEL_ALL ];
  3277. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3278. blend->BlendDeltaRotation( fromtime, totime, q, blendWeight );
  3279. }
  3280. if ( modelDef->Joints()[ 0 ].channel ) {
  3281. blend = channels[ modelDef->Joints()[ 0 ].channel ];
  3282. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3283. blend->BlendDeltaRotation( fromtime, totime, q, blendWeight );
  3284. }
  3285. }
  3286. if ( blendWeight > 0.0f ) {
  3287. delta = q.ToMat3();
  3288. return true;
  3289. } else {
  3290. delta.Identity();
  3291. return false;
  3292. }
  3293. }
  3294. /*
  3295. ====================
  3296. idAnimator::GetOrigin
  3297. ====================
  3298. */
  3299. void idAnimator::GetOrigin( int currentTime, idVec3 &pos ) const {
  3300. int i;
  3301. const idAnimBlend *blend;
  3302. float blendWeight;
  3303. if ( !modelDef || !modelDef->ModelHandle() ) {
  3304. pos.Zero();
  3305. return;
  3306. }
  3307. pos.Zero();
  3308. blendWeight = 0.0f;
  3309. blend = channels[ ANIMCHANNEL_ALL ];
  3310. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3311. blend->BlendOrigin( currentTime, pos, blendWeight, removeOriginOffset );
  3312. }
  3313. if ( modelDef->Joints()[ 0 ].channel ) {
  3314. blend = channels[ modelDef->Joints()[ 0 ].channel ];
  3315. for( i = 0; i < ANIM_MaxAnimsPerChannel; i++, blend++ ) {
  3316. blend->BlendOrigin( currentTime, pos, blendWeight, removeOriginOffset );
  3317. }
  3318. }
  3319. pos += modelDef->GetVisualOffset();
  3320. }
  3321. /*
  3322. ====================
  3323. idAnimator::GetBounds
  3324. ====================
  3325. */
  3326. bool idAnimator::GetBounds( int currentTime, idBounds &bounds ) {
  3327. int i, j;
  3328. const idAnimBlend *blend;
  3329. int count;
  3330. if ( !modelDef || !modelDef->ModelHandle() ) {
  3331. return false;
  3332. }
  3333. if ( AFPoseJoints.Num() ) {
  3334. bounds = AFPoseBounds;
  3335. count = 1;
  3336. } else {
  3337. bounds.Clear();
  3338. count = 0;
  3339. }
  3340. blend = channels[ 0 ];
  3341. for( i = ANIMCHANNEL_ALL; i < ANIM_NumAnimChannels; i++ ) {
  3342. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3343. if ( blend->AddBounds( currentTime, bounds, removeOriginOffset ) ) {
  3344. count++;
  3345. }
  3346. }
  3347. }
  3348. if ( !count ) {
  3349. if ( !frameBounds.IsCleared() ) {
  3350. bounds = frameBounds;
  3351. return true;
  3352. } else {
  3353. bounds.Zero();
  3354. return false;
  3355. }
  3356. }
  3357. bounds.TranslateSelf( modelDef->GetVisualOffset() );
  3358. if ( g_debugBounds.GetBool() ) {
  3359. if ( bounds[1][0] - bounds[0][0] > 2048 || bounds[1][1] - bounds[0][1] > 2048 ) {
  3360. if ( entity ) {
  3361. gameLocal.Warning( "big frameBounds on entity '%s' with model '%s': %f,%f", entity->name.c_str(), modelDef->ModelHandle()->Name(), bounds[1][0] - bounds[0][0], bounds[1][1] - bounds[0][1] );
  3362. } else {
  3363. gameLocal.Warning( "big frameBounds on model '%s': %f,%f", modelDef->ModelHandle()->Name(), bounds[1][0] - bounds[0][0], bounds[1][1] - bounds[0][1] );
  3364. }
  3365. }
  3366. }
  3367. frameBounds = bounds;
  3368. return true;
  3369. }
  3370. /*
  3371. =====================
  3372. idAnimator::InitAFPose
  3373. =====================
  3374. */
  3375. void idAnimator::InitAFPose( void ) {
  3376. if ( !modelDef ) {
  3377. return;
  3378. }
  3379. AFPoseJoints.SetNum( modelDef->Joints().Num(), false );
  3380. AFPoseJoints.SetNum( 0, false );
  3381. AFPoseJointMods.SetNum( modelDef->Joints().Num(), false );
  3382. AFPoseJointFrame.SetNum( modelDef->Joints().Num(), false );
  3383. }
  3384. /*
  3385. =====================
  3386. idAnimator::SetAFPoseJointMod
  3387. =====================
  3388. */
  3389. void idAnimator::SetAFPoseJointMod( const jointHandle_t jointNum, const AFJointModType_t mod, const idMat3 &axis, const idVec3 &origin ) {
  3390. AFPoseJointMods[jointNum].mod = mod;
  3391. AFPoseJointMods[jointNum].axis = axis;
  3392. AFPoseJointMods[jointNum].origin = origin;
  3393. int index = idBinSearch_GreaterEqual<int>( AFPoseJoints.Ptr(), AFPoseJoints.Num(), jointNum );
  3394. if ( index >= AFPoseJoints.Num() || jointNum != AFPoseJoints[index] ) {
  3395. AFPoseJoints.Insert( jointNum, index );
  3396. }
  3397. }
  3398. /*
  3399. =====================
  3400. idAnimator::FinishAFPose
  3401. =====================
  3402. */
  3403. void idAnimator::FinishAFPose( int animNum, const idBounds &bounds, const int time ) {
  3404. int i, j;
  3405. int numJoints;
  3406. int parentNum;
  3407. int jointMod;
  3408. int jointNum;
  3409. const int * jointParent;
  3410. if ( !modelDef ) {
  3411. return;
  3412. }
  3413. const idAnim *anim = modelDef->GetAnim( animNum );
  3414. if ( !anim ) {
  3415. return;
  3416. }
  3417. numJoints = modelDef->Joints().Num();
  3418. if ( !numJoints ) {
  3419. return;
  3420. }
  3421. idRenderModel *md5 = modelDef->ModelHandle();
  3422. const idMD5Anim *md5anim = anim->MD5Anim( 0 );
  3423. if ( numJoints != md5anim->NumJoints() ) {
  3424. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", md5->Name(), md5anim->Name() );
  3425. return;
  3426. }
  3427. idJointQuat *jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  3428. md5anim->GetSingleFrame( 0, jointFrame, modelDef->GetChannelJoints( ANIMCHANNEL_ALL ), modelDef->NumJointsOnChannel( ANIMCHANNEL_ALL ) );
  3429. if ( removeOriginOffset ) {
  3430. #ifdef VELOCITY_MOVE
  3431. jointFrame[ 0 ].t.x = 0.0f;
  3432. #else
  3433. jointFrame[ 0 ].t.Zero();
  3434. #endif
  3435. }
  3436. idJointMat *joints = ( idJointMat * )_alloca16( numJoints * sizeof( *joints ) );
  3437. // convert the joint quaternions to joint matrices
  3438. SIMDProcessor->ConvertJointQuatsToJointMats( joints, jointFrame, numJoints );
  3439. // first joint is always root of entire hierarchy
  3440. if ( AFPoseJoints.Num() && AFPoseJoints[0] == 0 ) {
  3441. switch( AFPoseJointMods[0].mod ) {
  3442. case AF_JOINTMOD_AXIS: {
  3443. joints[0].SetRotation( AFPoseJointMods[0].axis );
  3444. break;
  3445. }
  3446. case AF_JOINTMOD_ORIGIN: {
  3447. joints[0].SetTranslation( AFPoseJointMods[0].origin );
  3448. break;
  3449. }
  3450. case AF_JOINTMOD_BOTH: {
  3451. joints[0].SetRotation( AFPoseJointMods[0].axis );
  3452. joints[0].SetTranslation( AFPoseJointMods[0].origin );
  3453. break;
  3454. }
  3455. }
  3456. j = 1;
  3457. } else {
  3458. j = 0;
  3459. }
  3460. // pointer to joint info
  3461. jointParent = modelDef->JointParents();
  3462. // transform the child joints
  3463. for( i = 1; j < AFPoseJoints.Num(); j++, i++ ) {
  3464. jointMod = AFPoseJoints[j];
  3465. // transform any joints preceding the joint modifier
  3466. SIMDProcessor->TransformJoints( joints, jointParent, i, jointMod - 1 );
  3467. i = jointMod;
  3468. parentNum = jointParent[i];
  3469. switch( AFPoseJointMods[jointMod].mod ) {
  3470. case AF_JOINTMOD_AXIS: {
  3471. joints[i].SetRotation( AFPoseJointMods[jointMod].axis );
  3472. joints[i].SetTranslation( joints[parentNum].ToVec3() + joints[i].ToVec3() * joints[parentNum].ToMat3() );
  3473. break;
  3474. }
  3475. case AF_JOINTMOD_ORIGIN: {
  3476. joints[i].SetRotation( joints[i].ToMat3() * joints[parentNum].ToMat3() );
  3477. joints[i].SetTranslation( AFPoseJointMods[jointMod].origin );
  3478. break;
  3479. }
  3480. case AF_JOINTMOD_BOTH: {
  3481. joints[i].SetRotation( AFPoseJointMods[jointMod].axis );
  3482. joints[i].SetTranslation( AFPoseJointMods[jointMod].origin );
  3483. break;
  3484. }
  3485. }
  3486. }
  3487. // transform the rest of the hierarchy
  3488. SIMDProcessor->TransformJoints( joints, jointParent, i, numJoints - 1 );
  3489. // untransform hierarchy
  3490. SIMDProcessor->UntransformJoints( joints, jointParent, 1, numJoints - 1 );
  3491. // convert joint matrices back to joint quaternions
  3492. SIMDProcessor->ConvertJointMatsToJointQuats( AFPoseJointFrame.Ptr(), joints, numJoints );
  3493. // find all modified joints and their parents
  3494. bool *blendJoints = (bool *) _alloca16( numJoints * sizeof( bool ) );
  3495. memset( blendJoints, 0, numJoints * sizeof( bool ) );
  3496. // mark all modified joints and their parents
  3497. for( i = 0; i < AFPoseJoints.Num(); i++ ) {
  3498. for( jointNum = AFPoseJoints[i]; jointNum != INVALID_JOINT; jointNum = jointParent[jointNum] ) {
  3499. blendJoints[jointNum] = true;
  3500. }
  3501. }
  3502. // lock all parents of modified joints
  3503. AFPoseJoints.SetNum( 0, false );
  3504. for ( i = 0; i < numJoints; i++ ) {
  3505. if ( blendJoints[i] ) {
  3506. AFPoseJoints.Append( i );
  3507. }
  3508. }
  3509. AFPoseBounds = bounds;
  3510. AFPoseTime = time;
  3511. ForceUpdate();
  3512. }
  3513. /*
  3514. =====================
  3515. idAnimator::SetAFPoseBlendWeight
  3516. =====================
  3517. */
  3518. void idAnimator::SetAFPoseBlendWeight( float blendWeight ) {
  3519. AFPoseBlendWeight = blendWeight;
  3520. }
  3521. /*
  3522. =====================
  3523. idAnimator::BlendAFPose
  3524. =====================
  3525. */
  3526. bool idAnimator::BlendAFPose( idJointQuat *blendFrame ) const {
  3527. if ( !AFPoseJoints.Num() ) {
  3528. return false;
  3529. }
  3530. SIMDProcessor->BlendJoints( blendFrame, AFPoseJointFrame.Ptr(), AFPoseBlendWeight, AFPoseJoints.Ptr(), AFPoseJoints.Num() );
  3531. return true;
  3532. }
  3533. /*
  3534. =====================
  3535. idAnimator::ClearAFPose
  3536. =====================
  3537. */
  3538. void idAnimator::ClearAFPose( void ) {
  3539. if ( AFPoseJoints.Num() ) {
  3540. ForceUpdate();
  3541. }
  3542. AFPoseBlendWeight = 1.0f;
  3543. AFPoseJoints.SetNum( 0, false );
  3544. AFPoseBounds.Clear();
  3545. AFPoseTime = 0;
  3546. }
  3547. /*
  3548. =====================
  3549. idAnimator::ServiceAnims
  3550. =====================
  3551. */
  3552. void idAnimator::ServiceAnims( int fromtime, int totime ) {
  3553. int i, j;
  3554. idAnimBlend *blend;
  3555. if ( !modelDef ) {
  3556. return;
  3557. }
  3558. if ( modelDef->ModelHandle() ) {
  3559. blend = channels[ 0 ];
  3560. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3561. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3562. blend->CallFrameCommands( entity, fromtime, totime );
  3563. }
  3564. }
  3565. }
  3566. if ( !IsAnimating( totime ) ) {
  3567. stoppedAnimatingUpdate = true;
  3568. if ( entity ) {
  3569. entity->BecomeInactive( TH_ANIMATE );
  3570. // present one more time with stopped animations so the renderer can properly recreate interactions
  3571. entity->BecomeActive( TH_UPDATEVISUALS );
  3572. }
  3573. }
  3574. }
  3575. /*
  3576. =====================
  3577. idAnimator::IsAnimating
  3578. =====================
  3579. */
  3580. bool idAnimator::IsAnimating( int currentTime ) const {
  3581. int i, j;
  3582. const idAnimBlend *blend;
  3583. if ( !modelDef || !modelDef->ModelHandle() ) {
  3584. return false;
  3585. }
  3586. // if animating with an articulated figure
  3587. if ( AFPoseJoints.Num() && currentTime <= AFPoseTime ) {
  3588. return true;
  3589. }
  3590. blend = channels[ 0 ];
  3591. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3592. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3593. if ( !blend->IsDone( currentTime ) ) {
  3594. return true;
  3595. }
  3596. }
  3597. }
  3598. return false;
  3599. }
  3600. /*
  3601. =====================
  3602. idAnimator::FrameHasChanged
  3603. =====================
  3604. */
  3605. bool idAnimator::FrameHasChanged( int currentTime ) const {
  3606. int i, j;
  3607. const idAnimBlend *blend;
  3608. if ( !modelDef || !modelDef->ModelHandle() ) {
  3609. return false;
  3610. }
  3611. // if animating with an articulated figure
  3612. if ( AFPoseJoints.Num() && currentTime <= AFPoseTime ) {
  3613. return true;
  3614. }
  3615. blend = channels[ 0 ];
  3616. for( i = 0; i < ANIM_NumAnimChannels; i++ ) {
  3617. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3618. if ( blend->FrameHasChanged( currentTime ) ) {
  3619. return true;
  3620. }
  3621. }
  3622. }
  3623. if ( forceUpdate && IsAnimating( currentTime ) ) {
  3624. return true;
  3625. }
  3626. return false;
  3627. }
  3628. /*
  3629. =====================
  3630. idAnimator::CreateFrame
  3631. =====================
  3632. */
  3633. bool idAnimator::CreateFrame( int currentTime, bool force ) {
  3634. int i, j;
  3635. int numJoints;
  3636. int parentNum;
  3637. bool hasAnim;
  3638. bool debugInfo;
  3639. float baseBlend;
  3640. float blendWeight;
  3641. const idAnimBlend * blend;
  3642. const int * jointParent;
  3643. const jointMod_t * jointMod;
  3644. const idJointQuat * defaultPose;
  3645. static idCVar r_showSkel( "r_showSkel", "0", CVAR_RENDERER | CVAR_INTEGER, "", 0, 2, idCmdSystem::ArgCompletion_Integer<0,2> );
  3646. if ( gameLocal.inCinematic && gameLocal.skipCinematic ) {
  3647. return false;
  3648. }
  3649. if ( !modelDef || !modelDef->ModelHandle() ) {
  3650. return false;
  3651. }
  3652. if ( !force && !r_showSkel.GetInteger() ) {
  3653. if ( lastTransformTime == currentTime ) {
  3654. return false;
  3655. }
  3656. if ( lastTransformTime != -1 && !stoppedAnimatingUpdate && !IsAnimating( currentTime ) ) {
  3657. return false;
  3658. }
  3659. }
  3660. lastTransformTime = currentTime;
  3661. stoppedAnimatingUpdate = false;
  3662. if ( entity && ( ( g_debugAnim.GetInteger() == entity->entityNumber ) || ( g_debugAnim.GetInteger() == -2 ) ) ) {
  3663. debugInfo = true;
  3664. gameLocal.Printf( "---------------\n%d: entity '%s':\n", gameLocal.time, entity->GetName() );
  3665. gameLocal.Printf( "model '%s':\n", modelDef->GetModelName() );
  3666. } else {
  3667. debugInfo = false;
  3668. }
  3669. // init the joint buffer
  3670. if ( AFPoseJoints.Num() ) {
  3671. // initialize with AF pose anim for the case where there are no other animations and no AF pose joint modifications
  3672. defaultPose = AFPoseJointFrame.Ptr();
  3673. } else {
  3674. defaultPose = modelDef->GetDefaultPose();
  3675. }
  3676. if ( !defaultPose ) {
  3677. //gameLocal.Warning( "idAnimator::CreateFrame: no defaultPose on '%s'", modelDef->Name() );
  3678. return false;
  3679. }
  3680. numJoints = modelDef->Joints().Num();
  3681. idJointQuat *jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( jointFrame[0] ) );
  3682. SIMDProcessor->Memcpy( jointFrame, defaultPose, numJoints * sizeof( jointFrame[0] ) );
  3683. hasAnim = false;
  3684. // blend the all channel
  3685. baseBlend = 0.0f;
  3686. blend = channels[ ANIMCHANNEL_ALL ];
  3687. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3688. if ( blend->BlendAnim( currentTime, ANIMCHANNEL_ALL, numJoints, jointFrame, baseBlend, removeOriginOffset, false, debugInfo ) ) {
  3689. hasAnim = true;
  3690. if ( baseBlend >= 1.0f ) {
  3691. break;
  3692. }
  3693. }
  3694. }
  3695. // only blend other channels if there's enough space to blend into
  3696. if ( baseBlend < 1.0f ) {
  3697. for( i = ANIMCHANNEL_ALL + 1; i < ANIM_NumAnimChannels; i++ ) {
  3698. if ( !modelDef->NumJointsOnChannel( i ) ) {
  3699. continue;
  3700. }
  3701. if ( i == ANIMCHANNEL_EYELIDS ) {
  3702. // eyelids blend over any previous anims, so skip it and blend it later
  3703. continue;
  3704. }
  3705. blendWeight = baseBlend;
  3706. blend = channels[ i ];
  3707. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3708. if ( blend->BlendAnim( currentTime, i, numJoints, jointFrame, blendWeight, removeOriginOffset, false, debugInfo ) ) {
  3709. hasAnim = true;
  3710. if ( blendWeight >= 1.0f ) {
  3711. // fully blended
  3712. break;
  3713. }
  3714. }
  3715. }
  3716. if ( debugInfo && !AFPoseJoints.Num() && !blendWeight ) {
  3717. gameLocal.Printf( "%d: %s using default pose in model '%s'\n", gameLocal.time, channelNames[ i ], modelDef->GetModelName() );
  3718. }
  3719. }
  3720. }
  3721. // blend in the eyelids
  3722. if ( modelDef->NumJointsOnChannel( ANIMCHANNEL_EYELIDS ) ) {
  3723. blend = channels[ ANIMCHANNEL_EYELIDS ];
  3724. blendWeight = baseBlend;
  3725. for( j = 0; j < ANIM_MaxAnimsPerChannel; j++, blend++ ) {
  3726. if ( blend->BlendAnim( currentTime, ANIMCHANNEL_EYELIDS, numJoints, jointFrame, blendWeight, removeOriginOffset, true, debugInfo ) ) {
  3727. hasAnim = true;
  3728. if ( blendWeight >= 1.0f ) {
  3729. // fully blended
  3730. break;
  3731. }
  3732. }
  3733. }
  3734. }
  3735. // blend the articulated figure pose
  3736. if ( BlendAFPose( jointFrame ) ) {
  3737. hasAnim = true;
  3738. }
  3739. if ( !hasAnim && !jointMods.Num() ) {
  3740. // no animations were updated
  3741. return false;
  3742. }
  3743. // convert the joint quaternions to rotation matrices
  3744. SIMDProcessor->ConvertJointQuatsToJointMats( joints, jointFrame, numJoints );
  3745. // check if we need to modify the origin
  3746. if ( jointMods.Num() && ( jointMods[0]->jointnum == 0 ) ) {
  3747. jointMod = jointMods[0];
  3748. switch( jointMod->transform_axis ) {
  3749. case JOINTMOD_NONE:
  3750. break;
  3751. case JOINTMOD_LOCAL:
  3752. joints[0].SetRotation( jointMod->mat * joints[0].ToMat3() );
  3753. break;
  3754. case JOINTMOD_WORLD:
  3755. joints[0].SetRotation( joints[0].ToMat3() * jointMod->mat );
  3756. break;
  3757. case JOINTMOD_LOCAL_OVERRIDE:
  3758. case JOINTMOD_WORLD_OVERRIDE:
  3759. joints[0].SetRotation( jointMod->mat );
  3760. break;
  3761. }
  3762. switch( jointMod->transform_pos ) {
  3763. case JOINTMOD_NONE:
  3764. break;
  3765. case JOINTMOD_LOCAL:
  3766. joints[0].SetTranslation( joints[0].ToVec3() + jointMod->pos );
  3767. break;
  3768. case JOINTMOD_LOCAL_OVERRIDE:
  3769. case JOINTMOD_WORLD:
  3770. case JOINTMOD_WORLD_OVERRIDE:
  3771. joints[0].SetTranslation( jointMod->pos );
  3772. break;
  3773. }
  3774. j = 1;
  3775. } else {
  3776. j = 0;
  3777. }
  3778. // add in the model offset
  3779. joints[0].SetTranslation( joints[0].ToVec3() + modelDef->GetVisualOffset() );
  3780. // pointer to joint info
  3781. jointParent = modelDef->JointParents();
  3782. // add in any joint modifications
  3783. for( i = 1; j < jointMods.Num(); j++, i++ ) {
  3784. jointMod = jointMods[j];
  3785. // transform any joints preceding the joint modifier
  3786. SIMDProcessor->TransformJoints( joints, jointParent, i, jointMod->jointnum - 1 );
  3787. i = jointMod->jointnum;
  3788. parentNum = jointParent[i];
  3789. // modify the axis
  3790. switch( jointMod->transform_axis ) {
  3791. case JOINTMOD_NONE:
  3792. joints[i].SetRotation( joints[i].ToMat3() * joints[ parentNum ].ToMat3() );
  3793. break;
  3794. case JOINTMOD_LOCAL:
  3795. joints[i].SetRotation( jointMod->mat * ( joints[i].ToMat3() * joints[parentNum].ToMat3() ) );
  3796. break;
  3797. case JOINTMOD_LOCAL_OVERRIDE:
  3798. joints[i].SetRotation( jointMod->mat * joints[parentNum].ToMat3() );
  3799. break;
  3800. case JOINTMOD_WORLD:
  3801. joints[i].SetRotation( ( joints[i].ToMat3() * joints[parentNum].ToMat3() ) * jointMod->mat );
  3802. break;
  3803. case JOINTMOD_WORLD_OVERRIDE:
  3804. joints[i].SetRotation( jointMod->mat );
  3805. break;
  3806. }
  3807. // modify the position
  3808. switch( jointMod->transform_pos ) {
  3809. case JOINTMOD_NONE:
  3810. joints[i].SetTranslation( joints[parentNum].ToVec3() + joints[i].ToVec3() * joints[parentNum].ToMat3() );
  3811. break;
  3812. case JOINTMOD_LOCAL:
  3813. joints[i].SetTranslation( joints[parentNum].ToVec3() + ( joints[i].ToVec3() + jointMod->pos ) * joints[parentNum].ToMat3() );
  3814. break;
  3815. case JOINTMOD_LOCAL_OVERRIDE:
  3816. joints[i].SetTranslation( joints[parentNum].ToVec3() + jointMod->pos * joints[parentNum].ToMat3() );
  3817. break;
  3818. case JOINTMOD_WORLD:
  3819. joints[i].SetTranslation( joints[parentNum].ToVec3() + joints[i].ToVec3() * joints[parentNum].ToMat3() + jointMod->pos );
  3820. break;
  3821. case JOINTMOD_WORLD_OVERRIDE:
  3822. joints[i].SetTranslation( jointMod->pos );
  3823. break;
  3824. }
  3825. }
  3826. // transform the rest of the hierarchy
  3827. SIMDProcessor->TransformJoints( joints, jointParent, i, numJoints - 1 );
  3828. return true;
  3829. }
  3830. /*
  3831. =====================
  3832. idAnimator::ForceUpdate
  3833. =====================
  3834. */
  3835. void idAnimator::ForceUpdate( void ) {
  3836. lastTransformTime = -1;
  3837. forceUpdate = true;
  3838. }
  3839. /*
  3840. =====================
  3841. idAnimator::ClearForceUpdate
  3842. =====================
  3843. */
  3844. void idAnimator::ClearForceUpdate( void ) {
  3845. forceUpdate = false;
  3846. }
  3847. /*
  3848. =====================
  3849. idAnimator::GetJointTransform> gamex86.dll!idAnimator::ForceUpdate() Line 4268 C++
  3850. =====================
  3851. */
  3852. bool idAnimator::GetJointTransform( jointHandle_t jointHandle, int currentTime, idVec3 &offset, idMat3 &axis ) {
  3853. if ( !modelDef || ( jointHandle < 0 ) || ( jointHandle >= modelDef->NumJoints() ) ) {
  3854. return false;
  3855. }
  3856. CreateFrame( currentTime, false );
  3857. offset = joints[ jointHandle ].ToVec3();
  3858. axis = joints[ jointHandle ].ToMat3();
  3859. return true;
  3860. }
  3861. /*
  3862. =====================
  3863. idAnimator::GetJointLocalTransform
  3864. =====================
  3865. */
  3866. bool idAnimator::GetJointLocalTransform( jointHandle_t jointHandle, int currentTime, idVec3 &offset, idMat3 &axis ) {
  3867. if ( !modelDef ) {
  3868. return false;
  3869. }
  3870. const idList<jointInfo_t> &modelJoints = modelDef->Joints();
  3871. if ( ( jointHandle < 0 ) || ( jointHandle >= modelJoints.Num() ) ) {
  3872. return false;
  3873. }
  3874. // FIXME: overkill
  3875. CreateFrame( currentTime, false );
  3876. if ( jointHandle > 0 ) {
  3877. idJointMat m = joints[ jointHandle ];
  3878. m /= joints[ modelJoints[ jointHandle ].parentNum ];
  3879. offset = m.ToVec3();
  3880. axis = m.ToMat3();
  3881. } else {
  3882. offset = joints[ jointHandle ].ToVec3();
  3883. axis = joints[ jointHandle ].ToMat3();
  3884. }
  3885. return true;
  3886. }
  3887. /*
  3888. =====================
  3889. idAnimator::GetJointHandle
  3890. =====================
  3891. */
  3892. jointHandle_t idAnimator::GetJointHandle( const char *name ) const {
  3893. if ( !modelDef || !modelDef->ModelHandle() ) {
  3894. return INVALID_JOINT;
  3895. }
  3896. return modelDef->ModelHandle()->GetJointHandle( name );
  3897. }
  3898. /*
  3899. =====================
  3900. idAnimator::GetJointName
  3901. =====================
  3902. */
  3903. const char *idAnimator::GetJointName( jointHandle_t handle ) const {
  3904. if ( !modelDef || !modelDef->ModelHandle() ) {
  3905. return "";
  3906. }
  3907. return modelDef->ModelHandle()->GetJointName( handle );
  3908. }
  3909. /*
  3910. =====================
  3911. idAnimator::GetChannelForJoint
  3912. =====================
  3913. */
  3914. int idAnimator::GetChannelForJoint( jointHandle_t joint ) const {
  3915. if ( !modelDef ) {
  3916. gameLocal.Error( "idAnimator::GetChannelForJoint: NULL model" );
  3917. }
  3918. if ( ( joint < 0 ) || ( joint >= numJoints ) ) {
  3919. gameLocal.Error( "idAnimator::GetChannelForJoint: invalid joint num (%d)", joint );
  3920. }
  3921. return modelDef->GetJoint( joint )->channel;
  3922. }
  3923. /*
  3924. =====================
  3925. idAnimator::GetFirstChild
  3926. =====================
  3927. */
  3928. jointHandle_t idAnimator::GetFirstChild( const char *name ) const {
  3929. return GetFirstChild( GetJointHandle( name ) );
  3930. }
  3931. /*
  3932. =====================
  3933. idAnimator::GetFirstChild
  3934. =====================
  3935. */
  3936. jointHandle_t idAnimator::GetFirstChild( jointHandle_t jointnum ) const {
  3937. int i;
  3938. int num;
  3939. const jointInfo_t *joint;
  3940. if ( !modelDef ) {
  3941. return INVALID_JOINT;
  3942. }
  3943. num = modelDef->NumJoints();
  3944. if ( !num ) {
  3945. return jointnum;
  3946. }
  3947. joint = modelDef->GetJoint( 0 );
  3948. for( i = 0; i < num; i++, joint++ ) {
  3949. if ( joint->parentNum == jointnum ) {
  3950. return ( jointHandle_t )joint->num;
  3951. }
  3952. }
  3953. return jointnum;
  3954. }
  3955. /*
  3956. =====================
  3957. idAnimator::GetJoints
  3958. =====================
  3959. */
  3960. void idAnimator::GetJoints( int *numJoints, idJointMat **jointsPtr ) {
  3961. *numJoints = this->numJoints;
  3962. *jointsPtr = this->joints;
  3963. }
  3964. /*
  3965. =====================
  3966. idAnimator::GetAnimFlags
  3967. =====================
  3968. */
  3969. const animFlags_t idAnimator::GetAnimFlags( int animNum ) const {
  3970. animFlags_t result;
  3971. const idAnim *anim = GetAnim( animNum );
  3972. if ( anim ) {
  3973. return anim->GetAnimFlags();
  3974. }
  3975. memset( &result, 0, sizeof( result ) );
  3976. return result;
  3977. }
  3978. /*
  3979. =====================
  3980. idAnimator::NumFrames
  3981. =====================
  3982. */
  3983. int idAnimator::NumFrames( int animNum ) const {
  3984. const idAnim *anim = GetAnim( animNum );
  3985. if ( anim ) {
  3986. return anim->NumFrames();
  3987. } else {
  3988. return 0;
  3989. }
  3990. }
  3991. /*
  3992. =====================
  3993. idAnimator::NumSyncedAnims
  3994. =====================
  3995. */
  3996. int idAnimator::NumSyncedAnims( int animNum ) const {
  3997. const idAnim *anim = GetAnim( animNum );
  3998. if ( anim ) {
  3999. return anim->NumAnims();
  4000. } else {
  4001. return 0;
  4002. }
  4003. }
  4004. /*
  4005. =====================
  4006. idAnimator::AnimName
  4007. =====================
  4008. */
  4009. const char *idAnimator::AnimName( int animNum ) const {
  4010. const idAnim *anim = GetAnim( animNum );
  4011. if ( anim ) {
  4012. return anim->Name();
  4013. } else {
  4014. return "";
  4015. }
  4016. }
  4017. /*
  4018. =====================
  4019. idAnimator::AnimFullName
  4020. =====================
  4021. */
  4022. const char *idAnimator::AnimFullName( int animNum ) const {
  4023. const idAnim *anim = GetAnim( animNum );
  4024. if ( anim ) {
  4025. return anim->FullName();
  4026. } else {
  4027. return "";
  4028. }
  4029. }
  4030. /*
  4031. =====================
  4032. idAnimator::AnimLength
  4033. =====================
  4034. */
  4035. int idAnimator::AnimLength( int animNum ) const {
  4036. const idAnim *anim = GetAnim( animNum );
  4037. if ( anim ) {
  4038. return anim->Length();
  4039. } else {
  4040. return 0;
  4041. }
  4042. }
  4043. /*
  4044. =====================
  4045. idAnimator::TotalMovementDelta
  4046. =====================
  4047. */
  4048. const idVec3 &idAnimator::TotalMovementDelta( int animNum ) const {
  4049. const idAnim *anim = GetAnim( animNum );
  4050. if ( anim ) {
  4051. return anim->TotalMovementDelta();
  4052. } else {
  4053. return vec3_origin;
  4054. }
  4055. }
  4056. /***********************************************************************
  4057. Util functions
  4058. ***********************************************************************/
  4059. /*
  4060. =====================
  4061. ANIM_GetModelDefFromEntityDef
  4062. =====================
  4063. */
  4064. const idDeclModelDef *ANIM_GetModelDefFromEntityDef( const idDict *args ) {
  4065. const idDeclModelDef *modelDef;
  4066. idStr name = args->GetString( "model" );
  4067. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, name, false ) );
  4068. if ( modelDef && modelDef->ModelHandle() ) {
  4069. return modelDef;
  4070. }
  4071. return NULL;
  4072. }
  4073. /*
  4074. =====================
  4075. idGameEdit::ANIM_GetModelFromEntityDef
  4076. =====================
  4077. */
  4078. idRenderModel *idGameEdit::ANIM_GetModelFromEntityDef( const idDict *args ) {
  4079. idRenderModel *model;
  4080. const idDeclModelDef *modelDef;
  4081. model = NULL;
  4082. idStr name = args->GetString( "model" );
  4083. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, name, false ) );
  4084. if ( modelDef ) {
  4085. model = modelDef->ModelHandle();
  4086. }
  4087. if ( !model ) {
  4088. model = renderModelManager->FindModel( name );
  4089. }
  4090. if ( model && model->IsDefaultModel() ) {
  4091. return NULL;
  4092. }
  4093. return model;
  4094. }
  4095. /*
  4096. =====================
  4097. idGameEdit::ANIM_GetModelFromEntityDef
  4098. =====================
  4099. */
  4100. idRenderModel *idGameEdit::ANIM_GetModelFromEntityDef( const char *classname ) {
  4101. const idDict *args;
  4102. args = gameLocal.FindEntityDefDict( classname, false );
  4103. if ( !args ) {
  4104. return NULL;
  4105. }
  4106. return ANIM_GetModelFromEntityDef( args );
  4107. }
  4108. /*
  4109. =====================
  4110. idGameEdit::ANIM_GetModelOffsetFromEntityDef
  4111. =====================
  4112. */
  4113. const idVec3 &idGameEdit::ANIM_GetModelOffsetFromEntityDef( const char *classname ) {
  4114. const idDict *args;
  4115. const idDeclModelDef *modelDef;
  4116. args = gameLocal.FindEntityDefDict( classname, false );
  4117. if ( !args ) {
  4118. return vec3_origin;
  4119. }
  4120. modelDef = ANIM_GetModelDefFromEntityDef( args );
  4121. if ( !modelDef ) {
  4122. return vec3_origin;
  4123. }
  4124. return modelDef->GetVisualOffset();
  4125. }
  4126. /*
  4127. =====================
  4128. idGameEdit::ANIM_GetModelFromName
  4129. =====================
  4130. */
  4131. idRenderModel *idGameEdit::ANIM_GetModelFromName( const char *modelName ) {
  4132. const idDeclModelDef *modelDef;
  4133. idRenderModel *model;
  4134. model = NULL;
  4135. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelName, false ) );
  4136. if ( modelDef ) {
  4137. model = modelDef->ModelHandle();
  4138. }
  4139. if ( !model ) {
  4140. model = renderModelManager->FindModel( modelName );
  4141. }
  4142. return model;
  4143. }
  4144. /*
  4145. =====================
  4146. idGameEdit::ANIM_GetAnimFromEntityDef
  4147. =====================
  4148. */
  4149. const idMD5Anim *idGameEdit::ANIM_GetAnimFromEntityDef( const char *classname, const char *animname ) {
  4150. const idDict *args;
  4151. const idMD5Anim *md5anim;
  4152. const idAnim *anim;
  4153. int animNum;
  4154. const char *modelname;
  4155. const idDeclModelDef *modelDef;
  4156. args = gameLocal.FindEntityDefDict( classname, false );
  4157. if ( !args ) {
  4158. return NULL;
  4159. }
  4160. md5anim = NULL;
  4161. modelname = args->GetString( "model" );
  4162. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  4163. if ( modelDef ) {
  4164. animNum = modelDef->GetAnim( animname );
  4165. if ( animNum ) {
  4166. anim = modelDef->GetAnim( animNum );
  4167. if ( anim ) {
  4168. md5anim = anim->MD5Anim( 0 );
  4169. }
  4170. }
  4171. }
  4172. return md5anim;
  4173. }
  4174. /*
  4175. =====================
  4176. idGameEdit::ANIM_GetNumAnimsFromEntityDef
  4177. =====================
  4178. */
  4179. int idGameEdit::ANIM_GetNumAnimsFromEntityDef( const idDict *args ) {
  4180. const char *modelname;
  4181. const idDeclModelDef *modelDef;
  4182. modelname = args->GetString( "model" );
  4183. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  4184. if ( modelDef ) {
  4185. return modelDef->NumAnims();
  4186. }
  4187. return 0;
  4188. }
  4189. /*
  4190. =====================
  4191. idGameEdit::ANIM_GetAnimNameFromEntityDef
  4192. =====================
  4193. */
  4194. const char *idGameEdit::ANIM_GetAnimNameFromEntityDef( const idDict *args, int animNum ) {
  4195. const char *modelname;
  4196. const idDeclModelDef *modelDef;
  4197. modelname = args->GetString( "model" );
  4198. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelname, false ) );
  4199. if ( modelDef ) {
  4200. const idAnim* anim = modelDef->GetAnim( animNum );
  4201. if ( anim ) {
  4202. return anim->FullName();
  4203. }
  4204. }
  4205. return "";
  4206. }
  4207. /*
  4208. =====================
  4209. idGameEdit::ANIM_GetAnim
  4210. =====================
  4211. */
  4212. const idMD5Anim *idGameEdit::ANIM_GetAnim( const char *fileName ) {
  4213. return animationLib.GetAnim( fileName );
  4214. }
  4215. /*
  4216. =====================
  4217. idGameEdit::ANIM_GetLength
  4218. =====================
  4219. */
  4220. int idGameEdit::ANIM_GetLength( const idMD5Anim *anim ) {
  4221. if ( !anim ) {
  4222. return 0;
  4223. }
  4224. return anim->Length();
  4225. }
  4226. /*
  4227. =====================
  4228. idGameEdit::ANIM_GetNumFrames
  4229. =====================
  4230. */
  4231. int idGameEdit::ANIM_GetNumFrames( const idMD5Anim *anim ) {
  4232. if ( !anim ) {
  4233. return 0;
  4234. }
  4235. return anim->NumFrames();
  4236. }
  4237. /*
  4238. =====================
  4239. idGameEdit::ANIM_CreateAnimFrame
  4240. =====================
  4241. */
  4242. void idGameEdit::ANIM_CreateAnimFrame( const idRenderModel *model, const idMD5Anim *anim, int numJoints, idJointMat *joints, int time, const idVec3 &offset, bool remove_origin_offset ) {
  4243. int i;
  4244. frameBlend_t frame;
  4245. const idMD5Joint *md5joints;
  4246. int *index;
  4247. if ( !model || model->IsDefaultModel() || !anim ) {
  4248. return;
  4249. }
  4250. if ( numJoints != model->NumJoints() ) {
  4251. gameLocal.Error( "ANIM_CreateAnimFrame: different # of joints in renderEntity_t than in model (%s)", model->Name() );
  4252. }
  4253. if ( !model->NumJoints() ) {
  4254. // FIXME: Print out a warning?
  4255. return;
  4256. }
  4257. if ( !joints ) {
  4258. gameLocal.Error( "ANIM_CreateAnimFrame: NULL joint frame pointer on model (%s)", model->Name() );
  4259. }
  4260. if ( numJoints != anim->NumJoints() ) {
  4261. gameLocal.Warning( "Model '%s' has different # of joints than anim '%s'", model->Name(), anim->Name() );
  4262. for( i = 0; i < numJoints; i++ ) {
  4263. joints[i].SetRotation( mat3_identity );
  4264. joints[i].SetTranslation( offset );
  4265. }
  4266. return;
  4267. }
  4268. // create index for all joints
  4269. index = ( int * )_alloca16( numJoints * sizeof( int ) );
  4270. for ( i = 0; i < numJoints; i++ ) {
  4271. index[i] = i;
  4272. }
  4273. // create the frame
  4274. anim->ConvertTimeToFrame( time, 1, frame );
  4275. idJointQuat *jointFrame = ( idJointQuat * )_alloca16( numJoints * sizeof( *jointFrame ) );
  4276. anim->GetInterpolatedFrame( frame, jointFrame, index, numJoints );
  4277. // convert joint quaternions to joint matrices
  4278. SIMDProcessor->ConvertJointQuatsToJointMats( joints, jointFrame, numJoints );
  4279. // first joint is always root of entire hierarchy
  4280. if ( remove_origin_offset ) {
  4281. joints[0].SetTranslation( offset );
  4282. } else {
  4283. joints[0].SetTranslation( joints[0].ToVec3() + offset );
  4284. }
  4285. // transform the children
  4286. md5joints = model->GetJoints();
  4287. for( i = 1; i < numJoints; i++ ) {
  4288. joints[i] *= joints[ md5joints[i].parent - md5joints ];
  4289. }
  4290. }
  4291. /*
  4292. =====================
  4293. idGameEdit::ANIM_CreateMeshForAnim
  4294. =====================
  4295. */
  4296. idRenderModel *idGameEdit::ANIM_CreateMeshForAnim( idRenderModel *model, const char *classname, const char *animname, int frame, bool remove_origin_offset ) {
  4297. renderEntity_t ent;
  4298. const idDict *args;
  4299. const char *temp;
  4300. idRenderModel *newmodel;
  4301. const idMD5Anim *md5anim;
  4302. idStr filename;
  4303. idStr extension;
  4304. const idAnim *anim;
  4305. int animNum;
  4306. idVec3 offset;
  4307. const idDeclModelDef *modelDef;
  4308. if ( !model || model->IsDefaultModel() ) {
  4309. return NULL;
  4310. }
  4311. args = gameLocal.FindEntityDefDict( classname, false );
  4312. if ( !args ) {
  4313. return NULL;
  4314. }
  4315. memset( &ent, 0, sizeof( ent ) );
  4316. ent.bounds.Clear();
  4317. ent.suppressSurfaceInViewID = 0;
  4318. modelDef = ANIM_GetModelDefFromEntityDef( args );
  4319. if ( modelDef ) {
  4320. animNum = modelDef->GetAnim( animname );
  4321. if ( !animNum ) {
  4322. return NULL;
  4323. }
  4324. anim = modelDef->GetAnim( animNum );
  4325. if ( !anim ) {
  4326. return NULL;
  4327. }
  4328. md5anim = anim->MD5Anim( 0 );
  4329. ent.customSkin = modelDef->GetDefaultSkin();
  4330. offset = modelDef->GetVisualOffset();
  4331. } else {
  4332. filename = animname;
  4333. filename.ExtractFileExtension( extension );
  4334. if ( !extension.Length() ) {
  4335. animname = args->GetString( va( "anim %s", animname ) );
  4336. }
  4337. md5anim = animationLib.GetAnim( animname );
  4338. offset.Zero();
  4339. }
  4340. if ( !md5anim ) {
  4341. return NULL;
  4342. }
  4343. temp = args->GetString( "skin", "" );
  4344. if ( temp[ 0 ] ) {
  4345. ent.customSkin = declManager->FindSkin( temp );
  4346. }
  4347. ent.numJoints = model->NumJoints();
  4348. ent.joints = ( idJointMat * )Mem_Alloc16( ent.numJoints * sizeof( *ent.joints ) );
  4349. ANIM_CreateAnimFrame( model, md5anim, ent.numJoints, ent.joints, FRAME2MS( frame ), offset, remove_origin_offset );
  4350. newmodel = model->InstantiateDynamicModel( &ent, NULL, NULL );
  4351. Mem_Free16( ent.joints );
  4352. ent.joints = NULL;
  4353. return newmodel;
  4354. }