uibattle.c 45 KB

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