magicmenu.c 11 KB

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