magicmenu.c 12 KB

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