fight.c 130 KB

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