magicmenu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. static struct MAGICITEM
  23. {
  24. WORD wMagic;
  25. WORD wMP;
  26. BOOL fEnabled;
  27. } rgMagicItem[MAX_PLAYER_MAGICS];
  28. static int g_iNumMagic = 0;
  29. static int g_iCurrentItem = 0;
  30. static WORD g_wPlayerMP = 0;
  31. WORD
  32. PAL_MagicSelectionMenuUpdate(
  33. VOID
  34. )
  35. /*++
  36. Purpose:
  37. Update the magic selection menu.
  38. Parameters:
  39. None.
  40. Return value:
  41. The selected magic. 0 if cancelled, 0xFFFF if not confirmed.
  42. --*/
  43. {
  44. int i, j, k;
  45. BYTE bColor;
  46. //
  47. // Check for inputs
  48. //
  49. if (g_InputState.dwKeyPress & kKeyUp)
  50. {
  51. g_iCurrentItem -= 3;
  52. }
  53. else if (g_InputState.dwKeyPress & kKeyDown)
  54. {
  55. g_iCurrentItem += 3;
  56. }
  57. else if (g_InputState.dwKeyPress & kKeyLeft)
  58. {
  59. g_iCurrentItem--;
  60. }
  61. else if (g_InputState.dwKeyPress & kKeyRight)
  62. {
  63. g_iCurrentItem++;
  64. }
  65. else if (g_InputState.dwKeyPress & kKeyPgUp)
  66. {
  67. g_iCurrentItem -= 3 * 5;
  68. }
  69. else if (g_InputState.dwKeyPress & kKeyPgDn)
  70. {
  71. g_iCurrentItem += 3 * 5;
  72. }
  73. else if (g_InputState.dwKeyPress & kKeyMenu)
  74. {
  75. return 0;
  76. }
  77. //
  78. // Make sure the current menu item index is in bound
  79. //
  80. if (g_iCurrentItem < 0)
  81. {
  82. g_iCurrentItem = 0;
  83. }
  84. else if (g_iCurrentItem >= g_iNumMagic)
  85. {
  86. g_iCurrentItem = g_iNumMagic - 1;
  87. }
  88. //
  89. // Create the box.
  90. //
  91. PAL_CreateBox(PAL_XY(10, 42), 4, 16, 1, FALSE);
  92. if (gpGlobals->lpObjectDesc == NULL)
  93. {
  94. //
  95. // Draw the cash amount.
  96. //
  97. PAL_CreateSingleLineBox(PAL_XY(0, 0), 5, FALSE);
  98. PAL_DrawText(PAL_GetWord(CASH_LABEL), PAL_XY(10, 10), 0, FALSE, FALSE);
  99. PAL_DrawNumber(gpGlobals->dwCash, 6, PAL_XY(49, 14), kNumColorYellow, kNumAlignRight);
  100. //
  101. // Draw the MP of the selected magic.
  102. //
  103. PAL_CreateSingleLineBox(PAL_XY(215, 0), 5, FALSE);
  104. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH),
  105. gpScreen, PAL_XY(260, 14));
  106. PAL_DrawNumber(rgMagicItem[g_iCurrentItem].wMP, 4, PAL_XY(230, 14),
  107. kNumColorYellow, kNumAlignRight);
  108. PAL_DrawNumber(g_wPlayerMP, 4, PAL_XY(265, 14), kNumColorCyan, kNumAlignRight);
  109. }
  110. else
  111. {
  112. char szDesc[512], *next;
  113. const char *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, rgMagicItem[g_iCurrentItem].wMagic);
  114. //
  115. // Draw the magic description.
  116. //
  117. if (d != NULL)
  118. {
  119. k = 3;
  120. strcpy(szDesc, d);
  121. d = szDesc;
  122. while (TRUE)
  123. {
  124. next = strchr(d, '*');
  125. if (next != NULL)
  126. {
  127. *next = '\0';
  128. next++;
  129. }
  130. PAL_DrawText(d, PAL_XY(100, k), DESCTEXT_COLOR, TRUE, FALSE);
  131. k += 16;
  132. if (next == NULL)
  133. {
  134. break;
  135. }
  136. d = next;
  137. }
  138. }
  139. //
  140. // Draw the MP of the selected magic.
  141. //
  142. PAL_CreateSingleLineBox(PAL_XY(0, 0), 5, FALSE);
  143. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH),
  144. gpScreen, PAL_XY(45, 14));
  145. PAL_DrawNumber(rgMagicItem[g_iCurrentItem].wMP, 4, PAL_XY(15, 14),
  146. kNumColorYellow, kNumAlignRight);
  147. PAL_DrawNumber(g_wPlayerMP, 4, PAL_XY(50, 14), kNumColorCyan, kNumAlignRight);
  148. }
  149. //
  150. // Draw the texts of the current page
  151. //
  152. i = g_iCurrentItem / 3 * 3 - 3 * 2;
  153. if (i < 0)
  154. {
  155. i = 0;
  156. }
  157. for (j = 0; j < 5; j++)
  158. {
  159. for (k = 0; k < 3; k++)
  160. {
  161. bColor = MENUITEM_COLOR;
  162. if (i >= g_iNumMagic)
  163. {
  164. //
  165. // End of the list reached
  166. //
  167. j = 5;
  168. break;
  169. }
  170. if (i == g_iCurrentItem)
  171. {
  172. if (rgMagicItem[i].fEnabled)
  173. {
  174. bColor = MENUITEM_COLOR_SELECTED;
  175. }
  176. else
  177. {
  178. bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
  179. }
  180. }
  181. else if (!rgMagicItem[i].fEnabled)
  182. {
  183. bColor = MENUITEM_COLOR_INACTIVE;
  184. }
  185. //
  186. // Draw the text
  187. //
  188. PAL_DrawText(PAL_GetWord(rgMagicItem[i].wMagic),
  189. PAL_XY(35 + k * 87, 54 + j * 18), bColor, TRUE, FALSE);
  190. //
  191. // Draw the cursor on the current selected item
  192. //
  193. if (i == g_iCurrentItem)
  194. {
  195. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_CURSOR),
  196. gpScreen, PAL_XY(60 + k * 87, 64 + j * 18));
  197. }
  198. i++;
  199. }
  200. }
  201. if (g_InputState.dwKeyPress & kKeySearch)
  202. {
  203. if (rgMagicItem[g_iCurrentItem].fEnabled)
  204. {
  205. j = g_iCurrentItem % 3;
  206. k = (g_iCurrentItem < 3 * 2) ? (g_iCurrentItem / 3) : 2;
  207. j = 35 + j * 87;
  208. k = 54 + k * 18;
  209. PAL_DrawText(PAL_GetWord(rgMagicItem[g_iCurrentItem].wMagic), PAL_XY(j, k),
  210. MENUITEM_COLOR_CONFIRMED, FALSE, TRUE);
  211. return rgMagicItem[g_iCurrentItem].wMagic;
  212. }
  213. }
  214. return 0xFFFF;
  215. }
  216. VOID
  217. PAL_MagicSelectionMenuInit(
  218. WORD wPlayerRole,
  219. BOOL fInBattle,
  220. WORD wDefaultMagic
  221. )
  222. /*++
  223. Purpose:
  224. Initialize the magic selection menu.
  225. Parameters:
  226. [IN] wPlayerRole - the player ID.
  227. [IN] fInBattle - TRUE if in battle, FALSE if not.
  228. [IN] wDefaultMagic - the default magic item.
  229. Return value:
  230. None.
  231. --*/
  232. {
  233. WORD w;
  234. int i, j;
  235. g_iCurrentItem = 0;
  236. g_iNumMagic = 0;
  237. g_wPlayerMP = gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole];
  238. //
  239. // Put all magics of this player to the array
  240. //
  241. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  242. {
  243. w = gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole];
  244. if (w != 0)
  245. {
  246. rgMagicItem[g_iNumMagic].wMagic = w;
  247. w = gpGlobals->g.rgObject[w].magic.wMagicNumber;
  248. rgMagicItem[g_iNumMagic].wMP = gpGlobals->g.lprgMagic[w].wCostMP;
  249. rgMagicItem[g_iNumMagic].fEnabled = TRUE;
  250. if (rgMagicItem[g_iNumMagic].wMP > g_wPlayerMP)
  251. {
  252. rgMagicItem[g_iNumMagic].fEnabled = FALSE;
  253. }
  254. w = gpGlobals->g.rgObject[rgMagicItem[g_iNumMagic].wMagic].magic.wFlags;
  255. if (fInBattle)
  256. {
  257. if (!(w & kMagicFlagUsableInBattle))
  258. {
  259. rgMagicItem[g_iNumMagic].fEnabled = FALSE;
  260. }
  261. }
  262. else
  263. {
  264. if (!(w & kMagicFlagUsableOutsideBattle))
  265. {
  266. rgMagicItem[g_iNumMagic].fEnabled = FALSE;
  267. }
  268. }
  269. g_iNumMagic++;
  270. }
  271. }
  272. //
  273. // Sort the array
  274. //
  275. for (i = 0; i < g_iNumMagic - 1; i++)
  276. {
  277. BOOL fCompleted = TRUE;
  278. for (j = 0; j < g_iNumMagic - 1 - i; j++)
  279. {
  280. if (rgMagicItem[j].wMagic > rgMagicItem[j + 1].wMagic)
  281. {
  282. struct MAGICITEM t = rgMagicItem[j];
  283. rgMagicItem[j] = rgMagicItem[j + 1];
  284. rgMagicItem[j + 1] = t;
  285. fCompleted = FALSE;
  286. }
  287. }
  288. if (fCompleted)
  289. {
  290. break;
  291. }
  292. }
  293. //
  294. // Place the cursor to the default item
  295. //
  296. for (i = 0; i < g_iNumMagic; i++)
  297. {
  298. if (rgMagicItem[i].wMagic == wDefaultMagic)
  299. {
  300. g_iCurrentItem = i;
  301. break;
  302. }
  303. }
  304. }
  305. WORD
  306. PAL_MagicSelectionMenu(
  307. WORD wPlayerRole,
  308. BOOL fInBattle,
  309. WORD wDefaultMagic
  310. )
  311. /*++
  312. Purpose:
  313. Show the magic selection menu.
  314. Parameters:
  315. [IN] wPlayerRole - the player ID.
  316. [IN] fInBattle - TRUE if in battle, FALSE if not.
  317. [IN] wDefaultMagic - the default magic item.
  318. Return value:
  319. The selected magic. 0 if cancelled.
  320. --*/
  321. {
  322. WORD w;
  323. int i;
  324. DWORD dwTime;
  325. PAL_MagicSelectionMenuInit(wPlayerRole, fInBattle, wDefaultMagic);
  326. PAL_ClearKeyState();
  327. dwTime = SDL_GetTicks();
  328. while (TRUE)
  329. {
  330. PAL_MakeScene();
  331. w = 45;
  332. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  333. {
  334. PAL_PlayerInfoBox(PAL_XY(w, 165), gpGlobals->rgParty[i].wPlayerRole, 100,
  335. TIMEMETER_COLOR_DEFAULT, FALSE);
  336. w += 78;
  337. }
  338. w = PAL_MagicSelectionMenuUpdate();
  339. VIDEO_UpdateScreen(NULL);
  340. PAL_ClearKeyState();
  341. if (w != 0xFFFF)
  342. {
  343. return w;
  344. }
  345. PAL_ProcessEvent();
  346. while (SDL_GetTicks() < dwTime)
  347. {
  348. PAL_ProcessEvent();
  349. if (g_InputState.dwKeyPress != 0)
  350. {
  351. break;
  352. }
  353. SDL_Delay(5);
  354. }
  355. dwTime = SDL_GetTicks() + FRAME_TIME;
  356. }
  357. return 0; // should not really reach here
  358. }