fight.c 127 KB

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