Weapon.cpp 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939
  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. /***********************************************************************
  24. idWeapon
  25. ***********************************************************************/
  26. //
  27. // event defs
  28. //
  29. const idEventDef EV_Weapon_Clear( "<clear>" );
  30. const idEventDef EV_Weapon_GetOwner( "getOwner", NULL, 'e' );
  31. const idEventDef EV_Weapon_Next( "nextWeapon" );
  32. const idEventDef EV_Weapon_State( "weaponState", "sd" );
  33. const idEventDef EV_Weapon_UseAmmo( "useAmmo", "d" );
  34. const idEventDef EV_Weapon_AddToClip( "addToClip", "d" );
  35. const idEventDef EV_Weapon_AmmoInClip( "ammoInClip", NULL, 'f' );
  36. const idEventDef EV_Weapon_AmmoAvailable( "ammoAvailable", NULL, 'f' );
  37. const idEventDef EV_Weapon_TotalAmmoCount( "totalAmmoCount", NULL, 'f' );
  38. const idEventDef EV_Weapon_ClipSize( "clipSize", NULL, 'f' );
  39. const idEventDef EV_Weapon_WeaponOutOfAmmo( "weaponOutOfAmmo" );
  40. const idEventDef EV_Weapon_WeaponReady( "weaponReady" );
  41. const idEventDef EV_Weapon_WeaponReloading( "weaponReloading" );
  42. const idEventDef EV_Weapon_WeaponHolstered( "weaponHolstered" );
  43. const idEventDef EV_Weapon_WeaponRising( "weaponRising" );
  44. const idEventDef EV_Weapon_WeaponLowering( "weaponLowering" );
  45. const idEventDef EV_Weapon_Flashlight( "flashlight", "d" );
  46. const idEventDef EV_Weapon_LaunchProjectiles( "launchProjectiles", "dffff" );
  47. const idEventDef EV_Weapon_CreateProjectile( "createProjectile", NULL, 'e' );
  48. const idEventDef EV_Weapon_EjectBrass( "ejectBrass" );
  49. const idEventDef EV_Weapon_Melee( "melee", NULL, 'd' );
  50. const idEventDef EV_Weapon_GetWorldModel( "getWorldModel", NULL, 'e' );
  51. const idEventDef EV_Weapon_AllowDrop( "allowDrop", "d" );
  52. const idEventDef EV_Weapon_AutoReload( "autoReload", NULL, 'f' );
  53. const idEventDef EV_Weapon_NetReload( "netReload" );
  54. const idEventDef EV_Weapon_IsInvisible( "isInvisible", NULL, 'f' );
  55. const idEventDef EV_Weapon_NetEndReload( "netEndReload" );
  56. #ifdef _D3XP
  57. const idEventDef EV_Weapon_GrabberHasTarget( "grabberHasTarget", NULL, 'd' );
  58. const idEventDef EV_Weapon_Grabber( "grabber", "d" );
  59. const idEventDef EV_Weapon_Grabber_SetGrabDistance( "grabberGrabDistance", "f" );
  60. const idEventDef EV_Weapon_LaunchProjectilesEllipse( "launchProjectilesEllipse", "dffff" );
  61. const idEventDef EV_Weapon_LaunchPowerup( "launchPowerup", "sfd" );
  62. const idEventDef EV_Weapon_StartWeaponSmoke( "startWeaponSmoke" );
  63. const idEventDef EV_Weapon_StopWeaponSmoke( "stopWeaponSmoke" );
  64. const idEventDef EV_Weapon_StartWeaponParticle( "startWeaponParticle", "s" );
  65. const idEventDef EV_Weapon_StopWeaponParticle( "stopWeaponParticle", "s" );
  66. const idEventDef EV_Weapon_StartWeaponLight( "startWeaponLight", "s" );
  67. const idEventDef EV_Weapon_StopWeaponLight( "stopWeaponLight", "s" );
  68. #endif
  69. //
  70. // class def
  71. //
  72. CLASS_DECLARATION( idAnimatedEntity, idWeapon )
  73. EVENT( EV_Weapon_Clear, idWeapon::Event_Clear )
  74. EVENT( EV_Weapon_GetOwner, idWeapon::Event_GetOwner )
  75. EVENT( EV_Weapon_State, idWeapon::Event_WeaponState )
  76. EVENT( EV_Weapon_WeaponReady, idWeapon::Event_WeaponReady )
  77. EVENT( EV_Weapon_WeaponOutOfAmmo, idWeapon::Event_WeaponOutOfAmmo )
  78. EVENT( EV_Weapon_WeaponReloading, idWeapon::Event_WeaponReloading )
  79. EVENT( EV_Weapon_WeaponHolstered, idWeapon::Event_WeaponHolstered )
  80. EVENT( EV_Weapon_WeaponRising, idWeapon::Event_WeaponRising )
  81. EVENT( EV_Weapon_WeaponLowering, idWeapon::Event_WeaponLowering )
  82. EVENT( EV_Weapon_UseAmmo, idWeapon::Event_UseAmmo )
  83. EVENT( EV_Weapon_AddToClip, idWeapon::Event_AddToClip )
  84. EVENT( EV_Weapon_AmmoInClip, idWeapon::Event_AmmoInClip )
  85. EVENT( EV_Weapon_AmmoAvailable, idWeapon::Event_AmmoAvailable )
  86. EVENT( EV_Weapon_TotalAmmoCount, idWeapon::Event_TotalAmmoCount )
  87. EVENT( EV_Weapon_ClipSize, idWeapon::Event_ClipSize )
  88. EVENT( AI_PlayAnim, idWeapon::Event_PlayAnim )
  89. EVENT( AI_PlayCycle, idWeapon::Event_PlayCycle )
  90. EVENT( AI_SetBlendFrames, idWeapon::Event_SetBlendFrames )
  91. EVENT( AI_GetBlendFrames, idWeapon::Event_GetBlendFrames )
  92. EVENT( AI_AnimDone, idWeapon::Event_AnimDone )
  93. EVENT( EV_Weapon_Next, idWeapon::Event_Next )
  94. EVENT( EV_SetSkin, idWeapon::Event_SetSkin )
  95. EVENT( EV_Weapon_Flashlight, idWeapon::Event_Flashlight )
  96. EVENT( EV_Light_GetLightParm, idWeapon::Event_GetLightParm )
  97. EVENT( EV_Light_SetLightParm, idWeapon::Event_SetLightParm )
  98. EVENT( EV_Light_SetLightParms, idWeapon::Event_SetLightParms )
  99. EVENT( EV_Weapon_LaunchProjectiles, idWeapon::Event_LaunchProjectiles )
  100. EVENT( EV_Weapon_CreateProjectile, idWeapon::Event_CreateProjectile )
  101. EVENT( EV_Weapon_EjectBrass, idWeapon::Event_EjectBrass )
  102. EVENT( EV_Weapon_Melee, idWeapon::Event_Melee )
  103. EVENT( EV_Weapon_GetWorldModel, idWeapon::Event_GetWorldModel )
  104. EVENT( EV_Weapon_AllowDrop, idWeapon::Event_AllowDrop )
  105. EVENT( EV_Weapon_AutoReload, idWeapon::Event_AutoReload )
  106. EVENT( EV_Weapon_NetReload, idWeapon::Event_NetReload )
  107. EVENT( EV_Weapon_IsInvisible, idWeapon::Event_IsInvisible )
  108. EVENT( EV_Weapon_NetEndReload, idWeapon::Event_NetEndReload )
  109. #ifdef _D3XP
  110. EVENT( EV_Weapon_Grabber, idWeapon::Event_Grabber )
  111. EVENT( EV_Weapon_GrabberHasTarget, idWeapon::Event_GrabberHasTarget )
  112. EVENT( EV_Weapon_Grabber_SetGrabDistance, idWeapon::Event_GrabberSetGrabDistance )
  113. EVENT( EV_Weapon_LaunchProjectilesEllipse, idWeapon::Event_LaunchProjectilesEllipse )
  114. EVENT( EV_Weapon_LaunchPowerup, idWeapon::Event_LaunchPowerup )
  115. EVENT( EV_Weapon_StartWeaponSmoke, idWeapon::Event_StartWeaponSmoke )
  116. EVENT( EV_Weapon_StopWeaponSmoke, idWeapon::Event_StopWeaponSmoke )
  117. EVENT( EV_Weapon_StartWeaponParticle, idWeapon::Event_StartWeaponParticle )
  118. EVENT( EV_Weapon_StopWeaponParticle, idWeapon::Event_StopWeaponParticle )
  119. EVENT( EV_Weapon_StartWeaponLight, idWeapon::Event_StartWeaponLight )
  120. EVENT( EV_Weapon_StopWeaponLight, idWeapon::Event_StopWeaponLight )
  121. #endif
  122. END_CLASS
  123. /***********************************************************************
  124. init
  125. ***********************************************************************/
  126. /*
  127. ================
  128. idWeapon::idWeapon()
  129. ================
  130. */
  131. idWeapon::idWeapon() {
  132. owner = NULL;
  133. worldModel = NULL;
  134. weaponDef = NULL;
  135. thread = NULL;
  136. memset( &guiLight, 0, sizeof( guiLight ) );
  137. memset( &muzzleFlash, 0, sizeof( muzzleFlash ) );
  138. memset( &worldMuzzleFlash, 0, sizeof( worldMuzzleFlash ) );
  139. memset( &nozzleGlow, 0, sizeof( nozzleGlow ) );
  140. muzzleFlashEnd = 0;
  141. flashColor = vec3_origin;
  142. muzzleFlashHandle = -1;
  143. worldMuzzleFlashHandle = -1;
  144. guiLightHandle = -1;
  145. nozzleGlowHandle = -1;
  146. modelDefHandle = -1;
  147. #ifdef _D3XP
  148. grabberState = -1;
  149. #endif
  150. berserk = 2;
  151. brassDelay = 0;
  152. allowDrop = true;
  153. Clear();
  154. fl.networkSync = true;
  155. }
  156. /*
  157. ================
  158. idWeapon::~idWeapon()
  159. ================
  160. */
  161. idWeapon::~idWeapon() {
  162. Clear();
  163. delete worldModel.GetEntity();
  164. }
  165. /*
  166. ================
  167. idWeapon::Spawn
  168. ================
  169. */
  170. void idWeapon::Spawn( void ) {
  171. if ( !gameLocal.isClient ) {
  172. // setup the world model
  173. worldModel = static_cast< idAnimatedEntity * >( gameLocal.SpawnEntityType( idAnimatedEntity::Type, NULL ) );
  174. worldModel.GetEntity()->fl.networkSync = true;
  175. }
  176. #ifdef _D3XP
  177. if ( 1 /*!gameLocal.isMultiplayer*/ ) {
  178. grabber.Initialize();
  179. }
  180. #endif
  181. thread = new idThread();
  182. thread->ManualDelete();
  183. thread->ManualControl();
  184. }
  185. /*
  186. ================
  187. idWeapon::SetOwner
  188. Only called at player spawn time, not each weapon switch
  189. ================
  190. */
  191. void idWeapon::SetOwner( idPlayer *_owner ) {
  192. assert( !owner );
  193. owner = _owner;
  194. SetName( va( "%s_weapon", owner->name.c_str() ) );
  195. if ( worldModel.GetEntity() ) {
  196. worldModel.GetEntity()->SetName( va( "%s_weapon_worldmodel", owner->name.c_str() ) );
  197. }
  198. }
  199. /*
  200. ================
  201. idWeapon::ShouldConstructScriptObjectAtSpawn
  202. Called during idEntity::Spawn to see if it should construct the script object or not.
  203. Overridden by subclasses that need to spawn the script object themselves.
  204. ================
  205. */
  206. bool idWeapon::ShouldConstructScriptObjectAtSpawn( void ) const {
  207. return false;
  208. }
  209. /*
  210. ================
  211. idWeapon::CacheWeapon
  212. ================
  213. */
  214. void idWeapon::CacheWeapon( const char *weaponName ) {
  215. const idDeclEntityDef *weaponDef;
  216. const char *brassDefName;
  217. const char *clipModelName;
  218. idTraceModel trm;
  219. const char *guiName;
  220. weaponDef = gameLocal.FindEntityDef( weaponName, false );
  221. if ( !weaponDef ) {
  222. return;
  223. }
  224. // precache the brass collision model
  225. brassDefName = weaponDef->dict.GetString( "def_ejectBrass" );
  226. if ( brassDefName[0] ) {
  227. const idDeclEntityDef *brassDef = gameLocal.FindEntityDef( brassDefName, false );
  228. if ( brassDef ) {
  229. brassDef->dict.GetString( "clipmodel", "", &clipModelName );
  230. if ( !clipModelName[0] ) {
  231. clipModelName = brassDef->dict.GetString( "model" ); // use the visual model
  232. }
  233. // load the trace model
  234. collisionModelManager->TrmFromModel( clipModelName, trm );
  235. }
  236. }
  237. guiName = weaponDef->dict.GetString( "gui" );
  238. if ( guiName[0] ) {
  239. uiManager->FindGui( guiName, true, false, true );
  240. }
  241. }
  242. /*
  243. ================
  244. idWeapon::Save
  245. ================
  246. */
  247. void idWeapon::Save( idSaveGame *savefile ) const {
  248. savefile->WriteInt( status );
  249. savefile->WriteObject( thread );
  250. savefile->WriteString( state );
  251. savefile->WriteString( idealState );
  252. savefile->WriteInt( animBlendFrames );
  253. savefile->WriteInt( animDoneTime );
  254. savefile->WriteBool( isLinked );
  255. savefile->WriteObject( owner );
  256. worldModel.Save( savefile );
  257. savefile->WriteInt( hideTime );
  258. savefile->WriteFloat( hideDistance );
  259. savefile->WriteInt( hideStartTime );
  260. savefile->WriteFloat( hideStart );
  261. savefile->WriteFloat( hideEnd );
  262. savefile->WriteFloat( hideOffset );
  263. savefile->WriteBool( hide );
  264. savefile->WriteBool( disabled );
  265. savefile->WriteInt( berserk );
  266. savefile->WriteVec3( playerViewOrigin );
  267. savefile->WriteMat3( playerViewAxis );
  268. savefile->WriteVec3( viewWeaponOrigin );
  269. savefile->WriteMat3( viewWeaponAxis );
  270. savefile->WriteVec3( muzzleOrigin );
  271. savefile->WriteMat3( muzzleAxis );
  272. savefile->WriteVec3( pushVelocity );
  273. savefile->WriteString( weaponDef->GetName() );
  274. savefile->WriteFloat( meleeDistance );
  275. savefile->WriteString( meleeDefName );
  276. savefile->WriteInt( brassDelay );
  277. savefile->WriteString( icon );
  278. savefile->WriteInt( guiLightHandle );
  279. savefile->WriteRenderLight( guiLight );
  280. savefile->WriteInt( muzzleFlashHandle );
  281. savefile->WriteRenderLight( muzzleFlash );
  282. savefile->WriteInt( worldMuzzleFlashHandle );
  283. savefile->WriteRenderLight( worldMuzzleFlash );
  284. savefile->WriteVec3( flashColor );
  285. savefile->WriteInt( muzzleFlashEnd );
  286. savefile->WriteInt( flashTime );
  287. savefile->WriteBool( lightOn );
  288. savefile->WriteBool( silent_fire );
  289. savefile->WriteInt( kick_endtime );
  290. savefile->WriteInt( muzzle_kick_time );
  291. savefile->WriteInt( muzzle_kick_maxtime );
  292. savefile->WriteAngles( muzzle_kick_angles );
  293. savefile->WriteVec3( muzzle_kick_offset );
  294. savefile->WriteInt( ammoType );
  295. savefile->WriteInt( ammoRequired );
  296. savefile->WriteInt( clipSize );
  297. savefile->WriteInt( ammoClip );
  298. savefile->WriteInt( lowAmmo );
  299. savefile->WriteBool( powerAmmo );
  300. // savegames <= 17
  301. savefile->WriteInt( 0 );
  302. savefile->WriteInt( zoomFov );
  303. savefile->WriteJoint( barrelJointView );
  304. savefile->WriteJoint( flashJointView );
  305. savefile->WriteJoint( ejectJointView );
  306. savefile->WriteJoint( guiLightJointView );
  307. savefile->WriteJoint( ventLightJointView );
  308. savefile->WriteJoint( flashJointWorld );
  309. savefile->WriteJoint( barrelJointWorld );
  310. savefile->WriteJoint( ejectJointWorld );
  311. savefile->WriteBool( hasBloodSplat );
  312. savefile->WriteSoundShader( sndHum );
  313. savefile->WriteParticle( weaponSmoke );
  314. savefile->WriteInt( weaponSmokeStartTime );
  315. savefile->WriteBool( continuousSmoke );
  316. savefile->WriteParticle( strikeSmoke );
  317. savefile->WriteInt( strikeSmokeStartTime );
  318. savefile->WriteVec3( strikePos );
  319. savefile->WriteMat3( strikeAxis );
  320. savefile->WriteInt( nextStrikeFx );
  321. savefile->WriteBool( nozzleFx );
  322. savefile->WriteInt( nozzleFxFade );
  323. savefile->WriteInt( lastAttack );
  324. savefile->WriteInt( nozzleGlowHandle );
  325. savefile->WriteRenderLight( nozzleGlow );
  326. savefile->WriteVec3( nozzleGlowColor );
  327. savefile->WriteMaterial( nozzleGlowShader );
  328. savefile->WriteFloat( nozzleGlowRadius );
  329. savefile->WriteInt( weaponAngleOffsetAverages );
  330. savefile->WriteFloat( weaponAngleOffsetScale );
  331. savefile->WriteFloat( weaponAngleOffsetMax );
  332. savefile->WriteFloat( weaponOffsetTime );
  333. savefile->WriteFloat( weaponOffsetScale );
  334. savefile->WriteBool( allowDrop );
  335. savefile->WriteObject( projectileEnt );
  336. #ifdef _D3XP
  337. savefile->WriteStaticObject( grabber );
  338. savefile->WriteInt( grabberState );
  339. savefile->WriteJoint ( smokeJointView );
  340. savefile->WriteInt(weaponParticles.Num());
  341. for(int i = 0; i < weaponParticles.Num(); i++) {
  342. WeaponParticle_t* part = weaponParticles.GetIndex(i);
  343. savefile->WriteString( part->name );
  344. savefile->WriteString( part->particlename );
  345. savefile->WriteBool( part->active );
  346. savefile->WriteInt( part->startTime );
  347. savefile->WriteJoint( part->joint );
  348. savefile->WriteBool( part->smoke );
  349. if(!part->smoke) {
  350. savefile->WriteObject(part->emitter);
  351. }
  352. }
  353. savefile->WriteInt(weaponLights.Num());
  354. for(int i = 0; i < weaponLights.Num(); i++) {
  355. WeaponLight_t* light = weaponLights.GetIndex(i);
  356. savefile->WriteString( light->name );
  357. savefile->WriteBool( light->active );
  358. savefile->WriteInt( light->startTime );
  359. savefile->WriteJoint( light->joint );
  360. savefile->WriteInt( light->lightHandle );
  361. savefile->WriteRenderLight( light->light );
  362. }
  363. #endif
  364. }
  365. /*
  366. ================
  367. idWeapon::Restore
  368. ================
  369. */
  370. void idWeapon::Restore( idRestoreGame *savefile ) {
  371. savefile->ReadInt( (int &)status );
  372. savefile->ReadObject( reinterpret_cast<idClass *&>( thread ) );
  373. savefile->ReadString( state );
  374. savefile->ReadString( idealState );
  375. savefile->ReadInt( animBlendFrames );
  376. savefile->ReadInt( animDoneTime );
  377. savefile->ReadBool( isLinked );
  378. // Re-link script fields
  379. WEAPON_ATTACK.LinkTo( scriptObject, "WEAPON_ATTACK" );
  380. WEAPON_RELOAD.LinkTo( scriptObject, "WEAPON_RELOAD" );
  381. WEAPON_NETRELOAD.LinkTo( scriptObject, "WEAPON_NETRELOAD" );
  382. WEAPON_NETENDRELOAD.LinkTo( scriptObject, "WEAPON_NETENDRELOAD" );
  383. WEAPON_NETFIRING.LinkTo( scriptObject, "WEAPON_NETFIRING" );
  384. WEAPON_RAISEWEAPON.LinkTo( scriptObject, "WEAPON_RAISEWEAPON" );
  385. WEAPON_LOWERWEAPON.LinkTo( scriptObject, "WEAPON_LOWERWEAPON" );
  386. savefile->ReadObject( reinterpret_cast<idClass *&>( owner ) );
  387. worldModel.Restore( savefile );
  388. savefile->ReadInt( hideTime );
  389. savefile->ReadFloat( hideDistance );
  390. savefile->ReadInt( hideStartTime );
  391. savefile->ReadFloat( hideStart );
  392. savefile->ReadFloat( hideEnd );
  393. savefile->ReadFloat( hideOffset );
  394. savefile->ReadBool( hide );
  395. savefile->ReadBool( disabled );
  396. savefile->ReadInt( berserk );
  397. savefile->ReadVec3( playerViewOrigin );
  398. savefile->ReadMat3( playerViewAxis );
  399. savefile->ReadVec3( viewWeaponOrigin );
  400. savefile->ReadMat3( viewWeaponAxis );
  401. savefile->ReadVec3( muzzleOrigin );
  402. savefile->ReadMat3( muzzleAxis );
  403. savefile->ReadVec3( pushVelocity );
  404. idStr objectname;
  405. savefile->ReadString( objectname );
  406. weaponDef = gameLocal.FindEntityDef( objectname );
  407. meleeDef = gameLocal.FindEntityDef( weaponDef->dict.GetString( "def_melee" ), false );
  408. const idDeclEntityDef *projectileDef = gameLocal.FindEntityDef( weaponDef->dict.GetString( "def_projectile" ), false );
  409. if ( projectileDef ) {
  410. projectileDict = projectileDef->dict;
  411. } else {
  412. projectileDict.Clear();
  413. }
  414. const idDeclEntityDef *brassDef = gameLocal.FindEntityDef( weaponDef->dict.GetString( "def_ejectBrass" ), false );
  415. if ( brassDef ) {
  416. brassDict = brassDef->dict;
  417. } else {
  418. brassDict.Clear();
  419. }
  420. savefile->ReadFloat( meleeDistance );
  421. savefile->ReadString( meleeDefName );
  422. savefile->ReadInt( brassDelay );
  423. savefile->ReadString( icon );
  424. savefile->ReadInt( guiLightHandle );
  425. savefile->ReadRenderLight( guiLight );
  426. #ifdef _D3XP
  427. if ( guiLightHandle >= 0 ) {
  428. guiLightHandle = gameRenderWorld->AddLightDef( &guiLight );
  429. }
  430. #endif
  431. savefile->ReadInt( muzzleFlashHandle );
  432. savefile->ReadRenderLight( muzzleFlash );
  433. #ifdef _D3XP
  434. if ( muzzleFlashHandle >= 0 ) {
  435. muzzleFlashHandle = gameRenderWorld->AddLightDef( &muzzleFlash );
  436. }
  437. #endif
  438. savefile->ReadInt( worldMuzzleFlashHandle );
  439. savefile->ReadRenderLight( worldMuzzleFlash );
  440. #ifdef _D3XP
  441. if ( worldMuzzleFlashHandle >= 0 ) {
  442. worldMuzzleFlashHandle = gameRenderWorld->AddLightDef( &worldMuzzleFlash );
  443. }
  444. #endif
  445. savefile->ReadVec3( flashColor );
  446. savefile->ReadInt( muzzleFlashEnd );
  447. savefile->ReadInt( flashTime );
  448. savefile->ReadBool( lightOn );
  449. savefile->ReadBool( silent_fire );
  450. savefile->ReadInt( kick_endtime );
  451. savefile->ReadInt( muzzle_kick_time );
  452. savefile->ReadInt( muzzle_kick_maxtime );
  453. savefile->ReadAngles( muzzle_kick_angles );
  454. savefile->ReadVec3( muzzle_kick_offset );
  455. savefile->ReadInt( (int &)ammoType );
  456. savefile->ReadInt( ammoRequired );
  457. savefile->ReadInt( clipSize );
  458. savefile->ReadInt( ammoClip );
  459. savefile->ReadInt( lowAmmo );
  460. savefile->ReadBool( powerAmmo );
  461. // savegame versions <= 17
  462. int foo;
  463. savefile->ReadInt( foo );
  464. savefile->ReadInt( zoomFov );
  465. savefile->ReadJoint( barrelJointView );
  466. savefile->ReadJoint( flashJointView );
  467. savefile->ReadJoint( ejectJointView );
  468. savefile->ReadJoint( guiLightJointView );
  469. savefile->ReadJoint( ventLightJointView );
  470. savefile->ReadJoint( flashJointWorld );
  471. savefile->ReadJoint( barrelJointWorld );
  472. savefile->ReadJoint( ejectJointWorld );
  473. savefile->ReadBool( hasBloodSplat );
  474. savefile->ReadSoundShader( sndHum );
  475. savefile->ReadParticle( weaponSmoke );
  476. savefile->ReadInt( weaponSmokeStartTime );
  477. savefile->ReadBool( continuousSmoke );
  478. savefile->ReadParticle( strikeSmoke );
  479. savefile->ReadInt( strikeSmokeStartTime );
  480. savefile->ReadVec3( strikePos );
  481. savefile->ReadMat3( strikeAxis );
  482. savefile->ReadInt( nextStrikeFx );
  483. savefile->ReadBool( nozzleFx );
  484. savefile->ReadInt( nozzleFxFade );
  485. savefile->ReadInt( lastAttack );
  486. savefile->ReadInt( nozzleGlowHandle );
  487. savefile->ReadRenderLight( nozzleGlow );
  488. #ifdef _D3XP
  489. if ( nozzleGlowHandle >= 0 ) {
  490. nozzleGlowHandle = gameRenderWorld->AddLightDef( &nozzleGlow );
  491. }
  492. #endif
  493. savefile->ReadVec3( nozzleGlowColor );
  494. savefile->ReadMaterial( nozzleGlowShader );
  495. savefile->ReadFloat( nozzleGlowRadius );
  496. savefile->ReadInt( weaponAngleOffsetAverages );
  497. savefile->ReadFloat( weaponAngleOffsetScale );
  498. savefile->ReadFloat( weaponAngleOffsetMax );
  499. savefile->ReadFloat( weaponOffsetTime );
  500. savefile->ReadFloat( weaponOffsetScale );
  501. savefile->ReadBool( allowDrop );
  502. savefile->ReadObject( reinterpret_cast<idClass *&>( projectileEnt ) );
  503. #ifdef _D3XP
  504. savefile->ReadStaticObject( grabber );
  505. savefile->ReadInt( grabberState );
  506. savefile->ReadJoint ( smokeJointView );
  507. int particleCount;
  508. savefile->ReadInt( particleCount );
  509. for(int i = 0; i < particleCount; i++) {
  510. WeaponParticle_t newParticle;
  511. memset(&newParticle, 0, sizeof(newParticle));
  512. idStr name, particlename;
  513. savefile->ReadString( name );
  514. savefile->ReadString( particlename );
  515. strcpy( newParticle.name, name.c_str() );
  516. strcpy( newParticle.particlename, particlename.c_str() );
  517. savefile->ReadBool( newParticle.active );
  518. savefile->ReadInt( newParticle.startTime );
  519. savefile->ReadJoint( newParticle.joint );
  520. savefile->ReadBool( newParticle.smoke );
  521. if(newParticle.smoke) {
  522. newParticle.particle = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, particlename, false ) );
  523. } else {
  524. savefile->ReadObject(reinterpret_cast<idClass *&>(newParticle.emitter));
  525. }
  526. weaponParticles.Set(newParticle.name, newParticle);
  527. }
  528. int lightCount;
  529. savefile->ReadInt( lightCount );
  530. for(int i = 0; i < lightCount; i++) {
  531. WeaponLight_t newLight;
  532. memset(&newLight, 0, sizeof(newLight));
  533. idStr name;
  534. savefile->ReadString( name );
  535. strcpy( newLight.name, name.c_str() );
  536. savefile->ReadBool( newLight.active );
  537. savefile->ReadInt( newLight.startTime );
  538. savefile->ReadJoint( newLight.joint );
  539. savefile->ReadInt( newLight.lightHandle );
  540. savefile->ReadRenderLight( newLight.light );
  541. if ( newLight.lightHandle >= 0 ) {
  542. newLight.lightHandle = gameRenderWorld->AddLightDef( &newLight.light );
  543. }
  544. weaponLights.Set(newLight.name, newLight);
  545. }
  546. #endif
  547. }
  548. /***********************************************************************
  549. Weapon definition management
  550. ***********************************************************************/
  551. /*
  552. ================
  553. idWeapon::Clear
  554. ================
  555. */
  556. void idWeapon::Clear( void ) {
  557. CancelEvents( &EV_Weapon_Clear );
  558. DeconstructScriptObject();
  559. scriptObject.Free();
  560. WEAPON_ATTACK.Unlink();
  561. WEAPON_RELOAD.Unlink();
  562. WEAPON_NETRELOAD.Unlink();
  563. WEAPON_NETENDRELOAD.Unlink();
  564. WEAPON_NETFIRING.Unlink();
  565. WEAPON_RAISEWEAPON.Unlink();
  566. WEAPON_LOWERWEAPON.Unlink();
  567. if ( muzzleFlashHandle != -1 ) {
  568. gameRenderWorld->FreeLightDef( muzzleFlashHandle );
  569. muzzleFlashHandle = -1;
  570. }
  571. if ( muzzleFlashHandle != -1 ) {
  572. gameRenderWorld->FreeLightDef( muzzleFlashHandle );
  573. muzzleFlashHandle = -1;
  574. }
  575. if ( worldMuzzleFlashHandle != -1 ) {
  576. gameRenderWorld->FreeLightDef( worldMuzzleFlashHandle );
  577. worldMuzzleFlashHandle = -1;
  578. }
  579. if ( guiLightHandle != -1 ) {
  580. gameRenderWorld->FreeLightDef( guiLightHandle );
  581. guiLightHandle = -1;
  582. }
  583. if ( nozzleGlowHandle != -1 ) {
  584. gameRenderWorld->FreeLightDef( nozzleGlowHandle );
  585. nozzleGlowHandle = -1;
  586. }
  587. memset( &renderEntity, 0, sizeof( renderEntity ) );
  588. renderEntity.entityNum = entityNumber;
  589. renderEntity.noShadow = true;
  590. renderEntity.noSelfShadow = true;
  591. renderEntity.customSkin = NULL;
  592. // set default shader parms
  593. renderEntity.shaderParms[ SHADERPARM_RED ] = 1.0f;
  594. renderEntity.shaderParms[ SHADERPARM_GREEN ]= 1.0f;
  595. renderEntity.shaderParms[ SHADERPARM_BLUE ] = 1.0f;
  596. renderEntity.shaderParms[3] = 1.0f;
  597. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = 0.0f;
  598. renderEntity.shaderParms[5] = 0.0f;
  599. renderEntity.shaderParms[6] = 0.0f;
  600. renderEntity.shaderParms[7] = 0.0f;
  601. if ( refSound.referenceSound ) {
  602. refSound.referenceSound->Free( true );
  603. }
  604. memset( &refSound, 0, sizeof( refSound_t ) );
  605. // setting diversity to 0 results in no random sound. -1 indicates random.
  606. refSound.diversity = -1.0f;
  607. if ( owner ) {
  608. // don't spatialize the weapon sounds
  609. refSound.listenerId = owner->GetListenerId();
  610. }
  611. // clear out the sounds from our spawnargs since we'll copy them from the weapon def
  612. const idKeyValue *kv = spawnArgs.MatchPrefix( "snd_" );
  613. while( kv ) {
  614. spawnArgs.Delete( kv->GetKey() );
  615. kv = spawnArgs.MatchPrefix( "snd_" );
  616. }
  617. hideTime = 300;
  618. hideDistance = -15.0f;
  619. hideStartTime = gameLocal.time - hideTime;
  620. hideStart = 0.0f;
  621. hideEnd = 0.0f;
  622. hideOffset = 0.0f;
  623. hide = false;
  624. disabled = false;
  625. weaponSmoke = NULL;
  626. weaponSmokeStartTime = 0;
  627. continuousSmoke = false;
  628. strikeSmoke = NULL;
  629. strikeSmokeStartTime = 0;
  630. strikePos.Zero();
  631. strikeAxis = mat3_identity;
  632. nextStrikeFx = 0;
  633. icon = "";
  634. playerViewAxis.Identity();
  635. playerViewOrigin.Zero();
  636. viewWeaponAxis.Identity();
  637. viewWeaponOrigin.Zero();
  638. muzzleAxis.Identity();
  639. muzzleOrigin.Zero();
  640. pushVelocity.Zero();
  641. status = WP_HOLSTERED;
  642. state = "";
  643. idealState = "";
  644. animBlendFrames = 0;
  645. animDoneTime = 0;
  646. projectileDict.Clear();
  647. meleeDef = NULL;
  648. meleeDefName = "";
  649. meleeDistance = 0.0f;
  650. brassDict.Clear();
  651. flashTime = 250;
  652. lightOn = false;
  653. silent_fire = false;
  654. #ifdef _D3XP
  655. grabberState = -1;
  656. grabber.Update( owner, true );
  657. #endif
  658. ammoType = 0;
  659. ammoRequired = 0;
  660. ammoClip = 0;
  661. clipSize = 0;
  662. lowAmmo = 0;
  663. powerAmmo = false;
  664. kick_endtime = 0;
  665. muzzle_kick_time = 0;
  666. muzzle_kick_maxtime = 0;
  667. muzzle_kick_angles.Zero();
  668. muzzle_kick_offset.Zero();
  669. zoomFov = 90;
  670. barrelJointView = INVALID_JOINT;
  671. flashJointView = INVALID_JOINT;
  672. ejectJointView = INVALID_JOINT;
  673. guiLightJointView = INVALID_JOINT;
  674. ventLightJointView = INVALID_JOINT;
  675. barrelJointWorld = INVALID_JOINT;
  676. flashJointWorld = INVALID_JOINT;
  677. ejectJointWorld = INVALID_JOINT;
  678. #ifdef _D3XP
  679. smokeJointView = INVALID_JOINT;
  680. //Clean up the weapon particles
  681. for(int i = 0; i < weaponParticles.Num(); i++) {
  682. WeaponParticle_t* part = weaponParticles.GetIndex(i);
  683. if(!part->smoke) {
  684. //Destroy the emitters
  685. part->emitter->PostEventMS(&EV_Remove, 0 );
  686. }
  687. }
  688. weaponParticles.Clear();
  689. //Clean up the weapon lights
  690. for(int i = 0; i < weaponLights.Num(); i++) {
  691. WeaponLight_t* light = weaponLights.GetIndex(i);
  692. if ( light->lightHandle != -1 ) {
  693. gameRenderWorld->FreeLightDef( light->lightHandle );
  694. }
  695. }
  696. weaponLights.Clear();
  697. #endif
  698. hasBloodSplat = false;
  699. nozzleFx = false;
  700. nozzleFxFade = 1500;
  701. lastAttack = 0;
  702. nozzleGlowHandle = -1;
  703. nozzleGlowShader = NULL;
  704. nozzleGlowRadius = 10;
  705. nozzleGlowColor.Zero();
  706. weaponAngleOffsetAverages = 0;
  707. weaponAngleOffsetScale = 0.0f;
  708. weaponAngleOffsetMax = 0.0f;
  709. weaponOffsetTime = 0.0f;
  710. weaponOffsetScale = 0.0f;
  711. allowDrop = true;
  712. animator.ClearAllAnims( gameLocal.time, 0 );
  713. FreeModelDef();
  714. sndHum = NULL;
  715. isLinked = false;
  716. projectileEnt = NULL;
  717. isFiring = false;
  718. }
  719. /*
  720. ================
  721. idWeapon::InitWorldModel
  722. ================
  723. */
  724. void idWeapon::InitWorldModel( const idDeclEntityDef *def ) {
  725. idEntity *ent;
  726. ent = worldModel.GetEntity();
  727. assert( ent );
  728. assert( def );
  729. const char *model = def->dict.GetString( "model_world" );
  730. const char *attach = def->dict.GetString( "joint_attach" );
  731. ent->SetSkin( NULL );
  732. if ( model[0] && attach[0] ) {
  733. ent->Show();
  734. ent->SetModel( model );
  735. if ( ent->GetAnimator()->ModelDef() ) {
  736. ent->SetSkin( ent->GetAnimator()->ModelDef()->GetDefaultSkin() );
  737. }
  738. ent->GetPhysics()->SetContents( 0 );
  739. ent->GetPhysics()->SetClipModel( NULL, 1.0f );
  740. ent->BindToJoint( owner, attach, true );
  741. ent->GetPhysics()->SetOrigin( vec3_origin );
  742. ent->GetPhysics()->SetAxis( mat3_identity );
  743. // supress model in player views, but allow it in mirrors and remote views
  744. renderEntity_t *worldModelRenderEntity = ent->GetRenderEntity();
  745. if ( worldModelRenderEntity ) {
  746. worldModelRenderEntity->suppressSurfaceInViewID = owner->entityNumber+1;
  747. worldModelRenderEntity->suppressShadowInViewID = owner->entityNumber+1;
  748. worldModelRenderEntity->suppressShadowInLightID = LIGHTID_VIEW_MUZZLE_FLASH + owner->entityNumber;
  749. }
  750. } else {
  751. ent->SetModel( "" );
  752. ent->Hide();
  753. }
  754. flashJointWorld = ent->GetAnimator()->GetJointHandle( "flash" );
  755. barrelJointWorld = ent->GetAnimator()->GetJointHandle( "muzzle" );
  756. ejectJointWorld = ent->GetAnimator()->GetJointHandle( "eject" );
  757. }
  758. /*
  759. ================
  760. idWeapon::GetWeaponDef
  761. ================
  762. */
  763. void idWeapon::GetWeaponDef( const char *objectname, int ammoinclip ) {
  764. const char *shader;
  765. const char *objectType;
  766. const char *vmodel;
  767. const char *guiName;
  768. const char *projectileName;
  769. const char *brassDefName;
  770. const char *smokeName;
  771. int ammoAvail;
  772. Clear();
  773. if ( !objectname || !objectname[ 0 ] ) {
  774. return;
  775. }
  776. assert( owner );
  777. weaponDef = gameLocal.FindEntityDef( objectname );
  778. ammoType = GetAmmoNumForName( weaponDef->dict.GetString( "ammoType" ) );
  779. ammoRequired = weaponDef->dict.GetInt( "ammoRequired" );
  780. clipSize = weaponDef->dict.GetInt( "clipSize" );
  781. lowAmmo = weaponDef->dict.GetInt( "lowAmmo" );
  782. icon = weaponDef->dict.GetString( "icon" );
  783. silent_fire = weaponDef->dict.GetBool( "silent_fire" );
  784. powerAmmo = weaponDef->dict.GetBool( "powerAmmo" );
  785. muzzle_kick_time = SEC2MS( weaponDef->dict.GetFloat( "muzzle_kick_time" ) );
  786. muzzle_kick_maxtime = SEC2MS( weaponDef->dict.GetFloat( "muzzle_kick_maxtime" ) );
  787. muzzle_kick_angles = weaponDef->dict.GetAngles( "muzzle_kick_angles" );
  788. muzzle_kick_offset = weaponDef->dict.GetVector( "muzzle_kick_offset" );
  789. hideTime = SEC2MS( weaponDef->dict.GetFloat( "hide_time", "0.3" ) );
  790. hideDistance = weaponDef->dict.GetFloat( "hide_distance", "-15" );
  791. // muzzle smoke
  792. smokeName = weaponDef->dict.GetString( "smoke_muzzle" );
  793. if ( *smokeName != '\0' ) {
  794. weaponSmoke = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, smokeName ) );
  795. } else {
  796. weaponSmoke = NULL;
  797. }
  798. continuousSmoke = weaponDef->dict.GetBool( "continuousSmoke" );
  799. weaponSmokeStartTime = ( continuousSmoke ) ? gameLocal.time : 0;
  800. smokeName = weaponDef->dict.GetString( "smoke_strike" );
  801. if ( *smokeName != '\0' ) {
  802. strikeSmoke = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, smokeName ) );
  803. } else {
  804. strikeSmoke = NULL;
  805. }
  806. strikeSmokeStartTime = 0;
  807. strikePos.Zero();
  808. strikeAxis = mat3_identity;
  809. nextStrikeFx = 0;
  810. // setup gui light
  811. memset( &guiLight, 0, sizeof( guiLight ) );
  812. const char *guiLightShader = weaponDef->dict.GetString( "mtr_guiLightShader" );
  813. if ( *guiLightShader != '\0' ) {
  814. guiLight.shader = declManager->FindMaterial( guiLightShader, false );
  815. guiLight.lightRadius[0] = guiLight.lightRadius[1] = guiLight.lightRadius[2] = 3;
  816. guiLight.pointLight = true;
  817. }
  818. // setup the view model
  819. vmodel = weaponDef->dict.GetString( "model_view" );
  820. SetModel( vmodel );
  821. // setup the world model
  822. InitWorldModel( weaponDef );
  823. // copy the sounds from the weapon view model def into out spawnargs
  824. const idKeyValue *kv = weaponDef->dict.MatchPrefix( "snd_" );
  825. while( kv ) {
  826. spawnArgs.Set( kv->GetKey(), kv->GetValue() );
  827. kv = weaponDef->dict.MatchPrefix( "snd_", kv );
  828. }
  829. // find some joints in the model for locating effects
  830. barrelJointView = animator.GetJointHandle( "barrel" );
  831. flashJointView = animator.GetJointHandle( "flash" );
  832. ejectJointView = animator.GetJointHandle( "eject" );
  833. guiLightJointView = animator.GetJointHandle( "guiLight" );
  834. ventLightJointView = animator.GetJointHandle( "ventLight" );
  835. #ifdef _D3XP
  836. idStr smokeJoint = weaponDef->dict.GetString("smoke_joint");
  837. if(smokeJoint.Length() > 0) {
  838. smokeJointView = animator.GetJointHandle( smokeJoint );
  839. } else {
  840. smokeJointView = INVALID_JOINT;
  841. }
  842. #endif
  843. // get the projectile
  844. projectileDict.Clear();
  845. projectileName = weaponDef->dict.GetString( "def_projectile" );
  846. if ( projectileName[0] != '\0' ) {
  847. const idDeclEntityDef *projectileDef = gameLocal.FindEntityDef( projectileName, false );
  848. if ( !projectileDef ) {
  849. gameLocal.Warning( "Unknown projectile '%s' in weapon '%s'", projectileName, objectname );
  850. } else {
  851. const char *spawnclass = projectileDef->dict.GetString( "spawnclass" );
  852. idTypeInfo *cls = idClass::GetClass( spawnclass );
  853. if ( !cls || !cls->IsType( idProjectile::Type ) ) {
  854. gameLocal.Warning( "Invalid spawnclass '%s' on projectile '%s' (used by weapon '%s')", spawnclass, projectileName, objectname );
  855. } else {
  856. projectileDict = projectileDef->dict;
  857. }
  858. }
  859. }
  860. // set up muzzleflash render light
  861. const idMaterial*flashShader;
  862. idVec3 flashTarget;
  863. idVec3 flashUp;
  864. idVec3 flashRight;
  865. float flashRadius;
  866. bool flashPointLight;
  867. weaponDef->dict.GetString( "mtr_flashShader", "", &shader );
  868. flashShader = declManager->FindMaterial( shader, false );
  869. flashPointLight = weaponDef->dict.GetBool( "flashPointLight", "1" );
  870. weaponDef->dict.GetVector( "flashColor", "0 0 0", flashColor );
  871. flashRadius = (float)weaponDef->dict.GetInt( "flashRadius" ); // if 0, no light will spawn
  872. flashTime = SEC2MS( weaponDef->dict.GetFloat( "flashTime", "0.25" ) );
  873. flashTarget = weaponDef->dict.GetVector( "flashTarget" );
  874. flashUp = weaponDef->dict.GetVector( "flashUp" );
  875. flashRight = weaponDef->dict.GetVector( "flashRight" );
  876. memset( &muzzleFlash, 0, sizeof( muzzleFlash ) );
  877. muzzleFlash.lightId = LIGHTID_VIEW_MUZZLE_FLASH + owner->entityNumber;
  878. muzzleFlash.allowLightInViewID = owner->entityNumber+1;
  879. // the weapon lights will only be in first person
  880. guiLight.allowLightInViewID = owner->entityNumber+1;
  881. nozzleGlow.allowLightInViewID = owner->entityNumber+1;
  882. muzzleFlash.pointLight = flashPointLight;
  883. muzzleFlash.shader = flashShader;
  884. muzzleFlash.shaderParms[ SHADERPARM_RED ] = flashColor[0];
  885. muzzleFlash.shaderParms[ SHADERPARM_GREEN ] = flashColor[1];
  886. muzzleFlash.shaderParms[ SHADERPARM_BLUE ] = flashColor[2];
  887. muzzleFlash.shaderParms[ SHADERPARM_TIMESCALE ] = 1.0f;
  888. muzzleFlash.lightRadius[0] = flashRadius;
  889. muzzleFlash.lightRadius[1] = flashRadius;
  890. muzzleFlash.lightRadius[2] = flashRadius;
  891. if ( !flashPointLight ) {
  892. muzzleFlash.target = flashTarget;
  893. muzzleFlash.up = flashUp;
  894. muzzleFlash.right = flashRight;
  895. muzzleFlash.end = flashTarget;
  896. }
  897. // the world muzzle flash is the same, just positioned differently
  898. worldMuzzleFlash = muzzleFlash;
  899. worldMuzzleFlash.suppressLightInViewID = owner->entityNumber+1;
  900. worldMuzzleFlash.allowLightInViewID = 0;
  901. worldMuzzleFlash.lightId = LIGHTID_WORLD_MUZZLE_FLASH + owner->entityNumber;
  902. //-----------------------------------
  903. nozzleFx = weaponDef->dict.GetBool("nozzleFx");
  904. nozzleFxFade = weaponDef->dict.GetInt("nozzleFxFade", "1500");
  905. nozzleGlowColor = weaponDef->dict.GetVector("nozzleGlowColor", "1 1 1");
  906. nozzleGlowRadius = weaponDef->dict.GetFloat("nozzleGlowRadius", "10");
  907. weaponDef->dict.GetString( "mtr_nozzleGlowShader", "", &shader );
  908. nozzleGlowShader = declManager->FindMaterial( shader, false );
  909. // get the melee damage def
  910. meleeDistance = weaponDef->dict.GetFloat( "melee_distance" );
  911. meleeDefName = weaponDef->dict.GetString( "def_melee" );
  912. if ( meleeDefName.Length() ) {
  913. meleeDef = gameLocal.FindEntityDef( meleeDefName, false );
  914. if ( !meleeDef ) {
  915. gameLocal.Error( "Unknown melee '%s'", meleeDefName.c_str() );
  916. }
  917. }
  918. // get the brass def
  919. brassDict.Clear();
  920. brassDelay = weaponDef->dict.GetInt( "ejectBrassDelay", "0" );
  921. brassDefName = weaponDef->dict.GetString( "def_ejectBrass" );
  922. if ( brassDefName[0] ) {
  923. const idDeclEntityDef *brassDef = gameLocal.FindEntityDef( brassDefName, false );
  924. if ( !brassDef ) {
  925. gameLocal.Warning( "Unknown brass '%s'", brassDefName );
  926. } else {
  927. brassDict = brassDef->dict;
  928. }
  929. }
  930. if ( ( ammoType < 0 ) || ( ammoType >= AMMO_NUMTYPES ) ) {
  931. gameLocal.Warning( "Unknown ammotype in object '%s'", objectname );
  932. }
  933. ammoClip = ammoinclip;
  934. if ( ( ammoClip < 0 ) || ( ammoClip > clipSize ) ) {
  935. // first time using this weapon so have it fully loaded to start
  936. ammoClip = clipSize;
  937. ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  938. if ( ammoClip > ammoAvail ) {
  939. ammoClip = ammoAvail;
  940. }
  941. #ifdef _D3XP
  942. //In D3XP we use ammo as soon as it is moved into the clip. This allows for weapons that share ammo
  943. owner->inventory.UseAmmo(ammoType, ammoClip);
  944. #endif
  945. }
  946. renderEntity.gui[ 0 ] = NULL;
  947. guiName = weaponDef->dict.GetString( "gui" );
  948. if ( guiName[0] ) {
  949. renderEntity.gui[ 0 ] = uiManager->FindGui( guiName, true, false, true );
  950. }
  951. zoomFov = weaponDef->dict.GetInt( "zoomFov", "70" );
  952. berserk = weaponDef->dict.GetInt( "berserk", "2" );
  953. weaponAngleOffsetAverages = weaponDef->dict.GetInt( "weaponAngleOffsetAverages", "10" );
  954. weaponAngleOffsetScale = weaponDef->dict.GetFloat( "weaponAngleOffsetScale", "0.25" );
  955. weaponAngleOffsetMax = weaponDef->dict.GetFloat( "weaponAngleOffsetMax", "10" );
  956. weaponOffsetTime = weaponDef->dict.GetFloat( "weaponOffsetTime", "400" );
  957. weaponOffsetScale = weaponDef->dict.GetFloat( "weaponOffsetScale", "0.005" );
  958. if ( !weaponDef->dict.GetString( "weapon_scriptobject", NULL, &objectType ) ) {
  959. gameLocal.Error( "No 'weapon_scriptobject' set on '%s'.", objectname );
  960. }
  961. // setup script object
  962. if ( !scriptObject.SetType( objectType ) ) {
  963. gameLocal.Error( "Script object '%s' not found on weapon '%s'.", objectType, objectname );
  964. }
  965. WEAPON_ATTACK.LinkTo( scriptObject, "WEAPON_ATTACK" );
  966. WEAPON_RELOAD.LinkTo( scriptObject, "WEAPON_RELOAD" );
  967. WEAPON_NETRELOAD.LinkTo( scriptObject, "WEAPON_NETRELOAD" );
  968. WEAPON_NETENDRELOAD.LinkTo( scriptObject, "WEAPON_NETENDRELOAD" );
  969. WEAPON_NETFIRING.LinkTo( scriptObject, "WEAPON_NETFIRING" );
  970. WEAPON_RAISEWEAPON.LinkTo( scriptObject, "WEAPON_RAISEWEAPON" );
  971. WEAPON_LOWERWEAPON.LinkTo( scriptObject, "WEAPON_LOWERWEAPON" );
  972. spawnArgs = weaponDef->dict;
  973. shader = spawnArgs.GetString( "snd_hum" );
  974. if ( shader && *shader ) {
  975. sndHum = declManager->FindSound( shader );
  976. StartSoundShader( sndHum, SND_CHANNEL_BODY, 0, false, NULL );
  977. }
  978. isLinked = true;
  979. // call script object's constructor
  980. ConstructScriptObject();
  981. // make sure we have the correct skin
  982. UpdateSkin();
  983. #ifdef _D3XP
  984. idEntity *ent = worldModel.GetEntity();
  985. DetermineTimeGroup( weaponDef->dict.GetBool( "slowmo", "0" ) );
  986. if ( ent ) {
  987. ent->DetermineTimeGroup( weaponDef->dict.GetBool( "slowmo", "0" ) );
  988. }
  989. //Initialize the particles
  990. if ( !gameLocal.isMultiplayer ) {
  991. const idKeyValue *pkv = weaponDef->dict.MatchPrefix( "weapon_particle", NULL );
  992. while( pkv ) {
  993. WeaponParticle_t newParticle;
  994. memset( &newParticle, 0, sizeof( newParticle ) );
  995. idStr name = pkv->GetValue();
  996. strcpy(newParticle.name, name.c_str());
  997. idStr jointName = weaponDef->dict.GetString(va("%s_joint", name.c_str()));
  998. newParticle.joint = animator.GetJointHandle(jointName.c_str());
  999. newParticle.smoke = weaponDef->dict.GetBool(va("%s_smoke", name.c_str()));
  1000. newParticle.active = false;
  1001. newParticle.startTime = 0;
  1002. idStr particle = weaponDef->dict.GetString(va("%s_particle", name.c_str()));
  1003. strcpy(newParticle.particlename, particle.c_str());
  1004. if(newParticle.smoke) {
  1005. newParticle.particle = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, particle, false ) );
  1006. } else {
  1007. idDict args;
  1008. const idDeclEntityDef *emitterDef = gameLocal.FindEntityDef( "func_emitter", false );
  1009. args = emitterDef->dict;
  1010. args.Set("model", particle.c_str());
  1011. args.SetBool("start_off", true);
  1012. idEntity* ent;
  1013. gameLocal.SpawnEntityDef(args, &ent, false);
  1014. newParticle.emitter = (idFuncEmitter*)ent;
  1015. newParticle.emitter->BecomeActive(TH_THINK);
  1016. }
  1017. weaponParticles.Set(name.c_str(), newParticle);
  1018. pkv = weaponDef->dict.MatchPrefix( "weapon_particle", pkv );
  1019. }
  1020. const idKeyValue *lkv = weaponDef->dict.MatchPrefix( "weapon_light", NULL );
  1021. while( lkv ) {
  1022. WeaponLight_t newLight;
  1023. memset( &newLight, 0, sizeof( newLight ) );
  1024. newLight.lightHandle = -1;
  1025. newLight.active = false;
  1026. newLight.startTime = 0;
  1027. idStr name = lkv->GetValue();
  1028. strcpy(newLight.name, name.c_str());
  1029. idStr jointName = weaponDef->dict.GetString(va("%s_joint", name.c_str()));
  1030. newLight.joint = animator.GetJointHandle(jointName.c_str());
  1031. idStr shader = weaponDef->dict.GetString(va("%s_shader", name.c_str()));
  1032. newLight.light.shader = declManager->FindMaterial( shader, false );
  1033. float radius = weaponDef->dict.GetFloat(va("%s_radius", name.c_str()));
  1034. newLight.light.lightRadius[0] = newLight.light.lightRadius[1] = newLight.light.lightRadius[2] = radius;
  1035. newLight.light.pointLight = true;
  1036. newLight.light.noShadows = true;
  1037. newLight.light.allowLightInViewID = owner->entityNumber+1;
  1038. weaponLights.Set(name.c_str(), newLight);
  1039. lkv = weaponDef->dict.MatchPrefix( "weapon_light", lkv );
  1040. }
  1041. }
  1042. #endif
  1043. }
  1044. /***********************************************************************
  1045. GUIs
  1046. ***********************************************************************/
  1047. /*
  1048. ================
  1049. idWeapon::Icon
  1050. ================
  1051. */
  1052. const char *idWeapon::Icon( void ) const {
  1053. return icon;
  1054. }
  1055. /*
  1056. ================
  1057. idWeapon::UpdateGUI
  1058. ================
  1059. */
  1060. void idWeapon::UpdateGUI( void ) {
  1061. if ( !renderEntity.gui[ 0 ] ) {
  1062. return;
  1063. }
  1064. if ( status == WP_HOLSTERED ) {
  1065. return;
  1066. }
  1067. if ( owner->weaponGone ) {
  1068. // dropping weapons was implemented wierd, so we have to not update the gui when it happens or we'll get a negative ammo count
  1069. return;
  1070. }
  1071. if ( gameLocal.localClientNum != owner->entityNumber ) {
  1072. // if updating the hud for a followed client
  1073. if ( gameLocal.localClientNum >= 0 && gameLocal.entities[ gameLocal.localClientNum ] && gameLocal.entities[ gameLocal.localClientNum ]->IsType( idPlayer::Type ) ) {
  1074. idPlayer *p = static_cast< idPlayer * >( gameLocal.entities[ gameLocal.localClientNum ] );
  1075. if ( !p->spectating || p->spectator != owner->entityNumber ) {
  1076. return;
  1077. }
  1078. } else {
  1079. return;
  1080. }
  1081. }
  1082. int inclip = AmmoInClip();
  1083. int ammoamount = AmmoAvailable();
  1084. if ( ammoamount < 0 ) {
  1085. // show infinite ammo
  1086. renderEntity.gui[ 0 ]->SetStateString( "player_ammo", "" );
  1087. } else {
  1088. // show remaining ammo
  1089. #ifdef _D3XP
  1090. renderEntity.gui[ 0 ]->SetStateString( "player_totalammo", va( "%i", ammoamount) );
  1091. #else
  1092. renderEntity.gui[ 0 ]->SetStateString( "player_totalammo", va( "%i", ammoamount - inclip) );
  1093. #endif
  1094. renderEntity.gui[ 0 ]->SetStateString( "player_ammo", ClipSize() ? va( "%i", inclip ) : "--" );
  1095. renderEntity.gui[ 0 ]->SetStateString( "player_clips", ClipSize() ? va("%i", ammoamount / ClipSize()) : "--" );
  1096. #ifdef _D3XP
  1097. renderEntity.gui[ 0 ]->SetStateString( "player_allammo", va( "%i/%i", inclip, ammoamount ) );
  1098. #else
  1099. renderEntity.gui[ 0 ]->SetStateString( "player_allammo", va( "%i/%i", inclip, ammoamount - inclip ) );
  1100. #endif
  1101. }
  1102. renderEntity.gui[ 0 ]->SetStateBool( "player_ammo_empty", ( ammoamount == 0 ) );
  1103. renderEntity.gui[ 0 ]->SetStateBool( "player_clip_empty", ( inclip == 0 ) );
  1104. renderEntity.gui[ 0 ]->SetStateBool( "player_clip_low", ( inclip <= lowAmmo ) );
  1105. #ifdef _D3XP
  1106. //Let the HUD know the total amount of ammo regardless of the ammo required value
  1107. renderEntity.gui[ 0 ]->SetStateString( "player_ammo_count", va("%i", AmmoCount()));
  1108. //Grabber Gui Info
  1109. renderEntity.gui[ 0 ]->SetStateString( "grabber_state", va("%i", grabberState));
  1110. #endif
  1111. }
  1112. /***********************************************************************
  1113. Model and muzzleflash
  1114. ***********************************************************************/
  1115. /*
  1116. ================
  1117. idWeapon::UpdateFlashPosition
  1118. ================
  1119. */
  1120. void idWeapon::UpdateFlashPosition( void ) {
  1121. // the flash has an explicit joint for locating it
  1122. GetGlobalJointTransform( true, flashJointView, muzzleFlash.origin, muzzleFlash.axis );
  1123. // if the desired point is inside or very close to a wall, back it up until it is clear
  1124. idVec3 start = muzzleFlash.origin - playerViewAxis[0] * 16;
  1125. idVec3 end = muzzleFlash.origin + playerViewAxis[0] * 8;
  1126. trace_t tr;
  1127. gameLocal.clip.TracePoint( tr, start, end, MASK_SHOT_RENDERMODEL, owner );
  1128. // be at least 8 units away from a solid
  1129. muzzleFlash.origin = tr.endpos - playerViewAxis[0] * 8;
  1130. // put the world muzzle flash on the end of the joint, no matter what
  1131. GetGlobalJointTransform( false, flashJointWorld, worldMuzzleFlash.origin, worldMuzzleFlash.axis );
  1132. }
  1133. /*
  1134. ================
  1135. idWeapon::MuzzleFlashLight
  1136. ================
  1137. */
  1138. void idWeapon::MuzzleFlashLight( void ) {
  1139. if ( !lightOn && ( !g_muzzleFlash.GetBool() || !muzzleFlash.lightRadius[0] ) ) {
  1140. return;
  1141. }
  1142. if ( flashJointView == INVALID_JOINT ) {
  1143. return;
  1144. }
  1145. UpdateFlashPosition();
  1146. // these will be different each fire
  1147. muzzleFlash.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  1148. muzzleFlash.shaderParms[ SHADERPARM_DIVERSITY ] = renderEntity.shaderParms[ SHADERPARM_DIVERSITY ];
  1149. worldMuzzleFlash.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  1150. worldMuzzleFlash.shaderParms[ SHADERPARM_DIVERSITY ] = renderEntity.shaderParms[ SHADERPARM_DIVERSITY ];
  1151. // the light will be removed at this time
  1152. muzzleFlashEnd = gameLocal.time + flashTime;
  1153. if ( muzzleFlashHandle != -1 ) {
  1154. gameRenderWorld->UpdateLightDef( muzzleFlashHandle, &muzzleFlash );
  1155. gameRenderWorld->UpdateLightDef( worldMuzzleFlashHandle, &worldMuzzleFlash );
  1156. } else {
  1157. muzzleFlashHandle = gameRenderWorld->AddLightDef( &muzzleFlash );
  1158. worldMuzzleFlashHandle = gameRenderWorld->AddLightDef( &worldMuzzleFlash );
  1159. }
  1160. }
  1161. /*
  1162. ================
  1163. idWeapon::UpdateSkin
  1164. ================
  1165. */
  1166. bool idWeapon::UpdateSkin( void ) {
  1167. const function_t *func;
  1168. if ( !isLinked ) {
  1169. return false;
  1170. }
  1171. func = scriptObject.GetFunction( "UpdateSkin" );
  1172. if ( !func ) {
  1173. common->Warning( "Can't find function 'UpdateSkin' in object '%s'", scriptObject.GetTypeName() );
  1174. return false;
  1175. }
  1176. // use the frameCommandThread since it's safe to use outside of framecommands
  1177. gameLocal.frameCommandThread->CallFunction( this, func, true );
  1178. gameLocal.frameCommandThread->Execute();
  1179. return true;
  1180. }
  1181. /*
  1182. ================
  1183. idWeapon::SetModel
  1184. ================
  1185. */
  1186. void idWeapon::SetModel( const char *modelname ) {
  1187. assert( modelname );
  1188. if ( modelDefHandle >= 0 ) {
  1189. gameRenderWorld->RemoveDecals( modelDefHandle );
  1190. }
  1191. renderEntity.hModel = animator.SetModel( modelname );
  1192. if ( renderEntity.hModel ) {
  1193. renderEntity.customSkin = animator.ModelDef()->GetDefaultSkin();
  1194. animator.GetJoints( &renderEntity.numJoints, &renderEntity.joints );
  1195. } else {
  1196. renderEntity.customSkin = NULL;
  1197. renderEntity.callback = NULL;
  1198. renderEntity.numJoints = 0;
  1199. renderEntity.joints = NULL;
  1200. }
  1201. // hide the model until an animation is played
  1202. Hide();
  1203. }
  1204. /*
  1205. ================
  1206. idWeapon::GetGlobalJointTransform
  1207. This returns the offset and axis of a weapon bone in world space, suitable for attaching models or lights
  1208. ================
  1209. */
  1210. bool idWeapon::GetGlobalJointTransform( bool viewModel, const jointHandle_t jointHandle, idVec3 &offset, idMat3 &axis ) {
  1211. if ( viewModel ) {
  1212. // view model
  1213. if ( animator.GetJointTransform( jointHandle, gameLocal.time, offset, axis ) ) {
  1214. offset = offset * viewWeaponAxis + viewWeaponOrigin;
  1215. axis = axis * viewWeaponAxis;
  1216. return true;
  1217. }
  1218. } else {
  1219. // world model
  1220. if ( worldModel.GetEntity() && worldModel.GetEntity()->GetAnimator()->GetJointTransform( jointHandle, gameLocal.time, offset, axis ) ) {
  1221. offset = worldModel.GetEntity()->GetPhysics()->GetOrigin() + offset * worldModel.GetEntity()->GetPhysics()->GetAxis();
  1222. axis = axis * worldModel.GetEntity()->GetPhysics()->GetAxis();
  1223. return true;
  1224. }
  1225. }
  1226. offset = viewWeaponOrigin;
  1227. axis = viewWeaponAxis;
  1228. return false;
  1229. }
  1230. /*
  1231. ================
  1232. idWeapon::SetPushVelocity
  1233. ================
  1234. */
  1235. void idWeapon::SetPushVelocity( const idVec3 &pushVelocity ) {
  1236. this->pushVelocity = pushVelocity;
  1237. }
  1238. /***********************************************************************
  1239. State control/player interface
  1240. ***********************************************************************/
  1241. /*
  1242. ================
  1243. idWeapon::Think
  1244. ================
  1245. */
  1246. void idWeapon::Think( void ) {
  1247. // do nothing because the present is called from the player through PresentWeapon
  1248. }
  1249. /*
  1250. ================
  1251. idWeapon::Raise
  1252. ================
  1253. */
  1254. void idWeapon::Raise( void ) {
  1255. if ( isLinked ) {
  1256. WEAPON_RAISEWEAPON = true;
  1257. }
  1258. }
  1259. /*
  1260. ================
  1261. idWeapon::PutAway
  1262. ================
  1263. */
  1264. void idWeapon::PutAway( void ) {
  1265. hasBloodSplat = false;
  1266. if ( isLinked ) {
  1267. WEAPON_LOWERWEAPON = true;
  1268. }
  1269. }
  1270. /*
  1271. ================
  1272. idWeapon::Reload
  1273. NOTE: this is only for impulse-triggered reload, auto reload is scripted
  1274. ================
  1275. */
  1276. void idWeapon::Reload( void ) {
  1277. if ( isLinked ) {
  1278. WEAPON_RELOAD = true;
  1279. }
  1280. }
  1281. /*
  1282. ================
  1283. idWeapon::LowerWeapon
  1284. ================
  1285. */
  1286. void idWeapon::LowerWeapon( void ) {
  1287. if ( !hide ) {
  1288. hideStart = 0.0f;
  1289. hideEnd = hideDistance;
  1290. if ( gameLocal.time - hideStartTime < hideTime ) {
  1291. hideStartTime = gameLocal.time - ( hideTime - ( gameLocal.time - hideStartTime ) );
  1292. } else {
  1293. hideStartTime = gameLocal.time;
  1294. }
  1295. hide = true;
  1296. }
  1297. }
  1298. /*
  1299. ================
  1300. idWeapon::RaiseWeapon
  1301. ================
  1302. */
  1303. void idWeapon::RaiseWeapon( void ) {
  1304. Show();
  1305. if ( hide ) {
  1306. hideStart = hideDistance;
  1307. hideEnd = 0.0f;
  1308. if ( gameLocal.time - hideStartTime < hideTime ) {
  1309. hideStartTime = gameLocal.time - ( hideTime - ( gameLocal.time - hideStartTime ) );
  1310. } else {
  1311. hideStartTime = gameLocal.time;
  1312. }
  1313. hide = false;
  1314. }
  1315. }
  1316. /*
  1317. ================
  1318. idWeapon::HideWeapon
  1319. ================
  1320. */
  1321. void idWeapon::HideWeapon( void ) {
  1322. Hide();
  1323. if ( worldModel.GetEntity() ) {
  1324. worldModel.GetEntity()->Hide();
  1325. }
  1326. muzzleFlashEnd = 0;
  1327. }
  1328. /*
  1329. ================
  1330. idWeapon::ShowWeapon
  1331. ================
  1332. */
  1333. void idWeapon::ShowWeapon( void ) {
  1334. Show();
  1335. if ( worldModel.GetEntity() ) {
  1336. worldModel.GetEntity()->Show();
  1337. }
  1338. if ( lightOn ) {
  1339. MuzzleFlashLight();
  1340. }
  1341. }
  1342. /*
  1343. ================
  1344. idWeapon::HideWorldModel
  1345. ================
  1346. */
  1347. void idWeapon::HideWorldModel( void ) {
  1348. if ( worldModel.GetEntity() ) {
  1349. worldModel.GetEntity()->Hide();
  1350. }
  1351. }
  1352. /*
  1353. ================
  1354. idWeapon::ShowWorldModel
  1355. ================
  1356. */
  1357. void idWeapon::ShowWorldModel( void ) {
  1358. if ( worldModel.GetEntity() ) {
  1359. worldModel.GetEntity()->Show();
  1360. }
  1361. }
  1362. /*
  1363. ================
  1364. idWeapon::OwnerDied
  1365. ================
  1366. */
  1367. void idWeapon::OwnerDied( void ) {
  1368. if ( isLinked ) {
  1369. SetState( "OwnerDied", 0 );
  1370. thread->Execute();
  1371. #ifdef _D3XP
  1372. // Update the grabber effects
  1373. if ( /*!gameLocal.isMultiplayer &&*/ grabberState != -1 ) {
  1374. grabber.Update( owner, hide );
  1375. }
  1376. #endif
  1377. }
  1378. Hide();
  1379. if ( worldModel.GetEntity() ) {
  1380. worldModel.GetEntity()->Hide();
  1381. }
  1382. // don't clear the weapon immediately since the owner might have killed himself by firing the weapon
  1383. // within the current stack frame
  1384. PostEventMS( &EV_Weapon_Clear, 0 );
  1385. }
  1386. /*
  1387. ================
  1388. idWeapon::BeginAttack
  1389. ================
  1390. */
  1391. void idWeapon::BeginAttack( void ) {
  1392. if ( status != WP_OUTOFAMMO ) {
  1393. lastAttack = gameLocal.time;
  1394. }
  1395. if ( !isLinked ) {
  1396. return;
  1397. }
  1398. if ( !WEAPON_ATTACK ) {
  1399. if ( sndHum && grabberState == -1 ) { // _D3XP :: don't stop grabber hum
  1400. StopSound( SND_CHANNEL_BODY, false );
  1401. }
  1402. }
  1403. WEAPON_ATTACK = true;
  1404. }
  1405. /*
  1406. ================
  1407. idWeapon::EndAttack
  1408. ================
  1409. */
  1410. void idWeapon::EndAttack( void ) {
  1411. if ( !WEAPON_ATTACK.IsLinked() ) {
  1412. return;
  1413. }
  1414. if ( WEAPON_ATTACK ) {
  1415. WEAPON_ATTACK = false;
  1416. if ( sndHum && grabberState == -1 ) { // _D3XP :: don't stop grabber hum
  1417. StartSoundShader( sndHum, SND_CHANNEL_BODY, 0, false, NULL );
  1418. }
  1419. }
  1420. }
  1421. /*
  1422. ================
  1423. idWeapon::isReady
  1424. ================
  1425. */
  1426. bool idWeapon::IsReady( void ) const {
  1427. return !hide && !IsHidden() && ( ( status == WP_RELOAD ) || ( status == WP_READY ) || ( status == WP_OUTOFAMMO ) );
  1428. }
  1429. /*
  1430. ================
  1431. idWeapon::IsReloading
  1432. ================
  1433. */
  1434. bool idWeapon::IsReloading( void ) const {
  1435. return ( status == WP_RELOAD );
  1436. }
  1437. /*
  1438. ================
  1439. idWeapon::IsHolstered
  1440. ================
  1441. */
  1442. bool idWeapon::IsHolstered( void ) const {
  1443. return ( status == WP_HOLSTERED );
  1444. }
  1445. /*
  1446. ================
  1447. idWeapon::ShowCrosshair
  1448. ================
  1449. */
  1450. bool idWeapon::ShowCrosshair( void ) const {
  1451. return !( state == idStr( WP_RISING ) || state == idStr( WP_LOWERING ) || state == idStr( WP_HOLSTERED ) );
  1452. }
  1453. /*
  1454. =====================
  1455. idWeapon::CanDrop
  1456. =====================
  1457. */
  1458. bool idWeapon::CanDrop( void ) const {
  1459. if ( !weaponDef || !worldModel.GetEntity() ) {
  1460. return false;
  1461. }
  1462. const char *classname = weaponDef->dict.GetString( "def_dropItem" );
  1463. if ( !classname[ 0 ] ) {
  1464. return false;
  1465. }
  1466. return true;
  1467. }
  1468. /*
  1469. ================
  1470. idWeapon::WeaponStolen
  1471. ================
  1472. */
  1473. void idWeapon::WeaponStolen( void ) {
  1474. assert( !gameLocal.isClient );
  1475. if ( projectileEnt ) {
  1476. if ( isLinked ) {
  1477. SetState( "WeaponStolen", 0 );
  1478. thread->Execute();
  1479. }
  1480. projectileEnt = NULL;
  1481. }
  1482. // set to holstered so we can switch weapons right away
  1483. status = WP_HOLSTERED;
  1484. HideWeapon();
  1485. }
  1486. /*
  1487. =====================
  1488. idWeapon::DropItem
  1489. =====================
  1490. */
  1491. idEntity * idWeapon::DropItem( const idVec3 &velocity, int activateDelay, int removeDelay, bool died ) {
  1492. if ( !weaponDef || !worldModel.GetEntity() ) {
  1493. return NULL;
  1494. }
  1495. if ( !allowDrop ) {
  1496. return NULL;
  1497. }
  1498. const char *classname = weaponDef->dict.GetString( "def_dropItem" );
  1499. if ( !classname[0] ) {
  1500. return NULL;
  1501. }
  1502. StopSound( SND_CHANNEL_BODY, true );
  1503. StopSound( SND_CHANNEL_BODY3, true );
  1504. return idMoveableItem::DropItem( classname, worldModel.GetEntity()->GetPhysics()->GetOrigin(), worldModel.GetEntity()->GetPhysics()->GetAxis(), velocity, activateDelay, removeDelay );
  1505. }
  1506. /***********************************************************************
  1507. Script state management
  1508. ***********************************************************************/
  1509. /*
  1510. =====================
  1511. idWeapon::SetState
  1512. =====================
  1513. */
  1514. void idWeapon::SetState( const char *statename, int blendFrames ) {
  1515. const function_t *func;
  1516. if ( !isLinked ) {
  1517. return;
  1518. }
  1519. func = scriptObject.GetFunction( statename );
  1520. if ( !func ) {
  1521. assert( 0 );
  1522. gameLocal.Error( "Can't find function '%s' in object '%s'", statename, scriptObject.GetTypeName() );
  1523. }
  1524. thread->CallFunction( this, func, true );
  1525. state = statename;
  1526. animBlendFrames = blendFrames;
  1527. if ( g_debugWeapon.GetBool() ) {
  1528. gameLocal.Printf( "%d: weapon state : %s\n", gameLocal.time, statename );
  1529. }
  1530. idealState = "";
  1531. }
  1532. /***********************************************************************
  1533. Particles/Effects
  1534. ***********************************************************************/
  1535. /*
  1536. ================
  1537. idWeapon::UpdateNozzelFx
  1538. ================
  1539. */
  1540. void idWeapon::UpdateNozzleFx( void ) {
  1541. if ( !nozzleFx ) {
  1542. return;
  1543. }
  1544. //
  1545. // shader parms
  1546. //
  1547. int la = gameLocal.time - lastAttack + 1;
  1548. float s = 1.0f;
  1549. float l = 0.0f;
  1550. if ( la < nozzleFxFade ) {
  1551. s = ((float)la / nozzleFxFade);
  1552. l = 1.0f - s;
  1553. }
  1554. renderEntity.shaderParms[5] = s;
  1555. renderEntity.shaderParms[6] = l;
  1556. if ( ventLightJointView == INVALID_JOINT ) {
  1557. return;
  1558. }
  1559. //
  1560. // vent light
  1561. //
  1562. if ( nozzleGlowHandle == -1 ) {
  1563. memset(&nozzleGlow, 0, sizeof(nozzleGlow));
  1564. if ( owner ) {
  1565. nozzleGlow.allowLightInViewID = owner->entityNumber+1;
  1566. }
  1567. nozzleGlow.pointLight = true;
  1568. nozzleGlow.noShadows = true;
  1569. nozzleGlow.lightRadius.x = nozzleGlowRadius;
  1570. nozzleGlow.lightRadius.y = nozzleGlowRadius;
  1571. nozzleGlow.lightRadius.z = nozzleGlowRadius;
  1572. nozzleGlow.shader = nozzleGlowShader;
  1573. nozzleGlow.shaderParms[ SHADERPARM_TIMESCALE ] = 1.0f;
  1574. nozzleGlow.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  1575. GetGlobalJointTransform( true, ventLightJointView, nozzleGlow.origin, nozzleGlow.axis );
  1576. nozzleGlowHandle = gameRenderWorld->AddLightDef(&nozzleGlow);
  1577. }
  1578. GetGlobalJointTransform( true, ventLightJointView, nozzleGlow.origin, nozzleGlow.axis );
  1579. nozzleGlow.shaderParms[ SHADERPARM_RED ] = nozzleGlowColor.x * s;
  1580. nozzleGlow.shaderParms[ SHADERPARM_GREEN ] = nozzleGlowColor.y * s;
  1581. nozzleGlow.shaderParms[ SHADERPARM_BLUE ] = nozzleGlowColor.z * s;
  1582. gameRenderWorld->UpdateLightDef(nozzleGlowHandle, &nozzleGlow);
  1583. }
  1584. /*
  1585. ================
  1586. idWeapon::BloodSplat
  1587. ================
  1588. */
  1589. bool idWeapon::BloodSplat( float size ) {
  1590. float s, c;
  1591. idMat3 localAxis, axistemp;
  1592. idVec3 localOrigin, normal;
  1593. if ( hasBloodSplat ) {
  1594. return true;
  1595. }
  1596. hasBloodSplat = true;
  1597. if ( modelDefHandle < 0 ) {
  1598. return false;
  1599. }
  1600. if ( !GetGlobalJointTransform( true, ejectJointView, localOrigin, localAxis ) ) {
  1601. return false;
  1602. }
  1603. localOrigin[0] += gameLocal.random.RandomFloat() * -10.0f;
  1604. localOrigin[1] += gameLocal.random.RandomFloat() * 1.0f;
  1605. localOrigin[2] += gameLocal.random.RandomFloat() * -2.0f;
  1606. normal = idVec3( gameLocal.random.CRandomFloat(), -gameLocal.random.RandomFloat(), -1 );
  1607. normal.Normalize();
  1608. idMath::SinCos16( gameLocal.random.RandomFloat() * idMath::TWO_PI, s, c );
  1609. localAxis[2] = -normal;
  1610. localAxis[2].NormalVectors( axistemp[0], axistemp[1] );
  1611. localAxis[0] = axistemp[ 0 ] * c + axistemp[ 1 ] * -s;
  1612. localAxis[1] = axistemp[ 0 ] * -s + axistemp[ 1 ] * -c;
  1613. localAxis[0] *= 1.0f / size;
  1614. localAxis[1] *= 1.0f / size;
  1615. idPlane localPlane[2];
  1616. localPlane[0] = localAxis[0];
  1617. localPlane[0][3] = -(localOrigin * localAxis[0]) + 0.5f;
  1618. localPlane[1] = localAxis[1];
  1619. localPlane[1][3] = -(localOrigin * localAxis[1]) + 0.5f;
  1620. const idMaterial *mtr = declManager->FindMaterial( "textures/decals/duffysplatgun" );
  1621. gameRenderWorld->ProjectOverlay( modelDefHandle, localPlane, mtr );
  1622. return true;
  1623. }
  1624. /***********************************************************************
  1625. Visual presentation
  1626. ***********************************************************************/
  1627. /*
  1628. ================
  1629. idWeapon::MuzzleRise
  1630. The machinegun and chaingun will incrementally back up as they are being fired
  1631. ================
  1632. */
  1633. void idWeapon::MuzzleRise( idVec3 &origin, idMat3 &axis ) {
  1634. int time;
  1635. float amount;
  1636. idAngles ang;
  1637. idVec3 offset;
  1638. time = kick_endtime - gameLocal.time;
  1639. if ( time <= 0 ) {
  1640. return;
  1641. }
  1642. if ( muzzle_kick_maxtime <= 0 ) {
  1643. return;
  1644. }
  1645. if ( time > muzzle_kick_maxtime ) {
  1646. time = muzzle_kick_maxtime;
  1647. }
  1648. amount = ( float )time / ( float )muzzle_kick_maxtime;
  1649. ang = muzzle_kick_angles * amount;
  1650. offset = muzzle_kick_offset * amount;
  1651. origin = origin - axis * offset;
  1652. axis = ang.ToMat3() * axis;
  1653. }
  1654. /*
  1655. ================
  1656. idWeapon::ConstructScriptObject
  1657. Called during idEntity::Spawn. Calls the constructor on the script object.
  1658. Can be overridden by subclasses when a thread doesn't need to be allocated.
  1659. ================
  1660. */
  1661. idThread *idWeapon::ConstructScriptObject( void ) {
  1662. const function_t *constructor;
  1663. thread->EndThread();
  1664. // call script object's constructor
  1665. constructor = scriptObject.GetConstructor();
  1666. if ( !constructor ) {
  1667. gameLocal.Error( "Missing constructor on '%s' for weapon", scriptObject.GetTypeName() );
  1668. }
  1669. // init the script object's data
  1670. scriptObject.ClearObject();
  1671. thread->CallFunction( this, constructor, true );
  1672. thread->Execute();
  1673. return thread;
  1674. }
  1675. /*
  1676. ================
  1677. idWeapon::DeconstructScriptObject
  1678. Called during idEntity::~idEntity. Calls the destructor on the script object.
  1679. Can be overridden by subclasses when a thread doesn't need to be allocated.
  1680. Not called during idGameLocal::MapShutdown.
  1681. ================
  1682. */
  1683. void idWeapon::DeconstructScriptObject( void ) {
  1684. const function_t *destructor;
  1685. if ( !thread ) {
  1686. return;
  1687. }
  1688. // don't bother calling the script object's destructor on map shutdown
  1689. if ( gameLocal.GameState() == GAMESTATE_SHUTDOWN ) {
  1690. return;
  1691. }
  1692. thread->EndThread();
  1693. // call script object's destructor
  1694. destructor = scriptObject.GetDestructor();
  1695. if ( destructor ) {
  1696. // start a thread that will run immediately and end
  1697. thread->CallFunction( this, destructor, true );
  1698. thread->Execute();
  1699. thread->EndThread();
  1700. }
  1701. // clear out the object's memory
  1702. scriptObject.ClearObject();
  1703. }
  1704. /*
  1705. ================
  1706. idWeapon::UpdateScript
  1707. ================
  1708. */
  1709. void idWeapon::UpdateScript( void ) {
  1710. int count;
  1711. if ( !isLinked ) {
  1712. return;
  1713. }
  1714. // only update the script on new frames
  1715. if ( !gameLocal.isNewFrame ) {
  1716. return;
  1717. }
  1718. if ( idealState.Length() ) {
  1719. SetState( idealState, animBlendFrames );
  1720. }
  1721. // update script state, which may call Event_LaunchProjectiles, among other things
  1722. count = 10;
  1723. while( ( thread->Execute() || idealState.Length() ) && count-- ) {
  1724. // happens for weapons with no clip (like grenades)
  1725. if ( idealState.Length() ) {
  1726. SetState( idealState, animBlendFrames );
  1727. }
  1728. }
  1729. WEAPON_RELOAD = false;
  1730. }
  1731. /*
  1732. ================
  1733. idWeapon::AlertMonsters
  1734. ================
  1735. */
  1736. void idWeapon::AlertMonsters( void ) {
  1737. trace_t tr;
  1738. idEntity *ent;
  1739. idVec3 end = muzzleFlash.origin + muzzleFlash.axis * muzzleFlash.target;
  1740. gameLocal.clip.TracePoint( tr, muzzleFlash.origin, end, CONTENTS_OPAQUE | MASK_SHOT_RENDERMODEL | CONTENTS_FLASHLIGHT_TRIGGER, owner );
  1741. if ( g_debugWeapon.GetBool() ) {
  1742. gameRenderWorld->DebugLine( colorYellow, muzzleFlash.origin, end, 0 );
  1743. gameRenderWorld->DebugArrow( colorGreen, muzzleFlash.origin, tr.endpos, 2, 0 );
  1744. }
  1745. if ( tr.fraction < 1.0f ) {
  1746. ent = gameLocal.GetTraceEntity( tr );
  1747. if ( ent->IsType( idAI::Type ) ) {
  1748. static_cast<idAI *>( ent )->TouchedByFlashlight( owner );
  1749. } else if ( ent->IsType( idTrigger::Type ) ) {
  1750. ent->Signal( SIG_TOUCH );
  1751. ent->ProcessEvent( &EV_Touch, owner, &tr );
  1752. }
  1753. }
  1754. // jitter the trace to try to catch cases where a trace down the center doesn't hit the monster
  1755. end += muzzleFlash.axis * muzzleFlash.right * idMath::Sin16( MS2SEC( gameLocal.time ) * 31.34f );
  1756. end += muzzleFlash.axis * muzzleFlash.up * idMath::Sin16( MS2SEC( gameLocal.time ) * 12.17f );
  1757. gameLocal.clip.TracePoint( tr, muzzleFlash.origin, end, CONTENTS_OPAQUE | MASK_SHOT_RENDERMODEL | CONTENTS_FLASHLIGHT_TRIGGER, owner );
  1758. if ( g_debugWeapon.GetBool() ) {
  1759. gameRenderWorld->DebugLine( colorYellow, muzzleFlash.origin, end, 0 );
  1760. gameRenderWorld->DebugArrow( colorGreen, muzzleFlash.origin, tr.endpos, 2, 0 );
  1761. }
  1762. if ( tr.fraction < 1.0f ) {
  1763. ent = gameLocal.GetTraceEntity( tr );
  1764. if ( ent->IsType( idAI::Type ) ) {
  1765. static_cast<idAI *>( ent )->TouchedByFlashlight( owner );
  1766. } else if ( ent->IsType( idTrigger::Type ) ) {
  1767. ent->Signal( SIG_TOUCH );
  1768. ent->ProcessEvent( &EV_Touch, owner, &tr );
  1769. }
  1770. }
  1771. }
  1772. /*
  1773. ================
  1774. idWeapon::PresentWeapon
  1775. ================
  1776. */
  1777. void idWeapon::PresentWeapon( bool showViewModel ) {
  1778. playerViewOrigin = owner->firstPersonViewOrigin;
  1779. playerViewAxis = owner->firstPersonViewAxis;
  1780. // calculate weapon position based on player movement bobbing
  1781. owner->CalculateViewWeaponPos( viewWeaponOrigin, viewWeaponAxis );
  1782. // hide offset is for dropping the gun when approaching a GUI or NPC
  1783. // This is simpler to manage than doing the weapon put-away animation
  1784. if ( gameLocal.time - hideStartTime < hideTime ) {
  1785. float frac = ( float )( gameLocal.time - hideStartTime ) / ( float )hideTime;
  1786. if ( hideStart < hideEnd ) {
  1787. frac = 1.0f - frac;
  1788. frac = 1.0f - frac * frac;
  1789. } else {
  1790. frac = frac * frac;
  1791. }
  1792. hideOffset = hideStart + ( hideEnd - hideStart ) * frac;
  1793. } else {
  1794. hideOffset = hideEnd;
  1795. if ( hide && disabled ) {
  1796. Hide();
  1797. }
  1798. }
  1799. viewWeaponOrigin += hideOffset * viewWeaponAxis[ 2 ];
  1800. // kick up based on repeat firing
  1801. MuzzleRise( viewWeaponOrigin, viewWeaponAxis );
  1802. // set the physics position and orientation
  1803. GetPhysics()->SetOrigin( viewWeaponOrigin );
  1804. GetPhysics()->SetAxis( viewWeaponAxis );
  1805. UpdateVisuals();
  1806. // update the weapon script
  1807. UpdateScript();
  1808. UpdateGUI();
  1809. // update animation
  1810. UpdateAnimation();
  1811. // only show the surface in player view
  1812. renderEntity.allowSurfaceInViewID = owner->entityNumber+1;
  1813. // crunch the depth range so it never pokes into walls this breaks the machine gun gui
  1814. renderEntity.weaponDepthHack = true;
  1815. // present the model
  1816. if ( showViewModel ) {
  1817. Present();
  1818. } else {
  1819. FreeModelDef();
  1820. }
  1821. if ( worldModel.GetEntity() && worldModel.GetEntity()->GetRenderEntity() ) {
  1822. // deal with the third-person visible world model
  1823. // don't show shadows of the world model in first person
  1824. if ( gameLocal.isMultiplayer || g_showPlayerShadow.GetBool() || pm_thirdPerson.GetBool() ) {
  1825. worldModel.GetEntity()->GetRenderEntity()->suppressShadowInViewID = 0;
  1826. } else {
  1827. worldModel.GetEntity()->GetRenderEntity()->suppressShadowInViewID = owner->entityNumber+1;
  1828. worldModel.GetEntity()->GetRenderEntity()->suppressShadowInLightID = LIGHTID_VIEW_MUZZLE_FLASH + owner->entityNumber;
  1829. }
  1830. }
  1831. if ( nozzleFx ) {
  1832. UpdateNozzleFx();
  1833. }
  1834. // muzzle smoke
  1835. if ( showViewModel && !disabled && weaponSmoke && ( weaponSmokeStartTime != 0 ) ) {
  1836. // use the barrel joint if available
  1837. #ifdef _D3XP
  1838. if(smokeJointView != INVALID_JOINT) {
  1839. GetGlobalJointTransform( true, smokeJointView, muzzleOrigin, muzzleAxis );
  1840. } else if (barrelJointView != INVALID_JOINT) {
  1841. GetGlobalJointTransform( true, barrelJointView, muzzleOrigin, muzzleAxis );
  1842. #else
  1843. if ( barrelJointView ) {
  1844. GetGlobalJointTransform( true, barrelJointView, muzzleOrigin, muzzleAxis );
  1845. #endif
  1846. } else {
  1847. // default to going straight out the view
  1848. muzzleOrigin = playerViewOrigin;
  1849. muzzleAxis = playerViewAxis;
  1850. }
  1851. // spit out a particle
  1852. if ( !gameLocal.smokeParticles->EmitSmoke( weaponSmoke, weaponSmokeStartTime, gameLocal.random.RandomFloat(), muzzleOrigin, muzzleAxis, timeGroup /*_D3XP*/ ) ) {
  1853. weaponSmokeStartTime = ( continuousSmoke ) ? gameLocal.time : 0;
  1854. }
  1855. }
  1856. if ( showViewModel && strikeSmoke && strikeSmokeStartTime != 0 ) {
  1857. // spit out a particle
  1858. if ( !gameLocal.smokeParticles->EmitSmoke( strikeSmoke, strikeSmokeStartTime, gameLocal.random.RandomFloat(), strikePos, strikeAxis, timeGroup /*_D3XP*/ ) ) {
  1859. strikeSmokeStartTime = 0;
  1860. }
  1861. }
  1862. #ifdef _D3XP
  1863. if ( showViewModel && !hide ) {
  1864. for( int i = 0; i < weaponParticles.Num(); i++ ) {
  1865. WeaponParticle_t* part = weaponParticles.GetIndex(i);
  1866. if(part->active) {
  1867. if(part->smoke) {
  1868. if(part->joint != INVALID_JOINT) {
  1869. GetGlobalJointTransform( true, part->joint, muzzleOrigin, muzzleAxis );
  1870. } else {
  1871. // default to going straight out the view
  1872. muzzleOrigin = playerViewOrigin;
  1873. muzzleAxis = playerViewAxis;
  1874. }
  1875. if ( !gameLocal.smokeParticles->EmitSmoke( part->particle, part->startTime, gameLocal.random.RandomFloat(), muzzleOrigin, muzzleAxis, timeGroup /*_D3XP*/ ) ) {
  1876. part->active = false; // all done
  1877. part->startTime = 0;
  1878. }
  1879. } else {
  1880. //Manually update the position of the emitter so it follows the weapon
  1881. renderEntity_t* rendEnt = part->emitter->GetRenderEntity();
  1882. GetGlobalJointTransform( true, part->joint, rendEnt->origin, rendEnt->axis );
  1883. if ( part->emitter->GetModelDefHandle() != -1 ) {
  1884. gameRenderWorld->UpdateEntityDef( part->emitter->GetModelDefHandle(), rendEnt );
  1885. }
  1886. }
  1887. }
  1888. }
  1889. for(int i = 0; i < weaponLights.Num(); i++) {
  1890. WeaponLight_t* light = weaponLights.GetIndex(i);
  1891. if(light->active) {
  1892. GetGlobalJointTransform( true, light->joint, light->light.origin, light->light.axis );
  1893. if ( ( light->lightHandle != -1 ) ) {
  1894. gameRenderWorld->UpdateLightDef( light->lightHandle, &light->light );
  1895. } else {
  1896. light->lightHandle = gameRenderWorld->AddLightDef( &light->light );
  1897. }
  1898. }
  1899. }
  1900. }
  1901. // Update the grabber effects
  1902. if ( grabberState != -1 ) {
  1903. grabberState = grabber.Update( owner, hide );
  1904. }
  1905. #endif
  1906. // remove the muzzle flash light when it's done
  1907. if ( ( !lightOn && ( gameLocal.time >= muzzleFlashEnd ) ) || IsHidden() ) {
  1908. if ( muzzleFlashHandle != -1 ) {
  1909. gameRenderWorld->FreeLightDef( muzzleFlashHandle );
  1910. muzzleFlashHandle = -1;
  1911. }
  1912. if ( worldMuzzleFlashHandle != -1 ) {
  1913. gameRenderWorld->FreeLightDef( worldMuzzleFlashHandle );
  1914. worldMuzzleFlashHandle = -1;
  1915. }
  1916. }
  1917. // update the muzzle flash light, so it moves with the gun
  1918. if ( muzzleFlashHandle != -1 ) {
  1919. UpdateFlashPosition();
  1920. gameRenderWorld->UpdateLightDef( muzzleFlashHandle, &muzzleFlash );
  1921. gameRenderWorld->UpdateLightDef( worldMuzzleFlashHandle, &worldMuzzleFlash );
  1922. // wake up monsters with the flashlight
  1923. if ( !gameLocal.isMultiplayer && lightOn && !owner->fl.notarget ) {
  1924. AlertMonsters();
  1925. }
  1926. }
  1927. // update the gui light
  1928. if ( guiLight.lightRadius[0] && guiLightJointView != INVALID_JOINT ) {
  1929. GetGlobalJointTransform( true, guiLightJointView, guiLight.origin, guiLight.axis );
  1930. if ( ( guiLightHandle != -1 ) ) {
  1931. gameRenderWorld->UpdateLightDef( guiLightHandle, &guiLight );
  1932. } else {
  1933. guiLightHandle = gameRenderWorld->AddLightDef( &guiLight );
  1934. }
  1935. }
  1936. if ( status != WP_READY && sndHum ) {
  1937. StopSound( SND_CHANNEL_BODY, false );
  1938. }
  1939. UpdateSound();
  1940. }
  1941. /*
  1942. ================
  1943. idWeapon::EnterCinematic
  1944. ================
  1945. */
  1946. void idWeapon::EnterCinematic( void ) {
  1947. StopSound( SND_CHANNEL_ANY, false );
  1948. if ( isLinked ) {
  1949. SetState( "EnterCinematic", 0 );
  1950. thread->Execute();
  1951. WEAPON_ATTACK = false;
  1952. WEAPON_RELOAD = false;
  1953. WEAPON_NETRELOAD = false;
  1954. WEAPON_NETENDRELOAD = false;
  1955. WEAPON_NETFIRING = false;
  1956. WEAPON_RAISEWEAPON = false;
  1957. WEAPON_LOWERWEAPON = false;
  1958. #ifdef _D3XP
  1959. grabber.Update( this->GetOwner(), true );
  1960. #endif
  1961. }
  1962. disabled = true;
  1963. LowerWeapon();
  1964. }
  1965. /*
  1966. ================
  1967. idWeapon::ExitCinematic
  1968. ================
  1969. */
  1970. void idWeapon::ExitCinematic( void ) {
  1971. disabled = false;
  1972. if ( isLinked ) {
  1973. SetState( "ExitCinematic", 0 );
  1974. thread->Execute();
  1975. }
  1976. RaiseWeapon();
  1977. }
  1978. /*
  1979. ================
  1980. idWeapon::NetCatchup
  1981. ================
  1982. */
  1983. void idWeapon::NetCatchup( void ) {
  1984. if ( isLinked ) {
  1985. SetState( "NetCatchup", 0 );
  1986. thread->Execute();
  1987. }
  1988. }
  1989. /*
  1990. ================
  1991. idWeapon::GetZoomFov
  1992. ================
  1993. */
  1994. int idWeapon::GetZoomFov( void ) {
  1995. return zoomFov;
  1996. }
  1997. /*
  1998. ================
  1999. idWeapon::GetWeaponAngleOffsets
  2000. ================
  2001. */
  2002. void idWeapon::GetWeaponAngleOffsets( int *average, float *scale, float *max ) {
  2003. *average = weaponAngleOffsetAverages;
  2004. *scale = weaponAngleOffsetScale;
  2005. *max = weaponAngleOffsetMax;
  2006. }
  2007. /*
  2008. ================
  2009. idWeapon::GetWeaponTimeOffsets
  2010. ================
  2011. */
  2012. void idWeapon::GetWeaponTimeOffsets( float *time, float *scale ) {
  2013. *time = weaponOffsetTime;
  2014. *scale = weaponOffsetScale;
  2015. }
  2016. /***********************************************************************
  2017. Ammo
  2018. ***********************************************************************/
  2019. /*
  2020. ================
  2021. idWeapon::GetAmmoNumForName
  2022. ================
  2023. */
  2024. ammo_t idWeapon::GetAmmoNumForName( const char *ammoname ) {
  2025. int num;
  2026. const idDict *ammoDict;
  2027. assert( ammoname );
  2028. ammoDict = gameLocal.FindEntityDefDict( "ammo_types", false );
  2029. if ( !ammoDict ) {
  2030. gameLocal.Error( "Could not find entity definition for 'ammo_types'\n" );
  2031. }
  2032. if ( !ammoname[ 0 ] ) {
  2033. return 0;
  2034. }
  2035. if ( !ammoDict->GetInt( ammoname, "-1", num ) ) {
  2036. #ifdef _D3XP
  2037. //Lets look in a game specific ammo type definition for the weapon
  2038. idStr gamedir;
  2039. int i;
  2040. for ( i = 0; i < 2; i++ ) {
  2041. if ( i == 0 ) {
  2042. gamedir = cvarSystem->GetCVarString( "fs_game_base" );
  2043. } else if ( i == 1 ) {
  2044. gamedir = cvarSystem->GetCVarString( "fs_game" );
  2045. }
  2046. if ( gamedir.Length() > 0 ) {
  2047. ammoDict = gameLocal.FindEntityDefDict( va("ammo_types_%s", gamedir.c_str()), false );
  2048. if ( ammoDict ) {
  2049. if ( ammoDict->GetInt( ammoname, "-1", num ) ) {
  2050. break;
  2051. }
  2052. }
  2053. }
  2054. }
  2055. if ( i == 2 ) {
  2056. gameLocal.Error( "Unknown ammo type '%s'", ammoname );
  2057. }
  2058. #endif
  2059. }
  2060. if ( ( num < 0 ) || ( num >= AMMO_NUMTYPES ) ) {
  2061. gameLocal.Error( "Ammo type '%s' value out of range. Maximum ammo types is %d.\n", ammoname, AMMO_NUMTYPES );
  2062. }
  2063. return ( ammo_t )num;
  2064. }
  2065. /*
  2066. ================
  2067. idWeapon::GetAmmoNameForNum
  2068. ================
  2069. */
  2070. const char *idWeapon::GetAmmoNameForNum( ammo_t ammonum ) {
  2071. int i;
  2072. int num;
  2073. const idDict *ammoDict;
  2074. const idKeyValue *kv;
  2075. char text[ 32 ];
  2076. ammoDict = gameLocal.FindEntityDefDict( "ammo_types", false );
  2077. if ( !ammoDict ) {
  2078. gameLocal.Error( "Could not find entity definition for 'ammo_types'\n" );
  2079. }
  2080. sprintf( text, "%d", ammonum );
  2081. num = ammoDict->GetNumKeyVals();
  2082. for( i = 0; i < num; i++ ) {
  2083. kv = ammoDict->GetKeyVal( i );
  2084. if ( kv->GetValue() == text ) {
  2085. return kv->GetKey();
  2086. }
  2087. }
  2088. #ifdef _D3XP
  2089. // Look in the game specific ammo types
  2090. idStr gamedir;
  2091. for ( i = 0; i < 2; i++ ) {
  2092. if ( i == 0 ) {
  2093. gamedir = cvarSystem->GetCVarString( "fs_game_base" );
  2094. } else if ( i == 1 ) {
  2095. gamedir = cvarSystem->GetCVarString( "fs_game" );
  2096. }
  2097. if ( gamedir.Length() > 0 ) {
  2098. ammoDict = gameLocal.FindEntityDefDict( va("ammo_types_%s", gamedir.c_str()), false );
  2099. if ( ammoDict ) {
  2100. num = ammoDict->GetNumKeyVals();
  2101. for( i = 0; i < num; i++ ) {
  2102. kv = ammoDict->GetKeyVal( i );
  2103. if ( kv->GetValue() == text ) {
  2104. return kv->GetKey();
  2105. }
  2106. }
  2107. }
  2108. }
  2109. }
  2110. #endif
  2111. return NULL;
  2112. }
  2113. /*
  2114. ================
  2115. idWeapon::GetAmmoPickupNameForNum
  2116. ================
  2117. */
  2118. const char *idWeapon::GetAmmoPickupNameForNum( ammo_t ammonum ) {
  2119. int i;
  2120. int num;
  2121. const idDict *ammoDict;
  2122. const idKeyValue *kv;
  2123. ammoDict = gameLocal.FindEntityDefDict( "ammo_names", false );
  2124. if ( !ammoDict ) {
  2125. gameLocal.Error( "Could not find entity definition for 'ammo_names'\n" );
  2126. }
  2127. const char *name = GetAmmoNameForNum( ammonum );
  2128. if ( name && *name ) {
  2129. num = ammoDict->GetNumKeyVals();
  2130. for( i = 0; i < num; i++ ) {
  2131. kv = ammoDict->GetKeyVal( i );
  2132. if ( idStr::Icmp( kv->GetKey(), name) == 0 ) {
  2133. return kv->GetValue();
  2134. }
  2135. }
  2136. }
  2137. return "";
  2138. }
  2139. /*
  2140. ================
  2141. idWeapon::AmmoAvailable
  2142. ================
  2143. */
  2144. int idWeapon::AmmoAvailable( void ) const {
  2145. if ( owner ) {
  2146. return owner->inventory.HasAmmo( ammoType, ammoRequired );
  2147. } else {
  2148. return 0;
  2149. }
  2150. }
  2151. /*
  2152. ================
  2153. idWeapon::AmmoInClip
  2154. ================
  2155. */
  2156. int idWeapon::AmmoInClip( void ) const {
  2157. return ammoClip;
  2158. }
  2159. /*
  2160. ================
  2161. idWeapon::ResetAmmoClip
  2162. ================
  2163. */
  2164. void idWeapon::ResetAmmoClip( void ) {
  2165. ammoClip = -1;
  2166. }
  2167. /*
  2168. ================
  2169. idWeapon::GetAmmoType
  2170. ================
  2171. */
  2172. ammo_t idWeapon::GetAmmoType( void ) const {
  2173. return ammoType;
  2174. }
  2175. /*
  2176. ================
  2177. idWeapon::ClipSize
  2178. ================
  2179. */
  2180. int idWeapon::ClipSize( void ) const {
  2181. return clipSize;
  2182. }
  2183. /*
  2184. ================
  2185. idWeapon::LowAmmo
  2186. ================
  2187. */
  2188. int idWeapon::LowAmmo() const {
  2189. return lowAmmo;
  2190. }
  2191. /*
  2192. ================
  2193. idWeapon::AmmoRequired
  2194. ================
  2195. */
  2196. int idWeapon::AmmoRequired( void ) const {
  2197. return ammoRequired;
  2198. }
  2199. #ifdef _D3XP
  2200. /*
  2201. ================
  2202. idWeapon::GetGrabberState
  2203. Returns the current grabberState
  2204. ================
  2205. */
  2206. int idWeapon::GetGrabberState() const {
  2207. return grabberState;
  2208. }
  2209. /*
  2210. ================
  2211. idWeapon::AmmoCount
  2212. Returns the total number of rounds regardless of the required ammo
  2213. ================
  2214. */
  2215. int idWeapon::AmmoCount() const {
  2216. if ( owner ) {
  2217. return owner->inventory.HasAmmo( ammoType, 1 );
  2218. } else {
  2219. return 0;
  2220. }
  2221. }
  2222. #endif
  2223. /*
  2224. ================
  2225. idWeapon::WriteToSnapshot
  2226. ================
  2227. */
  2228. void idWeapon::WriteToSnapshot( idBitMsgDelta &msg ) const {
  2229. msg.WriteBits( ammoClip, ASYNC_PLAYER_INV_CLIP_BITS );
  2230. msg.WriteBits( worldModel.GetSpawnId(), 32 );
  2231. msg.WriteBits( lightOn, 1 );
  2232. msg.WriteBits( isFiring ? 1 : 0, 1 );
  2233. }
  2234. /*
  2235. ================
  2236. idWeapon::ReadFromSnapshot
  2237. ================
  2238. */
  2239. void idWeapon::ReadFromSnapshot( const idBitMsgDelta &msg ) {
  2240. ammoClip = msg.ReadBits( ASYNC_PLAYER_INV_CLIP_BITS );
  2241. worldModel.SetSpawnId( msg.ReadBits( 32 ) );
  2242. bool snapLight = msg.ReadBits( 1 ) != 0;
  2243. isFiring = msg.ReadBits( 1 ) != 0;
  2244. // WEAPON_NETFIRING is only turned on for other clients we're predicting. not for local client
  2245. if ( owner && gameLocal.localClientNum != owner->entityNumber && WEAPON_NETFIRING.IsLinked() ) {
  2246. // immediately go to the firing state so we don't skip fire animations
  2247. if ( !WEAPON_NETFIRING && isFiring ) {
  2248. idealState = "Fire";
  2249. }
  2250. // immediately switch back to idle
  2251. if ( WEAPON_NETFIRING && !isFiring ) {
  2252. idealState = "Idle";
  2253. }
  2254. WEAPON_NETFIRING = isFiring;
  2255. }
  2256. if ( snapLight != lightOn ) {
  2257. Reload();
  2258. }
  2259. }
  2260. /*
  2261. ================
  2262. idWeapon::ClientReceiveEvent
  2263. ================
  2264. */
  2265. bool idWeapon::ClientReceiveEvent( int event, int time, const idBitMsg &msg ) {
  2266. switch( event ) {
  2267. case EVENT_RELOAD: {
  2268. if ( gameLocal.time - time < 1000 ) {
  2269. if ( WEAPON_NETRELOAD.IsLinked() ) {
  2270. WEAPON_NETRELOAD = true;
  2271. WEAPON_NETENDRELOAD = false;
  2272. }
  2273. }
  2274. return true;
  2275. }
  2276. case EVENT_ENDRELOAD: {
  2277. if ( WEAPON_NETENDRELOAD.IsLinked() ) {
  2278. WEAPON_NETENDRELOAD = true;
  2279. }
  2280. return true;
  2281. }
  2282. case EVENT_CHANGESKIN: {
  2283. int index = gameLocal.ClientRemapDecl( DECL_SKIN, msg.ReadLong() );
  2284. renderEntity.customSkin = ( index != -1 ) ? static_cast<const idDeclSkin *>( declManager->DeclByIndex( DECL_SKIN, index ) ) : NULL;
  2285. UpdateVisuals();
  2286. if ( worldModel.GetEntity() ) {
  2287. worldModel.GetEntity()->SetSkin( renderEntity.customSkin );
  2288. }
  2289. return true;
  2290. }
  2291. default: {
  2292. return idEntity::ClientReceiveEvent( event, time, msg );
  2293. }
  2294. }
  2295. return false;
  2296. }
  2297. /***********************************************************************
  2298. Script events
  2299. ***********************************************************************/
  2300. /*
  2301. ===============
  2302. idWeapon::Event_Clear
  2303. ===============
  2304. */
  2305. void idWeapon::Event_Clear( void ) {
  2306. Clear();
  2307. }
  2308. /*
  2309. ===============
  2310. idWeapon::Event_GetOwner
  2311. ===============
  2312. */
  2313. void idWeapon::Event_GetOwner( void ) {
  2314. idThread::ReturnEntity( owner );
  2315. }
  2316. /*
  2317. ===============
  2318. idWeapon::Event_WeaponState
  2319. ===============
  2320. */
  2321. void idWeapon::Event_WeaponState( const char *statename, int blendFrames ) {
  2322. const function_t *func;
  2323. func = scriptObject.GetFunction( statename );
  2324. if ( !func ) {
  2325. assert( 0 );
  2326. gameLocal.Error( "Can't find function '%s' in object '%s'", statename, scriptObject.GetTypeName() );
  2327. }
  2328. idealState = statename;
  2329. if ( !idealState.Icmp( "Fire" ) ) {
  2330. isFiring = true;
  2331. } else {
  2332. isFiring = false;
  2333. }
  2334. animBlendFrames = blendFrames;
  2335. thread->DoneProcessing();
  2336. }
  2337. /*
  2338. ===============
  2339. idWeapon::Event_WeaponReady
  2340. ===============
  2341. */
  2342. void idWeapon::Event_WeaponReady( void ) {
  2343. status = WP_READY;
  2344. if ( isLinked ) {
  2345. WEAPON_RAISEWEAPON = false;
  2346. }
  2347. if ( sndHum ) {
  2348. StartSoundShader( sndHum, SND_CHANNEL_BODY, 0, false, NULL );
  2349. }
  2350. }
  2351. /*
  2352. ===============
  2353. idWeapon::Event_WeaponOutOfAmmo
  2354. ===============
  2355. */
  2356. void idWeapon::Event_WeaponOutOfAmmo( void ) {
  2357. status = WP_OUTOFAMMO;
  2358. if ( isLinked ) {
  2359. WEAPON_RAISEWEAPON = false;
  2360. }
  2361. }
  2362. /*
  2363. ===============
  2364. idWeapon::Event_WeaponReloading
  2365. ===============
  2366. */
  2367. void idWeapon::Event_WeaponReloading( void ) {
  2368. status = WP_RELOAD;
  2369. }
  2370. /*
  2371. ===============
  2372. idWeapon::Event_WeaponHolstered
  2373. ===============
  2374. */
  2375. void idWeapon::Event_WeaponHolstered( void ) {
  2376. status = WP_HOLSTERED;
  2377. if ( isLinked ) {
  2378. WEAPON_LOWERWEAPON = false;
  2379. }
  2380. }
  2381. /*
  2382. ===============
  2383. idWeapon::Event_WeaponRising
  2384. ===============
  2385. */
  2386. void idWeapon::Event_WeaponRising( void ) {
  2387. status = WP_RISING;
  2388. if ( isLinked ) {
  2389. WEAPON_LOWERWEAPON = false;
  2390. }
  2391. owner->WeaponRisingCallback();
  2392. }
  2393. /*
  2394. ===============
  2395. idWeapon::Event_WeaponLowering
  2396. ===============
  2397. */
  2398. void idWeapon::Event_WeaponLowering( void ) {
  2399. status = WP_LOWERING;
  2400. if ( isLinked ) {
  2401. WEAPON_RAISEWEAPON = false;
  2402. }
  2403. owner->WeaponLoweringCallback();
  2404. }
  2405. /*
  2406. ===============
  2407. idWeapon::Event_UseAmmo
  2408. ===============
  2409. */
  2410. void idWeapon::Event_UseAmmo( int amount ) {
  2411. if ( gameLocal.isClient ) {
  2412. return;
  2413. }
  2414. owner->inventory.UseAmmo( ammoType, ( powerAmmo ) ? amount : ( amount * ammoRequired ) );
  2415. if ( clipSize && ammoRequired ) {
  2416. ammoClip -= powerAmmo ? amount : ( amount * ammoRequired );
  2417. if ( ammoClip < 0 ) {
  2418. ammoClip = 0;
  2419. }
  2420. }
  2421. }
  2422. /*
  2423. ===============
  2424. idWeapon::Event_AddToClip
  2425. ===============
  2426. */
  2427. void idWeapon::Event_AddToClip( int amount ) {
  2428. int ammoAvail;
  2429. if ( gameLocal.isClient ) {
  2430. return;
  2431. }
  2432. #ifdef _D3XP
  2433. int oldAmmo = ammoClip;
  2434. ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired ) + AmmoInClip();
  2435. #endif
  2436. ammoClip += amount;
  2437. if ( ammoClip > clipSize ) {
  2438. ammoClip = clipSize;
  2439. }
  2440. #ifdef _D3XP
  2441. #else
  2442. ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  2443. #endif
  2444. if ( ammoClip > ammoAvail ) {
  2445. ammoClip = ammoAvail;
  2446. }
  2447. #ifdef _D3XP
  2448. // for shared ammo we need to use the ammo when it is moved into the clip
  2449. int usedAmmo = ammoClip - oldAmmo;
  2450. owner->inventory.UseAmmo(ammoType, usedAmmo);
  2451. #endif
  2452. }
  2453. /*
  2454. ===============
  2455. idWeapon::Event_AmmoInClip
  2456. ===============
  2457. */
  2458. void idWeapon::Event_AmmoInClip( void ) {
  2459. int ammo = AmmoInClip();
  2460. idThread::ReturnFloat( ammo );
  2461. }
  2462. /*
  2463. ===============
  2464. idWeapon::Event_AmmoAvailable
  2465. ===============
  2466. */
  2467. void idWeapon::Event_AmmoAvailable( void ) {
  2468. #ifdef _D3XP
  2469. int ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  2470. ammoAvail += AmmoInClip();
  2471. #else
  2472. int ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  2473. #endif
  2474. idThread::ReturnFloat( ammoAvail );
  2475. }
  2476. /*
  2477. ===============
  2478. idWeapon::Event_TotalAmmoCount
  2479. ===============
  2480. */
  2481. void idWeapon::Event_TotalAmmoCount( void ) {
  2482. int ammoAvail = owner->inventory.HasAmmo( ammoType, 1 );
  2483. idThread::ReturnFloat( ammoAvail );
  2484. }
  2485. /*
  2486. ===============
  2487. idWeapon::Event_ClipSize
  2488. ===============
  2489. */
  2490. void idWeapon::Event_ClipSize( void ) {
  2491. idThread::ReturnFloat( clipSize );
  2492. }
  2493. /*
  2494. ===============
  2495. idWeapon::Event_AutoReload
  2496. ===============
  2497. */
  2498. void idWeapon::Event_AutoReload( void ) {
  2499. assert( owner );
  2500. if ( gameLocal.isClient ) {
  2501. idThread::ReturnFloat( 0.0f );
  2502. return;
  2503. }
  2504. idThread::ReturnFloat( gameLocal.userInfo[ owner->entityNumber ].GetBool( "ui_autoReload" ) );
  2505. }
  2506. /*
  2507. ===============
  2508. idWeapon::Event_NetReload
  2509. ===============
  2510. */
  2511. void idWeapon::Event_NetReload( void ) {
  2512. assert( owner );
  2513. if ( gameLocal.isServer ) {
  2514. ServerSendEvent( EVENT_RELOAD, NULL, false, -1 );
  2515. }
  2516. }
  2517. /*
  2518. ===============
  2519. idWeapon::Event_NetEndReload
  2520. ===============
  2521. */
  2522. void idWeapon::Event_NetEndReload( void ) {
  2523. assert( owner );
  2524. if ( gameLocal.isServer ) {
  2525. ServerSendEvent( EVENT_ENDRELOAD, NULL, false, -1 );
  2526. }
  2527. }
  2528. /*
  2529. ===============
  2530. idWeapon::Event_PlayAnim
  2531. ===============
  2532. */
  2533. void idWeapon::Event_PlayAnim( int channel, const char *animname ) {
  2534. int anim;
  2535. anim = animator.GetAnim( animname );
  2536. if ( !anim ) {
  2537. gameLocal.Warning( "missing '%s' animation on '%s' (%s)", animname, name.c_str(), GetEntityDefName() );
  2538. animator.Clear( channel, gameLocal.time, FRAME2MS( animBlendFrames ) );
  2539. animDoneTime = 0;
  2540. } else {
  2541. if ( !( owner && owner->GetInfluenceLevel() ) ) {
  2542. Show();
  2543. }
  2544. animator.PlayAnim( channel, anim, gameLocal.time, FRAME2MS( animBlendFrames ) );
  2545. animDoneTime = animator.CurrentAnim( channel )->GetEndTime();
  2546. if ( worldModel.GetEntity() ) {
  2547. anim = worldModel.GetEntity()->GetAnimator()->GetAnim( animname );
  2548. if ( anim ) {
  2549. worldModel.GetEntity()->GetAnimator()->PlayAnim( channel, anim, gameLocal.time, FRAME2MS( animBlendFrames ) );
  2550. }
  2551. }
  2552. }
  2553. animBlendFrames = 0;
  2554. idThread::ReturnInt( 0 );
  2555. }
  2556. /*
  2557. ===============
  2558. idWeapon::Event_PlayCycle
  2559. ===============
  2560. */
  2561. void idWeapon::Event_PlayCycle( int channel, const char *animname ) {
  2562. int anim;
  2563. anim = animator.GetAnim( animname );
  2564. if ( !anim ) {
  2565. gameLocal.Warning( "missing '%s' animation on '%s' (%s)", animname, name.c_str(), GetEntityDefName() );
  2566. animator.Clear( channel, gameLocal.time, FRAME2MS( animBlendFrames ) );
  2567. animDoneTime = 0;
  2568. } else {
  2569. if ( !( owner && owner->GetInfluenceLevel() ) ) {
  2570. Show();
  2571. }
  2572. animator.CycleAnim( channel, anim, gameLocal.time, FRAME2MS( animBlendFrames ) );
  2573. animDoneTime = animator.CurrentAnim( channel )->GetEndTime();
  2574. if ( worldModel.GetEntity() ) {
  2575. anim = worldModel.GetEntity()->GetAnimator()->GetAnim( animname );
  2576. worldModel.GetEntity()->GetAnimator()->CycleAnim( channel, anim, gameLocal.time, FRAME2MS( animBlendFrames ) );
  2577. }
  2578. }
  2579. animBlendFrames = 0;
  2580. idThread::ReturnInt( 0 );
  2581. }
  2582. /*
  2583. ===============
  2584. idWeapon::Event_AnimDone
  2585. ===============
  2586. */
  2587. void idWeapon::Event_AnimDone( int channel, int blendFrames ) {
  2588. if ( animDoneTime - FRAME2MS( blendFrames ) <= gameLocal.time ) {
  2589. idThread::ReturnInt( true );
  2590. } else {
  2591. idThread::ReturnInt( false );
  2592. }
  2593. }
  2594. /*
  2595. ===============
  2596. idWeapon::Event_SetBlendFrames
  2597. ===============
  2598. */
  2599. void idWeapon::Event_SetBlendFrames( int channel, int blendFrames ) {
  2600. animBlendFrames = blendFrames;
  2601. }
  2602. /*
  2603. ===============
  2604. idWeapon::Event_GetBlendFrames
  2605. ===============
  2606. */
  2607. void idWeapon::Event_GetBlendFrames( int channel ) {
  2608. idThread::ReturnInt( animBlendFrames );
  2609. }
  2610. /*
  2611. ================
  2612. idWeapon::Event_Next
  2613. ================
  2614. */
  2615. void idWeapon::Event_Next( void ) {
  2616. // change to another weapon if possible
  2617. owner->NextBestWeapon();
  2618. }
  2619. /*
  2620. ================
  2621. idWeapon::Event_SetSkin
  2622. ================
  2623. */
  2624. void idWeapon::Event_SetSkin( const char *skinname ) {
  2625. const idDeclSkin *skinDecl;
  2626. if ( !skinname || !skinname[ 0 ] ) {
  2627. skinDecl = NULL;
  2628. } else {
  2629. skinDecl = declManager->FindSkin( skinname );
  2630. }
  2631. renderEntity.customSkin = skinDecl;
  2632. UpdateVisuals();
  2633. if ( worldModel.GetEntity() ) {
  2634. worldModel.GetEntity()->SetSkin( skinDecl );
  2635. }
  2636. if ( gameLocal.isServer ) {
  2637. idBitMsg msg;
  2638. byte msgBuf[MAX_EVENT_PARAM_SIZE];
  2639. msg.Init( msgBuf, sizeof( msgBuf ) );
  2640. msg.WriteLong( ( skinDecl != NULL ) ? gameLocal.ServerRemapDecl( -1, DECL_SKIN, skinDecl->Index() ) : -1 );
  2641. ServerSendEvent( EVENT_CHANGESKIN, &msg, false, -1 );
  2642. }
  2643. }
  2644. /*
  2645. ================
  2646. idWeapon::Event_Flashlight
  2647. ================
  2648. */
  2649. void idWeapon::Event_Flashlight( int enable ) {
  2650. if ( enable ) {
  2651. lightOn = true;
  2652. MuzzleFlashLight();
  2653. } else {
  2654. lightOn = false;
  2655. muzzleFlashEnd = 0;
  2656. }
  2657. }
  2658. /*
  2659. ================
  2660. idWeapon::Event_GetLightParm
  2661. ================
  2662. */
  2663. void idWeapon::Event_GetLightParm( int parmnum ) {
  2664. if ( ( parmnum < 0 ) || ( parmnum >= MAX_ENTITY_SHADER_PARMS ) ) {
  2665. gameLocal.Error( "shader parm index (%d) out of range", parmnum );
  2666. }
  2667. idThread::ReturnFloat( muzzleFlash.shaderParms[ parmnum ] );
  2668. }
  2669. /*
  2670. ================
  2671. idWeapon::Event_SetLightParm
  2672. ================
  2673. */
  2674. void idWeapon::Event_SetLightParm( int parmnum, float value ) {
  2675. if ( ( parmnum < 0 ) || ( parmnum >= MAX_ENTITY_SHADER_PARMS ) ) {
  2676. gameLocal.Error( "shader parm index (%d) out of range", parmnum );
  2677. }
  2678. muzzleFlash.shaderParms[ parmnum ] = value;
  2679. worldMuzzleFlash.shaderParms[ parmnum ] = value;
  2680. UpdateVisuals();
  2681. }
  2682. /*
  2683. ================
  2684. idWeapon::Event_SetLightParms
  2685. ================
  2686. */
  2687. void idWeapon::Event_SetLightParms( float parm0, float parm1, float parm2, float parm3 ) {
  2688. muzzleFlash.shaderParms[ SHADERPARM_RED ] = parm0;
  2689. muzzleFlash.shaderParms[ SHADERPARM_GREEN ] = parm1;
  2690. muzzleFlash.shaderParms[ SHADERPARM_BLUE ] = parm2;
  2691. muzzleFlash.shaderParms[ SHADERPARM_ALPHA ] = parm3;
  2692. worldMuzzleFlash.shaderParms[ SHADERPARM_RED ] = parm0;
  2693. worldMuzzleFlash.shaderParms[ SHADERPARM_GREEN ] = parm1;
  2694. worldMuzzleFlash.shaderParms[ SHADERPARM_BLUE ] = parm2;
  2695. worldMuzzleFlash.shaderParms[ SHADERPARM_ALPHA ] = parm3;
  2696. UpdateVisuals();
  2697. }
  2698. #ifdef _D3XP
  2699. /*
  2700. ================
  2701. idWeapon::Event_Grabber
  2702. ================
  2703. */
  2704. void idWeapon::Event_Grabber( int enable ) {
  2705. if ( enable ) {
  2706. grabberState = 0;
  2707. } else {
  2708. grabberState = -1;
  2709. }
  2710. }
  2711. /*
  2712. ================
  2713. idWeapon::Event_GrabberHasTarget
  2714. ================
  2715. */
  2716. void idWeapon::Event_GrabberHasTarget() {
  2717. idThread::ReturnInt( grabberState );
  2718. }
  2719. /*
  2720. ================
  2721. idWeapon::Event_GrabberSetGrabDistance
  2722. ================
  2723. */
  2724. void idWeapon::Event_GrabberSetGrabDistance( float dist ) {
  2725. grabber.SetDragDistance( dist );
  2726. }
  2727. #endif
  2728. /*
  2729. ================
  2730. idWeapon::Event_CreateProjectile
  2731. ================
  2732. */
  2733. void idWeapon::Event_CreateProjectile( void ) {
  2734. if ( !gameLocal.isClient ) {
  2735. projectileEnt = NULL;
  2736. gameLocal.SpawnEntityDef( projectileDict, &projectileEnt, false );
  2737. if ( projectileEnt ) {
  2738. projectileEnt->SetOrigin( GetPhysics()->GetOrigin() );
  2739. projectileEnt->Bind( owner, false );
  2740. projectileEnt->Hide();
  2741. }
  2742. idThread::ReturnEntity( projectileEnt );
  2743. } else {
  2744. idThread::ReturnEntity( NULL );
  2745. }
  2746. }
  2747. /*
  2748. ================
  2749. idWeapon::Event_LaunchProjectiles
  2750. ================
  2751. */
  2752. void idWeapon::Event_LaunchProjectiles( int num_projectiles, float spread, float fuseOffset, float launchPower, float dmgPower ) {
  2753. idProjectile *proj;
  2754. idEntity *ent;
  2755. int i;
  2756. idVec3 dir;
  2757. float ang;
  2758. float spin;
  2759. float distance;
  2760. trace_t tr;
  2761. idVec3 start;
  2762. idVec3 muzzle_pos;
  2763. idBounds ownerBounds, projBounds;
  2764. if ( IsHidden() ) {
  2765. return;
  2766. }
  2767. if ( !projectileDict.GetNumKeyVals() ) {
  2768. const char *classname = weaponDef->dict.GetString( "classname" );
  2769. gameLocal.Warning( "No projectile defined on '%s'", classname );
  2770. return;
  2771. }
  2772. // avoid all ammo considerations on an MP client
  2773. if ( !gameLocal.isClient ) {
  2774. #ifdef _D3XP
  2775. int ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  2776. if ( ( clipSize != 0 ) && ( ammoClip <= 0 ) ) {
  2777. return;
  2778. }
  2779. #else
  2780. // check if we're out of ammo or the clip is empty
  2781. int ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  2782. if ( !ammoAvail || ( ( clipSize != 0 ) && ( ammoClip <= 0 ) ) ) {
  2783. return;
  2784. }
  2785. #endif
  2786. // if this is a power ammo weapon ( currently only the bfg ) then make sure
  2787. // we only fire as much power as available in each clip
  2788. if ( powerAmmo ) {
  2789. // power comes in as a float from zero to max
  2790. // if we use this on more than the bfg will need to define the max
  2791. // in the .def as opposed to just in the script so proper calcs
  2792. // can be done here.
  2793. dmgPower = ( int )dmgPower + 1;
  2794. if ( dmgPower > ammoClip ) {
  2795. dmgPower = ammoClip;
  2796. }
  2797. }
  2798. #ifdef _D3XP
  2799. if(clipSize == 0) {
  2800. //Weapons with a clip size of 0 launch strait from inventory without moving to a clip
  2801. #endif
  2802. //In D3XP we used the ammo when the ammo was moved into the clip so we don't want to
  2803. //use it now.
  2804. owner->inventory.UseAmmo( ammoType, ( powerAmmo ) ? dmgPower : ammoRequired );
  2805. #ifdef _D3XP
  2806. }
  2807. #endif
  2808. if ( clipSize && ammoRequired ) {
  2809. #ifdef _D3XP
  2810. ammoClip -= powerAmmo ? dmgPower : ammoRequired;
  2811. #else
  2812. ammoClip -= powerAmmo ? dmgPower : 1;
  2813. #endif
  2814. }
  2815. }
  2816. if ( !silent_fire ) {
  2817. // wake up nearby monsters
  2818. gameLocal.AlertAI( owner );
  2819. }
  2820. // set the shader parm to the time of last projectile firing,
  2821. // which the gun material shaders can reference for single shot barrel glows, etc
  2822. renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] = gameLocal.random.CRandomFloat();
  2823. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.realClientTime );
  2824. if ( worldModel.GetEntity() ) {
  2825. worldModel.GetEntity()->SetShaderParm( SHADERPARM_DIVERSITY, renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] );
  2826. worldModel.GetEntity()->SetShaderParm( SHADERPARM_TIMEOFFSET, renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] );
  2827. }
  2828. // calculate the muzzle position
  2829. if ( barrelJointView != INVALID_JOINT && projectileDict.GetBool( "launchFromBarrel" ) ) {
  2830. // there is an explicit joint for the muzzle
  2831. GetGlobalJointTransform( true, barrelJointView, muzzleOrigin, muzzleAxis );
  2832. } else {
  2833. // go straight out of the view
  2834. muzzleOrigin = playerViewOrigin;
  2835. muzzleAxis = playerViewAxis;
  2836. }
  2837. // add some to the kick time, incrementally moving repeat firing weapons back
  2838. if ( kick_endtime < gameLocal.realClientTime ) {
  2839. kick_endtime = gameLocal.realClientTime;
  2840. }
  2841. kick_endtime += muzzle_kick_time;
  2842. if ( kick_endtime > gameLocal.realClientTime + muzzle_kick_maxtime ) {
  2843. kick_endtime = gameLocal.realClientTime + muzzle_kick_maxtime;
  2844. }
  2845. if ( gameLocal.isClient ) {
  2846. // predict instant hit projectiles
  2847. if ( projectileDict.GetBool( "net_instanthit" ) ) {
  2848. float spreadRad = DEG2RAD( spread );
  2849. muzzle_pos = muzzleOrigin + playerViewAxis[ 0 ] * 2.0f;
  2850. for( i = 0; i < num_projectiles; i++ ) {
  2851. ang = idMath::Sin( spreadRad * gameLocal.random.RandomFloat() );
  2852. spin = (float)DEG2RAD( 360.0f ) * gameLocal.random.RandomFloat();
  2853. dir = playerViewAxis[ 0 ] + playerViewAxis[ 2 ] * ( ang * idMath::Sin( spin ) ) - playerViewAxis[ 1 ] * ( ang * idMath::Cos( spin ) );
  2854. dir.Normalize();
  2855. gameLocal.clip.Translation( tr, muzzle_pos, muzzle_pos + dir * 4096.0f, NULL, mat3_identity, MASK_SHOT_RENDERMODEL, owner );
  2856. if ( tr.fraction < 1.0f ) {
  2857. idProjectile::ClientPredictionCollide( this, projectileDict, tr, vec3_origin, true );
  2858. }
  2859. }
  2860. }
  2861. } else {
  2862. ownerBounds = owner->GetPhysics()->GetAbsBounds();
  2863. owner->AddProjectilesFired( num_projectiles );
  2864. float spreadRad = DEG2RAD( spread );
  2865. for( i = 0; i < num_projectiles; i++ ) {
  2866. ang = idMath::Sin( spreadRad * gameLocal.random.RandomFloat() );
  2867. spin = (float)DEG2RAD( 360.0f ) * gameLocal.random.RandomFloat();
  2868. dir = playerViewAxis[ 0 ] + playerViewAxis[ 2 ] * ( ang * idMath::Sin( spin ) ) - playerViewAxis[ 1 ] * ( ang * idMath::Cos( spin ) );
  2869. dir.Normalize();
  2870. if ( projectileEnt ) {
  2871. ent = projectileEnt;
  2872. ent->Show();
  2873. ent->Unbind();
  2874. projectileEnt = NULL;
  2875. } else {
  2876. gameLocal.SpawnEntityDef( projectileDict, &ent, false );
  2877. }
  2878. if ( !ent || !ent->IsType( idProjectile::Type ) ) {
  2879. const char *projectileName = weaponDef->dict.GetString( "def_projectile" );
  2880. gameLocal.Error( "'%s' is not an idProjectile", projectileName );
  2881. }
  2882. if ( projectileDict.GetBool( "net_instanthit" ) ) {
  2883. // don't synchronize this on top of the already predicted effect
  2884. ent->fl.networkSync = false;
  2885. }
  2886. proj = static_cast<idProjectile *>(ent);
  2887. proj->Create( owner, muzzleOrigin, dir );
  2888. projBounds = proj->GetPhysics()->GetBounds().Rotate( proj->GetPhysics()->GetAxis() );
  2889. // make sure the projectile starts inside the bounding box of the owner
  2890. if ( i == 0 ) {
  2891. muzzle_pos = muzzleOrigin + playerViewAxis[ 0 ] * 2.0f;
  2892. if ( ( ownerBounds - projBounds).RayIntersection( muzzle_pos, playerViewAxis[0], distance ) ) {
  2893. start = muzzle_pos + distance * playerViewAxis[0];
  2894. } else {
  2895. start = ownerBounds.GetCenter();
  2896. }
  2897. gameLocal.clip.Translation( tr, start, muzzle_pos, proj->GetPhysics()->GetClipModel(), proj->GetPhysics()->GetClipModel()->GetAxis(), MASK_SHOT_RENDERMODEL, owner );
  2898. muzzle_pos = tr.endpos;
  2899. }
  2900. proj->Launch( muzzle_pos, dir, pushVelocity, fuseOffset, launchPower, dmgPower );
  2901. }
  2902. // toss the brass
  2903. #ifdef _D3XP
  2904. if(brassDelay >= 0)
  2905. #endif
  2906. PostEventMS( &EV_Weapon_EjectBrass, brassDelay );
  2907. }
  2908. // add the light for the muzzleflash
  2909. if ( !lightOn ) {
  2910. MuzzleFlashLight();
  2911. }
  2912. owner->WeaponFireFeedback( &weaponDef->dict );
  2913. // reset muzzle smoke
  2914. weaponSmokeStartTime = gameLocal.realClientTime;
  2915. }
  2916. #ifdef _D3XP
  2917. /*
  2918. ================
  2919. idWeapon::Event_LaunchProjectilesEllipse
  2920. ================
  2921. */
  2922. void idWeapon::Event_LaunchProjectilesEllipse( int num_projectiles, float spreada, float spreadb, float fuseOffset, float power ) {
  2923. idProjectile *proj;
  2924. idEntity *ent;
  2925. int i;
  2926. idVec3 dir;
  2927. float anga, angb;
  2928. float spin;
  2929. float distance;
  2930. trace_t tr;
  2931. idVec3 start;
  2932. idVec3 muzzle_pos;
  2933. idBounds ownerBounds, projBounds;
  2934. if ( IsHidden() ) {
  2935. return;
  2936. }
  2937. if ( !projectileDict.GetNumKeyVals() ) {
  2938. const char *classname = weaponDef->dict.GetString( "classname" );
  2939. gameLocal.Warning( "No projectile defined on '%s'", classname );
  2940. return;
  2941. }
  2942. // avoid all ammo considerations on a client
  2943. if ( !gameLocal.isClient ) {
  2944. int ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  2945. if ( ( clipSize != 0 ) && ( ammoClip <= 0 ) ) {
  2946. return;
  2947. }
  2948. if( clipSize == 0 ) {
  2949. //Weapons with a clip size of 0 launch strait from inventory without moving to a clip
  2950. owner->inventory.UseAmmo( ammoType, ammoRequired );
  2951. }
  2952. if ( clipSize && ammoRequired ) {
  2953. ammoClip -= ammoRequired;
  2954. }
  2955. if ( !silent_fire ) {
  2956. // wake up nearby monsters
  2957. gameLocal.AlertAI( owner );
  2958. }
  2959. }
  2960. // set the shader parm to the time of last projectile firing,
  2961. // which the gun material shaders can reference for single shot barrel glows, etc
  2962. renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] = gameLocal.random.CRandomFloat();
  2963. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  2964. if ( worldModel.GetEntity() ) {
  2965. worldModel.GetEntity()->SetShaderParm( SHADERPARM_DIVERSITY, renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] );
  2966. worldModel.GetEntity()->SetShaderParm( SHADERPARM_TIMEOFFSET, renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] );
  2967. }
  2968. // calculate the muzzle position
  2969. if ( barrelJointView != INVALID_JOINT && projectileDict.GetBool( "launchFromBarrel" ) ) {
  2970. // there is an explicit joint for the muzzle
  2971. GetGlobalJointTransform( true, barrelJointView, muzzleOrigin, muzzleAxis );
  2972. } else {
  2973. // go straight out of the view
  2974. muzzleOrigin = playerViewOrigin;
  2975. muzzleAxis = playerViewAxis;
  2976. }
  2977. // add some to the kick time, incrementally moving repeat firing weapons back
  2978. if ( kick_endtime < gameLocal.time ) {
  2979. kick_endtime = gameLocal.time;
  2980. }
  2981. kick_endtime += muzzle_kick_time;
  2982. if ( kick_endtime > gameLocal.time + muzzle_kick_maxtime ) {
  2983. kick_endtime = gameLocal.time + muzzle_kick_maxtime;
  2984. }
  2985. if ( !gameLocal.isClient ) {
  2986. ownerBounds = owner->GetPhysics()->GetAbsBounds();
  2987. owner->AddProjectilesFired( num_projectiles );
  2988. float spreadRadA = DEG2RAD( spreada );
  2989. float spreadRadB = DEG2RAD( spreadb );
  2990. for( i = 0; i < num_projectiles; i++ ) {
  2991. //Ellipse Form
  2992. spin = (float)DEG2RAD( 360.0f ) * gameLocal.random.RandomFloat();
  2993. anga = idMath::Sin(spreadRadA * gameLocal.random.RandomFloat());
  2994. angb = idMath::Sin(spreadRadB * gameLocal.random.RandomFloat());
  2995. dir = playerViewAxis[ 0 ] + playerViewAxis[ 2 ] * ( angb*idMath::Sin( spin ) ) - playerViewAxis[ 1 ] * ( anga*idMath::Cos( spin ) );
  2996. dir.Normalize();
  2997. gameLocal.SpawnEntityDef( projectileDict, &ent );
  2998. if ( !ent || !ent->IsType( idProjectile::Type ) ) {
  2999. const char *projectileName = weaponDef->dict.GetString( "def_projectile" );
  3000. gameLocal.Error( "'%s' is not an idProjectile", projectileName );
  3001. }
  3002. proj = static_cast<idProjectile *>(ent);
  3003. proj->Create( owner, muzzleOrigin, dir );
  3004. projBounds = proj->GetPhysics()->GetBounds().Rotate( proj->GetPhysics()->GetAxis() );
  3005. // make sure the projectile starts inside the bounding box of the owner
  3006. if ( i == 0 ) {
  3007. muzzle_pos = muzzleOrigin + playerViewAxis[ 0 ] * 2.0f;
  3008. if ( ( ownerBounds - projBounds).RayIntersection( muzzle_pos, playerViewAxis[0], distance ) ) {
  3009. start = muzzle_pos + distance * playerViewAxis[0];
  3010. }
  3011. else {
  3012. start = ownerBounds.GetCenter();
  3013. }
  3014. gameLocal.clip.Translation( tr, start, muzzle_pos, proj->GetPhysics()->GetClipModel(), proj->GetPhysics()->GetClipModel()->GetAxis(), MASK_SHOT_RENDERMODEL, owner );
  3015. muzzle_pos = tr.endpos;
  3016. }
  3017. proj->Launch( muzzle_pos, dir, pushVelocity, fuseOffset, power );
  3018. }
  3019. // toss the brass
  3020. if( brassDelay >= 0 ) {
  3021. PostEventMS( &EV_Weapon_EjectBrass, brassDelay );
  3022. }
  3023. }
  3024. // add the light for the muzzleflash
  3025. if ( !lightOn ) {
  3026. MuzzleFlashLight();
  3027. }
  3028. owner->WeaponFireFeedback( &weaponDef->dict );
  3029. // reset muzzle smoke
  3030. weaponSmokeStartTime = gameLocal.time;
  3031. }
  3032. /**
  3033. * Gives the player a powerup as if it were a weapon shot. It will use the ammo amount specified
  3034. * as ammoRequired.
  3035. */
  3036. void idWeapon::Event_LaunchPowerup( const char* powerup, float duration, int useAmmo ) {
  3037. if ( IsHidden() ) {
  3038. return;
  3039. }
  3040. // check if we're out of ammo
  3041. if(useAmmo) {
  3042. int ammoAvail = owner->inventory.HasAmmo( ammoType, ammoRequired );
  3043. if ( !ammoAvail ) {
  3044. return;
  3045. }
  3046. owner->inventory.UseAmmo( ammoType, ammoRequired );
  3047. }
  3048. // set the shader parm to the time of last projectile firing,
  3049. // which the gun material shaders can reference for single shot barrel glows, etc
  3050. renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] = gameLocal.random.CRandomFloat();
  3051. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  3052. if ( worldModel.GetEntity() ) {
  3053. worldModel.GetEntity()->SetShaderParm( SHADERPARM_DIVERSITY, renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] );
  3054. worldModel.GetEntity()->SetShaderParm( SHADERPARM_TIMEOFFSET, renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] );
  3055. }
  3056. // add the light for the muzzleflash
  3057. if ( !lightOn ) {
  3058. MuzzleFlashLight();
  3059. }
  3060. owner->Give(powerup, va("%f", duration));
  3061. }
  3062. void idWeapon::Event_StartWeaponSmoke() {
  3063. // reset muzzle smoke
  3064. weaponSmokeStartTime = gameLocal.time;
  3065. }
  3066. void idWeapon::Event_StopWeaponSmoke() {
  3067. // reset muzzle smoke
  3068. weaponSmokeStartTime = 0;
  3069. }
  3070. void idWeapon::Event_StartWeaponParticle( const char* name) {
  3071. WeaponParticle_t* part;
  3072. weaponParticles.Get(name, &part);
  3073. if(part) {
  3074. part->active = true;
  3075. part->startTime = gameLocal.time;
  3076. //Toggle the emitter
  3077. if(!part->smoke) {
  3078. part->emitter->Show();
  3079. part->emitter->PostEventMS(&EV_Activate, 0, this);
  3080. }
  3081. }
  3082. }
  3083. void idWeapon::Event_StopWeaponParticle( const char* name) {
  3084. WeaponParticle_t* part;
  3085. weaponParticles.Get(name, &part);
  3086. if(part) {
  3087. part->active = false;
  3088. part->startTime = 0;
  3089. //Toggle the emitter
  3090. if(!part->smoke) {
  3091. part->emitter->Hide();
  3092. part->emitter->PostEventMS(&EV_Activate, 0, this);
  3093. }
  3094. }
  3095. }
  3096. void idWeapon::Event_StartWeaponLight( const char* name) {
  3097. WeaponLight_t* light;
  3098. weaponLights.Get(name, &light);
  3099. if(light) {
  3100. light->active = true;
  3101. light->startTime = gameLocal.time;
  3102. }
  3103. }
  3104. void idWeapon::Event_StopWeaponLight( const char* name) {
  3105. WeaponLight_t* light;
  3106. weaponLights.Get(name, &light);
  3107. if(light) {
  3108. light->active = false;
  3109. light->startTime = 0;
  3110. if(light->lightHandle != -1) {
  3111. gameRenderWorld->FreeLightDef( light->lightHandle );
  3112. light->lightHandle = -1;
  3113. }
  3114. }
  3115. }
  3116. #endif
  3117. /*
  3118. =====================
  3119. idWeapon::Event_Melee
  3120. =====================
  3121. */
  3122. void idWeapon::Event_Melee( void ) {
  3123. idEntity *ent;
  3124. trace_t tr;
  3125. if ( !meleeDef ) {
  3126. gameLocal.Error( "No meleeDef on '%s'", weaponDef->dict.GetString( "classname" ) );
  3127. }
  3128. if ( !gameLocal.isClient ) {
  3129. idVec3 start = playerViewOrigin;
  3130. idVec3 end = start + playerViewAxis[0] * ( meleeDistance * owner->PowerUpModifier( MELEE_DISTANCE ) );
  3131. gameLocal.clip.TracePoint( tr, start, end, MASK_SHOT_RENDERMODEL, owner );
  3132. if ( tr.fraction < 1.0f ) {
  3133. ent = gameLocal.GetTraceEntity( tr );
  3134. } else {
  3135. ent = NULL;
  3136. }
  3137. if ( g_debugWeapon.GetBool() ) {
  3138. gameRenderWorld->DebugLine( colorYellow, start, end, 100 );
  3139. if ( ent ) {
  3140. gameRenderWorld->DebugBounds( colorRed, ent->GetPhysics()->GetBounds(), ent->GetPhysics()->GetOrigin(), 100 );
  3141. }
  3142. }
  3143. bool hit = false;
  3144. const char *hitSound = meleeDef->dict.GetString( "snd_miss" );
  3145. if ( ent ) {
  3146. float push = meleeDef->dict.GetFloat( "push" );
  3147. idVec3 impulse = -push * owner->PowerUpModifier( SPEED ) * tr.c.normal;
  3148. if ( gameLocal.world->spawnArgs.GetBool( "no_Weapons" ) && ( ent->IsType( idActor::Type ) || ent->IsType( idAFAttachment::Type) ) ) {
  3149. idThread::ReturnInt( 0 );
  3150. return;
  3151. }
  3152. ent->ApplyImpulse( this, tr.c.id, tr.c.point, impulse );
  3153. // weapon stealing - do this before damaging so weapons are not dropped twice
  3154. if ( gameLocal.isMultiplayer
  3155. && weaponDef && weaponDef->dict.GetBool( "stealing" )
  3156. && ent->IsType( idPlayer::Type )
  3157. && !owner->PowerUpActive( BERSERK )
  3158. && ( (gameLocal.gameType != GAME_TDM ) || gameLocal.serverInfo.GetBool( "si_teamDamage" ) || ( owner->team != static_cast< idPlayer * >( ent )->team ) )
  3159. ) {
  3160. #ifdef CTF /* Code is formed oddly for easy merge */
  3161. if ( gameLocal.mpGame.IsGametypeFlagBased() )
  3162. { /* Do nothing ... */ }
  3163. else
  3164. #endif
  3165. owner->StealWeapon( static_cast< idPlayer * >( ent ) );
  3166. }
  3167. if ( ent->fl.takedamage ) {
  3168. idVec3 kickDir, globalKickDir;
  3169. meleeDef->dict.GetVector( "kickDir", "0 0 0", kickDir );
  3170. globalKickDir = muzzleAxis * kickDir;
  3171. #ifdef _D3XP
  3172. //Adjust the melee powerup modifier for the invulnerability boss fight
  3173. float mod = owner->PowerUpModifier( MELEE_DAMAGE );
  3174. if(!strcmp(ent->GetEntityDefName(), "monster_hunter_invul")) {
  3175. //Only do a quater of the damage mod
  3176. mod *= 0.25f;
  3177. }
  3178. ent->Damage( owner, owner, globalKickDir, meleeDefName, mod, tr.c.id );
  3179. #else
  3180. ent->Damage( owner, owner, globalKickDir, meleeDefName, owner->PowerUpModifier( MELEE_DAMAGE ), tr.c.id );
  3181. #endif
  3182. hit = true;
  3183. }
  3184. if ( weaponDef->dict.GetBool( "impact_damage_effect" ) ) {
  3185. if ( ent->spawnArgs.GetBool( "bleed" ) ) {
  3186. hitSound = meleeDef->dict.GetString( owner->PowerUpActive( BERSERK ) ? "snd_hit_berserk" : "snd_hit" );
  3187. ent->AddDamageEffect( tr, impulse, meleeDef->dict.GetString( "classname" ) );
  3188. } else {
  3189. int type = tr.c.material->GetSurfaceType();
  3190. if ( type == SURFTYPE_NONE ) {
  3191. type = GetDefaultSurfaceType();
  3192. }
  3193. const char *materialType = gameLocal.sufaceTypeNames[ type ];
  3194. // start impact sound based on material type
  3195. hitSound = meleeDef->dict.GetString( va( "snd_%s", materialType ) );
  3196. if ( *hitSound == '\0' ) {
  3197. hitSound = meleeDef->dict.GetString( "snd_metal" );
  3198. }
  3199. if ( gameLocal.time > nextStrikeFx ) {
  3200. const char *decal;
  3201. // project decal
  3202. decal = weaponDef->dict.GetString( "mtr_strike" );
  3203. if ( decal && *decal ) {
  3204. gameLocal.ProjectDecal( tr.c.point, -tr.c.normal, 8.0f, true, 6.0, decal );
  3205. }
  3206. nextStrikeFx = gameLocal.time + 200;
  3207. } else {
  3208. hitSound = "";
  3209. }
  3210. strikeSmokeStartTime = gameLocal.time;
  3211. strikePos = tr.c.point;
  3212. strikeAxis = -tr.endAxis;
  3213. }
  3214. }
  3215. }
  3216. if ( *hitSound != '\0' ) {
  3217. const idSoundShader *snd = declManager->FindSound( hitSound );
  3218. StartSoundShader( snd, SND_CHANNEL_BODY2, 0, true, NULL );
  3219. }
  3220. idThread::ReturnInt( hit );
  3221. owner->WeaponFireFeedback( &weaponDef->dict );
  3222. return;
  3223. }
  3224. idThread::ReturnInt( 0 );
  3225. owner->WeaponFireFeedback( &weaponDef->dict );
  3226. }
  3227. /*
  3228. =====================
  3229. idWeapon::Event_GetWorldModel
  3230. =====================
  3231. */
  3232. void idWeapon::Event_GetWorldModel( void ) {
  3233. idThread::ReturnEntity( worldModel.GetEntity() );
  3234. }
  3235. /*
  3236. =====================
  3237. idWeapon::Event_AllowDrop
  3238. =====================
  3239. */
  3240. void idWeapon::Event_AllowDrop( int allow ) {
  3241. if ( allow ) {
  3242. allowDrop = true;
  3243. } else {
  3244. allowDrop = false;
  3245. }
  3246. }
  3247. /*
  3248. ================
  3249. idWeapon::Event_EjectBrass
  3250. Toss a shell model out from the breach if the bone is present
  3251. ================
  3252. */
  3253. void idWeapon::Event_EjectBrass( void ) {
  3254. if ( !g_showBrass.GetBool() || !owner->CanShowWeaponViewmodel() ) {
  3255. return;
  3256. }
  3257. if ( ejectJointView == INVALID_JOINT || !brassDict.GetNumKeyVals() ) {
  3258. return;
  3259. }
  3260. if ( gameLocal.isClient ) {
  3261. return;
  3262. }
  3263. idMat3 axis;
  3264. idVec3 origin, linear_velocity, angular_velocity;
  3265. idEntity *ent;
  3266. if ( !GetGlobalJointTransform( true, ejectJointView, origin, axis ) ) {
  3267. return;
  3268. }
  3269. gameLocal.SpawnEntityDef( brassDict, &ent, false );
  3270. if ( !ent || !ent->IsType( idDebris::Type ) ) {
  3271. gameLocal.Error( "'%s' is not an idDebris", weaponDef ? weaponDef->dict.GetString( "def_ejectBrass" ) : "def_ejectBrass" );
  3272. }
  3273. idDebris *debris = static_cast<idDebris *>(ent);
  3274. debris->Create( owner, origin, axis );
  3275. debris->Launch();
  3276. linear_velocity = 40 * ( playerViewAxis[0] + playerViewAxis[1] + playerViewAxis[2] );
  3277. angular_velocity.Set( 10 * gameLocal.random.CRandomFloat(), 10 * gameLocal.random.CRandomFloat(), 10 * gameLocal.random.CRandomFloat() );
  3278. debris->GetPhysics()->SetLinearVelocity( linear_velocity );
  3279. debris->GetPhysics()->SetAngularVelocity( angular_velocity );
  3280. }
  3281. /*
  3282. ===============
  3283. idWeapon::Event_IsInvisible
  3284. ===============
  3285. */
  3286. void idWeapon::Event_IsInvisible( void ) {
  3287. if ( !owner ) {
  3288. idThread::ReturnFloat( 0 );
  3289. return;
  3290. }
  3291. idThread::ReturnFloat( owner->PowerUpActive( INVISIBILITY ) ? 1 : 0 );
  3292. }
  3293. /*
  3294. ===============
  3295. idWeapon::ClientPredictionThink
  3296. ===============
  3297. */
  3298. void idWeapon::ClientPredictionThink( void ) {
  3299. UpdateAnimation();
  3300. }