uibattle.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "main.h"
  24. extern WORD g_rgPlayerPos[3][3][2];
  25. static int g_iCurMiscMenuItem = 0;
  26. static int g_iCurSubMenuItem = 0;
  27. VOID
  28. PAL_PlayerInfoBox(
  29. PAL_POS pos,
  30. WORD wPlayerRole,
  31. INT iTimeMeter,
  32. BYTE bTimeMeterColor,
  33. BOOL fUpdate
  34. )
  35. /*++
  36. Purpose:
  37. Show the player info box.
  38. Parameters:
  39. [IN] pos - the top-left corner position of the box.
  40. [IN] wPlayerRole - the player role ID to be shown.
  41. [IN] iTimeMeter - the value of time meter. 0 = empty, 100 = full.
  42. [IN] bTimeMeterColor - the color of time meter.
  43. [IN] fUpdate - whether to update the screen area or not.
  44. Return value:
  45. None.
  46. --*/
  47. {
  48. SDL_Rect rect;
  49. BYTE bPoisonColor;
  50. int i, iPartyIndex;
  51. WORD wMaxLevel, w;
  52. const BYTE rgStatusPos[kStatusAll][2] =
  53. {
  54. {35, 19}, // confused
  55. {0, 0}, // slow
  56. {54, 1}, // sleep
  57. {55, 20}, // silence
  58. {0, 0}, // puppet
  59. {0, 0}, // bravery
  60. {0, 0}, // protect
  61. {0, 0}, // haste
  62. {0, 0}, // dualattack
  63. };
  64. const WORD rgwStatusWord[kStatusAll] =
  65. {
  66. 0x1D, // confused
  67. 0x00, // slow
  68. 0x1C, // sleep
  69. 0x1A, // silence
  70. 0x00, // puppet
  71. 0x00, // bravery
  72. 0x00, // protect
  73. 0x00, // haste
  74. 0x00, // dualattack
  75. };
  76. const BYTE rgbStatusColor[kStatusAll] =
  77. {
  78. 0x5F, // confused
  79. 0x00, // slow
  80. 0x0E, // sleep
  81. 0x3C, // silence
  82. 0x00, // puppet
  83. 0x00, // bravery
  84. 0x00, // protect
  85. 0x00, // haste
  86. 0x00, // dualattack
  87. };
  88. //
  89. // Draw the box
  90. //
  91. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERINFOBOX),
  92. gpScreen, pos);
  93. //
  94. // Draw the player face
  95. //
  96. wMaxLevel = 0;
  97. bPoisonColor = 0xFF;
  98. for (iPartyIndex = 0; iPartyIndex <= gpGlobals->wMaxPartyMemberIndex; iPartyIndex++)
  99. {
  100. if (gpGlobals->rgParty[iPartyIndex].wPlayerRole == wPlayerRole)
  101. {
  102. break;
  103. }
  104. }
  105. if (iPartyIndex <= gpGlobals->wMaxPartyMemberIndex)
  106. {
  107. for (i = 0; i < MAX_POISONS; i++)
  108. {
  109. w = gpGlobals->rgPoisonStatus[i][iPartyIndex].wPoisonID;
  110. if (w != 0 &&
  111. gpGlobals->g.rgObject[w].poison.wPoisonLevel <= 3)
  112. {
  113. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel >= wMaxLevel)
  114. {
  115. wMaxLevel = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  116. bPoisonColor = (BYTE)(gpGlobals->g.rgObject[w].poison.wColor);
  117. }
  118. }
  119. }
  120. }
  121. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  122. {
  123. //
  124. // Always use the black/white color for dead players
  125. // and do not use the time meter
  126. //
  127. bPoisonColor = 0;
  128. iTimeMeter = 0;
  129. }
  130. if (bPoisonColor == 0xFF)
  131. {
  132. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERFACE_FIRST + wPlayerRole),
  133. gpScreen, PAL_XY(PAL_X(pos) - 2, PAL_Y(pos) - 4));
  134. }
  135. else
  136. {
  137. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERFACE_FIRST + wPlayerRole),
  138. gpScreen, PAL_XY(PAL_X(pos) - 2, PAL_Y(pos) - 4), bPoisonColor, 0);
  139. }
  140. #ifndef PAL_CLASSIC
  141. //
  142. // Draw a border for the Time Meter
  143. //
  144. rect.x = PAL_X(pos) + 31;
  145. rect.y = PAL_Y(pos) + 4;
  146. rect.w = 1;
  147. rect.h = 6;
  148. SDL_FillRect(gpScreen, &rect, 0xBD);
  149. rect.x += 39;
  150. SDL_FillRect(gpScreen, &rect, 0xBD);
  151. rect.x = PAL_X(pos) + 32;
  152. rect.y = PAL_Y(pos) + 3;
  153. rect.w = 38;
  154. rect.h = 1;
  155. SDL_FillRect(gpScreen, &rect, 0xBD);
  156. rect.y += 7;
  157. SDL_FillRect(gpScreen, &rect, 0xBD);
  158. //
  159. // Draw the Time meter bar
  160. //
  161. if (iTimeMeter >= 100)
  162. {
  163. rect.x = PAL_X(pos) + 33;
  164. rect.y = PAL_Y(pos) + 6;
  165. rect.w = 36;
  166. rect.h = 2;
  167. SDL_FillRect(gpScreen, &rect, 0x2C);
  168. }
  169. else if (iTimeMeter > 0)
  170. {
  171. rect.x = PAL_X(pos) + 33;
  172. rect.y = PAL_Y(pos) + 5;
  173. rect.w = iTimeMeter * 36 / 100;
  174. rect.h = 4;
  175. SDL_FillRect(gpScreen, &rect, bTimeMeterColor);
  176. }
  177. #endif
  178. //
  179. // Draw the HP and MP value
  180. //
  181. #ifdef PAL_CLASSIC
  182. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  183. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 6));
  184. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole], 4,
  185. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 8), kNumColorYellow, kNumAlignRight);
  186. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole], 4,
  187. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 5), kNumColorYellow, kNumAlignRight);
  188. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  189. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 22));
  190. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole], 4,
  191. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 24), kNumColorCyan, kNumAlignRight);
  192. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole], 4,
  193. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 21), kNumColorCyan, kNumAlignRight);
  194. #else
  195. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  196. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 14));
  197. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole], 4,
  198. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 16), kNumColorYellow, kNumAlignRight);
  199. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole], 4,
  200. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 13), kNumColorYellow, kNumAlignRight);
  201. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  202. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 24));
  203. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole], 4,
  204. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 26), kNumColorCyan, kNumAlignRight);
  205. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole], 4,
  206. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 23), kNumColorCyan, kNumAlignRight);
  207. #endif
  208. //
  209. // Draw Statuses
  210. //
  211. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  212. {
  213. for (i = 0; i < kStatusAll; i++)
  214. {
  215. if (gpGlobals->rgPlayerStatus[wPlayerRole][i] > 0 &&
  216. rgwStatusWord[i] != 0)
  217. {
  218. PAL_DrawText(PAL_GetWord(rgwStatusWord[i]),
  219. PAL_XY(PAL_X(pos) + rgStatusPos[i][0], PAL_Y(pos) + rgStatusPos[i][1]),
  220. rgbStatusColor[i], TRUE, FALSE);
  221. }
  222. }
  223. }
  224. //
  225. // Update the screen area if needed
  226. //
  227. if (fUpdate)
  228. {
  229. rect.x = PAL_X(pos) - 2;
  230. rect.y = PAL_Y(pos) - 4;
  231. rect.w = 77;
  232. rect.h = 39;
  233. VIDEO_UpdateScreen(&rect);
  234. }
  235. }
  236. static BOOL
  237. PAL_BattleUIIsActionValid(
  238. BATTLEUIACTION ActionType
  239. )
  240. /*++
  241. Purpose:
  242. Check if the specified action is valid.
  243. Parameters:
  244. [IN] ActionType - the type of the action.
  245. Return value:
  246. TRUE if the action is valid, FALSE if not.
  247. --*/
  248. {
  249. WORD wPlayerRole, w;
  250. int i;
  251. wPlayerRole = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  252. switch (ActionType)
  253. {
  254. case kBattleUIActionAttack:
  255. case kBattleUIActionMisc:
  256. break;
  257. case kBattleUIActionMagic:
  258. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] != 0)
  259. {
  260. return FALSE;
  261. }
  262. break;
  263. case kBattleUIActionCoopMagic:
  264. if (gpGlobals->wMaxPartyMemberIndex == 0)
  265. {
  266. return FALSE;
  267. }
  268. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  269. {
  270. w = gpGlobals->rgParty[i].wPlayerRole;
  271. #ifndef PAL_CLASSIC
  272. if (gpGlobals->g.PlayerRoles.rgwHP[w] < gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5 ||
  273. gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  274. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0 ||
  275. gpGlobals->rgPlayerStatus[w][kStatusSilence] != 0 ||
  276. g_Battle.rgPlayer[i].flTimeMeter < 100 ||
  277. g_Battle.rgPlayer[i].state == kFighterAct)
  278. #else
  279. if (gpGlobals->g.PlayerRoles.rgwHP[w] < gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5 ||
  280. gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  281. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0 ||
  282. gpGlobals->rgPlayerStatus[w][kStatusSilence] != 0)
  283. #endif
  284. {
  285. return FALSE;
  286. }
  287. }
  288. break;
  289. }
  290. return TRUE;
  291. }
  292. static VOID
  293. PAL_BattleUIDrawMiscMenu(
  294. WORD wCurrentItem,
  295. BOOL fConfirmed
  296. )
  297. /*++
  298. Purpose:
  299. Draw the misc menu.
  300. Parameters:
  301. [IN] wCurrentItem - the current selected menu item.
  302. [IN] fConfirmed - TRUE if confirmed, FALSE if not.
  303. Return value:
  304. None.
  305. --*/
  306. {
  307. int i;
  308. BYTE bColor;
  309. #ifdef PAL_CLASSIC
  310. MENUITEM rgMenuItem[] = {
  311. // value label enabled position
  312. { 0, BATTLEUI_LABEL_AUTO, TRUE, PAL_XY(16, 32) },
  313. { 1, BATTLEUI_LABEL_INVENTORY, TRUE, PAL_XY(16, 50) },
  314. { 2, BATTLEUI_LABEL_DEFEND, TRUE, PAL_XY(16, 68) },
  315. { 3, BATTLEUI_LABEL_FLEE, TRUE, PAL_XY(16, 86) },
  316. { 4, BATTLEUI_LABEL_STATUS, TRUE, PAL_XY(16, 104) }
  317. };
  318. #else
  319. MENUITEM rgMenuItem[] = {
  320. // value label enabled position
  321. { 0, BATTLEUI_LABEL_ITEM, TRUE, PAL_XY(16, 32) },
  322. { 1, BATTLEUI_LABEL_DEFEND, TRUE, PAL_XY(16, 50) },
  323. { 2, BATTLEUI_LABEL_AUTO, TRUE, PAL_XY(16, 68) },
  324. { 3, BATTLEUI_LABEL_FLEE, TRUE, PAL_XY(16, 86) },
  325. { 4, BATTLEUI_LABEL_STATUS, TRUE, PAL_XY(16, 104) }
  326. };
  327. #endif
  328. //
  329. // Draw the box
  330. //
  331. PAL_CreateBox(PAL_XY(2, 20), 4, 1, 0, FALSE);
  332. //
  333. // Draw the menu items
  334. //
  335. for (i = 0; i < 5; i++)
  336. {
  337. bColor = MENUITEM_COLOR;
  338. if (i == wCurrentItem)
  339. {
  340. if (fConfirmed)
  341. {
  342. bColor = MENUITEM_COLOR_CONFIRMED;
  343. }
  344. else
  345. {
  346. bColor = MENUITEM_COLOR_SELECTED;
  347. }
  348. }
  349. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor,
  350. TRUE, FALSE);
  351. }
  352. }
  353. static WORD
  354. PAL_BattleUIMiscMenuUpdate(
  355. VOID
  356. )
  357. /*++
  358. Purpose:
  359. Update the misc menu.
  360. Parameters:
  361. None.
  362. Return value:
  363. The selected item number. 0 if cancelled, 0xFFFF if not confirmed.
  364. --*/
  365. {
  366. //
  367. // Draw the menu
  368. //
  369. PAL_BattleUIDrawMiscMenu(g_iCurMiscMenuItem, FALSE);
  370. //
  371. // Process inputs
  372. //
  373. if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  374. {
  375. g_iCurMiscMenuItem--;
  376. if (g_iCurMiscMenuItem < 0)
  377. {
  378. g_iCurMiscMenuItem = 4;
  379. }
  380. }
  381. else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  382. {
  383. g_iCurMiscMenuItem++;
  384. if (g_iCurMiscMenuItem > 4)
  385. {
  386. g_iCurMiscMenuItem = 0;
  387. }
  388. }
  389. else if (g_InputState.dwKeyPress & kKeySearch)
  390. {
  391. return g_iCurMiscMenuItem + 1;
  392. }
  393. else if (g_InputState.dwKeyPress & kKeyMenu)
  394. {
  395. return 0;
  396. }
  397. return 0xFFFF;
  398. }
  399. static WORD
  400. PAL_BattleUIMiscItemSubMenuUpdate(
  401. VOID
  402. )
  403. /*++
  404. Purpose:
  405. Update the item sub menu of the misc menu.
  406. Parameters:
  407. None.
  408. Return value:
  409. The selected item number. 0 if cancelled, 0xFFFF if not confirmed.
  410. --*/
  411. {
  412. int i;
  413. BYTE bColor;
  414. MENUITEM rgMenuItem[] = {
  415. // value label enabled position
  416. { 0, BATTLEUI_LABEL_USEITEM, TRUE, PAL_XY(44, 62) },
  417. { 1, BATTLEUI_LABEL_THROWITEM, TRUE, PAL_XY(44, 80) },
  418. };
  419. //
  420. // Draw the menu
  421. //
  422. #ifdef PAL_CLASSIC
  423. PAL_BattleUIDrawMiscMenu(1, TRUE);
  424. #else
  425. PAL_BattleUIDrawMiscMenu(0, TRUE);
  426. #endif
  427. PAL_CreateBox(PAL_XY(30, 50), 1, PAL_MenuTextMaxWidth(rgMenuItem, 2) - 1, 0, FALSE);
  428. //
  429. // Draw the menu items
  430. //
  431. for (i = 0; i < 2; i++)
  432. {
  433. bColor = MENUITEM_COLOR;
  434. if (i == g_iCurSubMenuItem)
  435. {
  436. bColor = MENUITEM_COLOR_SELECTED;
  437. }
  438. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor,
  439. TRUE, FALSE);
  440. }
  441. //
  442. // Process inputs
  443. //
  444. if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  445. {
  446. g_iCurSubMenuItem = 0;
  447. }
  448. else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  449. {
  450. g_iCurSubMenuItem = 1;
  451. }
  452. else if (g_InputState.dwKeyPress & kKeySearch)
  453. {
  454. return g_iCurSubMenuItem + 1;
  455. }
  456. else if (g_InputState.dwKeyPress & kKeyMenu)
  457. {
  458. return 0;
  459. }
  460. return 0xFFFF;
  461. }
  462. VOID
  463. PAL_BattleUIShowText(
  464. LPCWSTR lpszText,
  465. WORD wDuration
  466. )
  467. /*++
  468. Purpose:
  469. Show a text message in the battle.
  470. Parameters:
  471. [IN] lpszText - the text message to be shown.
  472. [IN] wDuration - the duration of the message, in milliseconds.
  473. Return value:
  474. None.
  475. --*/
  476. {
  477. if (SDL_GetTicks() < g_Battle.UI.dwMsgShowTime)
  478. {
  479. wcscpy(g_Battle.UI.szNextMsg, lpszText);
  480. g_Battle.UI.wNextMsgDuration = wDuration;
  481. }
  482. else
  483. {
  484. wcscpy(g_Battle.UI.szMsg, lpszText);
  485. g_Battle.UI.dwMsgShowTime = SDL_GetTicks() + wDuration;
  486. }
  487. }
  488. VOID
  489. PAL_BattleUIPlayerReady(
  490. WORD wPlayerIndex
  491. )
  492. /*++
  493. Purpose:
  494. Start the action selection menu of the specified player.
  495. Parameters:
  496. [IN] wPlayerIndex - the player index.
  497. Return value:
  498. None.
  499. --*/
  500. {
  501. #ifndef PAL_CLASSIC
  502. WORD w = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  503. #endif
  504. g_Battle.UI.wCurPlayerIndex = wPlayerIndex;
  505. g_Battle.UI.state = kBattleUISelectMove;
  506. g_Battle.UI.wSelectedAction = 0;
  507. g_Battle.UI.MenuState = kBattleMenuMain;
  508. #ifndef PAL_CLASSIC
  509. //
  510. // Play a sound which indicates the player is ready
  511. //
  512. if (gpGlobals->rgPlayerStatus[w][kStatusPuppet] == 0 &&
  513. gpGlobals->rgPlayerStatus[w][kStatusSleep] == 0 &&
  514. gpGlobals->rgPlayerStatus[w][kStatusConfused] == 0 &&
  515. !g_Battle.UI.fAutoAttack && !gpGlobals->fAutoBattle)
  516. {
  517. SOUND_PlayChannel(78, 1);
  518. }
  519. #endif
  520. }
  521. static VOID
  522. PAL_BattleUIUseItem(
  523. VOID
  524. )
  525. /*++
  526. Purpose:
  527. Use an item in the battle UI.
  528. Parameters:
  529. None.
  530. Return value:
  531. None.
  532. --*/
  533. {
  534. WORD wSelectedItem;
  535. wSelectedItem = PAL_ItemSelectMenuUpdate();
  536. if (wSelectedItem != 0xFFFF)
  537. {
  538. if (wSelectedItem != 0)
  539. {
  540. g_Battle.UI.wActionType = kBattleActionUseItem;
  541. g_Battle.UI.wObjectID = wSelectedItem;
  542. if (gpGlobals->g.rgObject[wSelectedItem].item.wFlags & kItemFlagApplyToAll)
  543. {
  544. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  545. }
  546. else
  547. {
  548. #ifdef PAL_CLASSIC
  549. g_Battle.UI.wSelectedIndex = 0;
  550. #else
  551. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  552. #endif
  553. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  554. }
  555. }
  556. else
  557. {
  558. g_Battle.UI.MenuState = kBattleMenuMain;
  559. }
  560. }
  561. }
  562. static VOID
  563. PAL_BattleUIThrowItem(
  564. VOID
  565. )
  566. /*++
  567. Purpose:
  568. Throw an item in the battle UI.
  569. Parameters:
  570. None.
  571. Return value:
  572. None.
  573. --*/
  574. {
  575. WORD wSelectedItem = PAL_ItemSelectMenuUpdate();
  576. if (wSelectedItem != 0xFFFF)
  577. {
  578. if (wSelectedItem != 0)
  579. {
  580. g_Battle.UI.wActionType = kBattleActionThrowItem;
  581. g_Battle.UI.wObjectID = wSelectedItem;
  582. if (gpGlobals->g.rgObject[wSelectedItem].item.wFlags & kItemFlagApplyToAll)
  583. {
  584. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  585. }
  586. else
  587. {
  588. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  589. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  590. }
  591. }
  592. else
  593. {
  594. g_Battle.UI.MenuState = kBattleMenuMain;
  595. }
  596. }
  597. }
  598. static WORD
  599. PAL_BattleUIPickAutoMagic(
  600. WORD wPlayerRole,
  601. WORD wRandomRange
  602. )
  603. /*++
  604. Purpose:
  605. Pick a magic for the specified player for automatic usage.
  606. Parameters:
  607. [IN] wPlayerRole - the player role ID.
  608. [IN] wRandomRange - the range of the magic power.
  609. Return value:
  610. The object ID of the selected magic. 0 for physical attack.
  611. --*/
  612. {
  613. WORD wMagic = 0, w, wMagicNum;
  614. int i, iMaxPower = 0, iPower;
  615. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] != 0)
  616. {
  617. return 0;
  618. }
  619. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  620. {
  621. w = gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole];
  622. if (w == 0)
  623. {
  624. continue;
  625. }
  626. wMagicNum = gpGlobals->g.rgObject[w].magic.wMagicNumber;
  627. //
  628. // skip if the magic is an ultimate move or not enough MP
  629. //
  630. if (gpGlobals->g.lprgMagic[wMagicNum].wCostMP == 1 ||
  631. gpGlobals->g.lprgMagic[wMagicNum].wCostMP > gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] ||
  632. (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) <= 0)
  633. {
  634. continue;
  635. }
  636. iPower = (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) +
  637. RandomLong(0, wRandomRange);
  638. if (iPower > iMaxPower)
  639. {
  640. iMaxPower = iPower;
  641. wMagic = w;
  642. }
  643. }
  644. return wMagic;
  645. }
  646. VOID
  647. PAL_BattleUIUpdate(
  648. VOID
  649. )
  650. /*++
  651. Purpose:
  652. Update the status of battle UI.
  653. Parameters:
  654. None.
  655. Return value:
  656. None.
  657. --*/
  658. {
  659. int i, j, x, y;
  660. WORD wPlayerRole, w;
  661. static int s_iFrame = 0;
  662. s_iFrame++;
  663. if (g_Battle.UI.fAutoAttack && !gpGlobals->fAutoBattle)
  664. {
  665. //
  666. // Draw the "auto attack" message if in the autoattack mode.
  667. //
  668. if (g_InputState.dwKeyPress & kKeyMenu)
  669. {
  670. g_Battle.UI.fAutoAttack = FALSE;
  671. }
  672. else
  673. {
  674. PAL_DrawText(PAL_GetWord(BATTLEUI_LABEL_AUTO), PAL_XY(280, 10),
  675. MENUITEM_COLOR_CONFIRMED, TRUE, FALSE);
  676. }
  677. }
  678. if (gpGlobals->fAutoBattle)
  679. {
  680. PAL_BattlePlayerCheckReady();
  681. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  682. {
  683. if (g_Battle.rgPlayer[i].state == kFighterCom)
  684. {
  685. PAL_BattleUIPlayerReady(i);
  686. break;
  687. }
  688. }
  689. if (g_Battle.UI.state != kBattleUIWait)
  690. {
  691. w = PAL_BattleUIPickAutoMagic(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole, 9999);
  692. if (w == 0)
  693. {
  694. g_Battle.UI.wActionType = kBattleActionAttack;
  695. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  696. }
  697. else
  698. {
  699. g_Battle.UI.wActionType = kBattleActionMagic;
  700. g_Battle.UI.wObjectID = w;
  701. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  702. {
  703. g_Battle.UI.wSelectedIndex = -1;
  704. }
  705. else
  706. {
  707. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  708. }
  709. }
  710. PAL_BattleCommitAction(FALSE);
  711. }
  712. goto end;
  713. }
  714. if (g_InputState.dwKeyPress & kKeyAuto)
  715. {
  716. g_Battle.UI.fAutoAttack = !g_Battle.UI.fAutoAttack;
  717. g_Battle.UI.MenuState = kBattleMenuMain;
  718. }
  719. #ifdef PAL_CLASSIC
  720. if (g_Battle.Phase == kBattlePhasePerformAction)
  721. {
  722. goto end;
  723. }
  724. if (!g_Battle.UI.fAutoAttack)
  725. #endif
  726. {
  727. //
  728. // Draw the player info boxes.
  729. //
  730. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  731. {
  732. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  733. w = (WORD)(g_Battle.rgPlayer[i].flTimeMeter);
  734. j = TIMEMETER_COLOR_DEFAULT;
  735. #ifndef PAL_CLASSIC
  736. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] > 0)
  737. {
  738. j = TIMEMETER_COLOR_HASTE;
  739. }
  740. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0)
  741. {
  742. j = TIMEMETER_COLOR_SLOW;
  743. }
  744. #endif
  745. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] != 0 ||
  746. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] != 0 ||
  747. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] != 0)
  748. {
  749. w = 0;
  750. }
  751. PAL_PlayerInfoBox(PAL_XY(91 + 77 * i, 165), wPlayerRole,
  752. w, j, FALSE);
  753. }
  754. }
  755. if (g_InputState.dwKeyPress & kKeyStatus)
  756. {
  757. PAL_PlayerStatus();
  758. goto end;
  759. }
  760. if (g_Battle.UI.state != kBattleUIWait)
  761. {
  762. wPlayerRole = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  763. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  764. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet])
  765. {
  766. g_Battle.UI.wActionType = kBattleActionAttack;
  767. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  768. {
  769. g_Battle.UI.wSelectedIndex = -1;
  770. }
  771. else
  772. {
  773. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  774. }
  775. PAL_BattleCommitAction(FALSE);
  776. goto end; // don't go further
  777. }
  778. //
  779. // Cancel any actions if player is dead or sleeping.
  780. //
  781. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 ||
  782. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] != 0 ||
  783. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] != 0)
  784. {
  785. g_Battle.UI.wActionType = kBattleActionPass;
  786. PAL_BattleCommitAction(FALSE);
  787. goto end; // don't go further
  788. }
  789. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] != 0)
  790. {
  791. g_Battle.UI.wActionType = kBattleActionAttackMate;
  792. PAL_BattleCommitAction(FALSE);
  793. goto end; // don't go further
  794. }
  795. if (g_Battle.UI.fAutoAttack)
  796. {
  797. g_Battle.UI.wActionType = kBattleActionAttack;
  798. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  799. {
  800. g_Battle.UI.wSelectedIndex = -1;
  801. }
  802. else
  803. {
  804. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  805. }
  806. PAL_BattleCommitAction(FALSE);
  807. goto end; // don't go further
  808. }
  809. //
  810. // Draw the arrow on the player's head.
  811. //
  812. i = SPRITENUM_BATTLE_ARROW_CURRENTPLAYER_RED;
  813. if (s_iFrame & 1)
  814. {
  815. i = SPRITENUM_BATTLE_ARROW_CURRENTPLAYER;
  816. }
  817. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wCurPlayerIndex][0] - 8;
  818. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wCurPlayerIndex][1] - 74;
  819. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, i), gpScreen, PAL_XY(x, y));
  820. }
  821. switch (g_Battle.UI.state)
  822. {
  823. case kBattleUIWait:
  824. if (!g_Battle.fEnemyCleared)
  825. {
  826. PAL_BattlePlayerCheckReady();
  827. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  828. {
  829. if (g_Battle.rgPlayer[i].state == kFighterCom)
  830. {
  831. PAL_BattleUIPlayerReady(i);
  832. break;
  833. }
  834. }
  835. }
  836. break;
  837. case kBattleUISelectMove:
  838. //
  839. // Draw the icons
  840. //
  841. {
  842. struct {
  843. int iSpriteNum;
  844. PAL_POS pos;
  845. BATTLEUIACTION action;
  846. } rgItems[] =
  847. {
  848. {SPRITENUM_BATTLEICON_ATTACK, PAL_XY(27, 140), kBattleUIActionAttack},
  849. {SPRITENUM_BATTLEICON_MAGIC, PAL_XY(0, 155), kBattleUIActionMagic},
  850. {SPRITENUM_BATTLEICON_COOPMAGIC, PAL_XY(54, 155), kBattleUIActionCoopMagic},
  851. {SPRITENUM_BATTLEICON_MISCMENU, PAL_XY(27, 170), kBattleUIActionMisc}
  852. };
  853. if (g_Battle.UI.MenuState == kBattleMenuMain)
  854. {
  855. if (g_InputState.dir == kDirNorth)
  856. {
  857. g_Battle.UI.wSelectedAction = 0;
  858. }
  859. else if (g_InputState.dir == kDirSouth)
  860. {
  861. g_Battle.UI.wSelectedAction = 3;
  862. }
  863. else if (g_InputState.dir == kDirWest)
  864. {
  865. if (PAL_BattleUIIsActionValid(kBattleUIActionMagic))
  866. {
  867. g_Battle.UI.wSelectedAction = 1;
  868. }
  869. }
  870. else if (g_InputState.dir == kDirEast)
  871. {
  872. if (PAL_BattleUIIsActionValid(kBattleUIActionCoopMagic))
  873. {
  874. g_Battle.UI.wSelectedAction = 2;
  875. }
  876. }
  877. }
  878. if (!PAL_BattleUIIsActionValid(rgItems[g_Battle.UI.wSelectedAction].action))
  879. {
  880. g_Battle.UI.wSelectedAction = 0;
  881. }
  882. for (i = 0; i < 4; i++)
  883. {
  884. if (g_Battle.UI.wSelectedAction == i)
  885. {
  886. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, rgItems[i].iSpriteNum),
  887. gpScreen, rgItems[i].pos);
  888. }
  889. else if (PAL_BattleUIIsActionValid(rgItems[i].action))
  890. {
  891. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, rgItems[i].iSpriteNum),
  892. gpScreen, rgItems[i].pos, 0, -4);
  893. }
  894. else
  895. {
  896. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, rgItems[i].iSpriteNum),
  897. gpScreen, rgItems[i].pos, 0x10, -4);
  898. }
  899. }
  900. switch (g_Battle.UI.MenuState)
  901. {
  902. case kBattleMenuMain:
  903. if (g_InputState.dwKeyPress & kKeySearch)
  904. {
  905. switch (g_Battle.UI.wSelectedAction)
  906. {
  907. case 0:
  908. //
  909. // Attack
  910. //
  911. g_Battle.UI.wActionType = kBattleActionAttack;
  912. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  913. {
  914. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  915. }
  916. else
  917. {
  918. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  919. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  920. }
  921. break;
  922. case 1:
  923. //
  924. // Magic
  925. //
  926. g_Battle.UI.MenuState = kBattleMenuMagicSelect;
  927. PAL_MagicSelectionMenuInit(wPlayerRole, TRUE, 0);
  928. break;
  929. case 2:
  930. //
  931. // Cooperative magic
  932. //
  933. w = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  934. w = PAL_GetPlayerCooperativeMagic(w);
  935. g_Battle.UI.wActionType = kBattleActionCoopMagic;
  936. g_Battle.UI.wObjectID = w;
  937. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagUsableToEnemy)
  938. {
  939. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  940. {
  941. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  942. }
  943. else
  944. {
  945. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  946. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  947. }
  948. }
  949. else
  950. {
  951. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  952. {
  953. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  954. }
  955. else
  956. {
  957. #ifdef PAL_CLASSIC
  958. g_Battle.UI.wSelectedIndex = 0;
  959. #else
  960. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  961. #endif
  962. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  963. }
  964. }
  965. break;
  966. case 3:
  967. //
  968. // Misc menu
  969. //
  970. g_Battle.UI.MenuState = kBattleMenuMisc;
  971. g_iCurMiscMenuItem = 0;
  972. break;
  973. }
  974. }
  975. else if (g_InputState.dwKeyPress & kKeyDefend)
  976. {
  977. g_Battle.UI.wActionType = kBattleActionDefend;
  978. PAL_BattleCommitAction(FALSE);
  979. }
  980. else if (g_InputState.dwKeyPress & kKeyForce)
  981. {
  982. w = PAL_BattleUIPickAutoMagic(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole, 60);
  983. if (w == 0)
  984. {
  985. g_Battle.UI.wActionType = kBattleActionAttack;
  986. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  987. {
  988. g_Battle.UI.wSelectedIndex = -1;
  989. }
  990. else
  991. {
  992. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  993. }
  994. }
  995. else
  996. {
  997. g_Battle.UI.wActionType = kBattleActionMagic;
  998. g_Battle.UI.wObjectID = w;
  999. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  1000. {
  1001. g_Battle.UI.wSelectedIndex = -1;
  1002. }
  1003. else
  1004. {
  1005. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  1006. }
  1007. }
  1008. PAL_BattleCommitAction(FALSE);
  1009. }
  1010. else if (g_InputState.dwKeyPress & kKeyFlee)
  1011. {
  1012. g_Battle.UI.wActionType = kBattleActionFlee;
  1013. PAL_BattleCommitAction(FALSE);
  1014. }
  1015. else if (g_InputState.dwKeyPress & kKeyUseItem)
  1016. {
  1017. g_Battle.UI.MenuState = kBattleMenuUseItemSelect;
  1018. PAL_ItemSelectMenuInit(kItemFlagUsable);
  1019. }
  1020. else if (g_InputState.dwKeyPress & kKeyThrowItem)
  1021. {
  1022. g_Battle.UI.MenuState = kBattleMenuThrowItemSelect;
  1023. PAL_ItemSelectMenuInit(kItemFlagThrowable);
  1024. }
  1025. else if (g_InputState.dwKeyPress & kKeyRepeat)
  1026. {
  1027. PAL_BattleCommitAction(TRUE);
  1028. }
  1029. #ifdef PAL_CLASSIC
  1030. else if (g_InputState.dwKeyPress & kKeyMenu)
  1031. {
  1032. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].state = kFighterWait;
  1033. g_Battle.UI.state = kBattleUIWait;
  1034. if (g_Battle.UI.wCurPlayerIndex > 0)
  1035. {
  1036. //
  1037. // Revert to the previous player
  1038. //
  1039. do
  1040. {
  1041. g_Battle.rgPlayer[--g_Battle.UI.wCurPlayerIndex].state = kFighterWait;
  1042. if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType == kBattleActionThrowItem)
  1043. {
  1044. for (i = 0; i < MAX_INVENTORY; i++)
  1045. {
  1046. if (gpGlobals->rgInventory[i].wItem ==
  1047. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID)
  1048. {
  1049. gpGlobals->rgInventory[i].nAmountInUse--;
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. else if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType == kBattleActionUseItem)
  1055. {
  1056. if (gpGlobals->g.rgObject[g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID].item.wFlags & kItemFlagConsuming)
  1057. {
  1058. for (i = 0; i < MAX_INVENTORY; i++)
  1059. {
  1060. if (gpGlobals->rgInventory[i].wItem ==
  1061. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID)
  1062. {
  1063. gpGlobals->rgInventory[i].nAmountInUse--;
  1064. break;
  1065. }
  1066. }
  1067. }
  1068. }
  1069. } while (g_Battle.UI.wCurPlayerIndex > 0 &&
  1070. (gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole] == 0 ||
  1071. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole][kStatusConfused] > 0 ||
  1072. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole][kStatusSleep] > 0 ||
  1073. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole][kStatusParalyzed] > 0));
  1074. }
  1075. }
  1076. #else
  1077. else if (g_InputState.dwKeyPress & kKeyMenu)
  1078. {
  1079. float flMin = -1;
  1080. j = -1;
  1081. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1082. {
  1083. if (g_Battle.rgPlayer[i].flTimeMeter >= 100)
  1084. {
  1085. g_Battle.rgPlayer[i].flTimeMeter += 100; // HACKHACK: Prevent the time meter from going below 100
  1086. if ((g_Battle.rgPlayer[i].flTimeMeter < flMin || flMin < 0) &&
  1087. i != (int)g_Battle.UI.wCurPlayerIndex &&
  1088. g_Battle.rgPlayer[i].state == kFighterWait)
  1089. {
  1090. flMin = g_Battle.rgPlayer[i].flTimeMeter;
  1091. j = i;
  1092. }
  1093. }
  1094. }
  1095. if (j != -1)
  1096. {
  1097. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].flTimeMeter = flMin - 99;
  1098. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].state = kFighterWait;
  1099. g_Battle.UI.state = kBattleUIWait;
  1100. }
  1101. }
  1102. #endif
  1103. break;
  1104. case kBattleMenuMagicSelect:
  1105. w = PAL_MagicSelectionMenuUpdate();
  1106. if (w != 0xFFFF)
  1107. {
  1108. g_Battle.UI.MenuState = kBattleMenuMain;
  1109. if (w != 0)
  1110. {
  1111. g_Battle.UI.wActionType = kBattleActionMagic;
  1112. g_Battle.UI.wObjectID = w;
  1113. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagUsableToEnemy)
  1114. {
  1115. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  1116. {
  1117. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  1118. }
  1119. else
  1120. {
  1121. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  1122. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  1123. }
  1124. }
  1125. else
  1126. {
  1127. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  1128. {
  1129. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  1130. }
  1131. else
  1132. {
  1133. #ifdef PAL_CLASSIC
  1134. g_Battle.UI.wSelectedIndex = 0;
  1135. #else
  1136. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  1137. #endif
  1138. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  1139. }
  1140. }
  1141. }
  1142. }
  1143. break;
  1144. case kBattleMenuUseItemSelect:
  1145. PAL_BattleUIUseItem();
  1146. break;
  1147. case kBattleMenuThrowItemSelect:
  1148. PAL_BattleUIThrowItem();
  1149. break;
  1150. case kBattleMenuMisc:
  1151. w = PAL_BattleUIMiscMenuUpdate();
  1152. if (w != 0xFFFF)
  1153. {
  1154. g_Battle.UI.MenuState = kBattleMenuMain;
  1155. switch (w)
  1156. {
  1157. #ifdef PAL_CLASSIC
  1158. case 2: // item
  1159. #else
  1160. case 1: // item
  1161. #endif
  1162. g_Battle.UI.MenuState = kBattleMenuMiscItemSubMenu;
  1163. g_iCurSubMenuItem = 0;
  1164. break;
  1165. #ifdef PAL_CLASSIC
  1166. case 3: // defend
  1167. #else
  1168. case 2: // defend
  1169. #endif
  1170. g_Battle.UI.wActionType = kBattleActionDefend;
  1171. PAL_BattleCommitAction(FALSE);
  1172. break;
  1173. #ifdef PAL_CLASSIC
  1174. case 1: // auto
  1175. #else
  1176. case 3: // auto
  1177. #endif
  1178. g_Battle.UI.fAutoAttack = TRUE;
  1179. break;
  1180. case 4: // flee
  1181. g_Battle.UI.wActionType = kBattleActionFlee;
  1182. PAL_BattleCommitAction(FALSE);
  1183. break;
  1184. case 5: // status
  1185. PAL_PlayerStatus();
  1186. break;
  1187. }
  1188. }
  1189. break;
  1190. case kBattleMenuMiscItemSubMenu:
  1191. w = PAL_BattleUIMiscItemSubMenuUpdate();
  1192. if (w != 0xFFFF)
  1193. {
  1194. g_Battle.UI.MenuState = kBattleMenuMain;
  1195. switch (w)
  1196. {
  1197. case 1: // use
  1198. g_Battle.UI.MenuState = kBattleMenuUseItemSelect;
  1199. PAL_ItemSelectMenuInit(kItemFlagUsable);
  1200. break;
  1201. case 2: // throw
  1202. g_Battle.UI.MenuState = kBattleMenuThrowItemSelect;
  1203. PAL_ItemSelectMenuInit(kItemFlagThrowable);
  1204. break;
  1205. }
  1206. }
  1207. break;
  1208. }
  1209. }
  1210. break;
  1211. case kBattleUISelectTargetEnemy:
  1212. x = -1;
  1213. y = 0;
  1214. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  1215. {
  1216. if (g_Battle.rgEnemy[i].wObjectID != 0)
  1217. {
  1218. x = i;
  1219. y++;
  1220. }
  1221. }
  1222. if (x == -1)
  1223. {
  1224. g_Battle.UI.state = kBattleUISelectMove;
  1225. break;
  1226. }
  1227. if (g_Battle.UI.wActionType == kBattleActionCoopMagic)
  1228. {
  1229. if (!PAL_BattleUIIsActionValid(kBattleActionCoopMagic))
  1230. {
  1231. g_Battle.UI.state = kBattleUISelectMove;
  1232. break;
  1233. }
  1234. }
  1235. #ifdef PAL_CLASSIC
  1236. //
  1237. // Don't bother selecting when only 1 enemy left
  1238. //
  1239. if (y == 1)
  1240. {
  1241. g_Battle.UI.wPrevEnemyTarget = (WORD)x;
  1242. PAL_BattleCommitAction(FALSE);
  1243. break;
  1244. }
  1245. #endif
  1246. if (g_Battle.UI.wSelectedIndex > x)
  1247. {
  1248. g_Battle.UI.wSelectedIndex = x;
  1249. }
  1250. for (i = 0; i <= x; i++)
  1251. {
  1252. if (g_Battle.rgEnemy[g_Battle.UI.wSelectedIndex].wObjectID != 0)
  1253. {
  1254. break;
  1255. }
  1256. g_Battle.UI.wSelectedIndex++;
  1257. g_Battle.UI.wSelectedIndex %= x + 1;
  1258. }
  1259. //
  1260. // Highlight the selected enemy
  1261. //
  1262. if (s_iFrame & 1)
  1263. {
  1264. i = g_Battle.UI.wSelectedIndex;
  1265. x = PAL_X(g_Battle.rgEnemy[i].pos);
  1266. y = PAL_Y(g_Battle.rgEnemy[i].pos);
  1267. x -= PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame)) / 2;
  1268. y -= PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame));
  1269. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame),
  1270. gpScreen, PAL_XY(x, y), 7);
  1271. }
  1272. if (g_InputState.dwKeyPress & kKeyMenu)
  1273. {
  1274. g_Battle.UI.state = kBattleUISelectMove;
  1275. }
  1276. else if (g_InputState.dwKeyPress & kKeySearch)
  1277. {
  1278. g_Battle.UI.wPrevEnemyTarget = g_Battle.UI.wSelectedIndex;
  1279. PAL_BattleCommitAction(FALSE);
  1280. }
  1281. else if (g_InputState.dwKeyPress & (kKeyLeft | kKeyDown))
  1282. {
  1283. if (g_Battle.UI.wSelectedIndex != 0)
  1284. {
  1285. g_Battle.UI.wSelectedIndex--;
  1286. while (g_Battle.UI.wSelectedIndex != 0 &&
  1287. g_Battle.rgEnemy[g_Battle.UI.wSelectedIndex].wObjectID == 0)
  1288. {
  1289. g_Battle.UI.wSelectedIndex--;
  1290. }
  1291. }
  1292. }
  1293. else if (g_InputState.dwKeyPress & (kKeyRight | kKeyUp))
  1294. {
  1295. if (g_Battle.UI.wSelectedIndex < x)
  1296. {
  1297. g_Battle.UI.wSelectedIndex++;
  1298. while (g_Battle.UI.wSelectedIndex < x &&
  1299. g_Battle.rgEnemy[g_Battle.UI.wSelectedIndex].wObjectID == 0)
  1300. {
  1301. g_Battle.UI.wSelectedIndex++;
  1302. }
  1303. }
  1304. }
  1305. break;
  1306. case kBattleUISelectTargetPlayer:
  1307. #ifdef PAL_CLASSIC
  1308. //
  1309. // Don't bother selecting when only 1 player is in the party
  1310. //
  1311. if (gpGlobals->wMaxPartyMemberIndex == 0)
  1312. {
  1313. g_Battle.UI.wSelectedIndex = 0;
  1314. PAL_BattleCommitAction(FALSE);
  1315. }
  1316. #endif
  1317. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER;
  1318. if (s_iFrame & 1)
  1319. {
  1320. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER_RED;
  1321. }
  1322. //
  1323. // Draw arrows on the selected player
  1324. //
  1325. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wSelectedIndex][0] - 8;
  1326. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wSelectedIndex][1] - 67;
  1327. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, j), gpScreen, PAL_XY(x, y));
  1328. if (g_InputState.dwKeyPress & kKeyMenu)
  1329. {
  1330. g_Battle.UI.state = kBattleUISelectMove;
  1331. }
  1332. else if (g_InputState.dwKeyPress & kKeySearch)
  1333. {
  1334. PAL_BattleCommitAction(FALSE);
  1335. }
  1336. else if (g_InputState.dwKeyPress & (kKeyLeft | kKeyDown))
  1337. {
  1338. if (g_Battle.UI.wSelectedIndex != 0)
  1339. {
  1340. g_Battle.UI.wSelectedIndex--;
  1341. }
  1342. else
  1343. {
  1344. g_Battle.UI.wSelectedIndex = gpGlobals->wMaxPartyMemberIndex;
  1345. }
  1346. }
  1347. else if (g_InputState.dwKeyPress & (kKeyRight | kKeyUp))
  1348. {
  1349. if (g_Battle.UI.wSelectedIndex < gpGlobals->wMaxPartyMemberIndex)
  1350. {
  1351. g_Battle.UI.wSelectedIndex++;
  1352. }
  1353. else
  1354. {
  1355. g_Battle.UI.wSelectedIndex = 0;
  1356. }
  1357. }
  1358. break;
  1359. case kBattleUISelectTargetEnemyAll:
  1360. #ifdef PAL_CLASSIC
  1361. //
  1362. // Don't bother selecting
  1363. //
  1364. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1365. PAL_BattleCommitAction(FALSE);
  1366. #else
  1367. if (g_Battle.UI.wActionType == kBattleActionCoopMagic)
  1368. {
  1369. if (!PAL_BattleUIIsActionValid(kBattleActionCoopMagic))
  1370. {
  1371. g_Battle.UI.state = kBattleUISelectMove;
  1372. break;
  1373. }
  1374. }
  1375. if (s_iFrame & 1)
  1376. {
  1377. //
  1378. // Highlight all enemies
  1379. //
  1380. for (i = g_Battle.wMaxEnemyIndex; i >= 0; i--)
  1381. {
  1382. if (g_Battle.rgEnemy[i].wObjectID == 0)
  1383. {
  1384. continue;
  1385. }
  1386. x = PAL_X(g_Battle.rgEnemy[i].pos);
  1387. y = PAL_Y(g_Battle.rgEnemy[i].pos);
  1388. x -= PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame)) / 2;
  1389. y -= PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame));
  1390. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame),
  1391. gpScreen, PAL_XY(x, y), 7);
  1392. }
  1393. }
  1394. if (g_InputState.dwKeyPress & kKeyMenu)
  1395. {
  1396. g_Battle.UI.state = kBattleUISelectMove;
  1397. }
  1398. else if (g_InputState.dwKeyPress & kKeySearch)
  1399. {
  1400. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1401. PAL_BattleCommitAction(FALSE);
  1402. }
  1403. #endif
  1404. break;
  1405. case kBattleUISelectTargetPlayerAll:
  1406. #ifdef PAL_CLASSIC
  1407. //
  1408. // Don't bother selecting
  1409. //
  1410. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1411. PAL_BattleCommitAction(FALSE);
  1412. #else
  1413. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER;
  1414. if (s_iFrame & 1)
  1415. {
  1416. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER_RED;
  1417. }
  1418. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1419. {
  1420. if (g_Battle.UI.wActionType == kBattleActionMagic)
  1421. {
  1422. w = gpGlobals->g.rgObject[g_Battle.UI.wObjectID].magic.wMagicNumber;
  1423. if (gpGlobals->g.lprgMagic[w].wType == kMagicTypeTrance)
  1424. {
  1425. if (i != g_Battle.UI.wCurPlayerIndex)
  1426. continue;
  1427. }
  1428. }
  1429. //
  1430. // Draw arrows on all players, despite of dead or not
  1431. //
  1432. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][i][0] - 8;
  1433. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][i][1] - 67;
  1434. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, j), gpScreen, PAL_XY(x, y));
  1435. }
  1436. if (g_InputState.dwKeyPress & kKeyMenu)
  1437. {
  1438. g_Battle.UI.state = kBattleUISelectMove;
  1439. }
  1440. else if (g_InputState.dwKeyPress & kKeySearch)
  1441. {
  1442. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1443. PAL_BattleCommitAction(FALSE);
  1444. }
  1445. #endif
  1446. break;
  1447. }
  1448. end:
  1449. //
  1450. // Show the text message if there is one.
  1451. //
  1452. #ifndef PAL_CLASSIC
  1453. if (SDL_GetTicks() < g_Battle.UI.dwMsgShowTime)
  1454. {
  1455. //
  1456. // The text should be shown in a small window at the center of the screen
  1457. //
  1458. PAL_POS pos;
  1459. int i, w = wcslen(g_Battle.UI.szMsg), len = 0;
  1460. for (i = 0; i < w; i++)
  1461. len += PAL_CharWidth(g_Battle.UI.szMsg[i]) >> 3;
  1462. //
  1463. // Create the window box
  1464. //
  1465. pos = PAL_XY(160 - len * 4, 40);
  1466. PAL_CreateSingleLineBox(pos, (len + 1) / 2, FALSE);
  1467. //
  1468. // Show the text on the screen
  1469. //
  1470. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1471. PAL_DrawText(g_Battle.UI.szMsg, pos, 0, FALSE, FALSE);
  1472. }
  1473. else if (g_Battle.UI.szNextMsg[0] != '\0')
  1474. {
  1475. wcscpy(g_Battle.UI.szMsg, g_Battle.UI.szNextMsg);
  1476. g_Battle.UI.dwMsgShowTime = SDL_GetTicks() + g_Battle.UI.wNextMsgDuration;
  1477. g_Battle.UI.szNextMsg[0] = '\0';
  1478. }
  1479. #endif
  1480. //
  1481. // Draw the numbers
  1482. //
  1483. for (i = 0; i < BATTLEUI_MAX_SHOWNUM; i++)
  1484. {
  1485. if (g_Battle.UI.rgShowNum[i].wNum > 0)
  1486. {
  1487. if ((SDL_GetTicks() - g_Battle.UI.rgShowNum[i].dwTime) / BATTLE_FRAME_TIME > 10)
  1488. {
  1489. g_Battle.UI.rgShowNum[i].wNum = 0;
  1490. }
  1491. else
  1492. {
  1493. PAL_DrawNumber(g_Battle.UI.rgShowNum[i].wNum, 5,
  1494. PAL_XY(PAL_X(g_Battle.UI.rgShowNum[i].pos), PAL_Y(g_Battle.UI.rgShowNum[i].pos) - (SDL_GetTicks() - g_Battle.UI.rgShowNum[i].dwTime) / BATTLE_FRAME_TIME),
  1495. g_Battle.UI.rgShowNum[i].color, kNumAlignRight);
  1496. }
  1497. }
  1498. }
  1499. PAL_ClearKeyState();
  1500. }
  1501. VOID
  1502. PAL_BattleUIShowNum(
  1503. WORD wNum,
  1504. PAL_POS pos,
  1505. NUMCOLOR color
  1506. )
  1507. /*++
  1508. Purpose:
  1509. Show a number on battle screen (indicates HP/MP change).
  1510. Parameters:
  1511. [IN] wNum - number to be shown.
  1512. [IN] pos - position of the number on the screen.
  1513. [IN] color - color of the number.
  1514. Return value:
  1515. None.
  1516. --*/
  1517. {
  1518. int i;
  1519. for (i = 0; i < BATTLEUI_MAX_SHOWNUM; i++)
  1520. {
  1521. if (g_Battle.UI.rgShowNum[i].wNum == 0)
  1522. {
  1523. g_Battle.UI.rgShowNum[i].wNum = wNum;
  1524. g_Battle.UI.rgShowNum[i].pos = PAL_XY(PAL_X(pos) - 15, PAL_Y(pos));
  1525. g_Battle.UI.rgShowNum[i].color = color;
  1526. g_Battle.UI.rgShowNum[i].dwTime = SDL_GetTicks();
  1527. break;
  1528. }
  1529. }
  1530. }