magicmenu.c 12 KB

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