fight.c 130 KB

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