UNIT.CPP 208 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064
  1. /*
  2. ** Command & Conquer Red Alert(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /CounterStrike/UNIT.CPP 1 3/03/97 10:26a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : UNIT.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 10, 1993 *
  30. * *
  31. * Last Update : November 3, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * Recoil_Adjust -- Adjust pixel values in direction specified. *
  36. * UnitClass::AI -- AI processing for the unit. *
  37. * UnitClass::APC_Close_Door -- Closes an APC door. *
  38. * UnitClass::APC_Open_Door -- Opens an APC door. *
  39. * UnitClass::Active_Click_With -- Intercepts the active click to see if deployment is possib*
  40. * UnitClass::Active_Click_With -- Performs specified action on specified cell. *
  41. * UnitClass::Approach_Target -- Handles approaching the target in order to attack it. *
  42. * UnitClass::Assign_Destination -- Assign a destination to a unit. *
  43. * UnitClass::Blocking_Object -- Determines how a object blocks a unit *
  44. * UnitClass::Can_Enter_Cell -- Determines cell entry legality. *
  45. * UnitClass::Can_Fire -- Determines if turret can fire upon target. *
  46. * UnitClass::Click_With -- Handles player map clicking while this unit is selected. *
  47. * UnitClass::Credit_Load -- Fetch the full credit value of cargo carried. *
  48. * UnitClass::Crew_Type -- Fetches the kind of crew that this object produces. *
  49. * UnitClass::Debug_Dump -- Displays the status of the unit to the mono monitor. *
  50. * UnitClass::Desired_Load_Dir -- Determines the best cell and facing for loading. *
  51. * UnitClass::Draw_It -- Draws a unit object. *
  52. * UnitClass::Edge_Of_World_AI -- Check for falling off the edge of the world. *
  53. * UnitClass::Enter_Idle_Mode -- Unit enters idle mode state. *
  54. * UnitClass::Fire_Direction -- Determines the direction of firing. *
  55. * UnitClass::Firing_AI -- Handle firing logic for this unit. *
  56. * UnitClass::Flag_Attach -- Attaches a house flag to this unit. *
  57. * UnitClass::Flag_Remove -- Removes the house flag from this unit. *
  58. * UnitClass::Goto_Clear_Spot -- Finds a clear spot to deploy. *
  59. * UnitClass::Goto_Tiberium -- Search for and head toward nearest available Tiberium patch. *
  60. * UnitClass::Greatest_Threat -- Fetches the greatest threat for this unit. *
  61. * UnitClass::Harvesting -- Harvests tiberium at the current location. *
  62. * UnitClass::Init -- Clears all units for scenario preparation. *
  63. * UnitClass::Limbo -- Limbo this unit. *
  64. * UnitClass::Mission_Guard -- Special guard mission override processor. *
  65. * UnitClass::Mission_Guard_Area -- Guard area logic for units. *
  66. * UnitClass::Mission_Harvest -- Handles the harvesting process used by harvesters. *
  67. * UnitClass::Mission_Hunt -- This is the AI process for aggressive enemy units. *
  68. * UnitClass::Mission_Move -- Handles special move mission overrides. *
  69. * UnitClass::Mission_Repair -- Handles finding and proceeding on a repair mission. *
  70. * UnitClass::Mission_Unload -- Handles unloading cargo. *
  71. * UnitClass::Offload_Tiberium_Bail -- Offloads one Tiberium quantum from the object. *
  72. * UnitClass::Ok_To_Move -- Queries whether the vehicle can move. *
  73. * UnitClass::Overlap_List -- Determines overlap list for units. *
  74. * UnitClass::Overrun_Square -- Handles vehicle overrun of a cell. *
  75. * UnitClass::Per_Cell_Process -- Performs operations necessary on a per cell basis. *
  76. * UnitClass::Pip_Count -- Fetches the number of pips to display on unit. *
  77. * UnitClass::Random_Animate -- Handles random idle animation for the unit. *
  78. * UnitClass::Read_INI -- Reads units from scenario INI file. *
  79. * UnitClass::Receive_Message -- Handles receiving a radio message. *
  80. * UnitClass::Reload_AI -- Perform reload logic for this unit. *
  81. * UnitClass::Rotation_AI -- Process any turret or body rotation. *
  82. * UnitClass::Scatter -- Causes the unit to scatter to a nearby location. *
  83. * UnitClass::Set_Speed -- Initiate unit movement physics. *
  84. * UnitClass::Shape_Number -- Fetch the shape number to use for this unit. *
  85. * UnitClass::Should_Crush_It -- Determines if this unit should crush an object. *
  86. * UnitClass::Sort_Y -- Give Y coordinate sort value for unit. *
  87. * UnitClass::Start_Driver -- Starts driving and reserves destination cell. *
  88. * UnitClass::Take_Damage -- Inflicts damage points on a unit. *
  89. * UnitClass::Tiberium_Check -- Search for and head toward nearest available Tiberium patch. *
  90. * UnitClass::Tiberium_Load -- Determine the Tiberium load as a percentage. *
  91. * UnitClass::Try_To_Deploy -- The unit attempts to "deploy" at current location. *
  92. * UnitClass::UnitClass -- Constructor for units. *
  93. * UnitClass::Unlimbo -- Removes unit from stasis. *
  94. * UnitClass::What_Action -- Determines action to perform on specified cell. *
  95. * UnitClass::What_Action -- Determines what action would occur if clicked on object. *
  96. * UnitClass::Write_INI -- Store the units to the INI database. *
  97. * UnitClass::delete -- Deletion operator for units. *
  98. * UnitClass::new -- Allocate a unit slot and adjust access arrays. *
  99. * UnitClass::~UnitClass -- Destructor for unit objects. *
  100. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  101. #include "function.h"
  102. /***********************************************************************************************
  103. * Recoil_Adjust -- Adjust pixel values in direction specified. *
  104. * *
  105. * This is a helper routine that modifies the pixel coordinates provided according to the *
  106. * direction specified. The effect is the simulate recoil effects by moving an object 'back'*
  107. * one pixel. Since the pixels moved depend on facing, this routine handles the pixel *
  108. * adjustment quickly. *
  109. * *
  110. * INPUT: dir -- The direction to base the recoil on. *
  111. * *
  112. * x,y -- References to the pixel coordinates that will be adjusted. *
  113. * *
  114. * OUTPUT: none *
  115. * *
  116. * WARNINGS: none *
  117. * *
  118. * HISTORY: *
  119. * 05/08/1995 JLB : Created. *
  120. *=============================================================================================*/
  121. void Recoil_Adjust(DirType dir, int &x, int &y)
  122. {
  123. static struct {
  124. signed char X,Y;
  125. } _adjust[32] = {
  126. {0, 1}, // N
  127. {0, 1},
  128. {0, 1},
  129. {-1, 1},
  130. {-1, 1}, // NE
  131. {-1, 1},
  132. {-1, 0},
  133. {-1, 0},
  134. {-1, 0}, // E
  135. {-1, 0},
  136. {-1, -1},
  137. {-1, -1},
  138. {-1, -1}, // SE
  139. {-1, -1},
  140. {-1, -1},
  141. {0, -1},
  142. {0, -1}, // S
  143. {0, -1},
  144. {0, -1},
  145. {1, -1},
  146. {1, -1}, // SW
  147. {1, -1},
  148. {1, 0},
  149. {1, 0},
  150. {1, 0}, // W
  151. {1, 0},
  152. {1, 1},
  153. {1, 1},
  154. {1, 1}, // NW
  155. {1, 1},
  156. {0, 1},
  157. {0, 1}
  158. };
  159. int index = Dir_To_32(dir);
  160. x += _adjust[index].X;
  161. y += _adjust[index].Y;
  162. }
  163. /***********************************************************************************************
  164. * UnitClass::new -- Allocate a unit slot and adjust access arrays. *
  165. * *
  166. * This routine will allocate a unit from the available unit pool and *
  167. * fixup all the access lists to match. It will allocate a unit slot *
  168. * from within the range allowed for the specified unit type. If no *
  169. * slot was found, then it will fail. *
  170. * *
  171. * INPUT: none *
  172. * *
  173. * OUTPUT: Returns with a pointer to the allocated unit. *
  174. * *
  175. * WARNINGS: none *
  176. * *
  177. * HISTORY: *
  178. * 04/11/1994 JLB : Created. *
  179. * 04/21/1994 JLB : Converted to operator new. *
  180. *=============================================================================================*/
  181. void * UnitClass::operator new(size_t)
  182. {
  183. void * ptr = Units.Alloc();
  184. if (ptr != NULL) {
  185. ((UnitClass *)ptr)->IsActive = true;
  186. }
  187. return(ptr);
  188. }
  189. /***********************************************************************************************
  190. * UnitClass::delete -- Deletion operator for units. *
  191. * *
  192. * This removes the unit from the local allocation system. Since this *
  193. * is a fixed block of memory, not much has to be done to delete the *
  194. * unit. Merely marking it as inactive is enough. *
  195. * *
  196. * INPUT: ptr -- Pointer to the unit to delete. *
  197. * *
  198. * OUTPUT: none *
  199. * *
  200. * WARNINGS: none *
  201. * *
  202. * HISTORY: *
  203. * 04/21/1994 JLB : Created. *
  204. *=============================================================================================*/
  205. void UnitClass::operator delete(void * ptr)
  206. {
  207. if (ptr != NULL) {
  208. ((UnitClass *)ptr)->IsActive = false;
  209. }
  210. Units.Free((UnitClass *)ptr);
  211. }
  212. /***********************************************************************************************
  213. * UnitClass::~UnitClass -- Destructor for unit objects. *
  214. * *
  215. * This destructor will lower the unit count for the owning house as well as inform any *
  216. * other units in communication, that this unit is about to leave reality. *
  217. * *
  218. * INPUT: none *
  219. * *
  220. * OUTPUT: none *
  221. * *
  222. * WARNINGS: none *
  223. * *
  224. * HISTORY: *
  225. * 08/15/1994 JLB : Created. *
  226. *=============================================================================================*/
  227. UnitClass::~UnitClass(void)
  228. {
  229. if (GameActive && Class.Is_Valid()) {
  230. /*
  231. ** Remove this member from any team it may be associated with. This must occur at the
  232. ** top most level of the inheritance hierarchy because it may call virtual functions.
  233. */
  234. if (Team.Is_Valid()) {
  235. Team->Remove(this);
  236. Team = NULL;
  237. }
  238. House->Tracking_Remove(this);
  239. /*
  240. ** If there are any cargo members, delete them.
  241. */
  242. while (Is_Something_Attached()) {
  243. delete Detach_Object();
  244. }
  245. Limbo();
  246. }
  247. ID = -1;
  248. }
  249. /***********************************************************************************************
  250. * UnitClass::UnitClass -- Constructor for units. *
  251. * *
  252. * This constructor for units will initialize the unit into the game *
  253. * system. It will be placed in all necessary tracking lists. The initial condition will *
  254. * be in a state of limbo. *
  255. * *
  256. * INPUT: classid -- The type of unit to create. *
  257. * *
  258. * house -- The house owner of this unit. *
  259. * *
  260. * OUTPUT: none *
  261. * *
  262. * WARNINGS: none *
  263. * *
  264. * HISTORY: *
  265. * 04/21/1994 JLB : Created. *
  266. *=============================================================================================*/
  267. UnitClass::UnitClass(UnitType classid, HousesType house) :
  268. DriveClass(RTTI_UNIT, Units.ID(this), house),
  269. Class(UnitTypes.Ptr((int)classid)),
  270. Flagged(HOUSE_NONE),
  271. IsDumping(false),
  272. Gems(0),
  273. Gold(0),
  274. Tiberium(0),
  275. IsToScatter(false),
  276. ShroudBits(0xFFFFFFFFUL),
  277. ShroudCenter(0),
  278. Reload(0),
  279. SecondaryFacing(PrimaryFacing)
  280. {
  281. Reload = 0;
  282. House->Tracking_Add(this);
  283. Ammo = Class->MaxAmmo;
  284. IsCloakable = Class->IsCloakable;
  285. if (Class->IsAnimating) Set_Rate(Options.Normalize_Delay(3));
  286. /*
  287. ** For two shooters, clear out the second shot flag -- it will be set the first time
  288. ** the object fires. For non two shooters, set the flag since it will never be cleared
  289. ** and the second shot flag tells the system that normal rearm times apply -- this is
  290. ** what is desired for non two shooters.
  291. */
  292. IsSecondShot = !Class->Is_Two_Shooter();
  293. Strength = Class->MaxStrength;
  294. /*
  295. ** Keep count of the number of units created.
  296. */
  297. // if (Session.Type == GAME_INTERNET) {
  298. // House->UnitTotals->Increment_Unit_Total((int)classid);
  299. // }
  300. }
  301. #ifdef CHEAT_KEYS
  302. /***********************************************************************************************
  303. * UnitClass::Debug_Dump -- Displays the status of the unit to the mono monitor. *
  304. * *
  305. * This displays the current status of the unit class to the mono monitor. By this display *
  306. * bugs may be tracked down or prevented. *
  307. * *
  308. * INPUT: none *
  309. * *
  310. * OUTPUT: none *
  311. * *
  312. * WARNINGS: none *
  313. * *
  314. * HISTORY: *
  315. * 06/02/1994 JLB : Created. *
  316. *=============================================================================================*/
  317. void UnitClass::Debug_Dump(MonoClass * mono) const
  318. {
  319. assert(Units.ID(this) == ID);
  320. assert(IsActive);
  321. mono->Set_Cursor(0, 0);
  322. mono->Print(Text_String(TXT_DEBUG_VEHICLE));
  323. mono->Set_Cursor(47, 5);mono->Printf("%02X:%02X", SecondaryFacing.Current(), SecondaryFacing.Desired());
  324. mono->Set_Cursor(1, 11);mono->Printf("%03", Gems);
  325. mono->Set_Cursor(7, 11);mono->Printf("%03", Gold);
  326. mono->Fill_Attrib(66, 13, 12, 1, IsDumping ? MonoClass::INVERSE : MonoClass::NORMAL);
  327. DriveClass::Debug_Dump(mono);
  328. }
  329. #endif
  330. /***********************************************************************************************
  331. * UnitClass::Sort_Y -- Give Y coordinate sort value for unit. *
  332. * *
  333. * This routine is used by the rendering system in order to sort the *
  334. * game objects in a back to front order. This is now the correct *
  335. * overlap effect is achieved. *
  336. * *
  337. * INPUT: none *
  338. * *
  339. * OUTPUT: Returns with a coordinate value that can be used for sorting. *
  340. * *
  341. * WARNINGS: none *
  342. * *
  343. * HISTORY: *
  344. * 05/17/1994 JLB : Created. *
  345. *=============================================================================================*/
  346. COORDINATE UnitClass::Sort_Y(void) const
  347. {
  348. assert(Units.ID(this) == ID);
  349. assert(IsActive);
  350. return(Coord_Add(Coord, 0x00800000L));
  351. }
  352. /***********************************************************************************************
  353. * UnitClass::AI -- AI processing for the unit. *
  354. * *
  355. * This routine will perform the AI processing necessary for the unit. These are non- *
  356. * graphic related operations. *
  357. * *
  358. * INPUT: none *
  359. * *
  360. * OUTPUT: none *
  361. * *
  362. * WARNINGS: none *
  363. * *
  364. * HISTORY: *
  365. * 05/31/1994 JLB : Created. *
  366. *=============================================================================================*/
  367. void UnitClass::AI(void)
  368. {
  369. assert(Units.ID(this) == ID);
  370. assert(IsActive);
  371. /*
  372. ** Act on new orders if the unit is at a good position to do so.
  373. */
  374. if (Height == 0 && !IsDumping && !IsDriving && Is_Door_Closed() /*&& Mission != MISSION_UNLOAD*/) {
  375. // if (MissionQueue == MISSION_NONE) Enter_Idle_Mode();
  376. Commence();
  377. }
  378. DriveClass::AI();
  379. if (!IsActive || Height > 0) {
  380. return;
  381. }
  382. /*
  383. ** Hack check to ensure that a harvester won't harvest if it is not harvesting.
  384. */
  385. if (Mission != MISSION_HARVEST) {
  386. IsHarvesting = false;
  387. }
  388. /*
  389. ** Handle combat logic for this unit. It will determine if it has a target and
  390. ** if so, if conditions are favorable for firing. When conditions permit, the
  391. ** unit will fire upon its target.
  392. */
  393. Firing_AI();
  394. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  395. if (!IsActive) {
  396. return;
  397. }
  398. #endif
  399. /*
  400. ** Turret rotation processing. Handles rotating radar dish
  401. ** as well as conventional turrets if present. If no turret present, but
  402. ** it decides that the body should face its target, then body rotation
  403. ** would occur by this process as well.
  404. */
  405. Rotation_AI();
  406. /*
  407. ** Delete this unit if it finds itself off the edge of the map and it is in
  408. ** guard or other static mission mode.
  409. */
  410. if (Edge_Of_World_AI()) {
  411. return;
  412. }
  413. /*
  414. ** Units will reload every so often if they are under the burden of
  415. ** being required to reload between shots.
  416. */
  417. Reload_AI();
  418. /*
  419. ** Transporters require special logic handled here since there isn't a MISSION_WAIT_FOR_PASSENGERS
  420. ** mission that they can follow. Passenger loading is merely a part of their normal operation.
  421. */
  422. if (Class->Max_Passengers() > 0) {
  423. /*
  424. ** Double check that there is a passenger that is trying to load or unload.
  425. ** If not, then close the door.
  426. */
  427. if (!Is_Door_Closed() && Mission != MISSION_UNLOAD && Transmit_Message(RADIO_TRYING_TO_LOAD) != RADIO_ROGER) {
  428. APC_Close_Door();
  429. }
  430. }
  431. /*
  432. ** Don't start a new mission unless the vehicle is in the center of
  433. ** a cell (not driving) and the door (if any) is closed.
  434. */
  435. if (!IsDumping && !IsDriving && Is_Door_Closed()/*&& Mission != MISSION_UNLOAD*/) {
  436. Commence();
  437. }
  438. /*
  439. ** A cloaked object that is carrying the flag will always shimmer.
  440. */
  441. if (Cloak == CLOAKED && Flagged != HOUSE_NONE) {
  442. Do_Shimmer();
  443. }
  444. /*
  445. ** Mobile gap generators regenerate their gap every so often (just in case).
  446. */
  447. if (Class->IsGapper && !IsDriving && (Frame % TICKS_PER_SECOND) == 0) {
  448. Shroud_Regen();
  449. }
  450. }
  451. /***********************************************************************************************
  452. * UnitClass::Rotation_AI -- Process any turret or body rotation. *
  453. * *
  454. * This routine will handle the rotation logic for the unit's turret (if it has one) as *
  455. * well as its normal body shape. *
  456. * *
  457. * INPUT: none *
  458. * *
  459. * OUTPUT: none *
  460. * *
  461. * WARNINGS: none *
  462. * *
  463. * HISTORY: *
  464. * 07/30/1996 JLB : Created. *
  465. *=============================================================================================*/
  466. void UnitClass::Rotation_AI(void)
  467. {
  468. if (Target_Legal(TarCom) && !IsRotating) {
  469. DirType dir = Direction(TarCom);
  470. if (Class->IsTurretEquipped) {
  471. SecondaryFacing.Set_Desired(dir);
  472. } else {
  473. /*
  474. ** Non turret equipped vehicles will rotate their body to face the target only
  475. ** if the vehicle isn't currently moving or facing the correct direction. This
  476. ** applies only to tracked vehicles. Wheeled vehicles never rotate to face the
  477. ** target, since they aren't maneuverable enough.
  478. */
  479. if ((Class->Speed == SPEED_TRACK /* || *this == UNIT_BIKE */ ) && !Target_Legal(NavCom) && !IsDriving && PrimaryFacing.Difference(dir)) {
  480. PrimaryFacing.Set_Desired(dir);
  481. }
  482. }
  483. }
  484. if (Class->IsRadarEquipped) {
  485. Mark(MARK_CHANGE_REDRAW);
  486. SecondaryFacing.Set((DirType)(SecondaryFacing.Current() + 8));
  487. Mark(MARK_CHANGE_REDRAW);
  488. } else {
  489. IsRotating = false;
  490. if (Class->IsTurretEquipped) {
  491. if (IsTurretLockedDown) {
  492. SecondaryFacing.Set_Desired(PrimaryFacing.Current());
  493. }
  494. if (SecondaryFacing.Is_Rotating()) {
  495. Mark(MARK_CHANGE_REDRAW);
  496. if (SecondaryFacing.Rotation_Adjust(Class->ROT+1)) {
  497. Mark(MARK_CHANGE_REDRAW);
  498. }
  499. /*
  500. ** If no further rotation is necessary, flag that the rotation
  501. ** has stopped.
  502. */
  503. if (!Class->IsRadarEquipped) {
  504. IsRotating = SecondaryFacing.Is_Rotating();
  505. }
  506. } else {
  507. if (!IsTurretLockedDown && !Target_Legal(TarCom)) {
  508. if (!Target_Legal(NavCom)) {
  509. SecondaryFacing.Set_Desired(PrimaryFacing.Current());
  510. } else {
  511. SecondaryFacing.Set_Desired(Direction(NavCom));
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. /***********************************************************************************************
  519. * UnitClass::Edge_Of_World_AI -- Check for falling off the edge of the world. *
  520. * *
  521. * When a unit leaves the map it will be eliminated. This routine checks for this case *
  522. * and eliminates the unit accordingly. *
  523. * *
  524. * INPUT: none *
  525. * *
  526. * OUTPUT: bool; Was the unit eliminated by this routine? *
  527. * *
  528. * WARNINGS: Be sure to check for the return value and if 'true' abort any further processing*
  529. * of the unit since it is dead. Only call this routine once per unit per *
  530. * game logic loop. *
  531. * *
  532. * HISTORY: *
  533. * 07/30/1996 JLB : Created. *
  534. *=============================================================================================*/
  535. bool UnitClass::Edge_Of_World_AI(void)
  536. {
  537. if (Mission == MISSION_GUARD && !Map.In_Radar(Coord_Cell(Coord)) && IsLocked) {
  538. if (Team.Is_Valid()) Team->IsLeaveMap = true;
  539. Stun();
  540. delete this;
  541. return(true);
  542. }
  543. return(false);
  544. }
  545. /***********************************************************************************************
  546. * UnitClass::Reload_AI -- Perform reload logic for this unit. *
  547. * *
  548. * Some units require special reload logic. The V2 rocket launcher in particular. Perform *
  549. * this reload logic with this routine. *
  550. * *
  551. * INPUT: none *
  552. * *
  553. * OUTPUT: none *
  554. * *
  555. * WARNINGS: Only call this routine once per unit per game logic loop. *
  556. * *
  557. * HISTORY: *
  558. * 07/30/1996 JLB : Created. *
  559. *=============================================================================================*/
  560. void UnitClass::Reload_AI(void)
  561. {
  562. if (*this == UNIT_V2_LAUNCHER && Ammo < Class->MaxAmmo) {
  563. if (IsDriving) {
  564. Reload = Reload + 1;
  565. } else {
  566. if (Reload == 0) {
  567. Ammo++;
  568. if (Ammo < Class->MaxAmmo) {
  569. Reload = TICKS_PER_SECOND*30;
  570. }
  571. Mark(MARK_CHANGE);
  572. }
  573. }
  574. }
  575. }
  576. /***********************************************************************************************
  577. * UnitClass::Firing_AI -- Handle firing logic for this unit. *
  578. * *
  579. * This routine wil check for and perform any firing logic required of this unit. *
  580. * *
  581. * INPUT: none *
  582. * *
  583. * OUTPUT: none *
  584. * *
  585. * WARNINGS: This should be called only once per unit per game logic loop. *
  586. * *
  587. * HISTORY: *
  588. * 07/30/1996 JLB : Created. *
  589. *=============================================================================================*/
  590. void UnitClass::Firing_AI(void)
  591. {
  592. if (Target_Legal(TarCom) && Class->PrimaryWeapon != NULL) {
  593. /*
  594. ** Determine which weapon can fire. First check for the primary weapon. If that weapon
  595. ** cannot fire, then check any secondary weapon. If neither weapon can fire, then the
  596. ** failure code returned is that from the primary weapon.
  597. */
  598. int primary = What_Weapon_Should_I_Use(TarCom);
  599. FireErrorType ok = Can_Fire(TarCom, primary);
  600. switch (ok) {
  601. case FIRE_OK:
  602. if (!((UnitClass *)this)->Class->IsFireAnim) {
  603. Mark(MARK_OVERLAP_UP);
  604. IsFiring = false;
  605. Mark(MARK_OVERLAP_DOWN);
  606. }
  607. Fire_At(TarCom, primary);
  608. break;
  609. case FIRE_FACING:
  610. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  611. if (Class->IsLockTurret || Class->Type == UNIT_DEMOTRUCK) {
  612. #else
  613. if (Class->IsLockTurret) {
  614. #endif
  615. if (!Target_Legal(NavCom) && !IsDriving) {
  616. PrimaryFacing.Set_Desired(Direction(TarCom));
  617. SecondaryFacing.Set_Desired(PrimaryFacing.Desired());
  618. }
  619. } else {
  620. SecondaryFacing.Set_Desired(Direction(TarCom));
  621. }
  622. break;
  623. case FIRE_CLOAKED:
  624. Mark(MARK_OVERLAP_UP);
  625. IsFiring = false;
  626. Mark(MARK_OVERLAP_DOWN);
  627. Do_Uncloak();
  628. break;
  629. }
  630. }
  631. }
  632. /***********************************************************************************************
  633. * UnitClass::Receive_Message -- Handles receiving a radio message. *
  634. * *
  635. * This is the handler function for when a unit receives a radio *
  636. * message. Typical use of this is when a unit unloads from a hover *
  637. * class so that clearing of the transport is successful. *
  638. * *
  639. * INPUT: from -- Pointer to the originator of the message. *
  640. * *
  641. * message -- The radio message received. *
  642. * *
  643. * param -- Reference to an optional parameter the might be needed to return *
  644. * information back to the originator of the message. *
  645. * *
  646. * OUTPUT: Returns with the radio message response. *
  647. * *
  648. * WARNINGS: none *
  649. * *
  650. * HISTORY: *
  651. * 05/22/1994 JLB : Created. *
  652. *=============================================================================================*/
  653. RadioMessageType UnitClass::Receive_Message(RadioClass * from, RadioMessageType message, long & param)
  654. {
  655. assert(Units.ID(this) == ID);
  656. assert(IsActive);
  657. switch (message) {
  658. /*
  659. ** Checks to see if this object is in need of service depot processing.
  660. */
  661. case RADIO_NEED_REPAIR:
  662. if (!IsDriving && !Target_Legal(NavCom) && (Health_Ratio() >= 1 && (*this != UNIT_MINELAYER || Ammo >= Class->MaxAmmo))) return(RADIO_NEGATIVE);
  663. break;
  664. // return(RADIO_ROGER);
  665. /*
  666. ** Asks if the passenger can load on this transport.
  667. */
  668. case RADIO_CAN_LOAD:
  669. if (Class->Max_Passengers() == 0 || from == NULL || !House->Is_Ally(from->Owner())) return(RADIO_STATIC);
  670. if (How_Many() < Class->Max_Passengers()) {
  671. return(RADIO_ROGER);
  672. }
  673. return(RADIO_NEGATIVE);
  674. /*
  675. ** The refinery has told this harvester that it should begin the backup procedure
  676. ** so that proper unloading may take place.
  677. */
  678. case RADIO_BACKUP_NOW:
  679. DriveClass::Receive_Message(from, message, param);
  680. if (!IsRotating && PrimaryFacing != DIR_W) {
  681. Do_Turn(DIR_W);
  682. } else {
  683. if (!IsDriving) {
  684. TechnoClass * whom = Contact_With_Whom();
  685. if (IsTethered && whom != NULL) {
  686. if (whom->What_Am_I() == RTTI_BUILDING && Mission == MISSION_ENTER) {
  687. if (Transmit_Message(RADIO_IM_IN, whom) == RADIO_ROGER) {
  688. Transmit_Message(RADIO_UNLOADED, whom);
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return(RADIO_ROGER);
  695. /*
  696. ** This message is sent by the passenger when it determines that it has
  697. ** entered the transport.
  698. */
  699. case RADIO_IM_IN:
  700. if (How_Many() == Class->Max_Passengers()) {
  701. APC_Close_Door();
  702. }
  703. return(RADIO_ATTACH);
  704. /*
  705. ** Docking maintenance message received. Check to see if new orders should be given
  706. ** to the impatient unit.
  707. */
  708. case RADIO_DOCKING:
  709. /*
  710. ** If this transport is moving, then always abort the docking request.
  711. */
  712. if (IsDriving || Target_Legal(NavCom)) {
  713. return(RADIO_NEGATIVE);
  714. }
  715. /*
  716. ** Check for the case of a docking message arriving from a unit that does not
  717. ** have formal radio contact established. This might be a unit that is standing
  718. ** by. If this transport is free to proceed with normal docking operation, then
  719. ** establish formal contact now. If the transport is completely full, then break
  720. ** off contact. In all other cases, just tell the pending unit to stand by.
  721. */
  722. if (Contact_With_Whom() != from) {
  723. /*
  724. ** Can't ever load up so tell the passenger to bug off.
  725. */
  726. if (How_Many() >= Class->Max_Passengers()) {
  727. return(RADIO_NEGATIVE);
  728. }
  729. /*
  730. ** Establish contact and let the loading process proceed normally.
  731. */
  732. if (!In_Radio_Contact()) {
  733. Transmit_Message(RADIO_HELLO, from);
  734. } else {
  735. /*
  736. ** This causes the potential passenger to think that all is ok and to
  737. ** hold on for a bit.
  738. */
  739. return(RADIO_ROGER);
  740. }
  741. }
  742. if (Class->Max_Passengers() > 0 && How_Many() < Class->Max_Passengers()) {
  743. DriveClass::Receive_Message(from, message, param);
  744. if (!IsDriving && !IsRotating && !IsTethered) {
  745. /*
  746. ** If the potential passenger needs someplace to go, then figure out a good
  747. ** spot and tell it to go.
  748. */
  749. if (Transmit_Message(RADIO_NEED_TO_MOVE, from) == RADIO_ROGER) {
  750. CELL cell;
  751. DirType dir = Desired_Load_Dir(from, cell);
  752. /*
  753. ** If no adjacent free cells are detected, then passenger loading
  754. ** cannot occur. Break radio contact.
  755. */
  756. if (cell == 0) {
  757. Transmit_Message(RADIO_OVER_OUT, from);
  758. } else {
  759. param = (long)::As_Target(cell);
  760. Do_Turn(dir);
  761. /*
  762. ** If it is now facing the correct direction, then open the
  763. ** transport doors. Close the doors if the transport is or needs
  764. ** to rotate.
  765. */
  766. #ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
  767. if (*this == UNIT_APC || *this == UNIT_PHASE) {
  768. #else
  769. if (*this == UNIT_APC) {
  770. #endif
  771. if (IsRotating) {
  772. if (!Is_Door_Closed()) {
  773. APC_Close_Door();
  774. }
  775. } else {
  776. if (!Is_Door_Open()) {
  777. APC_Open_Door();
  778. }
  779. }
  780. }
  781. /*
  782. ** Tell the potential passenger where it should go. If the passenger is
  783. ** already at the staging location, then tell it to move onto the transport
  784. ** directly.
  785. */
  786. if (Transmit_Message(RADIO_MOVE_HERE, param, from) == RADIO_YEA_NOW_WHAT) {
  787. #ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
  788. if ( (*this != UNIT_APC && *this != UNIT_PHASE) || Is_Door_Open()) {
  789. #else
  790. if (*this != UNIT_APC || Is_Door_Open()) {
  791. #endif
  792. param = (long)As_Target();
  793. Transmit_Message(RADIO_TETHER);
  794. if (Transmit_Message(RADIO_MOVE_HERE, param, from) != RADIO_ROGER) {
  795. Transmit_Message(RADIO_OVER_OUT, from);
  796. } else {
  797. Contact_With_Whom()->Unselect();
  798. }
  799. }
  800. }
  801. }
  802. }
  803. }
  804. return(RADIO_ROGER);
  805. }
  806. break;
  807. /*
  808. ** When this message is received, it means that the other object
  809. ** has already turned its radio off. Turn this radio off as well.
  810. */
  811. case RADIO_OVER_OUT:
  812. if (Mission == MISSION_RETURN) {
  813. Assign_Mission(MISSION_GUARD);
  814. }
  815. DriveClass::Receive_Message(from, message, param);
  816. return(RADIO_ROGER);
  817. }
  818. return(DriveClass::Receive_Message(from, message, param));
  819. }
  820. /***********************************************************************************************
  821. * UnitClass::Unlimbo -- Removes unit from stasis. *
  822. * *
  823. * This routine will place a unit into the game and out of its limbo *
  824. * state. This occurs whenever a unit is unloaded from a transport. *
  825. * *
  826. * INPUT: coord -- The coordinate to make the unit appear. *
  827. * *
  828. * dir -- The initial facing to impart upon the unit. *
  829. * *
  830. * OUTPUT: bool; Was the unit unlimboed successfully? If the desired *
  831. * coordinate is illegal, then this might very well return *
  832. * false. *
  833. * *
  834. * WARNINGS: none *
  835. * *
  836. * HISTORY: *
  837. * 05/22/1994 JLB : Created. *
  838. *=============================================================================================*/
  839. bool UnitClass::Unlimbo(COORDINATE coord, DirType dir)
  840. {
  841. assert(Units.ID(this) == ID);
  842. assert(IsActive);
  843. /*
  844. ** All units must start out facing one of the 8 major directions.
  845. */
  846. dir = Facing_Dir(Dir_Facing(dir));
  847. if (DriveClass::Unlimbo(coord, dir)) {
  848. SecondaryFacing = dir;
  849. /*
  850. ** Ensure that the owning house knows about the
  851. ** new object.
  852. */
  853. House->UScan |= (1L << Class->Type);
  854. House->ActiveUScan |= (1L << Class->Type);
  855. /*
  856. ** If it starts off the edge of the map, then it already starts cloaked.
  857. */
  858. if (IsCloakable && !IsLocked) Cloak = CLOAKED;
  859. /*
  860. ** Units default to no special animation.
  861. */
  862. Set_Rate(0);
  863. Set_Stage(0);
  864. return(true);
  865. }
  866. return(false);
  867. }
  868. /***********************************************************************************************
  869. * UnitClass::Take_Damage -- Inflicts damage points on a unit. *
  870. * *
  871. * This routine will inflict the specified number of damage points on *
  872. * the given unit. If the unit is destroyed, then this routine will *
  873. * remove the unit cleanly from the game. The return value indicates *
  874. * whether the unit was destroyed. This will allow appropriate death *
  875. * animation or whatever. *
  876. * *
  877. * INPUT: damage-- The number of damage points to inflict. *
  878. * *
  879. * distance -- The distance from the damage center point to the object's center point.*
  880. * *
  881. * warhead--The type of damage to inflict. *
  882. * *
  883. * source -- Who is responsible for this damage? *
  884. * *
  885. * OUTPUT: Returns the result of the damage process. This can range from RESULT_NONE up to *
  886. * RESULT_DESTROYED. *
  887. * *
  888. * WARNINGS: none *
  889. * *
  890. * HISTORY: *
  891. * 05/30/1991 JLB : Created. *
  892. * 07/12/1991 JLB : Script initiated by unit destruction. *
  893. * 04/15/1994 JLB : Converted to member function. *
  894. * 04/16/1994 JLB : Warhead modifier. *
  895. * 06/03/1994 JLB : Added the source of the damage target value. *
  896. * 06/20/1994 JLB : Source is a base class pointer. *
  897. * 11/22/1994 JLB : Shares base damage handler for techno objects. *
  898. * 06/30/1995 JLB : Lasers do maximum damage against gunboat. *
  899. * 08/16/1995 JLB : Harvester crushing doesn't occur on early missions. *
  900. *=============================================================================================*/
  901. ResultType UnitClass::Take_Damage(int & damage, int distance, WarheadType warhead, TechnoClass * source, int forced)
  902. {
  903. assert(Units.ID(this) == ID);
  904. assert(IsActive);
  905. ResultType res = RESULT_NONE;
  906. /*
  907. ** Remember if this object was selected. If it was and it gets destroyed and it has
  908. ** passengers that pop out, then the passengers will inherit the select state.
  909. */
  910. bool select = (IsSelected && House->IsPlayerControl);
  911. /*
  912. ** In order for a this to be damaged, it must either be a unit
  913. ** with a crew or a sandworm.
  914. */
  915. res = DriveClass::Take_Damage(damage, distance, warhead, source, forced);
  916. if (res == RESULT_DESTROYED) {
  917. Death_Announcement(source);
  918. Shroud_Regen(); // remove the shroud if it's a gap generator
  919. if (Class->Explosion != ANIM_NONE) {
  920. AnimType anim = Class->Explosion;
  921. /*
  922. ** SSM launchers will really explode big if they are carrying
  923. ** missiles at the time of the explosion.
  924. */
  925. if (*this == UNIT_V2_LAUNCHER && Ammo) {
  926. anim = ANIM_NAPALM3;
  927. }
  928. new AnimClass(anim, Coord);
  929. /*
  930. ** Harvesters explode with a force equal to the amount of
  931. ** Tiberium they are carrying.
  932. */
  933. if (Tiberium > 0 && Rule.IsExplosiveHarvester) {
  934. Wide_Area_Damage(Coord, CELL_LEPTON_W + CELL_LEPTON_W/2, Credit_Load()+Class->MaxStrength, this, WARHEAD_HE);
  935. }
  936. /*
  937. ** Very strong units that have an explosion will also rock the
  938. ** screen when they are destroyed.
  939. */
  940. if (Class->MaxStrength > 400) {
  941. Shake_The_Screen(3);
  942. }
  943. }
  944. /*
  945. ** Possibly have the crew member run away.
  946. */
  947. CELL cell = Coord_Cell(Center_Coord());
  948. Mark(MARK_UP);
  949. if (Class->IsCrew && Class->Max_Passengers() == 0) {
  950. if (Percent_Chance(50)) {
  951. InfantryClass * i = 0;
  952. if (Class->PrimaryWeapon == NULL) {
  953. i = new InfantryClass(INFANTRY_C1, House->Class->House);
  954. if (i != NULL) i->IsTechnician = true;
  955. } else {
  956. i = new InfantryClass(INFANTRY_E1, House->Class->House);
  957. }
  958. if (i != NULL) {
  959. if (i->Unlimbo(Coord, DIR_N)) {
  960. i->Strength = Random_Pick(5, (int)i->Class->MaxStrength/2);
  961. i->Scatter(0, true);
  962. if (!House->IsHuman) {
  963. i->Assign_Mission(MISSION_HUNT);
  964. } else {
  965. i->Assign_Mission(MISSION_GUARD);
  966. }
  967. if (select) i->Select();
  968. } else {
  969. delete i;
  970. }
  971. }
  972. }
  973. } else {
  974. while (Is_Something_Attached()) {
  975. FootClass * object = Detach_Object();
  976. if (object == NULL) break; // How can this happen?
  977. /*
  978. ** Only infantry can run from a destroyed vehicle. Even then, it is not a sure
  979. ** thing.
  980. */
  981. if (object->Is_Infantry() && object->Unlimbo(Coord, DIR_N)) {
  982. object->Scatter(0, true);
  983. if (select) object->Select();
  984. } else {
  985. object->Record_The_Kill(source);
  986. delete object;
  987. }
  988. }
  989. }
  990. /*
  991. ** If this is a truck, there is a possibility that a crate will drop out
  992. ** if the scenario so indicates and there is room.
  993. */
  994. if (Scen.IsTruckCrate && *this == UNIT_TRUCK) {
  995. cell = Nearby_Location();
  996. if (cell != 0) {
  997. new OverlayClass(OVERLAY_WOOD_CRATE, cell);
  998. }
  999. }
  1000. /*
  1001. ** Finally, delete the vehicle.
  1002. */
  1003. delete this;
  1004. } else {
  1005. /*
  1006. ** When damaged and below half strength, start smoking if
  1007. ** it isn't already smoking.
  1008. */
  1009. if (Health_Ratio() <= Rule.ConditionYellow && !IsAnimAttached) {
  1010. #ifdef FIXIT_ANTS
  1011. if (*this != UNIT_ANT1 && *this != UNIT_ANT2 && *this != UNIT_ANT3) {
  1012. #endif
  1013. AnimClass * anim = new AnimClass(ANIM_SMOKE_M, Coord_Add(Coord, XYP_Coord(0, -8)));
  1014. if (anim) anim->Attach_To(this);
  1015. #ifdef FIXIT_ANTS
  1016. }
  1017. #endif
  1018. }
  1019. /*
  1020. ** Try to crush anyone that fires on this unit if possible. The harvester
  1021. ** typically is the only one that will qualify here.
  1022. */
  1023. if (!Team.Is_Valid() && source != NULL && !IsTethered && !House->Is_Ally(source) && (!House->IsHuman || Rule.IsAutoCrush)) {
  1024. /*
  1025. ** Try to crush the attacker if it can be crushed by this unit and this unit is
  1026. ** not equipped with a flame type weapon. If this unit has a weapon and the target
  1027. ** is not very close, then fire on it instead. In easy mode, they never run over the
  1028. ** player. In hard mode, they always do. In normal mode, they only overrun past
  1029. ** mission #8.
  1030. */
  1031. if (Should_Crush_It(source)) {
  1032. Assign_Destination(source->As_Target());
  1033. Assign_Mission(MISSION_MOVE);
  1034. } else {
  1035. /*
  1036. ** Try to return to base if possible.
  1037. */
  1038. if (*this == UNIT_HARVESTER && Pip_Count() && Health_Ratio() <= Rule.ConditionYellow) {
  1039. /*
  1040. ** Find nearby refinery and head to it?
  1041. */
  1042. BuildingClass * building = Find_Docking_Bay(STRUCT_REFINERY, false);
  1043. /*
  1044. ** Since the refinery said it was ok to load, establish radio
  1045. ** contact with the refinery and then await docking orders.
  1046. */
  1047. if (building != NULL && Transmit_Message(RADIO_HELLO, building) == RADIO_ROGER) {
  1048. Assign_Mission(MISSION_ENTER);
  1049. }
  1050. }
  1051. }
  1052. }
  1053. /*
  1054. ** Computer controlled harvester will radio for help if they are attacked.
  1055. */
  1056. if (*this == UNIT_HARVESTER && !House->IsHuman && source) {
  1057. Base_Is_Attacked(source);
  1058. }
  1059. }
  1060. return(res);
  1061. }
  1062. /***********************************************************************************************
  1063. * UnitClass::Active_Click_With -- Intercepts the active click to see if deployment is possible*
  1064. * *
  1065. * This routine intercepts the active click operation. It check to see if this is a self *
  1066. * deployment request (MCV's have this ability). If it is, then the object is initiated *
  1067. * to self deploy. In the other cases, it passes the operation down to the lower *
  1068. * classes for processing. *
  1069. * *
  1070. * INPUT: action -- The action requested of the unit. *
  1071. * *
  1072. * object -- The object that the mouse pointer is over. *
  1073. * *
  1074. * OUTPUT: none *
  1075. * *
  1076. * WARNINGS: none *
  1077. * *
  1078. * HISTORY: *
  1079. * 03/10/1995 JLB : Created. *
  1080. *=============================================================================================*/
  1081. void UnitClass::Active_Click_With(ActionType action, ObjectClass * object)
  1082. {
  1083. assert(Units.ID(this) == ID);
  1084. assert(IsActive);
  1085. if (action != What_Action(object)) {
  1086. action = What_Action(object);
  1087. switch (action) {
  1088. case ACTION_SABOTAGE:
  1089. case ACTION_CAPTURE:
  1090. action = ACTION_ATTACK;
  1091. break;
  1092. case ACTION_ENTER:
  1093. action = ACTION_MOVE;
  1094. break;
  1095. default:
  1096. break;
  1097. }
  1098. }
  1099. /*
  1100. ** Short circuit out if trying to tell a unit to "nomove" to itself. This bypass of the
  1101. ** normal active click with logic prevents any disturbance to the vehicle's state. Without
  1102. ** this bypass, a unit on a repair bay would stop repairing because it would break radio
  1103. ** contact.
  1104. */
  1105. if (object == this && action == ACTION_NOMOVE) {
  1106. return;
  1107. }
  1108. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  1109. if (*this == UNIT_MAD && (IsDumping || Gold)) {
  1110. } else {
  1111. DriveClass::Active_Click_With(action, object);
  1112. }
  1113. #else
  1114. DriveClass::Active_Click_With(action, object);
  1115. #endif
  1116. }
  1117. /***********************************************************************************************
  1118. * UnitClass::Active_Click_With -- Performs specified action on specified cell. *
  1119. * *
  1120. * This routine is called when the mouse has been clicked over a cell and this unit must *
  1121. * now respond. Notice that this is merely a placeholder function that exists because there *
  1122. * is another function of the same name that needs to be overloaded. C++ has scoping *
  1123. * restrictions when there are two identically named functions that are overridden in *
  1124. * different classes -- it handles it badly, hence the existence of this routine. *
  1125. * *
  1126. * INPUT: action -- The action to perform on the cell specified. *
  1127. * *
  1128. * cell -- The cell that the action is to be performed on. *
  1129. * *
  1130. * OUTPUT: none *
  1131. * *
  1132. * WARNINGS: none *
  1133. * *
  1134. * HISTORY: *
  1135. * 09/21/1995 JLB : Created. *
  1136. *=============================================================================================*/
  1137. void UnitClass::Active_Click_With(ActionType action, CELL cell)
  1138. {
  1139. assert(Units.ID(this) == ID);
  1140. assert(IsActive);
  1141. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  1142. if (*this == UNIT_MAD && (IsDumping || Gold)) {
  1143. } else {
  1144. DriveClass::Active_Click_With(action, cell);
  1145. }
  1146. #else
  1147. DriveClass::Active_Click_With(action, cell);
  1148. #endif
  1149. }
  1150. /***********************************************************************************************
  1151. * UnitClass::Enter_Idle_Mode -- Unit enters idle mode state. *
  1152. * *
  1153. * This routine is called when the unit completes one mission but does not have a clear *
  1154. * follow up mission to perform. In such a case, the unit should enter a default idle *
  1155. * state. This idle state varies depending on what the current internal computer *
  1156. * settings of the unit is as well as what kind of unit it is. *
  1157. * *
  1158. * INPUT: initial -- Is this called when the unit just leaves a factory or is initially *
  1159. * or is initially placed on the map? *
  1160. * *
  1161. * OUTPUT: none *
  1162. * *
  1163. * WARNINGS: none *
  1164. * *
  1165. * HISTORY: *
  1166. * 05/31/1994 JLB : Created. *
  1167. * 06/03/1994 JLB : Fixed to handle non-combat vehicles. *
  1168. * 06/18/1995 JLB : Allows a harvester to stop harvesting. *
  1169. *=============================================================================================*/
  1170. void UnitClass::Enter_Idle_Mode(bool initial)
  1171. {
  1172. assert(Units.ID(this) == ID);
  1173. assert(IsActive);
  1174. MissionType order = MISSION_GUARD;
  1175. if (IsToScatter) {
  1176. IsToScatter = false;
  1177. Scatter(0, true);
  1178. }
  1179. /*
  1180. ** A movement mission without a NavCom would be pointless to have a radio contact since
  1181. ** no radio coordination occurs on a just a simple movement mission.
  1182. */
  1183. if (Mission == MISSION_MOVE && !Target_Legal(NavCom)) {
  1184. Transmit_Message(RADIO_OVER_OUT);
  1185. }
  1186. Handle_Navigation_List();
  1187. if (Target_Legal(NavCom)) {
  1188. order = MISSION_MOVE;
  1189. } else {
  1190. if (!Is_Weapon_Equipped()) {
  1191. if (Class->IsToHarvest) {
  1192. if (!In_Radio_Contact() && Mission != MISSION_HARVEST && MissionQueue != MISSION_HARVEST) {
  1193. if (initial || !House->IsHuman || Map[Coord].Land_Type() == LAND_TIBERIUM) {
  1194. order = MISSION_HARVEST;
  1195. } else {
  1196. order = MISSION_GUARD;
  1197. }
  1198. Assign_Target(TARGET_NONE);
  1199. Assign_Destination(TARGET_NONE);
  1200. } else {
  1201. return;
  1202. }
  1203. } else {
  1204. if (IsALoaner && Class->Max_Passengers() > 0 && Is_Something_Attached() && !Team.Is_Valid()) {
  1205. order = MISSION_UNLOAD;
  1206. } else {
  1207. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  1208. if(*this == UNIT_MAD && Mission == MISSION_UNLOAD) {
  1209. order = MISSION_UNLOAD;
  1210. } else {
  1211. #endif
  1212. order = MISSION_GUARD;
  1213. Assign_Target(TARGET_NONE);
  1214. Assign_Destination(TARGET_NONE);
  1215. }
  1216. }
  1217. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  1218. }
  1219. #endif
  1220. } else {
  1221. if (Mission == MISSION_GUARD || Mission == MISSION_GUARD_AREA || MissionControl[Mission].IsParalyzed || MissionControl[Mission].IsZombie) {
  1222. return;
  1223. }
  1224. if (House->IQ < Rule.IQGuardArea || Team.Is_Valid()) {
  1225. order = MISSION_GUARD;
  1226. } else {
  1227. order = MISSION_GUARD_AREA;
  1228. }
  1229. }
  1230. }
  1231. Assign_Mission(order);
  1232. }
  1233. /***********************************************************************************************
  1234. * UnitClass::Goto_Clear_Spot -- Finds a clear spot to deploy. *
  1235. * *
  1236. * This routine is used by the MCV to find a clear spot to deploy. If a clear spot *
  1237. * is found, then the MCV will assign that location to its navigation computer. This only *
  1238. * occurs if the MCV isn't already heading toward a spot. *
  1239. * *
  1240. * INPUT: none *
  1241. * *
  1242. * OUTPUT: bool; Is the located at a spot where it can deploy? *
  1243. * *
  1244. * WARNINGS: none *
  1245. * *
  1246. * HISTORY: *
  1247. * 06/27/1994 JLB : Created. *
  1248. *=============================================================================================*/
  1249. bool UnitClass::Goto_Clear_Spot(void)
  1250. {
  1251. assert(Units.ID(this) == ID);
  1252. assert(IsActive);
  1253. Mark(MARK_UP);
  1254. if (!Target_Legal(NavCom) && BuildingTypeClass::As_Reference(STRUCT_CONST).Legal_Placement(Adjacent_Cell(Coord_Cell(Center_Coord()), FACING_NW))) {
  1255. Mark(MARK_DOWN);
  1256. return(true);
  1257. }
  1258. if (!Target_Legal(NavCom)) {
  1259. /*
  1260. ** This scan table is skewed to north scanning only. This should
  1261. ** probably be converted to a more flexible method.
  1262. */
  1263. static int _offsets[] = {
  1264. -MAP_CELL_W*1,
  1265. -MAP_CELL_W*2,
  1266. -(MAP_CELL_W*2)+1,
  1267. -(MAP_CELL_W*2)-1,
  1268. -MAP_CELL_W*3,
  1269. -(MAP_CELL_W*3)+1,
  1270. -(MAP_CELL_W*3)-1,
  1271. -(MAP_CELL_W*3)+2,
  1272. -(MAP_CELL_W*3)-2,
  1273. -MAP_CELL_W*4,
  1274. -(MAP_CELL_W*4)+1,
  1275. -(MAP_CELL_W*4)-1,
  1276. -(MAP_CELL_W*4)+2,
  1277. -(MAP_CELL_W*4)-2,
  1278. //BG: Added south scanning
  1279. MAP_CELL_W*1,
  1280. MAP_CELL_W*2,
  1281. (MAP_CELL_W*2)+1,
  1282. (MAP_CELL_W*2)-1,
  1283. MAP_CELL_W*3,
  1284. (MAP_CELL_W*3)+1,
  1285. (MAP_CELL_W*3)-1,
  1286. (MAP_CELL_W*3)+2,
  1287. (MAP_CELL_W*3)-2,
  1288. MAP_CELL_W*4,
  1289. (MAP_CELL_W*4)+1,
  1290. (MAP_CELL_W*4)-1,
  1291. (MAP_CELL_W*4)+2,
  1292. (MAP_CELL_W*4)-2,
  1293. //BG: Added some token east/west scanning
  1294. -1,-2,-3,-4,
  1295. 1, 2, 3, 4,
  1296. 0
  1297. };
  1298. int * ptr;
  1299. ptr = &_offsets[0];
  1300. while (*ptr) {
  1301. CELL cell = Coord_Cell(Coord)+*ptr++;
  1302. CELL check_cell = Adjacent_Cell(cell, FACING_NW);
  1303. if (BuildingTypeClass::As_Reference(STRUCT_CONST).Legal_Placement(check_cell)) {
  1304. Assign_Destination(::As_Target(cell));
  1305. break;
  1306. }
  1307. }
  1308. }
  1309. Mark(MARK_DOWN);
  1310. /*
  1311. ** If we couldn't find a destination to go to, let's try random movement
  1312. ** to see if that brings us to a better spot.
  1313. */
  1314. if(!Target_Legal(NavCom) && !House->IsHuman) {
  1315. Scatter(0);
  1316. }
  1317. return(false);
  1318. }
  1319. /***********************************************************************************************
  1320. * UnitClass::Try_To_Deploy -- The unit attempts to "deploy" at current location. *
  1321. * *
  1322. * Certain units have the ability to deploy into a building. When this routine is called *
  1323. * for one of those units, it will attempt to deploy at its current location. If the unit *
  1324. * is in motion to a destination or it isn't one of the special units that can deploy or *
  1325. * it isn't allowed to deploy at this location for some reason it won't deploy. In all *
  1326. * other cases, it will begin to deploy and once it begins only a player abort action will *
  1327. * stop it. *
  1328. * *
  1329. * INPUT: none *
  1330. * *
  1331. * OUTPUT: bool; Was deployment begun? *
  1332. * *
  1333. * WARNINGS: none *
  1334. * *
  1335. * HISTORY: *
  1336. * 06/18/1994 JLB : Created. *
  1337. *=============================================================================================*/
  1338. bool UnitClass::Try_To_Deploy(void)
  1339. {
  1340. assert(Units.ID(this) == ID);
  1341. assert(IsActive);
  1342. if (!Target_Legal(NavCom) && !IsRotating) {
  1343. if (*this == UNIT_MCV) {
  1344. /*
  1345. ** Determine if it is legal to deploy at this location. If not, tell the
  1346. ** player.
  1347. */
  1348. Mark(MARK_UP);
  1349. CELL cell = Coord_Cell(Adjacent_Cell(Center_Coord(), FACING_NW));
  1350. if (!BuildingTypeClass::As_Reference(STRUCT_CONST).Legal_Placement(cell)) {
  1351. if (PlayerPtr == House) {
  1352. Speak(VOX_DEPLOY);
  1353. }
  1354. if (!House->IsHuman) {
  1355. BuildingTypeClass::As_Reference(STRUCT_CONST).Flush_For_Placement(cell, House);
  1356. }
  1357. Mark(MARK_DOWN);
  1358. IsDeploying = false;
  1359. return(false);
  1360. }
  1361. Mark(MARK_DOWN);
  1362. /*
  1363. ** If the unit is not facing the correct direction, then start it rotating
  1364. ** toward the right facing, but still flag it as if it had deployed. This is
  1365. ** because it will deploy as soon as it reaches the correct facing.
  1366. */
  1367. if (PrimaryFacing.Current() != DIR_SW) {
  1368. Do_Turn(DIR_SW);
  1369. // PrimaryFacing.Set_Desired(DIR_SW);
  1370. IsDeploying = true;
  1371. return(true);
  1372. }
  1373. /*
  1374. ** Since the unit is already facing the correct direction, actually do the
  1375. ** deploy logic. If for some reason this cannot occur, then don't delete the
  1376. ** unit, just mark it as not deploying.
  1377. */
  1378. Mark(MARK_UP);
  1379. BuildingClass * building = new BuildingClass(STRUCT_CONST, House->Class->House);
  1380. if (building != NULL) {
  1381. if (building->Unlimbo(Adjacent_Cell(Coord, FACING_NW))) {
  1382. /*
  1383. ** Play the buildup sound for the player if this is the players
  1384. ** MCV.
  1385. */
  1386. if (building->House == PlayerPtr) {
  1387. Sound_Effect(VOC_PLACE_BUILDING_DOWN, Center_Coord());
  1388. } else {
  1389. building->IsToRebuild = true;
  1390. building->IsToRepair = true;
  1391. }
  1392. /*
  1393. ** Always reveal the construction yard to the player that owned the
  1394. ** mobile construction vehicle.
  1395. */
  1396. building->Revealed(House);
  1397. /*
  1398. ** When the MCV deploys, always consider production to have started
  1399. ** for the owning house. This ensures that in multiplay, computer
  1400. ** opponents will begin construction as soon as they start their
  1401. ** base.
  1402. */
  1403. House->IsStarted = true;
  1404. /*
  1405. ** Force the newly placed construction yard to be in the same strength
  1406. ** ratio as the MCV that deployed into it.
  1407. */
  1408. building->Strength = Health_Ratio() * building->Class->MaxStrength;
  1409. /*
  1410. ** Force the MCV to drop any flag it was carrying. This will also set
  1411. ** the owner house's flag home cell (since the house's FlagHome is
  1412. ** presumably 0 at this point).
  1413. */
  1414. Stun();
  1415. /*
  1416. ** If this MCV was teleported here, clear the gray flag so
  1417. ** the screen will go back to color.
  1418. */
  1419. if (IsMoebius && !Scen.IsFadingColor) {
  1420. Scen.IsFadingBW = false;
  1421. Scen.IsFadingColor = true;
  1422. Scen.FadeTimer = GRAYFADETIME;
  1423. }
  1424. delete this;
  1425. return(true);
  1426. } else {
  1427. /*
  1428. ** Could not deploy the construction yard at this location! Just revert
  1429. ** back to normal "just sitting there" mode and await further instructions.
  1430. */
  1431. delete building;
  1432. }
  1433. }
  1434. Mark(MARK_DOWN);
  1435. IsDeploying = false;
  1436. }
  1437. }
  1438. return(false);
  1439. }
  1440. /***********************************************************************************************
  1441. * UnitClass::Per_Cell_Process -- Performs operations necessary on a per cell basis. *
  1442. * *
  1443. * This routine will perform the operations necessary that occur when a unit is at the *
  1444. * center of a cell. These operations could entail deploying into a construction yard, *
  1445. * radioing a transport unit, and looking around for the enemy. *
  1446. * *
  1447. * INPUT: why -- Specifies the circumstances under which this routine was called. *
  1448. * *
  1449. * OUTPUT: none *
  1450. * *
  1451. * WARNINGS: none *
  1452. * *
  1453. * HISTORY: *
  1454. * 06/18/1994 JLB : Created. *
  1455. * 06/17/1995 JLB : Handles case when building says "NO!" *
  1456. * 06/30/1995 JLB : Gunboats head back and forth now. *
  1457. *=============================================================================================*/
  1458. void UnitClass::Per_Cell_Process(PCPType why)
  1459. {
  1460. assert(Units.ID(this) == ID);
  1461. assert(IsActive);
  1462. CELL cell = Coord_Cell(Coord);
  1463. HousesType house;
  1464. if (why == PCP_END || why == PCP_ROTATION) {
  1465. /*
  1466. ** Check to see if this is merely the end of a rotation for the MCV as it is
  1467. ** preparing to deploy. In this case, it should begin its deploy process.
  1468. */
  1469. if (IsDeploying) {
  1470. Try_To_Deploy();
  1471. if (!IsActive) return; // Unit no longer exists -- bail.
  1472. }
  1473. }
  1474. BStart(BENCH_PCP);
  1475. if (why == PCP_END) {
  1476. /*
  1477. ** If this is a unit that is driving onto a building then the unit must enter
  1478. ** the building as the final step.
  1479. */
  1480. TechnoClass * whom = Contact_With_Whom();
  1481. if (IsTethered && whom != NULL) {
  1482. if (whom->What_Am_I() == RTTI_BUILDING && Mission == MISSION_ENTER) {
  1483. if (whom == Map[CELL(cell-MAP_CELL_W)].Cell_Building()) {
  1484. switch (Transmit_Message(RADIO_IM_IN, whom)) {
  1485. case RADIO_ROGER:
  1486. break;
  1487. case RADIO_ATTACH:
  1488. break;
  1489. default:
  1490. Scatter(0, true);
  1491. break;
  1492. }
  1493. }
  1494. }
  1495. }
  1496. /*
  1497. ** Unit entering a transport vehicle will break radio contact
  1498. ** and attach itself to the transporter.
  1499. */
  1500. TechnoClass * techno = Contact_With_Whom();
  1501. if (Mission == MISSION_ENTER && techno && Coord_Cell(Coord) == Coord_Cell(techno->Coord) && techno == As_Techno(NavCom)) {
  1502. if (Transmit_Message(RADIO_IM_IN) == RADIO_ATTACH) {
  1503. Limbo();
  1504. techno->Attach(this);
  1505. }
  1506. BEnd(BENCH_PCP);
  1507. return;
  1508. }
  1509. /*
  1510. ** When breaking away from a transport object or building, possibly
  1511. ** scatter or otherwise begin normal unit operations.
  1512. */
  1513. if (IsTethered && (Mission != MISSION_ENTER ||
  1514. (As_Techno(NavCom) != NULL && Contact_With_Whom() != As_Techno(NavCom))
  1515. ) &&
  1516. Mission != MISSION_UNLOAD) {
  1517. /*
  1518. ** Special hack check to make sure that even though it has moved one
  1519. ** cell, if it is still on the building (e.g., service depot), have
  1520. ** it scatter again.
  1521. */
  1522. if (Map[Coord].Cell_Building() != NULL && !Target_Legal(NavCom)) {
  1523. Scatter(0, true, true);
  1524. } else {
  1525. if (Transmit_Message(RADIO_UNLOADED) == RADIO_RUN_AWAY) {
  1526. if (!Target_Legal(NavCom)) {
  1527. Scatter(0, true);
  1528. } else {
  1529. /*
  1530. ** Special case hack to allow automatic transition to loading
  1531. ** onto a transport (or other situation) if the destination
  1532. ** so indicates.
  1533. */
  1534. TechnoClass * techno = As_Techno(NavCom);
  1535. if (techno != NULL) {
  1536. Transmit_Message(RADIO_DOCKING, techno);
  1537. }
  1538. }
  1539. } else {
  1540. if (*this == UNIT_HARVESTER) {
  1541. if (Target_Legal(ArchiveTarget)) {
  1542. Assign_Mission(MISSION_HARVEST);
  1543. Assign_Destination(ArchiveTarget);
  1544. ArchiveTarget = TARGET_NONE;
  1545. } else {
  1546. /*
  1547. ** Since there is no place to go, move away to clear
  1548. ** the pad for another harvester.
  1549. */
  1550. if (!Target_Legal(NavCom)) {
  1551. Scatter(0, true);
  1552. }
  1553. }
  1554. }
  1555. }
  1556. }
  1557. }
  1558. /*
  1559. ** If this is a loaner unit and is is off the edge of the
  1560. ** map, then it gets eliminated. That is, unless it is carrying cargo. This means that
  1561. ** it is probably carrying an incoming reinforcement and it should not be eliminated.
  1562. */
  1563. if (Edge_Of_World_AI()) {
  1564. BEnd(BENCH_PCP);
  1565. return;
  1566. }
  1567. /*
  1568. ** The unit performs looking around at this time. If the
  1569. ** unit moved further than one square during the last track
  1570. ** move, don't do an incremental look. Do a full look around
  1571. ** instead.
  1572. */
  1573. if (IsPlanningToLook) {
  1574. IsPlanningToLook = false;
  1575. Look(false);
  1576. } else {
  1577. Look(true);
  1578. }
  1579. /*
  1580. ** If this is a mobile gap generator, restore the shroud where appropriate
  1581. ** and re-shroud around us.
  1582. */
  1583. if (Class->IsGapper && !House->IsPlayerControl) {
  1584. Shroud_Regen();
  1585. }
  1586. /*
  1587. ** Act on new orders if the unit is at a good position to do so.
  1588. */
  1589. if (!IsDumping) {
  1590. Commence();
  1591. }
  1592. /*
  1593. ** Certain units require some setup time after they come to a halt.
  1594. */
  1595. if (!Target_Legal(NavCom) && Path[0] == FACING_NONE) {
  1596. if (Class->IsNoFireWhileMoving) {
  1597. Arm = Rearm_Delay(true)/4;
  1598. }
  1599. }
  1600. /*
  1601. ** If there is a house flag here, then this unit just might pick it up.
  1602. */
  1603. if (Flagged == HOUSE_NONE) {
  1604. if (Map[cell].IsFlagged && Map[cell].Owner != House->Class->House) {
  1605. HouseClass::As_Pointer(Map[cell].Owner)->Flag_Attach(this);
  1606. }
  1607. }
  1608. /*
  1609. ** If this is the unit's own flag-home-cell and the unit is carrying
  1610. ** a flag, destroy the house of the flag the unit is carrying.
  1611. */
  1612. if (Flagged != HOUSE_NONE) {
  1613. /*
  1614. ** If this vehicle is carrying your flag, then it will reveal the
  1615. ** map for you as well as itself. This gives you and opportunity to
  1616. ** attack the unit.
  1617. */
  1618. if (!IsOwnedByPlayer && Flagged == PlayerPtr->Class->House) {
  1619. Map.Sight_From(Coord_Cell(Coord), Class->SightRange, House, true);
  1620. }
  1621. /*
  1622. ** If the flag reaches the home cell for the player, then the flag's
  1623. ** owner will be destroyed.
  1624. */
  1625. if (cell == HouseClass::As_Pointer(Owner())->FlagHome) {
  1626. house = Flagged; // Flag_Remove will clear 'Flagged', so save it
  1627. HouseClass::As_Pointer(house)->Flag_Remove(As_Target(), true);
  1628. HouseClass::As_Pointer(house)->Flag_To_Die();
  1629. }
  1630. }
  1631. /*
  1632. ** If entering a cell with a land mine in it, blow up the mine.
  1633. */
  1634. BuildingClass * bldng = Map[cell].Cell_Building();
  1635. if (bldng != NULL && (*bldng == STRUCT_AVMINE || *bldng == STRUCT_APMINE) && !bldng->House->Is_Ally(this)) {
  1636. /*
  1637. ** Special case: if it's a land mine deployer, and it ran over the
  1638. ** type of mine it deploys (only possible if it just dropped it
  1639. ** down) then ignore the mine.
  1640. */
  1641. if (*this != UNIT_MINELAYER || bldng->House != House) {
  1642. COORDINATE blcoord = bldng->Center_Coord();
  1643. new AnimClass(ANIM_MINE_EXP1, blcoord);
  1644. // new AnimClass(Combat_Anim(Rule.AVMineDamage, WARHEAD_HE, Map[cell].Land_Type()), blcoord);
  1645. /*
  1646. ** Vehicles blow up both mines, but they only take significant damage from AV mines.
  1647. */
  1648. if (*bldng == STRUCT_AVMINE) {
  1649. int damage = Rule.AVMineDamage;
  1650. Take_Damage(damage, 0, WARHEAD_HE);
  1651. } else {
  1652. int damage = 10;
  1653. Take_Damage(damage, 0, WARHEAD_HE);
  1654. }
  1655. delete bldng;
  1656. if (!IsActive) {
  1657. BEnd(BENCH_PCP);
  1658. return;
  1659. }
  1660. }
  1661. }
  1662. /*
  1663. ** If after all is said and done, the unit finishes its move on an impassable cell, then
  1664. ** it must presume that it is in the case of a unit driving onto a bridge that blows up
  1665. ** before the unit completes it's move. In such a case the unit should have been destroyed
  1666. ** anyway, so blow it up now.
  1667. */
  1668. LandType land = Map[Coord].Land_Type();
  1669. if (!IsDriving && (land == LAND_ROCK || land == LAND_WATER || land == LAND_RIVER)) {
  1670. new AnimClass(Combat_Anim(Strength, WARHEAD_AP, land), Coord);
  1671. int damage = Strength;
  1672. Take_Damage(damage, 0, WARHEAD_AP, NULL, true);
  1673. return;
  1674. }
  1675. }
  1676. /*
  1677. ** Destroy any crushable wall that is driven over by a tracked vehicle.
  1678. */
  1679. CellClass * cellptr = &Map[cell];
  1680. if (Class->IsCrusher && cellptr->Overlay != OVERLAY_NONE) {
  1681. // if (Class->Speed == SPEED_TRACK && cellptr->Overlay != OVERLAY_NONE) {
  1682. OverlayTypeClass const * optr = &OverlayTypeClass::As_Reference(cellptr->Overlay);
  1683. if (optr->IsCrushable) {
  1684. if (optr->Type == OVERLAY_SANDBAG_WALL) {
  1685. Sound_Effect(VOC_SANDBAG, Center_Coord());
  1686. } else {
  1687. Sound_Effect(VOC_WALLKILL2, Center_Coord());
  1688. }
  1689. cellptr->Reduce_Wall(-1);
  1690. }
  1691. }
  1692. /*
  1693. ** Check to see if crushing of any unfortunate infantry is warranted.
  1694. */
  1695. Overrun_Square(Coord_Cell(Coord), false);
  1696. if (!IsActive) {
  1697. BEnd(BENCH_PCP);
  1698. return;
  1699. }
  1700. DriveClass::Per_Cell_Process(why);
  1701. BEnd(BENCH_PCP);
  1702. }
  1703. /***********************************************************************************************
  1704. * UnitClass::Shape_Number -- Fetch the shape number to use for this unit. *
  1705. * *
  1706. * This routine will calculate the shape number for this unit. The shape number is used *
  1707. * for the body of the unit. *
  1708. * *
  1709. * INPUT: none *
  1710. * *
  1711. * OUTPUT: Returns with the shape number to be used for this unit. *
  1712. * *
  1713. * WARNINGS: none *
  1714. * *
  1715. * HISTORY: *
  1716. * 07/29/1996 JLB : Created. *
  1717. *=============================================================================================*/
  1718. int UnitClass::Shape_Number(void) const
  1719. {
  1720. assert(Units.ID(this) == ID);
  1721. assert(IsActive);
  1722. int shapenum; // Working shape number.
  1723. int facing = Dir_To_32(PrimaryFacing);
  1724. int tfacing = Dir_To_32(SecondaryFacing);
  1725. DirType rotation = DIR_N;
  1726. #ifdef FIXIT_ANTS
  1727. /*
  1728. ** This handles the ant case.
  1729. */
  1730. if (Class->Rotation == 8) {
  1731. /*
  1732. ** The starting frame is based on the facing of the unit.
  1733. */
  1734. shapenum = ((UnitClass::BodyShape[facing]+2)/4) & 0x07;
  1735. /*
  1736. ** If the unit is driving, then it has an animation adjustment to the frame number.
  1737. */
  1738. if (IsDriving) {
  1739. shapenum = 8 + (shapenum * 8) + ((::Frame+ID)/2) % 8;
  1740. } else {
  1741. /*
  1742. ** If in combat, then do combat anims.
  1743. */
  1744. if (Arm > 0) {
  1745. shapenum = 8 + 64 + (shapenum * 4) + ((::Frame+ID)/2) % 4;
  1746. }
  1747. }
  1748. } else {
  1749. #endif
  1750. /*
  1751. ** Fetch the harvesting animation stage as appropriate.
  1752. */
  1753. if (IsHarvesting && !PrimaryFacing.Is_Rotating() && !NavCom && !IsDriving) {
  1754. // static char _hstage[] = {0, 1, 2, 3, 4, 5, 6, 7, 0};
  1755. unsigned stage = Fetch_Stage();
  1756. if (stage >= ARRAY_SIZE(Class->Harvester_Load_List)) stage = ARRAY_SIZE(Class->Harvester_Load_List)-1;
  1757. shapenum = 32 + (((UnitClass::BodyShape[facing]+2)/4)*Class->Harvester_Load_Count)+Class->Harvester_Load_List[stage];
  1758. } else {
  1759. /*
  1760. ** If the harvester's dumping a load of ore, show that animation
  1761. */
  1762. if (IsDumping) {
  1763. unsigned stage = Fetch_Stage();
  1764. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  1765. if (*this == UNIT_MAD) {
  1766. if (stage >= 8) {
  1767. stage = 7;
  1768. }
  1769. shapenum = 32 + stage + (UnitClass::BodyShape[facing]/4)*8;
  1770. } else {
  1771. if (stage >= ARRAY_SIZE(Class->Harvester_Dump_List)) stage = ARRAY_SIZE(Class->Harvester_Dump_List)-1;
  1772. shapenum = Class->Harvester_Dump_List[stage]+96;
  1773. }
  1774. #else
  1775. if (stage >= ARRAY_SIZE(Class->Harvester_Dump_List)) stage = ARRAY_SIZE(Class->Harvester_Dump_List)-1;
  1776. shapenum = Class->Harvester_Dump_List[stage]+96;
  1777. #endif
  1778. } else {
  1779. shapenum = UnitClass::BodyShape[facing];
  1780. if (Class->IsAnimating) {
  1781. shapenum = Fetch_Stage();
  1782. }
  1783. /*
  1784. ** Door opening and closing animation must be handled carefully. There are only
  1785. ** certain directions where this door animation will work.
  1786. */
  1787. if (!Is_Door_Closed() && (PrimaryFacing == DIR_NW || PrimaryFacing == DIR_NE)) {
  1788. if (PrimaryFacing == DIR_NE) {
  1789. shapenum = 32;
  1790. } else {
  1791. if (PrimaryFacing == DIR_NW) {
  1792. shapenum = 35;
  1793. }
  1794. }
  1795. shapenum += Door_Stage();
  1796. }
  1797. }
  1798. }
  1799. #ifdef FIXIT_ANTS
  1800. }
  1801. #endif
  1802. /*
  1803. ** The body of the V2 launcher indicates whether it is loaded with a missile
  1804. ** or not.
  1805. */
  1806. if (*this == UNIT_V2_LAUNCHER) {
  1807. if (Ammo == 0) shapenum += 32;
  1808. }
  1809. return(shapenum);
  1810. }
  1811. /***********************************************************************************************
  1812. * UnitClass::Draw_It -- Draws a unit object. *
  1813. * *
  1814. * This routine is the one that actually draws a unit object. It displays the unit *
  1815. * according to its current state flags and centered at the location specified. *
  1816. * *
  1817. * INPUT: x,y -- The X and Y coordinate of where to draw the unit. *
  1818. * *
  1819. * window -- The clipping window to use. *
  1820. * *
  1821. * OUTPUT: none *
  1822. * *
  1823. * WARNINGS: none *
  1824. * *
  1825. * HISTORY: *
  1826. * 06/20/1994 JLB : Created. *
  1827. * 06/27/1994 JLB : Takes a window parameter. *
  1828. * 08/15/1994 JLB : Removed infantry support. *
  1829. * 01/07/1995 JLB : Harvester animation support. *
  1830. * 07/08/1995 JLB : Uses general purpose draw routine. *
  1831. *=============================================================================================*/
  1832. void UnitClass::Draw_It(int x, int y, WindowNumberType window) const
  1833. {
  1834. assert(Units.ID(this) == ID);
  1835. assert(IsActive);
  1836. int shapenum; // Working shape number.
  1837. void const * shapefile; // Working shape file pointer.
  1838. int facing = Dir_To_32(PrimaryFacing);
  1839. int tfacing = Dir_To_32(SecondaryFacing);
  1840. DirType rotation = DIR_N;
  1841. int scale = 0x0100;
  1842. /*
  1843. ** Verify the legality of the unit class.
  1844. */
  1845. shapefile = Get_Image_Data();
  1846. if (shapefile == NULL) return;
  1847. /*
  1848. ** If drawing of this unit is not explicitly prohibited, then proceed
  1849. ** with the render process.
  1850. */
  1851. if (Visual_Character() != VISUAL_HIDDEN) {
  1852. shapenum = Shape_Number();
  1853. /*
  1854. ** The artillery unit should have its entire body recoil when it fires.
  1855. */
  1856. if (*this == UNIT_ARTY && IsInRecoilState) {
  1857. Recoil_Adjust(PrimaryFacing.Current(), x, y);
  1858. }
  1859. /*
  1860. ** Actually perform the draw. Overlay an optional shimmer effect as necessary.
  1861. */
  1862. Techno_Draw_Object(shapefile, shapenum, x, y, window, rotation, scale);
  1863. /*
  1864. ** If there is a rotating radar dish, draw it now.
  1865. */
  1866. if (Class->IsRadarEquipped) {
  1867. if (*this == UNIT_MGG) {
  1868. int x2 = x, y2 = y;
  1869. shapenum = 32 + (Frame & 7);
  1870. Class->Turret_Adjust(PrimaryFacing, x2, y2);
  1871. Techno_Draw_Object(shapefile, shapenum, x2, y2, window);
  1872. } else {
  1873. //#ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
  1874. // if (*this == UNIT_PHASE) {
  1875. // shapenum = 38 + (Frame & 7);
  1876. // } else {
  1877. // shapenum = 32 + (Frame % 32);
  1878. // }
  1879. //#else
  1880. shapenum = 32 + (Frame % 32);
  1881. //#endif
  1882. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  1883. if (*this == UNIT_TESLATANK) {
  1884. Techno_Draw_Object(shapefile, shapenum, x, y, window);
  1885. } else {
  1886. Techno_Draw_Object(shapefile, shapenum, x, y-5, window);
  1887. }
  1888. #else
  1889. Techno_Draw_Object(shapefile, shapenum, x, y-5, window);
  1890. #endif
  1891. }
  1892. }
  1893. /*
  1894. ** If there is a turret, then it must be rendered as well. This may include
  1895. ** firing animation if required.
  1896. */
  1897. if (/*!Class->IsChunkyShape &&*/ Class->IsTurretEquipped) {
  1898. int xx = x;
  1899. int yy = y;
  1900. /*
  1901. ** Determine which turret shape to use. This depends on if there
  1902. ** is any firing animation in progress.
  1903. */
  1904. shapenum = TechnoClass::BodyShape[tfacing]+32;
  1905. #ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
  1906. if (*this == UNIT_PHASE) {
  1907. shapenum += 6;
  1908. }
  1909. #endif
  1910. /*
  1911. ** A recoiling turret moves "backward" one pixel.
  1912. */
  1913. if (IsInRecoilState) {
  1914. Recoil_Adjust(SecondaryFacing, xx, yy);
  1915. }
  1916. Class->Turret_Adjust(PrimaryFacing, xx, yy);
  1917. /*
  1918. ** Actually perform the draw. Overlay an optional shimmer effect as necessary.
  1919. */
  1920. Techno_Draw_Object(shapefile, shapenum, xx, yy, window);
  1921. }
  1922. }
  1923. /*
  1924. ** If this unit is carrying the flag, then draw that on top of everything else.
  1925. */
  1926. if (Flagged != HOUSE_NONE) {
  1927. CC_Draw_Shape(MFCD::Retrieve("FLAGFLY.SHP"), Frame % 14, x, y, window, SHAPE_CENTER|SHAPE_FADING|SHAPE_GHOST, HouseClass::As_Pointer(Flagged)->Remap_Table(false, Class->Remap), Map.UnitShadow);
  1928. }
  1929. DriveClass::Draw_It(x, y, window);
  1930. }
  1931. /***********************************************************************************************
  1932. * UnitClass::Tiberium_Check -- Search for and head toward nearest available Tiberium patch. *
  1933. * *
  1934. * This routine is used to move a harvester to a place where it can load up with *
  1935. * Tiberium. It will return true only if it can start harvesting. Otherwise, it sets *
  1936. * the navigation computer toward the nearest Tiberium and lets the unit head there *
  1937. * automatically. *
  1938. * *
  1939. * INPUT: center -- Reference to the center of the radius scan. *
  1940. * *
  1941. * x,y -- Relative offset from the center cell to perform the check upon. *
  1942. * *
  1943. * OUTPUT: bool; Is it located directly over a Tiberium patch? *
  1944. * *
  1945. * WARNINGS: none *
  1946. * *
  1947. * HISTORY: *
  1948. * 07/18/1994 JLB : Created. *
  1949. *=============================================================================================*/
  1950. bool UnitClass::Tiberium_Check(CELL & center, int x, int y)
  1951. {
  1952. assert(Units.ID(this) == ID);
  1953. assert(IsActive);
  1954. /*
  1955. ** If the specified offset from the origin will cause it
  1956. ** to spill past the map edge, then abort this cell check.
  1957. */
  1958. if (Cell_X(center)+x < Map.MapCellX) return(false);
  1959. if (Cell_X(center)+x >= Map.MapCellX+Map.MapCellWidth) return(false);
  1960. if (Cell_Y(center)+y < Map.MapCellY) return(false);
  1961. if (Cell_Y(center)+y >= Map.MapCellY+Map.MapCellHeight) return(false);
  1962. center = XY_Cell(Cell_X(center)+x, Cell_Y(center)+y);
  1963. if ((Session.Type != GAME_NORMAL || (!IsOwnedByPlayer || Map[center].IsMapped))) {
  1964. if (Map[Coord].Zones[Class->MZone] != Map[center].Zones[Class->MZone]) return(false);
  1965. if (!Map[center].Cell_Techno() && Map[center].Land_Type() == LAND_TIBERIUM) {
  1966. return(true);
  1967. }
  1968. }
  1969. return(false);
  1970. }
  1971. /***********************************************************************************************
  1972. * UnitClass::Goto_Tiberium -- Searches for and heads toward tiberium. *
  1973. * *
  1974. * This routine will cause the unit to search for and head toward nearby Tiberium. When *
  1975. * the Tiberium is reached, then this routine should not be called again until such time *
  1976. * as additional harvesting is required. When this routine returns false, then it should *
  1977. * be called again until such time as it returns true. *
  1978. * *
  1979. * INPUT: rad = size of ring to search *
  1980. * *
  1981. * OUTPUT: Has the unit reached Tiberium and harvesting should begin? *
  1982. * *
  1983. * WARNINGS: none *
  1984. * *
  1985. * HISTORY: *
  1986. * 09/22/1995 JLB : Created. *
  1987. *=============================================================================================*/
  1988. bool UnitClass::Goto_Tiberium(int rad)
  1989. {
  1990. assert(Units.ID(this) == ID);
  1991. assert(IsActive);
  1992. if (!Target_Legal(NavCom)) {
  1993. CELL center = Coord_Cell(Center_Coord());
  1994. if (Map[center].Land_Type() == LAND_TIBERIUM) {
  1995. return(true);
  1996. } else {
  1997. /*
  1998. ** Perform a ring search outward from the center.
  1999. */
  2000. for (int radius = 1; radius < rad; radius++) {
  2001. for (int x = -radius; x <= radius; x++) {
  2002. CELL cell = center;
  2003. if (Tiberium_Check(cell, x, -radius)) {
  2004. Assign_Destination(::As_Target(cell));
  2005. return(false);
  2006. }
  2007. cell = center;
  2008. if (Tiberium_Check(cell, x, +radius)) {
  2009. Assign_Destination(::As_Target(cell));
  2010. return(false);
  2011. }
  2012. cell = center;
  2013. if (Tiberium_Check(cell, -radius, x)) {
  2014. Assign_Destination(::As_Target(cell));
  2015. return(false);
  2016. }
  2017. cell = center;
  2018. if (Tiberium_Check(cell, +radius, x)) {
  2019. Assign_Destination(::As_Target(cell));
  2020. return(false);
  2021. }
  2022. }
  2023. }
  2024. }
  2025. }
  2026. return(false);
  2027. }
  2028. /***********************************************************************************************
  2029. * UnitClass::Harvesting -- Harvests tiberium at the current location. *
  2030. * *
  2031. * This routine is used to by the harvester to harvest Tiberium at the current location. *
  2032. * When harvesting is complete, this routine will return true. *
  2033. * *
  2034. * INPUT: none *
  2035. * *
  2036. * OUTPUT: bool; Is harvesting complete? *
  2037. * *
  2038. * WARNINGS: none *
  2039. * *
  2040. * HISTORY: *
  2041. * 07/18/1994 JLB : Created. *
  2042. *=============================================================================================*/
  2043. bool UnitClass::Harvesting(void)
  2044. {
  2045. assert(Units.ID(this) == ID);
  2046. assert(IsActive);
  2047. CELL cell = Coord_Cell(Coord);
  2048. CellClass * ptr = &Map[cell];
  2049. /*
  2050. ** Keep waiting if still heading toward a spot to harvest.
  2051. */
  2052. if (Target_Legal(NavCom)) return(true);
  2053. if (Tiberium_Load() < 1 && ptr->Land_Type() == LAND_TIBERIUM) {
  2054. /*
  2055. ** Lift some Tiberium from the ground. Try to lift a complete
  2056. ** "level" of Tiberium. A level happens to be 6 steps. If there
  2057. ** is a partial level, then lift that instead. Never lift more
  2058. ** than the harvester can carry.
  2059. */
  2060. // int reducer = (ptr->OverlayData % 6) + 1;
  2061. int reducer = 1;
  2062. OverlayType overlay = ptr->Overlay;
  2063. reducer = ptr->Reduce_Tiberium(min(reducer, Rule.BailCount-Tiberium));
  2064. Tiberium += reducer;
  2065. switch (overlay) {
  2066. case OVERLAY_GOLD1:
  2067. case OVERLAY_GOLD2:
  2068. case OVERLAY_GOLD3:
  2069. case OVERLAY_GOLD4:
  2070. Gold += reducer;
  2071. break;
  2072. case OVERLAY_GEMS1:
  2073. case OVERLAY_GEMS2:
  2074. case OVERLAY_GEMS3:
  2075. case OVERLAY_GEMS4:
  2076. Gems += reducer;
  2077. if (Rule.BailCount > Tiberium) {Gems++;Tiberium++;}
  2078. if (Rule.BailCount > Tiberium) {Gems++;Tiberium++;}
  2079. if (Rule.BailCount > Tiberium) {Gems++;Tiberium++;}
  2080. break;
  2081. default:
  2082. break;
  2083. }
  2084. Set_Stage(0);
  2085. Set_Rate(Rule.OreDumpRate);
  2086. } else {
  2087. /*
  2088. ** If the harvester is stopped on a non Tiberium field and the harvester
  2089. ** isn't loaded with Tiberium, then no further action can be performed
  2090. ** by this logic routine. Bail with a failure and thus cause a branch to
  2091. ** a better suited logic processor.
  2092. */
  2093. Set_Stage(0);
  2094. Set_Rate(0);
  2095. return(false);
  2096. }
  2097. return(true);
  2098. }
  2099. /***********************************************************************************************
  2100. * UnitClass::Mission_Unload -- Handles unloading cargo. *
  2101. * *
  2102. * This is the AI control sequence for when a transport desires to unload its cargo and *
  2103. * then exit the map. *
  2104. * *
  2105. * INPUT: none *
  2106. * *
  2107. * OUTPUT: Returns with the delay before calling this routine again. *
  2108. * *
  2109. * WARNINGS: none *
  2110. * *
  2111. * HISTORY: *
  2112. * 07/18/1994 JLB : Created. *
  2113. *=============================================================================================*/
  2114. int UnitClass::Mission_Unload(void)
  2115. {
  2116. assert(Units.ID(this) == ID);
  2117. assert(IsActive);
  2118. enum {
  2119. INITIAL_CHECK,
  2120. MANEUVERING,
  2121. OPENING_DOOR,
  2122. UNLOADING,
  2123. CLOSING_DOOR
  2124. };
  2125. DirType dir;
  2126. CELL cell;
  2127. switch (Class->Type) {
  2128. case UNIT_HARVESTER:
  2129. if (PrimaryFacing != DIR_W) {
  2130. if (!IsRotating) {
  2131. Do_Turn(DIR_W);
  2132. }
  2133. return(5);
  2134. }
  2135. if (!IsDumping) {
  2136. IsDumping = true;
  2137. Set_Stage(0);
  2138. Set_Rate(Rule.OreDumpRate);
  2139. break;
  2140. }
  2141. if (Fetch_Stage() < ARRAY_SIZE(Class->Harvester_Dump_List)-1) break;
  2142. IsDumping = false;
  2143. if (Tiberium) {
  2144. Tiberium = 0;
  2145. int credits = Credit_Load();
  2146. House->Harvested(credits);
  2147. Tiberium = Gold = Gems = 0;
  2148. }
  2149. Transmit_Message(RADIO_OVER_OUT);
  2150. Assign_Mission(MISSION_HARVEST);
  2151. break;
  2152. case UNIT_TRUCK:
  2153. switch (Status) {
  2154. case INITIAL_CHECK:
  2155. dir = Desired_Load_Dir(NULL, cell);
  2156. if (How_Many() && cell != 0) {
  2157. Do_Turn(dir);
  2158. Status = MANEUVERING;
  2159. return(1);
  2160. } else {
  2161. Assign_Mission(MISSION_GUARD);
  2162. }
  2163. break;
  2164. case MANEUVERING:
  2165. if (!IsRotating) {
  2166. Status = UNLOADING;
  2167. return(1);
  2168. }
  2169. break;
  2170. case UNLOADING:
  2171. if (How_Many()) {
  2172. FootClass * passenger = Detach_Object();
  2173. if (passenger != NULL) {
  2174. DirType toface = DIR_S + PrimaryFacing;
  2175. bool placed = false;
  2176. for (FacingType face = FACING_N; face < FACING_COUNT; face++) {
  2177. DirType newface = toface + Facing_Dir(face);
  2178. CELL newcell = Adjacent_Cell(Coord_Cell(Coord), newface);
  2179. if (passenger->Can_Enter_Cell(newcell) == MOVE_OK) {
  2180. ScenarioInit++;
  2181. passenger->Unlimbo(Coord_Move(Coord, newface, 0x0080), newface);
  2182. ScenarioInit--;
  2183. passenger->Assign_Mission(MISSION_MOVE);
  2184. passenger->Assign_Destination(::As_Target(newcell));
  2185. placed = true;
  2186. break;
  2187. }
  2188. }
  2189. /*
  2190. ** If the attached unit could NOT be deployed, then re-attach
  2191. ** it and then bail out of this deploy process.
  2192. */
  2193. if (!placed) {
  2194. Attach(passenger);
  2195. Status = CLOSING_DOOR;
  2196. }
  2197. }
  2198. } else {
  2199. Status = CLOSING_DOOR;
  2200. }
  2201. break;
  2202. /*
  2203. ** Close APC door in preparation for normal operation.
  2204. */
  2205. case CLOSING_DOOR:
  2206. Assign_Mission(MISSION_GUARD);
  2207. break;
  2208. }
  2209. break;
  2210. case UNIT_APC:
  2211. #ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
  2212. case UNIT_PHASE:
  2213. #endif
  2214. switch (Status) {
  2215. case INITIAL_CHECK:
  2216. dir = Desired_Load_Dir(NULL, cell);
  2217. if (How_Many() && cell != 0) {
  2218. Do_Turn(dir);
  2219. Status = MANEUVERING;
  2220. return(1);
  2221. } else {
  2222. Assign_Mission(MISSION_GUARD);
  2223. }
  2224. break;
  2225. case MANEUVERING:
  2226. if (!IsRotating) {
  2227. APC_Open_Door();
  2228. if (Is_Door_Opening()) {
  2229. Status = OPENING_DOOR;
  2230. return(1);
  2231. }
  2232. }
  2233. break;
  2234. case OPENING_DOOR:
  2235. if (Is_Door_Open()) {
  2236. Status = UNLOADING;
  2237. return(1);
  2238. } else {
  2239. if (!Is_Door_Opening()) {
  2240. Status = INITIAL_CHECK;
  2241. }
  2242. }
  2243. break;
  2244. case UNLOADING:
  2245. if (How_Many()) {
  2246. FootClass * passenger = Detach_Object();
  2247. if (passenger != NULL) {
  2248. DirType toface = DIR_S + PrimaryFacing;
  2249. bool placed = false;
  2250. for (FacingType face = FACING_N; face < FACING_COUNT; face++) {
  2251. DirType newface = toface + Facing_Dir(face);
  2252. CELL newcell = Adjacent_Cell(Coord_Cell(Coord), newface);
  2253. if (passenger->Can_Enter_Cell(newcell) == MOVE_OK) {
  2254. ScenarioInit++;
  2255. passenger->Unlimbo(Coord_Move(Coord, newface, 0x0080), newface);
  2256. ScenarioInit--;
  2257. passenger->Assign_Mission(MISSION_MOVE);
  2258. passenger->Assign_Destination(::As_Target(newcell));
  2259. placed = true;
  2260. break;
  2261. }
  2262. }
  2263. /*
  2264. ** If the attached unit could NOT be deployed, then re-attach
  2265. ** it and then bail out of this deploy process.
  2266. */
  2267. if (!placed) {
  2268. Attach(passenger);
  2269. Status = CLOSING_DOOR;
  2270. }
  2271. }
  2272. } else {
  2273. Status = CLOSING_DOOR;
  2274. }
  2275. break;
  2276. /*
  2277. ** Close APC door in preparation for normal operation.
  2278. */
  2279. case CLOSING_DOOR:
  2280. if (Is_Door_Open()) {
  2281. APC_Close_Door();
  2282. }
  2283. if (Is_Door_Closed()) {
  2284. Assign_Mission(MISSION_GUARD);
  2285. }
  2286. break;
  2287. }
  2288. break;
  2289. case UNIT_MCV:
  2290. switch (Status) {
  2291. case 0:
  2292. Path[0] = FACING_NONE;
  2293. Status = 1;
  2294. break;
  2295. case 1:
  2296. if (!IsDriving) {
  2297. Try_To_Deploy();
  2298. if (IsActive) {
  2299. if (IsDeploying) {
  2300. Status = 2;
  2301. } else {
  2302. if (!House->IsHuman && Session.Type != GAME_NORMAL) {
  2303. Assign_Mission(MISSION_HUNT);
  2304. } else {
  2305. Assign_Mission(MISSION_GUARD);
  2306. }
  2307. }
  2308. }
  2309. }
  2310. break;
  2311. case 2:
  2312. if (!IsDeploying) {
  2313. Assign_Mission(MISSION_GUARD);
  2314. }
  2315. break;
  2316. }
  2317. return(1);
  2318. case UNIT_MINELAYER:
  2319. switch (Status) {
  2320. case INITIAL_CHECK:
  2321. dir = DIR_NE;
  2322. if (Ammo > 0) {
  2323. Do_Turn(dir);
  2324. Status = MANEUVERING;
  2325. return(1);
  2326. } else {
  2327. Assign_Mission(MISSION_GUARD);
  2328. }
  2329. break;
  2330. case MANEUVERING:
  2331. if (!IsRotating) {
  2332. APC_Open_Door();
  2333. if (Is_Door_Opening()) {
  2334. Status = OPENING_DOOR;
  2335. return(1);
  2336. }
  2337. }
  2338. break;
  2339. case OPENING_DOOR:
  2340. if (Is_Door_Open()) {
  2341. Status = UNLOADING;
  2342. return(1);
  2343. } else {
  2344. if (!Is_Door_Opening()) {
  2345. Status = INITIAL_CHECK;
  2346. }
  2347. }
  2348. break;
  2349. case UNLOADING:
  2350. if (Ammo > 0) {
  2351. if (!Map[Center_Coord()].Cell_Building()) {
  2352. Mark(MARK_UP);
  2353. BuildingClass * building = new BuildingClass((House->ActLike == HOUSE_USSR || House->ActLike == HOUSE_UKRAINE || House->ActLike == HOUSE_BAD) ? STRUCT_APMINE : STRUCT_AVMINE, House->Class->House);
  2354. if (building != NULL) {
  2355. ScenarioInit = true;
  2356. if (building->Unlimbo(Coord)) {
  2357. Sound_Effect(VOC_MINELAY1, Coord);
  2358. ScenarioInit = false;
  2359. building->Revealed(House);
  2360. Ammo--;
  2361. }
  2362. ScenarioInit = false;
  2363. }
  2364. Status = CLOSING_DOOR;
  2365. Mark(MARK_DOWN);
  2366. } else {
  2367. Status = CLOSING_DOOR;
  2368. }
  2369. } else {
  2370. Status = CLOSING_DOOR;
  2371. }
  2372. break;
  2373. /*
  2374. ** Close APC door in preparation for normal operation.
  2375. */
  2376. case CLOSING_DOOR:
  2377. if (Is_Door_Open()) {
  2378. APC_Close_Door();
  2379. }
  2380. if (Is_Door_Closed()) {
  2381. Assign_Mission(MISSION_GUARD);
  2382. }
  2383. break;
  2384. }
  2385. break;
  2386. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  2387. case UNIT_MAD:
  2388. if (!Gems && !IsDumping) {
  2389. Gems = 1;
  2390. Gold = 0;
  2391. Arm = QuakeDelay * House->ROFBias;
  2392. #ifdef ENGLISH
  2393. Speak(VOX_MADTANK_DEPLOYED); // this voice only exists in English
  2394. #else
  2395. Sound_Effect(VOC_BUZZY1,Center_Coord());
  2396. #endif
  2397. Set_Stage(0);
  2398. Set_Rate(Rule.OreDumpRate*2);
  2399. IsDumping = true;
  2400. #if 1
  2401. InfantryClass *crew = new InfantryClass(INFANTRY_C1, House->Class->House);
  2402. if (crew != NULL) crew->IsTechnician = true;
  2403. if (crew != NULL) {
  2404. DirType toface = DIR_S + PrimaryFacing;
  2405. for (FacingType face = FACING_N; face < FACING_COUNT; face++) {
  2406. DirType newface = toface + Facing_Dir(face);
  2407. CELL newcell = Adjacent_Cell(Coord_Cell(Coord), newface);
  2408. if (crew->Can_Enter_Cell(newcell) == MOVE_OK) {
  2409. ScenarioInit++;
  2410. crew->Unlimbo(Coord_Move(Coord, newface, 0x0080), newface);
  2411. ScenarioInit--;
  2412. crew->Assign_Mission(MISSION_MOVE);
  2413. crew->Assign_Destination(::As_Target(newcell));
  2414. break;
  2415. }
  2416. }
  2417. }
  2418. #endif
  2419. }
  2420. if ( (Arm && !Gold) || IronCurtainCountDown) {
  2421. Set_Stage(Fetch_Stage() & 1);
  2422. return(1);
  2423. }
  2424. if (!Gold) {
  2425. Sound_Effect(VOC_MAD_CHARGE, Center_Coord());
  2426. Set_Stage(0);
  2427. Gold = 1;
  2428. return(1);
  2429. }
  2430. if (Fetch_Stage() < 7) {
  2431. return(1);
  2432. }
  2433. IsDumping = false;
  2434. Sound_Effect(VOC_MAD_EXPLODE, Center_Coord());
  2435. Strength = 1; // assure destruction
  2436. PendingTimeQuake = true; // trigger a time quake
  2437. TimeQuakeCenter = ::As_Target(Center_Coord());
  2438. break;
  2439. case UNIT_CHRONOTANK:
  2440. if (IsOwnedByPlayer) {
  2441. Map.IsTargettingMode = SPC_CHRONO2;
  2442. Unselect_All();
  2443. }
  2444. House->UnitToTeleport = As_Target();
  2445. Assign_Mission(MISSION_GUARD);
  2446. break;
  2447. #endif
  2448. default:
  2449. break;
  2450. }
  2451. return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  2452. }
  2453. /***********************************************************************************************
  2454. * UnitClass::Mission_Harvest -- Handles the harvesting process used by harvesters. *
  2455. * *
  2456. * This is the AI process used by harvesters when they are doing their harvesting action. *
  2457. * This entails searching for nearby Tiberium, heading there, harvesting, and then *
  2458. * returning to a refinery for unloading. *
  2459. * *
  2460. * INPUT: none *
  2461. * *
  2462. * OUTPUT: Returns with the delay before calling this routine again. *
  2463. * *
  2464. * WARNINGS: none *
  2465. * *
  2466. * HISTORY: *
  2467. * 07/18/1994 JLB : Created. *
  2468. * 06/21/1995 JLB : Force guard mode if no Tiberium found. *
  2469. * 09/28/1995 JLB : Aborts harvesting if there are no more refineries. *
  2470. *=============================================================================================*/
  2471. int UnitClass::Mission_Harvest(void)
  2472. {
  2473. assert(Units.ID(this) == ID);
  2474. assert(IsActive);
  2475. enum {
  2476. LOOKING,
  2477. HARVESTING,
  2478. FINDHOME,
  2479. HEADINGHOME,
  2480. GOINGTOIDLE,
  2481. };
  2482. /*
  2483. ** A non-harvesting type unit will just sit still if it is given the harvest mission. This
  2484. ** allows combat units to act "brain dead".
  2485. */
  2486. if (!Class->IsToHarvest) return(TICKS_PER_SECOND*30);
  2487. /*
  2488. ** If there are no more refineries, then drop into guard mode.
  2489. */
  2490. if (!(House->ActiveBScan & STRUCTF_REFINERY)) {
  2491. Assign_Mission(MISSION_GUARD);
  2492. return(1);
  2493. }
  2494. switch (Status) {
  2495. /*
  2496. ** Go and find a Tiberium field to harvest.
  2497. */
  2498. case LOOKING:
  2499. /*
  2500. ** When full of tiberium, just skip to finding a free refinery
  2501. ** to unload at.
  2502. */
  2503. if (Tiberium_Load() == 1) {
  2504. Status = FINDHOME;
  2505. return(1);
  2506. }
  2507. /*
  2508. ** Look for ore where we last found some - mine the same patch
  2509. */
  2510. if (Target_Legal(ArchiveTarget)) {
  2511. Assign_Destination(ArchiveTarget);
  2512. ArchiveTarget = 0;
  2513. }
  2514. IsHarvesting = false;
  2515. if (Goto_Tiberium(Rule.TiberiumLongScan / CELL_LEPTON_W)) {
  2516. IsHarvesting = true;
  2517. Set_Rate(2);
  2518. Set_Stage(0);
  2519. Status = HARVESTING;
  2520. return(1);
  2521. } else {
  2522. /*
  2523. ** If the harvester isn't on Tiberium and it is not heading toward Tiberium, then
  2524. ** force it to go into guard mode. This will prevent the harvester from repeatedly
  2525. ** searching for Tiberium.
  2526. */
  2527. if (!Target_Legal(NavCom)) {
  2528. /*
  2529. ** If the archive target is legal, then head there since it is presumed
  2530. ** that the archive target points to the last place it harvested at. This might
  2531. ** solve the case where the harvester gets stuck and can't find Tiberium just because
  2532. ** it is greater than 32 squares away.
  2533. */
  2534. if (Target_Legal(ArchiveTarget)) {
  2535. Assign_Destination(ArchiveTarget);
  2536. } else {
  2537. Status = GOINGTOIDLE;
  2538. IsUseless = true;
  2539. House->IsTiberiumShort = true;
  2540. return(TICKS_PER_SECOND*7);
  2541. }
  2542. } else {
  2543. IsUseless = false;
  2544. }
  2545. }
  2546. break;
  2547. /*
  2548. ** Harvest at current location until full or Tiberium exhausted.
  2549. */
  2550. case HARVESTING:
  2551. // if (Fetch_Stage() > ARRAY_SIZE(Class->Harvester_Load_List)) {
  2552. // Set_Stage(0);
  2553. // }
  2554. if (Fetch_Rate() == 0) {
  2555. Set_Stage(0);
  2556. Set_Rate(Rule.OreDumpRate);
  2557. }
  2558. if (Fetch_Stage() < ARRAY_SIZE(Class->Harvester_Load_List)) return(1);
  2559. if (!Harvesting()) {
  2560. IsHarvesting = false;
  2561. if (Tiberium_Load() == 1) {
  2562. Status = FINDHOME;
  2563. ArchiveTarget = ::As_Target(Coord_Cell(Coord));
  2564. } else {
  2565. if (!Goto_Tiberium(Rule.TiberiumShortScan / CELL_LEPTON_W) && !Target_Legal(NavCom)) {
  2566. ArchiveTarget = TARGET_NONE;
  2567. Status = FINDHOME;
  2568. } else {
  2569. Status = HARVESTING;
  2570. IsHarvesting = true;
  2571. }
  2572. }
  2573. return(1);
  2574. }
  2575. return(1);
  2576. // return(TICKS_PER_SECOND*Rule.OreDumpRate);
  2577. /*
  2578. ** Find and head to refinery.
  2579. */
  2580. case FINDHOME:
  2581. if (!Target_Legal(NavCom)) {
  2582. /*
  2583. ** Find nearby refinery and head to it?
  2584. */
  2585. BuildingClass * nearest = Find_Docking_Bay(STRUCT_REFINERY, false);
  2586. /*
  2587. ** Since the refinery said it was ok to load, establish radio
  2588. ** contact with the refinery and then await docking orders.
  2589. */
  2590. if (nearest != NULL && Transmit_Message(RADIO_HELLO, nearest) == RADIO_ROGER) {
  2591. Status = HEADINGHOME;
  2592. if (nearest->House == PlayerPtr && (PlayerPtr->Capacity - PlayerPtr->Tiberium) < 300 && PlayerPtr->Capacity > 500 && (PlayerPtr->ActiveBScan & (STRUCTF_REFINERY | STRUCTF_CONST))) {
  2593. Speak(VOX_NEED_MO_CAPACITY);
  2594. }
  2595. } else {
  2596. ScenarioInit++;
  2597. nearest = Find_Docking_Bay(STRUCT_REFINERY, false);
  2598. ScenarioInit--;
  2599. if (nearest != NULL) {
  2600. Assign_Destination(::As_Target(Nearby_Location(nearest)));
  2601. }
  2602. }
  2603. }
  2604. break;
  2605. /*
  2606. ** In communication with refinery so that it will successfully dock and
  2607. ** unload. If, for some reason, radio contact was lost, then hunt for
  2608. ** another refinery to unload at.
  2609. */
  2610. case HEADINGHOME:
  2611. Assign_Mission(MISSION_ENTER);
  2612. return(1);
  2613. /*
  2614. ** The harvester has nothing to do. There is no Tiberium nearby and
  2615. ** no where to go.
  2616. */
  2617. case GOINGTOIDLE:
  2618. if (IsUseless) {
  2619. if (House->ActiveBScan & STRUCTF_REPAIR) {
  2620. Assign_Mission(MISSION_REPAIR);
  2621. } else {
  2622. Assign_Mission(MISSION_HUNT);
  2623. }
  2624. }
  2625. Assign_Mission(MISSION_GUARD);
  2626. break;
  2627. }
  2628. return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  2629. }
  2630. /***********************************************************************************************
  2631. * UnitClass::Mission_Hunt -- This is the AI process for aggressive enemy units. *
  2632. * *
  2633. * Computer controlled units must be intelligent enough to find enemies as well as to *
  2634. * attack them. This AI process will handle both the simple attack process as well as the *
  2635. * scanning for enemy units to attack. *
  2636. * *
  2637. * INPUT: none *
  2638. * *
  2639. * OUTPUT: Returns with the delay before calling this routine again. *
  2640. * *
  2641. * WARNINGS: none *
  2642. * *
  2643. * HISTORY: *
  2644. * 07/18/1994 JLB : Created. *
  2645. *=============================================================================================*/
  2646. int UnitClass::Mission_Hunt(void)
  2647. {
  2648. assert(Units.ID(this) == ID);
  2649. assert(IsActive);
  2650. if (*this == UNIT_MCV) {
  2651. enum {
  2652. FIND_SPOT,
  2653. WAITING
  2654. };
  2655. switch (Status) {
  2656. /*
  2657. ** This stage handles locating a convenient spot, rotating to face the correct
  2658. ** direction and then commencing the deployment operation.
  2659. */
  2660. case FIND_SPOT:
  2661. if (Goto_Clear_Spot()) {
  2662. if (Try_To_Deploy()) {
  2663. Status = WAITING;
  2664. }
  2665. }
  2666. break;
  2667. /*
  2668. ** This stage watchdogs the deployment operation and if for some reason, the deployment
  2669. ** is aborted (the IsDeploying flag becomes false), then it reverts back to hunting for
  2670. ** a convenient spot to deploy.
  2671. */
  2672. case WAITING:
  2673. if (!IsDeploying) {
  2674. Status = FIND_SPOT;
  2675. }
  2676. break;
  2677. }
  2678. } else {
  2679. return(DriveClass::Mission_Hunt());
  2680. }
  2681. return(MissionControl[Mission].Normal_Delay()+Random_Pick(0, 2));
  2682. }
  2683. /***********************************************************************************************
  2684. * UnitClass::Overlap_List -- Determines overlap list for units. *
  2685. * *
  2686. * The unit overlap list is used to keep track of which cells are to *
  2687. * be marked for redraw. This is critical in order to keep the units *
  2688. * displayed correctly. *
  2689. * *
  2690. * INPUT: none *
  2691. * *
  2692. * OUTPUT: Returns with the overlap list pointer for the unit at its *
  2693. * present position. *
  2694. * *
  2695. * WARNINGS: none *
  2696. * *
  2697. * HISTORY: *
  2698. * 05/26/1994 JLB : Created. *
  2699. * 06/19/1994 JLB : Uses Coord_Spillable_List function. *
  2700. *=============================================================================================*/
  2701. short const * UnitClass::Overlap_List(bool redraw) const
  2702. {
  2703. assert(Units.ID(this) == ID);
  2704. assert(IsActive);
  2705. #ifdef PARTIAL
  2706. if (Height == 0 && redraw && Class->DimensionData != NULL) {
  2707. Rect rect;
  2708. int shapenum = Shape_Number();
  2709. if (Class->DimensionData[shapenum].Is_Valid()) {
  2710. rect = Class->DimensionData[shapenum];
  2711. } else {
  2712. rect = Class->DimensionData[shapenum] = Shape_Dimensions(Get_Image_Data(), shapenum);
  2713. }
  2714. if (IsSelected) {
  2715. rect = Union(rect, Rect(-15, -15, 30, 30));
  2716. }
  2717. if (Class->IsTurretEquipped || Class->IsRadarEquipped) {
  2718. rect = Union(rect, Rect(-15, -15, 30, 30));
  2719. }
  2720. return(Coord_Spillage_List(Coord, rect, true));
  2721. }
  2722. #else
  2723. redraw = redraw;
  2724. #endif
  2725. int size = ICON_PIXEL_W;
  2726. if (redraw && (IsSelected || IsFiring)) {
  2727. size += 24;
  2728. }
  2729. if (Class->IsGigundo || IsAnimAttached) {
  2730. size = ICON_PIXEL_W*2;
  2731. }
  2732. return(Coord_Spillage_List(Coord, size)+1);
  2733. }
  2734. /***********************************************************************************************
  2735. * UnitClass::Can_Enter_Cell -- Determines cell entry legality. *
  2736. * *
  2737. * Use this routine to determine if the unit can enter the cell *
  2738. * specified and given the direction of entry specified. Typically, *
  2739. * this is used when determining unit travel path. *
  2740. * *
  2741. * INPUT: cell -- The cell to examine. *
  2742. * *
  2743. * facing -- The facing that the unit would enter the specified *
  2744. * cell. If this value is -1, then don't consider *
  2745. * facing when performing the check. *
  2746. * *
  2747. * OUTPUT: Returns the reason why it couldn't enter the cell or MOVE_OK if movement is *
  2748. * allowed. *
  2749. * *
  2750. * WARNINGS: none *
  2751. * *
  2752. * HISTORY: *
  2753. * 09/07/1992 JLB : Created. *
  2754. * 04/16/1994 JLB : Converted to member function. *
  2755. * 07/04/1995 JLB : Allowed to drive on building trying to enter it. *
  2756. *=============================================================================================*/
  2757. MoveType UnitClass::Can_Enter_Cell(CELL cell, FacingType ) const
  2758. {
  2759. assert(Units.ID(this) == ID);
  2760. assert(IsActive);
  2761. bool cancrush = false;
  2762. CellClass const * cellptr = &Map[cell];
  2763. if ((unsigned)cell >= MAP_CELL_TOTAL) return(MOVE_NO);
  2764. /*
  2765. ** Moving off the edge of the map is not allowed unless
  2766. ** this is a loaner vehicle.
  2767. */
  2768. if (!ScenarioInit && !Map.In_Radar(cell) && !Is_Allowed_To_Leave_Map() && IsLocked) {
  2769. return(MOVE_NO);
  2770. }
  2771. MoveType retval = MOVE_OK;
  2772. /*
  2773. ** Certain vehicles can drive over walls. Check for this case and
  2774. ** and return the appropriate flag. Other units treat walls as impassable.
  2775. */
  2776. if (cellptr->Overlay != OVERLAY_NONE) {
  2777. OverlayTypeClass const * optr = &OverlayTypeClass::As_Reference(cellptr->Overlay);
  2778. if (optr->IsCrate && !((Session.Type == GAME_NORMAL) ? House->IsPlayerControl : House->IsHuman) && Session.Type == GAME_NORMAL) {
  2779. return(MOVE_NO);
  2780. }
  2781. if (optr->IsWall) {
  2782. /*
  2783. ** If the blocking wall is crushable (and not owned by this player or one of this players
  2784. ** allies, then record that it is crushable and let the normal logic take over. The end
  2785. ** result should cause this unit to consider the cell passable.
  2786. */
  2787. if (optr->IsCrushable && Class->IsCrusher) {
  2788. cancrush = !House->Is_Ally(cellptr->Owner);
  2789. }
  2790. if (!cancrush && Is_Weapon_Equipped()) {
  2791. WarheadTypeClass const * whead = Class->PrimaryWeapon->WarheadPtr;
  2792. if (whead->IsWallDestroyer || (whead->IsWoodDestroyer && optr->IsWooden)) {
  2793. // if (!House->IsHuman && !House->Is_Ally(cellptr->Owner)) {
  2794. if (retval < MOVE_DESTROYABLE) retval = MOVE_DESTROYABLE;
  2795. // } else {
  2796. // return(MOVE_NO);
  2797. // }
  2798. } else {
  2799. return(MOVE_NO);
  2800. }
  2801. }
  2802. }
  2803. }
  2804. /*
  2805. ** Loop through all of the objects in the square setting a bit
  2806. ** for how they affect movement.
  2807. */
  2808. bool crushable = false;
  2809. ObjectClass * obj = cellptr->Cell_Occupier();
  2810. while (obj != NULL) {
  2811. if (obj != this) {
  2812. /*
  2813. ** If object is a land mine, allow movement if possible.
  2814. */
  2815. if (obj->What_Am_I() == RTTI_BUILDING && (!Rule.IsMineAware || !((BuildingClass *)obj)->House->Is_Ally(House))) {
  2816. if ((*(BuildingClass *)obj) == STRUCT_APMINE) return(MOVE_OK);
  2817. if ((*(BuildingClass *)obj) == STRUCT_AVMINE) return(MOVE_OK);
  2818. }
  2819. /*
  2820. ** Always allow entry if trying to move on a cell with
  2821. ** authorization from the occupier.
  2822. */
  2823. if (obj == Contact_With_Whom() && (IsTethered || (obj->What_Am_I() == RTTI_BUILDING && *((BuildingClass *)obj) == STRUCT_REPAIR))) {
  2824. return(MOVE_OK);
  2825. }
  2826. /*
  2827. ** Special check to allow entry into the sea transport this vehicle
  2828. ** is trying to enter.
  2829. */
  2830. if (Mission == MISSION_ENTER && obj->As_Target() == NavCom && IsTethered) {
  2831. return(MOVE_OK);
  2832. }
  2833. /*
  2834. ** Guard area should not allow the guarding unit to enter the cell with the
  2835. ** guarded unit.
  2836. */
  2837. if (Mission == MISSION_GUARD_AREA && ArchiveTarget == obj->As_Target()) {
  2838. return(MOVE_NO);
  2839. }
  2840. bool is_moving = obj->Is_Foot() &&
  2841. (Target_Legal(((FootClass *)obj)->NavCom) || ((FootClass *)obj)->PrimaryFacing.Is_Rotating() || ((FootClass *)obj)->IsDriving);
  2842. // (((FootClass *)obj)->PrimaryFacing.Is_Rotating() || ((FootClass *)obj)->IsDriving);
  2843. // (((FootClass *)obj)->IsRotating || ((FootClass *)obj)->IsDriving);
  2844. // (Target_Legal(((FootClass *)obj)->NavCom) || ((FootClass *)obj)->IsDriving);
  2845. if (House->Is_Ally(obj)) {
  2846. if (is_moving) {
  2847. int face = Dir_Facing(PrimaryFacing);
  2848. int techface = Dir_Facing(((FootClass const *)obj)->PrimaryFacing) ^4;
  2849. if (face == techface && Distance((AbstractClass const *)obj) <= 0x1FF) {
  2850. return(MOVE_NO);
  2851. }
  2852. if (retval < MOVE_MOVING_BLOCK) retval = MOVE_MOVING_BLOCK;
  2853. } else {
  2854. if (obj->What_Am_I() == RTTI_BUILDING) return(MOVE_NO);
  2855. /*
  2856. ** If the blocking object is not in the same zone, then it certainly
  2857. ** isn't a temporary block, it is a permanent one.
  2858. */
  2859. if (Map[Coord].Zones[Class->MZone] != cellptr->Zones[Class->MZone]) return(MOVE_NO);
  2860. if (retval < MOVE_TEMP) retval = MOVE_TEMP;
  2861. }
  2862. } else {
  2863. /*
  2864. ** Cloaked enemy objects are not considered if this is a Find_Path()
  2865. ** call.
  2866. */
  2867. if (!obj->Is_Techno() || ((TechnoClass *)obj)->Cloak != CLOAKED) {
  2868. /*
  2869. ** If this unit can crush infantry, and there is an enemy infantry in the
  2870. ** cell, don't consider the cell impassible. This is true even if the unit
  2871. ** doesn't contain a legitimate weapon.
  2872. */
  2873. bool crusher = Class->IsCrusher;
  2874. if (!crusher || !obj->Class_Of().IsCrushable) {
  2875. /*
  2876. ** Any non-allied blockage is considered impassable if the unit
  2877. ** is not equipped with a weapon.
  2878. */
  2879. if (Class->PrimaryWeapon == NULL) return(MOVE_NO);
  2880. /*
  2881. ** Some kinds of terrain are considered destroyable if the unit is equipped
  2882. ** with the weapon that can destroy it. Otherwise, the terrain is considered
  2883. ** impassable.
  2884. */
  2885. switch (obj->What_Am_I()) {
  2886. case RTTI_TERRAIN:
  2887. #ifdef TOFIX
  2888. if (((TerrainClass *)obj)->Class->Armor == ARMOR_WOOD &&
  2889. Class->PrimaryWeapon->WarheadPtr->IsWoodDestroyer) {
  2890. if (retval < MOVE_DESTROYABLE) retval = MOVE_DESTROYABLE;
  2891. } else {
  2892. return(MOVE_NO);
  2893. }
  2894. break;
  2895. #else
  2896. return(MOVE_NO);
  2897. #endif
  2898. default:
  2899. if (retval < MOVE_DESTROYABLE) retval = MOVE_DESTROYABLE;
  2900. break;
  2901. }
  2902. } else {
  2903. crushable = true;
  2904. }
  2905. } else {
  2906. if (retval < MOVE_CLOAK) retval = MOVE_CLOAK;
  2907. }
  2908. }
  2909. }
  2910. /*
  2911. ** Move to next object in chain.
  2912. */
  2913. obj = obj->Next;
  2914. }
  2915. /*
  2916. ** If the cell is out and out impassable because of underlying terrain, then
  2917. ** return this immutable fact.
  2918. */
  2919. if (!cancrush && retval != MOVE_DESTROYABLE && Ground[cellptr->Land_Type()].Cost[Class->Speed] == 0) {
  2920. return(MOVE_NO);
  2921. }
  2922. /*
  2923. ** If some allied object has reserved the cell, then consider the cell
  2924. ** as blocked by a moving object.
  2925. */
  2926. if (retval == MOVE_OK && !crushable && (cellptr->Flag.Composite & 0x3F) != 0) {
  2927. /*
  2928. ** If reserved by a vehicle, then consider this blocked terrain.
  2929. */
  2930. if (cellptr->Flag.Occupy.Vehicle) {
  2931. retval = MOVE_MOVING_BLOCK;
  2932. } else {
  2933. if (cellptr->InfType != HOUSE_NONE && House->Is_Ally(cellptr->InfType)) {
  2934. retval = MOVE_MOVING_BLOCK;
  2935. } else {
  2936. /*
  2937. ** Enemy infantry have reserved the cell. If this unit can crush
  2938. ** infantry, consider the cell passable. If not, then consider the
  2939. ** cell destroyable if it has a weapon. If neither case applies, then
  2940. ** this vehicle should avoid the cell altogether.
  2941. */
  2942. if (!Class->IsCrusher) {
  2943. if (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->Bullet->IsAntiGround) {
  2944. retval = MOVE_DESTROYABLE;
  2945. } else {
  2946. return(MOVE_NO);
  2947. }
  2948. }
  2949. }
  2950. }
  2951. }
  2952. /*
  2953. ** If its ok to move into the cell because we can crush whats in the cell, then
  2954. ** make sure no one else is already moving into the cell to crush something.
  2955. */
  2956. if (retval == MOVE_OK && crushable && cellptr->Flag.Occupy.Vehicle) {
  2957. /*
  2958. ** However, if the cell is occupied by a crushable vehicle, then we can
  2959. ** never be sure if some other friendly vehicle is also trying to crush
  2960. ** the cell at the same time. In the case of a crushable vehicle in the
  2961. ** cell, then allow entry.
  2962. */
  2963. if (!cellptr->Cell_Unit() || !cellptr->Cell_Unit()->Class->IsCrushable) {
  2964. return(MOVE_MOVING_BLOCK);
  2965. }
  2966. }
  2967. /*
  2968. ** Return with the most severe reason why this cell would be impassable.
  2969. */
  2970. return(retval);
  2971. }
  2972. /***********************************************************************************************
  2973. * UnitClass::Init -- Clears all units for scenario preparation. *
  2974. * *
  2975. * This routine will zero out the unit list and unit objects. This routine is typically *
  2976. * used in preparation for a new scenario load. All units are guaranteed to be eliminated *
  2977. * by this routine. *
  2978. * *
  2979. * INPUT: none *
  2980. * *
  2981. * OUTPUT: none *
  2982. * *
  2983. * WARNINGS: none *
  2984. * *
  2985. * HISTORY: *
  2986. * 08/15/1994 JLB : Created. *
  2987. *=============================================================================================*/
  2988. void UnitClass::Init(void)
  2989. {
  2990. Units.Free_All();
  2991. }
  2992. /***********************************************************************************************
  2993. * UnitClass::Start_Driver -- Starts driving and reserves destination cell. *
  2994. * *
  2995. * This routine will start the vehicle moving by marking the destination cell as *
  2996. * reserved. Cells must be reserved in this fashion or else they might become occupied as *
  2997. * the vehicle is proceeding toward it. *
  2998. * *
  2999. * INPUT: headto -- The location where the vehicle will be heading. *
  3000. * *
  3001. * OUTPUT: bool; Was the vehicle started to move? Failure could be the result of the cell *
  3002. * being occupied. *
  3003. * *
  3004. * WARNINGS: none *
  3005. * *
  3006. * HISTORY: *
  3007. * 12/22/1994 JLB : Created. *
  3008. *=============================================================================================*/
  3009. bool UnitClass::Start_Driver(COORDINATE & headto)
  3010. {
  3011. assert(Units.ID(this) == ID);
  3012. assert(IsActive);
  3013. if (DriveClass::Start_Driver(headto) && IsActive) {//BG IsActive can be cleared by Start_Driver
  3014. Mark_Track(headto, MARK_DOWN);
  3015. return(true);
  3016. }
  3017. return(false);
  3018. }
  3019. /***********************************************************************************************
  3020. * UnitClass::What_Action -- Determines what action would occur if clicked on object. *
  3021. * *
  3022. * Use this function to determine what action would likely occur if the specified object *
  3023. * were clicked on while this unit was selected as current. This function controls, not *
  3024. * only the action to perform, but indirectly controls the cursor shape to use as well. *
  3025. * *
  3026. * INPUT: object -- The object that to check for against "this" object. *
  3027. * *
  3028. * OUTPUT: Returns with the default action to perform. If no clear action can be determined, *
  3029. * then ACTION_NONE is returned. *
  3030. * *
  3031. * WARNINGS: none *
  3032. * *
  3033. * HISTORY: *
  3034. * 01/11/1995 JLB : Created. *
  3035. *=============================================================================================*/
  3036. ActionType UnitClass::What_Action(ObjectClass const * object) const
  3037. {
  3038. assert(Units.ID(this) == ID);
  3039. assert(IsActive);
  3040. ActionType action = DriveClass::What_Action(object);
  3041. /*
  3042. ** Allow units to move onto land mines.
  3043. */
  3044. if (action == ACTION_NONE && object->What_Am_I() == RTTI_BUILDING) {
  3045. StructType blah = *((BuildingClass *)object);
  3046. if (blah == STRUCT_AVMINE || blah == STRUCT_APMINE) return(ACTION_MOVE);
  3047. }
  3048. /*
  3049. ** If the unit doesn't have a weapon, but can crush the object, then consider
  3050. ** the object as a movable location.
  3051. */
  3052. if (action == ACTION_ATTACK && !Can_Player_Fire()) {
  3053. if (Class->IsCrusher && object->Class_Of().IsCrushable) {
  3054. action = ACTION_MOVE;
  3055. } else {
  3056. action = ACTION_SELECT;
  3057. }
  3058. }
  3059. /*
  3060. ** Don't allow special deploy action unless there is something to deploy.
  3061. */
  3062. if (action == ACTION_SELF) {
  3063. if (*this == UNIT_MCV) {
  3064. /*
  3065. ** The MCV will get the no-deploy cursor if it couldn't
  3066. ** deploy at its current location.
  3067. */
  3068. ((ObjectClass &)(*this)).Mark(MARK_UP);
  3069. if (!BuildingTypeClass::As_Reference(STRUCT_CONST).Legal_Placement(Coord_Cell(Adjacent_Cell(Center_Coord(), FACING_NW)))) {
  3070. action = ACTION_NO_DEPLOY;
  3071. }
  3072. ((ObjectClass &)(*this)).Mark(MARK_DOWN);
  3073. } else {
  3074. /*
  3075. ** The mine layer can "deploy" its mines if it currently isn't
  3076. ** sitting on top of a mine and it still has mines available.
  3077. */
  3078. if (*this == UNIT_MINELAYER) {
  3079. if (!Ammo || Map[Center_Coord()].Cell_Building() || (Map[Center_Coord()].Smudge != SMUDGE_NONE && SmudgeTypeClass::As_Reference(Map[Center_Coord()].Smudge).IsBib)) {
  3080. action = ACTION_NO_DEPLOY;
  3081. }
  3082. } else {
  3083. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  3084. if (*this == UNIT_CHRONOTANK || *this == UNIT_MAD) {
  3085. if (*this == UNIT_CHRONOTANK) {
  3086. // If the chrono tank's counter is still charging up, don't allow deploy. Or,
  3087. // if it's a player-controlled chrono tank, and the player's currently trying
  3088. // to teleport a different unit, don't allow teleporting this unit.
  3089. if(MoebiusCountDown || (IsOwnedByPlayer && House->UnitToTeleport && Map.IsTargettingMode == SPC_CHRONO2)) {
  3090. action = ACTION_NO_DEPLOY;
  3091. }
  3092. }
  3093. } else {
  3094. #endif
  3095. /*
  3096. ** All other units can "deploy" their passengers if they in-fact have
  3097. ** passengers and are a transport vehicle. Otherwise, they cannot
  3098. ** perform any self action.
  3099. */
  3100. if (Class->Max_Passengers() > 0) {
  3101. if (How_Many() == 0) {
  3102. action = ACTION_NO_DEPLOY;
  3103. }
  3104. } else {
  3105. action = ACTION_NONE;
  3106. }
  3107. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  3108. }
  3109. #endif
  3110. }
  3111. }
  3112. }
  3113. /*
  3114. ** Special return to friendly refinery action.
  3115. */
  3116. if (House->IsPlayerControl && object->Is_Techno() && ((TechnoClass const *)object)->House->Is_Ally(this)) {
  3117. if (object->What_Am_I() == RTTI_BUILDING && ((UnitClass *)this)->Transmit_Message(RADIO_CAN_LOAD, (TechnoClass*)object) == RADIO_ROGER) {
  3118. action = ACTION_ENTER;
  3119. }
  3120. }
  3121. /*
  3122. ** Special return to friendly repair factory action.
  3123. */
  3124. if (House->IsPlayerControl && action == ACTION_SELECT && object->What_Am_I() == RTTI_BUILDING) {
  3125. BuildingClass * building = (BuildingClass *)object;
  3126. if (building->Class->Type == STRUCT_REPAIR && ((UnitClass *)this)->Transmit_Message(RADIO_CAN_LOAD, building) == RADIO_ROGER && !building->In_Radio_Contact() && !building->Is_Something_Attached()) {
  3127. action = ACTION_MOVE;
  3128. }
  3129. }
  3130. /*
  3131. ** Check to see if it can enter a transporter.
  3132. */
  3133. if (
  3134. House->Is_Ally(object) &&
  3135. House->IsPlayerControl && object->Is_Techno() && object->What_Am_I() == RTTI_VESSEL) {
  3136. #ifdef FIXIT_CARRIER // checked - ajw 9/28/98
  3137. if( *(VesselClass *)object != VESSEL_CARRIER) {
  3138. #endif
  3139. switch (((UnitClass *)this)->Transmit_Message(RADIO_CAN_LOAD, (TechnoClass*)object)) {
  3140. case RADIO_ROGER:
  3141. action = ACTION_ENTER;
  3142. break;
  3143. case RADIO_NEGATIVE:
  3144. action = ACTION_NO_ENTER;
  3145. break;
  3146. default:
  3147. action = ACTION_NONE;
  3148. break;
  3149. }
  3150. #ifdef FIXIT_CARRIER // checked - ajw 9/28/98
  3151. }
  3152. #endif
  3153. }
  3154. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  3155. if (*this == UNIT_MAD && (IsDumping || Gold)) {
  3156. action = ACTION_NONE;
  3157. }
  3158. #endif
  3159. /*
  3160. ** If it doesn't know what to do with the object, then just
  3161. ** say it can't move there.
  3162. */
  3163. if (action == ACTION_NONE) action = ACTION_NOMOVE;
  3164. return(action);
  3165. }
  3166. /***********************************************************************************************
  3167. * UnitClass::What_Action -- Determines action to perform on specified cell. *
  3168. * *
  3169. * This routine will determine what action to perform if the mouse were clicked over the *
  3170. * cell specified. At the unit level, only the harvester is checked for. The lower *
  3171. * classes determine the regular action response. *
  3172. * *
  3173. * INPUT: cell -- The cell that the mouse might be clicked on. *
  3174. * *
  3175. * OUTPUT: Returns with the action type that this unit will perform if the mouse were *
  3176. * clicked of the cell specified. *
  3177. * *
  3178. * WARNINGS: none *
  3179. * *
  3180. * HISTORY: *
  3181. * 09/21/1995 JLB : Created. *
  3182. *=============================================================================================*/
  3183. ActionType UnitClass::What_Action(CELL cell) const
  3184. {
  3185. assert(Units.ID(this) == ID);
  3186. assert(IsActive);
  3187. ActionType action = DriveClass::What_Action(cell);
  3188. if (action == ACTION_MOVE && Map[cell].Land_Type() == LAND_TIBERIUM && Class->IsToHarvest) {
  3189. return(ACTION_HARVEST);
  3190. }
  3191. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  3192. if (*this == UNIT_MAD && (IsDumping || Gold)) {
  3193. action = ACTION_NOMOVE;
  3194. }
  3195. #endif
  3196. return(action);
  3197. }
  3198. /***********************************************************************************************
  3199. * UnitClass::Exit_Repair -- Drive the unit off the repair facility. *
  3200. * *
  3201. * INPUT: none *
  3202. * *
  3203. * OUTPUT: none *
  3204. * *
  3205. * WARNINGS: none *
  3206. * *
  3207. * HISTORY: *
  3208. * 04/03/1995 BWG : Created. *
  3209. *=============================================================================================*/
  3210. #define XYCELL(x, y) (y*MAP_CELL_W+x)
  3211. void UnitClass::Exit_Repair(void)
  3212. {
  3213. assert(Units.ID(this) == ID);
  3214. assert(IsActive);
  3215. int i;
  3216. CELL cell;
  3217. bool found = false;
  3218. static short const ExitRepair[] = {
  3219. XYCELL(0, -2),
  3220. XYCELL(1, -1),
  3221. XYCELL(2, 0),
  3222. XYCELL(1, 1),
  3223. XYCELL(0, 2),
  3224. XYCELL(-1, 1),
  3225. XYCELL(-2, 0),
  3226. XYCELL(-1, -1)
  3227. };
  3228. cell = Coord_Cell(Coord) + ExitRepair[Dir_Facing(PrimaryFacing.Current())];
  3229. if (Can_Enter_Cell(cell) == MOVE_OK) found = true;
  3230. if (!found) for (i=0; i<8; i++) {
  3231. cell = Coord_Cell(Coord) + ExitRepair[i];
  3232. if (Can_Enter_Cell(cell) == MOVE_OK) {
  3233. found = true;
  3234. break;
  3235. }
  3236. }
  3237. if (found) {
  3238. // DirType dir = Direction(cell);
  3239. Assign_Mission(MISSION_MOVE);
  3240. Assign_Destination(::As_Target(cell));
  3241. }
  3242. }
  3243. /***********************************************************************************************
  3244. * UnitClass::Mission_Guard -- Special guard mission override processor. *
  3245. * *
  3246. * Handles the guard mission for the unit. If the IQ is high enough and the unit is *
  3247. * a harvester, it will begin to harvest automatically. An MCV might autodeploy. *
  3248. * *
  3249. * INPUT: none *
  3250. * *
  3251. * OUTPUT: Returns the time delay before this command is executed again. *
  3252. * *
  3253. * WARNINGS: none *
  3254. * *
  3255. * HISTORY: *
  3256. * 05/08/1995 JLB : Created. *
  3257. * 05/08/1995 JLB : Fixes gunboat problems. *
  3258. *=============================================================================================*/
  3259. int UnitClass::Mission_Guard(void)
  3260. {
  3261. assert(Units.ID(this) == ID);
  3262. assert(IsActive);
  3263. if (/*House->IsBaseBuilding &&*/ !House->IsHuman && Class->IsToHarvest && House->Get_Quantity(STRUCT_REFINERY) > 0 && !House->IsTiberiumShort) {
  3264. Assign_Mission(MISSION_HARVEST);
  3265. return(1);
  3266. // return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  3267. }
  3268. if (*this == UNIT_MCV && House->IsBaseBuilding) {
  3269. Assign_Mission(MISSION_UNLOAD);
  3270. return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  3271. }
  3272. return(DriveClass::Mission_Guard());
  3273. }
  3274. /***********************************************************************************************
  3275. * UnitClass::Mission_Move -- Handles special move mission overrides. *
  3276. * *
  3277. * This routine intercepts the normal move mission and if a gunboat is being processed, *
  3278. * changes its mission to hunt. This is an attempt to keep the gunboat on the hunt mission *
  3279. * regardless of what the player did. *
  3280. * *
  3281. * INPUT: none *
  3282. * *
  3283. * OUTPUT: Returns the number of ticks before this routine should be called again. *
  3284. * *
  3285. * WARNINGS: none *
  3286. * *
  3287. * HISTORY: *
  3288. * 05/09/1995 JLB : Created. *
  3289. * 09/28/1995 JLB : Harvester stick in guard mode if no more refineries. *
  3290. *=============================================================================================*/
  3291. int UnitClass::Mission_Move(void)
  3292. {
  3293. assert(Units.ID(this) == ID);
  3294. assert(IsActive);
  3295. IsHarvesting = false;
  3296. /*
  3297. ** Always make sure that that transport door is closed if the vehicle is moving.
  3298. */
  3299. if (!Is_Door_Closed()) {
  3300. APC_Close_Door();
  3301. }
  3302. return(DriveClass::Mission_Move());
  3303. }
  3304. /***********************************************************************************************
  3305. * UnitClass::Desired_Load_Dir -- Determines the best cell and facing for loading. *
  3306. * *
  3307. * This routine examines the unit and adjacent cells in order to find the best facing *
  3308. * for the transport and best staging cell for the potential passengers. This location is *
  3309. * modified by adjacent cell passability and direction of the potential passenger. *
  3310. * *
  3311. * INPUT: passenger -- Pointer to the potential passenger. *
  3312. * *
  3313. * moveto -- Reference to the cell number that specifies where the potential *
  3314. * passenger should move to first. *
  3315. * *
  3316. * OUTPUT: Returns with the direction the transport should face before opening the transport *
  3317. * door. *
  3318. * *
  3319. * WARNINGS: none *
  3320. * *
  3321. * HISTORY: *
  3322. * 05/23/1995 JLB : Created. *
  3323. *=============================================================================================*/
  3324. DirType UnitClass::Desired_Load_Dir(ObjectClass * passenger, CELL & moveto) const
  3325. {
  3326. assert(Units.ID(this) == ID);
  3327. assert(IsActive);
  3328. /*
  3329. ** Determine the ideal facing that provides the least resistance. This would be the direction
  3330. ** of the potential passenger or the current transport facing if it is going to unload.
  3331. */
  3332. DirType faceto;
  3333. if (passenger != NULL) {
  3334. faceto = Direction(passenger);
  3335. } else {
  3336. faceto = PrimaryFacing.Current() + DIR_S;
  3337. }
  3338. /*
  3339. ** Sweep through the adjacent cells in order to find the best candidate.
  3340. */
  3341. FacingType bestdir = FACING_N;
  3342. int bestval = -1;
  3343. for (FacingType face = FACING_N; face < FACING_COUNT; face++) {
  3344. int value = 0;
  3345. CELL cellnum = Adjacent_Cell(Coord_Cell(Coord), face);
  3346. /*
  3347. ** Base the initial value of the potential cell according to whether the passenger is
  3348. ** allowed to enter the cell. If it can't, then give such a negative value to the
  3349. ** cell so that it is prevented from ever choosing that cell for load/unload.
  3350. */
  3351. if (passenger != NULL) {
  3352. value = (passenger->Can_Enter_Cell(cellnum) == MOVE_OK || Coord_Cell(passenger->Coord) == cellnum) ? 128 : -128;
  3353. } else {
  3354. CellClass * cell = &Map[cellnum];
  3355. if (Ground[cell->Land_Type()].Cost[SPEED_FOOT] == 0 || cell->Flag.Occupy.Building || cell->Flag.Occupy.Vehicle || cell->Flag.Occupy.Monolith || (cell->Flag.Composite & 0x01F) == 0x01F) {
  3356. value = -128;
  3357. } else {
  3358. if (cell->Cell_Techno() && !House->Is_Ally(cell->Cell_Techno())) {
  3359. value = -128;
  3360. } else {
  3361. value = 128;
  3362. }
  3363. }
  3364. }
  3365. /*
  3366. ** Give more weight to the cells that require the least rotation of the transport or the
  3367. ** least roundabout movement for the potential passenger.
  3368. */
  3369. value -= (int)ABS((int)(signed char)Facing_Dir(face) - (int)(signed char)faceto);
  3370. if (face == FACING_S) {
  3371. value -= 100;
  3372. }
  3373. if (face == FACING_SW || face == FACING_SE) value += 64;
  3374. /*
  3375. ** If the value for the potential cell is greater than the last recorded potential
  3376. ** value, then record this cell as the best candidate.
  3377. */
  3378. if (bestval == -1 || value > bestval) {
  3379. bestval = value;
  3380. bestdir = face;
  3381. }
  3382. }
  3383. /*
  3384. ** If a suitable direction was found, then return with the direction value.
  3385. */
  3386. moveto = 0;
  3387. if (bestval > 0) {
  3388. static DirType _desired_to_actual[FACING_COUNT] = {DIR_S, DIR_SW, DIR_NW, DIR_NW, DIR_NE, DIR_NE, DIR_NE, DIR_SE};
  3389. moveto = Adjacent_Cell(Coord_Cell(Coord), bestdir);
  3390. return(_desired_to_actual[bestdir]);
  3391. }
  3392. return(DIR_S);
  3393. }
  3394. /***********************************************************************************************
  3395. * UnitClass::Flag_Attach -- Attaches a house flag to this unit. *
  3396. * *
  3397. * This routine will attach a house flag to this unit. *
  3398. * *
  3399. * INPUT: house -- The house that is having its flag attached to it. *
  3400. * *
  3401. * OUTPUT: Was the house flag successfully attached to this unit? *
  3402. * *
  3403. * WARNINGS: A unit can only carry one flag at a time. This might be a reason for failure *
  3404. * of this routine. *
  3405. * *
  3406. * HISTORY: *
  3407. * 05/23/1995 JLB : Created. *
  3408. *=============================================================================================*/
  3409. bool UnitClass::Flag_Attach(HousesType house)
  3410. {
  3411. assert(Units.ID(this) == ID);
  3412. assert(IsActive);
  3413. if (house != HOUSE_NONE && Flagged == HOUSE_NONE) {
  3414. Flagged = house;
  3415. Mark(MARK_CHANGE);
  3416. return(true);
  3417. }
  3418. return(false);
  3419. }
  3420. /***********************************************************************************************
  3421. * UnitClass::Flag_Remove -- Removes the house flag from this unit. *
  3422. * *
  3423. * This routine will remove the house flag that is attached to this unit. *
  3424. * *
  3425. * INPUT: none *
  3426. * *
  3427. * OUTPUT: Was the flag successfully removed? *
  3428. * *
  3429. * WARNINGS: This routine doesn't put the flag into a new location. That operation must *
  3430. * be performed or else the house flag will cease to exist. *
  3431. * *
  3432. * HISTORY: *
  3433. * 05/23/1995 JLB : Created. *
  3434. *=============================================================================================*/
  3435. bool UnitClass::Flag_Remove(void)
  3436. {
  3437. assert(Units.ID(this) == ID);
  3438. assert(IsActive);
  3439. if (Flagged != HOUSE_NONE) {
  3440. Flagged = HOUSE_NONE;
  3441. Mark(MARK_CHANGE);
  3442. return(true);
  3443. }
  3444. return(false);
  3445. }
  3446. /***********************************************************************************************
  3447. * UnitClass::Pip_Count -- Fetches the number of pips to display on unit. *
  3448. * *
  3449. * This routine is used to fetch the number of "fullness" pips to display on the unit. *
  3450. * This will either be the number of passengers or the percentage full (in 1/5ths) of *
  3451. * a harvester. *
  3452. * *
  3453. * INPUT: none *
  3454. * *
  3455. * OUTPUT: Returns with the number of pips to draw on this unit. *
  3456. * *
  3457. * WARNINGS: none *
  3458. * *
  3459. * HISTORY: *
  3460. * 06/25/1995 JLB : Created. *
  3461. *=============================================================================================*/
  3462. int UnitClass::Pip_Count(void) const
  3463. {
  3464. assert(Units.ID(this) == ID);
  3465. assert(IsActive);
  3466. if (Class->Max_Passengers() > 0) {
  3467. return(How_Many());
  3468. }
  3469. if (*this == UNIT_MINELAYER) {
  3470. int retval = 0;
  3471. if (Ammo > 0) {
  3472. retval = Class->Max_Pips() * fixed(Ammo, Class->MaxAmmo);
  3473. if (!retval) retval = 1;
  3474. }
  3475. return(retval);
  3476. }
  3477. if (*this == UNIT_HARVESTER) {
  3478. return((Gold + Gems) / 4);
  3479. }
  3480. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  3481. if (*this == UNIT_CHRONOTANK) {
  3482. int fulldur = ChronoTankDuration * TICKS_PER_MINUTE;
  3483. return( (fulldur - MoebiusCountDown) / (fulldur / 5));
  3484. }
  3485. #endif
  3486. return(0);
  3487. }
  3488. /***********************************************************************************************
  3489. * UnitClass::APC_Close_Door -- Closes an APC door. *
  3490. * *
  3491. * This routine will initiate closing of the APC door. *
  3492. * *
  3493. * INPUT: none *
  3494. * *
  3495. * OUTPUT: none *
  3496. * *
  3497. * WARNINGS: none *
  3498. * *
  3499. * HISTORY: *
  3500. * 06/25/1995 JLB : Created. *
  3501. *=============================================================================================*/
  3502. void UnitClass::APC_Close_Door(void)
  3503. {
  3504. assert(Units.ID(this) == ID);
  3505. assert(IsActive);
  3506. Close_Door(10, 2);
  3507. }
  3508. /***********************************************************************************************
  3509. * UnitClass::APC_Open_Door -- Opens an APC door. *
  3510. * *
  3511. * This routine will initiate opening of the APC door. *
  3512. * *
  3513. * INPUT: none *
  3514. * *
  3515. * OUTPUT: none *
  3516. * *
  3517. * WARNINGS: none *
  3518. * *
  3519. * HISTORY: *
  3520. * 06/25/1995 JLB : Created. *
  3521. *=============================================================================================*/
  3522. void UnitClass::APC_Open_Door(void)
  3523. {
  3524. assert(Units.ID(this) == ID);
  3525. assert(IsActive);
  3526. if (!IsDriving && !IsRotating) {
  3527. if (PrimaryFacing == DIR_NW || PrimaryFacing == DIR_NE) {
  3528. Open_Door(10, 2);
  3529. } else {
  3530. Open_Door(1, 2);
  3531. }
  3532. }
  3533. }
  3534. /***********************************************************************************************
  3535. * UnitClass::Crew_Type -- Fetches the kind of crew that this object produces. *
  3536. * *
  3537. * When a unit is destroyed, a crew member might be generated. This routine will return *
  3538. * with the infantry type to produce for this unit. This routine will be called for every *
  3539. * survivor that is generated. *
  3540. * *
  3541. * INPUT: none *
  3542. * *
  3543. * OUTPUT: Returns with a suggested infantry type to generate as a survivor from this unit. *
  3544. * *
  3545. * WARNINGS: none *
  3546. * *
  3547. * HISTORY: *
  3548. * 08/13/1995 JLB : Created. *
  3549. *=============================================================================================*/
  3550. InfantryType UnitClass::Crew_Type(void) const
  3551. {
  3552. assert(Units.ID(this) == ID);
  3553. assert(IsActive);
  3554. if (Class->PrimaryWeapon == NULL) {
  3555. if (Percent_Chance(50)) {
  3556. return(INFANTRY_C1);
  3557. } else {
  3558. return(INFANTRY_C7);
  3559. }
  3560. }
  3561. return(DriveClass::Crew_Type());
  3562. }
  3563. /***********************************************************************************************
  3564. * UnitClass::Mission_Repair -- Handles finding and proceeding on a repair mission. *
  3565. * *
  3566. * This mission handler will look for a repair facility. If one is found then contact *
  3567. * is established and then the normal Mission_Enter logic is performed. The repair facility *
  3568. * will take over the actual repair coordination process. *
  3569. * *
  3570. * INPUT: none *
  3571. * *
  3572. * OUTPUT: Returns the number of game frames to delay before calling this routine again. *
  3573. * *
  3574. * WARNINGS: none *
  3575. * *
  3576. * HISTORY: *
  3577. * 10/02/1995 JLB : Created. *
  3578. *=============================================================================================*/
  3579. int UnitClass::Mission_Repair(void)
  3580. {
  3581. assert(Units.ID(this) == ID);
  3582. assert(IsActive);
  3583. BuildingClass * nearest = Find_Docking_Bay(STRUCT_REFINERY, true);
  3584. IsHarvesting = false;
  3585. /*
  3586. ** If there is no available repair facility, then check to see if there
  3587. ** are any repair facilities at all. If not, then enter this unit
  3588. ** into idle state.
  3589. */
  3590. if (nearest == NULL) {
  3591. if (!(House->ActiveBScan & STRUCTF_REFINERY)) {
  3592. Enter_Idle_Mode();
  3593. }
  3594. } else {
  3595. /*
  3596. ** Try to establish radio contact with the repair facility. If contact
  3597. ** was established, then proceed with normal enter mission, which handles
  3598. ** the repair process.
  3599. */
  3600. if (Transmit_Message(RADIO_HELLO, nearest) == RADIO_ROGER) {
  3601. Assign_Mission(MISSION_ENTER);
  3602. return(1);
  3603. }
  3604. }
  3605. /*
  3606. ** If no action could be performed at this time, then wait
  3607. ** around for a bit before trying again.
  3608. */
  3609. return(MissionControl[Mission].Normal_Delay());
  3610. }
  3611. /***********************************************************************************************
  3612. * UnitClass::Fire_Direction -- Determines the direction of firing. *
  3613. * *
  3614. * This routine will return with the facing that a projectile will travel if it was *
  3615. * fired at this instant. The facing should match the turret facing for those units *
  3616. * equipped with a turret. If the unit doesn't have a turret, then it will be the facing *
  3617. * of the body. *
  3618. * *
  3619. * INPUT: none *
  3620. * *
  3621. * OUTPUT: Returns with the default firing direction for a projectile. *
  3622. * *
  3623. * WARNINGS: none *
  3624. * *
  3625. * HISTORY: *
  3626. * 06/25/1995 JLB : Created. *
  3627. *=============================================================================================*/
  3628. DirType UnitClass::Fire_Direction(void) const
  3629. {
  3630. assert(Units.ID(this) == ID);
  3631. assert(IsActive);
  3632. if (Class->IsTurretEquipped) {
  3633. if (*this == UNIT_V2_LAUNCHER) {
  3634. int diff1 = SecondaryFacing.Difference(DIR_E);
  3635. int diff2 = SecondaryFacing.Difference(DIR_W);
  3636. diff1 = ABS(diff1);
  3637. diff2 = ABS(diff2);
  3638. int diff = min(diff1, diff2);
  3639. int adj = Fixed_To_Cardinal(ABS(SecondaryFacing.Difference(DIR_N)), 64-diff);
  3640. if (SecondaryFacing.Difference(DIR_N) < 0) {
  3641. return(DirType)(SecondaryFacing - (DirType)adj);
  3642. } else {
  3643. return(DirType)(SecondaryFacing + (DirType)adj);
  3644. }
  3645. }
  3646. return(SecondaryFacing.Current());
  3647. }
  3648. return(DriveClass::Fire_Direction());
  3649. }
  3650. /***********************************************************************************************
  3651. * UnitClass::Ok_To_Move -- Queries whether the vehicle can move. *
  3652. * *
  3653. * This virtual routine is used to determine if the vehicle is allowed *
  3654. * to start moving. It is typically called when the vehicle desires *
  3655. * to move but needs confirmation from the turret logic before *
  3656. * proceeding. This happens when dealing with a vehicle that must have *
  3657. * its turret face the same direction as the body before the vehicle *
  3658. * may begin movement. *
  3659. * *
  3660. * INPUT: dir -- The facing the unit wants to travel in. *
  3661. * *
  3662. * OUTPUT: bool; Can the unit begin forward movement now? *
  3663. * *
  3664. * WARNINGS: none *
  3665. * *
  3666. * HISTORY: *
  3667. * 05/12/1994 JLB : Created. *
  3668. *=============================================================================================*/
  3669. bool UnitClass::Ok_To_Move(DirType dir) const
  3670. {
  3671. assert(Units.ID(this) == ID);
  3672. assert(IsActive);
  3673. if (Class->IsLockTurret) {
  3674. if (IsRotating) {
  3675. return(false);
  3676. } else {
  3677. if (SecondaryFacing.Difference(dir)) {
  3678. ((UnitClass *)this)->SecondaryFacing.Set_Desired(dir);
  3679. return(false);
  3680. }
  3681. }
  3682. }
  3683. return(true);
  3684. }
  3685. /***********************************************************************************************
  3686. * UnitClass::Can_Fire -- Determines if turret can fire upon target. *
  3687. * *
  3688. * This routine determines if the turret can fire upon the target *
  3689. * specified. *
  3690. * *
  3691. * INPUT: target -- The target to fire upon. *
  3692. * *
  3693. * which -- Which weapon to use to determine legality to fire. 0=primary, *
  3694. * 1=secondary. *
  3695. * *
  3696. * OUTPUT: Returns the fire status type that indicates if firing is allowed and if not, why. *
  3697. * *
  3698. * WARNINGS: none *
  3699. * *
  3700. * HISTORY: *
  3701. * 04/26/1994 JLB : Created. *
  3702. * 06/01/1994 JLB : Returns reason why it can't fire. *
  3703. *=============================================================================================*/
  3704. FireErrorType UnitClass::Can_Fire(TARGET target, int which) const
  3705. {
  3706. assert(Units.ID(this) == ID);
  3707. assert(IsActive);
  3708. DirType dir; // The facing to impart upon the projectile.
  3709. int diff;
  3710. FireErrorType fire = DriveClass::Can_Fire(target, which);
  3711. if (fire == FIRE_OK) {
  3712. WeaponTypeClass const * weapon = (which == 0) ? Class->PrimaryWeapon : Class->SecondaryWeapon;
  3713. /*
  3714. ** If this unit cannot fire while moving, then bail.
  3715. */
  3716. if ((Class->IsNoFireWhileMoving /*!Class->IsTurretEquipped || Class->IsLockTurret*/) && Target_Legal(NavCom)) {
  3717. return(FIRE_MOVING);
  3718. }
  3719. /*
  3720. ** If the turret is rotating and the projectile isn't a homing type, then
  3721. ** firing must be delayed until the rotation stops.
  3722. */
  3723. if (!IsFiring && IsRotating && weapon->Bullet->ROT == 0) {
  3724. return(FIRE_ROTATING);
  3725. }
  3726. dir = Direction(target);
  3727. /*
  3728. ** Determine if the turret facing isn't too far off of facing the target.
  3729. */
  3730. if (Class->IsTurretEquipped) {
  3731. diff = SecondaryFacing.Difference(dir);
  3732. } else {
  3733. diff = PrimaryFacing.Difference(dir);
  3734. }
  3735. diff = ABS(diff);
  3736. if (weapon->Bullet->ROT != 0) {
  3737. diff >>= 2;
  3738. }
  3739. if (diff < 8) {
  3740. return(DriveClass::Can_Fire(target, which));
  3741. }
  3742. return(FIRE_FACING);
  3743. }
  3744. return(fire);
  3745. }
  3746. /***********************************************************************************************
  3747. * UnitClass::Fire_At -- Try to fire upon the target specified. *
  3748. * *
  3749. * This routine is the auto-fire logic for the turret. It will check *
  3750. * to see if firing is technically legal given the specified target. *
  3751. * If it is legal to fire, it does so. It is safe to call this routine *
  3752. * every game tick. *
  3753. * *
  3754. * INPUT: target -- The target to fire upon. *
  3755. * *
  3756. * which -- Which weapon to use when firing. 0=primary, 1=secondary. *
  3757. * *
  3758. * OUTPUT: bool; Did firing occur? *
  3759. * *
  3760. * WARNINGS: none *
  3761. * *
  3762. * HISTORY: *
  3763. * 04/26/1994 JLB : Created. *
  3764. *=============================================================================================*/
  3765. BulletClass * UnitClass::Fire_At(TARGET target, int which)
  3766. {
  3767. assert(Units.ID(this) == ID);
  3768. assert(IsActive);
  3769. BulletClass * bullet = NULL;
  3770. WeaponTypeClass const * weap = (which == 0) ? Class->PrimaryWeapon : Class->SecondaryWeapon;
  3771. if (weap == NULL) return(NULL);
  3772. if (Can_Fire(target, which) == FIRE_OK) {
  3773. bullet = DriveClass::Fire_At(target, which);
  3774. if (bullet != NULL) {
  3775. #ifdef FIXIT_CSII // checked - ajw 9/28/98
  3776. if(Class->Type == UNIT_DEMOTRUCK && IsActive) delete this;
  3777. #endif
  3778. /*
  3779. ** Possible reload timer set.
  3780. */
  3781. if ((*this == UNIT_V2_LAUNCHER) && Reload == 0) {
  3782. Reload = TICKS_PER_SECOND * 30;
  3783. }
  3784. }
  3785. }
  3786. return(bullet);
  3787. }
  3788. /***********************************************************************************************
  3789. * UnitClass::Class_Of -- Fetches a reference to the class type for this object. *
  3790. * *
  3791. * This routine will fetch a reference to the TypeClass of this object. *
  3792. * *
  3793. * INPUT: none *
  3794. * *
  3795. * OUTPUT: Returns with reference to the type class of this object. *
  3796. * *
  3797. * WARNINGS: none *
  3798. * *
  3799. * HISTORY: *
  3800. * 07/29/1995 JLB : Created. *
  3801. *=============================================================================================*/
  3802. ObjectTypeClass const & UnitClass::Class_Of(void) const
  3803. {
  3804. assert(Units.ID(this) == ID);
  3805. assert(IsActive);
  3806. return(*Class);
  3807. }
  3808. /***********************************************************************************************
  3809. * UnitClass::Tiberium_Load -- Determine the Tiberium load as a percentage. *
  3810. * *
  3811. * Use this routine to determine what the Tiberium load is (as a fixed point percentage). *
  3812. * *
  3813. * INPUT: none *
  3814. * *
  3815. * OUTPUT: Returns with the current "fullness" rating for the object. *
  3816. * *
  3817. * WARNINGS: none *
  3818. * *
  3819. * HISTORY: *
  3820. * 03/17/1995 JLB : Created. *
  3821. *=============================================================================================*/
  3822. fixed UnitClass::Tiberium_Load(void) const
  3823. {
  3824. assert(IsActive);
  3825. if (*this == UNIT_HARVESTER) {
  3826. return(fixed(Tiberium, Rule.BailCount));
  3827. }
  3828. return(0);
  3829. }
  3830. /***********************************************************************************************
  3831. * UnitClass::Offload_Tiberium_Bail -- Offloads one Tiberium quantum from the object. *
  3832. * *
  3833. * This routine will offload one Tiberium packet/quantum/bail from the object. Multiple *
  3834. * calls to this routine are needed in order to fully offload all Tiberium. *
  3835. * *
  3836. * INPUT: none *
  3837. * *
  3838. * OUTPUT: Returns with the number of credits offloaded for the one call. If zero is returned,*
  3839. * then this indicates that all Tiberium has been offloaded. *
  3840. * *
  3841. * WARNINGS: none *
  3842. * *
  3843. * HISTORY: *
  3844. * 07/19/1995 JLB : Created. *
  3845. *=============================================================================================*/
  3846. int UnitClass::Offload_Tiberium_Bail(void)
  3847. {
  3848. assert(IsActive);
  3849. if (Tiberium) {
  3850. Tiberium--;
  3851. #ifdef TOFIX
  3852. if (House->IsHuman) {
  3853. return(UnitTypeClass::FULL_LOAD_CREDITS/UnitTypeClass::STEP_COUNT);
  3854. }
  3855. return(UnitTypeClass::FULL_LOAD_CREDITS+(UnitTypeClass::FULL_LOAD_CREDITS/3)/UnitTypeClass::STEP_COUNT);
  3856. #endif
  3857. }
  3858. return(0);
  3859. }
  3860. /***********************************************************************************************
  3861. * UnitClass::Approach_Target -- Handles approaching the target in order to attack it. *
  3862. * *
  3863. * This routine will check to see if the target is infantry and it can be overrun. It will *
  3864. * try to overrun the infantry rather than attack it. This only applies to computer *
  3865. * controlled vehicles. If it isn't the infantry overrun case, then it falls into the *
  3866. * base class for normal (complex) approach algorithm. *
  3867. * *
  3868. * INPUT: none *
  3869. * *
  3870. * OUTPUT: none *
  3871. * *
  3872. * WARNINGS: none *
  3873. * *
  3874. * HISTORY: *
  3875. * 03/17/1995 JLB : Created. *
  3876. * 07/12/1995 JLB : Flamethrower tanks don't overrun -- their weapon is better. *
  3877. *=============================================================================================*/
  3878. void UnitClass::Approach_Target(void)
  3879. {
  3880. assert(IsActive);
  3881. /*
  3882. ** Only if there is a legal target should the approach check occur.
  3883. */
  3884. if (!House->IsHuman && Target_Legal(TarCom) && !Target_Legal(NavCom)) {
  3885. /*
  3886. ** Special case:
  3887. ** If this is for a unit that can crush infantry, and the target is
  3888. ** infantry, AND the infantry is pretty darn close, then just try
  3889. ** to drive over the infantry instead of firing on it.
  3890. */
  3891. TechnoClass * target = As_Techno(TarCom);
  3892. if (Class->IsCrusher && Distance(TarCom) < Rule.CrushDistance && target && ((TechnoTypeClass const &)(target->Class_Of())).IsCrushable) {
  3893. Assign_Destination(TarCom);
  3894. return;
  3895. }
  3896. }
  3897. /*
  3898. ** In the other cases, uses the more complex "get to just within weapon range"
  3899. ** algorithm.
  3900. */
  3901. DriveClass::Approach_Target();
  3902. }
  3903. /***********************************************************************************************
  3904. * DriveClass::Overrun_Square -- Handles vehicle overrun of a cell. *
  3905. * *
  3906. * This routine is called when a vehicle enters a square or when it is about to enter a *
  3907. * square (controlled by parameter). When a vehicle that can crush infantry enters a *
  3908. * cell that contains infantry, then the infantry will be destroyed (regardless of *
  3909. * affiliation). When a vehicle threatens to overrun a square, all occupying infantry *
  3910. * will attempt to get out of the way. *
  3911. * *
  3912. * INPUT: cell -- The cell that is, or soon will be, entered by a vehicle. *
  3913. * *
  3914. * threaten -- Don't kill, but just threaten to enter the cell. *
  3915. * *
  3916. * OUTPUT: none *
  3917. * *
  3918. * WARNINGS: none *
  3919. * *
  3920. * HISTORY: *
  3921. * 01/19/1995 JLB : Created. *
  3922. *=============================================================================================*/
  3923. void UnitClass::Overrun_Square(CELL cell, bool threaten)
  3924. {
  3925. assert(IsActive);
  3926. CellClass * cellptr = &Map[cell];
  3927. if (Class->IsCrusher) {
  3928. if (threaten) {
  3929. /*
  3930. ** If the cell contains infantry, then they will panic when a vehicle tries
  3931. ** drive over them. Have the infantry run away instead.
  3932. */
  3933. if (cellptr->Flag.Composite & 0x1F) {
  3934. /*
  3935. ** Scattering is controlled by the game difficulty level.
  3936. */
  3937. cellptr->Incoming(0, true);
  3938. }
  3939. } else {
  3940. ObjectClass * object = cellptr->Cell_Occupier();
  3941. int crushed = false;
  3942. while (object != NULL) {
  3943. if (object->Class_Of().IsCrushable && !House->Is_Ally(object) && Distance(object->Center_Coord()) < CELL_LEPTON_W/2) {
  3944. #ifdef OBSOLETE
  3945. /*
  3946. ** If we're running over infantry, let's see if the infantry we're
  3947. ** squashing is a thief trying to capture us. If so, let him succeed.
  3948. */
  3949. if (object->What_Am_I() == RTTI_INFANTRY && *((InfantryClass *)object) == INFANTRY_THIEF && ((InfantryClass *)object)->NavCom == As_Target()) {
  3950. ObjectClass * next = object->Next;
  3951. IsOwnedByPlayer = ((InfantryClass *)object)->IsOwnedByPlayer;
  3952. House = ((InfantryClass *)object)->House;
  3953. delete object;
  3954. object = next;
  3955. } else {
  3956. #endif
  3957. ObjectClass * next = object->Next;
  3958. crushed = true;
  3959. /*
  3960. ** Record credit for the kill(s)
  3961. */
  3962. Sound_Effect(VOC_SQUISH, Coord);
  3963. if (object->Height == 0) {
  3964. new AnimClass(ANIM_CORPSE1, object->Center_Coord());
  3965. }
  3966. object->Record_The_Kill(this);
  3967. object->Mark(MARK_UP);
  3968. object->Limbo();
  3969. delete object;
  3970. //new OverlayClass(OVERLAY_SQUISH, Coord_Cell(Coord));
  3971. object = next;
  3972. #ifdef OBSOLETE
  3973. }
  3974. #endif
  3975. } else {
  3976. object = object->Next;
  3977. }
  3978. }
  3979. if (crushed) Do_Uncloak();
  3980. }
  3981. }
  3982. }
  3983. /***********************************************************************************************
  3984. * UnitClass::Assign_Destination -- Assign a destination to a unit. *
  3985. * *
  3986. * This will assign the specified destination to the unit. It is presumed that doing is *
  3987. * is all that is needed in order to cause the unit to move to the specified destination. *
  3988. * *
  3989. * INPUT: target -- The target (location) to move to. *
  3990. * *
  3991. * OUTPUT: none *
  3992. * *
  3993. * WARNINGS: none *
  3994. * *
  3995. * HISTORY: *
  3996. * 07/09/1996 JLB : Created. *
  3997. *=============================================================================================*/
  3998. void UnitClass::Assign_Destination(TARGET target)
  3999. {
  4000. assert(IsActive);
  4001. /*
  4002. ** Abort early if there is anything wrong with the parameters
  4003. ** or the unit already is assigned the specified destination.
  4004. */
  4005. if (target == NavCom) return;
  4006. /*
  4007. ** Transport vehicles must tell all passengers that are about to load, that they
  4008. ** cannot proceed. This is accomplished with a radio message to this effect.
  4009. */
  4010. if (In_Radio_Contact() && Class->Max_Passengers() > 0 && Contact_With_Whom()->Is_Infantry()) {
  4011. Transmit_Message(RADIO_OVER_OUT);
  4012. }
  4013. BuildingClass * b = As_Building(target);
  4014. /*
  4015. ** Handle entry logic here.
  4016. */
  4017. if (Mission == MISSION_ENTER || MissionQueue == MISSION_ENTER) {
  4018. /*
  4019. ** If not already in radio contact (presumed with the transport), then
  4020. ** either try to establish contact if allowed, or just move close and
  4021. ** wait until radio contact can be established.
  4022. */
  4023. if (!In_Radio_Contact()) {
  4024. if (b != NULL) {
  4025. /*
  4026. ** Determine if the transport is already in radio contact. If so, then just move
  4027. ** toward the transport and try to establish contact at a later time.
  4028. */
  4029. if (b->In_Radio_Contact()) {
  4030. // TCTCTC -- call for an update from the transport to get a good rendezvous position.
  4031. ArchiveTarget = target;
  4032. /*
  4033. ** HACK ALERT: The repair bay is counting on the assignment of the NavCom by this routine.
  4034. ** The refinery must NOT have the navcom assigned by this routine.
  4035. */
  4036. if (*b != STRUCT_REPAIR) {
  4037. target = TARGET_NONE;
  4038. }
  4039. } else {
  4040. if (Transmit_Message(RADIO_DOCKING, b) != RADIO_ROGER) {
  4041. Transmit_Message(RADIO_OVER_OUT);
  4042. if (*b == STRUCT_REPAIR) {
  4043. ArchiveTarget = target;
  4044. }
  4045. }
  4046. if (*b != STRUCT_REPAIR) {
  4047. ArchiveTarget = target;
  4048. // target = TARGET_NONE;
  4049. }
  4050. }
  4051. } else {
  4052. TechnoClass * techno = As_Techno(target);
  4053. if (techno != NULL) {
  4054. /*
  4055. ** Determine if the transport is already in radio contact. If so, then just move
  4056. ** toward the transport and try to establish contact at a later time.
  4057. */
  4058. if (techno->In_Radio_Contact()) {
  4059. // TCTCTC -- call for an update from the transport to get a good rendezvous position.
  4060. ArchiveTarget = target;
  4061. } else {
  4062. if (Transmit_Message(RADIO_HELLO, techno) == RADIO_ROGER) {
  4063. if (Transmit_Message(RADIO_DOCKING) != RADIO_ROGER) {
  4064. Transmit_Message(RADIO_OVER_OUT);
  4065. } else {
  4066. //BG: keep retransmitted navcom from radio-move-here.
  4067. return;
  4068. }
  4069. }
  4070. }
  4071. }
  4072. }
  4073. } else {
  4074. Path[0] = FACING_NONE;
  4075. }
  4076. } else {
  4077. Path[0] = FACING_NONE;
  4078. }
  4079. /*
  4080. ** If the player clicked on a friendly repair facility and the repair
  4081. ** facility is currently not involved with some other unit (radio or unloading).
  4082. */
  4083. if (b != NULL && *b == STRUCT_REPAIR) {
  4084. if (b->In_Radio_Contact() && (b->Contact_With_Whom() != this) ) {
  4085. // if (target != NULL) {
  4086. ArchiveTarget = target;
  4087. // }
  4088. // target = TARGET_NONE;
  4089. } else {
  4090. /*
  4091. ** Establish radio contact protocol. If the facility responds correctly,
  4092. ** then remain in radio contact and proceed toward the desired destination.
  4093. */
  4094. if (Transmit_Message(RADIO_HELLO, b) == RADIO_ROGER) {
  4095. /*
  4096. ** Last check to make sure that the loading square is free from permanent
  4097. ** occupation (such as a building).
  4098. */
  4099. CELL cell = (CELL)(Coord_Cell(b->Center_Coord()) + (MAP_CELL_W-1));
  4100. if (Ground[Map[cell].Land_Type()].Cost[Techno_Type_Class()->Speed] > 0) {
  4101. if (Transmit_Message(RADIO_DOCKING) == RADIO_ROGER) {
  4102. FootClass::Assign_Destination(target);
  4103. Path[0] = FACING_NONE;
  4104. return;
  4105. }
  4106. /*
  4107. ** Failure to establish a docking relationship with the refinery.
  4108. ** Bail & await further instructions.
  4109. */
  4110. Transmit_Message(RADIO_OVER_OUT);
  4111. }
  4112. }
  4113. }
  4114. }
  4115. DriveClass::Assign_Destination(target);
  4116. }
  4117. /***********************************************************************************************
  4118. * UnitClass::Greatest_Threat -- Fetches the greatest threat for this unit. *
  4119. * *
  4120. * This routine will search the map looking for a good target to attack. It takes into *
  4121. * consideration the type of weapon it is equipped with. *
  4122. * *
  4123. * INPUT: threat -- The threat type to search for. *
  4124. * *
  4125. * OUTPUT: Returns with a target value of the target that this unit should pursue. If there *
  4126. * is no suitable target, then TARGET_NONE is returned. *
  4127. * *
  4128. * WARNINGS: none *
  4129. * *
  4130. * HISTORY: *
  4131. * 07/09/1996 JLB : Created. *
  4132. *=============================================================================================*/
  4133. TARGET UnitClass::Greatest_Threat(ThreatType threat) const
  4134. {
  4135. assert(IsActive);
  4136. if (Class->PrimaryWeapon != NULL) {
  4137. threat = threat | Class->PrimaryWeapon->Allowed_Threats();
  4138. }
  4139. if (Class->SecondaryWeapon != NULL) {
  4140. threat = threat | Class->SecondaryWeapon->Allowed_Threats();
  4141. }
  4142. #ifdef OBSOLETE
  4143. if (House->IsHuman) {
  4144. threat = threat & ~THREAT_BUILDINGS;
  4145. }
  4146. #endif
  4147. return(FootClass::Greatest_Threat(threat));
  4148. }
  4149. /***********************************************************************************************
  4150. * UnitClass::Read_INI -- Reads units from scenario INI file. *
  4151. * *
  4152. * This routine is used to read all the starting units from the *
  4153. * scenario control INI file. The units are created and placed on the *
  4154. * map by this routine. *
  4155. * *
  4156. * INI entry format: *
  4157. * Housename, Typename, Strength, Coord, Facingnum, Missionname, Triggername *
  4158. * *
  4159. * INPUT: buffer -- Pointer to the loaded scenario INI file. *
  4160. * *
  4161. * OUTPUT: none *
  4162. * *
  4163. * WARNINGS: none *
  4164. * *
  4165. * HISTORY: *
  4166. * 05/24/1994 JLB : Created. *
  4167. *=============================================================================================*/
  4168. void UnitClass::Read_INI(CCINIClass & ini)
  4169. {
  4170. UnitClass * unit; // Working unit pointer.
  4171. HousesType inhouse; // Unit house.
  4172. UnitType classid; // Unit class.
  4173. char buf[128];
  4174. int len = ini.Entry_Count(INI_Name());
  4175. for (int index = 0; index < len; index++) {
  4176. char const * entry = ini.Get_Entry(INI_Name(), index);
  4177. ini.Get_String(INI_Name(), entry, NULL, buf, sizeof(buf));
  4178. inhouse = HouseTypeClass::From_Name(strtok(buf, ","));
  4179. if (inhouse != HOUSE_NONE) {
  4180. classid = UnitTypeClass::From_Name(strtok(NULL, ","));
  4181. if (classid != UNIT_NONE) {
  4182. unit = new UnitClass(classid, inhouse);
  4183. if (unit != NULL) {
  4184. /*
  4185. ** Read the raw data.
  4186. */
  4187. int strength = atoi(strtok(NULL, ",\r\n"));
  4188. CELL cell = atoi(strtok(NULL, ",\r\n"));
  4189. COORDINATE coord = Cell_Coord(cell);
  4190. DirType dir = (DirType)atoi(strtok(NULL, ",\r\n"));
  4191. MissionType mission = MissionClass::Mission_From_Name(strtok(NULL, ",\n\r"));
  4192. unit->Trigger = NULL;
  4193. TriggerTypeClass * tp = TriggerTypeClass::From_Name(strtok(NULL,",\r\n"));
  4194. if (tp != NULL) {
  4195. TriggerClass * tt = Find_Or_Make(tp);
  4196. if (tt != NULL) {
  4197. tt->AttachCount++;
  4198. unit->Trigger = tt;
  4199. }
  4200. }
  4201. if (unit->Unlimbo(coord, dir)) {
  4202. unit->Strength = unit->Class->MaxStrength * fixed(strength, 256);
  4203. if (unit->Strength > unit->Class->MaxStrength-3) unit->Strength = unit->Class->MaxStrength;
  4204. if (Session.Type == GAME_NORMAL || unit->House->IsHuman) {
  4205. unit->Assign_Mission(mission);
  4206. unit->Commence();
  4207. } else {
  4208. unit->Enter_Idle_Mode();
  4209. }
  4210. } else {
  4211. /*
  4212. ** If the unit could not be unlimboed, then this is a catastrophic error
  4213. ** condition. Delete the unit.
  4214. */
  4215. delete unit;
  4216. }
  4217. }
  4218. }
  4219. }
  4220. }
  4221. }
  4222. /***********************************************************************************************
  4223. * UnitClass::Write_INI -- Store the units to the INI database. *
  4224. * *
  4225. * This routine will store all the unit data to the INI database. *
  4226. * *
  4227. * INPUT: ini -- Reference to the INI database object to store to. *
  4228. * *
  4229. * OUTPUT: none *
  4230. * *
  4231. * WARNINGS: none *
  4232. * *
  4233. * HISTORY: *
  4234. * 07/03/1996 JLB : Created. *
  4235. *=============================================================================================*/
  4236. void UnitClass::Write_INI(CCINIClass & ini)
  4237. {
  4238. /*
  4239. ** First, clear out all existing unit data from the ini file.
  4240. */
  4241. ini.Clear(INI_Name());
  4242. /*
  4243. ** Write the unit data out.
  4244. */
  4245. for (int index = 0; index < Units.Count(); index++) {
  4246. UnitClass * unit = Units.Ptr(index);
  4247. if (unit != NULL && !unit->IsInLimbo && unit->IsActive) {
  4248. char uname[10];
  4249. char buf[128];
  4250. sprintf(uname, "%d", index);
  4251. sprintf(buf, "%s,%s,%d,%u,%d,%s,%s",
  4252. unit->House->Class->IniName,
  4253. unit->Class->IniName,
  4254. unit->Health_Ratio()*256,
  4255. Coord_Cell(unit->Coord),
  4256. unit->PrimaryFacing.Current(),
  4257. MissionClass::Mission_Name(unit->Mission),
  4258. unit->Trigger.Is_Valid() ? unit->Trigger->Class->IniName : "None"
  4259. );
  4260. ini.Put_String(INI_Name(), uname, buf);
  4261. }
  4262. }
  4263. }
  4264. /***********************************************************************************************
  4265. * UnitClass::Credit_Load -- Fetch the full credit value of cargo carried. *
  4266. * *
  4267. * This will determine the value of the cargo carried (limited to considering only gold *
  4268. * and gems) and return that value. Use this to determine how 'valuable' a harvester is. *
  4269. * *
  4270. * INPUT: none *
  4271. * *
  4272. * OUTPUT: Returns with the credit value of the cargo load of this unit (harvester). *
  4273. * *
  4274. * WARNINGS: none *
  4275. * *
  4276. * HISTORY: *
  4277. * 07/29/1996 JLB : Created. *
  4278. *=============================================================================================*/
  4279. int UnitClass::Credit_Load(void) const
  4280. {
  4281. return((Gold * Rule.GoldValue) + (Gems * Rule.GemValue));
  4282. }
  4283. /***********************************************************************************************
  4284. * UnitClass::Should_Crush_It -- Determines if this unit should crush an object. *
  4285. * *
  4286. * Call this routine to determine if this unit should crush the object specified. The *
  4287. * test for crushable action depends on proximity and ability of the unit. If a unit *
  4288. * should crush the object, then it should be given a movement order to enter the cell *
  4289. * where the object is located. *
  4290. * *
  4291. * INPUT: it -- The object to see if it should be crushed. *
  4292. * *
  4293. * OUTPUT: bool; Should "it" be crushed by this unit? *
  4294. * *
  4295. * WARNINGS: none *
  4296. * *
  4297. * HISTORY: *
  4298. * 07/29/1996 JLB : Created. *
  4299. *=============================================================================================*/
  4300. bool UnitClass::Should_Crush_It(TechnoClass const * it) const
  4301. {
  4302. assert(IsActive);
  4303. /*
  4304. ** If this unit cannot crush anything or the candidate object cannot be crushed,
  4305. ** then it obviously should not try to crush it -- return negative answer.
  4306. */
  4307. if (!Class->IsCrusher || it == NULL || !it->Techno_Type_Class()->IsCrushable) return(false);
  4308. /*
  4309. ** Objects that are far away should really be fired upon rather than crushed.
  4310. */
  4311. if (Distance(it) > Rule.CrushDistance) return(false);
  4312. /*
  4313. ** Human controlled units don't automatically crush. Neither do computer controlled ones
  4314. ** if they are at difficult setting.
  4315. */
  4316. if (House->IsHuman || House->Difficulty == DIFF_HARD) return(false);
  4317. /*
  4318. ** If the weapon this unit is equipped with is very good against crushable objects then
  4319. ** fire the weapon instead. It is presumed that a wood destroying weapon is good against
  4320. ** most crushable object types (infantry).
  4321. */
  4322. if (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->WarheadPtr->IsWoodDestroyer) return(false);
  4323. /*
  4324. ** If the house IQ indicates that crushing should not be allowed, then don't
  4325. ** suggest that crushing be done.
  4326. */
  4327. if (House->IQ < Rule.IQCrush) return(false);
  4328. /*
  4329. ** Don't allow crushing of spies by computer-controlled vehicles.
  4330. */
  4331. if (it->What_Am_I() == RTTI_INFANTRY && *(InfantryClass *)it == INFANTRY_SPY) {
  4332. return(false);
  4333. }
  4334. return(true);
  4335. }
  4336. /***********************************************************************************************
  4337. * UnitClass::Scatter -- Causes the unit to scatter to a nearby location. *
  4338. * *
  4339. * This scatter logic will actually look for a nearby location rather than an adjacent *
  4340. * free location. This is necessary because sometimes a unit is required to scatter more *
  4341. * than one cell. A vehicle on a service depot is a prime example. *
  4342. * *
  4343. * INPUT: threat -- The coordinate that a potential threat resides. If this is a non *
  4344. * threat related scatter, then this parameter will be zero. *
  4345. * *
  4346. * forced -- Should the scatter be performed even if it would be otherwise *
  4347. * inconvenient? *
  4348. * *
  4349. * nokidding-- Should the scatter be performed even if it would otherwise be *
  4350. * illegal? *
  4351. * *
  4352. * OUTPUT: none *
  4353. * *
  4354. * WARNINGS: none *
  4355. * *
  4356. * HISTORY: *
  4357. * 10/02/1996 JLB : Created. *
  4358. *=============================================================================================*/
  4359. void UnitClass::Scatter(COORDINATE threat, bool forced, bool nokidding)
  4360. {
  4361. assert(IsActive);
  4362. if (Mission == MISSION_SLEEP || Mission == MISSION_STICKY || Mission == MISSION_UNLOAD) return;
  4363. /*
  4364. ** Certain missions prevent scattering regardless of whether it would be
  4365. ** a good idea or not.
  4366. */
  4367. if (!MissionControl[Mission].IsScatter && !forced) return;
  4368. if (PrimaryFacing.Is_Rotating()) return;
  4369. // if (IsRotating) return;
  4370. if (Target_Legal(NavCom) && !nokidding) return;
  4371. if (threat == 0) {
  4372. Assign_Destination(::As_Target(Map.Nearby_Location(Coord_Cell(Coord), Class->Speed)));
  4373. } else {
  4374. DriveClass::Scatter(threat, forced, nokidding);
  4375. }
  4376. }
  4377. /***********************************************************************************************
  4378. * UnitClass::Limbo -- Limbo this unit. *
  4379. * *
  4380. * This will cause the unit to go into a limbo state. If it was carrying a flag, then *
  4381. * the flag will be dropped where the unit is at. *
  4382. * *
  4383. * INPUT: none *
  4384. * *
  4385. * OUTPUT: bool; Was this unit limboed? *
  4386. * *
  4387. * WARNINGS: none *
  4388. * *
  4389. * HISTORY: *
  4390. * 10/08/1996 JLB : Created. *
  4391. *=============================================================================================*/
  4392. bool UnitClass::Limbo(void)
  4393. {
  4394. if (DriveClass::Limbo()) {
  4395. if (Flagged != HOUSE_NONE) {
  4396. HouseClass::As_Pointer(Flagged)->Flag_Attach(Coord_Cell(Coord));
  4397. Flagged = HOUSE_NONE;
  4398. }
  4399. return(true);
  4400. }
  4401. return(false);
  4402. }
  4403. void UnitClass::Shroud_Regen(void)
  4404. {
  4405. if (Class->IsGapper/*KO && !House->IsPlayerControl*/) {
  4406. static int _xtab[]={
  4407. -1, 0, 1,
  4408. -2,-1, 0, 1, 2,
  4409. -2,-1, 0, 1, 2,
  4410. -2,-1, 0, 1, 2,
  4411. -2,-1, 0, 1, 2,
  4412. -2,-1, 0, 1, 2,
  4413. -1, 0, 1
  4414. };
  4415. static int _ytab[]={
  4416. -3,-3,-3,
  4417. -2,-2,-2,-2,-2,
  4418. -1,-1,-1,-1,-1,
  4419. 0, 0, 0, 0, 0,
  4420. 1, 1, 1, 1, 1,
  4421. 2, 2, 2, 2, 2,
  4422. 3, 3, 3
  4423. };
  4424. int index;
  4425. int centerx, centery;
  4426. CELL trycell;
  4427. // Only restore under the shroud if it's a valid field.
  4428. if (ShroudBits != (unsigned)-1L) {
  4429. centerx = Cell_X(ShroudCenter);
  4430. centery = Cell_Y(ShroudCenter);
  4431. for (index = 30; index >= 0 && ShroudBits; index--) {
  4432. if (ShroudBits & 1) {
  4433. trycell = XY_Cell(centerx + _xtab[index], centery + _ytab[index]);
  4434. #if(0)
  4435. Map.Map_Cell(trycell, PlayerPtr);
  4436. #else
  4437. Map.UnJam_Cell(trycell, House);
  4438. Map.Map_Cell(trycell, PlayerPtr);
  4439. #endif
  4440. }
  4441. ShroudBits >>= 1;
  4442. }
  4443. }
  4444. if(IsActive && Strength) {
  4445. // Now shroud around the new center
  4446. ShroudBits = 0L;
  4447. ShroudCenter = Coord_Cell(Center_Coord());
  4448. centerx = Cell_X(ShroudCenter);
  4449. centery = Cell_Y(ShroudCenter);
  4450. for (index = 0; index < 31; index++) {
  4451. ShroudBits <<= 1;
  4452. trycell = XY_Cell(centerx + _xtab[index], centery + _ytab[index]);
  4453. if (Map[trycell].IsMapped) {
  4454. #if(0)
  4455. Map.Shroud_Cell(trycell);
  4456. #else
  4457. Map.Jam_Cell(trycell, House);
  4458. #endif
  4459. ShroudBits |= 1;
  4460. }
  4461. }
  4462. }
  4463. if (!House->IsPlayerControl) {
  4464. Map.Constrained_Look(Coord, 5 * CELL_LEPTON_W);
  4465. // PlayerPtr->IsToLook = true;
  4466. }
  4467. }
  4468. }
  4469. /***********************************************************************************************
  4470. * UnitClass::Mission_Guard_Area -- Guard area logic for units. *
  4471. * *
  4472. * This logic is similar to normal guard area except that APCs owned by the computer will *
  4473. * try to load up with nearby infantry. This will give the computer some fake intelligence *
  4474. * when playing in skirmish mode. *
  4475. * *
  4476. * INPUT: none *
  4477. * *
  4478. * OUTPUT: Returns with the delay to use before calling this routine again. *
  4479. * *
  4480. * WARNINGS: none *
  4481. * *
  4482. * HISTORY: *
  4483. * 11/03/1996 JLB : Created. *
  4484. *=============================================================================================*/
  4485. int UnitClass::Mission_Guard_Area(void)
  4486. {
  4487. assert(IsActive);
  4488. /*
  4489. ** Check to see if this is an APC that is largely empty and not otherwise doing anything.
  4490. ** Such an APC should load up with infantry.
  4491. */
  4492. if (Session.Type != GAME_NORMAL &&
  4493. #ifdef FIXIT_PHASETRANSPORT // checked - ajw 9/28/98
  4494. (*this == UNIT_APC || *this == UNIT_PHASE ) &&
  4495. #else
  4496. *this == UNIT_APC &&
  4497. #endif
  4498. !Target_Legal(TarCom) &&
  4499. !In_Radio_Contact() &&
  4500. House->Which_Zone(this) != ZONE_NONE &&
  4501. !House->IsHuman) {
  4502. int needed = Class->Max_Passengers() - How_Many();
  4503. for (int index = 0; index < Infantry.Count(); index++) {
  4504. if (needed == 0) break;
  4505. InfantryClass * infantry = Infantry.Ptr(index);
  4506. if (infantry != NULL &&
  4507. infantry->IsActive &&
  4508. !infantry->IsInLimbo &&
  4509. infantry->Strength > 0 &&
  4510. infantry->House == House &&
  4511. !Target_Legal(infantry->TarCom) &&
  4512. !Target_Legal(infantry->NavCom) &&
  4513. Distance(infantry) < 7 * CELL_LEPTON_W &&
  4514. (infantry->Mission == MISSION_GUARD || infantry->Mission == MISSION_GUARD_AREA)) {
  4515. infantry->Assign_Mission(MISSION_ENTER);
  4516. infantry->ArchiveTarget = As_Target();
  4517. needed--;
  4518. }
  4519. }
  4520. }
  4521. return(DriveClass::Mission_Guard_Area());
  4522. }