fight.c 132 KB

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