fight.c 129 KB

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