fight.c 127 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "main.h"
  24. #include <math.h>
  25. //#define INVINCIBLE 1
  26. static BOOL
  27. PAL_IsPlayerDying(
  28. WORD wPlayerRole
  29. )
  30. /*++
  31. Purpose:
  32. Check if the player is dying.
  33. Parameters:
  34. [IN] wPlayerRole - the player role ID.
  35. Return value:
  36. TRUE if the player is dying, FALSE if not.
  37. --*/
  38. {
  39. return gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] < gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole] / 5;
  40. }
  41. INT
  42. PAL_BattleSelectAutoTarget(
  43. VOID
  44. )
  45. /*++
  46. Purpose:
  47. Pick an enemy target automatically.
  48. Parameters:
  49. None.
  50. Return value:
  51. The index of enemy. -1 if failed.
  52. --*/
  53. {
  54. int i;
  55. i = (int)g_Battle.UI.wPrevEnemyTarget;
  56. if (i >= 0 && i <= g_Battle.wMaxEnemyIndex &&
  57. g_Battle.rgEnemy[i].wObjectID != 0 &&
  58. g_Battle.rgEnemy[i].e.wHealth > 0)
  59. {
  60. return i;
  61. }
  62. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  63. {
  64. if (g_Battle.rgEnemy[i].wObjectID != 0 &&
  65. g_Battle.rgEnemy[i].e.wHealth > 0)
  66. {
  67. return i;
  68. }
  69. }
  70. return -1;
  71. }
  72. static SHORT
  73. PAL_CalcBaseDamage(
  74. WORD wAttackStrength,
  75. WORD wDefense
  76. )
  77. /*++
  78. Purpose:
  79. Calculate the base damage value of attacking.
  80. Parameters:
  81. [IN] wAttackStrength - attack strength of attacker.
  82. [IN] wDefense - defense value of inflictor.
  83. Return value:
  84. The base damage value of the attacking.
  85. --*/
  86. {
  87. SHORT sDamage;
  88. //
  89. // Formula courtesy of palxex and shenyanduxing
  90. //
  91. if (wAttackStrength > wDefense)
  92. {
  93. sDamage = (SHORT)(wAttackStrength * 2 - wDefense * 1.6 + 0.5);
  94. }
  95. else if (wAttackStrength > wDefense * 0.6)
  96. {
  97. sDamage = (SHORT)(wAttackStrength - wDefense * 0.6 + 0.5);
  98. }
  99. else
  100. {
  101. sDamage = 0;
  102. }
  103. return sDamage;
  104. }
  105. static SHORT
  106. PAL_CalcMagicDamage(
  107. WORD wMagicStrength,
  108. WORD wDefense,
  109. const WORD rgwElementalResistance[NUM_MAGIC_ELEMENTAL],
  110. WORD wPoisonResistance,
  111. WORD wMagicID
  112. )
  113. /*++
  114. Purpose:
  115. Calculate the damage of magic.
  116. Parameters:
  117. [IN] wMagicStrength - magic strength of attacker.
  118. [IN] wDefense - defense value of inflictor.
  119. [IN] rgwElementalResistance - inflictor's resistance to the elemental magics.
  120. [IN] wPoisonResistance - inflictor's resistance to poison.
  121. [IN] wMagicID - object ID of the magic.
  122. Return value:
  123. The damage value of the magic attack.
  124. --*/
  125. {
  126. SHORT sDamage;
  127. WORD wElem;
  128. wMagicID = gpGlobals->g.rgObject[wMagicID].magic.wMagicNumber;
  129. //
  130. // Formula courtesy of palxex and shenyanduxing
  131. //
  132. wMagicStrength *= RandomFloat(10, 11);
  133. wMagicStrength /= 10;
  134. sDamage = PAL_CalcBaseDamage(wMagicStrength, wDefense);
  135. sDamage /= 4;
  136. sDamage += gpGlobals->g.lprgMagic[wMagicID].wBaseDamage;
  137. if (gpGlobals->g.lprgMagic[wMagicID].wElemental != 0)
  138. {
  139. wElem = gpGlobals->g.lprgMagic[wMagicID].wElemental;
  140. if (wElem > NUM_MAGIC_ELEMENTAL)
  141. {
  142. sDamage *= 10 - wPoisonResistance;
  143. }
  144. else if (wElem == 0)
  145. {
  146. sDamage *= 5;
  147. }
  148. else
  149. {
  150. sDamage *= 10 - rgwElementalResistance[wElem - 1];
  151. }
  152. sDamage /= 5;
  153. if (wElem <= NUM_MAGIC_ELEMENTAL)
  154. {
  155. sDamage *= 10 + gpGlobals->g.lprgBattleField[gpGlobals->wNumBattleField].rgsMagicEffect[wElem - 1];
  156. sDamage /= 10;
  157. }
  158. }
  159. return sDamage;
  160. }
  161. SHORT
  162. PAL_CalcPhysicalAttackDamage(
  163. WORD wAttackStrength,
  164. WORD wDefense,
  165. WORD wAttackResistance
  166. )
  167. /*++
  168. Purpose:
  169. Calculate the damage value of physical attacking.
  170. Parameters:
  171. [IN] wAttackStrength - attack strength of attacker.
  172. [IN] wDefense - defense value of inflictor.
  173. [IN] wAttackResistance - inflictor's resistance to physical attack.
  174. Return value:
  175. The damage value of the physical attacking.
  176. --*/
  177. {
  178. SHORT sDamage;
  179. sDamage = PAL_CalcBaseDamage(wAttackStrength, wDefense);
  180. if (wAttackResistance != 0)
  181. {
  182. sDamage /= wAttackResistance;
  183. }
  184. return sDamage;
  185. }
  186. static SHORT
  187. PAL_GetEnemyDexterity(
  188. WORD wEnemyIndex
  189. )
  190. /*++
  191. Purpose:
  192. Get the dexterity value of the enemy.
  193. Parameters:
  194. [IN] wEnemyIndex - the index of the enemy.
  195. Return value:
  196. The dexterity value of the enemy.
  197. --*/
  198. {
  199. SHORT s;
  200. assert(g_Battle.rgEnemy[wEnemyIndex].wObjectID != 0);
  201. s = (g_Battle.rgEnemy[wEnemyIndex].e.wLevel + 6) * 3;
  202. s += (SHORT)g_Battle.rgEnemy[wEnemyIndex].e.wDexterity;
  203. #ifndef PAL_CLASSIC
  204. if (s < 20)
  205. {
  206. s = 20;
  207. }
  208. if (g_Battle.rgEnemy[wEnemyIndex].rgwStatus[kStatusHaste] != 0)
  209. {
  210. s *= 6;
  211. s /= 5;
  212. }
  213. else if (g_Battle.rgEnemy[wEnemyIndex].rgwStatus[kStatusSlow] != 0)
  214. {
  215. s *= 2;
  216. s /= 3;
  217. }
  218. #endif
  219. return s;
  220. }
  221. static WORD
  222. PAL_GetPlayerActualDexterity(
  223. WORD wPlayerRole
  224. )
  225. /*++
  226. Purpose:
  227. Get player's actual dexterity value in battle.
  228. Parameters:
  229. [IN] wPlayerRole - the player role ID.
  230. Return value:
  231. The player's actual dexterity value.
  232. --*/
  233. {
  234. WORD wDexterity = PAL_GetPlayerDexterity(wPlayerRole);
  235. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] != 0)
  236. {
  237. #ifdef PAL_CLASSIC
  238. wDexterity *= 3;
  239. #else
  240. wDexterity *= 6;
  241. wDexterity /= 5;
  242. #endif
  243. }
  244. #ifndef PAL_CLASSIC
  245. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] != 0)
  246. {
  247. wDexterity *= 2;
  248. wDexterity /= 3;
  249. }
  250. #endif
  251. if (PAL_IsPlayerDying(wPlayerRole))
  252. {
  253. //
  254. // player who is low of HP should be slower
  255. //
  256. #ifdef PAL_CLASSIC
  257. wDexterity /= 2;
  258. #else
  259. wDexterity *= 4;
  260. wDexterity /= 5;
  261. #endif
  262. }
  263. #ifdef PAL_CLASSIC
  264. if (wDexterity > 999)
  265. {
  266. wDexterity = 999;
  267. }
  268. #endif
  269. return wDexterity;
  270. }
  271. #ifndef PAL_CLASSIC
  272. VOID
  273. PAL_UpdateTimeChargingUnit(
  274. VOID
  275. )
  276. /*++
  277. Purpose:
  278. Update the base time unit of time-charging.
  279. Parameters:
  280. None.
  281. Return value:
  282. None.
  283. --*/
  284. {
  285. g_Battle.flTimeChargingUnit = (FLOAT)(pow(PAL_GetPlayerDexterity(0) + 5, 0.3));
  286. g_Battle.flTimeChargingUnit /= PAL_GetPlayerDexterity(0);
  287. if (gpGlobals->bBattleSpeed > 1)
  288. {
  289. g_Battle.flTimeChargingUnit /= 1 + (gpGlobals->bBattleSpeed - 1) * 0.5;
  290. }
  291. else
  292. {
  293. g_Battle.flTimeChargingUnit /= 1.2f;
  294. }
  295. }
  296. FLOAT
  297. PAL_GetTimeChargingSpeed(
  298. WORD wDexterity
  299. )
  300. /*++
  301. Purpose:
  302. Calculate the time charging speed.
  303. Parameters:
  304. [IN] wDexterity - the dexterity value of player or enemy.
  305. Return value:
  306. The time-charging speed of the player or enemy.
  307. --*/
  308. {
  309. if ((g_Battle.UI.state == kBattleUISelectMove &&
  310. g_Battle.UI.MenuState != kBattleMenuMain) ||
  311. !SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
  312. {
  313. //
  314. // Pause the time when there are submenus or text messages
  315. //
  316. return 0;
  317. }
  318. //
  319. // The battle should be faster when using Auto-Battle
  320. //
  321. if (gpGlobals->fAutoBattle)
  322. {
  323. wDexterity *= 3;
  324. }
  325. return g_Battle.flTimeChargingUnit * wDexterity;
  326. }
  327. #endif
  328. VOID
  329. PAL_BattleDelay(
  330. WORD wDuration,
  331. WORD wObjectID,
  332. BOOL fUpdateGesture
  333. )
  334. /*++
  335. Purpose:
  336. Delay a while during battle.
  337. Parameters:
  338. [IN] wDuration - Number of frames of the delay.
  339. [IN] wObjectID - The object ID to be displayed during the delay.
  340. [IN] fUpdateGesture - TRUE if update the gesture for enemies, FALSE if not.
  341. Return value:
  342. None.
  343. --*/
  344. {
  345. int i, j;
  346. DWORD dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
  347. for (i = 0; i < wDuration; i++)
  348. {
  349. if (fUpdateGesture)
  350. {
  351. //
  352. // Update the gesture of enemies.
  353. //
  354. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  355. {
  356. if (g_Battle.rgEnemy[j].wObjectID == 0 ||
  357. g_Battle.rgEnemy[j].rgwStatus[kStatusSleep] != 0 ||
  358. g_Battle.rgEnemy[j].rgwStatus[kStatusParalyzed] != 0)
  359. {
  360. continue;
  361. }
  362. if (--g_Battle.rgEnemy[j].e.wIdleAnimSpeed == 0)
  363. {
  364. g_Battle.rgEnemy[j].wCurrentFrame++;
  365. g_Battle.rgEnemy[j].e.wIdleAnimSpeed =
  366. gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[g_Battle.rgEnemy[j].wObjectID].enemy.wEnemyID].wIdleAnimSpeed;
  367. }
  368. if (g_Battle.rgEnemy[j].wCurrentFrame >= g_Battle.rgEnemy[j].e.wIdleFrames)
  369. {
  370. g_Battle.rgEnemy[j].wCurrentFrame = 0;
  371. }
  372. }
  373. }
  374. //
  375. // Wait for the time of one frame. Accept input here.
  376. //
  377. PAL_DelayUntil(dwTime);
  378. //
  379. // Set the time of the next frame.
  380. //
  381. dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
  382. PAL_BattleMakeScene();
  383. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  384. PAL_BattleUIUpdate();
  385. if (wObjectID != 0)
  386. {
  387. if (wObjectID == BATTLE_LABEL_ESCAPEFAIL) // HACKHACK
  388. {
  389. PAL_DrawText(PAL_GetWord(wObjectID), PAL_XY(130, 75), 15, TRUE, FALSE, FALSE);
  390. }
  391. else if ((SHORT)wObjectID < 0)
  392. {
  393. PAL_DrawText(PAL_GetWord(-((SHORT)wObjectID)), PAL_XY(170, 45), DESCTEXT_COLOR, TRUE, FALSE, FALSE);
  394. }
  395. else
  396. {
  397. PAL_DrawText(PAL_GetWord(wObjectID), PAL_XY(210, 50), 15, TRUE, FALSE, FALSE);
  398. }
  399. }
  400. VIDEO_UpdateScreen(NULL);
  401. }
  402. }
  403. static VOID
  404. PAL_BattleBackupStat(
  405. VOID
  406. )
  407. /*++
  408. Purpose:
  409. Backup HP and MP values of all players and enemies.
  410. Parameters:
  411. None.
  412. Return value:
  413. None.
  414. --*/
  415. {
  416. int i;
  417. WORD wPlayerRole;
  418. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  419. {
  420. if (g_Battle.rgEnemy[i].wObjectID == 0)
  421. {
  422. continue;
  423. }
  424. g_Battle.rgEnemy[i].wPrevHP = g_Battle.rgEnemy[i].e.wHealth;
  425. }
  426. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  427. {
  428. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  429. g_Battle.rgPlayer[i].wPrevHP =
  430. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole];
  431. g_Battle.rgPlayer[i].wPrevMP =
  432. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole];
  433. }
  434. }
  435. static BOOL
  436. PAL_BattleDisplayStatChange(
  437. VOID
  438. )
  439. /*++
  440. Purpose:
  441. Display the HP and MP changes of all players and enemies.
  442. Parameters:
  443. None.
  444. Return value:
  445. TRUE if there are any number displayed, FALSE if not.
  446. --*/
  447. {
  448. int i, x, y;
  449. SHORT sDamage;
  450. WORD wPlayerRole;
  451. BOOL f = FALSE;
  452. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  453. {
  454. if (g_Battle.rgEnemy[i].wObjectID == 0)
  455. {
  456. continue;
  457. }
  458. if (g_Battle.rgEnemy[i].wPrevHP != g_Battle.rgEnemy[i].e.wHealth)
  459. {
  460. //
  461. // Show the number of damage
  462. //
  463. sDamage = g_Battle.rgEnemy[i].e.wHealth - g_Battle.rgEnemy[i].wPrevHP;
  464. x = PAL_X(g_Battle.rgEnemy[i].pos) - 9;
  465. y = PAL_Y(g_Battle.rgEnemy[i].pos) - 115;
  466. if (y < 10)
  467. {
  468. y = 10;
  469. }
  470. if (sDamage < 0)
  471. {
  472. PAL_BattleUIShowNum((WORD)(-sDamage), PAL_XY(x, y), kNumColorBlue);
  473. }
  474. else
  475. {
  476. PAL_BattleUIShowNum((WORD)(sDamage), PAL_XY(x, y), kNumColorYellow);
  477. }
  478. f = TRUE;
  479. }
  480. }
  481. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  482. {
  483. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  484. if (g_Battle.rgPlayer[i].wPrevHP != gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole])
  485. {
  486. sDamage =
  487. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] - g_Battle.rgPlayer[i].wPrevHP;
  488. x = PAL_X(g_Battle.rgPlayer[i].pos) - 9;
  489. y = PAL_Y(g_Battle.rgPlayer[i].pos) - 75;
  490. if (y < 10)
  491. {
  492. y = 10;
  493. }
  494. if (sDamage < 0)
  495. {
  496. PAL_BattleUIShowNum((WORD)(-sDamage), PAL_XY(x, y), kNumColorBlue);
  497. }
  498. else
  499. {
  500. PAL_BattleUIShowNum((WORD)(sDamage), PAL_XY(x, y), kNumColorYellow);
  501. }
  502. f = TRUE;
  503. }
  504. if (g_Battle.rgPlayer[i].wPrevMP != gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole])
  505. {
  506. sDamage =
  507. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] - g_Battle.rgPlayer[i].wPrevMP;
  508. x = PAL_X(g_Battle.rgPlayer[i].pos) - 9;
  509. y = PAL_Y(g_Battle.rgPlayer[i].pos) - 67;
  510. if (y < 10)
  511. {
  512. y = 10;
  513. }
  514. //
  515. // Only show MP increasing
  516. //
  517. if (sDamage > 0)
  518. {
  519. PAL_BattleUIShowNum((WORD)(sDamage), PAL_XY(x, y), kNumColorCyan);
  520. }
  521. f = TRUE;
  522. }
  523. }
  524. return f;
  525. }
  526. static VOID
  527. PAL_BattlePostActionCheck(
  528. BOOL fCheckPlayers
  529. )
  530. /*++
  531. Purpose:
  532. Essential checks after an action is executed.
  533. Parameters:
  534. [IN] fCheckPlayers - TRUE if check for players, FALSE if not.
  535. Return value:
  536. None.
  537. --*/
  538. {
  539. int i, j;
  540. BOOL fFade = FALSE;
  541. BOOL fEnemyRemaining = FALSE;
  542. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  543. {
  544. if (g_Battle.rgEnemy[i].wObjectID == 0)
  545. {
  546. continue;
  547. }
  548. if ((SHORT)(g_Battle.rgEnemy[i].e.wHealth) <= 0)
  549. {
  550. //
  551. // This enemy is KO'ed
  552. //
  553. g_Battle.iExpGained += g_Battle.rgEnemy[i].e.wExp;
  554. g_Battle.iCashGained += g_Battle.rgEnemy[i].e.wCash;
  555. AUDIO_PlaySound(g_Battle.rgEnemy[i].e.wDeathSound);
  556. g_Battle.rgEnemy[i].wObjectID = 0;
  557. fFade = TRUE;
  558. continue;
  559. }
  560. fEnemyRemaining = TRUE;
  561. }
  562. if (!fEnemyRemaining)
  563. {
  564. g_Battle.fEnemyCleared = TRUE;
  565. g_Battle.UI.state = kBattleUIWait;
  566. }
  567. if (fCheckPlayers && !gpGlobals->fAutoBattle)
  568. {
  569. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  570. {
  571. WORD w = gpGlobals->rgParty[i].wPlayerRole, wName;
  572. if (gpGlobals->g.PlayerRoles.rgwHP[w] < g_Battle.rgPlayer[i].wPrevHP &&
  573. gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  574. {
  575. w = gpGlobals->g.PlayerRoles.rgwCoveredBy[w];
  576. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  577. {
  578. if (gpGlobals->rgParty[j].wPlayerRole == w)
  579. {
  580. break;
  581. }
  582. }
  583. if (gpGlobals->g.PlayerRoles.rgwHP[w] > 0 &&
  584. gpGlobals->rgPlayerStatus[w][kStatusSleep] == 0 &&
  585. gpGlobals->rgPlayerStatus[w][kStatusParalyzed] == 0 &&
  586. gpGlobals->rgPlayerStatus[w][kStatusConfused] == 0 &&
  587. j <= gpGlobals->wMaxPartyMemberIndex)
  588. {
  589. wName = gpGlobals->g.PlayerRoles.rgwName[w];
  590. if (gpGlobals->g.rgObject[wName].player.wScriptOnFriendDeath != 0)
  591. {
  592. PAL_BattleDelay(10, 0, TRUE);
  593. PAL_BattleMakeScene();
  594. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  595. VIDEO_UpdateScreen(NULL);
  596. g_Battle.BattleResult = kBattleResultPause;
  597. gpGlobals->g.rgObject[wName].player.wScriptOnFriendDeath =
  598. PAL_RunTriggerScript(gpGlobals->g.rgObject[wName].player.wScriptOnFriendDeath, w);
  599. g_Battle.BattleResult = kBattleResultOnGoing;
  600. PAL_ClearKeyState();
  601. goto end;
  602. }
  603. }
  604. }
  605. }
  606. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  607. {
  608. WORD w = gpGlobals->rgParty[i].wPlayerRole, wName;
  609. if (gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  610. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0)
  611. {
  612. continue;
  613. }
  614. if (gpGlobals->g.PlayerRoles.rgwHP[w] < g_Battle.rgPlayer[i].wPrevHP)
  615. {
  616. if (gpGlobals->g.PlayerRoles.rgwHP[w] > 0 && PAL_IsPlayerDying(w) &&
  617. g_Battle.rgPlayer[i].wPrevHP >= gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5)
  618. {
  619. WORD wCover = gpGlobals->g.PlayerRoles.rgwCoveredBy[w];
  620. if (gpGlobals->rgPlayerStatus[wCover][kStatusSleep] != 0 ||
  621. gpGlobals->rgPlayerStatus[wCover][kStatusParalyzed] != 0 ||
  622. gpGlobals->rgPlayerStatus[wCover][kStatusConfused] != 0)
  623. {
  624. continue;
  625. }
  626. wName = gpGlobals->g.PlayerRoles.rgwName[w];
  627. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwDyingSound[w]);
  628. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  629. {
  630. if (gpGlobals->rgParty[j].wPlayerRole == wCover)
  631. {
  632. break;
  633. }
  634. }
  635. if (j > gpGlobals->wMaxPartyMemberIndex || gpGlobals->g.PlayerRoles.rgwHP[wCover] == 0)
  636. {
  637. continue;
  638. }
  639. if (gpGlobals->g.rgObject[wName].player.wScriptOnDying != 0)
  640. {
  641. PAL_BattleDelay(10, 0, TRUE);
  642. PAL_BattleMakeScene();
  643. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  644. VIDEO_UpdateScreen(NULL);
  645. g_Battle.BattleResult = kBattleResultPause;
  646. gpGlobals->g.rgObject[wName].player.wScriptOnDying =
  647. PAL_RunTriggerScript(gpGlobals->g.rgObject[wName].player.wScriptOnDying, w);
  648. g_Battle.BattleResult = kBattleResultOnGoing;
  649. PAL_ClearKeyState();
  650. }
  651. goto end;
  652. }
  653. }
  654. }
  655. }
  656. end:
  657. if (fFade)
  658. {
  659. PAL_BattleBackupScene();
  660. PAL_BattleMakeScene();
  661. PAL_BattleFadeScene();
  662. }
  663. //
  664. // Fade out the summoned god
  665. //
  666. if (g_Battle.lpSummonSprite != NULL)
  667. {
  668. PAL_BattleUpdateFighters();
  669. PAL_BattleDelay(1, 0, FALSE);
  670. free(g_Battle.lpSummonSprite);
  671. g_Battle.lpSummonSprite = NULL;
  672. g_Battle.sBackgroundColorShift = 0;
  673. PAL_BattleBackupScene();
  674. PAL_BattleMakeScene();
  675. PAL_BattleFadeScene();
  676. }
  677. }
  678. VOID
  679. PAL_BattleUpdateFighters(
  680. VOID
  681. )
  682. /*++
  683. Purpose:
  684. Update players' and enemies' gestures and locations in battle.
  685. Parameters:
  686. None.
  687. Return value:
  688. None.
  689. --*/
  690. {
  691. int i;
  692. WORD wPlayerRole;
  693. //
  694. // Update the gesture for all players
  695. //
  696. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  697. {
  698. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  699. g_Battle.rgPlayer[i].pos = g_Battle.rgPlayer[i].posOriginal;
  700. g_Battle.rgPlayer[i].iColorShift = 0;
  701. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  702. {
  703. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] == 0)
  704. {
  705. g_Battle.rgPlayer[i].wCurrentFrame = 2; // dead
  706. }
  707. else
  708. {
  709. g_Battle.rgPlayer[i].wCurrentFrame = 0; // puppet
  710. }
  711. }
  712. else
  713. {
  714. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] != 0 ||
  715. PAL_IsPlayerDying(wPlayerRole))
  716. {
  717. g_Battle.rgPlayer[i].wCurrentFrame = 1;
  718. }
  719. #ifndef PAL_CLASSIC
  720. else if (g_Battle.rgPlayer[i].state == kFighterAct &&
  721. g_Battle.rgPlayer[i].action.ActionType == kBattleActionMagic &&
  722. !g_Battle.fEnemyCleared)
  723. {
  724. //
  725. // Player is using a magic
  726. //
  727. g_Battle.rgPlayer[i].wCurrentFrame = 5;
  728. }
  729. #endif
  730. else if (g_Battle.rgPlayer[i].fDefending && !g_Battle.fEnemyCleared)
  731. {
  732. g_Battle.rgPlayer[i].wCurrentFrame = 3;
  733. }
  734. else
  735. {
  736. g_Battle.rgPlayer[i].wCurrentFrame = 0;
  737. }
  738. }
  739. }
  740. //
  741. // Update the gesture for all enemies
  742. //
  743. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  744. {
  745. if (g_Battle.rgEnemy[i].wObjectID == 0)
  746. {
  747. continue;
  748. }
  749. g_Battle.rgEnemy[i].pos = g_Battle.rgEnemy[i].posOriginal;
  750. g_Battle.rgEnemy[i].iColorShift = 0;
  751. if (g_Battle.rgEnemy[i].rgwStatus[kStatusSleep] > 0 ||
  752. g_Battle.rgEnemy[i].rgwStatus[kStatusParalyzed] > 0)
  753. {
  754. g_Battle.rgEnemy[i].wCurrentFrame = 0;
  755. continue;
  756. }
  757. if (--g_Battle.rgEnemy[i].e.wIdleAnimSpeed == 0)
  758. {
  759. g_Battle.rgEnemy[i].wCurrentFrame++;
  760. g_Battle.rgEnemy[i].e.wIdleAnimSpeed =
  761. gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[g_Battle.rgEnemy[i].wObjectID].enemy.wEnemyID].wIdleAnimSpeed;
  762. }
  763. if (g_Battle.rgEnemy[i].wCurrentFrame >= g_Battle.rgEnemy[i].e.wIdleFrames)
  764. {
  765. g_Battle.rgEnemy[i].wCurrentFrame = 0;
  766. }
  767. }
  768. }
  769. VOID
  770. PAL_BattlePlayerCheckReady(
  771. VOID
  772. )
  773. /*++
  774. Purpose:
  775. Check if there are player who is ready.
  776. Parameters:
  777. None.
  778. Return value:
  779. None.
  780. --*/
  781. {
  782. float flMax = 0;
  783. int iMax = 0, i;
  784. //
  785. // Start the UI for the fastest and ready player
  786. //
  787. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  788. {
  789. if (g_Battle.rgPlayer[i].state == kFighterCom ||
  790. (g_Battle.rgPlayer[i].state == kFighterAct && g_Battle.rgPlayer[i].action.ActionType == kBattleActionCoopMagic))
  791. {
  792. flMax = 0;
  793. break;
  794. }
  795. else if (g_Battle.rgPlayer[i].state == kFighterWait)
  796. {
  797. if (g_Battle.rgPlayer[i].flTimeMeter > flMax)
  798. {
  799. iMax = i;
  800. flMax = g_Battle.rgPlayer[i].flTimeMeter;
  801. }
  802. }
  803. }
  804. if (flMax >= 100.0f)
  805. {
  806. g_Battle.rgPlayer[iMax].state = kFighterCom;
  807. g_Battle.rgPlayer[iMax].fDefending = FALSE;
  808. }
  809. }
  810. VOID
  811. PAL_BattleStartFrame(
  812. VOID
  813. )
  814. /*++
  815. Purpose:
  816. Called once per video frame in battle.
  817. Parameters:
  818. None.
  819. Return value:
  820. None.
  821. --*/
  822. {
  823. int i, j;
  824. WORD wPlayerRole;
  825. WORD wDexterity;
  826. BOOL fOnlyPuppet = TRUE;
  827. #ifndef PAL_CLASSIC
  828. FLOAT flMax;
  829. BOOL fMoved = FALSE;
  830. SHORT sMax, sMaxIndex;
  831. #endif
  832. if (!g_Battle.fEnemyCleared)
  833. {
  834. PAL_BattleUpdateFighters();
  835. }
  836. //
  837. // Update the scene
  838. //
  839. PAL_BattleMakeScene();
  840. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  841. //
  842. // Check if the battle is over
  843. //
  844. if (g_Battle.fEnemyCleared)
  845. {
  846. //
  847. // All enemies are cleared. Won the battle.
  848. //
  849. g_Battle.BattleResult = kBattleResultWon;
  850. AUDIO_PlaySound(0);
  851. return;
  852. }
  853. else
  854. {
  855. BOOL fEnded = TRUE;
  856. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  857. {
  858. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  859. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] != 0)
  860. {
  861. fOnlyPuppet = FALSE;
  862. fEnded = FALSE;
  863. break;
  864. }
  865. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] != 0)
  866. {
  867. fEnded = FALSE;
  868. }
  869. }
  870. if (fEnded)
  871. {
  872. //
  873. // All players are dead. Lost the battle.
  874. //
  875. g_Battle.BattleResult = kBattleResultLost;
  876. return;
  877. }
  878. }
  879. #ifndef PAL_CLASSIC
  880. //
  881. // Check for hiding status
  882. //
  883. if (g_Battle.iHidingTime > 0)
  884. {
  885. if (PAL_GetTimeChargingSpeed(9999) > 0)
  886. {
  887. g_Battle.iHidingTime--;
  888. }
  889. if (g_Battle.iHidingTime == 0)
  890. {
  891. PAL_BattleBackupScene();
  892. PAL_BattleMakeScene();
  893. PAL_BattleFadeScene();
  894. }
  895. }
  896. //
  897. // Run the logic for all enemies
  898. //
  899. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  900. {
  901. if (g_Battle.rgEnemy[i].wObjectID == 0)
  902. {
  903. continue;
  904. }
  905. if (g_Battle.rgEnemy[i].fTurnStart)
  906. {
  907. g_Battle.rgEnemy[i].wScriptOnTurnStart =
  908. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnTurnStart, i);
  909. g_Battle.rgEnemy[i].fTurnStart = FALSE;
  910. fMoved = TRUE;
  911. }
  912. }
  913. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  914. {
  915. if (g_Battle.rgEnemy[i].wObjectID == 0)
  916. {
  917. continue;
  918. }
  919. switch (g_Battle.rgEnemy[i].state)
  920. {
  921. case kFighterWait:
  922. flMax = PAL_GetTimeChargingSpeed(PAL_GetEnemyDexterity(i));
  923. flMax /= (gpGlobals->fAutoBattle ? 2 : 1);
  924. if (flMax != 0)
  925. {
  926. g_Battle.rgEnemy[i].flTimeMeter += flMax;
  927. if (g_Battle.rgEnemy[i].flTimeMeter > 100 && flMax > 0)
  928. {
  929. if (g_Battle.iHidingTime == 0)
  930. {
  931. g_Battle.rgEnemy[i].state = kFighterCom;
  932. }
  933. else
  934. {
  935. g_Battle.rgEnemy[i].flTimeMeter = 0;
  936. }
  937. }
  938. }
  939. break;
  940. case kFighterCom:
  941. g_Battle.rgEnemy[i].wScriptOnReady =
  942. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnReady, i);
  943. g_Battle.rgEnemy[i].state = kFighterAct;
  944. fMoved = TRUE;
  945. break;
  946. case kFighterAct:
  947. if (!fMoved && (PAL_GetTimeChargingSpeed(9999) > 0 || g_Battle.rgEnemy[i].fDualMove) && !fOnlyPuppet)
  948. {
  949. fMoved = TRUE;
  950. g_Battle.fEnemyMoving = TRUE;
  951. g_Battle.rgEnemy[i].fDualMove =
  952. (!g_Battle.rgEnemy[i].fFirstMoveDone &&
  953. (g_Battle.rgEnemy[i].e.wDualMove >= 2 ||
  954. (g_Battle.rgEnemy[i].e.wDualMove != 0 && RandomLong(0, 1))));
  955. PAL_BattleEnemyPerformAction(i);
  956. g_Battle.rgEnemy[i].flTimeMeter = 0;
  957. g_Battle.rgEnemy[i].state = kFighterWait;
  958. g_Battle.fEnemyMoving = FALSE;
  959. if (g_Battle.rgEnemy[i].fDualMove)
  960. {
  961. g_Battle.rgEnemy[i].flTimeMeter = 100;
  962. g_Battle.rgEnemy[i].state = kFighterCom;
  963. g_Battle.rgEnemy[i].fFirstMoveDone = TRUE;
  964. }
  965. else
  966. {
  967. g_Battle.rgEnemy[i].fFirstMoveDone = FALSE;
  968. g_Battle.rgEnemy[i].fTurnStart = TRUE;
  969. }
  970. }
  971. break;
  972. }
  973. }
  974. //
  975. // Update the battle UI
  976. //
  977. PAL_BattleUIUpdate();
  978. //
  979. // Run the logic for all players
  980. //
  981. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  982. {
  983. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  984. //
  985. // Skip dead players
  986. //
  987. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  988. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] == 0)
  989. {
  990. g_Battle.rgPlayer[i].state = kFighterWait;
  991. g_Battle.rgPlayer[i].flTimeMeter = 0;
  992. g_Battle.rgPlayer[i].flTimeSpeedModifier = 1.0f;
  993. g_Battle.rgPlayer[i].sTurnOrder = -1;
  994. continue;
  995. }
  996. switch (g_Battle.rgPlayer[i].state)
  997. {
  998. case kFighterWait:
  999. wDexterity = PAL_GetPlayerActualDexterity(wPlayerRole);
  1000. g_Battle.rgPlayer[i].flTimeMeter +=
  1001. PAL_GetTimeChargingSpeed(wDexterity) * g_Battle.rgPlayer[i].flTimeSpeedModifier;
  1002. break;
  1003. case kFighterCom:
  1004. break;
  1005. case kFighterAct:
  1006. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] > 0)
  1007. {
  1008. g_Battle.rgPlayer[i].action.ActionType = kBattleActionPass;
  1009. g_Battle.rgPlayer[i].action.flRemainingTime = 0;
  1010. }
  1011. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] > 0)
  1012. {
  1013. g_Battle.rgPlayer[i].action.ActionType = kBattleActionAttackMate;
  1014. g_Battle.rgPlayer[i].action.flRemainingTime = 0;
  1015. }
  1016. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] > 0 &&
  1017. g_Battle.rgPlayer[i].action.ActionType == kBattleActionMagic)
  1018. {
  1019. g_Battle.rgPlayer[i].action.flRemainingTime = 0;
  1020. }
  1021. wDexterity = PAL_GetPlayerActualDexterity(wPlayerRole);
  1022. g_Battle.rgPlayer[i].action.flRemainingTime -= PAL_GetTimeChargingSpeed(wDexterity);
  1023. if (g_Battle.rgPlayer[i].action.flRemainingTime <= 0 &&
  1024. g_Battle.rgPlayer[i].sTurnOrder == -1)
  1025. {
  1026. sMax = -1;
  1027. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  1028. {
  1029. if (g_Battle.rgPlayer[j].sTurnOrder > sMax)
  1030. {
  1031. sMax = g_Battle.rgPlayer[j].sTurnOrder;
  1032. }
  1033. }
  1034. g_Battle.rgPlayer[i].sTurnOrder = sMax + 1;
  1035. }
  1036. break;
  1037. }
  1038. }
  1039. //
  1040. // Preform action for player
  1041. //
  1042. if (!fMoved)
  1043. {
  1044. sMax = 9999;
  1045. sMaxIndex = -1;
  1046. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1047. {
  1048. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  1049. //
  1050. // Skip dead players
  1051. //
  1052. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  1053. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] == 0)
  1054. {
  1055. continue;
  1056. }
  1057. if (g_Battle.rgPlayer[i].state == kFighterAct &&
  1058. g_Battle.rgPlayer[i].sTurnOrder != -1 &&
  1059. g_Battle.rgPlayer[i].sTurnOrder < sMax)
  1060. {
  1061. sMax = g_Battle.rgPlayer[i].sTurnOrder;
  1062. sMaxIndex = i;
  1063. }
  1064. }
  1065. if (sMaxIndex != -1)
  1066. {
  1067. //
  1068. // Perform the action for this player.
  1069. //
  1070. PAL_BattlePlayerPerformAction(sMaxIndex);
  1071. g_Battle.rgPlayer[sMaxIndex].flTimeMeter = 0;
  1072. g_Battle.rgPlayer[sMaxIndex].flTimeSpeedModifier = 1.0f;
  1073. g_Battle.rgPlayer[sMaxIndex].sTurnOrder = -1;
  1074. }
  1075. }
  1076. #else
  1077. if (g_Battle.Phase == kBattlePhaseSelectAction)
  1078. {
  1079. if (g_Battle.UI.state == kBattleUIWait)
  1080. {
  1081. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1082. {
  1083. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  1084. //
  1085. // Don't select action for this player if player is KO'ed,
  1086. // sleeped, confused or paralyzed
  1087. //
  1088. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 ||
  1089. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] ||
  1090. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] ||
  1091. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed])
  1092. {
  1093. continue;
  1094. }
  1095. //
  1096. // Start the menu for the first player whose action is not
  1097. // yet selected
  1098. //
  1099. if (g_Battle.rgPlayer[i].state == kFighterWait)
  1100. {
  1101. g_Battle.wMovingPlayerIndex = i;
  1102. g_Battle.rgPlayer[i].state = kFighterCom;
  1103. PAL_BattleUIPlayerReady(i);
  1104. break;
  1105. }
  1106. else if (g_Battle.rgPlayer[i].action.ActionType == kBattleActionCoopMagic)
  1107. {
  1108. //
  1109. // Skip other players if someone selected coopmagic
  1110. //
  1111. i = gpGlobals->wMaxPartyMemberIndex + 1;
  1112. break;
  1113. }
  1114. }
  1115. if (i > gpGlobals->wMaxPartyMemberIndex)
  1116. {
  1117. //
  1118. // actions for all players are decided. fill in the action queue.
  1119. //
  1120. g_Battle.fRepeat = FALSE;
  1121. g_Battle.fForce = FALSE;
  1122. g_Battle.fFlee = FALSE;
  1123. g_Battle.iCurAction = 0;
  1124. for (i = 0; i < MAX_ACTIONQUEUE_ITEMS; i++)
  1125. {
  1126. g_Battle.ActionQueue[i].wIndex = 0xFFFF;
  1127. g_Battle.ActionQueue[i].wDexterity = 0xFFFF;
  1128. }
  1129. j = 0;
  1130. //
  1131. // Put all enemies into action queue
  1132. //
  1133. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  1134. {
  1135. if (g_Battle.rgEnemy[i].wObjectID == 0)
  1136. {
  1137. continue;
  1138. }
  1139. g_Battle.ActionQueue[j].fIsEnemy = TRUE;
  1140. g_Battle.ActionQueue[j].wIndex = i;
  1141. g_Battle.ActionQueue[j].wDexterity = PAL_GetEnemyDexterity(i);
  1142. g_Battle.ActionQueue[j].wDexterity *= RandomFloat(0.9f, 1.1f);
  1143. j++;
  1144. if (g_Battle.rgEnemy[i].e.wDualMove * 50 + RandomLong(0, 100) > 100)
  1145. {
  1146. g_Battle.ActionQueue[j].fIsEnemy = TRUE;
  1147. g_Battle.ActionQueue[j].wIndex = i;
  1148. g_Battle.ActionQueue[j].wDexterity = PAL_GetEnemyDexterity(i);
  1149. g_Battle.ActionQueue[j].wDexterity *= RandomFloat(0.9f, 1.1f);
  1150. j++;
  1151. }
  1152. }
  1153. //
  1154. // Put all players into action queue
  1155. //
  1156. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1157. {
  1158. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  1159. g_Battle.ActionQueue[j].fIsEnemy = FALSE;
  1160. g_Battle.ActionQueue[j].wIndex = i;
  1161. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 ||
  1162. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] > 0 ||
  1163. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] > 0)
  1164. {
  1165. //
  1166. // players who are unable to move should attack physically if recovered
  1167. // in the same turn
  1168. //
  1169. g_Battle.ActionQueue[j].wDexterity = 0;
  1170. g_Battle.rgPlayer[i].action.ActionType = kBattleActionAttack;
  1171. g_Battle.rgPlayer[i].state = kFighterAct;
  1172. }
  1173. else
  1174. {
  1175. wDexterity = PAL_GetPlayerActualDexterity(wPlayerRole);
  1176. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] > 0)
  1177. {
  1178. g_Battle.rgPlayer[i].action.ActionType = kBattleActionAttack;
  1179. g_Battle.rgPlayer[i].state = kFighterAct;
  1180. }
  1181. switch (g_Battle.rgPlayer[i].action.ActionType)
  1182. {
  1183. case kBattleActionCoopMagic:
  1184. wDexterity *= 10;
  1185. break;
  1186. case kBattleActionDefend:
  1187. wDexterity *= 5;
  1188. break;
  1189. case kBattleActionMagic:
  1190. if ((gpGlobals->g.rgObject[g_Battle.rgPlayer[i].action.wActionID].magic.wFlags & kMagicFlagUsableToEnemy) == 0)
  1191. {
  1192. wDexterity *= 3;
  1193. }
  1194. break;
  1195. case kBattleActionFlee:
  1196. wDexterity /= 2;
  1197. break;
  1198. case kBattleActionUseItem:
  1199. wDexterity *= 3;
  1200. break;
  1201. default:
  1202. break;
  1203. }
  1204. if (PAL_IsPlayerDying(wPlayerRole))
  1205. {
  1206. wDexterity /= 2;
  1207. }
  1208. wDexterity *= RandomFloat(0.9f, 1.1f);
  1209. g_Battle.ActionQueue[j].wDexterity = wDexterity;
  1210. }
  1211. j++;
  1212. }
  1213. //
  1214. // Sort the action queue by dexterity value
  1215. //
  1216. for (i = 0; i < MAX_ACTIONQUEUE_ITEMS; i++)
  1217. {
  1218. for (j = i; j < MAX_ACTIONQUEUE_ITEMS; j++)
  1219. {
  1220. if ((SHORT)g_Battle.ActionQueue[i].wDexterity < (SHORT)g_Battle.ActionQueue[j].wDexterity)
  1221. {
  1222. ACTIONQUEUE t = g_Battle.ActionQueue[i];
  1223. g_Battle.ActionQueue[i] = g_Battle.ActionQueue[j];
  1224. g_Battle.ActionQueue[j] = t;
  1225. }
  1226. }
  1227. }
  1228. //
  1229. // Perform the actions
  1230. //
  1231. g_Battle.Phase = kBattlePhasePerformAction;
  1232. }
  1233. }
  1234. }
  1235. else
  1236. {
  1237. //
  1238. // Are all actions finished?
  1239. //
  1240. if (g_Battle.iCurAction >= MAX_ACTIONQUEUE_ITEMS ||
  1241. g_Battle.ActionQueue[g_Battle.iCurAction].wDexterity == 0xFFFF)
  1242. {
  1243. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1244. {
  1245. g_Battle.rgPlayer[i].fDefending = FALSE;
  1246. }
  1247. //
  1248. // Run poison scripts
  1249. //
  1250. PAL_BattleBackupStat();
  1251. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1252. {
  1253. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  1254. for (j = 0; j < MAX_POISONS; j++)
  1255. {
  1256. if (gpGlobals->rgPoisonStatus[j][i].wPoisonID != 0)
  1257. {
  1258. gpGlobals->rgPoisonStatus[j][i].wPoisonScript =
  1259. PAL_RunTriggerScript(gpGlobals->rgPoisonStatus[j][i].wPoisonScript, wPlayerRole);
  1260. }
  1261. }
  1262. //
  1263. // Update statuses
  1264. //
  1265. for (j = 0; j < kStatusAll; j++)
  1266. {
  1267. if (gpGlobals->rgPlayerStatus[wPlayerRole][j] > 0)
  1268. {
  1269. gpGlobals->rgPlayerStatus[wPlayerRole][j]--;
  1270. }
  1271. }
  1272. }
  1273. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  1274. {
  1275. for (j = 0; j < MAX_POISONS; j++)
  1276. {
  1277. if (g_Battle.rgEnemy[i].rgPoisons[j].wPoisonID != 0)
  1278. {
  1279. g_Battle.rgEnemy[i].rgPoisons[j].wPoisonScript =
  1280. PAL_RunTriggerScript(g_Battle.rgEnemy[i].rgPoisons[j].wPoisonScript, (WORD)i);
  1281. }
  1282. }
  1283. //
  1284. // Update statuses
  1285. //
  1286. for (j = 0; j < kStatusAll; j++)
  1287. {
  1288. if (g_Battle.rgEnemy[i].rgwStatus[j] > 0)
  1289. {
  1290. g_Battle.rgEnemy[i].rgwStatus[j]--;
  1291. }
  1292. }
  1293. }
  1294. PAL_BattlePostActionCheck(FALSE);
  1295. if (PAL_BattleDisplayStatChange())
  1296. {
  1297. PAL_BattleDelay(8, 0, TRUE);
  1298. }
  1299. if (g_Battle.iHidingTime > 0)
  1300. {
  1301. if (--g_Battle.iHidingTime == 0)
  1302. {
  1303. PAL_BattleBackupScene();
  1304. PAL_BattleMakeScene();
  1305. PAL_BattleFadeScene();
  1306. }
  1307. }
  1308. if (g_Battle.iHidingTime == 0)
  1309. {
  1310. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  1311. {
  1312. if (g_Battle.rgEnemy[i].wObjectID == 0)
  1313. {
  1314. continue;
  1315. }
  1316. g_Battle.rgEnemy[i].wScriptOnTurnStart =
  1317. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnTurnStart, i);
  1318. }
  1319. }
  1320. //
  1321. // Clear all item-using records
  1322. //
  1323. for (i = 0; i < MAX_INVENTORY; i++)
  1324. {
  1325. gpGlobals->rgInventory[i].nAmountInUse = 0;
  1326. }
  1327. //
  1328. // Proceed to next turn...
  1329. //
  1330. g_Battle.Phase = kBattlePhaseSelectAction;
  1331. }
  1332. else
  1333. {
  1334. i = g_Battle.ActionQueue[g_Battle.iCurAction].wIndex;
  1335. if (g_Battle.ActionQueue[g_Battle.iCurAction].fIsEnemy)
  1336. {
  1337. if (g_Battle.iHidingTime == 0 && !fOnlyPuppet &&
  1338. g_Battle.rgEnemy[i].wObjectID != 0)
  1339. {
  1340. g_Battle.rgEnemy[i].wScriptOnReady =
  1341. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnReady, i);
  1342. g_Battle.fEnemyMoving = TRUE;
  1343. PAL_BattleEnemyPerformAction(i);
  1344. g_Battle.fEnemyMoving = FALSE;
  1345. }
  1346. }
  1347. else if (g_Battle.rgPlayer[i].state == kFighterAct)
  1348. {
  1349. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  1350. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  1351. {
  1352. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] == 0)
  1353. {
  1354. g_Battle.rgPlayer[i].action.ActionType = kBattleActionPass;
  1355. }
  1356. }
  1357. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] > 0 ||
  1358. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] > 0)
  1359. {
  1360. g_Battle.rgPlayer[i].action.ActionType = kBattleActionPass;
  1361. }
  1362. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] > 0)
  1363. {
  1364. g_Battle.rgPlayer[i].action.ActionType = kBattleActionAttackMate;
  1365. }
  1366. //
  1367. // Perform the action for this player.
  1368. //
  1369. g_Battle.wMovingPlayerIndex = i;
  1370. PAL_BattlePlayerPerformAction(i);
  1371. }
  1372. g_Battle.iCurAction++;
  1373. }
  1374. }
  1375. //
  1376. // The R and F keys and Fleeing should affect all players
  1377. //
  1378. if (g_Battle.UI.MenuState == kBattleMenuMain &&
  1379. g_Battle.UI.state == kBattleUISelectMove)
  1380. {
  1381. if (g_InputState.dwKeyPress & kKeyRepeat)
  1382. {
  1383. g_Battle.fRepeat = TRUE;
  1384. }
  1385. else if (g_InputState.dwKeyPress & kKeyForce)
  1386. {
  1387. g_Battle.fForce = TRUE;
  1388. }
  1389. }
  1390. if (g_Battle.fRepeat)
  1391. {
  1392. g_InputState.dwKeyPress = kKeyRepeat;
  1393. }
  1394. else if (g_Battle.fForce)
  1395. {
  1396. g_InputState.dwKeyPress = kKeyForce;
  1397. }
  1398. else if (g_Battle.fFlee)
  1399. {
  1400. g_InputState.dwKeyPress = kKeyFlee;
  1401. }
  1402. //
  1403. // Update the battle UI
  1404. //
  1405. PAL_BattleUIUpdate();
  1406. #endif
  1407. }
  1408. VOID
  1409. PAL_BattleCommitAction(
  1410. BOOL fRepeat
  1411. )
  1412. /*++
  1413. Purpose:
  1414. Commit the action which the player decided.
  1415. Parameters:
  1416. [IN] fRepeat - TRUE if repeat the last action.
  1417. Return value:
  1418. None.
  1419. --*/
  1420. {
  1421. WORD w;
  1422. if (!fRepeat)
  1423. {
  1424. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType =
  1425. g_Battle.UI.wActionType;
  1426. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.sTarget =
  1427. (SHORT)g_Battle.UI.wSelectedIndex;
  1428. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID =
  1429. g_Battle.UI.wObjectID;
  1430. }
  1431. else if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType == kBattleActionPass)
  1432. {
  1433. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType = kBattleActionAttack;
  1434. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.sTarget = -1;
  1435. }
  1436. //
  1437. // Check if the action is valid
  1438. //
  1439. switch (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType)
  1440. {
  1441. case kBattleActionMagic:
  1442. w = g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID;
  1443. w = gpGlobals->g.lprgMagic[gpGlobals->g.rgObject[w].magic.wMagicNumber].wCostMP;
  1444. if (gpGlobals->g.PlayerRoles.rgwMP[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole] < w)
  1445. {
  1446. w = g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID;
  1447. w = gpGlobals->g.lprgMagic[gpGlobals->g.rgObject[w].magic.wMagicNumber].wType;
  1448. if (w == kMagicTypeApplyToPlayer || w == kMagicTypeApplyToParty ||
  1449. w == kMagicTypeTrance)
  1450. {
  1451. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType = kBattleActionDefend;
  1452. }
  1453. else
  1454. {
  1455. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType = kBattleActionAttack;
  1456. if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.sTarget == -1)
  1457. {
  1458. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.sTarget = 0;
  1459. }
  1460. }
  1461. }
  1462. break;
  1463. #ifdef PAL_CLASSIC
  1464. case kBattleActionUseItem:
  1465. if ((gpGlobals->g.rgObject[g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID].item.wFlags & kItemFlagConsuming) == 0)
  1466. {
  1467. break;
  1468. }
  1469. case kBattleActionThrowItem:
  1470. for (w = 0; w < MAX_INVENTORY; w++)
  1471. {
  1472. if (gpGlobals->rgInventory[w].wItem == g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID)
  1473. {
  1474. gpGlobals->rgInventory[w].nAmountInUse++;
  1475. break;
  1476. }
  1477. }
  1478. break;
  1479. #endif
  1480. default:
  1481. break;
  1482. }
  1483. #ifndef PAL_CLASSIC
  1484. //
  1485. // Calculate the waiting time for the action
  1486. //
  1487. switch (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType)
  1488. {
  1489. case kBattleActionMagic:
  1490. {
  1491. LPMAGIC p;
  1492. WORD wCostMP;
  1493. //
  1494. // The base casting time of magic is set to the MP costed
  1495. //
  1496. w = g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID;
  1497. p = &(gpGlobals->g.lprgMagic[gpGlobals->g.rgObject[w].magic.wMagicNumber]);
  1498. wCostMP = p->wCostMP;
  1499. if (wCostMP == 1)
  1500. {
  1501. if (p->wType == kMagicTypeSummon)
  1502. {
  1503. //
  1504. // The Wine God is an ultimate move which should take long
  1505. //
  1506. wCostMP = 175;
  1507. }
  1508. }
  1509. else if (p->wType == kMagicTypeApplyToPlayer || p->wType == kMagicTypeApplyToParty ||
  1510. p->wType == kMagicTypeTrance)
  1511. {
  1512. //
  1513. // Healing magics should take shorter
  1514. //
  1515. wCostMP /= 3;
  1516. }
  1517. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.flRemainingTime = wCostMP + 5;
  1518. }
  1519. break;
  1520. case kBattleActionAttack:
  1521. case kBattleActionFlee:
  1522. case kBattleActionUseItem:
  1523. case kBattleActionThrowItem:
  1524. default:
  1525. //
  1526. // Other actions take no time
  1527. //
  1528. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.flRemainingTime = 0;
  1529. break;
  1530. }
  1531. #else
  1532. if (g_Battle.UI.wActionType == kBattleActionFlee)
  1533. {
  1534. g_Battle.fFlee = TRUE;
  1535. }
  1536. #endif
  1537. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].state = kFighterAct;
  1538. g_Battle.UI.state = kBattleUIWait;
  1539. #ifndef PAL_CLASSIC
  1540. if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.flRemainingTime <= 0)
  1541. {
  1542. SHORT sMax = -1;
  1543. for (w = 0; w <= gpGlobals->wMaxPartyMemberIndex; w++)
  1544. {
  1545. if (g_Battle.rgPlayer[w].sTurnOrder > sMax)
  1546. {
  1547. sMax = g_Battle.rgPlayer[w].sTurnOrder;
  1548. }
  1549. }
  1550. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].sTurnOrder = sMax + 1;
  1551. }
  1552. else
  1553. {
  1554. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].sTurnOrder = -1;
  1555. }
  1556. #endif
  1557. }
  1558. static VOID
  1559. PAL_BattleShowPlayerAttackAnim(
  1560. WORD wPlayerIndex,
  1561. BOOL fCritical
  1562. )
  1563. /*++
  1564. Purpose:
  1565. Show the physical attack effect for player.
  1566. Parameters:
  1567. [IN] wPlayerIndex - the index of the player.
  1568. [IN] fCritical - TRUE if this is a critical hit.
  1569. Return value:
  1570. None.
  1571. --*/
  1572. {
  1573. WORD wPlayerRole = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  1574. SHORT sTarget = g_Battle.rgPlayer[wPlayerIndex].action.sTarget;
  1575. int index, i, j;
  1576. int enemy_x = 0, enemy_y = 0, enemy_h = 0, x, y, dist = 0;
  1577. DWORD dwTime;
  1578. if (sTarget != -1)
  1579. {
  1580. enemy_x = PAL_X(g_Battle.rgEnemy[sTarget].pos);
  1581. enemy_y = PAL_Y(g_Battle.rgEnemy[sTarget].pos);
  1582. enemy_h = PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgEnemy[sTarget].lpSprite, g_Battle.rgEnemy[sTarget].wCurrentFrame));
  1583. if (sTarget >= 3)
  1584. {
  1585. dist = (sTarget - wPlayerIndex) * 8;
  1586. }
  1587. }
  1588. else
  1589. {
  1590. enemy_x = 150;
  1591. enemy_y = 100;
  1592. }
  1593. index = gpGlobals->g.rgwBattleEffectIndex[PAL_GetPlayerBattleSprite(wPlayerRole)][1];
  1594. index *= 3;
  1595. //
  1596. // Play the attack voice
  1597. //
  1598. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  1599. {
  1600. if (!fCritical)
  1601. {
  1602. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwAttackSound[wPlayerRole]);
  1603. }
  1604. else
  1605. {
  1606. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwCriticalSound[wPlayerRole]);
  1607. }
  1608. }
  1609. //
  1610. // Show the animation
  1611. //
  1612. x = enemy_x - dist + 64;
  1613. y = enemy_y + dist + 20;
  1614. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 8;
  1615. g_Battle.rgPlayer[wPlayerIndex].pos = PAL_XY(x, y);
  1616. PAL_BattleDelay(2, 0, TRUE);
  1617. x -= 10;
  1618. y -= 2;
  1619. g_Battle.rgPlayer[wPlayerIndex].pos = PAL_XY(x, y);
  1620. PAL_BattleDelay(1, 0, TRUE);
  1621. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 9;
  1622. x -= 16;
  1623. y -= 4;
  1624. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwWeaponSound[wPlayerRole]);
  1625. x = enemy_x;
  1626. y = enemy_y - enemy_h / 3 + 10;
  1627. dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
  1628. for (i = 0; i < 3; i++)
  1629. {
  1630. LPCBITMAPRLE b = PAL_SpriteGetFrame(g_Battle.lpEffectSprite, index++);
  1631. //
  1632. // Wait for the time of one frame. Accept input here.
  1633. //
  1634. PAL_DelayUntil(dwTime);
  1635. //
  1636. // Set the time of the next frame.
  1637. //
  1638. dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
  1639. //
  1640. // Update the gesture of enemies.
  1641. //
  1642. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  1643. {
  1644. if (g_Battle.rgEnemy[j].wObjectID == 0 ||
  1645. g_Battle.rgEnemy[j].rgwStatus[kStatusSleep] > 0 ||
  1646. g_Battle.rgEnemy[j].rgwStatus[kStatusParalyzed] > 0)
  1647. {
  1648. continue;
  1649. }
  1650. if (--g_Battle.rgEnemy[j].e.wIdleAnimSpeed == 0)
  1651. {
  1652. g_Battle.rgEnemy[j].wCurrentFrame++;
  1653. g_Battle.rgEnemy[j].e.wIdleAnimSpeed =
  1654. gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[g_Battle.rgEnemy[j].wObjectID].enemy.wEnemyID].wIdleAnimSpeed;
  1655. }
  1656. if (g_Battle.rgEnemy[j].wCurrentFrame >= g_Battle.rgEnemy[j].e.wIdleFrames)
  1657. {
  1658. g_Battle.rgEnemy[j].wCurrentFrame = 0;
  1659. }
  1660. }
  1661. PAL_BattleMakeScene();
  1662. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  1663. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  1664. x -= 16;
  1665. y += 16;
  1666. PAL_BattleUIUpdate();
  1667. if (i == 0)
  1668. {
  1669. if (sTarget == -1)
  1670. {
  1671. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  1672. {
  1673. g_Battle.rgEnemy[j].iColorShift = 6;
  1674. }
  1675. }
  1676. else
  1677. {
  1678. g_Battle.rgEnemy[sTarget].iColorShift = 6;
  1679. }
  1680. PAL_BattleDisplayStatChange();
  1681. PAL_BattleBackupStat();
  1682. }
  1683. VIDEO_UpdateScreen(NULL);
  1684. if (i == 1)
  1685. {
  1686. g_Battle.rgPlayer[wPlayerIndex].pos =
  1687. PAL_XY(PAL_X(g_Battle.rgPlayer[wPlayerIndex].pos) + 2,
  1688. PAL_Y(g_Battle.rgPlayer[wPlayerIndex].pos) + 1);
  1689. }
  1690. }
  1691. dist = 8;
  1692. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  1693. {
  1694. g_Battle.rgEnemy[i].iColorShift = 0;
  1695. }
  1696. if (sTarget == -1)
  1697. {
  1698. for (i = 0; i < 3; i++)
  1699. {
  1700. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  1701. {
  1702. x = PAL_X(g_Battle.rgEnemy[j].pos);
  1703. y = PAL_Y(g_Battle.rgEnemy[j].pos);
  1704. x -= dist;
  1705. y -= dist / 2;
  1706. g_Battle.rgEnemy[j].pos = PAL_XY(x, y);
  1707. }
  1708. PAL_BattleDelay(1, 0, TRUE);
  1709. dist /= -2;
  1710. }
  1711. }
  1712. else
  1713. {
  1714. x = PAL_X(g_Battle.rgEnemy[sTarget].pos);
  1715. y = PAL_Y(g_Battle.rgEnemy[sTarget].pos);
  1716. for (i = 0; i < 3; i++)
  1717. {
  1718. x -= dist;
  1719. dist /= -2;
  1720. y += dist;
  1721. g_Battle.rgEnemy[sTarget].pos = PAL_XY(x, y);
  1722. PAL_BattleDelay(1, 0, TRUE);
  1723. }
  1724. }
  1725. }
  1726. static VOID
  1727. PAL_BattleShowPlayerUseItemAnim(
  1728. WORD wPlayerIndex,
  1729. WORD wObjectID,
  1730. SHORT sTarget
  1731. )
  1732. /*++
  1733. Purpose:
  1734. Show the "use item" effect for player.
  1735. Parameters:
  1736. [IN] wPlayerIndex - the index of the player.
  1737. [IN] wObjectID - the object ID of the item to be used.
  1738. [IN] sTarget - the target player of the action.
  1739. Return value:
  1740. None.
  1741. --*/
  1742. {
  1743. int i, j;
  1744. PAL_BattleDelay(4, 0, TRUE);
  1745. g_Battle.rgPlayer[wPlayerIndex].pos =
  1746. PAL_XY(PAL_X(g_Battle.rgPlayer[wPlayerIndex].pos) - 15,
  1747. PAL_Y(g_Battle.rgPlayer[wPlayerIndex].pos) - 7);
  1748. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 5;
  1749. AUDIO_PlaySound(28);
  1750. for (i = 0; i <= 6; i++)
  1751. {
  1752. if (sTarget == -1)
  1753. {
  1754. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  1755. {
  1756. g_Battle.rgPlayer[j].iColorShift = i;
  1757. }
  1758. }
  1759. else
  1760. {
  1761. g_Battle.rgPlayer[sTarget].iColorShift = i;
  1762. }
  1763. PAL_BattleDelay(1, wObjectID, TRUE);
  1764. }
  1765. for (i = 5; i >= 0; i--)
  1766. {
  1767. if (sTarget == -1)
  1768. {
  1769. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  1770. {
  1771. g_Battle.rgPlayer[j].iColorShift = i;
  1772. }
  1773. }
  1774. else
  1775. {
  1776. g_Battle.rgPlayer[sTarget].iColorShift = i;
  1777. }
  1778. PAL_BattleDelay(1, wObjectID, TRUE);
  1779. }
  1780. }
  1781. VOID
  1782. PAL_BattleShowPlayerPreMagicAnim(
  1783. WORD wPlayerIndex,
  1784. BOOL fSummon
  1785. )
  1786. /*++
  1787. Purpose:
  1788. Show the effect for player before using a magic.
  1789. Parameters:
  1790. [IN] wPlayerIndex - the index of the player.
  1791. [IN] fSummon - TRUE if player is using a summon magic.
  1792. Return value:
  1793. None.
  1794. --*/
  1795. {
  1796. int i, j;
  1797. DWORD dwTime = SDL_GetTicks();
  1798. WORD wPlayerRole = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  1799. for (i = 0; i < 4; i++)
  1800. {
  1801. g_Battle.rgPlayer[wPlayerIndex].pos =
  1802. PAL_XY(PAL_X(g_Battle.rgPlayer[wPlayerIndex].pos) - (4 - i),
  1803. PAL_Y(g_Battle.rgPlayer[wPlayerIndex].pos) - (4 - i) / 2);
  1804. PAL_BattleDelay(1, 0, TRUE);
  1805. }
  1806. PAL_BattleDelay(2, 0, TRUE);
  1807. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 5;
  1808. if (!gConfig.fIsWIN95)
  1809. {
  1810. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwMagicSound[wPlayerRole]);
  1811. }
  1812. if (!fSummon)
  1813. {
  1814. int x, y, index;
  1815. x = PAL_X(g_Battle.rgPlayer[wPlayerIndex].pos);
  1816. y = PAL_Y(g_Battle.rgPlayer[wPlayerIndex].pos);
  1817. index = gpGlobals->g.rgwBattleEffectIndex[PAL_GetPlayerBattleSprite(wPlayerRole)][0];
  1818. index *= 10;
  1819. index += 15;
  1820. if (gConfig.fIsWIN95)
  1821. {
  1822. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwMagicSound[wPlayerRole]);
  1823. }
  1824. for (i = 0; i < 10; i++)
  1825. {
  1826. LPCBITMAPRLE b = PAL_SpriteGetFrame(g_Battle.lpEffectSprite, index++);
  1827. //
  1828. // Wait for the time of one frame. Accept input here.
  1829. //
  1830. PAL_DelayUntil(dwTime);
  1831. //
  1832. // Set the time of the next frame.
  1833. //
  1834. dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
  1835. //
  1836. // Update the gesture of enemies.
  1837. //
  1838. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  1839. {
  1840. if (g_Battle.rgEnemy[j].wObjectID == 0 ||
  1841. g_Battle.rgEnemy[j].rgwStatus[kStatusSleep] != 0 ||
  1842. g_Battle.rgEnemy[j].rgwStatus[kStatusParalyzed] != 0)
  1843. {
  1844. continue;
  1845. }
  1846. if (--g_Battle.rgEnemy[j].e.wIdleAnimSpeed == 0)
  1847. {
  1848. g_Battle.rgEnemy[j].wCurrentFrame++;
  1849. g_Battle.rgEnemy[j].e.wIdleAnimSpeed =
  1850. gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[g_Battle.rgEnemy[j].wObjectID].enemy.wEnemyID].wIdleAnimSpeed;
  1851. }
  1852. if (g_Battle.rgEnemy[j].wCurrentFrame >= g_Battle.rgEnemy[j].e.wIdleFrames)
  1853. {
  1854. g_Battle.rgEnemy[j].wCurrentFrame = 0;
  1855. }
  1856. }
  1857. PAL_BattleMakeScene();
  1858. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  1859. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  1860. PAL_BattleUIUpdate();
  1861. VIDEO_UpdateScreen(NULL);
  1862. }
  1863. }
  1864. PAL_BattleDelay(1, 0, TRUE);
  1865. }
  1866. static VOID
  1867. PAL_BattleShowPlayerDefMagicAnim(
  1868. WORD wPlayerIndex,
  1869. WORD wObjectID,
  1870. SHORT sTarget
  1871. )
  1872. /*++
  1873. Purpose:
  1874. Show the defensive magic effect for player.
  1875. Parameters:
  1876. [IN] wPlayerIndex - the index of the player.
  1877. [IN] wObjectID - the object ID of the magic to be used.
  1878. [IN] sTarget - the target player of the action.
  1879. Return value:
  1880. None.
  1881. --*/
  1882. {
  1883. LPSPRITE lpSpriteEffect;
  1884. int l, iMagicNum, iEffectNum, n, i, j, x, y;
  1885. DWORD dwTime = SDL_GetTicks();
  1886. iMagicNum = gpGlobals->g.rgObject[wObjectID].magic.wMagicNumber;
  1887. iEffectNum = gpGlobals->g.lprgMagic[iMagicNum].wEffect;
  1888. l = PAL_MKFGetDecompressedSize(iEffectNum, gpGlobals->f.fpFIRE);
  1889. if (l <= 0)
  1890. {
  1891. return;
  1892. }
  1893. lpSpriteEffect = (LPSPRITE)UTIL_malloc(l);
  1894. PAL_MKFDecompressChunk((LPBYTE)lpSpriteEffect, l, iEffectNum, gpGlobals->f.fpFIRE);
  1895. n = PAL_SpriteGetNumFrames(lpSpriteEffect);
  1896. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 6;
  1897. PAL_BattleDelay(1, 0, TRUE);
  1898. for (i = 0; i < n; i++)
  1899. {
  1900. LPCBITMAPRLE b = PAL_SpriteGetFrame(lpSpriteEffect, i);
  1901. if (i == gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay)
  1902. {
  1903. AUDIO_PlaySound(gpGlobals->g.lprgMagic[iMagicNum].wSound);
  1904. }
  1905. //
  1906. // Wait for the time of one frame. Accept input here.
  1907. //
  1908. PAL_DelayUntil(dwTime);
  1909. //
  1910. // Set the time of the next frame.
  1911. //
  1912. dwTime = SDL_GetTicks() +
  1913. ((SHORT)(gpGlobals->g.lprgMagic[iMagicNum].wSpeed) + 5) * 10;
  1914. PAL_BattleMakeScene();
  1915. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  1916. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeApplyToParty)
  1917. {
  1918. assert(sTarget == -1);
  1919. for (l = 0; l <= gpGlobals->wMaxPartyMemberIndex; l++)
  1920. {
  1921. x = PAL_X(g_Battle.rgPlayer[l].pos);
  1922. y = PAL_Y(g_Battle.rgPlayer[l].pos);
  1923. x += (SHORT) gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  1924. y += (SHORT) gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  1925. PAL_RLEBlitToSurface(b, gpScreen,
  1926. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  1927. }
  1928. }
  1929. else if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeApplyToPlayer)
  1930. {
  1931. assert(sTarget != -1);
  1932. x = PAL_X(g_Battle.rgPlayer[sTarget].pos);
  1933. y = PAL_Y(g_Battle.rgPlayer[sTarget].pos);
  1934. x += (SHORT) gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  1935. y += (SHORT) gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  1936. PAL_RLEBlitToSurface(b, gpScreen,
  1937. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  1938. //
  1939. // Repaint the previous player
  1940. //
  1941. if (sTarget > 0 && g_Battle.iHidingTime == 0)
  1942. {
  1943. if (gpGlobals->rgPlayerStatus[gpGlobals->rgParty[sTarget - 1].wPlayerRole][kStatusConfused] == 0)
  1944. {
  1945. LPCBITMAPRLE p = PAL_SpriteGetFrame(g_Battle.rgPlayer[sTarget - 1].lpSprite, g_Battle.rgPlayer[sTarget - 1].wCurrentFrame);
  1946. x = PAL_X(g_Battle.rgPlayer[sTarget - 1].pos);
  1947. y = PAL_Y(g_Battle.rgPlayer[sTarget - 1].pos);
  1948. x -= PAL_RLEGetWidth(p) / 2;
  1949. y -= PAL_RLEGetHeight(p);
  1950. PAL_RLEBlitToSurface(p, gpScreen, PAL_XY(x, y));
  1951. }
  1952. }
  1953. }
  1954. else
  1955. {
  1956. assert(FALSE);
  1957. }
  1958. PAL_BattleUIUpdate();
  1959. VIDEO_UpdateScreen(NULL);
  1960. }
  1961. free(lpSpriteEffect);
  1962. for (i = 0; i < 6; i++)
  1963. {
  1964. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeApplyToParty)
  1965. {
  1966. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  1967. {
  1968. g_Battle.rgPlayer[j].iColorShift = i;
  1969. }
  1970. }
  1971. else
  1972. {
  1973. g_Battle.rgPlayer[sTarget].iColorShift = i;
  1974. }
  1975. PAL_BattleDelay(1, 0, TRUE);
  1976. }
  1977. for (i = 6; i >= 0; i--)
  1978. {
  1979. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeApplyToParty)
  1980. {
  1981. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  1982. {
  1983. g_Battle.rgPlayer[j].iColorShift = i;
  1984. }
  1985. }
  1986. else
  1987. {
  1988. g_Battle.rgPlayer[sTarget].iColorShift = i;
  1989. }
  1990. PAL_BattleDelay(1, 0, TRUE);
  1991. }
  1992. }
  1993. static VOID
  1994. PAL_BattleShowPlayerOffMagicAnim(
  1995. WORD wPlayerIndex,
  1996. WORD wObjectID,
  1997. SHORT sTarget,
  1998. BOOL fSummon
  1999. )
  2000. /*++
  2001. Purpose:
  2002. Show the offensive magic animation for player.
  2003. Parameters:
  2004. [IN] wPlayerIndex - the index of the player.
  2005. [IN] wObjectID - the object ID of the magic to be used.
  2006. [IN] sTarget - the target enemy of the action.
  2007. Return value:
  2008. None.
  2009. --*/
  2010. {
  2011. LPSPRITE lpSpriteEffect;
  2012. int l, iMagicNum, iEffectNum, n, i, k, x, y, wave, blow;
  2013. DWORD dwTime = SDL_GetTicks();
  2014. iMagicNum = gpGlobals->g.rgObject[wObjectID].magic.wMagicNumber;
  2015. iEffectNum = gpGlobals->g.lprgMagic[iMagicNum].wEffect;
  2016. l = PAL_MKFGetDecompressedSize(iEffectNum, gpGlobals->f.fpFIRE);
  2017. if (l <= 0)
  2018. {
  2019. return;
  2020. }
  2021. lpSpriteEffect = (LPSPRITE)UTIL_malloc(l);
  2022. PAL_MKFDecompressChunk((LPBYTE)lpSpriteEffect, l, iEffectNum, gpGlobals->f.fpFIRE);
  2023. n = PAL_SpriteGetNumFrames(lpSpriteEffect);
  2024. if (gConfig.fIsWIN95 && wPlayerIndex != (WORD)-1)
  2025. {
  2026. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 6;
  2027. }
  2028. PAL_BattleDelay(1, 0, TRUE);
  2029. l = n - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2030. l *= (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wEffectTimes;
  2031. l += n;
  2032. l += gpGlobals->g.lprgMagic[iMagicNum].wShake;
  2033. wave = gpGlobals->wScreenWave;
  2034. gpGlobals->wScreenWave += gpGlobals->g.lprgMagic[iMagicNum].wWave;
  2035. if (gConfig.fIsWIN95 && !fSummon && gpGlobals->g.lprgMagic[iMagicNum].wSound != 0)
  2036. {
  2037. AUDIO_PlaySound(gpGlobals->g.lprgMagic[iMagicNum].wSound);
  2038. }
  2039. for (i = 0; i < l; i++)
  2040. {
  2041. LPCBITMAPRLE b;
  2042. if (!gConfig.fIsWIN95 && i == gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay && wPlayerIndex != (WORD)-1)
  2043. {
  2044. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 6;
  2045. }
  2046. blow = ((g_Battle.iBlow > 0) ? RandomLong(0, g_Battle.iBlow) : RandomLong(g_Battle.iBlow, 0));
  2047. for (k = 0; k <= g_Battle.wMaxEnemyIndex; k++)
  2048. {
  2049. if (g_Battle.rgEnemy[k].wObjectID == 0)
  2050. {
  2051. continue;
  2052. }
  2053. x = PAL_X(g_Battle.rgEnemy[k].pos) + blow;
  2054. y = PAL_Y(g_Battle.rgEnemy[k].pos) + blow / 2;
  2055. g_Battle.rgEnemy[k].pos = PAL_XY(x, y);
  2056. }
  2057. if (l - i > gpGlobals->g.lprgMagic[iMagicNum].wShake)
  2058. {
  2059. if (i < n)
  2060. {
  2061. k = i;
  2062. }
  2063. else
  2064. {
  2065. k = i - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2066. k %= n - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2067. k += gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2068. }
  2069. b = PAL_SpriteGetFrame(lpSpriteEffect, k);
  2070. if (!gConfig.fIsWIN95 && (i - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay) % n == 0)
  2071. {
  2072. AUDIO_PlaySound(gpGlobals->g.lprgMagic[iMagicNum].wSound);
  2073. }
  2074. }
  2075. else
  2076. {
  2077. VIDEO_ShakeScreen(i, 3);
  2078. b = PAL_SpriteGetFrame(lpSpriteEffect, (l - gpGlobals->g.lprgMagic[iMagicNum].wShake - 1) % n);
  2079. }
  2080. //
  2081. // Wait for the time of one frame. Accept input here.
  2082. //
  2083. PAL_DelayUntil(dwTime);
  2084. //
  2085. // Set the time of the next frame.
  2086. //
  2087. dwTime = SDL_GetTicks() +
  2088. ((SHORT)(gpGlobals->g.lprgMagic[iMagicNum].wSpeed) + 5) * 10;
  2089. PAL_BattleMakeScene();
  2090. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  2091. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeNormal)
  2092. {
  2093. assert(sTarget != -1);
  2094. x = PAL_X(g_Battle.rgEnemy[sTarget].pos);
  2095. y = PAL_Y(g_Battle.rgEnemy[sTarget].pos);
  2096. x += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  2097. y += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  2098. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2099. if (i == l - 1 && gpGlobals->wScreenWave < 9 &&
  2100. gpGlobals->g.lprgMagic[iMagicNum].wKeepEffect == 0xFFFF)
  2101. {
  2102. PAL_RLEBlitToSurface(b, g_Battle.lpBackground,
  2103. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2104. }
  2105. }
  2106. else if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackAll)
  2107. {
  2108. const int effectpos[3][2] = {{70, 140}, {100, 110}, {160, 100}};
  2109. assert(sTarget == -1);
  2110. for (k = 0; k < 3; k++)
  2111. {
  2112. x = effectpos[k][0];
  2113. y = effectpos[k][1];
  2114. x += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  2115. y += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  2116. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2117. if (i == l - 1 && gpGlobals->wScreenWave < 9 &&
  2118. gpGlobals->g.lprgMagic[iMagicNum].wKeepEffect == 0xFFFF)
  2119. {
  2120. PAL_RLEBlitToSurface(b, g_Battle.lpBackground,
  2121. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2122. }
  2123. }
  2124. }
  2125. else if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackWhole ||
  2126. gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackField)
  2127. {
  2128. assert(sTarget == -1);
  2129. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackWhole)
  2130. {
  2131. x = 120;
  2132. y = 100;
  2133. }
  2134. else
  2135. {
  2136. x = 160;
  2137. y = 200;
  2138. }
  2139. x += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  2140. y += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  2141. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2142. if (i == l - 1 && gpGlobals->wScreenWave < 9 &&
  2143. gpGlobals->g.lprgMagic[iMagicNum].wKeepEffect == 0xFFFF)
  2144. {
  2145. PAL_RLEBlitToSurface(b, g_Battle.lpBackground,
  2146. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2147. }
  2148. }
  2149. else
  2150. {
  2151. assert(FALSE);
  2152. }
  2153. PAL_BattleUIUpdate();
  2154. VIDEO_UpdateScreen(NULL);
  2155. }
  2156. gpGlobals->wScreenWave = wave;
  2157. VIDEO_ShakeScreen(0, 0);
  2158. free(lpSpriteEffect);
  2159. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  2160. {
  2161. g_Battle.rgEnemy[i].pos = g_Battle.rgEnemy[i].posOriginal;
  2162. }
  2163. }
  2164. static VOID
  2165. PAL_BattleShowEnemyMagicAnim(
  2166. WORD wObjectID,
  2167. SHORT sTarget
  2168. )
  2169. /*++
  2170. Purpose:
  2171. Show the offensive magic animation for enemy.
  2172. Parameters:
  2173. [IN] wObjectID - the object ID of the magic to be used.
  2174. [IN] sTarget - the target player index of the action.
  2175. Return value:
  2176. None.
  2177. --*/
  2178. {
  2179. LPSPRITE lpSpriteEffect;
  2180. int l, iMagicNum, iEffectNum, n, i, k, x, y, wave, blow;
  2181. DWORD dwTime = SDL_GetTicks();
  2182. iMagicNum = gpGlobals->g.rgObject[wObjectID].magic.wMagicNumber;
  2183. iEffectNum = gpGlobals->g.lprgMagic[iMagicNum].wEffect;
  2184. l = PAL_MKFGetDecompressedSize(iEffectNum, gpGlobals->f.fpFIRE);
  2185. if (l <= 0)
  2186. {
  2187. return;
  2188. }
  2189. lpSpriteEffect = (LPSPRITE)UTIL_malloc(l);
  2190. PAL_MKFDecompressChunk((LPBYTE)lpSpriteEffect, l, iEffectNum, gpGlobals->f.fpFIRE);
  2191. n = PAL_SpriteGetNumFrames(lpSpriteEffect);
  2192. l = n - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2193. l *= (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wEffectTimes;
  2194. l += n;
  2195. l += gpGlobals->g.lprgMagic[iMagicNum].wShake;
  2196. wave = gpGlobals->wScreenWave;
  2197. gpGlobals->wScreenWave += gpGlobals->g.lprgMagic[iMagicNum].wWave;
  2198. for (i = 0; i < l; i++)
  2199. {
  2200. LPCBITMAPRLE b;
  2201. blow = ((g_Battle.iBlow > 0) ? RandomLong(0, g_Battle.iBlow) : RandomLong(g_Battle.iBlow, 0));
  2202. for (k = 0; k <= gpGlobals->wMaxPartyMemberIndex; k++)
  2203. {
  2204. x = PAL_X(g_Battle.rgPlayer[k].pos) + blow;
  2205. y = PAL_Y(g_Battle.rgPlayer[k].pos) + blow / 2;
  2206. g_Battle.rgPlayer[k].pos = PAL_XY(x, y);
  2207. }
  2208. if (l - i > gpGlobals->g.lprgMagic[iMagicNum].wShake)
  2209. {
  2210. if (i < n)
  2211. {
  2212. k = i;
  2213. }
  2214. else
  2215. {
  2216. k = i - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2217. k %= n - gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2218. k += gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay;
  2219. }
  2220. b = PAL_SpriteGetFrame(lpSpriteEffect, k);
  2221. if (i == gpGlobals->g.lprgMagic[iMagicNum].wSoundDelay)
  2222. {
  2223. AUDIO_PlaySound(gpGlobals->g.lprgMagic[iMagicNum].wSound);
  2224. }
  2225. }
  2226. else
  2227. {
  2228. VIDEO_ShakeScreen(i, 3);
  2229. b = PAL_SpriteGetFrame(lpSpriteEffect, (l - gpGlobals->g.lprgMagic[iMagicNum].wShake - 1) % n);
  2230. }
  2231. //
  2232. // Wait for the time of one frame. Accept input here.
  2233. //
  2234. PAL_DelayUntil(dwTime);
  2235. //
  2236. // Set the time of the next frame.
  2237. //
  2238. dwTime = SDL_GetTicks() +
  2239. ((SHORT)(gpGlobals->g.lprgMagic[iMagicNum].wSpeed) + 5) * 10;
  2240. PAL_BattleMakeScene();
  2241. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  2242. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeNormal)
  2243. {
  2244. assert(sTarget != -1);
  2245. x = PAL_X(g_Battle.rgPlayer[sTarget].pos);
  2246. y = PAL_Y(g_Battle.rgPlayer[sTarget].pos);
  2247. x += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  2248. y += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  2249. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2250. if (i == l - 1 && gpGlobals->wScreenWave < 9 &&
  2251. gpGlobals->g.lprgMagic[iMagicNum].wKeepEffect == 0xFFFF)
  2252. {
  2253. PAL_RLEBlitToSurface(b, g_Battle.lpBackground,
  2254. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2255. }
  2256. }
  2257. else if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackAll)
  2258. {
  2259. const int effectpos[3][2] = {{180, 180}, {234, 170}, {270, 146}};
  2260. assert(sTarget == -1);
  2261. for (k = 0; k < 3; k++)
  2262. {
  2263. x = effectpos[k][0];
  2264. y = effectpos[k][1];
  2265. x += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  2266. y += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  2267. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2268. if (i == l - 1 && gpGlobals->wScreenWave < 9 &&
  2269. gpGlobals->g.lprgMagic[iMagicNum].wKeepEffect == 0xFFFF)
  2270. {
  2271. PAL_RLEBlitToSurface(b, g_Battle.lpBackground,
  2272. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2273. }
  2274. }
  2275. }
  2276. else if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackWhole ||
  2277. gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackField)
  2278. {
  2279. assert(sTarget == -1);
  2280. if (gpGlobals->g.lprgMagic[iMagicNum].wType == kMagicTypeAttackWhole)
  2281. {
  2282. x = 240;
  2283. y = 150;
  2284. }
  2285. else
  2286. {
  2287. x = 160;
  2288. y = 200;
  2289. }
  2290. x += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wXOffset;
  2291. y += (SHORT)gpGlobals->g.lprgMagic[iMagicNum].wYOffset;
  2292. PAL_RLEBlitToSurface(b, gpScreen, PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2293. if (i == l - 1 && gpGlobals->wScreenWave < 9 &&
  2294. gpGlobals->g.lprgMagic[iMagicNum].wKeepEffect == 0xFFFF)
  2295. {
  2296. PAL_RLEBlitToSurface(b, g_Battle.lpBackground,
  2297. PAL_XY(x - PAL_RLEGetWidth(b) / 2, y - PAL_RLEGetHeight(b)));
  2298. }
  2299. }
  2300. else
  2301. {
  2302. assert(FALSE);
  2303. }
  2304. PAL_BattleUIUpdate();
  2305. VIDEO_UpdateScreen(NULL);
  2306. }
  2307. gpGlobals->wScreenWave = wave;
  2308. VIDEO_ShakeScreen(0, 0);
  2309. free(lpSpriteEffect);
  2310. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  2311. {
  2312. g_Battle.rgPlayer[i].pos = g_Battle.rgPlayer[i].posOriginal;
  2313. }
  2314. }
  2315. static VOID
  2316. PAL_BattleShowPlayerSummonMagicAnim(
  2317. WORD wPlayerIndex,
  2318. WORD wObjectID
  2319. )
  2320. /*++
  2321. Purpose:
  2322. Show the summon magic animation for player.
  2323. Parameters:
  2324. [IN] wPlayerIndex - the index of the player.
  2325. [IN] wObjectID - the object ID of the magic to be used.
  2326. Return value:
  2327. None.
  2328. --*/
  2329. {
  2330. int i, j;
  2331. WORD wMagicNum = gpGlobals->g.rgObject[wObjectID].magic.wMagicNumber;
  2332. WORD wEffectMagicID = 0;
  2333. DWORD dwTime = SDL_GetTicks();
  2334. for (wEffectMagicID = 0; wEffectMagicID < MAX_OBJECTS; wEffectMagicID++)
  2335. {
  2336. if (gpGlobals->g.rgObject[wEffectMagicID].magic.wMagicNumber ==
  2337. gpGlobals->g.lprgMagic[wMagicNum].wEffect)
  2338. {
  2339. break;
  2340. }
  2341. }
  2342. assert(wEffectMagicID < MAX_OBJECTS);
  2343. //
  2344. // Sound should be played before magic begins
  2345. //
  2346. if (gConfig.fIsWIN95)
  2347. {
  2348. AUDIO_PlaySound(gpGlobals->g.lprgMagic[wMagicNum].wSound);
  2349. }
  2350. //
  2351. // Brighten the players
  2352. //
  2353. for (i = 1; i <= 10; i++)
  2354. {
  2355. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  2356. {
  2357. g_Battle.rgPlayer[j].iColorShift = i;
  2358. }
  2359. PAL_BattleDelay(1, wObjectID, TRUE);
  2360. }
  2361. PAL_BattleBackupScene();
  2362. //
  2363. // Load the sprite of the summoned god
  2364. //
  2365. j = gpGlobals->g.lprgMagic[wMagicNum].wSummonEffect + 10;
  2366. i = PAL_MKFGetDecompressedSize(j, gpGlobals->f.fpF);
  2367. g_Battle.lpSummonSprite = UTIL_malloc(i);
  2368. PAL_MKFDecompressChunk(g_Battle.lpSummonSprite, i, j, gpGlobals->f.fpF);
  2369. g_Battle.iSummonFrame = 0;
  2370. g_Battle.posSummon = PAL_XY(230 + (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wXOffset),
  2371. 155 + (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wYOffset));
  2372. g_Battle.sBackgroundColorShift = (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wEffectTimes);
  2373. //
  2374. // Fade in the summoned god
  2375. //
  2376. PAL_BattleMakeScene();
  2377. PAL_BattleFadeScene();
  2378. //
  2379. // Show the animation of the summoned god
  2380. // TODO: There is still something missing here compared to the original game.
  2381. //
  2382. while (g_Battle.iSummonFrame < PAL_SpriteGetNumFrames(g_Battle.lpSummonSprite) - 1)
  2383. {
  2384. //
  2385. // Wait for the time of one frame. Accept input here.
  2386. //
  2387. PAL_DelayUntil(dwTime);
  2388. //
  2389. // Set the time of the next frame.
  2390. //
  2391. dwTime = SDL_GetTicks() +
  2392. ((SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wSpeed) + 5) * 10;
  2393. PAL_BattleMakeScene();
  2394. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  2395. PAL_BattleUIUpdate();
  2396. VIDEO_UpdateScreen(NULL);
  2397. g_Battle.iSummonFrame++;
  2398. }
  2399. //
  2400. // Show the actual magic effect
  2401. //
  2402. PAL_BattleShowPlayerOffMagicAnim((WORD)-1, wEffectMagicID, -1, TRUE);
  2403. }
  2404. static VOID
  2405. PAL_BattleShowPostMagicAnim(
  2406. VOID
  2407. )
  2408. /*++
  2409. Purpose:
  2410. Show the post-magic animation.
  2411. Parameters:
  2412. None
  2413. Return value:
  2414. None.
  2415. --*/
  2416. {
  2417. int i, j, x, y, dist = 8;
  2418. PAL_POS rgEnemyPosBak[MAX_ENEMIES_IN_TEAM];
  2419. for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
  2420. {
  2421. rgEnemyPosBak[i] = g_Battle.rgEnemy[i].pos;
  2422. }
  2423. for (i = 0; i < 3; i++)
  2424. {
  2425. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  2426. {
  2427. if (g_Battle.rgEnemy[j].e.wHealth == g_Battle.rgEnemy[j].wPrevHP)
  2428. {
  2429. continue;
  2430. }
  2431. x = PAL_X(g_Battle.rgEnemy[j].pos);
  2432. y = PAL_Y(g_Battle.rgEnemy[j].pos);
  2433. x -= dist;
  2434. y -= dist / 2;
  2435. g_Battle.rgEnemy[j].pos = PAL_XY(x, y);
  2436. g_Battle.rgEnemy[j].iColorShift = ((i == 1) ? 6 : 0);
  2437. }
  2438. PAL_BattleDelay(1, 0, TRUE);
  2439. dist /= -2;
  2440. }
  2441. for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
  2442. {
  2443. g_Battle.rgEnemy[i].pos = rgEnemyPosBak[i];
  2444. }
  2445. PAL_BattleDelay(1, 0, TRUE);
  2446. }
  2447. static VOID
  2448. PAL_BattlePlayerValidateAction(
  2449. WORD wPlayerIndex
  2450. )
  2451. /*++
  2452. Purpose:
  2453. Validate player's action, fallback to other action when needed.
  2454. Parameters:
  2455. [IN] wPlayerIndex - the index of the player.
  2456. Return value:
  2457. None.
  2458. --*/
  2459. {
  2460. const WORD wPlayerRole = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  2461. const WORD wObjectID = g_Battle.rgPlayer[wPlayerIndex].action.wActionID;
  2462. const SHORT sTarget = g_Battle.rgPlayer[wPlayerIndex].action.sTarget;
  2463. BOOL fValid = TRUE, fToEnemy = FALSE;
  2464. WORD w;
  2465. int i;
  2466. switch (g_Battle.rgPlayer[wPlayerIndex].action.ActionType)
  2467. {
  2468. case kBattleActionAttack:
  2469. fToEnemy = TRUE;
  2470. break;
  2471. case kBattleActionPass:
  2472. break;
  2473. case kBattleActionDefend:
  2474. break;
  2475. case kBattleActionMagic:
  2476. //
  2477. // Make sure player actually has the magic to be used
  2478. //
  2479. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  2480. {
  2481. if (gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole] == wObjectID)
  2482. {
  2483. break; // player has this magic
  2484. }
  2485. }
  2486. if (i >= MAX_PLAYER_MAGICS)
  2487. {
  2488. fValid = FALSE;
  2489. }
  2490. w = gpGlobals->g.rgObject[wObjectID].magic.wMagicNumber;
  2491. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] > 0)
  2492. {
  2493. //
  2494. // Player is silenced
  2495. //
  2496. fValid = FALSE;
  2497. }
  2498. if (gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] <
  2499. gpGlobals->g.lprgMagic[w].wCostMP)
  2500. {
  2501. //
  2502. // No enough MP
  2503. //
  2504. fValid = FALSE;
  2505. }
  2506. //
  2507. // Fallback to physical attack if player is using an offensive magic,
  2508. // defend if player is using a defensive or healing magic
  2509. //
  2510. if (gpGlobals->g.rgObject[wObjectID].magic.wFlags & kMagicFlagUsableToEnemy)
  2511. {
  2512. if (!fValid)
  2513. {
  2514. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionAttack;
  2515. }
  2516. else if (gpGlobals->g.rgObject[wObjectID].magic.wFlags & kMagicFlagApplyToAll)
  2517. {
  2518. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = -1;
  2519. }
  2520. else if (sTarget == -1)
  2521. {
  2522. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = PAL_BattleSelectAutoTarget();
  2523. }
  2524. fToEnemy = TRUE;
  2525. }
  2526. else
  2527. {
  2528. if (!fValid)
  2529. {
  2530. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionDefend;
  2531. }
  2532. else if (gpGlobals->g.rgObject[wObjectID].magic.wFlags & kMagicFlagApplyToAll)
  2533. {
  2534. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = -1;
  2535. }
  2536. else if (g_Battle.rgPlayer[wPlayerIndex].action.sTarget == -1)
  2537. {
  2538. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = wPlayerIndex;
  2539. }
  2540. }
  2541. break;
  2542. case kBattleActionCoopMagic:
  2543. fToEnemy = TRUE;
  2544. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  2545. {
  2546. w = gpGlobals->rgParty[i].wPlayerRole;
  2547. #ifdef PAL_CLASSIC
  2548. if (PAL_IsPlayerDying(w) ||
  2549. gpGlobals->rgPlayerStatus[w][kStatusSilence] > 0 ||
  2550. gpGlobals->rgPlayerStatus[w][kStatusSleep] > 0 ||
  2551. gpGlobals->rgPlayerStatus[w][kStatusParalyzed] > 0 ||
  2552. gpGlobals->rgPlayerStatus[w][kStatusConfused] > 0)
  2553. #else
  2554. if (PAL_IsPlayerDying(w) ||
  2555. gpGlobals->rgPlayerStatus[w][kStatusSilence] > 0 ||
  2556. gpGlobals->rgPlayerStatus[w][kStatusSleep] > 0 ||
  2557. gpGlobals->rgPlayerStatus[w][kStatusConfused] > 0 ||
  2558. g_Battle.rgPlayer[i].flTimeMeter < 100 ||
  2559. (g_Battle.rgPlayer[i].state == kFighterAct && i != wPlayerIndex))
  2560. #endif
  2561. {
  2562. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionAttack;
  2563. break;
  2564. }
  2565. }
  2566. if (g_Battle.rgPlayer[wPlayerIndex].action.ActionType == kBattleActionCoopMagic)
  2567. {
  2568. if (gpGlobals->g.rgObject[wObjectID].magic.wFlags & kMagicFlagApplyToAll)
  2569. {
  2570. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = -1;
  2571. }
  2572. else if (sTarget == -1)
  2573. {
  2574. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = PAL_BattleSelectAutoTarget();
  2575. }
  2576. }
  2577. break;
  2578. case kBattleActionFlee:
  2579. break;
  2580. case kBattleActionThrowItem:
  2581. fToEnemy = TRUE;
  2582. if (PAL_GetItemAmount(wObjectID) == 0)
  2583. {
  2584. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionAttack;
  2585. }
  2586. else if (gpGlobals->g.rgObject[wObjectID].item.wFlags & kItemFlagApplyToAll)
  2587. {
  2588. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = -1;
  2589. }
  2590. else if (g_Battle.rgPlayer[wPlayerIndex].action.sTarget == -1)
  2591. {
  2592. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = PAL_BattleSelectAutoTarget();
  2593. }
  2594. break;
  2595. case kBattleActionUseItem:
  2596. if (PAL_GetItemAmount(wObjectID) == 0)
  2597. {
  2598. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionDefend;
  2599. }
  2600. else if (gpGlobals->g.rgObject[wObjectID].item.wFlags & kItemFlagApplyToAll)
  2601. {
  2602. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = -1;
  2603. }
  2604. else if (g_Battle.rgPlayer[wPlayerIndex].action.sTarget == -1)
  2605. {
  2606. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = wPlayerIndex;
  2607. }
  2608. break;
  2609. case kBattleActionAttackMate:
  2610. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] == 0)
  2611. {
  2612. //
  2613. // Attack enemies instead if player is not confused
  2614. //
  2615. fToEnemy = TRUE;
  2616. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionAttack;
  2617. }
  2618. else
  2619. {
  2620. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  2621. {
  2622. if (i != wPlayerIndex &&
  2623. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] != 0)
  2624. {
  2625. break;
  2626. }
  2627. }
  2628. if (i > gpGlobals->wMaxPartyMemberIndex)
  2629. {
  2630. //
  2631. // Attack enemies if no one else is alive
  2632. //
  2633. fToEnemy = TRUE;
  2634. g_Battle.rgPlayer[wPlayerIndex].action.ActionType = kBattleActionAttack;
  2635. }
  2636. }
  2637. break;
  2638. }
  2639. //
  2640. // Check if player can attack all enemies at once, or attack one enemy
  2641. //
  2642. if (g_Battle.rgPlayer[wPlayerIndex].action.ActionType == kBattleActionAttack)
  2643. {
  2644. if (sTarget == -1)
  2645. {
  2646. if (!PAL_PlayerCanAttackAll(wPlayerRole))
  2647. {
  2648. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = PAL_BattleSelectAutoTarget();
  2649. }
  2650. }
  2651. else if (PAL_PlayerCanAttackAll(wPlayerRole))
  2652. {
  2653. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = -1;
  2654. }
  2655. }
  2656. if (fToEnemy && g_Battle.rgPlayer[wPlayerIndex].action.sTarget >= 0)
  2657. {
  2658. if (g_Battle.rgEnemy[g_Battle.rgPlayer[wPlayerIndex].action.sTarget].wObjectID == 0)
  2659. {
  2660. g_Battle.rgPlayer[wPlayerIndex].action.sTarget = PAL_BattleSelectAutoTarget();
  2661. assert(g_Battle.rgPlayer[wPlayerIndex].action.sTarget >= 0);
  2662. }
  2663. }
  2664. }
  2665. VOID
  2666. PAL_BattlePlayerPerformAction(
  2667. WORD wPlayerIndex
  2668. )
  2669. /*++
  2670. Purpose:
  2671. Perform the selected action for a player.
  2672. Parameters:
  2673. [IN] wPlayerIndex - the index of the player.
  2674. Return value:
  2675. None.
  2676. --*/
  2677. {
  2678. SHORT sDamage;
  2679. WORD wPlayerRole = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  2680. SHORT sTarget;
  2681. int x, y;
  2682. int i, j, t;
  2683. WORD str, def, res, wObject, wMagicNum;
  2684. BOOL fCritical;
  2685. WORD rgwCoopPos[3][2] = {{208, 157}, {234, 170}, {260, 183}};
  2686. #ifndef PAL_CLASSIC
  2687. BOOL fPoisoned, fCheckPoison;
  2688. #endif
  2689. g_Battle.wMovingPlayerIndex = wPlayerIndex;
  2690. g_Battle.iBlow = 0;
  2691. PAL_BattlePlayerValidateAction(wPlayerIndex);
  2692. PAL_BattleBackupStat();
  2693. sTarget = g_Battle.rgPlayer[wPlayerIndex].action.sTarget;
  2694. switch (g_Battle.rgPlayer[wPlayerIndex].action.ActionType)
  2695. {
  2696. case kBattleActionAttack:
  2697. if (sTarget != -1)
  2698. {
  2699. //
  2700. // Attack one enemy
  2701. //
  2702. for (t = 0; t < (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusDualAttack] ? 2 : 1); t++)
  2703. {
  2704. str = PAL_GetPlayerAttackStrength(wPlayerRole);
  2705. def = g_Battle.rgEnemy[sTarget].e.wDefense;
  2706. def += (g_Battle.rgEnemy[sTarget].e.wLevel + 6) * 4;
  2707. res = g_Battle.rgEnemy[sTarget].e.wPhysicalResistance;
  2708. fCritical = FALSE;
  2709. sDamage = PAL_CalcPhysicalAttackDamage(str, def, res);
  2710. sDamage += RandomLong(1, 2);
  2711. if (RandomLong(0, 5) == 0 ||
  2712. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusBravery] > 0)
  2713. {
  2714. //
  2715. // Critical Hit
  2716. //
  2717. sDamage *= 3;
  2718. fCritical = TRUE;
  2719. }
  2720. if (wPlayerRole == 0 && RandomLong(0, 11) == 0)
  2721. {
  2722. //
  2723. // Bonus hit for Li Xiaoyao
  2724. //
  2725. sDamage *= 2;
  2726. fCritical = TRUE;
  2727. }
  2728. sDamage = (SHORT)(sDamage * RandomFloat(1, 1.125));
  2729. if (sDamage <= 0)
  2730. {
  2731. sDamage = 1;
  2732. }
  2733. g_Battle.rgEnemy[sTarget].e.wHealth -= sDamage;
  2734. if (t == 0)
  2735. {
  2736. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 7;
  2737. PAL_BattleDelay(4, 0, TRUE);
  2738. }
  2739. PAL_BattleShowPlayerAttackAnim(wPlayerIndex, fCritical);
  2740. }
  2741. }
  2742. else
  2743. {
  2744. //
  2745. // Attack all enemies
  2746. //
  2747. for (t = 0; t < (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusDualAttack] ? 2 : 1); t++)
  2748. {
  2749. int division = 1;
  2750. const int index[MAX_ENEMIES_IN_TEAM] = {2, 1, 0, 4, 3};
  2751. fCritical =
  2752. (RandomLong(0, 5) == 0 || gpGlobals->rgPlayerStatus[wPlayerRole][kStatusBravery] > 0);
  2753. if (t == 0)
  2754. {
  2755. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 7;
  2756. PAL_BattleDelay(4, 0, TRUE);
  2757. }
  2758. for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
  2759. {
  2760. if (g_Battle.rgEnemy[index[i]].wObjectID == 0 ||
  2761. index[i] > g_Battle.wMaxEnemyIndex)
  2762. {
  2763. continue;
  2764. }
  2765. str = PAL_GetPlayerAttackStrength(wPlayerRole);
  2766. def = g_Battle.rgEnemy[index[i]].e.wDefense;
  2767. def += (g_Battle.rgEnemy[index[i]].e.wLevel + 6) * 4;
  2768. res = g_Battle.rgEnemy[index[i]].e.wPhysicalResistance;
  2769. sDamage = PAL_CalcPhysicalAttackDamage(str, def, res);
  2770. sDamage += RandomLong(1, 2);
  2771. if (fCritical)
  2772. {
  2773. //
  2774. // Critical Hit
  2775. //
  2776. sDamage *= 3;
  2777. }
  2778. sDamage /= division;
  2779. sDamage = (SHORT)(sDamage * RandomFloat(1, 1.125));
  2780. if (sDamage <= 0)
  2781. {
  2782. sDamage = 1;
  2783. }
  2784. g_Battle.rgEnemy[index[i]].e.wHealth -= sDamage;
  2785. division++;
  2786. if (division > 3)
  2787. {
  2788. division = 3;
  2789. }
  2790. }
  2791. PAL_BattleShowPlayerAttackAnim(wPlayerIndex, fCritical);
  2792. }
  2793. }
  2794. PAL_BattleUpdateFighters();
  2795. PAL_BattleMakeScene();
  2796. PAL_BattleDelay(3, 0, TRUE);
  2797. gpGlobals->Exp.rgAttackExp[wPlayerRole].wCount++;
  2798. gpGlobals->Exp.rgHealthExp[wPlayerRole].wCount += RandomLong(2, 3);
  2799. break;
  2800. case kBattleActionAttackMate:
  2801. //
  2802. // Check if there is someone else who is alive
  2803. //
  2804. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  2805. {
  2806. if (i == wPlayerIndex)
  2807. {
  2808. continue;
  2809. }
  2810. if (gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] > 0)
  2811. {
  2812. break;
  2813. }
  2814. }
  2815. if (i <= gpGlobals->wMaxPartyMemberIndex)
  2816. {
  2817. //
  2818. // Pick a target randomly
  2819. //
  2820. do
  2821. {
  2822. sTarget = RandomLong(0, gpGlobals->wMaxPartyMemberIndex);
  2823. } while (sTarget == wPlayerIndex || gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[sTarget].wPlayerRole] == 0);
  2824. for (j = 0; j < 2; j++)
  2825. {
  2826. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 8;
  2827. PAL_BattleDelay(1, 0, TRUE);
  2828. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 0;
  2829. PAL_BattleDelay(1, 0, TRUE);
  2830. }
  2831. PAL_BattleDelay(2, 0, TRUE);
  2832. x = PAL_X(g_Battle.rgPlayer[sTarget].pos) + 30;
  2833. y = PAL_Y(g_Battle.rgPlayer[sTarget].pos) + 12;
  2834. g_Battle.rgPlayer[wPlayerIndex].pos = PAL_XY(x, y);
  2835. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 8;
  2836. PAL_BattleDelay(5, 0, TRUE);
  2837. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 9;
  2838. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwWeaponSound[wPlayerRole]);
  2839. str = PAL_GetPlayerAttackStrength(wPlayerRole);
  2840. def = PAL_GetPlayerDefense(gpGlobals->rgParty[sTarget].wPlayerRole);
  2841. if (g_Battle.rgPlayer[sTarget].fDefending)
  2842. {
  2843. def *= 2;
  2844. }
  2845. sDamage = PAL_CalcPhysicalAttackDamage(str, def, 2);
  2846. if (gpGlobals->rgPlayerStatus[gpGlobals->rgParty[sTarget].wPlayerRole][kStatusProtect] > 0)
  2847. {
  2848. sDamage /= 2;
  2849. }
  2850. if (sDamage <= 0)
  2851. {
  2852. sDamage = 1;
  2853. }
  2854. if (sDamage > (SHORT)gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[sTarget].wPlayerRole])
  2855. {
  2856. sDamage = gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[sTarget].wPlayerRole];
  2857. }
  2858. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[sTarget].wPlayerRole] -= sDamage;
  2859. g_Battle.rgPlayer[sTarget].pos =
  2860. PAL_XY(PAL_X(g_Battle.rgPlayer[sTarget].pos) - 12,
  2861. PAL_Y(g_Battle.rgPlayer[sTarget].pos) - 6);
  2862. PAL_BattleDelay(1, 0, TRUE);
  2863. g_Battle.rgPlayer[sTarget].iColorShift = 6;
  2864. PAL_BattleDelay(1, 0, TRUE);
  2865. PAL_BattleDisplayStatChange();
  2866. g_Battle.rgPlayer[sTarget].iColorShift = 0;
  2867. PAL_BattleDelay(4, 0, TRUE);
  2868. PAL_BattleUpdateFighters();
  2869. PAL_BattleDelay(4, 0, TRUE);
  2870. }
  2871. break;
  2872. case kBattleActionCoopMagic:
  2873. wObject = PAL_GetPlayerCooperativeMagic(gpGlobals->rgParty[wPlayerIndex].wPlayerRole);
  2874. wMagicNum = gpGlobals->g.rgObject[wObject].magic.wMagicNumber;
  2875. if (gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeSummon)
  2876. {
  2877. PAL_BattleShowPlayerPreMagicAnim(wPlayerIndex, TRUE);
  2878. PAL_BattleShowPlayerSummonMagicAnim((WORD)-1, wObject);
  2879. }
  2880. else
  2881. {
  2882. //
  2883. // Sound should be played before action begins
  2884. //
  2885. AUDIO_PlaySound(29);
  2886. for (i = 1; i <= 6; i++)
  2887. {
  2888. //
  2889. // Update the position for the player who invoked the action
  2890. //
  2891. x = PAL_X(g_Battle.rgPlayer[wPlayerIndex].posOriginal) * (6 - i);
  2892. y = PAL_Y(g_Battle.rgPlayer[wPlayerIndex].posOriginal) * (6 - i);
  2893. x += rgwCoopPos[0][0] * i;
  2894. y += rgwCoopPos[0][1] * i;
  2895. x /= 6;
  2896. y /= 6;
  2897. g_Battle.rgPlayer[wPlayerIndex].pos = PAL_XY(x, y);
  2898. //
  2899. // Update the position for other players
  2900. //
  2901. t = 0;
  2902. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  2903. {
  2904. if ((WORD)j == wPlayerIndex)
  2905. {
  2906. continue;
  2907. }
  2908. t++;
  2909. x = PAL_X(g_Battle.rgPlayer[j].posOriginal) * (6 - i);
  2910. y = PAL_Y(g_Battle.rgPlayer[j].posOriginal) * (6 - i);
  2911. x += rgwCoopPos[t][0] * i;
  2912. y += rgwCoopPos[t][1] * i;
  2913. x /= 6;
  2914. y /= 6;
  2915. g_Battle.rgPlayer[j].pos = PAL_XY(x, y);
  2916. }
  2917. PAL_BattleDelay(1, 0, TRUE);
  2918. }
  2919. for (i = gpGlobals->wMaxPartyMemberIndex; i >= 0; i--)
  2920. {
  2921. if ((WORD)i == wPlayerIndex)
  2922. {
  2923. continue;
  2924. }
  2925. g_Battle.rgPlayer[i].wCurrentFrame = 5;
  2926. PAL_BattleDelay(3, 0, TRUE);
  2927. }
  2928. g_Battle.rgPlayer[wPlayerIndex].iColorShift = 6;
  2929. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 5;
  2930. PAL_BattleDelay(5, 0, TRUE);
  2931. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 6;
  2932. g_Battle.rgPlayer[wPlayerIndex].iColorShift = 0;
  2933. PAL_BattleDelay(3, 0, TRUE);
  2934. PAL_BattleShowPlayerOffMagicAnim((WORD)-1, wObject, sTarget, FALSE);
  2935. }
  2936. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  2937. {
  2938. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] -=
  2939. gpGlobals->g.lprgMagic[wMagicNum].wCostMP;
  2940. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole]) <= 0)
  2941. {
  2942. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] = 1;
  2943. }
  2944. //
  2945. // Reset the time meter for everyone when using coopmagic
  2946. //
  2947. #ifdef PAL_CLASSIC
  2948. g_Battle.rgPlayer[i].state = kFighterWait;
  2949. #else
  2950. g_Battle.rgPlayer[i].flTimeMeter = 0;
  2951. g_Battle.rgPlayer[i].flTimeSpeedModifier = 2;
  2952. #endif
  2953. }
  2954. PAL_BattleBackupStat(); // so that "damages" to players won't be shown
  2955. str = 0;
  2956. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  2957. {
  2958. str += PAL_GetPlayerAttackStrength(gpGlobals->rgParty[i].wPlayerRole);
  2959. str += PAL_GetPlayerMagicStrength(gpGlobals->rgParty[i].wPlayerRole);
  2960. }
  2961. str /= 4;
  2962. //
  2963. // Inflict damage to enemies
  2964. //
  2965. if (sTarget == -1)
  2966. {
  2967. //
  2968. // Attack all enemies
  2969. //
  2970. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  2971. {
  2972. if (g_Battle.rgEnemy[i].wObjectID == 0)
  2973. {
  2974. continue;
  2975. }
  2976. def = g_Battle.rgEnemy[i].e.wDefense;
  2977. def += (g_Battle.rgEnemy[i].e.wLevel + 6) * 4;
  2978. sDamage = PAL_CalcMagicDamage(str, def,
  2979. g_Battle.rgEnemy[i].e.wElemResistance, g_Battle.rgEnemy[i].e.wPoisonResistance, wObject);
  2980. if (sDamage <= 0)
  2981. {
  2982. sDamage = 1;
  2983. }
  2984. g_Battle.rgEnemy[i].e.wHealth -= sDamage;
  2985. }
  2986. }
  2987. else
  2988. {
  2989. //
  2990. // Attack one enemy
  2991. //
  2992. def = g_Battle.rgEnemy[sTarget].e.wDefense;
  2993. def += (g_Battle.rgEnemy[sTarget].e.wLevel + 6) * 4;
  2994. sDamage = PAL_CalcMagicDamage(str, def,
  2995. g_Battle.rgEnemy[sTarget].e.wElemResistance, g_Battle.rgEnemy[sTarget].e.wPoisonResistance, wObject);
  2996. if (sDamage <= 0)
  2997. {
  2998. sDamage = 1;
  2999. }
  3000. g_Battle.rgEnemy[sTarget].e.wHealth -= sDamage;
  3001. }
  3002. PAL_BattleDisplayStatChange();
  3003. PAL_BattleShowPostMagicAnim();
  3004. PAL_BattleDelay(5, 0, TRUE);
  3005. if (gpGlobals->g.lprgMagic[wMagicNum].wType != kMagicTypeSummon)
  3006. {
  3007. PAL_BattlePostActionCheck(FALSE);
  3008. //
  3009. // Move all players back to the original position
  3010. //
  3011. for (i = 1; i <= 6; i++)
  3012. {
  3013. //
  3014. // Update the position for the player who invoked the action
  3015. //
  3016. x = PAL_X(g_Battle.rgPlayer[wPlayerIndex].posOriginal) * i;
  3017. y = PAL_Y(g_Battle.rgPlayer[wPlayerIndex].posOriginal) * i;
  3018. x += rgwCoopPos[0][0] * (6 - i);
  3019. y += rgwCoopPos[0][1] * (6 - i);
  3020. x /= 6;
  3021. y /= 6;
  3022. g_Battle.rgPlayer[wPlayerIndex].pos = PAL_XY(x, y);
  3023. //
  3024. // Update the position for other players
  3025. //
  3026. t = 0;
  3027. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  3028. {
  3029. g_Battle.rgPlayer[j].wCurrentFrame = 0;
  3030. if ((WORD)j == wPlayerIndex)
  3031. {
  3032. continue;
  3033. }
  3034. t++;
  3035. x = PAL_X(g_Battle.rgPlayer[j].posOriginal) * i;
  3036. y = PAL_Y(g_Battle.rgPlayer[j].posOriginal) * i;
  3037. x += rgwCoopPos[t][0] * (6 - i);
  3038. y += rgwCoopPos[t][1] * (6 - i);
  3039. x /= 6;
  3040. y /= 6;
  3041. g_Battle.rgPlayer[j].pos = PAL_XY(x, y);
  3042. }
  3043. PAL_BattleDelay(1, 0, TRUE);
  3044. }
  3045. }
  3046. break;
  3047. case kBattleActionDefend:
  3048. g_Battle.rgPlayer[wPlayerIndex].fDefending = TRUE;
  3049. gpGlobals->Exp.rgDefenseExp[wPlayerRole].wCount += 2;
  3050. break;
  3051. case kBattleActionFlee:
  3052. str = PAL_GetPlayerFleeRate(wPlayerRole);
  3053. def = 0;
  3054. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  3055. {
  3056. if (g_Battle.rgEnemy[i].wObjectID == 0)
  3057. {
  3058. continue;
  3059. }
  3060. def += (SHORT)(g_Battle.rgEnemy[i].e.wFleeRate);
  3061. def += (g_Battle.rgEnemy[i].e.wLevel + 6) * 2;
  3062. }
  3063. if ((SHORT)def < 0)
  3064. {
  3065. def = 0;
  3066. }
  3067. if (RandomLong(0, str) >= RandomLong(0, def) && !g_Battle.fIsBoss)
  3068. {
  3069. //
  3070. // Successful escape
  3071. //
  3072. PAL_BattlePlayerEscape();
  3073. }
  3074. else
  3075. {
  3076. //
  3077. // Failed escape
  3078. //
  3079. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 0;
  3080. for (i = 0; i < 3; i++)
  3081. {
  3082. x = PAL_X(g_Battle.rgPlayer[wPlayerIndex].pos) + 4;
  3083. y = PAL_Y(g_Battle.rgPlayer[wPlayerIndex].pos) + 2;
  3084. g_Battle.rgPlayer[wPlayerIndex].pos = PAL_XY(x, y);
  3085. PAL_BattleDelay(1, 0, TRUE);
  3086. }
  3087. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 1;
  3088. PAL_BattleDelay(8, BATTLE_LABEL_ESCAPEFAIL, TRUE);
  3089. gpGlobals->Exp.rgFleeExp[wPlayerRole].wCount += 2;
  3090. }
  3091. break;
  3092. case kBattleActionMagic:
  3093. wObject = g_Battle.rgPlayer[wPlayerIndex].action.wActionID;
  3094. wMagicNum = gpGlobals->g.rgObject[wObject].magic.wMagicNumber;
  3095. PAL_BattleShowPlayerPreMagicAnim(wPlayerIndex,
  3096. (gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeSummon));
  3097. if (!gpGlobals->fAutoBattle)
  3098. {
  3099. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] -= gpGlobals->g.lprgMagic[wMagicNum].wCostMP;
  3100. if ((SHORT)(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole]) < 0)
  3101. {
  3102. gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] = 0;
  3103. }
  3104. }
  3105. if (gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeApplyToPlayer ||
  3106. gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeApplyToParty ||
  3107. gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeTrance)
  3108. {
  3109. //
  3110. // Using a defensive magic
  3111. //
  3112. WORD w = 0;
  3113. if (g_Battle.rgPlayer[wPlayerIndex].action.sTarget != -1)
  3114. {
  3115. w = gpGlobals->rgParty[g_Battle.rgPlayer[wPlayerIndex].action.sTarget].wPlayerRole;
  3116. }
  3117. else if (gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeTrance)
  3118. {
  3119. w = wPlayerRole;
  3120. }
  3121. gpGlobals->g.rgObject[wObject].magic.wScriptOnUse =
  3122. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].magic.wScriptOnUse, wPlayerRole);
  3123. if (g_fScriptSuccess)
  3124. {
  3125. PAL_BattleShowPlayerDefMagicAnim(wPlayerIndex, wObject, sTarget);
  3126. gpGlobals->g.rgObject[wObject].magic.wScriptOnSuccess =
  3127. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].magic.wScriptOnSuccess, w);
  3128. if (g_fScriptSuccess)
  3129. {
  3130. if (gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeTrance)
  3131. {
  3132. for (i = 0; i < 6; i++)
  3133. {
  3134. g_Battle.rgPlayer[wPlayerIndex].iColorShift = i * 2;
  3135. PAL_BattleDelay(1, 0, TRUE);
  3136. }
  3137. PAL_BattleBackupScene();
  3138. PAL_LoadBattleSprites();
  3139. g_Battle.rgPlayer[wPlayerIndex].iColorShift = 0;
  3140. PAL_BattleMakeScene();
  3141. PAL_BattleFadeScene();
  3142. }
  3143. }
  3144. }
  3145. }
  3146. else
  3147. {
  3148. //
  3149. // Using an offensive magic
  3150. //
  3151. gpGlobals->g.rgObject[wObject].magic.wScriptOnUse =
  3152. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].magic.wScriptOnUse, wPlayerRole);
  3153. if (g_fScriptSuccess)
  3154. {
  3155. if (gpGlobals->g.lprgMagic[wMagicNum].wType == kMagicTypeSummon)
  3156. {
  3157. PAL_BattleShowPlayerSummonMagicAnim(wPlayerIndex, wObject);
  3158. }
  3159. else
  3160. {
  3161. PAL_BattleShowPlayerOffMagicAnim(wPlayerIndex, wObject, sTarget, FALSE);
  3162. }
  3163. gpGlobals->g.rgObject[wObject].magic.wScriptOnSuccess =
  3164. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].magic.wScriptOnSuccess, (WORD)sTarget);
  3165. //
  3166. // Inflict damage to enemies
  3167. //
  3168. if ((SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) > 0)
  3169. {
  3170. if (sTarget == -1)
  3171. {
  3172. //
  3173. // Attack all enemies
  3174. //
  3175. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  3176. {
  3177. if (g_Battle.rgEnemy[i].wObjectID == 0)
  3178. {
  3179. continue;
  3180. }
  3181. str = PAL_GetPlayerMagicStrength(wPlayerRole);
  3182. def = g_Battle.rgEnemy[i].e.wDefense;
  3183. def += (g_Battle.rgEnemy[i].e.wLevel + 6) * 4;
  3184. sDamage = PAL_CalcMagicDamage(str, def,
  3185. g_Battle.rgEnemy[i].e.wElemResistance, g_Battle.rgEnemy[i].e.wPoisonResistance, wObject);
  3186. if (sDamage <= 0)
  3187. {
  3188. sDamage = 1;
  3189. }
  3190. g_Battle.rgEnemy[i].e.wHealth -= sDamage;
  3191. }
  3192. }
  3193. else
  3194. {
  3195. //
  3196. // Attack one enemy
  3197. //
  3198. str = PAL_GetPlayerMagicStrength(wPlayerRole);
  3199. def = g_Battle.rgEnemy[sTarget].e.wDefense;
  3200. def += (g_Battle.rgEnemy[sTarget].e.wLevel + 6) * 4;
  3201. sDamage = PAL_CalcMagicDamage(str, def,
  3202. g_Battle.rgEnemy[sTarget].e.wElemResistance, g_Battle.rgEnemy[sTarget].e.wPoisonResistance, wObject);
  3203. if (sDamage <= 0)
  3204. {
  3205. sDamage = 1;
  3206. }
  3207. g_Battle.rgEnemy[sTarget].e.wHealth -= sDamage;
  3208. }
  3209. }
  3210. }
  3211. }
  3212. PAL_BattleDisplayStatChange();
  3213. PAL_BattleShowPostMagicAnim();
  3214. PAL_BattleDelay(5, 0, TRUE);
  3215. gpGlobals->Exp.rgMagicExp[wPlayerRole].wCount += RandomLong(2, 3);
  3216. gpGlobals->Exp.rgMagicPowerExp[wPlayerRole].wCount++;
  3217. break;
  3218. case kBattleActionThrowItem:
  3219. wObject = g_Battle.rgPlayer[wPlayerIndex].action.wActionID;
  3220. for (i = 0; i < 4; i++)
  3221. {
  3222. g_Battle.rgPlayer[wPlayerIndex].pos =
  3223. PAL_XY(PAL_X(g_Battle.rgPlayer[wPlayerIndex].pos) - (4 - i),
  3224. PAL_Y(g_Battle.rgPlayer[wPlayerIndex].pos) - (4 - i) / 2);
  3225. PAL_BattleDelay(1, 0, TRUE);
  3226. }
  3227. PAL_BattleDelay(2, wObject, TRUE);
  3228. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 5;
  3229. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwMagicSound[wPlayerRole]);
  3230. PAL_BattleDelay(8, wObject, TRUE);
  3231. g_Battle.rgPlayer[wPlayerIndex].wCurrentFrame = 6;
  3232. PAL_BattleDelay(2, wObject, TRUE);
  3233. //
  3234. // Run the script
  3235. //
  3236. gpGlobals->g.rgObject[wObject].item.wScriptOnThrow =
  3237. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].item.wScriptOnThrow, (WORD)sTarget);
  3238. //
  3239. // Remove the thrown item from inventory
  3240. //
  3241. PAL_AddItemToInventory(wObject, -1);
  3242. PAL_BattleDisplayStatChange();
  3243. PAL_BattleDelay(4, 0, TRUE);
  3244. PAL_BattleUpdateFighters();
  3245. PAL_BattleDelay(4, 0, TRUE);
  3246. break;
  3247. case kBattleActionUseItem:
  3248. wObject = g_Battle.rgPlayer[wPlayerIndex].action.wActionID;
  3249. PAL_BattleShowPlayerUseItemAnim(wPlayerIndex, wObject, sTarget);
  3250. //
  3251. // Run the script
  3252. //
  3253. gpGlobals->g.rgObject[wObject].item.wScriptOnUse =
  3254. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].item.wScriptOnUse,
  3255. (sTarget == -1) ? 0xFFFF : gpGlobals->rgParty[sTarget].wPlayerRole);
  3256. //
  3257. // Remove the item if the item is consuming
  3258. //
  3259. if (gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagConsuming)
  3260. {
  3261. PAL_AddItemToInventory(wObject, -1);
  3262. }
  3263. if (g_Battle.iHidingTime < 0)
  3264. {
  3265. #ifdef PAL_CLASSIC
  3266. g_Battle.iHidingTime = -g_Battle.iHidingTime;
  3267. #else
  3268. g_Battle.iHidingTime = -g_Battle.iHidingTime * 20;
  3269. if (gpGlobals->bBattleSpeed > 1)
  3270. {
  3271. g_Battle.iHidingTime *= 1 + (gpGlobals->bBattleSpeed - 1) * 0.5;
  3272. }
  3273. else
  3274. {
  3275. g_Battle.iHidingTime *= 1.2;
  3276. }
  3277. #endif
  3278. PAL_BattleBackupScene();
  3279. PAL_BattleMakeScene();
  3280. PAL_BattleFadeScene();
  3281. }
  3282. PAL_BattleUpdateFighters();
  3283. PAL_BattleDisplayStatChange();
  3284. PAL_BattleDelay(8, 0, TRUE);
  3285. break;
  3286. case kBattleActionPass:
  3287. break;
  3288. }
  3289. //
  3290. // Revert this player back to waiting state.
  3291. //
  3292. g_Battle.rgPlayer[wPlayerIndex].state = kFighterWait;
  3293. g_Battle.rgPlayer[wPlayerIndex].flTimeMeter = 0;
  3294. PAL_BattlePostActionCheck(FALSE);
  3295. #ifndef PAL_CLASSIC
  3296. //
  3297. // Only check for poisons when the battle is not ended
  3298. //
  3299. fCheckPoison = FALSE;
  3300. if (g_Battle.BattleResult == kBattleResultOnGoing)
  3301. {
  3302. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  3303. {
  3304. if (g_Battle.rgEnemy[i].wObjectID != 0)
  3305. {
  3306. fCheckPoison = TRUE;
  3307. break;
  3308. }
  3309. }
  3310. }
  3311. //
  3312. // Check for poisons
  3313. //
  3314. if (fCheckPoison)
  3315. {
  3316. fPoisoned = FALSE;
  3317. PAL_BattleBackupStat();
  3318. for (i = 0; i < MAX_POISONS; i++)
  3319. {
  3320. wObject = gpGlobals->rgPoisonStatus[i][wPlayerIndex].wPoisonID;
  3321. if (wObject != 0)
  3322. {
  3323. fPoisoned = TRUE;
  3324. gpGlobals->rgPoisonStatus[i][wPlayerIndex].wPoisonScript =
  3325. PAL_RunTriggerScript(gpGlobals->rgPoisonStatus[i][wPlayerIndex].wPoisonScript, wPlayerRole);
  3326. }
  3327. }
  3328. if (fPoisoned)
  3329. {
  3330. PAL_BattleDelay(3, 0, TRUE);
  3331. PAL_BattleUpdateFighters();
  3332. if (PAL_BattleDisplayStatChange())
  3333. {
  3334. PAL_BattleDelay(6, 0, TRUE);
  3335. }
  3336. }
  3337. }
  3338. //
  3339. // Update statuses
  3340. //
  3341. for (i = 0; i < kStatusAll; i++)
  3342. {
  3343. if (gpGlobals->rgPlayerStatus[wPlayerRole][i] > 0)
  3344. {
  3345. gpGlobals->rgPlayerStatus[wPlayerRole][i]--;
  3346. }
  3347. }
  3348. #endif
  3349. }
  3350. static INT
  3351. PAL_BattleEnemySelectTargetIndex(
  3352. VOID
  3353. )
  3354. /*++
  3355. Purpose:
  3356. Select a attackable player randomly.
  3357. Parameters:
  3358. None.
  3359. Return value:
  3360. None.
  3361. --*/
  3362. {
  3363. int i;
  3364. i = RandomLong(0, gpGlobals->wMaxPartyMemberIndex);
  3365. while (gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] == 0)
  3366. {
  3367. i = RandomLong(0, gpGlobals->wMaxPartyMemberIndex);
  3368. }
  3369. return i;
  3370. }
  3371. VOID
  3372. PAL_BattleEnemyPerformAction(
  3373. WORD wEnemyIndex
  3374. )
  3375. /*++
  3376. Purpose:
  3377. Perform the selected action for a player.
  3378. Parameters:
  3379. [IN] wEnemyIndex - the index of the player.
  3380. Return value:
  3381. None.
  3382. --*/
  3383. {
  3384. int str, def, iCoverIndex, i, x, y, ex, ey, iSound;
  3385. WORD rgwElementalResistance[NUM_MAGIC_ELEMENTAL];
  3386. WORD wPlayerRole, w, wMagic, wMagicNum;
  3387. SHORT sTarget, sDamage;
  3388. BOOL fAutoDefend = FALSE, rgfMagAutoDefend[MAX_PLAYERS_IN_PARTY];
  3389. PAL_BattleBackupStat();
  3390. g_Battle.iBlow = 0;
  3391. sTarget = PAL_BattleEnemySelectTargetIndex();
  3392. wPlayerRole = gpGlobals->rgParty[sTarget].wPlayerRole;
  3393. wMagic = g_Battle.rgEnemy[wEnemyIndex].e.wMagic;
  3394. if (g_Battle.rgEnemy[wEnemyIndex].rgwStatus[kStatusSleep] > 0 ||
  3395. g_Battle.rgEnemy[wEnemyIndex].rgwStatus[kStatusParalyzed] > 0 ||
  3396. g_Battle.iHidingTime > 0)
  3397. {
  3398. //
  3399. // Do nothing
  3400. //
  3401. goto end;
  3402. }
  3403. else if (g_Battle.rgEnemy[wEnemyIndex].rgwStatus[kStatusConfused] > 0)
  3404. {
  3405. // TODO
  3406. }
  3407. else if (wMagic != 0 &&
  3408. RandomLong(0, 9) < g_Battle.rgEnemy[wEnemyIndex].e.wMagicRate &&
  3409. g_Battle.rgEnemy[wEnemyIndex].rgwStatus[kStatusSilence] == 0)
  3410. {
  3411. //
  3412. // Magical attack
  3413. //
  3414. if (wMagic == 0xFFFF)
  3415. {
  3416. //
  3417. // Do nothing
  3418. //
  3419. goto end;
  3420. }
  3421. wMagicNum = gpGlobals->g.rgObject[wMagic].magic.wMagicNumber;
  3422. str = (SHORT)g_Battle.rgEnemy[wEnemyIndex].e.wMagicStrength;
  3423. str += (g_Battle.rgEnemy[wEnemyIndex].e.wLevel + 6) * 6;
  3424. if (str < 0)
  3425. {
  3426. str = 0;
  3427. }
  3428. ex = PAL_X(g_Battle.rgEnemy[wEnemyIndex].pos);
  3429. ey = PAL_Y(g_Battle.rgEnemy[wEnemyIndex].pos);
  3430. ex += 12;
  3431. ey += 6;
  3432. g_Battle.rgEnemy[wEnemyIndex].pos = PAL_XY(ex, ey);
  3433. PAL_BattleDelay(1, 0, FALSE);
  3434. ex += 4;
  3435. ey += 2;
  3436. g_Battle.rgEnemy[wEnemyIndex].pos = PAL_XY(ex, ey);
  3437. PAL_BattleDelay(1, 0, FALSE);
  3438. AUDIO_PlaySound(g_Battle.rgEnemy[wEnemyIndex].e.wMagicSound);
  3439. for (i = 0; i < g_Battle.rgEnemy[wEnemyIndex].e.wMagicFrames; i++)
  3440. {
  3441. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame =
  3442. g_Battle.rgEnemy[wEnemyIndex].e.wIdleFrames + i;
  3443. PAL_BattleDelay(g_Battle.rgEnemy[wEnemyIndex].e.wActWaitFrames, 0, FALSE);
  3444. }
  3445. if (g_Battle.rgEnemy[wEnemyIndex].e.wMagicFrames == 0)
  3446. {
  3447. PAL_BattleDelay(1, 0, FALSE);
  3448. }
  3449. if (gpGlobals->g.lprgMagic[wMagicNum].wSoundDelay == 0)
  3450. {
  3451. for (i = 0; i <= g_Battle.rgEnemy[wEnemyIndex].e.wAttackFrames; i++)
  3452. {
  3453. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame =
  3454. i - 1 + g_Battle.rgEnemy[wEnemyIndex].e.wIdleFrames + g_Battle.rgEnemy[wEnemyIndex].e.wMagicFrames;
  3455. PAL_BattleDelay(g_Battle.rgEnemy[wEnemyIndex].e.wActWaitFrames, 0, FALSE);
  3456. }
  3457. }
  3458. if (gpGlobals->g.lprgMagic[wMagicNum].wType != kMagicTypeNormal)
  3459. {
  3460. sTarget = -1;
  3461. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  3462. {
  3463. w = gpGlobals->rgParty[i].wPlayerRole;
  3464. if (gpGlobals->rgPlayerStatus[w][kStatusSleep] == 0 &&
  3465. #ifdef PAL_CLASSIC
  3466. gpGlobals->rgPlayerStatus[w][kStatusParalyzed] == 0 &&
  3467. #else
  3468. gpGlobals->rgPlayerStatus[w][kStatusSlow] == 0 &&
  3469. #endif
  3470. gpGlobals->rgPlayerStatus[w][kStatusConfused] == 0 &&
  3471. RandomLong(0, 2) == 0 &&
  3472. gpGlobals->g.PlayerRoles.rgwHP[w] != 0)
  3473. {
  3474. rgfMagAutoDefend[i] = TRUE;
  3475. g_Battle.rgPlayer[i].wCurrentFrame = 3;
  3476. }
  3477. else
  3478. {
  3479. rgfMagAutoDefend[i] = FALSE;
  3480. }
  3481. }
  3482. }
  3483. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] == 0 &&
  3484. #ifdef PAL_CLASSIC
  3485. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] == 0 &&
  3486. #else
  3487. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] == 0 &&
  3488. #endif
  3489. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] == 0 &&
  3490. RandomLong(0, 2) == 0)
  3491. {
  3492. fAutoDefend = TRUE;
  3493. g_Battle.rgPlayer[sTarget].wCurrentFrame = 3;
  3494. }
  3495. // PAL_BattleDelay(12, (WORD)(-((SHORT)wMagic)), FALSE);
  3496. gpGlobals->g.rgObject[wMagic].magic.wScriptOnUse =
  3497. PAL_RunTriggerScript(gpGlobals->g.rgObject[wMagic].magic.wScriptOnUse, wPlayerRole);
  3498. if (g_fScriptSuccess)
  3499. {
  3500. PAL_BattleShowEnemyMagicAnim(wMagic, sTarget);
  3501. gpGlobals->g.rgObject[wMagic].magic.wScriptOnSuccess =
  3502. PAL_RunTriggerScript(gpGlobals->g.rgObject[wMagic].magic.wScriptOnSuccess, wPlayerRole);
  3503. }
  3504. if ((SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) > 0)
  3505. {
  3506. if (sTarget == -1)
  3507. {
  3508. //
  3509. // damage all players
  3510. //
  3511. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  3512. {
  3513. w = gpGlobals->rgParty[i].wPlayerRole;
  3514. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  3515. {
  3516. //
  3517. // skip dead players
  3518. //
  3519. continue;
  3520. }
  3521. def = PAL_GetPlayerDefense(w);
  3522. for (x = 0; x < NUM_MAGIC_ELEMENTAL; x++)
  3523. {
  3524. rgwElementalResistance[x] =
  3525. 5 + PAL_GetPlayerElementalResistance(w, x) / 20;
  3526. }
  3527. sDamage = PAL_CalcMagicDamage(str, def, rgwElementalResistance,
  3528. 5 + PAL_GetPlayerPoisonResistance(w) / 20, wMagic);
  3529. sDamage /= ((g_Battle.rgPlayer[i].fDefending ? 2 : 1) *
  3530. ((gpGlobals->rgPlayerStatus[w][kStatusProtect] > 0) ? 2 : 1)) +
  3531. (rgfMagAutoDefend[i] ? 1 : 0);
  3532. if (sDamage > gpGlobals->g.PlayerRoles.rgwHP[w])
  3533. {
  3534. sDamage = gpGlobals->g.PlayerRoles.rgwHP[w];
  3535. }
  3536. #ifndef INVINCIBLE
  3537. gpGlobals->g.PlayerRoles.rgwHP[w] -= sDamage;
  3538. #endif
  3539. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  3540. {
  3541. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwDeathSound[w]);
  3542. }
  3543. }
  3544. }
  3545. else
  3546. {
  3547. //
  3548. // damage one player
  3549. //
  3550. def = PAL_GetPlayerDefense(wPlayerRole);
  3551. for (x = 0; x < NUM_MAGIC_ELEMENTAL; x++)
  3552. {
  3553. rgwElementalResistance[x] =
  3554. 5 + PAL_GetPlayerElementalResistance(wPlayerRole, x) / 20;
  3555. }
  3556. sDamage = PAL_CalcMagicDamage(str, def, rgwElementalResistance,
  3557. 5 + PAL_GetPlayerPoisonResistance(wPlayerRole) / 20, wMagic);
  3558. sDamage /= ((g_Battle.rgPlayer[sTarget].fDefending ? 2 : 1) *
  3559. ((gpGlobals->rgPlayerStatus[wPlayerRole][kStatusProtect] > 0) ? 2 : 1)) +
  3560. (fAutoDefend ? 1 : 0);
  3561. if (sDamage > gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole])
  3562. {
  3563. sDamage = gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole];
  3564. }
  3565. #ifndef INVINCIBLE
  3566. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] -= sDamage;
  3567. #endif
  3568. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  3569. {
  3570. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwDeathSound[wPlayerRole]);
  3571. }
  3572. }
  3573. }
  3574. if (!gpGlobals->fAutoBattle)
  3575. {
  3576. PAL_BattleDisplayStatChange();
  3577. }
  3578. for (i = 0; i < 5; i++)
  3579. {
  3580. if (sTarget == -1)
  3581. {
  3582. for (x = 0; x <= gpGlobals->wMaxPartyMemberIndex; x++)
  3583. {
  3584. if (g_Battle.rgPlayer[x].wPrevHP ==
  3585. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[x].wPlayerRole])
  3586. {
  3587. //
  3588. // Skip unaffected players
  3589. //
  3590. continue;
  3591. }
  3592. g_Battle.rgPlayer[x].wCurrentFrame = 4;
  3593. if (i > 0)
  3594. {
  3595. g_Battle.rgPlayer[x].pos =
  3596. PAL_XY(PAL_X(g_Battle.rgPlayer[x].pos) + (8 >> i),
  3597. PAL_Y(g_Battle.rgPlayer[x].pos) + (4 >> i));
  3598. }
  3599. g_Battle.rgPlayer[x].iColorShift = ((i < 3) ? 6 : 0);
  3600. }
  3601. }
  3602. else
  3603. {
  3604. g_Battle.rgPlayer[sTarget].wCurrentFrame = 4;
  3605. if (i > 0)
  3606. {
  3607. g_Battle.rgPlayer[sTarget].pos =
  3608. PAL_XY(PAL_X(g_Battle.rgPlayer[sTarget].pos) + (8 >> i),
  3609. PAL_Y(g_Battle.rgPlayer[sTarget].pos) + (4 >> i));
  3610. }
  3611. g_Battle.rgPlayer[sTarget].iColorShift = ((i < 3) ? 6 : 0);
  3612. }
  3613. PAL_BattleDelay(1, 0, FALSE);
  3614. }
  3615. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame = 0;
  3616. g_Battle.rgEnemy[wEnemyIndex].pos = g_Battle.rgEnemy[wEnemyIndex].posOriginal;
  3617. PAL_BattleDelay(1, 0, FALSE);
  3618. PAL_BattleUpdateFighters();
  3619. PAL_BattlePostActionCheck(TRUE);
  3620. PAL_BattleDelay(8, 0, TRUE);
  3621. }
  3622. else
  3623. {
  3624. //
  3625. // Physical attack
  3626. //
  3627. WORD wFrameBak = g_Battle.rgPlayer[sTarget].wCurrentFrame;
  3628. str = (SHORT)g_Battle.rgEnemy[wEnemyIndex].e.wAttackStrength;
  3629. str += (g_Battle.rgEnemy[wEnemyIndex].e.wLevel + 6) * 6;
  3630. if (str < 0)
  3631. {
  3632. str = 0;
  3633. }
  3634. def = PAL_GetPlayerDefense(wPlayerRole);
  3635. if (g_Battle.rgPlayer[sTarget].fDefending)
  3636. {
  3637. def *= 2;
  3638. }
  3639. AUDIO_PlaySound(g_Battle.rgEnemy[wEnemyIndex].e.wAttackSound);
  3640. iCoverIndex = -1;
  3641. fAutoDefend = (RandomLong(0, 16) >= 10);
  3642. //
  3643. // Check if the inflictor should be protected
  3644. //
  3645. if ((PAL_IsPlayerDying(wPlayerRole) ||
  3646. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] > 0 ||
  3647. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] > 0 ||
  3648. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] > 0) && fAutoDefend)
  3649. {
  3650. w = gpGlobals->g.PlayerRoles.rgwCoveredBy[wPlayerRole];
  3651. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  3652. {
  3653. if (gpGlobals->rgParty[i].wPlayerRole == w)
  3654. {
  3655. iCoverIndex = i;
  3656. break;
  3657. }
  3658. }
  3659. if (iCoverIndex != -1)
  3660. {
  3661. if (PAL_IsPlayerDying(gpGlobals->rgParty[iCoverIndex].wPlayerRole) ||
  3662. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[iCoverIndex].wPlayerRole][kStatusConfused] > 0 ||
  3663. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[iCoverIndex].wPlayerRole][kStatusSleep] > 0 ||
  3664. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[iCoverIndex].wPlayerRole][kStatusParalyzed] > 0)
  3665. {
  3666. iCoverIndex = -1;
  3667. }
  3668. }
  3669. }
  3670. //
  3671. // If no one can cover the inflictor and inflictor is in a
  3672. // bad status, don't evade
  3673. //
  3674. if (iCoverIndex == -1 &&
  3675. (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] > 0 ||
  3676. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] > 0 ||
  3677. #ifdef PAL_CLASSIC
  3678. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] > 0))
  3679. #else
  3680. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0))
  3681. #endif
  3682. {
  3683. fAutoDefend = FALSE;
  3684. }
  3685. for (i = 0; i < g_Battle.rgEnemy[wEnemyIndex].e.wMagicFrames; i++)
  3686. {
  3687. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame =
  3688. g_Battle.rgEnemy[wEnemyIndex].e.wIdleFrames + i;
  3689. PAL_BattleDelay(2, 0, FALSE);
  3690. }
  3691. for (i = 0; i < 3 - g_Battle.rgEnemy[wEnemyIndex].e.wMagicFrames; i++)
  3692. {
  3693. x = PAL_X(g_Battle.rgEnemy[wEnemyIndex].pos) - 2;
  3694. y = PAL_Y(g_Battle.rgEnemy[wEnemyIndex].pos) - 1;
  3695. g_Battle.rgEnemy[wEnemyIndex].pos = PAL_XY(x, y);
  3696. PAL_BattleDelay(1, 0, FALSE);
  3697. }
  3698. if (!gConfig.fIsWIN95 || g_Battle.rgEnemy[wEnemyIndex].e.wActionSound != 0)
  3699. {
  3700. AUDIO_PlaySound(g_Battle.rgEnemy[wEnemyIndex].e.wActionSound);
  3701. }
  3702. PAL_BattleDelay(1, 0, FALSE);
  3703. ex = PAL_X(g_Battle.rgPlayer[sTarget].pos) - 44;
  3704. ey = PAL_Y(g_Battle.rgPlayer[sTarget].pos) - 16;
  3705. iSound = g_Battle.rgEnemy[wEnemyIndex].e.wCallSound;
  3706. if (iCoverIndex != -1)
  3707. {
  3708. iSound = gpGlobals->g.PlayerRoles.rgwCoverSound[gpGlobals->rgParty[iCoverIndex].wPlayerRole];
  3709. g_Battle.rgPlayer[iCoverIndex].wCurrentFrame = 3;
  3710. x = PAL_X(g_Battle.rgPlayer[sTarget].pos) - 24;
  3711. y = PAL_Y(g_Battle.rgPlayer[sTarget].pos) - 12;
  3712. g_Battle.rgPlayer[iCoverIndex].pos = PAL_XY(x, y);
  3713. }
  3714. else if (fAutoDefend)
  3715. {
  3716. g_Battle.rgPlayer[sTarget].wCurrentFrame = 3;
  3717. iSound = gpGlobals->g.PlayerRoles.rgwCoverSound[wPlayerRole];
  3718. }
  3719. if (g_Battle.rgEnemy[wEnemyIndex].e.wAttackFrames == 0)
  3720. {
  3721. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame =
  3722. g_Battle.rgEnemy[wEnemyIndex].e.wIdleFrames - 1;
  3723. g_Battle.rgEnemy[wEnemyIndex].pos = PAL_XY(ex, ey);
  3724. PAL_BattleDelay(2, 0, FALSE);
  3725. }
  3726. else
  3727. {
  3728. for (i = 0; i <= g_Battle.rgEnemy[wEnemyIndex].e.wAttackFrames; i++)
  3729. {
  3730. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame =
  3731. g_Battle.rgEnemy[wEnemyIndex].e.wIdleFrames +
  3732. g_Battle.rgEnemy[wEnemyIndex].e.wMagicFrames + i - 1;
  3733. g_Battle.rgEnemy[wEnemyIndex].pos = PAL_XY(ex, ey);
  3734. PAL_BattleDelay(g_Battle.rgEnemy[wEnemyIndex].e.wActWaitFrames, 0, FALSE);
  3735. }
  3736. }
  3737. if (!fAutoDefend)
  3738. {
  3739. g_Battle.rgPlayer[sTarget].wCurrentFrame = 4;
  3740. sDamage = PAL_CalcPhysicalAttackDamage(str + RandomLong(0, 2), def, 2);
  3741. sDamage += RandomLong(0, 1);
  3742. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusProtect])
  3743. {
  3744. sDamage /= 2;
  3745. }
  3746. if ((SHORT)gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] < sDamage)
  3747. {
  3748. sDamage = gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole];
  3749. }
  3750. if (sDamage <= 0)
  3751. {
  3752. sDamage = 1;
  3753. }
  3754. #ifndef INVINCIBLE
  3755. gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] -= sDamage;
  3756. #endif
  3757. PAL_BattleDisplayStatChange();
  3758. g_Battle.rgPlayer[sTarget].iColorShift = 6;
  3759. }
  3760. if (!gConfig.fIsWIN95 || iSound != 0)
  3761. {
  3762. AUDIO_PlaySound(iSound);
  3763. }
  3764. PAL_BattleDelay(1, 0, FALSE);
  3765. g_Battle.rgPlayer[sTarget].iColorShift = 0;
  3766. if (iCoverIndex != -1)
  3767. {
  3768. g_Battle.rgEnemy[wEnemyIndex].pos =
  3769. PAL_XY(PAL_X(g_Battle.rgEnemy[wEnemyIndex].pos) - 10,
  3770. PAL_Y(g_Battle.rgEnemy[wEnemyIndex].pos) - 8);
  3771. g_Battle.rgPlayer[iCoverIndex].pos =
  3772. PAL_XY(PAL_X(g_Battle.rgPlayer[iCoverIndex].pos) + 4,
  3773. PAL_Y(g_Battle.rgPlayer[iCoverIndex].pos) + 2);
  3774. }
  3775. else
  3776. {
  3777. g_Battle.rgPlayer[sTarget].pos =
  3778. PAL_XY(PAL_X(g_Battle.rgPlayer[sTarget].pos) + 8,
  3779. PAL_Y(g_Battle.rgPlayer[sTarget].pos) + 4);
  3780. }
  3781. PAL_BattleDelay(1, 0, FALSE);
  3782. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  3783. {
  3784. AUDIO_PlaySound(gpGlobals->g.PlayerRoles.rgwDeathSound[wPlayerRole]);
  3785. wFrameBak = 2;
  3786. }
  3787. else if (PAL_IsPlayerDying(wPlayerRole))
  3788. {
  3789. wFrameBak = 1;
  3790. }
  3791. if (iCoverIndex == -1)
  3792. {
  3793. g_Battle.rgPlayer[sTarget].pos =
  3794. PAL_XY(PAL_X(g_Battle.rgPlayer[sTarget].pos) + 2,
  3795. PAL_Y(g_Battle.rgPlayer[sTarget].pos) + 1);
  3796. }
  3797. PAL_BattleDelay(3, 0, FALSE);
  3798. g_Battle.rgEnemy[wEnemyIndex].pos = g_Battle.rgEnemy[wEnemyIndex].posOriginal;
  3799. g_Battle.rgEnemy[wEnemyIndex].wCurrentFrame = 0;
  3800. PAL_BattleDelay(1, 0, FALSE);
  3801. g_Battle.rgPlayer[sTarget].wCurrentFrame = wFrameBak;
  3802. PAL_BattleDelay(1, 0, TRUE);
  3803. g_Battle.rgPlayer[sTarget].pos = g_Battle.rgPlayer[sTarget].posOriginal;
  3804. PAL_BattleDelay(4, 0, TRUE);
  3805. PAL_BattleUpdateFighters();
  3806. if (iCoverIndex == -1 && !fAutoDefend &&
  3807. g_Battle.rgEnemy[wEnemyIndex].e.wAttackEquivItemRate >= RandomLong(1, 10))
  3808. {
  3809. i = g_Battle.rgEnemy[wEnemyIndex].e.wAttackEquivItem;
  3810. gpGlobals->g.rgObject[i].item.wScriptOnUse =
  3811. PAL_RunTriggerScript(gpGlobals->g.rgObject[i].item.wScriptOnUse, wPlayerRole);
  3812. }
  3813. PAL_BattlePostActionCheck(TRUE);
  3814. }
  3815. end:
  3816. #ifndef PAL_CLASSIC
  3817. //
  3818. // Check poisons
  3819. //
  3820. if (!g_Battle.rgEnemy[wEnemyIndex].fDualMove)
  3821. {
  3822. PAL_BattleBackupStat();
  3823. for (i = 0; i < MAX_POISONS; i++)
  3824. {
  3825. if (g_Battle.rgEnemy[wEnemyIndex].rgPoisons[i].wPoisonID != 0)
  3826. {
  3827. g_Battle.rgEnemy[wEnemyIndex].rgPoisons[i].wPoisonScript =
  3828. PAL_RunTriggerScript(g_Battle.rgEnemy[wEnemyIndex].rgPoisons[i].wPoisonScript, wEnemyIndex);
  3829. }
  3830. }
  3831. if (PAL_BattleDisplayStatChange())
  3832. {
  3833. PAL_BattleDelay(6, 0, FALSE);
  3834. }
  3835. }
  3836. PAL_BattlePostActionCheck(FALSE);
  3837. //
  3838. // Update statuses
  3839. //
  3840. for (i = 0; i < kStatusAll; i++)
  3841. {
  3842. if (g_Battle.rgEnemy[wEnemyIndex].rgwStatus[i] > 0)
  3843. {
  3844. g_Battle.rgEnemy[wEnemyIndex].rgwStatus[i]--;
  3845. }
  3846. }
  3847. #else
  3848. i = 0; // do nothing
  3849. #endif
  3850. }
  3851. VOID
  3852. PAL_BattleStealFromEnemy(
  3853. WORD wTarget,
  3854. WORD wStealRate
  3855. )
  3856. /*++
  3857. Purpose:
  3858. Steal from the enemy.
  3859. Parameters:
  3860. [IN] wTarget - the target enemy index.
  3861. [IN] wStealRate - the rate of successful theft.
  3862. Return value:
  3863. None.
  3864. --*/
  3865. {
  3866. int iPlayerIndex = g_Battle.wMovingPlayerIndex;
  3867. int offset, x, y, i;
  3868. WCHAR s[256] = L"";
  3869. g_Battle.rgPlayer[iPlayerIndex].wCurrentFrame = 10;
  3870. offset = ((INT)wTarget - iPlayerIndex) * 8;
  3871. x = PAL_X(g_Battle.rgEnemy[wTarget].pos) + 64 - offset;
  3872. y = PAL_Y(g_Battle.rgEnemy[wTarget].pos) + 20 - offset / 2;
  3873. g_Battle.rgPlayer[iPlayerIndex].pos = PAL_XY(x, y);
  3874. PAL_BattleDelay(1, 0, TRUE);
  3875. for (i = 0; i < 5; i++)
  3876. {
  3877. x -= i + 8;
  3878. y -= 4;
  3879. g_Battle.rgPlayer[iPlayerIndex].pos = PAL_XY(x, y);
  3880. if (i == 4)
  3881. {
  3882. g_Battle.rgEnemy[wTarget].iColorShift = 6;
  3883. }
  3884. PAL_BattleDelay(1, 0, TRUE);
  3885. }
  3886. g_Battle.rgEnemy[wTarget].iColorShift = 0;
  3887. x--;
  3888. g_Battle.rgPlayer[iPlayerIndex].pos = PAL_XY(x, y);
  3889. PAL_BattleDelay(3, 0, TRUE);
  3890. g_Battle.rgPlayer[iPlayerIndex].state = kFighterWait;
  3891. g_Battle.rgPlayer[iPlayerIndex].flTimeMeter = 0;
  3892. PAL_BattleUpdateFighters();
  3893. PAL_BattleDelay(1, 0, TRUE);
  3894. if (g_Battle.rgEnemy[wTarget].e.nStealItem > 0 &&
  3895. (RandomLong(0, 10) <= wStealRate || wStealRate == 0))
  3896. {
  3897. if (g_Battle.rgEnemy[wTarget].e.wStealItem == 0)
  3898. {
  3899. //
  3900. // stolen coins
  3901. //
  3902. int c = g_Battle.rgEnemy[wTarget].e.nStealItem / RandomLong(2, 3);
  3903. g_Battle.rgEnemy[wTarget].e.nStealItem -= c;
  3904. gpGlobals->dwCash += c;
  3905. if (c > 0)
  3906. {
  3907. swprintf(s, 256, L"%s %d %s", PAL_GetWord(34), c, PAL_GetWord(10));
  3908. }
  3909. }
  3910. else
  3911. {
  3912. //
  3913. // stolen item
  3914. //
  3915. g_Battle.rgEnemy[wTarget].e.nStealItem--;
  3916. PAL_AddItemToInventory(g_Battle.rgEnemy[wTarget].e.wStealItem, 1);
  3917. wcscpy(s, PAL_GetWord(34));
  3918. wcscat(s, PAL_GetWord(g_Battle.rgEnemy[wTarget].e.wStealItem));
  3919. }
  3920. if (s[0] != '\0')
  3921. {
  3922. #ifdef PAL_CLASSIC
  3923. PAL_StartDialog(kDialogCenterWindow, 0, 0, FALSE);
  3924. PAL_ShowDialogText(s);
  3925. #else
  3926. PAL_BattleUIShowText(s, 800);
  3927. #endif
  3928. }
  3929. }
  3930. }
  3931. VOID
  3932. PAL_BattleSimulateMagic(
  3933. SHORT sTarget,
  3934. WORD wMagicObjectID,
  3935. WORD wBaseDamage
  3936. )
  3937. /*++
  3938. Purpose:
  3939. Simulate a magic for players. Mostly used in item throwing script.
  3940. Parameters:
  3941. [IN] sTarget - the target enemy index. -1 = all enemies.
  3942. [IN] wMagicObjectID - the object ID of the magic to be simulated.
  3943. [IN] wBaseDamage - the base damage of the simulation.
  3944. Return value:
  3945. None.
  3946. --*/
  3947. {
  3948. SHORT sDamage;
  3949. int i, def;
  3950. if (gpGlobals->g.rgObject[wMagicObjectID].magic.wFlags & kMagicFlagApplyToAll)
  3951. {
  3952. sTarget = -1;
  3953. }
  3954. else if (sTarget == -1)
  3955. {
  3956. sTarget = PAL_BattleSelectAutoTarget();
  3957. }
  3958. //
  3959. // Show the magic animation
  3960. //
  3961. PAL_BattleShowPlayerOffMagicAnim(0xFFFF, wMagicObjectID, sTarget, FALSE);
  3962. if (gpGlobals->g.lprgMagic[gpGlobals->g.rgObject[wMagicObjectID].magic.wMagicNumber].wBaseDamage > 0 ||
  3963. wBaseDamage > 0)
  3964. {
  3965. if (sTarget == -1)
  3966. {
  3967. //
  3968. // Apply to all enemies
  3969. //
  3970. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  3971. {
  3972. if (g_Battle.rgEnemy[i].wObjectID == 0)
  3973. {
  3974. continue;
  3975. }
  3976. def = (SHORT)g_Battle.rgEnemy[i].e.wDefense;
  3977. def += (g_Battle.rgEnemy[i].e.wLevel + 6) * 4;
  3978. if (def < 0)
  3979. {
  3980. def = 0;
  3981. }
  3982. sDamage = PAL_CalcMagicDamage(wBaseDamage, (WORD)def, g_Battle.rgEnemy[i].e.wElemResistance,
  3983. g_Battle.rgEnemy[i].e.wPoisonResistance, wMagicObjectID);
  3984. if (sDamage < 0)
  3985. {
  3986. sDamage = 0;
  3987. }
  3988. g_Battle.rgEnemy[i].e.wHealth -= sDamage;
  3989. }
  3990. }
  3991. else
  3992. {
  3993. //
  3994. // Apply to one enemy
  3995. //
  3996. def = (SHORT)g_Battle.rgEnemy[sTarget].e.wDefense;
  3997. def += (g_Battle.rgEnemy[sTarget].e.wLevel + 6) * 4;
  3998. if (def < 0)
  3999. {
  4000. def = 0;
  4001. }
  4002. sDamage = PAL_CalcMagicDamage(wBaseDamage, (WORD)def, g_Battle.rgEnemy[sTarget].e.wElemResistance,
  4003. g_Battle.rgEnemy[sTarget].e.wPoisonResistance, wMagicObjectID);
  4004. if (sDamage < 0)
  4005. {
  4006. sDamage = 0;
  4007. }
  4008. g_Battle.rgEnemy[sTarget].e.wHealth -= sDamage;
  4009. }
  4010. }
  4011. }