fight.c 129 KB

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