uibattle.c 47 KB

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