magicmenu.c 10 KB

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