magicmenu.c 11 KB

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