fight.c 127 KB

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