magicmenu.c 12 KB

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