magicmenu.c 11 KB

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