fight.c 128 KB

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