fight.c 128 KB

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