AFEntity.cpp 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690
  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. ===============================================================================
  25. idMultiModelAF
  26. ===============================================================================
  27. */
  28. CLASS_DECLARATION( idEntity, idMultiModelAF )
  29. END_CLASS
  30. /*
  31. ================
  32. idMultiModelAF::Spawn
  33. ================
  34. */
  35. void idMultiModelAF::Spawn( void ) {
  36. physicsObj.SetSelf( this );
  37. }
  38. /*
  39. ================
  40. idMultiModelAF::~idMultiModelAF
  41. ================
  42. */
  43. idMultiModelAF::~idMultiModelAF( void ) {
  44. int i;
  45. for ( i = 0; i < modelDefHandles.Num(); i++ ) {
  46. if ( modelDefHandles[i] != -1 ) {
  47. gameRenderWorld->FreeEntityDef( modelDefHandles[i] );
  48. modelDefHandles[i] = -1;
  49. }
  50. }
  51. }
  52. /*
  53. ================
  54. idMultiModelAF::SetModelForId
  55. ================
  56. */
  57. void idMultiModelAF::SetModelForId( int id, const idStr &modelName ) {
  58. modelHandles.AssureSize( id+1, NULL );
  59. modelDefHandles.AssureSize( id+1, -1 );
  60. modelHandles[id] = renderModelManager->FindModel( modelName );
  61. }
  62. /*
  63. ================
  64. idMultiModelAF::Present
  65. ================
  66. */
  67. void idMultiModelAF::Present( void ) {
  68. int i;
  69. // don't present to the renderer if the entity hasn't changed
  70. if ( !( thinkFlags & TH_UPDATEVISUALS ) ) {
  71. return;
  72. }
  73. BecomeInactive( TH_UPDATEVISUALS );
  74. for ( i = 0; i < modelHandles.Num(); i++ ) {
  75. if ( !modelHandles[i] ) {
  76. continue;
  77. }
  78. renderEntity.origin = physicsObj.GetOrigin( i );
  79. renderEntity.axis = physicsObj.GetAxis( i );
  80. renderEntity.hModel = modelHandles[i];
  81. renderEntity.bodyId = i;
  82. // add to refresh list
  83. if ( modelDefHandles[i] == -1 ) {
  84. modelDefHandles[i] = gameRenderWorld->AddEntityDef( &renderEntity );
  85. } else {
  86. gameRenderWorld->UpdateEntityDef( modelDefHandles[i], &renderEntity );
  87. }
  88. }
  89. }
  90. /*
  91. ================
  92. idMultiModelAF::Think
  93. ================
  94. */
  95. void idMultiModelAF::Think( void ) {
  96. RunPhysics();
  97. Present();
  98. }
  99. /*
  100. ===============================================================================
  101. idChain
  102. ===============================================================================
  103. */
  104. CLASS_DECLARATION( idMultiModelAF, idChain )
  105. END_CLASS
  106. /*
  107. ================
  108. idChain::BuildChain
  109. builds a chain hanging down from the ceiling
  110. the highest link is a child of the link below it etc.
  111. this allows an object to be attached to multiple chains while keeping a single tree structure
  112. ================
  113. */
  114. void idChain::BuildChain( const idStr &name, const idVec3 &origin, float linkLength, float linkWidth, float density, int numLinks, bool bindToWorld ) {
  115. int i;
  116. float halfLinkLength = linkLength * 0.5f;
  117. idTraceModel trm;
  118. idClipModel *clip;
  119. idAFBody *body, *lastBody;
  120. idAFConstraint_BallAndSocketJoint *bsj;
  121. idAFConstraint_UniversalJoint *uj;
  122. idVec3 org;
  123. // create a trace model
  124. trm = idTraceModel( linkLength, linkWidth );
  125. trm.Translate( -trm.offset );
  126. org = origin - idVec3( 0, 0, halfLinkLength );
  127. lastBody = NULL;
  128. for ( i = 0; i < numLinks; i++ ) {
  129. // add body
  130. clip = new idClipModel( trm );
  131. clip->SetContents( CONTENTS_SOLID );
  132. clip->Link( gameLocal.clip, this, 0, org, mat3_identity );
  133. body = new idAFBody( name + idStr(i), clip, density );
  134. physicsObj.AddBody( body );
  135. // visual model for body
  136. SetModelForId( physicsObj.GetBodyId( body ), spawnArgs.GetString( "model" ) );
  137. // add constraint
  138. if ( bindToWorld ) {
  139. if ( !lastBody ) {
  140. uj = new idAFConstraint_UniversalJoint( name + idStr(i), body, lastBody );
  141. uj->SetShafts( idVec3( 0, 0, -1 ), idVec3( 0, 0, 1 ) );
  142. //uj->SetConeLimit( idVec3( 0, 0, -1 ), 30.0f );
  143. //uj->SetPyramidLimit( idVec3( 0, 0, -1 ), idVec3( 1, 0, 0 ), 90.0f, 30.0f );
  144. }
  145. else {
  146. uj = new idAFConstraint_UniversalJoint( name + idStr(i), lastBody, body );
  147. uj->SetShafts( idVec3( 0, 0, 1 ), idVec3( 0, 0, -1 ) );
  148. //uj->SetConeLimit( idVec3( 0, 0, 1 ), 30.0f );
  149. }
  150. uj->SetAnchor( org + idVec3( 0, 0, halfLinkLength ) );
  151. uj->SetFriction( 0.9f );
  152. physicsObj.AddConstraint( uj );
  153. }
  154. else {
  155. if ( lastBody ) {
  156. bsj = new idAFConstraint_BallAndSocketJoint( "joint" + idStr(i), lastBody, body );
  157. bsj->SetAnchor( org + idVec3( 0, 0, halfLinkLength ) );
  158. bsj->SetConeLimit( idVec3( 0, 0, 1 ), 60.0f, idVec3( 0, 0, 1 ) );
  159. physicsObj.AddConstraint( bsj );
  160. }
  161. }
  162. org[2] -= linkLength;
  163. lastBody = body;
  164. }
  165. }
  166. /*
  167. ================
  168. idChain::Spawn
  169. ================
  170. */
  171. void idChain::Spawn( void ) {
  172. int numLinks;
  173. float length, linkLength, linkWidth, density;
  174. bool drop;
  175. idVec3 origin;
  176. spawnArgs.GetBool( "drop", "0", drop );
  177. spawnArgs.GetInt( "links", "3", numLinks );
  178. spawnArgs.GetFloat( "length", idStr( numLinks * 32.0f ), length );
  179. spawnArgs.GetFloat( "width", "8", linkWidth );
  180. spawnArgs.GetFloat( "density", "0.2", density );
  181. linkLength = length / numLinks;
  182. origin = GetPhysics()->GetOrigin();
  183. // initialize physics
  184. physicsObj.SetSelf( this );
  185. physicsObj.SetGravity( gameLocal.GetGravity() );
  186. physicsObj.SetClipMask( MASK_SOLID | CONTENTS_BODY );
  187. SetPhysics( &physicsObj );
  188. BuildChain( "link", origin, linkLength, linkWidth, density, numLinks, !drop );
  189. }
  190. /*
  191. ===============================================================================
  192. idAFAttachment
  193. ===============================================================================
  194. */
  195. CLASS_DECLARATION( idAnimatedEntity, idAFAttachment )
  196. END_CLASS
  197. /*
  198. =====================
  199. idAFAttachment::idAFAttachment
  200. =====================
  201. */
  202. idAFAttachment::idAFAttachment( void ) {
  203. body = NULL;
  204. combatModel = NULL;
  205. idleAnim = 0;
  206. attachJoint = INVALID_JOINT;
  207. }
  208. /*
  209. =====================
  210. idAFAttachment::~idAFAttachment
  211. =====================
  212. */
  213. idAFAttachment::~idAFAttachment( void ) {
  214. StopSound( SND_CHANNEL_ANY, false );
  215. delete combatModel;
  216. combatModel = NULL;
  217. }
  218. /*
  219. =====================
  220. idAFAttachment::Spawn
  221. =====================
  222. */
  223. void idAFAttachment::Spawn( void ) {
  224. idleAnim = animator.GetAnim( "idle" );
  225. }
  226. /*
  227. =====================
  228. idAFAttachment::SetBody
  229. =====================
  230. */
  231. void idAFAttachment::SetBody( idEntity *bodyEnt, const char *model, jointHandle_t attachJoint ) {
  232. bool bleed;
  233. body = bodyEnt;
  234. this->attachJoint = attachJoint;
  235. SetModel( model );
  236. fl.takedamage = true;
  237. bleed = body->spawnArgs.GetBool( "bleed" );
  238. spawnArgs.SetBool( "bleed", bleed );
  239. }
  240. /*
  241. =====================
  242. idAFAttachment::ClearBody
  243. =====================
  244. */
  245. void idAFAttachment::ClearBody( void ) {
  246. body = NULL;
  247. attachJoint = INVALID_JOINT;
  248. Hide();
  249. }
  250. /*
  251. =====================
  252. idAFAttachment::GetBody
  253. =====================
  254. */
  255. idEntity *idAFAttachment::GetBody( void ) const {
  256. return body;
  257. }
  258. /*
  259. ================
  260. idAFAttachment::Save
  261. archive object for savegame file
  262. ================
  263. */
  264. void idAFAttachment::Save( idSaveGame *savefile ) const {
  265. savefile->WriteObject( body );
  266. savefile->WriteInt( idleAnim );
  267. savefile->WriteJoint( attachJoint );
  268. }
  269. /*
  270. ================
  271. idAFAttachment::Restore
  272. unarchives object from save game file
  273. ================
  274. */
  275. void idAFAttachment::Restore( idRestoreGame *savefile ) {
  276. savefile->ReadObject( reinterpret_cast<idClass *&>( body ) );
  277. savefile->ReadInt( idleAnim );
  278. savefile->ReadJoint( attachJoint );
  279. SetCombatModel();
  280. LinkCombat();
  281. }
  282. /*
  283. ================
  284. idAFAttachment::Hide
  285. ================
  286. */
  287. void idAFAttachment::Hide( void ) {
  288. idEntity::Hide();
  289. UnlinkCombat();
  290. }
  291. /*
  292. ================
  293. idAFAttachment::Show
  294. ================
  295. */
  296. void idAFAttachment::Show( void ) {
  297. idEntity::Show();
  298. LinkCombat();
  299. }
  300. /*
  301. ============
  302. idAFAttachment::Damage
  303. Pass damage to body at the bindjoint
  304. ============
  305. */
  306. void idAFAttachment::Damage( idEntity *inflictor, idEntity *attacker, const idVec3 &dir,
  307. const char *damageDefName, const float damageScale, const int location ) {
  308. if ( body ) {
  309. body->Damage( inflictor, attacker, dir, damageDefName, damageScale, attachJoint );
  310. }
  311. }
  312. /*
  313. ================
  314. idAFAttachment::AddDamageEffect
  315. ================
  316. */
  317. void idAFAttachment::AddDamageEffect( const trace_t &collision, const idVec3 &velocity, const char *damageDefName ) {
  318. if ( body ) {
  319. trace_t c = collision;
  320. c.c.id = JOINT_HANDLE_TO_CLIPMODEL_ID( attachJoint );
  321. body->AddDamageEffect( c, velocity, damageDefName );
  322. }
  323. }
  324. /*
  325. ================
  326. idAFAttachment::GetImpactInfo
  327. ================
  328. */
  329. void idAFAttachment::GetImpactInfo( idEntity *ent, int id, const idVec3 &point, impactInfo_t *info ) {
  330. if ( body ) {
  331. body->GetImpactInfo( ent, JOINT_HANDLE_TO_CLIPMODEL_ID( attachJoint ), point, info );
  332. } else {
  333. idEntity::GetImpactInfo( ent, id, point, info );
  334. }
  335. }
  336. /*
  337. ================
  338. idAFAttachment::ApplyImpulse
  339. ================
  340. */
  341. void idAFAttachment::ApplyImpulse( idEntity *ent, int id, const idVec3 &point, const idVec3 &impulse ) {
  342. if ( body ) {
  343. body->ApplyImpulse( ent, JOINT_HANDLE_TO_CLIPMODEL_ID( attachJoint ), point, impulse );
  344. } else {
  345. idEntity::ApplyImpulse( ent, id, point, impulse );
  346. }
  347. }
  348. /*
  349. ================
  350. idAFAttachment::AddForce
  351. ================
  352. */
  353. void idAFAttachment::AddForce( idEntity *ent, int id, const idVec3 &point, const idVec3 &force ) {
  354. if ( body ) {
  355. body->AddForce( ent, JOINT_HANDLE_TO_CLIPMODEL_ID( attachJoint ), point, force );
  356. } else {
  357. idEntity::AddForce( ent, id, point, force );
  358. }
  359. }
  360. /*
  361. ================
  362. idAFAttachment::PlayIdleAnim
  363. ================
  364. */
  365. void idAFAttachment::PlayIdleAnim( int blendTime ) {
  366. if ( idleAnim && ( idleAnim != animator.CurrentAnim( ANIMCHANNEL_ALL )->AnimNum() ) ) {
  367. animator.CycleAnim( ANIMCHANNEL_ALL, idleAnim, gameLocal.time, blendTime );
  368. }
  369. }
  370. /*
  371. ================
  372. idAfAttachment::Think
  373. ================
  374. */
  375. void idAFAttachment::Think( void ) {
  376. idAnimatedEntity::Think();
  377. if ( thinkFlags & TH_UPDATEPARTICLES ) {
  378. UpdateDamageEffects();
  379. }
  380. }
  381. /*
  382. ================
  383. idAFAttachment::SetCombatModel
  384. ================
  385. */
  386. void idAFAttachment::SetCombatModel( void ) {
  387. if ( combatModel ) {
  388. combatModel->Unlink();
  389. combatModel->LoadModel( modelDefHandle );
  390. } else {
  391. combatModel = new idClipModel( modelDefHandle );
  392. }
  393. combatModel->SetOwner( body );
  394. }
  395. /*
  396. ================
  397. idAFAttachment::GetCombatModel
  398. ================
  399. */
  400. idClipModel *idAFAttachment::GetCombatModel( void ) const {
  401. return combatModel;
  402. }
  403. /*
  404. ================
  405. idAFAttachment::LinkCombat
  406. ================
  407. */
  408. void idAFAttachment::LinkCombat( void ) {
  409. if ( fl.hidden ) {
  410. return;
  411. }
  412. if ( combatModel ) {
  413. combatModel->Link( gameLocal.clip, this, 0, renderEntity.origin, renderEntity.axis, modelDefHandle );
  414. }
  415. }
  416. /*
  417. ================
  418. idAFAttachment::UnlinkCombat
  419. ================
  420. */
  421. void idAFAttachment::UnlinkCombat( void ) {
  422. if ( combatModel ) {
  423. combatModel->Unlink();
  424. }
  425. }
  426. /*
  427. ===============================================================================
  428. idAFEntity_Base
  429. ===============================================================================
  430. */
  431. const idEventDef EV_SetConstraintPosition( "SetConstraintPosition", "sv" );
  432. CLASS_DECLARATION( idAnimatedEntity, idAFEntity_Base )
  433. EVENT( EV_SetConstraintPosition, idAFEntity_Base::Event_SetConstraintPosition )
  434. END_CLASS
  435. static const float BOUNCE_SOUND_MIN_VELOCITY = 80.0f;
  436. static const float BOUNCE_SOUND_MAX_VELOCITY = 200.0f;
  437. /*
  438. ================
  439. idAFEntity_Base::idAFEntity_Base
  440. ================
  441. */
  442. idAFEntity_Base::idAFEntity_Base( void ) {
  443. combatModel = NULL;
  444. combatModelContents = 0;
  445. nextSoundTime = 0;
  446. spawnOrigin.Zero();
  447. spawnAxis.Identity();
  448. }
  449. /*
  450. ================
  451. idAFEntity_Base::~idAFEntity_Base
  452. ================
  453. */
  454. idAFEntity_Base::~idAFEntity_Base( void ) {
  455. delete combatModel;
  456. combatModel = NULL;
  457. }
  458. /*
  459. ================
  460. idAFEntity_Base::Save
  461. ================
  462. */
  463. void idAFEntity_Base::Save( idSaveGame *savefile ) const {
  464. savefile->WriteInt( combatModelContents );
  465. savefile->WriteClipModel( combatModel );
  466. savefile->WriteVec3( spawnOrigin );
  467. savefile->WriteMat3( spawnAxis );
  468. savefile->WriteInt( nextSoundTime );
  469. af.Save( savefile );
  470. }
  471. /*
  472. ================
  473. idAFEntity_Base::Restore
  474. ================
  475. */
  476. void idAFEntity_Base::Restore( idRestoreGame *savefile ) {
  477. savefile->ReadInt( combatModelContents );
  478. savefile->ReadClipModel( combatModel );
  479. savefile->ReadVec3( spawnOrigin );
  480. savefile->ReadMat3( spawnAxis );
  481. savefile->ReadInt( nextSoundTime );
  482. LinkCombat();
  483. af.Restore( savefile );
  484. }
  485. /*
  486. ================
  487. idAFEntity_Base::Spawn
  488. ================
  489. */
  490. void idAFEntity_Base::Spawn( void ) {
  491. spawnOrigin = GetPhysics()->GetOrigin();
  492. spawnAxis = GetPhysics()->GetAxis();
  493. nextSoundTime = 0;
  494. }
  495. /*
  496. ================
  497. idAFEntity_Base::LoadAF
  498. ================
  499. */
  500. bool idAFEntity_Base::LoadAF( void ) {
  501. idStr fileName;
  502. if ( !spawnArgs.GetString( "articulatedFigure", "*unknown*", fileName ) ) {
  503. return false;
  504. }
  505. af.SetAnimator( GetAnimator() );
  506. if ( !af.Load( this, fileName ) ) {
  507. gameLocal.Error( "idAFEntity_Base::LoadAF: Couldn't load af file '%s' on entity '%s'", fileName.c_str(), name.c_str() );
  508. }
  509. af.Start();
  510. af.GetPhysics()->Rotate( spawnAxis.ToRotation() );
  511. af.GetPhysics()->Translate( spawnOrigin );
  512. LoadState( spawnArgs );
  513. af.UpdateAnimation();
  514. animator.CreateFrame( gameLocal.time, true );
  515. UpdateVisuals();
  516. return true;
  517. }
  518. /*
  519. ================
  520. idAFEntity_Base::Think
  521. ================
  522. */
  523. void idAFEntity_Base::Think( void ) {
  524. RunPhysics();
  525. UpdateAnimation();
  526. if ( thinkFlags & TH_UPDATEVISUALS ) {
  527. Present();
  528. LinkCombat();
  529. }
  530. }
  531. /*
  532. ================
  533. idAFEntity_Base::BodyForClipModelId
  534. ================
  535. */
  536. int idAFEntity_Base::BodyForClipModelId( int id ) const {
  537. return af.BodyForClipModelId( id );
  538. }
  539. /*
  540. ================
  541. idAFEntity_Base::SaveState
  542. ================
  543. */
  544. void idAFEntity_Base::SaveState( idDict &args ) const {
  545. const idKeyValue *kv;
  546. // save the ragdoll pose
  547. af.SaveState( args );
  548. // save all the bind constraints
  549. kv = spawnArgs.MatchPrefix( "bindConstraint ", NULL );
  550. while ( kv ) {
  551. args.Set( kv->GetKey(), kv->GetValue() );
  552. kv = spawnArgs.MatchPrefix( "bindConstraint ", kv );
  553. }
  554. // save the bind if it exists
  555. kv = spawnArgs.FindKey( "bind" );
  556. if ( kv ) {
  557. args.Set( kv->GetKey(), kv->GetValue() );
  558. }
  559. kv = spawnArgs.FindKey( "bindToJoint" );
  560. if ( kv ) {
  561. args.Set( kv->GetKey(), kv->GetValue() );
  562. }
  563. kv = spawnArgs.FindKey( "bindToBody" );
  564. if ( kv ) {
  565. args.Set( kv->GetKey(), kv->GetValue() );
  566. }
  567. }
  568. /*
  569. ================
  570. idAFEntity_Base::LoadState
  571. ================
  572. */
  573. void idAFEntity_Base::LoadState( const idDict &args ) {
  574. af.LoadState( args );
  575. }
  576. /*
  577. ================
  578. idAFEntity_Base::AddBindConstraints
  579. ================
  580. */
  581. void idAFEntity_Base::AddBindConstraints( void ) {
  582. af.AddBindConstraints();
  583. }
  584. /*
  585. ================
  586. idAFEntity_Base::RemoveBindConstraints
  587. ================
  588. */
  589. void idAFEntity_Base::RemoveBindConstraints( void ) {
  590. af.RemoveBindConstraints();
  591. }
  592. /*
  593. ================
  594. idAFEntity_Base::GetImpactInfo
  595. ================
  596. */
  597. void idAFEntity_Base::GetImpactInfo( idEntity *ent, int id, const idVec3 &point, impactInfo_t *info ) {
  598. if ( af.IsActive() ) {
  599. af.GetImpactInfo( ent, id, point, info );
  600. } else {
  601. idEntity::GetImpactInfo( ent, id, point, info );
  602. }
  603. }
  604. /*
  605. ================
  606. idAFEntity_Base::ApplyImpulse
  607. ================
  608. */
  609. void idAFEntity_Base::ApplyImpulse( idEntity *ent, int id, const idVec3 &point, const idVec3 &impulse ) {
  610. if ( af.IsLoaded() ) {
  611. af.ApplyImpulse( ent, id, point, impulse );
  612. }
  613. if ( !af.IsActive() ) {
  614. idEntity::ApplyImpulse( ent, id, point, impulse );
  615. }
  616. }
  617. /*
  618. ================
  619. idAFEntity_Base::AddForce
  620. ================
  621. */
  622. void idAFEntity_Base::AddForce( idEntity *ent, int id, const idVec3 &point, const idVec3 &force ) {
  623. if ( af.IsLoaded() ) {
  624. af.AddForce( ent, id, point, force );
  625. }
  626. if ( !af.IsActive() ) {
  627. idEntity::AddForce( ent, id, point, force );
  628. }
  629. }
  630. /*
  631. ================
  632. idAFEntity_Base::Collide
  633. ================
  634. */
  635. bool idAFEntity_Base::Collide( const trace_t &collision, const idVec3 &velocity ) {
  636. float v, f;
  637. if ( af.IsActive() ) {
  638. v = -( velocity * collision.c.normal );
  639. if ( v > BOUNCE_SOUND_MIN_VELOCITY && gameLocal.time > nextSoundTime ) {
  640. f = v > BOUNCE_SOUND_MAX_VELOCITY ? 1.0f : idMath::Sqrt( v - BOUNCE_SOUND_MIN_VELOCITY ) * ( 1.0f / idMath::Sqrt( BOUNCE_SOUND_MAX_VELOCITY - BOUNCE_SOUND_MIN_VELOCITY ) );
  641. if ( StartSound( "snd_bounce", SND_CHANNEL_ANY, 0, false, NULL ) ) {
  642. // don't set the volume unless there is a bounce sound as it overrides the entire channel
  643. // which causes footsteps on ai's to not honor their shader parms
  644. SetSoundVolume( f );
  645. }
  646. nextSoundTime = gameLocal.time + 500;
  647. }
  648. }
  649. return false;
  650. }
  651. /*
  652. ================
  653. idAFEntity_Base::GetPhysicsToVisualTransform
  654. ================
  655. */
  656. bool idAFEntity_Base::GetPhysicsToVisualTransform( idVec3 &origin, idMat3 &axis ) {
  657. if ( af.IsActive() ) {
  658. af.GetPhysicsToVisualTransform( origin, axis );
  659. return true;
  660. }
  661. return idEntity::GetPhysicsToVisualTransform( origin, axis );
  662. }
  663. /*
  664. ================
  665. idAFEntity_Base::UpdateAnimationControllers
  666. ================
  667. */
  668. bool idAFEntity_Base::UpdateAnimationControllers( void ) {
  669. if ( af.IsActive() ) {
  670. if ( af.UpdateAnimation() ) {
  671. return true;
  672. }
  673. }
  674. return false;
  675. }
  676. /*
  677. ================
  678. idAFEntity_Base::SetCombatModel
  679. ================
  680. */
  681. void idAFEntity_Base::SetCombatModel( void ) {
  682. if ( combatModel ) {
  683. combatModel->Unlink();
  684. combatModel->LoadModel( modelDefHandle );
  685. } else {
  686. combatModel = new idClipModel( modelDefHandle );
  687. }
  688. }
  689. /*
  690. ================
  691. idAFEntity_Base::GetCombatModel
  692. ================
  693. */
  694. idClipModel *idAFEntity_Base::GetCombatModel( void ) const {
  695. return combatModel;
  696. }
  697. /*
  698. ================
  699. idAFEntity_Base::SetCombatContents
  700. ================
  701. */
  702. void idAFEntity_Base::SetCombatContents( bool enable ) {
  703. assert( combatModel );
  704. if ( enable && combatModelContents ) {
  705. assert( !combatModel->GetContents() );
  706. combatModel->SetContents( combatModelContents );
  707. combatModelContents = 0;
  708. } else if ( !enable && combatModel->GetContents() ) {
  709. assert( !combatModelContents );
  710. combatModelContents = combatModel->GetContents();
  711. combatModel->SetContents( 0 );
  712. }
  713. }
  714. /*
  715. ================
  716. idAFEntity_Base::LinkCombat
  717. ================
  718. */
  719. void idAFEntity_Base::LinkCombat( void ) {
  720. if ( fl.hidden ) {
  721. return;
  722. }
  723. if ( combatModel ) {
  724. combatModel->Link( gameLocal.clip, this, 0, renderEntity.origin, renderEntity.axis, modelDefHandle );
  725. }
  726. }
  727. /*
  728. ================
  729. idAFEntity_Base::UnlinkCombat
  730. ================
  731. */
  732. void idAFEntity_Base::UnlinkCombat( void ) {
  733. if ( combatModel ) {
  734. combatModel->Unlink();
  735. }
  736. }
  737. /*
  738. ================
  739. idAFEntity_Base::FreeModelDef
  740. ================
  741. */
  742. void idAFEntity_Base::FreeModelDef( void ) {
  743. UnlinkCombat();
  744. idEntity::FreeModelDef();
  745. }
  746. /*
  747. ===============
  748. idAFEntity_Base::ShowEditingDialog
  749. ===============
  750. */
  751. void idAFEntity_Base::ShowEditingDialog( void ) {
  752. common->InitTool( EDITOR_AF, &spawnArgs );
  753. }
  754. /*
  755. ================
  756. idAFEntity_Base::DropAFs
  757. The entity should have the following key/value pairs set:
  758. "def_drop<type>AF" "af def"
  759. "drop<type>Skin" "skin name"
  760. To drop multiple articulated figures the following key/value pairs can be used:
  761. "def_drop<type>AF*" "af def"
  762. where * is an aribtrary string.
  763. ================
  764. */
  765. void idAFEntity_Base::DropAFs( idEntity *ent, const char *type, idList<idEntity *> *list ) {
  766. const idKeyValue *kv;
  767. const char *skinName;
  768. idEntity *newEnt;
  769. idAFEntity_Base *af;
  770. idDict args;
  771. const idDeclSkin *skin;
  772. // drop the articulated figures
  773. kv = ent->spawnArgs.MatchPrefix( va( "def_drop%sAF", type ), NULL );
  774. while ( kv ) {
  775. args.Set( "classname", kv->GetValue() );
  776. gameLocal.SpawnEntityDef( args, &newEnt );
  777. if ( newEnt && newEnt->IsType( idAFEntity_Base::Type ) ) {
  778. af = static_cast<idAFEntity_Base *>(newEnt);
  779. af->GetPhysics()->SetOrigin( ent->GetPhysics()->GetOrigin() );
  780. af->GetPhysics()->SetAxis( ent->GetPhysics()->GetAxis() );
  781. af->af.SetupPose( ent, gameLocal.time );
  782. if ( list ) {
  783. list->Append( af );
  784. }
  785. }
  786. kv = ent->spawnArgs.MatchPrefix( va( "def_drop%sAF", type ), kv );
  787. }
  788. // change the skin to hide all the dropped articulated figures
  789. skinName = ent->spawnArgs.GetString( va( "skin_drop%s", type ) );
  790. if ( skinName[0] ) {
  791. skin = declManager->FindSkin( skinName );
  792. ent->SetSkin( skin );
  793. }
  794. }
  795. /*
  796. ================
  797. idAFEntity_Base::Event_SetConstraintPosition
  798. ================
  799. */
  800. void idAFEntity_Base::Event_SetConstraintPosition( const char *name, const idVec3 &pos ) {
  801. af.SetConstraintPosition( name, pos );
  802. }
  803. /*
  804. ===============================================================================
  805. idAFEntity_Gibbable
  806. ===============================================================================
  807. */
  808. const idEventDef EV_Gib( "gib", "s" );
  809. const idEventDef EV_Gibbed( "<gibbed>" );
  810. CLASS_DECLARATION( idAFEntity_Base, idAFEntity_Gibbable )
  811. EVENT( EV_Gib, idAFEntity_Gibbable::Event_Gib )
  812. EVENT( EV_Gibbed, idAFEntity_Base::Event_Remove )
  813. END_CLASS
  814. /*
  815. ================
  816. idAFEntity_Gibbable::idAFEntity_Gibbable
  817. ================
  818. */
  819. idAFEntity_Gibbable::idAFEntity_Gibbable( void ) {
  820. skeletonModel = NULL;
  821. skeletonModelDefHandle = -1;
  822. gibbed = false;
  823. #ifdef _D3XP
  824. wasThrown = false;
  825. #endif
  826. }
  827. /*
  828. ================
  829. idAFEntity_Gibbable::~idAFEntity_Gibbable
  830. ================
  831. */
  832. idAFEntity_Gibbable::~idAFEntity_Gibbable() {
  833. if ( skeletonModelDefHandle != -1 ) {
  834. gameRenderWorld->FreeEntityDef( skeletonModelDefHandle );
  835. skeletonModelDefHandle = -1;
  836. }
  837. }
  838. /*
  839. ================
  840. idAFEntity_Gibbable::Save
  841. ================
  842. */
  843. void idAFEntity_Gibbable::Save( idSaveGame *savefile ) const {
  844. savefile->WriteBool( gibbed );
  845. savefile->WriteBool( combatModel != NULL );
  846. #ifdef _D3XP
  847. savefile->WriteBool( wasThrown );
  848. #endif
  849. }
  850. /*
  851. ================
  852. idAFEntity_Gibbable::Restore
  853. ================
  854. */
  855. void idAFEntity_Gibbable::Restore( idRestoreGame *savefile ) {
  856. bool hasCombatModel;
  857. savefile->ReadBool( gibbed );
  858. savefile->ReadBool( hasCombatModel );
  859. #ifdef _D3XP
  860. savefile->ReadBool( wasThrown );
  861. #endif
  862. InitSkeletonModel();
  863. if ( hasCombatModel ) {
  864. SetCombatModel();
  865. LinkCombat();
  866. }
  867. }
  868. /*
  869. ================
  870. idAFEntity_Gibbable::Spawn
  871. ================
  872. */
  873. void idAFEntity_Gibbable::Spawn( void ) {
  874. InitSkeletonModel();
  875. gibbed = false;
  876. #ifdef _D3XP
  877. wasThrown = false;
  878. #endif
  879. }
  880. /*
  881. ================
  882. idAFEntity_Gibbable::InitSkeletonModel
  883. ================
  884. */
  885. void idAFEntity_Gibbable::InitSkeletonModel( void ) {
  886. const char *modelName;
  887. const idDeclModelDef *modelDef;
  888. skeletonModel = NULL;
  889. skeletonModelDefHandle = -1;
  890. modelName = spawnArgs.GetString( "model_gib" );
  891. modelDef = NULL;
  892. if ( modelName[0] != '\0' ) {
  893. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelName, false ) );
  894. if ( modelDef ) {
  895. skeletonModel = modelDef->ModelHandle();
  896. } else {
  897. skeletonModel = renderModelManager->FindModel( modelName );
  898. }
  899. if ( skeletonModel != NULL && renderEntity.hModel != NULL ) {
  900. if ( skeletonModel->NumJoints() != renderEntity.hModel->NumJoints() ) {
  901. gameLocal.Error( "gib model '%s' has different number of joints than model '%s'",
  902. skeletonModel->Name(), renderEntity.hModel->Name() );
  903. }
  904. }
  905. }
  906. }
  907. /*
  908. ================
  909. idAFEntity_Gibbable::Present
  910. ================
  911. */
  912. void idAFEntity_Gibbable::Present( void ) {
  913. renderEntity_t skeleton;
  914. if ( !gameLocal.isNewFrame ) {
  915. return;
  916. }
  917. // don't present to the renderer if the entity hasn't changed
  918. if ( !( thinkFlags & TH_UPDATEVISUALS ) ) {
  919. return;
  920. }
  921. // update skeleton model
  922. if ( gibbed && !IsHidden() && skeletonModel != NULL ) {
  923. skeleton = renderEntity;
  924. skeleton.hModel = skeletonModel;
  925. // add to refresh list
  926. if ( skeletonModelDefHandle == -1 ) {
  927. skeletonModelDefHandle = gameRenderWorld->AddEntityDef( &skeleton );
  928. } else {
  929. gameRenderWorld->UpdateEntityDef( skeletonModelDefHandle, &skeleton );
  930. }
  931. }
  932. idEntity::Present();
  933. }
  934. /*
  935. ================
  936. idAFEntity_Gibbable::Damage
  937. ================
  938. */
  939. void idAFEntity_Gibbable::Damage( idEntity *inflictor, idEntity *attacker, const idVec3 &dir, const char *damageDefName, const float damageScale, const int location ) {
  940. if ( !fl.takedamage ) {
  941. return;
  942. }
  943. idAFEntity_Base::Damage( inflictor, attacker, dir, damageDefName, damageScale, location );
  944. if ( health < -20 && spawnArgs.GetBool( "gib" ) ) {
  945. Gib( dir, damageDefName );
  946. }
  947. }
  948. #ifdef _D3XP
  949. /*
  950. =====================
  951. idAFEntity_Gibbable::SetThrown
  952. =====================
  953. */
  954. void idAFEntity_Gibbable::SetThrown( bool isThrown ) {
  955. if ( isThrown ) {
  956. int i, num = af.GetPhysics()->GetNumBodies();
  957. for ( i=0; i<num; i++ ) {
  958. idAFBody *body;
  959. body = af.GetPhysics()->GetBody( i );
  960. body->SetClipMask( MASK_MONSTERSOLID );
  961. }
  962. }
  963. wasThrown = isThrown;
  964. }
  965. /*
  966. =====================
  967. idAFEntity_Gibbable::Collide
  968. =====================
  969. */
  970. bool idAFEntity_Gibbable::Collide( const trace_t &collision, const idVec3 &velocity ) {
  971. if ( !gibbed && wasThrown ) {
  972. // Everything gibs (if possible)
  973. if ( spawnArgs.GetBool( "gib" ) ) {
  974. idEntity *ent;
  975. ent = gameLocal.entities[ collision.c.entityNum ];
  976. if ( ent->fl.takedamage ) {
  977. ent->Damage( this, gameLocal.GetLocalPlayer(), collision.c.normal, "damage_thrown_ragdoll", 1.f, CLIPMODEL_ID_TO_JOINT_HANDLE( collision.c.id ) );
  978. }
  979. idVec3 vel = velocity;
  980. vel.NormalizeFast();
  981. Gib( vel, "damage_gib" );
  982. }
  983. }
  984. return idAFEntity_Base::Collide( collision, velocity );
  985. }
  986. #endif
  987. /*
  988. =====================
  989. idAFEntity_Gibbable::SpawnGibs
  990. =====================
  991. */
  992. void idAFEntity_Gibbable::SpawnGibs( const idVec3 &dir, const char *damageDefName ) {
  993. int i;
  994. bool gibNonSolid;
  995. idVec3 entityCenter, velocity;
  996. idList<idEntity *> list;
  997. assert( !gameLocal.isClient );
  998. const idDict *damageDef = gameLocal.FindEntityDefDict( damageDefName );
  999. if ( !damageDef ) {
  1000. gameLocal.Error( "Unknown damageDef '%s'", damageDefName );
  1001. }
  1002. // spawn gib articulated figures
  1003. idAFEntity_Base::DropAFs( this, "gib", &list );
  1004. // spawn gib items
  1005. idMoveableItem::DropItems( this, "gib", &list );
  1006. // blow out the gibs in the given direction away from the center of the entity
  1007. entityCenter = GetPhysics()->GetAbsBounds().GetCenter();
  1008. gibNonSolid = damageDef->GetBool( "gibNonSolid" );
  1009. for ( i = 0; i < list.Num(); i++ ) {
  1010. if ( gibNonSolid ) {
  1011. list[i]->GetPhysics()->SetContents( 0 );
  1012. list[i]->GetPhysics()->SetClipMask( 0 );
  1013. list[i]->GetPhysics()->UnlinkClip();
  1014. list[i]->GetPhysics()->PutToRest();
  1015. } else {
  1016. #ifdef _D3XP
  1017. list[i]->GetPhysics()->SetContents( 0 );
  1018. #else
  1019. list[i]->GetPhysics()->SetContents( CONTENTS_CORPSE );
  1020. #endif
  1021. list[i]->GetPhysics()->SetClipMask( CONTENTS_SOLID );
  1022. velocity = list[i]->GetPhysics()->GetAbsBounds().GetCenter() - entityCenter;
  1023. velocity.NormalizeFast();
  1024. velocity += ( i & 1 ) ? dir : -dir;
  1025. list[i]->GetPhysics()->SetLinearVelocity( velocity * 75.0f );
  1026. }
  1027. #ifdef _D3XP
  1028. // Don't allow grabber to pick up temporary gibs
  1029. list[i]->noGrab = true;
  1030. #endif
  1031. list[i]->GetRenderEntity()->noShadow = true;
  1032. list[i]->GetRenderEntity()->shaderParms[ SHADERPARM_TIME_OF_DEATH ] = gameLocal.time * 0.001f;
  1033. list[i]->PostEventSec( &EV_Remove, 4.0f );
  1034. }
  1035. }
  1036. /*
  1037. ============
  1038. idAFEntity_Gibbable::Gib
  1039. ============
  1040. */
  1041. void idAFEntity_Gibbable::Gib( const idVec3 &dir, const char *damageDefName ) {
  1042. // only gib once
  1043. if ( gibbed ) {
  1044. return;
  1045. }
  1046. #ifdef _D3XP
  1047. // Don't grab this ent after it's been gibbed (and now invisible!)
  1048. noGrab = true;
  1049. #endif
  1050. const idDict *damageDef = gameLocal.FindEntityDefDict( damageDefName );
  1051. if ( !damageDef ) {
  1052. gameLocal.Error( "Unknown damageDef '%s'", damageDefName );
  1053. }
  1054. if ( damageDef->GetBool( "gibNonSolid" ) ) {
  1055. GetAFPhysics()->SetContents( 0 );
  1056. GetAFPhysics()->SetClipMask( 0 );
  1057. GetAFPhysics()->UnlinkClip();
  1058. GetAFPhysics()->PutToRest();
  1059. } else {
  1060. GetAFPhysics()->SetContents( CONTENTS_CORPSE );
  1061. GetAFPhysics()->SetClipMask( CONTENTS_SOLID );
  1062. }
  1063. UnlinkCombat();
  1064. if ( g_bloodEffects.GetBool() ) {
  1065. if ( gameLocal.time > gameLocal.GetGibTime() ) {
  1066. gameLocal.SetGibTime( gameLocal.time + GIB_DELAY );
  1067. SpawnGibs( dir, damageDefName );
  1068. renderEntity.noShadow = true;
  1069. renderEntity.shaderParms[ SHADERPARM_TIME_OF_DEATH ] = gameLocal.time * 0.001f;
  1070. StartSound( "snd_gibbed", SND_CHANNEL_ANY, 0, false, NULL );
  1071. gibbed = true;
  1072. }
  1073. } else {
  1074. gibbed = true;
  1075. }
  1076. PostEventSec( &EV_Gibbed, 4.0f );
  1077. }
  1078. /*
  1079. ============
  1080. idAFEntity_Gibbable::Event_Gib
  1081. ============
  1082. */
  1083. void idAFEntity_Gibbable::Event_Gib( const char *damageDefName ) {
  1084. Gib( idVec3( 0, 0, 1 ), damageDefName );
  1085. }
  1086. /*
  1087. ===============================================================================
  1088. idAFEntity_Generic
  1089. ===============================================================================
  1090. */
  1091. CLASS_DECLARATION( idAFEntity_Gibbable, idAFEntity_Generic )
  1092. EVENT( EV_Activate, idAFEntity_Generic::Event_Activate )
  1093. END_CLASS
  1094. /*
  1095. ================
  1096. idAFEntity_Generic::idAFEntity_Generic
  1097. ================
  1098. */
  1099. idAFEntity_Generic::idAFEntity_Generic( void ) {
  1100. keepRunningPhysics = false;
  1101. }
  1102. /*
  1103. ================
  1104. idAFEntity_Generic::~idAFEntity_Generic
  1105. ================
  1106. */
  1107. idAFEntity_Generic::~idAFEntity_Generic( void ) {
  1108. }
  1109. /*
  1110. ================
  1111. idAFEntity_Generic::Save
  1112. ================
  1113. */
  1114. void idAFEntity_Generic::Save( idSaveGame *savefile ) const {
  1115. savefile->WriteBool( keepRunningPhysics );
  1116. }
  1117. /*
  1118. ================
  1119. idAFEntity_Generic::Restore
  1120. ================
  1121. */
  1122. void idAFEntity_Generic::Restore( idRestoreGame *savefile ) {
  1123. savefile->ReadBool( keepRunningPhysics );
  1124. }
  1125. /*
  1126. ================
  1127. idAFEntity_Generic::Think
  1128. ================
  1129. */
  1130. void idAFEntity_Generic::Think( void ) {
  1131. idAFEntity_Base::Think();
  1132. if ( keepRunningPhysics ) {
  1133. BecomeActive( TH_PHYSICS );
  1134. }
  1135. }
  1136. /*
  1137. ================
  1138. idAFEntity_Generic::Spawn
  1139. ================
  1140. */
  1141. void idAFEntity_Generic::Spawn( void ) {
  1142. if ( !LoadAF() ) {
  1143. gameLocal.Error( "Couldn't load af file on entity '%s'", name.c_str() );
  1144. }
  1145. SetCombatModel();
  1146. SetPhysics( af.GetPhysics() );
  1147. af.GetPhysics()->PutToRest();
  1148. if ( !spawnArgs.GetBool( "nodrop", "0" ) ) {
  1149. af.GetPhysics()->Activate();
  1150. }
  1151. fl.takedamage = true;
  1152. }
  1153. /*
  1154. ================
  1155. idAFEntity_Generic::Event_Activate
  1156. ================
  1157. */
  1158. void idAFEntity_Generic::Event_Activate( idEntity *activator ) {
  1159. float delay;
  1160. idVec3 init_velocity, init_avelocity;
  1161. Show();
  1162. af.GetPhysics()->EnableImpact();
  1163. af.GetPhysics()->Activate();
  1164. spawnArgs.GetVector( "init_velocity", "0 0 0", init_velocity );
  1165. spawnArgs.GetVector( "init_avelocity", "0 0 0", init_avelocity );
  1166. delay = spawnArgs.GetFloat( "init_velocityDelay", "0" );
  1167. if ( delay == 0.0f ) {
  1168. af.GetPhysics()->SetLinearVelocity( init_velocity );
  1169. } else {
  1170. PostEventSec( &EV_SetLinearVelocity, delay, init_velocity );
  1171. }
  1172. delay = spawnArgs.GetFloat( "init_avelocityDelay", "0" );
  1173. if ( delay == 0.0f ) {
  1174. af.GetPhysics()->SetAngularVelocity( init_avelocity );
  1175. } else {
  1176. PostEventSec( &EV_SetAngularVelocity, delay, init_avelocity );
  1177. }
  1178. }
  1179. /*
  1180. ===============================================================================
  1181. idAFEntity_WithAttachedHead
  1182. ===============================================================================
  1183. */
  1184. CLASS_DECLARATION( idAFEntity_Gibbable, idAFEntity_WithAttachedHead )
  1185. EVENT( EV_Gib, idAFEntity_WithAttachedHead::Event_Gib )
  1186. EVENT( EV_Activate, idAFEntity_WithAttachedHead::Event_Activate )
  1187. END_CLASS
  1188. /*
  1189. ================
  1190. idAFEntity_WithAttachedHead::idAFEntity_WithAttachedHead
  1191. ================
  1192. */
  1193. idAFEntity_WithAttachedHead::idAFEntity_WithAttachedHead() {
  1194. head = NULL;
  1195. }
  1196. /*
  1197. ================
  1198. idAFEntity_WithAttachedHead::~idAFEntity_WithAttachedHead
  1199. ================
  1200. */
  1201. idAFEntity_WithAttachedHead::~idAFEntity_WithAttachedHead() {
  1202. if ( head.GetEntity() ) {
  1203. head.GetEntity()->ClearBody();
  1204. head.GetEntity()->PostEventMS( &EV_Remove, 0 );
  1205. }
  1206. }
  1207. /*
  1208. ================
  1209. idAFEntity_WithAttachedHead::Spawn
  1210. ================
  1211. */
  1212. void idAFEntity_WithAttachedHead::Spawn( void ) {
  1213. SetupHead();
  1214. LoadAF();
  1215. SetCombatModel();
  1216. SetPhysics( af.GetPhysics() );
  1217. af.GetPhysics()->PutToRest();
  1218. if ( !spawnArgs.GetBool( "nodrop", "0" ) ) {
  1219. af.GetPhysics()->Activate();
  1220. }
  1221. fl.takedamage = true;
  1222. if ( head.GetEntity() ) {
  1223. int anim = head.GetEntity()->GetAnimator()->GetAnim( "dead" );
  1224. if ( anim ) {
  1225. head.GetEntity()->GetAnimator()->SetFrame( ANIMCHANNEL_ALL, anim, 0, gameLocal.time, 0 );
  1226. }
  1227. }
  1228. }
  1229. /*
  1230. ================
  1231. idAFEntity_WithAttachedHead::Save
  1232. ================
  1233. */
  1234. void idAFEntity_WithAttachedHead::Save( idSaveGame *savefile ) const {
  1235. head.Save( savefile );
  1236. }
  1237. /*
  1238. ================
  1239. idAFEntity_WithAttachedHead::Restore
  1240. ================
  1241. */
  1242. void idAFEntity_WithAttachedHead::Restore( idRestoreGame *savefile ) {
  1243. head.Restore( savefile );
  1244. }
  1245. /*
  1246. ================
  1247. idAFEntity_WithAttachedHead::SetupHead
  1248. ================
  1249. */
  1250. void idAFEntity_WithAttachedHead::SetupHead( void ) {
  1251. idAFAttachment *headEnt;
  1252. idStr jointName;
  1253. const char *headModel;
  1254. jointHandle_t joint;
  1255. idVec3 origin;
  1256. idMat3 axis;
  1257. headModel = spawnArgs.GetString( "def_head", "" );
  1258. if ( headModel[ 0 ] ) {
  1259. jointName = spawnArgs.GetString( "head_joint" );
  1260. joint = animator.GetJointHandle( jointName );
  1261. if ( joint == INVALID_JOINT ) {
  1262. gameLocal.Error( "Joint '%s' not found for 'head_joint' on '%s'", jointName.c_str(), name.c_str() );
  1263. }
  1264. headEnt = static_cast<idAFAttachment *>( gameLocal.SpawnEntityType( idAFAttachment::Type, NULL ) );
  1265. headEnt->SetName( va( "%s_head", name.c_str() ) );
  1266. headEnt->SetBody( this, headModel, joint );
  1267. headEnt->SetCombatModel();
  1268. head = headEnt;
  1269. #ifdef _D3XP
  1270. idStr xSkin;
  1271. if ( spawnArgs.GetString( "skin_head_xray", "", xSkin ) ) {
  1272. headEnt->xraySkin = declManager->FindSkin( xSkin.c_str() );
  1273. headEnt->UpdateModel();
  1274. }
  1275. #endif
  1276. animator.GetJointTransform( joint, gameLocal.time, origin, axis );
  1277. origin = renderEntity.origin + origin * renderEntity.axis;
  1278. headEnt->SetOrigin( origin );
  1279. headEnt->SetAxis( renderEntity.axis );
  1280. headEnt->BindToJoint( this, joint, true );
  1281. }
  1282. }
  1283. /*
  1284. ================
  1285. idAFEntity_WithAttachedHead::Think
  1286. ================
  1287. */
  1288. void idAFEntity_WithAttachedHead::Think( void ) {
  1289. idAFEntity_Base::Think();
  1290. }
  1291. /*
  1292. ================
  1293. idAFEntity_WithAttachedHead::LinkCombat
  1294. ================
  1295. */
  1296. void idAFEntity_WithAttachedHead::LinkCombat( void ) {
  1297. idAFAttachment *headEnt;
  1298. if ( fl.hidden ) {
  1299. return;
  1300. }
  1301. if ( combatModel ) {
  1302. combatModel->Link( gameLocal.clip, this, 0, renderEntity.origin, renderEntity.axis, modelDefHandle );
  1303. }
  1304. headEnt = head.GetEntity();
  1305. if ( headEnt ) {
  1306. headEnt->LinkCombat();
  1307. }
  1308. }
  1309. /*
  1310. ================
  1311. idAFEntity_WithAttachedHead::UnlinkCombat
  1312. ================
  1313. */
  1314. void idAFEntity_WithAttachedHead::UnlinkCombat( void ) {
  1315. idAFAttachment *headEnt;
  1316. if ( combatModel ) {
  1317. combatModel->Unlink();
  1318. }
  1319. headEnt = head.GetEntity();
  1320. if ( headEnt ) {
  1321. headEnt->UnlinkCombat();
  1322. }
  1323. }
  1324. /*
  1325. ================
  1326. idAFEntity_WithAttachedHead::Hide
  1327. ================
  1328. */
  1329. void idAFEntity_WithAttachedHead::Hide( void ) {
  1330. idAFEntity_Base::Hide();
  1331. if ( head.GetEntity() ) {
  1332. head.GetEntity()->Hide();
  1333. }
  1334. UnlinkCombat();
  1335. }
  1336. /*
  1337. ================
  1338. idAFEntity_WithAttachedHead::Show
  1339. ================
  1340. */
  1341. void idAFEntity_WithAttachedHead::Show( void ) {
  1342. idAFEntity_Base::Show();
  1343. if ( head.GetEntity() ) {
  1344. head.GetEntity()->Show();
  1345. }
  1346. LinkCombat();
  1347. }
  1348. /*
  1349. ================
  1350. idAFEntity_WithAttachedHead::ProjectOverlay
  1351. ================
  1352. */
  1353. void idAFEntity_WithAttachedHead::ProjectOverlay( const idVec3 &origin, const idVec3 &dir, float size, const char *material ) {
  1354. idEntity::ProjectOverlay( origin, dir, size, material );
  1355. if ( head.GetEntity() ) {
  1356. head.GetEntity()->ProjectOverlay( origin, dir, size, material );
  1357. }
  1358. }
  1359. /*
  1360. ============
  1361. idAFEntity_WithAttachedHead::Gib
  1362. ============
  1363. */
  1364. void idAFEntity_WithAttachedHead::Gib( const idVec3 &dir, const char *damageDefName ) {
  1365. // only gib once
  1366. if ( gibbed ) {
  1367. return;
  1368. }
  1369. idAFEntity_Gibbable::Gib( dir, damageDefName );
  1370. if ( head.GetEntity() ) {
  1371. head.GetEntity()->Hide();
  1372. }
  1373. }
  1374. /*
  1375. ============
  1376. idAFEntity_WithAttachedHead::Event_Gib
  1377. ============
  1378. */
  1379. void idAFEntity_WithAttachedHead::Event_Gib( const char *damageDefName ) {
  1380. Gib( idVec3( 0, 0, 1 ), damageDefName );
  1381. }
  1382. /*
  1383. ================
  1384. idAFEntity_WithAttachedHead::Event_Activate
  1385. ================
  1386. */
  1387. void idAFEntity_WithAttachedHead::Event_Activate( idEntity *activator ) {
  1388. float delay;
  1389. idVec3 init_velocity, init_avelocity;
  1390. Show();
  1391. af.GetPhysics()->EnableImpact();
  1392. af.GetPhysics()->Activate();
  1393. spawnArgs.GetVector( "init_velocity", "0 0 0", init_velocity );
  1394. spawnArgs.GetVector( "init_avelocity", "0 0 0", init_avelocity );
  1395. delay = spawnArgs.GetFloat( "init_velocityDelay", "0" );
  1396. if ( delay == 0.0f ) {
  1397. af.GetPhysics()->SetLinearVelocity( init_velocity );
  1398. } else {
  1399. PostEventSec( &EV_SetLinearVelocity, delay, init_velocity );
  1400. }
  1401. delay = spawnArgs.GetFloat( "init_avelocityDelay", "0" );
  1402. if ( delay == 0.0f ) {
  1403. af.GetPhysics()->SetAngularVelocity( init_avelocity );
  1404. } else {
  1405. PostEventSec( &EV_SetAngularVelocity, delay, init_avelocity );
  1406. }
  1407. }
  1408. /*
  1409. ===============================================================================
  1410. idAFEntity_Vehicle
  1411. ===============================================================================
  1412. */
  1413. CLASS_DECLARATION( idAFEntity_Base, idAFEntity_Vehicle )
  1414. END_CLASS
  1415. /*
  1416. ================
  1417. idAFEntity_Vehicle::idAFEntity_Vehicle
  1418. ================
  1419. */
  1420. idAFEntity_Vehicle::idAFEntity_Vehicle( void ) {
  1421. player = NULL;
  1422. eyesJoint = INVALID_JOINT;
  1423. steeringWheelJoint = INVALID_JOINT;
  1424. wheelRadius = 0.0f;
  1425. steerAngle = 0.0f;
  1426. steerSpeed = 0.0f;
  1427. dustSmoke = NULL;
  1428. }
  1429. /*
  1430. ================
  1431. idAFEntity_Vehicle::Spawn
  1432. ================
  1433. */
  1434. void idAFEntity_Vehicle::Spawn( void ) {
  1435. const char *eyesJointName = spawnArgs.GetString( "eyesJoint", "eyes" );
  1436. const char *steeringWheelJointName = spawnArgs.GetString( "steeringWheelJoint", "steeringWheel" );
  1437. LoadAF();
  1438. SetCombatModel();
  1439. SetPhysics( af.GetPhysics() );
  1440. fl.takedamage = true;
  1441. if ( !eyesJointName[0] ) {
  1442. gameLocal.Error( "idAFEntity_Vehicle '%s' no eyes joint specified", name.c_str() );
  1443. }
  1444. eyesJoint = animator.GetJointHandle( eyesJointName );
  1445. if ( !steeringWheelJointName[0] ) {
  1446. gameLocal.Error( "idAFEntity_Vehicle '%s' no steering wheel joint specified", name.c_str() );
  1447. }
  1448. steeringWheelJoint = animator.GetJointHandle( steeringWheelJointName );
  1449. spawnArgs.GetFloat( "wheelRadius", "20", wheelRadius );
  1450. spawnArgs.GetFloat( "steerSpeed", "5", steerSpeed );
  1451. player = NULL;
  1452. steerAngle = 0.0f;
  1453. const char *smokeName = spawnArgs.GetString( "smoke_vehicle_dust", "muzzlesmoke" );
  1454. if ( *smokeName != '\0' ) {
  1455. dustSmoke = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, smokeName ) );
  1456. }
  1457. }
  1458. /*
  1459. ================
  1460. idAFEntity_Vehicle::Use
  1461. ================
  1462. */
  1463. void idAFEntity_Vehicle::Use( idPlayer *other ) {
  1464. idVec3 origin;
  1465. idMat3 axis;
  1466. if ( player ) {
  1467. if ( player == other ) {
  1468. other->Unbind();
  1469. player = NULL;
  1470. af.GetPhysics()->SetComeToRest( true );
  1471. }
  1472. }
  1473. else {
  1474. player = other;
  1475. animator.GetJointTransform( eyesJoint, gameLocal.time, origin, axis );
  1476. origin = renderEntity.origin + origin * renderEntity.axis;
  1477. player->GetPhysics()->SetOrigin( origin );
  1478. player->BindToBody( this, 0, true );
  1479. af.GetPhysics()->SetComeToRest( false );
  1480. af.GetPhysics()->Activate();
  1481. }
  1482. }
  1483. /*
  1484. ================
  1485. idAFEntity_Vehicle::GetSteerAngle
  1486. ================
  1487. */
  1488. float idAFEntity_Vehicle::GetSteerAngle( void ) {
  1489. float idealSteerAngle, angleDelta;
  1490. idealSteerAngle = player->usercmd.rightmove * ( 30.0f / 128.0f );
  1491. angleDelta = idealSteerAngle - steerAngle;
  1492. if ( angleDelta > steerSpeed ) {
  1493. steerAngle += steerSpeed;
  1494. } else if ( angleDelta < -steerSpeed ) {
  1495. steerAngle -= steerSpeed;
  1496. } else {
  1497. steerAngle = idealSteerAngle;
  1498. }
  1499. return steerAngle;
  1500. }
  1501. /*
  1502. ===============================================================================
  1503. idAFEntity_VehicleSimple
  1504. ===============================================================================
  1505. */
  1506. CLASS_DECLARATION( idAFEntity_Vehicle, idAFEntity_VehicleSimple )
  1507. END_CLASS
  1508. /*
  1509. ================
  1510. idAFEntity_VehicleSimple::idAFEntity_VehicleSimple
  1511. ================
  1512. */
  1513. idAFEntity_VehicleSimple::idAFEntity_VehicleSimple( void ) {
  1514. int i;
  1515. for ( i = 0; i < 4; i++ ) {
  1516. suspension[i] = NULL;
  1517. }
  1518. }
  1519. /*
  1520. ================
  1521. idAFEntity_VehicleSimple::~idAFEntity_VehicleSimple
  1522. ================
  1523. */
  1524. idAFEntity_VehicleSimple::~idAFEntity_VehicleSimple( void ) {
  1525. delete wheelModel;
  1526. wheelModel = NULL;
  1527. }
  1528. /*
  1529. ================
  1530. idAFEntity_VehicleSimple::Spawn
  1531. ================
  1532. */
  1533. void idAFEntity_VehicleSimple::Spawn( void ) {
  1534. static const char *wheelJointKeys[] = {
  1535. "wheelJointFrontLeft",
  1536. "wheelJointFrontRight",
  1537. "wheelJointRearLeft",
  1538. "wheelJointRearRight"
  1539. };
  1540. static idVec3 wheelPoly[4] = { idVec3( 2, 2, 0 ), idVec3( 2, -2, 0 ), idVec3( -2, -2, 0 ), idVec3( -2, 2, 0 ) };
  1541. int i;
  1542. idVec3 origin;
  1543. idMat3 axis;
  1544. idTraceModel trm;
  1545. trm.SetupPolygon( wheelPoly, 4 );
  1546. trm.Translate( idVec3( 0, 0, -wheelRadius ) );
  1547. wheelModel = new idClipModel( trm );
  1548. for ( i = 0; i < 4; i++ ) {
  1549. const char *wheelJointName = spawnArgs.GetString( wheelJointKeys[i], "" );
  1550. if ( !wheelJointName[0] ) {
  1551. gameLocal.Error( "idAFEntity_VehicleSimple '%s' no '%s' specified", name.c_str(), wheelJointKeys[i] );
  1552. }
  1553. wheelJoints[i] = animator.GetJointHandle( wheelJointName );
  1554. if ( wheelJoints[i] == INVALID_JOINT ) {
  1555. gameLocal.Error( "idAFEntity_VehicleSimple '%s' can't find wheel joint '%s'", name.c_str(), wheelJointName );
  1556. }
  1557. GetAnimator()->GetJointTransform( wheelJoints[i], 0, origin, axis );
  1558. origin = renderEntity.origin + origin * renderEntity.axis;
  1559. suspension[i] = new idAFConstraint_Suspension();
  1560. suspension[i]->Setup( va( "suspension%d", i ), af.GetPhysics()->GetBody( 0 ), origin, af.GetPhysics()->GetAxis( 0 ), wheelModel );
  1561. suspension[i]->SetSuspension( g_vehicleSuspensionUp.GetFloat(),
  1562. g_vehicleSuspensionDown.GetFloat(),
  1563. g_vehicleSuspensionKCompress.GetFloat(),
  1564. g_vehicleSuspensionDamping.GetFloat(),
  1565. g_vehicleTireFriction.GetFloat() );
  1566. af.GetPhysics()->AddConstraint( suspension[i] );
  1567. }
  1568. memset( wheelAngles, 0, sizeof( wheelAngles ) );
  1569. BecomeActive( TH_THINK );
  1570. }
  1571. /*
  1572. ================
  1573. idAFEntity_VehicleSimple::Think
  1574. ================
  1575. */
  1576. void idAFEntity_VehicleSimple::Think( void ) {
  1577. int i;
  1578. float force = 0.0f, velocity = 0.0f, steerAngle = 0.0f;
  1579. idVec3 origin;
  1580. idMat3 axis;
  1581. idRotation wheelRotation, steerRotation;
  1582. if ( thinkFlags & TH_THINK ) {
  1583. if ( player ) {
  1584. // capture the input from a player
  1585. velocity = g_vehicleVelocity.GetFloat();
  1586. if ( player->usercmd.forwardmove < 0 ) {
  1587. velocity = -velocity;
  1588. }
  1589. force = idMath::Fabs( player->usercmd.forwardmove * g_vehicleForce.GetFloat() ) * (1.0f / 128.0f);
  1590. steerAngle = GetSteerAngle();
  1591. }
  1592. // update the wheel motor force and steering
  1593. for ( i = 0; i < 2; i++ ) {
  1594. // front wheel drive
  1595. if ( velocity != 0.0f ) {
  1596. suspension[i]->EnableMotor( true );
  1597. } else {
  1598. suspension[i]->EnableMotor( false );
  1599. }
  1600. suspension[i]->SetMotorVelocity( velocity );
  1601. suspension[i]->SetMotorForce( force );
  1602. // update the wheel steering
  1603. suspension[i]->SetSteerAngle( steerAngle );
  1604. }
  1605. // adjust wheel velocity for better steering because there are no differentials between the wheels
  1606. if ( steerAngle < 0.0f ) {
  1607. suspension[0]->SetMotorVelocity( velocity * 0.5f );
  1608. } else if ( steerAngle > 0.0f ) {
  1609. suspension[1]->SetMotorVelocity( velocity * 0.5f );
  1610. }
  1611. // update suspension with latest cvar settings
  1612. for ( i = 0; i < 4; i++ ) {
  1613. suspension[i]->SetSuspension( g_vehicleSuspensionUp.GetFloat(),
  1614. g_vehicleSuspensionDown.GetFloat(),
  1615. g_vehicleSuspensionKCompress.GetFloat(),
  1616. g_vehicleSuspensionDamping.GetFloat(),
  1617. g_vehicleTireFriction.GetFloat() );
  1618. }
  1619. // run the physics
  1620. RunPhysics();
  1621. // move and rotate the wheels visually
  1622. for ( i = 0; i < 4; i++ ) {
  1623. idAFBody *body = af.GetPhysics()->GetBody( 0 );
  1624. origin = suspension[i]->GetWheelOrigin();
  1625. velocity = body->GetPointVelocity( origin ) * body->GetWorldAxis()[0];
  1626. wheelAngles[i] += velocity * MS2SEC( gameLocal.msec ) / wheelRadius;
  1627. // additional rotation about the wheel axis
  1628. wheelRotation.SetAngle( RAD2DEG( wheelAngles[i] ) );
  1629. wheelRotation.SetVec( 0, -1, 0 );
  1630. if ( i < 2 ) {
  1631. // rotate the wheel for steering
  1632. steerRotation.SetAngle( steerAngle );
  1633. steerRotation.SetVec( 0, 0, 1 );
  1634. // set wheel rotation
  1635. animator.SetJointAxis( wheelJoints[i], JOINTMOD_WORLD, wheelRotation.ToMat3() * steerRotation.ToMat3() );
  1636. } else {
  1637. // set wheel rotation
  1638. animator.SetJointAxis( wheelJoints[i], JOINTMOD_WORLD, wheelRotation.ToMat3() );
  1639. }
  1640. // set wheel position for suspension
  1641. origin = ( origin - renderEntity.origin ) * renderEntity.axis.Transpose();
  1642. GetAnimator()->SetJointPos( wheelJoints[i], JOINTMOD_WORLD_OVERRIDE, origin );
  1643. }
  1644. /*
  1645. // spawn dust particle effects
  1646. if ( force != 0.0f && !( gameLocal.framenum & 7 ) ) {
  1647. int numContacts;
  1648. idAFConstraint_Contact *contacts[2];
  1649. for ( i = 0; i < 4; i++ ) {
  1650. numContacts = af.GetPhysics()->GetBodyContactConstraints( wheels[i]->GetClipModel()->GetId(), contacts, 2 );
  1651. for ( int j = 0; j < numContacts; j++ ) {
  1652. gameLocal.smokeParticles->EmitSmoke( dustSmoke, gameLocal.time, gameLocal.random.RandomFloat(), contacts[j]->GetContact().point, contacts[j]->GetContact().normal.ToMat3() );
  1653. }
  1654. }
  1655. }
  1656. */
  1657. }
  1658. UpdateAnimation();
  1659. if ( thinkFlags & TH_UPDATEVISUALS ) {
  1660. Present();
  1661. LinkCombat();
  1662. }
  1663. }
  1664. /*
  1665. ===============================================================================
  1666. idAFEntity_VehicleFourWheels
  1667. ===============================================================================
  1668. */
  1669. CLASS_DECLARATION( idAFEntity_Vehicle, idAFEntity_VehicleFourWheels )
  1670. END_CLASS
  1671. /*
  1672. ================
  1673. idAFEntity_VehicleFourWheels::idAFEntity_VehicleFourWheels
  1674. ================
  1675. */
  1676. idAFEntity_VehicleFourWheels::idAFEntity_VehicleFourWheels( void ) {
  1677. int i;
  1678. for ( i = 0; i < 4; i++ ) {
  1679. wheels[i] = NULL;
  1680. wheelJoints[i] = INVALID_JOINT;
  1681. wheelAngles[i] = 0.0f;
  1682. }
  1683. steering[0] = NULL;
  1684. steering[1] = NULL;
  1685. }
  1686. /*
  1687. ================
  1688. idAFEntity_VehicleFourWheels::Spawn
  1689. ================
  1690. */
  1691. void idAFEntity_VehicleFourWheels::Spawn( void ) {
  1692. int i;
  1693. static const char *wheelBodyKeys[] = {
  1694. "wheelBodyFrontLeft",
  1695. "wheelBodyFrontRight",
  1696. "wheelBodyRearLeft",
  1697. "wheelBodyRearRight"
  1698. };
  1699. static const char *wheelJointKeys[] = {
  1700. "wheelJointFrontLeft",
  1701. "wheelJointFrontRight",
  1702. "wheelJointRearLeft",
  1703. "wheelJointRearRight"
  1704. };
  1705. static const char *steeringHingeKeys[] = {
  1706. "steeringHingeFrontLeft",
  1707. "steeringHingeFrontRight",
  1708. };
  1709. const char *wheelBodyName, *wheelJointName, *steeringHingeName;
  1710. for ( i = 0; i < 4; i++ ) {
  1711. wheelBodyName = spawnArgs.GetString( wheelBodyKeys[i], "" );
  1712. if ( !wheelBodyName[0] ) {
  1713. gameLocal.Error( "idAFEntity_VehicleFourWheels '%s' no '%s' specified", name.c_str(), wheelBodyKeys[i] );
  1714. }
  1715. wheels[i] = af.GetPhysics()->GetBody( wheelBodyName );
  1716. if ( !wheels[i] ) {
  1717. gameLocal.Error( "idAFEntity_VehicleFourWheels '%s' can't find wheel body '%s'", name.c_str(), wheelBodyName );
  1718. }
  1719. wheelJointName = spawnArgs.GetString( wheelJointKeys[i], "" );
  1720. if ( !wheelJointName[0] ) {
  1721. gameLocal.Error( "idAFEntity_VehicleFourWheels '%s' no '%s' specified", name.c_str(), wheelJointKeys[i] );
  1722. }
  1723. wheelJoints[i] = animator.GetJointHandle( wheelJointName );
  1724. if ( wheelJoints[i] == INVALID_JOINT ) {
  1725. gameLocal.Error( "idAFEntity_VehicleFourWheels '%s' can't find wheel joint '%s'", name.c_str(), wheelJointName );
  1726. }
  1727. }
  1728. for ( i = 0; i < 2; i++ ) {
  1729. steeringHingeName = spawnArgs.GetString( steeringHingeKeys[i], "" );
  1730. if ( !steeringHingeName[0] ) {
  1731. gameLocal.Error( "idAFEntity_VehicleFourWheels '%s' no '%s' specified", name.c_str(), steeringHingeKeys[i] );
  1732. }
  1733. steering[i] = static_cast<idAFConstraint_Hinge *>(af.GetPhysics()->GetConstraint( steeringHingeName ));
  1734. if ( !steering[i] ) {
  1735. gameLocal.Error( "idAFEntity_VehicleFourWheels '%s': can't find steering hinge '%s'", name.c_str(), steeringHingeName );
  1736. }
  1737. }
  1738. memset( wheelAngles, 0, sizeof( wheelAngles ) );
  1739. BecomeActive( TH_THINK );
  1740. }
  1741. /*
  1742. ================
  1743. idAFEntity_VehicleFourWheels::Think
  1744. ================
  1745. */
  1746. void idAFEntity_VehicleFourWheels::Think( void ) {
  1747. int i;
  1748. float force = 0.0f, velocity = 0.0f, steerAngle = 0.0f;
  1749. idVec3 origin;
  1750. idMat3 axis;
  1751. idRotation rotation;
  1752. if ( thinkFlags & TH_THINK ) {
  1753. if ( player ) {
  1754. // capture the input from a player
  1755. velocity = g_vehicleVelocity.GetFloat();
  1756. if ( player->usercmd.forwardmove < 0 ) {
  1757. velocity = -velocity;
  1758. }
  1759. force = idMath::Fabs( player->usercmd.forwardmove * g_vehicleForce.GetFloat() ) * (1.0f / 128.0f);
  1760. steerAngle = GetSteerAngle();
  1761. }
  1762. // update the wheel motor force
  1763. for ( i = 0; i < 2; i++ ) {
  1764. wheels[2+i]->SetContactMotorVelocity( velocity );
  1765. wheels[2+i]->SetContactMotorForce( force );
  1766. }
  1767. // adjust wheel velocity for better steering because there are no differentials between the wheels
  1768. if ( steerAngle < 0.0f ) {
  1769. wheels[2]->SetContactMotorVelocity( velocity * 0.5f );
  1770. }
  1771. else if ( steerAngle > 0.0f ) {
  1772. wheels[3]->SetContactMotorVelocity( velocity * 0.5f );
  1773. }
  1774. // update the wheel steering
  1775. steering[0]->SetSteerAngle( steerAngle );
  1776. steering[1]->SetSteerAngle( steerAngle );
  1777. for ( i = 0; i < 2; i++ ) {
  1778. steering[i]->SetSteerSpeed( 3.0f );
  1779. }
  1780. // update the steering wheel
  1781. animator.GetJointTransform( steeringWheelJoint, gameLocal.time, origin, axis );
  1782. rotation.SetVec( axis[2] );
  1783. rotation.SetAngle( -steerAngle );
  1784. animator.SetJointAxis( steeringWheelJoint, JOINTMOD_WORLD, rotation.ToMat3() );
  1785. // run the physics
  1786. RunPhysics();
  1787. // rotate the wheels visually
  1788. for ( i = 0; i < 4; i++ ) {
  1789. if ( force == 0.0f ) {
  1790. velocity = wheels[i]->GetLinearVelocity() * wheels[i]->GetWorldAxis()[0];
  1791. }
  1792. wheelAngles[i] += velocity * MS2SEC( gameLocal.msec ) / wheelRadius;
  1793. // give the wheel joint an additional rotation about the wheel axis
  1794. rotation.SetAngle( RAD2DEG( wheelAngles[i] ) );
  1795. axis = af.GetPhysics()->GetAxis( 0 );
  1796. rotation.SetVec( (wheels[i]->GetWorldAxis() * axis.Transpose())[2] );
  1797. animator.SetJointAxis( wheelJoints[i], JOINTMOD_WORLD, rotation.ToMat3() );
  1798. }
  1799. // spawn dust particle effects
  1800. if ( force != 0.0f && !( gameLocal.framenum & 7 ) ) {
  1801. int numContacts;
  1802. idAFConstraint_Contact *contacts[2];
  1803. for ( i = 0; i < 4; i++ ) {
  1804. numContacts = af.GetPhysics()->GetBodyContactConstraints( wheels[i]->GetClipModel()->GetId(), contacts, 2 );
  1805. for ( int j = 0; j < numContacts; j++ ) {
  1806. gameLocal.smokeParticles->EmitSmoke( dustSmoke, gameLocal.time, gameLocal.random.RandomFloat(), contacts[j]->GetContact().point, contacts[j]->GetContact().normal.ToMat3(), timeGroup /* D3XP */ );
  1807. }
  1808. }
  1809. }
  1810. }
  1811. UpdateAnimation();
  1812. if ( thinkFlags & TH_UPDATEVISUALS ) {
  1813. Present();
  1814. LinkCombat();
  1815. }
  1816. }
  1817. /*
  1818. ===============================================================================
  1819. idAFEntity_VehicleSixWheels
  1820. ===============================================================================
  1821. */
  1822. CLASS_DECLARATION( idAFEntity_Vehicle, idAFEntity_VehicleSixWheels )
  1823. END_CLASS
  1824. /*
  1825. ================
  1826. idAFEntity_VehicleSixWheels::idAFEntity_VehicleSixWheels
  1827. ================
  1828. */
  1829. idAFEntity_VehicleSixWheels::idAFEntity_VehicleSixWheels( void ) {
  1830. int i;
  1831. for ( i = 0; i < 6; i++ ) {
  1832. wheels[i] = NULL;
  1833. wheelJoints[i] = INVALID_JOINT;
  1834. wheelAngles[i] = 0.0f;
  1835. }
  1836. steering[0] = NULL;
  1837. steering[1] = NULL;
  1838. steering[2] = NULL;
  1839. steering[3] = NULL;
  1840. }
  1841. /*
  1842. ================
  1843. idAFEntity_VehicleSixWheels::Spawn
  1844. ================
  1845. */
  1846. void idAFEntity_VehicleSixWheels::Spawn( void ) {
  1847. int i;
  1848. static const char *wheelBodyKeys[] = {
  1849. "wheelBodyFrontLeft",
  1850. "wheelBodyFrontRight",
  1851. "wheelBodyMiddleLeft",
  1852. "wheelBodyMiddleRight",
  1853. "wheelBodyRearLeft",
  1854. "wheelBodyRearRight"
  1855. };
  1856. static const char *wheelJointKeys[] = {
  1857. "wheelJointFrontLeft",
  1858. "wheelJointFrontRight",
  1859. "wheelJointMiddleLeft",
  1860. "wheelJointMiddleRight",
  1861. "wheelJointRearLeft",
  1862. "wheelJointRearRight"
  1863. };
  1864. static const char *steeringHingeKeys[] = {
  1865. "steeringHingeFrontLeft",
  1866. "steeringHingeFrontRight",
  1867. "steeringHingeRearLeft",
  1868. "steeringHingeRearRight"
  1869. };
  1870. const char *wheelBodyName, *wheelJointName, *steeringHingeName;
  1871. for ( i = 0; i < 6; i++ ) {
  1872. wheelBodyName = spawnArgs.GetString( wheelBodyKeys[i], "" );
  1873. if ( !wheelBodyName[0] ) {
  1874. gameLocal.Error( "idAFEntity_VehicleSixWheels '%s' no '%s' specified", name.c_str(), wheelBodyKeys[i] );
  1875. }
  1876. wheels[i] = af.GetPhysics()->GetBody( wheelBodyName );
  1877. if ( !wheels[i] ) {
  1878. gameLocal.Error( "idAFEntity_VehicleSixWheels '%s' can't find wheel body '%s'", name.c_str(), wheelBodyName );
  1879. }
  1880. wheelJointName = spawnArgs.GetString( wheelJointKeys[i], "" );
  1881. if ( !wheelJointName[0] ) {
  1882. gameLocal.Error( "idAFEntity_VehicleSixWheels '%s' no '%s' specified", name.c_str(), wheelJointKeys[i] );
  1883. }
  1884. wheelJoints[i] = animator.GetJointHandle( wheelJointName );
  1885. if ( wheelJoints[i] == INVALID_JOINT ) {
  1886. gameLocal.Error( "idAFEntity_VehicleSixWheels '%s' can't find wheel joint '%s'", name.c_str(), wheelJointName );
  1887. }
  1888. }
  1889. for ( i = 0; i < 4; i++ ) {
  1890. steeringHingeName = spawnArgs.GetString( steeringHingeKeys[i], "" );
  1891. if ( !steeringHingeName[0] ) {
  1892. gameLocal.Error( "idAFEntity_VehicleSixWheels '%s' no '%s' specified", name.c_str(), steeringHingeKeys[i] );
  1893. }
  1894. steering[i] = static_cast<idAFConstraint_Hinge *>(af.GetPhysics()->GetConstraint( steeringHingeName ));
  1895. if ( !steering[i] ) {
  1896. gameLocal.Error( "idAFEntity_VehicleSixWheels '%s': can't find steering hinge '%s'", name.c_str(), steeringHingeName );
  1897. }
  1898. }
  1899. memset( wheelAngles, 0, sizeof( wheelAngles ) );
  1900. BecomeActive( TH_THINK );
  1901. }
  1902. /*
  1903. ================
  1904. idAFEntity_VehicleSixWheels::Think
  1905. ================
  1906. */
  1907. void idAFEntity_VehicleSixWheels::Think( void ) {
  1908. int i;
  1909. #ifndef _D3XP
  1910. float force = 0.0f, velocity = 0.0f, steerAngle = 0.0f;
  1911. #endif
  1912. idVec3 origin;
  1913. idMat3 axis;
  1914. idRotation rotation;
  1915. if ( thinkFlags & TH_THINK ) {
  1916. if ( player ) {
  1917. // capture the input from a player
  1918. velocity = g_vehicleVelocity.GetFloat();
  1919. if ( player->usercmd.forwardmove < 0 ) {
  1920. velocity = -velocity;
  1921. }
  1922. force = idMath::Fabs( player->usercmd.forwardmove * g_vehicleForce.GetFloat() ) * (1.0f / 128.0f);
  1923. steerAngle = GetSteerAngle();
  1924. }
  1925. // update the wheel motor force
  1926. for ( i = 0; i < 6; i++ ) {
  1927. wheels[i]->SetContactMotorVelocity( velocity );
  1928. wheels[i]->SetContactMotorForce( force );
  1929. }
  1930. // adjust wheel velocity for better steering because there are no differentials between the wheels
  1931. if ( steerAngle < 0.0f ) {
  1932. for ( i = 0; i < 3; i++ ) {
  1933. wheels[(i<<1)]->SetContactMotorVelocity( velocity * 0.5f );
  1934. }
  1935. }
  1936. else if ( steerAngle > 0.0f ) {
  1937. for ( i = 0; i < 3; i++ ) {
  1938. wheels[1+(i<<1)]->SetContactMotorVelocity( velocity * 0.5f );
  1939. }
  1940. }
  1941. // update the wheel steering
  1942. steering[0]->SetSteerAngle( steerAngle );
  1943. steering[1]->SetSteerAngle( steerAngle );
  1944. steering[2]->SetSteerAngle( -steerAngle );
  1945. steering[3]->SetSteerAngle( -steerAngle );
  1946. for ( i = 0; i < 4; i++ ) {
  1947. steering[i]->SetSteerSpeed( 3.0f );
  1948. }
  1949. // update the steering wheel
  1950. animator.GetJointTransform( steeringWheelJoint, gameLocal.time, origin, axis );
  1951. rotation.SetVec( axis[2] );
  1952. rotation.SetAngle( -steerAngle );
  1953. animator.SetJointAxis( steeringWheelJoint, JOINTMOD_WORLD, rotation.ToMat3() );
  1954. // run the physics
  1955. RunPhysics();
  1956. // rotate the wheels visually
  1957. for ( i = 0; i < 6; i++ ) {
  1958. if ( force == 0.0f ) {
  1959. velocity = wheels[i]->GetLinearVelocity() * wheels[i]->GetWorldAxis()[0];
  1960. }
  1961. wheelAngles[i] += velocity * MS2SEC( gameLocal.msec ) / wheelRadius;
  1962. // give the wheel joint an additional rotation about the wheel axis
  1963. rotation.SetAngle( RAD2DEG( wheelAngles[i] ) );
  1964. axis = af.GetPhysics()->GetAxis( 0 );
  1965. rotation.SetVec( (wheels[i]->GetWorldAxis() * axis.Transpose())[2] );
  1966. animator.SetJointAxis( wheelJoints[i], JOINTMOD_WORLD, rotation.ToMat3() );
  1967. }
  1968. // spawn dust particle effects
  1969. if ( force != 0.0f && !( gameLocal.framenum & 7 ) ) {
  1970. int numContacts;
  1971. idAFConstraint_Contact *contacts[2];
  1972. for ( i = 0; i < 6; i++ ) {
  1973. numContacts = af.GetPhysics()->GetBodyContactConstraints( wheels[i]->GetClipModel()->GetId(), contacts, 2 );
  1974. for ( int j = 0; j < numContacts; j++ ) {
  1975. gameLocal.smokeParticles->EmitSmoke( dustSmoke, gameLocal.time, gameLocal.random.RandomFloat(), contacts[j]->GetContact().point, contacts[j]->GetContact().normal.ToMat3(), timeGroup /* D3XP */ );
  1976. }
  1977. }
  1978. }
  1979. }
  1980. UpdateAnimation();
  1981. if ( thinkFlags & TH_UPDATEVISUALS ) {
  1982. Present();
  1983. LinkCombat();
  1984. }
  1985. }
  1986. #ifdef _D3XP
  1987. /*
  1988. ===============================================================================
  1989. idAFEntity_VehicleAutomated
  1990. ===============================================================================
  1991. */
  1992. const idEventDef EV_Vehicle_setVelocity( "setVelocity", "f" );
  1993. const idEventDef EV_Vehicle_setTorque( "setTorque", "f" );
  1994. const idEventDef EV_Vehicle_setSteeringSpeed( "setSteeringSpeed", "f" );
  1995. const idEventDef EV_Vehicle_setWaypoint( "setWaypoint", "e" );
  1996. CLASS_DECLARATION( idAFEntity_VehicleSixWheels, idAFEntity_VehicleAutomated )
  1997. EVENT( EV_PostSpawn, idAFEntity_VehicleAutomated::PostSpawn )
  1998. EVENT( EV_Vehicle_setVelocity, idAFEntity_VehicleAutomated::Event_SetVelocity )
  1999. EVENT( EV_Vehicle_setTorque, idAFEntity_VehicleAutomated::Event_SetTorque )
  2000. EVENT( EV_Vehicle_setSteeringSpeed, idAFEntity_VehicleAutomated::Event_SetSteeringSpeed )
  2001. EVENT( EV_Vehicle_setWaypoint, idAFEntity_VehicleAutomated::Event_SetWayPoint )
  2002. END_CLASS
  2003. /*
  2004. ================
  2005. idAFEntity_VehicleAutomated::Spawn
  2006. ================
  2007. */
  2008. void idAFEntity_VehicleAutomated::Spawn( void ) {
  2009. velocity = force = steerAngle = 0.f;
  2010. currentSteering = steeringSpeed = 0.f;
  2011. originHeight = 0.f;
  2012. waypoint = NULL;
  2013. spawnArgs.GetFloat( "velocity", "150", velocity );
  2014. spawnArgs.GetFloat( "torque", "200000", force );
  2015. spawnArgs.GetFloat( "steeringSpeed", "1", steeringSpeed );
  2016. spawnArgs.GetFloat( "originHeight", "0", originHeight );
  2017. PostEventMS( &EV_PostSpawn, 0 );
  2018. }
  2019. /*
  2020. ================
  2021. idAFEntity_VehicleAutomated::PostSpawn
  2022. ================
  2023. */
  2024. void idAFEntity_VehicleAutomated::PostSpawn( void ) {
  2025. if ( targets.Num() ) {
  2026. waypoint = targets[0].GetEntity();
  2027. }
  2028. }
  2029. /*
  2030. ================
  2031. idAFEntity_VehicleAutomated::Event_SetVelocity
  2032. ================
  2033. */
  2034. void idAFEntity_VehicleAutomated::Event_SetVelocity( float _velocity ) {
  2035. velocity = _velocity;
  2036. }
  2037. /*
  2038. ================
  2039. idAFEntity_VehicleAutomated::Event_SetTorque
  2040. ================
  2041. */
  2042. void idAFEntity_VehicleAutomated::Event_SetTorque( float _torque ) {
  2043. force = _torque;
  2044. }
  2045. /*
  2046. ================
  2047. idAFEntity_VehicleAutomated::Event_SetSteeringSpeed
  2048. ================
  2049. */
  2050. void idAFEntity_VehicleAutomated::Event_SetSteeringSpeed( float _steeringSpeed ) {
  2051. steeringSpeed = _steeringSpeed;
  2052. }
  2053. /*
  2054. ================
  2055. idAFEntity_VehicleAutomated::Event_SetWayPoint
  2056. ================
  2057. */
  2058. void idAFEntity_VehicleAutomated::Event_SetWayPoint( idEntity *_waypoint ) {
  2059. waypoint = _waypoint;
  2060. }
  2061. /*
  2062. ================
  2063. idAFEntity_VehicleAutomated::Think
  2064. ================
  2065. */
  2066. #define HIT_WAYPOINT_THRESHOLD 80.f
  2067. void idAFEntity_VehicleAutomated::Think( void ) {
  2068. // If we don't have a waypoint, coast to a stop
  2069. if ( !waypoint ) {
  2070. velocity = force = steerAngle = 0.f;
  2071. idAFEntity_VehicleSixWheels::Think();
  2072. return;
  2073. }
  2074. idVec3 waypoint_origin, vehicle_origin;
  2075. idVec3 travel_vector;
  2076. float distance_from_waypoint;
  2077. // Set up the vector from the vehicle origin, to the waypoint
  2078. vehicle_origin = GetPhysics()->GetOrigin();
  2079. vehicle_origin.z -= originHeight;
  2080. waypoint_origin = waypoint->GetPhysics()->GetOrigin();
  2081. travel_vector = waypoint_origin - vehicle_origin;
  2082. distance_from_waypoint = travel_vector.Length();
  2083. // Check if we've hit the waypoint (within a certain threshold)
  2084. if ( distance_from_waypoint < HIT_WAYPOINT_THRESHOLD ) {
  2085. idStr callfunc;
  2086. const function_t *func;
  2087. idThread *thread;
  2088. // Waypoints can call script functions
  2089. waypoint->spawnArgs.GetString( "call", "", callfunc );
  2090. if ( callfunc.Length() ) {
  2091. func = gameLocal.program.FindFunction( callfunc );
  2092. if ( func != NULL ) {
  2093. thread = new idThread( func );
  2094. thread->DelayedStart( 0 );
  2095. }
  2096. }
  2097. // Get next waypoint
  2098. if ( waypoint->targets.Num() ) {
  2099. waypoint = waypoint->targets[0].GetEntity();
  2100. } else {
  2101. waypoint = NULL;
  2102. }
  2103. // We are switching waypoints, adjust steering next frame
  2104. idAFEntity_VehicleSixWheels::Think();
  2105. return;
  2106. }
  2107. idAngles vehicle_angles, travel_angles;
  2108. // Get the angles we need to steer towards
  2109. travel_angles = travel_vector.ToAngles().Normalize360();
  2110. vehicle_angles = this->GetPhysics()->GetAxis().ToAngles().Normalize360();
  2111. float delta_yaw;
  2112. // Get the shortest steering angle towards the travel angles
  2113. delta_yaw = vehicle_angles.yaw - travel_angles.yaw;
  2114. if ( idMath::Fabs( delta_yaw ) > 180.f ) {
  2115. if ( delta_yaw > 0 ) {
  2116. delta_yaw = delta_yaw - 360;
  2117. } else {
  2118. delta_yaw = delta_yaw + 360;
  2119. }
  2120. }
  2121. // Maximum steering angle is 35 degrees
  2122. delta_yaw = idMath::ClampFloat( -35.f, 35.f, delta_yaw );
  2123. idealSteering = delta_yaw;
  2124. // Adjust steering incrementally so it doesn't snap to the ideal angle
  2125. if ( idMath::Fabs( (idealSteering - currentSteering) ) > steeringSpeed ) {
  2126. if ( idealSteering > currentSteering ) {
  2127. currentSteering += steeringSpeed;
  2128. } else {
  2129. currentSteering -= steeringSpeed;
  2130. }
  2131. } else {
  2132. currentSteering = idealSteering;
  2133. }
  2134. // DEBUG
  2135. if ( g_vehicleDebug.GetBool() ) {
  2136. gameRenderWorld->DebugBounds( colorRed, idBounds(idVec3(-4,-4,-4),idVec3(4,4,4)), vehicle_origin );
  2137. gameRenderWorld->DebugBounds( colorRed, idBounds(idVec3(-4,-4,-4),idVec3(4,4,4)), waypoint_origin );
  2138. gameRenderWorld->DrawText( waypoint->name.c_str(), waypoint_origin + idVec3(0,0,16), 0.25f, colorYellow, gameLocal.GetLocalPlayer()->viewAxis );
  2139. gameRenderWorld->DebugArrow( colorWhite, vehicle_origin, waypoint_origin, 12.f );
  2140. }
  2141. // Set the final steerAngle for the vehicle
  2142. steerAngle = currentSteering;
  2143. idAFEntity_VehicleSixWheels::Think();
  2144. }
  2145. #endif
  2146. /*
  2147. ===============================================================================
  2148. idAFEntity_SteamPipe
  2149. ===============================================================================
  2150. */
  2151. CLASS_DECLARATION( idAFEntity_Base, idAFEntity_SteamPipe )
  2152. END_CLASS
  2153. /*
  2154. ================
  2155. idAFEntity_SteamPipe::idAFEntity_SteamPipe
  2156. ================
  2157. */
  2158. idAFEntity_SteamPipe::idAFEntity_SteamPipe( void ) {
  2159. steamBody = 0;
  2160. steamForce = 0.0f;
  2161. steamUpForce = 0.0f;
  2162. steamModelDefHandle = -1;
  2163. memset( &steamRenderEntity, 0, sizeof( steamRenderEntity ) );
  2164. }
  2165. /*
  2166. ================
  2167. idAFEntity_SteamPipe::~idAFEntity_SteamPipe
  2168. ================
  2169. */
  2170. idAFEntity_SteamPipe::~idAFEntity_SteamPipe( void ) {
  2171. if ( steamModelDefHandle >= 0 ){
  2172. gameRenderWorld->FreeEntityDef( steamModelDefHandle );
  2173. }
  2174. }
  2175. /*
  2176. ================
  2177. idAFEntity_SteamPipe::Save
  2178. ================
  2179. */
  2180. void idAFEntity_SteamPipe::Save( idSaveGame *savefile ) const {
  2181. }
  2182. /*
  2183. ================
  2184. idAFEntity_SteamPipe::Restore
  2185. ================
  2186. */
  2187. void idAFEntity_SteamPipe::Restore( idRestoreGame *savefile ) {
  2188. Spawn();
  2189. }
  2190. /*
  2191. ================
  2192. idAFEntity_SteamPipe::Spawn
  2193. ================
  2194. */
  2195. void idAFEntity_SteamPipe::Spawn( void ) {
  2196. idVec3 steamDir;
  2197. const char *steamBodyName;
  2198. LoadAF();
  2199. SetCombatModel();
  2200. SetPhysics( af.GetPhysics() );
  2201. fl.takedamage = true;
  2202. steamBodyName = spawnArgs.GetString( "steamBody", "" );
  2203. steamForce = spawnArgs.GetFloat( "steamForce", "2000" );
  2204. steamUpForce = spawnArgs.GetFloat( "steamUpForce", "10" );
  2205. steamDir = af.GetPhysics()->GetAxis( steamBody )[2];
  2206. steamBody = af.GetPhysics()->GetBodyId( steamBodyName );
  2207. force.SetPosition( af.GetPhysics(), steamBody, af.GetPhysics()->GetOrigin( steamBody ) );
  2208. force.SetForce( steamDir * -steamForce );
  2209. InitSteamRenderEntity();
  2210. BecomeActive( TH_THINK );
  2211. }
  2212. /*
  2213. ================
  2214. idAFEntity_SteamPipe::InitSteamRenderEntity
  2215. ================
  2216. */
  2217. void idAFEntity_SteamPipe::InitSteamRenderEntity( void ) {
  2218. const char *temp;
  2219. const idDeclModelDef *modelDef;
  2220. memset( &steamRenderEntity, 0, sizeof( steamRenderEntity ) );
  2221. steamRenderEntity.shaderParms[ SHADERPARM_RED ] = 1.0f;
  2222. steamRenderEntity.shaderParms[ SHADERPARM_GREEN ] = 1.0f;
  2223. steamRenderEntity.shaderParms[ SHADERPARM_BLUE ] = 1.0f;
  2224. modelDef = NULL;
  2225. temp = spawnArgs.GetString ( "model_steam" );
  2226. if ( *temp != '\0' ) {
  2227. if ( !strstr( temp, "." ) ) {
  2228. modelDef = static_cast<const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, temp, false ) );
  2229. if ( modelDef ) {
  2230. steamRenderEntity.hModel = modelDef->ModelHandle();
  2231. }
  2232. }
  2233. if ( !steamRenderEntity.hModel ) {
  2234. steamRenderEntity.hModel = renderModelManager->FindModel( temp );
  2235. }
  2236. if ( steamRenderEntity.hModel ) {
  2237. steamRenderEntity.bounds = steamRenderEntity.hModel->Bounds( &steamRenderEntity );
  2238. } else {
  2239. steamRenderEntity.bounds.Zero();
  2240. }
  2241. steamRenderEntity.origin = af.GetPhysics()->GetOrigin( steamBody );
  2242. steamRenderEntity.axis = af.GetPhysics()->GetAxis( steamBody );
  2243. steamModelDefHandle = gameRenderWorld->AddEntityDef( &steamRenderEntity );
  2244. }
  2245. }
  2246. /*
  2247. ================
  2248. idAFEntity_SteamPipe::Think
  2249. ================
  2250. */
  2251. void idAFEntity_SteamPipe::Think( void ) {
  2252. idVec3 steamDir;
  2253. if ( thinkFlags & TH_THINK ) {
  2254. steamDir.x = gameLocal.random.CRandomFloat() * steamForce;
  2255. steamDir.y = gameLocal.random.CRandomFloat() * steamForce;
  2256. steamDir.z = steamUpForce;
  2257. force.SetForce( steamDir );
  2258. force.Evaluate( gameLocal.time );
  2259. //gameRenderWorld->DebugArrow( colorWhite, af.GetPhysics()->GetOrigin( steamBody ), af.GetPhysics()->GetOrigin( steamBody ) - 10.0f * steamDir, 4 );
  2260. }
  2261. if ( steamModelDefHandle >= 0 ){
  2262. steamRenderEntity.origin = af.GetPhysics()->GetOrigin( steamBody );
  2263. steamRenderEntity.axis = af.GetPhysics()->GetAxis( steamBody );
  2264. gameRenderWorld->UpdateEntityDef( steamModelDefHandle, &steamRenderEntity );
  2265. }
  2266. idAFEntity_Base::Think();
  2267. }
  2268. /*
  2269. ===============================================================================
  2270. idAFEntity_ClawFourFingers
  2271. ===============================================================================
  2272. */
  2273. const idEventDef EV_SetFingerAngle( "setFingerAngle", "f" );
  2274. const idEventDef EV_StopFingers( "stopFingers" );
  2275. CLASS_DECLARATION( idAFEntity_Base, idAFEntity_ClawFourFingers )
  2276. EVENT( EV_SetFingerAngle, idAFEntity_ClawFourFingers::Event_SetFingerAngle )
  2277. EVENT( EV_StopFingers, idAFEntity_ClawFourFingers::Event_StopFingers )
  2278. END_CLASS
  2279. static const char *clawConstraintNames[] = {
  2280. "claw1", "claw2", "claw3", "claw4"
  2281. };
  2282. /*
  2283. ================
  2284. idAFEntity_ClawFourFingers::idAFEntity_ClawFourFingers
  2285. ================
  2286. */
  2287. idAFEntity_ClawFourFingers::idAFEntity_ClawFourFingers( void ) {
  2288. fingers[0] = NULL;
  2289. fingers[1] = NULL;
  2290. fingers[2] = NULL;
  2291. fingers[3] = NULL;
  2292. }
  2293. /*
  2294. ================
  2295. idAFEntity_ClawFourFingers::Save
  2296. ================
  2297. */
  2298. void idAFEntity_ClawFourFingers::Save( idSaveGame *savefile ) const {
  2299. int i;
  2300. for ( i = 0; i < 4; i++ ) {
  2301. fingers[i]->Save( savefile );
  2302. }
  2303. }
  2304. /*
  2305. ================
  2306. idAFEntity_ClawFourFingers::Restore
  2307. ================
  2308. */
  2309. void idAFEntity_ClawFourFingers::Restore( idRestoreGame *savefile ) {
  2310. int i;
  2311. for ( i = 0; i < 4; i++ ) {
  2312. fingers[i] = static_cast<idAFConstraint_Hinge *>(af.GetPhysics()->GetConstraint( clawConstraintNames[i] ));
  2313. fingers[i]->Restore( savefile );
  2314. }
  2315. SetCombatModel();
  2316. LinkCombat();
  2317. }
  2318. /*
  2319. ================
  2320. idAFEntity_ClawFourFingers::Spawn
  2321. ================
  2322. */
  2323. void idAFEntity_ClawFourFingers::Spawn( void ) {
  2324. int i;
  2325. LoadAF();
  2326. SetCombatModel();
  2327. af.GetPhysics()->LockWorldConstraints( true );
  2328. af.GetPhysics()->SetForcePushable( true );
  2329. SetPhysics( af.GetPhysics() );
  2330. fl.takedamage = true;
  2331. for ( i = 0; i < 4; i++ ) {
  2332. fingers[i] = static_cast<idAFConstraint_Hinge *>(af.GetPhysics()->GetConstraint( clawConstraintNames[i] ));
  2333. if ( !fingers[i] ) {
  2334. gameLocal.Error( "idClaw_FourFingers '%s': can't find claw constraint '%s'", name.c_str(), clawConstraintNames[i] );
  2335. }
  2336. }
  2337. }
  2338. /*
  2339. ================
  2340. idAFEntity_ClawFourFingers::Event_SetFingerAngle
  2341. ================
  2342. */
  2343. void idAFEntity_ClawFourFingers::Event_SetFingerAngle( float angle ) {
  2344. int i;
  2345. for ( i = 0; i < 4; i++ ) {
  2346. fingers[i]->SetSteerAngle( angle );
  2347. fingers[i]->SetSteerSpeed( 0.5f );
  2348. }
  2349. af.GetPhysics()->Activate();
  2350. }
  2351. /*
  2352. ================
  2353. idAFEntity_ClawFourFingers::Event_StopFingers
  2354. ================
  2355. */
  2356. void idAFEntity_ClawFourFingers::Event_StopFingers( void ) {
  2357. int i;
  2358. for ( i = 0; i < 4; i++ ) {
  2359. fingers[i]->SetSteerAngle( fingers[i]->GetAngle() );
  2360. }
  2361. }
  2362. /*
  2363. ===============================================================================
  2364. editor support routines
  2365. ===============================================================================
  2366. */
  2367. /*
  2368. ================
  2369. idGameEdit::AF_SpawnEntity
  2370. ================
  2371. */
  2372. bool idGameEdit::AF_SpawnEntity( const char *fileName ) {
  2373. idDict args;
  2374. idPlayer *player;
  2375. idAFEntity_Generic *ent;
  2376. const idDeclAF *af;
  2377. idVec3 org;
  2378. float yaw;
  2379. player = gameLocal.GetLocalPlayer();
  2380. if ( !player || !gameLocal.CheatsOk( false ) ) {
  2381. return false;
  2382. }
  2383. af = static_cast<const idDeclAF *>( declManager->FindType( DECL_AF, fileName ) );
  2384. if ( !af ) {
  2385. return false;
  2386. }
  2387. yaw = player->viewAngles.yaw;
  2388. args.Set( "angle", va( "%f", yaw + 180 ) );
  2389. org = player->GetPhysics()->GetOrigin() + idAngles( 0, yaw, 0 ).ToForward() * 80 + idVec3( 0, 0, 1 );
  2390. args.Set( "origin", org.ToString() );
  2391. args.Set( "spawnclass", "idAFEntity_Generic" );
  2392. if ( af->model[0] ) {
  2393. args.Set( "model", af->model.c_str() );
  2394. } else {
  2395. args.Set( "model", fileName );
  2396. }
  2397. if ( af->skin[0] ) {
  2398. args.Set( "skin", af->skin.c_str() );
  2399. }
  2400. args.Set( "articulatedFigure", fileName );
  2401. args.Set( "nodrop", "1" );
  2402. ent = static_cast<idAFEntity_Generic *>(gameLocal.SpawnEntityType( idAFEntity_Generic::Type, &args));
  2403. // always update this entity
  2404. ent->BecomeActive( TH_THINK );
  2405. ent->KeepRunningPhysics();
  2406. ent->fl.forcePhysicsUpdate = true;
  2407. player->dragEntity.SetSelected( ent );
  2408. return true;
  2409. }
  2410. /*
  2411. ================
  2412. idGameEdit::AF_UpdateEntities
  2413. ================
  2414. */
  2415. void idGameEdit::AF_UpdateEntities( const char *fileName ) {
  2416. idEntity *ent;
  2417. idAFEntity_Base *af;
  2418. idStr name;
  2419. name = fileName;
  2420. name.StripFileExtension();
  2421. // reload any idAFEntity_Generic which uses the given articulated figure file
  2422. for( ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() ) {
  2423. if ( ent->IsType( idAFEntity_Base::Type ) ) {
  2424. af = static_cast<idAFEntity_Base *>(ent);
  2425. if ( name.Icmp( af->GetAFName() ) == 0 ) {
  2426. af->LoadAF();
  2427. af->GetAFPhysics()->PutToRest();
  2428. }
  2429. }
  2430. }
  2431. }
  2432. /*
  2433. ================
  2434. idGameEdit::AF_UndoChanges
  2435. ================
  2436. */
  2437. void idGameEdit::AF_UndoChanges( void ) {
  2438. int i, c;
  2439. idEntity *ent;
  2440. idAFEntity_Base *af;
  2441. idDeclAF *decl;
  2442. c = declManager->GetNumDecls( DECL_AF );
  2443. for ( i = 0; i < c; i++ ) {
  2444. decl = static_cast<idDeclAF *>( const_cast<idDecl *>( declManager->DeclByIndex( DECL_AF, i, false ) ) );
  2445. if ( !decl->modified ) {
  2446. continue;
  2447. }
  2448. decl->Invalidate();
  2449. declManager->FindType( DECL_AF, decl->GetName() );
  2450. // reload all AF entities using the file
  2451. for( ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() ) {
  2452. if ( ent->IsType( idAFEntity_Base::Type ) ) {
  2453. af = static_cast<idAFEntity_Base *>(ent);
  2454. if ( idStr::Icmp( decl->GetName(), af->GetAFName() ) == 0 ) {
  2455. af->LoadAF();
  2456. }
  2457. }
  2458. }
  2459. }
  2460. }
  2461. /*
  2462. ================
  2463. GetJointTransform
  2464. ================
  2465. */
  2466. typedef struct {
  2467. renderEntity_t *ent;
  2468. const idMD5Joint *joints;
  2469. } jointTransformData_t;
  2470. static bool GetJointTransform( void *model, const idJointMat *frame, const char *jointName, idVec3 &origin, idMat3 &axis ) {
  2471. int i;
  2472. jointTransformData_t *data = reinterpret_cast<jointTransformData_t *>(model);
  2473. for ( i = 0; i < data->ent->numJoints; i++ ) {
  2474. if ( data->joints[i].name.Icmp( jointName ) == 0 ) {
  2475. break;
  2476. }
  2477. }
  2478. if ( i >= data->ent->numJoints ) {
  2479. return false;
  2480. }
  2481. origin = frame[i].ToVec3();
  2482. axis = frame[i].ToMat3();
  2483. return true;
  2484. }
  2485. /*
  2486. ================
  2487. GetArgString
  2488. ================
  2489. */
  2490. static const char *GetArgString( const idDict &args, const idDict *defArgs, const char *key ) {
  2491. const char *s;
  2492. s = args.GetString( key );
  2493. if ( !s[0] && defArgs ) {
  2494. s = defArgs->GetString( key );
  2495. }
  2496. return s;
  2497. }
  2498. /*
  2499. ================
  2500. idGameEdit::AF_CreateMesh
  2501. ================
  2502. */
  2503. idRenderModel *idGameEdit::AF_CreateMesh( const idDict &args, idVec3 &meshOrigin, idMat3 &meshAxis, bool &poseIsSet ) {
  2504. int i, jointNum;
  2505. const idDeclAF *af;
  2506. const idDeclAF_Body *fb;
  2507. renderEntity_t ent;
  2508. idVec3 origin, *bodyOrigin, *newBodyOrigin, *modifiedOrigin;
  2509. idMat3 axis, *bodyAxis, *newBodyAxis, *modifiedAxis;
  2510. declAFJointMod_t *jointMod;
  2511. idAngles angles;
  2512. const idDict *defArgs;
  2513. const idKeyValue *arg;
  2514. idStr name;
  2515. jointTransformData_t data;
  2516. const char *classname, *afName, *modelName;
  2517. idRenderModel *md5;
  2518. const idDeclModelDef *modelDef;
  2519. const idMD5Anim *MD5anim;
  2520. const idMD5Joint *MD5joint;
  2521. const idMD5Joint *MD5joints;
  2522. int numMD5joints;
  2523. idJointMat *originalJoints;
  2524. int parentNum;
  2525. poseIsSet = false;
  2526. meshOrigin.Zero();
  2527. meshAxis.Identity();
  2528. classname = args.GetString( "classname" );
  2529. defArgs = gameLocal.FindEntityDefDict( classname );
  2530. // get the articulated figure
  2531. afName = GetArgString( args, defArgs, "articulatedFigure" );
  2532. af = static_cast<const idDeclAF *>( declManager->FindType( DECL_AF, afName ) );
  2533. if ( !af ) {
  2534. return NULL;
  2535. }
  2536. // get the md5 model
  2537. modelName = GetArgString( args, defArgs, "model" );
  2538. modelDef = static_cast< const idDeclModelDef *>( declManager->FindType( DECL_MODELDEF, modelName, false ) );
  2539. if ( !modelDef ) {
  2540. return NULL;
  2541. }
  2542. // make sure model hasn't been purged
  2543. if ( modelDef->ModelHandle() && !modelDef->ModelHandle()->IsLoaded() ) {
  2544. modelDef->ModelHandle()->LoadModel();
  2545. }
  2546. // get the md5
  2547. md5 = modelDef->ModelHandle();
  2548. if ( !md5 || md5->IsDefaultModel() ) {
  2549. return NULL;
  2550. }
  2551. // get the articulated figure pose anim
  2552. int animNum = modelDef->GetAnim( "af_pose" );
  2553. if ( !animNum ) {
  2554. return NULL;
  2555. }
  2556. const idAnim *anim = modelDef->GetAnim( animNum );
  2557. if ( !anim ) {
  2558. return NULL;
  2559. }
  2560. MD5anim = anim->MD5Anim( 0 );
  2561. MD5joints = md5->GetJoints();
  2562. numMD5joints = md5->NumJoints();
  2563. // setup a render entity
  2564. memset( &ent, 0, sizeof( ent ) );
  2565. ent.customSkin = modelDef->GetSkin();
  2566. ent.bounds.Clear();
  2567. ent.numJoints = numMD5joints;
  2568. ent.joints = ( idJointMat * )_alloca16( ent.numJoints * sizeof( *ent.joints ) );
  2569. // create animation from of the af_pose
  2570. ANIM_CreateAnimFrame( md5, MD5anim, ent.numJoints, ent.joints, 1, modelDef->GetVisualOffset(), false );
  2571. // buffers to store the initial origin and axis for each body
  2572. bodyOrigin = (idVec3 *) _alloca16( af->bodies.Num() * sizeof( idVec3 ) );
  2573. bodyAxis = (idMat3 *) _alloca16( af->bodies.Num() * sizeof( idMat3 ) );
  2574. newBodyOrigin = (idVec3 *) _alloca16( af->bodies.Num() * sizeof( idVec3 ) );
  2575. newBodyAxis = (idMat3 *) _alloca16( af->bodies.Num() * sizeof( idMat3 ) );
  2576. // finish the AF positions
  2577. data.ent = &ent;
  2578. data.joints = MD5joints;
  2579. af->Finish( GetJointTransform, ent.joints, &data );
  2580. // get the initial origin and axis for each AF body
  2581. for ( i = 0; i < af->bodies.Num(); i++ ) {
  2582. fb = af->bodies[i];
  2583. if ( fb->modelType == TRM_BONE ) {
  2584. // axis of bone trace model
  2585. axis[2] = fb->v2.ToVec3() - fb->v1.ToVec3();
  2586. axis[2].Normalize();
  2587. axis[2].NormalVectors( axis[0], axis[1] );
  2588. axis[1] = -axis[1];
  2589. } else {
  2590. axis = fb->angles.ToMat3();
  2591. }
  2592. newBodyOrigin[i] = bodyOrigin[i] = fb->origin.ToVec3();
  2593. newBodyAxis[i] = bodyAxis[i] = axis;
  2594. }
  2595. // get any new body transforms stored in the key/value pairs
  2596. for ( arg = args.MatchPrefix( "body ", NULL ); arg; arg = args.MatchPrefix( "body ", arg ) ) {
  2597. name = arg->GetKey();
  2598. name.Strip( "body " );
  2599. for ( i = 0; i < af->bodies.Num(); i++ ) {
  2600. fb = af->bodies[i];
  2601. if ( fb->name.Icmp( name ) == 0 ) {
  2602. break;
  2603. }
  2604. }
  2605. if ( i >= af->bodies.Num() ) {
  2606. continue;
  2607. }
  2608. sscanf( arg->GetValue(), "%f %f %f %f %f %f", &origin.x, &origin.y, &origin.z, &angles.pitch, &angles.yaw, &angles.roll );
  2609. if ( fb->jointName.Icmp( "origin" ) == 0 ) {
  2610. meshAxis = bodyAxis[i].Transpose() * angles.ToMat3();
  2611. meshOrigin = origin - bodyOrigin[i] * meshAxis;
  2612. poseIsSet = true;
  2613. } else {
  2614. newBodyOrigin[i] = origin;
  2615. newBodyAxis[i] = angles.ToMat3();
  2616. }
  2617. }
  2618. // save the original joints
  2619. originalJoints = ( idJointMat * )_alloca16( numMD5joints * sizeof( originalJoints[0] ) );
  2620. memcpy( originalJoints, ent.joints, numMD5joints * sizeof( originalJoints[0] ) );
  2621. // buffer to store the joint mods
  2622. jointMod = (declAFJointMod_t *) _alloca16( numMD5joints * sizeof( declAFJointMod_t ) );
  2623. memset( jointMod, -1, numMD5joints * sizeof( declAFJointMod_t ) );
  2624. modifiedOrigin = (idVec3 *) _alloca16( numMD5joints * sizeof( idVec3 ) );
  2625. memset( modifiedOrigin, 0, numMD5joints * sizeof( idVec3 ) );
  2626. modifiedAxis = (idMat3 *) _alloca16( numMD5joints * sizeof( idMat3 ) );
  2627. memset( modifiedAxis, 0, numMD5joints * sizeof( idMat3 ) );
  2628. // get all the joint modifications
  2629. for ( i = 0; i < af->bodies.Num(); i++ ) {
  2630. fb = af->bodies[i];
  2631. if ( fb->jointName.Icmp( "origin" ) == 0 ) {
  2632. continue;
  2633. }
  2634. for ( jointNum = 0; jointNum < numMD5joints; jointNum++ ) {
  2635. if ( MD5joints[jointNum].name.Icmp( fb->jointName ) == 0 ) {
  2636. break;
  2637. }
  2638. }
  2639. if ( jointNum >= 0 && jointNum < ent.numJoints ) {
  2640. jointMod[ jointNum ] = fb->jointMod;
  2641. modifiedAxis[ jointNum ] = ( bodyAxis[i] * originalJoints[jointNum].ToMat3().Transpose() ).Transpose() * ( newBodyAxis[i] * meshAxis.Transpose() );
  2642. // FIXME: calculate correct modifiedOrigin
  2643. modifiedOrigin[ jointNum ] = originalJoints[ jointNum ].ToVec3();
  2644. }
  2645. }
  2646. // apply joint modifications to the skeleton
  2647. MD5joint = MD5joints + 1;
  2648. for( i = 1; i < numMD5joints; i++, MD5joint++ ) {
  2649. parentNum = MD5joint->parent - MD5joints;
  2650. idMat3 parentAxis = originalJoints[ parentNum ].ToMat3();
  2651. idMat3 localm = originalJoints[i].ToMat3() * parentAxis.Transpose();
  2652. idVec3 localt = ( originalJoints[i].ToVec3() - originalJoints[ parentNum ].ToVec3() ) * parentAxis.Transpose();
  2653. switch( jointMod[i] ) {
  2654. case DECLAF_JOINTMOD_ORIGIN: {
  2655. ent.joints[ i ].SetRotation( localm * ent.joints[ parentNum ].ToMat3() );
  2656. ent.joints[ i ].SetTranslation( modifiedOrigin[ i ] );
  2657. break;
  2658. }
  2659. case DECLAF_JOINTMOD_AXIS: {
  2660. ent.joints[ i ].SetRotation( modifiedAxis[ i ] );
  2661. ent.joints[ i ].SetTranslation( ent.joints[ parentNum ].ToVec3() + localt * ent.joints[ parentNum ].ToMat3() );
  2662. break;
  2663. }
  2664. case DECLAF_JOINTMOD_BOTH: {
  2665. ent.joints[ i ].SetRotation( modifiedAxis[ i ] );
  2666. ent.joints[ i ].SetTranslation( modifiedOrigin[ i ] );
  2667. break;
  2668. }
  2669. default: {
  2670. ent.joints[ i ].SetRotation( localm * ent.joints[ parentNum ].ToMat3() );
  2671. ent.joints[ i ].SetTranslation( ent.joints[ parentNum ].ToVec3() + localt * ent.joints[ parentNum ].ToMat3() );
  2672. break;
  2673. }
  2674. }
  2675. }
  2676. // instantiate a mesh using the joint information from the render entity
  2677. return md5->InstantiateDynamicModel( &ent, NULL, NULL );
  2678. }
  2679. #ifdef _D3XP
  2680. /*
  2681. ===============================================================================
  2682. idHarvestable
  2683. ===============================================================================
  2684. */
  2685. const idEventDef EV_Harvest_SpawnHarvestTrigger( "<spawnHarvestTrigger>", NULL );
  2686. CLASS_DECLARATION( idEntity, idHarvestable )
  2687. EVENT( EV_Harvest_SpawnHarvestTrigger, idHarvestable::Event_SpawnHarvestTrigger )
  2688. EVENT( EV_Touch, idHarvestable::Event_Touch )
  2689. END_CLASS
  2690. idHarvestable::idHarvestable() {
  2691. trigger = NULL;
  2692. parentEnt = NULL;
  2693. }
  2694. idHarvestable::~idHarvestable() {
  2695. if ( trigger ) {
  2696. delete trigger;
  2697. trigger = NULL;
  2698. }
  2699. }
  2700. void idHarvestable::Spawn() {
  2701. startTime = 0;
  2702. spawnArgs.GetFloat( "triggersize", "120", triggersize );
  2703. spawnArgs.GetFloat( "give_delay", "3", giveDelay);
  2704. giveDelay *= 1000;
  2705. given = false;
  2706. removeDelay = spawnArgs.GetFloat( "remove_delay") * 1000.0f;
  2707. fxFollowPlayer = spawnArgs.GetBool("fx_follow_player", "1");
  2708. fxOrient = spawnArgs.GetString("fx_orient");
  2709. }
  2710. void idHarvestable::Init(idEntity* parent) {
  2711. assert(parent);
  2712. parentEnt = parent;
  2713. GetPhysics()->SetOrigin( parent->GetPhysics()->GetOrigin() );
  2714. this->Bind(parent, true);
  2715. //Set the skin of the entity to the harvest skin
  2716. idStr skin = parent->spawnArgs.GetString("skin_harvest", "");
  2717. if(skin.Length()) {
  2718. parent->SetSkin(declManager->FindSkin(skin.c_str()));
  2719. }
  2720. idEntity* head;
  2721. if(parent->IsType(idActor::Type)) {
  2722. idActor* withHead = (idActor*)parent;
  2723. head = withHead->GetHeadEntity();
  2724. }
  2725. if(parent->IsType(idAFEntity_WithAttachedHead::Type)) {
  2726. idAFEntity_WithAttachedHead* withHead = (idAFEntity_WithAttachedHead*)parent;
  2727. head = withHead->head.GetEntity();
  2728. }
  2729. if(head) {
  2730. idStr headskin = parent->spawnArgs.GetString("skin_harvest_head", "");
  2731. if(headskin.Length()) {
  2732. head->SetSkin(declManager->FindSkin(headskin.c_str()));
  2733. }
  2734. }
  2735. idStr sound = parent->spawnArgs.GetString("harvest_sound");
  2736. if(sound.Length() > 0) {
  2737. parent->StartSound( sound.c_str(), SND_CHANNEL_ANY, 0, false, NULL);
  2738. }
  2739. PostEventMS( &EV_Harvest_SpawnHarvestTrigger, 0 );
  2740. }
  2741. void idHarvestable::Save( idSaveGame *savefile ) const {
  2742. savefile->WriteFloat( triggersize );
  2743. savefile->WriteClipModel( trigger );
  2744. savefile->WriteFloat( giveDelay );
  2745. savefile->WriteFloat( removeDelay );
  2746. savefile->WriteBool( given );
  2747. player.Save( savefile );
  2748. savefile->WriteInt( startTime );
  2749. savefile->WriteBool( fxFollowPlayer );
  2750. fx.Save( savefile );
  2751. savefile->WriteString( fxOrient );
  2752. parentEnt.Save(savefile);
  2753. }
  2754. void idHarvestable::Restore( idRestoreGame *savefile ) {
  2755. savefile->ReadFloat( triggersize );
  2756. savefile->ReadClipModel( trigger );
  2757. savefile->ReadFloat( giveDelay );
  2758. savefile->ReadFloat( removeDelay );
  2759. savefile->ReadBool( given );
  2760. player.Restore( savefile );
  2761. savefile->ReadInt( startTime );
  2762. savefile->ReadBool( fxFollowPlayer );
  2763. fx.Restore( savefile );
  2764. savefile->ReadString( fxOrient );
  2765. parentEnt.Restore(savefile);
  2766. }
  2767. void idHarvestable::SetParent(idEntity* parent) {
  2768. parentEnt = parent;
  2769. }
  2770. void idHarvestable::Think() {
  2771. idEntity* parent = parentEnt.GetEntity();
  2772. if(!parent) {
  2773. return;
  2774. }
  2775. //Update the orientation of the box
  2776. if(trigger && parent && !parent->GetPhysics()->IsAtRest()) {
  2777. trigger->Link( gameLocal.clip, this, 0, parent->GetPhysics()->GetOrigin(), parent->GetPhysics()->GetAxis());
  2778. }
  2779. if(startTime && gameLocal.slow.time - startTime > giveDelay && ! given) {
  2780. idPlayer *thePlayer = player.GetEntity();
  2781. thePlayer->Give(spawnArgs.GetString("give_item"), spawnArgs.GetString("give_value"));
  2782. thePlayer->harvest_lock = false;
  2783. given = true;
  2784. }
  2785. if(startTime && gameLocal.slow.time - startTime > removeDelay) {
  2786. parent->PostEventMS( &EV_Remove, 0 );
  2787. PostEventMS( &EV_Remove, 0 );
  2788. }
  2789. if(fxFollowPlayer) {
  2790. idEntityFx* fxEnt = fx.GetEntity();
  2791. if(fxEnt) {
  2792. idMat3 orientAxisLocal;
  2793. if(GetFxOrientationAxis(orientAxisLocal)) {
  2794. //gameRenderWorld->DebugAxis(fxEnt->GetPhysics()->GetOrigin(), orientAxisLocal);
  2795. fxEnt->GetPhysics()->SetAxis(orientAxisLocal);
  2796. }
  2797. }
  2798. }
  2799. }
  2800. /*
  2801. ================
  2802. idAFEntity_Harvest::Gib
  2803. Called when the parent object has been gibbed.
  2804. ================
  2805. */
  2806. void idHarvestable::Gib() {
  2807. //Stop any looping sound that was playing
  2808. idEntity* parent = parentEnt.GetEntity();
  2809. if(parent) {
  2810. idStr sound = parent->spawnArgs.GetString("harvest_sound");
  2811. if(sound.Length() > 0) {
  2812. parent->StopSound(SND_CHANNEL_ANY, false);
  2813. }
  2814. }
  2815. }
  2816. /*
  2817. ================
  2818. idAFEntity_Harvest::BeginBurn
  2819. ================
  2820. */
  2821. void idHarvestable::BeginBurn() {
  2822. idEntity* parent = parentEnt.GetEntity();
  2823. if(!parent) {
  2824. return;
  2825. }
  2826. if(!spawnArgs.GetBool("burn")) {
  2827. return;
  2828. }
  2829. //Switch Skins if the parent would like us to.
  2830. idStr skin = parent->spawnArgs.GetString("skin_harvest_burn", "");
  2831. if(skin.Length()) {
  2832. parent->SetSkin(declManager->FindSkin(skin.c_str()));
  2833. }
  2834. parent->GetRenderEntity()->noShadow = true;
  2835. parent->SetShaderParm( SHADERPARM_TIME_OF_DEATH, gameLocal.slow.time * 0.001f );
  2836. idEntity* head;
  2837. if(parent->IsType(idActor::Type)) {
  2838. idActor* withHead = (idActor*)parent;
  2839. head = withHead->GetHeadEntity();
  2840. }
  2841. if(parent->IsType(idAFEntity_WithAttachedHead::Type)) {
  2842. idAFEntity_WithAttachedHead* withHead = (idAFEntity_WithAttachedHead*)parent;
  2843. head = withHead->head.GetEntity();
  2844. }
  2845. if(head) {
  2846. idStr headskin = parent->spawnArgs.GetString("skin_harvest_burn_head", "");
  2847. if(headskin.Length()) {
  2848. head->SetSkin(declManager->FindSkin(headskin.c_str()));
  2849. }
  2850. head->GetRenderEntity()->noShadow = true;
  2851. head->SetShaderParm( SHADERPARM_TIME_OF_DEATH, gameLocal.slow.time * 0.001f );
  2852. }
  2853. }
  2854. /*
  2855. ================
  2856. idAFEntity_Harvest::BeginFX
  2857. ================
  2858. */
  2859. void idHarvestable::BeginFX() {
  2860. if(strlen(spawnArgs.GetString("fx")) <= 0) {
  2861. return;
  2862. }
  2863. idMat3* orientAxis = NULL;
  2864. idMat3 orientAxisLocal;
  2865. if(GetFxOrientationAxis(orientAxisLocal)) {
  2866. orientAxis = &orientAxisLocal;
  2867. }
  2868. fx = idEntityFx::StartFx( spawnArgs.GetString("fx"), NULL, orientAxis, this, spawnArgs.GetBool("fx_bind") );
  2869. }
  2870. /*
  2871. ================
  2872. idAFEntity_Harvest::CalcTriggerBounds
  2873. ================
  2874. */
  2875. void idHarvestable::CalcTriggerBounds( float size, idBounds &bounds ) {
  2876. idEntity* parent = parentEnt.GetEntity();
  2877. if(!parent) {
  2878. return;
  2879. }
  2880. //Simple trigger bounds is the absolute bounds of the AF plus a defined size
  2881. bounds = parent->GetPhysics()->GetAbsBounds();
  2882. bounds.ExpandSelf(size);
  2883. bounds[0] -= parent->GetPhysics()->GetOrigin();
  2884. bounds[1] -= parent->GetPhysics()->GetOrigin();
  2885. }
  2886. bool idHarvestable::GetFxOrientationAxis(idMat3& mat) {
  2887. idEntity* parent = parentEnt.GetEntity();
  2888. if(!parent) {
  2889. return false;
  2890. }
  2891. idPlayer *thePlayer = player.GetEntity();
  2892. if(!fxOrient.Icmp("up")) {
  2893. //Orient up
  2894. idVec3 grav = parent->GetPhysics()->GetGravityNormal()*-1;
  2895. idVec3 left, up;
  2896. grav.OrthogonalBasis(left, up);
  2897. idMat3 temp(left.x, left.y, left.z, up.x, up.y, up.z, grav.x, grav.y, grav.z);
  2898. mat = temp;
  2899. return true;
  2900. } else if(!fxOrient.Icmp("weapon")) {
  2901. //Orient the fx towards the muzzle of the weapon
  2902. jointHandle_t joint;
  2903. idVec3 joint_origin;
  2904. idMat3 joint_axis;
  2905. joint = thePlayer->weapon.GetEntity()->GetAnimator()->GetJointHandle( spawnArgs.GetString("fx_weapon_joint") );
  2906. if ( joint != INVALID_JOINT ) {
  2907. thePlayer->weapon.GetEntity()->GetJointWorldTransform( joint, gameLocal.slow.time, joint_origin, joint_axis );
  2908. } else {
  2909. joint_origin = thePlayer->GetPhysics()->GetOrigin();
  2910. }
  2911. idVec3 toPlayer = joint_origin-parent->GetPhysics()->GetOrigin();
  2912. toPlayer.NormalizeFast();
  2913. idVec3 left, up;
  2914. toPlayer.OrthogonalBasis(left, up);
  2915. idMat3 temp(left.x, left.y, left.z, up.x, up.y, up.z, toPlayer.x, toPlayer.y, toPlayer.z);
  2916. mat = temp;
  2917. return true;
  2918. } else if(!fxOrient.Icmp("player")) {
  2919. //Orient the fx towards the eye of the player
  2920. idVec3 eye = thePlayer->GetEyePosition();
  2921. idVec3 toPlayer = eye-parent->GetPhysics()->GetOrigin();
  2922. toPlayer.Normalize();
  2923. idVec3 left, up;
  2924. up.Set(0, 1, 0);
  2925. left = toPlayer.Cross(up);
  2926. up = left.Cross(toPlayer);
  2927. //common->Printf("%.2f %.2f %.2f - %.2f %.2f %.2f - %.2f %.2f %.2f\n", toPlayer.x, toPlayer.y, toPlayer.z, left.x, left.y, left.z, up.x, up.y, up.z );
  2928. idMat3 temp(left.x, left.y, left.z, up.x, up.y, up.z, toPlayer.x, toPlayer.y, toPlayer.z);
  2929. mat = temp;
  2930. return true;
  2931. }
  2932. //Returning false indicates that the orientation is not used;
  2933. return false;
  2934. }
  2935. /*
  2936. ================
  2937. idAFEntity_Harvest::Event_SpawnHarvestTrigger
  2938. ================
  2939. */
  2940. void idHarvestable::Event_SpawnHarvestTrigger( void ) {
  2941. idBounds bounds;
  2942. idEntity* parent = parentEnt.GetEntity();
  2943. if(!parent) {
  2944. return;
  2945. }
  2946. CalcTriggerBounds( triggersize, bounds );
  2947. // create a trigger clip model
  2948. trigger = new idClipModel( idTraceModel( bounds ) );
  2949. trigger->Link( gameLocal.clip, this, 255, parent->GetPhysics()->GetOrigin(), mat3_identity);
  2950. trigger->SetContents( CONTENTS_TRIGGER );
  2951. startTime = 0;
  2952. }
  2953. /*
  2954. ================
  2955. idAFEntity_Harvest::Event_Touch
  2956. ================
  2957. */
  2958. void idHarvestable::Event_Touch( idEntity *other, trace_t *trace ) {
  2959. idEntity* parent = parentEnt.GetEntity();
  2960. if(!parent) {
  2961. return;
  2962. }
  2963. if(parent->IsType(idAFEntity_Gibbable::Type)) {
  2964. idAFEntity_Gibbable* gibParent = (idAFEntity_Gibbable*)parent;
  2965. if(gibParent->IsGibbed())
  2966. return;
  2967. }
  2968. if(!startTime && other && other->IsType(idPlayer::Type)) {
  2969. idPlayer *thePlayer = static_cast<idPlayer *>(other);
  2970. if(thePlayer->harvest_lock) {
  2971. //Don't harvest if the player is in mid harvest
  2972. return;
  2973. }
  2974. player = thePlayer;
  2975. bool okToGive = true;
  2976. idStr requiredWeapons = spawnArgs.GetString("required_weapons");
  2977. if(requiredWeapons.Length() > 0) {
  2978. idStr playerWeap = thePlayer->GetCurrentWeapon();
  2979. if(playerWeap.Length() == 0 || requiredWeapons.Find(playerWeap, false) == -1) {
  2980. okToGive = false;
  2981. }
  2982. }
  2983. if(okToGive) {
  2984. if(thePlayer->CanGive(spawnArgs.GetString("give_item"), spawnArgs.GetString("give_value"))) {
  2985. startTime = gameLocal.slow.time;
  2986. //Lock the player from harvesting to prevent multiple harvests when only one is needed
  2987. thePlayer->harvest_lock = true;
  2988. idWeapon* weap = (idWeapon*)thePlayer->weapon.GetEntity();
  2989. if(weap) {
  2990. //weap->PostEventMS(&EV_Weapon_State, 0, "Charge", 8);
  2991. weap->ProcessEvent(&EV_Weapon_State, "Charge", 8);
  2992. }
  2993. BeginBurn();
  2994. BeginFX();
  2995. //Stop any looping sound that was playing
  2996. idStr sound = parent->spawnArgs.GetString("harvest_sound");
  2997. if(sound.Length() > 0) {
  2998. parent->StopSound(SND_CHANNEL_ANY, false);
  2999. }
  3000. //Make the parent object non-solid
  3001. parent->GetPhysics()->SetContents( 0 );
  3002. parent->GetPhysics()->GetClipModel()->Unlink();
  3003. //Turn of the trigger so it doesn't process twice
  3004. trigger->SetContents( 0 );
  3005. }
  3006. }
  3007. }
  3008. }
  3009. /*
  3010. ===============================================================================
  3011. idAFEntity_Harvest
  3012. ===============================================================================
  3013. */
  3014. const idEventDef EV_Harvest_SpawnHarvestEntity( "<spawnHarvestEntity>", NULL );
  3015. CLASS_DECLARATION( idAFEntity_WithAttachedHead, idAFEntity_Harvest )
  3016. EVENT( EV_Harvest_SpawnHarvestEntity, idAFEntity_Harvest::Event_SpawnHarvestEntity )
  3017. END_CLASS
  3018. /*
  3019. ================
  3020. idAFEntity_Harvest::idAFEntity_Harvest
  3021. ================
  3022. */
  3023. idAFEntity_Harvest::idAFEntity_Harvest() {
  3024. harvestEnt = NULL;
  3025. }
  3026. /*
  3027. ================
  3028. idAFEntity_Harvest::~idAFEntity_Harvest
  3029. ================
  3030. */
  3031. idAFEntity_Harvest::~idAFEntity_Harvest() {
  3032. if ( harvestEnt.GetEntity() ) {
  3033. harvestEnt.GetEntity()->PostEventMS( &EV_Remove, 0 );
  3034. }
  3035. }
  3036. /*
  3037. ================
  3038. idAFEntity_Harvest::Save
  3039. ================
  3040. */
  3041. void idAFEntity_Harvest::Save( idSaveGame *savefile ) const {
  3042. harvestEnt.Save(savefile);
  3043. }
  3044. /*
  3045. ================
  3046. idAFEntity_Harvest::Restore
  3047. ================
  3048. */
  3049. void idAFEntity_Harvest::Restore( idRestoreGame *savefile ) {
  3050. harvestEnt.Restore(savefile);
  3051. //if(harvestEnt.GetEntity()) {
  3052. // harvestEnt.GetEntity()->SetParent(this);
  3053. //}
  3054. }
  3055. /*
  3056. ================
  3057. idAFEntity_Harvest::Spawn
  3058. ================
  3059. */
  3060. void idAFEntity_Harvest::Spawn( void ) {
  3061. PostEventMS( &EV_Harvest_SpawnHarvestEntity, 0 );
  3062. }
  3063. /*
  3064. ================
  3065. idAFEntity_Harvest::Think
  3066. ================
  3067. */
  3068. void idAFEntity_Harvest::Think( void ) {
  3069. idAFEntity_WithAttachedHead::Think();
  3070. }
  3071. void idAFEntity_Harvest::Event_SpawnHarvestEntity( void ) {
  3072. const idDict *harvestDef = gameLocal.FindEntityDefDict( spawnArgs.GetString("def_harvest_type"), false );
  3073. if ( harvestDef ) {
  3074. idEntity *temp;
  3075. gameLocal.SpawnEntityDef( *harvestDef, &temp, false );
  3076. harvestEnt = static_cast<idHarvestable *>(temp);
  3077. }
  3078. if(harvestEnt.GetEntity()) {
  3079. //Let the harvest entity set itself up
  3080. harvestEnt.GetEntity()->Init(this);
  3081. harvestEnt.GetEntity()->BecomeActive( TH_THINK );
  3082. }
  3083. }
  3084. void idAFEntity_Harvest::Gib( const idVec3 &dir, const char *damageDefName ) {
  3085. if(harvestEnt.GetEntity()) {
  3086. //Let the harvest ent know that we gibbed
  3087. harvestEnt.GetEntity()->Gib();
  3088. }
  3089. idAFEntity_WithAttachedHead::Gib(dir, damageDefName);
  3090. }
  3091. #endif