uibattle.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  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. #include "main.h"
  22. extern WORD g_rgPlayerPos[3][3][2];
  23. static int g_iCurMiscMenuItem = 0;
  24. static int g_iCurSubMenuItem = 0;
  25. VOID
  26. PAL_PlayerInfoBox(
  27. PAL_POS pos,
  28. WORD wPlayerRole,
  29. INT iTimeMeter,
  30. BYTE bTimeMeterColor,
  31. BOOL fUpdate
  32. )
  33. /*++
  34. Purpose:
  35. Show the player info box.
  36. Parameters:
  37. [IN] pos - the top-left corner position of the box.
  38. [IN] wPlayerRole - the player role ID to be shown.
  39. [IN] iTimeMeter - the value of time meter. 0 = empty, 100 = full.
  40. [IN] bTimeMeterColor - the color of time meter.
  41. [IN] fUpdate - whether to update the screen area or not.
  42. Return value:
  43. None.
  44. --*/
  45. {
  46. SDL_Rect rect;
  47. BYTE bPoisonColor;
  48. int i, iPartyIndex;
  49. WORD wMaxLevel, w;
  50. const BYTE rgStatusPos[kStatusAll][2] =
  51. {
  52. {35, 19}, // confused
  53. {0, 0}, // slow
  54. {54, 1}, // sleep
  55. {55, 20}, // silence
  56. {0, 0}, // puppet
  57. {0, 0}, // bravery
  58. {0, 0}, // protect
  59. {0, 0}, // haste
  60. {0, 0}, // dualattack
  61. };
  62. const WORD rgwStatusWord[kStatusAll] =
  63. {
  64. 0x1D, // confused
  65. 0x00, // slow
  66. 0x1C, // sleep
  67. 0x1A, // silence
  68. 0x00, // puppet
  69. 0x00, // bravery
  70. 0x00, // protect
  71. 0x00, // haste
  72. 0x00, // dualattack
  73. };
  74. const BYTE rgbStatusColor[kStatusAll] =
  75. {
  76. 0x5F, // confused
  77. 0x00, // slow
  78. 0x0E, // sleep
  79. 0x3C, // silence
  80. 0x00, // puppet
  81. 0x00, // bravery
  82. 0x00, // protect
  83. 0x00, // haste
  84. 0x00, // dualattack
  85. };
  86. //
  87. // Draw the box
  88. //
  89. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERINFOBOX),
  90. gpScreen, pos);
  91. //
  92. // Draw the player face
  93. //
  94. wMaxLevel = 0;
  95. bPoisonColor = 0xFF;
  96. for (iPartyIndex = 0; iPartyIndex <= gpGlobals->wMaxPartyMemberIndex; iPartyIndex++)
  97. {
  98. if (gpGlobals->rgParty[iPartyIndex].wPlayerRole == wPlayerRole)
  99. {
  100. break;
  101. }
  102. }
  103. if (iPartyIndex <= gpGlobals->wMaxPartyMemberIndex)
  104. {
  105. for (i = 0; i < MAX_POISONS; i++)
  106. {
  107. w = gpGlobals->rgPoisonStatus[i][iPartyIndex].wPoisonID;
  108. if (w != 0 &&
  109. gpGlobals->g.rgObject[w].poison.wPoisonLevel <= 3)
  110. {
  111. if (gpGlobals->g.rgObject[w].poison.wPoisonLevel >= wMaxLevel)
  112. {
  113. wMaxLevel = gpGlobals->g.rgObject[w].poison.wPoisonLevel;
  114. bPoisonColor = (BYTE)(gpGlobals->g.rgObject[w].poison.wColor);
  115. }
  116. }
  117. }
  118. }
  119. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] == 0)
  120. {
  121. //
  122. // Always use the black/white color for dead players
  123. // and do not use the time meter
  124. //
  125. bPoisonColor = 0;
  126. iTimeMeter = 0;
  127. }
  128. if (bPoisonColor == 0xFF)
  129. {
  130. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERFACE_FIRST + wPlayerRole),
  131. gpScreen, PAL_XY(PAL_X(pos) - 2, PAL_Y(pos) - 4));
  132. }
  133. else
  134. {
  135. PAL_RLEBlitMonoColor(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_PLAYERFACE_FIRST + wPlayerRole),
  136. gpScreen, PAL_XY(PAL_X(pos) - 2, PAL_Y(pos) - 4), bPoisonColor, 0);
  137. }
  138. #ifndef PAL_CLASSIC
  139. //
  140. // Draw a border for the Time Meter
  141. //
  142. rect.x = PAL_X(pos) + 31;
  143. rect.y = PAL_Y(pos) + 4;
  144. rect.w = 1;
  145. rect.h = 6;
  146. SDL_FillRect(gpScreen, &rect, 0xBD);
  147. rect.x += 39;
  148. SDL_FillRect(gpScreen, &rect, 0xBD);
  149. rect.x = PAL_X(pos) + 32;
  150. rect.y = PAL_Y(pos) + 3;
  151. rect.w = 38;
  152. rect.h = 1;
  153. SDL_FillRect(gpScreen, &rect, 0xBD);
  154. rect.y += 7;
  155. SDL_FillRect(gpScreen, &rect, 0xBD);
  156. //
  157. // Draw the Time meter bar
  158. //
  159. if (iTimeMeter >= 100)
  160. {
  161. rect.x = PAL_X(pos) + 33;
  162. rect.y = PAL_Y(pos) + 6;
  163. rect.w = 36;
  164. rect.h = 2;
  165. SDL_FillRect(gpScreen, &rect, 0x2C);
  166. }
  167. else if (iTimeMeter > 0)
  168. {
  169. rect.x = PAL_X(pos) + 33;
  170. rect.y = PAL_Y(pos) + 5;
  171. rect.w = iTimeMeter * 36 / 100;
  172. rect.h = 4;
  173. SDL_FillRect(gpScreen, &rect, bTimeMeterColor);
  174. }
  175. #endif
  176. //
  177. // Draw the HP and MP value
  178. //
  179. #ifdef PAL_CLASSIC
  180. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  181. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 6));
  182. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole], 4,
  183. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 8), kNumColorYellow, kNumAlignRight);
  184. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole], 4,
  185. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 5), kNumColorYellow, kNumAlignRight);
  186. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  187. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 22));
  188. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole], 4,
  189. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 24), kNumColorCyan, kNumAlignRight);
  190. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole], 4,
  191. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 21), kNumColorCyan, kNumAlignRight);
  192. #else
  193. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  194. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 14));
  195. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[wPlayerRole], 4,
  196. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 16), kNumColorYellow, kNumAlignRight);
  197. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole], 4,
  198. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 13), kNumColorYellow, kNumAlignRight);
  199. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  200. PAL_XY(PAL_X(pos) + 49, PAL_Y(pos) + 24));
  201. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[wPlayerRole], 4,
  202. PAL_XY(PAL_X(pos) + 47, PAL_Y(pos) + 26), kNumColorCyan, kNumAlignRight);
  203. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole], 4,
  204. PAL_XY(PAL_X(pos) + 26, PAL_Y(pos) + 23), kNumColorCyan, kNumAlignRight);
  205. #endif
  206. //
  207. // Draw Statuses
  208. //
  209. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  210. {
  211. for (i = 0; i < kStatusAll; i++)
  212. {
  213. if (gpGlobals->rgPlayerStatus[wPlayerRole][i] > 0 &&
  214. rgwStatusWord[i] != 0)
  215. {
  216. PAL_DrawText(PAL_GetWord(rgwStatusWord[i]),
  217. PAL_XY(PAL_X(pos) + rgStatusPos[i][0], PAL_Y(pos) + rgStatusPos[i][1]),
  218. rgbStatusColor[i], TRUE, FALSE);
  219. }
  220. }
  221. }
  222. //
  223. // Update the screen area if needed
  224. //
  225. if (fUpdate)
  226. {
  227. rect.x = PAL_X(pos) - 2;
  228. rect.y = PAL_Y(pos) - 4;
  229. rect.w = 77;
  230. rect.h = 39;
  231. VIDEO_UpdateScreen(&rect);
  232. }
  233. }
  234. static BOOL
  235. PAL_BattleUIIsActionValid(
  236. BATTLEUIACTION ActionType
  237. )
  238. /*++
  239. Purpose:
  240. Check if the specified action is valid.
  241. Parameters:
  242. [IN] ActionType - the type of the action.
  243. Return value:
  244. TRUE if the action is valid, FALSE if not.
  245. --*/
  246. {
  247. WORD wPlayerRole, w;
  248. int i;
  249. wPlayerRole = gpGlobals->rgParty[g_Battle.UI.wCurPlayerIndex].wPlayerRole;
  250. switch (ActionType)
  251. {
  252. case kBattleUIActionAttack:
  253. case kBattleUIActionMisc:
  254. break;
  255. case kBattleUIActionMagic:
  256. if (gpGlobals->rgPlayerStatus[wPlayerRole][kStatusSilence] != 0)
  257. {
  258. return FALSE;
  259. }
  260. break;
  261. case kBattleUIActionCoopMagic:
  262. if (gpGlobals->wMaxPartyMemberIndex == 0)
  263. {
  264. return FALSE;
  265. }
  266. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  267. {
  268. w = gpGlobals->rgParty[i].wPlayerRole;
  269. #ifndef PAL_CLASSIC
  270. if (gpGlobals->g.PlayerRoles.rgwHP[w] < gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5 ||
  271. gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  272. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0 ||
  273. gpGlobals->rgPlayerStatus[w][kStatusSilence] != 0 ||
  274. g_Battle.rgPlayer[i].flTimeMeter < 100 ||
  275. g_Battle.rgPlayer[i].state == kFighterAct)
  276. #else
  277. if (gpGlobals->g.PlayerRoles.rgwHP[w] < gpGlobals->g.PlayerRoles.rgwMaxHP[w] / 5 ||
  278. gpGlobals->rgPlayerStatus[w][kStatusSleep] != 0 ||
  279. gpGlobals->rgPlayerStatus[w][kStatusConfused] != 0 ||
  280. gpGlobals->rgPlayerStatus[w][kStatusSilence] != 0)
  281. #endif
  282. {
  283. return FALSE;
  284. }
  285. }
  286. break;
  287. }
  288. return TRUE;
  289. }
  290. static VOID
  291. PAL_BattleUIDrawMiscMenu(
  292. WORD wCurrentItem,
  293. BOOL fConfirmed
  294. )
  295. /*++
  296. Purpose:
  297. Draw the misc menu.
  298. Parameters:
  299. [IN] wCurrentItem - the current selected menu item.
  300. [IN] fConfirmed - TRUE if confirmed, FALSE if not.
  301. Return value:
  302. None.
  303. --*/
  304. {
  305. int i;
  306. BYTE bColor;
  307. #ifdef PAL_CLASSIC
  308. MENUITEM rgMenuItem[] = {
  309. // value label enabled position
  310. { 0, BATTLEUI_LABEL_AUTO, TRUE, PAL_XY(16, 32) },
  311. { 1, BATTLEUI_LABEL_INVENTORY, TRUE, PAL_XY(16, 50) },
  312. { 2, BATTLEUI_LABEL_DEFEND, TRUE, PAL_XY(16, 68) },
  313. { 3, BATTLEUI_LABEL_FLEE, TRUE, PAL_XY(16, 86) },
  314. { 4, BATTLEUI_LABEL_STATUS, TRUE, PAL_XY(16, 104) }
  315. };
  316. #else
  317. MENUITEM rgMenuItem[] = {
  318. // value label enabled position
  319. { 0, BATTLEUI_LABEL_ITEM, TRUE, PAL_XY(16, 32) },
  320. { 1, BATTLEUI_LABEL_DEFEND, TRUE, PAL_XY(16, 50) },
  321. { 2, BATTLEUI_LABEL_AUTO, TRUE, PAL_XY(16, 68) },
  322. { 3, BATTLEUI_LABEL_FLEE, TRUE, PAL_XY(16, 86) },
  323. { 4, BATTLEUI_LABEL_STATUS, TRUE, PAL_XY(16, 104) }
  324. };
  325. #endif
  326. //
  327. // Draw the box
  328. //
  329. PAL_CreateBox(PAL_XY(2, 20), 4, 1, 0, FALSE);
  330. //
  331. // Draw the menu items
  332. //
  333. for (i = 0; i < 5; i++)
  334. {
  335. bColor = MENUITEM_COLOR;
  336. if (i == wCurrentItem)
  337. {
  338. if (fConfirmed)
  339. {
  340. bColor = MENUITEM_COLOR_CONFIRMED;
  341. }
  342. else
  343. {
  344. bColor = MENUITEM_COLOR_SELECTED;
  345. }
  346. }
  347. PAL_DrawText(PAL_GetWord(rgMenuItem[i].wNumWord), rgMenuItem[i].pos, bColor,
  348. TRUE, 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, 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,
  437. TRUE, 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. LPCSTR 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_GetTicks() < g_Battle.UI.dwMsgShowTime)
  476. {
  477. strcpy(g_Battle.UI.szNextMsg, lpszText);
  478. g_Battle.UI.wNextMsgDuration = wDuration;
  479. }
  480. else
  481. {
  482. strcpy(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. SOUND_PlayChannel(78, 1);
  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);
  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_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 len = strlen(g_Battle.UI.szMsg);
  1458. //
  1459. // Create the window box
  1460. //
  1461. pos = PAL_XY(160 - len * 4, 40);
  1462. PAL_CreateSingleLineBox(pos, (len + 1) / 2, FALSE);
  1463. //
  1464. // Show the text on the screen
  1465. //
  1466. pos = PAL_XY(PAL_X(pos) + 8 + ((len & 1) << 2), PAL_Y(pos) + 10);
  1467. PAL_DrawText(g_Battle.UI.szMsg, pos, 0, FALSE, FALSE);
  1468. }
  1469. else if (g_Battle.UI.szNextMsg[0] != '\0')
  1470. {
  1471. strcpy(g_Battle.UI.szMsg, g_Battle.UI.szNextMsg);
  1472. g_Battle.UI.dwMsgShowTime = SDL_GetTicks() + g_Battle.UI.wNextMsgDuration;
  1473. g_Battle.UI.szNextMsg[0] = '\0';
  1474. }
  1475. #endif
  1476. //
  1477. // Draw the numbers
  1478. //
  1479. for (i = 0; i < BATTLEUI_MAX_SHOWNUM; i++)
  1480. {
  1481. if (g_Battle.UI.rgShowNum[i].wNum > 0)
  1482. {
  1483. if ((SDL_GetTicks() - g_Battle.UI.rgShowNum[i].dwTime) / BATTLE_FRAME_TIME > 10)
  1484. {
  1485. g_Battle.UI.rgShowNum[i].wNum = 0;
  1486. }
  1487. else
  1488. {
  1489. PAL_DrawNumber(g_Battle.UI.rgShowNum[i].wNum, 5,
  1490. 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),
  1491. g_Battle.UI.rgShowNum[i].color, kNumAlignRight);
  1492. }
  1493. }
  1494. }
  1495. PAL_ClearKeyState();
  1496. }
  1497. VOID
  1498. PAL_BattleUIShowNum(
  1499. WORD wNum,
  1500. PAL_POS pos,
  1501. NUMCOLOR color
  1502. )
  1503. /*++
  1504. Purpose:
  1505. Show a number on battle screen (indicates HP/MP change).
  1506. Parameters:
  1507. [IN] wNum - number to be shown.
  1508. [IN] pos - position of the number on the screen.
  1509. [IN] color - color of the number.
  1510. Return value:
  1511. None.
  1512. --*/
  1513. {
  1514. int i;
  1515. for (i = 0; i < BATTLEUI_MAX_SHOWNUM; i++)
  1516. {
  1517. if (g_Battle.UI.rgShowNum[i].wNum == 0)
  1518. {
  1519. g_Battle.UI.rgShowNum[i].wNum = wNum;
  1520. g_Battle.UI.rgShowNum[i].pos = PAL_XY(PAL_X(pos) - 15, PAL_Y(pos));
  1521. g_Battle.UI.rgShowNum[i].color = color;
  1522. g_Battle.UI.rgShowNum[i].dwTime = SDL_GetTicks();
  1523. break;
  1524. }
  1525. }
  1526. }