fight.c 128 KB

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