magicmenu.c 11 KB

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