AI.cpp 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition 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 BFG Edition 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 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition 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 BFG Edition 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. #pragma hdrstop
  21. #include "../../idlib/precompiled.h"
  22. #include "../Game_local.h"
  23. static const char *moveCommandString[ NUM_MOVE_COMMANDS ] = {
  24. "MOVE_NONE",
  25. "MOVE_FACE_ENEMY",
  26. "MOVE_FACE_ENTITY",
  27. "MOVE_TO_ENEMY",
  28. "MOVE_TO_ENEMYHEIGHT",
  29. "MOVE_TO_ENTITY",
  30. "MOVE_OUT_OF_RANGE",
  31. "MOVE_TO_ATTACK_POSITION",
  32. "MOVE_TO_COVER",
  33. "MOVE_TO_POSITION",
  34. "MOVE_TO_POSITION_DIRECT",
  35. "MOVE_SLIDE_TO_POSITION",
  36. "MOVE_WANDER"
  37. };
  38. /*
  39. =====================
  40. idMoveState::idMoveState
  41. =====================
  42. */
  43. idMoveState::idMoveState() {
  44. moveType = MOVETYPE_ANIM;
  45. moveCommand = MOVE_NONE;
  46. moveStatus = MOVE_STATUS_DONE;
  47. moveDest.Zero();
  48. moveDir.Set( 1.0f, 0.0f, 0.0f );
  49. goalEntity = NULL;
  50. goalEntityOrigin.Zero();
  51. toAreaNum = 0;
  52. startTime = 0;
  53. duration = 0;
  54. speed = 0.0f;
  55. range = 0.0f;
  56. wanderYaw = 0;
  57. nextWanderTime = 0;
  58. blockTime = 0;
  59. obstacle = NULL;
  60. lastMoveOrigin = vec3_origin;
  61. lastMoveTime = 0;
  62. anim = 0;
  63. }
  64. /*
  65. =====================
  66. idMoveState::Save
  67. =====================
  68. */
  69. void idMoveState::Save( idSaveGame *savefile ) const {
  70. savefile->WriteInt( (int)moveType );
  71. savefile->WriteInt( (int)moveCommand );
  72. savefile->WriteInt( (int)moveStatus );
  73. savefile->WriteVec3( moveDest );
  74. savefile->WriteVec3( moveDir );
  75. goalEntity.Save( savefile );
  76. savefile->WriteVec3( goalEntityOrigin );
  77. savefile->WriteInt( toAreaNum );
  78. savefile->WriteInt( startTime );
  79. savefile->WriteInt( duration );
  80. savefile->WriteFloat( speed );
  81. savefile->WriteFloat( range );
  82. savefile->WriteFloat( wanderYaw );
  83. savefile->WriteInt( nextWanderTime );
  84. savefile->WriteInt( blockTime );
  85. obstacle.Save( savefile );
  86. savefile->WriteVec3( lastMoveOrigin );
  87. savefile->WriteInt( lastMoveTime );
  88. savefile->WriteInt( anim );
  89. }
  90. /*
  91. =====================
  92. idMoveState::Restore
  93. =====================
  94. */
  95. void idMoveState::Restore( idRestoreGame *savefile ) {
  96. savefile->ReadInt( (int &)moveType );
  97. savefile->ReadInt( (int &)moveCommand );
  98. savefile->ReadInt( (int &)moveStatus );
  99. savefile->ReadVec3( moveDest );
  100. savefile->ReadVec3( moveDir );
  101. goalEntity.Restore( savefile );
  102. savefile->ReadVec3( goalEntityOrigin );
  103. savefile->ReadInt( toAreaNum );
  104. savefile->ReadInt( startTime );
  105. savefile->ReadInt( duration );
  106. savefile->ReadFloat( speed );
  107. savefile->ReadFloat( range );
  108. savefile->ReadFloat( wanderYaw );
  109. savefile->ReadInt( nextWanderTime );
  110. savefile->ReadInt( blockTime );
  111. obstacle.Restore( savefile );
  112. savefile->ReadVec3( lastMoveOrigin );
  113. savefile->ReadInt( lastMoveTime );
  114. savefile->ReadInt( anim );
  115. }
  116. /*
  117. ============
  118. idAASFindCover::idAASFindCover
  119. ============
  120. */
  121. idAASFindCover::idAASFindCover( const idVec3 &hideFromPos ) {
  122. int numPVSAreas;
  123. idBounds bounds( hideFromPos - idVec3( 16, 16, 0 ), hideFromPos + idVec3( 16, 16, 64 ) );
  124. // setup PVS
  125. numPVSAreas = gameLocal.pvs.GetPVSAreas( bounds, PVSAreas, idEntity::MAX_PVS_AREAS );
  126. hidePVS = gameLocal.pvs.SetupCurrentPVS( PVSAreas, numPVSAreas );
  127. }
  128. /*
  129. ============
  130. idAASFindCover::~idAASFindCover
  131. ============
  132. */
  133. idAASFindCover::~idAASFindCover() {
  134. gameLocal.pvs.FreeCurrentPVS( hidePVS );
  135. }
  136. /*
  137. ============
  138. idAASFindCover::TestArea
  139. ============
  140. */
  141. bool idAASFindCover::TestArea( const idAAS *aas, int areaNum ) {
  142. idVec3 areaCenter;
  143. int numPVSAreas;
  144. int PVSAreas[ idEntity::MAX_PVS_AREAS ];
  145. areaCenter = aas->AreaCenter( areaNum );
  146. areaCenter[ 2 ] += 1.0f;
  147. numPVSAreas = gameLocal.pvs.GetPVSAreas( idBounds( areaCenter ).Expand( 16.0f ), PVSAreas, idEntity::MAX_PVS_AREAS );
  148. if ( !gameLocal.pvs.InCurrentPVS( hidePVS, PVSAreas, numPVSAreas ) ) {
  149. return true;
  150. }
  151. return false;
  152. }
  153. /*
  154. ============
  155. idAASFindAreaOutOfRange::idAASFindAreaOutOfRange
  156. ============
  157. */
  158. idAASFindAreaOutOfRange::idAASFindAreaOutOfRange( const idVec3 &targetPos, float maxDist ) {
  159. this->targetPos = targetPos;
  160. this->maxDistSqr = maxDist * maxDist;
  161. }
  162. /*
  163. ============
  164. idAASFindAreaOutOfRange::TestArea
  165. ============
  166. */
  167. bool idAASFindAreaOutOfRange::TestArea( const idAAS *aas, int areaNum ) {
  168. const idVec3 &areaCenter = aas->AreaCenter( areaNum );
  169. trace_t trace;
  170. float dist;
  171. dist = ( targetPos.ToVec2() - areaCenter.ToVec2() ).LengthSqr();
  172. if ( ( maxDistSqr > 0.0f ) && ( dist < maxDistSqr ) ) {
  173. return false;
  174. }
  175. gameLocal.clip.TracePoint( trace, targetPos, areaCenter + idVec3( 0.0f, 0.0f, 1.0f ), MASK_OPAQUE, NULL );
  176. if ( trace.fraction < 1.0f ) {
  177. return false;
  178. }
  179. return true;
  180. }
  181. /*
  182. ============
  183. idAASFindAttackPosition::idAASFindAttackPosition
  184. ============
  185. */
  186. idAASFindAttackPosition::idAASFindAttackPosition( const idAI *self, const idMat3 &gravityAxis, idEntity *target, const idVec3 &targetPos, const idVec3 &fireOffset ) {
  187. int numPVSAreas;
  188. this->target = target;
  189. this->targetPos = targetPos;
  190. this->fireOffset = fireOffset;
  191. this->self = self;
  192. this->gravityAxis = gravityAxis;
  193. excludeBounds = idBounds( idVec3( -64.0, -64.0f, -8.0f ), idVec3( 64.0, 64.0f, 64.0f ) );
  194. excludeBounds.TranslateSelf( self->GetPhysics()->GetOrigin() );
  195. // setup PVS
  196. idBounds bounds( targetPos - idVec3( 16, 16, 0 ), targetPos + idVec3( 16, 16, 64 ) );
  197. numPVSAreas = gameLocal.pvs.GetPVSAreas( bounds, PVSAreas, idEntity::MAX_PVS_AREAS );
  198. targetPVS = gameLocal.pvs.SetupCurrentPVS( PVSAreas, numPVSAreas );
  199. }
  200. /*
  201. ============
  202. idAASFindAttackPosition::~idAASFindAttackPosition
  203. ============
  204. */
  205. idAASFindAttackPosition::~idAASFindAttackPosition() {
  206. gameLocal.pvs.FreeCurrentPVS( targetPVS );
  207. }
  208. /*
  209. ============
  210. idAASFindAttackPosition::TestArea
  211. ============
  212. */
  213. bool idAASFindAttackPosition::TestArea( const idAAS *aas, int areaNum ) {
  214. idVec3 dir;
  215. idVec3 local_dir;
  216. idVec3 fromPos;
  217. idMat3 axis;
  218. idVec3 areaCenter;
  219. int numPVSAreas;
  220. int PVSAreas[ idEntity::MAX_PVS_AREAS ];
  221. areaCenter = aas->AreaCenter( areaNum );
  222. areaCenter[ 2 ] += 1.0f;
  223. if ( excludeBounds.ContainsPoint( areaCenter ) ) {
  224. // too close to where we already are
  225. return false;
  226. }
  227. numPVSAreas = gameLocal.pvs.GetPVSAreas( idBounds( areaCenter ).Expand( 16.0f ), PVSAreas, idEntity::MAX_PVS_AREAS );
  228. if ( !gameLocal.pvs.InCurrentPVS( targetPVS, PVSAreas, numPVSAreas ) ) {
  229. return false;
  230. }
  231. // calculate the world transform of the launch position
  232. dir = targetPos - areaCenter;
  233. gravityAxis.ProjectVector( dir, local_dir );
  234. local_dir.z = 0.0f;
  235. local_dir.ToVec2().Normalize();
  236. axis = local_dir.ToMat3();
  237. fromPos = areaCenter + fireOffset * axis;
  238. return self->GetAimDir( fromPos, target, self, dir );
  239. }
  240. /*
  241. =====================
  242. idAI::idAI
  243. =====================
  244. */
  245. idAI::idAI() {
  246. aas = NULL;
  247. travelFlags = TFL_WALK|TFL_AIR;
  248. kickForce = 2048.0f;
  249. ignore_obstacles = false;
  250. blockedRadius = 0.0f;
  251. blockedMoveTime = 750;
  252. blockedAttackTime = 750;
  253. turnRate = 360.0f;
  254. turnVel = 0.0f;
  255. anim_turn_yaw = 0.0f;
  256. anim_turn_amount = 0.0f;
  257. anim_turn_angles = 0.0f;
  258. fly_offset = 0;
  259. fly_seek_scale = 1.0f;
  260. fly_roll_scale = 0.0f;
  261. fly_roll_max = 0.0f;
  262. fly_roll = 0.0f;
  263. fly_pitch_scale = 0.0f;
  264. fly_pitch_max = 0.0f;
  265. fly_pitch = 0.0f;
  266. allowMove = false;
  267. allowHiddenMovement = false;
  268. fly_speed = 0.0f;
  269. fly_bob_strength = 0.0f;
  270. fly_bob_vert = 0.0f;
  271. fly_bob_horz = 0.0f;
  272. lastHitCheckResult = false;
  273. lastHitCheckTime = 0;
  274. lastAttackTime = 0;
  275. melee_range = 0.0f;
  276. projectile_height_to_distance_ratio = 1.0f;
  277. projectileDef = NULL;
  278. projectile = NULL;
  279. projectileClipModel = NULL;
  280. projectileRadius = 0.0f;
  281. projectileVelocity = vec3_origin;
  282. projectileGravity = vec3_origin;
  283. projectileSpeed = 0.0f;
  284. chat_snd = NULL;
  285. chat_min = 0;
  286. chat_max = 0;
  287. chat_time = 0;
  288. talk_state = TALK_NEVER;
  289. talkTarget = NULL;
  290. particles.Clear();
  291. restartParticles = true;
  292. useBoneAxis = false;
  293. wakeOnFlashlight = false;
  294. memset( &worldMuzzleFlash, 0, sizeof ( worldMuzzleFlash ) );
  295. worldMuzzleFlashHandle = -1;
  296. enemy = NULL;
  297. lastVisibleEnemyPos.Zero();
  298. lastVisibleEnemyEyeOffset.Zero();
  299. lastVisibleReachableEnemyPos.Zero();
  300. lastReachableEnemyPos.Zero();
  301. fl.neverDormant = false; // AI's can go dormant
  302. current_yaw = 0.0f;
  303. ideal_yaw = 0.0f;
  304. spawnClearMoveables = false;
  305. harvestEnt = NULL;
  306. num_cinematics = 0;
  307. current_cinematic = 0;
  308. allowEyeFocus = true;
  309. allowPain = true;
  310. allowJointMod = true;
  311. focusEntity = NULL;
  312. focusTime = 0;
  313. alignHeadTime = 0;
  314. forceAlignHeadTime = 0;
  315. currentFocusPos.Zero();
  316. eyeAng.Zero();
  317. lookAng.Zero();
  318. destLookAng.Zero();
  319. lookMin.Zero();
  320. lookMax.Zero();
  321. eyeMin.Zero();
  322. eyeMax.Zero();
  323. muzzleFlashEnd = 0;
  324. flashTime = 0;
  325. flashJointWorld = INVALID_JOINT;
  326. focusJoint = INVALID_JOINT;
  327. orientationJoint = INVALID_JOINT;
  328. flyTiltJoint = INVALID_JOINT;
  329. eyeVerticalOffset = 0.0f;
  330. eyeHorizontalOffset = 0.0f;
  331. eyeFocusRate = 0.0f;
  332. headFocusRate = 0.0f;
  333. focusAlignTime = 0;
  334. }
  335. /*
  336. =====================
  337. idAI::~idAI
  338. =====================
  339. */
  340. idAI::~idAI() {
  341. delete projectileClipModel;
  342. DeconstructScriptObject();
  343. scriptObject.Free();
  344. if ( worldMuzzleFlashHandle != -1 ) {
  345. gameRenderWorld->FreeLightDef( worldMuzzleFlashHandle );
  346. worldMuzzleFlashHandle = -1;
  347. }
  348. if ( harvestEnt.GetEntity() ) {
  349. harvestEnt.GetEntity()->PostEventMS( &EV_Remove, 0 );
  350. }
  351. }
  352. /*
  353. =====================
  354. idAI::Save
  355. =====================
  356. */
  357. void idAI::Save( idSaveGame *savefile ) const {
  358. int i;
  359. savefile->WriteInt( travelFlags );
  360. move.Save( savefile );
  361. savedMove.Save( savefile );
  362. savefile->WriteFloat( kickForce );
  363. savefile->WriteBool( ignore_obstacles );
  364. savefile->WriteFloat( blockedRadius );
  365. savefile->WriteInt( blockedMoveTime );
  366. savefile->WriteInt( blockedAttackTime );
  367. savefile->WriteFloat( ideal_yaw );
  368. savefile->WriteFloat( current_yaw );
  369. savefile->WriteFloat( turnRate );
  370. savefile->WriteFloat( turnVel );
  371. savefile->WriteFloat( anim_turn_yaw );
  372. savefile->WriteFloat( anim_turn_amount );
  373. savefile->WriteFloat( anim_turn_angles );
  374. savefile->WriteStaticObject( physicsObj );
  375. savefile->WriteFloat( fly_speed );
  376. savefile->WriteFloat( fly_bob_strength );
  377. savefile->WriteFloat( fly_bob_vert );
  378. savefile->WriteFloat( fly_bob_horz );
  379. savefile->WriteInt( fly_offset );
  380. savefile->WriteFloat( fly_seek_scale );
  381. savefile->WriteFloat( fly_roll_scale );
  382. savefile->WriteFloat( fly_roll_max );
  383. savefile->WriteFloat( fly_roll );
  384. savefile->WriteFloat( fly_pitch_scale );
  385. savefile->WriteFloat( fly_pitch_max );
  386. savefile->WriteFloat( fly_pitch );
  387. savefile->WriteBool( allowMove );
  388. savefile->WriteBool( allowHiddenMovement );
  389. savefile->WriteBool( disableGravity );
  390. savefile->WriteBool( af_push_moveables );
  391. savefile->WriteBool( lastHitCheckResult );
  392. savefile->WriteInt( lastHitCheckTime );
  393. savefile->WriteInt( lastAttackTime );
  394. savefile->WriteFloat( melee_range );
  395. savefile->WriteFloat( projectile_height_to_distance_ratio );
  396. savefile->WriteInt( missileLaunchOffset.Num() );
  397. for( i = 0; i < missileLaunchOffset.Num(); i++ ) {
  398. savefile->WriteVec3( missileLaunchOffset[ i ] );
  399. }
  400. idStr projectileName;
  401. spawnArgs.GetString( "def_projectile", "", projectileName );
  402. savefile->WriteString( projectileName );
  403. savefile->WriteFloat( projectileRadius );
  404. savefile->WriteFloat( projectileSpeed );
  405. savefile->WriteVec3( projectileVelocity );
  406. savefile->WriteVec3( projectileGravity );
  407. projectile.Save( savefile );
  408. savefile->WriteString( attack );
  409. savefile->WriteSoundShader( chat_snd );
  410. savefile->WriteInt( chat_min );
  411. savefile->WriteInt( chat_max );
  412. savefile->WriteInt( chat_time );
  413. savefile->WriteInt( talk_state );
  414. talkTarget.Save( savefile );
  415. savefile->WriteInt( num_cinematics );
  416. savefile->WriteInt( current_cinematic );
  417. savefile->WriteBool( allowJointMod );
  418. focusEntity.Save( savefile );
  419. savefile->WriteVec3( currentFocusPos );
  420. savefile->WriteInt( focusTime );
  421. savefile->WriteInt( alignHeadTime );
  422. savefile->WriteInt( forceAlignHeadTime );
  423. savefile->WriteAngles( eyeAng );
  424. savefile->WriteAngles( lookAng );
  425. savefile->WriteAngles( destLookAng );
  426. savefile->WriteAngles( lookMin );
  427. savefile->WriteAngles( lookMax );
  428. savefile->WriteInt( lookJoints.Num() );
  429. for( i = 0; i < lookJoints.Num(); i++ ) {
  430. savefile->WriteJoint( lookJoints[ i ] );
  431. savefile->WriteAngles( lookJointAngles[ i ] );
  432. }
  433. savefile->WriteInt( particles.Num() );
  434. for ( i = 0; i < particles.Num(); i++ ) {
  435. savefile->WriteParticle( particles[i].particle );
  436. savefile->WriteInt( particles[i].time );
  437. savefile->WriteJoint( particles[i].joint );
  438. }
  439. savefile->WriteBool( restartParticles );
  440. savefile->WriteBool( useBoneAxis );
  441. enemy.Save( savefile );
  442. savefile->WriteVec3( lastVisibleEnemyPos );
  443. savefile->WriteVec3( lastVisibleEnemyEyeOffset );
  444. savefile->WriteVec3( lastVisibleReachableEnemyPos );
  445. savefile->WriteVec3( lastReachableEnemyPos );
  446. savefile->WriteBool( wakeOnFlashlight );
  447. savefile->WriteAngles( eyeMin );
  448. savefile->WriteAngles( eyeMax );
  449. savefile->WriteFloat( eyeVerticalOffset );
  450. savefile->WriteFloat( eyeHorizontalOffset );
  451. savefile->WriteFloat( eyeFocusRate );
  452. savefile->WriteFloat( headFocusRate );
  453. savefile->WriteInt( focusAlignTime );
  454. savefile->WriteJoint( flashJointWorld );
  455. savefile->WriteInt( muzzleFlashEnd );
  456. savefile->WriteJoint( focusJoint );
  457. savefile->WriteJoint( orientationJoint );
  458. savefile->WriteJoint( flyTiltJoint );
  459. savefile->WriteBool( GetPhysics() == static_cast<const idPhysics *>(&physicsObj) );
  460. savefile->WriteInt(funcEmitters.Num());
  461. for(int i = 0; i < funcEmitters.Num(); i++) {
  462. funcEmitter_t* emitter = funcEmitters.GetIndex(i);
  463. savefile->WriteString(emitter->name);
  464. savefile->WriteJoint(emitter->joint);
  465. savefile->WriteObject(emitter->particle);
  466. }
  467. harvestEnt.Save( savefile);
  468. }
  469. /*
  470. =====================
  471. idAI::Restore
  472. =====================
  473. */
  474. void idAI::Restore( idRestoreGame *savefile ) {
  475. bool restorePhysics;
  476. int i;
  477. int num;
  478. idBounds bounds;
  479. savefile->ReadInt( travelFlags );
  480. move.Restore( savefile );
  481. savedMove.Restore( savefile );
  482. savefile->ReadFloat( kickForce );
  483. savefile->ReadBool( ignore_obstacles );
  484. savefile->ReadFloat( blockedRadius );
  485. savefile->ReadInt( blockedMoveTime );
  486. savefile->ReadInt( blockedAttackTime );
  487. savefile->ReadFloat( ideal_yaw );
  488. savefile->ReadFloat( current_yaw );
  489. savefile->ReadFloat( turnRate );
  490. savefile->ReadFloat( turnVel );
  491. savefile->ReadFloat( anim_turn_yaw );
  492. savefile->ReadFloat( anim_turn_amount );
  493. savefile->ReadFloat( anim_turn_angles );
  494. savefile->ReadStaticObject( physicsObj );
  495. savefile->ReadFloat( fly_speed );
  496. savefile->ReadFloat( fly_bob_strength );
  497. savefile->ReadFloat( fly_bob_vert );
  498. savefile->ReadFloat( fly_bob_horz );
  499. savefile->ReadInt( fly_offset );
  500. savefile->ReadFloat( fly_seek_scale );
  501. savefile->ReadFloat( fly_roll_scale );
  502. savefile->ReadFloat( fly_roll_max );
  503. savefile->ReadFloat( fly_roll );
  504. savefile->ReadFloat( fly_pitch_scale );
  505. savefile->ReadFloat( fly_pitch_max );
  506. savefile->ReadFloat( fly_pitch );
  507. savefile->ReadBool( allowMove );
  508. savefile->ReadBool( allowHiddenMovement );
  509. savefile->ReadBool( disableGravity );
  510. savefile->ReadBool( af_push_moveables );
  511. savefile->ReadBool( lastHitCheckResult );
  512. savefile->ReadInt( lastHitCheckTime );
  513. savefile->ReadInt( lastAttackTime );
  514. savefile->ReadFloat( melee_range );
  515. savefile->ReadFloat( projectile_height_to_distance_ratio );
  516. savefile->ReadInt( num );
  517. missileLaunchOffset.SetGranularity( 1 );
  518. missileLaunchOffset.SetNum( num );
  519. for( i = 0; i < num; i++ ) {
  520. savefile->ReadVec3( missileLaunchOffset[ i ] );
  521. }
  522. idStr projectileName;
  523. savefile->ReadString( projectileName );
  524. if ( projectileName.Length() ) {
  525. projectileDef = gameLocal.FindEntityDefDict( projectileName );
  526. } else {
  527. projectileDef = NULL;
  528. }
  529. savefile->ReadFloat( projectileRadius );
  530. savefile->ReadFloat( projectileSpeed );
  531. savefile->ReadVec3( projectileVelocity );
  532. savefile->ReadVec3( projectileGravity );
  533. projectile.Restore( savefile );
  534. savefile->ReadString( attack );
  535. savefile->ReadSoundShader( chat_snd );
  536. savefile->ReadInt( chat_min );
  537. savefile->ReadInt( chat_max );
  538. savefile->ReadInt( chat_time );
  539. savefile->ReadInt( i );
  540. talk_state = static_cast<talkState_t>( i );
  541. talkTarget.Restore( savefile );
  542. savefile->ReadInt( num_cinematics );
  543. savefile->ReadInt( current_cinematic );
  544. savefile->ReadBool( allowJointMod );
  545. focusEntity.Restore( savefile );
  546. savefile->ReadVec3( currentFocusPos );
  547. savefile->ReadInt( focusTime );
  548. savefile->ReadInt( alignHeadTime );
  549. savefile->ReadInt( forceAlignHeadTime );
  550. savefile->ReadAngles( eyeAng );
  551. savefile->ReadAngles( lookAng );
  552. savefile->ReadAngles( destLookAng );
  553. savefile->ReadAngles( lookMin );
  554. savefile->ReadAngles( lookMax );
  555. savefile->ReadInt( num );
  556. lookJoints.SetGranularity( 1 );
  557. lookJoints.SetNum( num );
  558. lookJointAngles.SetGranularity( 1 );
  559. lookJointAngles.SetNum( num );
  560. for( i = 0; i < num; i++ ) {
  561. savefile->ReadJoint( lookJoints[ i ] );
  562. savefile->ReadAngles( lookJointAngles[ i ] );
  563. }
  564. savefile->ReadInt( num );
  565. particles.SetNum( num );
  566. for ( i = 0; i < particles.Num(); i++ ) {
  567. savefile->ReadParticle( particles[i].particle );
  568. savefile->ReadInt( particles[i].time );
  569. savefile->ReadJoint( particles[i].joint );
  570. }
  571. savefile->ReadBool( restartParticles );
  572. savefile->ReadBool( useBoneAxis );
  573. enemy.Restore( savefile );
  574. savefile->ReadVec3( lastVisibleEnemyPos );
  575. savefile->ReadVec3( lastVisibleEnemyEyeOffset );
  576. savefile->ReadVec3( lastVisibleReachableEnemyPos );
  577. savefile->ReadVec3( lastReachableEnemyPos );
  578. savefile->ReadBool( wakeOnFlashlight );
  579. savefile->ReadAngles( eyeMin );
  580. savefile->ReadAngles( eyeMax );
  581. savefile->ReadFloat( eyeVerticalOffset );
  582. savefile->ReadFloat( eyeHorizontalOffset );
  583. savefile->ReadFloat( eyeFocusRate );
  584. savefile->ReadFloat( headFocusRate );
  585. savefile->ReadInt( focusAlignTime );
  586. savefile->ReadJoint( flashJointWorld );
  587. savefile->ReadInt( muzzleFlashEnd );
  588. savefile->ReadJoint( focusJoint );
  589. savefile->ReadJoint( orientationJoint );
  590. savefile->ReadJoint( flyTiltJoint );
  591. savefile->ReadBool( restorePhysics );
  592. // Set the AAS if the character has the correct gravity vector
  593. idVec3 gravity = spawnArgs.GetVector( "gravityDir", "0 0 -1" );
  594. gravity *= g_gravity.GetFloat();
  595. if ( gravity == gameLocal.GetGravity() ) {
  596. SetAAS();
  597. }
  598. SetCombatModel();
  599. LinkCombat();
  600. InitMuzzleFlash();
  601. // Link the script variables back to the scriptobject
  602. LinkScriptVariables();
  603. if ( restorePhysics ) {
  604. RestorePhysics( &physicsObj );
  605. }
  606. //Clean up the emitters
  607. for(int i = 0; i < funcEmitters.Num(); i++) {
  608. funcEmitter_t* emitter = funcEmitters.GetIndex(i);
  609. if(emitter->particle) {
  610. //Destroy the emitters
  611. emitter->particle->PostEventMS(&EV_Remove, 0 );
  612. }
  613. }
  614. funcEmitters.Clear();
  615. int emitterCount;
  616. savefile->ReadInt( emitterCount );
  617. for(int i = 0; i < emitterCount; i++) {
  618. funcEmitter_t newEmitter;
  619. memset(&newEmitter, 0, sizeof(newEmitter));
  620. idStr name;
  621. savefile->ReadString( name );
  622. strcpy( newEmitter.name, name.c_str() );
  623. savefile->ReadJoint( newEmitter.joint );
  624. savefile->ReadObject(reinterpret_cast<idClass *&>(newEmitter.particle));
  625. funcEmitters.Set(newEmitter.name, newEmitter);
  626. }
  627. harvestEnt.Restore(savefile);
  628. //if(harvestEnt.GetEntity()) {
  629. // harvestEnt.GetEntity()->SetParent(this);
  630. //}
  631. }
  632. /*
  633. =====================
  634. idAI::Spawn
  635. =====================
  636. */
  637. void idAI::Spawn() {
  638. const char *jointname;
  639. const idKeyValue *kv;
  640. idStr jointName;
  641. idAngles jointScale;
  642. jointHandle_t joint;
  643. idVec3 local_dir;
  644. bool talks;
  645. if ( !g_monsters.GetBool() ) {
  646. PostEventMS( &EV_Remove, 0 );
  647. return;
  648. }
  649. spawnArgs.GetInt( "team", "1", team );
  650. spawnArgs.GetInt( "rank", "0", rank );
  651. spawnArgs.GetInt( "fly_offset", "0", fly_offset );
  652. spawnArgs.GetFloat( "fly_speed", "100", fly_speed );
  653. spawnArgs.GetFloat( "fly_bob_strength", "50", fly_bob_strength );
  654. spawnArgs.GetFloat( "fly_bob_vert", "2", fly_bob_horz );
  655. spawnArgs.GetFloat( "fly_bob_horz", "2.7", fly_bob_vert );
  656. spawnArgs.GetFloat( "fly_seek_scale", "4", fly_seek_scale );
  657. spawnArgs.GetFloat( "fly_roll_scale", "90", fly_roll_scale );
  658. spawnArgs.GetFloat( "fly_roll_max", "60", fly_roll_max );
  659. spawnArgs.GetFloat( "fly_pitch_scale", "45", fly_pitch_scale );
  660. spawnArgs.GetFloat( "fly_pitch_max", "30", fly_pitch_max );
  661. spawnArgs.GetFloat( "melee_range", "64", melee_range );
  662. spawnArgs.GetFloat( "projectile_height_to_distance_ratio", "1", projectile_height_to_distance_ratio );
  663. spawnArgs.GetFloat( "turn_rate", "360", turnRate );
  664. spawnArgs.GetBool( "talks", "0", talks );
  665. if ( spawnArgs.GetString( "npc_name", NULL ) != NULL ) {
  666. if ( talks ) {
  667. talk_state = TALK_OK;
  668. } else {
  669. talk_state = TALK_BUSY;
  670. }
  671. } else {
  672. talk_state = TALK_NEVER;
  673. }
  674. spawnArgs.GetBool( "animate_z", "0", disableGravity );
  675. spawnArgs.GetBool( "af_push_moveables", "0", af_push_moveables );
  676. spawnArgs.GetFloat( "kick_force", "4096", kickForce );
  677. spawnArgs.GetBool( "ignore_obstacles", "0", ignore_obstacles );
  678. spawnArgs.GetFloat( "blockedRadius", "-1", blockedRadius );
  679. spawnArgs.GetInt( "blockedMoveTime", "750", blockedMoveTime );
  680. spawnArgs.GetInt( "blockedAttackTime", "750", blockedAttackTime );
  681. spawnArgs.GetInt( "num_cinematics", "0", num_cinematics );
  682. current_cinematic = 0;
  683. LinkScriptVariables();
  684. fl.takedamage = !spawnArgs.GetBool( "noDamage" );
  685. enemy = NULL;
  686. allowMove = true;
  687. allowHiddenMovement = false;
  688. animator.RemoveOriginOffset( true );
  689. // create combat collision hull for exact collision detection
  690. SetCombatModel();
  691. lookMin = spawnArgs.GetAngles( "look_min", "-80 -75 0" );
  692. lookMax = spawnArgs.GetAngles( "look_max", "80 75 0" );
  693. lookJoints.SetGranularity( 1 );
  694. lookJointAngles.SetGranularity( 1 );
  695. kv = spawnArgs.MatchPrefix( "look_joint", NULL );
  696. while( kv ) {
  697. jointName = kv->GetKey();
  698. jointName.StripLeadingOnce( "look_joint " );
  699. joint = animator.GetJointHandle( jointName );
  700. if ( joint == INVALID_JOINT ) {
  701. gameLocal.Warning( "Unknown look_joint '%s' on entity %s", jointName.c_str(), name.c_str() );
  702. } else {
  703. jointScale = spawnArgs.GetAngles( kv->GetKey(), "0 0 0" );
  704. jointScale.roll = 0.0f;
  705. // if no scale on any component, then don't bother adding it. this may be done to
  706. // zero out rotation from an inherited entitydef.
  707. if ( jointScale != ang_zero ) {
  708. lookJoints.Append( joint );
  709. lookJointAngles.Append( jointScale );
  710. }
  711. }
  712. kv = spawnArgs.MatchPrefix( "look_joint", kv );
  713. }
  714. // calculate joint positions on attack frames so we can do proper "can hit" tests
  715. CalculateAttackOffsets();
  716. eyeMin = spawnArgs.GetAngles( "eye_turn_min", "-10 -30 0" );
  717. eyeMax = spawnArgs.GetAngles( "eye_turn_max", "10 30 0" );
  718. eyeVerticalOffset = spawnArgs.GetFloat( "eye_verticle_offset", "5" );
  719. eyeHorizontalOffset = spawnArgs.GetFloat( "eye_horizontal_offset", "-8" );
  720. eyeFocusRate = spawnArgs.GetFloat( "eye_focus_rate", "0.5" );
  721. headFocusRate = spawnArgs.GetFloat( "head_focus_rate", "0.1" );
  722. focusAlignTime = SEC2MS( spawnArgs.GetFloat( "focus_align_time", "1" ) );
  723. flashJointWorld = animator.GetJointHandle( "flash" );
  724. if ( head.GetEntity() ) {
  725. idAnimator *headAnimator = head.GetEntity()->GetAnimator();
  726. jointname = spawnArgs.GetString( "bone_focus" );
  727. if ( *jointname ) {
  728. focusJoint = headAnimator->GetJointHandle( jointname );
  729. if ( focusJoint == INVALID_JOINT ) {
  730. gameLocal.Warning( "Joint '%s' not found on head on '%s'", jointname, name.c_str() );
  731. }
  732. }
  733. } else {
  734. jointname = spawnArgs.GetString( "bone_focus" );
  735. if ( *jointname ) {
  736. focusJoint = animator.GetJointHandle( jointname );
  737. if ( focusJoint == INVALID_JOINT ) {
  738. gameLocal.Warning( "Joint '%s' not found on '%s'", jointname, name.c_str() );
  739. }
  740. }
  741. }
  742. jointname = spawnArgs.GetString( "bone_orientation" );
  743. if ( *jointname ) {
  744. orientationJoint = animator.GetJointHandle( jointname );
  745. if ( orientationJoint == INVALID_JOINT ) {
  746. gameLocal.Warning( "Joint '%s' not found on '%s'", jointname, name.c_str() );
  747. }
  748. }
  749. jointname = spawnArgs.GetString( "bone_flytilt" );
  750. if ( *jointname ) {
  751. flyTiltJoint = animator.GetJointHandle( jointname );
  752. if ( flyTiltJoint == INVALID_JOINT ) {
  753. gameLocal.Warning( "Joint '%s' not found on '%s'", jointname, name.c_str() );
  754. }
  755. }
  756. InitMuzzleFlash();
  757. physicsObj.SetSelf( this );
  758. physicsObj.SetClipModel( new (TAG_PHYSICS_CLIP_ENTITY) idClipModel( GetPhysics()->GetClipModel() ), 1.0f );
  759. physicsObj.SetMass( spawnArgs.GetFloat( "mass", "100" ) );
  760. if ( spawnArgs.GetBool( "big_monster" ) ) {
  761. physicsObj.SetContents( 0 );
  762. physicsObj.SetClipMask( MASK_MONSTERSOLID & ~CONTENTS_BODY );
  763. } else {
  764. if ( use_combat_bbox ) {
  765. physicsObj.SetContents( CONTENTS_BODY|CONTENTS_SOLID );
  766. } else {
  767. physicsObj.SetContents( CONTENTS_BODY );
  768. }
  769. physicsObj.SetClipMask( MASK_MONSTERSOLID );
  770. }
  771. // move up to make sure the monster is at least an epsilon above the floor
  772. physicsObj.SetOrigin( GetPhysics()->GetOrigin() + idVec3( 0, 0, CM_CLIP_EPSILON ) );
  773. if ( num_cinematics ) {
  774. physicsObj.SetGravity( vec3_origin );
  775. } else {
  776. idVec3 gravity = spawnArgs.GetVector( "gravityDir", "0 0 -1" );
  777. gravity *= g_gravity.GetFloat();
  778. physicsObj.SetGravity( gravity );
  779. }
  780. SetPhysics( &physicsObj );
  781. physicsObj.GetGravityAxis().ProjectVector( viewAxis[ 0 ], local_dir );
  782. current_yaw = local_dir.ToYaw();
  783. ideal_yaw = idMath::AngleNormalize180( current_yaw );
  784. move.blockTime = 0;
  785. SetAAS();
  786. projectile = NULL;
  787. projectileDef = NULL;
  788. projectileClipModel = NULL;
  789. idStr projectileName;
  790. if ( spawnArgs.GetString( "def_projectile", "", projectileName ) && projectileName.Length() ) {
  791. projectileDef = gameLocal.FindEntityDefDict( projectileName );
  792. CreateProjectile( vec3_origin, viewAxis[ 0 ] );
  793. projectileRadius = projectile.GetEntity()->GetPhysics()->GetClipModel()->GetBounds().GetRadius();
  794. projectileVelocity = idProjectile::GetVelocity( projectileDef );
  795. projectileGravity = idProjectile::GetGravity( projectileDef );
  796. projectileSpeed = projectileVelocity.Length();
  797. delete projectile.GetEntity();
  798. projectile = NULL;
  799. }
  800. particles.Clear();
  801. restartParticles = true;
  802. useBoneAxis = spawnArgs.GetBool( "useBoneAxis" );
  803. SpawnParticles( "smokeParticleSystem" );
  804. if ( num_cinematics || spawnArgs.GetBool( "hide" ) || spawnArgs.GetBool( "teleport" ) || spawnArgs.GetBool( "trigger_anim" ) ) {
  805. fl.takedamage = false;
  806. physicsObj.SetContents( 0 );
  807. physicsObj.GetClipModel()->Unlink();
  808. Hide();
  809. } else {
  810. // play a looping ambient sound if we have one
  811. StartSound( "snd_ambient", SND_CHANNEL_AMBIENT, 0, false, NULL );
  812. }
  813. if ( health <= 0 ) {
  814. gameLocal.Warning( "entity '%s' doesn't have health set", name.c_str() );
  815. health = 1;
  816. }
  817. // set up monster chatter
  818. SetChatSound();
  819. BecomeActive( TH_THINK );
  820. if ( af_push_moveables ) {
  821. af.SetupPose( this, gameLocal.time );
  822. af.GetPhysics()->EnableClip();
  823. }
  824. // init the move variables
  825. StopMove( MOVE_STATUS_DONE );
  826. spawnArgs.GetBool( "spawnClearMoveables", "0", spawnClearMoveables );
  827. }
  828. void idAI::Gib( const idVec3 &dir, const char *damageDefName ) {
  829. if(harvestEnt.GetEntity()) {
  830. //Let the harvest ent know that we gibbed
  831. harvestEnt.GetEntity()->Gib();
  832. }
  833. idActor::Gib(dir, damageDefName);
  834. }
  835. /*
  836. ===================
  837. idAI::InitMuzzleFlash
  838. ===================
  839. */
  840. void idAI::InitMuzzleFlash() {
  841. const char *shader;
  842. idVec3 flashColor;
  843. spawnArgs.GetString( "mtr_flashShader", "muzzleflash", &shader );
  844. spawnArgs.GetVector( "flashColor", "0 0 0", flashColor );
  845. float flashRadius = spawnArgs.GetFloat( "flashRadius" );
  846. flashTime = SEC2MS( spawnArgs.GetFloat( "flashTime", "0.25" ) );
  847. memset( &worldMuzzleFlash, 0, sizeof ( worldMuzzleFlash ) );
  848. worldMuzzleFlash.pointLight = true;
  849. worldMuzzleFlash.shader = declManager->FindMaterial( shader, false );
  850. worldMuzzleFlash.shaderParms[ SHADERPARM_RED ] = flashColor[0];
  851. worldMuzzleFlash.shaderParms[ SHADERPARM_GREEN ] = flashColor[1];
  852. worldMuzzleFlash.shaderParms[ SHADERPARM_BLUE ] = flashColor[2];
  853. worldMuzzleFlash.shaderParms[ SHADERPARM_ALPHA ] = 1.0f;
  854. worldMuzzleFlash.shaderParms[ SHADERPARM_TIMESCALE ] = 1.0f;
  855. worldMuzzleFlash.lightRadius[0] = flashRadius;
  856. worldMuzzleFlash.lightRadius[1] = flashRadius;
  857. worldMuzzleFlash.lightRadius[2] = flashRadius;
  858. worldMuzzleFlashHandle = -1;
  859. }
  860. /*
  861. ===================
  862. idAI::List_f
  863. ===================
  864. */
  865. void idAI::List_f( const idCmdArgs &args ) {
  866. int e;
  867. idAI *check;
  868. int count;
  869. const char *statename;
  870. count = 0;
  871. gameLocal.Printf( "%-4s %-20s %s\n", " Num", "EntityDef", "Name" );
  872. gameLocal.Printf( "------------------------------------------------\n" );
  873. for( e = 0; e < MAX_GENTITIES; e++ ) {
  874. check = static_cast<idAI *>(gameLocal.entities[ e ]);
  875. if ( !check || !check->IsType( idAI::Type ) ) {
  876. continue;
  877. }
  878. if ( check->state ) {
  879. statename = check->state->Name();
  880. } else {
  881. statename = "NULL state";
  882. }
  883. gameLocal.Printf( "%4i: %-20s %-20s %s move: %d\n", e, check->GetEntityDefName(), check->name.c_str(), statename, check->allowMove );
  884. count++;
  885. }
  886. gameLocal.Printf( "...%d monsters\n", count );
  887. }
  888. /*
  889. ================
  890. idAI::DormantBegin
  891. called when entity becomes dormant
  892. ================
  893. */
  894. void idAI::DormantBegin() {
  895. // since dormant happens on a timer, we wont get to update particles to
  896. // hidden through the think loop, but we need to hide them though.
  897. if ( particles.Num() ) {
  898. for ( int i = 0; i < particles.Num(); i++ ) {
  899. particles[i].time = 0;
  900. }
  901. }
  902. if ( enemyNode.InList() ) {
  903. // remove ourselves from the enemy's enemylist
  904. enemyNode.Remove();
  905. }
  906. idActor::DormantBegin();
  907. }
  908. /*
  909. ================
  910. idAI::DormantEnd
  911. called when entity wakes from being dormant
  912. ================
  913. */
  914. void idAI::DormantEnd() {
  915. if ( enemy.GetEntity() && !enemyNode.InList() ) {
  916. // let our enemy know we're back on the trail
  917. enemyNode.AddToEnd( enemy.GetEntity()->enemyList );
  918. }
  919. if ( particles.Num() ) {
  920. for ( int i = 0; i < particles.Num(); i++ ) {
  921. particles[i].time = gameLocal.time;
  922. }
  923. }
  924. idActor::DormantEnd();
  925. }
  926. /*
  927. =====================
  928. idAI::Think
  929. =====================
  930. */
  931. idCVar ai_think( "ai_think", "1", CVAR_BOOL, "for testing.." );
  932. void idAI::Think() {
  933. // if we are completely closed off from the player, don't do anything at all
  934. if ( CheckDormant() ) {
  935. return;
  936. }
  937. if ( !ai_think.GetBool() ) {
  938. return;
  939. }
  940. if ( thinkFlags & TH_THINK ) {
  941. // clear out the enemy when he dies or is hidden
  942. idActor *enemyEnt = enemy.GetEntity();
  943. if ( enemyEnt ) {
  944. if ( enemyEnt->health <= 0 ) {
  945. EnemyDead();
  946. }
  947. }
  948. current_yaw += deltaViewAngles.yaw;
  949. ideal_yaw = idMath::AngleNormalize180( ideal_yaw + deltaViewAngles.yaw );
  950. deltaViewAngles.Zero();
  951. viewAxis = idAngles( 0, current_yaw, 0 ).ToMat3();
  952. if ( num_cinematics ) {
  953. if ( !IsHidden() && torsoAnim.AnimDone( 0 ) ) {
  954. PlayCinematic();
  955. }
  956. RunPhysics();
  957. } else if ( !allowHiddenMovement && IsHidden() ) {
  958. // hidden monsters
  959. UpdateAIScript();
  960. } else {
  961. // clear the ik before we do anything else so the skeleton doesn't get updated twice
  962. walkIK.ClearJointMods();
  963. switch( move.moveType ) {
  964. case MOVETYPE_DEAD :
  965. // dead monsters
  966. UpdateAIScript();
  967. DeadMove();
  968. break;
  969. case MOVETYPE_FLY :
  970. // flying monsters
  971. UpdateEnemyPosition();
  972. UpdateAIScript();
  973. FlyMove();
  974. PlayChatter();
  975. CheckBlink();
  976. break;
  977. case MOVETYPE_STATIC :
  978. // static monsters
  979. UpdateEnemyPosition();
  980. UpdateAIScript();
  981. StaticMove();
  982. PlayChatter();
  983. CheckBlink();
  984. break;
  985. case MOVETYPE_ANIM :
  986. // animation based movement
  987. UpdateEnemyPosition();
  988. UpdateAIScript();
  989. AnimMove();
  990. PlayChatter();
  991. CheckBlink();
  992. break;
  993. case MOVETYPE_SLIDE :
  994. // velocity based movement
  995. UpdateEnemyPosition();
  996. UpdateAIScript();
  997. SlideMove();
  998. PlayChatter();
  999. CheckBlink();
  1000. break;
  1001. }
  1002. }
  1003. // clear pain flag so that we recieve any damage between now and the next time we run the script
  1004. AI_PAIN = false;
  1005. AI_SPECIAL_DAMAGE = 0;
  1006. AI_PUSHED = false;
  1007. } else if ( thinkFlags & TH_PHYSICS ) {
  1008. RunPhysics();
  1009. }
  1010. if ( af_push_moveables ) {
  1011. PushWithAF();
  1012. }
  1013. if ( fl.hidden && allowHiddenMovement ) {
  1014. // UpdateAnimation won't call frame commands when hidden, so call them here when we allow hidden movement
  1015. animator.ServiceAnims( gameLocal.previousTime, gameLocal.time );
  1016. }
  1017. /* this still draws in retail builds.. not sure why.. don't care at this point.
  1018. if ( !aas && developer.GetBool() && !fl.hidden && !num_cinematics ) {
  1019. gameRenderWorld->DrawText( "No AAS", physicsObj.GetAbsBounds().GetCenter(), 0.1f, colorWhite, gameLocal.GetLocalPlayer()->viewAngles.ToMat3(), 1, 1 );
  1020. }
  1021. */
  1022. UpdateMuzzleFlash();
  1023. UpdateAnimation();
  1024. UpdateParticles();
  1025. Present();
  1026. UpdateDamageEffects();
  1027. LinkCombat();
  1028. if(ai_showHealth.GetBool()) {
  1029. idVec3 aboveHead(0,0,20);
  1030. gameRenderWorld->DrawText( va( "%d", ( int )health), this->GetEyePosition()+aboveHead, 0.5f, colorWhite, gameLocal.GetLocalPlayer()->viewAngles.ToMat3() );
  1031. }
  1032. }
  1033. /***********************************************************************
  1034. AI script state management
  1035. ***********************************************************************/
  1036. /*
  1037. =====================
  1038. idAI::LinkScriptVariables
  1039. =====================
  1040. */
  1041. void idAI::LinkScriptVariables() {
  1042. AI_TALK.LinkTo( scriptObject, "AI_TALK" );
  1043. AI_DAMAGE.LinkTo( scriptObject, "AI_DAMAGE" );
  1044. AI_PAIN.LinkTo( scriptObject, "AI_PAIN" );
  1045. AI_SPECIAL_DAMAGE.LinkTo( scriptObject, "AI_SPECIAL_DAMAGE" );
  1046. AI_DEAD.LinkTo( scriptObject, "AI_DEAD" );
  1047. AI_ENEMY_VISIBLE.LinkTo( scriptObject, "AI_ENEMY_VISIBLE" );
  1048. AI_ENEMY_IN_FOV.LinkTo( scriptObject, "AI_ENEMY_IN_FOV" );
  1049. AI_ENEMY_DEAD.LinkTo( scriptObject, "AI_ENEMY_DEAD" );
  1050. AI_MOVE_DONE.LinkTo( scriptObject, "AI_MOVE_DONE" );
  1051. AI_ONGROUND.LinkTo( scriptObject, "AI_ONGROUND" );
  1052. AI_ACTIVATED.LinkTo( scriptObject, "AI_ACTIVATED" );
  1053. AI_FORWARD.LinkTo( scriptObject, "AI_FORWARD" );
  1054. AI_JUMP.LinkTo( scriptObject, "AI_JUMP" );
  1055. AI_BLOCKED.LinkTo( scriptObject, "AI_BLOCKED" );
  1056. AI_DEST_UNREACHABLE.LinkTo( scriptObject, "AI_DEST_UNREACHABLE" );
  1057. AI_HIT_ENEMY.LinkTo( scriptObject, "AI_HIT_ENEMY" );
  1058. AI_OBSTACLE_IN_PATH.LinkTo( scriptObject, "AI_OBSTACLE_IN_PATH" );
  1059. AI_PUSHED.LinkTo( scriptObject, "AI_PUSHED" );
  1060. }
  1061. /*
  1062. =====================
  1063. idAI::UpdateAIScript
  1064. =====================
  1065. */
  1066. void idAI::UpdateAIScript() {
  1067. UpdateScript();
  1068. // clear the hit enemy flag so we catch the next time we hit someone
  1069. AI_HIT_ENEMY = false;
  1070. if ( allowHiddenMovement || !IsHidden() ) {
  1071. // update the animstate if we're not hidden
  1072. UpdateAnimState();
  1073. }
  1074. }
  1075. /***********************************************************************
  1076. navigation
  1077. ***********************************************************************/
  1078. /*
  1079. ============
  1080. idAI::KickObstacles
  1081. ============
  1082. */
  1083. void idAI::KickObstacles( const idVec3 &dir, float force, idEntity *alwaysKick ) {
  1084. int i, numListedClipModels;
  1085. idBounds clipBounds;
  1086. idEntity *obEnt;
  1087. idClipModel *clipModel;
  1088. idClipModel *clipModelList[ MAX_GENTITIES ];
  1089. int clipmask;
  1090. idVec3 org;
  1091. idVec3 forceVec;
  1092. idVec3 delta;
  1093. idVec2 perpendicular;
  1094. org = physicsObj.GetOrigin();
  1095. // find all possible obstacles
  1096. clipBounds = physicsObj.GetAbsBounds();
  1097. clipBounds.TranslateSelf( dir * 32.0f );
  1098. clipBounds.ExpandSelf( 8.0f );
  1099. clipBounds.AddPoint( org );
  1100. clipmask = physicsObj.GetClipMask();
  1101. numListedClipModels = gameLocal.clip.ClipModelsTouchingBounds( clipBounds, clipmask, clipModelList, MAX_GENTITIES );
  1102. for ( i = 0; i < numListedClipModels; i++ ) {
  1103. clipModel = clipModelList[i];
  1104. obEnt = clipModel->GetEntity();
  1105. if ( obEnt == alwaysKick ) {
  1106. // we'll kick this one outside the loop
  1107. continue;
  1108. }
  1109. if ( !clipModel->IsTraceModel() ) {
  1110. continue;
  1111. }
  1112. if ( obEnt->IsType( idMoveable::Type ) && obEnt->GetPhysics()->IsPushable() ) {
  1113. delta = obEnt->GetPhysics()->GetOrigin() - org;
  1114. delta.NormalizeFast();
  1115. perpendicular.x = -delta.y;
  1116. perpendicular.y = delta.x;
  1117. delta.z += 0.5f;
  1118. delta.ToVec2() += perpendicular * gameLocal.random.CRandomFloat() * 0.5f;
  1119. forceVec = delta * force * obEnt->GetPhysics()->GetMass();
  1120. obEnt->ApplyImpulse( this, 0, obEnt->GetPhysics()->GetOrigin(), forceVec );
  1121. }
  1122. }
  1123. if ( alwaysKick ) {
  1124. delta = alwaysKick->GetPhysics()->GetOrigin() - org;
  1125. delta.NormalizeFast();
  1126. perpendicular.x = -delta.y;
  1127. perpendicular.y = delta.x;
  1128. delta.z += 0.5f;
  1129. delta.ToVec2() += perpendicular * gameLocal.random.CRandomFloat() * 0.5f;
  1130. forceVec = delta * force * alwaysKick->GetPhysics()->GetMass();
  1131. alwaysKick->ApplyImpulse( this, 0, alwaysKick->GetPhysics()->GetOrigin(), forceVec );
  1132. }
  1133. }
  1134. /*
  1135. ============
  1136. ValidForBounds
  1137. ============
  1138. */
  1139. bool ValidForBounds( const idAASSettings *settings, const idBounds &bounds ) {
  1140. int i;
  1141. for ( i = 0; i < 3; i++ ) {
  1142. if ( bounds[0][i] < settings->boundingBoxes[0][0][i] ) {
  1143. return false;
  1144. }
  1145. if ( bounds[1][i] > settings->boundingBoxes[0][1][i] ) {
  1146. return false;
  1147. }
  1148. }
  1149. return true;
  1150. }
  1151. /*
  1152. =====================
  1153. idAI::SetAAS
  1154. =====================
  1155. */
  1156. void idAI::SetAAS() {
  1157. idStr use_aas;
  1158. spawnArgs.GetString( "use_aas", NULL, use_aas );
  1159. aas = gameLocal.GetAAS( use_aas );
  1160. if ( aas ) {
  1161. const idAASSettings *settings = aas->GetSettings();
  1162. if ( settings ) {
  1163. if ( !ValidForBounds( settings, physicsObj.GetBounds() ) ) {
  1164. gameLocal.Error( "%s cannot use use_aas %s\n", name.c_str(), use_aas.c_str() );
  1165. }
  1166. float height = settings->maxStepHeight;
  1167. physicsObj.SetMaxStepHeight( height );
  1168. return;
  1169. } else {
  1170. aas = NULL;
  1171. }
  1172. }
  1173. gameLocal.Printf( "WARNING: %s has no AAS file\n", name.c_str() );
  1174. }
  1175. /*
  1176. =====================
  1177. idAI::DrawRoute
  1178. =====================
  1179. */
  1180. void idAI::DrawRoute() const {
  1181. if ( aas && move.toAreaNum && move.moveCommand != MOVE_NONE && move.moveCommand != MOVE_WANDER && move.moveCommand != MOVE_FACE_ENEMY && move.moveCommand != MOVE_FACE_ENTITY && move.moveCommand != MOVE_TO_POSITION_DIRECT ) {
  1182. if ( move.moveType == MOVETYPE_FLY ) {
  1183. aas->ShowFlyPath( physicsObj.GetOrigin(), move.toAreaNum, move.moveDest );
  1184. } else {
  1185. aas->ShowWalkPath( physicsObj.GetOrigin(), move.toAreaNum, move.moveDest );
  1186. }
  1187. }
  1188. }
  1189. /*
  1190. =====================
  1191. idAI::ReachedPos
  1192. =====================
  1193. */
  1194. bool idAI::ReachedPos( const idVec3 &pos, const moveCommand_t moveCommand ) const {
  1195. if ( move.moveType == MOVETYPE_SLIDE ) {
  1196. idBounds bnds( idVec3( -4, -4.0f, -8.0f ), idVec3( 4.0f, 4.0f, 64.0f ) );
  1197. bnds.TranslateSelf( physicsObj.GetOrigin() );
  1198. if ( bnds.ContainsPoint( pos ) ) {
  1199. return true;
  1200. }
  1201. } else {
  1202. if ( ( moveCommand == MOVE_TO_ENEMY ) || ( moveCommand == MOVE_TO_ENTITY ) ) {
  1203. if ( physicsObj.GetAbsBounds().IntersectsBounds( idBounds( pos ).Expand( 8.0f ) ) ) {
  1204. return true;
  1205. }
  1206. } else {
  1207. idBounds bnds( idVec3( -16.0, -16.0f, -8.0f ), idVec3( 16.0, 16.0f, 64.0f ) );
  1208. bnds.TranslateSelf( physicsObj.GetOrigin() );
  1209. if ( bnds.ContainsPoint( pos ) ) {
  1210. return true;
  1211. }
  1212. }
  1213. }
  1214. return false;
  1215. }
  1216. /*
  1217. =====================
  1218. idAI::PointReachableAreaNum
  1219. =====================
  1220. */
  1221. int idAI::PointReachableAreaNum( const idVec3 &pos, const float boundsScale ) const {
  1222. int areaNum;
  1223. idVec3 size;
  1224. idBounds bounds;
  1225. if ( !aas ) {
  1226. return 0;
  1227. }
  1228. size = aas->GetSettings()->boundingBoxes[0][1] * boundsScale;
  1229. bounds[0] = -size;
  1230. size.z = 32.0f;
  1231. bounds[1] = size;
  1232. if ( move.moveType == MOVETYPE_FLY ) {
  1233. areaNum = aas->PointReachableAreaNum( pos, bounds, AREA_REACHABLE_WALK | AREA_REACHABLE_FLY );
  1234. } else {
  1235. areaNum = aas->PointReachableAreaNum( pos, bounds, AREA_REACHABLE_WALK );
  1236. }
  1237. return areaNum;
  1238. }
  1239. /*
  1240. =====================
  1241. idAI::PathToGoal
  1242. =====================
  1243. */
  1244. bool idAI::PathToGoal( aasPath_t &path, int areaNum, const idVec3 &origin, int goalAreaNum, const idVec3 &goalOrigin ) const {
  1245. idVec3 org;
  1246. idVec3 goal;
  1247. if ( !aas ) {
  1248. return false;
  1249. }
  1250. org = origin;
  1251. aas->PushPointIntoAreaNum( areaNum, org );
  1252. if ( !areaNum ) {
  1253. return false;
  1254. }
  1255. goal = goalOrigin;
  1256. aas->PushPointIntoAreaNum( goalAreaNum, goal );
  1257. if ( !goalAreaNum ) {
  1258. return false;
  1259. }
  1260. if ( move.moveType == MOVETYPE_FLY ) {
  1261. return aas->FlyPathToGoal( path, areaNum, org, goalAreaNum, goal, travelFlags );
  1262. } else {
  1263. return aas->WalkPathToGoal( path, areaNum, org, goalAreaNum, goal, travelFlags );
  1264. }
  1265. }
  1266. /*
  1267. =====================
  1268. idAI::TravelDistance
  1269. Returns the approximate travel distance from one position to the goal, or if no AAS, the straight line distance.
  1270. This is feakin' slow, so it's not good to do it too many times per frame. It also is slower the further you
  1271. are from the goal, so try to break the goals up into shorter distances.
  1272. =====================
  1273. */
  1274. float idAI::TravelDistance( const idVec3 &start, const idVec3 &end ) const {
  1275. int fromArea;
  1276. int toArea;
  1277. float dist;
  1278. idVec2 delta;
  1279. aasPath_t path;
  1280. if ( !aas ) {
  1281. // no aas, so just take the straight line distance
  1282. delta = end.ToVec2() - start.ToVec2();
  1283. dist = delta.LengthFast();
  1284. if ( ai_debugMove.GetBool() ) {
  1285. gameRenderWorld->DebugLine( colorBlue, start, end, 1, false );
  1286. gameRenderWorld->DrawText( va( "%d", ( int )dist ), ( start + end ) * 0.5f, 0.1f, colorWhite, gameLocal.GetLocalPlayer()->viewAngles.ToMat3() );
  1287. }
  1288. return dist;
  1289. }
  1290. fromArea = PointReachableAreaNum( start );
  1291. toArea = PointReachableAreaNum( end );
  1292. if ( !fromArea || !toArea ) {
  1293. // can't seem to get there
  1294. return -1;
  1295. }
  1296. if ( fromArea == toArea ) {
  1297. // same area, so just take the straight line distance
  1298. delta = end.ToVec2() - start.ToVec2();
  1299. dist = delta.LengthFast();
  1300. if ( ai_debugMove.GetBool() ) {
  1301. gameRenderWorld->DebugLine( colorBlue, start, end, 1, false );
  1302. gameRenderWorld->DrawText( va( "%d", ( int )dist ), ( start + end ) * 0.5f, 0.1f, colorWhite, gameLocal.GetLocalPlayer()->viewAngles.ToMat3() );
  1303. }
  1304. return dist;
  1305. }
  1306. idReachability *reach;
  1307. int travelTime;
  1308. if ( !aas->RouteToGoalArea( fromArea, start, toArea, travelFlags, travelTime, &reach ) ) {
  1309. return -1;
  1310. }
  1311. if ( ai_debugMove.GetBool() ) {
  1312. if ( move.moveType == MOVETYPE_FLY ) {
  1313. aas->ShowFlyPath( start, toArea, end );
  1314. } else {
  1315. aas->ShowWalkPath( start, toArea, end );
  1316. }
  1317. }
  1318. return travelTime;
  1319. }
  1320. /*
  1321. =====================
  1322. idAI::StopMove
  1323. =====================
  1324. */
  1325. void idAI::StopMove( moveStatus_t status ) {
  1326. AI_MOVE_DONE = true;
  1327. AI_FORWARD = false;
  1328. move.moveCommand = MOVE_NONE;
  1329. move.moveStatus = status;
  1330. move.toAreaNum = 0;
  1331. move.goalEntity = NULL;
  1332. move.moveDest = physicsObj.GetOrigin();
  1333. AI_DEST_UNREACHABLE = false;
  1334. AI_OBSTACLE_IN_PATH = false;
  1335. AI_BLOCKED = false;
  1336. move.startTime = gameLocal.time;
  1337. move.duration = 0;
  1338. move.range = 0.0f;
  1339. move.speed = 0.0f;
  1340. move.anim = 0;
  1341. move.moveDir.Zero();
  1342. move.lastMoveOrigin.Zero();
  1343. move.lastMoveTime = gameLocal.time;
  1344. }
  1345. /*
  1346. =====================
  1347. idAI::FaceEnemy
  1348. Continually face the enemy's last known position. MoveDone is always true in this case.
  1349. =====================
  1350. */
  1351. bool idAI::FaceEnemy() {
  1352. idActor *enemyEnt = enemy.GetEntity();
  1353. if ( !enemyEnt ) {
  1354. StopMove( MOVE_STATUS_DEST_NOT_FOUND );
  1355. return false;
  1356. }
  1357. TurnToward( lastVisibleEnemyPos );
  1358. move.goalEntity = enemyEnt;
  1359. move.moveDest = physicsObj.GetOrigin();
  1360. move.moveCommand = MOVE_FACE_ENEMY;
  1361. move.moveStatus = MOVE_STATUS_WAITING;
  1362. move.startTime = gameLocal.time;
  1363. move.speed = 0.0f;
  1364. AI_MOVE_DONE = true;
  1365. AI_FORWARD = false;
  1366. AI_DEST_UNREACHABLE = false;
  1367. return true;
  1368. }
  1369. /*
  1370. =====================
  1371. idAI::FaceEntity
  1372. Continually face the entity position. MoveDone is always true in this case.
  1373. =====================
  1374. */
  1375. bool idAI::FaceEntity( idEntity *ent ) {
  1376. if ( !ent ) {
  1377. StopMove( MOVE_STATUS_DEST_NOT_FOUND );
  1378. return false;
  1379. }
  1380. idVec3 entityOrg = ent->GetPhysics()->GetOrigin();
  1381. TurnToward( entityOrg );
  1382. move.goalEntity = ent;
  1383. move.moveDest = physicsObj.GetOrigin();
  1384. move.moveCommand = MOVE_FACE_ENTITY;
  1385. move.moveStatus = MOVE_STATUS_WAITING;
  1386. move.startTime = gameLocal.time;
  1387. move.speed = 0.0f;
  1388. AI_MOVE_DONE = true;
  1389. AI_FORWARD = false;
  1390. AI_DEST_UNREACHABLE = false;
  1391. return true;
  1392. }
  1393. /*
  1394. =====================
  1395. idAI::DirectMoveToPosition
  1396. =====================
  1397. */
  1398. bool idAI::DirectMoveToPosition( const idVec3 &pos ) {
  1399. if ( ReachedPos( pos, move.moveCommand ) ) {
  1400. StopMove( MOVE_STATUS_DONE );
  1401. return true;
  1402. }
  1403. move.moveDest = pos;
  1404. move.goalEntity = NULL;
  1405. move.moveCommand = MOVE_TO_POSITION_DIRECT;
  1406. move.moveStatus = MOVE_STATUS_MOVING;
  1407. move.startTime = gameLocal.time;
  1408. move.speed = fly_speed;
  1409. AI_MOVE_DONE = false;
  1410. AI_DEST_UNREACHABLE = false;
  1411. AI_FORWARD = true;
  1412. if ( move.moveType == MOVETYPE_FLY ) {
  1413. idVec3 dir = pos - physicsObj.GetOrigin();
  1414. dir.Normalize();
  1415. dir *= fly_speed;
  1416. physicsObj.SetLinearVelocity( dir );
  1417. }
  1418. return true;
  1419. }
  1420. /*
  1421. =====================
  1422. idAI::MoveToEnemyHeight
  1423. =====================
  1424. */
  1425. bool idAI::MoveToEnemyHeight() {
  1426. idActor *enemyEnt = enemy.GetEntity();
  1427. if ( !enemyEnt || ( move.moveType != MOVETYPE_FLY ) ) {
  1428. StopMove( MOVE_STATUS_DEST_NOT_FOUND );
  1429. return false;
  1430. }
  1431. move.moveDest.z = lastVisibleEnemyPos.z + enemyEnt->EyeOffset().z + fly_offset;
  1432. move.goalEntity = enemyEnt;
  1433. move.moveCommand = MOVE_TO_ENEMYHEIGHT;
  1434. move.moveStatus = MOVE_STATUS_MOVING;
  1435. move.startTime = gameLocal.time;
  1436. move.speed = 0.0f;
  1437. AI_MOVE_DONE = false;
  1438. AI_DEST_UNREACHABLE = false;
  1439. AI_FORWARD = false;
  1440. return true;
  1441. }
  1442. /*
  1443. =====================
  1444. idAI::MoveToEnemy
  1445. =====================
  1446. */
  1447. bool idAI::MoveToEnemy() {
  1448. int areaNum;
  1449. aasPath_t path;
  1450. idActor *enemyEnt = enemy.GetEntity();
  1451. if ( !enemyEnt ) {
  1452. StopMove( MOVE_STATUS_DEST_NOT_FOUND );
  1453. return false;
  1454. }
  1455. if ( ReachedPos( lastVisibleReachableEnemyPos, MOVE_TO_ENEMY ) ) {
  1456. if ( !ReachedPos( lastVisibleEnemyPos, MOVE_TO_ENEMY ) || !AI_ENEMY_VISIBLE ) {
  1457. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1458. AI_DEST_UNREACHABLE = true;
  1459. return false;
  1460. }
  1461. StopMove( MOVE_STATUS_DONE );
  1462. return true;
  1463. }
  1464. idVec3 pos = lastVisibleReachableEnemyPos;
  1465. move.toAreaNum = 0;
  1466. if ( aas ) {
  1467. move.toAreaNum = PointReachableAreaNum( pos );
  1468. aas->PushPointIntoAreaNum( move.toAreaNum, pos );
  1469. areaNum = PointReachableAreaNum( physicsObj.GetOrigin() );
  1470. if ( !PathToGoal( path, areaNum, physicsObj.GetOrigin(), move.toAreaNum, pos ) ) {
  1471. AI_DEST_UNREACHABLE = true;
  1472. return false;
  1473. }
  1474. }
  1475. if ( !move.toAreaNum ) {
  1476. // if only trying to update the enemy position
  1477. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  1478. if ( !aas ) {
  1479. // keep the move destination up to date for wandering
  1480. move.moveDest = pos;
  1481. }
  1482. return false;
  1483. }
  1484. if ( !NewWanderDir( pos ) ) {
  1485. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1486. AI_DEST_UNREACHABLE = true;
  1487. return false;
  1488. }
  1489. }
  1490. if ( move.moveCommand != MOVE_TO_ENEMY ) {
  1491. move.moveCommand = MOVE_TO_ENEMY;
  1492. move.startTime = gameLocal.time;
  1493. }
  1494. move.moveDest = pos;
  1495. move.goalEntity = enemyEnt;
  1496. move.speed = fly_speed;
  1497. move.moveStatus = MOVE_STATUS_MOVING;
  1498. AI_MOVE_DONE = false;
  1499. AI_DEST_UNREACHABLE = false;
  1500. AI_FORWARD = true;
  1501. return true;
  1502. }
  1503. /*
  1504. =====================
  1505. idAI::MoveToEntity
  1506. =====================
  1507. */
  1508. bool idAI::MoveToEntity( idEntity *ent ) {
  1509. int areaNum;
  1510. aasPath_t path;
  1511. idVec3 pos;
  1512. if ( !ent ) {
  1513. StopMove( MOVE_STATUS_DEST_NOT_FOUND );
  1514. return false;
  1515. }
  1516. pos = ent->GetPhysics()->GetOrigin();
  1517. if ( ( move.moveType != MOVETYPE_FLY ) && ( ( move.moveCommand != MOVE_TO_ENTITY ) || ( move.goalEntityOrigin != pos ) ) ) {
  1518. ent->GetFloorPos( 64.0f, pos );
  1519. }
  1520. if ( ReachedPos( pos, MOVE_TO_ENTITY ) ) {
  1521. StopMove( MOVE_STATUS_DONE );
  1522. return true;
  1523. }
  1524. move.toAreaNum = 0;
  1525. if ( aas ) {
  1526. move.toAreaNum = PointReachableAreaNum( pos );
  1527. aas->PushPointIntoAreaNum( move.toAreaNum, pos );
  1528. areaNum = PointReachableAreaNum( physicsObj.GetOrigin() );
  1529. if ( !PathToGoal( path, areaNum, physicsObj.GetOrigin(), move.toAreaNum, pos ) ) {
  1530. AI_DEST_UNREACHABLE = true;
  1531. return false;
  1532. }
  1533. }
  1534. if ( !move.toAreaNum ) {
  1535. // if only trying to update the entity position
  1536. if ( move.moveCommand == MOVE_TO_ENTITY ) {
  1537. if ( !aas ) {
  1538. // keep the move destination up to date for wandering
  1539. move.moveDest = pos;
  1540. }
  1541. return false;
  1542. }
  1543. if ( !NewWanderDir( pos ) ) {
  1544. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1545. AI_DEST_UNREACHABLE = true;
  1546. return false;
  1547. }
  1548. }
  1549. if ( ( move.moveCommand != MOVE_TO_ENTITY ) || ( move.goalEntity.GetEntity() != ent ) ) {
  1550. move.startTime = gameLocal.time;
  1551. move.goalEntity = ent;
  1552. move.moveCommand = MOVE_TO_ENTITY;
  1553. }
  1554. move.moveDest = pos;
  1555. move.goalEntityOrigin = ent->GetPhysics()->GetOrigin();
  1556. move.moveStatus = MOVE_STATUS_MOVING;
  1557. move.speed = fly_speed;
  1558. AI_MOVE_DONE = false;
  1559. AI_DEST_UNREACHABLE = false;
  1560. AI_FORWARD = true;
  1561. return true;
  1562. }
  1563. /*
  1564. =====================
  1565. idAI::MoveOutOfRange
  1566. =====================
  1567. */
  1568. bool idAI::MoveOutOfRange( idEntity *ent, float range ) {
  1569. int areaNum;
  1570. aasObstacle_t obstacle;
  1571. aasGoal_t goal;
  1572. idBounds bounds;
  1573. idVec3 pos;
  1574. if ( !aas || !ent ) {
  1575. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1576. AI_DEST_UNREACHABLE = true;
  1577. return false;
  1578. }
  1579. const idVec3 &org = physicsObj.GetOrigin();
  1580. areaNum = PointReachableAreaNum( org );
  1581. // consider the entity the monster is getting close to as an obstacle
  1582. obstacle.absBounds = ent->GetPhysics()->GetAbsBounds();
  1583. if ( ent == enemy.GetEntity() ) {
  1584. pos = lastVisibleEnemyPos;
  1585. } else {
  1586. pos = ent->GetPhysics()->GetOrigin();
  1587. }
  1588. idAASFindAreaOutOfRange findGoal( pos, range );
  1589. if ( !aas->FindNearestGoal( goal, areaNum, org, pos, travelFlags, &obstacle, 1, findGoal ) ) {
  1590. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1591. AI_DEST_UNREACHABLE = true;
  1592. return false;
  1593. }
  1594. if ( ReachedPos( goal.origin, move.moveCommand ) ) {
  1595. StopMove( MOVE_STATUS_DONE );
  1596. return true;
  1597. }
  1598. move.moveDest = goal.origin;
  1599. move.toAreaNum = goal.areaNum;
  1600. move.goalEntity = ent;
  1601. move.moveCommand = MOVE_OUT_OF_RANGE;
  1602. move.moveStatus = MOVE_STATUS_MOVING;
  1603. move.range = range;
  1604. move.speed = fly_speed;
  1605. move.startTime = gameLocal.time;
  1606. AI_MOVE_DONE = false;
  1607. AI_DEST_UNREACHABLE = false;
  1608. AI_FORWARD = true;
  1609. return true;
  1610. }
  1611. /*
  1612. =====================
  1613. idAI::MoveToAttackPosition
  1614. =====================
  1615. */
  1616. bool idAI::MoveToAttackPosition( idEntity *ent, int attack_anim ) {
  1617. int areaNum;
  1618. aasObstacle_t obstacle;
  1619. aasGoal_t goal;
  1620. idBounds bounds;
  1621. idVec3 pos;
  1622. if ( !aas || !ent ) {
  1623. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1624. AI_DEST_UNREACHABLE = true;
  1625. return false;
  1626. }
  1627. const idVec3 &org = physicsObj.GetOrigin();
  1628. areaNum = PointReachableAreaNum( org );
  1629. // consider the entity the monster is getting close to as an obstacle
  1630. obstacle.absBounds = ent->GetPhysics()->GetAbsBounds();
  1631. if ( ent == enemy.GetEntity() ) {
  1632. pos = lastVisibleEnemyPos;
  1633. } else {
  1634. pos = ent->GetPhysics()->GetOrigin();
  1635. }
  1636. idAASFindAttackPosition findGoal( this, physicsObj.GetGravityAxis(), ent, pos, missileLaunchOffset[ attack_anim ] );
  1637. if ( !aas->FindNearestGoal( goal, areaNum, org, pos, travelFlags, &obstacle, 1, findGoal ) ) {
  1638. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1639. AI_DEST_UNREACHABLE = true;
  1640. return false;
  1641. }
  1642. move.moveDest = goal.origin;
  1643. move.toAreaNum = goal.areaNum;
  1644. move.goalEntity = ent;
  1645. move.moveCommand = MOVE_TO_ATTACK_POSITION;
  1646. move.moveStatus = MOVE_STATUS_MOVING;
  1647. move.speed = fly_speed;
  1648. move.startTime = gameLocal.time;
  1649. move.anim = attack_anim;
  1650. AI_MOVE_DONE = false;
  1651. AI_DEST_UNREACHABLE = false;
  1652. AI_FORWARD = true;
  1653. return true;
  1654. }
  1655. /*
  1656. =====================
  1657. idAI::MoveToPosition
  1658. =====================
  1659. */
  1660. bool idAI::MoveToPosition( const idVec3 &pos ) {
  1661. idVec3 org;
  1662. int areaNum;
  1663. aasPath_t path;
  1664. if ( ReachedPos( pos, move.moveCommand ) ) {
  1665. StopMove( MOVE_STATUS_DONE );
  1666. return true;
  1667. }
  1668. org = pos;
  1669. move.toAreaNum = 0;
  1670. if ( aas ) {
  1671. move.toAreaNum = PointReachableAreaNum( org );
  1672. aas->PushPointIntoAreaNum( move.toAreaNum, org );
  1673. areaNum = PointReachableAreaNum( physicsObj.GetOrigin() );
  1674. if ( !PathToGoal( path, areaNum, physicsObj.GetOrigin(), move.toAreaNum, org ) ) {
  1675. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1676. AI_DEST_UNREACHABLE = true;
  1677. return false;
  1678. }
  1679. }
  1680. if ( !move.toAreaNum && !NewWanderDir( org ) ) {
  1681. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1682. AI_DEST_UNREACHABLE = true;
  1683. return false;
  1684. }
  1685. move.moveDest = org;
  1686. move.goalEntity = NULL;
  1687. move.moveCommand = MOVE_TO_POSITION;
  1688. move.moveStatus = MOVE_STATUS_MOVING;
  1689. move.startTime = gameLocal.time;
  1690. move.speed = fly_speed;
  1691. AI_MOVE_DONE = false;
  1692. AI_DEST_UNREACHABLE = false;
  1693. AI_FORWARD = true;
  1694. return true;
  1695. }
  1696. /*
  1697. =====================
  1698. idAI::MoveToCover
  1699. =====================
  1700. */
  1701. bool idAI::MoveToCover( idEntity *entity, const idVec3 &hideFromPos ) {
  1702. int areaNum;
  1703. aasObstacle_t obstacle;
  1704. aasGoal_t hideGoal;
  1705. idBounds bounds;
  1706. if ( !aas || !entity ) {
  1707. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1708. AI_DEST_UNREACHABLE = true;
  1709. return false;
  1710. }
  1711. const idVec3 &org = physicsObj.GetOrigin();
  1712. areaNum = PointReachableAreaNum( org );
  1713. // consider the entity the monster tries to hide from as an obstacle
  1714. obstacle.absBounds = entity->GetPhysics()->GetAbsBounds();
  1715. idAASFindCover findCover( hideFromPos );
  1716. if ( !aas->FindNearestGoal( hideGoal, areaNum, org, hideFromPos, travelFlags, &obstacle, 1, findCover ) ) {
  1717. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1718. AI_DEST_UNREACHABLE = true;
  1719. return false;
  1720. }
  1721. if ( ReachedPos( hideGoal.origin, move.moveCommand ) ) {
  1722. StopMove( MOVE_STATUS_DONE );
  1723. return true;
  1724. }
  1725. move.moveDest = hideGoal.origin;
  1726. move.toAreaNum = hideGoal.areaNum;
  1727. move.goalEntity = entity;
  1728. move.moveCommand = MOVE_TO_COVER;
  1729. move.moveStatus = MOVE_STATUS_MOVING;
  1730. move.startTime = gameLocal.time;
  1731. move.speed = fly_speed;
  1732. AI_MOVE_DONE = false;
  1733. AI_DEST_UNREACHABLE = false;
  1734. AI_FORWARD = true;
  1735. return true;
  1736. }
  1737. /*
  1738. =====================
  1739. idAI::SlideToPosition
  1740. =====================
  1741. */
  1742. bool idAI::SlideToPosition( const idVec3 &pos, float time ) {
  1743. StopMove( MOVE_STATUS_DONE );
  1744. move.moveDest = pos;
  1745. move.goalEntity = NULL;
  1746. move.moveCommand = MOVE_SLIDE_TO_POSITION;
  1747. move.moveStatus = MOVE_STATUS_MOVING;
  1748. move.startTime = gameLocal.time;
  1749. move.duration = idPhysics::SnapTimeToPhysicsFrame( SEC2MS( time ) );
  1750. AI_MOVE_DONE = false;
  1751. AI_DEST_UNREACHABLE = false;
  1752. AI_FORWARD = false;
  1753. if ( move.duration > 0 ) {
  1754. move.moveDir = ( pos - physicsObj.GetOrigin() ) / MS2SEC( move.duration );
  1755. if ( move.moveType != MOVETYPE_FLY ) {
  1756. move.moveDir.z = 0.0f;
  1757. }
  1758. move.speed = move.moveDir.LengthFast();
  1759. }
  1760. return true;
  1761. }
  1762. /*
  1763. =====================
  1764. idAI::WanderAround
  1765. =====================
  1766. */
  1767. bool idAI::WanderAround() {
  1768. StopMove( MOVE_STATUS_DONE );
  1769. move.moveDest = physicsObj.GetOrigin() + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 256.0f;
  1770. if ( !NewWanderDir( move.moveDest ) ) {
  1771. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1772. AI_DEST_UNREACHABLE = true;
  1773. return false;
  1774. }
  1775. move.moveCommand = MOVE_WANDER;
  1776. move.moveStatus = MOVE_STATUS_MOVING;
  1777. move.startTime = gameLocal.time;
  1778. move.speed = fly_speed;
  1779. AI_MOVE_DONE = false;
  1780. AI_FORWARD = true;
  1781. return true;
  1782. }
  1783. /*
  1784. =====================
  1785. idAI::MoveDone
  1786. =====================
  1787. */
  1788. bool idAI::MoveDone() const {
  1789. return ( move.moveCommand == MOVE_NONE );
  1790. }
  1791. /*
  1792. ================
  1793. idAI::StepDirection
  1794. ================
  1795. */
  1796. bool idAI::StepDirection( float dir ) {
  1797. predictedPath_t path;
  1798. idVec3 org;
  1799. move.wanderYaw = dir;
  1800. move.moveDir = idAngles( 0, move.wanderYaw, 0 ).ToForward();
  1801. org = physicsObj.GetOrigin();
  1802. idAI::PredictPath( this, aas, org, move.moveDir * 48.0f, 1000, 1000, ( move.moveType == MOVETYPE_FLY ) ? SE_BLOCKED : ( SE_ENTER_OBSTACLE | SE_BLOCKED | SE_ENTER_LEDGE_AREA ), path );
  1803. if ( path.blockingEntity && ( ( move.moveCommand == MOVE_TO_ENEMY ) || ( move.moveCommand == MOVE_TO_ENTITY ) ) && ( path.blockingEntity == move.goalEntity.GetEntity() ) ) {
  1804. // don't report being blocked if we ran into our goal entity
  1805. return true;
  1806. }
  1807. if ( ( move.moveType == MOVETYPE_FLY ) && ( path.endEvent == SE_BLOCKED ) ) {
  1808. float z;
  1809. move.moveDir = path.endVelocity * 1.0f / 48.0f;
  1810. // trace down to the floor and see if we can go forward
  1811. idAI::PredictPath( this, aas, org, idVec3( 0.0f, 0.0f, -1024.0f ), 1000, 1000, SE_BLOCKED, path );
  1812. idVec3 floorPos = path.endPos;
  1813. idAI::PredictPath( this, aas, floorPos, move.moveDir * 48.0f, 1000, 1000, SE_BLOCKED, path );
  1814. if ( !path.endEvent ) {
  1815. move.moveDir.z = -1.0f;
  1816. return true;
  1817. }
  1818. // trace up to see if we can go over something and go forward
  1819. idAI::PredictPath( this, aas, org, idVec3( 0.0f, 0.0f, 256.0f ), 1000, 1000, SE_BLOCKED, path );
  1820. idVec3 ceilingPos = path.endPos;
  1821. for( z = org.z; z <= ceilingPos.z + 64.0f; z += 64.0f ) {
  1822. idVec3 start;
  1823. if ( z <= ceilingPos.z ) {
  1824. start.x = org.x;
  1825. start.y = org.y;
  1826. start.z = z;
  1827. } else {
  1828. start = ceilingPos;
  1829. }
  1830. idAI::PredictPath( this, aas, start, move.moveDir * 48.0f, 1000, 1000, SE_BLOCKED, path );
  1831. if ( !path.endEvent ) {
  1832. move.moveDir.z = 1.0f;
  1833. return true;
  1834. }
  1835. }
  1836. return false;
  1837. }
  1838. return ( path.endEvent == 0 );
  1839. }
  1840. /*
  1841. ================
  1842. idAI::NewWanderDir
  1843. ================
  1844. */
  1845. bool idAI::NewWanderDir( const idVec3 &dest ) {
  1846. float deltax, deltay;
  1847. float d[ 3 ];
  1848. float tdir, olddir, turnaround;
  1849. move.nextWanderTime = gameLocal.time + ( gameLocal.random.RandomFloat() * 500 + 500 );
  1850. olddir = idMath::AngleNormalize360( ( int )( current_yaw / 45 ) * 45 );
  1851. turnaround = idMath::AngleNormalize360( olddir - 180 );
  1852. idVec3 org = physicsObj.GetOrigin();
  1853. deltax = dest.x - org.x;
  1854. deltay = dest.y - org.y;
  1855. if ( deltax > 10 ) {
  1856. d[ 1 ]= 0;
  1857. } else if ( deltax < -10 ) {
  1858. d[ 1 ] = 180;
  1859. } else {
  1860. d[ 1 ] = DI_NODIR;
  1861. }
  1862. if ( deltay < -10 ) {
  1863. d[ 2 ] = 270;
  1864. } else if ( deltay > 10 ) {
  1865. d[ 2 ] = 90;
  1866. } else {
  1867. d[ 2 ] = DI_NODIR;
  1868. }
  1869. // try direct route
  1870. if ( d[ 1 ] != DI_NODIR && d[ 2 ] != DI_NODIR ) {
  1871. if ( d[ 1 ] == 0 ) {
  1872. tdir = d[ 2 ] == 90 ? 45 : 315;
  1873. } else {
  1874. tdir = d[ 2 ] == 90 ? 135 : 215;
  1875. }
  1876. if ( tdir != turnaround && StepDirection( tdir ) ) {
  1877. return true;
  1878. }
  1879. }
  1880. // try other directions
  1881. if ( ( gameLocal.random.RandomInt() & 1 ) || abs( deltay ) > abs( deltax ) ) {
  1882. tdir = d[ 1 ];
  1883. d[ 1 ] = d[ 2 ];
  1884. d[ 2 ] = tdir;
  1885. }
  1886. if ( d[ 1 ] != DI_NODIR && d[ 1 ] != turnaround && StepDirection( d[1] ) ) {
  1887. return true;
  1888. }
  1889. if ( d[ 2 ] != DI_NODIR && d[ 2 ] != turnaround && StepDirection( d[ 2 ] ) ) {
  1890. return true;
  1891. }
  1892. // there is no direct path to the player, so pick another direction
  1893. if ( olddir != DI_NODIR && StepDirection( olddir ) ) {
  1894. return true;
  1895. }
  1896. // randomly determine direction of search
  1897. if ( gameLocal.random.RandomInt() & 1 ) {
  1898. for( tdir = 0; tdir <= 315; tdir += 45 ) {
  1899. if ( tdir != turnaround && StepDirection( tdir ) ) {
  1900. return true;
  1901. }
  1902. }
  1903. } else {
  1904. for ( tdir = 315; tdir >= 0; tdir -= 45 ) {
  1905. if ( tdir != turnaround && StepDirection( tdir ) ) {
  1906. return true;
  1907. }
  1908. }
  1909. }
  1910. if ( turnaround != DI_NODIR && StepDirection( turnaround ) ) {
  1911. return true;
  1912. }
  1913. // can't move
  1914. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1915. return false;
  1916. }
  1917. /*
  1918. =====================
  1919. idAI::GetMovePos
  1920. =====================
  1921. */
  1922. bool idAI::GetMovePos( idVec3 &seekPos ) {
  1923. int areaNum;
  1924. aasPath_t path;
  1925. bool result;
  1926. idVec3 org;
  1927. org = physicsObj.GetOrigin();
  1928. seekPos = org;
  1929. switch( move.moveCommand ) {
  1930. case MOVE_NONE :
  1931. seekPos = move.moveDest;
  1932. return false;
  1933. break;
  1934. case MOVE_FACE_ENEMY :
  1935. case MOVE_FACE_ENTITY :
  1936. seekPos = move.moveDest;
  1937. return false;
  1938. break;
  1939. case MOVE_TO_POSITION_DIRECT :
  1940. seekPos = move.moveDest;
  1941. if ( ReachedPos( move.moveDest, move.moveCommand ) ) {
  1942. StopMove( MOVE_STATUS_DONE );
  1943. }
  1944. return false;
  1945. break;
  1946. case MOVE_SLIDE_TO_POSITION :
  1947. seekPos = org;
  1948. return false;
  1949. break;
  1950. }
  1951. if ( move.moveCommand == MOVE_TO_ENTITY ) {
  1952. MoveToEntity( move.goalEntity.GetEntity() );
  1953. }
  1954. move.moveStatus = MOVE_STATUS_MOVING;
  1955. result = false;
  1956. if ( gameLocal.time > move.blockTime ) {
  1957. if ( move.moveCommand == MOVE_WANDER ) {
  1958. move.moveDest = org + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 256.0f;
  1959. } else {
  1960. if ( ReachedPos( move.moveDest, move.moveCommand ) ) {
  1961. StopMove( MOVE_STATUS_DONE );
  1962. seekPos = org;
  1963. return false;
  1964. }
  1965. }
  1966. if ( aas && move.toAreaNum ) {
  1967. areaNum = PointReachableAreaNum( org );
  1968. if ( PathToGoal( path, areaNum, org, move.toAreaNum, move.moveDest ) ) {
  1969. seekPos = path.moveGoal;
  1970. result = true;
  1971. move.nextWanderTime = 0;
  1972. } else {
  1973. AI_DEST_UNREACHABLE = true;
  1974. }
  1975. }
  1976. }
  1977. if ( !result ) {
  1978. // wander around
  1979. if ( ( gameLocal.time > move.nextWanderTime ) || !StepDirection( move.wanderYaw ) ) {
  1980. result = NewWanderDir( move.moveDest );
  1981. if ( !result ) {
  1982. StopMove( MOVE_STATUS_DEST_UNREACHABLE );
  1983. AI_DEST_UNREACHABLE = true;
  1984. seekPos = org;
  1985. return false;
  1986. }
  1987. } else {
  1988. result = true;
  1989. }
  1990. seekPos = org + move.moveDir * 2048.0f;
  1991. if ( ai_debugMove.GetBool() ) {
  1992. gameRenderWorld->DebugLine( colorYellow, org, seekPos, 1, true );
  1993. }
  1994. } else {
  1995. AI_DEST_UNREACHABLE = false;
  1996. }
  1997. if ( result && ( ai_debugMove.GetBool() ) ) {
  1998. gameRenderWorld->DebugLine( colorCyan, physicsObj.GetOrigin(), seekPos );
  1999. }
  2000. return result;
  2001. }
  2002. /*
  2003. =====================
  2004. idAI::EntityCanSeePos
  2005. =====================
  2006. */
  2007. bool idAI::EntityCanSeePos( idActor *actor, const idVec3 &actorOrigin, const idVec3 &pos ) {
  2008. idVec3 eye, point;
  2009. trace_t results;
  2010. pvsHandle_t handle;
  2011. handle = gameLocal.pvs.SetupCurrentPVS( actor->GetPVSAreas(), actor->GetNumPVSAreas() );
  2012. if ( !gameLocal.pvs.InCurrentPVS( handle, GetPVSAreas(), GetNumPVSAreas() ) ) {
  2013. gameLocal.pvs.FreeCurrentPVS( handle );
  2014. return false;
  2015. }
  2016. gameLocal.pvs.FreeCurrentPVS( handle );
  2017. eye = actorOrigin + actor->EyeOffset();
  2018. point = pos;
  2019. point[2] += 1.0f;
  2020. physicsObj.DisableClip();
  2021. gameLocal.clip.TracePoint( results, eye, point, MASK_SOLID, actor );
  2022. if ( results.fraction >= 1.0f || ( gameLocal.GetTraceEntity( results ) == this ) ) {
  2023. physicsObj.EnableClip();
  2024. return true;
  2025. }
  2026. const idBounds &bounds = physicsObj.GetBounds();
  2027. point[2] += bounds[1][2] - bounds[0][2];
  2028. gameLocal.clip.TracePoint( results, eye, point, MASK_SOLID, actor );
  2029. physicsObj.EnableClip();
  2030. if ( results.fraction >= 1.0f || ( gameLocal.GetTraceEntity( results ) == this ) ) {
  2031. return true;
  2032. }
  2033. return false;
  2034. }
  2035. /*
  2036. =====================
  2037. idAI::BlockedFailSafe
  2038. =====================
  2039. */
  2040. void idAI::BlockedFailSafe() {
  2041. if ( !ai_blockedFailSafe.GetBool() || blockedRadius < 0.0f ) {
  2042. return;
  2043. }
  2044. if ( !physicsObj.OnGround() || enemy.GetEntity() == NULL ||
  2045. ( physicsObj.GetOrigin() - move.lastMoveOrigin ).LengthSqr() > Square( blockedRadius ) ) {
  2046. move.lastMoveOrigin = physicsObj.GetOrigin();
  2047. move.lastMoveTime = gameLocal.time;
  2048. }
  2049. if ( move.lastMoveTime < gameLocal.time - blockedMoveTime ) {
  2050. if ( lastAttackTime < gameLocal.time - blockedAttackTime ) {
  2051. AI_BLOCKED = true;
  2052. move.lastMoveTime = gameLocal.time;
  2053. }
  2054. }
  2055. }
  2056. /***********************************************************************
  2057. turning
  2058. ***********************************************************************/
  2059. /*
  2060. =====================
  2061. idAI::Turn
  2062. =====================
  2063. */
  2064. void idAI::Turn() {
  2065. float diff;
  2066. float diff2;
  2067. float turnAmount;
  2068. animFlags_t animflags;
  2069. if ( !turnRate ) {
  2070. return;
  2071. }
  2072. // check if the animator has marker this anim as non-turning
  2073. if ( !legsAnim.Disabled() && !legsAnim.AnimDone( 0 ) ) {
  2074. animflags = legsAnim.GetAnimFlags();
  2075. } else {
  2076. animflags = torsoAnim.GetAnimFlags();
  2077. }
  2078. if ( animflags.ai_no_turn ) {
  2079. return;
  2080. }
  2081. if ( anim_turn_angles && animflags.anim_turn ) {
  2082. idMat3 rotateAxis;
  2083. // set the blend between no turn and full turn
  2084. float frac = anim_turn_amount / anim_turn_angles;
  2085. animator.CurrentAnim( ANIMCHANNEL_LEGS )->SetSyncedAnimWeight( 0, 1.0f - frac );
  2086. animator.CurrentAnim( ANIMCHANNEL_LEGS )->SetSyncedAnimWeight( 1, frac );
  2087. animator.CurrentAnim( ANIMCHANNEL_TORSO )->SetSyncedAnimWeight( 0, 1.0f - frac );
  2088. animator.CurrentAnim( ANIMCHANNEL_TORSO )->SetSyncedAnimWeight( 1, frac );
  2089. // get the total rotation from the start of the anim
  2090. animator.GetDeltaRotation( 0, gameLocal.time, rotateAxis );
  2091. current_yaw = idMath::AngleNormalize180( anim_turn_yaw + rotateAxis[ 0 ].ToYaw() );
  2092. } else {
  2093. diff = idMath::AngleNormalize180( ideal_yaw - current_yaw );
  2094. turnVel += AI_TURN_SCALE * diff * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2095. if ( turnVel > turnRate ) {
  2096. turnVel = turnRate;
  2097. } else if ( turnVel < -turnRate ) {
  2098. turnVel = -turnRate;
  2099. }
  2100. turnAmount = turnVel * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2101. if ( ( diff >= 0.0f ) && ( turnAmount >= diff ) ) {
  2102. turnVel = diff / MS2SEC( gameLocal.time - gameLocal.previousTime );
  2103. turnAmount = diff;
  2104. } else if ( ( diff <= 0.0f ) && ( turnAmount <= diff ) ) {
  2105. turnVel = diff / MS2SEC( gameLocal.time - gameLocal.previousTime );
  2106. turnAmount = diff;
  2107. }
  2108. current_yaw += turnAmount;
  2109. current_yaw = idMath::AngleNormalize180( current_yaw );
  2110. diff2 = idMath::AngleNormalize180( ideal_yaw - current_yaw );
  2111. if ( idMath::Fabs( diff2 ) < 0.1f ) {
  2112. current_yaw = ideal_yaw;
  2113. }
  2114. }
  2115. viewAxis = idAngles( 0, current_yaw, 0 ).ToMat3();
  2116. if ( ai_debugMove.GetBool() ) {
  2117. const idVec3 &org = physicsObj.GetOrigin();
  2118. gameRenderWorld->DebugLine( colorRed, org, org + idAngles( 0, ideal_yaw, 0 ).ToForward() * 64, 1 );
  2119. gameRenderWorld->DebugLine( colorGreen, org, org + idAngles( 0, current_yaw, 0 ).ToForward() * 48, 1 );
  2120. gameRenderWorld->DebugLine( colorYellow, org, org + idAngles( 0, current_yaw + turnVel, 0 ).ToForward() * 32, 1 );
  2121. }
  2122. }
  2123. /*
  2124. =====================
  2125. idAI::FacingIdeal
  2126. =====================
  2127. */
  2128. bool idAI::FacingIdeal() {
  2129. float diff;
  2130. if ( !turnRate ) {
  2131. return true;
  2132. }
  2133. diff = idMath::AngleNormalize180( current_yaw - ideal_yaw );
  2134. if ( idMath::Fabs( diff ) < 0.01f ) {
  2135. // force it to be exact
  2136. current_yaw = ideal_yaw;
  2137. return true;
  2138. }
  2139. return false;
  2140. }
  2141. /*
  2142. =====================
  2143. idAI::TurnToward
  2144. =====================
  2145. */
  2146. bool idAI::TurnToward( float yaw ) {
  2147. ideal_yaw = idMath::AngleNormalize180( yaw );
  2148. bool result = FacingIdeal();
  2149. return result;
  2150. }
  2151. /*
  2152. =====================
  2153. idAI::TurnToward
  2154. =====================
  2155. */
  2156. bool idAI::TurnToward( const idVec3 &pos ) {
  2157. idVec3 dir;
  2158. idVec3 local_dir;
  2159. float lengthSqr;
  2160. dir = pos - physicsObj.GetOrigin();
  2161. physicsObj.GetGravityAxis().ProjectVector( dir, local_dir );
  2162. local_dir.z = 0.0f;
  2163. lengthSqr = local_dir.LengthSqr();
  2164. if ( lengthSqr > Square( 2.0f ) || ( lengthSqr > Square( 0.1f ) && enemy.GetEntity() == NULL ) ) {
  2165. ideal_yaw = idMath::AngleNormalize180( local_dir.ToYaw() );
  2166. }
  2167. bool result = FacingIdeal();
  2168. return result;
  2169. }
  2170. /***********************************************************************
  2171. Movement
  2172. ***********************************************************************/
  2173. /*
  2174. ================
  2175. idAI::ApplyImpulse
  2176. ================
  2177. */
  2178. void idAI::ApplyImpulse( idEntity *ent, int id, const idVec3 &point, const idVec3 &impulse ) {
  2179. // FIXME: Jim take a look at this and see if this is a reasonable thing to do
  2180. // instead of a spawnArg flag.. Sabaoth is the only slide monster ( and should be the only one for D3 )
  2181. // and we don't want him taking physics impulses as it can knock him off the path
  2182. if ( move.moveType != MOVETYPE_STATIC && move.moveType != MOVETYPE_SLIDE ) {
  2183. idActor::ApplyImpulse( ent, id, point, impulse );
  2184. }
  2185. }
  2186. /*
  2187. =====================
  2188. idAI::GetMoveDelta
  2189. =====================
  2190. */
  2191. void idAI::GetMoveDelta( const idMat3 &oldaxis, const idMat3 &axis, idVec3 &delta ) {
  2192. idVec3 oldModelOrigin;
  2193. idVec3 modelOrigin;
  2194. animator.GetDelta( gameLocal.previousTime, gameLocal.time, delta );
  2195. delta = axis * delta;
  2196. if ( modelOffset != vec3_zero ) {
  2197. // the pivot of the monster's model is around its origin, and not around the bounding
  2198. // box's origin, so we have to compensate for this when the model is offset so that
  2199. // the monster still appears to rotate around it's origin.
  2200. oldModelOrigin = modelOffset * oldaxis;
  2201. modelOrigin = modelOffset * axis;
  2202. delta += oldModelOrigin - modelOrigin;
  2203. }
  2204. delta *= physicsObj.GetGravityAxis();
  2205. }
  2206. /*
  2207. =====================
  2208. idAI::CheckObstacleAvoidance
  2209. =====================
  2210. */
  2211. void idAI::CheckObstacleAvoidance( const idVec3 &goalPos, idVec3 &newPos ) {
  2212. idEntity *obstacle;
  2213. obstaclePath_t path;
  2214. idVec3 dir;
  2215. float dist;
  2216. bool foundPath;
  2217. if ( ignore_obstacles ) {
  2218. newPos = goalPos;
  2219. move.obstacle = NULL;
  2220. return;
  2221. }
  2222. const idVec3 &origin = physicsObj.GetOrigin();
  2223. obstacle = NULL;
  2224. AI_OBSTACLE_IN_PATH = false;
  2225. foundPath = FindPathAroundObstacles( &physicsObj, aas, enemy.GetEntity(), origin, goalPos, path );
  2226. if ( ai_showObstacleAvoidance.GetBool() ) {
  2227. gameRenderWorld->DebugLine( colorBlue, goalPos + idVec3( 1.0f, 1.0f, 0.0f ), goalPos + idVec3( 1.0f, 1.0f, 64.0f ), 1 );
  2228. gameRenderWorld->DebugLine( foundPath ? colorYellow : colorRed, path.seekPos, path.seekPos + idVec3( 0.0f, 0.0f, 64.0f ), 1 );
  2229. }
  2230. if ( !foundPath ) {
  2231. // couldn't get around obstacles
  2232. if ( path.firstObstacle ) {
  2233. AI_OBSTACLE_IN_PATH = true;
  2234. if ( physicsObj.GetAbsBounds().Expand( 2.0f ).IntersectsBounds( path.firstObstacle->GetPhysics()->GetAbsBounds() ) ) {
  2235. obstacle = path.firstObstacle;
  2236. }
  2237. } else if ( path.startPosObstacle ) {
  2238. AI_OBSTACLE_IN_PATH = true;
  2239. if ( physicsObj.GetAbsBounds().Expand( 2.0f ).IntersectsBounds( path.startPosObstacle->GetPhysics()->GetAbsBounds() ) ) {
  2240. obstacle = path.startPosObstacle;
  2241. }
  2242. } else {
  2243. // Blocked by wall
  2244. move.moveStatus = MOVE_STATUS_BLOCKED_BY_WALL;
  2245. }
  2246. #if 0
  2247. } else if ( path.startPosObstacle ) {
  2248. // check if we're past where the our origin was pushed out of the obstacle
  2249. dir = goalPos - origin;
  2250. dir.Normalize();
  2251. dist = ( path.seekPos - origin ) * dir;
  2252. if ( dist < 1.0f ) {
  2253. AI_OBSTACLE_IN_PATH = true;
  2254. obstacle = path.startPosObstacle;
  2255. }
  2256. #endif
  2257. } else if ( path.seekPosObstacle ) {
  2258. // if the AI is very close to the path.seekPos already and path.seekPosObstacle != NULL
  2259. // then we want to push the path.seekPosObstacle entity out of the way
  2260. AI_OBSTACLE_IN_PATH = true;
  2261. // check if we're past where the goalPos was pushed out of the obstacle
  2262. dir = goalPos - origin;
  2263. dir.Normalize();
  2264. dist = ( path.seekPos - origin ) * dir;
  2265. if ( dist < 1.0f ) {
  2266. obstacle = path.seekPosObstacle;
  2267. }
  2268. }
  2269. // if we had an obstacle, set our move status based on the type, and kick it out of the way if it's a moveable
  2270. if ( obstacle ) {
  2271. if ( obstacle->IsType( idActor::Type ) ) {
  2272. // monsters aren't kickable
  2273. if ( obstacle == enemy.GetEntity() ) {
  2274. move.moveStatus = MOVE_STATUS_BLOCKED_BY_ENEMY;
  2275. } else {
  2276. move.moveStatus = MOVE_STATUS_BLOCKED_BY_MONSTER;
  2277. }
  2278. } else {
  2279. // try kicking the object out of the way
  2280. move.moveStatus = MOVE_STATUS_BLOCKED_BY_OBJECT;
  2281. }
  2282. newPos = obstacle->GetPhysics()->GetOrigin();
  2283. //newPos = path.seekPos;
  2284. move.obstacle = obstacle;
  2285. } else {
  2286. newPos = path.seekPos;
  2287. move.obstacle = NULL;
  2288. }
  2289. }
  2290. /*
  2291. =====================
  2292. idAI::DeadMove
  2293. =====================
  2294. */
  2295. void idAI::DeadMove() {
  2296. idVec3 delta;
  2297. monsterMoveResult_t moveResult;
  2298. idVec3 org = physicsObj.GetOrigin();
  2299. GetMoveDelta( viewAxis, viewAxis, delta );
  2300. physicsObj.SetDelta( delta );
  2301. RunPhysics();
  2302. moveResult = physicsObj.GetMoveResult();
  2303. AI_ONGROUND = physicsObj.OnGround();
  2304. }
  2305. /*
  2306. =====================
  2307. idAI::AnimMove
  2308. =====================
  2309. */
  2310. void idAI::AnimMove() {
  2311. idVec3 goalPos;
  2312. idVec3 delta;
  2313. idVec3 goalDelta;
  2314. float goalDist;
  2315. monsterMoveResult_t moveResult;
  2316. idVec3 newDest;
  2317. idVec3 oldorigin = physicsObj.GetOrigin();
  2318. idMat3 oldaxis = viewAxis;
  2319. AI_BLOCKED = false;
  2320. if ( move.moveCommand < NUM_NONMOVING_COMMANDS ){
  2321. move.lastMoveOrigin.Zero();
  2322. move.lastMoveTime = gameLocal.time;
  2323. }
  2324. move.obstacle = NULL;
  2325. if ( ( move.moveCommand == MOVE_FACE_ENEMY ) && enemy.GetEntity() ) {
  2326. TurnToward( lastVisibleEnemyPos );
  2327. goalPos = oldorigin;
  2328. } else if ( ( move.moveCommand == MOVE_FACE_ENTITY ) && move.goalEntity.GetEntity() ) {
  2329. TurnToward( move.goalEntity.GetEntity()->GetPhysics()->GetOrigin() );
  2330. goalPos = oldorigin;
  2331. } else if ( GetMovePos( goalPos ) ) {
  2332. if ( move.moveCommand != MOVE_WANDER ) {
  2333. CheckObstacleAvoidance( goalPos, newDest );
  2334. TurnToward( newDest );
  2335. } else {
  2336. TurnToward( goalPos );
  2337. }
  2338. }
  2339. Turn();
  2340. if ( move.moveCommand == MOVE_SLIDE_TO_POSITION ) {
  2341. if ( gameLocal.time < move.startTime + move.duration ) {
  2342. goalPos = move.moveDest - move.moveDir * MS2SEC( move.startTime + move.duration - gameLocal.time );
  2343. delta = goalPos - oldorigin;
  2344. delta.z = 0.0f;
  2345. } else {
  2346. delta = move.moveDest - oldorigin;
  2347. delta.z = 0.0f;
  2348. StopMove( MOVE_STATUS_DONE );
  2349. }
  2350. } else if ( allowMove ) {
  2351. GetMoveDelta( oldaxis, viewAxis, delta );
  2352. } else {
  2353. delta.Zero();
  2354. }
  2355. if ( move.moveCommand == MOVE_TO_POSITION ) {
  2356. goalDelta = move.moveDest - oldorigin;
  2357. goalDist = goalDelta.LengthFast();
  2358. if ( goalDist < delta.LengthFast() ) {
  2359. delta = goalDelta;
  2360. }
  2361. }
  2362. physicsObj.UseFlyMove( false );
  2363. physicsObj.SetDelta( delta );
  2364. physicsObj.ForceDeltaMove( disableGravity );
  2365. RunPhysics();
  2366. if ( ai_debugMove.GetBool() ) {
  2367. gameRenderWorld->DebugLine( colorCyan, oldorigin, physicsObj.GetOrigin(), 5000 );
  2368. }
  2369. moveResult = physicsObj.GetMoveResult();
  2370. if ( !af_push_moveables && attack.Length() && TestMelee() ) {
  2371. DirectDamage( attack, enemy.GetEntity() );
  2372. } else {
  2373. idEntity *blockEnt = physicsObj.GetSlideMoveEntity();
  2374. if ( blockEnt != NULL && blockEnt->IsType( idMoveable::Type ) && blockEnt->GetPhysics()->IsPushable() ) {
  2375. KickObstacles( viewAxis[ 0 ], kickForce, blockEnt );
  2376. }
  2377. }
  2378. BlockedFailSafe();
  2379. AI_ONGROUND = physicsObj.OnGround();
  2380. idVec3 org = physicsObj.GetOrigin();
  2381. if ( oldorigin != org ) {
  2382. TouchTriggers();
  2383. }
  2384. if ( ai_debugMove.GetBool() ) {
  2385. gameRenderWorld->DebugBounds( colorMagenta, physicsObj.GetBounds(), org, 1 );
  2386. gameRenderWorld->DebugBounds( colorMagenta, physicsObj.GetBounds(), move.moveDest, 1 );
  2387. gameRenderWorld->DebugLine( colorYellow, org + EyeOffset(), org + EyeOffset() + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 16.0f, 1, true );
  2388. DrawRoute();
  2389. }
  2390. }
  2391. /*
  2392. =====================
  2393. Seek
  2394. =====================
  2395. */
  2396. idVec3 Seek( idVec3 &vel, const idVec3 &org, const idVec3 &goal, float prediction ) {
  2397. idVec3 predictedPos;
  2398. idVec3 goalDelta;
  2399. idVec3 seekVel;
  2400. // predict our position
  2401. predictedPos = org + vel * prediction;
  2402. goalDelta = goal - predictedPos;
  2403. seekVel = goalDelta * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2404. return seekVel;
  2405. }
  2406. /*
  2407. =====================
  2408. idAI::SlideMove
  2409. =====================
  2410. */
  2411. void idAI::SlideMove() {
  2412. idVec3 goalPos;
  2413. idVec3 delta;
  2414. idVec3 goalDelta;
  2415. float goalDist;
  2416. monsterMoveResult_t moveResult;
  2417. idVec3 newDest;
  2418. idVec3 oldorigin = physicsObj.GetOrigin();
  2419. idMat3 oldaxis = viewAxis;
  2420. AI_BLOCKED = false;
  2421. if ( move.moveCommand < NUM_NONMOVING_COMMANDS ){
  2422. move.lastMoveOrigin.Zero();
  2423. move.lastMoveTime = gameLocal.time;
  2424. }
  2425. move.obstacle = NULL;
  2426. if ( ( move.moveCommand == MOVE_FACE_ENEMY ) && enemy.GetEntity() ) {
  2427. TurnToward( lastVisibleEnemyPos );
  2428. goalPos = move.moveDest;
  2429. } else if ( ( move.moveCommand == MOVE_FACE_ENTITY ) && move.goalEntity.GetEntity() ) {
  2430. TurnToward( move.goalEntity.GetEntity()->GetPhysics()->GetOrigin() );
  2431. goalPos = move.moveDest;
  2432. } else if ( GetMovePos( goalPos ) ) {
  2433. CheckObstacleAvoidance( goalPos, newDest );
  2434. TurnToward( newDest );
  2435. goalPos = newDest;
  2436. }
  2437. if ( move.moveCommand == MOVE_SLIDE_TO_POSITION ) {
  2438. if ( gameLocal.time < move.startTime + move.duration ) {
  2439. goalPos = move.moveDest - move.moveDir * MS2SEC( move.startTime + move.duration - gameLocal.time );
  2440. } else {
  2441. goalPos = move.moveDest;
  2442. StopMove( MOVE_STATUS_DONE );
  2443. }
  2444. }
  2445. if ( move.moveCommand == MOVE_TO_POSITION ) {
  2446. goalDelta = move.moveDest - oldorigin;
  2447. goalDist = goalDelta.LengthFast();
  2448. if ( goalDist < delta.LengthFast() ) {
  2449. delta = goalDelta;
  2450. }
  2451. }
  2452. idVec3 vel = physicsObj.GetLinearVelocity();
  2453. float z = vel.z;
  2454. idVec3 predictedPos = oldorigin + vel * AI_SEEK_PREDICTION;
  2455. // seek the goal position
  2456. goalDelta = goalPos - predictedPos;
  2457. vel -= vel * AI_FLY_DAMPENING * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2458. vel += goalDelta * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2459. // cap our speed
  2460. vel = vel.Truncate( fly_speed );
  2461. vel.z = z;
  2462. physicsObj.SetLinearVelocity( vel );
  2463. physicsObj.UseVelocityMove( true );
  2464. RunPhysics();
  2465. if ( ( move.moveCommand == MOVE_FACE_ENEMY ) && enemy.GetEntity() ) {
  2466. TurnToward( lastVisibleEnemyPos );
  2467. } else if ( ( move.moveCommand == MOVE_FACE_ENTITY ) && move.goalEntity.GetEntity() ) {
  2468. TurnToward( move.goalEntity.GetEntity()->GetPhysics()->GetOrigin() );
  2469. } else if ( move.moveCommand != MOVE_NONE ) {
  2470. if ( vel.ToVec2().LengthSqr() > 0.1f ) {
  2471. TurnToward( vel.ToYaw() );
  2472. }
  2473. }
  2474. Turn();
  2475. if ( ai_debugMove.GetBool() ) {
  2476. gameRenderWorld->DebugLine( colorCyan, oldorigin, physicsObj.GetOrigin(), 5000 );
  2477. }
  2478. moveResult = physicsObj.GetMoveResult();
  2479. if ( !af_push_moveables && attack.Length() && TestMelee() ) {
  2480. DirectDamage( attack, enemy.GetEntity() );
  2481. } else {
  2482. idEntity *blockEnt = physicsObj.GetSlideMoveEntity();
  2483. if ( blockEnt != NULL && blockEnt->IsType( idMoveable::Type ) && blockEnt->GetPhysics()->IsPushable() ) {
  2484. KickObstacles( viewAxis[ 0 ], kickForce, blockEnt );
  2485. }
  2486. }
  2487. BlockedFailSafe();
  2488. AI_ONGROUND = physicsObj.OnGround();
  2489. idVec3 org = physicsObj.GetOrigin();
  2490. if ( oldorigin != org ) {
  2491. TouchTriggers();
  2492. }
  2493. if ( ai_debugMove.GetBool() ) {
  2494. gameRenderWorld->DebugBounds( colorMagenta, physicsObj.GetBounds(), org, 1 );
  2495. gameRenderWorld->DebugBounds( colorMagenta, physicsObj.GetBounds(), move.moveDest, 1 );
  2496. gameRenderWorld->DebugLine( colorYellow, org + EyeOffset(), org + EyeOffset() + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 16.0f, 1, true );
  2497. DrawRoute();
  2498. }
  2499. }
  2500. /*
  2501. =====================
  2502. idAI::AdjustFlyingAngles
  2503. =====================
  2504. */
  2505. void idAI::AdjustFlyingAngles() {
  2506. idVec3 vel;
  2507. float speed;
  2508. float roll;
  2509. float pitch;
  2510. vel = physicsObj.GetLinearVelocity();
  2511. speed = vel.Length();
  2512. if ( speed < 5.0f ) {
  2513. roll = 0.0f;
  2514. pitch = 0.0f;
  2515. } else {
  2516. roll = vel * viewAxis[ 1 ] * -fly_roll_scale / fly_speed;
  2517. if ( roll > fly_roll_max ) {
  2518. roll = fly_roll_max;
  2519. } else if ( roll < -fly_roll_max ) {
  2520. roll = -fly_roll_max;
  2521. }
  2522. pitch = vel * viewAxis[ 2 ] * -fly_pitch_scale / fly_speed;
  2523. if ( pitch > fly_pitch_max ) {
  2524. pitch = fly_pitch_max;
  2525. } else if ( pitch < -fly_pitch_max ) {
  2526. pitch = -fly_pitch_max;
  2527. }
  2528. }
  2529. fly_roll = fly_roll * 0.95f + roll * 0.05f;
  2530. fly_pitch = fly_pitch * 0.95f + pitch * 0.05f;
  2531. if ( flyTiltJoint != INVALID_JOINT ) {
  2532. animator.SetJointAxis( flyTiltJoint, JOINTMOD_WORLD, idAngles( fly_pitch, 0.0f, fly_roll ).ToMat3() );
  2533. } else {
  2534. viewAxis = idAngles( fly_pitch, current_yaw, fly_roll ).ToMat3();
  2535. }
  2536. }
  2537. /*
  2538. =====================
  2539. idAI::AddFlyBob
  2540. =====================
  2541. */
  2542. void idAI::AddFlyBob( idVec3 &vel ) {
  2543. idVec3 fly_bob_add;
  2544. float t;
  2545. if ( fly_bob_strength ) {
  2546. t = MS2SEC( gameLocal.time + entityNumber * 497 );
  2547. fly_bob_add = ( viewAxis[ 1 ] * idMath::Sin16( t * fly_bob_horz ) + viewAxis[ 2 ] * idMath::Sin16( t * fly_bob_vert ) ) * fly_bob_strength;
  2548. vel += fly_bob_add * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2549. if ( ai_debugMove.GetBool() ) {
  2550. const idVec3 &origin = physicsObj.GetOrigin();
  2551. gameRenderWorld->DebugArrow( colorOrange, origin, origin + fly_bob_add, 0 );
  2552. }
  2553. }
  2554. }
  2555. /*
  2556. =====================
  2557. idAI::AdjustFlyHeight
  2558. =====================
  2559. */
  2560. void idAI::AdjustFlyHeight( idVec3 &vel, const idVec3 &goalPos ) {
  2561. const idVec3 &origin = physicsObj.GetOrigin();
  2562. predictedPath_t path;
  2563. idVec3 end;
  2564. idVec3 dest;
  2565. trace_t trace;
  2566. idActor *enemyEnt;
  2567. bool goLower;
  2568. // make sure we're not flying too high to get through doors
  2569. goLower = false;
  2570. if ( origin.z > goalPos.z ) {
  2571. dest = goalPos;
  2572. dest.z = origin.z + 128.0f;
  2573. idAI::PredictPath( this, aas, goalPos, dest - origin, 1000, 1000, SE_BLOCKED, path );
  2574. if ( path.endPos.z < origin.z ) {
  2575. idVec3 addVel = Seek( vel, origin, path.endPos, AI_SEEK_PREDICTION );
  2576. vel.z += addVel.z;
  2577. goLower = true;
  2578. }
  2579. if ( ai_debugMove.GetBool() ) {
  2580. gameRenderWorld->DebugBounds( goLower ? colorRed : colorGreen, physicsObj.GetBounds(), path.endPos, 1 );
  2581. }
  2582. }
  2583. if ( !goLower ) {
  2584. // make sure we don't fly too low
  2585. end = origin;
  2586. enemyEnt = enemy.GetEntity();
  2587. if ( enemyEnt ) {
  2588. end.z = lastVisibleEnemyPos.z + lastVisibleEnemyEyeOffset.z + fly_offset;
  2589. } else {
  2590. // just use the default eye height for the player
  2591. end.z = goalPos.z + DEFAULT_FLY_OFFSET + fly_offset;
  2592. }
  2593. gameLocal.clip.Translation( trace, origin, end, physicsObj.GetClipModel(), mat3_identity, MASK_MONSTERSOLID, this );
  2594. vel += Seek( vel, origin, trace.endpos, AI_SEEK_PREDICTION );
  2595. }
  2596. }
  2597. /*
  2598. =====================
  2599. idAI::FlySeekGoal
  2600. =====================
  2601. */
  2602. void idAI::FlySeekGoal( idVec3 &vel, idVec3 &goalPos ) {
  2603. idVec3 seekVel;
  2604. // seek the goal position
  2605. seekVel = Seek( vel, physicsObj.GetOrigin(), goalPos, AI_SEEK_PREDICTION );
  2606. seekVel *= fly_seek_scale;
  2607. vel += seekVel;
  2608. }
  2609. /*
  2610. =====================
  2611. idAI::AdjustFlySpeed
  2612. =====================
  2613. */
  2614. void idAI::AdjustFlySpeed( idVec3 &vel ) {
  2615. float speed;
  2616. // apply dampening
  2617. vel -= vel * AI_FLY_DAMPENING * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2618. // gradually speed up/slow down to desired speed
  2619. speed = vel.Normalize();
  2620. speed += ( move.speed - speed ) * MS2SEC( gameLocal.time - gameLocal.previousTime );
  2621. if ( speed < 0.0f ) {
  2622. speed = 0.0f;
  2623. } else if ( move.speed && ( speed > move.speed ) ) {
  2624. speed = move.speed;
  2625. }
  2626. vel *= speed;
  2627. }
  2628. /*
  2629. =====================
  2630. idAI::FlyTurn
  2631. =====================
  2632. */
  2633. void idAI::FlyTurn() {
  2634. if ( move.moveCommand == MOVE_FACE_ENEMY ) {
  2635. TurnToward( lastVisibleEnemyPos );
  2636. } else if ( ( move.moveCommand == MOVE_FACE_ENTITY ) && move.goalEntity.GetEntity() ) {
  2637. TurnToward( move.goalEntity.GetEntity()->GetPhysics()->GetOrigin() );
  2638. } else if ( move.speed > 0.0f ) {
  2639. const idVec3 &vel = physicsObj.GetLinearVelocity();
  2640. if ( vel.ToVec2().LengthSqr() > 0.1f ) {
  2641. TurnToward( vel.ToYaw() );
  2642. }
  2643. }
  2644. Turn();
  2645. }
  2646. /*
  2647. =====================
  2648. idAI::FlyMove
  2649. =====================
  2650. */
  2651. void idAI::FlyMove() {
  2652. idVec3 goalPos;
  2653. idVec3 oldorigin;
  2654. idVec3 newDest;
  2655. AI_BLOCKED = false;
  2656. if ( ( move.moveCommand != MOVE_NONE ) && ReachedPos( move.moveDest, move.moveCommand ) ) {
  2657. StopMove( MOVE_STATUS_DONE );
  2658. }
  2659. if ( ai_debugMove.GetBool() ) {
  2660. gameLocal.Printf( "%d: %s: %s, vel = %.2f, sp = %.2f, maxsp = %.2f\n", gameLocal.time, name.c_str(), moveCommandString[ move.moveCommand ], physicsObj.GetLinearVelocity().Length(), move.speed, fly_speed );
  2661. }
  2662. if ( move.moveCommand != MOVE_TO_POSITION_DIRECT ) {
  2663. idVec3 vel = physicsObj.GetLinearVelocity();
  2664. if ( GetMovePos( goalPos ) ) {
  2665. CheckObstacleAvoidance( goalPos, newDest );
  2666. goalPos = newDest;
  2667. }
  2668. if ( move.speed ) {
  2669. FlySeekGoal( vel, goalPos );
  2670. }
  2671. // add in bobbing
  2672. AddFlyBob( vel );
  2673. if ( enemy.GetEntity() && ( move.moveCommand != MOVE_TO_POSITION ) ) {
  2674. AdjustFlyHeight( vel, goalPos );
  2675. }
  2676. AdjustFlySpeed( vel );
  2677. physicsObj.SetLinearVelocity( vel );
  2678. }
  2679. // turn
  2680. FlyTurn();
  2681. // run the physics for this frame
  2682. oldorigin = physicsObj.GetOrigin();
  2683. physicsObj.UseFlyMove( true );
  2684. physicsObj.UseVelocityMove( false );
  2685. physicsObj.SetDelta( vec3_zero );
  2686. physicsObj.ForceDeltaMove( disableGravity );
  2687. RunPhysics();
  2688. monsterMoveResult_t moveResult = physicsObj.GetMoveResult();
  2689. if ( !af_push_moveables && attack.Length() && TestMelee() ) {
  2690. DirectDamage( attack, enemy.GetEntity() );
  2691. } else {
  2692. idEntity *blockEnt = physicsObj.GetSlideMoveEntity();
  2693. if ( blockEnt != NULL && blockEnt->IsType( idMoveable::Type ) && blockEnt->GetPhysics()->IsPushable() ) {
  2694. KickObstacles( viewAxis[ 0 ], kickForce, blockEnt );
  2695. } else if ( moveResult == MM_BLOCKED ) {
  2696. move.blockTime = gameLocal.time + 500;
  2697. AI_BLOCKED = true;
  2698. }
  2699. }
  2700. idVec3 org = physicsObj.GetOrigin();
  2701. if ( oldorigin != org ) {
  2702. TouchTriggers();
  2703. }
  2704. if ( ai_debugMove.GetBool() ) {
  2705. gameRenderWorld->DebugLine( colorCyan, oldorigin, physicsObj.GetOrigin(), 4000 );
  2706. gameRenderWorld->DebugBounds( colorOrange, physicsObj.GetBounds(), org, 1 );
  2707. gameRenderWorld->DebugBounds( colorMagenta, physicsObj.GetBounds(), move.moveDest, 1 );
  2708. gameRenderWorld->DebugLine( colorRed, org, org + physicsObj.GetLinearVelocity(), 1, true );
  2709. gameRenderWorld->DebugLine( colorBlue, org, goalPos, 1, true );
  2710. gameRenderWorld->DebugLine( colorYellow, org + EyeOffset(), org + EyeOffset() + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 16.0f, 1, true );
  2711. DrawRoute();
  2712. }
  2713. }
  2714. /*
  2715. =====================
  2716. idAI::StaticMove
  2717. =====================
  2718. */
  2719. void idAI::StaticMove() {
  2720. idActor *enemyEnt = enemy.GetEntity();
  2721. if ( AI_DEAD ) {
  2722. return;
  2723. }
  2724. if ( ( move.moveCommand == MOVE_FACE_ENEMY ) && enemyEnt ) {
  2725. TurnToward( lastVisibleEnemyPos );
  2726. } else if ( ( move.moveCommand == MOVE_FACE_ENTITY ) && move.goalEntity.GetEntity() ) {
  2727. TurnToward( move.goalEntity.GetEntity()->GetPhysics()->GetOrigin() );
  2728. } else if ( move.moveCommand != MOVE_NONE ) {
  2729. TurnToward( move.moveDest );
  2730. }
  2731. Turn();
  2732. physicsObj.ForceDeltaMove( true ); // disable gravity
  2733. RunPhysics();
  2734. AI_ONGROUND = false;
  2735. if ( !af_push_moveables && attack.Length() && TestMelee() ) {
  2736. DirectDamage( attack, enemyEnt );
  2737. }
  2738. if ( ai_debugMove.GetBool() ) {
  2739. const idVec3 &org = physicsObj.GetOrigin();
  2740. gameRenderWorld->DebugBounds( colorMagenta, physicsObj.GetBounds(), org, 1 );
  2741. gameRenderWorld->DebugLine( colorBlue, org, move.moveDest, 1, true );
  2742. gameRenderWorld->DebugLine( colorYellow, org + EyeOffset(), org + EyeOffset() + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 16.0f, 1, true );
  2743. }
  2744. }
  2745. /***********************************************************************
  2746. Damage
  2747. ***********************************************************************/
  2748. /*
  2749. =====================
  2750. idAI::ReactionTo
  2751. =====================
  2752. */
  2753. int idAI::ReactionTo( const idEntity *ent ) {
  2754. if ( ent->fl.hidden ) {
  2755. // ignore hidden entities
  2756. return ATTACK_IGNORE;
  2757. }
  2758. if ( !ent->IsType( idActor::Type ) ) {
  2759. return ATTACK_IGNORE;
  2760. }
  2761. const idActor *actor = static_cast<const idActor *>( ent );
  2762. if ( actor->IsType( idPlayer::Type ) && static_cast<const idPlayer *>(actor)->noclip ) {
  2763. // ignore players in noclip mode
  2764. return ATTACK_IGNORE;
  2765. }
  2766. // actors on different teams will always fight each other
  2767. if ( actor->team != team ) {
  2768. if ( actor->fl.notarget ) {
  2769. // don't attack on sight when attacker is notargeted
  2770. return ATTACK_ON_DAMAGE | ATTACK_ON_ACTIVATE;
  2771. }
  2772. return ATTACK_ON_SIGHT | ATTACK_ON_DAMAGE | ATTACK_ON_ACTIVATE;
  2773. }
  2774. // monsters will fight when attacked by lower ranked monsters. rank 0 never fights back.
  2775. if ( rank && ( actor->rank < rank ) ) {
  2776. return ATTACK_ON_DAMAGE;
  2777. }
  2778. // don't fight back
  2779. return ATTACK_IGNORE;
  2780. }
  2781. /*
  2782. =====================
  2783. idAI::Pain
  2784. =====================
  2785. */
  2786. bool idAI::Pain( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location ) {
  2787. idActor *actor;
  2788. AI_PAIN = idActor::Pain( inflictor, attacker, damage, dir, location );
  2789. AI_DAMAGE = true;
  2790. // force a blink
  2791. blink_time = 0;
  2792. // ignore damage from self
  2793. if ( attacker != this ) {
  2794. if ( inflictor ) {
  2795. AI_SPECIAL_DAMAGE = inflictor->spawnArgs.GetInt( "special_damage" );
  2796. } else {
  2797. AI_SPECIAL_DAMAGE = 0;
  2798. }
  2799. if ( enemy.GetEntity() != attacker && attacker->IsType( idActor::Type ) ) {
  2800. actor = ( idActor * )attacker;
  2801. if ( ReactionTo( actor ) & ATTACK_ON_DAMAGE ) {
  2802. gameLocal.AlertAI( actor );
  2803. SetEnemy( actor );
  2804. }
  2805. }
  2806. }
  2807. return ( AI_PAIN != 0 );
  2808. }
  2809. /*
  2810. =====================
  2811. idAI::SpawnParticles
  2812. =====================
  2813. */
  2814. void idAI::SpawnParticles( const char *keyName ) {
  2815. const idKeyValue *kv = spawnArgs.MatchPrefix( keyName, NULL );
  2816. while ( kv ) {
  2817. particleEmitter_t pe;
  2818. idStr particleName = kv->GetValue();
  2819. if ( particleName.Length() ) {
  2820. idStr jointName = kv->GetValue();
  2821. int dash = jointName.Find('-');
  2822. if ( dash > 0 ) {
  2823. particleName = particleName.Left( dash );
  2824. jointName = jointName.Right( jointName.Length() - dash - 1 );
  2825. }
  2826. SpawnParticlesOnJoint( pe, particleName, jointName );
  2827. particles.Append( pe );
  2828. }
  2829. kv = spawnArgs.MatchPrefix( keyName, kv );
  2830. }
  2831. }
  2832. /*
  2833. =====================
  2834. idAI::SpawnParticlesOnJoint
  2835. =====================
  2836. */
  2837. const idDeclParticle *idAI::SpawnParticlesOnJoint( particleEmitter_t &pe, const char *particleName, const char *jointName ) {
  2838. idVec3 origin;
  2839. idMat3 axis;
  2840. if ( *particleName == '\0' ) {
  2841. memset( &pe, 0, sizeof( pe ) );
  2842. return pe.particle;
  2843. }
  2844. pe.joint = animator.GetJointHandle( jointName );
  2845. if ( pe.joint == INVALID_JOINT ) {
  2846. gameLocal.Warning( "Unknown particleJoint '%s' on '%s'", jointName, name.c_str() );
  2847. pe.time = 0;
  2848. pe.particle = NULL;
  2849. } else {
  2850. animator.GetJointTransform( pe.joint, gameLocal.time, origin, axis );
  2851. origin = renderEntity.origin + origin * renderEntity.axis;
  2852. BecomeActive( TH_UPDATEPARTICLES );
  2853. if ( !gameLocal.time ) {
  2854. // particles with time of 0 don't show, so set the time differently on the first frame
  2855. pe.time = 1;
  2856. } else {
  2857. pe.time = gameLocal.time;
  2858. }
  2859. pe.particle = static_cast<const idDeclParticle *>( declManager->FindType( DECL_PARTICLE, particleName ) );
  2860. gameLocal.smokeParticles->EmitSmoke( pe.particle, pe.time, gameLocal.random.CRandomFloat(), origin, axis, timeGroup /*_D3XP*/ );
  2861. }
  2862. return pe.particle;
  2863. }
  2864. /*
  2865. =====================
  2866. idAI::Killed
  2867. =====================
  2868. */
  2869. void idAI::Killed( idEntity *inflictor, idEntity *attacker, int damage, const idVec3 &dir, int location ) {
  2870. idAngles ang;
  2871. const char *modelDeath;
  2872. // Guardian died? grats, you get an achievement
  2873. if ( idStr::Icmp( name, "guardian_spawn" ) == 0 ) {
  2874. idPlayer * player = gameLocal.GetLocalPlayer();
  2875. if ( player != NULL && player->GetExpansionType() == GAME_BASE ) {
  2876. player->GetAchievementManager().EventCompletesAchievement( ACHIEVEMENT_DEFEAT_GUARDIAN_BOSS );
  2877. }
  2878. }
  2879. // make sure the monster is activated
  2880. EndAttack();
  2881. if ( g_debugDamage.GetBool() ) {
  2882. gameLocal.Printf( "Damage: joint: '%s', zone '%s'\n", animator.GetJointName( ( jointHandle_t )location ),
  2883. GetDamageGroup( location ) );
  2884. }
  2885. if ( inflictor ) {
  2886. AI_SPECIAL_DAMAGE = inflictor->spawnArgs.GetInt( "special_damage" );
  2887. } else {
  2888. AI_SPECIAL_DAMAGE = 0;
  2889. }
  2890. if ( AI_DEAD ) {
  2891. AI_PAIN = true;
  2892. AI_DAMAGE = true;
  2893. return;
  2894. }
  2895. // stop all voice sounds
  2896. StopSound( SND_CHANNEL_VOICE, false );
  2897. if ( head.GetEntity() ) {
  2898. head.GetEntity()->StopSound( SND_CHANNEL_VOICE, false );
  2899. head.GetEntity()->GetAnimator()->ClearAllAnims( gameLocal.time, 100 );
  2900. }
  2901. disableGravity = false;
  2902. move.moveType = MOVETYPE_DEAD;
  2903. af_push_moveables = false;
  2904. physicsObj.UseFlyMove( false );
  2905. physicsObj.ForceDeltaMove( false );
  2906. // end our looping ambient sound
  2907. StopSound( SND_CHANNEL_AMBIENT, false );
  2908. if ( attacker && attacker->IsType( idActor::Type ) ) {
  2909. gameLocal.AlertAI( ( idActor * )attacker );
  2910. }
  2911. // activate targets
  2912. ActivateTargets( attacker );
  2913. RemoveAttachments();
  2914. RemoveProjectile();
  2915. StopMove( MOVE_STATUS_DONE );
  2916. ClearEnemy();
  2917. AI_DEAD = true;
  2918. // make monster nonsolid
  2919. physicsObj.SetContents( 0 );
  2920. physicsObj.GetClipModel()->Unlink();
  2921. Unbind();
  2922. if ( StartRagdoll() ) {
  2923. StartSound( "snd_death", SND_CHANNEL_VOICE, 0, false, NULL );
  2924. }
  2925. if ( spawnArgs.GetString( "model_death", "", &modelDeath ) ) {
  2926. // lost soul is only case that does not use a ragdoll and has a model_death so get the death sound in here
  2927. StartSound( "snd_death", SND_CHANNEL_VOICE, 0, false, NULL );
  2928. renderEntity.shaderParms[ SHADERPARM_TIMEOFFSET ] = -MS2SEC( gameLocal.time );
  2929. SetModel( modelDeath );
  2930. physicsObj.SetLinearVelocity( vec3_zero );
  2931. physicsObj.PutToRest();
  2932. physicsObj.DisableImpact();
  2933. // No grabbing if "model_death"
  2934. noGrab = true;
  2935. }
  2936. restartParticles = false;
  2937. state = GetScriptFunction( "state_Killed" );
  2938. SetState( state );
  2939. SetWaitState( "" );
  2940. const idKeyValue *kv = spawnArgs.MatchPrefix( "def_drops", NULL );
  2941. while( kv ) {
  2942. idDict args;
  2943. args.Set( "classname", kv->GetValue() );
  2944. args.Set( "origin", physicsObj.GetOrigin().ToString() );
  2945. gameLocal.SpawnEntityDef( args );
  2946. kv = spawnArgs.MatchPrefix( "def_drops", kv );
  2947. }
  2948. if ( ( attacker && attacker->IsType( idPlayer::Type ) ) && ( inflictor && !inflictor->IsType( idSoulCubeMissile::Type ) ) ) {
  2949. static_cast< idPlayer* >( attacker )->AddAIKill();
  2950. }
  2951. if(spawnArgs.GetBool("harvest_on_death")) {
  2952. const idDict *harvestDef = gameLocal.FindEntityDefDict( spawnArgs.GetString("def_harvest_type"), false );
  2953. if ( harvestDef ) {
  2954. idEntity *temp;
  2955. gameLocal.SpawnEntityDef( *harvestDef, &temp, false );
  2956. harvestEnt = static_cast<idHarvestable *>(temp);
  2957. }
  2958. if(harvestEnt.GetEntity()) {
  2959. //Let the harvest entity set itself up
  2960. harvestEnt.GetEntity()->Init(this);
  2961. harvestEnt.GetEntity()->BecomeActive( TH_THINK );
  2962. }
  2963. }
  2964. }
  2965. /***********************************************************************
  2966. Targeting/Combat
  2967. ***********************************************************************/
  2968. /*
  2969. =====================
  2970. idAI::PlayCinematic
  2971. =====================
  2972. */
  2973. void idAI::PlayCinematic() {
  2974. const char *animname;
  2975. if ( current_cinematic >= num_cinematics ) {
  2976. if ( g_debugCinematic.GetBool() ) {
  2977. gameLocal.Printf( "%d: '%s' stop\n", gameLocal.framenum, GetName() );
  2978. }
  2979. if ( !spawnArgs.GetBool( "cinematic_no_hide" ) ) {
  2980. Hide();
  2981. }
  2982. current_cinematic = 0;
  2983. ActivateTargets( gameLocal.GetLocalPlayer() );
  2984. fl.neverDormant = false;
  2985. return;
  2986. }
  2987. Show();
  2988. current_cinematic++;
  2989. allowJointMod = false;
  2990. allowEyeFocus = false;
  2991. spawnArgs.GetString( va( "anim%d", current_cinematic ), NULL, &animname );
  2992. if ( !animname ) {
  2993. gameLocal.Warning( "missing 'anim%d' key on %s", current_cinematic, name.c_str() );
  2994. return;
  2995. }
  2996. if ( g_debugCinematic.GetBool() ) {
  2997. gameLocal.Printf( "%d: '%s' start '%s'\n", gameLocal.framenum, GetName(), animname );
  2998. }
  2999. headAnim.animBlendFrames = 0;
  3000. headAnim.lastAnimBlendFrames = 0;
  3001. headAnim.BecomeIdle();
  3002. legsAnim.animBlendFrames = 0;
  3003. legsAnim.lastAnimBlendFrames = 0;
  3004. legsAnim.BecomeIdle();
  3005. torsoAnim.animBlendFrames = 0;
  3006. torsoAnim.lastAnimBlendFrames = 0;
  3007. ProcessEvent( &AI_PlayAnim, ANIMCHANNEL_TORSO, animname );
  3008. // make sure our model gets updated
  3009. animator.ForceUpdate();
  3010. // update the anim bounds
  3011. UpdateAnimation();
  3012. UpdateVisuals();
  3013. Present();
  3014. if ( head.GetEntity() ) {
  3015. // since the body anim was updated, we need to run physics to update the position of the head
  3016. RunPhysics();
  3017. // make sure our model gets updated
  3018. head.GetEntity()->GetAnimator()->ForceUpdate();
  3019. // update the anim bounds
  3020. head.GetEntity()->UpdateAnimation();
  3021. head.GetEntity()->UpdateVisuals();
  3022. head.GetEntity()->Present();
  3023. }
  3024. fl.neverDormant = true;
  3025. }
  3026. /*
  3027. =====================
  3028. idAI::Activate
  3029. Notifies the script that a monster has been activated by a trigger or flashlight
  3030. =====================
  3031. */
  3032. void idAI::Activate( idEntity *activator ) {
  3033. idPlayer *player;
  3034. if ( AI_DEAD ) {
  3035. // ignore it when they're dead
  3036. return;
  3037. }
  3038. // make sure he's not dormant
  3039. dormantStart = 0;
  3040. if ( num_cinematics ) {
  3041. PlayCinematic();
  3042. } else {
  3043. AI_ACTIVATED = true;
  3044. if ( !activator || !activator->IsType( idPlayer::Type ) ) {
  3045. player = gameLocal.GetLocalPlayer();
  3046. } else {
  3047. player = static_cast<idPlayer *>( activator );
  3048. }
  3049. if ( ReactionTo( player ) & ATTACK_ON_ACTIVATE ) {
  3050. SetEnemy( player );
  3051. }
  3052. // update the script in cinematics so that entities don't start anims or show themselves a frame late.
  3053. if ( cinematic ) {
  3054. UpdateAIScript();
  3055. // make sure our model gets updated
  3056. animator.ForceUpdate();
  3057. // update the anim bounds
  3058. UpdateAnimation();
  3059. UpdateVisuals();
  3060. Present();
  3061. if ( head.GetEntity() ) {
  3062. // since the body anim was updated, we need to run physics to update the position of the head
  3063. RunPhysics();
  3064. // make sure our model gets updated
  3065. head.GetEntity()->GetAnimator()->ForceUpdate();
  3066. // update the anim bounds
  3067. head.GetEntity()->UpdateAnimation();
  3068. head.GetEntity()->UpdateVisuals();
  3069. head.GetEntity()->Present();
  3070. }
  3071. }
  3072. }
  3073. }
  3074. /*
  3075. =====================
  3076. idAI::EnemyDead
  3077. =====================
  3078. */
  3079. void idAI::EnemyDead() {
  3080. ClearEnemy();
  3081. AI_ENEMY_DEAD = true;
  3082. }
  3083. /*
  3084. =====================
  3085. idAI::TalkTo
  3086. =====================
  3087. */
  3088. void idAI::TalkTo( idActor *actor ) {
  3089. if ( talk_state != TALK_OK ) {
  3090. return;
  3091. }
  3092. // Wake up monsters that are pretending to be NPC's
  3093. if ( team == 1 && actor && actor->team != team ) {
  3094. ProcessEvent( &EV_Activate, actor );
  3095. }
  3096. talkTarget = actor;
  3097. if ( actor ) {
  3098. AI_TALK = true;
  3099. } else {
  3100. AI_TALK = false;
  3101. }
  3102. }
  3103. /*
  3104. =====================
  3105. idAI::GetEnemy
  3106. =====================
  3107. */
  3108. idActor *idAI::GetEnemy() const {
  3109. return enemy.GetEntity();
  3110. }
  3111. /*
  3112. =====================
  3113. idAI::GetTalkState
  3114. =====================
  3115. */
  3116. talkState_t idAI::GetTalkState() const {
  3117. if ( ( talk_state != TALK_NEVER ) && AI_DEAD ) {
  3118. return TALK_DEAD;
  3119. }
  3120. if ( IsHidden() ) {
  3121. return TALK_NEVER;
  3122. }
  3123. return talk_state;
  3124. }
  3125. /*
  3126. =====================
  3127. idAI::TouchedByFlashlight
  3128. =====================
  3129. */
  3130. void idAI::TouchedByFlashlight( idActor *flashlight_owner ) {
  3131. if ( wakeOnFlashlight ) {
  3132. Activate( flashlight_owner );
  3133. }
  3134. }
  3135. /*
  3136. =====================
  3137. idAI::ClearEnemy
  3138. =====================
  3139. */
  3140. void idAI::ClearEnemy() {
  3141. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3142. StopMove( MOVE_STATUS_DEST_NOT_FOUND );
  3143. }
  3144. enemyNode.Remove();
  3145. enemy = NULL;
  3146. AI_ENEMY_IN_FOV = false;
  3147. AI_ENEMY_VISIBLE = false;
  3148. AI_ENEMY_DEAD = true;
  3149. SetChatSound();
  3150. }
  3151. /*
  3152. =====================
  3153. idAI::EnemyPositionValid
  3154. =====================
  3155. */
  3156. bool idAI::EnemyPositionValid() const {
  3157. trace_t tr;
  3158. idVec3 muzzle;
  3159. idMat3 axis;
  3160. if ( !enemy.GetEntity() ) {
  3161. return false;
  3162. }
  3163. if ( AI_ENEMY_VISIBLE ) {
  3164. return true;
  3165. }
  3166. gameLocal.clip.TracePoint( tr, GetEyePosition(), lastVisibleEnemyPos + lastVisibleEnemyEyeOffset, MASK_OPAQUE, this );
  3167. if ( tr.fraction < 1.0f ) {
  3168. // can't see the area yet, so don't know if he's there or not
  3169. return true;
  3170. }
  3171. return false;
  3172. }
  3173. /*
  3174. =====================
  3175. idAI::SetEnemyPosition
  3176. =====================
  3177. */
  3178. void idAI::SetEnemyPosition() {
  3179. idActor *enemyEnt = enemy.GetEntity();
  3180. int enemyAreaNum;
  3181. int areaNum;
  3182. int lastVisibleReachableEnemyAreaNum = 0;
  3183. aasPath_t path;
  3184. idVec3 pos;
  3185. bool onGround;
  3186. if ( !enemyEnt ) {
  3187. return;
  3188. }
  3189. lastVisibleReachableEnemyPos = lastReachableEnemyPos;
  3190. lastVisibleEnemyEyeOffset = enemyEnt->EyeOffset();
  3191. lastVisibleEnemyPos = enemyEnt->GetPhysics()->GetOrigin();
  3192. if ( move.moveType == MOVETYPE_FLY ) {
  3193. pos = lastVisibleEnemyPos;
  3194. onGround = true;
  3195. } else {
  3196. onGround = enemyEnt->GetFloorPos( 64.0f, pos );
  3197. if ( enemyEnt->OnLadder() ) {
  3198. onGround = false;
  3199. }
  3200. }
  3201. if ( !onGround ) {
  3202. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3203. AI_DEST_UNREACHABLE = true;
  3204. }
  3205. return;
  3206. }
  3207. // when we don't have an AAS, we can't tell if an enemy is reachable or not,
  3208. // so just assume that he is.
  3209. if ( !aas ) {
  3210. lastVisibleReachableEnemyPos = lastVisibleEnemyPos;
  3211. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3212. AI_DEST_UNREACHABLE = false;
  3213. }
  3214. enemyAreaNum = 0;
  3215. areaNum = 0;
  3216. } else {
  3217. lastVisibleReachableEnemyAreaNum = move.toAreaNum;
  3218. enemyAreaNum = PointReachableAreaNum( lastVisibleEnemyPos, 1.0f );
  3219. if ( !enemyAreaNum ) {
  3220. enemyAreaNum = PointReachableAreaNum( lastReachableEnemyPos, 1.0f );
  3221. pos = lastReachableEnemyPos;
  3222. }
  3223. if ( !enemyAreaNum ) {
  3224. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3225. AI_DEST_UNREACHABLE = true;
  3226. }
  3227. areaNum = 0;
  3228. } else {
  3229. const idVec3 &org = physicsObj.GetOrigin();
  3230. areaNum = PointReachableAreaNum( org );
  3231. if ( PathToGoal( path, areaNum, org, enemyAreaNum, pos ) ) {
  3232. lastVisibleReachableEnemyPos = pos;
  3233. lastVisibleReachableEnemyAreaNum = enemyAreaNum;
  3234. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3235. AI_DEST_UNREACHABLE = false;
  3236. }
  3237. } else if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3238. AI_DEST_UNREACHABLE = true;
  3239. }
  3240. }
  3241. }
  3242. if ( move.moveCommand == MOVE_TO_ENEMY ) {
  3243. if ( !aas ) {
  3244. // keep the move destination up to date for wandering
  3245. move.moveDest = lastVisibleReachableEnemyPos;
  3246. } else if ( enemyAreaNum ) {
  3247. move.toAreaNum = lastVisibleReachableEnemyAreaNum;
  3248. move.moveDest = lastVisibleReachableEnemyPos;
  3249. }
  3250. if ( move.moveType == MOVETYPE_FLY ) {
  3251. predictedPath_t path;
  3252. idVec3 end = move.moveDest;
  3253. end.z += enemyEnt->EyeOffset().z + fly_offset;
  3254. idAI::PredictPath( this, aas, move.moveDest, end - move.moveDest, 1000, 1000, SE_BLOCKED, path );
  3255. move.moveDest = path.endPos;
  3256. move.toAreaNum = PointReachableAreaNum( move.moveDest, 1.0f );
  3257. }
  3258. }
  3259. }
  3260. /*
  3261. =====================
  3262. idAI::UpdateEnemyPosition
  3263. =====================
  3264. */
  3265. void idAI::UpdateEnemyPosition() {
  3266. idActor *enemyEnt = enemy.GetEntity();
  3267. int enemyAreaNum;
  3268. int areaNum;
  3269. aasPath_t path;
  3270. predictedPath_t predictedPath;
  3271. idVec3 enemyPos;
  3272. bool onGround;
  3273. if ( !enemyEnt ) {
  3274. return;
  3275. }
  3276. const idVec3 &org = physicsObj.GetOrigin();
  3277. if ( move.moveType == MOVETYPE_FLY ) {
  3278. enemyPos = enemyEnt->GetPhysics()->GetOrigin();
  3279. onGround = true;
  3280. } else {
  3281. onGround = enemyEnt->GetFloorPos( 64.0f, enemyPos );
  3282. if ( enemyEnt->OnLadder() ) {
  3283. onGround = false;
  3284. }
  3285. }
  3286. if ( onGround ) {
  3287. // when we don't have an AAS, we can't tell if an enemy is reachable or not,
  3288. // so just assume that he is.
  3289. if ( !aas ) {
  3290. enemyAreaNum = 0;
  3291. lastReachableEnemyPos = enemyPos;
  3292. } else {
  3293. enemyAreaNum = PointReachableAreaNum( enemyPos, 1.0f );
  3294. if ( enemyAreaNum ) {
  3295. areaNum = PointReachableAreaNum( org );
  3296. if ( PathToGoal( path, areaNum, org, enemyAreaNum, enemyPos ) ) {
  3297. lastReachableEnemyPos = enemyPos;
  3298. }
  3299. }
  3300. }
  3301. }
  3302. AI_ENEMY_IN_FOV = false;
  3303. AI_ENEMY_VISIBLE = false;
  3304. if ( CanSee( enemyEnt, false ) ) {
  3305. AI_ENEMY_VISIBLE = true;
  3306. if ( CheckFOV( enemyEnt->GetPhysics()->GetOrigin() ) ) {
  3307. AI_ENEMY_IN_FOV = true;
  3308. }
  3309. SetEnemyPosition();
  3310. } else {
  3311. // check if we heard any sounds in the last frame
  3312. if ( enemyEnt == gameLocal.GetAlertEntity() ) {
  3313. float dist = ( enemyEnt->GetPhysics()->GetOrigin() - org ).LengthSqr();
  3314. if ( dist < Square( AI_HEARING_RANGE ) ) {
  3315. SetEnemyPosition();
  3316. }
  3317. }
  3318. }
  3319. if ( ai_debugMove.GetBool() ) {
  3320. gameRenderWorld->DebugBounds( colorLtGrey, enemyEnt->GetPhysics()->GetBounds(), lastReachableEnemyPos, 1 );
  3321. gameRenderWorld->DebugBounds( colorWhite, enemyEnt->GetPhysics()->GetBounds(), lastVisibleReachableEnemyPos, 1 );
  3322. }
  3323. }
  3324. /*
  3325. =====================
  3326. idAI::SetEnemy
  3327. =====================
  3328. */
  3329. void idAI::SetEnemy( idActor *newEnemy ) {
  3330. int enemyAreaNum;
  3331. if ( AI_DEAD ) {
  3332. ClearEnemy();
  3333. return;
  3334. }
  3335. AI_ENEMY_DEAD = false;
  3336. if ( !newEnemy ) {
  3337. ClearEnemy();
  3338. } else if ( enemy.GetEntity() != newEnemy ) {
  3339. // Check to see if we should unlock the 'Turncloak' achievement
  3340. const idActor * enemyEnt = enemy.GetEntity();
  3341. if ( enemyEnt != NULL && enemyEnt->IsType( idPlayer::Type ) && newEnemy->IsType( idAI::Type ) && newEnemy->team == this->team && ( idStr::Icmp( newEnemy->GetName(), "hazmat_dummy") != 0 ) ) {
  3342. idPlayer *player = gameLocal.GetLocalPlayer();
  3343. if ( player != NULL ) {
  3344. player->GetAchievementManager().EventCompletesAchievement( ACHIEVEMENT_TWO_DEMONS_FIGHT_EACH_OTHER );
  3345. }
  3346. }
  3347. enemy = newEnemy;
  3348. enemyNode.AddToEnd( newEnemy->enemyList );
  3349. if ( newEnemy->health <= 0 ) {
  3350. EnemyDead();
  3351. return;
  3352. }
  3353. // let the monster know where the enemy is
  3354. newEnemy->GetAASLocation( aas, lastReachableEnemyPos, enemyAreaNum );
  3355. SetEnemyPosition();
  3356. SetChatSound();
  3357. lastReachableEnemyPos = lastVisibleEnemyPos;
  3358. lastVisibleReachableEnemyPos = lastReachableEnemyPos;
  3359. enemyAreaNum = PointReachableAreaNum( lastReachableEnemyPos, 1.0f );
  3360. if ( aas && enemyAreaNum ) {
  3361. aas->PushPointIntoAreaNum( enemyAreaNum, lastReachableEnemyPos );
  3362. lastVisibleReachableEnemyPos = lastReachableEnemyPos;
  3363. }
  3364. }
  3365. }
  3366. /*
  3367. ============
  3368. idAI::FirstVisiblePointOnPath
  3369. ============
  3370. */
  3371. idVec3 idAI::FirstVisiblePointOnPath( const idVec3 origin, const idVec3 &target, int travelFlags ) const {
  3372. int i, areaNum, targetAreaNum, curAreaNum, travelTime;
  3373. idVec3 curOrigin;
  3374. idReachability *reach;
  3375. if ( !aas ) {
  3376. return origin;
  3377. }
  3378. areaNum = PointReachableAreaNum( origin );
  3379. targetAreaNum = PointReachableAreaNum( target );
  3380. if ( !areaNum || !targetAreaNum ) {
  3381. return origin;
  3382. }
  3383. if ( ( areaNum == targetAreaNum ) || PointVisible( origin ) ) {
  3384. return origin;
  3385. }
  3386. curAreaNum = areaNum;
  3387. curOrigin = origin;
  3388. for( i = 0; i < 10; i++ ) {
  3389. if ( !aas->RouteToGoalArea( curAreaNum, curOrigin, targetAreaNum, travelFlags, travelTime, &reach ) ) {
  3390. break;
  3391. }
  3392. if ( !reach ) {
  3393. return target;
  3394. }
  3395. curAreaNum = reach->toAreaNum;
  3396. curOrigin = reach->end;
  3397. if ( PointVisible( curOrigin ) ) {
  3398. return curOrigin;
  3399. }
  3400. }
  3401. return origin;
  3402. }
  3403. /*
  3404. ===================
  3405. idAI::CalculateAttackOffsets
  3406. calculate joint positions on attack frames so we can do proper "can hit" tests
  3407. ===================
  3408. */
  3409. void idAI::CalculateAttackOffsets() {
  3410. const idDeclModelDef *modelDef;
  3411. int num;
  3412. int i;
  3413. int frame;
  3414. const frameCommand_t *command;
  3415. idMat3 axis;
  3416. const idAnim *anim;
  3417. jointHandle_t joint;
  3418. modelDef = animator.ModelDef();
  3419. if ( !modelDef ) {
  3420. return;
  3421. }
  3422. num = modelDef->NumAnims();
  3423. // needs to be off while getting the offsets so that we account for the distance the monster moves in the attack anim
  3424. animator.RemoveOriginOffset( false );
  3425. // anim number 0 is reserved for non-existant anims. to avoid off by one issues, just allocate an extra spot for
  3426. // launch offsets so that anim number can be used without subtracting 1.
  3427. missileLaunchOffset.SetGranularity( 1 );
  3428. missileLaunchOffset.SetNum( num + 1 );
  3429. missileLaunchOffset[ 0 ].Zero();
  3430. for( i = 1; i <= num; i++ ) {
  3431. missileLaunchOffset[ i ].Zero();
  3432. anim = modelDef->GetAnim( i );
  3433. if ( anim ) {
  3434. frame = anim->FindFrameForFrameCommand( FC_LAUNCHMISSILE, &command );
  3435. if ( frame >= 0 ) {
  3436. joint = animator.GetJointHandle( command->string->c_str() );
  3437. if ( joint == INVALID_JOINT ) {
  3438. gameLocal.Error( "Invalid joint '%s' on 'launch_missile' frame command on frame %d of model '%s'", command->string->c_str(), frame, modelDef->GetName() );
  3439. }
  3440. GetJointTransformForAnim( joint, i, FRAME2MS( frame ), missileLaunchOffset[ i ], axis );
  3441. }
  3442. }
  3443. }
  3444. animator.RemoveOriginOffset( true );
  3445. }
  3446. /*
  3447. =====================
  3448. idAI::CreateProjectileClipModel
  3449. =====================
  3450. */
  3451. void idAI::CreateProjectileClipModel() const {
  3452. if ( projectileClipModel == NULL ) {
  3453. idBounds projectileBounds( vec3_origin );
  3454. projectileBounds.ExpandSelf( projectileRadius );
  3455. projectileClipModel = new (TAG_MODEL) idClipModel( idTraceModel( projectileBounds ) );
  3456. }
  3457. }
  3458. /*
  3459. =====================
  3460. idAI::GetAimDir
  3461. =====================
  3462. */
  3463. bool idAI::GetAimDir( const idVec3 &firePos, idEntity *aimAtEnt, const idEntity *ignore, idVec3 &aimDir ) const {
  3464. idVec3 targetPos1;
  3465. idVec3 targetPos2;
  3466. idVec3 delta;
  3467. float max_height;
  3468. bool result;
  3469. // if no aimAtEnt or projectile set
  3470. if ( !aimAtEnt || !projectileDef ) {
  3471. aimDir = viewAxis[ 0 ] * physicsObj.GetGravityAxis();
  3472. return false;
  3473. }
  3474. if ( projectileClipModel == NULL ) {
  3475. CreateProjectileClipModel();
  3476. }
  3477. if ( aimAtEnt == enemy.GetEntity() ) {
  3478. static_cast<idActor *>( aimAtEnt )->GetAIAimTargets( lastVisibleEnemyPos, targetPos1, targetPos2 );
  3479. } else if ( aimAtEnt->IsType( idActor::Type ) ) {
  3480. static_cast<idActor *>( aimAtEnt )->GetAIAimTargets( aimAtEnt->GetPhysics()->GetOrigin(), targetPos1, targetPos2 );
  3481. } else {
  3482. targetPos1 = aimAtEnt->GetPhysics()->GetAbsBounds().GetCenter();
  3483. targetPos2 = targetPos1;
  3484. }
  3485. if ( this->team == 0 && !idStr::Cmp( aimAtEnt->GetEntityDefName(), "monster_demon_vulgar" ) ) {
  3486. targetPos1.z -= 28.f;
  3487. targetPos2.z -= 12.f;
  3488. }
  3489. // try aiming for chest
  3490. delta = firePos - targetPos1;
  3491. max_height = delta.LengthFast() * projectile_height_to_distance_ratio;
  3492. result = PredictTrajectory( firePos, targetPos1, projectileSpeed, projectileGravity, projectileClipModel, MASK_SHOT_RENDERMODEL, max_height, ignore, aimAtEnt, ai_debugTrajectory.GetBool() ? 1000 : 0, aimDir );
  3493. if ( result || !aimAtEnt->IsType( idActor::Type ) ) {
  3494. return result;
  3495. }
  3496. // try aiming for head
  3497. delta = firePos - targetPos2;
  3498. max_height = delta.LengthFast() * projectile_height_to_distance_ratio;
  3499. result = PredictTrajectory( firePos, targetPos2, projectileSpeed, projectileGravity, projectileClipModel, MASK_SHOT_RENDERMODEL, max_height, ignore, aimAtEnt, ai_debugTrajectory.GetBool() ? 1000 : 0, aimDir );
  3500. return result;
  3501. }
  3502. /*
  3503. =====================
  3504. idAI::BeginAttack
  3505. =====================
  3506. */
  3507. void idAI::BeginAttack( const char *name ) {
  3508. attack = name;
  3509. lastAttackTime = gameLocal.time;
  3510. }
  3511. /*
  3512. =====================
  3513. idAI::EndAttack
  3514. =====================
  3515. */
  3516. void idAI::EndAttack() {
  3517. attack = "";
  3518. }
  3519. /*
  3520. =====================
  3521. idAI::CreateProjectile
  3522. =====================
  3523. */
  3524. idProjectile *idAI::CreateProjectile( const idVec3 &pos, const idVec3 &dir ) {
  3525. idEntity *ent;
  3526. const char *clsname;
  3527. if ( !projectile.GetEntity() ) {
  3528. gameLocal.SpawnEntityDef( *projectileDef, &ent, false );
  3529. if ( ent == NULL ) {
  3530. clsname = projectileDef->GetString( "classname" );
  3531. gameLocal.Error( "Could not spawn entityDef '%s'", clsname );
  3532. return NULL;
  3533. }
  3534. if ( !ent->IsType( idProjectile::Type ) ) {
  3535. clsname = ent->GetClassname();
  3536. gameLocal.Error( "'%s' is not an idProjectile", clsname );
  3537. }
  3538. projectile = ( idProjectile * )ent;
  3539. }
  3540. projectile.GetEntity()->Create( this, pos, dir );
  3541. return projectile.GetEntity();
  3542. }
  3543. /*
  3544. =====================
  3545. idAI::RemoveProjectile
  3546. =====================
  3547. */
  3548. void idAI::RemoveProjectile() {
  3549. if ( projectile.GetEntity() ) {
  3550. projectile.GetEntity()->PostEventMS( &EV_Remove, 0 );
  3551. projectile = NULL;
  3552. }
  3553. }
  3554. /*
  3555. =====================
  3556. idAI::LaunchProjectile
  3557. =====================
  3558. */
  3559. idProjectile *idAI::LaunchProjectile( const char *jointname, idEntity *target, bool clampToAttackCone ) {
  3560. idVec3 muzzle;
  3561. idVec3 dir;
  3562. idVec3 start;
  3563. trace_t tr;
  3564. idBounds projBounds;
  3565. float distance;
  3566. const idClipModel *projClip;
  3567. float attack_accuracy;
  3568. float attack_cone;
  3569. float projectile_spread;
  3570. float diff;
  3571. float angle;
  3572. float spin;
  3573. idAngles ang;
  3574. int num_projectiles;
  3575. int i;
  3576. idMat3 axis;
  3577. idMat3 proj_axis;
  3578. bool forceMuzzle;
  3579. idVec3 tmp;
  3580. idProjectile *lastProjectile;
  3581. if ( !projectileDef ) {
  3582. gameLocal.Warning( "%s (%s) doesn't have a projectile specified", name.c_str(), GetEntityDefName() );
  3583. return NULL;
  3584. }
  3585. attack_accuracy = spawnArgs.GetFloat( "attack_accuracy", "7" );
  3586. attack_cone = spawnArgs.GetFloat( "attack_cone", "70" );
  3587. projectile_spread = spawnArgs.GetFloat( "projectile_spread", "0" );
  3588. num_projectiles = spawnArgs.GetInt( "num_projectiles", "1" );
  3589. forceMuzzle = spawnArgs.GetBool( "forceMuzzle", "0" );
  3590. GetMuzzle( jointname, muzzle, axis );
  3591. if ( !projectile.GetEntity() ) {
  3592. CreateProjectile( muzzle, axis[ 0 ] );
  3593. }
  3594. lastProjectile = projectile.GetEntity();
  3595. if ( target != NULL ) {
  3596. tmp = target->GetPhysics()->GetAbsBounds().GetCenter() - muzzle;
  3597. tmp.Normalize();
  3598. axis = tmp.ToMat3();
  3599. } else {
  3600. axis = viewAxis;
  3601. }
  3602. // rotate it because the cone points up by default
  3603. tmp = axis[2];
  3604. axis[2] = axis[0];
  3605. axis[0] = -tmp;
  3606. proj_axis = axis;
  3607. if ( !forceMuzzle ) { // _D3XP
  3608. // make sure the projectile starts inside the monster bounding box
  3609. const idBounds &ownerBounds = physicsObj.GetAbsBounds();
  3610. projClip = lastProjectile->GetPhysics()->GetClipModel();
  3611. projBounds = projClip->GetBounds().Rotate( axis );
  3612. // check if the owner bounds is bigger than the projectile bounds
  3613. if ( ( ( ownerBounds[1][0] - ownerBounds[0][0] ) > ( projBounds[1][0] - projBounds[0][0] ) ) &&
  3614. ( ( ownerBounds[1][1] - ownerBounds[0][1] ) > ( projBounds[1][1] - projBounds[0][1] ) ) &&
  3615. ( ( ownerBounds[1][2] - ownerBounds[0][2] ) > ( projBounds[1][2] - projBounds[0][2] ) ) ) {
  3616. if ( (ownerBounds - projBounds).RayIntersection( muzzle, viewAxis[ 0 ], distance ) ) {
  3617. start = muzzle + distance * viewAxis[ 0 ];
  3618. } else {
  3619. start = ownerBounds.GetCenter();
  3620. }
  3621. } else {
  3622. // projectile bounds bigger than the owner bounds, so just start it from the center
  3623. start = ownerBounds.GetCenter();
  3624. }
  3625. gameLocal.clip.Translation( tr, start, muzzle, projClip, axis, MASK_SHOT_RENDERMODEL, this );
  3626. muzzle = tr.endpos;
  3627. }
  3628. // set aiming direction
  3629. GetAimDir( muzzle, target, this, dir );
  3630. ang = dir.ToAngles();
  3631. // adjust his aim so it's not perfect. uses sine based movement so the tracers appear less random in their spread.
  3632. float t = MS2SEC( gameLocal.time + entityNumber * 497 );
  3633. ang.pitch += idMath::Sin16( t * 5.1 ) * attack_accuracy;
  3634. ang.yaw += idMath::Sin16( t * 6.7 ) * attack_accuracy;
  3635. if ( clampToAttackCone ) {
  3636. // clamp the attack direction to be within monster's attack cone so he doesn't do
  3637. // things like throw the missile backwards if you're behind him
  3638. diff = idMath::AngleDelta( ang.yaw, current_yaw );
  3639. if ( diff > attack_cone ) {
  3640. ang.yaw = current_yaw + attack_cone;
  3641. } else if ( diff < -attack_cone ) {
  3642. ang.yaw = current_yaw - attack_cone;
  3643. }
  3644. }
  3645. axis = ang.ToMat3();
  3646. float spreadRad = DEG2RAD( projectile_spread );
  3647. for( i = 0; i < num_projectiles; i++ ) {
  3648. // spread the projectiles out
  3649. angle = idMath::Sin( spreadRad * gameLocal.random.RandomFloat() );
  3650. spin = (float)DEG2RAD( 360.0f ) * gameLocal.random.RandomFloat();
  3651. dir = axis[ 0 ] + axis[ 2 ] * ( angle * idMath::Sin( spin ) ) - axis[ 1 ] * ( angle * idMath::Cos( spin ) );
  3652. dir.Normalize();
  3653. // launch the projectile
  3654. if ( !projectile.GetEntity() ) {
  3655. CreateProjectile( muzzle, dir );
  3656. }
  3657. lastProjectile = projectile.GetEntity();
  3658. lastProjectile->Launch( muzzle, dir, vec3_origin );
  3659. projectile = NULL;
  3660. }
  3661. TriggerWeaponEffects( muzzle );
  3662. lastAttackTime = gameLocal.time;
  3663. return lastProjectile;
  3664. }
  3665. /*
  3666. ================
  3667. idAI::DamageFeedback
  3668. callback function for when another entity received damage from this entity. damage can be adjusted and returned to the caller.
  3669. FIXME: This gets called when we call idPlayer::CalcDamagePoints from idAI::AttackMelee, which then checks for a saving throw,
  3670. possibly forcing a miss. This is harmless behavior ATM, but is not intuitive.
  3671. ================
  3672. */
  3673. void idAI::DamageFeedback( idEntity *victim, idEntity *inflictor, int &damage ) {
  3674. if ( ( victim == this ) && inflictor->IsType( idProjectile::Type ) ) {
  3675. // monsters only get half damage from their own projectiles
  3676. damage = ( damage + 1 ) / 2; // round up so we don't do 0 damage
  3677. } else if ( victim == enemy.GetEntity() ) {
  3678. AI_HIT_ENEMY = true;
  3679. }
  3680. }
  3681. /*
  3682. =====================
  3683. idAI::DirectDamage
  3684. Causes direct damage to an entity
  3685. kickDir is specified in the monster's coordinate system, and gives the direction
  3686. that the view kick and knockback should go
  3687. =====================
  3688. */
  3689. void idAI::DirectDamage( const char *meleeDefName, idEntity *ent ) {
  3690. const idDict *meleeDef;
  3691. const char *p;
  3692. const idSoundShader *shader;
  3693. meleeDef = gameLocal.FindEntityDefDict( meleeDefName, false );
  3694. if ( meleeDef == NULL ) {
  3695. gameLocal.Error( "Unknown damage def '%s' on '%s'", meleeDefName, name.c_str() );
  3696. return;
  3697. }
  3698. if ( !ent->fl.takedamage ) {
  3699. const idSoundShader *shader = declManager->FindSound(meleeDef->GetString( "snd_miss" ));
  3700. StartSoundShader( shader, SND_CHANNEL_DAMAGE, 0, false, NULL );
  3701. return;
  3702. }
  3703. //
  3704. // do the damage
  3705. //
  3706. p = meleeDef->GetString( "snd_hit" );
  3707. if ( p != NULL && *p != NULL ) {
  3708. shader = declManager->FindSound( p );
  3709. StartSoundShader( shader, SND_CHANNEL_DAMAGE, 0, false, NULL );
  3710. }
  3711. idVec3 kickDir;
  3712. meleeDef->GetVector( "kickDir", "0 0 0", kickDir );
  3713. idVec3 globalKickDir;
  3714. globalKickDir = ( viewAxis * physicsObj.GetGravityAxis() ) * kickDir;
  3715. ent->Damage( this, this, globalKickDir, meleeDefName, 1.0f, INVALID_JOINT );
  3716. // end the attack if we're a multiframe attack
  3717. EndAttack();
  3718. }
  3719. /*
  3720. =====================
  3721. idAI::TestMelee
  3722. =====================
  3723. */
  3724. bool idAI::TestMelee() const {
  3725. trace_t trace;
  3726. idActor *enemyEnt = enemy.GetEntity();
  3727. if ( !enemyEnt || !melee_range ) {
  3728. return false;
  3729. }
  3730. //FIXME: make work with gravity vector
  3731. idVec3 org = physicsObj.GetOrigin();
  3732. const idBounds &myBounds = physicsObj.GetBounds();
  3733. idBounds bounds;
  3734. // expand the bounds out by our melee range
  3735. bounds[0][0] = -melee_range;
  3736. bounds[0][1] = -melee_range;
  3737. bounds[0][2] = myBounds[0][2] - 4.0f;
  3738. bounds[1][0] = melee_range;
  3739. bounds[1][1] = melee_range;
  3740. bounds[1][2] = myBounds[1][2] + 4.0f;
  3741. bounds.TranslateSelf( org );
  3742. idVec3 enemyOrg = enemyEnt->GetPhysics()->GetOrigin();
  3743. idBounds enemyBounds = enemyEnt->GetPhysics()->GetBounds();
  3744. enemyBounds.TranslateSelf( enemyOrg );
  3745. if ( ai_debugMove.GetBool() ) {
  3746. gameRenderWorld->DebugBounds( colorYellow, bounds, vec3_zero, 1 );
  3747. }
  3748. if ( !bounds.IntersectsBounds( enemyBounds ) ) {
  3749. return false;
  3750. }
  3751. idVec3 start = GetEyePosition();
  3752. idVec3 end = enemyEnt->GetEyePosition();
  3753. gameLocal.clip.TracePoint( trace, start, end, MASK_SHOT_BOUNDINGBOX, this );
  3754. if ( ( trace.fraction == 1.0f ) || ( gameLocal.GetTraceEntity( trace ) == enemyEnt ) ) {
  3755. return true;
  3756. }
  3757. return false;
  3758. }
  3759. /*
  3760. =====================
  3761. idAI::AttackMelee
  3762. jointname allows the endpoint to be exactly specified in the model,
  3763. as for the commando tentacle. If not specified, it will be set to
  3764. the facing direction + melee_range.
  3765. kickDir is specified in the monster's coordinate system, and gives the direction
  3766. that the view kick and knockback should go
  3767. =====================
  3768. */
  3769. bool idAI::AttackMelee( const char *meleeDefName ) {
  3770. const idDict *meleeDef;
  3771. idActor *enemyEnt = enemy.GetEntity();
  3772. const char *p;
  3773. const idSoundShader *shader;
  3774. meleeDef = gameLocal.FindEntityDefDict( meleeDefName, false );
  3775. if ( meleeDef == NULL ) {
  3776. gameLocal.Error( "Unknown melee '%s'", meleeDefName );
  3777. return false;
  3778. }
  3779. if ( enemyEnt == NULL ) {
  3780. p = meleeDef->GetString( "snd_miss" );
  3781. if ( p != NULL && *p != NULL ) {
  3782. shader = declManager->FindSound( p );
  3783. StartSoundShader( shader, SND_CHANNEL_DAMAGE, 0, false, NULL );
  3784. }
  3785. return false;
  3786. }
  3787. // check for the "saving throw" automatic melee miss on lethal blow
  3788. // stupid place for this.
  3789. bool forceMiss = false;
  3790. if ( enemyEnt->IsType( idPlayer::Type ) && g_skill.GetInteger() < 2 ) {
  3791. int damage, armor;
  3792. idPlayer *player = static_cast<idPlayer*>( enemyEnt );
  3793. player->CalcDamagePoints( this, this, meleeDef, 1.0f, INVALID_JOINT, &damage, &armor );
  3794. if ( enemyEnt->health <= damage ) {
  3795. int t = gameLocal.time - player->lastSavingThrowTime;
  3796. if ( t > SAVING_THROW_TIME ) {
  3797. player->lastSavingThrowTime = gameLocal.time;
  3798. t = 0;
  3799. }
  3800. if ( t < 1000 ) {
  3801. gameLocal.Printf( "Saving throw.\n" );
  3802. forceMiss = true;
  3803. }
  3804. }
  3805. }
  3806. // make sure the trace can actually hit the enemy
  3807. if ( forceMiss || !TestMelee() ) {
  3808. // missed
  3809. p = meleeDef->GetString( "snd_miss" );
  3810. if ( p != NULL && *p != NULL ) {
  3811. shader = declManager->FindSound( p );
  3812. StartSoundShader( shader, SND_CHANNEL_DAMAGE, 0, false, NULL );
  3813. }
  3814. return false;
  3815. }
  3816. //
  3817. // do the damage
  3818. //
  3819. p = meleeDef->GetString( "snd_hit" );
  3820. if ( p != NULL && *p != NULL ) {
  3821. shader = declManager->FindSound( p );
  3822. StartSoundShader( shader, SND_CHANNEL_DAMAGE, 0, false, NULL );
  3823. }
  3824. idVec3 kickDir;
  3825. meleeDef->GetVector( "kickDir", "0 0 0", kickDir );
  3826. idVec3 globalKickDir;
  3827. globalKickDir = ( viewAxis * physicsObj.GetGravityAxis() ) * kickDir;
  3828. enemyEnt->Damage( this, this, globalKickDir, meleeDefName, 1.0f, INVALID_JOINT );
  3829. lastAttackTime = gameLocal.time;
  3830. return true;
  3831. }
  3832. /*
  3833. ================
  3834. idAI::PushWithAF
  3835. ================
  3836. */
  3837. void idAI::PushWithAF() {
  3838. int i, j;
  3839. afTouch_t touchList[ MAX_GENTITIES ];
  3840. idEntity *pushed_ents[ MAX_GENTITIES ];
  3841. idEntity *ent;
  3842. idVec3 vel;
  3843. int num_pushed;
  3844. num_pushed = 0;
  3845. af.ChangePose( this, gameLocal.time );
  3846. int num = af.EntitiesTouchingAF( touchList );
  3847. for( i = 0; i < num; i++ ) {
  3848. if ( touchList[ i ].touchedEnt->IsType( idProjectile::Type ) ) {
  3849. // skip projectiles
  3850. continue;
  3851. }
  3852. // make sure we havent pushed this entity already. this avoids causing double damage
  3853. for( j = 0; j < num_pushed; j++ ) {
  3854. if ( pushed_ents[ j ] == touchList[ i ].touchedEnt ) {
  3855. break;
  3856. }
  3857. }
  3858. if ( j >= num_pushed ) {
  3859. ent = touchList[ i ].touchedEnt;
  3860. pushed_ents[num_pushed++] = ent;
  3861. vel = ent->GetPhysics()->GetAbsBounds().GetCenter() - touchList[ i ].touchedByBody->GetWorldOrigin();
  3862. vel.Normalize();
  3863. if ( attack.Length() && ent->IsType( idActor::Type ) ) {
  3864. ent->Damage( this, this, vel, attack, 1.0f, INVALID_JOINT );
  3865. } else {
  3866. ent->GetPhysics()->SetLinearVelocity( 100.0f * vel, touchList[ i ].touchedClipModel->GetId() );
  3867. }
  3868. }
  3869. }
  3870. }
  3871. /***********************************************************************
  3872. Misc
  3873. ***********************************************************************/
  3874. /*
  3875. ================
  3876. idAI::GetMuzzle
  3877. ================
  3878. */
  3879. void idAI::GetMuzzle( const char *jointname, idVec3 &muzzle, idMat3 &axis ) {
  3880. jointHandle_t joint;
  3881. if ( !jointname || !jointname[ 0 ] ) {
  3882. muzzle = physicsObj.GetOrigin() + viewAxis[ 0 ] * physicsObj.GetGravityAxis() * 14;
  3883. muzzle -= physicsObj.GetGravityNormal() * physicsObj.GetBounds()[ 1 ].z * 0.5f;
  3884. } else {
  3885. joint = animator.GetJointHandle( jointname );
  3886. if ( joint == INVALID_JOINT ) {
  3887. gameLocal.Error( "Unknown joint '%s' on %s", jointname, GetEntityDefName() );
  3888. }
  3889. GetJointWorldTransform( joint, gameLocal.time, muzzle, axis );
  3890. }
  3891. }
  3892. /*
  3893. ================
  3894. idAI::TriggerWeaponEffects
  3895. ================
  3896. */
  3897. void idAI::TriggerWeaponEffects( const idVec3 &muzzle ) {
  3898. idVec3 org;
  3899. idMat3 axis;
  3900. if ( !g_muzzleFlash.GetBool() ) {
  3901. return;
  3902. }
  3903. // muzzle flash
  3904. // offset the shader parms so muzzle flashes show up
  3905. renderEntity.shaderParms[SHADERPARM_TIMEOFFSET] = -MS2SEC( gameLocal.time );
  3906. renderEntity.shaderParms[ SHADERPARM_DIVERSITY ] = gameLocal.random.CRandomFloat();
  3907. if ( flashJointWorld != INVALID_JOINT ) {
  3908. GetJointWorldTransform( flashJointWorld, gameLocal.time, org, axis );
  3909. if ( worldMuzzleFlash.lightRadius.x > 0.0f ) {
  3910. worldMuzzleFlash.axis = axis;
  3911. worldMuzzleFlash.shaderParms[SHADERPARM_TIMEOFFSET] = -MS2SEC( gameLocal.time );
  3912. if ( worldMuzzleFlashHandle != - 1 ) {
  3913. gameRenderWorld->UpdateLightDef( worldMuzzleFlashHandle, &worldMuzzleFlash );
  3914. } else {
  3915. worldMuzzleFlashHandle = gameRenderWorld->AddLightDef( &worldMuzzleFlash );
  3916. }
  3917. muzzleFlashEnd = gameLocal.time + flashTime;
  3918. UpdateVisuals();
  3919. }
  3920. }
  3921. }
  3922. /*
  3923. ================
  3924. idAI::UpdateMuzzleFlash
  3925. ================
  3926. */
  3927. void idAI::UpdateMuzzleFlash() {
  3928. if ( worldMuzzleFlashHandle != -1 ) {
  3929. if ( gameLocal.time >= muzzleFlashEnd ) {
  3930. gameRenderWorld->FreeLightDef( worldMuzzleFlashHandle );
  3931. worldMuzzleFlashHandle = -1;
  3932. } else {
  3933. idVec3 muzzle;
  3934. animator.GetJointTransform( flashJointWorld, gameLocal.time, muzzle, worldMuzzleFlash.axis );
  3935. animator.GetJointTransform( flashJointWorld, gameLocal.time, muzzle, worldMuzzleFlash.axis );
  3936. muzzle = physicsObj.GetOrigin() + ( muzzle + modelOffset ) * viewAxis * physicsObj.GetGravityAxis();
  3937. worldMuzzleFlash.origin = muzzle;
  3938. gameRenderWorld->UpdateLightDef( worldMuzzleFlashHandle, &worldMuzzleFlash );
  3939. }
  3940. }
  3941. }
  3942. /*
  3943. ================
  3944. idAI::Hide
  3945. ================
  3946. */
  3947. void idAI::Hide() {
  3948. idActor::Hide();
  3949. fl.takedamage = false;
  3950. physicsObj.SetContents( 0 );
  3951. physicsObj.GetClipModel()->Unlink();
  3952. StopSound( SND_CHANNEL_AMBIENT, false );
  3953. SetChatSound();
  3954. AI_ENEMY_IN_FOV = false;
  3955. AI_ENEMY_VISIBLE = false;
  3956. StopMove( MOVE_STATUS_DONE );
  3957. }
  3958. /*
  3959. ================
  3960. idAI::Show
  3961. ================
  3962. */
  3963. void idAI::Show() {
  3964. idActor::Show();
  3965. if ( spawnArgs.GetBool( "big_monster" ) ) {
  3966. physicsObj.SetContents( 0 );
  3967. } else if ( use_combat_bbox ) {
  3968. physicsObj.SetContents( CONTENTS_BODY|CONTENTS_SOLID );
  3969. } else {
  3970. physicsObj.SetContents( CONTENTS_BODY );
  3971. }
  3972. physicsObj.GetClipModel()->Link( gameLocal.clip );
  3973. fl.takedamage = !spawnArgs.GetBool( "noDamage" );
  3974. SetChatSound();
  3975. StartSound( "snd_ambient", SND_CHANNEL_AMBIENT, 0, false, NULL );
  3976. }
  3977. /*
  3978. =====================
  3979. idAI::SetChatSound
  3980. =====================
  3981. */
  3982. void idAI::SetChatSound() {
  3983. const char *snd;
  3984. if ( IsHidden() ) {
  3985. snd = NULL;
  3986. } else if ( enemy.GetEntity() ) {
  3987. snd = spawnArgs.GetString( "snd_chatter_combat", NULL );
  3988. chat_min = SEC2MS( spawnArgs.GetFloat( "chatter_combat_min", "5" ) );
  3989. chat_max = SEC2MS( spawnArgs.GetFloat( "chatter_combat_max", "10" ) );
  3990. } else if ( !spawnArgs.GetBool( "no_idle_chatter" ) ) {
  3991. snd = spawnArgs.GetString( "snd_chatter", NULL );
  3992. chat_min = SEC2MS( spawnArgs.GetFloat( "chatter_min", "5" ) );
  3993. chat_max = SEC2MS( spawnArgs.GetFloat( "chatter_max", "10" ) );
  3994. } else {
  3995. snd = NULL;
  3996. }
  3997. if ( snd != NULL && *snd != NULL ) {
  3998. chat_snd = declManager->FindSound( snd );
  3999. // set the next chat time
  4000. chat_time = gameLocal.time + chat_min + gameLocal.random.RandomFloat() * ( chat_max - chat_min );
  4001. } else {
  4002. chat_snd = NULL;
  4003. }
  4004. }
  4005. /*
  4006. ================
  4007. idAI::CanPlayChatterSounds
  4008. Used for playing chatter sounds on monsters.
  4009. ================
  4010. */
  4011. bool idAI::CanPlayChatterSounds() const {
  4012. if ( AI_DEAD ) {
  4013. return false;
  4014. }
  4015. if ( IsHidden() ) {
  4016. return false;
  4017. }
  4018. if ( enemy.GetEntity() ) {
  4019. return true;
  4020. }
  4021. if ( spawnArgs.GetBool( "no_idle_chatter" ) ) {
  4022. return false;
  4023. }
  4024. return true;
  4025. }
  4026. /*
  4027. =====================
  4028. idAI::PlayChatter
  4029. =====================
  4030. */
  4031. void idAI::PlayChatter() {
  4032. // check if it's time to play a chat sound
  4033. if ( AI_DEAD || !chat_snd || ( chat_time > gameLocal.time ) ) {
  4034. return;
  4035. }
  4036. StartSoundShader( chat_snd, SND_CHANNEL_VOICE, 0, false, NULL );
  4037. // set the next chat time
  4038. chat_time = gameLocal.time + chat_min + gameLocal.random.RandomFloat() * ( chat_max - chat_min );
  4039. }
  4040. /*
  4041. =====================
  4042. idAI::UpdateParticles
  4043. =====================
  4044. */
  4045. void idAI::UpdateParticles() {
  4046. if ( ( thinkFlags & TH_UPDATEPARTICLES) && !IsHidden() ) {
  4047. idVec3 realVector;
  4048. idMat3 realAxis;
  4049. int particlesAlive = 0;
  4050. for ( int i = 0; i < particles.Num(); i++ ) {
  4051. // Smoke particles on AI characters will always be "slow", even when held by grabber
  4052. SetTimeState ts(TIME_GROUP1);
  4053. if ( particles[i].particle && particles[i].time ) {
  4054. particlesAlive++;
  4055. if (af.IsActive()) {
  4056. realAxis = mat3_identity;
  4057. realVector = GetPhysics()->GetOrigin();
  4058. } else {
  4059. animator.GetJointTransform( particles[i].joint, gameLocal.time, realVector, realAxis );
  4060. realAxis *= renderEntity.axis;
  4061. realVector = physicsObj.GetOrigin() + ( realVector + modelOffset ) * ( viewAxis * physicsObj.GetGravityAxis() );
  4062. }
  4063. if ( !gameLocal.smokeParticles->EmitSmoke( particles[i].particle, particles[i].time, gameLocal.random.CRandomFloat(), realVector, realAxis, timeGroup /*_D3XP*/ )) {
  4064. if ( restartParticles ) {
  4065. particles[i].time = gameLocal.time;
  4066. } else {
  4067. particles[i].time = 0;
  4068. particlesAlive--;
  4069. }
  4070. }
  4071. }
  4072. }
  4073. if ( particlesAlive == 0 ) {
  4074. BecomeInactive( TH_UPDATEPARTICLES );
  4075. }
  4076. }
  4077. }
  4078. /*
  4079. =====================
  4080. idAI::TriggerParticles
  4081. =====================
  4082. */
  4083. void idAI::TriggerParticles( const char *jointName ) {
  4084. jointHandle_t jointNum;
  4085. jointNum = animator.GetJointHandle( jointName );
  4086. for ( int i = 0; i < particles.Num(); i++ ) {
  4087. if ( particles[i].joint == jointNum ) {
  4088. particles[i].time = gameLocal.time;
  4089. BecomeActive( TH_UPDATEPARTICLES );
  4090. }
  4091. }
  4092. }
  4093. void idAI::TriggerFX( const char* joint, const char* fx ) {
  4094. if( !strcmp(joint, "origin") ) {
  4095. idEntityFx::StartFx( fx, NULL, NULL, this, true );
  4096. } else {
  4097. idVec3 joint_origin;
  4098. idMat3 joint_axis;
  4099. jointHandle_t jointNum;
  4100. jointNum = animator.GetJointHandle( joint );
  4101. if ( jointNum == INVALID_JOINT ) {
  4102. gameLocal.Warning( "Unknown fx joint '%s' on entity %s", joint, name.c_str() );
  4103. return;
  4104. }
  4105. GetJointWorldTransform( jointNum, gameLocal.time, joint_origin, joint_axis );
  4106. idEntityFx::StartFx( fx, &joint_origin, &joint_axis, this, true );
  4107. }
  4108. }
  4109. idEntity* idAI::StartEmitter( const char* name, const char* joint, const char* particle ) {
  4110. idEntity* existing = GetEmitter(name);
  4111. if(existing) {
  4112. return existing;
  4113. }
  4114. jointHandle_t jointNum;
  4115. jointNum = animator.GetJointHandle( joint );
  4116. idVec3 offset;
  4117. idMat3 axis;
  4118. GetJointWorldTransform( jointNum, gameLocal.time, offset, axis );
  4119. /*animator.GetJointTransform( jointNum, gameLocal.time, offset, axis );
  4120. offset = GetPhysics()->GetOrigin() + offset * GetPhysics()->GetAxis();
  4121. axis = axis * GetPhysics()->GetAxis();*/
  4122. idDict args;
  4123. const idDeclEntityDef *emitterDef = gameLocal.FindEntityDef( "func_emitter", false );
  4124. args = emitterDef->dict;
  4125. args.Set("model", particle);
  4126. args.Set( "origin", offset.ToString() );
  4127. args.SetBool("start_off", true);
  4128. idEntity* ent;
  4129. gameLocal.SpawnEntityDef(args, &ent, false);
  4130. ent->GetPhysics()->SetOrigin(offset);
  4131. //ent->GetPhysics()->SetAxis(axis);
  4132. // align z-axis of model with the direction
  4133. /*idVec3 tmp;
  4134. axis = (viewAxis[ 0 ] * physicsObj.GetGravityAxis()).ToMat3();
  4135. tmp = axis[2];
  4136. axis[2] = axis[0];
  4137. axis[0] = -tmp;
  4138. ent->GetPhysics()->SetAxis(axis);*/
  4139. axis = physicsObj.GetGravityAxis();
  4140. ent->GetPhysics()->SetAxis(axis);
  4141. ent->GetPhysics()->GetClipModel()->SetOwner( this );
  4142. //Keep a reference to the emitter so we can track it
  4143. funcEmitter_t newEmitter;
  4144. strcpy(newEmitter.name, name);
  4145. newEmitter.particle = (idFuncEmitter*)ent;
  4146. newEmitter.joint = jointNum;
  4147. funcEmitters.Set(newEmitter.name, newEmitter);
  4148. //Bind it to the joint and make it active
  4149. newEmitter.particle->BindToJoint(this, jointNum, true);
  4150. newEmitter.particle->BecomeActive(TH_THINK);
  4151. newEmitter.particle->Show();
  4152. newEmitter.particle->PostEventMS(&EV_Activate, 0, this);
  4153. return newEmitter.particle;
  4154. }
  4155. idEntity* idAI::GetEmitter( const char* name ) {
  4156. funcEmitter_t* emitter;
  4157. funcEmitters.Get(name, &emitter);
  4158. if(emitter) {
  4159. return emitter->particle;
  4160. }
  4161. return NULL;
  4162. }
  4163. void idAI::StopEmitter( const char* name ) {
  4164. funcEmitter_t* emitter;
  4165. funcEmitters.Get(name, &emitter);
  4166. if(emitter) {
  4167. emitter->particle->Unbind();
  4168. emitter->particle->PostEventMS( &EV_Remove, 0 );
  4169. funcEmitters.Remove(name);
  4170. }
  4171. }
  4172. /***********************************************************************
  4173. Head & torso aiming
  4174. ***********************************************************************/
  4175. /*
  4176. ================
  4177. idAI::UpdateAnimationControllers
  4178. ================
  4179. */
  4180. bool idAI::UpdateAnimationControllers() {
  4181. idVec3 local;
  4182. idVec3 focusPos;
  4183. idQuat jawQuat;
  4184. idVec3 left;
  4185. idVec3 dir;
  4186. idVec3 orientationJointPos;
  4187. idVec3 localDir;
  4188. idAngles newLookAng;
  4189. idAngles diff;
  4190. idMat3 mat;
  4191. idMat3 axis;
  4192. idMat3 orientationJointAxis;
  4193. idAFAttachment *headEnt = head.GetEntity();
  4194. idVec3 eyepos;
  4195. idVec3 pos;
  4196. int i;
  4197. idAngles jointAng;
  4198. float orientationJointYaw;
  4199. if ( AI_DEAD ) {
  4200. return idActor::UpdateAnimationControllers();
  4201. }
  4202. if ( orientationJoint == INVALID_JOINT ) {
  4203. orientationJointAxis = viewAxis;
  4204. orientationJointPos = physicsObj.GetOrigin();
  4205. orientationJointYaw = current_yaw;
  4206. } else {
  4207. GetJointWorldTransform( orientationJoint, gameLocal.time, orientationJointPos, orientationJointAxis );
  4208. orientationJointYaw = orientationJointAxis[ 2 ].ToYaw();
  4209. orientationJointAxis = idAngles( 0.0f, orientationJointYaw, 0.0f ).ToMat3();
  4210. }
  4211. if ( focusJoint != INVALID_JOINT ) {
  4212. if ( headEnt ) {
  4213. headEnt->GetJointWorldTransform( focusJoint, gameLocal.time, eyepos, axis );
  4214. } else {
  4215. GetJointWorldTransform( focusJoint, gameLocal.time, eyepos, axis );
  4216. }
  4217. eyeOffset.z = eyepos.z - physicsObj.GetOrigin().z;
  4218. if ( ai_debugMove.GetBool() ) {
  4219. gameRenderWorld->DebugLine( colorRed, eyepos, eyepos + orientationJointAxis[ 0 ] * 32.0f, 1 );
  4220. }
  4221. } else {
  4222. eyepos = GetEyePosition();
  4223. }
  4224. if ( headEnt ) {
  4225. CopyJointsFromBodyToHead();
  4226. }
  4227. // Update the IK after we've gotten all the joint positions we need, but before we set any joint positions.
  4228. // Getting the joint positions causes the joints to be updated. The IK gets joint positions itself (which
  4229. // are already up to date because of getting the joints in this function) and then sets their positions, which
  4230. // forces the heirarchy to be updated again next time we get a joint or present the model. If IK is enabled,
  4231. // or if we have a seperate head, we end up transforming the joints twice per frame. Characters with no
  4232. // head entity and no ik will only transform their joints once. Set g_debuganim to the current entity number
  4233. // in order to see how many times an entity transforms the joints per frame.
  4234. idActor::UpdateAnimationControllers();
  4235. idEntity *focusEnt = focusEntity.GetEntity();
  4236. if ( !allowJointMod || !allowEyeFocus || ( gameLocal.time >= focusTime ) ) {
  4237. focusPos = GetEyePosition() + orientationJointAxis[ 0 ] * 512.0f;
  4238. } else if ( focusEnt == NULL ) {
  4239. // keep looking at last position until focusTime is up
  4240. focusPos = currentFocusPos;
  4241. } else if ( focusEnt == enemy.GetEntity() ) {
  4242. focusPos = lastVisibleEnemyPos + lastVisibleEnemyEyeOffset - eyeVerticalOffset * enemy.GetEntity()->GetPhysics()->GetGravityNormal();
  4243. } else if ( focusEnt->IsType( idActor::Type ) ) {
  4244. focusPos = static_cast<idActor *>( focusEnt )->GetEyePosition() - eyeVerticalOffset * focusEnt->GetPhysics()->GetGravityNormal();
  4245. } else {
  4246. focusPos = focusEnt->GetPhysics()->GetOrigin();
  4247. }
  4248. currentFocusPos = currentFocusPos + ( focusPos - currentFocusPos ) * eyeFocusRate;
  4249. // determine yaw from origin instead of from focus joint since joint may be offset, which can cause us to bounce between two angles
  4250. dir = focusPos - orientationJointPos;
  4251. newLookAng.yaw = idMath::AngleNormalize180( dir.ToYaw() - orientationJointYaw );
  4252. newLookAng.roll = 0.0f;
  4253. newLookAng.pitch = 0.0f;
  4254. #if 0
  4255. gameRenderWorld->DebugLine( colorRed, orientationJointPos, focusPos, 1 );
  4256. gameRenderWorld->DebugLine( colorYellow, orientationJointPos, orientationJointPos + orientationJointAxis[ 0 ] * 32.0f, 1 );
  4257. gameRenderWorld->DebugLine( colorGreen, orientationJointPos, orientationJointPos + newLookAng.ToForward() * 48.0f, 1 );
  4258. #endif
  4259. // determine pitch from joint position
  4260. dir = focusPos - eyepos;
  4261. dir.NormalizeFast();
  4262. orientationJointAxis.ProjectVector( dir, localDir );
  4263. newLookAng.pitch = -idMath::AngleNormalize180( localDir.ToPitch() );
  4264. newLookAng.roll = 0.0f;
  4265. diff = newLookAng - lookAng;
  4266. if ( eyeAng != diff ) {
  4267. eyeAng = diff;
  4268. eyeAng.Clamp( eyeMin, eyeMax );
  4269. idAngles angDelta = diff - eyeAng;
  4270. if ( !angDelta.Compare( ang_zero, 0.1f ) ) {
  4271. alignHeadTime = gameLocal.time;
  4272. } else {
  4273. alignHeadTime = gameLocal.time + ( 0.5f + 0.5f * gameLocal.random.RandomFloat() ) * focusAlignTime;
  4274. }
  4275. }
  4276. if ( idMath::Fabs( newLookAng.yaw ) < 0.1f ) {
  4277. alignHeadTime = gameLocal.time;
  4278. }
  4279. if ( ( gameLocal.time >= alignHeadTime ) || ( gameLocal.time < forceAlignHeadTime ) ) {
  4280. alignHeadTime = gameLocal.time + ( 0.5f + 0.5f * gameLocal.random.RandomFloat() ) * focusAlignTime;
  4281. destLookAng = newLookAng;
  4282. destLookAng.Clamp( lookMin, lookMax );
  4283. }
  4284. diff = destLookAng - lookAng;
  4285. if ( ( lookMin.pitch == -180.0f ) && ( lookMax.pitch == 180.0f ) ) {
  4286. if ( ( diff.pitch > 180.0f ) || ( diff.pitch <= -180.0f ) ) {
  4287. diff.pitch = 360.0f - diff.pitch;
  4288. }
  4289. }
  4290. if ( ( lookMin.yaw == -180.0f ) && ( lookMax.yaw == 180.0f ) ) {
  4291. if ( diff.yaw > 180.0f ) {
  4292. diff.yaw -= 360.0f;
  4293. } else if ( diff.yaw <= -180.0f ) {
  4294. diff.yaw += 360.0f;
  4295. }
  4296. }
  4297. lookAng = lookAng + diff * headFocusRate;
  4298. lookAng.Normalize180();
  4299. jointAng.roll = 0.0f;
  4300. for( i = 0; i < lookJoints.Num(); i++ ) {
  4301. jointAng.pitch = lookAng.pitch * lookJointAngles[ i ].pitch;
  4302. jointAng.yaw = lookAng.yaw * lookJointAngles[ i ].yaw;
  4303. animator.SetJointAxis( lookJoints[ i ], JOINTMOD_WORLD, jointAng.ToMat3() );
  4304. }
  4305. if ( move.moveType == MOVETYPE_FLY ) {
  4306. // lean into turns
  4307. AdjustFlyingAngles();
  4308. }
  4309. if ( headEnt ) {
  4310. idAnimator *headAnimator = headEnt->GetAnimator();
  4311. if ( allowEyeFocus ) {
  4312. idMat3 eyeAxis = ( lookAng + eyeAng ).ToMat3(); idMat3 headTranspose = headEnt->GetPhysics()->GetAxis().Transpose();
  4313. axis = eyeAxis * orientationJointAxis;
  4314. left = axis[ 1 ] * eyeHorizontalOffset;
  4315. eyepos -= headEnt->GetPhysics()->GetOrigin();
  4316. headAnimator->SetJointPos( leftEyeJoint, JOINTMOD_WORLD_OVERRIDE, eyepos + ( axis[ 0 ] * 64.0f + left ) * headTranspose );
  4317. headAnimator->SetJointPos( rightEyeJoint, JOINTMOD_WORLD_OVERRIDE, eyepos + ( axis[ 0 ] * 64.0f - left ) * headTranspose );
  4318. } else {
  4319. headAnimator->ClearJoint( leftEyeJoint );
  4320. headAnimator->ClearJoint( rightEyeJoint );
  4321. }
  4322. } else {
  4323. if ( allowEyeFocus ) {
  4324. idMat3 eyeAxis = ( lookAng + eyeAng ).ToMat3();
  4325. axis = eyeAxis * orientationJointAxis;
  4326. left = axis[ 1 ] * eyeHorizontalOffset;
  4327. eyepos += axis[ 0 ] * 64.0f - physicsObj.GetOrigin();
  4328. animator.SetJointPos( leftEyeJoint, JOINTMOD_WORLD_OVERRIDE, eyepos + left );
  4329. animator.SetJointPos( rightEyeJoint, JOINTMOD_WORLD_OVERRIDE, eyepos - left );
  4330. } else {
  4331. animator.ClearJoint( leftEyeJoint );
  4332. animator.ClearJoint( rightEyeJoint );
  4333. }
  4334. }
  4335. return true;
  4336. }
  4337. /***********************************************************************
  4338. idCombatNode
  4339. ***********************************************************************/
  4340. const idEventDef EV_CombatNode_MarkUsed( "markUsed" );
  4341. CLASS_DECLARATION( idEntity, idCombatNode )
  4342. EVENT( EV_CombatNode_MarkUsed, idCombatNode::Event_MarkUsed )
  4343. EVENT( EV_Activate, idCombatNode::Event_Activate )
  4344. END_CLASS
  4345. /*
  4346. =====================
  4347. idCombatNode::idCombatNode
  4348. =====================
  4349. */
  4350. idCombatNode::idCombatNode() {
  4351. min_dist = 0.0f;
  4352. max_dist = 0.0f;
  4353. cone_dist = 0.0f;
  4354. min_height = 0.0f;
  4355. max_height = 0.0f;
  4356. cone_left.Zero();
  4357. cone_right.Zero();
  4358. offset.Zero();
  4359. disabled = false;
  4360. }
  4361. /*
  4362. =====================
  4363. idCombatNode::Save
  4364. =====================
  4365. */
  4366. void idCombatNode::Save( idSaveGame *savefile ) const {
  4367. savefile->WriteFloat( min_dist );
  4368. savefile->WriteFloat( max_dist );
  4369. savefile->WriteFloat( cone_dist );
  4370. savefile->WriteFloat( min_height );
  4371. savefile->WriteFloat( max_height );
  4372. savefile->WriteVec3( cone_left );
  4373. savefile->WriteVec3( cone_right );
  4374. savefile->WriteVec3( offset );
  4375. savefile->WriteBool( disabled );
  4376. }
  4377. /*
  4378. =====================
  4379. idCombatNode::Restore
  4380. =====================
  4381. */
  4382. void idCombatNode::Restore( idRestoreGame *savefile ) {
  4383. savefile->ReadFloat( min_dist );
  4384. savefile->ReadFloat( max_dist );
  4385. savefile->ReadFloat( cone_dist );
  4386. savefile->ReadFloat( min_height );
  4387. savefile->ReadFloat( max_height );
  4388. savefile->ReadVec3( cone_left );
  4389. savefile->ReadVec3( cone_right );
  4390. savefile->ReadVec3( offset );
  4391. savefile->ReadBool( disabled );
  4392. }
  4393. /*
  4394. =====================
  4395. idCombatNode::Spawn
  4396. =====================
  4397. */
  4398. void idCombatNode::Spawn() {
  4399. float fov;
  4400. float yaw;
  4401. float height;
  4402. min_dist = spawnArgs.GetFloat( "min" );
  4403. max_dist = spawnArgs.GetFloat( "max" );
  4404. height = spawnArgs.GetFloat( "height" );
  4405. fov = spawnArgs.GetFloat( "fov", "60" );
  4406. offset = spawnArgs.GetVector( "offset" );
  4407. const idVec3 &org = GetPhysics()->GetOrigin() + offset;
  4408. min_height = org.z - height * 0.5f;
  4409. max_height = min_height + height;
  4410. const idMat3 &axis = GetPhysics()->GetAxis();
  4411. yaw = axis[ 0 ].ToYaw();
  4412. idAngles leftang( 0.0f, yaw + fov * 0.5f - 90.0f, 0.0f );
  4413. cone_left = leftang.ToForward();
  4414. idAngles rightang( 0.0f, yaw - fov * 0.5f + 90.0f, 0.0f );
  4415. cone_right = rightang.ToForward();
  4416. disabled = spawnArgs.GetBool( "start_off" );
  4417. }
  4418. /*
  4419. =====================
  4420. idCombatNode::IsDisabled
  4421. =====================
  4422. */
  4423. bool idCombatNode::IsDisabled() const {
  4424. return disabled;
  4425. }
  4426. /*
  4427. =====================
  4428. idCombatNode::DrawDebugInfo
  4429. =====================
  4430. */
  4431. void idCombatNode::DrawDebugInfo() {
  4432. idEntity *ent;
  4433. idCombatNode *node;
  4434. idPlayer *player = gameLocal.GetLocalPlayer();
  4435. idVec4 color;
  4436. idBounds bounds( idVec3( -16, -16, 0 ), idVec3( 16, 16, 0 ) );
  4437. for( ent = gameLocal.spawnedEntities.Next(); ent != NULL; ent = ent->spawnNode.Next() ) {
  4438. if ( !ent->IsType( idCombatNode::Type ) ) {
  4439. continue;
  4440. }
  4441. node = static_cast<idCombatNode *>( ent );
  4442. if ( node->disabled ) {
  4443. color = colorMdGrey;
  4444. } else if ( player != NULL && node->EntityInView( player, player->GetPhysics()->GetOrigin() ) ) {
  4445. color = colorYellow;
  4446. } else {
  4447. color = colorRed;
  4448. }
  4449. idVec3 leftDir( -node->cone_left.y, node->cone_left.x, 0.0f );
  4450. idVec3 rightDir( node->cone_right.y, -node->cone_right.x, 0.0f );
  4451. idVec3 org = node->GetPhysics()->GetOrigin() + node->offset;
  4452. bounds[ 1 ].z = node->max_height;
  4453. leftDir.NormalizeFast();
  4454. rightDir.NormalizeFast();
  4455. const idMat3 &axis = node->GetPhysics()->GetAxis();
  4456. float cone_dot = node->cone_right * axis[ 1 ];
  4457. if ( idMath::Fabs( cone_dot ) > 0.1 ) {
  4458. float cone_dist = node->max_dist / cone_dot;
  4459. idVec3 pos1 = org + leftDir * node->min_dist;
  4460. idVec3 pos2 = org + leftDir * cone_dist;
  4461. idVec3 pos3 = org + rightDir * node->min_dist;
  4462. idVec3 pos4 = org + rightDir * cone_dist;
  4463. gameRenderWorld->DebugLine( color, node->GetPhysics()->GetOrigin(), ( pos1 + pos3 ) * 0.5f, 1 );
  4464. gameRenderWorld->DebugLine( color, pos1, pos2, 1 );
  4465. gameRenderWorld->DebugLine( color, pos1, pos3, 1 );
  4466. gameRenderWorld->DebugLine( color, pos3, pos4, 1 );
  4467. gameRenderWorld->DebugLine( color, pos2, pos4, 1 );
  4468. gameRenderWorld->DebugBounds( color, bounds, org, 1 );
  4469. }
  4470. }
  4471. }
  4472. /*
  4473. =====================
  4474. idCombatNode::EntityInView
  4475. =====================
  4476. */
  4477. bool idCombatNode::EntityInView( idActor *actor, const idVec3 &pos ) {
  4478. if ( !actor || ( actor->health <= 0 ) ) {
  4479. return false;
  4480. }
  4481. const idBounds &bounds = actor->GetPhysics()->GetBounds();
  4482. if ( ( pos.z + bounds[ 1 ].z < min_height ) || ( pos.z + bounds[ 0 ].z >= max_height ) ) {
  4483. return false;
  4484. }
  4485. const idVec3 &org = GetPhysics()->GetOrigin() + offset;
  4486. const idMat3 &axis = GetPhysics()->GetAxis();
  4487. idVec3 dir = pos - org;
  4488. float dist = dir * axis[ 0 ];
  4489. if ( ( dist < min_dist ) || ( dist > max_dist ) ) {
  4490. return false;
  4491. }
  4492. float left_dot = dir * cone_left;
  4493. if ( left_dot < 0.0f ) {
  4494. return false;
  4495. }
  4496. float right_dot = dir * cone_right;
  4497. if ( right_dot < 0.0f ) {
  4498. return false;
  4499. }
  4500. return true;
  4501. }
  4502. /*
  4503. =====================
  4504. idCombatNode::Event_Activate
  4505. =====================
  4506. */
  4507. void idCombatNode::Event_Activate( idEntity *activator ) {
  4508. disabled = !disabled;
  4509. }
  4510. /*
  4511. =====================
  4512. idCombatNode::Event_MarkUsed
  4513. =====================
  4514. */
  4515. void idCombatNode::Event_MarkUsed() {
  4516. if ( spawnArgs.GetBool( "use_once" ) ) {
  4517. disabled = true;
  4518. }
  4519. }