fight.c 130 KB

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