doofus.cpp 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // doofus.cpp
  19. // Project: Postal
  20. //
  21. // This module implements the CDoofus class which is the class of enemy
  22. // guys for the game.
  23. //
  24. // History:
  25. // 01/13/97 BRH Started this file from CDude and modified it
  26. // to do some enemy logic using the same assets
  27. // as the sample 2D guy.
  28. //
  29. // 01/15/97 BRH Changed the render to draw the guy with his
  30. // hotspot between his feet.
  31. //
  32. // 02/04/97 JMI Changed LoadDib() call to Load() (which now supports
  33. // loading of DIBs).
  34. //
  35. // 02/05/97 BRH Fixed problem loading Instance ID. Also fixed some other
  36. // problems with the run routine. He now will follow the
  37. // bouys to get to the bouy nearest the CDude.
  38. //
  39. // 02/23/97 BRH Changed coordinate system to x, -z in Find Direction.
  40. //
  41. // 02/25/97 BRH Fixed problem with the Startup where the height was not
  42. // being multiplied by 4, so enemies would start too low
  43. // if placed on a roof.
  44. //
  45. // 03/04/97 BRH Derived this from CCharacter instead of CThing
  46. //
  47. // 03/05/97 BRH Added SelectRandomBouy function which picks a random
  48. // bouy number (a valid one) or returns 0 if there are
  49. // no bouys.
  50. //
  51. // 03/06/97 BRH Added realignment timer and function so that the
  52. // direction to the bouy can be recalculated every
  53. // so often to avoid missing it.
  54. //
  55. // 03/13/97 JMI Load now takes a version number.
  56. //
  57. // 03/14/97 JMI SelectDude() now chooses the closest dude on the X/Z plane.
  58. //
  59. // 04/04/97 JMI SelectDude() no longer chooses dead dudes.
  60. // Also, last update to make SelectDude() find the closest
  61. // CDude was not comparing the distance to the CDude from this
  62. // guy, but instead was comparing the distance to the CDude from
  63. // (0, 0, 0).
  64. //
  65. // 04/16/97 BRH Changed references to the realm's list of CThings to use
  66. // the new non-STL methods.
  67. //
  68. // 04/22/97 BRH Moved common code and some variables like the animations
  69. // to the base class. Put common logic routines like the
  70. // reactions to weapons in this base class.
  71. //
  72. // 04/24/97 BRH Added TryClearDirection function that uses the
  73. // IsPathClear() funciton to try 3 directions to see if they
  74. // are clear.
  75. //
  76. // 05/02/97 JMI Added check to make sure not already in shot state or
  77. // writhing state before changing to shot state in OnShotMsg.
  78. // Also, now OnBurnMsg() sets him into the m_animOnFire in-
  79. // stead of m_animRun.
  80. //
  81. // 05/04/97 BRH Removed #ifdef code sections referring to STL lists.
  82. //
  83. // 05/06/97 BRH Added Popout logic, detection smash.
  84. //
  85. // 05/09/97 BRH Added Writhing and Shot logic from CPerson.
  86. //
  87. // 05/11/97 BRH Fixed problem with Logic_Guard.
  88. //
  89. // 05/15/97 BRH In the popout and run & shoot wait states, changed from
  90. // the temporary timeout method to checking for the
  91. // triggered pylon before running the logic.
  92. //
  93. // 05/18/97 BRH Added some logic functions for the victims to use.
  94. //
  95. // 05/20/97 BRH Added the hiding states. Also added calls to
  96. // ReevaluateState() at times in the action cycles
  97. // where the action can be changed. Changed Logic_PylonDetect
  98. // to set flags for use in ReevaluateState() and
  99. // in the logic table variables evaluation.
  100. //
  101. // 05/21/97 BRH Added m_dAnimRot to specify the direction the guy is
  102. // facing and separating that from the direction he is
  103. // moving. This will be used for the run and shoot
  104. // so that he can run and face sideways.
  105. //
  106. // 05/22/97 BRH Fixed typo bug in Logic_Shoot.
  107. //
  108. // 05/25/97 BRH Added the m_ShootAngle variable and an override for
  109. // ShootWeapon that uses this angle to aim the weapon.
  110. //
  111. // 05/26/97 BRH Changed ShootWeapon so that CSmash bits are passed in
  112. // so that enemy bullets don't hit other enemies.
  113. //
  114. // 05/26/97 BRH Added a timer for shooting to limit the number of shots
  115. // for each type of gun. Also added some avoidance
  116. // of obstacles during MoveNext in case the guy gets stuck
  117. // trying to get to the next bouy.
  118. //
  119. // 05/27/97 BRH Fixed a few problems with logic table transitions.
  120. //
  121. // 05/27/97 BRH Added some avoidance of obstacles to Logic_PopBegin in
  122. // case he gets stuck on walls while trying to find the
  123. // first pylon.
  124. //
  125. // 05/31/97 JMI Replaced m_pDude with m_idDude. The problem was that, by
  126. // just using a pointer to the dude, we never found out when
  127. // the dude was gone (deleted). Although this is rare for
  128. // CDudes, it does happen. For example, in the beginning of
  129. // a level all CDudes that do not have an associated player
  130. // are sent a Delete msg. They do not process this message
  131. // until their respective Update() calls. If a CDoofus
  132. // derived guy happened to be placed in the level before a
  133. // CDude (that is, the CDoofus' Update() got called before the
  134. // CDude's), and the CDoofus happened to point its m_pDude at
  135. // this CDude (that was destined to soon be deleted), later
  136. // when referencing the pointer to the freed and/or reallocated
  137. // memory, the CDoofus could cause a protection fault or math
  138. // overflow (due to invalid values returned by
  139. // m_pDude->GetX, Y, Z() with the non-CDude 'this' pointer).
  140. // Also, in the case that SelectDude() did not select a dude,
  141. // SQDistanceToDude() was returning an unitialized double
  142. // which, in some cases, could be a totally illegal double
  143. // value.
  144. //
  145. // 06/02/97 BRH Added AdvanceHold action and state so that once he reaches
  146. // the end of the advancement, he goes into this hold state
  147. // rather than Engage automatically. This way the logic
  148. // table can have more control over the next state.
  149. //
  150. // 06/02/97 BRH Changed TryClearShot to use the CRealm version of
  151. // IsPathClear which just checks for terrain obstacles.
  152. //
  153. // 06/05/97 JMI Changed m_sHitPoints to m_stockpile.m_sHitPoints to
  154. // accommodate new m_stockpile in base class, CThing3d (which
  155. // used to contain the m_sHitPoints).
  156. //
  157. // 06/10/97 BRH Now sends messages to CDemon for Explosion, and burning.
  158. //
  159. // 06/10/97 BRH Added RunIdleAnimation() function which monitors the
  160. // idle timer and controls which of 3 idle animations to
  161. // use. This should be called when in some kind of wait
  162. // state.
  163. //
  164. // 06/10/97 BRH Fixed "YMCA" bug where the enemy guy would cycle
  165. // each frame between Advance and AdvanceHold.
  166. //
  167. // 06/11/97 BRH Fixed the crouch and search animations since the
  168. // search is done from the crouch posiiton. It was
  169. // previously backwards because I thought the search
  170. // animation was done from a standing position.
  171. //
  172. // 06/13/97 JMI Changed FindDirection() to return m_dRot if we cannot find
  173. // a CDude.
  174. // Also, changed Logic_Shoot() over to using events.
  175. //
  176. // 06/17/97 BRH Changed NEAR_DEATH_HITPOINTS to a higher value since the
  177. // machine gun bullets were increased, it was diffucult to
  178. // get a writhing person.
  179. //
  180. // 06/17/97 BRH Attempted to make enemies stop shooting dead CDudes.
  181. //
  182. // 06/17/97 JMI Now doubles smash radius when in writhing state.
  183. //
  184. // 06/18/97 JMI Changed PlaySoundWrithing() to return the duration of the
  185. // played sample.
  186. //
  187. // 06/18/97 BRH Changed over to using GetRandom()
  188. //
  189. // 06/24/97 JMI Added intialization of m_sRotateDir.
  190. //
  191. // 06/26/97 BRH Added a special case for Writhers who get burned.
  192. // Previously they didn't react because their smash bits
  193. // were changed when they went into writhing. Then we wanted
  194. // them to get killed by fire so included the AlmostDead bits
  195. // but they they would jump to their feet and run around
  196. // once they got burned. So now they will just lie there
  197. // and die.
  198. //
  199. // 06/26/97 BRH When a doofus is killed after preparing a weapon but
  200. // before shooting it, he will drop it if it was a throwing
  201. // weapon, but will just delete it if it was a launched
  202. // weapon.
  203. //
  204. // 06/27/97 BRH Added a flag for recently stuck so that when a character
  205. // gets stuck on a wall, he sets the flag, then when he gets
  206. // free of the obstacle, it will get the closest bouy rather
  207. // than trying to align to the one it was trying to get to
  208. // previously which many times caused him to get stuck in
  209. // the same manner.
  210. //
  211. // 06/28/97 BRH Changed the RegisterBirth and RegisterDeath calls to pass
  212. // m_bCivilian so that the scoring can updated for hostiles
  213. // or civilians.
  214. //
  215. // 07/09/97 BRH Added logic for walking and running around on a bouy
  216. // network - used for victims.
  217. //
  218. // 07/11/97 BRH Added call to inline Cheater() to disable game if necessary
  219. //
  220. // 07/12/97 BRH Added addional text strings for the new actions that were
  221. // added. Also changed PylonDetect function to use
  222. // QuickCheckClosest rather than QuickCheck so that enemies
  223. // will detect the pylon you put them closest to and not
  224. // just a marker pylon.
  225. //
  226. // 07/12/97 BRH Changed macro MAX_STEPUP_THRESHOLD to use the CThing3d
  227. // definitino MaxStepUpThreshold so that the detecting and
  228. // ability to move will be the same.
  229. //
  230. // 07/15/97 BRH Added a few more calls to Cheater.
  231. //
  232. // 07/20/97 BRH Effectively canceled the DelayShoot state by setting
  233. // the timeout to zero since it caused the guys to
  234. // not shoot very often on levels that were difficult
  235. // to move in.
  236. //
  237. // 07/21/97 JMI Now Update() calls base class version.
  238. //
  239. // 07/23/97 BRH Added tunable values for different timeouts which will get
  240. // set in doofus to the default static values that they were
  241. // before, and can be set in Person to the personatorium values
  242. // so that different people have different traits.
  243. //
  244. // 08/01/97 BRH Changed the aiming so that it is based on the game
  245. // difficulty setting.
  246. //
  247. // 08/02/97 BRH Added YellForHelp funciton that enemies can call when they
  248. // get shot to alert others in the are that they need help.
  249. //
  250. // 08/05/97 BRH Fixed the problem with the doofus leaving the Run&Shoot
  251. // state at higher difficulty settings due to re-aligning
  252. // the angles with the shoot angle.
  253. //
  254. // 08/06/97 BRH Changed TryClearShot to first do the point translation
  255. // to the rigid body where the weapon is shot from, in order
  256. // to get the correct height. This will give the guys more
  257. // opportunities to shoot, and will work when the guys
  258. // are scaled larger or smaller.
  259. //
  260. // 08/06/97 JMI Added m_ptransExecutionTarget link point for execution
  261. // sphere. Also, added PositionSmash() to provide overridable
  262. // method for updating the collision sphere.
  263. //
  264. // 08/06/97 JMI Now TryClearPath() returns false if there's no weapon
  265. // link point.
  266. //
  267. // 08/07/97 JMI Added ms_awdWeapons[], ms_apszWeaponResNames[], and
  268. // GetResources() and FreeResources() for loading these anims.
  269. // Also, added ms_lWeaponResRefCount so we could know when the
  270. // weapons were no longer needed.
  271. //
  272. // 08/08/97 JMI Now Logic_Shoot() handles flamer.
  273. //
  274. // 08/08/97 JMI Now Logic_Shoot() handles AutoRifle, Uzi, and SmallPistol.
  275. //
  276. // 08/08/97 BRH Added start and end bouy ID's for special cases like
  277. // marching. Added these to load and save. They are set
  278. // by the dialog in the CPerson. Also added marching logic.
  279. //
  280. // 08/09/97 BRH Changed panic to be vicinity based so only the nearby
  281. // people panic. Also added checks for prepared weapons
  282. // when guys get blown up or burned, where they should
  283. // either randomly discard their weapon, or delete it.
  284. //
  285. // 08/10/97 JMI Moved CDoofus() and ~CDoofus() into doofus.cpp from
  286. // doofus.h.
  287. // Was going to move the registering of the birth into the
  288. // constructor but I realized that m_bCivilian probably won't
  289. // get set until Load() so I left it in Startup() (it could
  290. // probably also get set via EditModify() but this doesn't
  291. // matter b/c that's in edit mode only).
  292. // Now Registers death with the realm in the destructor if
  293. // m_bRegisteredBirth is true.
  294. // Also, now OnShotMsg() calls base class version even if
  295. // other states don't permit a state change b/c this gives
  296. // better feedback to the user (base class OnShotMsg() creates
  297. // blood).
  298. //
  299. // 08/10/97 JMI Moved NoWeapon up to enum value 0 and created a new one
  300. // to take its -1 place as an invalid weapon (InvalidWeapon).
  301. // Also, added block in PrepareWeapon() for the NoWeapon case.
  302. // Also, moved prepare weapon from the .H to the .CPP.
  303. //
  304. // 08/11/97 BRH Found the problem with the parade level victims where they
  305. // were flipping around one bouy. The problem was that they
  306. // asked how to get to a particular bouy which was unreachable
  307. // because the network was not fully connected, so when it
  308. // got the unreachable flag, it kept retrying the same bouy.
  309. // I fixed the problem so they will pick a new bouy, but also
  310. // we are deleting the unconnected nodes in the parade level.
  311. //
  312. // 08/11/97 JMI Now sets backup weapon to default 'none' and uses the
  313. // backup weapon when there's no animation for the current
  314. // weapon type.
  315. // Also, changed incorrectly name ms_awtType2Id to
  316. // ms_awtId2Type mapping.
  317. //
  318. // 08/11/97 BRH Fixed problem with run & shoot not using the correct
  319. // angle.
  320. //
  321. // 08/12/97 JMI Now hides weapon if current event is 10 or more.
  322. //
  323. // 08/12/97 BRH Checks for events channel in render so that people
  324. // without animation events like the band guys will still
  325. // work. Also made shooting timing based on game
  326. // difficulty.
  327. //
  328. // 08/13/97 JMI Changed so OnShotMsg() calls base class to generate blood
  329. // no matter what.
  330. //
  331. // 08/14/97 JMI Switched references to g_GameSettings.m_sDifficulty to
  332. // m_pRealm->m_flags.sDifficulty.
  333. //
  334. // 08/14/97 BRH Added static default bits to pass to weapons for their
  335. // collision testing. Changed call to WhileHoldingWeapon
  336. // to include these defaults.
  337. //
  338. // 08/15/97 BRH Changed the check for available pylons to check for a
  339. // clear path to the pylon before saying it is available.
  340. // Some guys were getting stuck on fences between themselves
  341. // an an available pylon. Also fixed the writhing to
  342. // executed transition so that the guys don't flip around
  343. // 180 degrees. Also fixed the smash bits that get passed
  344. // to the missile weapons.
  345. //
  346. // 08/16/97 BRH Changed the person's PlaySound functions so that comments
  347. // are only made when the CDude is alive. So in this module, I
  348. // made sure that the victims were also calling SelectDude()
  349. // for the logic that they were doing, just to keep track of
  350. // whether the CDude was alive or dead. Also tuned the
  351. // shooting accuracy for the levels to make them more accurate
  352. // on the easier levels since the easy dudes were missing too
  353. // much.
  354. //
  355. // 08/18/97 BRH Added virtual override for CCharacter's WhileHoldingWeapon
  356. // so that for higher difficulty settings where the enemies
  357. // re-aim after preparing the weapon, they could do it
  358. // every frame between PrepareWeapon and ShootWeapon so that
  359. // they turned smoothy and didn't just flip around when they
  360. // got to ShootWeapon. This was a problem with the Rocketman
  361. // who had a long shoot animation. If the target moved a
  362. // quite a bit between PrepareWeapon and ShootWeapon, he would
  363. // spin around just before releasing the shot. Also fixed
  364. // a problem with the ShootWeapon adjustment of the
  365. // random shooting innacuracy when I changed the values
  366. // last time, I adjusted the max left swing, but forgot to
  367. // adjust the random amount.
  368. //
  369. // 08/18/97 JMI Now sends doofuses who are writhing to the back most sprite
  370. // layer. As people pointed out this is another of two evils.
  371. // It is, however, a lessor. It appears a little wierd in one
  372. // case but better in all others I've seen so far.
  373. //
  374. // 08/19/97 JMI Now sends doofuses who are dying to the back as well.
  375. //
  376. // 08/19/97 JMI No longer shoots CSmash::Misc items.
  377. //
  378. // 08/20/97 JMI Now Logic_Shot() does not wait to the end of the animation
  379. // to check for death. This way they don't seem to kick back
  380. // from the shot, shoot forward, and then die.
  381. //
  382. // 08/20/97 BRH Trying a smaller tolerance on the Bouys so that the
  383. // enemies must get closer to the hotspot before
  384. // moving to the next one. We wanted to see if this helps
  385. // certain situations where guys are getting stuck. Changed
  386. // from 10 pixel radius to 5.
  387. //
  388. // 08/21/97 JMI Now does deluxe reporting on whether this doofus finds his
  389. // NavNet.
  390. //
  391. // 08/21/97 JMI Now after reporting that the doofus did not find its NavNet
  392. // it sets him to the current NavNet.
  393. //
  394. // 08/21/97 BRH Added a blooc counter so that the number of blood pools
  395. // created while writhing could be cut down a little bit.
  396. //
  397. // 08/24/97 BRH Changed the TryClearShot to use the Character version of
  398. // IsPathClear which also checks for people in the way. Added
  399. // an additional call to TryClearShot in ShootWeapon which is
  400. // after the weapon has been re-aimed to make sure that the
  401. // path is still clear. It was a problem with the rocketman
  402. // who has the slowest animation and may turn completely
  403. // around before shooting.
  404. //
  405. // 08/24/97 BRH Aborts sample that was playing when the guy gets executed
  406. // so that he doesn't keep making noises after he is dead.
  407. //
  408. // 08/25/97 BRH Undid a bunch of changes made yesterday which changed the
  409. // logic too much which threw off the tuning. Added two new
  410. // escape routes for the Engage mode instead. Fixed the
  411. // shoot timer which was being set to two different values
  412. // in two different places. Made the shooting more accurate
  413. // and more of the difficulty tuning will be with the
  414. // shooting times.
  415. //
  416. // 08/27/97 BRH Victims now panic when they are shot.
  417. //
  418. // 09/03/97 JMI Sentries now exclude CSmash::Bads and CSmash::Civilians.
  419. //
  420. // 09/07/97 BRH As Steve requested, the medium difficulty will now
  421. // re-adjust aiming just like hard difficulty in ShootWeapon.
  422. //
  423. // 12/18/97 JMI Changed SelectRandomBouy() to return 0 if a the number of
  424. // bouys is less than or equal to one. It used to be less
  425. // than one but, for some reason, GetNumNodes() returns 1 when
  426. // there's no bouys. This caused it to lock up when no bouys
  427. // for some lgk files.
  428. //
  429. ////////////////////////////////////////////////////////////////////////////////
  430. #define DOOFUS_CPP
  431. #include "RSPiX.h"
  432. #include <math.h>
  433. #include "doofus.h"
  434. #include "dude.h"
  435. #include "SampleMaster.h"
  436. #include "grenade.h"
  437. #include "pylon.h"
  438. ////////////////////////////////////////////////////////////////////////////////
  439. // Macros/types/etc.
  440. ////////////////////////////////////////////////////////////////////////////////
  441. #define NEAR_DEATH_HITPOINTS 31 // Below this, start writhing
  442. #define MS_BETWEEN_SAMPLES 100 // Time between pain groans.
  443. #define BURNT_BRIGHTNESS -40 // -128 to 127.
  444. ////////////////////////////////////////////////////////////////////////////////
  445. // Variables/data
  446. ////////////////////////////////////////////////////////////////////////////////
  447. // These are default values -- actually values are set using the editor!
  448. double CDoofus::ms_dAccUser = 150.0; // Acceleration due to user
  449. double CDoofus::ms_dAccDrag = 80.0; // Acceleration due to drag
  450. double CDoofus::ms_dMaxVelFore = 80.0; // Maximum forward velocity
  451. double CDoofus::ms_dMaxVelBack = -60.0; // Maximum backward velocity
  452. double CDoofus::ms_dDegPerSec = 150.0; // Degrees of rotation per second
  453. double CDoofus::ms_dOffScreenDistance = 500*500; // Square distance off screen
  454. double CDoofus::ms_dGuardDistance = 300*300; // Square distance before attacking
  455. double CDoofus::ms_dThrowHorizVel = 200; // Throw out at this velocity
  456. double CDoofus::ms_dMinFightDistance = 80.0; // Closest you want to get
  457. double CDoofus::ms_dMedFightDistance = 200.0; // Median distance for fighting
  458. double CDoofus::ms_dMaxFightDistance = 400.0; // Farthest distance for fighting
  459. double CDoofus::ms_dMinFightDistanceSQ = 80.0*80.0;// Square distance for
  460. double CDoofus::ms_dMedFightDistanceSQ = 200.0*200.0;
  461. double CDoofus::ms_dMaxFightDistanceSQ = 400.0*400.0;
  462. double CDoofus::ms_dMarchVelocity = 20.0; // Speed at which to march
  463. long CDoofus::ms_lDefaultAlignTime = 100; //2000; // Time to realign to bouy position
  464. long CDoofus::ms_lGuardTimeoutMin = 4000; // Time to check for CDudes again
  465. long CDoofus::ms_lGuardTimeoutInc = 500; // Interval to add for each easier difficulty level
  466. long CDoofus::ms_lShootTimeoutMin = 1000; // Min time between shots, adjused for difficulty
  467. long CDoofus::ms_lShootTimeoutInc = 200; // Adjustment time for difficulty level between shots
  468. long CDoofus::ms_lDetectionRadius = 100;//80 // Radius of detection smash sphere
  469. long CDoofus::ms_lRunShootInterval = 2000; // Time between shots while running
  470. long CDoofus::ms_lReseekTime = 15 * 1000; // Seek the dude again after this time
  471. long CDoofus::ms_lShotTimeout = 3000; // Time between full shot reaction animations
  472. // which gives him a chance to attack or run
  473. long CDoofus::ms_lStuckRecoveryTime = 5000; // Time to stay in recoverys state
  474. long CDoofus::ms_lAvoidRadius = 40; // Radius of fire detection smash
  475. long CDoofus::ms_lYellRadius = 150; // Yell for help in this vicinity
  476. long CDoofus::ms_lHelpTimeout = 3000; // Time to react to a call for help.
  477. long CDoofus::ms_lDelayShootTimeout = 0; //2000; // Time before shooting
  478. long CDoofus::ms_lHelpingTimeout = 1000; // Only shoot every this often
  479. // Note that these seem to apply to all weapons except bullet weapons. That is, these are
  480. // passed to WhileHoldingWeapon() which passes them on to ShootWeapon(), but WhileHoldingWeapon()
  481. // is only used for non-bullet weapons. In the bullet weapons case, it uses the default parameters
  482. // to ShootWeapon().
  483. U32 CDoofus::ms_u32CollideBitsInclude = CSmash::Character | CSmash::Barrel;
  484. U32 CDoofus::ms_u32CollideBitsDontcare = CSmash::Good | CSmash::Bad;
  485. U32 CDoofus::ms_u32CollideBitsExclude = CSmash::SpecialBarrel | CSmash::Ducking | CSmash::Bad | CSmash::Civilian;
  486. short CDoofus::ms_sStuckLimit = 3; // Number of times to retry before attempting to
  487. // get free of whatever you are stuck on.
  488. // Weapon animations.
  489. CAnim3D CDoofus::ms_aanimWeapons[NumWeaponTypes];
  490. // Current ref count on ms_aanimWeapons[].
  491. long CDoofus::ms_lWeaponResRefCount = 0;
  492. // Weapon details (descriptions, res names, etc.).
  493. CDoofus::WeaponDetails CDoofus::ms_awdWeapons[NumWeaponTypes] =
  494. {
  495. //////// NoWeapon
  496. { // pszName, pszResName, CThing ID
  497. "Verbal Abuse",
  498. NULL,
  499. TotalIDs,
  500. },
  501. //////// Rocket
  502. { // pszName, pszResName, CThing ID
  503. "Rocket",
  504. "3d/launcher",
  505. CRocketID,
  506. },
  507. //////// Grenade
  508. { // pszName, pszResName, CThing ID
  509. "Grenade",
  510. NULL,
  511. CGrenadeID,
  512. },
  513. //////// Napalm
  514. { // pszName, pszResName, CThing ID
  515. "Napalm",
  516. "3d/napalmer",
  517. CNapalmID,
  518. },
  519. //////// Firebomb
  520. { // pszName, pszResName, CThing ID
  521. "Cocktail",
  522. NULL,
  523. CFirebombID,
  524. },
  525. //////// ProximityMine
  526. { // pszName, pszResName, CThing ID
  527. "ProximityMine",
  528. NULL,
  529. CProximityMineID,
  530. },
  531. //////// TimedMine
  532. { // pszName, pszResName, CThing ID
  533. "TimedMine",
  534. NULL,
  535. CTimedMineID,
  536. },
  537. //////// RemoteMine
  538. { // pszName, pszResName, CThing ID
  539. "RemoteMine",
  540. NULL,
  541. CRemoteControlMineID,
  542. },
  543. //////// BouncingBetty
  544. { // pszName, pszResName, CThing ID
  545. "BouncingBetty",
  546. NULL,
  547. CBouncingBettyMineID,
  548. },
  549. //////// Flamer
  550. { // pszName, pszResName, CThing ID
  551. "Flamer",
  552. "3d/flmthrower",
  553. CFirestreamID,
  554. },
  555. //////// Pistol
  556. { // pszName, pszResName, CThing ID
  557. "Pistol",
  558. "3d/bigpistol",
  559. CPistolID,
  560. },
  561. //////// MachineGun
  562. { // pszName, pszResName, CThing ID
  563. "MachineGun",
  564. "3d/submachine",
  565. CMachineGunID,
  566. },
  567. //////// ShotGun
  568. { // pszName, pszResName, CThing ID
  569. "ShotGun",
  570. "3d/ShotGun",
  571. CShotGunID,
  572. },
  573. //////// Heatseeker
  574. { // pszName, pszResName, CThing ID
  575. "Heatseeker",
  576. "3d/launcher",
  577. CHeatseekerID,
  578. },
  579. //////// Assault
  580. { // pszName, pszResName, CThing ID
  581. "Assault",
  582. "3d/SprayGun",
  583. CAssaultWeaponID,
  584. },
  585. //////// DeathWad
  586. { // pszName, pszResName, CThing ID
  587. "DeathWad",
  588. "3d/napalmer",
  589. CDeathWadID,
  590. },
  591. //////// DoubleBarrel
  592. { // pszName, pszResName, CThing ID
  593. "DoubleBarrel",
  594. "3d/shotgun",
  595. CDoubleBarrelID,
  596. },
  597. //////// Uzi
  598. { // pszName, pszResName, CThing ID
  599. "Uzi",
  600. "3d/Uzi",
  601. CUziID,
  602. },
  603. //////// AutoRifle
  604. { // pszName, pszResName, CThing ID
  605. "AutoRifle",
  606. "3d/AutoRifle",
  607. CAutoRifleID,
  608. },
  609. //////// SmallPistol
  610. { // pszName, pszResName, CThing ID
  611. "SmallPistol",
  612. "3d/Pistol",
  613. CSmallPistolID,
  614. },
  615. //////// Dynamite
  616. { // pszName, pszResName, CThing ID
  617. "Dynamite",
  618. NULL,
  619. CDynamiteID,
  620. },
  621. };
  622. // Maps a CThing ID to a WeaponType enum.
  623. CDoofus::WeaponType CDoofus::ms_awtId2Type[TotalIDs] =
  624. {
  625. NoWeapon,
  626. InvalidWeapon,
  627. InvalidWeapon,
  628. InvalidWeapon,
  629. InvalidWeapon,
  630. InvalidWeapon,
  631. Rocket,
  632. Grenade,
  633. InvalidWeapon,
  634. InvalidWeapon,
  635. InvalidWeapon,
  636. InvalidWeapon,
  637. InvalidWeapon,
  638. Napalm,
  639. InvalidWeapon,
  640. InvalidWeapon,
  641. Firebomb,
  642. InvalidWeapon,
  643. InvalidWeapon,
  644. InvalidWeapon,
  645. InvalidWeapon,
  646. InvalidWeapon,
  647. InvalidWeapon,
  648. InvalidWeapon,
  649. ProximityMine,
  650. InvalidWeapon,
  651. InvalidWeapon,
  652. InvalidWeapon,
  653. Pistol,
  654. MachineGun,
  655. ShotGun,
  656. InvalidWeapon,
  657. TimedMine,
  658. BouncingBettyMine,
  659. RemoteControlMine,
  660. InvalidWeapon,
  661. InvalidWeapon,
  662. InvalidWeapon,
  663. InvalidWeapon,
  664. Heatseeker,
  665. InvalidWeapon,
  666. Assault,
  667. InvalidWeapon,
  668. InvalidWeapon,
  669. InvalidWeapon,
  670. InvalidWeapon,
  671. InvalidWeapon,
  672. InvalidWeapon,
  673. InvalidWeapon,
  674. InvalidWeapon,
  675. Flamer,
  676. DeathWad,
  677. DoubleBarrel,
  678. Uzi,
  679. AutoRifle,
  680. SmallPistol,
  681. Dynamite,
  682. };
  683. char* CDoofus::ms_apszActionNames[] =
  684. {
  685. "Guard",
  686. "Advance",
  687. "Retreat",
  688. "Engage",
  689. "Popout",
  690. "Run&Shoot",
  691. "Hide",
  692. "Advance-Hold",
  693. "Walk",
  694. "Panic",
  695. "March",
  696. "Madness",
  697. "Help",
  698. };
  699. // Let this auto-init to 0
  700. short CDoofus::ms_sFileCount;
  701. //
  702. //
  703. ////////////////////////////////////////////////////////////////////////////////
  704. // Constructor
  705. ////////////////////////////////////////////////////////////////////////////////
  706. CDoofus::CDoofus(CRealm* pRealm, ClassIDType id)
  707. : CCharacter(pRealm, id)
  708. {
  709. m_sSuspend = 0;
  710. m_pNavNet = NULL;
  711. m_u16NavNetID = 0;
  712. m_idDude = CIdBank::IdNil;
  713. m_pNextBouy = NULL;
  714. m_sNextX = m_sNextZ = 0;
  715. m_ucDestBouyID = m_ucNextBouyID = 0;
  716. m_lAlignTimer = 0;
  717. m_lEvalTimer = 0;
  718. m_lShootTimer = 0;
  719. m_lShotTimeout = 0;
  720. m_pPylonStart = NULL;
  721. m_pPylonEnd = NULL;
  722. m_lCommentTimer = 0;
  723. m_usCommentCounter = 0;
  724. m_lLastHelpCallTime = 0;
  725. m_lSampleTimeIsPlaying = 0;
  726. m_bRecentlyStuck = false;
  727. m_bCivilian = false;
  728. m_ptransExecutionTarget = NULL;
  729. m_spriteWeapon.m_pthing = this;
  730. m_ucSpecialBouy0ID = 0;
  731. m_ucSpecialBouy1ID = 0;
  732. m_bPanic = false;
  733. m_bRegisteredBirth = false;
  734. // Default to no fallback weapon.
  735. m_eFallbackWeaponType = TotalIDs;
  736. m_sStuckCounter = 0;
  737. m_usBloodCounter = 0;
  738. m_siPlaying = 0;
  739. // explicitly initialize to stop Valgrind whining. --ryan.
  740. m_lIdleTimer = pRealm->m_time.GetGameTime() + 2000 + GetRandom() % 2000;
  741. m_lStuckTimeout = 0;
  742. m_u16KillerId = 0;
  743. }
  744. ////////////////////////////////////////////////////////////////////////////////
  745. // Destructor
  746. ////////////////////////////////////////////////////////////////////////////////
  747. CDoofus::~CDoofus()
  748. {
  749. // If we've been born . . .
  750. if (m_bRegisteredBirth == true)
  751. {
  752. // See who killed us, if anyone
  753. bool bPlayerKill = false;
  754. CThing* pthing;
  755. if (m_u16KillerId != 0 && m_pRealm->m_idbank.GetThingByID(&pthing, m_u16KillerId) == 0)
  756. if (pthing->GetClassID() == CDudeID)
  757. bPlayerKill = true; // Dude killed us.
  758. // Good bye.
  759. m_pRealm->RegisterDeath(m_bCivilian, bPlayerKill);
  760. }
  761. }
  762. ////////////////////////////////////////////////////////////////////////////////
  763. // Load object (should call base class version!)
  764. ////////////////////////////////////////////////////////////////////////////////
  765. short CDoofus::Load( // Returns 0 if successfull, non-zero otherwise
  766. RFile* pFile, // In: File to load from
  767. bool bEditMode, // In: True for edit mode, false otherwise
  768. short sFileCount, // In: File count (unique per file, never 0)
  769. ULONG ulFileVersion) // In: Version of file format to load.
  770. {
  771. short sResult = CCharacter::Load(pFile, bEditMode, sFileCount, ulFileVersion);
  772. if (sResult == SUCCESS)
  773. {
  774. // Load common data just once per file (not with each object)
  775. if (ms_sFileCount != sFileCount)
  776. {
  777. ms_sFileCount = sFileCount;
  778. // Load static data
  779. switch (ulFileVersion)
  780. {
  781. default:
  782. case 1:
  783. pFile->Read(&ms_dAccUser);
  784. pFile->Read(&ms_dAccDrag);
  785. pFile->Read(&ms_dMaxVelFore);
  786. pFile->Read(&ms_dMaxVelBack);
  787. pFile->Read(&ms_dDegPerSec);
  788. // Clear the panic flag at the load of a level.
  789. m_bPanic = false;
  790. break;
  791. }
  792. }
  793. // Load object data
  794. switch (ulFileVersion)
  795. {
  796. default:
  797. case 43:
  798. pFile->Read(&m_ucSpecialBouy0ID);
  799. pFile->Read(&m_ucSpecialBouy1ID);
  800. case 42:
  801. case 41:
  802. case 40:
  803. case 39:
  804. case 38:
  805. case 37:
  806. case 36:
  807. case 35:
  808. case 34:
  809. case 33:
  810. case 32:
  811. case 31:
  812. case 30:
  813. case 29:
  814. case 28:
  815. case 27:
  816. case 26:
  817. case 25:
  818. case 24:
  819. case 23:
  820. case 22:
  821. case 21:
  822. case 20:
  823. case 19:
  824. case 18:
  825. case 17:
  826. case 16:
  827. case 15:
  828. case 14:
  829. case 13:
  830. case 12:
  831. case 11:
  832. case 10:
  833. case 9:
  834. case 8:
  835. case 7:
  836. case 6:
  837. case 5:
  838. case 4:
  839. case 3:
  840. case 2:
  841. case 1:
  842. // Get the instance ID for the NavNet
  843. U16 u16Data;
  844. short sres = pFile->Read(&u16Data);
  845. m_u16NavNetID = u16Data;
  846. break;
  847. }
  848. // Make sure there were no file errors
  849. if (!pFile->Error())
  850. {
  851. // Get resources
  852. }
  853. else
  854. {
  855. sResult = -1;
  856. TRACE("CDoofus::Load(): Error reading from file!\n");
  857. }
  858. }
  859. return sResult;
  860. }
  861. ////////////////////////////////////////////////////////////////////////////////
  862. // Save object (should call base class version!)
  863. ////////////////////////////////////////////////////////////////////////////////
  864. short CDoofus::Save( // Returns 0 if successfull, non-zero otherwise
  865. RFile* pFile, // In: File to save to
  866. short sFileCount) // In: File count (unique per file, never 0)
  867. {
  868. // Call the base class save to save the u16InstanceID
  869. short sResult = CCharacter::Save(pFile, sFileCount);
  870. if (sResult == SUCCESS)
  871. {
  872. // Save common data just once per file (not with each object)
  873. if (ms_sFileCount != sFileCount)
  874. {
  875. ms_sFileCount = sFileCount;
  876. // Save static data
  877. pFile->Write(&ms_dAccUser);
  878. pFile->Write(&ms_dAccDrag);
  879. pFile->Write(&ms_dMaxVelFore);
  880. pFile->Write(&ms_dMaxVelBack);
  881. pFile->Write(&ms_dDegPerSec);
  882. }
  883. // Save object data
  884. // Save the instance ID for the parent NavNet so it can be connected
  885. // again after load
  886. pFile->Write(&m_ucSpecialBouy0ID);
  887. pFile->Write(&m_ucSpecialBouy1ID);
  888. U16 u16Data = CIdBank::IdNil; // Safety.
  889. if (m_pNavNet)
  890. u16Data = m_pNavNet->GetInstanceID();
  891. pFile->Write(&u16Data);
  892. sResult = pFile->Error();
  893. }
  894. return sResult;
  895. }
  896. ////////////////////////////////////////////////////////////////////////////////
  897. // Startup object
  898. ////////////////////////////////////////////////////////////////////////////////
  899. short CDoofus::Startup(void) // Returns 0 if successfull, non-zero otherwise
  900. {
  901. CCharacter::Startup();
  902. // If we don't have a pointer to the Nav Net yet, get it from the ID
  903. if (m_pNavNet == NULL)
  904. {
  905. if (m_pRealm->m_idbank.GetThingByID((CThing**) &m_pNavNet, m_u16NavNetID) == 0)
  906. {
  907. // Verify it's a NavNet . . .
  908. if (m_pNavNet->GetClassID() == CNavigationNetID)
  909. {
  910. // Wahoo.
  911. }
  912. else
  913. {
  914. m_pNavNet = NULL;
  915. }
  916. }
  917. // If still no nav net . . .
  918. if (m_pNavNet == NULL)
  919. {
  920. // Message depends on user mode.
  921. if (m_pRealm->m_flags.bEditing)
  922. {
  923. rspMsgBox(
  924. RSP_MB_ICN_STOP | RSP_MB_BUT_OK,
  925. g_pszAppName,
  926. g_pszDoofusCannotFindNavNet_EditMode_hu_hu,
  927. m_u16InstanceId,
  928. m_u16NavNetID);
  929. }
  930. else
  931. {
  932. rspMsgBox(
  933. RSP_MB_ICN_INFO | RSP_MB_BUT_OK,
  934. g_pszAppName,
  935. g_pszDoofusCannotFindNavNet_PlayMode_hu_hu,
  936. m_u16InstanceId,
  937. m_u16NavNetID);
  938. }
  939. // Use the current NavNet.
  940. m_pNavNet = m_pRealm->GetCurrentNavNet();
  941. if (m_pNavNet)
  942. {
  943. // Remember to reset our ID so this doesn't happen again.
  944. m_u16NavNetID = m_pNavNet->GetInstanceID();
  945. }
  946. }
  947. }
  948. // Clear stuck counter
  949. m_sStuckCounter = 0;
  950. // Init other stuff
  951. m_dVel = 0.0;
  952. m_dRot = 0.0;
  953. m_dAnimRot = 0.0;
  954. m_sPrevHeight = (short) m_dY;
  955. m_sRotateDir = 0;
  956. // Set up the detection smash
  957. m_smashDetect.m_pThing = this;
  958. m_smashDetect.m_bits = 0;
  959. m_smashDetect.m_sphere.sphere.lRadius = ms_lDetectionRadius;
  960. // Set up the fire avoidance smash
  961. m_smashAvoid.m_sphere.sphere.X = m_dX + (rspCos(m_dRot) * ms_lAvoidRadius);
  962. m_smashAvoid.m_sphere.sphere.Y = m_dY;
  963. m_smashAvoid.m_sphere.sphere.Z = m_dZ - (rspSin(m_dRot) * ms_lAvoidRadius);
  964. m_smashAvoid.m_sphere.sphere.lRadius = ms_lAvoidRadius;
  965. m_smashAvoid.m_bits = 0;
  966. m_smashAvoid.m_pThing = this;
  967. // Setup weapon sprite.
  968. m_spriteWeapon.m_sInFlags = CSprite::InHidden;
  969. m_sprite.AddChild(&m_spriteWeapon);
  970. // Start in a neutral state
  971. m_state = State_Idle;
  972. // Hello, world.
  973. m_pRealm->RegisterBirth(m_bCivilian);
  974. // Note that we've registered our birth so we know later that we need to
  975. // register our death.
  976. m_bRegisteredBirth = true;
  977. // Set tunable values to their doofus defaults
  978. m_lShootTimeout = ms_lShootTimeoutMin + ((11-m_pRealm->m_flags.sDifficulty) * ms_lShootTimeoutInc);
  979. m_lRunShootInterval = ms_lRunShootInterval;
  980. m_lShotReactionTimeout = ms_lShotTimeout;
  981. return 0;
  982. }
  983. ////////////////////////////////////////////////////////////////////////////////
  984. // Called by editor to init new object at specified position
  985. ////////////////////////////////////////////////////////////////////////////////
  986. short CDoofus::EditNew( // Returns 0 if successfull, non-zero otherwise
  987. short sX, // In: New x coord
  988. short sY, // In: New y coord
  989. short sZ) // In: New z coord
  990. {
  991. short sResult = 0;
  992. CCharacter::EditNew(sX, sY, sZ);
  993. // Since we were just created in the editor, set our nav net to the
  994. // current one for this realm.
  995. m_pNavNet = m_pRealm->GetCurrentNavNet();
  996. m_u16NavNetID = m_pNavNet->GetInstanceID();
  997. return sResult;
  998. }
  999. ////////////////////////////////////////////////////////////////////////////////
  1000. // Render - This override version temporarily replaces the m_dRot with the
  1001. // m_dAnimRot so that it draws the person in the desired facing
  1002. // direction.
  1003. ////////////////////////////////////////////////////////////////////////////////
  1004. void CDoofus::Render(void)
  1005. {
  1006. double dSaveRotation = m_dRot;
  1007. m_dRot = m_dAnimRot;
  1008. CCharacter::Render();
  1009. m_dRot = dSaveRotation;
  1010. CAnim3D* panimWeapon = NULL;
  1011. if (m_panimCur->m_pevent)
  1012. {
  1013. // Get current event.
  1014. U8 u8Event = *( (U8*)(m_panimCur->m_pevent->GetAtTime(m_lAnimTime) ) );
  1015. // If gun not hidden by Randy . . .
  1016. if (u8Event < 10)
  1017. {
  1018. panimWeapon = GetWeaponAnim(m_eWeaponType);
  1019. // If we got an anim . . .
  1020. if (panimWeapon)
  1021. {
  1022. // If this anim is empty (and not intentional) . . .
  1023. if (panimWeapon->m_pmeshes == NULL && m_eWeaponType != TotalIDs)
  1024. {
  1025. // Get fallback weapon, if any.
  1026. panimWeapon = GetWeaponAnim(m_eFallbackWeaponType);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. // If we have a visible weapon . . .
  1032. if (panimWeapon)
  1033. {
  1034. // If we have the necessary components . . .
  1035. if (panimWeapon->m_pmeshes && m_panimCur->m_ptransWeapon)
  1036. {
  1037. // Show weapon sprite.
  1038. m_spriteWeapon.m_sInFlags &= ~CSprite::InHidden;
  1039. m_spriteWeapon.m_pmesh = panimWeapon->m_pmeshes->GetAtTime(m_lAnimTime);
  1040. m_spriteWeapon.m_psop = panimWeapon->m_psops->GetAtTime(m_lAnimTime);
  1041. m_spriteWeapon.m_ptex = panimWeapon->m_ptextures->GetAtTime(m_lAnimTime);
  1042. m_spriteWeapon.m_psphere = panimWeapon->m_pbounds->GetAtTime(m_lAnimTime);
  1043. m_spriteWeapon.m_ptrans = m_panimCur->m_ptransWeapon->GetAtTime(m_lAnimTime);
  1044. }
  1045. else
  1046. {
  1047. // Hide weapon sprite.
  1048. m_spriteWeapon.m_sInFlags |= CSprite::InHidden;
  1049. }
  1050. }
  1051. else
  1052. {
  1053. // Hide weapon sprite.
  1054. m_spriteWeapon.m_sInFlags |= CSprite::InHidden;
  1055. }
  1056. }
  1057. ////////////////////////////////////////////////////////////////////////////////
  1058. // Called by editor to render object
  1059. ////////////////////////////////////////////////////////////////////////////////
  1060. void CDoofus::EditRender(void)
  1061. {
  1062. // In some cases, object's might need to do a special-case render in edit
  1063. // mode because Startup() isn't called. In this case it doesn't matter, so
  1064. // we can call the normal Render().
  1065. Render();
  1066. }
  1067. ////////////////////////////////////////////////////////////////////////////////
  1068. // SelectDude
  1069. ////////////////////////////////////////////////////////////////////////////////
  1070. short CDoofus::SelectDudeBouy(void)
  1071. {
  1072. short sReturn = SUCCESS;
  1073. // CDude* pDude;
  1074. // CBouy* pBouytest;
  1075. if (SelectDude() == SUCCESS)
  1076. {
  1077. CDude* pdude;
  1078. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pdude, m_idDude) == 0)
  1079. {
  1080. m_ucDestBouyID = m_pNavNet->FindNearestBouy(pdude->GetX(), pdude->GetZ());
  1081. }
  1082. }
  1083. else
  1084. sReturn = FAILURE;
  1085. /*
  1086. if (m_pRealm->m_asClassNumThings[CThing::CDudeID] > 0)
  1087. {
  1088. pDude = (CDude*) m_pRealm->m_aclassHeads[CThing::CDudeID].GetNext();
  1089. m_ucDestBouyID = m_pNavNet->FindNearestBouy(pDude->GetX(), pDude->GetZ());
  1090. }
  1091. else
  1092. {
  1093. if (m_pNavNet->GetNumNodes() < 1)
  1094. sReturn = FAILURE;
  1095. else
  1096. {
  1097. pBouytest = NULL;
  1098. while (pBouytest == NULL)
  1099. {
  1100. m_ucDestBouyID = GetRandom() % m_pNavNet->GetNumNodes();
  1101. pBouytest = m_pNavNet->GetBouy(m_ucDestBouyID);
  1102. }
  1103. }
  1104. }
  1105. */
  1106. return sReturn;
  1107. }
  1108. ////////////////////////////////////////////////////////////////////////////////
  1109. // SelectRandomBouy - make sure it exists before setting it
  1110. ////////////////////////////////////////////////////////////////////////////////
  1111. UCHAR CDoofus::SelectRandomBouy(void)
  1112. {
  1113. UCHAR ucSelect = 0;
  1114. CBouy* pBouy = NULL;
  1115. if (m_pNavNet->GetNumNodes() <= 1)
  1116. return 0;
  1117. while (pBouy == NULL)
  1118. {
  1119. ucSelect = GetRandom() % m_pNavNet->GetNumNodes();
  1120. pBouy = m_pNavNet->GetBouy(ucSelect);
  1121. }
  1122. return ucSelect;
  1123. }
  1124. ////////////////////////////////////////////////////////////////////////////////
  1125. // SelectDude - Picks the closest dude from the dude list and assignes it to
  1126. // this enemy's CDude pointer.
  1127. ////////////////////////////////////////////////////////////////////////////////
  1128. short CDoofus::SelectDude(void)
  1129. {
  1130. // Things::iterator i;
  1131. // Things* pDudes;
  1132. m_idDude = CIdBank::IdNil;
  1133. ULONG ulSqrDistance;
  1134. ULONG ulCurSqrDistance = 0xFFFFFFFF;
  1135. ULONG ulDistX;
  1136. ULONG ulDistZ;
  1137. // pDudes = m_pRealm->m_apthings[CThing::CDudeID];
  1138. CDude* pdude;
  1139. CListNode<CThing>* pDudeList = m_pRealm->m_aclassHeads[CThing::CDudeID].m_pnNext;
  1140. while (pDudeList && pDudeList->m_powner)
  1141. {
  1142. pdude = (CDude*) pDudeList->m_powner;
  1143. // If this dude is not dead . . .
  1144. if (pdude->m_state != State_Dead)
  1145. {
  1146. ulDistX = pdude->m_dX - m_dX;
  1147. ulDistZ = pdude->m_dZ - m_dZ;
  1148. ulSqrDistance = ulDistX * ulDistX + ulDistZ * ulDistZ;
  1149. if (ulSqrDistance < ulCurSqrDistance)
  1150. {
  1151. // This one is closer.
  1152. ulCurSqrDistance = ulSqrDistance;
  1153. m_idDude = pdude->GetInstanceID();
  1154. }
  1155. }
  1156. pDudeList = pDudeList->m_pnNext;
  1157. }
  1158. return (m_idDude != CIdBank::IdNil) ? SUCCESS : FAILURE;
  1159. }
  1160. ////////////////////////////////////////////////////////////////////////////////
  1161. // FindDirection - gives the direction toward the cDude
  1162. ////////////////////////////////////////////////////////////////////////////////
  1163. short CDoofus::FindDirection()
  1164. {
  1165. short sAngle;
  1166. double dDudeX;
  1167. double dDudeZ;
  1168. double dX;
  1169. double dZ;
  1170. if (m_idDude == CIdBank::IdNil)
  1171. SelectDude();
  1172. if (m_idDude != CIdBank::IdNil)
  1173. {
  1174. CDude* pdude;
  1175. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pdude, m_idDude) == 0)
  1176. {
  1177. dDudeX = pdude->GetX();
  1178. dDudeZ = pdude->GetZ();
  1179. dX = dDudeX - m_dX;
  1180. dZ = m_dZ - dDudeZ;
  1181. sAngle = rspATan(dZ, dX);
  1182. }
  1183. else
  1184. sAngle = m_dRot;
  1185. }
  1186. else
  1187. sAngle = m_dRot;
  1188. return sAngle;
  1189. }
  1190. ////////////////////////////////////////////////////////////////////////////////
  1191. // Return the square of the distance to the guy (to test for proximity
  1192. ////////////////////////////////////////////////////////////////////////////////
  1193. double CDoofus::SQDistanceToDude()
  1194. {
  1195. double dSquareDistance = 0.0;
  1196. double dX;
  1197. double dZ;
  1198. if (m_idDude == CIdBank::IdNil)
  1199. SelectDude();
  1200. if (m_idDude != CIdBank::IdNil)
  1201. {
  1202. CDude* pdude;
  1203. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pdude, m_idDude) == 0)
  1204. {
  1205. dX = pdude->GetX() - m_dX;
  1206. dZ = pdude->GetZ() - m_dZ;
  1207. dSquareDistance = (dX * dX) + (dZ * dZ);
  1208. }
  1209. }
  1210. return dSquareDistance;
  1211. }
  1212. ////////////////////////////////////////////////////////////////////////////////
  1213. // AlignToBouy - When timer is up, recalculate direction to bouy
  1214. ////////////////////////////////////////////////////////////////////////////////
  1215. void CDoofus::AlignToBouy(void)
  1216. {
  1217. long lThisTime = m_pRealm->m_time.GetGameTime();
  1218. if (lThisTime > m_lAlignTimer || m_bRecentlyStuck)
  1219. {
  1220. // If he got stuck and is now free, pick a new bouy
  1221. if (m_bRecentlyStuck)
  1222. {
  1223. m_bRecentlyStuck = false;
  1224. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  1225. if (m_ucNextBouyID > 0)
  1226. {
  1227. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  1228. if (m_pNextBouy != NULL)
  1229. {
  1230. m_sNextX = m_pNextBouy->GetX();
  1231. m_sNextZ = m_pNextBouy->GetZ();
  1232. }
  1233. }
  1234. }
  1235. m_lAlignTimer = lThisTime + ms_lDefaultAlignTime;
  1236. m_dAnimRot = m_dRot = rspATan(m_dZ - m_sNextZ, m_sNextX - m_dX);
  1237. }
  1238. }
  1239. ////////////////////////////////////////////////////////////////////////////////
  1240. // TryClearDirection - Given an angle, and a variance, checks the given angle
  1241. // to see if it is clear. If it is blocked by walls or fire
  1242. // it will try the max variance in either direction for a
  1243. // clear path and set the angle if it finds one. If these
  1244. // three angles fail, it will return false.
  1245. //
  1246. // Enemy guys can use this to attempt several paths before moving. If it fails
  1247. // then they can fall back on using a random direction, or they could try again
  1248. // with a completely different angle and variance.
  1249. ////////////////////////////////////////////////////////////////////////////////
  1250. bool CDoofus::TryClearDirection(double* pdRot, short sVariance)
  1251. {
  1252. bool bFoundPath = false;
  1253. short sTries = 0;
  1254. short sX, sY, sZ;
  1255. double dRotAttempt = *pdRot;
  1256. CThing* pthing;
  1257. while (!bFoundPath && sTries < 2)
  1258. {
  1259. // Check clear path
  1260. if (IsPathClear( // Returns true, if the entire path is clear.
  1261. // Returns false, if only a portion of the path is clear.
  1262. // (see *psX, *psY, *psZ, *ppthing).
  1263. m_dX, // In: Starting X.
  1264. m_dY, // In: Starting Y.
  1265. m_dZ, // In: Starting Z.
  1266. dRotAttempt, // In: Rotation around y axis (direction on X/Z plane).
  1267. 10, // In: Rate at which to scan ('crawl') path in pixels per
  1268. // iteration.
  1269. // NOTE: We scan terrain using GetFloorAttributes()
  1270. // so small values of sCrawl are not necessary.
  1271. // NOTE: We could change this to a speed in pixels per second
  1272. // where we'd assume a certain frame rate.
  1273. 200, // In: Range on X/Z plane.
  1274. m_smash.m_sphere.sphere.lRadius, // In: Radius of path traverser.
  1275. MaxStepUpThreshold, // In: Max traverser can step up.
  1276. CSmash::Fire, // In: Mask of CSmash bits that would terminate path.
  1277. CSmash::Bad | CSmash::Good, // In: Mask of CSmash bits that would not affect path.
  1278. CSmash::Dead, // In: Mask of CSmash bits that cannot affect path.
  1279. &sX, // Out: Point of intercept, if any, on path.
  1280. &sY, // Out: Point of intercept, if any, on path.
  1281. &sZ, // Out: Point of intercept, if any, on path.
  1282. &pthing, // Out: Thing that intercepted us or NULL, if none.
  1283. &m_smash) // In: Optional CSmash to exclude or NULL, if none.
  1284. == false)
  1285. {
  1286. switch (sTries)
  1287. {
  1288. case 0:
  1289. dRotAttempt = rspMod360(dRotAttempt + (sVariance/2));
  1290. break;
  1291. case 1:
  1292. dRotAttempt = rspMod360(dRotAttempt - sVariance);
  1293. break;
  1294. }
  1295. }
  1296. else
  1297. {
  1298. bFoundPath = true;
  1299. *pdRot = dRotAttempt;
  1300. }
  1301. sTries++;
  1302. }
  1303. return bFoundPath;
  1304. }
  1305. ////////////////////////////////////////////////////////////////////////////////
  1306. // TryClearShot - Works like the TryClearDirection, but doesn't check for
  1307. // fire, only walls. So it knows if it could hit the target
  1308. // from here.
  1309. ////////////////////////////////////////////////////////////////////////////////
  1310. bool CDoofus::TryClearShot(double dRot, short sVariance)
  1311. {
  1312. bool bFoundPath = false;
  1313. // If we have a weapon transform . . .
  1314. if (m_panimCur->m_ptransRigid)
  1315. {
  1316. short sTries = 0;
  1317. short sX, sY, sZ;
  1318. CThing* pthing = NULL;
  1319. double dRotAttempt = dRot;
  1320. // Do a translation to the weapon position so that it is at the correct
  1321. // height for checking
  1322. double dMuzzleX, dMuzzleY, dMuzzleZ;
  1323. GetLinkPoint( // Returns nothing.
  1324. m_panimCur->m_ptransRigid->GetAtTime(m_lAnimTime), // In: Transform specifying point.
  1325. &dMuzzleX, // Out: Point speicfied.
  1326. &dMuzzleY, // Out: Point speicfied.
  1327. &dMuzzleZ); // Out: Point speicfied. // Update execution point via link point.
  1328. while (!bFoundPath && sTries < 2)
  1329. {
  1330. // Check clear path
  1331. /*
  1332. // This one checks terrain and for people in the way
  1333. if (IsPathClear(
  1334. (short) (m_dX + dMuzzleX), // Start x position
  1335. (short) (m_dY + dMuzzleY), // Start y position
  1336. (short) (m_dZ + dMuzzleZ), // Start z position
  1337. (short) dRotAttempt, // Angle of shot
  1338. 4, // Crawl rate
  1339. rspSqrt(CDoofus::SQDistanceToDude()),
  1340. 2, // radius of traverser
  1341. 2, // vertical tolerance
  1342. CSmash::Bad, // Bits that would terminate path
  1343. 0, // Bits that would not affect path
  1344. 0, // Bits that cannot affect path
  1345. &sX, // pointer to terminating point
  1346. &sY, // pointer to terminating point
  1347. &sZ, // pointer to terminating point
  1348. &pthing, // handle to terminating target
  1349. &m_smash) // exclude your own smash
  1350. == false)
  1351. */
  1352. // This one only checks terrain
  1353. if (m_pRealm->IsPathClear(
  1354. m_dX + dMuzzleX,
  1355. m_dY + dMuzzleY,
  1356. m_dZ + dMuzzleZ,
  1357. (short) dRotAttempt,
  1358. 3,
  1359. rspSqrt(CDoofus::SQDistanceToDude()),
  1360. 0,
  1361. &sX,
  1362. &sY,
  1363. &sZ)
  1364. == false)
  1365. {
  1366. switch (sTries)
  1367. {
  1368. case 0:
  1369. dRotAttempt = rspMod360(dRotAttempt + (sVariance/2));
  1370. break;
  1371. case 1:
  1372. dRotAttempt = rspMod360(dRotAttempt - sVariance);
  1373. break;
  1374. }
  1375. }
  1376. else
  1377. {
  1378. bFoundPath = true;
  1379. }
  1380. sTries++;
  1381. }
  1382. }
  1383. return bFoundPath;
  1384. }
  1385. ////////////////////////////////////////////////////////////////////////////////
  1386. // Update - default implementation of logic for enemy characters
  1387. ////////////////////////////////////////////////////////////////////////////////
  1388. void CDoofus::Update(void)
  1389. {
  1390. if (!m_sSuspend)
  1391. {
  1392. long lThisTime = m_pRealm->m_time.GetGameTime();
  1393. // Check for new messages that may change the state
  1394. ProcessMessages();
  1395. // If he has no network, then he should stick to Guard mode
  1396. // if (m_pNavNet == NULL)
  1397. // m_state = State_Guard;
  1398. // See if there are any pylons nearby that he wants to use.
  1399. if (m_state == State_Idle ||
  1400. m_state == State_Wait ||
  1401. m_state == State_Hunt)
  1402. Logic_PylonDetect();
  1403. switch (m_state)
  1404. {
  1405. case State_Guard:
  1406. Logic_Guard();
  1407. break;
  1408. case State_PopBegin:
  1409. Logic_PopBegin();
  1410. break;
  1411. case State_PopWait:
  1412. Logic_PopWait();
  1413. break;
  1414. case State_Popout:
  1415. Logic_Popout();
  1416. break;
  1417. case State_RunShootBegin:
  1418. Logic_RunShootBegin();
  1419. break;
  1420. case State_RunShoot:
  1421. Logic_RunShoot();
  1422. break;
  1423. case State_RunShootWait:
  1424. Logic_RunShootWait();
  1425. break;
  1426. case State_Shoot:
  1427. Logic_Shoot();
  1428. break;
  1429. case State_Hunt:
  1430. Logic_Hunt();
  1431. break;
  1432. case State_HuntNext:
  1433. Logic_MoveNext();
  1434. break;
  1435. case State_Shot:
  1436. Logic_Shot();
  1437. break;
  1438. case State_BlownUp:
  1439. Logic_BlownUp();
  1440. break;
  1441. case State_Burning:
  1442. Logic_Burning();
  1443. break;
  1444. case State_Die:
  1445. Logic_Die();
  1446. break;
  1447. case State_Dead:
  1448. CCharacter::OnDead();
  1449. delete this;
  1450. return;
  1451. break;
  1452. }
  1453. // Determine appropriate position for main smash.
  1454. PositionSmash();
  1455. // Update the smash.
  1456. m_pRealm->m_smashatorium.Update(&m_smash);
  1457. m_lPrevTime = lThisTime;
  1458. // Call base class //////////////////////////////////////////////////////
  1459. CCharacter::Update();
  1460. }
  1461. }
  1462. ////////////////////////////////////////////////////////////////////////////////
  1463. // Logic_Shot - You got shot so check your damage
  1464. ////////////////////////////////////////////////////////////////////////////////
  1465. void CDoofus::Logic_Shot(void)
  1466. {
  1467. if (m_stockpile.m_sHitPoints > NEAR_DEATH_HITPOINTS)
  1468. // Restore previous state & go back to what you were doing
  1469. {
  1470. // If your shot animation is over...
  1471. if (m_lAnimTime > m_panimCur->m_psops->TotalTime())
  1472. {
  1473. // Add this check for low health -> retreat
  1474. // if (m_stockpile.m_sHitPoints < DefHitPoints / 2)
  1475. //{
  1476. // m_state = State_Retreat;
  1477. // m_panimCur = &m_animRun;
  1478. // m_lAnimTime = 0;
  1479. // m_dRot = rspMod360(CDoofus::FindDirection() + 180);
  1480. // if (CDoofus::TryClearDirection(&m_dRot, 90) == false)
  1481. // m_dRot = rspMod360(GetRandom());
  1482. //}
  1483. //
  1484. // else // go back to previous state
  1485. m_state = m_ePreviousState;
  1486. m_panimCur = m_panimPrev;
  1487. m_lAnimTime = 0;
  1488. }
  1489. else
  1490. // Else continue shot animation
  1491. {
  1492. m_lAnimTime += m_pRealm->m_time.GetGameTime() - m_lPrevTime;
  1493. }
  1494. Cheater();
  1495. }
  1496. else
  1497. // else you are dead or writhing so die
  1498. {
  1499. m_state = State_Die;
  1500. m_panimCur = &m_animDie;
  1501. m_lAnimTime = 0;
  1502. }
  1503. }
  1504. ////////////////////////////////////////////////////////////////////////////////
  1505. // Logic_BlownUp - You were blown up so pop up into the air and come down again
  1506. ////////////////////////////////////////////////////////////////////////////////
  1507. void CDoofus::Logic_BlownUp(void)
  1508. {
  1509. // Make him animate
  1510. m_lAnimTime += (m_pRealm->m_time.GetGameTime() - m_lPrevTime);
  1511. if (!WhileBlownUp())
  1512. m_state = State_Dead;
  1513. else
  1514. UpdateFirePosition();
  1515. Cheater();
  1516. }
  1517. ////////////////////////////////////////////////////////////////////////////////
  1518. // Logic_Burning - You are on fire, so run aroudn burning until you are dead
  1519. ////////////////////////////////////////////////////////////////////////////////
  1520. void CDoofus::Logic_Burning(void)
  1521. {
  1522. m_lAnimTime += m_pRealm->m_time.GetGameTime() - m_lPrevTime;
  1523. if (!CCharacter::WhileBurning())
  1524. {
  1525. m_state = State_Die;
  1526. m_lAnimTime = 0;
  1527. m_panimCur = &m_animDie;
  1528. }
  1529. Cheater();
  1530. }
  1531. ////////////////////////////////////////////////////////////////////////////////
  1532. // Logic_Die - You are about to die, so look like it!
  1533. ////////////////////////////////////////////////////////////////////////////////
  1534. void CDoofus::Logic_Die(void)
  1535. {
  1536. // Change our smashatorium bits
  1537. // enable this if you want them to always writhe when dead - for testing
  1538. #if 0
  1539. if ((m_smash.m_bits & CSmash::AlmostDead) == 0)
  1540. {
  1541. m_smash.m_bits |= CSmash::AlmostDead;
  1542. m_stockpile.m_sHitPoints = 30;
  1543. }
  1544. #else
  1545. m_smash.m_bits |= CSmash::AlmostDead;
  1546. #endif
  1547. // Send to back.
  1548. m_sLayerOverride = CRealm::LayerSprite1;
  1549. // When the die animation is finsihed, so are you.
  1550. if (m_lAnimTime > m_panimCur->m_psops->TotalTime())
  1551. {
  1552. if (m_stockpile.m_sHitPoints > 0)
  1553. {
  1554. m_state = State_Writhing;
  1555. m_panimCur = &m_animWrithing;
  1556. m_lAnimTime = 0;
  1557. GameMessage msg;
  1558. msg.msg_Writhing.eType = typeWrithing;
  1559. msg.msg_Writhing.sPriority = 0;
  1560. CThing* pDemon = m_pRealm->m_aclassHeads[CThing::CDemonID].GetNext();
  1561. if (pDemon)
  1562. SendThingMessage(&msg, pDemon);
  1563. }
  1564. else
  1565. {
  1566. m_state = State_Dead;
  1567. }
  1568. }
  1569. else
  1570. {
  1571. CWeapon* pweapon;
  1572. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  1573. {
  1574. // It should drop like a rock if its a throwing weapon or just delete it if it
  1575. // is a launched weapon
  1576. if (m_eWeaponType == CHeatseekerID || m_eWeaponType == CRocketID || m_eWeaponType == CNapalmID)
  1577. {
  1578. GameMessage msg;
  1579. msg.msg_ObjectDelete.eType = typeObjectDelete;
  1580. msg.msg_ObjectDelete.sPriority = 0;
  1581. SendThingMessage(&msg, pweapon);
  1582. }
  1583. else
  1584. {
  1585. pweapon->m_dHorizVel = (GetRandom() % (short) CGrenade::ms_dThrowHorizVel);
  1586. m_dShootAngle = pweapon->m_dRot = GetRandom() % 360;
  1587. ShootWeapon();
  1588. }
  1589. }
  1590. m_lAnimTime += m_pRealm->m_time.GetGameTime() - m_lPrevTime;
  1591. #if 0 // causes people to spin when they die. --ryan.
  1592. // Rotate him so that he doesn't fall the same way
  1593. // Don't rotate again if he was writhing and has been executed
  1594. if (m_panimCur != &m_animExecuted)
  1595. {
  1596. short sRot = GetRandom() % 2;
  1597. if (sRot & 0x01)
  1598. sRot++;
  1599. if (((short) m_dRot) & 0x01)
  1600. m_dAnimRot = m_dRot = rspMod360(m_dRot + sRot);
  1601. else
  1602. m_dAnimRot = m_dRot = rspMod360(m_dRot - sRot);
  1603. }
  1604. #endif
  1605. }
  1606. }
  1607. ////////////////////////////////////////////////////////////////////////////////
  1608. // Logic_Writhing - Rolling around on the ground in pain, waiting to be
  1609. // executed or until the pain is no longer tolerable.
  1610. ////////////////////////////////////////////////////////////////////////////////
  1611. void CDoofus::Logic_Writhing(void)
  1612. {
  1613. long lThisTime = m_pRealm->m_time.GetGameTime();
  1614. long lTimeDifference = lThisTime - m_lPrevTime;
  1615. // Send to back.
  1616. m_sLayerOverride = CRealm::LayerSprite1;
  1617. if (m_lAnimTime > m_panimCur->m_psops->TotalTime())
  1618. {
  1619. if (--m_stockpile.m_sHitPoints <= 0)
  1620. {
  1621. // Die. - Run the executed anim first to transition between the
  1622. // writhing animation and being dead, otherwise some characters will
  1623. // die on their knees.
  1624. m_state = State_Die;
  1625. m_panimCur = &m_animExecuted;
  1626. m_lAnimTime = 0;
  1627. // m_state = State_Dead;
  1628. }
  1629. else
  1630. {
  1631. // Whoa! MOD EQUALS is deep, man!
  1632. m_lAnimTime %= m_panimCur->m_psops->TotalTime();
  1633. // Now you can tune the amount of blood if it gets too thick.
  1634. if ((++m_usBloodCounter % 2) == 0)
  1635. MakeBloodPool();
  1636. if (lThisTime >= m_lTimer)
  1637. {
  1638. // Launch a sample and get the duration of the sample.
  1639. long lSampleDuration;
  1640. PlaySoundWrithing(&lSampleDuration);
  1641. m_lTimer = lThisTime + lSampleDuration + MS_BETWEEN_SAMPLES;
  1642. }
  1643. }
  1644. }
  1645. else
  1646. {
  1647. CWeapon* pweapon;
  1648. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  1649. {
  1650. // It should drop like a rock if its a throwing weapon or just delete it if it
  1651. // is a launched weapon
  1652. if (m_eWeaponType == CHeatseekerID || m_eWeaponType == CRocketID || m_eWeaponType == CNapalmID)
  1653. {
  1654. GameMessage msg;
  1655. msg.msg_ObjectDelete.eType = typeObjectDelete;
  1656. msg.msg_ObjectDelete.sPriority = 0;
  1657. SendThingMessage(&msg, pweapon);
  1658. }
  1659. else
  1660. {
  1661. pweapon->m_dHorizVel = (GetRandom() % (short) CGrenade::ms_dThrowHorizVel);
  1662. m_dShootAngle = pweapon->m_dRot = GetRandom() % 360;
  1663. ShootWeapon();
  1664. }
  1665. }
  1666. m_lAnimTime += lTimeDifference;
  1667. }
  1668. }
  1669. ////////////////////////////////////////////////////////////////////////////////
  1670. // Logic_Guard - Guard the area until a CDude is nearby
  1671. ////////////////////////////////////////////////////////////////////////////////
  1672. void CDoofus::Logic_Guard(void)
  1673. {
  1674. long lThisTime;
  1675. long lTimeDifference;
  1676. double dSeconds;
  1677. // Get new time
  1678. lThisTime = m_pRealm->m_time.GetGameTime();
  1679. lTimeDifference = lThisTime - m_lPrevTime;
  1680. m_eCurrentAction = Action_Guard;
  1681. // Calculate the elapsed time in seconds
  1682. dSeconds = (double)(lThisTime - m_lPrevTime) / 1000.0;
  1683. // if (m_panimCur != &m_animStand)
  1684. // {
  1685. // m_panimCur = &m_animStand;
  1686. // m_lAnimTime = 0;
  1687. // }
  1688. m_dRot = m_dAnimRot = m_dShootAngle = FindDirection();
  1689. RunIdleAnimation();
  1690. // We assume at first that nothing much is going on in the area, so he
  1691. // only evaluates CDude position every once in a while to cut down on
  1692. // the processing.
  1693. if (lThisTime > m_lTimer)
  1694. {
  1695. m_eCurrentAction = Action_Guard;
  1696. m_lTimer = lThisTime + m_lGuardTimeout;
  1697. if (SelectDude() == SUCCESS && SQDistanceToDude() < ms_dGuardDistance)
  1698. {
  1699. if (CDoofus::TryClearShot(m_dRot, 20) == true)
  1700. {
  1701. CWeapon* pweapon = PrepareWeapon();
  1702. if (pweapon != NULL)
  1703. {
  1704. // Keep it hidden, for now.
  1705. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  1706. pweapon->SetRangeToTarget(rspSqrt(SQDistanceToDude()));
  1707. }
  1708. m_panimCur = &m_animShoot;
  1709. m_lAnimTime = 0;
  1710. SelectDude();
  1711. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  1712. m_state = State_Shoot;
  1713. m_eNextState = State_Guard;
  1714. m_lTimer = lThisTime + m_lShootTimeout;
  1715. }
  1716. else
  1717. {
  1718. m_lTimer = lThisTime + m_lShootTimeout;
  1719. }
  1720. }
  1721. }
  1722. else
  1723. ReevaluateState();
  1724. }
  1725. ////////////////////////////////////////////////////////////////////////////////
  1726. // Logic_Patrol - Keep an eye on the CDude and look like you're doing something
  1727. ////////////////////////////////////////////////////////////////////////////////
  1728. void CDoofus::Logic_Patrol(void)
  1729. {
  1730. // Turn to face dude but do not attack yet
  1731. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  1732. }
  1733. ////////////////////////////////////////////////////////////////////////////////
  1734. // Logic_Hunt - Find a CDude, use the bouy network to get to him wherever he is
  1735. ////////////////////////////////////////////////////////////////////////////////
  1736. void CDoofus::Logic_Hunt(void)
  1737. {
  1738. // Find the destination bouy (one closest to the Dude)
  1739. m_eDestinationState = State_HuntHold;
  1740. SelectDudeBouy();
  1741. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  1742. if (m_ucNextBouyID > 0)
  1743. {
  1744. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  1745. if (m_pNextBouy != NULL)
  1746. {
  1747. m_sNextX = m_pNextBouy->GetX();
  1748. m_sNextZ = m_pNextBouy->GetZ();
  1749. m_state = State_HuntNext;
  1750. m_lTimer = m_pRealm->m_time.GetGameTime() + ms_lReseekTime;
  1751. }
  1752. else
  1753. {
  1754. // Couldn't get bouy position, so stay put
  1755. m_state = State_Guard;
  1756. m_eCurrentAction = Action_Guard;
  1757. }
  1758. }
  1759. else
  1760. {
  1761. // Couldn't find a bouy so stay put
  1762. m_state = State_Guard;
  1763. m_eCurrentAction = Action_Guard;
  1764. }
  1765. }
  1766. ////////////////////////////////////////////////////////////////////////////////
  1767. // Logic_HuntHold - This state means that you were trying to advance to the
  1768. // Dude and have got to the bouy closest to the Dude. The
  1769. // Logic table can either break you out into Engage or
  1770. // whatever, or you will stay in this state where you will
  1771. // be in a guard mode, but always looking to see if you should
  1772. // move to a closer bouy once the Dude changes his position.
  1773. ////////////////////////////////////////////////////////////////////////////////
  1774. void CDoofus::Logic_HuntHold(void)
  1775. {
  1776. // Evaluate the logic table suggestion to see if you should change states
  1777. m_eCurrentAction = Action_AdvanceHold;
  1778. if (!ReevaluateState())
  1779. {
  1780. // Check to see if the dude has moved and you should move
  1781. long lThisTime = m_pRealm->m_time.GetGameTime();
  1782. if (lThisTime > m_lTimer)
  1783. {
  1784. m_lTimer = lThisTime + 1000;
  1785. SelectDudeBouy();
  1786. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  1787. // See if he should move closer.
  1788. if (m_ucNextBouyID != m_ucDestBouyID)
  1789. {
  1790. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  1791. if (m_pNextBouy != NULL)
  1792. {
  1793. m_sNextX = m_pNextBouy->GetX();
  1794. m_sNextZ = m_pNextBouy->GetZ();
  1795. m_lTimer = lThisTime + ms_lReseekTime;
  1796. m_state = State_HuntNext;
  1797. m_eCurrentAction = Action_Advance;
  1798. }
  1799. }
  1800. // Else see if he is close enough to take a shot.
  1801. else
  1802. {
  1803. if (SelectDude() == SUCCESS && SQDistanceToDude() < ms_dGuardDistance)
  1804. {
  1805. CWeapon* pweapon = PrepareWeapon();
  1806. if (pweapon != NULL)
  1807. {
  1808. // Keep it hidden, for now.
  1809. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  1810. pweapon->SetRangeToTarget(rspSqrt(SQDistanceToDude()));
  1811. }
  1812. m_panimCur = &m_animShoot;
  1813. m_lAnimTime = 0;
  1814. SelectDude();
  1815. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  1816. m_state = State_Shoot;
  1817. m_eNextState = State_HuntHold;
  1818. m_lTimer = lThisTime + m_lGuardTimeout;
  1819. }
  1820. }
  1821. }
  1822. // Check to see if the dude is nearby and you should shoot at him.
  1823. }
  1824. }
  1825. ////////////////////////////////////////////////////////////////////////////////
  1826. // Logic_HuntNext - going to next bouy in network en route to final destination
  1827. ////////////////////////////////////////////////////////////////////////////////
  1828. void CDoofus::Logic_MoveNext(void)
  1829. {
  1830. long lThisTime = m_pRealm->m_time.GetGameTime();
  1831. long lElapsedTime = lThisTime - m_lPrevTime;
  1832. double dSeconds = lElapsedTime / 1000.0;
  1833. double dStartX = m_dX;
  1834. double dStartZ = m_dZ;
  1835. if (!ReevaluateState())
  1836. {
  1837. // Make sure its using the correct animation
  1838. if (m_state == State_MarchNext || m_state == State_WalkNext)
  1839. {
  1840. if (m_panimCur != &m_animWalk)
  1841. {
  1842. m_panimCur = &m_animWalk;
  1843. m_lAnimTime = 0;
  1844. }
  1845. else
  1846. m_lAnimTime += lElapsedTime;
  1847. // Cheat and make him walk slower by continuously
  1848. // resetting the velocity.
  1849. m_dVel = ms_dMarchVelocity;
  1850. }
  1851. // Normally use run
  1852. else
  1853. {
  1854. if (m_panimCur != &m_animRun)
  1855. {
  1856. m_panimCur = &m_animRun;
  1857. m_lAnimTime = 0;
  1858. }
  1859. else
  1860. m_lAnimTime += lElapsedTime;
  1861. }
  1862. AlignToBouy();
  1863. m_dAcc = ms_dAccUser;
  1864. DeluxeUpdatePosVel(dSeconds);
  1865. // If not moving when you are trying to, rotate
  1866. if (m_dX == dStartX && m_dZ == dStartZ)
  1867. {
  1868. if (m_sRotateDir)
  1869. m_dAnimRot = m_dRot = rspMod360(m_dRot + 20);
  1870. else
  1871. m_dAnimRot = m_dRot = rspMod360(m_dRot - 20);
  1872. // Reset alignment timer so he doesn't reseek the bouy direction immediately
  1873. m_lAlignTimer = lThisTime + ms_lDefaultAlignTime;
  1874. // Set a flag indicating that you were stuck, so we know what bouy to look
  1875. // for once you get free
  1876. m_bRecentlyStuck = true;
  1877. }
  1878. else
  1879. {
  1880. m_sRotateDir = GetRandom() % 2;
  1881. }
  1882. // See if we are at the next bouy yet
  1883. double dX = m_dX - (double) m_sNextX;
  1884. double dZ = m_dZ - (double) m_sNextZ;
  1885. double dsq = (dX * dX) + (dZ * dZ);
  1886. if (dsq < 5*5) // Was 10*10 for a long time, trying smaller to see if it keeps guys from getting stuck
  1887. {
  1888. UCHAR ucNext = m_pNextBouy->NextRouteNode(m_ucDestBouyID);
  1889. if (ucNext == 0 || ucNext == 255) // you are here or you are lost
  1890. {
  1891. m_state = m_eDestinationState;
  1892. switch (m_state)
  1893. {
  1894. case State_Engage:
  1895. m_eCurrentAction = Action_Engage;
  1896. break;
  1897. case State_Guard:
  1898. m_eCurrentAction = Action_Guard;
  1899. break;
  1900. case State_March:
  1901. m_eCurrentAction = Action_March;
  1902. break;
  1903. case State_Walk:
  1904. m_eCurrentAction = Action_Walk;
  1905. break;
  1906. }
  1907. }
  1908. else
  1909. {
  1910. // If the reseek timer has expired, find the Dude bouy
  1911. // again since he may have moved
  1912. if (lThisTime > m_lTimer || ucNext == 255)
  1913. {
  1914. if (m_state == State_HuntNext)
  1915. m_state = State_Hunt;
  1916. else
  1917. m_lTimer = lThisTime + ms_lReseekTime;
  1918. }
  1919. else
  1920. {
  1921. m_ucNextBouyID = ucNext;
  1922. m_pNextBouy = m_pNavNet->GetBouy(ucNext);
  1923. m_sNextX = m_pNextBouy->GetX();
  1924. m_sNextZ = m_pNextBouy->GetZ();
  1925. }
  1926. }
  1927. }
  1928. }
  1929. // Check for fire in your path so you don't keep moving
  1930. AvoidFire();
  1931. }
  1932. ////////////////////////////////////////////////////////////////////////////////
  1933. // Logic_PositionSet - Get yourself set up to get into range for shooting
  1934. ////////////////////////////////////////////////////////////////////////////////
  1935. void CDoofus::Logic_PositionSet(void)
  1936. {
  1937. if (!ReevaluateState())
  1938. {
  1939. long lThisTime = m_pRealm->m_time.GetGameTime();
  1940. short sVarRot = GetRandom() % 40;
  1941. double dTargetDist = SQDistanceToDude();
  1942. double dTestAngle;
  1943. double dAngleTurn;
  1944. // If he is out of range now, reset desired range to the middle value.
  1945. if (dTargetDist < ms_dMinFightDistanceSQ || dTargetDist > ms_dMaxFightDistanceSQ)
  1946. dTargetDist = ms_dMedFightDistanceSQ;
  1947. bool bFoundDirection = false;
  1948. short sAttempts = 0;
  1949. while (!bFoundDirection && sAttempts < 8)
  1950. {
  1951. switch (sAttempts)
  1952. {
  1953. case 0:
  1954. if (sVarRot & 0x01)
  1955. dTestAngle = rspMod360(m_dRot + 40 + sVarRot);
  1956. else
  1957. dTestAngle = rspMod360(m_dRot - 40 - sVarRot);
  1958. dAngleTurn = 40 + sVarRot;
  1959. break;
  1960. case 1:
  1961. if (sVarRot & 0x01)
  1962. dTestAngle = rspMod360(m_dRot - 40 - sVarRot);
  1963. else
  1964. dTestAngle = rspMod360(m_dRot + 40 + sVarRot);
  1965. // Same dAngleTurn as last time
  1966. break;
  1967. case 2:
  1968. if (sVarRot & 0x01)
  1969. dTestAngle = rspMod360(m_dRot + 20 + sVarRot);
  1970. else
  1971. dTestAngle = rspMod360(m_dRot - 20 - sVarRot);
  1972. dAngleTurn = 20 + sVarRot;
  1973. break;
  1974. case 3:
  1975. if (sVarRot & 0x01)
  1976. dTestAngle = rspMod360(m_dRot - 20 - sVarRot);
  1977. else
  1978. dTestAngle = rspMod360(m_dRot + 20 + sVarRot);
  1979. // Same dAngleTurn as last time
  1980. break;
  1981. case 4:
  1982. dTestAngle = rspMod360(m_dRot + 95);
  1983. break;
  1984. case 5:
  1985. dTestAngle = rspMod360(m_dRot - 95);
  1986. break;
  1987. case 6:
  1988. dTestAngle = m_dRot;
  1989. break;
  1990. case 7:
  1991. dTestAngle = rspMod360(m_dRot + 180);
  1992. break;
  1993. }
  1994. if (CDoofus::TryClearDirection(&dTestAngle, 30) == true)
  1995. {
  1996. bFoundDirection = true;
  1997. if (sAttempts < 4)
  1998. {
  1999. m_sDistRemaining = 2 * 3.1415 * rspSqrt(dTargetDist) * ((180.0-(2.0*(dAngleTurn)))/360.0);
  2000. m_lTimer = lThisTime + (1000 * (m_sDistRemaining / ms_dMaxVelFore));
  2001. }
  2002. else
  2003. {
  2004. m_lTimer = 800 + GetRandom() % 400;
  2005. }
  2006. }
  2007. else
  2008. {
  2009. sAttempts++;
  2010. }
  2011. }
  2012. // If you found a clear direction, take it
  2013. if (bFoundDirection)
  2014. {
  2015. m_dAnimRot = m_dRot = dTestAngle;
  2016. // Set new animation
  2017. m_dAcc = ms_dAccUser;
  2018. m_state = State_PositionMove;
  2019. m_panimCur = &m_animRun;
  2020. m_lAnimTime = 0;
  2021. }
  2022. else
  2023. // else stay where you are.
  2024. {
  2025. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  2026. if (m_idDude != CIdBank::IdNil)
  2027. {
  2028. m_state = State_DelayShoot;
  2029. m_lTimer = lThisTime + ms_lDelayShootTimeout;
  2030. m_eNextState = State_PositionSet;
  2031. m_sStuckCounter++;
  2032. m_lAnimTime = 0;
  2033. if (m_sStuckCounter > ms_sStuckLimit)
  2034. {
  2035. m_sStuckCounter = 0;
  2036. m_lStuckTimeout = lThisTime + ms_lStuckRecoveryTime;
  2037. ReevaluateState();
  2038. }
  2039. }
  2040. }
  2041. }
  2042. }
  2043. ////////////////////////////////////////////////////////////////////////////////
  2044. // Logic_DelayShoot - wait for timer to expire then shoot if clear
  2045. ////////////////////////////////////////////////////////////////////////////////
  2046. void CDoofus::Logic_DelayShoot(void)
  2047. {
  2048. long lThisTime = m_pRealm->m_time.GetGameTime();
  2049. // See if its time to shoot yet
  2050. if (lThisTime > m_lTimer)
  2051. {
  2052. if (TryClearShot(m_dRot, 20) == true && SelectDude() == SUCCESS)
  2053. {
  2054. CWeapon* pweapon = PrepareWeapon();
  2055. if (pweapon != NULL)
  2056. {
  2057. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  2058. pweapon->SetRangeToTarget(rspSqrt(SQDistanceToDude()));
  2059. }
  2060. m_panimCur = &m_animShoot;
  2061. m_lAnimTime = 0;
  2062. m_state = State_Shoot;
  2063. }
  2064. else
  2065. // Skip the shot and go back to the correct state
  2066. {
  2067. m_state = m_eNextState;
  2068. }
  2069. }
  2070. }
  2071. ////////////////////////////////////////////////////////////////////////////////
  2072. // Logic_PositionMove - Move into position then shoot
  2073. ////////////////////////////////////////////////////////////////////////////////
  2074. void CDoofus::Logic_PositionMove(void)
  2075. {
  2076. long lThisTime = m_pRealm->m_time.GetGameTime();
  2077. long lTimeDifference = lThisTime - m_lPrevTime;
  2078. double dSeconds = lTimeDifference / 1000.0;
  2079. if (lThisTime > m_lTimer)
  2080. {
  2081. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  2082. // Check for clear shot before shooting. If not clear, go back to moving
  2083. if (CDoofus::TryClearShot(m_dRot, 20) == true && SelectDude() == SUCCESS)
  2084. {
  2085. CWeapon* pweapon = PrepareWeapon();
  2086. if (pweapon != NULL)
  2087. {
  2088. // Keep it hidden, for now.
  2089. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  2090. pweapon->SetRangeToTarget(rspSqrt(SQDistanceToDude()));
  2091. }
  2092. m_state = State_Shoot;
  2093. m_eNextState = State_PositionSet;
  2094. m_panimCur = &m_animShoot;
  2095. m_lAnimTime = 0;
  2096. }
  2097. // If path is blocked, go on the move again and don't shoot
  2098. else
  2099. {
  2100. m_state = State_PositionSet;
  2101. }
  2102. }
  2103. else
  2104. {
  2105. double dLastPosX = m_dX;
  2106. double dLastPosZ = m_dZ;
  2107. DeluxeUpdatePosVel(dSeconds);
  2108. m_lAnimTime += lTimeDifference;
  2109. // If not moving when intending to, rotate one way or the other
  2110. // to try to avoid the obstacle
  2111. if (m_dVel != 0.0 && dLastPosX == m_dX && dLastPosZ == m_dZ)
  2112. {
  2113. if (((short) m_dRot) & 0x01)
  2114. m_dAnimRot = m_dRot = rspMod360(m_dRot + 20);
  2115. else
  2116. m_dAnimRot = m_dRot = rspMod360(m_dRot - 20);
  2117. }
  2118. // Avoid fire in your path
  2119. AvoidFire();
  2120. }
  2121. }
  2122. ////////////////////////////////////////////////////////////////////////////////
  2123. // Logic_Engage - engage the Dude and start fighting at close range
  2124. ////////////////////////////////////////////////////////////////////////////////
  2125. void CDoofus::Logic_Engage(void)
  2126. {
  2127. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  2128. m_state = State_PositionSet;
  2129. m_sStuckCounter = 0;
  2130. }
  2131. ////////////////////////////////////////////////////////////////////////////////
  2132. // Logic_BeSafe - Stay behind the SafeSpot bouy if the Dude is facing you
  2133. ////////////////////////////////////////////////////////////////////////////////
  2134. void CDoofus::Logic_BeSafe(void)
  2135. {
  2136. }
  2137. ////////////////////////////////////////////////////////////////////////////////
  2138. // Logic_Firefight - Shoot and move
  2139. ////////////////////////////////////////////////////////////////////////////////
  2140. void CDoofus::Logic_Firefight(void)
  2141. {
  2142. }
  2143. ////////////////////////////////////////////////////////////////////////////////
  2144. // Logic_PylonDetect - If you detect that a pylon is near, find out what kind
  2145. // it is and decide if you will use it based on the
  2146. // suggested action. This function is only called when
  2147. // the suggested action requires the use of a pylon.
  2148. ////////////////////////////////////////////////////////////////////////////////
  2149. void CDoofus::Logic_PylonDetect(void)
  2150. {
  2151. CSmash* pSmashPylon;
  2152. CPylon* pPylon = NULL;
  2153. double dSmashedX = 0.0;
  2154. double dSmashedZ = 0.0;
  2155. m_bPylonSafeAvailable = false;
  2156. m_bPylonPopoutAvailable = false;
  2157. m_bPylonRunShootAvailable = false;
  2158. // Update the detection smash.
  2159. m_smashDetect.m_sphere.sphere.X = m_dX;
  2160. m_smashDetect.m_sphere.sphere.Y = m_dY;
  2161. m_smashDetect.m_sphere.sphere.Z = m_dZ;
  2162. // For now when we detect a pylon we are assuming that this
  2163. // character should use that logic. Later we will have to
  2164. // decide based on the personality numbers and current state.
  2165. if (m_pRealm->m_smashatorium.QuickCheckClosest(&m_smashDetect,
  2166. CSmash::Pylon,
  2167. 0,
  2168. 0,
  2169. &pSmashPylon))
  2170. {
  2171. ASSERT(pSmashPylon);
  2172. pPylon = (CPylon*) pSmashPylon->m_pThing;
  2173. ASSERT(pPylon);
  2174. if (pSmashPylon->m_pThing != this)
  2175. {
  2176. dSmashedX = pSmashPylon->m_sphere.sphere.X;
  2177. dSmashedZ = pSmashPylon->m_sphere.sphere.Z;
  2178. // Check IsPathClear to that thing
  2179. if (m_pRealm->IsPathClear( // Returns true, if the entire path is clear.
  2180. // Returns false, if only a portion of the path is clear.
  2181. // (see *psX, *psY, *psZ).
  2182. (short) m_dX, // In: Starting X.
  2183. (short) m_dY, // In: Starting Y.
  2184. (short) m_dZ, // In: Starting Z.
  2185. 3.0, // In: Rate at which to scan ('crawl') path in pixels per
  2186. // iteration.
  2187. // NOTE: Values less than 1.0 are inefficient.
  2188. // NOTE: We scan terrain using GetHeight()
  2189. // at only one pixel.
  2190. // NOTE: We could change this to a speed in pixels per second
  2191. // where we'd assume a certain frame rate.
  2192. (short) dSmashedX, // In: Destination X.
  2193. (short) dSmashedZ, // In: Destination Z.
  2194. 0, // In: Max traverser can step up.
  2195. NULL, // Out: If not NULL, last clear point on path.
  2196. NULL, // Out: If not NULL, last clear point on path.
  2197. NULL, // Out: If not NULL, last clear point on path.
  2198. true) ) // In: If true, will consider the edge of the realm a path
  2199. // inhibitor. If false, reaching the edge of the realm
  2200. // indicates a clear path.
  2201. {
  2202. switch (pPylon->m_msg.msg_Generic.eType)
  2203. {
  2204. case typeSafeSpot:
  2205. m_bPylonSafeAvailable = true;
  2206. m_pPylonStart = pPylon;
  2207. break;
  2208. case typePopout:
  2209. m_bPylonPopoutAvailable = true;
  2210. m_pPylonStart = pPylon;
  2211. break;
  2212. case typeShootCycle:
  2213. m_bPylonRunShootAvailable = true;
  2214. m_pPylonStart = pPylon;
  2215. break;
  2216. default:
  2217. break;
  2218. }
  2219. }
  2220. }
  2221. }
  2222. else
  2223. {
  2224. // If no pylons were nearby where he started, and he was just idle,
  2225. // then set him up as a stationary guard for now.
  2226. if (m_state == State_Idle || m_state == State_Wait)
  2227. {
  2228. // See if there are bouys, if there aren't any, then stay in Guard mode
  2229. if (m_pNavNet->GetNumNodes() > 1)
  2230. {
  2231. m_state = State_Hunt;
  2232. m_eCurrentAction = Action_Advance;
  2233. }
  2234. else
  2235. {
  2236. m_state = State_Guard;
  2237. m_eCurrentAction = Action_Guard;
  2238. }
  2239. }
  2240. }
  2241. }
  2242. ////////////////////////////////////////////////////////////////////////////////
  2243. // Logic_HideBegin - Go to the hiding pylon and then wait once you get there
  2244. ////////////////////////////////////////////////////////////////////////////////
  2245. void CDoofus::Logic_HideBegin(void)
  2246. {
  2247. long lThisTime = m_pRealm->m_time.GetGameTime();
  2248. // If we'renot using the run animation yet then switch to it
  2249. if (m_panimCur != &m_animRun)
  2250. {
  2251. m_panimCur = &m_animRun;
  2252. m_lAnimTime = 0;
  2253. }
  2254. else
  2255. m_lAnimTime += lThisTime - m_lPrevTime;
  2256. // Set angle to pylon.
  2257. m_dAnimRot = m_dRot = FindAngleTo(m_sNextX, m_sNextZ);
  2258. m_dAcc = ms_dAccUser;
  2259. long lElapsedTime = m_pRealm->m_time.GetGameTime() - m_lPrevTime;
  2260. double dSeconds = lElapsedTime / 1000.0;
  2261. DeluxeUpdatePosVel(dSeconds);
  2262. // if close to pylon, go to next state
  2263. // for now just check the square distance, but later, probably use
  2264. // QuickCheckCloses in smash to see if you are there yet.
  2265. double dX = m_dX - (double) m_sNextX;
  2266. double dZ = m_dZ - (double) m_sNextZ;
  2267. double dsq = (dX * dX) + (dZ * dZ);
  2268. if (dsq < 300)
  2269. {
  2270. m_state = State_Hide;
  2271. }
  2272. // Check for fire in your path
  2273. AvoidFire();
  2274. }
  2275. ////////////////////////////////////////////////////////////////////////////////
  2276. // Logic_Hide - Wait here and hope nobody sees you and blows you away.
  2277. ////////////////////////////////////////////////////////////////////////////////
  2278. void CDoofus::Logic_Hide(void)
  2279. {
  2280. ReevaluateState();
  2281. }
  2282. ////////////////////////////////////////////////////////////////////////////////
  2283. // Logic_PopBegin - Once you have detected a nearby Pylon, begin the popout
  2284. // phase here by going to that pylon.
  2285. ////////////////////////////////////////////////////////////////////////////////
  2286. void CDoofus::Logic_PopBegin(void)
  2287. {
  2288. long lThisTime = m_pRealm->m_time.GetGameTime();
  2289. double dStartX = m_dX;
  2290. double dStartZ = m_dZ;
  2291. // If we're not using the run animation yet then switch to it
  2292. if (m_panimCur != &m_animRun)
  2293. {
  2294. m_panimCur = &m_animRun;
  2295. m_lAnimTime = 0;
  2296. }
  2297. else
  2298. m_lAnimTime += lThisTime - m_lPrevTime;
  2299. m_eCurrentAction = Action_Popout;
  2300. // Set angle to pylon.
  2301. if (lThisTime > m_lAlignTimer)
  2302. {
  2303. m_dAnimRot = m_dRot = FindAngleTo(m_sNextX, m_sNextZ);
  2304. m_lAlignTimer = lThisTime + 100;
  2305. m_sRotateDir = GetRandom() % 2;
  2306. }
  2307. m_dAcc = ms_dAccUser;
  2308. long lElapsedTime = m_pRealm->m_time.GetGameTime() - m_lPrevTime;
  2309. double dSeconds = lElapsedTime / 1000.0;
  2310. DeluxeUpdatePosVel(dSeconds);
  2311. // Check for fire in your path
  2312. AvoidFire();
  2313. // If not moving when meaning to, rotate to avoid obstacle
  2314. if (m_dX == dStartX && m_dZ == dStartZ)
  2315. {
  2316. if (m_sRotateDir)
  2317. m_dAnimRot = m_dRot = rspMod360(m_dRot + 20);
  2318. else
  2319. m_dAnimRot = m_dRot = rspMod360(m_dRot - 20);
  2320. m_lAlignTimer = lThisTime + 100;
  2321. }
  2322. // if close to pylon, go to next state
  2323. // for now just check the square distance, but later, probably use
  2324. // QuickCheckCloses in smash to see if you are there yet.
  2325. double dX = m_dX - (double) m_sNextX;
  2326. double dZ = m_dZ - (double) m_sNextZ;
  2327. double dsq = (dX * dX) + (dZ * dZ);
  2328. if (dsq < 300)
  2329. {
  2330. m_state = State_PopWait;
  2331. }
  2332. }
  2333. ////////////////////////////////////////////////////////////////////////////////
  2334. // Logic_PopWait - Wait at covered pylon until a CDude steps on the trigger
  2335. ////////////////////////////////////////////////////////////////////////////////
  2336. void CDoofus::Logic_PopWait(void)
  2337. {
  2338. // If the stand animation is not being used yet then switch to it.
  2339. // if (m_panimCur != &m_animStand)
  2340. // {
  2341. // m_panimCur = &m_animStand;
  2342. // m_lAnimTime = 0;
  2343. // }
  2344. RunIdleAnimation();
  2345. if (!ReevaluateState())
  2346. {
  2347. m_eCurrentAction = Action_Popout;
  2348. if (m_pRealm->m_time.GetGameTime() > m_lTimer && m_pPylonStart->Triggered())
  2349. {
  2350. // Set next pylon to go to
  2351. m_sNextX = m_pPylonEnd->GetX();
  2352. m_sNextZ = m_pPylonEnd->GetZ();
  2353. m_state = State_Popout;
  2354. m_panimCur = &m_animRun;
  2355. m_lAnimTime = 0;
  2356. }
  2357. }
  2358. else
  2359. {
  2360. m_pPylonStart = NULL;
  2361. }
  2362. }
  2363. ////////////////////////////////////////////////////////////////////////////////
  2364. // Logic_Popout - When at a popout bouy, if the guy is close, popout, shoot,
  2365. // and duck back behind the bouy. If he is not in range, stay
  2366. // behind the bouy and wait for some amount of time. Reevaluate
  2367. // logic choice based on enemy attributes.
  2368. ////////////////////////////////////////////////////////////////////////////////
  2369. void CDoofus::Logic_Popout(void)
  2370. {
  2371. long lThisTime = m_pRealm->m_time.GetGameTime();
  2372. long lElapsedTime = lThisTime - m_lPrevTime;
  2373. double dSeconds = lElapsedTime / 1000.0;
  2374. m_lAnimTime += lElapsedTime;
  2375. m_eCurrentAction = Action_Popout;
  2376. // Go to next pylon
  2377. m_dAnimRot = m_dRot = FindAngleTo(m_sNextX, m_sNextZ);
  2378. m_dAcc = ms_dAccUser;
  2379. DeluxeUpdatePosVel(dSeconds);
  2380. // Check for fire in your path
  2381. AvoidFire();
  2382. // If you are at the pylon, then go to the next state
  2383. // if close to pylon, go to next state
  2384. // for now just check the square distance, but later, probably use
  2385. // QuickCheckCloses in smash to see if you are there yet.
  2386. double dX = m_dX - (double) m_sNextX;
  2387. double dZ = m_dZ - (double) m_sNextZ;
  2388. double dsq;
  2389. dsq = (dX * dX) + (dZ * dZ);
  2390. if (dsq < 300)
  2391. {
  2392. m_state = State_Shoot;
  2393. // After shooting, go back to beginning
  2394. m_eNextState = State_PopBegin;
  2395. m_lTimer = lThisTime + 2000 + GetRandom() % 2000;
  2396. m_sNextX = m_pPylonStart->GetX();
  2397. m_sNextZ = m_pPylonStart->GetZ();
  2398. CWeapon* pweapon = PrepareWeapon();
  2399. if (pweapon != NULL)
  2400. {
  2401. // Keep it hidden, for now.
  2402. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  2403. }
  2404. m_panimCur = &m_animShoot;
  2405. m_lAnimTime = 0;
  2406. SelectDude();
  2407. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  2408. }
  2409. }
  2410. ////////////////////////////////////////////////////////////////////////////////
  2411. // Logic_Shoot - run the animation to the proper time and then shoot the weapon
  2412. ////////////////////////////////////////////////////////////////////////////////
  2413. void CDoofus::Logic_Shoot(void)
  2414. {
  2415. long lThisTime = m_pRealm->m_time.GetGameTime();
  2416. long lTimeDifference = lThisTime - m_lPrevTime;
  2417. // Switch behavior on weapon type.
  2418. switch (m_eWeaponType)
  2419. {
  2420. case CThing::CFirestreamID:
  2421. // If out of fire streams . . .
  2422. if (m_u16IdWeapon == CIdBank::IdNil)
  2423. {
  2424. // Must prepare more fire.
  2425. if (PrepareWeapon() == NULL)
  2426. break;
  2427. }
  2428. // Intentional fall through.
  2429. case CThing::CShotGunID:
  2430. case CThing::CMachineGunID:
  2431. case CThing::CPistolID:
  2432. case CThing::CAssaultWeaponID:
  2433. case CThing::CDoubleBarrelID:
  2434. case CUziID:
  2435. case CAutoRifleID:
  2436. case CSmallPistolID:
  2437. {
  2438. // Get event.
  2439. U8 u8Event = *( (U8*)(m_panimCur->m_pevent->GetAtTime(m_lAnimTime) ) );
  2440. // We don't care about show point for these non-object weapon types.
  2441. // If it's time to fire the weapon . . .
  2442. if (u8Event > 0 && lThisTime > m_lShootTimer)
  2443. {
  2444. // Fire!
  2445. ShootWeapon();
  2446. switch (m_eWeaponType)
  2447. {
  2448. case CThing::CShotGunID:
  2449. case CThing::CDoubleBarrelID:
  2450. m_lShootTimer = lThisTime + 2000;
  2451. break;
  2452. case CUziID:
  2453. case CAutoRifleID:
  2454. case CThing::CMachineGunID:
  2455. case CThing::CAssaultWeaponID:
  2456. case CThing::CFirestreamID:
  2457. m_lShootTimer = lThisTime + 100;
  2458. break;
  2459. case CSmallPistolID:
  2460. case CThing::CPistolID:
  2461. m_lShootTimer = lThisTime + 300;
  2462. break;
  2463. }
  2464. }
  2465. break;
  2466. }
  2467. // This is the "I don't have a weapon" case (i.e., NoWeapon).
  2468. case TotalIDs:
  2469. break;
  2470. default:
  2471. if (m_u16IdWeapon != CIdBank::IdNil)
  2472. {
  2473. if (WhileHoldingWeapon(ms_u32CollideBitsInclude, ms_u32CollideBitsDontcare, ms_u32CollideBitsExclude) == true)
  2474. {
  2475. // Note we are done with the child. Now we're just finishing the anim.
  2476. ASSERT(m_u16IdWeapon == CIdBank::IdNil);
  2477. }
  2478. }
  2479. break;
  2480. }
  2481. // See if we are at the end of the animation or we have no weapon . . .
  2482. if (m_lAnimTime > m_panimCur->m_psops->TotalTime() || m_eWeaponType == TotalIDs)
  2483. {
  2484. m_state = m_eNextState;
  2485. }
  2486. // Else advance the animation timer
  2487. else
  2488. {
  2489. m_lAnimTime += lTimeDifference;
  2490. }
  2491. }
  2492. ////////////////////////////////////////////////////////////////////////////////
  2493. // Logic_ShootRun - Shoot while running - use the Shoot logic to control
  2494. // the firing of the shot and the animation, and just add on
  2495. // the motion update
  2496. ////////////////////////////////////////////////////////////////////////////////
  2497. void CDoofus::Logic_ShootRun(void)
  2498. {
  2499. double dSeconds = (m_pRealm->m_time.GetGameTime() - m_lPrevTime) / 1000.0;
  2500. Logic_Shoot();
  2501. DeluxeUpdatePosVel(dSeconds);
  2502. // Check for fire in your path
  2503. AvoidFire();
  2504. }
  2505. ////////////////////////////////////////////////////////////////////////////////
  2506. // Logic_RunShootBegin - Set up the destination pylon and shooting interval
  2507. ////////////////////////////////////////////////////////////////////////////////
  2508. void CDoofus::Logic_RunShootBegin(void)
  2509. {
  2510. long lThisTime = m_pRealm->m_time.GetGameTime();
  2511. long lElapsedTime = lThisTime - m_lPrevTime;
  2512. double dStartX = m_dX;
  2513. double dStartZ = m_dZ;
  2514. // If we're not using the run animation yet, then switch to it
  2515. if (m_panimCur != &m_animRun)
  2516. {
  2517. m_panimCur = &m_animRun;
  2518. m_lAnimTime = 0;
  2519. }
  2520. else
  2521. m_lAnimTime += lElapsedTime;
  2522. // Set angle to pylon.
  2523. if (lThisTime > m_lAlignTimer)
  2524. {
  2525. m_dAnimRot = m_dRot = FindAngleTo(m_sNextX, m_sNextZ);
  2526. m_lAlignTimer = lThisTime + 100;
  2527. m_sRotateDir = GetRandom() % 2;
  2528. }
  2529. m_dAcc = ms_dAccUser;
  2530. double dSeconds = lElapsedTime / 1000.0;
  2531. DeluxeUpdatePosVel(dSeconds);
  2532. // Check for fire in your path
  2533. AvoidFire();
  2534. // If not moving when you meant to, start to rotate to avoid obstacles
  2535. if (m_dX == dStartX && m_dZ == dStartZ)
  2536. {
  2537. if (m_sRotateDir)
  2538. m_dAnimRot = m_dRot = rspMod360(m_dRot + 20);
  2539. else
  2540. m_dAnimRot = m_dRot = rspMod360(m_dRot - 20);
  2541. m_lAlignTimer = lThisTime + 100;
  2542. }
  2543. // If close to pylon, go to nex state
  2544. double dX = m_dX - (double) m_sNextX;
  2545. double dZ = m_dZ - (double) m_sNextZ;
  2546. double dsq = (dX * dX) + (dZ * dZ);
  2547. if (dsq < 300)
  2548. {
  2549. // Find position of destinaiton pylon
  2550. m_sNextX = m_pPylonEnd->GetX();
  2551. m_sNextZ = m_pPylonEnd->GetZ();
  2552. m_dAnimRot = m_dRot = FindAngleTo(m_sNextX, m_sNextZ);
  2553. m_lTimer = lThisTime + m_lRunShootInterval;
  2554. // m_state = State_RunShoot;
  2555. m_state = State_RunShootWait;
  2556. }
  2557. }
  2558. ////////////////////////////////////////////////////////////////////////////////
  2559. // Logic_RunShoot - Run and Shoot logic. When near a ShootCycle bouy, hide,
  2560. // run out and shoot at a Dude, run towards other bouy, stop
  2561. // and shoot again, then duck behind the second bouy. Repeat
  2562. ////////////////////////////////////////////////////////////////////////////////
  2563. void CDoofus::Logic_RunShoot(void)
  2564. {
  2565. long lThisTime = m_pRealm->m_time.GetGameTime();
  2566. long lElapsedTime = lThisTime - m_lPrevTime;
  2567. // If its not using the run animation, then switch to it
  2568. if (m_panimCur != &m_animRun)
  2569. {
  2570. m_panimCur = &m_animRun;
  2571. // m_lAnimTime = 0;
  2572. m_dAnimRot = m_dRot;
  2573. }
  2574. if (!ReevaluateState())
  2575. {
  2576. // Advance the run animation
  2577. m_lAnimTime += lElapsedTime;
  2578. // If its time to shoot then do it.
  2579. if (lThisTime > m_lTimer)
  2580. {
  2581. m_state = State_ShootRun;
  2582. // After shooting, go back to beginning
  2583. m_eNextState = State_RunShoot;
  2584. CWeapon* pweapon = PrepareWeapon();
  2585. if (pweapon != NULL)
  2586. {
  2587. // Keep it hidden, for now.
  2588. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  2589. }
  2590. SelectDude();
  2591. short sTargetAngle = FindDirection();
  2592. m_dShootAngle = sTargetAngle;
  2593. short sAngleCCL = rspMod360(sTargetAngle - m_dRot);
  2594. short sAngleCL = rspMod360((360 - sTargetAngle) + m_dRot);
  2595. short sAngleDistance = MIN(sAngleCCL, sAngleCL);
  2596. if (sAngleCCL < sAngleCL)
  2597. // Rotate Counter Clockwise - Use left animations
  2598. {
  2599. if (sAngleDistance > 150)
  2600. {
  2601. m_panimCur = &m_animShootRunBack;
  2602. m_dAnimRot = rspMod360(m_dRot + (sAngleDistance - 180));
  2603. }
  2604. else
  2605. {
  2606. if (sAngleDistance > 68)
  2607. {
  2608. m_panimCur = &m_animShootRunL1;
  2609. m_dAnimRot = rspMod360(m_dRot + (sAngleDistance - 90));
  2610. }
  2611. else
  2612. {
  2613. if (sAngleDistance > 23)
  2614. {
  2615. m_panimCur = &m_animShootRunL0;
  2616. m_dAnimRot = rspMod360(m_dRot + (sAngleDistance - 45));
  2617. }
  2618. else
  2619. {
  2620. m_panimCur = &m_animShootRun;
  2621. m_dAnimRot = rspMod360(m_dRot + sAngleDistance);
  2622. }
  2623. }
  2624. }
  2625. }
  2626. else
  2627. // Rotate Clockwise - Use right animations
  2628. {
  2629. if (sAngleDistance > 150)
  2630. {
  2631. m_panimCur = &m_animShootRunBack;
  2632. m_dAnimRot = rspMod360(m_dRot - (sAngleDistance - 180));
  2633. }
  2634. else
  2635. {
  2636. if (sAngleDistance > 68)
  2637. {
  2638. m_panimCur = &m_animShootRunR1;
  2639. m_dAnimRot = rspMod360(m_dRot - (sAngleDistance - 90));
  2640. }
  2641. else
  2642. {
  2643. if (sAngleDistance > 23)
  2644. {
  2645. m_panimCur = &m_animShootRunR0;
  2646. m_dAnimRot = rspMod360(m_dRot - (sAngleDistance - 45));
  2647. }
  2648. else
  2649. {
  2650. m_panimCur = &m_animShootRun;
  2651. m_dAnimRot = rspMod360(m_dRot - sAngleDistance);
  2652. }
  2653. }
  2654. }
  2655. }
  2656. m_lTimer = lThisTime + m_lRunShootInterval;
  2657. m_lAnimTime = 0;
  2658. }
  2659. else
  2660. {
  2661. // Set angle to pylon.
  2662. m_dAnimRot = m_dRot = FindAngleTo(m_sNextX, m_sNextZ);
  2663. m_dAcc = ms_dAccUser;
  2664. double dSeconds = lElapsedTime / 1000.0;
  2665. DeluxeUpdatePosVel(dSeconds);
  2666. // Check for fire in your path
  2667. AvoidFire();
  2668. // if close to pylon, go to next state
  2669. // for now just check the square distance, but later, probably use
  2670. // QuickCheckCloses in smash to see if you are there yet.
  2671. double dX = m_dX - (double) m_sNextX;
  2672. double dZ = m_dZ - (double) m_sNextZ;
  2673. double dsq = (dX * dX) + (dZ * dZ);
  2674. if (dsq < 300)
  2675. {
  2676. m_state = State_RunShootWait;
  2677. m_lTimer = lThisTime + m_lGuardTimeout;
  2678. // Make him face the other way.
  2679. m_dAnimRot = m_dRot = FindAngleTo(m_pPylonEnd->GetX(), m_pPylonEnd->GetZ());
  2680. }
  2681. }
  2682. }
  2683. else
  2684. {
  2685. m_pPylonStart = NULL;
  2686. }
  2687. }
  2688. ////////////////////////////////////////////////////////////////////////////////
  2689. // Logic_RunShootWait - Wait at the endpoint before going again.
  2690. ////////////////////////////////////////////////////////////////////////////////
  2691. void CDoofus::Logic_RunShootWait(void)
  2692. {
  2693. long lThisTime = m_pRealm->m_time.GetGameTime();
  2694. RunIdleAnimation();
  2695. // If the waiting is over and its triggered
  2696. if (lThisTime > m_lTimer && m_pPylonStart->Triggered())
  2697. {
  2698. CPylon* pPylonTemp = m_pPylonStart;
  2699. m_pPylonStart = m_pPylonEnd;
  2700. m_pPylonEnd = pPylonTemp;
  2701. m_state = State_RunShoot;
  2702. m_sNextX = m_pPylonStart->GetX();
  2703. m_sNextZ = m_pPylonStart->GetZ();
  2704. m_dVel = 0.0;
  2705. }
  2706. }
  2707. ////////////////////////////////////////////////////////////////////////////////
  2708. // Logic_Retreat - Once you are low on health, you may choose this logic to
  2709. // run away from the Dude, taking shelter wherever you can.
  2710. ////////////////////////////////////////////////////////////////////////////////
  2711. void CDoofus::Logic_Retreat(void)
  2712. {
  2713. // Get your closest bouy, pick a random destination bouy, then
  2714. // start traversing 3 away from your current one and set that
  2715. // as the destination bouy, then switch to State_MoveNext
  2716. m_eDestinationState = State_Guard;
  2717. m_ucDestBouyID = SelectRandomBouy();
  2718. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  2719. if (m_ucNextBouyID > 0)
  2720. {
  2721. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  2722. if (m_pNextBouy != NULL)
  2723. {
  2724. m_sNextX = m_pNextBouy->GetX();
  2725. m_sNextZ = m_pNextBouy->GetZ();
  2726. m_state = State_MoveNext;
  2727. m_eCurrentAction = Action_Retreat;
  2728. }
  2729. else
  2730. {
  2731. // Couldn't find bouy so stay engaged
  2732. m_state = State_Engage;
  2733. m_eCurrentAction = Action_Engage;
  2734. }
  2735. }
  2736. else
  2737. {
  2738. // Couldn't find a bouy so just stay in engage.
  2739. m_state = State_Engage;
  2740. m_eCurrentAction = Action_Engage;
  2741. }
  2742. }
  2743. ////////////////////////////////////////////////////////////////////////////////
  2744. // Logic_PanicBegin - Pick a random bouy to run to and set animation
  2745. ////////////////////////////////////////////////////////////////////////////////
  2746. void CDoofus::Logic_PanicBegin(void)
  2747. {
  2748. // See if dude is still alive
  2749. SelectDude();
  2750. // Switch to correct animation if not already.
  2751. if (m_panimCur != &m_animRun)
  2752. {
  2753. m_panimCur = &m_animRun;
  2754. m_lAnimTime = 0;
  2755. }
  2756. m_eDestinationState = State_PanicContinue;
  2757. m_bPanic = true;
  2758. m_ucDestBouyID = SelectRandomBouy();
  2759. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  2760. if (m_ucNextBouyID > 0)
  2761. {
  2762. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  2763. if (m_pNextBouy != NULL)
  2764. {
  2765. m_sNextX = m_pNextBouy->GetX();
  2766. m_sNextZ = m_pNextBouy->GetZ();
  2767. m_state = State_MoveNext;
  2768. }
  2769. else
  2770. {
  2771. // Couldn't find bouy so stay engaged
  2772. m_state = State_PanicBegin;
  2773. }
  2774. }
  2775. else
  2776. {
  2777. // Couldn't find a bouy so just stay in engage.
  2778. m_state = State_PanicBegin;
  2779. }
  2780. }
  2781. ////////////////////////////////////////////////////////////////////////////////
  2782. // Logic_PanicContinue - End of panic state - pick another bouy
  2783. ////////////////////////////////////////////////////////////////////////////////
  2784. void CDoofus::Logic_PanicContinue(void)
  2785. {
  2786. m_state = State_PanicBegin;
  2787. }
  2788. ////////////////////////////////////////////////////////////////////////////////
  2789. // Logic_MarchBegin - Pick an endpoint and march to it.
  2790. ////////////////////////////////////////////////////////////////////////////////
  2791. void CDoofus::Logic_MarchBegin(void)
  2792. {
  2793. // See if Dude is still alive
  2794. SelectDude();
  2795. // Switch to correct animation if not already.
  2796. if (m_panimCur != &m_animWalk)
  2797. {
  2798. m_panimCur = &m_animWalk;
  2799. m_lAnimTime = 0;
  2800. }
  2801. m_eDestinationState = State_March;
  2802. m_eCurrentAction = Action_March;
  2803. // Pick an endpoint that we are not already at.
  2804. if (m_ucDestBouyID == m_ucSpecialBouy0ID)
  2805. m_ucDestBouyID = m_ucSpecialBouy1ID;
  2806. else
  2807. m_ucDestBouyID = m_ucSpecialBouy0ID;
  2808. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  2809. if (m_ucNextBouyID > 0)
  2810. {
  2811. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  2812. if (m_pNextBouy != NULL)
  2813. {
  2814. m_sNextX = m_pNextBouy->GetX();
  2815. m_sNextZ = m_pNextBouy->GetZ();
  2816. m_state = State_MarchNext;
  2817. }
  2818. // Couldn't find a bouy - try guard mode
  2819. else
  2820. {
  2821. m_state = State_Guard;
  2822. m_eCurrentAction = Action_Guard;
  2823. }
  2824. }
  2825. // Couldn't find a bouy - try guard mode
  2826. else
  2827. {
  2828. m_state = State_Guard;
  2829. m_eCurrentAction = Action_Guard;
  2830. }
  2831. }
  2832. ////////////////////////////////////////////////////////////////////////////////
  2833. // Logic_WalkBegin - Pick a random bouy to run to and set animation
  2834. ////////////////////////////////////////////////////////////////////////////////
  2835. void CDoofus::Logic_WalkBegin(void)
  2836. {
  2837. // See if dude is still alive
  2838. SelectDude();
  2839. m_eDestinationState = State_WalkContinue;
  2840. m_ucDestBouyID = SelectRandomBouy();
  2841. m_ucNextBouyID = m_pNavNet->FindNearestBouy(m_dX, m_dZ);
  2842. if (m_ucNextBouyID > 0)
  2843. {
  2844. m_pNextBouy = m_pNavNet->GetBouy(m_ucNextBouyID);
  2845. if (m_pNextBouy != NULL)
  2846. {
  2847. m_sNextX = m_pNextBouy->GetX();
  2848. m_sNextZ = m_pNextBouy->GetZ();
  2849. m_state = State_WalkNext;
  2850. }
  2851. else
  2852. {
  2853. // Couldn't find bouy so stay engaged
  2854. m_state = State_Walk;
  2855. }
  2856. }
  2857. else
  2858. {
  2859. // Couldn't find a bouy so just stay in engage.
  2860. m_state = State_Walk;
  2861. }
  2862. }
  2863. ////////////////////////////////////////////////////////////////////////////////
  2864. // Logic_WalkContinue - End of walk state - pick another bouy
  2865. ////////////////////////////////////////////////////////////////////////////////
  2866. void CDoofus::Logic_WalkContinue(void)
  2867. {
  2868. m_state = State_WalkBegin;
  2869. }
  2870. ////////////////////////////////////////////////////////////////////////////////
  2871. // Logic_Helping - Help out your buddies without moving around and screwing
  2872. // up your positioning
  2873. ////////////////////////////////////////////////////////////////////////////////
  2874. void CDoofus::Logic_Helping(void)
  2875. {
  2876. long lThisTime = m_pRealm->m_time.GetGameTime();
  2877. if (lThisTime > m_lTimer)
  2878. {
  2879. m_eCurrentAction = Action_Help;
  2880. m_lTimer = lThisTime + ms_lHelpingTimeout;
  2881. if (SelectDude() == SUCCESS && SQDistanceToDude() <= ms_lYellRadius)
  2882. {
  2883. if (CDoofus::TryClearShot(m_dRot, 20) == true)
  2884. {
  2885. CWeapon* pweapon = PrepareWeapon();
  2886. if (pweapon != NULL)
  2887. {
  2888. // Keep it hidden, for now.
  2889. pweapon->GetSprite()->m_sInFlags |= CSprite::InHidden;
  2890. pweapon->SetRangeToTarget(rspSqrt(SQDistanceToDude()));
  2891. }
  2892. m_panimCur = &m_animShoot;
  2893. m_lAnimTime = 0;
  2894. SelectDude();
  2895. m_dShootAngle = m_dAnimRot = m_dRot = FindDirection();
  2896. m_state = State_Shoot;
  2897. m_eNextState = State_Helping;
  2898. m_lTimer = lThisTime + ms_lHelpingTimeout;
  2899. }
  2900. else
  2901. {
  2902. m_lTimer = lThisTime + ms_lHelpingTimeout;
  2903. }
  2904. }
  2905. }
  2906. else
  2907. ReevaluateState();
  2908. }
  2909. ////////////////////////////////////////////////////////////////////////////////
  2910. // Logic_AvoidFire - Wait for fire danger to pass before going back to your
  2911. // previous state. This state is set by AvoidFire function
  2912. // when there is a fire in your path.
  2913. ////////////////////////////////////////////////////////////////////////////////
  2914. void CDoofus::Logic_AvoidFire(void)
  2915. {
  2916. if (AvoidFire())
  2917. {
  2918. // Check for a different state
  2919. ReevaluateState();
  2920. }
  2921. else
  2922. // Go back to your previous state
  2923. {
  2924. m_state = m_ePreviousState;
  2925. m_panimCur = m_panimPrev;
  2926. }
  2927. }
  2928. ////////////////////////////////////////////////////////////////////////////////
  2929. // AvoidFire - checks for fire in your path, returns true if there is a fire
  2930. // danger. If you are not already in the fire avoidance state,
  2931. // it will save your previous state info and change your state.
  2932. ////////////////////////////////////////////////////////////////////////////////
  2933. bool CDoofus::AvoidFire(void)
  2934. {
  2935. bool bFireDanger = false;
  2936. CSmash* pSmashed = NULL;
  2937. // Change this to quick check closest
  2938. if (m_pRealm->m_smashatorium.QuickCheck(&m_smashAvoid,
  2939. CSmash::Fire,
  2940. CSmash::Good | CSmash::Bad,
  2941. 0,
  2942. &pSmashed))
  2943. {
  2944. bFireDanger = true;
  2945. // If its not already avoiding fire, switch to this safe state
  2946. // and save the previous state
  2947. if (m_state != State_AvoidFire)
  2948. {
  2949. m_ePreviousState = m_state;
  2950. m_panimPrev = m_panimCur;
  2951. m_state = State_AvoidFire;
  2952. m_panimCur = &m_animStand;
  2953. }
  2954. }
  2955. return bFireDanger;
  2956. }
  2957. ////////////////////////////////////////////////////////////////////////////////
  2958. // YellForHelp - Call this when you get shot and it will alert other enemies in
  2959. // the area within line of sight that you are in trouble. Then
  2960. // they can choose to react.
  2961. ////////////////////////////////////////////////////////////////////////////////
  2962. void CDoofus::YellForHelp(void)
  2963. {
  2964. CSmash* pSmashed = NULL;
  2965. CSmash smashYell;
  2966. double dSmashedX;
  2967. double dSmashedZ;
  2968. GameMessage msg;
  2969. msg.msg_Help.eType = typeHelp;
  2970. msg.msg_Help.sPriority = 0;
  2971. // Set up the yell smash
  2972. smashYell.m_sphere.sphere.X = m_dX;
  2973. smashYell.m_sphere.sphere.Y = m_dY;
  2974. smashYell.m_sphere.sphere.Z = m_dZ;
  2975. smashYell.m_sphere.sphere.lRadius = ms_lYellRadius;
  2976. smashYell.m_bits = 0;
  2977. smashYell.m_pThing = this;
  2978. m_pRealm->m_smashatorium.QuickCheckReset(&smashYell,
  2979. CSmash::Character | CSmash::Bad,
  2980. 0,
  2981. CSmash::Good);
  2982. while (m_pRealm->m_smashatorium.QuickCheckNext(&pSmashed))
  2983. {
  2984. ASSERT(pSmashed->m_pThing);
  2985. if (pSmashed->m_pThing != this)
  2986. {
  2987. dSmashedX = pSmashed->m_sphere.sphere.X;
  2988. dSmashedZ = pSmashed->m_sphere.sphere.Z;
  2989. // Check IsPathClear to that thing
  2990. if (m_pRealm->IsPathClear( // Returns true, if the entire path is clear.
  2991. // Returns false, if only a portion of the path is clear.
  2992. // (see *psX, *psY, *psZ).
  2993. (short) m_dX, // In: Starting X.
  2994. (short) m_dY, // In: Starting Y.
  2995. (short) m_dZ, // In: Starting Z.
  2996. 3.0, // In: Rate at which to scan ('crawl') path in pixels per
  2997. // iteration.
  2998. // NOTE: Values less than 1.0 are inefficient.
  2999. // NOTE: We scan terrain using GetHeight()
  3000. // at only one pixel.
  3001. // NOTE: We could change this to a speed in pixels per second
  3002. // where we'd assume a certain frame rate.
  3003. (short) dSmashedX, // In: Destination X.
  3004. (short) dSmashedZ, // In: Destination Z.
  3005. 0, // In: Max traverser can step up.
  3006. NULL, // Out: If not NULL, last clear point on path.
  3007. NULL, // Out: If not NULL, last clear point on path.
  3008. NULL, // Out: If not NULL, last clear point on path.
  3009. true) ) // In: If true, will consider the edge of the realm a path
  3010. // inhibitor. If false, reaching the edge of the realm
  3011. // indicates a clear path.
  3012. {
  3013. SendThingMessage(&msg, pSmashed->m_pThing);
  3014. }
  3015. }
  3016. }
  3017. }
  3018. ////////////////////////////////////////////////////////////////////////////////
  3019. // ReevaluateState - Based on parameters, some randomness, current state etc,
  3020. // come up with a new state.
  3021. //
  3022. // Returns true if the state changed
  3023. ////////////////////////////////////////////////////////////////////////////////
  3024. bool CDoofus::ReevaluateState(void)
  3025. {
  3026. bool bChanged = false;
  3027. // If its not already doing the suggested Action, then attempt to switch to it.
  3028. // and if its not in one of the final death states
  3029. if (m_eSuggestedAction != m_eCurrentAction &&
  3030. m_state != State_Burning &&
  3031. m_state != State_BlownUp &&
  3032. m_state != State_Die &&
  3033. m_state != State_Dead &&
  3034. m_state != State_Writhing)
  3035. {
  3036. switch (m_eSuggestedAction)
  3037. {
  3038. case Action_Guard:
  3039. if (m_panimCur != &m_animStand &&
  3040. m_panimCur != &m_animCrouch &&
  3041. m_panimCur != &m_animSearch)
  3042. {
  3043. m_panimCur = &m_animStand;
  3044. m_lAnimTime = 0;
  3045. }
  3046. m_state = State_Guard;
  3047. m_eCurrentAction = Action_Guard;
  3048. bChanged = true;
  3049. break;
  3050. case Action_Advance:
  3051. if (m_eCurrentAction != Action_AdvanceHold)
  3052. {
  3053. m_state = State_Hunt;
  3054. m_eCurrentAction = Action_Advance;
  3055. bChanged = true;
  3056. }
  3057. break;
  3058. case Action_Retreat:
  3059. m_state = State_Retreat;
  3060. m_eCurrentAction = Action_Retreat;
  3061. bChanged = true;
  3062. break;
  3063. case Action_Engage:
  3064. m_state = State_Engage;
  3065. m_eCurrentAction = Action_Engage;
  3066. bChanged = true;
  3067. break;
  3068. case Action_Walk:
  3069. m_state = State_WalkBegin;
  3070. m_eCurrentAction = Action_Walk;
  3071. bChanged = true;
  3072. break;
  3073. case Action_March:
  3074. m_state = State_March;
  3075. m_eCurrentAction = Action_March;
  3076. bChanged = true;
  3077. break;
  3078. case Action_Panic:
  3079. m_state = State_PanicBegin;
  3080. m_eCurrentAction = Action_Panic;
  3081. bChanged = true;
  3082. break;
  3083. case Action_Help:
  3084. m_state = State_Helping;
  3085. m_eCurrentAction = Action_Help;
  3086. bChanged = true;
  3087. // Force it to shoot right away.
  3088. m_lTimer = 0;
  3089. break;
  3090. // In these cases, if a pylon of their type is detected, it
  3091. // will switch them to the correct state, otherwise it will leave
  3092. // the action and the state unchanged.
  3093. case Action_Popout:
  3094. Logic_PylonDetect();
  3095. if (m_bPylonPopoutAvailable)
  3096. {
  3097. m_pPylonEnd = m_pPylonStart->GetPylon(m_pPylonStart->m_msg.msg_Popout.ucIDNext);
  3098. ASSERT(m_pPylonEnd != NULL);
  3099. m_sNextX = m_pPylonStart->GetX();
  3100. m_sNextZ = m_pPylonStart->GetZ();
  3101. m_state = State_PopBegin;
  3102. m_eCurrentAction = Action_Popout;
  3103. bChanged = true;
  3104. }
  3105. break;
  3106. case Action_RunShoot:
  3107. Logic_PylonDetect();
  3108. if (m_bPylonRunShootAvailable)
  3109. {
  3110. m_pPylonEnd = m_pPylonStart->GetPylon(m_pPylonStart->m_msg.msg_ShootCycle.ucIDNext);
  3111. ASSERT(m_pPylonEnd != NULL);
  3112. m_sNextX = m_pPylonStart->GetX();
  3113. m_sNextZ = m_pPylonStart->GetZ();
  3114. m_state = State_RunShootBegin;
  3115. m_eCurrentAction = Action_RunShoot;
  3116. bChanged = true;
  3117. }
  3118. break;
  3119. case Action_Hide:
  3120. Logic_PylonDetect();
  3121. if (m_bPylonSafeAvailable)
  3122. {
  3123. m_sNextX = m_pPylonStart->GetX();
  3124. m_sNextZ = m_pPylonStart->GetZ();
  3125. m_state = State_HideBegin;
  3126. m_eCurrentAction = Action_Hide;
  3127. bChanged = true;
  3128. }
  3129. break;
  3130. }
  3131. }
  3132. return bChanged;
  3133. }
  3134. ////////////////////////////////////////////////////////////////////////////////
  3135. // Message handlers
  3136. ////////////////////////////////////////////////////////////////////////////////
  3137. ////////////////////////////////////////////////////////////////////////////////
  3138. // Shot Message
  3139. ////////////////////////////////////////////////////////////////////////////////
  3140. void CDoofus::OnShotMsg(Shot_Message* pMessage)
  3141. {
  3142. // If he is already shot, just deduct hit points for
  3143. // the additional bullets
  3144. m_stockpile.m_sHitPoints -= pMessage->sDamage;
  3145. if (m_state != State_Burning &&
  3146. m_state != State_BlownUp &&
  3147. m_state != State_Die &&
  3148. m_state != State_Dead)
  3149. {
  3150. // Alert other in the area that you are being attacked.
  3151. YellForHelp();
  3152. // Set panic so that victims will panic after being shot
  3153. m_bPanic = true;
  3154. // If he got shot while he was writhing, then he was executed
  3155. // so switch to that animation.
  3156. if (m_state == State_Writhing)
  3157. {
  3158. m_stockpile.m_sHitPoints = 0;
  3159. m_panimCur = &m_animExecuted;
  3160. m_lAnimTime = 0;
  3161. AbortSample(m_siPlaying);
  3162. }
  3163. else
  3164. {
  3165. long lThisTime = m_pRealm->m_time.GetGameTime();
  3166. if (lThisTime > m_lShotTimeout)
  3167. {
  3168. m_lShotTimeout = lThisTime + m_lShotReactionTimeout;
  3169. PlaySoundShot();
  3170. if ( m_state != State_Shot &&
  3171. m_state != State_Writhing)
  3172. {
  3173. m_ePreviousState = m_state;
  3174. m_panimPrev = m_panimCur;
  3175. m_state = State_Shot;
  3176. m_panimCur = &m_animShot;
  3177. m_lAnimTime = 0;
  3178. }
  3179. }
  3180. // Give him time to escape and move without starting the full
  3181. // shot animation again
  3182. else
  3183. {
  3184. PlaySoundShot();
  3185. }
  3186. }
  3187. }
  3188. // Let's have some blood no matter what!
  3189. CCharacter::OnShotMsg(pMessage);
  3190. }
  3191. ////////////////////////////////////////////////////////////////////////////////
  3192. // Explosion message
  3193. ////////////////////////////////////////////////////////////////////////////////
  3194. void CDoofus::OnExplosionMsg(Explosion_Message* pMessage)
  3195. {
  3196. if (
  3197. m_state != State_BlownUp &&
  3198. m_state != State_Die &&
  3199. m_state != State_Dead)
  3200. {
  3201. CCharacter::OnExplosionMsg(pMessage);
  3202. // PlaySample(g_smidBlownupYell);
  3203. PlaySoundBlownup();
  3204. m_ePreviousState = m_state;
  3205. m_state = State_BlownUp;
  3206. m_panimPrev = m_panimCur;
  3207. m_panimCur = &m_animDie;
  3208. m_lAnimTime = 0;
  3209. m_stockpile.m_sHitPoints = 0;
  3210. m_lTimer = m_pRealm->m_time.GetGameTime() + 6000;
  3211. GameMessage msg;
  3212. msg.msg_Explosion.eType = typeExplosion;
  3213. msg.msg_Explosion.sPriority = 0;
  3214. CThing* pDemon = m_pRealm->m_aclassHeads[CThing::CDemonID].GetNext();
  3215. if (pDemon)
  3216. SendThingMessage(&msg, pDemon);
  3217. // If he has a weapon ready, either get rid of it or shoot it off randomly
  3218. CWeapon* pweapon;
  3219. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  3220. {
  3221. // It should drop like a rock if its a throwing weapon or just delete it if it
  3222. // is a launched weapon
  3223. if (m_eWeaponType == CHeatseekerID || m_eWeaponType == CRocketID || m_eWeaponType == CNapalmID)
  3224. {
  3225. GameMessage msg;
  3226. msg.msg_ObjectDelete.eType = typeObjectDelete;
  3227. msg.msg_ObjectDelete.sPriority = 0;
  3228. SendThingMessage(&msg, pweapon);
  3229. }
  3230. else
  3231. {
  3232. pweapon->m_dHorizVel = (GetRandom() % (short) CGrenade::ms_dThrowHorizVel);
  3233. m_dShootAngle = pweapon->m_dRot = GetRandom() % 360;
  3234. ShootWeapon();
  3235. }
  3236. }
  3237. }
  3238. }
  3239. ////////////////////////////////////////////////////////////////////////////////
  3240. // Burning message
  3241. ////////////////////////////////////////////////////////////////////////////////
  3242. void CDoofus::OnBurnMsg(Burn_Message* pMessage)
  3243. {
  3244. CCharacter::OnBurnMsg(pMessage);
  3245. m_stockpile.m_sHitPoints -= pMessage->sDamage;
  3246. if (m_state != State_Burning &&
  3247. m_state != State_BlownUp &&
  3248. m_state != State_Die &&
  3249. m_state != State_Dead)
  3250. {
  3251. PlaySoundBurning();
  3252. if (m_state == State_Writhing)
  3253. {
  3254. m_sBrightness = BURNT_BRIGHTNESS;
  3255. // May also have to create a small fire at this position
  3256. m_stockpile.m_sHitPoints = 0;
  3257. m_state = State_Dead;
  3258. }
  3259. else
  3260. {
  3261. m_ePreviousState = m_state;
  3262. m_state = State_Burning;
  3263. // If he's not on fire already, switch to on fire animation
  3264. if (m_panimCur != &m_animOnfire)
  3265. {
  3266. m_panimPrev = m_panimCur;
  3267. m_panimCur = &m_animOnfire;
  3268. m_lAnimTime = 0;
  3269. GameMessage msg;
  3270. msg.msg_Burn.eType = typeBurn;
  3271. msg.msg_Burn.sPriority = 0;
  3272. CThing* pDemon = m_pRealm->m_aclassHeads[CThing::CDemonID].GetNext();
  3273. if (pDemon)
  3274. SendThingMessage(&msg, pDemon);
  3275. }
  3276. }
  3277. // If he has a weapon ready, either get rid of it or shoot it off randomly.
  3278. CWeapon* pweapon;
  3279. if (m_pRealm->m_idbank.GetThingByID((CThing**)&pweapon, m_u16IdWeapon) == 0)
  3280. {
  3281. // It should drop like a rock if its a throwing weapon or just delete it if it
  3282. // is a launched weapon
  3283. if (m_eWeaponType == CHeatseekerID || m_eWeaponType == CRocketID || m_eWeaponType == CNapalmID)
  3284. {
  3285. GameMessage msg;
  3286. msg.msg_ObjectDelete.eType = typeObjectDelete;
  3287. msg.msg_ObjectDelete.sPriority = 0;
  3288. SendThingMessage(&msg, pweapon);
  3289. }
  3290. else
  3291. {
  3292. pweapon->m_dHorizVel = (GetRandom() % (short) CGrenade::ms_dThrowHorizVel);
  3293. m_dShootAngle = pweapon->m_dRot = GetRandom() % 360;
  3294. ShootWeapon();
  3295. }
  3296. }
  3297. }
  3298. }
  3299. ////////////////////////////////////////////////////////////////////////////////
  3300. // Handles an ObjectDelete_Message.
  3301. // (virtual).
  3302. ////////////////////////////////////////////////////////////////////////////////
  3303. void CDoofus::OnDeleteMsg( // Returns nothing.
  3304. ObjectDelete_Message* pdeletemsg) // In: Message to handle.
  3305. {
  3306. CCharacter::OnDeleteMsg(pdeletemsg);
  3307. }
  3308. ////////////////////////////////////////////////////////////////////////////////
  3309. // Handles a Help_Message
  3310. // (virtual)
  3311. ////////////////////////////////////////////////////////////////////////////////
  3312. void CDoofus::OnHelpMsg( // Returns nothing
  3313. Help_Message* phelpmsg) // In: Message to handle
  3314. {
  3315. m_lLastHelpCallTime = m_pRealm->m_time.GetGameTime();
  3316. // Victium take the yell for help as a indication to panic.
  3317. m_bPanic = true;
  3318. }
  3319. ////////////////////////////////////////////////////////////////////////////////
  3320. // OnDead - override of CCharacter version
  3321. ////////////////////////////////////////////////////////////////////////////////
  3322. void CDoofus::OnDead(void)
  3323. {
  3324. CCharacter::OnDead();
  3325. }
  3326. ////////////////////////////////////////////////////////////////////////////////
  3327. // Prepare current weapon (ammo).
  3328. // This should be done when the character starts its shoot animation.
  3329. // (virtual (overriden here) ).
  3330. ////////////////////////////////////////////////////////////////////////////////
  3331. CWeapon* CDoofus::PrepareWeapon(void) // Returns the weapon ptr or NULL.
  3332. {
  3333. // Play sound even if we have no weapon...seems like they're threatening him.
  3334. PlaySoundShooting();
  3335. CWeapon* pweapon;
  3336. // If we have a weapon . . .
  3337. if (m_eWeaponType != TotalIDs)
  3338. {
  3339. pweapon = CCharacter::PrepareWeapon();
  3340. }
  3341. else
  3342. {
  3343. pweapon = NULL;
  3344. }
  3345. return pweapon;
  3346. };
  3347. ////////////////////////////////////////////////////////////////////////////////
  3348. // ShootWeapon - Overloaded version to use the Shooting Angle
  3349. ////////////////////////////////////////////////////////////////////////////////
  3350. CWeapon* CDoofus::ShootWeapon(CSmash::Bits bitsInclude,
  3351. CSmash::Bits bitsDontcare,
  3352. CSmash::Bits bitsExclude)
  3353. {
  3354. CWeapon* pWeapon = NULL;
  3355. double dSaveDirection = m_dRot;
  3356. // Don't adjust shooting angle if shooting on the run
  3357. if (m_state != State_ShootRun)
  3358. {
  3359. // Tuned for difficulty level - if the game is in the harder
  3360. // levels, the enemies will tune their aim at the last second
  3361. // before shooting, and then also depending on the difficulty,
  3362. // they will add different amounts of random misses
  3363. // Easy levels = 1, 2, 3
  3364. // Medium levels = 4, 5, 6
  3365. // Hard levels = 7, 8, 9, 10, 11
  3366. switch (m_pRealm->m_flags.sDifficulty)
  3367. {
  3368. case 0:
  3369. case 1:
  3370. m_dRot = rspMod360(m_dShootAngle - 8 + (GetRandom() % 17));
  3371. break;
  3372. case 2:
  3373. m_dRot = rspMod360(m_dShootAngle - 7 + (GetRandom() % 15));
  3374. break;
  3375. case 3:
  3376. m_dRot = rspMod360(m_dShootAngle - 6 + (GetRandom() % 13));
  3377. break;
  3378. case 4:
  3379. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 6 + (GetRandom() % 13));
  3380. break;
  3381. case 5:
  3382. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 5 + (GetRandom() % 11));
  3383. break;
  3384. case 6:
  3385. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 4 + (GetRandom() % 9));
  3386. break;
  3387. case 7:
  3388. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 6 + (GetRandom() % 13));
  3389. break;
  3390. case 8:
  3391. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 5 + (GetRandom() % 11));
  3392. break;
  3393. case 9:
  3394. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 4 + (GetRandom() % 9));
  3395. break;
  3396. case 10:
  3397. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = rspMod360(FindDirection() - 3 + (GetRandom() % 7));
  3398. break;
  3399. case 11:
  3400. default:
  3401. m_dRot = m_dAnimRot = m_dShootAngle = dSaveDirection = FindDirection();
  3402. break;
  3403. }
  3404. }
  3405. else
  3406. {
  3407. m_dRot = m_dShootAngle;
  3408. }
  3409. /*
  3410. // Allow rockets to hit other enemies, but still not the special barrel type
  3411. if (m_eWeaponType == CHeatseekerID || m_eWeaponType == CRocketID)
  3412. {
  3413. bitsExclude &= ~CSmash::Bad;
  3414. }
  3415. */
  3416. pWeapon = CCharacter::ShootWeapon(bitsInclude, bitsDontcare, bitsExclude);
  3417. if (pWeapon)
  3418. pWeapon->SetDetectionBits(CSmash::Character | CSmash::Fire,
  3419. 0,
  3420. CSmash::Bad | CSmash::Ducking | CSmash::AlmostDead);
  3421. m_dRot = dSaveDirection;
  3422. return pWeapon;
  3423. }
  3424. ////////////////////////////////////////////////////////////////////////////////
  3425. // RunIdleAnimation - Check the idle timer and pick an idle animation to run
  3426. ////////////////////////////////////////////////////////////////////////////////
  3427. void CDoofus::RunIdleAnimation(void)
  3428. {
  3429. long lThisTime = m_pRealm->m_time.GetGameTime();
  3430. if (m_panimCur == &m_animStand)
  3431. {
  3432. if (lThisTime > m_lIdleTimer)
  3433. {
  3434. if (GetRandom() % 3 == 0)
  3435. {
  3436. // Stay with current animation
  3437. m_lIdleTimer = lThisTime + 2000 + GetRandom() % 2000;
  3438. }
  3439. else
  3440. {
  3441. // Go to the Crouch animaiton
  3442. m_panimCur = &m_animCrouch;
  3443. m_lAnimTime = 0;
  3444. m_bAnimUp = true;
  3445. m_lIdleTimer = lThisTime + 3000 + GetRandom() % 2000;
  3446. }
  3447. }
  3448. }
  3449. else
  3450. {
  3451. if (m_panimCur == &m_animCrouch)
  3452. {
  3453. // Animate forward or backward depending on if he is standing up or
  3454. if (m_bAnimUp)
  3455. {
  3456. m_lAnimTime += lThisTime - m_lPrevTime;
  3457. // See if it is time to switch yet
  3458. if (lThisTime > m_lIdleTimer)
  3459. {
  3460. if ((GetRandom() % 3 ) == 0)
  3461. // Start standing up to use the stand animation
  3462. {
  3463. // Start to stand up - run the animation backwards
  3464. m_lAnimTime = m_panimCur->m_psops->TotalTime();
  3465. m_bAnimUp = false;
  3466. }
  3467. else
  3468. {
  3469. // Pick either crouch still or search
  3470. if (GetRandom() % 2)
  3471. {
  3472. // Start search animation
  3473. m_panimCur = &m_animSearch;
  3474. m_lAnimTime = 0;
  3475. m_lIdleTimer = lThisTime + 1000 + GetRandom() % 1500;
  3476. }
  3477. else
  3478. {
  3479. // Stay with still crouch animation
  3480. m_lIdleTimer = lThisTime + 2000 + GetRandom() % 2000;
  3481. }
  3482. }
  3483. }
  3484. }
  3485. else
  3486. {
  3487. m_lAnimTime -= lThisTime - m_lPrevTime;
  3488. // See if it is done standing yet. If so, you have
  3489. // already previously decided to end the crouch animation
  3490. // so now pick the stand animation
  3491. if (m_lAnimTime < 0)
  3492. {
  3493. m_panimCur = &m_animStand;
  3494. m_lAnimTime = 0;
  3495. m_lIdleTimer = lThisTime + 3000 + GetRandom() % 2000;
  3496. }
  3497. }
  3498. }
  3499. else
  3500. {
  3501. if (m_panimCur == &m_animSearch)
  3502. {
  3503. // Animate the search
  3504. m_lAnimTime += lThisTime - m_lPrevTime;
  3505. // See if its time to switch yet
  3506. if (lThisTime > m_lIdleTimer)
  3507. {
  3508. switch (GetRandom() % 3)
  3509. {
  3510. case 0: // Stay with current animation
  3511. m_lIdleTimer = lThisTime + 1000 + GetRandom() % 1000;
  3512. break;
  3513. case 1: // Go to Crouch animation
  3514. m_panimCur = &m_animCrouch;
  3515. m_bAnimUp = true;
  3516. m_lIdleTimer = lThisTime + 3000 + GetRandom() % 2000;
  3517. break;
  3518. case 2: // Go to stand - use Crouch backwards to get up
  3519. m_panimCur = &m_animCrouch;
  3520. m_bAnimUp = false;
  3521. m_lAnimTime = m_panimCur->m_psops->TotalTime();
  3522. m_lIdleTimer = lThisTime + 3000 + GetRandom() % 2500;
  3523. break;
  3524. }
  3525. }
  3526. }
  3527. // Start the stand animation since it isn't currently using any idle animations.
  3528. else
  3529. {
  3530. m_panimCur = &m_animStand;
  3531. m_lIdleTimer = lThisTime + 3000;
  3532. m_lAnimTime = 0;
  3533. }
  3534. }
  3535. }
  3536. }
  3537. ////////////////////////////////////////////////////////////////////////////////
  3538. // Position our smash approriately.
  3539. // (virtual).
  3540. ////////////////////////////////////////////////////////////////////////////////
  3541. void CDoofus::PositionSmash(void)
  3542. {
  3543. // If not in writhing state . . .
  3544. if (m_state != State_Writhing)
  3545. {
  3546. // Update sphere.
  3547. m_smash.m_sphere.sphere.X = m_dX;
  3548. m_smash.m_sphere.sphere.Y = m_dY + m_sprite.m_sRadius;
  3549. m_smash.m_sphere.sphere.Z = m_dZ;
  3550. m_smash.m_sphere.sphere.lRadius = m_sprite.m_sRadius;
  3551. }
  3552. else
  3553. {
  3554. // If we have the execution points . . .
  3555. if (m_ptransExecutionTarget)
  3556. {
  3557. // Compute link point for execution target.
  3558. double dVitalOrganX;
  3559. double dVitalOrganY;
  3560. double dVitalOrganZ;
  3561. GetLinkPoint( // Returns nothing.
  3562. m_ptransExecutionTarget->GetAtTime(m_lAnimTime), // In: Transform specifying point.
  3563. &dVitalOrganX, // Out: Point speicfied.
  3564. &dVitalOrganY, // Out: Point speicfied.
  3565. &dVitalOrganZ); // Out: Point speicfied. // Update execution point via link point.
  3566. // Offset from hotspot to set collision sphere position.
  3567. m_smash.m_sphere.sphere.X = m_dX + dVitalOrganX;
  3568. m_smash.m_sphere.sphere.Y = m_dY + dVitalOrganY;
  3569. m_smash.m_sphere.sphere.Z = m_dZ + dVitalOrganZ;
  3570. m_smash.m_sphere.sphere.lRadius = m_sprite.m_sRadius;
  3571. }
  3572. else
  3573. {
  3574. ASSERT(m_dRot >= 0);
  3575. ASSERT(m_dRot < 360);
  3576. // Try to find center.
  3577. // Let's go a radius up their torso. Say... .
  3578. // This only looks decent if m_dRot is the direction they fell which is
  3579. // not always the case.
  3580. short sPseudoCenter = m_sprite.m_sRadius;
  3581. m_smash.m_sphere.sphere.X = m_dX + COSQ[short(m_dRot)] * sPseudoCenter;
  3582. m_smash.m_sphere.sphere.Y = m_dY + m_sprite.m_sRadius;
  3583. m_smash.m_sphere.sphere.Z = m_dZ - SINQ[short(m_dRot)] * sPseudoCenter;
  3584. m_smash.m_sphere.sphere.lRadius = m_sprite.m_sRadius;
  3585. }
  3586. }
  3587. }
  3588. ////////////////////////////////////////////////////////////////////////////////
  3589. // WhileHoldingWeapon
  3590. //
  3591. // Override for the character version which will just re-aim the guy during the
  3592. // shoot-prepare frames. Otherwise, guys like the rocketman which has a long
  3593. // prepare animation, quickly flip around in ShootWeapon if the target has moved
  3594. // between PrepareWeapon and ShootWeapon.
  3595. //
  3596. ////////////////////////////////////////////////////////////////////////////////
  3597. bool CDoofus::WhileHoldingWeapon( // Returns true when weapon is released.
  3598. U32 u32BitsInclude, // In: Collision bits passed to ShootWeapon
  3599. U32 u32BitsDontcare, // In: Collision bits passed to ShootWeapon
  3600. U32 u32BitsExclude) // In: Collision bits passed to ShootWeapon
  3601. {
  3602. bool bResult = true;
  3603. // If they haven't shot the weapon yet, adjust their aiming if the difficulty
  3604. // level allows them to re-aim after preparing the weapon
  3605. if (!(CCharacter::WhileHoldingWeapon(u32BitsInclude, u32BitsDontcare, u32BitsExclude)))
  3606. {
  3607. bResult = false;
  3608. // Don't adjust shooting angle if shooting on the run
  3609. if (m_state != State_ShootRun)
  3610. {
  3611. // Tuned for difficulty level - if the game is in the harder
  3612. // levels, the enemies will tune their aim at the last second
  3613. // before shooting, and then also depending on the difficulty,
  3614. // they will add different amounts of random misses
  3615. // Easy levels = 1, 2, 3
  3616. // Medium levels = 4, 5, 6
  3617. // Hard levels = 7, 8, 9, 10, 11
  3618. switch (m_pRealm->m_flags.sDifficulty)
  3619. {
  3620. // Easy levels don't re-aim after preparing weapon, so leve it alone
  3621. case 0:
  3622. case 1:
  3623. case 2:
  3624. case 3:
  3625. case 4:
  3626. case 5:
  3627. case 6:
  3628. break;
  3629. // More difficult levels allow constant re-aiming.
  3630. case 7:
  3631. case 8:
  3632. case 9:
  3633. case 10:
  3634. case 11:
  3635. default:
  3636. m_dRot = m_dAnimRot = m_dShootAngle = rspMod360(FindDirection());
  3637. break;
  3638. }
  3639. }
  3640. }
  3641. return bResult;
  3642. }
  3643. ////////////////////////////////////////////////////////////////////////////////
  3644. // A way for the base class to get resources. If you are going to use
  3645. // any of this class's resources (e.g., ms_aanimWeapons[]), call this
  3646. // when getting your resources.
  3647. ////////////////////////////////////////////////////////////////////////////////
  3648. short CDoofus::GetResources(void)
  3649. {
  3650. short sResult = 0;
  3651. // If the ref count was 0 . . .
  3652. if (ms_lWeaponResRefCount++ == 0)
  3653. {
  3654. // Get the actual resources.
  3655. short i;
  3656. short sLoadResult;
  3657. for (i = 0; i < NumWeaponTypes; i++)
  3658. {
  3659. // If this weapon has a visible resource . . .
  3660. if (ms_awdWeapons[i].pszResName)
  3661. {
  3662. sLoadResult = ms_aanimWeapons[i].Get(
  3663. ms_awdWeapons[i].pszResName,
  3664. NULL,
  3665. NULL,
  3666. NULL,
  3667. RChannel_LoopAtStart | RChannel_LoopAtEnd);
  3668. if (sLoadResult != 0)
  3669. {
  3670. TRACE("GetResources(): Failed to load weapon resource \"%s\".\n",
  3671. ms_awdWeapons[i].pszResName);
  3672. sResult = -1;
  3673. }
  3674. }
  3675. }
  3676. }
  3677. return sResult;
  3678. }
  3679. ////////////////////////////////////////////////////////////////////////////////
  3680. // A way for the base class to release resources. If you are going to use
  3681. // any of this class's resources (e.g., ms_aanimWeapons[]), call this
  3682. // when releasing your resources.
  3683. ////////////////////////////////////////////////////////////////////////////////
  3684. void CDoofus::ReleaseResources(void)
  3685. {
  3686. ASSERT(ms_lWeaponResRefCount > 0);
  3687. // If the ref count hits zero with this release . . .
  3688. if (--ms_lWeaponResRefCount == 0)
  3689. {
  3690. // Release the actual resources.
  3691. short i;
  3692. for (i = 0; i < NumWeaponTypes; i++)
  3693. {
  3694. if (ms_aanimWeapons[i].m_pmeshes)
  3695. {
  3696. ms_aanimWeapons[i].Release();
  3697. }
  3698. }
  3699. }
  3700. }
  3701. ////////////////////////////////////////////////////////////////////////////////
  3702. // EOF
  3703. ////////////////////////////////////////////////////////////////////////////////