fight.c 128 KB

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