uibattle.c 47 KB

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