magicmenu.c 11 KB

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