fight.c 129 KB

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