battle.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. #include "main.h"
  22. BATTLE g_Battle;
  23. WORD
  24. g_rgPlayerPos[3][3][2] = {
  25. {{240, 170}}, // one player
  26. {{200, 176}, {256, 152}}, // two players
  27. {{180, 180}, {234, 170}, {270, 146}} // three players
  28. };
  29. VOID
  30. PAL_BattleMakeScene(
  31. VOID
  32. )
  33. /*++
  34. Purpose:
  35. Generate the battle scene into the scene buffer.
  36. Parameters:
  37. None.
  38. Return value:
  39. None.
  40. --*/
  41. {
  42. int i;
  43. PAL_POS pos;
  44. LPBYTE pSrc, pDst;
  45. BYTE b;
  46. //
  47. // Draw the background
  48. //
  49. pSrc = g_Battle.lpBackground->pixels;
  50. pDst = g_Battle.lpSceneBuf->pixels;
  51. for (i = 0; i < g_Battle.lpSceneBuf->pitch * g_Battle.lpSceneBuf->h; i++)
  52. {
  53. b = (*pSrc & 0x0F);
  54. b += g_Battle.sBackgroundColorShift;
  55. if (b & 0x80)
  56. {
  57. b = 0;
  58. }
  59. else if (b & 0x70)
  60. {
  61. b = 0x0F;
  62. }
  63. *pDst = (b | (*pSrc & 0xF0));
  64. ++pSrc;
  65. ++pDst;
  66. }
  67. PAL_ApplyWave(g_Battle.lpSceneBuf);
  68. //
  69. // Draw the enemies
  70. //
  71. for (i = g_Battle.wMaxEnemyIndex; i >= 0; i--)
  72. {
  73. pos = g_Battle.rgEnemy[i].pos;
  74. if (g_Battle.rgEnemy[i].rgwStatus[kStatusConfused] > 0 &&
  75. g_Battle.rgEnemy[i].rgwStatus[kStatusSleep] == 0 &&
  76. g_Battle.rgEnemy[i].rgwStatus[kStatusParalyzed] == 0)
  77. {
  78. //
  79. // Enemy is confused
  80. //
  81. pos = PAL_XY(PAL_X(pos) + RandomLong(-1, 1), PAL_Y(pos));
  82. }
  83. pos = PAL_XY(PAL_X(pos) - PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame)) / 2,
  84. PAL_Y(pos) - PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame)));
  85. if (g_Battle.rgEnemy[i].wObjectID != 0)
  86. {
  87. if (g_Battle.rgEnemy[i].iColorShift)
  88. {
  89. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame),
  90. g_Battle.lpSceneBuf, pos, g_Battle.rgEnemy[i].iColorShift);
  91. }
  92. else
  93. {
  94. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(g_Battle.rgEnemy[i].lpSprite, g_Battle.rgEnemy[i].wCurrentFrame),
  95. g_Battle.lpSceneBuf, pos);
  96. }
  97. }
  98. }
  99. if (g_Battle.lpSummonSprite != NULL)
  100. {
  101. //
  102. // Draw the summoned god
  103. //
  104. pos = PAL_XY(PAL_X(g_Battle.posSummon) - PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.lpSummonSprite, g_Battle.iSummonFrame)) / 2,
  105. PAL_Y(g_Battle.posSummon) - PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.lpSummonSprite, g_Battle.iSummonFrame)));
  106. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(g_Battle.lpSummonSprite, g_Battle.iSummonFrame),
  107. g_Battle.lpSceneBuf, pos);
  108. }
  109. else
  110. {
  111. //
  112. // Draw the players
  113. //
  114. for (i = gpGlobals->wMaxPartyMemberIndex; i >= 0; i--)
  115. {
  116. pos = g_Battle.rgPlayer[i].pos;
  117. if (gpGlobals->rgPlayerStatus[gpGlobals->rgParty[i].wPlayerRole][kStatusConfused] != 0 &&
  118. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[i].wPlayerRole][kStatusSleep] == 0 &&
  119. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[i].wPlayerRole][kStatusParalyzed] == 0 &&
  120. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] > 0)
  121. {
  122. //
  123. // Player is confused
  124. //
  125. continue;
  126. }
  127. pos = PAL_XY(PAL_X(pos) - PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame)) / 2,
  128. PAL_Y(pos) - PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame)));
  129. if (g_Battle.rgPlayer[i].iColorShift != 0)
  130. {
  131. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame),
  132. g_Battle.lpSceneBuf, pos, g_Battle.rgPlayer[i].iColorShift);
  133. }
  134. else if (g_Battle.iHidingTime == 0)
  135. {
  136. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame),
  137. g_Battle.lpSceneBuf, pos);
  138. }
  139. }
  140. //
  141. // Confused players should be drawn on top of normal players
  142. //
  143. for (i = gpGlobals->wMaxPartyMemberIndex; i >= 0; i--)
  144. {
  145. if (gpGlobals->rgPlayerStatus[gpGlobals->rgParty[i].wPlayerRole][kStatusConfused] != 0 &&
  146. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[i].wPlayerRole][kStatusSleep] == 0 &&
  147. gpGlobals->rgPlayerStatus[gpGlobals->rgParty[i].wPlayerRole][kStatusParalyzed] == 0 &&
  148. gpGlobals->g.PlayerRoles.rgwHP[gpGlobals->rgParty[i].wPlayerRole] > 0)
  149. {
  150. //
  151. // Player is confused
  152. //
  153. pos = PAL_XY(PAL_X(g_Battle.rgPlayer[i].pos), PAL_Y(g_Battle.rgPlayer[i].pos) + RandomLong(-1, 1));
  154. pos = PAL_XY(PAL_X(pos) - PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame)) / 2,
  155. PAL_Y(pos) - PAL_RLEGetHeight(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame)));
  156. if (g_Battle.rgPlayer[i].iColorShift != 0)
  157. {
  158. PAL_RLEBlitWithColorShift(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame),
  159. g_Battle.lpSceneBuf, pos, g_Battle.rgPlayer[i].iColorShift);
  160. }
  161. else if (g_Battle.iHidingTime == 0)
  162. {
  163. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(g_Battle.rgPlayer[i].lpSprite, g_Battle.rgPlayer[i].wCurrentFrame),
  164. g_Battle.lpSceneBuf, pos);
  165. }
  166. }
  167. }
  168. }
  169. }
  170. VOID
  171. PAL_BattleBackupScene(
  172. VOID
  173. )
  174. /*++
  175. Purpose:
  176. Backup the scene buffer.
  177. Parameters:
  178. None.
  179. Return value:
  180. None.
  181. --*/
  182. {
  183. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreenBak, NULL);
  184. }
  185. VOID
  186. PAL_BattleFadeScene(
  187. VOID
  188. )
  189. /*++
  190. Purpose:
  191. Fade in the scene of battle.
  192. Parameters:
  193. None.
  194. Return value:
  195. None.
  196. --*/
  197. {
  198. int i, j, k;
  199. DWORD time;
  200. BYTE a, b;
  201. const int rgIndex[6] = {0, 3, 1, 5, 2, 4};
  202. time = SDL_GetTicks();
  203. for (i = 0; i < 12; i++)
  204. {
  205. for (j = 0; j < 6; j++)
  206. {
  207. PAL_DelayUntil(time);
  208. time = SDL_GetTicks() + 16;
  209. //
  210. // Blend the pixels in the 2 buffers, and put the result into the
  211. // backup buffer
  212. //
  213. for (k = rgIndex[j]; k < gpScreen->pitch * gpScreen->h; k += 6)
  214. {
  215. a = ((LPBYTE)(g_Battle.lpSceneBuf->pixels))[k];
  216. b = ((LPBYTE)(gpScreenBak->pixels))[k];
  217. if (i > 0)
  218. {
  219. if ((a & 0x0F) > (b & 0x0F))
  220. {
  221. b++;
  222. }
  223. else if ((a & 0x0F) < (b & 0x0F))
  224. {
  225. b--;
  226. }
  227. }
  228. ((LPBYTE)(gpScreenBak->pixels))[k] = ((a & 0xF0) | (b & 0x0F));
  229. }
  230. //
  231. // Draw the backup buffer to the screen
  232. //
  233. SDL_BlitSurface(gpScreenBak, NULL, gpScreen, NULL);
  234. PAL_BattleUIUpdate();
  235. VIDEO_UpdateScreen(NULL);
  236. }
  237. }
  238. //
  239. // Draw the result buffer to the screen as the final step
  240. //
  241. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  242. PAL_BattleUIUpdate();
  243. VIDEO_UpdateScreen(NULL);
  244. }
  245. static BATTLERESULT
  246. PAL_BattleMain(
  247. VOID
  248. )
  249. /*++
  250. Purpose:
  251. The main battle routine.
  252. Parameters:
  253. None.
  254. Return value:
  255. The result of the battle.
  256. --*/
  257. {
  258. int i;
  259. DWORD dwTime;
  260. VIDEO_BackupScreen();
  261. //
  262. // Generate the scene and draw the scene to the screen buffer
  263. //
  264. PAL_BattleMakeScene();
  265. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  266. //
  267. // Fade out the music and delay for a while
  268. //
  269. AUDIO_PlayMusic(0, FALSE, 1);
  270. UTIL_Delay(200);
  271. //
  272. // Switch the screen
  273. //
  274. VIDEO_SwitchScreen(5);
  275. //
  276. // Play the battle music
  277. //
  278. AUDIO_PlayMusic(gpGlobals->wNumBattleMusic, TRUE, 0);
  279. //
  280. // Fade in the screen when needed
  281. //
  282. if (gpGlobals->fNeedToFadeIn)
  283. {
  284. PAL_FadeIn(gpGlobals->wNumPalette, gpGlobals->fNightPalette, 1);
  285. gpGlobals->fNeedToFadeIn = FALSE;
  286. }
  287. //
  288. // Run the pre-battle scripts for each enemies
  289. //
  290. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  291. {
  292. g_Battle.rgEnemy[i].wScriptOnTurnStart =
  293. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnTurnStart, i);
  294. if (g_Battle.BattleResult != kBattleResultPreBattle)
  295. {
  296. break;
  297. }
  298. }
  299. if (g_Battle.BattleResult == kBattleResultPreBattle)
  300. {
  301. g_Battle.BattleResult = kBattleResultOnGoing;
  302. }
  303. #ifndef PAL_CLASSIC
  304. PAL_UpdateTimeChargingUnit();
  305. #endif
  306. dwTime = SDL_GetTicks();
  307. PAL_ClearKeyState();
  308. //
  309. // Run the main battle loop.
  310. //
  311. while (TRUE)
  312. {
  313. //
  314. // Break out if the battle ended.
  315. //
  316. if (g_Battle.BattleResult != kBattleResultOnGoing)
  317. {
  318. break;
  319. }
  320. //
  321. // Wait for the time of one frame. Accept input here.
  322. //
  323. PAL_DelayUntil(dwTime);
  324. //
  325. // Set the time of the next frame.
  326. //
  327. dwTime = SDL_GetTicks() + BATTLE_FRAME_TIME;
  328. //
  329. // Run the main frame routine.
  330. //
  331. PAL_BattleStartFrame();
  332. //
  333. // Update the screen.
  334. //
  335. VIDEO_UpdateScreen(NULL);
  336. }
  337. //
  338. // Return the battle result
  339. //
  340. return g_Battle.BattleResult;
  341. }
  342. static VOID
  343. PAL_FreeBattleSprites(
  344. VOID
  345. )
  346. /*++
  347. Purpose:
  348. Free all the loaded sprites.
  349. Parameters:
  350. None.
  351. Return value:
  352. None.
  353. --*/
  354. {
  355. int i;
  356. //
  357. // Free all the loaded sprites
  358. //
  359. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  360. {
  361. if (g_Battle.rgPlayer[i].lpSprite != NULL)
  362. {
  363. free(g_Battle.rgPlayer[i].lpSprite);
  364. }
  365. g_Battle.rgPlayer[i].lpSprite = NULL;
  366. }
  367. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  368. {
  369. if (g_Battle.rgEnemy[i].lpSprite != NULL)
  370. {
  371. free(g_Battle.rgEnemy[i].lpSprite);
  372. }
  373. g_Battle.rgEnemy[i].lpSprite = NULL;
  374. }
  375. if (g_Battle.lpSummonSprite != NULL)
  376. {
  377. free(g_Battle.lpSummonSprite);
  378. }
  379. g_Battle.lpSummonSprite = NULL;
  380. }
  381. VOID
  382. PAL_LoadBattleSprites(
  383. VOID
  384. )
  385. /*++
  386. Purpose:
  387. Load all the loaded sprites.
  388. Parameters:
  389. None.
  390. Return value:
  391. None.
  392. --*/
  393. {
  394. int i, l, x, y, s;
  395. FILE *fp;
  396. PAL_FreeBattleSprites();
  397. fp = UTIL_OpenRequiredFile("abc.mkf");
  398. //
  399. // Load battle sprites for players
  400. //
  401. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  402. {
  403. s = PAL_GetPlayerBattleSprite(gpGlobals->rgParty[i].wPlayerRole);
  404. l = PAL_MKFGetDecompressedSize(s, gpGlobals->f.fpF);
  405. if (l <= 0)
  406. {
  407. continue;
  408. }
  409. g_Battle.rgPlayer[i].lpSprite = UTIL_calloc(l, 1);
  410. PAL_MKFDecompressChunk(g_Battle.rgPlayer[i].lpSprite, l,
  411. s, gpGlobals->f.fpF);
  412. //
  413. // Set the default position for this player
  414. //
  415. x = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][i][0];
  416. y = g_rgPlayerPos[gpGlobals->wMaxPartyMemberIndex][i][1];
  417. g_Battle.rgPlayer[i].posOriginal = PAL_XY(x, y);
  418. g_Battle.rgPlayer[i].pos = PAL_XY(x, y);
  419. }
  420. //
  421. // Load battle sprites for enemies
  422. //
  423. for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
  424. {
  425. if (g_Battle.rgEnemy[i].wObjectID == 0)
  426. {
  427. continue;
  428. }
  429. l = PAL_MKFGetDecompressedSize(
  430. gpGlobals->g.rgObject[g_Battle.rgEnemy[i].wObjectID].enemy.wEnemyID, fp);
  431. if (l <= 0)
  432. {
  433. continue;
  434. }
  435. g_Battle.rgEnemy[i].lpSprite = UTIL_calloc(l, 1);
  436. PAL_MKFDecompressChunk(g_Battle.rgEnemy[i].lpSprite, l,
  437. gpGlobals->g.rgObject[g_Battle.rgEnemy[i].wObjectID].enemy.wEnemyID, fp);
  438. //
  439. // Set the default position for this enemy
  440. //
  441. x = gpGlobals->g.EnemyPos.pos[i][g_Battle.wMaxEnemyIndex].x;
  442. y = gpGlobals->g.EnemyPos.pos[i][g_Battle.wMaxEnemyIndex].y;
  443. y += g_Battle.rgEnemy[i].e.wYPosOffset;
  444. g_Battle.rgEnemy[i].posOriginal = PAL_XY(x, y);
  445. g_Battle.rgEnemy[i].pos = PAL_XY(x, y);
  446. }
  447. fclose(fp);
  448. }
  449. static VOID
  450. PAL_LoadBattleBackground(
  451. VOID
  452. )
  453. /*++
  454. Purpose:
  455. Load the screen background picture of the battle.
  456. Parameters:
  457. None.
  458. Return value:
  459. None.
  460. --*/
  461. {
  462. PAL_LARGE BYTE buf[320 * 200];
  463. //
  464. // Create the surface
  465. //
  466. g_Battle.lpBackground =
  467. SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
  468. gpScreen->format->Rmask, gpScreen->format->Gmask,
  469. gpScreen->format->Bmask, gpScreen->format->Amask);
  470. if (g_Battle.lpBackground == NULL)
  471. {
  472. TerminateOnError("PAL_LoadBattleBackground(): failed to create surface!");
  473. }
  474. #if SDL_VERSION_ATLEAST(2, 0, 0)
  475. SDL_SetSurfacePalette(g_Battle.lpBackground, gpScreen->format->palette);
  476. #else
  477. SDL_SetPalette(g_Battle.lpBackground, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  478. #endif
  479. //
  480. // Load the picture
  481. //
  482. PAL_MKFDecompressChunk(buf, 320 * 200, gpGlobals->wNumBattleField, gpGlobals->f.fpFBP);
  483. //
  484. // Draw the picture to the surface.
  485. //
  486. PAL_FBPBlitToSurface(buf, g_Battle.lpBackground);
  487. }
  488. static VOID
  489. PAL_BattleWon(
  490. VOID
  491. )
  492. /*++
  493. Purpose:
  494. Show the "you win" message and add the experience points for players.
  495. Parameters:
  496. None.
  497. Return value:
  498. None.
  499. --*/
  500. {
  501. const SDL_Rect rect = {0, 60, 320, 100};
  502. SDL_Rect rect1 = {80, 0, 180, 200};
  503. int i, j, iTotalCount;
  504. DWORD dwExp;
  505. WORD w;
  506. BOOL fLevelUp;
  507. PLAYERROLES OrigPlayerRoles;
  508. //
  509. // Backup the initial player stats
  510. //
  511. OrigPlayerRoles = gpGlobals->g.PlayerRoles;
  512. if (g_Battle.iExpGained > 0)
  513. {
  514. int w1 = PAL_WordWidth(BATTLEWIN_GETEXP_LABEL) + 3;
  515. int ww1 = (w1 - 8) << 3;
  516. //
  517. // Play the "battle win" music
  518. //
  519. AUDIO_PlayMusic(g_Battle.fIsBoss ? 2 : 3, FALSE, 0);
  520. //
  521. // Show the message about the total number of exp. and cash gained
  522. //
  523. PAL_CreateSingleLineBox(PAL_XY(83 - ww1, 60), w1, FALSE);
  524. PAL_CreateSingleLineBox(PAL_XY(65, 105), 10, FALSE);
  525. PAL_DrawText(PAL_GetWord(BATTLEWIN_GETEXP_LABEL), PAL_XY(95 - ww1, 70), 0, FALSE, FALSE, FALSE);
  526. PAL_DrawText(PAL_GetWord(BATTLEWIN_BEATENEMY_LABEL), PAL_XY(77, 115), 0, FALSE, FALSE, FALSE);
  527. PAL_DrawText(PAL_GetWord(BATTLEWIN_DOLLAR_LABEL), PAL_XY(197, 115), 0, FALSE, FALSE, FALSE);
  528. PAL_DrawNumber(g_Battle.iExpGained, 5, PAL_XY(182 + ww1, 74), kNumColorYellow, kNumAlignRight);
  529. PAL_DrawNumber(g_Battle.iCashGained, 5, PAL_XY(162, 119), kNumColorYellow, kNumAlignMid);
  530. VIDEO_UpdateScreen(&rect);
  531. PAL_WaitForKey(g_Battle.fIsBoss ? 5500 : 3000);
  532. }
  533. //
  534. // Add the cash value
  535. //
  536. gpGlobals->dwCash += g_Battle.iCashGained;
  537. //
  538. // Add the experience points for each players
  539. //
  540. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  541. {
  542. fLevelUp = FALSE;
  543. w = gpGlobals->rgParty[i].wPlayerRole;
  544. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  545. {
  546. continue; // don't care about dead players
  547. }
  548. dwExp = gpGlobals->Exp.rgPrimaryExp[w].wExp;
  549. dwExp += g_Battle.iExpGained;
  550. if (gpGlobals->g.PlayerRoles.rgwLevel[w] > MAX_LEVELS)
  551. {
  552. gpGlobals->g.PlayerRoles.rgwLevel[w] = MAX_LEVELS;
  553. }
  554. while (dwExp >= gpGlobals->g.rgLevelUpExp[gpGlobals->g.PlayerRoles.rgwLevel[w]])
  555. {
  556. dwExp -= gpGlobals->g.rgLevelUpExp[gpGlobals->g.PlayerRoles.rgwLevel[w]];
  557. if (gpGlobals->g.PlayerRoles.rgwLevel[w] < MAX_LEVELS)
  558. {
  559. fLevelUp = TRUE;
  560. PAL_PlayerLevelUp(w, 1);
  561. gpGlobals->g.PlayerRoles.rgwHP[w] = gpGlobals->g.PlayerRoles.rgwMaxHP[w];
  562. gpGlobals->g.PlayerRoles.rgwMP[w] = gpGlobals->g.PlayerRoles.rgwMaxMP[w];
  563. }
  564. }
  565. gpGlobals->Exp.rgPrimaryExp[w].wExp = (WORD)dwExp;
  566. const MENUITEM rgFakeMenuItem[] =
  567. {
  568. // value label enabled pos
  569. { 1, STATUS_LABEL_LEVEL, TRUE, PAL_XY(0, 0) },
  570. { 2, STATUS_LABEL_HP, TRUE, PAL_XY(0, 0) },
  571. { 3, STATUS_LABEL_MP, TRUE, PAL_XY(0, 0) },
  572. { 4, STATUS_LABEL_ATTACKPOWER, TRUE, PAL_XY(0, 0) },
  573. { 5, STATUS_LABEL_MAGICPOWER, TRUE, PAL_XY(0, 0) },
  574. { 6, STATUS_LABEL_RESISTANCE, TRUE, PAL_XY(0, 0) },
  575. { 7, STATUS_LABEL_DEXTERITY, TRUE, PAL_XY(0, 0) },
  576. { 8, STATUS_LABEL_FLEERATE, TRUE, PAL_XY(0, 0) },
  577. };
  578. int maxTextWidth = PAL_MenuTextMaxWidth(rgFakeMenuItem, sizeof(rgFakeMenuItem) / sizeof(MENUITEM)) - 1;
  579. int chars = maxTextWidth - 1;
  580. int offsetX = -8*chars;
  581. rect1.x += offsetX;
  582. rect1.w -= 2*offsetX;
  583. if (fLevelUp)
  584. {
  585. //
  586. // Player has gained a level. Show the message
  587. //
  588. PAL_CreateSingleLineBox(PAL_XY(offsetX+80, 0), chars+10, FALSE);
  589. PAL_CreateBox(PAL_XY(offsetX+82, 32), 7, chars+8, 1, FALSE);
  590. PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]), PAL_XY(110, 10), 0, FALSE, FALSE, FALSE);
  591. PAL_DrawText(PAL_GetWord(STATUS_LABEL_LEVEL), PAL_XY(110+PAL_TextWidth(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w])), 10), 0, FALSE, FALSE, FALSE);
  592. PAL_DrawText(PAL_GetWord(BATTLEWIN_LEVELUP_LABEL), PAL_XY(110+PAL_TextWidth(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]))+PAL_TextWidth(PAL_GetWord(STATUS_LABEL_LEVEL)), 10), 0, FALSE, FALSE, FALSE);
  593. for (j = 0; j < 8; j++)
  594. {
  595. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ARROW),
  596. gpScreen, PAL_XY(-offsetX+180, 48 + 18 * j));
  597. }
  598. PAL_DrawText(PAL_GetWord(STATUS_LABEL_LEVEL), PAL_XY(offsetX+100, 44), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  599. PAL_DrawText(PAL_GetWord(STATUS_LABEL_HP), PAL_XY(offsetX+100, 62), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  600. PAL_DrawText(PAL_GetWord(STATUS_LABEL_MP), PAL_XY(offsetX+100, 80), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  601. PAL_DrawText(PAL_GetWord(STATUS_LABEL_ATTACKPOWER), PAL_XY(offsetX+100, 98), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  602. PAL_DrawText(PAL_GetWord(STATUS_LABEL_MAGICPOWER), PAL_XY(offsetX+100, 116), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  603. PAL_DrawText(PAL_GetWord(STATUS_LABEL_RESISTANCE), PAL_XY(offsetX+100, 134), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  604. PAL_DrawText(PAL_GetWord(STATUS_LABEL_DEXTERITY), PAL_XY(offsetX+100, 152), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  605. PAL_DrawText(PAL_GetWord(STATUS_LABEL_FLEERATE), PAL_XY(offsetX+100, 170), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  606. //
  607. // Draw the original stats and stats after level up
  608. //
  609. PAL_DrawNumber(OrigPlayerRoles.rgwLevel[w], 4, PAL_XY(-offsetX+133, 47),
  610. kNumColorYellow, kNumAlignRight);
  611. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwLevel[w], 4, PAL_XY(-offsetX+195, 47),
  612. kNumColorYellow, kNumAlignRight);
  613. PAL_DrawNumber(OrigPlayerRoles.rgwHP[w], 4, PAL_XY(-offsetX+133, 64),
  614. kNumColorYellow, kNumAlignRight);
  615. PAL_DrawNumber(OrigPlayerRoles.rgwMaxHP[w], 4, PAL_XY(-offsetX+154, 68),
  616. kNumColorBlue, kNumAlignRight);
  617. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  618. PAL_XY(-offsetX+156, 66));
  619. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[w], 4, PAL_XY(-offsetX+195, 64),
  620. kNumColorYellow, kNumAlignRight);
  621. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[w], 4, PAL_XY(-offsetX+216, 68),
  622. kNumColorBlue, kNumAlignRight);
  623. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  624. PAL_XY(-offsetX+218, 66));
  625. PAL_DrawNumber(OrigPlayerRoles.rgwMP[w], 4, PAL_XY(-offsetX+133, 82),
  626. kNumColorYellow, kNumAlignRight);
  627. PAL_DrawNumber(OrigPlayerRoles.rgwMaxMP[w], 4, PAL_XY(-offsetX+154, 86),
  628. kNumColorBlue, kNumAlignRight);
  629. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  630. PAL_XY(-offsetX+156, 84));
  631. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[w], 4, PAL_XY(-offsetX+195, 82),
  632. kNumColorYellow, kNumAlignRight);
  633. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[w], 4, PAL_XY(-offsetX+216, 86),
  634. kNumColorBlue, kNumAlignRight);
  635. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  636. PAL_XY(-offsetX+218, 84));
  637. PAL_DrawNumber(OrigPlayerRoles.rgwAttackStrength[w] + PAL_GetPlayerAttackStrength(w) -
  638. gpGlobals->g.PlayerRoles.rgwAttackStrength[w],
  639. 4, PAL_XY(-offsetX+133, 101), kNumColorYellow, kNumAlignRight);
  640. PAL_DrawNumber(PAL_GetPlayerAttackStrength(w), 4, PAL_XY(-offsetX+195, 101),
  641. kNumColorYellow, kNumAlignRight);
  642. PAL_DrawNumber(OrigPlayerRoles.rgwMagicStrength[w] + PAL_GetPlayerMagicStrength(w) -
  643. gpGlobals->g.PlayerRoles.rgwMagicStrength[w],
  644. 4, PAL_XY(-offsetX+133, 119), kNumColorYellow, kNumAlignRight);
  645. PAL_DrawNumber(PAL_GetPlayerMagicStrength(w), 4, PAL_XY(-offsetX+195, 119),
  646. kNumColorYellow, kNumAlignRight);
  647. PAL_DrawNumber(OrigPlayerRoles.rgwDefense[w] + PAL_GetPlayerDefense(w) -
  648. gpGlobals->g.PlayerRoles.rgwDefense[w],
  649. 4, PAL_XY(-offsetX+133, 137), kNumColorYellow, kNumAlignRight);
  650. PAL_DrawNumber(PAL_GetPlayerDefense(w), 4, PAL_XY(-offsetX+195, 137),
  651. kNumColorYellow, kNumAlignRight);
  652. PAL_DrawNumber(OrigPlayerRoles.rgwDexterity[w] + PAL_GetPlayerDexterity(w) -
  653. gpGlobals->g.PlayerRoles.rgwDexterity[w],
  654. 4, PAL_XY(-offsetX+133, 155), kNumColorYellow, kNumAlignRight);
  655. PAL_DrawNumber(PAL_GetPlayerDexterity(w), 4, PAL_XY(-offsetX+195, 155),
  656. kNumColorYellow, kNumAlignRight);
  657. PAL_DrawNumber(OrigPlayerRoles.rgwFleeRate[w] + PAL_GetPlayerFleeRate(w) -
  658. gpGlobals->g.PlayerRoles.rgwFleeRate[w],
  659. 4, PAL_XY(-offsetX+133, 173), kNumColorYellow, kNumAlignRight);
  660. PAL_DrawNumber(PAL_GetPlayerFleeRate(w), 4, PAL_XY(-offsetX+195, 173),
  661. kNumColorYellow, kNumAlignRight);
  662. //
  663. // Update the screen and wait for key
  664. //
  665. VIDEO_UpdateScreen(&rect1);
  666. PAL_WaitForKey(3000);
  667. OrigPlayerRoles = gpGlobals->g.PlayerRoles;
  668. }
  669. //
  670. // Increasing of other hidden levels
  671. //
  672. iTotalCount = 0;
  673. iTotalCount += gpGlobals->Exp.rgAttackExp[w].wCount;
  674. iTotalCount += gpGlobals->Exp.rgDefenseExp[w].wCount;
  675. iTotalCount += gpGlobals->Exp.rgDexterityExp[w].wCount;
  676. iTotalCount += gpGlobals->Exp.rgFleeExp[w].wCount;
  677. iTotalCount += gpGlobals->Exp.rgHealthExp[w].wCount;
  678. iTotalCount += gpGlobals->Exp.rgMagicExp[w].wCount;
  679. iTotalCount += gpGlobals->Exp.rgMagicPowerExp[w].wCount;
  680. if (iTotalCount > 0)
  681. {
  682. #define CHECK_HIDDEN_EXP(expname, statname, label) \
  683. { \
  684. dwExp = g_Battle.iExpGained; \
  685. dwExp *= gpGlobals->Exp.expname[w].wCount; \
  686. dwExp /= iTotalCount; \
  687. dwExp *= 2; \
  688. \
  689. dwExp += gpGlobals->Exp.expname[w].wExp; \
  690. \
  691. if (gpGlobals->Exp.expname[w].wLevel > MAX_LEVELS) \
  692. { \
  693. gpGlobals->Exp.expname[w].wLevel = MAX_LEVELS; \
  694. } \
  695. \
  696. while (dwExp >= gpGlobals->g.rgLevelUpExp[gpGlobals->Exp.expname[w].wLevel]) \
  697. { \
  698. dwExp -= gpGlobals->g.rgLevelUpExp[gpGlobals->Exp.expname[w].wLevel]; \
  699. gpGlobals->g.PlayerRoles.statname[w] += RandomLong(1, 2); \
  700. if (gpGlobals->Exp.expname[w].wLevel < MAX_LEVELS) \
  701. { \
  702. gpGlobals->Exp.expname[w].wLevel++; \
  703. } \
  704. } \
  705. \
  706. gpGlobals->Exp.expname[w].wExp = (WORD)dwExp; \
  707. \
  708. if (gpGlobals->g.PlayerRoles.statname[w] != OrigPlayerRoles.statname[w]) \
  709. { \
  710. PAL_CreateSingleLineBox(PAL_XY(83, 60), 8, FALSE); \
  711. PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]), PAL_XY(95, 70), 0, FALSE, FALSE, FALSE); \
  712. PAL_DrawText(PAL_GetWord(label), PAL_XY(143, 70), 0, FALSE, FALSE, FALSE); \
  713. PAL_DrawText(PAL_GetWord(BATTLEWIN_LEVELUP_LABEL), PAL_XY(175, 70), 0, FALSE, FALSE, FALSE); \
  714. PAL_DrawNumber(gpGlobals->g.PlayerRoles.statname[w] - OrigPlayerRoles.statname[w], 5, PAL_XY(188, 74), kNumColorYellow, kNumAlignRight); \
  715. VIDEO_UpdateScreen(&rect); \
  716. PAL_WaitForKey(3000); \
  717. } \
  718. }
  719. CHECK_HIDDEN_EXP(rgHealthExp, rgwMaxHP, STATUS_LABEL_HP);
  720. CHECK_HIDDEN_EXP(rgMagicExp, rgwMaxMP, STATUS_LABEL_MP);
  721. CHECK_HIDDEN_EXP(rgAttackExp, rgwAttackStrength, STATUS_LABEL_ATTACKPOWER);
  722. CHECK_HIDDEN_EXP(rgMagicPowerExp, rgwMagicStrength, STATUS_LABEL_MAGICPOWER);
  723. CHECK_HIDDEN_EXP(rgDefenseExp, rgwDefense, STATUS_LABEL_RESISTANCE);
  724. CHECK_HIDDEN_EXP(rgDexterityExp, rgwDexterity, STATUS_LABEL_DEXTERITY);
  725. CHECK_HIDDEN_EXP(rgFleeExp, rgwFleeRate, STATUS_LABEL_FLEERATE);
  726. #undef CHECK_HIDDEN_EXP
  727. }
  728. //
  729. // Learn all magics at the current level
  730. //
  731. j = 0;
  732. while (j < gpGlobals->g.nLevelUpMagic)
  733. {
  734. if (gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic == 0 ||
  735. gpGlobals->g.lprgLevelUpMagic[j].m[w].wLevel > gpGlobals->g.PlayerRoles.rgwLevel[w])
  736. {
  737. j++;
  738. continue;
  739. }
  740. if (PAL_AddMagic(w, gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic))
  741. {
  742. int ww;
  743. int w1 = (ww = PAL_WordWidth(gpGlobals->g.PlayerRoles.rgwName[w])) > 3 ? ww : 3;
  744. int w2 = (ww = PAL_WordWidth(BATTLEWIN_ADDMAGIC_LABEL)) > 2 ? ww : 2;
  745. int w3 = (ww = PAL_WordWidth(gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic)) > 5 ? ww : 5;
  746. ww = (w1 + w2 + w3 - 10) << 3;
  747. PAL_CreateSingleLineBox(PAL_XY(65 - ww, 105), w1 + w2 + w3, FALSE);
  748. PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]), PAL_XY(75 - ww, 115), 0, FALSE, FALSE, FALSE);
  749. PAL_DrawText(PAL_GetWord(BATTLEWIN_ADDMAGIC_LABEL), PAL_XY(75 + 16 * w1 - ww, 115), 0, FALSE, FALSE, FALSE);
  750. PAL_DrawText(PAL_GetWord(gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic), PAL_XY(75 + 16 * (w1 + w2) - ww, 115), 0x1B, FALSE, FALSE, FALSE);
  751. VIDEO_UpdateScreen(&rect);
  752. PAL_WaitForKey(3000);
  753. }
  754. j++;
  755. }
  756. }
  757. //
  758. // Run the post-battle scripts
  759. //
  760. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  761. {
  762. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnBattleEnd, i);
  763. }
  764. //
  765. // Recover automatically after each battle
  766. //
  767. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  768. {
  769. w = gpGlobals->rgParty[i].wPlayerRole;
  770. #if 1//def PAL_CLASSIC
  771. gpGlobals->g.PlayerRoles.rgwHP[w] +=
  772. (gpGlobals->g.PlayerRoles.rgwMaxHP[w] - gpGlobals->g.PlayerRoles.rgwHP[w]) / 2;
  773. gpGlobals->g.PlayerRoles.rgwMP[w] +=
  774. (gpGlobals->g.PlayerRoles.rgwMaxMP[w] - gpGlobals->g.PlayerRoles.rgwMP[w]) / 2;
  775. #else
  776. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  777. {
  778. gpGlobals->g.PlayerRoles.rgwHP[w] = 1;
  779. }
  780. else if (g_Battle.iExpGained > 0)
  781. {
  782. FLOAT f =
  783. (gpGlobals->g.rgLevelUpExp[gpGlobals->g.PlayerRoles.rgwLevel[w]] / 5.0f) / g_Battle.iExpGained;
  784. if (f < 2)
  785. {
  786. f = 2;
  787. }
  788. gpGlobals->g.PlayerRoles.rgwHP[w] +=
  789. (gpGlobals->g.PlayerRoles.rgwMaxHP[w] - gpGlobals->g.PlayerRoles.rgwHP[w]) / f;
  790. gpGlobals->g.PlayerRoles.rgwMP[w] +=
  791. (gpGlobals->g.PlayerRoles.rgwMaxMP[w] - gpGlobals->g.PlayerRoles.rgwMP[w]) / f / 1.2;
  792. }
  793. #endif
  794. }
  795. }
  796. VOID
  797. PAL_BattleEnemyEscape(
  798. VOID
  799. )
  800. /*++
  801. Purpose:
  802. Enemy flee the battle.
  803. Parameters:
  804. None.
  805. Return value:
  806. None.
  807. --*/
  808. {
  809. int j, x, y, w;
  810. BOOL f = TRUE;
  811. AUDIO_PlaySound(45);
  812. //
  813. // Show the animation
  814. //
  815. while (f)
  816. {
  817. f = FALSE;
  818. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  819. {
  820. if (g_Battle.rgEnemy[j].wObjectID == 0)
  821. {
  822. continue;
  823. }
  824. x = PAL_X(g_Battle.rgEnemy[j].pos) - 5;
  825. y = PAL_Y(g_Battle.rgEnemy[j].pos);
  826. g_Battle.rgEnemy[j].pos = PAL_XY(x, y);
  827. w = PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[j].lpSprite, 0));
  828. if (x + w > 0)
  829. {
  830. f = TRUE;
  831. }
  832. }
  833. PAL_BattleMakeScene();
  834. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  835. VIDEO_UpdateScreen(NULL);
  836. UTIL_Delay(10);
  837. }
  838. UTIL_Delay(500);
  839. g_Battle.BattleResult = kBattleResultTerminated;
  840. }
  841. VOID
  842. PAL_BattlePlayerEscape(
  843. VOID
  844. )
  845. /*++
  846. Purpose:
  847. Player flee the battle.
  848. Parameters:
  849. None.
  850. Return value:
  851. None.
  852. --*/
  853. {
  854. int i, j;
  855. WORD wPlayerRole;
  856. AUDIO_PlaySound(45);
  857. PAL_BattleUpdateFighters();
  858. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  859. {
  860. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  861. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  862. {
  863. g_Battle.rgPlayer[i].wCurrentFrame = 0;
  864. }
  865. }
  866. for (i = 0; i < 16; i++)
  867. {
  868. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  869. {
  870. wPlayerRole = gpGlobals->rgParty[j].wPlayerRole;
  871. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  872. {
  873. //
  874. // TODO: This is still not the same as the original game
  875. //
  876. switch (j)
  877. {
  878. case 0:
  879. if (gpGlobals->wMaxPartyMemberIndex > 0)
  880. {
  881. g_Battle.rgPlayer[j].pos =
  882. PAL_XY(PAL_X(g_Battle.rgPlayer[j].pos) + 4,
  883. PAL_Y(g_Battle.rgPlayer[j].pos) + 6);
  884. break;
  885. }
  886. case 1:
  887. g_Battle.rgPlayer[j].pos =
  888. PAL_XY(PAL_X(g_Battle.rgPlayer[j].pos) + 4,
  889. PAL_Y(g_Battle.rgPlayer[j].pos) + 4);
  890. break;
  891. case 2:
  892. g_Battle.rgPlayer[j].pos =
  893. PAL_XY(PAL_X(g_Battle.rgPlayer[j].pos) + 6,
  894. PAL_Y(g_Battle.rgPlayer[j].pos) + 3);
  895. break;
  896. default:
  897. assert(FALSE); // Not possible
  898. break;
  899. }
  900. }
  901. }
  902. PAL_BattleDelay(1, 0, FALSE);
  903. }
  904. //
  905. // Remove all players from the screen
  906. //
  907. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  908. {
  909. g_Battle.rgPlayer[i].pos = PAL_XY(9999, 9999);
  910. }
  911. PAL_BattleDelay(1, 0, FALSE);
  912. g_Battle.BattleResult = kBattleResultFleed;
  913. }
  914. BATTLERESULT
  915. PAL_StartBattle(
  916. WORD wEnemyTeam,
  917. BOOL fIsBoss
  918. )
  919. /*++
  920. Purpose:
  921. Start a battle.
  922. Parameters:
  923. [IN] wEnemyTeam - the number of the enemy team.
  924. [IN] fIsBoss - TRUE for boss fight (not allowed to flee).
  925. Return value:
  926. The result of the battle.
  927. --*/
  928. {
  929. int i;
  930. WORD w, wPrevWaveLevel;
  931. SHORT sPrevWaveProgression;
  932. //
  933. // Set the screen waving effects
  934. //
  935. wPrevWaveLevel = gpGlobals->wScreenWave;
  936. sPrevWaveProgression = gpGlobals->sWaveProgression;
  937. gpGlobals->sWaveProgression = 0;
  938. gpGlobals->wScreenWave = gpGlobals->g.lprgBattleField[gpGlobals->wNumBattleField].wScreenWave;
  939. //
  940. // Make sure everyone in the party is alive, also clear all hidden
  941. // EXP count records
  942. //
  943. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  944. {
  945. w = gpGlobals->rgParty[i].wPlayerRole;
  946. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  947. {
  948. gpGlobals->g.PlayerRoles.rgwHP[w] = 1;
  949. gpGlobals->rgPlayerStatus[w][kStatusPuppet] = 0;
  950. }
  951. gpGlobals->Exp.rgHealthExp[w].wCount = 0;
  952. gpGlobals->Exp.rgMagicExp[w].wCount = 0;
  953. gpGlobals->Exp.rgAttackExp[w].wCount = 0;
  954. gpGlobals->Exp.rgMagicPowerExp[w].wCount = 0;
  955. gpGlobals->Exp.rgDefenseExp[w].wCount = 0;
  956. gpGlobals->Exp.rgDexterityExp[w].wCount = 0;
  957. gpGlobals->Exp.rgFleeExp[w].wCount = 0;
  958. }
  959. //
  960. // Clear all item-using records
  961. //
  962. for (i = 0; i < MAX_INVENTORY; i++)
  963. {
  964. gpGlobals->rgInventory[i].nAmountInUse = 0;
  965. }
  966. //
  967. // Store all enemies
  968. //
  969. for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
  970. {
  971. memset(&(g_Battle.rgEnemy[i]), 0, sizeof(BATTLEENEMY));
  972. w = gpGlobals->g.lprgEnemyTeam[wEnemyTeam].rgwEnemy[i];
  973. if (w == 0xFFFF)
  974. {
  975. break;
  976. }
  977. if (w != 0)
  978. {
  979. g_Battle.rgEnemy[i].e = gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[w].enemy.wEnemyID];
  980. g_Battle.rgEnemy[i].wObjectID = w;
  981. g_Battle.rgEnemy[i].state = kFighterWait;
  982. g_Battle.rgEnemy[i].wScriptOnTurnStart = gpGlobals->g.rgObject[w].enemy.wScriptOnTurnStart;
  983. g_Battle.rgEnemy[i].wScriptOnBattleEnd = gpGlobals->g.rgObject[w].enemy.wScriptOnBattleEnd;
  984. g_Battle.rgEnemy[i].wScriptOnReady = gpGlobals->g.rgObject[w].enemy.wScriptOnReady;
  985. g_Battle.rgEnemy[i].iColorShift = 0;
  986. #ifndef PAL_CLASSIC
  987. g_Battle.rgEnemy[i].flTimeMeter = 50;
  988. //
  989. // HACK: Otherwise the black thief lady will be too hard to beat
  990. //
  991. if (g_Battle.rgEnemy[i].e.wDexterity == 164)
  992. {
  993. g_Battle.rgEnemy[i].e.wDexterity /= ((gpGlobals->wMaxPartyMemberIndex == 0) ? 6 : 3);
  994. }
  995. //
  996. // HACK: Heal up automatically for final boss
  997. //
  998. if (g_Battle.rgEnemy[i].e.wHealth == 32760)
  999. {
  1000. for (w = 0; w < MAX_PLAYER_ROLES; w++)
  1001. {
  1002. gpGlobals->g.PlayerRoles.rgwHP[w] = gpGlobals->g.PlayerRoles.rgwMaxHP[w];
  1003. gpGlobals->g.PlayerRoles.rgwMP[w] = gpGlobals->g.PlayerRoles.rgwMaxMP[w];
  1004. }
  1005. }
  1006. //
  1007. // Yet another HACKs
  1008. //
  1009. if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -32)
  1010. {
  1011. g_Battle.rgEnemy[i].e.wDexterity = 0; // for Grandma Knife
  1012. }
  1013. else if (g_Battle.rgEnemy[i].e.wDexterity == 20)
  1014. {
  1015. //
  1016. // for Fox Demon
  1017. //
  1018. if (gpGlobals->g.PlayerRoles.rgwLevel[0] < 15)
  1019. {
  1020. g_Battle.rgEnemy[i].e.wDexterity = 8;
  1021. }
  1022. else if (gpGlobals->g.PlayerRoles.rgwLevel[4] > 28 ||
  1023. gpGlobals->Exp.rgPrimaryExp[4].wExp > 0)
  1024. {
  1025. g_Battle.rgEnemy[i].e.wDexterity = 60;
  1026. }
  1027. }
  1028. else if (g_Battle.rgEnemy[i].e.wExp == 250 &&
  1029. g_Battle.rgEnemy[i].e.wCash == 1100)
  1030. {
  1031. g_Battle.rgEnemy[i].e.wDexterity += 12; // for Snake Demon
  1032. }
  1033. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -60)
  1034. {
  1035. g_Battle.rgEnemy[i].e.wDexterity = 15; // for Spider
  1036. }
  1037. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -30)
  1038. {
  1039. g_Battle.rgEnemy[i].e.wDexterity = (WORD)-10; // for Stone Head
  1040. }
  1041. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -16)
  1042. {
  1043. g_Battle.rgEnemy[i].e.wDexterity = 0; // for Zombie
  1044. }
  1045. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -20)
  1046. {
  1047. g_Battle.rgEnemy[i].e.wDexterity = -8; // for Flower Demon
  1048. }
  1049. else if (g_Battle.rgEnemy[i].e.wLevel < 20 &&
  1050. gpGlobals->wNumScene >= 0xD8 && gpGlobals->wNumScene <= 0xE2)
  1051. {
  1052. //
  1053. // for low-level monsters in the Cave of Trial
  1054. //
  1055. g_Battle.rgEnemy[i].e.wLevel += 15;
  1056. g_Battle.rgEnemy[i].e.wDexterity += 25;
  1057. }
  1058. else if (gpGlobals->wNumScene == 0x90)
  1059. {
  1060. g_Battle.rgEnemy[i].e.wDexterity += 25; // for Tower Dragons
  1061. }
  1062. else if (g_Battle.rgEnemy[i].e.wLevel == 2 &&
  1063. g_Battle.rgEnemy[i].e.wCash == 48)
  1064. {
  1065. g_Battle.rgEnemy[i].e.wDexterity += 8; // for Miao Fists
  1066. }
  1067. else if (g_Battle.rgEnemy[i].e.wLevel == 4 &&
  1068. g_Battle.rgEnemy[i].e.wCash == 240)
  1069. {
  1070. g_Battle.rgEnemy[i].e.wDexterity += 18; // for Fat Miao
  1071. }
  1072. else if (g_Battle.rgEnemy[i].e.wLevel == 16 &&
  1073. g_Battle.rgEnemy[i].e.wMagicRate == 4 &&
  1074. g_Battle.rgEnemy[i].e.wAttackEquivItemRate == 4)
  1075. {
  1076. g_Battle.rgEnemy[i].e.wDexterity += 50; // for Black Spider
  1077. }
  1078. #endif
  1079. }
  1080. }
  1081. g_Battle.wMaxEnemyIndex = i - 1;
  1082. //
  1083. // Store all players
  1084. //
  1085. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1086. {
  1087. g_Battle.rgPlayer[i].flTimeMeter = 15.0f;
  1088. #ifndef PAL_CLASSIC
  1089. g_Battle.rgPlayer[i].flTimeSpeedModifier = 2.0f;
  1090. g_Battle.rgPlayer[i].sTurnOrder = -1;
  1091. #endif
  1092. g_Battle.rgPlayer[i].wHidingTime = 0;
  1093. g_Battle.rgPlayer[i].state = kFighterWait;
  1094. g_Battle.rgPlayer[i].action.sTarget = -1;
  1095. g_Battle.rgPlayer[i].fDefending = FALSE;
  1096. g_Battle.rgPlayer[i].wCurrentFrame = 0;
  1097. g_Battle.rgPlayer[i].iColorShift = FALSE;
  1098. }
  1099. //
  1100. // Load sprites and background
  1101. //
  1102. PAL_LoadBattleSprites();
  1103. PAL_LoadBattleBackground();
  1104. //
  1105. // Create the surface for scene buffer
  1106. //
  1107. g_Battle.lpSceneBuf =
  1108. SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
  1109. gpScreen->format->Rmask, gpScreen->format->Gmask,
  1110. gpScreen->format->Bmask, gpScreen->format->Amask);
  1111. if (g_Battle.lpSceneBuf == NULL)
  1112. {
  1113. TerminateOnError("PAL_StartBattle(): creating surface for scene buffer failed!");
  1114. }
  1115. #if SDL_VERSION_ATLEAST(2, 0, 0)
  1116. SDL_SetSurfacePalette(g_Battle.lpSceneBuf, gpScreen->format->palette);
  1117. #else
  1118. SDL_SetPalette(g_Battle.lpSceneBuf, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  1119. #endif
  1120. PAL_UpdateEquipments();
  1121. g_Battle.iExpGained = 0;
  1122. g_Battle.iCashGained = 0;
  1123. g_Battle.fIsBoss = fIsBoss;
  1124. g_Battle.fEnemyCleared = FALSE;
  1125. g_Battle.fEnemyMoving = FALSE;
  1126. g_Battle.iHidingTime = 0;
  1127. g_Battle.wMovingPlayerIndex = 0;
  1128. g_Battle.UI.szMsg[0] = '\0';
  1129. g_Battle.UI.szNextMsg[0] = '\0';
  1130. g_Battle.UI.dwMsgShowTime = 0;
  1131. g_Battle.UI.state = kBattleUIWait;
  1132. g_Battle.UI.fAutoAttack = FALSE;
  1133. g_Battle.UI.wSelectedIndex = 0;
  1134. g_Battle.UI.wPrevEnemyTarget = 0;
  1135. memset(g_Battle.UI.rgShowNum, 0, sizeof(g_Battle.UI.rgShowNum));
  1136. g_Battle.lpSummonSprite = NULL;
  1137. g_Battle.sBackgroundColorShift = 0;
  1138. gpGlobals->fInBattle = TRUE;
  1139. g_Battle.BattleResult = kBattleResultPreBattle;
  1140. PAL_BattleUpdateFighters();
  1141. //
  1142. // Load the battle effect sprite.
  1143. //
  1144. i = PAL_MKFGetChunkSize(10, gpGlobals->f.fpDATA);
  1145. g_Battle.lpEffectSprite = UTIL_malloc(i);
  1146. PAL_MKFReadChunk(g_Battle.lpEffectSprite, i, 10, gpGlobals->f.fpDATA);
  1147. #ifdef PAL_CLASSIC
  1148. g_Battle.Phase = kBattlePhaseSelectAction;
  1149. g_Battle.fRepeat = FALSE;
  1150. g_Battle.fForce = FALSE;
  1151. g_Battle.fFlee = FALSE;
  1152. #endif
  1153. #ifdef PAL_ALLOW_KEYREPEAT
  1154. SDL_EnableKeyRepeat(120, 75);
  1155. #endif
  1156. //
  1157. // Run the main battle routine.
  1158. //
  1159. i = PAL_BattleMain();
  1160. #ifdef PAL_ALLOW_KEYREPEAT
  1161. SDL_EnableKeyRepeat(0, 0);
  1162. PAL_ClearKeyState();
  1163. g_InputState.prevdir = kDirUnknown;
  1164. #endif
  1165. if (i == kBattleResultWon)
  1166. {
  1167. //
  1168. // Player won the battle. Add the Experience points.
  1169. //
  1170. PAL_BattleWon();
  1171. }
  1172. //
  1173. // Clear all item-using records
  1174. //
  1175. for (w = 0; w < MAX_INVENTORY; w++)
  1176. {
  1177. gpGlobals->rgInventory[w].nAmountInUse = 0;
  1178. }
  1179. //
  1180. // Clear all player status, poisons and temporary effects
  1181. //
  1182. PAL_ClearAllPlayerStatus();
  1183. for (w = 0; w < MAX_PLAYER_ROLES; w++)
  1184. {
  1185. PAL_CurePoisonByLevel(w, 3);
  1186. PAL_RemoveEquipmentEffect(w, kBodyPartExtra);
  1187. }
  1188. //
  1189. // Free all the battle sprites
  1190. //
  1191. PAL_FreeBattleSprites();
  1192. free(g_Battle.lpEffectSprite);
  1193. //
  1194. // Free the surfaces for the background picture and scene buffer
  1195. //
  1196. SDL_FreeSurface(g_Battle.lpBackground);
  1197. SDL_FreeSurface(g_Battle.lpSceneBuf);
  1198. g_Battle.lpBackground = NULL;
  1199. g_Battle.lpSceneBuf = NULL;
  1200. gpGlobals->fInBattle = FALSE;
  1201. AUDIO_PlayMusic(gpGlobals->wNumMusic, TRUE, 1);
  1202. //
  1203. // Restore the screen waving effects
  1204. //
  1205. gpGlobals->sWaveProgression = sPrevWaveProgression;
  1206. gpGlobals->wScreenWave = wPrevWaveLevel;
  1207. return i;
  1208. }