fight.c 128 KB

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