fight.c 129 KB

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