fight.c 126 KB

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