fight.c 129 KB

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