fight.c 128 KB

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