BUILDING.CPP 235 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712
  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/BUILDING.CPP 5 3/13/97 5:18p Joe_b $ */
  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 : BUILDING.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : September 10, 1993 *
  30. * *
  31. * Last Update : October 27, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * BuildingClass::AI -- Handles non-graphic AI processing for buildings. *
  36. * BuildingClass::Active_Click_With -- Handles cell selection for buildings. *
  37. * BuildingClass::Animation_AI -- Handles normal building animation processing. *
  38. * BuildingClass::Assign_Target -- Assigns a target to the building. *
  39. * BuildingClass::Begin_Mode -- Begins an animation mode for the building. *
  40. * BuildingClass::BuildingClass -- Constructor for buildings. *
  41. * BuildingClass::Can_Demolish -- Can the player demolish (sell back) the building? *
  42. * BuildingClass::Can_Enter_Cell -- Determines if building can be placed down. *
  43. * BuildingClass::Can_Fire -- Determines if this building can fire. *
  44. * BuildingClass::Can_Player_Move -- Can this building be moved? *
  45. * BuildingClass::Captured -- Captures the building. *
  46. * BuildingClass::Center_Coord -- Fetches the center coordinate for the building. *
  47. * BuildingClass::Charging_AI -- Handles the special charging logic for Tesla coils. *
  48. * BuildingClass::Check_Point -- Fetches the landing checkpoint for the given flight pattern.*
  49. * BuildingClass::Click_With -- Handles clicking on the map while the building is selected. *
  50. * BuildingClass::Crew_Type -- This determines the crew that this object generates. *
  51. * BuildingClass::Death_Announcement -- Announce the death of this building. *
  52. * BuildingClass::Debug_Dump -- Displays building status to the monochrome screen. *
  53. * BuildingClass::Detach -- Handles target removal from the game system. *
  54. * BuildingClass::Detach_All -- Possibly abandons production according to factory type. *
  55. * BuildingClass::Docking_Coord -- Fetches the coordinate to use for docking. *
  56. * BuildingClass::Draw_It -- Displays the building at the location specified. *
  57. * BuildingClass::Drop_Debris -- Drops rubble when building is destroyed. *
  58. * BuildingClass::Enter_Idle_Mode -- The building will enter its idle mode. *
  59. * BuildingClass::Exit_Coord -- Determines location where object will leave it. *
  60. * BuildingClass::Exit_Object -- Initiates an object to leave the building. *
  61. * BuildingClass::Factory_AI -- Handle factory production and initiation. *
  62. * BuildingClass::Find_Exit_Cell -- Find a clear location to exit an object from this buildin*
  63. * BuildingClass::Fire_Direction -- Fetches the direction of firing. *
  64. * BuildingClass::Fire_Out -- Handles when attached animation expires. *
  65. * BuildingClass::Flush_For_Placement -- Handles clearing a zone for object placement. *
  66. * BuildingClass::Get_Image_Data -- Fetch the image pointer for the building. *
  67. * BuildingClass::Grand_Opening -- Handles construction completed special operations. *
  68. * BuildingClass::Greatest_Threat -- Searches for target that building can fire upon. *
  69. * BuildingClass::How_Many_Survivors -- This determine the maximum number of survivors. *
  70. * BuildingClass::Init -- Initialize the building system to an empty null state. *
  71. * BuildingClass::Limbo -- Handles power adjustment as building goes into limbo. *
  72. * BuildingClass::Mark -- Building interface to map rendering system. *
  73. * BuildingClass::Mission_Attack -- Handles attack mission for building. *
  74. * BuildingClass::Mission_Construction -- Handles mission construction. *
  75. * BuildingClass::Mission_Deconstruction -- Handles building deconstruction. *
  76. * BuildingClass::Mission_Guard -- Handles guard mission for combat buildings. *
  77. * BuildingClass::Mission_Harvest -- Handles refinery unloading harvesters. *
  78. * BuildingClass::Mission_Missile -- State machine for nuclear missile launch. *
  79. * BuildingClass::Mission_Repair -- Handles the repair (active) state for building. *
  80. * BuildingClass::Mission_Unload -- Handles the unload mission for a building. *
  81. * BuildingClass::Pip_Count -- Determines "full" pips to display for building. *
  82. * BuildingClass::Power_Output -- Fetches the current power output from this building. *
  83. * BuildingClass::Read_INI -- Reads buildings from INI file. *
  84. * BuildingClass::Receive_Message -- Handle an incoming message to the building. *
  85. * BuildingClass::Remap_Table -- Fetches the remap table to use for this building. *
  86. * BuildingClass::Remove_Gap_Effect -- Stop a gap generator from jamming cells *
  87. * BuildingClass::Repair -- Initiates or terminates the repair process. *
  88. * BuildingClass::Repair_AI -- Handle the repair (and sell) logic for the building. *
  89. * BuildingClass::Revealed -- Reveals the building to the specified house. *
  90. * BuildingClass::Rotation_AI -- Process any turret rotation required of this building. *
  91. * BuildingClass::Sell_Back -- Controls the sell back (demolish) operation. *
  92. * BuildingClass::Shape_Number -- Fetch the shape number for this building. *
  93. * BuildingClass::Sort_Y -- Returns the building coordinate used for sorting. *
  94. * BuildingClass::Take_Damage -- Inflicts damage points upon a building. *
  95. * BuildingClass::Target_Coord -- Return the coordinate to use when firing on this building. *
  96. * BuildingClass::Toggle_Primary -- Toggles the primary factory state. *
  97. * BuildingClass::Turret_Facing -- Fetches the turret facing for this building. *
  98. * BuildingClass::Unlimbo -- Removes a building from limbo state. *
  99. * BuildingClass::Update_Buildables -- Informs sidebar of additional construction options. *
  100. * BuildingClass::Value -- Determine the value of this building. *
  101. * BuildingClass::What_Action -- Determines action to perform if click on specified object. *
  102. * BuildingClass::What_Action -- Determines what action will occur. *
  103. * BuildingClass::Write_INI -- Write out the building data to the INI file specified. *
  104. * BuildingClass::delete -- Deallocates building object. *
  105. * BuildingClass::new -- Allocates a building object from building pool. *
  106. * BuildingClass::~BuildingClass -- Destructor for building type objects. *
  107. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  108. #include "function.h"
  109. enum SAMState {
  110. SAM_READY, // Launcher can be facing any direction tracking targets.
  111. SAM_FIRING // Stationary while missile is being fired.
  112. };
  113. /***************************************************************************
  114. ** Center of building offset table.
  115. */
  116. COORDINATE const BuildingClass::CenterOffset[BSIZE_COUNT] = {
  117. 0x00800080L,
  118. 0x008000FFL,
  119. 0x00FF0080L,
  120. 0x00FF00FFL,
  121. 0x018000FFL,
  122. 0x00FF0180L,
  123. 0x01800180L,
  124. 0x00FF0200L,
  125. 0x02800280L,
  126. };
  127. /***********************************************************************************************
  128. * BuildingClass::Receive_Message -- Handle an incoming message to the building. *
  129. * *
  130. * This routine handles an incoming message to the building. Messages regulate the *
  131. * various cooperative ventures between buildings and units. This might include such *
  132. * actions as coordinating the construction yard animation with the actual building's *
  133. * construction animation. *
  134. * *
  135. * INPUT: from -- The originator of the message received. *
  136. * *
  137. * message -- The radio message received. *
  138. * *
  139. * param -- Reference to an optional parameter that might be used to return *
  140. * extra information to the message originator. *
  141. * *
  142. * OUTPUT: Returns with the response to the message (typically, this is just RADIO_OK). *
  143. * *
  144. * WARNINGS: none *
  145. * *
  146. * HISTORY: *
  147. * 06/09/1994 JLB : Created. *
  148. * 06/26/1995 JLB : Forces refinery load anim to start immediately. *
  149. * 08/13/1995 JLB : Uses ScenarioInit for special loose "CAN_LOAD" check. *
  150. *=============================================================================================*/
  151. RadioMessageType BuildingClass::Receive_Message(RadioClass * from, RadioMessageType message, long & param)
  152. {
  153. assert(Buildings.ID(this) == ID);
  154. assert(IsActive);
  155. switch (message) {
  156. /*
  157. ** This message is received as a request to attach/load/dock with this building.
  158. ** Verify that this is allowed and return the appropriate response.
  159. */
  160. case RADIO_CAN_LOAD:
  161. TechnoClass::Receive_Message(from, message, param);
  162. if (!House->Is_Ally(from)) return(RADIO_STATIC);
  163. if (Mission == MISSION_CONSTRUCTION || Mission == MISSION_DECONSTRUCTION || BState == BSTATE_CONSTRUCTION || (!ScenarioInit && In_Radio_Contact() && Contact_With_Whom() != from)) return(RADIO_NEGATIVE);
  164. switch (Class->Type) {
  165. case STRUCT_AIRSTRIP:
  166. if (from->What_Am_I() == RTTI_AIRCRAFT && ((AircraftClass const *)from)->Class->IsFixedWing) {
  167. return(RADIO_ROGER);
  168. }
  169. break;
  170. case STRUCT_HELIPAD:
  171. if (from->What_Am_I() == RTTI_AIRCRAFT && !((AircraftClass const *)from)->Class->IsFixedWing) {
  172. return(RADIO_ROGER);
  173. }
  174. break;
  175. case STRUCT_REPAIR:
  176. if (from->What_Am_I() == RTTI_UNIT || (from->What_Am_I() == RTTI_AIRCRAFT)) {
  177. if (Transmit_Message(RADIO_ON_DEPOT, from) != RADIO_ROGER) {
  178. return(RADIO_ROGER);
  179. }
  180. }
  181. return(RADIO_NEGATIVE);
  182. case STRUCT_REFINERY:
  183. if (from->What_Am_I() == RTTI_UNIT &&
  184. *((UnitClass *)from) == UNIT_HARVESTER &&
  185. (ScenarioInit || !Is_Something_Attached())) {
  186. return(RADIO_ROGER);
  187. }
  188. break;
  189. default:
  190. break;
  191. }
  192. return(RADIO_STATIC);
  193. /*
  194. ** This message is received when the object has attached itself to this
  195. ** building.
  196. */
  197. case RADIO_IM_IN:
  198. if (Mission == MISSION_DECONSTRUCTION) {
  199. return(RADIO_NEGATIVE);
  200. }
  201. switch (Class->Type) {
  202. case STRUCT_REPAIR:
  203. IsReadyToCommence = true;
  204. Assign_Mission(MISSION_REPAIR);
  205. from->Assign_Mission(MISSION_SLEEP);
  206. return(RADIO_ROGER);
  207. case STRUCT_AIRSTRIP:
  208. case STRUCT_HELIPAD:
  209. Assign_Mission(MISSION_REPAIR);
  210. from->Assign_Mission(MISSION_SLEEP);
  211. return(RADIO_ROGER);
  212. case STRUCT_REFINERY:
  213. Mark(MARK_CHANGE);
  214. from->Assign_Mission(MISSION_UNLOAD);
  215. return(RADIO_ROGER);
  216. default:
  217. break;
  218. }
  219. break;
  220. /*
  221. ** Docking maneuver maintenance message. See if new order should be given to the
  222. ** unit trying to dock.
  223. */
  224. case RADIO_DOCKING:
  225. TechnoClass::Receive_Message(from, message, param);
  226. /*
  227. ** When in radio contact for loading, the refinery starts
  228. ** flashing the lights.
  229. */
  230. if (*this == STRUCT_REFINERY && BState != BSTATE_FULL) {
  231. Begin_Mode(BSTATE_FULL);
  232. }
  233. /*
  234. ** If this building is already in radio contact, then it might
  235. ** be able to satisfy the request to load by bumping off any
  236. ** preoccupying task.
  237. */
  238. if (*this == STRUCT_REPAIR) {
  239. if (Contact_With_Whom() != from) {
  240. if (Transmit_Message(RADIO_ON_DEPOT) == RADIO_ROGER) {
  241. if (Transmit_Message(RADIO_NEED_REPAIR) == RADIO_NEGATIVE) {
  242. Transmit_Message(RADIO_RUN_AWAY);
  243. return(RADIO_ROGER);
  244. }
  245. // } else {
  246. // if (Transmit_Message(RADIO_NEED_TO_MOVE, from) == RADIO_ROGER) {
  247. // param = (long)As_Target();
  248. // Transmit_Message(RADIO_MOVE_HERE, param, from);
  249. // }
  250. }
  251. } else {
  252. if (Transmit_Message(RADIO_NEED_REPAIR) == RADIO_NEGATIVE) {
  253. return(RADIO_NEGATIVE);
  254. }
  255. }
  256. }
  257. /*
  258. ** Establish contact with the object if this building isn't already in contact
  259. ** with another.
  260. */
  261. if (!In_Radio_Contact()) {
  262. Transmit_Message(RADIO_HELLO, from);
  263. }
  264. if (Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER) {
  265. switch (Class->Type) {
  266. case STRUCT_AIRSTRIP:
  267. param = As_Target();
  268. break;
  269. case STRUCT_HELIPAD:
  270. param = As_Target();
  271. break;
  272. case STRUCT_REPAIR:
  273. Transmit_Message(RADIO_TETHER);
  274. param = ::As_Target(Coord_Cell(Center_Coord()));
  275. break;
  276. case STRUCT_REFINERY:
  277. param = ::As_Target(Coord_Cell(Adjacent_Cell(Center_Coord(), DIR_S)));
  278. break;
  279. }
  280. /*
  281. ** Tell the harvester to move to the docking pad of the building.
  282. */
  283. if (Transmit_Message(RADIO_MOVE_HERE, param) == RADIO_YEA_NOW_WHAT) {
  284. /*
  285. ** Since the harvester is already there, tell it to begin the backup
  286. ** procedure now. If it can't, then tell it to get outta here.
  287. */
  288. Transmit_Message(RADIO_TETHER);
  289. if (*this == STRUCT_REFINERY && Transmit_Message(RADIO_BACKUP_NOW, from) != RADIO_ROGER) {
  290. from->Scatter(NULL, true, true);
  291. }
  292. }
  293. }
  294. return(RADIO_ROGER);
  295. /*
  296. ** If a transport or harvester is requesting permission to head toward, dock
  297. ** and load/unload, check to make sure that this is allowed given the current
  298. ** state of the building.
  299. */
  300. case RADIO_ARE_REFINERY:
  301. if (Is_Something_Attached() || In_Radio_Contact() || IsInLimbo || House->Class->House != from->Owner() || (*this != STRUCT_REFINERY/* && *this != STRUCT_REPAIR*/)) {
  302. return(RADIO_NEGATIVE);
  303. }
  304. return(RADIO_ROGER);
  305. /*
  306. ** Someone is telling us that it is starting construction. This should only
  307. ** occur if this is a construction yard and a building was just placed on
  308. ** the map.
  309. */
  310. case RADIO_BUILDING:
  311. Assign_Mission(MISSION_REPAIR);
  312. TechnoClass::Receive_Message(from, message, param);
  313. return(RADIO_ROGER);
  314. /*
  315. ** Someone is telling us that they have finished construction. This should
  316. ** only occur if this is a construction yard and the building that was being
  317. ** constructed has finished. In this case, stop the construction yard
  318. ** animation.
  319. */
  320. case RADIO_COMPLETE:
  321. if (Mission != MISSION_DECONSTRUCTION) {
  322. Assign_Mission(MISSION_GUARD);
  323. }
  324. TechnoClass::Receive_Message(from, message, param);
  325. return(RADIO_ROGER);
  326. /*
  327. ** This message may occur unexpectedly if the unit in contact with this
  328. ** building is suddenly destroyed. Handle any cleanup necessary. For example,
  329. ** a construction yard should stop its construction animation in this case.
  330. */
  331. case RADIO_OVER_OUT:
  332. Begin_Mode(BSTATE_IDLE);
  333. TechnoClass::Receive_Message(from, message, param);
  334. return(RADIO_ROGER);
  335. /*
  336. ** This message is received when an object has completely left
  337. ** building. Sometimes special cleanup action is required when
  338. ** this event occurs.
  339. */
  340. case RADIO_UNLOADED:
  341. if (*this == STRUCT_REPAIR) {
  342. if (Distance(from) < 0x0180) {
  343. return(RADIO_ROGER);
  344. }
  345. }
  346. TechnoClass::Receive_Message(from, message, param);
  347. if (*this == STRUCT_WEAP || *this == STRUCT_AIRSTRIP || *this == STRUCT_REPAIR) return(RADIO_RUN_AWAY);
  348. return(RADIO_ROGER);
  349. default:
  350. break;
  351. }
  352. /*
  353. ** Pass along the message to the default message handler in the radio itself.
  354. */
  355. return(TechnoClass::Receive_Message(from, message, param));
  356. }
  357. #ifdef CHEAT_KEYS
  358. /***********************************************************************************************
  359. * BuildingClass::Debug_Dump -- Displays building status to the monochrome screen. *
  360. * *
  361. * This utility function will output the current status of the building class to the *
  362. * monochrome screen. It is through this data that bugs may be fixed or detected. *
  363. * *
  364. * INPUT: none *
  365. * *
  366. * OUTPUT: none *
  367. * *
  368. * WARNINGS: none *
  369. * *
  370. * HISTORY: *
  371. * 05/31/1994 JLB : Created. *
  372. *=============================================================================================*/
  373. void BuildingClass::Debug_Dump(MonoClass * mono) const
  374. {
  375. assert(Buildings.ID(this) == ID);
  376. assert(IsActive);
  377. mono->Set_Cursor(0, 0);
  378. mono->Print(Text_String(TXT_DEBUG_BUILDING));
  379. mono->Fill_Attrib(66, 13, 12, 1, IsRepairing ? MonoClass::INVERSE : MonoClass::NORMAL);
  380. mono->Fill_Attrib(66, 14, 12, 1, IsToRebuild ? MonoClass::INVERSE : MonoClass::NORMAL);
  381. mono->Fill_Attrib(66, 15, 12, 1, IsAllowedToSell ? MonoClass::INVERSE : MonoClass::NORMAL);
  382. mono->Fill_Attrib(66, 16, 12, 1, IsCharging ? MonoClass::INVERSE : MonoClass::NORMAL);
  383. mono->Fill_Attrib(66, 17, 12, 1, IsCharged ? MonoClass::INVERSE : MonoClass::NORMAL);
  384. mono->Fill_Attrib(66, 18, 12, 1, IsJamming ? MonoClass::INVERSE : MonoClass::NORMAL);
  385. mono->Fill_Attrib(66, 19, 12, 1, IsJammed ? MonoClass::INVERSE : MonoClass::NORMAL);
  386. mono->Set_Cursor(1, 11);
  387. if (Factory) {
  388. mono->Printf("%s %d%%", Factory->Get_Object()->Class_Of().IniName, (100*Factory->Completion())/FactoryClass::STEP_COUNT);
  389. }
  390. TechnoClass::Debug_Dump(mono);
  391. }
  392. #endif
  393. /***********************************************************************************************
  394. * BuildingClass::Draw_It -- Displays the building at the location specified. *
  395. * *
  396. * This is the low level graphic routine that displays the building at the location *
  397. * specified. *
  398. * *
  399. * INPUT: x,y -- The coordinate to draw the building at. *
  400. * *
  401. * window -- The clipping window to use. *
  402. * *
  403. * OUTPUT: none *
  404. * *
  405. * WARNINGS: none *
  406. * *
  407. * HISTORY: *
  408. * 06/20/1994 JLB : Created. *
  409. * 06/27/1994 JLB : Takes a clipping window parameter. *
  410. * 07/06/1995 JLB : Handles damaged silos correctly. *
  411. *=============================================================================================*/
  412. void BuildingClass::Draw_It(int x, int y, WindowNumberType window) const
  413. {
  414. assert(Buildings.ID(this) == ID);
  415. assert(IsActive);
  416. /*
  417. ** The shape file to use for rendering depends on whether the building
  418. ** is undergoing construction or not.
  419. */
  420. void const * shapefile = Get_Image_Data();
  421. if (shapefile == NULL) return;
  422. /*
  423. ** Actually draw the building shape.
  424. */
  425. IsTheaterShape = Class->IsTheater; //Let Build_Frame know if this is a theater specific shape
  426. Techno_Draw_Object(shapefile, Shape_Number(), x, y, window);
  427. IsTheaterShape = false;
  428. /*
  429. ** Patch for adding overlay onto weapon factory. Only add the overlay if
  430. ** the building has more than 1 hp. Also, if the building's in radio
  431. ** contact, he must be unloading a constructed vehicle, so draw that
  432. ** vehicle before drawing the overlay.
  433. */
  434. if (BState != BSTATE_CONSTRUCTION) {
  435. /*
  436. ** A Tethered object is always rendered AFTER the building.
  437. */
  438. if (IsTethered && In_Radio_Contact() && !Contact_With_Whom()->IsInLimbo && Contact_With_Whom()->What_Am_I() != RTTI_BUILDING) {
  439. TechnoClass * contact = Contact_With_Whom();
  440. assert(contact->IsActive);
  441. int xxx = x + ((int)Lepton_To_Pixel((int)Coord_X(contact->Render_Coord())) - (int)Lepton_To_Pixel((int)Coord_X(Render_Coord())));
  442. int yyy = y + ((int)Lepton_To_Pixel((int)Coord_Y(contact->Render_Coord())) - (int)Lepton_To_Pixel((int)Coord_Y(Render_Coord())));
  443. contact->Draw_It(xxx, yyy, window);
  444. contact->IsToDisplay = false;
  445. }
  446. /*
  447. ** Draw the weapon factory custom overlay graphic.
  448. */
  449. if ( (*this == STRUCT_WEAP || *this == STRUCT_FAKEWEAP)) {
  450. int shapenum = Door_Stage();
  451. if (Health_Ratio() <= Rule.ConditionYellow) shapenum += 4;
  452. Techno_Draw_Object(Class->WarFactoryOverlay, shapenum, x, y, window);
  453. }
  454. /*
  455. ** Draw any repair feedback graphic required.
  456. */
  457. if (IsRepairing && IsWrenchVisible) {
  458. CC_Draw_Shape(ObjectTypeClass::SelectShapes, SELECT_WRENCH, x, y, window, SHAPE_CENTER|SHAPE_WIN_REL);
  459. }
  460. }
  461. TechnoClass::Draw_It(x, y, window);
  462. /*
  463. ** If this is a factory that we're spying on, show what it's producing
  464. */
  465. if (SpiedBy & (1<<(PlayerPtr->Class->House)) && IsSelected) {
  466. /*
  467. ** Fetch the factory that is associate with this building. For computer controlled buildings, the
  468. ** factory pointer is integral to the building itself. For human controlled buildings, the factory
  469. ** pointer is part of the house structure and must be retrieved from there.
  470. */
  471. FactoryClass * factory = NULL;
  472. if (House->IsHuman) {
  473. factory = House->Fetch_Factory(Class->ToBuild);
  474. } else {
  475. factory = Factory;
  476. }
  477. /*
  478. ** If there is a factory associated with this building, then fetch any attached
  479. ** object under production and display its cameo image over the top of this building.
  480. */
  481. if (factory != NULL) {
  482. TechnoClass * obj = factory->Get_Object();
  483. if (obj != NULL) {
  484. #ifdef FIXIT_CSII
  485. CC_Draw_Shape(obj->Techno_Type_Class()->Get_Cameo_Data(), 0, x, y, window, SHAPE_CENTER|SHAPE_WIN_REL|SHAPE_NORMAL, NULL);
  486. #else
  487. void const * remapper = obj->House->Remap_Table(false, obj->Techno_Type_Class()->Remap);
  488. CC_Draw_Shape(obj->Techno_Type_Class()->Get_Cameo_Data(), 0, x, y, window, SHAPE_CENTER|SHAPE_WIN_REL | ((remapper != NULL) ? SHAPE_FADING : SHAPE_NORMAL), remapper);
  489. #endif
  490. }
  491. }
  492. }
  493. }
  494. /***********************************************************************************************
  495. * BuildingClass::Shape_Number -- Fetch the shape number for this building. *
  496. * *
  497. * This routine will examine the current state of the building and return with the shape *
  498. * number to use. The shape number is subordinate to the building graphic image data. *
  499. * *
  500. * INPUT: none *
  501. * *
  502. * OUTPUT: Returns with the shape number to use when rendering this building. *
  503. * *
  504. * WARNINGS: none *
  505. * *
  506. * HISTORY: *
  507. * 07/29/1996 JLB : Created. *
  508. *=============================================================================================*/
  509. int BuildingClass::Shape_Number(void) const
  510. {
  511. assert(Buildings.ID(this) == ID);
  512. assert(IsActive);
  513. int shapenum = Fetch_Stage();
  514. /*
  515. ** The shape file to use for rendering depends on whether the building
  516. ** is undergoing construction or not.
  517. */
  518. if (BState == BSTATE_CONSTRUCTION) {
  519. /*
  520. ** If the building is deconstructing, then the display frame progresses
  521. ** from the end to the beginning. Reverse the shape number accordingly.
  522. */
  523. if (Mission == MISSION_DECONSTRUCTION) {
  524. shapenum = (Class->Anims[BState].Start+Class->Anims[BState].Count-1)-shapenum;
  525. }
  526. } else {
  527. /*
  528. ** If this is a camouflaged pill box and it is not owned by the player, then
  529. ** it is displayed with the MEGA-camouflaged imagery.
  530. */
  531. if ((!IsOwnedByPlayer) && (*this == STRUCT_CAMOPILLBOX)) {
  532. shapenum += 1;
  533. }
  534. /*
  535. ** The Tesla Coil has a stage value that can be overridden by
  536. ** its current state.
  537. */
  538. if (*this == STRUCT_TESLA) {
  539. if (IsCharged) {
  540. shapenum = 3;
  541. } else {
  542. if (IsCharging) {
  543. shapenum = Fetch_Stage();
  544. } else {
  545. shapenum = 0;
  546. }
  547. }
  548. }
  549. /*
  550. ** Buildings that contain a turret handle their shape determination
  551. ** differently than normal buildings. They need to take into consideration
  552. ** the direction the turret is facing.
  553. */
  554. if (Class->IsTurretEquipped) {
  555. shapenum = UnitClass::BodyShape[Dir_To_32(PrimaryFacing.Current())];
  556. if (*this == STRUCT_SAM) {
  557. /*
  558. ** SAM sites that are free to rotate fetch their animation frame
  559. ** from the building's turret facing. All other animation stages
  560. ** fetch their frame from the embedded animation sequencer.
  561. */
  562. // if (Status == SAM_READY || Status == SAM_FIRING || Status == SAM_LOCKING) {
  563. // shapenum = Fetch_Stage();
  564. // }
  565. if (Health_Ratio() <= Rule.ConditionYellow) {
  566. shapenum += 35;
  567. }
  568. } else {
  569. if (IsInRecoilState) {
  570. shapenum += 32;
  571. }
  572. if (Health_Ratio() <= Rule.ConditionYellow) {
  573. shapenum += 64;
  574. }
  575. }
  576. } else {
  577. /*
  578. ** If it is a significantly damaged weapons factory, it is shown in
  579. ** the worst state possible.
  580. */
  581. if (*this == STRUCT_WEAP || *this == STRUCT_FAKEWEAP) {
  582. shapenum = 0;
  583. if (Health_Ratio() <= Rule.ConditionYellow) {
  584. shapenum = 1;
  585. }
  586. } else {
  587. /*
  588. ** Special render stage for silos. The stage is dependent on the current
  589. ** Tiberium collected as it relates to Tiberium capacity.
  590. */
  591. if (*this == STRUCT_STORAGE) {
  592. int level = 0;
  593. if (House->Capacity) {
  594. level = (House->Tiberium * 5) / House->Capacity;
  595. }
  596. shapenum += Bound(level, 0, 4);
  597. if (Health_Ratio() <= Rule.ConditionYellow) {
  598. shapenum += 5;
  599. }
  600. } else {
  601. /*
  602. ** If below half strenth, then show the damage frames of the
  603. ** building.
  604. */
  605. if (Health_Ratio() <= Rule.ConditionYellow) {
  606. int last1 = Class->Anims[BSTATE_IDLE].Start + Class->Anims[BSTATE_IDLE].Count;
  607. int last2 = Class->Anims[BSTATE_ACTIVE].Start + Class->Anims[BSTATE_ACTIVE].Count;
  608. int largest = max(last1, last2);
  609. last2 = Class->Anims[BSTATE_AUX1].Start + Class->Anims[BSTATE_AUX1].Count;
  610. largest = max(largest, last2);
  611. last2 = Class->Anims[BSTATE_AUX2].Start + Class->Anims[BSTATE_AUX2].Count;
  612. largest = max(largest, last2);
  613. shapenum += largest;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. return(shapenum);
  620. }
  621. /***********************************************************************************************
  622. * BuildingClass::Mark -- Building interface to map rendering system. *
  623. * *
  624. * This routine is used to mark the map cells so that when it renders *
  625. * the underlying icons will also be updated as necessary. *
  626. * *
  627. * INPUT: mark -- Type of image change (MARK_UP, _DOWN, _CHANGE) *
  628. * MARK_UP -- Building is removed. *
  629. * MARK_CHANGE -- Building changes shape. *
  630. * MARK_DOWN -- Building is added. *
  631. * *
  632. * OUTPUT: bool; Did the mark operation succeed? Failure could be the result of marking down *
  633. * when the building is already marked down, or visa versa. *
  634. * *
  635. * WARNINGS: none *
  636. * *
  637. * HISTORY: *
  638. * 03/31/1994 JLB : Created. *
  639. * 04/15/1994 JLB : Converted to member function. *
  640. * 04/16/1994 JLB : Added health bar tracking. *
  641. * 12/23/1994 JLB : Calls low level check before proceeding. *
  642. * 01/27/1995 JLB : Special road spacer template added. *
  643. *=============================================================================================*/
  644. bool BuildingClass::Mark(MarkType mark)
  645. {
  646. assert(Buildings.ID(this) == ID);
  647. assert(IsActive);
  648. if (TechnoClass::Mark(mark)) {
  649. short const * offset = Overlap_List();
  650. short const * occupy = Occupy_List();
  651. CELL cell = Coord_Cell(Coord);
  652. SmudgeType bib;
  653. switch (mark) {
  654. case MARK_UP:
  655. Map.Pick_Up(cell, this);
  656. if (Class->Bib_And_Offset(bib, cell)) {
  657. SmudgeClass * smudge = new SmudgeClass(bib);
  658. if (smudge != NULL) {
  659. smudge->Disown(cell);
  660. delete smudge;
  661. }
  662. }
  663. break;
  664. case MARK_DOWN:
  665. /*
  666. ** Special wall logic is handled here. A building that is really a wall
  667. ** gets converted into an overlay wall type when it is placed down. The
  668. ** actual building object itself is destroyed.
  669. */
  670. if (Class->IsWall) {
  671. switch (Class->Type) {
  672. case STRUCT_BRICK_WALL:
  673. new OverlayClass(OVERLAY_BRICK_WALL, cell, House->Class->House);
  674. break;
  675. case STRUCT_BARBWIRE_WALL:
  676. new OverlayClass(OVERLAY_BARBWIRE_WALL, cell, House->Class->House);
  677. break;
  678. case STRUCT_SANDBAG_WALL:
  679. new OverlayClass(OVERLAY_SANDBAG_WALL, cell, House->Class->House);
  680. break;
  681. case STRUCT_WOOD_WALL:
  682. new OverlayClass(OVERLAY_WOOD_WALL, cell, House->Class->House);
  683. break;
  684. case STRUCT_CYCLONE_WALL:
  685. new OverlayClass(OVERLAY_CYCLONE_WALL, cell, House->Class->House);
  686. break;
  687. case STRUCT_FENCE:
  688. new OverlayClass(OVERLAY_FENCE, cell, House->Class->House);
  689. break;
  690. default:
  691. break;
  692. }
  693. Transmit_Message(RADIO_OVER_OUT);
  694. delete this;
  695. } else {
  696. if (Can_Enter_Cell(cell) == MOVE_OK) {
  697. /*
  698. ** Determine if a bib is required for this building. If one is, then
  699. ** create and place it.
  700. */
  701. CELL newcell = cell;
  702. if (Class->Bib_And_Offset(bib, newcell)) {
  703. new SmudgeClass(bib, Cell_Coord(newcell), Class->IsBase ? House->Class->House : HOUSE_NONE);
  704. }
  705. Map.Place_Down(cell, this);
  706. } else {
  707. return(false);
  708. }
  709. }
  710. break;
  711. case MARK_CHANGE_REDRAW:
  712. Map.Refresh_Cells(cell, Overlap_List(true));
  713. break;
  714. default:
  715. Map.Refresh_Cells(cell, Overlap_List(false));
  716. Map.Refresh_Cells(cell, occupy);
  717. break;
  718. }
  719. return(true);
  720. }
  721. return(false);
  722. }
  723. /***********************************************************************************************
  724. * BuildingClass::AI -- Handles non-graphic AI processing for buildings. *
  725. * *
  726. * This function is to handle the AI logic for the building. The graphic logic (facing, *
  727. * firing, and animation) is handled elsewhere. *
  728. * *
  729. * INPUT: none *
  730. * *
  731. * OUTPUT: none *
  732. * *
  733. * WARNINGS: none *
  734. * *
  735. * HISTORY: *
  736. * 05/31/1994 JLB : Created. *
  737. * 12/26/1994 JLB : Handles production. *
  738. * 06/11/1995 JLB : Revamped. *
  739. *=============================================================================================*/
  740. void BuildingClass::AI(void)
  741. {
  742. assert(Buildings.ID(this) == ID);
  743. assert(IsActive);
  744. /*
  745. ** Process building animation state changes. Transition to a following state
  746. ** if there is one specified and the current animation sequence has expired.
  747. ** This process must occur before mission AI since the mission AI relies on
  748. ** the bstate change to occur immediately before the MissionClass::AI.
  749. */
  750. Animation_AI();
  751. /*
  752. ** If now is a good time to act on a new mission, then do so. This process occurs
  753. ** here because some outside event may have requested a mission change for the building.
  754. ** Such outside requests (player input) must be initiated BEFORE the normal AI process.
  755. */
  756. if (IsReadyToCommence && BState != BSTATE_CONSTRUCTION) {
  757. /*
  758. ** Clear the commencement flag ONLY if something actually occurred. By acting
  759. ** this way, a building can set the IsReadyToCommence flag before it goes
  760. ** to "sleep" knowing that it will wake up as soon as a new mission comes
  761. ** along.
  762. */
  763. if (Commence()) {
  764. IsReadyToCommence = false;
  765. }
  766. }
  767. /*
  768. ** Proceed with normal logic processing. This is where the mission processing
  769. ** occurs. This call must be located after the animation sequence makes the
  770. ** transition to the next frame (see above) in order for the mission logic to
  771. ** act at the exact moment of graphic transition BEFORE it has a chance to
  772. ** be displayed.
  773. */
  774. TechnoClass::AI();
  775. /*
  776. ** Bail if the object died in the AI routine.
  777. */
  778. if (!IsActive) {
  779. return;
  780. }
  781. /*
  782. ** Building ammo is instantly reloaded.
  783. */
  784. if (!Ammo) {
  785. Ammo = Class->MaxAmmo;
  786. }
  787. /*
  788. ** If now is a good time to act on a new mission, then do so. This occurs here because
  789. ** some AI event may have requested a mission change (usually from another mission
  790. ** state machine). This must occur here before it has a chance to render.
  791. */
  792. if (IsReadyToCommence) {
  793. /*
  794. ** Clear the commencement flag ONLY if something actually occurred. By acting
  795. ** this way, a building can set the IsReadyToCommence flag before it goes
  796. ** to "sleep" knowing that it will wake up as soon as a new mission comes
  797. ** along.
  798. */
  799. if (Commence()) {
  800. IsReadyToCommence = false;
  801. }
  802. }
  803. /*
  804. ** If a change of animation was requested, then make the change
  805. ** now. The building animation system acts independently but subordinate
  806. ** to the mission state machine system. By performing the animation change-up
  807. ** here, the mission AI system is ensured of immediate visual affect when it
  808. ** decides to change the animation state of the building.
  809. */
  810. if (QueueBState != BSTATE_NONE) {
  811. if (BState != QueueBState) {
  812. BState = QueueBState;
  813. BuildingTypeClass::AnimControlType const * ctrl = Fetch_Anim_Control();
  814. if (BState == BSTATE_CONSTRUCTION || BState == BSTATE_IDLE) {
  815. Set_Rate(Options.Normalize_Delay(ctrl->Rate));
  816. } else {
  817. Set_Rate(ctrl->Rate);
  818. }
  819. Set_Stage(ctrl->Start);
  820. }
  821. QueueBState = BSTATE_NONE;
  822. }
  823. /*
  824. ** If the building's strength has changed, then update the power
  825. ** accordingly.
  826. */
  827. if (Strength != LastStrength) {
  828. int oldpower = Power_Output();
  829. LastStrength = Strength;
  830. int newpower = Power_Output();
  831. House->Adjust_Power(newpower - oldpower);
  832. }
  833. /*
  834. ** Check to see if the destruction countdown timer is active. If so, then decrement it.
  835. ** When this timer reaches zero, the building is removed from the map. All the explosions
  836. ** are presumed to be in progress at this time.
  837. */
  838. if (Strength == 0) {
  839. if (CountDown == 0) {
  840. Limbo();
  841. Drop_Debris(WhomToRepay);
  842. delete this;
  843. }
  844. return;
  845. }
  846. /*
  847. ** Charging logic.
  848. */
  849. Charging_AI();
  850. /*
  851. ** Handle any repair process that may be going on.
  852. */
  853. Repair_AI();
  854. /*
  855. ** For computer controlled buildings, determine what should be produced and start
  856. ** production accordingly.
  857. */
  858. Factory_AI();
  859. /*
  860. ** Check for demolition timeout. When timeout has expired, the building explodes.
  861. */
  862. if (IsGoingToBlow && CountDown == 0) {
  863. int damage = Strength;
  864. Take_Damage(damage, 0, WARHEAD_FIRE, As_Techno(WhomToRepay), true);
  865. if (!IsActive) {
  866. return;
  867. }
  868. Mark(MARK_CHANGE);
  869. }
  870. /*
  871. ** Turret equiped buildings must handle turret rotation logic here. This entails
  872. ** rotating the turret to the desired facing as well as figuring out what that
  873. ** desired facing should be.
  874. */
  875. Rotation_AI();
  876. /*
  877. ** Gap Generators need to scan if they've just become activated, or if
  878. ** the power has just come on enough so they can scan. Also, they need
  879. ** to un-jam if the power has just dropped off.
  880. */
  881. if (*this == STRUCT_GAP) {
  882. if (Arm == 0) {
  883. IsJamming = false;
  884. Arm = TICKS_PER_MINUTE * Rule.GapRegenInterval + Random_Pick(1, TICKS_PER_SECOND);
  885. }
  886. if (!IsJamming) {
  887. if (House->Power_Fraction() >= 1) {
  888. Map.Jam_From(Coord_Cell(Center_Coord()), Rule.GapShroudRadius, House);
  889. IsJamming = true;
  890. }
  891. } else {
  892. if (House->Power_Fraction() < 1) {
  893. IsJamming = false;
  894. Map.UnJam_From(Coord_Cell(Center_Coord()), Rule.GapShroudRadius, House);
  895. }
  896. }
  897. }
  898. /*
  899. ** Radar facilities and SAMs need to check for the proximity of a mobile
  900. ** radar jammer.
  901. */
  902. if ((*this == STRUCT_RADAR || *this == STRUCT_SAM) && (Frame % TICKS_PER_SECOND) == 0) {
  903. IsJammed = false;
  904. for (int index = 0; index < Units.Count(); index++) {
  905. UnitClass * obj = Units.Ptr(index);
  906. if (obj != NULL &&
  907. !obj->IsInLimbo &&
  908. !obj->House->Is_Ally(House) &&
  909. obj->Class->IsJammer &&
  910. Distance(obj) <= Rule.RadarJamRadius) {
  911. IsJammed = true;
  912. break;
  913. }
  914. }
  915. }
  916. }
  917. /***********************************************************************************************
  918. * BuildingClass::Unlimbo -- Removes a building from limbo state. *
  919. * *
  920. * Use this routine to transform a building that has been held in limbo *
  921. * state, into one that really exists on the map. Once a building as *
  922. * been unlimboed, then it becomes a normal object in the game world. *
  923. * *
  924. * INPUT: pos -- The position to place the building on the map. *
  925. * *
  926. * dir (optional) -- not used for this class *
  927. * *
  928. * OUTPUT: bool; Was the unlimbo successful? *
  929. * *
  930. * WARNINGS: The unlimbo operation might not be successful if the *
  931. * building could not be placed at the location specified. *
  932. * *
  933. * HISTORY: *
  934. * 04/16/1994 JLB : Created. *
  935. * 06/07/1994 JLB : Matches virtual function format for base class. *
  936. * 05/09/1995 JLB : Handles wall placement. *
  937. * 06/18/1995 JLB : Checks for wall legality before placing down. *
  938. *=============================================================================================*/
  939. bool BuildingClass::Unlimbo(COORDINATE coord, DirType dir)
  940. {
  941. assert(Buildings.ID(this) == ID);
  942. assert(IsActive);
  943. /*
  944. ** If this is a wall type building, then it never gets unlimboed. Instead, it gets
  945. ** converted to an overlay type.
  946. */
  947. if (Class->IsWall) {
  948. if (Can_Enter_Cell(Coord_Cell(coord), FACING_NONE) == MOVE_OK) {
  949. OverlayType otype = OVERLAY_NONE;
  950. switch (Class->Type) {
  951. case STRUCT_SANDBAG_WALL:
  952. otype = OVERLAY_SANDBAG_WALL;
  953. break;
  954. case STRUCT_CYCLONE_WALL:
  955. otype = OVERLAY_CYCLONE_WALL;
  956. break;
  957. case STRUCT_BRICK_WALL:
  958. otype = OVERLAY_BRICK_WALL;
  959. break;
  960. case STRUCT_BARBWIRE_WALL:
  961. otype = OVERLAY_BARBWIRE_WALL;
  962. break;
  963. case STRUCT_WOOD_WALL:
  964. otype = OVERLAY_WOOD_WALL;
  965. break;
  966. case STRUCT_FENCE:
  967. otype = OVERLAY_FENCE;
  968. break;
  969. default:
  970. otype = OVERLAY_NONE;
  971. break;
  972. }
  973. if (otype != OVERLAY_NONE) {
  974. ObjectClass * o = OverlayTypeClass::As_Reference(otype).Create_One_Of(House);
  975. if (o && o->Unlimbo(coord)) {
  976. Map[coord].Owner = House->Class->House;
  977. Transmit_Message(RADIO_OVER_OUT);
  978. Map.Sight_From(Coord_Cell(coord), Class->SightRange, House);
  979. delete this;
  980. return(true);
  981. }
  982. }
  983. }
  984. return(false);
  985. }
  986. /*
  987. ** Normal building unlimbo process.
  988. */
  989. if (TechnoClass::Unlimbo(coord, dir)) {
  990. /*
  991. ** Ensure that the owning house knows about the
  992. ** new object.
  993. */
  994. House->BScan |= (1L << Class->Type);
  995. House->ActiveBScan |= (1L << Class->Type);
  996. /*
  997. ** Recalculate the center point of the house's base.
  998. */
  999. House->Recalc_Center();
  1000. /*
  1001. ** Update the total factory type, assuming this building has a factory.
  1002. */
  1003. House->Active_Add(this);
  1004. /*
  1005. ** Possibly the sidebar will be affected by this addition.
  1006. */
  1007. House->IsRecalcNeeded = true;
  1008. LastStrength = 0;
  1009. if ((!IsDiscoveredByPlayer && Map[coord].IsVisible) || Session.Type != GAME_NORMAL) {
  1010. Revealed(PlayerPtr);
  1011. }
  1012. if (!House->IsHuman) {
  1013. Revealed(House);
  1014. }
  1015. if (IsOwnedByPlayer) {
  1016. Map.PowerClass::IsToRedraw = true;
  1017. Map.Flag_To_Redraw(false);
  1018. }
  1019. if ((Class->Ownable & (HOUSEF_GOOD | HOUSEF_BAD)) != (HOUSEF_GOOD | HOUSEF_BAD)) {
  1020. if (Class->Ownable & HOUSEF_GOOD) {
  1021. ActLike = HOUSE_GREECE;
  1022. } else {
  1023. ActLike = HOUSE_USSR;
  1024. }
  1025. }
  1026. return(true);
  1027. }
  1028. return(false);
  1029. }
  1030. /***********************************************************************************************
  1031. * BuildingClass::Take_Damage -- Inflicts damage points upon a building. *
  1032. * *
  1033. * This routine will inflict damage points upon the specified building. *
  1034. * It will handle the damage animation and building destruction. Use *
  1035. * this routine whenever a building is attacked. *
  1036. * *
  1037. * INPUT: damage -- Amount of damage to inflict. *
  1038. * *
  1039. * distance -- The distance from the damage center point to the object's center point.*
  1040. * *
  1041. * warhead -- The kind of damage to inflict. *
  1042. * *
  1043. * source -- The source of the damage. This is used to change targeting. *
  1044. * *
  1045. * forced -- Is the damage forced upon the object regardless of whether it *
  1046. * is normally immune? *
  1047. * *
  1048. * OUTPUT: true/false; Was the building destroyed? *
  1049. * *
  1050. * WARNINGS: none *
  1051. * *
  1052. * HISTORY: *
  1053. * 07/21/1991 : Created. *
  1054. * 04/15/1994 JLB : Converted to member function. *
  1055. * 04/16/1994 JLB : Added warhead modifier to damage. *
  1056. * 06/03/1994 JLB : Added source of damage as target value. *
  1057. * 06/20/1994 JLB : Source is a base class pointer. *
  1058. * 11/22/1994 JLB : Shares base damage handler for techno objects. *
  1059. * 07/15/1995 JLB : Power ratio gets adjusted. *
  1060. *=============================================================================================*/
  1061. ResultType BuildingClass::Take_Damage(int & damage, int distance, WarheadType warhead, TechnoClass * source, int forced)
  1062. {
  1063. assert(Buildings.ID(this) == ID);
  1064. assert(IsActive);
  1065. ResultType res = RESULT_NONE;
  1066. int shakes;
  1067. if (this != source /*&& !Class->IsInsignificant*/) {
  1068. if (source) {
  1069. House->LATime = Frame;
  1070. House->LAType = source->What_Am_I();
  1071. House->LAZone = House->Which_Zone(this);
  1072. House->LAEnemy = source->Owner();
  1073. if (!House->Is_Ally(source)) {
  1074. House->Enemy = source->Owner();
  1075. }
  1076. Base_Is_Attacked(source);
  1077. }
  1078. short const * offset = Occupy_List();
  1079. /*
  1080. ** Memorize who they used to be in radio contact with.
  1081. */
  1082. TechnoClass *tech = Contact_With_Whom();
  1083. /*
  1084. ** Perform the low level damage assessment.
  1085. */
  1086. res = TechnoClass::Take_Damage(damage, distance, warhead, source, forced);
  1087. switch (res) {
  1088. case RESULT_DESTROYED:
  1089. /*
  1090. ** Add the building to the base prebuild list if allowed. This will force
  1091. ** the computer to rebuild this structure if it can.
  1092. */
  1093. if (IsToRebuild && Class->Level != -1 && Base.House == House->Class->House && Base.Get_Node(this) == 0) {
  1094. // if (IsToRebuild && Class->IsBuildable && Base.House == House->Class->House && Base.Get_Node(this) == 0) {
  1095. Base.Nodes.Add(BaseNodeClass(Class->Type, Coord_Cell(Coord)));
  1096. }
  1097. /*
  1098. ** Destroy all attached objects.
  1099. */
  1100. while (Attached_Object()) {
  1101. FootClass * obj = Detach_Object();
  1102. Detach_All(true);
  1103. delete obj;
  1104. }
  1105. /*
  1106. ** If we were in contact with a landed plane, blow the plane up too.
  1107. */
  1108. if (tech && tech->IsActive && tech->What_Am_I() == RTTI_AIRCRAFT && ((AircraftClass *)tech)->Class->IsFixedWing && ((AircraftClass *)tech)->In_Which_Layer() == LAYER_GROUND) {
  1109. int damage = 500;
  1110. tech->Take_Damage(damage, 0, WARHEAD_AP, source, forced);
  1111. }
  1112. Sound_Effect(VOC_KABOOM22, Coord);
  1113. while (*offset != REFRESH_EOL) {
  1114. CELL cell = Coord_Cell(Coord) + *offset++;
  1115. /*
  1116. ** If the building is destroyed, then lots of
  1117. ** explosions occur.
  1118. */
  1119. new SmudgeClass(Random_Pick(SMUDGE_CRATER1, SMUDGE_CRATER6), Cell_Coord(cell));
  1120. if (Percent_Chance(50)) {
  1121. new AnimClass(ANIM_FIRE_SMALL, Coord_Scatter(Cell_Coord(cell), 0x0080), Random_Pick(0, 7), Random_Pick(1, 3));
  1122. if (Percent_Chance(50)) {
  1123. new AnimClass(ANIM_FIRE_MED, Coord_Scatter(Cell_Coord(cell), 0x0040), Random_Pick(0, 7), Random_Pick(1, 3));
  1124. }
  1125. }
  1126. new AnimClass(ANIM_FBALL1, Coord_Scatter(Cell_Coord(cell), 0x0040), Random_Pick(0, 3));
  1127. }
  1128. shakes = Class->Cost_Of() / 400;
  1129. if (shakes) {
  1130. Shake_The_Screen(shakes);
  1131. }
  1132. Sound_Effect(VOC_CRUMBLE, Coord);
  1133. if (Mission == MISSION_DECONSTRUCTION) {
  1134. CountDown = 0;
  1135. Set_Rate(0);
  1136. } else {
  1137. CountDown = 8;
  1138. }
  1139. /*
  1140. ** If it is in radio contact and the object seems to be attached, then tell
  1141. ** it to run away.
  1142. */
  1143. if (In_Radio_Contact() && Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER) {
  1144. Transmit_Message(RADIO_RUN_AWAY);
  1145. }
  1146. /*
  1147. ** A force destruction will not generate survivors.
  1148. */
  1149. if (forced || *this == STRUCT_KENNEL) {
  1150. IsSurvivorless = true;
  1151. }
  1152. /*
  1153. ** Destruction of a radar facility or advanced communications
  1154. ** center will cause the spiedby field to change...
  1155. */
  1156. if (SpiedBy) {
  1157. SpiedBy = 0;
  1158. StructType struc = *this;
  1159. if (struc == STRUCT_RADAR /* || struc == STRUCT_EYE */) {
  1160. Update_Radar_Spied();
  1161. }
  1162. }
  1163. /*
  1164. ** Destruction of a gap generator will cause the cells it affects
  1165. ** to stop being jammed.
  1166. */
  1167. if (*this == STRUCT_GAP) {
  1168. Remove_Gap_Effect();
  1169. }
  1170. /*
  1171. ** Destruction of a shipyard or sub pen may cause attached ships
  1172. ** who are repairing themselves to discontinue repairs.
  1173. */
  1174. if (*this == STRUCT_SHIP_YARD || *this == STRUCT_SUB_PEN) {
  1175. for (int index = 0; index < Vessels.Count(); index++) {
  1176. VesselClass *obj = Vessels.Ptr(index);
  1177. if (obj && !obj->IsInLimbo && obj->House == House) {
  1178. if (obj->IsSelfRepairing) {
  1179. if (::Distance(Center_Coord(), obj->Center_Coord()) < 0x0200) {
  1180. obj->IsSelfRepairing = false;
  1181. obj->IsToSelfRepair = false;
  1182. }
  1183. }
  1184. }
  1185. }
  1186. }
  1187. /*
  1188. ** Destruction of a barrel will cause the surrounding squares to
  1189. ** be hit with damage.
  1190. */
  1191. if (*this == STRUCT_BARREL || *this == STRUCT_BARREL3) {
  1192. COORDINATE center = Center_Coord();
  1193. CELL cellcenter = Coord_Cell(center);
  1194. BulletClass * bullet;
  1195. bullet = new BulletClass(BULLET_INVISIBLE, ::As_Target(Adjacent_Cell(cellcenter, FACING_N)), 0, 200, WARHEAD_FIRE, MPH_MEDIUM_FAST);
  1196. if (bullet) {
  1197. bullet->Unlimbo(center, DIR_N);
  1198. }
  1199. bullet = new BulletClass(BULLET_INVISIBLE, ::As_Target(Adjacent_Cell(cellcenter, FACING_E)), 0, 200, WARHEAD_FIRE, MPH_MEDIUM_FAST);
  1200. if (bullet) {
  1201. bullet->Unlimbo(center, DIR_E);
  1202. }
  1203. bullet = new BulletClass(BULLET_INVISIBLE, ::As_Target(Adjacent_Cell(cellcenter, FACING_S)), 0, 200, WARHEAD_FIRE, MPH_MEDIUM_FAST);
  1204. if (bullet) {
  1205. bullet->Unlimbo(center, DIR_S);
  1206. }
  1207. bullet = new BulletClass(BULLET_INVISIBLE, ::As_Target(Adjacent_Cell(cellcenter, FACING_W)), 0, 200, WARHEAD_FIRE, MPH_MEDIUM_FAST);
  1208. if (bullet) {
  1209. bullet->Unlimbo(center, DIR_W);
  1210. }
  1211. }
  1212. break;
  1213. case RESULT_HALF:
  1214. if (*this == STRUCT_PUMP) {
  1215. AnimClass * anim = new AnimClass(ANIM_OILFIELD_BURN, Coord_Add(Coord, 0x00400130L), 1);
  1216. if (anim) {
  1217. anim->Attach_To(this);
  1218. }
  1219. }
  1220. // Fall into next case.
  1221. case RESULT_MAJOR:
  1222. Sound_Effect(VOC_KABOOM1, Coord);
  1223. while (*offset != REFRESH_EOL) {
  1224. CELL cell = Coord_Cell(Coord) + *offset++;
  1225. AnimClass * anim = NULL;
  1226. /*
  1227. ** Show pieces of fire to indicate that a significant change in
  1228. ** damage level has occurred.
  1229. */
  1230. if (warhead == WARHEAD_FIRE) {
  1231. switch (Random_Pick(0, 5+Class->Width()+Class->Height())) {
  1232. case 0:
  1233. break;
  1234. case 1:
  1235. case 2:
  1236. case 3:
  1237. case 4:
  1238. case 5:
  1239. anim = new AnimClass(ANIM_ON_FIRE_SMALL, Coord_Scatter(Cell_Coord(cell), 0x0060), 0, Random_Pick(1, 3));
  1240. break;
  1241. case 6:
  1242. case 7:
  1243. case 8:
  1244. anim = new AnimClass(ANIM_ON_FIRE_MED, Coord_Scatter(Cell_Coord(cell), 0x0060), 0, Random_Pick(1, 3));
  1245. break;
  1246. case 9:
  1247. anim = new AnimClass(ANIM_ON_FIRE_BIG, Coord_Scatter(Cell_Coord(cell), 0x0060), 0, 1);
  1248. break;
  1249. default:
  1250. break;
  1251. }
  1252. } else {
  1253. if (Percent_Chance(50)) {
  1254. /*
  1255. ** Building may catch on fire, but only if it wasn't a
  1256. ** renovator that caused the damage.
  1257. */
  1258. if (source == NULL || source->What_Am_I() != RTTI_INFANTRY || *(InfantryClass *)source != INFANTRY_RENOVATOR) {
  1259. anim = new AnimClass(ANIM_FIRE_SMALL, Coord_Scatter(Cell_Coord(cell), 0x0060), Random_Pick(0, 7), Random_Pick(1, 3));
  1260. }
  1261. }
  1262. }
  1263. /*
  1264. ** If the animation was created, then attach it to the building.
  1265. */
  1266. if (anim) {
  1267. anim->Attach_To(this);
  1268. }
  1269. }
  1270. break;
  1271. case RESULT_NONE:
  1272. break;
  1273. case RESULT_LIGHT:
  1274. break;
  1275. }
  1276. if (source && res != RESULT_NONE) {
  1277. /*
  1278. ** If any damage occurred, then inform the house of this fact. If it is the player's
  1279. ** house, it might announce this fact.
  1280. */
  1281. if (!Class->IsInsignificant) {
  1282. House->Attacked();
  1283. }
  1284. /*
  1285. ** Save the type of the house that's doing the damage, so if the building burns
  1286. ** to death credit can still be given for the kill
  1287. */
  1288. WhoLastHurtMe = source->Owner();
  1289. /*
  1290. ** When certain buildings are hit, they "snap out of it" and
  1291. ** return fire if they are able and allowed.
  1292. */
  1293. if (*this != STRUCT_SAM && *this != STRUCT_AAGUN &&
  1294. !House->Is_Ally(source) &&
  1295. Class->PrimaryWeapon != NULL &&
  1296. (!Target_Legal(TarCom) || !In_Range(TarCom))) {
  1297. if (source->What_Am_I() != RTTI_AIRCRAFT && (!House->IsHuman || Rule.IsSmartDefense)) {
  1298. Assign_Target(source->As_Target());
  1299. } else {
  1300. /*
  1301. ** Generate a random rotation effect since there is nothing else that this
  1302. ** building can do.
  1303. */
  1304. if (!PrimaryFacing.Is_Rotating()) {
  1305. PrimaryFacing.Set_Desired(Random_Pick(DIR_N, DIR_MAX));
  1306. }
  1307. }
  1308. }
  1309. }
  1310. }
  1311. return(res);
  1312. }
  1313. /***********************************************************************************************
  1314. * BuildingClass::new -- Allocates a building object from building pool. *
  1315. * *
  1316. * This routine will allocate a building slot from the building alloc *
  1317. * system. *
  1318. * *
  1319. * INPUT: none *
  1320. * *
  1321. * OUTPUT: Returns with a pointer to the allocated building. If NULL is *
  1322. * returned, then this indicates a failure to allocate. *
  1323. * *
  1324. * WARNINGS: none *
  1325. * *
  1326. * HISTORY: *
  1327. * 04/11/1994 JLB : Created. *
  1328. * 04/21/1994 JLB : Converted to operator new. *
  1329. * 05/17/1994 JLB : Revamped allocation scheme *
  1330. * 07/29/1994 JLB : Simplified. *
  1331. *=============================================================================================*/
  1332. void * BuildingClass::operator new(size_t )
  1333. {
  1334. void * ptr = Buildings.Allocate();
  1335. if (ptr) {
  1336. ((BuildingClass *)ptr)->IsActive = true;
  1337. }
  1338. return(ptr);
  1339. }
  1340. /***********************************************************************************************
  1341. * BuildingClass::delete -- Deallocates building object. *
  1342. * *
  1343. * This is the memory deallocation operation for a building object. *
  1344. * Since buildings are allocated out of a fixed memory block, all that *
  1345. * is needed is to flag the unit as inactive. *
  1346. * *
  1347. * INPUT: ptr -- Pointer to building to deallocate. *
  1348. * *
  1349. * OUTPUT: none *
  1350. * *
  1351. * WARNINGS: none *
  1352. * *
  1353. * HISTORY: *
  1354. * 04/21/1994 JLB : Created. *
  1355. *=============================================================================================*/
  1356. void BuildingClass::operator delete(void *ptr)
  1357. {
  1358. if (ptr) {
  1359. ((BuildingClass *)ptr)->IsActive = false;
  1360. }
  1361. Buildings.Free((BuildingClass *)ptr);
  1362. }
  1363. /***********************************************************************************************
  1364. * BuildingClass::BuildingClass -- Constructor for buildings. *
  1365. * *
  1366. * This routine inserts a building into the object tracking system. *
  1367. * It is placed into a limbo state unless a location is provided for *
  1368. * it to unlimbo at. *
  1369. * *
  1370. * INPUT: type -- The structure type to make this object. *
  1371. * *
  1372. * house -- The owner of this building. *
  1373. * *
  1374. * pos -- The position to unlimbo the building. If -1 is *
  1375. * specified, then the building remains in a limbo *
  1376. * state. *
  1377. * *
  1378. * OUTPUT: none *
  1379. * *
  1380. * WARNINGS: none *
  1381. * *
  1382. * HISTORY: *
  1383. * 04/21/1994 JLB : Created. *
  1384. * 08/07/1995 JLB : Fixed act like value to match expected value. *
  1385. *=============================================================================================*/
  1386. BuildingClass::BuildingClass(StructType type, HousesType house) :
  1387. TechnoClass(RTTI_BUILDING, Buildings.ID(this), house),
  1388. Class(BuildingTypes.Ptr((int)type)),
  1389. Factory(0),
  1390. ActLike(House->ActLike),
  1391. IsToRebuild(false),
  1392. IsToRepair(false),
  1393. IsAllowedToSell(true),
  1394. IsReadyToCommence(false),
  1395. IsRepairing(false),
  1396. IsWrenchVisible(false),
  1397. IsGoingToBlow(false),
  1398. IsSurvivorless(false),
  1399. IsCharging(false),
  1400. IsCharged(false),
  1401. IsCaptured(false),
  1402. IsJamming(false),
  1403. IsJammed(false),
  1404. HasFired(false),
  1405. HasOpened(false),
  1406. CountDown(0),
  1407. BState(BSTATE_NONE),
  1408. QueueBState(BSTATE_NONE),
  1409. WhoLastHurtMe(house),
  1410. WhomToRepay(TARGET_NONE),
  1411. AnimToTrack(TARGET_NONE),
  1412. LastStrength(0),
  1413. PlacementDelay(0)
  1414. {
  1415. House->Tracking_Add(this);
  1416. IsSecondShot = !Class->Is_Two_Shooter();
  1417. Strength = Class->MaxStrength;
  1418. Ammo = Class->MaxAmmo;
  1419. /*
  1420. ** If the building could never be built, then it can never be sold either. This
  1421. ** is due to the lack of buildup animation.
  1422. */
  1423. if (Class->Get_Buildup_Data() != NULL) {
  1424. // if (!Class->IsBuildable) {
  1425. IsAllowedToSell = false;
  1426. }
  1427. // if (Session.Type == GAME_INTERNET) {
  1428. // House->BuildingTotals->Increment_Unit_Total( (int) type);
  1429. // }
  1430. }
  1431. /***********************************************************************************************
  1432. * BuildingClass::~BuildingClass -- Destructor for building type objects. *
  1433. * *
  1434. * This destructor for building objects will put the building in limbo if possible. *
  1435. * *
  1436. * INPUT: none *
  1437. * *
  1438. * OUTPUT: none *
  1439. * *
  1440. * WARNINGS: none *
  1441. * *
  1442. * HISTORY: *
  1443. * 01/18/1995 JLB : Created. *
  1444. *=============================================================================================*/
  1445. BuildingClass::~BuildingClass(void)
  1446. {
  1447. if (GameActive && Class) {
  1448. if (House) {
  1449. House->Tracking_Remove(this);
  1450. }
  1451. BuildingClass::Limbo();
  1452. }
  1453. Class = 0;
  1454. delete (FactoryClass *)Factory;
  1455. Factory = 0;
  1456. ID = -1;
  1457. }
  1458. /***********************************************************************************************
  1459. * BuildingClass::Drop_Debris -- Drops rubble when building is destroyed. *
  1460. * *
  1461. * This routine is called when a building is destroyed. It handles *
  1462. * placing the rubble down. *
  1463. * *
  1464. * INPUT: none *
  1465. * *
  1466. * OUTPUT: none *
  1467. * *
  1468. * WARNINGS: none *
  1469. * *
  1470. * HISTORY: *
  1471. * 05/14/1994 JLB : Created. *
  1472. * 06/13/1995 JLB : Added smoke and normal infantry survivor possibility. *
  1473. * 07/16/1995 JLB : Survival rate depends on if captured or sabotaged. *
  1474. *=============================================================================================*/
  1475. void BuildingClass::Drop_Debris(TARGET source)
  1476. {
  1477. assert(Buildings.ID(this) == ID);
  1478. assert(IsActive);
  1479. CELL const * offset;
  1480. CELL cell;
  1481. /*
  1482. ** Generate random survivors from the destroyed building.
  1483. */
  1484. cell = Coord_Cell(Coord);
  1485. offset = Occupy_List();
  1486. int odds = 2;
  1487. if (Target_Legal(WhomToRepay)) odds -= 1;
  1488. if (IsCaptured) odds += 6;
  1489. int count = How_Many_Survivors();
  1490. while (*offset != REFRESH_EOL) {
  1491. CELL newcell;
  1492. newcell = cell + *offset++;
  1493. CellClass const * cellptr = &Map[newcell];
  1494. /*
  1495. ** Infantry could run out of a destroyed building.
  1496. */
  1497. if (!House->IsToDie && count > 0) {
  1498. InfantryClass * i = NULL;
  1499. if (Random_Pick(0, odds) == 1) {
  1500. i = NULL;
  1501. InfantryType typ = Crew_Type();
  1502. if (typ != INFANTRY_NONE) i = new InfantryClass(typ, House->Class->House);
  1503. if (i != NULL) {
  1504. if (Class->Get_Buildup_Data() != NULL && i->Class->IsNominal) i->IsTechnician = true;
  1505. ScenarioInit++;
  1506. if (i->Unlimbo(Cell_Coord(newcell), DIR_N)) {
  1507. count--;
  1508. i->Strength = Random_Pick(5, (int)i->Class->MaxStrength);
  1509. i->Scatter(0, true);
  1510. if (source != TARGET_NONE && !House->Is_Ally(As_Object(source))) {
  1511. i->Assign_Mission(MISSION_ATTACK);
  1512. i->Assign_Target(source);
  1513. } else {
  1514. if (House->IsHuman) {
  1515. i->Assign_Mission(MISSION_GUARD);
  1516. } else {
  1517. i->Assign_Mission(MISSION_HUNT);
  1518. }
  1519. }
  1520. } else {
  1521. delete i;
  1522. }
  1523. ScenarioInit--;
  1524. }
  1525. }
  1526. }
  1527. /*
  1528. ** Smoke and fire only appear on terrestrail cells. They should not appear on
  1529. ** rivers, clifs, or water cells.
  1530. */
  1531. if (cellptr->Is_Clear_To_Move(SPEED_TRACK, true, true)) {
  1532. /*
  1533. ** Possibly add some smoke rising from the ashes of the building.
  1534. */
  1535. switch (Random_Pick(0, 5)) {
  1536. case 0:
  1537. case 1:
  1538. case 2:
  1539. new AnimClass(ANIM_SMOKE_M, Coord_Scatter(Cell_Coord(newcell), 0x0050, false), Random_Pick(0, 5), Random_Pick(1, 2));
  1540. break;
  1541. default:
  1542. break;
  1543. }
  1544. /*
  1545. ** The building always scars the ground in some fashion.
  1546. */
  1547. if (Percent_Chance(25)) {
  1548. new SmudgeClass(Random_Pick(SMUDGE_SCORCH1, SMUDGE_SCORCH6), Cell_Coord(newcell));
  1549. } else {
  1550. new SmudgeClass(Random_Pick(SMUDGE_CRATER1, SMUDGE_CRATER6), Coord_Scatter(Cell_Coord(newcell), 0x0080, false));
  1551. }
  1552. }
  1553. }
  1554. }
  1555. /***********************************************************************************************
  1556. * BuildingClass::Active_Click_With -- Handles clicking on the map while the building is selected.*
  1557. * *
  1558. * This interface routine handles when the player clicks on the map while this building *
  1559. * is currently selected. This is used to assign an override target to a turret or *
  1560. * guard tower. *
  1561. * *
  1562. * INPUT: target -- The target that was clicked upon. *
  1563. * *
  1564. * OUTPUT: none *
  1565. * *
  1566. * WARNINGS: none *
  1567. * *
  1568. * HISTORY: *
  1569. * 05/28/1994 JLB : Created. *
  1570. *=============================================================================================*/
  1571. void BuildingClass::Active_Click_With(ActionType action, ObjectClass * object)
  1572. {
  1573. assert(Buildings.ID(this) == ID);
  1574. assert(IsActive);
  1575. if (action == ACTION_ATTACK && object != NULL) {
  1576. Player_Assign_Mission(MISSION_ATTACK, object->As_Target());
  1577. }
  1578. if (action == ACTION_SELF && Class->Is_Factory()) {
  1579. OutList.Add(EventClass(EventClass::PRIMARY, TargetClass(this)));
  1580. }
  1581. }
  1582. /***********************************************************************************************
  1583. * BuildingClass::Active_Click_With -- Handles cell selection for buildings. *
  1584. * *
  1585. * This routine really only serves one purpose -- to allow targeting of the ground for *
  1586. * buildings that are equipped with weapons. *
  1587. * *
  1588. * INPUT: action -- The requested action to perform. *
  1589. * *
  1590. * cell -- The cell location to perform the action upon. *
  1591. * *
  1592. * OUTPUT: none *
  1593. * *
  1594. * WARNINGS: none *
  1595. * *
  1596. * HISTORY: *
  1597. * 07/04/1995 JLB : Created. *
  1598. * 10/04/1995 JLB : Handles construction yard undeploy to move logic. *
  1599. *=============================================================================================*/
  1600. void BuildingClass::Active_Click_With(ActionType action, CELL cell)
  1601. {
  1602. assert(Buildings.ID(this) == ID);
  1603. assert(IsActive);
  1604. if (action == ACTION_ATTACK) {
  1605. Player_Assign_Mission(MISSION_ATTACK, ::As_Target(cell));
  1606. }
  1607. if (action == ACTION_MOVE && *this == STRUCT_CONST) {
  1608. OutList.Add(EventClass(EventClass::ARCHIVE, TargetClass(this), TargetClass(cell)));
  1609. OutList.Add(EventClass(EventClass::SELL, TargetClass(this)));
  1610. }
  1611. }
  1612. /***********************************************************************************************
  1613. * BuildingClass::Assign_Target -- Assigns a target to the building. *
  1614. * *
  1615. * Assigning of a target to a building makes sense if the building is one that can attack. *
  1616. * This routine would be used to assign the attack target to a turret or guard tower. *
  1617. * *
  1618. * INPUT: target -- The target that was clicked on while this building was selected. *
  1619. * *
  1620. * OUTPUT: none *
  1621. * *
  1622. * WARNINGS: none *
  1623. * *
  1624. * HISTORY: *
  1625. * 05/28/1994 JLB : Created. *
  1626. * 11/02/1994 JLB : Checks for range before assigning target. *
  1627. *=============================================================================================*/
  1628. void BuildingClass::Assign_Target(TARGET target)
  1629. {
  1630. assert(Buildings.ID(this) == ID);
  1631. assert(IsActive);
  1632. if (*this != STRUCT_SAM && *this != STRUCT_AAGUN && !In_Range(target, 0)) {
  1633. target = TARGET_NONE;
  1634. }
  1635. TechnoClass::Assign_Target(target);
  1636. }
  1637. /***********************************************************************************************
  1638. * BuildingClass::Init -- Initialize the building system to an empty null state. *
  1639. * *
  1640. * This routine initializes the building system in preparation for a scenario load. *
  1641. * *
  1642. * INPUT: none *
  1643. * *
  1644. * OUTPUT: none *
  1645. * *
  1646. * WARNINGS: none *
  1647. * *
  1648. * HISTORY: *
  1649. * 09/19/1994 JLB : Created. *
  1650. *=============================================================================================*/
  1651. void BuildingClass::Init(void)
  1652. {
  1653. Buildings.Free_All();
  1654. }
  1655. /***********************************************************************************************
  1656. * BuildingClass::Exit_Object -- Initiates an object to leave the building. *
  1657. * *
  1658. * This function is used to cause an object to exit the building. It is called when a *
  1659. * factory produces a vehicle or other mobile object and that object needs to exit the *
  1660. * building to join the ranks of a regular unit. Typically, the object is placed down on *
  1661. * the map such that it overlaps the building and then it is given a movement order so that *
  1662. * it will move to an adjacent free cell. *
  1663. * *
  1664. * INPUT: base -- Pointer to the object that is to exit the building. *
  1665. * *
  1666. * OUTPUT: Returns the success rating for the exit attempt; *
  1667. * 0 = complete failure (refund money please) *
  1668. * 1 = temporarily prevented (try again later please) *
  1669. * 2 = successful *
  1670. * *
  1671. * WARNINGS: The building is placed in radio contact with the object. The object is in a *
  1672. * tethered condition. This condition will be automatically broken when the *
  1673. * object reaches the adjacent square. *
  1674. * *
  1675. * HISTORY: *
  1676. * 11/28/1994 JLB : Created. *
  1677. * 04/10/1995 JLB : Handles building production by computer. *
  1678. * 06/17/1995 JLB : Handles refinery exit. *
  1679. *=============================================================================================*/
  1680. int BuildingClass::Exit_Object(TechnoClass * base)
  1681. {
  1682. assert(Buildings.ID(this) == ID);
  1683. assert(IsActive);
  1684. if (!base) return(0);
  1685. TechnoTypeClass const * ttype = (TechnoTypeClass const *)&base->Class_Of();
  1686. /*
  1687. ** A unit exiting a building is always considered to be "locked". That means, it
  1688. ** will be considered as to have legally entered the visible map domain.
  1689. */
  1690. base->IsLocked = true;
  1691. /*
  1692. ** Find a good cell to unload the object to. The object, probably a vehicle
  1693. ** will drive/walk to the adjacent free cell.
  1694. */
  1695. CELL cell = 0;
  1696. switch (base->What_Am_I()) {
  1697. case RTTI_AIRCRAFT:
  1698. if (!In_Radio_Contact()) {
  1699. AircraftClass * air = (AircraftClass *)base;
  1700. air->Height = 0;
  1701. ScenarioInit++;
  1702. if (air->Unlimbo(Docking_Coord(), air->Pose_Dir())) {
  1703. Transmit_Message(RADIO_HELLO, air);
  1704. Transmit_Message(RADIO_TETHER);
  1705. ScenarioInit--;
  1706. return(2);
  1707. }
  1708. ScenarioInit--;
  1709. } else {
  1710. AircraftClass * air = (AircraftClass *)base;
  1711. if (Cell_X(Coord_Cell(Center_Coord())) - Map.MapCellX < Map.MapCellWidth/2) {
  1712. cell = XY_Cell(Map.MapCellX-1, Random_Pick(0, Map.MapCellHeight-1)+Map.MapCellY);
  1713. } else {
  1714. cell = XY_Cell(Map.MapCellX+Map.MapCellWidth, Random_Pick(0, Map.MapCellHeight-1)+Map.MapCellY);
  1715. }
  1716. ScenarioInit++;
  1717. if (air->Unlimbo(Cell_Coord(cell), DIR_N)) {
  1718. //BG air->Assign_Destination(::As_Target(Nearby_Location(air)));
  1719. /*BG*/ air->Assign_Destination(::As_Target(air->Nearby_Location(this)));
  1720. air->Assign_Mission(MISSION_MOVE);
  1721. ScenarioInit--;
  1722. return(2);
  1723. }
  1724. ScenarioInit--;
  1725. }
  1726. break;
  1727. case RTTI_VESSEL:
  1728. switch (Class->Type) {
  1729. case STRUCT_SUB_PEN:
  1730. case STRUCT_SHIP_YARD:
  1731. ScenarioInit++;
  1732. cell = Find_Exit_Cell(base);
  1733. if (cell != 0 && base->Unlimbo(Cell_Coord(cell), Direction(Cell_Coord(cell)))) {
  1734. base->Assign_Mission(MISSION_GUARD);
  1735. ScenarioInit--;
  1736. return(2);
  1737. }
  1738. ScenarioInit--;
  1739. break;
  1740. default:
  1741. break;
  1742. }
  1743. break;
  1744. case RTTI_INFANTRY:
  1745. case RTTI_UNIT:
  1746. switch (Class->Type) {
  1747. case STRUCT_REFINERY:
  1748. if (base->What_Am_I() == RTTI_UNIT) {
  1749. cell = Coord_Cell(Center_Coord());
  1750. UnitClass * unit = (UnitClass *)base;
  1751. cell = Adjacent_Cell(cell, FACING_SW);
  1752. ScenarioInit++;
  1753. if (unit->Unlimbo(Cell_Coord(Adjacent_Cell(cell, DIR_S)), DIR_SW_X2)) {
  1754. unit->PrimaryFacing = DIR_S;
  1755. unit->Assign_Mission(MISSION_HARVEST);
  1756. }
  1757. ScenarioInit--;
  1758. } else {
  1759. base->Scatter(0, true);
  1760. }
  1761. break;
  1762. case STRUCT_WEAP:
  1763. if (Mission == MISSION_UNLOAD) {
  1764. for(int index = 0; index < Buildings.Count(); index++) {
  1765. BuildingClass *bldg = Buildings.Ptr(index);
  1766. if (bldg->Owner() == Owner() && *bldg == STRUCT_WEAP && bldg != this && bldg->Mission == MISSION_GUARD && !bldg->Factory) {
  1767. FactoryClass * temp = Factory;
  1768. bldg->Factory = Factory;
  1769. Factory = 0;
  1770. int retval = (bldg->Exit_Object(base));
  1771. bldg->Factory = 0;
  1772. Factory = temp;
  1773. return(retval);
  1774. }
  1775. }
  1776. return(1); // fail while we're still unloading previous
  1777. }
  1778. ScenarioInit++;
  1779. if (base->Unlimbo(Exit_Coord(), DIR_S)) {
  1780. base->Mark(MARK_UP);
  1781. base->Coord = Exit_Coord();
  1782. base->Mark(MARK_DOWN);
  1783. Transmit_Message(RADIO_HELLO, base);
  1784. Transmit_Message(RADIO_TETHER);
  1785. Assign_Mission(MISSION_UNLOAD);
  1786. ScenarioInit--;
  1787. return(2);
  1788. }
  1789. ScenarioInit--;
  1790. break;
  1791. case STRUCT_BARRACKS:
  1792. case STRUCT_TENT:
  1793. case STRUCT_KENNEL:
  1794. cell = Find_Exit_Cell(base);
  1795. if (cell != 0) {
  1796. DirType dir = Direction(cell);
  1797. COORDINATE start = Exit_Coord();
  1798. ScenarioInit++;
  1799. if (base->Unlimbo(start, dir)) {
  1800. base->Assign_Mission(MISSION_MOVE);
  1801. /*
  1802. ** When disembarking from a transport then guard an area around the
  1803. ** center of the base.
  1804. */
  1805. base->Assign_Destination(::As_Target(cell));
  1806. if (House->IQ >= Rule.IQGuardArea) {
  1807. base->Assign_Mission(MISSION_GUARD_AREA);
  1808. base->ArchiveTarget = ::As_Target(House->Where_To_Go((FootClass *)base));
  1809. }
  1810. /*
  1811. ** Establish radio contact so unload coordination can occur. This
  1812. ** radio contact should always succeed.
  1813. */
  1814. if (Transmit_Message(RADIO_HELLO, base) == RADIO_ROGER) {
  1815. Transmit_Message(RADIO_UNLOAD);
  1816. }
  1817. ScenarioInit--;
  1818. return(2);
  1819. }
  1820. ScenarioInit--;
  1821. }
  1822. break;
  1823. default:
  1824. cell = Find_Exit_Cell(base);
  1825. if (cell != 0) {
  1826. DirType dir = Direction(cell);
  1827. COORDINATE start = Exit_Coord();
  1828. ScenarioInit++;
  1829. if (base->Unlimbo(start, dir)) {
  1830. base->Assign_Mission(MISSION_MOVE);
  1831. /*
  1832. ** When disembarking from a transport then guard an area around the
  1833. ** center of the base.
  1834. */
  1835. base->Assign_Destination(::As_Target(cell));
  1836. if (House->IQ >= Rule.IQGuardArea) {
  1837. base->Assign_Mission(MISSION_GUARD_AREA);
  1838. base->ArchiveTarget = ::As_Target(House->Where_To_Go((FootClass *)base));
  1839. }
  1840. ScenarioInit--;
  1841. return(2);
  1842. }
  1843. ScenarioInit--;
  1844. }
  1845. break;
  1846. }
  1847. break;
  1848. case RTTI_BUILDING:
  1849. if (!House->IsHuman) {
  1850. /*
  1851. ** Find the next available spot to place this newly created building. If the
  1852. ** building could be placed at the desired location, fine. If not, then this
  1853. ** routine will return failure. The calling routine will probably abandon this
  1854. ** building in preference to building another.
  1855. */
  1856. BaseNodeClass * node = Base.Next_Buildable(((BuildingClass *)base)->Class->Type);
  1857. COORDINATE coord = 0;
  1858. if (node) {
  1859. coord = Cell_Coord(node->Cell);
  1860. } else {
  1861. /*
  1862. ** Find a suitable new spot to place.
  1863. */
  1864. coord = House->Find_Build_Location((BuildingClass *)base);
  1865. }
  1866. if (coord) {
  1867. if (Flush_For_Placement(base, Coord_Cell(coord))) {
  1868. return(1);
  1869. }
  1870. if (base->Unlimbo(coord)) {
  1871. if (node && ((BuildingClass *)base)->Class->Type == House->BuildStructure) {
  1872. House->BuildStructure = STRUCT_NONE;
  1873. }
  1874. return(2);
  1875. }
  1876. }
  1877. }
  1878. break;
  1879. default:
  1880. break;
  1881. }
  1882. /*
  1883. ** Failure to exit the object results in a false return value.
  1884. */
  1885. return(0);
  1886. }
  1887. /***********************************************************************************************
  1888. * BuildingClass::Update_Buildables -- Informs sidebar of additional construction options. *
  1889. * *
  1890. * This routine will tell the sidebar of objects that can be built. The function is called *
  1891. * whenever a building matures. *
  1892. * *
  1893. * INPUT: none *
  1894. * *
  1895. * OUTPUT: none *
  1896. * *
  1897. * WARNINGS: none *
  1898. * *
  1899. * HISTORY: *
  1900. * 11/11/1994 JLB : Created. *
  1901. * 12/23/1994 JLB : Only updates for PLAYER buildings. *
  1902. *=============================================================================================*/
  1903. void BuildingClass::Update_Buildables(void)
  1904. {
  1905. assert(Buildings.ID(this) == ID);
  1906. assert(IsActive);
  1907. if (House == PlayerPtr && !IsInLimbo && IsDiscoveredByPlayer) {
  1908. switch (Class->ToBuild) {
  1909. StructType i;
  1910. UnitType u;
  1911. InfantryType f;
  1912. AircraftType a;
  1913. VesselType v;
  1914. case RTTI_VESSELTYPE:
  1915. for (v = VESSEL_FIRST; v < VESSEL_COUNT; v++) {
  1916. if (PlayerPtr->Can_Build(&VesselTypeClass::As_Reference(v), ActLike)) {
  1917. Map.Add(RTTI_VESSELTYPE, v);
  1918. }
  1919. }
  1920. break;
  1921. case RTTI_BUILDINGTYPE:
  1922. for (i = STRUCT_FIRST; i < STRUCT_COUNT; i++) {
  1923. if (PlayerPtr->Can_Build(&BuildingTypeClass::As_Reference(i), ActLike)) {
  1924. Map.Add(RTTI_BUILDINGTYPE, i);
  1925. }
  1926. }
  1927. break;
  1928. case RTTI_UNITTYPE:
  1929. for (u = UNIT_FIRST; u < UNIT_COUNT; u++) {
  1930. if (PlayerPtr->Can_Build(&UnitTypeClass::As_Reference(u), ActLike)) {
  1931. Map.Add(RTTI_UNITTYPE, u);
  1932. }
  1933. }
  1934. break;
  1935. case RTTI_INFANTRYTYPE:
  1936. for (f = INFANTRY_FIRST; f < INFANTRY_COUNT; f++) {
  1937. if (PlayerPtr->Can_Build(&InfantryTypeClass::As_Reference(f), ActLike)) {
  1938. if (InfantryTypeClass::As_Reference(f).IsDog) {
  1939. if (*this == STRUCT_KENNEL) {
  1940. Map.Add(RTTI_INFANTRYTYPE, f);
  1941. }
  1942. } else {
  1943. if (*this != STRUCT_KENNEL) {
  1944. Map.Add(RTTI_INFANTRYTYPE, f);
  1945. }
  1946. }
  1947. }
  1948. }
  1949. break;
  1950. case RTTI_AIRCRAFTTYPE:
  1951. for (a = AIRCRAFT_FIRST; a < AIRCRAFT_COUNT; a++) {
  1952. if (PlayerPtr->Can_Build(&AircraftTypeClass::As_Reference(a), ActLike)) {
  1953. Map.Add(RTTI_AIRCRAFTTYPE, a);
  1954. }
  1955. }
  1956. break;
  1957. default:
  1958. break;
  1959. }
  1960. }
  1961. }
  1962. /***********************************************************************************************
  1963. * BuildingClass::Fire_Out -- Handles when attached animation expires. *
  1964. * *
  1965. * This routine is used to perform any fixups necessary when the attached animation has *
  1966. * terminated. This occurs when the fire & smoke animation that a SAM site produces stops. *
  1967. * At that point, normal reload procedures can commence. *
  1968. * *
  1969. * INPUT: none *
  1970. * *
  1971. * OUTPUT: none *
  1972. * *
  1973. * WARNINGS: none *
  1974. * *
  1975. * HISTORY: *
  1976. * 11/30/1994 JLB : Created. *
  1977. *=============================================================================================*/
  1978. void BuildingClass::Fire_Out(void)
  1979. {
  1980. assert(Buildings.ID(this) == ID);
  1981. assert(IsActive);
  1982. }
  1983. /***********************************************************************************************
  1984. * BuildingClass::Limbo -- Handles power adjustment as building goes into limbo. *
  1985. * *
  1986. * This routine will handle the power adjustments for the associated house when the *
  1987. * building goes into limbo. This means that its power drain or production is subtracted *
  1988. * from the house accumulated totals. *
  1989. * *
  1990. * INPUT: none *
  1991. * *
  1992. * OUTPUT: bool; Was the building limboed? *
  1993. * *
  1994. * WARNINGS: none *
  1995. * *
  1996. * HISTORY: *
  1997. * 12/24/1994 JLB : Created. *
  1998. *=============================================================================================*/
  1999. bool BuildingClass::Limbo(void)
  2000. {
  2001. assert(Buildings.ID(this) == ID);
  2002. assert(IsActive);
  2003. if (!IsInLimbo) {
  2004. /*
  2005. ** Update the total factory type, assuming this building has a factory.
  2006. */
  2007. House->Active_Remove(this);
  2008. House->IsRecalcNeeded = true;
  2009. House->Recalc_Center();
  2010. /*
  2011. ** Update the power status of the owner's house.
  2012. */
  2013. House->Adjust_Power(-Power_Output());
  2014. House->Adjust_Drain(-Class->Drain);
  2015. House->Adjust_Capacity(-Class->Capacity, true);
  2016. if (House == PlayerPtr) {
  2017. Map.PowerClass::IsToRedraw = true;
  2018. Map.Flag_To_Redraw(false);
  2019. }
  2020. /*
  2021. ** This could be a building that builds. If so, then the sidebar may need adjustment.
  2022. ** Set IsInLimbo to true to "fool" the sidebar into knowing that this building
  2023. ** isn't available. Set it back to false so the rest of the Limbo code works.
  2024. ** Otherwise, the sidebar won't properly remove non-available buildables.
  2025. */
  2026. // if (IsOwnedByPlayer && !ScenarioInit) {
  2027. // IsInLimbo = true;
  2028. // Map.Recalc();
  2029. // IsInLimbo = false;
  2030. // }
  2031. }
  2032. return(TechnoClass::Limbo());
  2033. }
  2034. /***********************************************************************************************
  2035. * BuildingClass::Turret_Facing -- Fetches the turret facing for this building. *
  2036. * *
  2037. * This will return the turret facing for this building. Some buildings don't have a *
  2038. * visual turret (e.g., pillbox) so they return a turret facing that always faces their *
  2039. * current target. *
  2040. * *
  2041. * INPUT: none *
  2042. * *
  2043. * OUTPUT: Returns with the current facing of the turret. *
  2044. * *
  2045. * WARNINGS: none *
  2046. * *
  2047. * HISTORY: *
  2048. * 07/29/1996 JLB : Created. *
  2049. *=============================================================================================*/
  2050. DirType BuildingClass::Turret_Facing(void) const
  2051. {
  2052. if (!Class->IsTurretEquipped && Target_Legal(TarCom)) {
  2053. return(::Direction(Center_Coord(), As_Coord(TarCom)));
  2054. }
  2055. return(PrimaryFacing.Current());
  2056. }
  2057. /***********************************************************************************************
  2058. * BuildingClass::Greatest_Threat -- Searches for target that building can fire upon. *
  2059. * *
  2060. * This routine intercepts the Greatest_Threat function so that it can add the ability *
  2061. * to search for ground targets, if this isn't a SAM site. *
  2062. * *
  2063. * INPUT: threat -- The base threat control value. Typically, it might be THREAT_RANGE *
  2064. * or THREAT_NORMAL. *
  2065. * *
  2066. * OUTPUT: Returns with a suitable target. If none could be found, then TARGET_NONE is *
  2067. * returned instead. *
  2068. * *
  2069. * WARNINGS: none *
  2070. * *
  2071. * HISTORY: *
  2072. * 01/01/1995 JLB : Created. *
  2073. *=============================================================================================*/
  2074. TARGET BuildingClass::Greatest_Threat(ThreatType threat) const
  2075. {
  2076. assert(Buildings.ID(this) == ID);
  2077. assert(IsActive);
  2078. if (Class->PrimaryWeapon != NULL) {
  2079. threat = threat | Class->PrimaryWeapon->Allowed_Threats();
  2080. }
  2081. if (Class->SecondaryWeapon != NULL) {
  2082. threat = threat | Class->SecondaryWeapon->Allowed_Threats();
  2083. }
  2084. if (House->IsHuman) {
  2085. threat = threat & ~THREAT_BUILDINGS;
  2086. }
  2087. threat = threat | THREAT_RANGE;
  2088. // if (Class->PrimaryWeapon != NULL) {
  2089. // if (Class->PrimaryWeapon->Bullet->IsAntiAircraft) {
  2090. // threat = threat | THREAT_AIR;
  2091. // }
  2092. // if (Class->PrimaryWeapon->Bullet->IsAntiGround) {
  2093. // threat = threat | THREAT_BUILDINGS|THREAT_INFANTRY|THREAT_BOATS|THREAT_VEHICLES;
  2094. // }
  2095. // threat = threat | THREAT_RANGE;
  2096. // }
  2097. return(TechnoClass::Greatest_Threat(threat));
  2098. }
  2099. /***********************************************************************************************
  2100. * BuildingClass::Grand_Opening -- Handles construction completed special operations. *
  2101. * *
  2102. * This routine is called when construction has finished. Typically, this enables *
  2103. * new production options for factories. *
  2104. * *
  2105. * INPUT: none *
  2106. * *
  2107. * OUTPUT: none *
  2108. * *
  2109. * WARNINGS: none *
  2110. * *
  2111. * HISTORY: *
  2112. * 01/08/1995 JLB : Created. *
  2113. * 06/13/1995 JLB : Added helipad. *
  2114. *=============================================================================================*/
  2115. void BuildingClass::Grand_Opening(bool captured)
  2116. {
  2117. assert(Buildings.ID(this) == ID);
  2118. assert(IsActive);
  2119. if (!HasOpened || captured) {
  2120. HasOpened = true;
  2121. /*
  2122. ** Adjust the owning house according to the power, drain, and Tiberium capacity that
  2123. ** this building has.
  2124. */
  2125. House->Adjust_Drain(Class->Drain);
  2126. House->Adjust_Capacity(Class->Capacity);
  2127. House->IsRecalcNeeded = true;
  2128. /* SPECIAL CASE:
  2129. ** Tiberium Refineries get a free harvester. Add a harvester to the
  2130. ** reinforcement list at this time.
  2131. */
  2132. if (*this == STRUCT_REFINERY && !ScenarioInit && !captured && !Debug_Map && (!House->IsHuman || PurchasePrice == 0 || PurchasePrice > Class->Raw_Cost())) {
  2133. CELL cell = Coord_Cell(Adjacent_Cell(Center_Coord(), DIR_S));
  2134. UnitClass * unit = new UnitClass(UNIT_HARVESTER, House->Class->House);
  2135. if (unit != NULL) {
  2136. /*
  2137. ** Try to place down the harvesters. If it could not be placed, then try
  2138. ** to place it in a nearby location.
  2139. */
  2140. if (!unit->Unlimbo(Cell_Coord(cell), DIR_W)) {
  2141. cell = unit->Nearby_Location(this);
  2142. /*
  2143. ** If the harvester could still not be placed, then refund the money
  2144. ** to the owner and then bail.
  2145. */
  2146. if (!unit->Unlimbo(Cell_Coord(cell), DIR_SW)) {
  2147. House->Refund_Money(unit->Class->Cost_Of());
  2148. delete unit;
  2149. }
  2150. }
  2151. } else {
  2152. /*
  2153. ** If the harvester could not be created in the first place, then give
  2154. ** the full refund price to the owning player.
  2155. */
  2156. House->Refund_Money(UnitTypeClass::As_Reference(UNIT_HARVESTER).Cost_Of());
  2157. }
  2158. }
  2159. /*
  2160. ** Helicopter pads get a free attack helicopter.
  2161. */
  2162. if (!Rule.IsSeparate && *this == STRUCT_HELIPAD && !captured) {
  2163. ScenarioInit++;
  2164. AircraftClass * air = 0;
  2165. if (House->ActLike == HOUSE_USSR || House->ActLike == HOUSE_BAD || House->ActLike == HOUSE_UKRAINE) {
  2166. air = new AircraftClass(AIRCRAFT_HIND, House->Class->House);
  2167. } else {
  2168. air = new AircraftClass(AIRCRAFT_LONGBOW, House->Class->House);
  2169. }
  2170. if (air) {
  2171. air->Height = 0;
  2172. if (air->Unlimbo(Docking_Coord(), air->Pose_Dir())) {
  2173. air->Assign_Mission(MISSION_GUARD);
  2174. air->Transmit_Message(RADIO_HELLO, this);
  2175. Transmit_Message(RADIO_TETHER);
  2176. }
  2177. }
  2178. ScenarioInit--;
  2179. }
  2180. }
  2181. }
  2182. /***********************************************************************************************
  2183. * BuildingClass::Repair -- Initiates or terminates the repair process. *
  2184. * *
  2185. * This routine will start, stop, or toggle the repair process. When a building repairs, it *
  2186. * occurs incrementally over time. *
  2187. * *
  2188. * INPUT: control -- Determines how to control the repair process. *
  2189. * 0: Turns repair process off (if it was on). *
  2190. * 1: Turns repair process on (if it was off). *
  2191. * -1:Toggles repair process to other state. *
  2192. * *
  2193. * OUTPUT: none *
  2194. * *
  2195. * WARNINGS: none *
  2196. * *
  2197. * HISTORY: *
  2198. * 01/08/1995 JLB : Created. *
  2199. *=============================================================================================*/
  2200. void BuildingClass::Repair(int control)
  2201. {
  2202. assert(Buildings.ID(this) == ID);
  2203. assert(IsActive);
  2204. switch (control) {
  2205. case -1:
  2206. IsRepairing = (IsRepairing == false);
  2207. break;
  2208. case 1:
  2209. if (IsRepairing) return;
  2210. IsRepairing = true;
  2211. break;
  2212. case 0:
  2213. if (!IsRepairing) return;
  2214. IsRepairing = false;
  2215. break;
  2216. default:
  2217. break;
  2218. }
  2219. /*
  2220. ** At this point, we know that the repair state has changed. Perform
  2221. ** appropriate action.
  2222. */
  2223. VocType soundid = VOC_NONE;
  2224. if (IsRepairing) {
  2225. if (Strength == Class->MaxStrength) {
  2226. soundid = VOC_SCOLD;
  2227. } else {
  2228. soundid = VOC_CLICK;
  2229. if (House->IsPlayerControl) {
  2230. Clicked_As_Target();
  2231. }
  2232. IsWrenchVisible = true;
  2233. }
  2234. } else {
  2235. soundid = VOC_CLICK;
  2236. }
  2237. if (House->IsPlayerControl) {
  2238. Sound_Effect(soundid, Coord);
  2239. }
  2240. }
  2241. /***********************************************************************************************
  2242. * BuildingClass::Sell_Back -- Controls the sell back (demolish) operation. *
  2243. * *
  2244. * This routine will initiate or stop the sell back process for a building. It is called *
  2245. * when the player clicks on a building when the sell mode is active. *
  2246. * *
  2247. * INPUT: control -- The action to perform. 0 = turn deconstruction off, 1 = deconstruct, *
  2248. * -1 = toggle deconstruction state. *
  2249. * *
  2250. * OUTPUT: none *
  2251. * *
  2252. * WARNINGS: none *
  2253. * *
  2254. * HISTORY: *
  2255. * 06/25/1995 JLB : Created. *
  2256. *=============================================================================================*/
  2257. void BuildingClass::Sell_Back(int control)
  2258. {
  2259. assert(Buildings.ID(this) == ID);
  2260. assert(IsActive);
  2261. if (Class->Get_Buildup_Data()) {
  2262. bool decon = false;
  2263. switch (control) {
  2264. case -1:
  2265. decon = (Mission != MISSION_DECONSTRUCTION);
  2266. break;
  2267. case 1:
  2268. if (Mission == MISSION_DECONSTRUCTION) return;
  2269. if (IsGoingToBlow) return;
  2270. decon = true;
  2271. break;
  2272. case 0:
  2273. if (Mission != MISSION_DECONSTRUCTION) return;
  2274. decon = false;
  2275. break;
  2276. default:
  2277. break;
  2278. }
  2279. /*
  2280. ** At this point, we know that the repair state has changed. Perform
  2281. ** appropriate action.
  2282. */
  2283. if (decon) {
  2284. Assign_Mission(MISSION_DECONSTRUCTION);
  2285. Commence();
  2286. if (House->IsPlayerControl) {
  2287. Clicked_As_Target();
  2288. }
  2289. }
  2290. if (House->IsPlayerControl) {
  2291. Sound_Effect(VOC_CLICK);
  2292. }
  2293. }
  2294. }
  2295. /***********************************************************************************************
  2296. * BuildingClass::What_Action -- Determines action to perform if click on specified object. *
  2297. * *
  2298. * This routine will determine what action to perform if the mouse was clicked on the *
  2299. * object specified. This determination is used to control the mouse imagery and the *
  2300. * function process when the mouse button is pressed. *
  2301. * *
  2302. * INPUT: object -- Pointer to the object that, if clicked on, will control what action *
  2303. * is to be performed. *
  2304. * *
  2305. * OUTPUT: Returns with the ActionType that will occur if the mouse is clicked over the *
  2306. * object specified while the building is currently selected. *
  2307. * *
  2308. * WARNINGS: none *
  2309. * *
  2310. * HISTORY: *
  2311. * 01/18/1995 JLB : Created. *
  2312. *=============================================================================================*/
  2313. ActionType BuildingClass::What_Action(ObjectClass const * object) const
  2314. {
  2315. assert(Buildings.ID(this) == ID);
  2316. assert(IsActive);
  2317. ActionType action = TechnoClass::What_Action(object);
  2318. if (action == ACTION_SELF) {
  2319. int index;
  2320. if (Class->Is_Factory() && PlayerPtr == House && *House->Factory_Counter(Class->ToBuild) > 1) {
  2321. switch (Class->ToBuild) {
  2322. case RTTI_INFANTRYTYPE:
  2323. case RTTI_INFANTRY:
  2324. action = ACTION_NONE;
  2325. if (*this == STRUCT_KENNEL) {
  2326. for (index = 0; index < Buildings.Count(); index++) {
  2327. BuildingClass *bldg = Buildings.Ptr(index);
  2328. if (bldg != this && bldg->Owner() == Owner() && *bldg == STRUCT_KENNEL) {
  2329. action = ACTION_SELF;
  2330. break;
  2331. }
  2332. }
  2333. } else {
  2334. for (index = 0; index < Buildings.Count(); index++) {
  2335. BuildingClass *bldg = Buildings.Ptr(index);
  2336. if (bldg != this && bldg->Owner() == Owner() && bldg->Class->ToBuild == RTTI_INFANTRYTYPE && *bldg != STRUCT_KENNEL) {
  2337. action = ACTION_SELF;
  2338. break;
  2339. }
  2340. }
  2341. }
  2342. break;
  2343. case RTTI_NONE:
  2344. action = ACTION_NONE;
  2345. break;
  2346. default:
  2347. break;
  2348. }
  2349. } else {
  2350. action = ACTION_NONE;
  2351. }
  2352. }
  2353. /*
  2354. ** Don't allow targeting of SAM sites, even if the CTRL key
  2355. ** is held down. Also don't allow targeting if the object is too
  2356. ** far away.
  2357. */
  2358. if (action == ACTION_ATTACK && (*this == STRUCT_SAM || *this == STRUCT_AAGUN || !In_Range(object, 0))) {
  2359. action = ACTION_NONE;
  2360. }
  2361. if (action == ACTION_MOVE) {
  2362. action = ACTION_NONE;
  2363. }
  2364. return(action);
  2365. }
  2366. /***********************************************************************************************
  2367. * BuildingClass::What_Action -- Determines what action will occur. *
  2368. * *
  2369. * This routine examines the cell specified and returns with the action that will be *
  2370. * performed if that cell were clicked upon while the building is selected. *
  2371. * *
  2372. * INPUT: cell -- The cell to examine. *
  2373. * *
  2374. * OUTPUT: Returns the ActionType that indicates what should occur if the mouse is clicked *
  2375. * on this cell. *
  2376. * *
  2377. * WARNINGS: none *
  2378. * *
  2379. * HISTORY: *
  2380. * 01/18/1995 JLB : Created. *
  2381. *=============================================================================================*/
  2382. ActionType BuildingClass::What_Action(CELL cell) const
  2383. {
  2384. assert(Buildings.ID(this) == ID);
  2385. assert(IsActive);
  2386. ActionType action = TechnoClass::What_Action(cell);
  2387. if (action == ACTION_MOVE && (*this != STRUCT_CONST || !Rule.IsMCVDeploy)) {
  2388. action = ACTION_NONE;
  2389. }
  2390. /*
  2391. ** Don't allow targeting of SAM sites, even if the CTRL key
  2392. ** is held down.
  2393. */
  2394. if (action == ACTION_ATTACK && Class->PrimaryWeapon != NULL && !Class->PrimaryWeapon->Bullet->IsAntiGround) {
  2395. // if (action == ACTION_ATTACK && (*this == STRUCT_SAM || *this == STRUCT_AAGUN)) {
  2396. action = ACTION_NONE;
  2397. }
  2398. return(action);
  2399. }
  2400. /***********************************************************************************************
  2401. * BuildingClass::Begin_Mode -- Begins an animation mode for the building. *
  2402. * *
  2403. * This routine will start the building animating. This animation will loop indefinitely *
  2404. * until explicitly stopped. *
  2405. * *
  2406. * INPUT: bstate -- The animation state to initiate. *
  2407. * *
  2408. * OUTPUT: none *
  2409. * *
  2410. * WARNINGS: The building graphic state will reflect the first stage of this animation the *
  2411. * very next time it is rendered. *
  2412. * *
  2413. * HISTORY: *
  2414. * 06/25/1995 JLB : Created. *
  2415. * 07/02/1995 JLB : Uses normalize animation rate where applicable. *
  2416. *=============================================================================================*/
  2417. void BuildingClass::Begin_Mode(BStateType bstate)
  2418. {
  2419. assert(Buildings.ID(this) == ID);
  2420. assert(IsActive);
  2421. QueueBState = bstate;
  2422. if (BState == BSTATE_NONE || bstate == BSTATE_CONSTRUCTION || ScenarioInit) {
  2423. BState = bstate;
  2424. QueueBState = BSTATE_NONE;
  2425. BuildingTypeClass::AnimControlType const * ctrl = Fetch_Anim_Control();
  2426. int rate = ctrl->Rate;
  2427. if (Class->IsRegulated && bstate != BSTATE_CONSTRUCTION) {
  2428. rate = Options.Normalize_Delay(rate);
  2429. }
  2430. Set_Rate(rate);
  2431. Set_Stage(ctrl->Start);
  2432. }
  2433. }
  2434. /***********************************************************************************************
  2435. * BuildingClass::Center_Coord -- Fetches the center coordinate for the building. *
  2436. * *
  2437. * This routine is used to set the center coordinate for this building. *
  2438. * *
  2439. * INPUT: none *
  2440. * *
  2441. * OUTPUT: Returns with the coordinate for the center location for the building. *
  2442. * *
  2443. * WARNINGS: none *
  2444. * *
  2445. * HISTORY: *
  2446. * 03/10/1995 JLB : Created. *
  2447. *=============================================================================================*/
  2448. COORDINATE BuildingClass::Center_Coord(void) const
  2449. {
  2450. assert(Buildings.ID(this) == ID);
  2451. assert(IsActive);
  2452. return(Coord_Add(Coord, CenterOffset[Class->Size]));
  2453. }
  2454. /***********************************************************************************************
  2455. * BuildingClass::Docking_Coord -- Fetches the coordinate to use for docking. *
  2456. * *
  2457. * This routine will return the coordinate to use when an object wishes to dock with this *
  2458. * building. Normally the docking coordinate would be the center of the building. *
  2459. * Exceptions to this would be the airfield and helipad. Their docking coordinates are *
  2460. * offset to match the building artwork. *
  2461. * *
  2462. * INPUT: none *
  2463. * *
  2464. * OUTPUT: Returns with the coordinate to head to when trying to dock with this building. *
  2465. * *
  2466. * WARNINGS: none *
  2467. * *
  2468. * HISTORY: *
  2469. * 09/21/1995 JLB : Created. *
  2470. *=============================================================================================*/
  2471. COORDINATE BuildingClass::Docking_Coord(void) const
  2472. {
  2473. assert(Buildings.ID(this) == ID);
  2474. assert(IsActive);
  2475. if (*this == STRUCT_HELIPAD) {
  2476. return(Coord_Add(Coord, XYP_COORD(24, 18)));
  2477. }
  2478. if (*this == STRUCT_AIRSTRIP) {
  2479. return(Coord_Add(Coord, XYP_COORD(ICON_PIXEL_W + ICON_PIXEL_W/2, 28)));
  2480. }
  2481. return(TechnoClass::Docking_Coord());
  2482. }
  2483. /***********************************************************************************************
  2484. * BuildingClass::Can_Fire -- Determines if this building can fire. *
  2485. * *
  2486. * Use this routine to see if the building can fire its weapon. *
  2487. * *
  2488. * *
  2489. * INPUT: target -- The target that firing upon is desired. *
  2490. * *
  2491. * which -- Which weapon to use when firing. 0=primary, 1=secondary. *
  2492. * *
  2493. * OUTPUT: Returns with the fire possibility code. If firing is allowed, then FIRE_OK is *
  2494. * returned. Other cases will result in appropriate fire code value that indicates *
  2495. * why firing is not allowed. *
  2496. * *
  2497. * WARNINGS: none *
  2498. * *
  2499. * HISTORY: *
  2500. * 05/03/1995 JLB : Created. *
  2501. *=============================================================================================*/
  2502. FireErrorType BuildingClass::Can_Fire(TARGET target, int which) const
  2503. {
  2504. assert(Buildings.ID(this) == ID);
  2505. assert(IsActive);
  2506. FireErrorType canfire = TechnoClass::Can_Fire(target, which);
  2507. if (canfire == FIRE_OK) {
  2508. /*
  2509. ** Double check to make sure that the facing is roughly toward
  2510. ** the target. If the difference is too great, then firing is
  2511. ** temporarily postponed.
  2512. */
  2513. if (Class->IsTurretEquipped) {
  2514. int diff = PrimaryFacing.Difference(Direction(TarCom));
  2515. diff = abs(diff);
  2516. if (ABS(diff) > (*this == STRUCT_SAM ? 64 : 8)) {
  2517. // if (ABS(diff) > 8) {
  2518. return(FIRE_FACING);
  2519. }
  2520. /*
  2521. ** If the turret is rotating then firing must be delayed.
  2522. */
  2523. // if (PrimaryFacing.Is_Rotating()) {
  2524. // return(FIRE_ROTATING);
  2525. // }
  2526. }
  2527. /*
  2528. ** Certain buildings cannot fire if there is insufficient power.
  2529. */
  2530. if (Class->IsPowered && House->Power_Fraction() < 1) {
  2531. return(FIRE_BUSY);
  2532. }
  2533. /*
  2534. ** If an obelisk can fire, check the state of charge.
  2535. */
  2536. if (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->IsElectric && !IsCharged) {
  2537. return(FIRE_BUSY);
  2538. }
  2539. }
  2540. return(canfire);
  2541. }
  2542. /***********************************************************************************************
  2543. * BuildingClass::Toggle_Primary -- Toggles the primary factory state. *
  2544. * *
  2545. * This routine will change the primary factory state of this building. The primary *
  2546. * factory is the one that units will be produced from (by default). *
  2547. * *
  2548. * INPUT: none *
  2549. * *
  2550. * OUTPUT: Is this building NOW the primary factory? *
  2551. * *
  2552. * WARNINGS: none *
  2553. * *
  2554. * HISTORY: *
  2555. * 05/03/1995 JLB : Created. *
  2556. *=============================================================================================*/
  2557. bool BuildingClass::Toggle_Primary(void)
  2558. {
  2559. assert(Buildings.ID(this) == ID);
  2560. assert(IsActive);
  2561. if (IsLeader) {
  2562. IsLeader = false;
  2563. } else {
  2564. for (int index = 0; index < Buildings.Count(); index++) {
  2565. BuildingClass * building = Buildings.Ptr(index);
  2566. if (!building->IsInLimbo && building->Owner() == Owner() && building->Class->ToBuild == Class->ToBuild) {
  2567. if (Class->ToBuild == RTTI_INFANTRYTYPE) {
  2568. if (*building == STRUCT_KENNEL && *this == STRUCT_KENNEL) {
  2569. building->IsLeader = false;
  2570. } else {
  2571. if (*building != STRUCT_KENNEL && *this != STRUCT_KENNEL) {
  2572. building->IsLeader = false;
  2573. }
  2574. }
  2575. } else {
  2576. building->IsLeader = false;
  2577. }
  2578. }
  2579. }
  2580. IsLeader = true;
  2581. if ((HouseClass *)House == PlayerPtr) {
  2582. Speak(VOX_PRIMARY_SELECTED);
  2583. }
  2584. }
  2585. Mark(MARK_CHANGE);
  2586. return(IsLeader);
  2587. }
  2588. /***********************************************************************************************
  2589. * BuildingClass::Captured -- Captures the building. *
  2590. * *
  2591. * This routine will change the owner of the building. It handles updating any related *
  2592. * game systems as a result. Factories are the most prone to have great game related *
  2593. * consequences when captured. This could also affect the sidebar and building ownership. *
  2594. * *
  2595. * INPUT: newowner -- Pointer to the house that is now the new owner. *
  2596. * *
  2597. * OUTPUT: Was the capture attempt successful? *
  2598. * *
  2599. * WARNINGS: Capturing could fail if the house is already owned by the one specified or *
  2600. * the building isn't allowed to be captured. *
  2601. * *
  2602. * HISTORY: *
  2603. * 05/03/1995 JLB : Created. *
  2604. * 07/05/1995 JLB : Fixed production problem with capturing enemy buildings. *
  2605. *=============================================================================================*/
  2606. bool BuildingClass::Captured(HouseClass * newowner)
  2607. {
  2608. assert(Buildings.ID(this) == ID);
  2609. assert(IsActive);
  2610. if (Class->IsCaptureable && newowner != House) {
  2611. #ifdef TOFIX
  2612. switch (Owner()) {
  2613. case HOUSE_GOOD:
  2614. Speak(VOX_GDI_CAPTURED);
  2615. break;
  2616. case HOUSE_BAD:
  2617. Speak(VOX_NOD_CAPTURED);
  2618. break;
  2619. }
  2620. #endif
  2621. /*
  2622. ** Make sure the capturer isn't spying on his own building, and if
  2623. ** it was a radar facility, update the target house's RadarSpied field.
  2624. */
  2625. if (SpiedBy & (1<<(newowner->Class->House)) ) {
  2626. SpiedBy -= (1<<(newowner->Class->House));
  2627. if (*this == STRUCT_RADAR) {
  2628. Update_Radar_Spied();
  2629. }
  2630. }
  2631. if (House == PlayerPtr) {
  2632. Map.PowerClass::IsToRedraw = true;
  2633. Map.Flag_To_Redraw(false);
  2634. }
  2635. if (*this == STRUCT_GAP) {
  2636. Remove_Gap_Effect();
  2637. IsJamming = false;
  2638. Arm = 0;
  2639. }
  2640. /*
  2641. ** Add this building to the list of buildings captured this game. For internet stats purposes.
  2642. */
  2643. if (Session.Type == GAME_INTERNET) {
  2644. newowner->CapturedBuildings->Increment_Unit_Total (Class->Type);
  2645. }
  2646. House->Adjust_Power(-Power_Output());
  2647. LastStrength = 0;
  2648. House->Adjust_Drain(-Class->Drain);
  2649. int booty = House->Adjust_Capacity(-Class->Capacity, true);
  2650. /*
  2651. ** If there is something loaded, then it gets captured as well.
  2652. */
  2653. TechnoClass * tech = Attached_Object();
  2654. if (tech) tech->Captured(newowner);
  2655. /*
  2656. ** If something isn't technically attached, but is sitting on this
  2657. ** building for another reason (e.g., helicopter on helipad), then it
  2658. ** gets captured as well.
  2659. */
  2660. tech = Contact_With_Whom();
  2661. if (tech) {
  2662. if (Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER && (::Distance(tech->Center_Coord(), Docking_Coord()) < 0x0040 ||
  2663. (tech->What_Am_I() == RTTI_AIRCRAFT && ((AircraftClass *)tech)->Class->IsFixedWing && ((AircraftClass *)tech)->In_Which_Layer() == LAYER_GROUND)) ) {
  2664. tech->Captured(newowner);
  2665. } else {
  2666. Transmit_Message(RADIO_RUN_AWAY);
  2667. Transmit_Message(RADIO_OVER_OUT);
  2668. }
  2669. }
  2670. /*
  2671. ** Abort any computer production in progress.
  2672. */
  2673. if (Factory) {
  2674. delete (FactoryClass *)Factory;
  2675. Factory = 0;
  2676. }
  2677. /*
  2678. ** Decrement the factory counter for the original owner.
  2679. */
  2680. House->Active_Remove(this);
  2681. /*
  2682. ** Flag that both owners now need to update their buildable lists.
  2683. */
  2684. House->IsRecalcNeeded = true;
  2685. newowner->IsRecalcNeeded = true;
  2686. HouseClass * oldowner = House;
  2687. TARGET tocap = As_Target();
  2688. IsCaptured = true;
  2689. TechnoClass::Captured(newowner);
  2690. oldowner->ToCapture = tocap;
  2691. oldowner->Recalc_Center();
  2692. House->Recalc_Center();
  2693. if (House->ToCapture == As_Target()) {
  2694. House->ToCapture = TARGET_NONE;
  2695. }
  2696. SmudgeType bib;
  2697. CELL cell = Coord_Cell(Coord);
  2698. if (Class->Bib_And_Offset(bib, cell)) {
  2699. SmudgeClass * smudge = new SmudgeClass(bib);
  2700. if (smudge) {
  2701. smudge->Disown(cell);
  2702. delete smudge;
  2703. }
  2704. #ifdef FIXIT_CAPTURE_BIB
  2705. if (Session.Type == GAME_NORMAL) {
  2706. new SmudgeClass(bib, Cell_Coord(cell), Class->IsBase ? House->Class->House : HOUSE_NONE);
  2707. } else {
  2708. new SmudgeClass(bib, Cell_Coord(cell), House->Class->House);
  2709. }
  2710. #else
  2711. new SmudgeClass(bib, Cell_Coord(cell), House->Class->House);
  2712. #endif
  2713. }
  2714. House->Harvested(booty);
  2715. House->Stole(Refund_Amount());
  2716. /*
  2717. ** Increment the factory count for the new owner.
  2718. */
  2719. House->Active_Add(this);
  2720. IsRepairing = false;
  2721. Grand_Opening(true);
  2722. Mark(MARK_CHANGE);
  2723. /*
  2724. ** Perform a look operation when captured if it was the player
  2725. ** that performed the capture.
  2726. */
  2727. if (House == PlayerPtr) {
  2728. Look(false);
  2729. }
  2730. /*
  2731. ** If it was spied upon by the player who just captured it, clear the
  2732. ** spiedby flag for that house.
  2733. */
  2734. if (SpiedBy & (1 << (newowner->Class->House))) {
  2735. SpiedBy &= ~(1 << (newowner->Class->House));
  2736. }
  2737. /*
  2738. ** Update the new building's colors on the radar map.
  2739. */
  2740. short const * offset = Occupy_List();
  2741. while (*offset != REFRESH_EOL) {
  2742. CELL cell = Coord_Cell(Coord) + *offset++;
  2743. Map.Radar_Pixel(cell);
  2744. }
  2745. return(true);
  2746. }
  2747. return(false);
  2748. }
  2749. /***********************************************************************************************
  2750. * BuildingClass::Sort_Y -- Returns the building coordinate used for sorting. *
  2751. * *
  2752. * The coordinate value returned from this function should be used for sorting purposes. *
  2753. * It has special offset adjustment applied so that vehicles don't overlap (as much). *
  2754. * *
  2755. * INPUT: none *
  2756. * *
  2757. * OUTPUT: Returns with a coordinate value suitable to be used for sorting. *
  2758. * *
  2759. * WARNINGS: none *
  2760. * *
  2761. * HISTORY: *
  2762. * 05/23/1995 JLB : Created. *
  2763. * 06/19/1995 JLB : Handles buildings that come with bibs built-in. *
  2764. *=============================================================================================*/
  2765. COORDINATE BuildingClass::Sort_Y(void) const
  2766. {
  2767. assert(Buildings.ID(this) == ID);
  2768. assert(IsActive);
  2769. if (*this == STRUCT_REPAIR) {
  2770. return(Coord);
  2771. }
  2772. if (*this == STRUCT_BARRACKS /*|| *this == STRUCT_POWER*/) {
  2773. return(Center_Coord());
  2774. }
  2775. if (*this == STRUCT_REFINERY) {
  2776. return(Center_Coord());
  2777. }
  2778. /*
  2779. ** Mines need to bias their sort location such that they are typically drawn
  2780. ** before any objects that might overlap them.
  2781. */
  2782. if (*this == STRUCT_AVMINE || *this == STRUCT_APMINE) {
  2783. return(Coord_Move(Center_Coord(), DIR_N, CELL_LEPTON_H));
  2784. }
  2785. return(Coord_Add(Center_Coord(), XY_Coord(0, (Class->Height()*256)/3)));
  2786. }
  2787. /***********************************************************************************************
  2788. * BuildingClass::Can_Enter_Cell -- Determines if building can be placed down. *
  2789. * *
  2790. * This routine will determine if the building can be placed down at the location *
  2791. * specified. *
  2792. * *
  2793. * INPUT: cell -- The cell to examine. This is usually the cell of the upper left corner *
  2794. * of the building if it were to be placed down. *
  2795. * *
  2796. * OUTPUT: Returns with the move legality value for placement at the location specified. This *
  2797. * will either be MOVE_OK or MOVE_NO. *
  2798. * *
  2799. * WARNINGS: none *
  2800. * *
  2801. * HISTORY: *
  2802. * 06/25/1995 JLB : Created. *
  2803. *=============================================================================================*/
  2804. MoveType BuildingClass::Can_Enter_Cell(CELL cell, FacingType) const
  2805. {
  2806. assert(Buildings.ID(this) == ID);
  2807. assert(IsActive);
  2808. if (*this == STRUCT_CONST && IsDown) {
  2809. return(Map[cell].Is_Clear_To_Build(Class->Speed) ? MOVE_OK : MOVE_NO);
  2810. }
  2811. if (!Debug_Map && ScenarioInit == 0 && Session.Type == GAME_NORMAL && House->IsPlayerControl && !Map[cell].IsMapped) {
  2812. return(MOVE_NO);
  2813. }
  2814. return(Class->Legal_Placement(cell) ? MOVE_OK : MOVE_NO);
  2815. }
  2816. /***********************************************************************************************
  2817. * BuildingClass::Can_Demolish -- Can the player demolish (sell back) the building? *
  2818. * *
  2819. * Determines if the player can sell this building. Selling is possible if the building *
  2820. * is not currently in construction or deconstruction animation. *
  2821. * *
  2822. * INPUT: none *
  2823. * *
  2824. * OUTPUT: Can the building be demolished at this time? *
  2825. * *
  2826. * WARNINGS: none *
  2827. * *
  2828. * HISTORY: *
  2829. * 06/25/1995 JLB : Created. *
  2830. * 07/01/1995 JLB : If there is no buildup data, then the building can't be sold. *
  2831. * 07/17/1995 JLB : Cannot sell a refinery that has a harvester attached. *
  2832. *=============================================================================================*/
  2833. bool BuildingClass::Can_Demolish(void) const
  2834. {
  2835. assert(Buildings.ID(this) == ID);
  2836. assert(IsActive);
  2837. if (Class->IsUnsellable) return(false);
  2838. if (Class->Get_Buildup_Data() && BState != BSTATE_CONSTRUCTION && Mission != MISSION_DECONSTRUCTION && Mission != MISSION_CONSTRUCTION) {
  2839. if (*this == STRUCT_REFINERY && Is_Something_Attached()) return(false);
  2840. return(true);
  2841. }
  2842. return(false);
  2843. }
  2844. /***********************************************************************************************
  2845. * BuildingClass::Mission_Guard -- Handles guard mission for combat buildings. *
  2846. * *
  2847. * Buildings that can attack are given this mission. They will wait until a suitable target *
  2848. * comes within range and then launch into the attack mission. Buildings that have no *
  2849. * weaponry will just sit in this routine forever. *
  2850. * *
  2851. * INPUT: none *
  2852. * *
  2853. * OUTPUT: Returns with the number of game frames to delay before this routine will be called *
  2854. * again. *
  2855. * *
  2856. * WARNINGS: none *
  2857. * *
  2858. * HISTORY: *
  2859. * 06/25/1995 JLB : Created. *
  2860. *=============================================================================================*/
  2861. int BuildingClass::Mission_Guard(void)
  2862. {
  2863. assert(Buildings.ID(this) == ID);
  2864. assert(IsActive);
  2865. /*
  2866. ** If this building has a weapon, then search for a target to attack. When
  2867. ** a target is found, switch into attack mode to deal with the threat.
  2868. */
  2869. if (Is_Weapon_Equipped()) {
  2870. /*
  2871. ** Weapon equipped buildings are ALWAYS ready to launch into another mission if
  2872. ** they are sitting around in guard mode.
  2873. */
  2874. IsReadyToCommence = true;
  2875. /*
  2876. ** If there is no target available, then search for one.
  2877. */
  2878. if (!Target_Legal(TarCom)) {
  2879. ThreatType threat = THREAT_NORMAL;
  2880. Assign_Target(Greatest_Threat(threat));
  2881. }
  2882. /*
  2883. ** There is a valid target. Switch into attack mode right away.
  2884. */
  2885. if (Target_Legal(TarCom)) {
  2886. Assign_Mission(MISSION_ATTACK);
  2887. Commence();
  2888. return(1);
  2889. }
  2890. } else {
  2891. /*
  2892. ** This is the very simple state machine that basically does
  2893. ** nothing. This is the mode that non weapon equipped buildings
  2894. ** are normally in.
  2895. */
  2896. enum {
  2897. INITIAL_ENTRY,
  2898. IDLE
  2899. };
  2900. switch (Status) {
  2901. case INITIAL_ENTRY:
  2902. Begin_Mode(BSTATE_IDLE);
  2903. Status = IDLE;
  2904. break;
  2905. case IDLE:
  2906. /*
  2907. ** Special case to break out of guard mode if this is a repair
  2908. ** facility and there is a customer waiting at the grease pit.
  2909. */
  2910. if (*this == STRUCT_REPAIR &&
  2911. In_Radio_Contact() &&
  2912. Contact_With_Whom()->Is_Techno() &&
  2913. ((TechnoClass *)Contact_With_Whom())->Mission == MISSION_ENTER &&
  2914. Distance(Contact_With_Whom()) < 0x0040 &&
  2915. Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER) {
  2916. Assign_Mission(MISSION_REPAIR);
  2917. return(1);
  2918. }
  2919. break;
  2920. default:
  2921. break;
  2922. }
  2923. if (*this == STRUCT_REPAIR) {
  2924. return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  2925. } else {
  2926. return(MissionControl[Mission].Normal_Delay() * 3 + Random_Pick(0, 2));
  2927. }
  2928. }
  2929. return(MissionControl[Mission].AA_Delay() + Random_Pick(0, 2));
  2930. }
  2931. /***********************************************************************************************
  2932. * BuildingClass::Mission_Construction -- Handles mission construction. *
  2933. * *
  2934. * This routine will handle mission construction. When this mission is complete, the *
  2935. * building will begin normal operation. *
  2936. * *
  2937. * INPUT: none *
  2938. * *
  2939. * OUTPUT: Returns with the number of game frames to delay before calling this routine *
  2940. * again. *
  2941. * *
  2942. * WARNINGS: none *
  2943. * *
  2944. * HISTORY: *
  2945. * 06/25/1995 JLB : Created. *
  2946. *=============================================================================================*/
  2947. int BuildingClass::Mission_Construction(void)
  2948. {
  2949. assert(Buildings.ID(this) == ID);
  2950. assert(IsActive);
  2951. enum {
  2952. INITIAL,
  2953. DURING
  2954. };
  2955. switch (Status) {
  2956. case INITIAL:
  2957. Begin_Mode(BSTATE_CONSTRUCTION);
  2958. Transmit_Message(RADIO_BUILDING);
  2959. if (House->IsPlayerControl) {
  2960. Sound_Effect(VOC_CONSTRUCTION, Coord);
  2961. }
  2962. Status = DURING;
  2963. break;
  2964. case DURING:
  2965. if (IsReadyToCommence) {
  2966. /*
  2967. ** When construction is complete, then transmit this
  2968. ** to the construction yard so that it can stop its
  2969. ** construction animation.
  2970. */
  2971. Transmit_Message(RADIO_COMPLETE); // "I'm finished."
  2972. Transmit_Message(RADIO_OVER_OUT); // "You're free."
  2973. Begin_Mode(BSTATE_IDLE);
  2974. Grand_Opening();
  2975. Assign_Mission(MISSION_GUARD);
  2976. PrimaryFacing = Class->StartFace;
  2977. }
  2978. break;
  2979. default:
  2980. break;
  2981. }
  2982. return(1);
  2983. }
  2984. /***********************************************************************************************
  2985. * BuildingClass::Mission_Deconstruction -- Handles building deconstruction. *
  2986. * *
  2987. * This state machine is only used when the building is deconstructing as a result of *
  2988. * selling. When this mission is finished, the building will no longer exist. *
  2989. * *
  2990. * INPUT: none *
  2991. * *
  2992. * OUTPUT: Returns with the number of game frames to delay before calling this routine again. *
  2993. * *
  2994. * WARNINGS: none *
  2995. * *
  2996. * HISTORY: *
  2997. * 06/25/1995 JLB : Created. *
  2998. * 08/13/1995 JLB : Enable selling of units on a repair bay. *
  2999. * 08/20/1995 JLB : Scatters infantry from scattered starting points. *
  3000. *=============================================================================================*/
  3001. int BuildingClass::Mission_Deconstruction(void)
  3002. {
  3003. assert(Buildings.ID(this) == ID);
  3004. assert(IsActive);
  3005. /*
  3006. ** Always force repair off.
  3007. */
  3008. Repair(0);
  3009. enum {
  3010. INITIAL,
  3011. HOLDING,
  3012. DURING
  3013. };
  3014. switch (Status) {
  3015. case INITIAL:
  3016. /*
  3017. ** Special check for the repair bay which has the ability to sell
  3018. ** whatever is on it. If there is something on the repair bay, then
  3019. ** it will be sold. If there is nothing on the repair bay, then
  3020. ** the repair bay itself will be sold.
  3021. */
  3022. if ( (*this == STRUCT_REPAIR || *this == STRUCT_AIRSTRIP) && Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER && Distance(Contact_With_Whom()) < 0x0080) {
  3023. TechnoClass * tech = Contact_With_Whom();
  3024. Transmit_Message(RADIO_OVER_OUT);
  3025. if (IsOwnedByPlayer) Speak(VOX_UNIT_SOLD);
  3026. tech->Sell_Back(1);
  3027. Assign_Mission(MISSION_GUARD);
  3028. return(1);
  3029. }
  3030. /*
  3031. ** Selling off a shipyard or sub pen may cause attached ships
  3032. ** who are repairing themselves to discontinue repairs.
  3033. */
  3034. if (*this == STRUCT_SHIP_YARD || *this == STRUCT_SUB_PEN) {
  3035. for (int index = 0; index < Vessels.Count(); index++) {
  3036. VesselClass * obj = Vessels.Ptr(index);
  3037. if (obj && !obj->IsInLimbo && obj->House == House) {
  3038. if (obj->IsSelfRepairing) {
  3039. if (::Distance(Center_Coord(), obj->Center_Coord()) < 0x0200) {
  3040. obj->IsSelfRepairing = false;
  3041. obj->IsToSelfRepair = false;
  3042. }
  3043. }
  3044. }
  3045. }
  3046. }
  3047. IsReadyToCommence = false;
  3048. Transmit_Message(RADIO_RUN_AWAY);
  3049. Status = HOLDING;
  3050. break;
  3051. case HOLDING:
  3052. if (!IsTethered) {
  3053. /*
  3054. ** The crew will evacuate from the building. The number of crew
  3055. ** members leaving is equal to the unrecovered cost of the building
  3056. ** divided by 100 (the typical cost of a minigunner infantryman).
  3057. */
  3058. if (!Target_Legal(ArchiveTarget) || !Rule.IsMCVDeploy || *this != STRUCT_CONST) {
  3059. int count = How_Many_Survivors();
  3060. bool engine = false;
  3061. while (count) {
  3062. /*
  3063. ** Ensure that the player only gets ONE engineer and not from a captured
  3064. ** construction yard.
  3065. */
  3066. InfantryType typ = Crew_Type();
  3067. while (typ == INFANTRY_RENOVATOR && engine) {
  3068. typ = Crew_Type();
  3069. }
  3070. if (typ == INFANTRY_RENOVATOR) engine = true;
  3071. InfantryClass * infantry = 0;
  3072. if (typ != INFANTRY_NONE) infantry = new InfantryClass(typ, House->Class->House);
  3073. if (infantry != NULL) {
  3074. ScenarioInit++;
  3075. COORDINATE coord = Coord_Add(Center_Coord(), XYP_COORD(0, -12));
  3076. coord = Map[coord].Closest_Free_Spot(coord, false);
  3077. if (infantry->Unlimbo(coord, DIR_N)) {
  3078. if (infantry->Class->IsNominal) infantry->IsTechnician = true;
  3079. ScenarioInit--;
  3080. infantry->Scatter(0, true);
  3081. ScenarioInit++;
  3082. infantry->Assign_Mission(MISSION_GUARD_AREA);
  3083. } else {
  3084. delete infantry;
  3085. }
  3086. ScenarioInit--;
  3087. }
  3088. count--;
  3089. }
  3090. }
  3091. if (House->IsPlayerControl) {
  3092. Sound_Effect(VOC_CASHTURN, Coord);
  3093. }
  3094. Status = DURING;
  3095. Begin_Mode(BSTATE_CONSTRUCTION);
  3096. Detach_All(true);
  3097. Transmit_Message(RADIO_OVER_OUT);
  3098. IsReadyToCommence = false;
  3099. break;
  3100. }
  3101. Transmit_Message(RADIO_RUN_AWAY);
  3102. break;
  3103. case DURING:
  3104. if (IsReadyToCommence) {
  3105. House->IsRecalcNeeded = true;
  3106. if (IsOwnedByPlayer) Speak(VOX_STRUCTURE_SOLD);
  3107. /*
  3108. ** Construction yards that deconstruct, really just revert back
  3109. ** to an MCV.
  3110. */
  3111. if (Target_Legal(ArchiveTarget) && *this == STRUCT_CONST && House->IsHuman && Strength > 0) {
  3112. ScenarioInit++;
  3113. UnitClass * unit = new UnitClass(UNIT_MCV, House->Class->House);
  3114. ScenarioInit--;
  3115. if (unit != NULL) {
  3116. /*
  3117. ** Unlimbo the MCV onto the map. The MCV should start in the same
  3118. ** health condition that the construction yard was in.
  3119. */
  3120. fixed ratio = Health_Ratio();
  3121. int money = Refund_Amount();
  3122. TARGET arch = ArchiveTarget;
  3123. COORDINATE place = Coord_Snap(Adjacent_Cell(Coord, DIR_SE));
  3124. delete this;
  3125. if (unit->Unlimbo(place, DIR_SW)) {
  3126. unit->Strength = unit->Class_Of().MaxStrength * ratio;
  3127. /*
  3128. ** Lift the move destination from the building and assign
  3129. ** it to the unit.
  3130. */
  3131. if (Target_Legal(arch)) {
  3132. unit->Assign_Destination(arch);
  3133. unit->Assign_Mission(MISSION_MOVE);
  3134. }
  3135. } else {
  3136. /*
  3137. ** If, for some strange reason, the MCV could not be placed on the
  3138. ** map, then give the player some money to compensate.
  3139. */
  3140. House->Refund_Money(money);
  3141. }
  3142. } else {
  3143. House->Refund_Money(Refund_Amount());
  3144. delete this;
  3145. }
  3146. } else {
  3147. /*
  3148. ** Selling off a gap generator will cause the cells it affects
  3149. ** to stop being jammed.
  3150. */
  3151. if (*this == STRUCT_GAP) {
  3152. Remove_Gap_Effect();
  3153. }
  3154. /*
  3155. ** A sold building still counts as a kill, but it just isn't directly
  3156. ** attributed to the enemy.
  3157. */
  3158. WhoLastHurtMe = HOUSE_NONE;
  3159. Record_The_Kill(NULL);
  3160. /*
  3161. ** The player gets part of the money back for the sell.
  3162. */
  3163. House->Refund_Money(Refund_Amount());
  3164. House->Stole(-Refund_Amount());
  3165. Limbo();
  3166. /*
  3167. ** Finally, delete the building from the game.
  3168. */
  3169. delete this;
  3170. }
  3171. }
  3172. break;
  3173. default:
  3174. break;
  3175. }
  3176. return(1);
  3177. }
  3178. /***********************************************************************************************
  3179. * BuildingClass::Mission_Attack -- Handles attack mission for building. *
  3180. * *
  3181. * Buildings that can attack are processed by this attack mission state machine. *
  3182. * *
  3183. * INPUT: none *
  3184. * *
  3185. * OUTPUT: Returns with the number of game frames to delay before calling this routine *
  3186. * again. *
  3187. * *
  3188. * WARNINGS: none *
  3189. * *
  3190. * HISTORY: *
  3191. * 06/25/1995 JLB : Created. *
  3192. * 02/22/1996 JLB : SAM doesn't lower back into ground. *
  3193. *=============================================================================================*/
  3194. int BuildingClass::Mission_Attack(void)
  3195. {
  3196. assert(Buildings.ID(this) == ID);
  3197. assert(IsActive);
  3198. if (*this == STRUCT_SAM) {
  3199. switch (Status) {
  3200. /*
  3201. ** This is the target tracking state of the launcher. It will rotate
  3202. ** to face the current TarCom of the launcher.
  3203. */
  3204. case SAM_READY:
  3205. if ((Class->IsPowered && House->Power_Fraction() < 1) || IsJammed) {
  3206. return(1);
  3207. }
  3208. if (!Target_Legal(TarCom) || !Is_Target_Aircraft(TarCom) || As_Aircraft(TarCom)->Height == 0) {
  3209. Assign_Target(TARGET_NONE);
  3210. Status = SAM_READY;
  3211. Assign_Mission(MISSION_GUARD);
  3212. Commence();
  3213. return(1);
  3214. } else {
  3215. if (!PrimaryFacing.Is_Rotating()) {
  3216. DirType facing = Direction(TarCom);
  3217. if (PrimaryFacing.Difference(facing)) {
  3218. PrimaryFacing.Set_Desired(facing);
  3219. } else {
  3220. Status = SAM_FIRING;
  3221. }
  3222. }
  3223. }
  3224. return(1);
  3225. /*
  3226. ** The launcher is in the process of firing.
  3227. */
  3228. case SAM_FIRING:
  3229. if (!Target_Legal(TarCom) || !Is_Target_Aircraft(TarCom) || As_Aircraft(TarCom)->Height == 0) {
  3230. Assign_Target(TARGET_NONE);
  3231. Status = SAM_READY;
  3232. } else {
  3233. FireErrorType error = Can_Fire(TarCom, 0);
  3234. if (error == FIRE_ILLEGAL || error == FIRE_CANT || error == FIRE_RANGE) {
  3235. Assign_Target(TARGET_NONE);
  3236. Status = SAM_READY;
  3237. } else {
  3238. if (error == FIRE_FACING) {
  3239. Status = SAM_READY;
  3240. } else {
  3241. if (error == FIRE_OK) {
  3242. Fire_At(TarCom, 0);
  3243. Fire_At(TarCom, 1);
  3244. Status = SAM_READY;
  3245. }
  3246. }
  3247. }
  3248. }
  3249. return(1);
  3250. default:
  3251. break;
  3252. }
  3253. return(MissionControl[Mission].AA_Delay() + Random_Pick(0, 2));
  3254. }
  3255. if (!Target_Legal(TarCom)) {
  3256. Assign_Target(TARGET_NONE);
  3257. Assign_Mission(MISSION_GUARD);
  3258. Commence();
  3259. return(1);
  3260. }
  3261. int primary = What_Weapon_Should_I_Use(TarCom);
  3262. IsReadyToCommence = true;
  3263. switch (Can_Fire(TarCom, primary)) {
  3264. case FIRE_ILLEGAL:
  3265. case FIRE_CANT:
  3266. case FIRE_RANGE:
  3267. case FIRE_AMMO:
  3268. Assign_Target(TARGET_NONE);
  3269. Assign_Mission(MISSION_GUARD);
  3270. Commence();
  3271. break;
  3272. case FIRE_FACING:
  3273. PrimaryFacing.Set_Desired(Direction(TarCom));
  3274. return(2);
  3275. case FIRE_REARM:
  3276. PrimaryFacing.Set_Desired(Direction(TarCom));
  3277. return(Arm);
  3278. case FIRE_BUSY:
  3279. return(1);
  3280. case FIRE_CLOAKED:
  3281. Do_Uncloak();
  3282. break;
  3283. case FIRE_OK:
  3284. Fire_At(TarCom, primary);
  3285. return(1);
  3286. default:
  3287. break;
  3288. }
  3289. PrimaryFacing.Set_Desired(Direction(TarCom));
  3290. return(1);
  3291. // return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  3292. }
  3293. /***********************************************************************************************
  3294. * BuildingClass::Mission_Harvest -- Handles refinery unloading harvesters. *
  3295. * *
  3296. * This state machine handles the refinery when it unloads the harvester. *
  3297. * *
  3298. * INPUT: none *
  3299. * *
  3300. * OUTPUT: Returns with the number of game frames to delay before calling this routine *
  3301. * again. *
  3302. * *
  3303. * WARNINGS: none *
  3304. * *
  3305. * HISTORY: *
  3306. * 06/25/1995 JLB : Created. *
  3307. *=============================================================================================*/
  3308. int BuildingClass::Mission_Harvest(void)
  3309. {
  3310. assert(Buildings.ID(this) == ID);
  3311. assert(IsActive);
  3312. enum {
  3313. INITIAL, // Dock the Tiberium cannister.
  3314. WAIT_FOR_DOCK, // Waiting for docking to complete.
  3315. MIDDLE, // Offload "bails" of tiberium.
  3316. WAIT_FOR_UNDOCK // Waiting for undocking to complete.
  3317. };
  3318. switch (Status) {
  3319. case INITIAL:
  3320. Status = WAIT_FOR_DOCK;
  3321. break;
  3322. case WAIT_FOR_DOCK:
  3323. if (IsReadyToCommence) {
  3324. IsReadyToCommence = false;
  3325. Status = MIDDLE;
  3326. }
  3327. break;
  3328. case MIDDLE:
  3329. if (IsReadyToCommence) {
  3330. IsReadyToCommence = false;
  3331. /*
  3332. ** Force any bib squatters to scatter.
  3333. */
  3334. Map[Adjacent_Cell(Coord_Cell(Center_Coord()), DIR_S)].Incoming(0, true, true);
  3335. FootClass * techno = Attached_Object();
  3336. if (techno) {
  3337. int bail = techno->Offload_Tiberium_Bail();
  3338. if (bail) {
  3339. House->Harvested(bail);
  3340. if (techno->Tiberium_Load() > 0) {
  3341. return(1);
  3342. }
  3343. }
  3344. }
  3345. Status = WAIT_FOR_UNDOCK;
  3346. }
  3347. break;
  3348. case WAIT_FOR_UNDOCK:
  3349. if (IsReadyToCommence) {
  3350. /*
  3351. ** Detach harvester and go back into idle state.
  3352. */
  3353. Assign_Mission(MISSION_GUARD);
  3354. }
  3355. break;
  3356. default:
  3357. break;
  3358. }
  3359. return(1);
  3360. }
  3361. /***********************************************************************************************
  3362. * BuildingClass::Mission_Repair -- Handles the repair (active) state for building. *
  3363. * *
  3364. * This state machine is used when the building is active in some sort of repair or *
  3365. * construction mode. The construction yard will animate. The repair facility will repair *
  3366. * anything that it docked on it. *
  3367. * *
  3368. * INPUT: none *
  3369. * *
  3370. * OUTPUT: Returns with the number of game frames to delay before calling this routine again. *
  3371. * *
  3372. * WARNINGS: none *
  3373. * *
  3374. * HISTORY: *
  3375. * 06/25/1995 JLB : Created. *
  3376. * 06/25/1995 JLB : Handles repair facility *
  3377. * 07/29/1995 JLB : Repair rate is controlled by power rating. *
  3378. *=============================================================================================*/
  3379. int BuildingClass::Mission_Repair(void)
  3380. {
  3381. assert(Buildings.ID(this) == ID);
  3382. assert(IsActive);
  3383. if (*this == STRUCT_CONST) {
  3384. enum {
  3385. INITIAL,
  3386. DURING
  3387. };
  3388. switch (Status) {
  3389. case INITIAL:
  3390. Begin_Mode(BSTATE_ACTIVE);
  3391. Status = DURING;
  3392. break;
  3393. case DURING:
  3394. if (!In_Radio_Contact()) {
  3395. Assign_Mission(MISSION_GUARD);
  3396. }
  3397. break;
  3398. default:
  3399. break;
  3400. }
  3401. return(1);
  3402. }
  3403. if (*this == STRUCT_REPAIR) {
  3404. enum {
  3405. INITIAL,
  3406. IDLE,
  3407. DURING
  3408. };
  3409. switch (Status) {
  3410. case INITIAL:
  3411. {
  3412. if (!In_Radio_Contact()) {
  3413. Begin_Mode(BSTATE_IDLE);
  3414. Assign_Mission(MISSION_GUARD);
  3415. return(1);
  3416. }
  3417. IsReadyToCommence = false;
  3418. int distance = 0x10;
  3419. TechnoClass *tech = Contact_With_Whom();
  3420. /*
  3421. ** BG: If the unit to repair is an aircraft, and the aircraft is
  3422. ** fixed-wing, and it's landed, be much more liberal with the
  3423. ** distance check. Fixed-wing aircraft are very inaccurate with
  3424. ** their landings.
  3425. */
  3426. if (tech->What_Am_I() == RTTI_AIRCRAFT) {
  3427. if ( ((AircraftClass *)tech)->Class->IsFixedWing &&
  3428. ((AircraftClass *)tech)->In_Which_Layer() == LAYER_GROUND) {
  3429. distance = 0x80;
  3430. }
  3431. }
  3432. if (Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER && Distance(Contact_With_Whom()) < distance) {
  3433. Status = IDLE;
  3434. return(TICKS_PER_SECOND/4);
  3435. }
  3436. break;
  3437. }
  3438. case IDLE:
  3439. if (!In_Radio_Contact()) {
  3440. Assign_Mission(MISSION_GUARD);
  3441. return(1);
  3442. }
  3443. if (Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER) {
  3444. TechnoClass * radio = Contact_With_Whom();
  3445. if ( ((radio->Health_Ratio() < Rule.ConditionGreen) ||
  3446. (radio->What_Am_I() == RTTI_UNIT && *(UnitClass *)radio == UNIT_MINELAYER))
  3447. && Transmit_Message(RADIO_REPAIR) == RADIO_ROGER) {
  3448. /*
  3449. ** If the object over the repair bay is marked as useless, then
  3450. ** sell it back to get some money.
  3451. */
  3452. if (radio->IsUseless) {
  3453. if (!radio->House->IsHuman) {
  3454. radio->Sell_Back(1);
  3455. }
  3456. Status = INITIAL;
  3457. IsReadyToCommence = true;
  3458. } else {
  3459. if (IsOwnedByPlayer) Speak(VOX_REPAIRING);
  3460. Status = DURING;
  3461. Begin_Mode(BSTATE_ACTIVE);
  3462. IsReadyToCommence = false;
  3463. }
  3464. } else {
  3465. // Transmit_Message(RADIO_RUN_AWAY);
  3466. ///*BG*/ if(radio->Health_Ratio() >= Rule.ConditionGreen) {
  3467. // Transmit_Message(RADIO_RUN_AWAY);
  3468. // }
  3469. }
  3470. }
  3471. break;
  3472. case DURING:
  3473. if (!In_Radio_Contact()) {
  3474. Begin_Mode(BSTATE_IDLE);
  3475. Status = IDLE;
  3476. return(1);
  3477. }
  3478. /*
  3479. ** Check to see if the repair light blink has completed and the attached
  3480. ** unit is not doing something else. If these conditions are favorable,
  3481. ** the repair can proceed another step.
  3482. */
  3483. if (IsReadyToCommence && Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER) {
  3484. IsReadyToCommence = false;
  3485. /*
  3486. ** Tell the attached unit to repair one step. It will respond with how
  3487. ** it fared.
  3488. */
  3489. switch (Transmit_Message(RADIO_REPAIR)) {
  3490. /*
  3491. ** The repair step proceeded smoothly. Proceed normally with the
  3492. ** repair process.
  3493. */
  3494. case RADIO_ROGER:
  3495. break;
  3496. /*
  3497. ** The repair operation was aborted because of some reason. Presume
  3498. ** that the reason is because of low cash.
  3499. */
  3500. case RADIO_CANT:
  3501. if (IsOwnedByPlayer) Speak(VOX_NO_CASH);
  3502. Begin_Mode(BSTATE_IDLE);
  3503. Status = IDLE;
  3504. break;
  3505. /*
  3506. ** The repair step resulted in a completely repaired unit.
  3507. */
  3508. case RADIO_ALL_DONE:
  3509. if (IsOwnedByPlayer) Speak(VOX_UNIT_REPAIRED);
  3510. // Transmit_Message(RADIO_RUN_AWAY);
  3511. Begin_Mode(BSTATE_IDLE);
  3512. Status = IDLE;
  3513. break;
  3514. /*
  3515. ** The repair step could not be completed because this unit is already
  3516. ** at full strength.
  3517. */
  3518. case RADIO_NEGATIVE:
  3519. default:
  3520. // Transmit_Message(RADIO_RUN_AWAY);
  3521. Begin_Mode(BSTATE_IDLE);
  3522. Status = IDLE;
  3523. break;
  3524. }
  3525. }
  3526. return(1);
  3527. default:
  3528. break;
  3529. }
  3530. return(MissionControl[Mission].Normal_Delay());
  3531. }
  3532. if (*this == STRUCT_HELIPAD || *this == STRUCT_AIRSTRIP) {
  3533. enum {
  3534. INITIAL,
  3535. DURING
  3536. };
  3537. switch (Status) {
  3538. case INITIAL:
  3539. if (Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_ROGER && Transmit_Message(RADIO_PREPARED) == RADIO_NEGATIVE) {
  3540. Begin_Mode(BSTATE_ACTIVE);
  3541. Contact_With_Whom()->Assign_Mission(MISSION_SLEEP);
  3542. Status = DURING;
  3543. return(1);
  3544. }
  3545. Assign_Mission(MISSION_GUARD);
  3546. break;
  3547. case DURING:
  3548. if (IsReadyToCommence) {
  3549. if (!In_Radio_Contact() || Transmit_Message(RADIO_NEED_TO_MOVE) == RADIO_NEGATIVE) {
  3550. Assign_Mission(MISSION_GUARD);
  3551. return(1);
  3552. }
  3553. if (Transmit_Message(RADIO_PREPARED) == RADIO_ROGER) {
  3554. Contact_With_Whom()->Assign_Mission(MISSION_GUARD);
  3555. Assign_Mission(MISSION_GUARD);
  3556. return(1);
  3557. }
  3558. if (Transmit_Message(RADIO_RELOAD) != RADIO_ROGER) {
  3559. Assign_Mission(MISSION_GUARD);
  3560. Contact_With_Whom()->Assign_Mission(MISSION_GUARD);
  3561. return(1);
  3562. } else {
  3563. fixed pfrac = Saturate(House->Power_Fraction(), 1);
  3564. if (pfrac < fixed::_1_2) pfrac = fixed::_1_2;
  3565. int time = Inverse(pfrac) * Rule.ReloadRate * TICKS_PER_MINUTE;
  3566. // int time = Bound((int)(TICKS_PER_SECOND * Saturate(House->Power_Fraction(), 1)), 0, TICKS_PER_SECOND);
  3567. // time = (TICKS_PER_SECOND*3) - time;
  3568. IsReadyToCommence = false;
  3569. return(time);
  3570. }
  3571. }
  3572. break;
  3573. default:
  3574. break;
  3575. }
  3576. return(3);
  3577. }
  3578. return(TICKS_PER_SECOND);
  3579. }
  3580. /***********************************************************************************************
  3581. * BuildingClass::Mission_Missile -- State machine for nuclear missile launch. *
  3582. * *
  3583. * This handles the Temple of Nod launching its nuclear missile. *
  3584. * *
  3585. * INPUT: none *
  3586. * *
  3587. * OUTPUT: Returns with the number of frames to delay before calling this routine again. *
  3588. * *
  3589. * WARNINGS: none *
  3590. * *
  3591. * HISTORY: *
  3592. * 07/04/1995 JLB : Commented. *
  3593. *=============================================================================================*/
  3594. int BuildingClass::Mission_Missile(void)
  3595. {
  3596. assert(Buildings.ID(this) == ID);
  3597. assert(IsActive);
  3598. if (*this == STRUCT_ADVANCED_TECH) {
  3599. enum {
  3600. DOOR_OPENING,
  3601. LAUNCH_UP,
  3602. SATELLITE_DEPLOY,
  3603. DONE_LAUNCH
  3604. };
  3605. switch (Status) {
  3606. /*
  3607. ** The initial case is responsible for starting the door
  3608. ** opening on the building, the missile rising, and smoke broiling.
  3609. */
  3610. case DOOR_OPENING:
  3611. {
  3612. #ifdef FIXIT_VERSION_3
  3613. COORDINATE door = Coord_Move(Center_Coord(), (DirType)0xC0, 0x30);
  3614. AnimClass * sput = new AnimClass(ANIM_SPUTDOOR, door);
  3615. if (sput) {
  3616. IsReadyToCommence = false;
  3617. Status = LAUNCH_UP;
  3618. AnimToTrack = sput->As_Target();
  3619. }
  3620. #else
  3621. IsReadyToCommence = false;
  3622. COORDINATE door = Coord_Move(Center_Coord(), (DirType)0xC0, 0x30);
  3623. AnimClass * sput = new AnimClass(ANIM_SPUTDOOR, door);
  3624. Status = LAUNCH_UP;
  3625. AnimToTrack = sput->As_Target();
  3626. return(1);
  3627. #endif
  3628. }
  3629. /*
  3630. ** Once the smoke has been going for a little while this
  3631. ** actually handles launching the missile into the air.
  3632. */
  3633. case LAUNCH_UP:
  3634. {
  3635. AnimClass * sput = As_Animation(AnimToTrack);
  3636. if (sput) {
  3637. if (sput->Fetch_Stage() >= 19) {
  3638. CELL center = Coord_Cell(Center_Coord());
  3639. CELL cell = XY_Cell( Cell_X(center), 1);
  3640. TARGET targ = ::As_Target(cell);
  3641. BulletClass * bullet = new BulletClass(BULLET_GPS_SATELLITE, targ, this, 200, WARHEAD_FIRE, MPH_ROCKET);
  3642. if (bullet) {
  3643. COORDINATE launch = Coord_Move(Center_Coord(), (DirType)0xC0, 0x30);
  3644. if (!bullet->Unlimbo(launch, DIR_N)) {
  3645. delete bullet;
  3646. bullet = NULL;
  3647. }
  3648. }
  3649. if (bullet) {
  3650. Assign_Mission(MISSION_GUARD);
  3651. }
  3652. }
  3653. }
  3654. }
  3655. return(1);
  3656. }
  3657. }
  3658. if (*this == STRUCT_MSLO) {
  3659. enum {
  3660. INITIAL,
  3661. DOOR_OPENING,
  3662. LAUNCH_UP,
  3663. LAUNCH_DOWN,
  3664. DONE_LAUNCH
  3665. };
  3666. switch (Status) {
  3667. /*
  3668. ** The initial case is responsible for starting the door
  3669. ** opening on the building.
  3670. */
  3671. case INITIAL:
  3672. IsReadyToCommence = false;
  3673. Begin_Mode(BSTATE_ACTIVE); // open the door
  3674. Status = DOOR_OPENING;
  3675. return(1);
  3676. /*
  3677. ** This polls for the case when the door is actually open and
  3678. ** then kicks off the missile smoke.
  3679. */
  3680. case DOOR_OPENING:
  3681. if (IsReadyToCommence) {
  3682. Begin_Mode(BSTATE_AUX1); // hold the door open
  3683. Status = LAUNCH_UP;
  3684. return(14);
  3685. }
  3686. return(1);
  3687. /*
  3688. ** Once the smoke has been going for a little while this
  3689. ** actually handles launching the missile into the air.
  3690. */
  3691. case LAUNCH_UP:
  3692. {
  3693. CELL center = Coord_Cell(Center_Coord());
  3694. CELL cell = XY_Cell( Cell_X(center), 1);
  3695. TARGET targ = ::As_Target(cell);
  3696. BulletClass * bullet = new BulletClass(BULLET_NUKE_UP, targ, this, 200, WARHEAD_HE, MPH_VERY_FAST);
  3697. if (bullet) {
  3698. COORDINATE launch = Coord_Move(Center_Coord(), (DirType)28, 0xA0);
  3699. if (!bullet->Unlimbo(launch, DIR_N)) {
  3700. delete bullet;
  3701. bullet = NULL;
  3702. }
  3703. }
  3704. if (bullet) {
  3705. Speak(VOX_ABOMB_LAUNCH);
  3706. Status = LAUNCH_DOWN;
  3707. /*
  3708. ** Hack: If it's the artificial nukes, don't let the bullets come down (as
  3709. ** they're the only ones that blow up). We know it's artificial if you're
  3710. ** at tech level 10 or below, because you can't build the nuclear silo until
  3711. ** tech level 15 or so.
  3712. */
  3713. if (House->Control.TechLevel <= 10) {
  3714. return(6);
  3715. }
  3716. bullet = new BulletClass(BULLET_NUKE_DOWN, ::As_Target(House->NukeDest), this, 200, WARHEAD_NUKE, MPH_VERY_FAST);
  3717. if (bullet) {
  3718. int celly = Cell_Y(House->NukeDest);
  3719. celly -= 64;
  3720. if (celly < 1) celly = 1;
  3721. COORDINATE start = Cell_Coord(XY_Cell(Cell_X(House->NukeDest), celly));
  3722. if (!bullet->Unlimbo(start, DIR_S)) {
  3723. delete bullet;
  3724. }
  3725. }
  3726. return(8 * TICKS_PER_SECOND);
  3727. }
  3728. }
  3729. return(1);
  3730. /*
  3731. ** Once the missile is in the air, this handles waiting for
  3732. ** the missile to be off the screen and then launching one down
  3733. ** over the target.
  3734. */
  3735. case LAUNCH_DOWN:
  3736. {
  3737. Begin_Mode(BSTATE_AUX2); // start the door closing
  3738. #ifdef OBSOLETE
  3739. /*
  3740. ** Hack: If it's the artificial nukes, don't let the bullets come down (as
  3741. ** they're the only ones that blow up). We know it's artificial if you're
  3742. ** at tech level 10 or below, because you can't build the nuclear silo until
  3743. ** tech level 15 or so.
  3744. */
  3745. if (House->Control.TechLevel <= 10) {
  3746. Status = DONE_LAUNCH;
  3747. return(6);
  3748. }
  3749. BulletClass * bullet = new BulletClass(BULLET_NUKE_DOWN, ::As_Target(House->NukeDest), this, 200, WARHEAD_NUKE, MPH_VERY_FAST);
  3750. if (bullet) {
  3751. int celly = Cell_Y(House->NukeDest);
  3752. celly -= 15;
  3753. if (celly < 1) celly = 1;
  3754. COORDINATE start = Cell_Coord(XY_Cell(Cell_X(House->NukeDest), celly));
  3755. if (!bullet->Unlimbo(start, DIR_S)) {
  3756. delete bullet;
  3757. }
  3758. }
  3759. if (bullet) {
  3760. #endif
  3761. Status = DONE_LAUNCH;
  3762. return(6);
  3763. }
  3764. #ifdef OBSOLETE
  3765. }
  3766. return(1);
  3767. #endif
  3768. /*
  3769. ** Once the missile is done launching this handles allowing
  3770. ** the building to sit there with its door closed.
  3771. */
  3772. case DONE_LAUNCH:
  3773. Begin_Mode(BSTATE_IDLE); // keep the door closed.
  3774. Assign_Mission(MISSION_GUARD);
  3775. return(60);
  3776. }
  3777. }
  3778. return(MissionControl[Mission].Normal_Delay());
  3779. }
  3780. /***********************************************************************************************
  3781. * BuildingClass::Revealed -- Reveals the building to the specified house. *
  3782. * *
  3783. * This routine will reveal the building to the specified house. It will handle updating *
  3784. * the sidebar for player owned buildings. A player owned building that hasn't been *
  3785. * revealed, is in a state of pseudo-limbo. It cannot be used for any of its special *
  3786. * abilities even though it exists on the map for all other purposes. *
  3787. * *
  3788. * INPUT: house -- The house that this building is being revealed to. *
  3789. * *
  3790. * OUTPUT: Was this building revealed by this procedure? *
  3791. * *
  3792. * WARNINGS: none *
  3793. * *
  3794. * HISTORY: *
  3795. * 06/25/1995 JLB : Created. *
  3796. *=============================================================================================*/
  3797. bool BuildingClass::Revealed(HouseClass * house)
  3798. {
  3799. assert(Buildings.ID(this) == ID);
  3800. assert(IsActive);
  3801. if (TechnoClass::Revealed(house)) {
  3802. if (!ScenarioInit) {
  3803. House->JustBuiltStructure = Class->Type;
  3804. House->IsBuiltSomething = true;
  3805. }
  3806. House->IsRecalcNeeded = true;
  3807. /*
  3808. ** Perform any grand opening here so that in the scenarios where a player
  3809. ** owned house is not yet revealed, it won't be reflected in the sidebar
  3810. ** selection icons.
  3811. */
  3812. if (!In_Radio_Contact() && House->IsHuman && Mission != MISSION_CONSTRUCTION) {
  3813. Grand_Opening();
  3814. } else {
  3815. if (!In_Radio_Contact() && !House->IsHuman && house == House && Mission != MISSION_CONSTRUCTION) {
  3816. Grand_Opening();
  3817. }
  3818. }
  3819. return(true);
  3820. }
  3821. return(false);
  3822. }
  3823. /***********************************************************************************************
  3824. * BuildingClass::Enter_Idle_Mode -- The building will enter its idle mode. *
  3825. * *
  3826. * This routine is called when the exact mode of the building isn't known. By examining *
  3827. * the building's condition, this routine will assign an appropriate mission. *
  3828. * *
  3829. * INPUT: initial -- This this being called during scenario init? *
  3830. * *
  3831. * OUTPUT: none *
  3832. * *
  3833. * WARNINGS: none *
  3834. * *
  3835. * HISTORY: *
  3836. * 06/25/1995 JLB : Created. *
  3837. *=============================================================================================*/
  3838. void BuildingClass::Enter_Idle_Mode(bool initial)
  3839. {
  3840. assert(Buildings.ID(this) == ID);
  3841. assert(IsActive);
  3842. /*
  3843. ** Assign an appropriate mission for the building. If the ScenarioInit flag is true, then
  3844. ** this must be an initial building. Start such buildings in idle state. For other buildings
  3845. ** it indicates that it is being placed during game play and thus it must start in
  3846. ** the "construction" mission.
  3847. */
  3848. MissionType mission = MISSION_GUARD;
  3849. if (!initial || ScenarioInit || Debug_Map) {
  3850. Begin_Mode(BSTATE_IDLE);
  3851. mission = MISSION_GUARD;
  3852. } else {
  3853. Begin_Mode(BSTATE_CONSTRUCTION);
  3854. mission = MISSION_CONSTRUCTION;
  3855. }
  3856. Assign_Mission(mission);
  3857. }
  3858. /***********************************************************************************************
  3859. * BuildingClass::Pip_Count -- Determines "full" pips to display for building. *
  3860. * *
  3861. * This routine will determine the number of pips that should be filled in when rendering *
  3862. * the building. *
  3863. * *
  3864. * INPUT: none *
  3865. * *
  3866. * OUTPUT: Returns the number of pips to display as filled in. *
  3867. * *
  3868. * WARNINGS: none *
  3869. * *
  3870. * HISTORY: *
  3871. * 06/28/1995 JLB : Created. *
  3872. *=============================================================================================*/
  3873. int BuildingClass::Pip_Count(void) const
  3874. {
  3875. assert(Buildings.ID(this) == ID);
  3876. assert(IsActive);
  3877. return(Class->Max_Pips() * House->Tiberium_Fraction());
  3878. }
  3879. /***********************************************************************************************
  3880. * BuildingClass::Death_Announcement -- Announce the death of this building. *
  3881. * *
  3882. * This routine is called when the building is destroyed by "unnatural" means. Typically *
  3883. * as a result of combat. If the building is known to the player, then it should be *
  3884. * announced. *
  3885. * *
  3886. * INPUT: source -- The object most directly responsible for the building's death. *
  3887. * *
  3888. * OUTPUT: none *
  3889. * *
  3890. * WARNINGS: none *
  3891. * *
  3892. * HISTORY: *
  3893. * 07/04/1995 JLB : Created. *
  3894. *=============================================================================================*/
  3895. void BuildingClass::Death_Announcement(TechnoClass const * source) const
  3896. {
  3897. assert(Buildings.ID(this) == ID);
  3898. assert(IsActive);
  3899. if (source != NULL && House->IsPlayerControl) {
  3900. Speak(VOX_STRUCTURE_DESTROYED);
  3901. }
  3902. }
  3903. /***********************************************************************************************
  3904. * BuildingClass::Fire_Direction -- Fetches the direction of firing. *
  3905. * *
  3906. * This routine will return with the default direction to use when firing from this *
  3907. * building. This is the facing of the turret except for the case of non-turret equipped *
  3908. * buildings that have a weapon (e.g., guard tower). *
  3909. * *
  3910. * INPUT: none *
  3911. * *
  3912. * OUTPUT: Returns with the default firing direction for this building. *
  3913. * *
  3914. * WARNINGS: none *
  3915. * *
  3916. * HISTORY: *
  3917. * 07/04/1995 JLB : Created. *
  3918. *=============================================================================================*/
  3919. DirType BuildingClass::Fire_Direction(void) const
  3920. {
  3921. assert(Buildings.ID(this) == ID);
  3922. assert(IsActive);
  3923. if (Class->IsTurretEquipped) {
  3924. return(PrimaryFacing.Current());
  3925. }
  3926. return(Direction(TarCom));
  3927. }
  3928. /***********************************************************************************************
  3929. * BuildingClass::Remap_Table -- Fetches the remap table to use for this building. *
  3930. * *
  3931. * Use this routine to fetch the remap table to use. This override function is needed *
  3932. * because the default remap table for techno objects presumes the object is a unit. *
  3933. * Buildings aren't units. *
  3934. * *
  3935. * INPUT: none *
  3936. * *
  3937. * OUTPUT: Returns with the proper remap table to use for this building. *
  3938. * *
  3939. * WARNINGS: none *
  3940. * *
  3941. * HISTORY: *
  3942. * 07/08/1995 JLB : Created. *
  3943. *=============================================================================================*/
  3944. void const * BuildingClass::Remap_Table(void)
  3945. {
  3946. assert(Buildings.ID(this) == ID);
  3947. assert(IsActive);
  3948. return(House->Remap_Table(IsBlushing, Class->Remap));
  3949. }
  3950. /***********************************************************************************************
  3951. * BuildingClass::Mission_Unload -- Handles the unload mission for a building. *
  3952. * *
  3953. * This is the unload mission for a building. This really only applies to the weapon's *
  3954. * factory, since it needs the sophistication of an unload mission due to the door *
  3955. * animation. *
  3956. * *
  3957. * INPUT: none *
  3958. * *
  3959. * OUTPUT: Returns with the number of game frames to delay before calling this routine *
  3960. * again. *
  3961. * *
  3962. * WARNINGS: none *
  3963. * *
  3964. * HISTORY: *
  3965. * 07/29/1995 JLB : Created. *
  3966. *=============================================================================================*/
  3967. int BuildingClass::Mission_Unload(void)
  3968. {
  3969. assert(Buildings.ID(this) == ID);
  3970. assert(IsActive);
  3971. if (*this == STRUCT_WEAP) {
  3972. CELL cell = Coord_Cell(Coord) + Class->ExitList[0];
  3973. COORDINATE coord = Cell_Coord(cell);
  3974. CellClass * cellptr = &Map[cell];
  3975. enum {
  3976. INITIAL,
  3977. CLEAR_BIB,
  3978. OPEN,
  3979. LEAVE,
  3980. CLOSE
  3981. };
  3982. enum {
  3983. DOOR_STAGES = 5,
  3984. DOOR_RATE = 8
  3985. };
  3986. UnitClass * unit;
  3987. switch (Status) {
  3988. /*
  3989. ** Start the door opening.
  3990. */
  3991. case INITIAL:
  3992. // if (cellptr->Cell_Techno()) {
  3993. // cellptr->Incoming(0, true);
  3994. // }
  3995. unit = (UnitClass *)Contact_With_Whom();
  3996. if (unit) {
  3997. unit->Assign_Mission(MISSION_GUARD);
  3998. unit->Commence();
  3999. }
  4000. Open_Door(DOOR_RATE, DOOR_STAGES);
  4001. Status = CLEAR_BIB;
  4002. break;
  4003. /*
  4004. ** Now that the occupants can peek out the door, they will tell
  4005. ** everyone that could be blocking the way, that they should
  4006. ** scatter away.
  4007. */
  4008. case CLEAR_BIB:
  4009. if (cellptr->Cell_Techno()) {
  4010. cellptr->Incoming(0, true, true);
  4011. /*
  4012. ** Scatter everything around the weapon's factory door.
  4013. */
  4014. for (FacingType f = FACING_FIRST; f < FACING_COUNT; f++) {
  4015. CellClass * cptr = &cellptr->Adjacent_Cell(f);
  4016. if (cptr->Cell_Building() == NULL) {
  4017. cptr->Incoming(coord, true, true);
  4018. }
  4019. }
  4020. } else {
  4021. Status = OPEN;
  4022. }
  4023. break;
  4024. /*
  4025. ** When the door is finally open and the way is clear, tell the
  4026. ** unit to drive out.
  4027. */
  4028. case OPEN:
  4029. if (Is_Door_Open()) {
  4030. unit = (UnitClass *)Contact_With_Whom();
  4031. if (unit) {
  4032. unit->Assign_Mission(MISSION_MOVE);
  4033. if (House->IQ >= Rule.IQGuardArea) {
  4034. unit->Assign_Mission(MISSION_GUARD_AREA);
  4035. unit->ArchiveTarget = ::As_Target(House->Where_To_Go(unit));
  4036. }
  4037. unit->Force_Track(DriveClass::OUT_OF_WEAPON_FACTORY, coord);
  4038. // unit->Force_Track(DriveClass::OUT_OF_WEAPON_FACTORY, Adjacent_Cell(Adjacent_Cell(Center_Coord(), FACING_S), FACING_S));
  4039. unit->Set_Speed(128);
  4040. Status = LEAVE;
  4041. } else {
  4042. Close_Door(DOOR_RATE, DOOR_STAGES);
  4043. Status = CLOSE;
  4044. }
  4045. }
  4046. break;
  4047. /*
  4048. ** Wait until the unit has completely left the building.
  4049. */
  4050. case LEAVE:
  4051. if (!IsTethered) {
  4052. Close_Door(DOOR_RATE, DOOR_STAGES);
  4053. Status = CLOSE;
  4054. } else {
  4055. // if (In_Radio_Contact() && !((FootClass *)Contact_With_Whom())->IsDriving) {
  4056. // Transmit_Message(RADIO_OVER_OUT);
  4057. // }
  4058. }
  4059. break;
  4060. /*
  4061. ** Wait while the door closes.
  4062. */
  4063. case CLOSE:
  4064. if (Is_Door_Closed()) {
  4065. Enter_Idle_Mode();
  4066. }
  4067. break;
  4068. default:
  4069. break;
  4070. }
  4071. return(MissionControl[Mission].Normal_Delay() + Random_Pick(0, 2));
  4072. }
  4073. Assign_Mission(MISSION_GUARD);
  4074. return(1);
  4075. }
  4076. /***********************************************************************************************
  4077. * BuildingClass::Power_Output -- Fetches the current power output from this building. *
  4078. * *
  4079. * This routine will return the current power output for this building. The power output *
  4080. * is adjusted according to the damage level of the building. *
  4081. * *
  4082. * INPUT: none *
  4083. * *
  4084. * OUTPUT: Returns the current power output for this building. *
  4085. * *
  4086. * WARNINGS: none *
  4087. * *
  4088. * HISTORY: *
  4089. * 07/29/1995 JLB : Created. *
  4090. *=============================================================================================*/
  4091. int BuildingClass::Power_Output(void) const
  4092. {
  4093. assert(Buildings.ID(this) == ID);
  4094. assert(IsActive);
  4095. if (Class->Power) {
  4096. return(Class->Power * fixed(LastStrength, Class->MaxStrength));
  4097. }
  4098. return(0);
  4099. }
  4100. /***********************************************************************************************
  4101. * BuildingClass::Detach -- Handles target removal from the game system. *
  4102. * *
  4103. * This routine is called when the specified target is about to be removed from the game *
  4104. * system. *
  4105. * *
  4106. * INPUT: target -- The target to be removed from this building's targeting computer. *
  4107. * *
  4108. * all -- Is the target about to be completely eliminated? *
  4109. * *
  4110. * OUTPUT: none *
  4111. * *
  4112. * WARNINGS: none *
  4113. * *
  4114. * HISTORY: *
  4115. * 07/29/1995 JLB : Created. *
  4116. *=============================================================================================*/
  4117. void BuildingClass::Detach(TARGET target, bool all)
  4118. {
  4119. assert(Buildings.ID(this) == ID);
  4120. assert(IsActive);
  4121. TechnoClass::Detach(target, all);
  4122. if (target == WhomToRepay) {
  4123. WhomToRepay = TARGET_NONE;
  4124. }
  4125. if (target == AnimToTrack) {
  4126. AnimToTrack = TARGET_NONE;
  4127. }
  4128. }
  4129. /***********************************************************************************************
  4130. * BuildingClass::Crew_Type -- This determines the crew that this object generates. *
  4131. * *
  4132. * When selling very cheap buildings (such as the silo), a technician will pop out since *
  4133. * generating minigunners would be overkill -- the player could use this loophole to *
  4134. * gain an advantage. *
  4135. * *
  4136. * INPUT: none *
  4137. * *
  4138. * OUTPUT: Returns the infantry type that this building will generate as a survivor. *
  4139. * *
  4140. * WARNINGS: none *
  4141. * *
  4142. * HISTORY: *
  4143. * 08/05/1995 JLB : Created. *
  4144. *=============================================================================================*/
  4145. InfantryType BuildingClass::Crew_Type(void) const
  4146. {
  4147. assert(Buildings.ID(this) == ID);
  4148. assert(IsActive);
  4149. switch (Class->Type) {
  4150. case STRUCT_STORAGE:
  4151. if (Percent_Chance(50)) {
  4152. return(INFANTRY_C1);
  4153. } else {
  4154. return(INFANTRY_C7);
  4155. }
  4156. case STRUCT_CONST:
  4157. if (!IsCaptured && House->IsHuman && Percent_Chance(25)) {
  4158. return(INFANTRY_RENOVATOR);
  4159. }
  4160. break;
  4161. case STRUCT_KENNEL:
  4162. if (Percent_Chance(50)) {
  4163. return(INFANTRY_DOG);
  4164. } else {
  4165. return(INFANTRY_NONE);
  4166. }
  4167. case STRUCT_TENT:
  4168. case STRUCT_BARRACKS:
  4169. return(INFANTRY_E1);
  4170. default:
  4171. break;
  4172. }
  4173. return(TechnoClass::Crew_Type());
  4174. }
  4175. /***********************************************************************************************
  4176. * BuildingClass::Detach_All -- Possibly abandons production according to factory type. *
  4177. * *
  4178. * When this routine is called, it indicates that the building is about to be destroyed *
  4179. * or captured. In such a case any production it may be doing, must be abandoned. *
  4180. * *
  4181. * INPUT: all -- Is the object about the be completely destroyed? *
  4182. * *
  4183. * OUTPUT: none *
  4184. * *
  4185. * WARNINGS: none *
  4186. * *
  4187. * HISTORY: *
  4188. * 08/05/1995 JLB : Created. *
  4189. *=============================================================================================*/
  4190. void BuildingClass::Detach_All(bool all)
  4191. {
  4192. assert(Buildings.ID(this) == ID);
  4193. assert(IsActive);
  4194. /*
  4195. ** If it is producing something, then it must be abandoned.
  4196. */
  4197. if (Factory) {
  4198. Factory->Abandon();
  4199. delete (FactoryClass *)Factory;
  4200. Factory = 0;
  4201. }
  4202. /*
  4203. ** If the owner HouseClass is building something, and this building can
  4204. ** build that thing, we may be the last building for that house that can
  4205. ** build that thing; if so, abandon production of it.
  4206. */
  4207. if (House) {
  4208. FactoryClass * factory = House->Fetch_Factory(Class->ToBuild);
  4209. /*
  4210. ** If a factory was found, then temporarily disable this building and then
  4211. ** determine if any object that is being produced can still be produced. If
  4212. ** not, then the object being produced must be abandoned.
  4213. */
  4214. if (factory) {
  4215. TechnoClass * object = factory->Get_Object();
  4216. IsInLimbo = true;
  4217. if (object && !object->Techno_Type_Class()->Who_Can_Build_Me(true, false, House->Class->House)) {
  4218. House->Abandon_Production(Class->ToBuild);
  4219. }
  4220. IsInLimbo = false;
  4221. }
  4222. }
  4223. TechnoClass::Detach_All(all);
  4224. }
  4225. /***********************************************************************************************
  4226. * BuildingClass::Flush_For_Placement -- Handles clearing a zone for object placement. *
  4227. * *
  4228. * This routine is used to clear the way for placement of the specified object (usually *
  4229. * a building). If there are friendly units blocking the placement area, they are told *
  4230. * to scatter. Enemy blocking units are attacked. *
  4231. * *
  4232. * INPUT: techno -- Pointer to the object that is desired to be placed. *
  4233. * *
  4234. * cell -- The cell that placement wants to occur at. *
  4235. * *
  4236. * OUTPUT: none *
  4237. * *
  4238. * WARNINGS: none *
  4239. * *
  4240. * HISTORY: *
  4241. * 08/06/1995 JLB : Created. *
  4242. * 09/27/1995 JLB : Revised to use type class function. *
  4243. *=============================================================================================*/
  4244. bool BuildingClass::Flush_For_Placement(TechnoClass * techno, CELL cell)
  4245. {
  4246. assert(Buildings.ID(this) == ID);
  4247. assert(IsActive);
  4248. if (techno) {
  4249. return (((BuildingTypeClass const &)techno->Class_Of()).Flush_For_Placement(cell, House));
  4250. }
  4251. return(false);
  4252. }
  4253. /***********************************************************************************************
  4254. * BuildingClass::Find_Exit_Cell -- Find a clear location to exit an object from this building *
  4255. * *
  4256. * This routine is called when the building needs to discharge a unit. It will find a *
  4257. * nearby (adjacent) cell that is clear enough for the specified object to enter. Typical *
  4258. * use of this routine is when the airfield disgorges its cargo. *
  4259. * *
  4260. * INPUT: techno -- Pointer to the object that wishes to exit this building. *
  4261. * *
  4262. * OUTPUT: Returns with the cell number to use for object placement. If no free location *
  4263. * could be found, then zero (0) is returned. *
  4264. * *
  4265. * WARNINGS: none *
  4266. * *
  4267. * HISTORY: *
  4268. * 09/21/1995 JLB : Created. *
  4269. * 02/20/1996 JLB : Added default case for exit cell calculation. *
  4270. *=============================================================================================*/
  4271. CELL BuildingClass::Find_Exit_Cell(TechnoClass const * techno) const
  4272. {
  4273. assert(Buildings.ID(this) == ID);
  4274. assert(IsActive);
  4275. CELL const * ptr;
  4276. CELL origin = Coord_Cell(Coord);
  4277. ptr = Class->ExitList;
  4278. if (ptr != NULL) {
  4279. while (*ptr != REFRESH_EOL) {
  4280. CELL cell = origin + *ptr++;
  4281. if (Map.In_Radar(cell) && techno->Can_Enter_Cell(cell) == MOVE_OK) {
  4282. return(cell);
  4283. }
  4284. }
  4285. } else {
  4286. int x1, x2;
  4287. int y1, y2;
  4288. CELL cell;
  4289. y1 = -1;
  4290. y2 = Class->Height();
  4291. for (x1 = -1; x1 <= Class->Width(); x1++) {
  4292. cell = origin + x1 + (y1 * MAP_CELL_W);
  4293. if (Map.In_Radar(cell) && techno->Can_Enter_Cell(cell) == MOVE_OK) {
  4294. return(cell);
  4295. }
  4296. cell = origin + x1 + (y2 * MAP_CELL_W);
  4297. if (Map.In_Radar(cell) && techno->Can_Enter_Cell(cell) == MOVE_OK) {
  4298. return(cell);
  4299. }
  4300. }
  4301. x1 = -1;
  4302. x2 = Class->Width();
  4303. for (y1 = -1; y1 <= Class->Height(); y1++) {
  4304. cell = origin + (y1 * MAP_CELL_W) + x1;
  4305. if (Map.In_Radar(cell) && techno->Can_Enter_Cell(cell) == MOVE_OK) {
  4306. return(cell);
  4307. }
  4308. cell = origin + (y1 * MAP_CELL_W) + x2;
  4309. if (Map.In_Radar(cell) && techno->Can_Enter_Cell(cell) == MOVE_OK) {
  4310. return(cell);
  4311. }
  4312. }
  4313. }
  4314. return(0);
  4315. }
  4316. /***********************************************************************************************
  4317. * BuildingClass::Can_Player_Move -- Can this building be moved? *
  4318. * *
  4319. * This routine answers the question 'can this building be moved?' Typically, only the *
  4320. * construction yard can be moved and it does this by undeploying back into a MCV. *
  4321. * *
  4322. * INPUT: none *
  4323. * *
  4324. * OUTPUT: Can the building move to a new location under player control? *
  4325. * *
  4326. * WARNINGS: none *
  4327. * *
  4328. * HISTORY: *
  4329. * 10/04/1995 JLB : Created. *
  4330. *=============================================================================================*/
  4331. bool BuildingClass::Can_Player_Move(void) const
  4332. {
  4333. assert(Buildings.ID(this) == ID);
  4334. assert(IsActive);
  4335. return(*this == STRUCT_CONST);
  4336. }
  4337. /***********************************************************************************************
  4338. * BuildingClass::Exit_Coord -- Determines location where object will leave it. *
  4339. * *
  4340. * This routine will return the coordinate where an object that wishes to leave the *
  4341. * building will exit at. *
  4342. * *
  4343. * INPUT: none *
  4344. * *
  4345. * OUTPUT: Returns with the coordinate that the object should be created at. *
  4346. * *
  4347. * WARNINGS: none *
  4348. * *
  4349. * HISTORY: *
  4350. * 02/20/1996 JLB : Created. *
  4351. *=============================================================================================*/
  4352. COORDINATE BuildingClass::Exit_Coord(void) const
  4353. {
  4354. assert(Buildings.ID(this) == ID);
  4355. assert(IsActive);
  4356. if (Class->ExitCoordinate) {
  4357. return(Coord_Add(Coord, Class->ExitCoordinate));
  4358. }
  4359. return(TechnoClass::Exit_Coord());
  4360. }
  4361. /***********************************************************************************************
  4362. * BuildingClass::Check_Point -- Fetches the landing checkpoint for the given flight pattern. *
  4363. * *
  4364. * Use this routine to coordinate a landing operation. The specified checkpoint is *
  4365. * converted into a cell number. The landing aircraft should fly over that cell and then *
  4366. * request the next check point. *
  4367. * *
  4368. * INPUT: cp -- The check point to convert to a cell number. *
  4369. * *
  4370. * OUTPUT: Returns with the cell that the aircraft should fly over in order to complete *
  4371. * that portion of the landing pattern. *
  4372. * *
  4373. * WARNINGS: none *
  4374. * *
  4375. * HISTORY: *
  4376. * 07/06/1996 JLB : Created. *
  4377. *=============================================================================================*/
  4378. CELL BuildingClass::Check_Point(CheckPointType cp) const
  4379. {
  4380. CELL xoffset = 6; // Downwind offset.
  4381. CELL yoffset = 5; // Crosswind offset.
  4382. CELL cell = Coord_Cell(Center_Coord());
  4383. switch (cp) {
  4384. case CHECK_STACK:
  4385. xoffset = 0;
  4386. break;
  4387. case CHECK_CROSSWIND:
  4388. yoffset = 0;
  4389. break;
  4390. case CHECK_DOWNWIND:
  4391. default:
  4392. break;
  4393. }
  4394. if ((Cell_X(cell) - Map.MapCellX) > Map.MapCellWidth/2) {
  4395. xoffset = -xoffset;
  4396. }
  4397. if ((Cell_Y(cell) - Map.MapCellY) > Map.MapCellHeight/2) {
  4398. yoffset = -yoffset;
  4399. }
  4400. return(XY_Cell(Cell_X(cell)+xoffset, Cell_Y(cell)+yoffset));
  4401. }
  4402. /***********************************************************************************************
  4403. * BuildingClass::Update_Radar_Spied - set house's RadarSpied field appropriately. *
  4404. * *
  4405. * This routine is called when a radar facility is captured or destroyed. It fills in the *
  4406. * RadarSpied field of the house based on whether there's a spied-upon radar facility or not*
  4407. * *
  4408. * INPUT: none *
  4409. * *
  4410. * OUTPUT: House->RadarSpied field gets set appropriately. *
  4411. * *
  4412. * WARNINGS: none *
  4413. * *
  4414. * HISTORY: *
  4415. * 03/22/1996 BWG : Created. *
  4416. *=============================================================================================*/
  4417. void BuildingClass::Update_Radar_Spied(void)
  4418. {
  4419. House->RadarSpied = 0;
  4420. for (int index = 0; index < Buildings.Count(); index++) {
  4421. BuildingClass * obj = Buildings.Ptr(index);
  4422. if (obj && !obj->IsInLimbo && obj->House == House) {
  4423. if (*obj == STRUCT_RADAR /* || *obj == STRUCT_EYE */) {
  4424. House->RadarSpied |= obj->SpiedBy;
  4425. }
  4426. }
  4427. }
  4428. Map.RadarClass::Flag_To_Redraw(true);
  4429. }
  4430. /***********************************************************************************************
  4431. * BuildingClass::Read_INI -- Reads buildings from INI file. *
  4432. * *
  4433. * This is the basic scenario initialization of building function. It *
  4434. * is called when reading the scenario startup INI file and it handles *
  4435. * creation of all specified buildings. *
  4436. * *
  4437. * INI entry format: *
  4438. * Housename, Typename, Strength, Cell, Facing, Triggername *
  4439. * *
  4440. * INPUT: buffer -- Pointer to the loaded INI file data. *
  4441. * *
  4442. * OUTPUT: none *
  4443. * *
  4444. * WARNINGS: none *
  4445. * *
  4446. * HISTORY: *
  4447. * 05/24/1994 JLB : Created. *
  4448. *=============================================================================================*/
  4449. void BuildingClass::Read_INI(CCINIClass & ini)
  4450. {
  4451. BuildingClass * b; // Working unit pointer.
  4452. HousesType bhouse; // Building house.
  4453. StructType classid; // Building type.
  4454. CELL cell; // Cell of building.
  4455. char buf[128];
  4456. char * trigname; // building's trigger's name
  4457. int len = ini.Entry_Count(INI_Name());
  4458. for (int index = 0; index < len; index++) {
  4459. char const * entry = ini.Get_Entry(INI_Name(), index);
  4460. /*
  4461. ** Get a building entry.
  4462. */
  4463. ini.Get_String(INI_Name(), entry, NULL, buf, sizeof(buf));
  4464. /*
  4465. ** 1st token: house name.
  4466. */
  4467. bhouse = HouseTypeClass::From_Name(strtok(buf, ","));
  4468. /*
  4469. ** 2nd token: building name.
  4470. */
  4471. classid = BuildingTypeClass::From_Name(strtok(NULL, ","));
  4472. if (bhouse != HOUSE_NONE && classid != STRUCT_NONE) {
  4473. int strength;
  4474. DirType facing;
  4475. /*
  4476. ** 3rd token: strength.
  4477. */
  4478. strength = atoi(strtok(NULL, ","));
  4479. /*
  4480. ** 4th token: cell #.
  4481. */
  4482. cell = atoi(strtok(NULL, ","));
  4483. /*
  4484. ** 5th token: facing.
  4485. */
  4486. facing = (DirType)atoi(strtok(NULL, ","));
  4487. /*
  4488. ** 6th token: triggername (can be NULL).
  4489. */
  4490. trigname = strtok(NULL, ",");
  4491. bool sellable = false;
  4492. char * token_pointer = strtok(NULL, ",");
  4493. if (token_pointer) {
  4494. sellable = atoi(token_pointer);
  4495. }
  4496. bool rebuild = false;
  4497. token_pointer = strtok(NULL, ",");
  4498. if (token_pointer) {
  4499. rebuild = atoi(token_pointer);
  4500. }
  4501. b = new BuildingClass(classid, bhouse);
  4502. if (b) {
  4503. TriggerTypeClass * tp = TriggerTypeClass::From_Name(trigname);
  4504. if (tp) {
  4505. TriggerClass * tt = Find_Or_Make(tp);
  4506. if (tt) {
  4507. tt->AttachCount++;
  4508. b->Trigger = tt;
  4509. }
  4510. }
  4511. b->IsAllowedToSell = sellable;
  4512. b->IsToRebuild = rebuild;
  4513. b->IsToRepair = rebuild || *b == STRUCT_CONST;
  4514. if (b->Unlimbo(Cell_Coord(cell), facing)) {
  4515. strength = min(strength, 0x100);
  4516. strength = b->Class->MaxStrength * fixed(strength, 256);
  4517. b->Strength = strength;
  4518. if (b->Strength > b->Class->MaxStrength-3) b->Strength = b->Class->MaxStrength;
  4519. b->IsALemon = false;
  4520. } else {
  4521. /*
  4522. ** If the building could not be unlimboed on the map, then this indicates
  4523. ** a serious error. Delete the building.
  4524. */
  4525. delete b;
  4526. }
  4527. }
  4528. }
  4529. }
  4530. }
  4531. /***********************************************************************************************
  4532. * BuildingClass::Write_INI -- Write out the building data to the INI file specified. *
  4533. * *
  4534. * This will store the building data (as it relates to scenario initialization) to the *
  4535. * INI database specified. *
  4536. * *
  4537. * INPUT: ini -- Reference to the INI database that the building data will be stored to. *
  4538. * *
  4539. * OUTPUT: none *
  4540. * *
  4541. * WARNINGS: none *
  4542. * *
  4543. * HISTORY: *
  4544. * 07/06/1996 JLB : Created. *
  4545. *=============================================================================================*/
  4546. void BuildingClass::Write_INI(CCINIClass & ini)
  4547. {
  4548. /*
  4549. ** First, clear out all existing building data from the ini file.
  4550. */
  4551. ini.Clear(INI_Name());
  4552. /*
  4553. ** Write the data out.
  4554. */
  4555. for (int index = 0; index < Buildings.Count(); index++) {
  4556. BuildingClass * building = Buildings.Ptr(index);
  4557. if (!building->IsInLimbo) {
  4558. char uname[10];
  4559. char buf[127];
  4560. sprintf(uname, "%d", index);
  4561. sprintf(buf, "%s,%s,%d,%u,%d,%s,%d,%d",
  4562. building->House->Class->IniName,
  4563. building->Class->IniName,
  4564. building->Health_Ratio()*256,
  4565. Coord_Cell(building->Coord),
  4566. building->PrimaryFacing.Current(),
  4567. building->Trigger.Is_Valid() ? building->Trigger->Class->IniName : "None",
  4568. building->IsAllowedToSell,
  4569. building->IsToRebuild
  4570. );
  4571. ini.Put_String(INI_Name(), uname, buf);
  4572. }
  4573. }
  4574. }
  4575. /***********************************************************************************************
  4576. * BuildingClass::Target_Coord -- Return the coordinate to use when firing on this building. *
  4577. * *
  4578. * This routine will determine the "center" location of this building for purposes of *
  4579. * targeting. Usually, this location is somewhere near the foundation of the building. *
  4580. * *
  4581. * INPUT: none *
  4582. * *
  4583. * OUTPUT: Returns with the coordinate to use when firing upon this building (or trying to *
  4584. * walk onto it). *
  4585. * *
  4586. * WARNINGS: none *
  4587. * *
  4588. * HISTORY: *
  4589. * 07/19/1996 JLB : Created. *
  4590. *=============================================================================================*/
  4591. COORDINATE BuildingClass::Target_Coord(void) const
  4592. {
  4593. COORDINATE coord = Center_Coord();
  4594. if (Class->FoundationFace != FACING_NONE) {
  4595. return(Adjacent_Cell(coord, Class->FoundationFace));
  4596. }
  4597. return(coord);
  4598. }
  4599. /***********************************************************************************************
  4600. * BuildingClass::Factory_AI -- Handle factory production and initiation. *
  4601. * *
  4602. * Some building (notably the computer controlled ones) can have a factory object attached. *
  4603. * This routine handles processing of that factory and also detecting when production *
  4604. * should begin in order to initiate production. *
  4605. * *
  4606. * INPUT: none *
  4607. * *
  4608. * OUTPUT: none *
  4609. * *
  4610. * WARNINGS: Only call this routine once per building per game logic loop. *
  4611. * *
  4612. * HISTORY: *
  4613. * 07/29/1996 JLB : Created. *
  4614. *=============================================================================================*/
  4615. void BuildingClass::Factory_AI(void)
  4616. {
  4617. /*
  4618. ** Handle any production tied to this building. Only computer controlled buildings have
  4619. ** production attached to the building itself. The player uses the sidebar interface for
  4620. ** all production control.
  4621. */
  4622. if (Factory.Is_Valid() && Factory->Has_Completed() && PlacementDelay == 0) {
  4623. TechnoClass * product = Factory->Get_Object();
  4624. // FactoryClass * fact = Factory;
  4625. switch (Exit_Object(product)) {
  4626. /*
  4627. ** If the object could not leave the factory, then either request
  4628. ** a transport, place the (what must be a) building using another method, or
  4629. ** abort the production and refund money.
  4630. */
  4631. case 0:
  4632. Factory->Abandon();
  4633. delete (FactoryClass *)Factory;
  4634. Factory = 0;
  4635. break;
  4636. /*
  4637. ** Exiting this building is prevented by some temporary blockage. Wait
  4638. ** a bit before trying again.
  4639. */
  4640. case 1:
  4641. PlacementDelay = TICKS_PER_SECOND*3;
  4642. break;
  4643. /*
  4644. ** The object was successfully sent from this factory. Inform the house
  4645. ** tracking logic that the requested object has been produced.
  4646. */
  4647. case 2:
  4648. switch (product->What_Am_I()) {
  4649. case RTTI_VESSEL:
  4650. House->JustBuiltVessel = ((VesselClass*)product)->Class->Type;
  4651. House->IsBuiltSomething = true;
  4652. break;
  4653. case RTTI_UNIT:
  4654. House->JustBuiltUnit = ((UnitClass*)product)->Class->Type;
  4655. House->IsBuiltSomething = true;
  4656. break;
  4657. case RTTI_INFANTRY:
  4658. House->JustBuiltInfantry = ((InfantryClass*)product)->Class->Type;
  4659. House->IsBuiltSomething = true;
  4660. break;
  4661. case RTTI_BUILDING:
  4662. House->JustBuiltStructure = ((BuildingClass*)product)->Class->Type;
  4663. House->IsBuiltSomething = true;
  4664. break;
  4665. case RTTI_AIRCRAFT:
  4666. House->JustBuiltAircraft = ((AircraftClass*)product)->Class->Type;
  4667. House->IsBuiltSomething = true;
  4668. break;
  4669. default:
  4670. break;
  4671. }
  4672. // fact->Completed();
  4673. Factory->Completed();
  4674. // delete fact;
  4675. delete (FactoryClass *)Factory;
  4676. Factory = 0;
  4677. break;
  4678. default:
  4679. break;
  4680. }
  4681. }
  4682. /*
  4683. ** Pick something to create for this factory.
  4684. */
  4685. if (House->IsStarted && Mission != MISSION_CONSTRUCTION && Mission != MISSION_DECONSTRUCTION) {
  4686. /*
  4687. ** Buildings that produce other objects have special factory logic handled here.
  4688. */
  4689. if (Class->ToBuild != RTTI_NONE) {
  4690. if (Factory.Is_Valid()) {
  4691. /*
  4692. ** If production has halted, then just abort production and make the
  4693. ** funds available for something else.
  4694. */
  4695. if (PlacementDelay == 0 && !Factory->Is_Building()) {
  4696. Factory->Abandon();
  4697. delete (FactoryClass *)Factory;
  4698. Factory = 0;
  4699. }
  4700. } else {
  4701. /*
  4702. ** Only look to start production if there is at least a small amount of
  4703. ** money available. In cases where there is no practical money left, then
  4704. ** production can never complete -- don't bother starting it.
  4705. */
  4706. if (House->IsStarted && House->Available_Money() > 10) {
  4707. TechnoTypeClass const * techno = House->Suggest_New_Object(Class->ToBuild, *this == STRUCT_KENNEL);
  4708. /*
  4709. ** If a suitable object type was selected for production, then start
  4710. ** producing it now.
  4711. */
  4712. if (techno != NULL) {
  4713. Factory = new FactoryClass;
  4714. if (Factory.Is_Valid()) {
  4715. if (!Factory->Set(*techno, *House)) {
  4716. delete (FactoryClass *)Factory;
  4717. Factory = 0;
  4718. } else {
  4719. House->Production_Begun(Factory->Get_Object());
  4720. Factory->Start();
  4721. }
  4722. }
  4723. }
  4724. }
  4725. }
  4726. }
  4727. }
  4728. }
  4729. /***********************************************************************************************
  4730. * BuildingClass::Rotation_AI -- Process any turret rotation required of this building. *
  4731. * *
  4732. * Some buildings have a turret and this routine handles processing the turret rotation. *
  4733. * *
  4734. * INPUT: none *
  4735. * *
  4736. * OUTPUT: none *
  4737. * *
  4738. * WARNINGS: Only call this routine once per building per game logic loop. *
  4739. * *
  4740. * HISTORY: *
  4741. * 07/29/1996 JLB : Created. *
  4742. * 10/27/1996 JLB : Rotation does not occur if power and no power avail. *
  4743. *=============================================================================================*/
  4744. void BuildingClass::Rotation_AI(void)
  4745. {
  4746. if (Class->IsTurretEquipped &&
  4747. Mission != MISSION_CONSTRUCTION &&
  4748. Mission != MISSION_DECONSTRUCTION &&
  4749. (!Class->IsPowered || House->Power_Fraction() >= 1)) {
  4750. /*
  4751. ** Rotate turret to match desired facing.
  4752. */
  4753. if (PrimaryFacing.Is_Rotating()) {
  4754. if (PrimaryFacing.Rotation_Adjust(Class->ROT)) {
  4755. Mark(MARK_CHANGE);
  4756. }
  4757. }
  4758. }
  4759. }
  4760. /***********************************************************************************************
  4761. * BuildingClass::Charging_AI -- Handles the special charging logic for Tesla coils. *
  4762. * *
  4763. * This handles the special logic required of the charging tesla coil. It requires special *
  4764. * processing since its charge up is dependant upon the target and power surplus of the *
  4765. * owning house. *
  4766. * *
  4767. * INPUT: none *
  4768. * *
  4769. * OUTPUT: none *
  4770. * *
  4771. * WARNINGS: none *
  4772. * *
  4773. * HISTORY: *
  4774. * 07/29/1996 JLB : Created. *
  4775. *=============================================================================================*/
  4776. void BuildingClass::Charging_AI(void)
  4777. {
  4778. if (Class->PrimaryWeapon != NULL && Class->PrimaryWeapon->IsElectric && BState != BSTATE_CONSTRUCTION) {
  4779. if (Target_Legal(TarCom) && House->Power_Fraction() >= 1) {
  4780. if (!IsCharged) {
  4781. if (IsCharging) {
  4782. // if (stagechange) {
  4783. Mark(MARK_CHANGE);
  4784. if (Fetch_Stage() >= 9) {
  4785. IsCharged = true;
  4786. IsCharging = false;
  4787. Set_Rate(0);
  4788. }
  4789. // }
  4790. } else if (!Arm) {
  4791. IsCharged = false;
  4792. IsCharging = true;
  4793. Set_Stage(0);
  4794. Set_Rate(3);
  4795. Sound_Effect(VOC_TESLA_POWER_UP, Coord);
  4796. }
  4797. }
  4798. } else {
  4799. if (IsCharging || IsCharged) {
  4800. Mark(MARK_CHANGE);
  4801. IsCharging = false;
  4802. IsCharged = false;
  4803. Set_Stage(0);
  4804. Set_Rate(0);
  4805. }
  4806. }
  4807. }
  4808. }
  4809. /***********************************************************************************************
  4810. * BuildingClass::Repair_AI -- Handle the repair (and sell) logic for the building. *
  4811. * *
  4812. * This routine handle the repair animation and healing logic. It also detects when the *
  4813. * (computer controlled) building should begin repair or sell itself. *
  4814. * *
  4815. * INPUT: none *
  4816. * *
  4817. * OUTPUT: none *
  4818. * *
  4819. * WARNINGS: Only call this routine once per building per game logic loop. *
  4820. * *
  4821. * HISTORY: *
  4822. * 07/29/1996 JLB : Created. *
  4823. *=============================================================================================*/
  4824. void BuildingClass::Repair_AI(void)
  4825. {
  4826. if (House->IQ >= Rule.IQRepairSell && Mission != MISSION_CONSTRUCTION && Mission != MISSION_DECONSTRUCTION) {
  4827. /*
  4828. ** Possibly start repair process if the building is below half strength.
  4829. */
  4830. // unsigned ratio = MIN(House->Smartness, 0x00F0);
  4831. if (Can_Repair()) {
  4832. if (House->Available_Money() >= Rule.RepairThreshhold) {
  4833. if (!House->DidRepair) {
  4834. if (!IsRepairing && (IsCaptured || IsToRepair || House->IsHuman || Session.Type != GAME_NORMAL)) {
  4835. House->DidRepair = true; // flag that this house did its repair allocation for this frame
  4836. Repair(1);
  4837. if (!House->IsHuman) {
  4838. House->RepairTimer = Random_Pick((int)(House->RepairDelay * (TICKS_PER_MINUTE/4)), (int)(House->RepairDelay * TICKS_PER_MINUTE * 2));
  4839. }
  4840. }
  4841. }
  4842. } else {
  4843. if ((Session.Type != GAME_NORMAL || IsAllowedToSell) && IsTickedOff && House->Control.TechLevel >= Rule.IQSellBack && Random_Pick(0, 50) < House->Control.TechLevel && !Trigger.Is_Valid() && *this != STRUCT_CONST && Health_Ratio() < Rule.ConditionRed) {
  4844. Sell_Back(1);
  4845. }
  4846. }
  4847. }
  4848. }
  4849. /*
  4850. ** If it is repairing, then apply any repair effects as necessary.
  4851. */
  4852. if (IsRepairing && (Frame % (Rule.RepairRate * TICKS_PER_MINUTE)) == 0) {
  4853. IsWrenchVisible = (IsWrenchVisible == false);
  4854. Mark(MARK_CHANGE);
  4855. int cost = Class->Repair_Cost();
  4856. int step = Class->Repair_Step();
  4857. /*
  4858. ** Check for and expend any necessary monies to continue the repair.
  4859. */
  4860. if (House->Available_Money() >= cost) {
  4861. House->Spend_Money(cost);
  4862. Strength += step;
  4863. if (Strength >= Class->MaxStrength) {
  4864. Strength = Class->MaxStrength;
  4865. IsRepairing = false;
  4866. }
  4867. } else {
  4868. IsRepairing = false;
  4869. }
  4870. }
  4871. }
  4872. /***********************************************************************************************
  4873. * BuildingClass::Animation_AI -- Handles normal building animation processing. *
  4874. * *
  4875. * This will process the general building animation mechanism. It detects when the *
  4876. * building animation sequence has completed and flags the building to perform mission *
  4877. * changes as a result. *
  4878. * *
  4879. * INPUT: none *
  4880. * *
  4881. * OUTPUT: none *
  4882. * *
  4883. * WARNINGS: Call this routine only once per building per game logic loop. *
  4884. * *
  4885. * HISTORY: *
  4886. * 07/29/1996 JLB : Created. *
  4887. *=============================================================================================*/
  4888. void BuildingClass::Animation_AI(void)
  4889. {
  4890. bool stagechange = Graphic_Logic();
  4891. bool toloop = false;
  4892. /*
  4893. ** Always refresh the SAM site if it has an animation change.
  4894. */
  4895. if (*this == STRUCT_SAM && stagechange) Mark(MARK_CHANGE);
  4896. if ((!Class->IsTurretEquipped && *this != STRUCT_TESLA) || Mission == MISSION_CONSTRUCTION || Mission == MISSION_DECONSTRUCTION) {
  4897. if (stagechange) {
  4898. /*
  4899. ** Check for animation end or if special case of MCV deconstructing when it is allowed
  4900. ** to convert back into an MCV.
  4901. */
  4902. BuildingTypeClass::AnimControlType const * ctrl = Fetch_Anim_Control();
  4903. /*
  4904. ** When the last frame of the current animation sequence is reached, flag that
  4905. ** a new mission may be started. This must occur before the animation actually
  4906. ** loops so that if a mission change does occur, it will have a chance to change
  4907. ** the building graphic before the last frame is replaced by the first frame of
  4908. ** the loop.
  4909. */
  4910. if (Fetch_Stage() == ctrl->Start+ctrl->Count-1 || (!Target_Legal(ArchiveTarget) /*Special.IsMCVDeploy*/ && *this == STRUCT_CONST && Mission == MISSION_DECONSTRUCTION && Fetch_Stage() == (42-19))) {
  4911. IsReadyToCommence = true;
  4912. }
  4913. /*
  4914. ** If the animation advances beyond the last frame, then start the animation
  4915. ** sequence over from the beginning.
  4916. */
  4917. if (Fetch_Stage() >= ctrl->Start+ctrl->Count) {
  4918. toloop = true;
  4919. }
  4920. Mark(MARK_CHANGE);
  4921. } else {
  4922. if (BState == BSTATE_NONE || Fetch_Rate() == 0) {
  4923. IsReadyToCommence = true;
  4924. }
  4925. }
  4926. }
  4927. /*
  4928. ** If there is a door that is animating, then it might cause this building
  4929. ** to be redrawn. Check for and flag to redraw as necessary.
  4930. */
  4931. if (Time_To_Redraw()) {
  4932. Clear_Redraw_Flag();
  4933. Mark(MARK_CHANGE);
  4934. }
  4935. /*
  4936. ** The animation sequence has looped. Restart it and flag this loop condition.
  4937. ** This is used to tell the mission system that the animation has completed. It
  4938. ** also signals that now is a good time to act on any pending mission.
  4939. */
  4940. if (toloop) {
  4941. BuildingTypeClass::AnimControlType const * ctrl = Fetch_Anim_Control();
  4942. if (BState == BSTATE_CONSTRUCTION || BState == BSTATE_IDLE) {
  4943. Set_Rate(Options.Normalize_Delay(ctrl->Rate));
  4944. } else {
  4945. Set_Rate(ctrl->Rate);
  4946. }
  4947. Set_Stage(ctrl->Start);
  4948. Mark(MARK_CHANGE);
  4949. }
  4950. }
  4951. /***********************************************************************************************
  4952. * BuildingClass::How_Many_Survivors -- This determine the maximum number of survivors. *
  4953. * *
  4954. * This routine is called to determine how many survivors should run from this building *
  4955. * when it is either sold or destroyed. Buildings that are captured have fewer survivors. *
  4956. * The number of survivors is a portion of the cost of the building divided by the cost *
  4957. * of a minigunner. *
  4958. * *
  4959. * INPUT: none *
  4960. * *
  4961. * OUTPUT: Returns with the number of soldiers that should run from this building. *
  4962. * *
  4963. * WARNINGS: none *
  4964. * *
  4965. * HISTORY: *
  4966. * 08/04/1996 JLB : Created. *
  4967. *=============================================================================================*/
  4968. int BuildingClass::How_Many_Survivors(void) const
  4969. {
  4970. if (IsSurvivorless || !Class->IsCrew) return(0);
  4971. int divisor = InfantryTypeClass::As_Reference(INFANTRY_E1).Raw_Cost();
  4972. if (divisor == 0) return(0);
  4973. if (IsCaptured) divisor *= 2;
  4974. int count = (Class->Raw_Cost() * Rule.SurvivorFraction) / divisor;
  4975. return(Bound(count, 1, 5));
  4976. }
  4977. /***********************************************************************************************
  4978. * BuildingClass::Get_Image_Data -- Fetch the image pointer for the building. *
  4979. * *
  4980. * This routine will return with a pointer to the shape data for the building. The shape *
  4981. * data is different than normal when the building is undergoing construction and *
  4982. * disassembly. *
  4983. * *
  4984. * INPUT: none *
  4985. * *
  4986. * OUTPUT: Returns with a pointer to the shape data for this building. *
  4987. * *
  4988. * WARNINGS: none *
  4989. * *
  4990. * HISTORY: *
  4991. * 08/06/1996 JLB : Created. *
  4992. *=============================================================================================*/
  4993. void const * BuildingClass::Get_Image_Data(void) const
  4994. {
  4995. if (BState == BSTATE_CONSTRUCTION) {
  4996. return(Class->Get_Buildup_Data());
  4997. }
  4998. return(TechnoClass::Get_Image_Data());
  4999. }
  5000. /***********************************************************************************************
  5001. * BuildingClass::Value -- Determine the value of this building. *
  5002. * *
  5003. * The value of the building is normally just its ordinary assigned value. However, in the *
  5004. * case of fakes, the value is artificially enhanced to match the structure that is *
  5005. * being faked. *
  5006. * *
  5007. * INPUT: none *
  5008. * *
  5009. * OUTPUT: Returns with the point value of the building type. *
  5010. * *
  5011. * WARNINGS: The point value returned should not be used for scoring, only for target *
  5012. * scanning. *
  5013. * *
  5014. * HISTORY: *
  5015. * 09/16/1996 JLB : Created. *
  5016. *=============================================================================================*/
  5017. int BuildingClass::Value(void) const
  5018. {
  5019. if (Class->IsFake) {
  5020. switch (Class->Type) {
  5021. case STRUCT_FAKEWEAP:
  5022. return(BuildingTypeClass::As_Reference(STRUCT_WEAP).Reward + BuildingTypeClass::As_Reference(STRUCT_WEAP).Risk);
  5023. case STRUCT_FAKECONST:
  5024. return(BuildingTypeClass::As_Reference(STRUCT_CONST).Reward + BuildingTypeClass::As_Reference(STRUCT_CONST).Risk);
  5025. case STRUCT_FAKE_YARD:
  5026. return(BuildingTypeClass::As_Reference(STRUCT_SHIP_YARD).Reward + BuildingTypeClass::As_Reference(STRUCT_SHIP_YARD).Risk);
  5027. case STRUCT_FAKE_PEN:
  5028. return(BuildingTypeClass::As_Reference(STRUCT_SUB_PEN).Reward + BuildingTypeClass::As_Reference(STRUCT_SUB_PEN).Risk);
  5029. case STRUCT_FAKE_RADAR:
  5030. return(BuildingTypeClass::As_Reference(STRUCT_RADAR).Reward + BuildingTypeClass::As_Reference(STRUCT_RADAR).Risk);
  5031. default:
  5032. break;
  5033. }
  5034. }
  5035. return(TechnoClass::Value());
  5036. }
  5037. /***********************************************************************************************
  5038. * BuildingClass::Remove_Gap_Effect -- Stop a gap generator from jamming cells. *
  5039. * *
  5040. * INPUT: none *
  5041. * *
  5042. * OUTPUT: none *
  5043. * *
  5044. * WARNINGS: *
  5045. * *
  5046. * HISTORY: *
  5047. * 09/20/1996 BWG : Created. *
  5048. *=============================================================================================*/
  5049. void BuildingClass::Remove_Gap_Effect(void)
  5050. {
  5051. // unjam this one's field...
  5052. Map.UnJam_From(Coord_Cell(Center_Coord()), Rule.GapShroudRadius, House);
  5053. if (!House->IsPlayerControl && PlayerPtr->IsGPSActive) {
  5054. Map.Sight_From(Coord_Cell(Center_Coord()), Rule.GapShroudRadius, PlayerPtr);
  5055. }
  5056. // and rejam any overlapping buildings' fields
  5057. for (int index = 0; index < Buildings.Count(); index++) {
  5058. BuildingClass *obj = Buildings.Ptr(index);
  5059. if (obj && !obj->IsInLimbo && obj->House == House && *obj == STRUCT_GAP && obj!=this) {
  5060. obj->IsJamming = false;
  5061. obj->Arm = 0;
  5062. // Map.Jam_From(Coord_Cell(obj->Center_Coord()), Rule.GapShroudRadius, PlayerPtr);
  5063. }
  5064. }
  5065. }
  5066. short const * BuildingClass::Overlap_List(bool redraw) const
  5067. {
  5068. if ((SpiedBy & (1 << PlayerPtr->Class->House)) != 0 && IsSelected && (*this == STRUCT_BARRACKS || *this == STRUCT_TENT)) {
  5069. static short const _list[] = {
  5070. -1, 2, (MAP_CELL_W*1)-1, (MAP_CELL_W*1)+2, REFRESH_EOL
  5071. };
  5072. return(_list);
  5073. }
  5074. return(TechnoClass::Overlap_List(redraw));
  5075. }