uibattle.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // This file is part of SDLPAL.
  7. //
  8. // SDLPAL is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU General Public License
  19. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. //
  21. // Modified by Lou Yihua <louyihua@21cn.com> with Unicode support, 2015
  22. //
  23. #include "main.h"
  24. extern WORD g_rgPlayerPos[3][3][2];
  25. static int g_iCurMiscMenuItem = 0;
  26. static int g_iCurSubMenuItem = 0;
  27. VOID
  28. PAL_PlayerInfoBox(
  29. PAL_POS pos,
  30. WORD wPlayerRole,
  31. INT iTimeMeter,
  32. BYTE bTimeMeterColor,
  33. BOOL fUpdate
  34. )
  35. /*++
  36. Purpose:
  37. Show the player info box.
  38. Parameters:
  39. [IN] pos - the top-left corner position of the box.
  40. [IN] wPlayerRole - the player role ID to be shown.
  41. [IN] iTimeMeter - the value of time meter. 0 = empty, 100 = full.
  42. [IN] bTimeMeterColor - the color of time meter.
  43. [IN] fUpdate - whether to update the screen area or not.
  44. Return value:
  45. None.
  46. --*/
  47. {
  48. SDL_Rect rect;
  49. BYTE bPoisonColor;
  50. int i, iPartyIndex;
  51. WORD wMaxLevel, w;
  52. const BYTE rgStatusPos[kStatusAll][2] =
  53. {
  54. {35, 19}, // confused
  55. {0, 0}, // slow
  56. {54, 1}, // sleep
  57. {55, 20}, // silence
  58. {0, 0}, // puppet
  59. {0, 0}, // bravery
  60. {0, 0}, // protect
  61. {0, 0}, // haste
  62. {0, 0}, // dualattack
  63. };
  64. const WORD rgwStatusWord[kStatusAll] =
  65. {
  66. 0x1D, // confused
  67. 0x00, // slow
  68. 0x1C, // sleep
  69. 0x1A, // silence
  70. 0x00, // puppet
  71. 0x00, // bravery
  72. 0x00, // protect
  73. 0x00, // haste
  74. 0x00, // dualattack
  75. };
  76. const BYTE rgbStatusColor[kStatusAll] =
  77. {
  78. 0x5F, // confused
  79. 0x00, // slow
  80. 0x0E, // sleep
  81. 0x3C, // silence
  82. 0x00, // puppet
  83. 0x00, // bravery
  84. 0x00, // protect
  85. 0x00, // haste
  86. 0x00, // dualattack
  87. };
  88. //
  89. // Draw the box
  90. //
  91. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERINFOBOX),
  92. gpScreen, pos);
  93. //
  94. // Draw the player face
  95. //
  96. wMaxLevel = 0;
  97. bPoisonColor = 0xFF;
  98. for (iPartyIndex = 0; iPartyIndex <= gpGlobals->wMaxPartyMemberIndex; iPartyIndex++)
  99. {
  100. if (gpGlobals->rgParty[iPartyIndex].wPlayerRole == wPlayerRole)
  101. {
  102. break;
  103. }
  104. }
  105. if (iPartyIndex <= gpGlobals->wMaxPartyMemberIndex)
  106. {
  107. for (i = 0; i < MAX_POISONS; i++)
  108. {
  109. w = gpGlobals->rgPoisonStatus[i][iPartyIndex].wPoisonID;
  110. if (w != 0 &&
  111. gpGlobals->g.rgObject[w].poison.wPoisonLevel <= 3)
  112. {
  113. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel >= wMaxLevel)
  114. {
  115. wMaxLevel = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  116. bPoisonColor = (BYTE)(gpGlobals->g.rgObject[w].poison.wColor);
  117. }
  118. }
  119. }
  120. }
  121. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  122. {
  123. //
  124. // Always use the black/white color for dead players
  125. // and do not use the time meter
  126. //
  127. bPoisonColor = 0;
  128. iTimeMeter = 0;
  129. }
  130. if (bPoisonColor == 0xFF)
  131. {
  132. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERFACE_FIRST + wPlayerRole),
  133. gpScreen, PAL_XY(PAL_X(pos) - 2, PAL_Y(pos) - 4));
  134. }
  135. else
  136. {
  137. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERFACE_FIRST + wPlayerRole),
  138. gpScreen, PAL_XY(PAL_X(pos) - 2, PAL_Y(pos) - 4), bPoisonColor, 0);
  139. }
  140. #ifndef PAL_CLASSIC
  141. //
  142. // Draw a border for the Time Meter
  143. //
  144. rect.x = PAL_X(pos) + 31;
  145. rect.y = PAL_Y(pos) + 4;
  146. rect.w = 1;
  147. rect.h = 6;
  148. SDL_FillRect(gpScreen, &rect, 0xBD);
  149. rect.x += 39;
  150. SDL_FillRect(gpScreen, &rect, 0xBD);
  151. rect.x = PAL_X(pos) + 32;
  152. rect.y = PAL_Y(pos) + 3;
  153. rect.w = 38;
  154. rect.h = 1;
  155. SDL_FillRect(gpScreen, &rect, 0xBD);
  156. rect.y += 7;
  157. SDL_FillRect(gpScreen, &rect, 0xBD);
  158. //
  159. // Draw the Time meter bar
  160. //
  161. if (iTimeMeter >= 100)
  162. {
  163. rect.x = PAL_X(pos) + 33;
  164. rect.y = PAL_Y(pos) + 6;
  165. rect.w = 36;
  166. rect.h = 2;
  167. SDL_FillRect(gpScreen, &rect, 0x2C);
  168. }
  169. else if (iTimeMeter > 0)
  170. {
  171. rect.x = PAL_X(pos) + 33;
  172. rect.y = PAL_Y(pos) + 5;
  173. rect.w = iTimeMeter * 36 / 100;
  174. rect.h = 4;
  175. SDL_FillRect(gpScreen, &rect, bTimeMeterColor);
  176. }
  177. #endif
  178. //
  179. // Draw the HP and MP value
  180. //
  181. #ifdef PAL_CLASSIC
  182. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  183. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 6));
  184. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole], 4,
  185. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 8), kNumColorYellow, kNumAlignRight);
  186. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole], 4,
  187. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 5), kNumColorYellow, kNumAlignRight);
  188. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  189. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 22));
  190. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole], 4,
  191. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 24), kNumColorCyan, kNumAlignRight);
  192. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole], 4,
  193. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 21), kNumColorCyan, kNumAlignRight);
  194. #else
  195. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  196. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 14));
  197. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole], 4,
  198. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 16), kNumColorYellow, kNumAlignRight);
  199. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole], 4,
  200. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 13), kNumColorYellow, kNumAlignRight);
  201. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  202. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 24));
  203. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole], 4,
  204. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 26), kNumColorCyan, kNumAlignRight);
  205. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole], 4,
  206. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 23), kNumColorCyan, kNumAlignRight);
  207. #endif
  208. //
  209. // Draw Statuses
  210. //
  211. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  212. {
  213. for (i = 0; i < kStatusAll; i++)
  214. {
  215. if (gpGlobals->rgPlayerStatus[wPlayerRole][i] > 0 &&
  216. rgwStatusWord[i] != 0)
  217. {
  218. PAL_DrawText(PAL_GetWord(rgwStatusWord[i]),
  219. PAL_XY(PAL_X(pos) + rgStatusPos[i][0], PAL_Y(pos) + rgStatusPos[i][1]),
  220. rgbStatusColor[i], TRUE, FALSE, FALSE);
  221. }
  222. }
  223. }
  224. //
  225. // Update the screen area if needed
  226. //
  227. if (fUpdate)
  228. {
  229. rect.x = PAL_X(pos) - 2;
  230. rect.y = PAL_Y(pos) - 4;
  231. rect.w = 77;
  232. rect.h = 39;
  233. VIDEO_UpdateScreen(&rect);
  234. }
  235. }
  236. static BOOL
  237. PAL_BattleUIIsActionValid(
  238. BATTLEUIACTION ActionType
  239. )
  240. /*++
  241. Purpose:
  242. Check if the specified action is valid.
  243. Parameters:
  244. [IN] ActionType - the type of the action.
  245. Return value:
  246. TRUE if the action is valid, FALSE if not.
  247. --*/
  248. {
  249. WORD wPlayerRole, w;
  250. int i;
  251. wPlayerRole = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  252. switch (ActionType)
  253. {
  254. case kBattleUIActionAttack:
  255. case kBattleUIActionMisc:
  256. break;
  257. case kBattleUIActionMagic:
  258. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] != 0)
  259. {
  260. return FALSE;
  261. }
  262. break;
  263. case kBattleUIActionCoopMagic:
  264. if (gpGlobals->wMaxPartyMemberIndex == 0)
  265. {
  266. return FALSE;
  267. }
  268. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  269. {
  270. w = gpGlobals->rgParty[i].wPlayerRole;
  271. #ifndef PAL_CLASSIC
  272. if (gpGlobals->g.PlayerRoles.rgwHP[w] < gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5 ||
  273. gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  274. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0 ||
  275. gpGlobals->rgPlayerStatus[w][kStatusSilence] != 0 ||
  276. g_Battle.rgPlayer[i].flTimeMeter < 100 ||
  277. g_Battle.rgPlayer[i].state == kFighterAct)
  278. #else
  279. if (gpGlobals->g.PlayerRoles.rgwHP[w] < gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5 ||
  280. gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  281. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0 ||
  282. gpGlobals->rgPlayerStatus[w][kStatusSilence] != 0)
  283. #endif
  284. {
  285. return FALSE;
  286. }
  287. }
  288. break;
  289. }
  290. return TRUE;
  291. }
  292. static VOID
  293. PAL_BattleUIDrawMiscMenu(
  294. WORD wCurrentItem,
  295. BOOL fConfirmed
  296. )
  297. /*++
  298. Purpose:
  299. Draw the misc menu.
  300. Parameters:
  301. [IN] wCurrentItem - the current selected menu item.
  302. [IN] fConfirmed - TRUE if confirmed, FALSE if not.
  303. Return value:
  304. None.
  305. --*/
  306. {
  307. int i;
  308. BYTE bColor;
  309. #ifdef PAL_CLASSIC
  310. MENUITEM rgMenuItem[] = {
  311. // value label enabled position
  312. { 0, BATTLEUI_LABEL_AUTO, TRUE, PAL_XY(16, 32) },
  313. { 1, BATTLEUI_LABEL_INVENTORY, TRUE, PAL_XY(16, 50) },
  314. { 2, BATTLEUI_LABEL_DEFEND, TRUE, PAL_XY(16, 68) },
  315. { 3, BATTLEUI_LABEL_FLEE, TRUE, PAL_XY(16, 86) },
  316. { 4, BATTLEUI_LABEL_STATUS, TRUE, PAL_XY(16, 104) }
  317. };
  318. #else
  319. MENUITEM rgMenuItem[] = {
  320. // value label enabled position
  321. { 0, BATTLEUI_LABEL_ITEM, TRUE, PAL_XY(16, 32) },
  322. { 1, BATTLEUI_LABEL_DEFEND, TRUE, PAL_XY(16, 50) },
  323. { 2, BATTLEUI_LABEL_AUTO, TRUE, PAL_XY(16, 68) },
  324. { 3, BATTLEUI_LABEL_FLEE, TRUE, PAL_XY(16, 86) },
  325. { 4, BATTLEUI_LABEL_STATUS, TRUE, PAL_XY(16, 104) }
  326. };
  327. #endif
  328. //
  329. // Draw the box
  330. //
  331. PAL_CreateBox(PAL_XY(2, 20), 4, 1, 0, FALSE);
  332. //
  333. // Draw the menu items
  334. //
  335. for (i = 0; i < 5; i++)
  336. {
  337. bColor = MENUITEM_COLOR;
  338. if (i == wCurrentItem)
  339. {
  340. if (fConfirmed)
  341. {
  342. bColor = MENUITEM_COLOR_CONFIRMED;
  343. }
  344. else
  345. {
  346. bColor = MENUITEM_COLOR_SELECTED;
  347. }
  348. }
  349. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor, TRUE, FALSE, FALSE);
  350. }
  351. }
  352. static WORD
  353. PAL_BattleUIMiscMenuUpdate(
  354. VOID
  355. )
  356. /*++
  357. Purpose:
  358. Update the misc menu.
  359. Parameters:
  360. None.
  361. Return value:
  362. The selected item number. 0 if cancelled, 0xFFFF if not confirmed.
  363. --*/
  364. {
  365. //
  366. // Draw the menu
  367. //
  368. PAL_BattleUIDrawMiscMenu(g_iCurMiscMenuItem, FALSE);
  369. //
  370. // Process inputs
  371. //
  372. if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  373. {
  374. g_iCurMiscMenuItem--;
  375. if (g_iCurMiscMenuItem < 0)
  376. {
  377. g_iCurMiscMenuItem = 4;
  378. }
  379. }
  380. else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  381. {
  382. g_iCurMiscMenuItem++;
  383. if (g_iCurMiscMenuItem > 4)
  384. {
  385. g_iCurMiscMenuItem = 0;
  386. }
  387. }
  388. else if (g_InputState.dwKeyPress & kKeySearch)
  389. {
  390. return g_iCurMiscMenuItem + 1;
  391. }
  392. else if (g_InputState.dwKeyPress & kKeyMenu)
  393. {
  394. return 0;
  395. }
  396. return 0xFFFF;
  397. }
  398. static WORD
  399. PAL_BattleUIMiscItemSubMenuUpdate(
  400. VOID
  401. )
  402. /*++
  403. Purpose:
  404. Update the item sub menu of the misc menu.
  405. Parameters:
  406. None.
  407. Return value:
  408. The selected item number. 0 if cancelled, 0xFFFF if not confirmed.
  409. --*/
  410. {
  411. int i;
  412. BYTE bColor;
  413. MENUITEM rgMenuItem[] = {
  414. // value label enabled position
  415. { 0, BATTLEUI_LABEL_USEITEM, TRUE, PAL_XY(44, 62) },
  416. { 1, BATTLEUI_LABEL_THROWITEM, TRUE, PAL_XY(44, 80) },
  417. };
  418. //
  419. // Draw the menu
  420. //
  421. #ifdef PAL_CLASSIC
  422. PAL_BattleUIDrawMiscMenu(1, TRUE);
  423. #else
  424. PAL_BattleUIDrawMiscMenu(0, TRUE);
  425. #endif
  426. PAL_CreateBox(PAL_XY(30, 50), 1, PAL_MenuTextMaxWidth(rgMenuItem, 2) - 1, 0, FALSE);
  427. //
  428. // Draw the menu items
  429. //
  430. for (i = 0; i < 2; i++)
  431. {
  432. bColor = MENUITEM_COLOR;
  433. if (i == g_iCurSubMenuItem)
  434. {
  435. bColor = MENUITEM_COLOR_SELECTED;
  436. }
  437. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor, TRUE, FALSE, FALSE);
  438. }
  439. //
  440. // Process inputs
  441. //
  442. if (g_InputState.dwKeyPress & (kKeyUp | kKeyLeft))
  443. {
  444. g_iCurSubMenuItem = 0;
  445. }
  446. else if (g_InputState.dwKeyPress & (kKeyDown | kKeyRight))
  447. {
  448. g_iCurSubMenuItem = 1;
  449. }
  450. else if (g_InputState.dwKeyPress & kKeySearch)
  451. {
  452. return g_iCurSubMenuItem + 1;
  453. }
  454. else if (g_InputState.dwKeyPress & kKeyMenu)
  455. {
  456. return 0;
  457. }
  458. return 0xFFFF;
  459. }
  460. VOID
  461. PAL_BattleUIShowText(
  462. LPCWSTR lpszText,
  463. WORD wDuration
  464. )
  465. /*++
  466. Purpose:
  467. Show a text message in the battle.
  468. Parameters:
  469. [IN] lpszText - the text message to be shown.
  470. [IN] wDuration - the duration of the message, in milliseconds.
  471. Return value:
  472. None.
  473. --*/
  474. {
  475. if (!SDL_TICKS_PASSED(SDL_GetTicks(), g_Battle.UI.dwMsgShowTime))
  476. {
  477. wcscpy(g_Battle.UI.szNextMsg, lpszText);
  478. g_Battle.UI.wNextMsgDuration = wDuration;
  479. }
  480. else
  481. {
  482. wcscpy(g_Battle.UI.szMsg, lpszText);
  483. g_Battle.UI.dwMsgShowTime = SDL_GetTicks() + wDuration;
  484. }
  485. }
  486. VOID
  487. PAL_BattleUIPlayerReady(
  488. WORD wPlayerIndex
  489. )
  490. /*++
  491. Purpose:
  492. Start the action selection menu of the specified player.
  493. Parameters:
  494. [IN] wPlayerIndex - the player index.
  495. Return value:
  496. None.
  497. --*/
  498. {
  499. #ifndef PAL_CLASSIC
  500. WORD w = gpGlobals->rgParty[wPlayerIndex].wPlayerRole;
  501. #endif
  502. g_Battle.UI.wCurPlayerIndex = wPlayerIndex;
  503. g_Battle.UI.state = kBattleUISelectMove;
  504. g_Battle.UI.wSelectedAction = 0;
  505. g_Battle.UI.MenuState = kBattleMenuMain;
  506. #ifndef PAL_CLASSIC
  507. //
  508. // Play a sound which indicates the player is ready
  509. //
  510. if (gpGlobals->rgPlayerStatus[w][kStatusPuppet] == 0 &&
  511. gpGlobals->rgPlayerStatus[w][kStatusSleep] == 0 &&
  512. gpGlobals->rgPlayerStatus[w][kStatusConfused] == 0 &&
  513. !g_Battle.UI.fAutoAttack && !gpGlobals->fAutoBattle)
  514. {
  515. AUDIO_PlaySound(78);
  516. }
  517. #endif
  518. }
  519. static VOID
  520. PAL_BattleUIUseItem(
  521. VOID
  522. )
  523. /*++
  524. Purpose:
  525. Use an item in the battle UI.
  526. Parameters:
  527. None.
  528. Return value:
  529. None.
  530. --*/
  531. {
  532. WORD wSelectedItem;
  533. wSelectedItem = PAL_ItemSelectMenuUpdate();
  534. if (wSelectedItem != 0xFFFF)
  535. {
  536. if (wSelectedItem != 0)
  537. {
  538. g_Battle.UI.wActionType = kBattleActionUseItem;
  539. g_Battle.UI.wObjectID = wSelectedItem;
  540. if (gpGlobals->g.rgObject[wSelectedItem].item.wFlags & kItemFlagApplyToAll)
  541. {
  542. g_Battle.UI.state = kBattleUISelectTargetPlayerAll;
  543. }
  544. else
  545. {
  546. #ifdef PAL_CLASSIC
  547. g_Battle.UI.wSelectedIndex = 0;
  548. #else
  549. g_Battle.UI.wSelectedIndex = g_Battle.UI.wCurPlayerIndex;
  550. #endif
  551. g_Battle.UI.state = kBattleUISelectTargetPlayer;
  552. }
  553. }
  554. else
  555. {
  556. g_Battle.UI.MenuState = kBattleMenuMain;
  557. }
  558. }
  559. }
  560. static VOID
  561. PAL_BattleUIThrowItem(
  562. VOID
  563. )
  564. /*++
  565. Purpose:
  566. Throw an item in the battle UI.
  567. Parameters:
  568. None.
  569. Return value:
  570. None.
  571. --*/
  572. {
  573. WORD wSelectedItem = PAL_ItemSelectMenuUpdate();
  574. if (wSelectedItem != 0xFFFF)
  575. {
  576. if (wSelectedItem != 0)
  577. {
  578. g_Battle.UI.wActionType = kBattleActionThrowItem;
  579. g_Battle.UI.wObjectID = wSelectedItem;
  580. if (gpGlobals->g.rgObject[wSelectedItem].item.wFlags & kItemFlagApplyToAll)
  581. {
  582. g_Battle.UI.state = kBattleUISelectTargetEnemyAll;
  583. }
  584. else
  585. {
  586. g_Battle.UI.wSelectedIndex = g_Battle.UI.wPrevEnemyTarget;
  587. g_Battle.UI.state = kBattleUISelectTargetEnemy;
  588. }
  589. }
  590. else
  591. {
  592. g_Battle.UI.MenuState = kBattleMenuMain;
  593. }
  594. }
  595. }
  596. static WORD
  597. PAL_BattleUIPickAutoMagic(
  598. WORD wPlayerRole,
  599. WORD wRandomRange
  600. )
  601. /*++
  602. Purpose:
  603. Pick a magic for the specified player for automatic usage.
  604. Parameters:
  605. [IN] wPlayerRole - the player role ID.
  606. [IN] wRandomRange - the range of the magic power.
  607. Return value:
  608. The object ID of the selected magic. 0 for physical attack.
  609. --*/
  610. {
  611. WORD wMagic = 0, w, wMagicNum;
  612. int i, iMaxPower = 0, iPower;
  613. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] != 0)
  614. {
  615. return 0;
  616. }
  617. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  618. {
  619. w = gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole];
  620. if (w == 0)
  621. {
  622. continue;
  623. }
  624. wMagicNum = gpGlobals->g.rgObject[w].magic.wMagicNumber;
  625. //
  626. // skip if the magic is an ultimate move or not enough MP
  627. //
  628. if (gpGlobals->g.lprgMagic[wMagicNum].wCostMP == 1 ||
  629. gpGlobals->g.lprgMagic[wMagicNum].wCostMP > gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole] ||
  630. (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) <= 0)
  631. {
  632. continue;
  633. }
  634. iPower = (SHORT)(gpGlobals->g.lprgMagic[wMagicNum].wBaseDamage) +
  635. RandomLong(0, wRandomRange);
  636. if (iPower > iMaxPower)
  637. {
  638. iMaxPower = iPower;
  639. wMagic = w;
  640. }
  641. }
  642. return wMagic;
  643. }
  644. VOID
  645. PAL_BattleUIUpdate(
  646. VOID
  647. )
  648. /*++
  649. Purpose:
  650. Update the status of battle UI.
  651. Parameters:
  652. None.
  653. Return value:
  654. None.
  655. --*/
  656. {
  657. int i, j, x, y;
  658. WORD wPlayerRole, w;
  659. static int s_iFrame = 0;
  660. s_iFrame++;
  661. if (g_Battle.UI.fAutoAttack && !gpGlobals->fAutoBattle)
  662. {
  663. //
  664. // Draw the "auto attack" message if in the autoattack mode.
  665. //
  666. if (g_InputState.dwKeyPress & kKeyMenu)
  667. {
  668. g_Battle.UI.fAutoAttack = FALSE;
  669. }
  670. else
  671. {
  672. PAL_DrawText(PAL_GetWord(BATTLEUI_LABEL_AUTO), PAL_XY(280, 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(kBattleActionCoopMagic))
  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. }