uibattle.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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, 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, PAL_MenuTextMaxWidth(rgMenuItem, sizeof(rgMenuItem)/sizeof(MENUITEM)) - 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, TRUE, FALSE, FALSE);
  350. }
  351. }
  352. static WORD
  353. PAL_BattleUIMiscMenuUpdate(
  354. VOID
  355. )
  356. /*++
  357. Purpose:
  358. Update the misc menu.
  359. Parameters:
  360. None.
  361. Return value:
  362. The selected item number. 0 if cancelled, 0xFFFF if not confirmed.
  363. --*/
  364. {
  365. //
  366. // Draw the menu
  367. //
  368. PAL_BattleUIDrawMiscMenu(g_iCurMiscMenuItem, FALSE);
  369. //
  370. // Process inputs
  371. //
  372. if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  373. {
  374. g_iCurMiscMenuItem--;
  375. if (g_iCurMiscMenuItem < 0)
  376. {
  377. g_iCurMiscMenuItem = 4;
  378. }
  379. }
  380. else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  381. {
  382. g_iCurMiscMenuItem++;
  383. if (g_iCurMiscMenuItem > 4)
  384. {
  385. g_iCurMiscMenuItem = 0;
  386. }
  387. }
  388. else if (g_InputState.dwKeyPress & kKeySearch)
  389. {
  390. return g_iCurMiscMenuItem + 1;
  391. }
  392. else if (g_InputState.dwKeyPress & kKeyMenu)
  393. {
  394. return 0;
  395. }
  396. return 0xFFFF;
  397. }
  398. static WORD
  399. PAL_BattleUIMiscItemSubMenuUpdate(
  400. VOID
  401. )
  402. /*++
  403. Purpose:
  404. Update the item sub menu of the misc menu.
  405. Parameters:
  406. None.
  407. Return value:
  408. The selected item number. 0 if cancelled, 0xFFFF if not confirmed.
  409. --*/
  410. {
  411. int i;
  412. BYTE bColor;
  413. MENUITEM rgMenuItem[] = {
  414. // value label enabled position
  415. { 0, BATTLEUI_LABEL_USEITEM, TRUE, PAL_XY(44, 62) },
  416. { 1, BATTLEUI_LABEL_THROWITEM, TRUE, PAL_XY(44, 80) },
  417. };
  418. //
  419. // Draw the menu
  420. //
  421. #ifdef PAL_CLASSIC
  422. PAL_BattleUIDrawMiscMenu(1, TRUE);
  423. #else
  424. PAL_BattleUIDrawMiscMenu(0, TRUE);
  425. #endif
  426. PAL_CreateBox(PAL_XY(30, 50), 1, PAL_MenuTextMaxWidth(rgMenuItem, 2) - 1, 0, FALSE);
  427. //
  428. // Draw the menu items
  429. //
  430. for (i = 0; i < 2; i++)
  431. {
  432. bColor = MENUITEM_COLOR;
  433. if (i == g_iCurSubMenuItem)
  434. {
  435. bColor = MENUITEM_COLOR_SELECTED;
  436. }
  437. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor, TRUE, FALSE, FALSE);
  438. }
  439. //
  440. // Process inputs
  441. //
  442. if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  443. {
  444. g_iCurSubMenuItem = 0;
  445. }
  446. else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  447. {
  448. g_iCurSubMenuItem = 1;
  449. }
  450. else if (g_InputState.dwKeyPress & kKeySearch)
  451. {
  452. return g_iCurSubMenuItem + 1;
  453. }
  454. else if (g_InputState.dwKeyPress & kKeyMenu)
  455. {
  456. return 0;
  457. }
  458. return 0xFFFF;
  459. }
  460. VOID
  461. PAL_BattleUIShowText(
  462. LPCWSTR lpszText,
  463. WORD wDuration
  464. )
  465. /*++
  466. Purpose:
  467. Show a text message in the battle.
  468. Parameters:
  469. [IN] lpszText - the text message to be shown.
  470. [IN] wDuration - the duration of the message, in milliseconds.
  471. Return value:
  472. None.
  473. --*/
  474. {
  475. if (!SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
  476. {
  477. wcscpy(g_Battle.UI.szNextMsg, lpszText);
  478. g_Battle.UI.wNextMsgDuration = wDuration;
  479. }
  480. else
  481. {
  482. wcscpy(g_Battle.UI.szMsg, lpszText);
  483. g_Battle.UI.dwMsgShowTime = SDL_GetTicks() + wDuration;
  484. }
  485. }
  486. VOID
  487. PAL_BattleUIPlayerReady(
  488. WORD wPlayerIndex
  489. )
  490. /*++
  491. Purpose:
  492. Start the action selection menu of the specified player.
  493. Parameters:
  494. [IN] wPlayerIndex - the player index.
  495. Return value:
  496. None.
  497. --*/
  498. {
  499. #ifndef PAL_CLASSIC
  500. WORD w = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  501. #endif
  502. g_Battle.UI.wCurPlayerIndex = wPlayerIndex;
  503. g_Battle.UI.state = kBattleUISelectMove;
  504. g_Battle.UI.wSelectedAction = 0;
  505. g_Battle.UI.MenuState = kBattleMenuMain;
  506. #ifndef PAL_CLASSIC
  507. //
  508. // Play a sound which indicates the player is ready
  509. //
  510. if (gpGlobals->rgPlayerStatus[w][kStatusPuppet] == 0 &&
  511. gpGlobals->rgPlayerStatus[w][kStatusSleep] == 0 &&
  512. gpGlobals->rgPlayerStatus[w][kStatusConfused] == 0 &&
  513. !g_Battle.UI.fAutoAttack && !gpGlobals->fAutoBattle)
  514. {
  515. AUDIO_PlaySound(78);
  516. }
  517. #endif
  518. }
  519. static VOID
  520. PAL_BattleUIUseItem(
  521. VOID
  522. )
  523. /*++
  524. Purpose:
  525. Use an item in the battle UI.
  526. Parameters:
  527. None.
  528. Return value:
  529. None.
  530. --*/
  531. {
  532. WORD wSelectedItem;
  533. wSelectedItem = PAL_ItemSelectMenuUpdate();
  534. if (wSelectedItem != 0xFFFF)
  535. {
  536. if (wSelectedItem != 0)
  537. {
  538. g_Battle.UI.wActionType = kBattleActionUseItem;
  539. g_Battle.UI.wObjectID = wSelectedItem;
  540. if (gpGlobals->g.rgObject[wSelectedItem].item.wFlags & kItemFlagApplyToAll)
  541. {
  542. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  543. }
  544. else
  545. {
  546. #ifdef PAL_CLASSIC
  547. g_Battle.UI.wSelectedIndex = 0;
  548. #else
  549. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  550. #endif
  551. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  552. }
  553. }
  554. else
  555. {
  556. g_Battle.UI.MenuState = kBattleMenuMain;
  557. }
  558. }
  559. }
  560. static VOID
  561. PAL_BattleUIThrowItem(
  562. VOID
  563. )
  564. /*++
  565. Purpose:
  566. Throw an item in the battle UI.
  567. Parameters:
  568. None.
  569. Return value:
  570. None.
  571. --*/
  572. {
  573. WORD wSelectedItem = PAL_ItemSelectMenuUpdate();
  574. if (wSelectedItem != 0xFFFF)
  575. {
  576. if (wSelectedItem != 0)
  577. {
  578. g_Battle.UI.wActionType = kBattleActionThrowItem;
  579. g_Battle.UI.wObjectID = wSelectedItem;
  580. if (gpGlobals->g.rgObject[wSelectedItem].item.wFlags & kItemFlagApplyToAll)
  581. {
  582. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  583. }
  584. else
  585. {
  586. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  587. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  588. }
  589. }
  590. else
  591. {
  592. g_Battle.UI.MenuState = kBattleMenuMain;
  593. }
  594. }
  595. }
  596. static WORD
  597. PAL_BattleUIPickAutoMagic(
  598. WORD wPlayerRole,
  599. WORD wRandomRange
  600. )
  601. /*++
  602. Purpose:
  603. Pick a magic for the specified player for automatic usage.
  604. Parameters:
  605. [IN] wPlayerRole - the player role ID.
  606. [IN] wRandomRange - the range of the magic power.
  607. Return value:
  608. The object ID of the selected magic. 0 for physical attack.
  609. --*/
  610. {
  611. WORD wMagic = 0, w, wMagicNum;
  612. int i, iMaxPower = 0, iPower;
  613. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] != 0)
  614. {
  615. return 0;
  616. }
  617. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  618. {
  619. w = gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole];
  620. if (w == 0)
  621. {
  622. continue;
  623. }
  624. wMagicNum = gpGlobals->g.rgObject[w].magic.wMagicNumber;
  625. //
  626. // skip if the magic is an ultimate move or not enough MP
  627. //
  628. if (gpGlobals->g.lprgMagic[wMagicNum].wCostMP == 1 ||
  629. gpGlobals->g.lprgMagic[wMagicNum].wCostMP > gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] ||
  630. (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) <= 0)
  631. {
  632. continue;
  633. }
  634. iPower = (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) +
  635. RandomLong(0, wRandomRange);
  636. if (iPower > iMaxPower)
  637. {
  638. iMaxPower = iPower;
  639. wMagic = w;
  640. }
  641. }
  642. return wMagic;
  643. }
  644. VOID
  645. PAL_BattleUIUpdate(
  646. VOID
  647. )
  648. /*++
  649. Purpose:
  650. Update the status of battle UI.
  651. Parameters:
  652. None.
  653. Return value:
  654. None.
  655. --*/
  656. {
  657. int i, j, x, y;
  658. WORD wPlayerRole, w;
  659. static int s_iFrame = 0;
  660. s_iFrame++;
  661. if (g_Battle.UI.fAutoAttack && !gpGlobals->fAutoBattle)
  662. {
  663. //
  664. // Draw the "auto attack" message if in the autoattack mode.
  665. //
  666. if (g_InputState.dwKeyPress & kKeyMenu)
  667. {
  668. g_Battle.UI.fAutoAttack = FALSE;
  669. }
  670. else
  671. {
  672. LPCWSTR itemText = PAL_GetWord(BATTLEUI_LABEL_AUTO);
  673. PAL_DrawText(itemText, PAL_XY(312-PAL_TextWidth(itemText), 10),
  674. MENUITEM_COLOR_CONFIRMED, TRUE, FALSE, FALSE);
  675. }
  676. }
  677. if (gpGlobals->fAutoBattle)
  678. {
  679. PAL_BattlePlayerCheckReady();
  680. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  681. {
  682. if (g_Battle.rgPlayer[i].state == kFighterCom)
  683. {
  684. PAL_BattleUIPlayerReady(i);
  685. break;
  686. }
  687. }
  688. if (g_Battle.UI.state != kBattleUIWait)
  689. {
  690. w = PAL_BattleUIPickAutoMagic(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole, 9999);
  691. if (w == 0)
  692. {
  693. g_Battle.UI.wActionType = kBattleActionAttack;
  694. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  695. }
  696. else
  697. {
  698. g_Battle.UI.wActionType = kBattleActionMagic;
  699. g_Battle.UI.wObjectID = w;
  700. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  701. {
  702. g_Battle.UI.wSelectedIndex = -1;
  703. }
  704. else
  705. {
  706. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  707. }
  708. }
  709. PAL_BattleCommitAction(FALSE);
  710. }
  711. goto end;
  712. }
  713. if (g_InputState.dwKeyPress & kKeyAuto)
  714. {
  715. g_Battle.UI.fAutoAttack = !g_Battle.UI.fAutoAttack;
  716. g_Battle.UI.MenuState = kBattleMenuMain;
  717. }
  718. #ifdef PAL_CLASSIC
  719. if (g_Battle.Phase == kBattlePhasePerformAction)
  720. {
  721. goto end;
  722. }
  723. if (!g_Battle.UI.fAutoAttack)
  724. #endif
  725. {
  726. //
  727. // Draw the player info boxes.
  728. //
  729. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  730. {
  731. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  732. w = (WORD)(g_Battle.rgPlayer[i].flTimeMeter);
  733. j = TIMEMETER_COLOR_DEFAULT;
  734. #ifndef PAL_CLASSIC
  735. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusHaste] > 0)
  736. {
  737. j = TIMEMETER_COLOR_HASTE;
  738. }
  739. else if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSlow] > 0)
  740. {
  741. j = TIMEMETER_COLOR_SLOW;
  742. }
  743. #endif
  744. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] != 0 ||
  745. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] != 0 ||
  746. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet] != 0)
  747. {
  748. w = 0;
  749. }
  750. PAL_PlayerInfoBox(PAL_XY(91 + 77 * i, 165), wPlayerRole,
  751. w, j, FALSE);
  752. }
  753. }
  754. if (g_InputState.dwKeyPress & kKeyStatus)
  755. {
  756. PAL_PlayerStatus();
  757. goto end;
  758. }
  759. if (g_Battle.UI.state != kBattleUIWait)
  760. {
  761. wPlayerRole = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  762. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 &&
  763. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusPuppet])
  764. {
  765. g_Battle.UI.wActionType = kBattleActionAttack;
  766. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  767. {
  768. g_Battle.UI.wSelectedIndex = -1;
  769. }
  770. else
  771. {
  772. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  773. }
  774. PAL_BattleCommitAction(FALSE);
  775. goto end; // don't go further
  776. }
  777. //
  778. // Cancel any actions if player is dead or sleeping.
  779. //
  780. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0 ||
  781. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSleep] != 0 ||
  782. gpGlobals->rgPlayerStatus[wPlayerRole][kStatusParalyzed] != 0)
  783. {
  784. g_Battle.UI.wActionType = kBattleActionPass;
  785. PAL_BattleCommitAction(FALSE);
  786. goto end; // don't go further
  787. }
  788. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusConfused] != 0)
  789. {
  790. g_Battle.UI.wActionType = kBattleActionAttackMate;
  791. PAL_BattleCommitAction(FALSE);
  792. goto end; // don't go further
  793. }
  794. if (g_Battle.UI.fAutoAttack)
  795. {
  796. g_Battle.UI.wActionType = kBattleActionAttack;
  797. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  798. {
  799. g_Battle.UI.wSelectedIndex = -1;
  800. }
  801. else
  802. {
  803. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  804. }
  805. PAL_BattleCommitAction(FALSE);
  806. goto end; // don't go further
  807. }
  808. //
  809. // Draw the arrow on the player's head.
  810. //
  811. i = SPRITENUM_BATTLE_ARROW_CURRENTPLAYER_RED;
  812. if (s_iFrame & 1)
  813. {
  814. i = SPRITENUM_BATTLE_ARROW_CURRENTPLAYER;
  815. }
  816. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wCurPlayerIndex][0] - 8;
  817. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wCurPlayerIndex][1] - 74;
  818. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, i), gpScreen, PAL_XY(x, y));
  819. }
  820. switch (g_Battle.UI.state)
  821. {
  822. case kBattleUIWait:
  823. if (!g_Battle.fEnemyCleared)
  824. {
  825. PAL_BattlePlayerCheckReady();
  826. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  827. {
  828. if (g_Battle.rgPlayer[i].state == kFighterCom)
  829. {
  830. PAL_BattleUIPlayerReady(i);
  831. break;
  832. }
  833. }
  834. }
  835. break;
  836. case kBattleUISelectMove:
  837. //
  838. // Draw the icons
  839. //
  840. {
  841. struct {
  842. int iSpriteNum;
  843. PAL_POS pos;
  844. BATTLEUIACTION action;
  845. } rgItems[] =
  846. {
  847. {SPRITENUM_BATTLEICON_ATTACK, PAL_XY(27, 140), kBattleUIActionAttack},
  848. {SPRITENUM_BATTLEICON_MAGIC, PAL_XY(0, 155), kBattleUIActionMagic},
  849. {SPRITENUM_BATTLEICON_COOPMAGIC, PAL_XY(54, 155), kBattleUIActionCoopMagic},
  850. {SPRITENUM_BATTLEICON_MISCMENU, PAL_XY(27, 170), kBattleUIActionMisc}
  851. };
  852. if (g_Battle.UI.MenuState == kBattleMenuMain)
  853. {
  854. if (g_InputState.dir == kDirNorth)
  855. {
  856. g_Battle.UI.wSelectedAction = 0;
  857. }
  858. else if (g_InputState.dir == kDirSouth)
  859. {
  860. g_Battle.UI.wSelectedAction = 3;
  861. }
  862. else if (g_InputState.dir == kDirWest)
  863. {
  864. if (PAL_BattleUIIsActionValid(kBattleUIActionMagic))
  865. {
  866. g_Battle.UI.wSelectedAction = 1;
  867. }
  868. }
  869. else if (g_InputState.dir == kDirEast)
  870. {
  871. if (PAL_BattleUIIsActionValid(kBattleUIActionCoopMagic))
  872. {
  873. g_Battle.UI.wSelectedAction = 2;
  874. }
  875. }
  876. }
  877. if (!PAL_BattleUIIsActionValid(rgItems[g_Battle.UI.wSelectedAction].action))
  878. {
  879. g_Battle.UI.wSelectedAction = 0;
  880. }
  881. for (i = 0; i < 4; i++)
  882. {
  883. if (g_Battle.UI.wSelectedAction == i)
  884. {
  885. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, rgItems[i].iSpriteNum),
  886. gpScreen, rgItems[i].pos);
  887. }
  888. else if (PAL_BattleUIIsActionValid(rgItems[i].action))
  889. {
  890. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, rgItems[i].iSpriteNum),
  891. gpScreen, rgItems[i].pos, 0, -4);
  892. }
  893. else
  894. {
  895. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, rgItems[i].iSpriteNum),
  896. gpScreen, rgItems[i].pos, 0x10, -4);
  897. }
  898. }
  899. switch (g_Battle.UI.MenuState)
  900. {
  901. case kBattleMenuMain:
  902. if (g_InputState.dwKeyPress & kKeySearch)
  903. {
  904. switch (g_Battle.UI.wSelectedAction)
  905. {
  906. case 0:
  907. //
  908. // Attack
  909. //
  910. g_Battle.UI.wActionType = kBattleActionAttack;
  911. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  912. {
  913. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  914. }
  915. else
  916. {
  917. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  918. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  919. }
  920. break;
  921. case 1:
  922. //
  923. // Magic
  924. //
  925. g_Battle.UI.MenuState = kBattleMenuMagicSelect;
  926. PAL_MagicSelectionMenuInit(wPlayerRole, TRUE, 0);
  927. break;
  928. case 2:
  929. //
  930. // Cooperative magic
  931. //
  932. w = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  933. w = PAL_GetPlayerCooperativeMagic(w);
  934. g_Battle.UI.wActionType = kBattleActionCoopMagic;
  935. g_Battle.UI.wObjectID = w;
  936. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagUsableToEnemy)
  937. {
  938. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  939. {
  940. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  941. }
  942. else
  943. {
  944. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  945. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  946. }
  947. }
  948. else
  949. {
  950. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  951. {
  952. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  953. }
  954. else
  955. {
  956. #ifdef PAL_CLASSIC
  957. g_Battle.UI.wSelectedIndex = 0;
  958. #else
  959. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  960. #endif
  961. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  962. }
  963. }
  964. break;
  965. case 3:
  966. //
  967. // Misc menu
  968. //
  969. g_Battle.UI.MenuState = kBattleMenuMisc;
  970. g_iCurMiscMenuItem = 0;
  971. break;
  972. }
  973. }
  974. else if (g_InputState.dwKeyPress & kKeyDefend)
  975. {
  976. g_Battle.UI.wActionType = kBattleActionDefend;
  977. PAL_BattleCommitAction(FALSE);
  978. }
  979. else if (g_InputState.dwKeyPress & kKeyForce)
  980. {
  981. w = PAL_BattleUIPickAutoMagic(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole, 60);
  982. if (w == 0)
  983. {
  984. g_Battle.UI.wActionType = kBattleActionAttack;
  985. if (PAL_PlayerCanAttackAll(gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole))
  986. {
  987. g_Battle.UI.wSelectedIndex = -1;
  988. }
  989. else
  990. {
  991. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  992. }
  993. }
  994. else
  995. {
  996. g_Battle.UI.wActionType = kBattleActionMagic;
  997. g_Battle.UI.wObjectID = w;
  998. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  999. {
  1000. g_Battle.UI.wSelectedIndex = -1;
  1001. }
  1002. else
  1003. {
  1004. g_Battle.UI.wSelectedIndex = PAL_BattleSelectAutoTarget();
  1005. }
  1006. }
  1007. PAL_BattleCommitAction(FALSE);
  1008. }
  1009. else if (g_InputState.dwKeyPress & kKeyFlee)
  1010. {
  1011. g_Battle.UI.wActionType = kBattleActionFlee;
  1012. PAL_BattleCommitAction(FALSE);
  1013. }
  1014. else if (g_InputState.dwKeyPress & kKeyUseItem)
  1015. {
  1016. g_Battle.UI.MenuState = kBattleMenuUseItemSelect;
  1017. PAL_ItemSelectMenuInit(kItemFlagUsable);
  1018. }
  1019. else if (g_InputState.dwKeyPress & kKeyThrowItem)
  1020. {
  1021. g_Battle.UI.MenuState = kBattleMenuThrowItemSelect;
  1022. PAL_ItemSelectMenuInit(kItemFlagThrowable);
  1023. }
  1024. else if (g_InputState.dwKeyPress & kKeyRepeat)
  1025. {
  1026. PAL_BattleCommitAction(TRUE);
  1027. }
  1028. #ifdef PAL_CLASSIC
  1029. else if (g_InputState.dwKeyPress & kKeyMenu)
  1030. {
  1031. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].state = kFighterWait;
  1032. g_Battle.UI.state = kBattleUIWait;
  1033. if (g_Battle.UI.wCurPlayerIndex > 0)
  1034. {
  1035. //
  1036. // Revert to the previous player
  1037. //
  1038. do
  1039. {
  1040. g_Battle.rgPlayer[--g_Battle.UI.wCurPlayerIndex].state = kFighterWait;
  1041. if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType == kBattleActionThrowItem)
  1042. {
  1043. for (i = 0; i < MAX_INVENTORY; i++)
  1044. {
  1045. if (gpGlobals->rgInventory[i].wItem ==
  1046. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID)
  1047. {
  1048. gpGlobals->rgInventory[i].nAmountInUse--;
  1049. break;
  1050. }
  1051. }
  1052. }
  1053. else if (g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.ActionType == kBattleActionUseItem)
  1054. {
  1055. if (gpGlobals->g.rgObject[g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID].item.wFlags & kItemFlagConsuming)
  1056. {
  1057. for (i = 0; i < MAX_INVENTORY; i++)
  1058. {
  1059. if (gpGlobals->rgInventory[i].wItem ==
  1060. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].action.wActionID)
  1061. {
  1062. gpGlobals->rgInventory[i].nAmountInUse--;
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. } while (g_Battle.UI.wCurPlayerIndex > 0 &&
  1069. (gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole] == 0 ||
  1070. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole][kStatusConfused] > 0 ||
  1071. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole][kStatusSleep] > 0 ||
  1072. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole][kStatusParalyzed] > 0));
  1073. }
  1074. }
  1075. #else
  1076. else if (g_InputState.dwKeyPress & kKeyMenu)
  1077. {
  1078. float flMin = -1;
  1079. j = -1;
  1080. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1081. {
  1082. if (g_Battle.rgPlayer[i].flTimeMeter >= 100)
  1083. {
  1084. g_Battle.rgPlayer[i].flTimeMeter += 100; // HACKHACK: Prevent the time meter from going below 100
  1085. if ((g_Battle.rgPlayer[i].flTimeMeter < flMin || flMin < 0) &&
  1086. i != (int)g_Battle.UI.wCurPlayerIndex &&
  1087. g_Battle.rgPlayer[i].state == kFighterWait)
  1088. {
  1089. flMin = g_Battle.rgPlayer[i].flTimeMeter;
  1090. j = i;
  1091. }
  1092. }
  1093. }
  1094. if (j != -1)
  1095. {
  1096. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].flTimeMeter = flMin - 99;
  1097. g_Battle.rgPlayer[g_Battle.UI.wCurPlayerIndex].state = kFighterWait;
  1098. g_Battle.UI.state = kBattleUIWait;
  1099. }
  1100. }
  1101. #endif
  1102. break;
  1103. case kBattleMenuMagicSelect:
  1104. w = PAL_MagicSelectionMenuUpdate();
  1105. if (w != 0xFFFF)
  1106. {
  1107. g_Battle.UI.MenuState = kBattleMenuMain;
  1108. if (w != 0)
  1109. {
  1110. g_Battle.UI.wActionType = kBattleActionMagic;
  1111. g_Battle.UI.wObjectID = w;
  1112. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagUsableToEnemy)
  1113. {
  1114. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  1115. {
  1116. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  1117. }
  1118. else
  1119. {
  1120. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  1121. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  1122. }
  1123. }
  1124. else
  1125. {
  1126. if (gpGlobals->g.rgObject[w].magic.wFlags & kMagicFlagApplyToAll)
  1127. {
  1128. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  1129. }
  1130. else
  1131. {
  1132. #ifdef PAL_CLASSIC
  1133. g_Battle.UI.wSelectedIndex = 0;
  1134. #else
  1135. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  1136. #endif
  1137. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  1138. }
  1139. }
  1140. }
  1141. }
  1142. break;
  1143. case kBattleMenuUseItemSelect:
  1144. PAL_BattleUIUseItem();
  1145. break;
  1146. case kBattleMenuThrowItemSelect:
  1147. PAL_BattleUIThrowItem();
  1148. break;
  1149. case kBattleMenuMisc:
  1150. w = PAL_BattleUIMiscMenuUpdate();
  1151. if (w != 0xFFFF)
  1152. {
  1153. g_Battle.UI.MenuState = kBattleMenuMain;
  1154. switch (w)
  1155. {
  1156. #ifdef PAL_CLASSIC
  1157. case 2: // item
  1158. #else
  1159. case 1: // item
  1160. #endif
  1161. g_Battle.UI.MenuState = kBattleMenuMiscItemSubMenu;
  1162. g_iCurSubMenuItem = 0;
  1163. break;
  1164. #ifdef PAL_CLASSIC
  1165. case 3: // defend
  1166. #else
  1167. case 2: // defend
  1168. #endif
  1169. g_Battle.UI.wActionType = kBattleActionDefend;
  1170. PAL_BattleCommitAction(FALSE);
  1171. break;
  1172. #ifdef PAL_CLASSIC
  1173. case 1: // auto
  1174. #else
  1175. case 3: // auto
  1176. #endif
  1177. g_Battle.UI.fAutoAttack = TRUE;
  1178. break;
  1179. case 4: // flee
  1180. g_Battle.UI.wActionType = kBattleActionFlee;
  1181. PAL_BattleCommitAction(FALSE);
  1182. break;
  1183. case 5: // status
  1184. PAL_PlayerStatus();
  1185. break;
  1186. }
  1187. }
  1188. break;
  1189. case kBattleMenuMiscItemSubMenu:
  1190. w = PAL_BattleUIMiscItemSubMenuUpdate();
  1191. if (w != 0xFFFF)
  1192. {
  1193. g_Battle.UI.MenuState = kBattleMenuMain;
  1194. switch (w)
  1195. {
  1196. case 1: // use
  1197. g_Battle.UI.MenuState = kBattleMenuUseItemSelect;
  1198. PAL_ItemSelectMenuInit(kItemFlagUsable);
  1199. break;
  1200. case 2: // throw
  1201. g_Battle.UI.MenuState = kBattleMenuThrowItemSelect;
  1202. PAL_ItemSelectMenuInit(kItemFlagThrowable);
  1203. break;
  1204. }
  1205. }
  1206. break;
  1207. }
  1208. }
  1209. break;
  1210. case kBattleUISelectTargetEnemy:
  1211. x = -1;
  1212. y = 0;
  1213. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  1214. {
  1215. if (g_Battle.rgEnemy[i].wObjectID != 0)
  1216. {
  1217. x = i;
  1218. y++;
  1219. }
  1220. }
  1221. if (x == -1)
  1222. {
  1223. g_Battle.UI.state = kBattleUISelectMove;
  1224. break;
  1225. }
  1226. if (g_Battle.UI.wActionType == kBattleActionCoopMagic)
  1227. {
  1228. if (!PAL_BattleUIIsActionValid(kBattleActionCoopMagic))
  1229. {
  1230. g_Battle.UI.state = kBattleUISelectMove;
  1231. break;
  1232. }
  1233. }
  1234. #ifdef PAL_CLASSIC
  1235. //
  1236. // Don't bother selecting when only 1 enemy left
  1237. //
  1238. if (y == 1)
  1239. {
  1240. g_Battle.UI.wPrevEnemyTarget = (WORD)x;
  1241. PAL_BattleCommitAction(FALSE);
  1242. break;
  1243. }
  1244. #endif
  1245. if (g_Battle.UI.wSelectedIndex > x)
  1246. {
  1247. g_Battle.UI.wSelectedIndex = x;
  1248. }
  1249. for (i = 0; i <= x; i++)
  1250. {
  1251. if (g_Battle.rgEnemy[g_Battle.UI.wSelectedIndex].wObjectID != 0)
  1252. {
  1253. break;
  1254. }
  1255. g_Battle.UI.wSelectedIndex++;
  1256. g_Battle.UI.wSelectedIndex %= x + 1;
  1257. }
  1258. //
  1259. // Highlight the selected enemy
  1260. //
  1261. if (s_iFrame & 1)
  1262. {
  1263. i = g_Battle.UI.wSelectedIndex;
  1264. x = PAL_X(g_Battle.rgEnemy[i].pos);
  1265. y = PAL_Y(g_Battle.rgEnemy[i].pos);
  1266. x -= PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame)) / 2;
  1267. y -= PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame));
  1268. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame),
  1269. gpScreen, PAL_XY(x, y), 7);
  1270. }
  1271. if (g_InputState.dwKeyPress & kKeyMenu)
  1272. {
  1273. g_Battle.UI.state = kBattleUISelectMove;
  1274. }
  1275. else if (g_InputState.dwKeyPress & kKeySearch)
  1276. {
  1277. g_Battle.UI.wPrevEnemyTarget = g_Battle.UI.wSelectedIndex;
  1278. PAL_BattleCommitAction(FALSE);
  1279. }
  1280. else if (g_InputState.dwKeyPress & (kKeyLeft | kKeyDown))
  1281. {
  1282. if (g_Battle.UI.wSelectedIndex != 0)
  1283. {
  1284. g_Battle.UI.wSelectedIndex--;
  1285. while (g_Battle.UI.wSelectedIndex != 0 &&
  1286. g_Battle.rgEnemy[g_Battle.UI.wSelectedIndex].wObjectID == 0)
  1287. {
  1288. g_Battle.UI.wSelectedIndex--;
  1289. }
  1290. }
  1291. }
  1292. else if (g_InputState.dwKeyPress & (kKeyRight | kKeyUp))
  1293. {
  1294. if (g_Battle.UI.wSelectedIndex < x)
  1295. {
  1296. g_Battle.UI.wSelectedIndex++;
  1297. while (g_Battle.UI.wSelectedIndex < x &&
  1298. g_Battle.rgEnemy[g_Battle.UI.wSelectedIndex].wObjectID == 0)
  1299. {
  1300. g_Battle.UI.wSelectedIndex++;
  1301. }
  1302. }
  1303. }
  1304. break;
  1305. case kBattleUISelectTargetPlayer:
  1306. #ifdef PAL_CLASSIC
  1307. //
  1308. // Don't bother selecting when only 1 player is in the party
  1309. //
  1310. if (gpGlobals->wMaxPartyMemberIndex == 0)
  1311. {
  1312. g_Battle.UI.wSelectedIndex = 0;
  1313. PAL_BattleCommitAction(FALSE);
  1314. }
  1315. #endif
  1316. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER;
  1317. if (s_iFrame & 1)
  1318. {
  1319. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER_RED;
  1320. }
  1321. //
  1322. // Draw arrows on the selected player
  1323. //
  1324. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wSelectedIndex][0] - 8;
  1325. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][g_Battle.UI.wSelectedIndex][1] - 67;
  1326. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, j), gpScreen, PAL_XY(x, y));
  1327. if (g_InputState.dwKeyPress & kKeyMenu)
  1328. {
  1329. g_Battle.UI.state = kBattleUISelectMove;
  1330. }
  1331. else if (g_InputState.dwKeyPress & kKeySearch)
  1332. {
  1333. PAL_BattleCommitAction(FALSE);
  1334. }
  1335. else if (g_InputState.dwKeyPress & (kKeyLeft | kKeyDown))
  1336. {
  1337. if (g_Battle.UI.wSelectedIndex != 0)
  1338. {
  1339. g_Battle.UI.wSelectedIndex--;
  1340. }
  1341. else
  1342. {
  1343. g_Battle.UI.wSelectedIndex = gpGlobals->wMaxPartyMemberIndex;
  1344. }
  1345. }
  1346. else if (g_InputState.dwKeyPress & (kKeyRight | kKeyUp))
  1347. {
  1348. if (g_Battle.UI.wSelectedIndex < gpGlobals->wMaxPartyMemberIndex)
  1349. {
  1350. g_Battle.UI.wSelectedIndex++;
  1351. }
  1352. else
  1353. {
  1354. g_Battle.UI.wSelectedIndex = 0;
  1355. }
  1356. }
  1357. break;
  1358. case kBattleUISelectTargetEnemyAll:
  1359. #ifdef PAL_CLASSIC
  1360. //
  1361. // Don't bother selecting
  1362. //
  1363. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1364. PAL_BattleCommitAction(FALSE);
  1365. #else
  1366. if (g_Battle.UI.wActionType == kBattleActionCoopMagic)
  1367. {
  1368. if (!PAL_BattleUIIsActionValid(kBattleActionCoopMagic))
  1369. {
  1370. g_Battle.UI.state = kBattleUISelectMove;
  1371. break;
  1372. }
  1373. }
  1374. if (s_iFrame & 1)
  1375. {
  1376. //
  1377. // Highlight all enemies
  1378. //
  1379. for (i = g_Battle.wMaxEnemyIndex; i >= 0; i--)
  1380. {
  1381. if (g_Battle.rgEnemy[i].wObjectID == 0)
  1382. {
  1383. continue;
  1384. }
  1385. x = PAL_X(g_Battle.rgEnemy[i].pos);
  1386. y = PAL_Y(g_Battle.rgEnemy[i].pos);
  1387. x -= PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame)) / 2;
  1388. y -= PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame));
  1389. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame),
  1390. gpScreen, PAL_XY(x, y), 7);
  1391. }
  1392. }
  1393. if (g_InputState.dwKeyPress & kKeyMenu)
  1394. {
  1395. g_Battle.UI.state = kBattleUISelectMove;
  1396. }
  1397. else if (g_InputState.dwKeyPress & kKeySearch)
  1398. {
  1399. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1400. PAL_BattleCommitAction(FALSE);
  1401. }
  1402. #endif
  1403. break;
  1404. case kBattleUISelectTargetPlayerAll:
  1405. #ifdef PAL_CLASSIC
  1406. //
  1407. // Don't bother selecting
  1408. //
  1409. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1410. PAL_BattleCommitAction(FALSE);
  1411. #else
  1412. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER;
  1413. if (s_iFrame & 1)
  1414. {
  1415. j = SPRITENUM_BATTLE_ARROW_SELECTEDPLAYER_RED;
  1416. }
  1417. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1418. {
  1419. if (g_Battle.UI.wActionType == kBattleActionMagic)
  1420. {
  1421. w = gpGlobals->g.rgObject[g_Battle.UI.wObjectID].magic.wMagicNumber;
  1422. if (gpGlobals->g.lprgMagic[w].wType == kMagicTypeTrance)
  1423. {
  1424. if (i != g_Battle.UI.wCurPlayerIndex)
  1425. continue;
  1426. }
  1427. }
  1428. //
  1429. // Draw arrows on all players, despite of dead or not
  1430. //
  1431. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][i][0] - 8;
  1432. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][i][1] - 67;
  1433. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, j), gpScreen, PAL_XY(x, y));
  1434. }
  1435. if (g_InputState.dwKeyPress & kKeyMenu)
  1436. {
  1437. g_Battle.UI.state = kBattleUISelectMove;
  1438. }
  1439. else if (g_InputState.dwKeyPress & kKeySearch)
  1440. {
  1441. g_Battle.UI.wSelectedIndex = (WORD)-1;
  1442. PAL_BattleCommitAction(FALSE);
  1443. }
  1444. #endif
  1445. break;
  1446. }
  1447. end:
  1448. //
  1449. // Show the text message if there is one.
  1450. //
  1451. #ifndef PAL_CLASSIC
  1452. if (!SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
  1453. {
  1454. //
  1455. // The text should be shown in a small window at the center of the screen
  1456. //
  1457. PAL_POS pos;
  1458. int i, w = wcslen(g_Battle.UI.szMsg), len = 0;
  1459. for (i = 0; i < w; i++)
  1460. len += PAL_CharWidth(g_Battle.UI.szMsg[i]) >> 3;
  1461. //
  1462. // Create the window box
  1463. //
  1464. pos = PAL_XY(160 - len * 4, 40);
  1465. PAL_CreateSingleLineBox(pos, (len + 1) / 2, FALSE);
  1466. //
  1467. // Show the text on the screen
  1468. //
  1469. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1470. PAL_DrawText(g_Battle.UI.szMsg, pos, 0, FALSE, FALSE, FALSE);
  1471. }
  1472. else if (g_Battle.UI.szNextMsg[0] != '\0')
  1473. {
  1474. wcscpy(g_Battle.UI.szMsg, g_Battle.UI.szNextMsg);
  1475. g_Battle.UI.dwMsgShowTime = SDL_GetTicks() + g_Battle.UI.wNextMsgDuration;
  1476. g_Battle.UI.szNextMsg[0] = '\0';
  1477. }
  1478. #endif
  1479. //
  1480. // Draw the numbers
  1481. //
  1482. for (i = 0; i < BATTLEUI_MAX_SHOWNUM; i++)
  1483. {
  1484. if (g_Battle.UI.rgShowNum[i].wNum > 0)
  1485. {
  1486. if ((SDL_GetTicks() - g_Battle.UI.rgShowNum[i].dwTime) / BATTLE_FRAME_TIME > 10)
  1487. {
  1488. g_Battle.UI.rgShowNum[i].wNum = 0;
  1489. }
  1490. else
  1491. {
  1492. PAL_DrawNumber(g_Battle.UI.rgShowNum[i].wNum, 5,
  1493. 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),
  1494. g_Battle.UI.rgShowNum[i].color, kNumAlignRight);
  1495. }
  1496. }
  1497. }
  1498. PAL_ClearKeyState();
  1499. }
  1500. VOID
  1501. PAL_BattleUIShowNum(
  1502. WORD wNum,
  1503. PAL_POS pos,
  1504. NUMCOLOR color
  1505. )
  1506. /*++
  1507. Purpose:
  1508. Show a number on battle screen (indicates HP/MP change).
  1509. Parameters:
  1510. [IN] wNum - number to be shown.
  1511. [IN] pos - position of the number on the screen.
  1512. [IN] color - color of the number.
  1513. Return value:
  1514. None.
  1515. --*/
  1516. {
  1517. int i;
  1518. for (i = 0; i < BATTLEUI_MAX_SHOWNUM; i++)
  1519. {
  1520. if (g_Battle.UI.rgShowNum[i].wNum == 0)
  1521. {
  1522. g_Battle.UI.rgShowNum[i].wNum = wNum;
  1523. g_Battle.UI.rgShowNum[i].pos = PAL_XY(PAL_X(pos) - 15, PAL_Y(pos));
  1524. g_Battle.UI.rgShowNum[i].color = color;
  1525. g_Battle.UI.rgShowNum[i].dwTime = SDL_GetTicks();
  1526. break;
  1527. }
  1528. }
  1529. }