magicmenu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. #include "main.h"
  23. static struct MAGICITEM
  24. {
  25. WORD wMagic;
  26. WORD wMP;
  27. BOOL fEnabled;
  28. } rgMagicItem[MAX_PLAYER_MAGICS];
  29. static int g_iNumMagic = 0;
  30. static int g_iCurrentItem = 0;
  31. static WORD g_wPlayerMP = 0;
  32. WORD
  33. PAL_MagicSelectionMenuUpdate(
  34. VOID
  35. )
  36. /*++
  37. Purpose:
  38. Update the magic selection menu.
  39. Parameters:
  40. None.
  41. Return value:
  42. The selected magic. 0 if cancelled, 0xFFFF if not confirmed.
  43. --*/
  44. {
  45. int i, j, k, line, item_delta;
  46. BYTE bColor;
  47. WORD wScript;
  48. const int iItemsPerLine = 32 / gConfig.dwWordLength;
  49. const int iItemTextWidth = 8 * gConfig.dwWordLength + 7;
  50. const int iLinesPerPage = 5 - gConfig.ScreenLayout.ExtraMagicDescLines;
  51. const int iBoxYOffset = gConfig.ScreenLayout.ExtraMagicDescLines * 16;
  52. const int iCursorXOffset = gConfig.dwWordLength * 5 / 2;
  53. const int iPageLineOffset = iLinesPerPage / 2;
  54. //
  55. // Check for inputs
  56. //
  57. if (g_InputState.dwKeyPress & kKeyUp)
  58. {
  59. item_delta = -iItemsPerLine;
  60. }
  61. else if (g_InputState.dwKeyPress & kKeyDown)
  62. {
  63. item_delta = iItemsPerLine;
  64. }
  65. else if (g_InputState.dwKeyPress & kKeyLeft)
  66. {
  67. item_delta = -1;
  68. }
  69. else if (g_InputState.dwKeyPress & kKeyRight)
  70. {
  71. item_delta = 1;
  72. }
  73. else if (g_InputState.dwKeyPress & kKeyPgUp)
  74. {
  75. item_delta = -(iItemsPerLine * iLinesPerPage);
  76. }
  77. else if (g_InputState.dwKeyPress & kKeyPgDn)
  78. {
  79. item_delta = iItemsPerLine * iLinesPerPage;
  80. }
  81. else if (g_InputState.dwKeyPress & kKeyHome)
  82. {
  83. item_delta = -g_iCurrentItem;
  84. }
  85. else if (g_InputState.dwKeyPress & kKeyEnd)
  86. {
  87. item_delta = g_iNumMagic - g_iCurrentItem - 1;
  88. }
  89. else if (g_InputState.dwKeyPress & kKeyMenu)
  90. {
  91. return 0;
  92. }
  93. else
  94. {
  95. item_delta = 0;
  96. }
  97. //
  98. // Make sure the current menu item index is in bound
  99. //
  100. if (g_iCurrentItem + item_delta >= 0 &&
  101. g_iCurrentItem + item_delta < g_iNumMagic)
  102. g_iCurrentItem += item_delta;
  103. //
  104. // Create the box.
  105. //
  106. PAL_CreateBoxWithShadow(PAL_XY(10, 42 + iBoxYOffset), iLinesPerPage - 1, 16, 1, FALSE, 0);
  107. if (!gConfig.fIsWIN95)
  108. {
  109. if (gpGlobals->lpObjectDesc == NULL)
  110. {
  111. //
  112. // Draw the cash amount.
  113. //
  114. PAL_CreateSingleLineBox(PAL_XY(0, 0), 5, FALSE);
  115. PAL_DrawText(PAL_GetWord(CASH_LABEL), PAL_XY(10, 10), 0, FALSE, FALSE, FALSE);
  116. PAL_DrawNumber(gpGlobals->dwCash, 6, PAL_XY(49, 14), kNumColorYellow, kNumAlignRight);
  117. //
  118. // Draw the MP of the selected magic.
  119. //
  120. PAL_CreateSingleLineBox(PAL_XY(215, 0), 5, FALSE);
  121. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH),
  122. gpScreen, PAL_XY(260, 14));
  123. PAL_DrawNumber(rgMagicItem[g_iCurrentItem].wMP, 4, PAL_XY(230, 14),
  124. kNumColorYellow, kNumAlignRight);
  125. PAL_DrawNumber(g_wPlayerMP, 4, PAL_XY(265, 14), kNumColorCyan, kNumAlignRight);
  126. }
  127. else
  128. {
  129. WCHAR szDesc[512], *next;
  130. const WCHAR *d = PAL_GetObjectDesc(gpGlobals->lpObjectDesc, rgMagicItem[g_iCurrentItem].wMagic);
  131. //
  132. // Draw the magic description.
  133. //
  134. if (d != NULL)
  135. {
  136. k = 3;
  137. wcscpy(szDesc, d);
  138. d = szDesc;
  139. while (TRUE)
  140. {
  141. next = wcschr(d, '*');
  142. if (next != NULL)
  143. {
  144. *next++ = '\0';
  145. }
  146. PAL_DrawText(d, PAL_XY(100, k), DESCTEXT_COLOR, TRUE, FALSE, FALSE);
  147. k += 16;
  148. if (next == NULL)
  149. {
  150. break;
  151. }
  152. d = next;
  153. }
  154. }
  155. //
  156. // Draw the MP of the selected magic.
  157. //
  158. PAL_CreateSingleLineBoxWithShadow(PAL_XY(0, 0), 5, FALSE,0);
  159. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH),
  160. gpScreen, PAL_XY(45, 14));
  161. PAL_DrawNumber(rgMagicItem[g_iCurrentItem].wMP, 4, PAL_XY(15, 14),
  162. kNumColorYellow, kNumAlignRight);
  163. PAL_DrawNumber(g_wPlayerMP, 4, PAL_XY(50, 14), kNumColorCyan, kNumAlignRight);
  164. }
  165. }
  166. else
  167. {
  168. wScript = gpGlobals->g.rgObject[rgMagicItem[g_iCurrentItem].wMagic].item.wScriptDesc;
  169. line = 0;
  170. while (wScript && gpGlobals->g.lprgScriptEntry[wScript].wOperation != 0)
  171. {
  172. if (gpGlobals->g.lprgScriptEntry[wScript].wOperation == 0xFFFF)
  173. {
  174. int line_incr = (gpGlobals->g.lprgScriptEntry[wScript].rgwOperand[1] != 1) ? 1 : 0;
  175. wScript = PAL_RunAutoScript(wScript, line);
  176. line += line_incr;
  177. }
  178. else
  179. {
  180. wScript = PAL_RunAutoScript(wScript, 0);
  181. }
  182. }
  183. //
  184. // Draw the MP of the selected magic.
  185. //
  186. PAL_CreateSingleLineBox(PAL_XY(0, 0), 5, FALSE);
  187. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH),
  188. gpScreen, PAL_XY(45, 14));
  189. PAL_DrawNumber(rgMagicItem[g_iCurrentItem].wMP, 4, PAL_XY(15, 14),
  190. kNumColorYellow, kNumAlignRight);
  191. PAL_DrawNumber(g_wPlayerMP, 4, PAL_XY(50, 14), kNumColorCyan, kNumAlignRight);
  192. }
  193. //
  194. // Draw the texts of the current page
  195. //
  196. i = g_iCurrentItem / iItemsPerLine * iItemsPerLine - iItemsPerLine * iPageLineOffset;
  197. if (i < 0)
  198. {
  199. i = 0;
  200. }
  201. for (j = 0; j < iLinesPerPage; j++)
  202. {
  203. for (k = 0; k < iItemsPerLine; k++)
  204. {
  205. bColor = MENUITEM_COLOR;
  206. if (i >= g_iNumMagic)
  207. {
  208. //
  209. // End of the list reached
  210. //
  211. j = iLinesPerPage;
  212. break;
  213. }
  214. if (i == g_iCurrentItem)
  215. {
  216. if (rgMagicItem[i].fEnabled)
  217. {
  218. bColor = MENUITEM_COLOR_SELECTED;
  219. }
  220. else
  221. {
  222. bColor = MENUITEM_COLOR_SELECTED_INACTIVE;
  223. }
  224. }
  225. else if (!rgMagicItem[i].fEnabled)
  226. {
  227. bColor = MENUITEM_COLOR_INACTIVE;
  228. }
  229. //
  230. // Draw the text
  231. //
  232. PAL_DrawText(PAL_GetWord(rgMagicItem[i].wMagic), PAL_XY(35 + k * iItemTextWidth, 54 + j * 18 + iBoxYOffset), bColor, TRUE, FALSE, FALSE);
  233. //
  234. // Draw the cursor on the current selected item
  235. //
  236. if (i == g_iCurrentItem)
  237. {
  238. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_CURSOR),
  239. gpScreen, PAL_XY(35 + iCursorXOffset + k * iItemTextWidth, 64 + j * 18 + iBoxYOffset));
  240. }
  241. i++;
  242. }
  243. }
  244. if (g_InputState.dwKeyPress & kKeySearch)
  245. {
  246. if (rgMagicItem[g_iCurrentItem].fEnabled)
  247. {
  248. j = g_iCurrentItem % iItemsPerLine;
  249. k = (g_iCurrentItem < iItemsPerLine * iPageLineOffset) ? (g_iCurrentItem / iItemsPerLine) : iPageLineOffset;
  250. j = 35 + j * iItemTextWidth;
  251. k = 54 + k * 18 + iBoxYOffset;
  252. PAL_DrawText(PAL_GetWord(rgMagicItem[g_iCurrentItem].wMagic), PAL_XY(j, k), MENUITEM_COLOR_CONFIRMED, FALSE, TRUE, FALSE);
  253. return rgMagicItem[g_iCurrentItem].wMagic;
  254. }
  255. }
  256. return 0xFFFF;
  257. }
  258. VOID
  259. PAL_MagicSelectionMenuInit(
  260. WORD wPlayerRole,
  261. BOOL fInBattle,
  262. WORD wDefaultMagic
  263. )
  264. /*++
  265. Purpose:
  266. Initialize the magic selection menu.
  267. Parameters:
  268. [IN] wPlayerRole - the player ID.
  269. [IN] fInBattle - TRUE if in battle, FALSE if not.
  270. [IN] wDefaultMagic - the default magic item.
  271. Return value:
  272. None.
  273. --*/
  274. {
  275. WORD w;
  276. int i, j;
  277. g_iCurrentItem = 0;
  278. g_iNumMagic = 0;
  279. g_wPlayerMP = gpGlobals->g.PlayerRoles.rgwMP[wPlayerRole];
  280. //
  281. // Put all magics of this player to the array
  282. //
  283. for (i = 0; i < MAX_PLAYER_MAGICS; i++)
  284. {
  285. w = gpGlobals->g.PlayerRoles.rgwMagic[i][wPlayerRole];
  286. if (w != 0)
  287. {
  288. rgMagicItem[g_iNumMagic].wMagic = w;
  289. w = gpGlobals->g.rgObject[w].magic.wMagicNumber;
  290. rgMagicItem[g_iNumMagic].wMP = gpGlobals->g.lprgMagic[w].wCostMP;
  291. rgMagicItem[g_iNumMagic].fEnabled = TRUE;
  292. if (rgMagicItem[g_iNumMagic].wMP > g_wPlayerMP)
  293. {
  294. rgMagicItem[g_iNumMagic].fEnabled = FALSE;
  295. }
  296. w = gpGlobals->g.rgObject[rgMagicItem[g_iNumMagic].wMagic].magic.wFlags;
  297. if (fInBattle)
  298. {
  299. if (!(w & kMagicFlagUsableInBattle))
  300. {
  301. rgMagicItem[g_iNumMagic].fEnabled = FALSE;
  302. }
  303. }
  304. else
  305. {
  306. if (!(w & kMagicFlagUsableOutsideBattle))
  307. {
  308. rgMagicItem[g_iNumMagic].fEnabled = FALSE;
  309. }
  310. }
  311. g_iNumMagic++;
  312. }
  313. }
  314. //
  315. // Sort the array
  316. //
  317. for (i = 0; i < g_iNumMagic - 1; i++)
  318. {
  319. BOOL fCompleted = TRUE;
  320. for (j = 0; j < g_iNumMagic - 1 - i; j++)
  321. {
  322. if (rgMagicItem[j].wMagic > rgMagicItem[j + 1].wMagic)
  323. {
  324. struct MAGICITEM t = rgMagicItem[j];
  325. rgMagicItem[j] = rgMagicItem[j + 1];
  326. rgMagicItem[j + 1] = t;
  327. fCompleted = FALSE;
  328. }
  329. }
  330. if (fCompleted)
  331. {
  332. break;
  333. }
  334. }
  335. //
  336. // Place the cursor to the default item
  337. //
  338. for (i = 0; i < g_iNumMagic; i++)
  339. {
  340. if (rgMagicItem[i].wMagic == wDefaultMagic)
  341. {
  342. g_iCurrentItem = i;
  343. break;
  344. }
  345. }
  346. }
  347. WORD
  348. PAL_MagicSelectionMenu(
  349. WORD wPlayerRole,
  350. BOOL fInBattle,
  351. WORD wDefaultMagic
  352. )
  353. /*++
  354. Purpose:
  355. Show the magic selection menu.
  356. Parameters:
  357. [IN] wPlayerRole - the player ID.
  358. [IN] fInBattle - TRUE if in battle, FALSE if not.
  359. [IN] wDefaultMagic - the default magic item.
  360. Return value:
  361. The selected magic. 0 if cancelled.
  362. --*/
  363. {
  364. WORD w;
  365. int i;
  366. DWORD dwTime;
  367. PAL_MagicSelectionMenuInit(wPlayerRole, fInBattle, wDefaultMagic);
  368. PAL_ClearKeyState();
  369. dwTime = SDL_GetTicks();
  370. while (TRUE)
  371. {
  372. PAL_MakeScene();
  373. w = 45;
  374. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  375. {
  376. PAL_PlayerInfoBox(PAL_XY(w, 165), gpGlobals->rgParty[i].wPlayerRole, 100,
  377. TIMEMETER_COLOR_DEFAULT, FALSE);
  378. w += 78;
  379. }
  380. w = PAL_MagicSelectionMenuUpdate();
  381. VIDEO_UpdateScreen(NULL);
  382. PAL_ClearKeyState();
  383. if (w != 0xFFFF)
  384. {
  385. return w;
  386. }
  387. PAL_ProcessEvent();
  388. while (!SDL_TICKS_PASSED(SDL_GetTicks(), dwTime))
  389. {
  390. PAL_ProcessEvent();
  391. if (g_InputState.dwKeyPress != 0)
  392. {
  393. break;
  394. }
  395. SDL_Delay(5);
  396. }
  397. dwTime = SDL_GetTicks() + FRAME_TIME;
  398. }
  399. return 0; // should not really reach here
  400. }