battle.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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. const 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. if (fLevelUp)
  567. {
  568. //
  569. // Player has gained a level. Show the message
  570. //
  571. PAL_CreateSingleLineBox(PAL_XY(80, 0), 10, FALSE);
  572. PAL_CreateBox(PAL_XY(82, 32), 7, 8, 1, FALSE);
  573. PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]), PAL_XY(110, 10), 0, FALSE, FALSE, FALSE);
  574. PAL_DrawText(PAL_GetWord(STATUS_LABEL_LEVEL), PAL_XY(110 + 16 * 3, 10), 0, FALSE, FALSE, FALSE);
  575. PAL_DrawText(PAL_GetWord(BATTLEWIN_LEVELUP_LABEL), PAL_XY(110 + 16 * 5, 10), 0, FALSE, FALSE, FALSE);
  576. for (j = 0; j < 8; j++)
  577. {
  578. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_ARROW),
  579. gpScreen, PAL_XY(183, 48 + 18 * j));
  580. }
  581. PAL_DrawText(PAL_GetWord(STATUS_LABEL_LEVEL), PAL_XY(100, 44), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  582. PAL_DrawText(PAL_GetWord(STATUS_LABEL_HP), PAL_XY(100, 62), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  583. PAL_DrawText(PAL_GetWord(STATUS_LABEL_MP), PAL_XY(100, 80), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  584. PAL_DrawText(PAL_GetWord(STATUS_LABEL_ATTACKPOWER), PAL_XY(100, 98), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  585. PAL_DrawText(PAL_GetWord(STATUS_LABEL_MAGICPOWER), PAL_XY(100, 116), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  586. PAL_DrawText(PAL_GetWord(STATUS_LABEL_RESISTANCE), PAL_XY(100, 134), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  587. PAL_DrawText(PAL_GetWord(STATUS_LABEL_DEXTERITY), PAL_XY(100, 152), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  588. PAL_DrawText(PAL_GetWord(STATUS_LABEL_FLEERATE), PAL_XY(100, 170), BATTLEWIN_LEVELUP_LABEL_COLOR, TRUE, FALSE, FALSE);
  589. //
  590. // Draw the original stats and stats after level up
  591. //
  592. PAL_DrawNumber(OrigPlayerRoles.rgwLevel[w], 4, PAL_XY(133, 47),
  593. kNumColorYellow, kNumAlignRight);
  594. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwLevel[w], 4, PAL_XY(195, 47),
  595. kNumColorYellow, kNumAlignRight);
  596. PAL_DrawNumber(OrigPlayerRoles.rgwHP[w], 4, PAL_XY(133, 64),
  597. kNumColorYellow, kNumAlignRight);
  598. PAL_DrawNumber(OrigPlayerRoles.rgwMaxHP[w], 4, PAL_XY(154, 68),
  599. kNumColorBlue, kNumAlignRight);
  600. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  601. PAL_XY(156, 66));
  602. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwHP[w], 4, PAL_XY(195, 64),
  603. kNumColorYellow, kNumAlignRight);
  604. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxHP[w], 4, PAL_XY(216, 68),
  605. kNumColorBlue, kNumAlignRight);
  606. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  607. PAL_XY(218, 66));
  608. PAL_DrawNumber(OrigPlayerRoles.rgwMP[w], 4, PAL_XY(133, 82),
  609. kNumColorYellow, kNumAlignRight);
  610. PAL_DrawNumber(OrigPlayerRoles.rgwMaxMP[w], 4, PAL_XY(154, 86),
  611. kNumColorBlue, kNumAlignRight);
  612. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  613. PAL_XY(156, 84));
  614. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMP[w], 4, PAL_XY(195, 82),
  615. kNumColorYellow, kNumAlignRight);
  616. PAL_DrawNumber(gpGlobals->g.PlayerRoles.rgwMaxMP[w], 4, PAL_XY(216, 86),
  617. kNumColorBlue, kNumAlignRight);
  618. PAL_RLEBlitToSurface(PAL_SpriteGetFrame(gpSpriteUI, SPRITENUM_SLASH), gpScreen,
  619. PAL_XY(218, 84));
  620. PAL_DrawNumber(OrigPlayerRoles.rgwAttackStrength[w] + PAL_GetPlayerAttackStrength(w) -
  621. gpGlobals->g.PlayerRoles.rgwAttackStrength[w],
  622. 4, PAL_XY(133, 101), kNumColorYellow, kNumAlignRight);
  623. PAL_DrawNumber(PAL_GetPlayerAttackStrength(w), 4, PAL_XY(195, 101),
  624. kNumColorYellow, kNumAlignRight);
  625. PAL_DrawNumber(OrigPlayerRoles.rgwMagicStrength[w] + PAL_GetPlayerMagicStrength(w) -
  626. gpGlobals->g.PlayerRoles.rgwMagicStrength[w],
  627. 4, PAL_XY(133, 119), kNumColorYellow, kNumAlignRight);
  628. PAL_DrawNumber(PAL_GetPlayerMagicStrength(w), 4, PAL_XY(195, 119),
  629. kNumColorYellow, kNumAlignRight);
  630. PAL_DrawNumber(OrigPlayerRoles.rgwDefense[w] + PAL_GetPlayerDefense(w) -
  631. gpGlobals->g.PlayerRoles.rgwDefense[w],
  632. 4, PAL_XY(133, 137), kNumColorYellow, kNumAlignRight);
  633. PAL_DrawNumber(PAL_GetPlayerDefense(w), 4, PAL_XY(195, 137),
  634. kNumColorYellow, kNumAlignRight);
  635. PAL_DrawNumber(OrigPlayerRoles.rgwDexterity[w] + PAL_GetPlayerDexterity(w) -
  636. gpGlobals->g.PlayerRoles.rgwDexterity[w],
  637. 4, PAL_XY(133, 155), kNumColorYellow, kNumAlignRight);
  638. PAL_DrawNumber(PAL_GetPlayerDexterity(w), 4, PAL_XY(195, 155),
  639. kNumColorYellow, kNumAlignRight);
  640. PAL_DrawNumber(OrigPlayerRoles.rgwFleeRate[w] + PAL_GetPlayerFleeRate(w) -
  641. gpGlobals->g.PlayerRoles.rgwFleeRate[w],
  642. 4, PAL_XY(133, 173), kNumColorYellow, kNumAlignRight);
  643. PAL_DrawNumber(PAL_GetPlayerFleeRate(w), 4, PAL_XY(195, 173),
  644. kNumColorYellow, kNumAlignRight);
  645. //
  646. // Update the screen and wait for key
  647. //
  648. VIDEO_UpdateScreen(&rect1);
  649. PAL_WaitForKey(3000);
  650. OrigPlayerRoles = gpGlobals->g.PlayerRoles;
  651. }
  652. //
  653. // Increasing of other hidden levels
  654. //
  655. iTotalCount = 0;
  656. iTotalCount += gpGlobals->Exp.rgAttackExp[w].wCount;
  657. iTotalCount += gpGlobals->Exp.rgDefenseExp[w].wCount;
  658. iTotalCount += gpGlobals->Exp.rgDexterityExp[w].wCount;
  659. iTotalCount += gpGlobals->Exp.rgFleeExp[w].wCount;
  660. iTotalCount += gpGlobals->Exp.rgHealthExp[w].wCount;
  661. iTotalCount += gpGlobals->Exp.rgMagicExp[w].wCount;
  662. iTotalCount += gpGlobals->Exp.rgMagicPowerExp[w].wCount;
  663. if (iTotalCount > 0)
  664. {
  665. #define CHECK_HIDDEN_EXP(expname, statname, label) \
  666. { \
  667. dwExp = g_Battle.iExpGained; \
  668. dwExp *= gpGlobals->Exp.expname[w].wCount; \
  669. dwExp /= iTotalCount; \
  670. dwExp *= 2; \
  671. \
  672. dwExp += gpGlobals->Exp.expname[w].wExp; \
  673. \
  674. if (gpGlobals->Exp.expname[w].wLevel > MAX_LEVELS) \
  675. { \
  676. gpGlobals->Exp.expname[w].wLevel = MAX_LEVELS; \
  677. } \
  678. \
  679. while (dwExp >= gpGlobals->g.rgLevelUpExp[gpGlobals->Exp.expname[w].wLevel]) \
  680. { \
  681. dwExp -= gpGlobals->g.rgLevelUpExp[gpGlobals->Exp.expname[w].wLevel]; \
  682. gpGlobals->g.PlayerRoles.statname[w] += RandomLong(1, 2); \
  683. if (gpGlobals->Exp.expname[w].wLevel < MAX_LEVELS) \
  684. { \
  685. gpGlobals->Exp.expname[w].wLevel++; \
  686. } \
  687. } \
  688. \
  689. gpGlobals->Exp.expname[w].wExp = (WORD)dwExp; \
  690. \
  691. if (gpGlobals->g.PlayerRoles.statname[w] != OrigPlayerRoles.statname[w]) \
  692. { \
  693. PAL_CreateSingleLineBox(PAL_XY(83, 60), 8, FALSE); \
  694. PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]), PAL_XY(95, 70), 0, FALSE, FALSE, FALSE); \
  695. PAL_DrawText(PAL_GetWord(label), PAL_XY(143, 70), 0, FALSE, FALSE, FALSE); \
  696. PAL_DrawText(PAL_GetWord(BATTLEWIN_LEVELUP_LABEL), PAL_XY(175, 70), 0, FALSE, FALSE, FALSE); \
  697. PAL_DrawNumber(gpGlobals->g.PlayerRoles.statname[w] - OrigPlayerRoles.statname[w], 5, PAL_XY(188, 74), kNumColorYellow, kNumAlignRight); \
  698. VIDEO_UpdateScreen(&rect); \
  699. PAL_WaitForKey(3000); \
  700. } \
  701. }
  702. CHECK_HIDDEN_EXP(rgHealthExp, rgwMaxHP, STATUS_LABEL_HP);
  703. CHECK_HIDDEN_EXP(rgMagicExp, rgwMaxMP, STATUS_LABEL_MP);
  704. CHECK_HIDDEN_EXP(rgAttackExp, rgwAttackStrength, STATUS_LABEL_ATTACKPOWER);
  705. CHECK_HIDDEN_EXP(rgMagicPowerExp, rgwMagicStrength, STATUS_LABEL_MAGICPOWER);
  706. CHECK_HIDDEN_EXP(rgDefenseExp, rgwDefense, STATUS_LABEL_RESISTANCE);
  707. CHECK_HIDDEN_EXP(rgDexterityExp, rgwDexterity, STATUS_LABEL_DEXTERITY);
  708. CHECK_HIDDEN_EXP(rgFleeExp, rgwFleeRate, STATUS_LABEL_FLEERATE);
  709. #undef CHECK_HIDDEN_EXP
  710. }
  711. //
  712. // Learn all magics at the current level
  713. //
  714. j = 0;
  715. while (j < gpGlobals->g.nLevelUpMagic)
  716. {
  717. if (gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic == 0 ||
  718. gpGlobals->g.lprgLevelUpMagic[j].m[w].wLevel > gpGlobals->g.PlayerRoles.rgwLevel[w])
  719. {
  720. j++;
  721. continue;
  722. }
  723. if (PAL_AddMagic(w, gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic))
  724. {
  725. int ww;
  726. int w1 = (ww = PAL_WordWidth(gpGlobals->g.PlayerRoles.rgwName[w])) > 3 ? ww : 3;
  727. int w2 = (ww = PAL_WordWidth(BATTLEWIN_ADDMAGIC_LABEL)) > 2 ? ww : 2;
  728. int w3 = (ww = PAL_WordWidth(gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic)) > 5 ? ww : 5;
  729. ww = (w1 + w2 + w3 - 10) << 3;
  730. PAL_CreateSingleLineBox(PAL_XY(65 - ww, 105), w1 + w2 + w3, FALSE);
  731. PAL_DrawText(PAL_GetWord(gpGlobals->g.PlayerRoles.rgwName[w]), PAL_XY(75 - ww, 115), 0, FALSE, FALSE, FALSE);
  732. PAL_DrawText(PAL_GetWord(BATTLEWIN_ADDMAGIC_LABEL), PAL_XY(75 + 16 * w1 - ww, 115), 0, FALSE, FALSE, FALSE);
  733. PAL_DrawText(PAL_GetWord(gpGlobals->g.lprgLevelUpMagic[j].m[w].wMagic), PAL_XY(75 + 16 * (w1 + w2) - ww, 115), 0x1B, FALSE, FALSE, FALSE);
  734. VIDEO_UpdateScreen(&rect);
  735. PAL_WaitForKey(3000);
  736. }
  737. j++;
  738. }
  739. }
  740. //
  741. // Run the post-battle scripts
  742. //
  743. for (i = 0; i <= g_Battle.wMaxEnemyIndex; i++)
  744. {
  745. PAL_RunTriggerScript(g_Battle.rgEnemy[i].wScriptOnBattleEnd, i);
  746. }
  747. //
  748. // Recover automatically after each battle
  749. //
  750. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  751. {
  752. w = gpGlobals->rgParty[i].wPlayerRole;
  753. #if 1//def PAL_CLASSIC
  754. gpGlobals->g.PlayerRoles.rgwHP[w] +=
  755. (gpGlobals->g.PlayerRoles.rgwMaxHP[w] - gpGlobals->g.PlayerRoles.rgwHP[w]) / 2;
  756. gpGlobals->g.PlayerRoles.rgwMP[w] +=
  757. (gpGlobals->g.PlayerRoles.rgwMaxMP[w] - gpGlobals->g.PlayerRoles.rgwMP[w]) / 2;
  758. #else
  759. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  760. {
  761. gpGlobals->g.PlayerRoles.rgwHP[w] = 1;
  762. }
  763. else if (g_Battle.iExpGained > 0)
  764. {
  765. FLOAT f =
  766. (gpGlobals->g.rgLevelUpExp[gpGlobals->g.PlayerRoles.rgwLevel[w]] / 5.0f) / g_Battle.iExpGained;
  767. if (f < 2)
  768. {
  769. f = 2;
  770. }
  771. gpGlobals->g.PlayerRoles.rgwHP[w] +=
  772. (gpGlobals->g.PlayerRoles.rgwMaxHP[w] - gpGlobals->g.PlayerRoles.rgwHP[w]) / f;
  773. gpGlobals->g.PlayerRoles.rgwMP[w] +=
  774. (gpGlobals->g.PlayerRoles.rgwMaxMP[w] - gpGlobals->g.PlayerRoles.rgwMP[w]) / f / 1.2;
  775. }
  776. #endif
  777. }
  778. }
  779. VOID
  780. PAL_BattleEnemyEscape(
  781. VOID
  782. )
  783. /*++
  784. Purpose:
  785. Enemy flee the battle.
  786. Parameters:
  787. None.
  788. Return value:
  789. None.
  790. --*/
  791. {
  792. int j, x, y, w;
  793. BOOL f = TRUE;
  794. AUDIO_PlaySound(45);
  795. //
  796. // Show the animation
  797. //
  798. while (f)
  799. {
  800. f = FALSE;
  801. for (j = 0; j <= g_Battle.wMaxEnemyIndex; j++)
  802. {
  803. if (g_Battle.rgEnemy[j].wObjectID == 0)
  804. {
  805. continue;
  806. }
  807. x = PAL_X(g_Battle.rgEnemy[j].pos) - 5;
  808. y = PAL_Y(g_Battle.rgEnemy[j].pos);
  809. g_Battle.rgEnemy[j].pos = PAL_XY(x, y);
  810. w = PAL_RLEGetWidth(PAL_SpriteGetFrame(g_Battle.rgEnemy[j].lpSprite, 0));
  811. if (x + w > 0)
  812. {
  813. f = TRUE;
  814. }
  815. }
  816. PAL_BattleMakeScene();
  817. SDL_BlitSurface(g_Battle.lpSceneBuf, NULL, gpScreen, NULL);
  818. VIDEO_UpdateScreen(NULL);
  819. UTIL_Delay(10);
  820. }
  821. UTIL_Delay(500);
  822. g_Battle.BattleResult = kBattleResultTerminated;
  823. }
  824. VOID
  825. PAL_BattlePlayerEscape(
  826. VOID
  827. )
  828. /*++
  829. Purpose:
  830. Player flee the battle.
  831. Parameters:
  832. None.
  833. Return value:
  834. None.
  835. --*/
  836. {
  837. int i, j;
  838. WORD wPlayerRole;
  839. AUDIO_PlaySound(45);
  840. PAL_BattleUpdateFighters();
  841. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  842. {
  843. wPlayerRole = gpGlobals->rgParty[i].wPlayerRole;
  844. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  845. {
  846. g_Battle.rgPlayer[i].wCurrentFrame = 0;
  847. }
  848. }
  849. for (i = 0; i < 16; i++)
  850. {
  851. for (j = 0; j <= gpGlobals->wMaxPartyMemberIndex; j++)
  852. {
  853. wPlayerRole = gpGlobals->rgParty[j].wPlayerRole;
  854. if (gpGlobals->g.PlayerRoles.rgwHP[wPlayerRole] > 0)
  855. {
  856. //
  857. // TODO: This is still not the same as the original game
  858. //
  859. switch (j)
  860. {
  861. case 0:
  862. if (gpGlobals->wMaxPartyMemberIndex > 0)
  863. {
  864. g_Battle.rgPlayer[j].pos =
  865. PAL_XY(PAL_X(g_Battle.rgPlayer[j].pos) + 4,
  866. PAL_Y(g_Battle.rgPlayer[j].pos) + 6);
  867. break;
  868. }
  869. case 1:
  870. g_Battle.rgPlayer[j].pos =
  871. PAL_XY(PAL_X(g_Battle.rgPlayer[j].pos) + 4,
  872. PAL_Y(g_Battle.rgPlayer[j].pos) + 4);
  873. break;
  874. case 2:
  875. g_Battle.rgPlayer[j].pos =
  876. PAL_XY(PAL_X(g_Battle.rgPlayer[j].pos) + 6,
  877. PAL_Y(g_Battle.rgPlayer[j].pos) + 3);
  878. break;
  879. default:
  880. assert(FALSE); // Not possible
  881. break;
  882. }
  883. }
  884. }
  885. PAL_BattleDelay(1, 0, FALSE);
  886. }
  887. //
  888. // Remove all players from the screen
  889. //
  890. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  891. {
  892. g_Battle.rgPlayer[i].pos = PAL_XY(9999, 9999);
  893. }
  894. PAL_BattleDelay(1, 0, FALSE);
  895. g_Battle.BattleResult = kBattleResultFleed;
  896. }
  897. BATTLERESULT
  898. PAL_StartBattle(
  899. WORD wEnemyTeam,
  900. BOOL fIsBoss
  901. )
  902. /*++
  903. Purpose:
  904. Start a battle.
  905. Parameters:
  906. [IN] wEnemyTeam - the number of the enemy team.
  907. [IN] fIsBoss - TRUE for boss fight (not allowed to flee).
  908. Return value:
  909. The result of the battle.
  910. --*/
  911. {
  912. int i;
  913. WORD w, wPrevWaveLevel;
  914. SHORT sPrevWaveProgression;
  915. //
  916. // Set the screen waving effects
  917. //
  918. wPrevWaveLevel = gpGlobals->wScreenWave;
  919. sPrevWaveProgression = gpGlobals->sWaveProgression;
  920. gpGlobals->sWaveProgression = 0;
  921. gpGlobals->wScreenWave = gpGlobals->g.lprgBattleField[gpGlobals->wNumBattleField].wScreenWave;
  922. //
  923. // Make sure everyone in the party is alive, also clear all hidden
  924. // EXP count records
  925. //
  926. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  927. {
  928. w = gpGlobals->rgParty[i].wPlayerRole;
  929. if (gpGlobals->g.PlayerRoles.rgwHP[w] == 0)
  930. {
  931. gpGlobals->g.PlayerRoles.rgwHP[w] = 1;
  932. gpGlobals->rgPlayerStatus[w][kStatusPuppet] = 0;
  933. }
  934. gpGlobals->Exp.rgHealthExp[w].wCount = 0;
  935. gpGlobals->Exp.rgMagicExp[w].wCount = 0;
  936. gpGlobals->Exp.rgAttackExp[w].wCount = 0;
  937. gpGlobals->Exp.rgMagicPowerExp[w].wCount = 0;
  938. gpGlobals->Exp.rgDefenseExp[w].wCount = 0;
  939. gpGlobals->Exp.rgDexterityExp[w].wCount = 0;
  940. gpGlobals->Exp.rgFleeExp[w].wCount = 0;
  941. }
  942. //
  943. // Clear all item-using records
  944. //
  945. for (i = 0; i < MAX_INVENTORY; i++)
  946. {
  947. gpGlobals->rgInventory[i].nAmountInUse = 0;
  948. }
  949. //
  950. // Store all enemies
  951. //
  952. for (i = 0; i < MAX_ENEMIES_IN_TEAM; i++)
  953. {
  954. memset(&(g_Battle.rgEnemy[i]), 0, sizeof(BATTLEENEMY));
  955. w = gpGlobals->g.lprgEnemyTeam[wEnemyTeam].rgwEnemy[i];
  956. if (w == 0xFFFF)
  957. {
  958. break;
  959. }
  960. if (w != 0)
  961. {
  962. g_Battle.rgEnemy[i].e = gpGlobals->g.lprgEnemy[gpGlobals->g.rgObject[w].enemy.wEnemyID];
  963. g_Battle.rgEnemy[i].wObjectID = w;
  964. g_Battle.rgEnemy[i].state = kFighterWait;
  965. g_Battle.rgEnemy[i].wScriptOnTurnStart = gpGlobals->g.rgObject[w].enemy.wScriptOnTurnStart;
  966. g_Battle.rgEnemy[i].wScriptOnBattleEnd = gpGlobals->g.rgObject[w].enemy.wScriptOnBattleEnd;
  967. g_Battle.rgEnemy[i].wScriptOnReady = gpGlobals->g.rgObject[w].enemy.wScriptOnReady;
  968. g_Battle.rgEnemy[i].iColorShift = 0;
  969. #ifndef PAL_CLASSIC
  970. g_Battle.rgEnemy[i].flTimeMeter = 50;
  971. //
  972. // HACK: Otherwise the black thief lady will be too hard to beat
  973. //
  974. if (g_Battle.rgEnemy[i].e.wDexterity == 164)
  975. {
  976. g_Battle.rgEnemy[i].e.wDexterity /= ((gpGlobals->wMaxPartyMemberIndex == 0) ? 6 : 3);
  977. }
  978. //
  979. // HACK: Heal up automatically for final boss
  980. //
  981. if (g_Battle.rgEnemy[i].e.wHealth == 32760)
  982. {
  983. for (w = 0; w < MAX_PLAYER_ROLES; w++)
  984. {
  985. gpGlobals->g.PlayerRoles.rgwHP[w] = gpGlobals->g.PlayerRoles.rgwMaxHP[w];
  986. gpGlobals->g.PlayerRoles.rgwMP[w] = gpGlobals->g.PlayerRoles.rgwMaxMP[w];
  987. }
  988. }
  989. //
  990. // Yet another HACKs
  991. //
  992. if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -32)
  993. {
  994. g_Battle.rgEnemy[i].e.wDexterity = 0; // for Grandma Knife
  995. }
  996. else if (g_Battle.rgEnemy[i].e.wDexterity == 20)
  997. {
  998. //
  999. // for Fox Demon
  1000. //
  1001. if (gpGlobals->g.PlayerRoles.rgwLevel[0] < 15)
  1002. {
  1003. g_Battle.rgEnemy[i].e.wDexterity = 8;
  1004. }
  1005. else if (gpGlobals->g.PlayerRoles.rgwLevel[4] > 28 ||
  1006. gpGlobals->Exp.rgPrimaryExp[4].wExp > 0)
  1007. {
  1008. g_Battle.rgEnemy[i].e.wDexterity = 60;
  1009. }
  1010. }
  1011. else if (g_Battle.rgEnemy[i].e.wExp == 250 &&
  1012. g_Battle.rgEnemy[i].e.wCash == 1100)
  1013. {
  1014. g_Battle.rgEnemy[i].e.wDexterity += 12; // for Snake Demon
  1015. }
  1016. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -60)
  1017. {
  1018. g_Battle.rgEnemy[i].e.wDexterity = 15; // for Spider
  1019. }
  1020. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -30)
  1021. {
  1022. g_Battle.rgEnemy[i].e.wDexterity = (WORD)-10; // for Stone Head
  1023. }
  1024. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -16)
  1025. {
  1026. g_Battle.rgEnemy[i].e.wDexterity = 0; // for Zombie
  1027. }
  1028. else if ((SHORT)g_Battle.rgEnemy[i].e.wDexterity == -20)
  1029. {
  1030. g_Battle.rgEnemy[i].e.wDexterity = -8; // for Flower Demon
  1031. }
  1032. else if (g_Battle.rgEnemy[i].e.wLevel < 20 &&
  1033. gpGlobals->wNumScene >= 0xD8 && gpGlobals->wNumScene <= 0xE2)
  1034. {
  1035. //
  1036. // for low-level monsters in the Cave of Trial
  1037. //
  1038. g_Battle.rgEnemy[i].e.wLevel += 15;
  1039. g_Battle.rgEnemy[i].e.wDexterity += 25;
  1040. }
  1041. else if (gpGlobals->wNumScene == 0x90)
  1042. {
  1043. g_Battle.rgEnemy[i].e.wDexterity += 25; // for Tower Dragons
  1044. }
  1045. else if (g_Battle.rgEnemy[i].e.wLevel == 2 &&
  1046. g_Battle.rgEnemy[i].e.wCash == 48)
  1047. {
  1048. g_Battle.rgEnemy[i].e.wDexterity += 8; // for Miao Fists
  1049. }
  1050. else if (g_Battle.rgEnemy[i].e.wLevel == 4 &&
  1051. g_Battle.rgEnemy[i].e.wCash == 240)
  1052. {
  1053. g_Battle.rgEnemy[i].e.wDexterity += 18; // for Fat Miao
  1054. }
  1055. else if (g_Battle.rgEnemy[i].e.wLevel == 16 &&
  1056. g_Battle.rgEnemy[i].e.wMagicRate == 4 &&
  1057. g_Battle.rgEnemy[i].e.wAttackEquivItemRate == 4)
  1058. {
  1059. g_Battle.rgEnemy[i].e.wDexterity += 50; // for Black Spider
  1060. }
  1061. #endif
  1062. }
  1063. }
  1064. g_Battle.wMaxEnemyIndex = i - 1;
  1065. //
  1066. // Store all players
  1067. //
  1068. for (i = 0; i <= gpGlobals->wMaxPartyMemberIndex; i++)
  1069. {
  1070. g_Battle.rgPlayer[i].flTimeMeter = 15.0f;
  1071. #ifndef PAL_CLASSIC
  1072. g_Battle.rgPlayer[i].flTimeSpeedModifier = 2.0f;
  1073. g_Battle.rgPlayer[i].sTurnOrder = -1;
  1074. #endif
  1075. g_Battle.rgPlayer[i].wHidingTime = 0;
  1076. g_Battle.rgPlayer[i].state = kFighterWait;
  1077. g_Battle.rgPlayer[i].action.sTarget = -1;
  1078. g_Battle.rgPlayer[i].fDefending = FALSE;
  1079. g_Battle.rgPlayer[i].wCurrentFrame = 0;
  1080. g_Battle.rgPlayer[i].iColorShift = FALSE;
  1081. }
  1082. //
  1083. // Load sprites and background
  1084. //
  1085. PAL_LoadBattleSprites();
  1086. PAL_LoadBattleBackground();
  1087. //
  1088. // Create the surface for scene buffer
  1089. //
  1090. g_Battle.lpSceneBuf =
  1091. SDL_CreateRGBSurface(gpScreen->flags & ~SDL_HWSURFACE, 320, 200, 8,
  1092. gpScreen->format->Rmask, gpScreen->format->Gmask,
  1093. gpScreen->format->Bmask, gpScreen->format->Amask);
  1094. if (g_Battle.lpSceneBuf == NULL)
  1095. {
  1096. TerminateOnError("PAL_StartBattle(): creating surface for scene buffer failed!");
  1097. }
  1098. #if SDL_VERSION_ATLEAST(2, 0, 0)
  1099. SDL_SetSurfacePalette(g_Battle.lpSceneBuf, gpScreen->format->palette);
  1100. #else
  1101. SDL_SetPalette(g_Battle.lpSceneBuf, SDL_PHYSPAL | SDL_LOGPAL, VIDEO_GetPalette(), 0, 256);
  1102. #endif
  1103. PAL_UpdateEquipments();
  1104. g_Battle.iExpGained = 0;
  1105. g_Battle.iCashGained = 0;
  1106. g_Battle.fIsBoss = fIsBoss;
  1107. g_Battle.fEnemyCleared = FALSE;
  1108. g_Battle.fEnemyMoving = FALSE;
  1109. g_Battle.iHidingTime = 0;
  1110. g_Battle.wMovingPlayerIndex = 0;
  1111. g_Battle.UI.szMsg[0] = '\0';
  1112. g_Battle.UI.szNextMsg[0] = '\0';
  1113. g_Battle.UI.dwMsgShowTime = 0;
  1114. g_Battle.UI.state = kBattleUIWait;
  1115. g_Battle.UI.fAutoAttack = FALSE;
  1116. g_Battle.UI.wSelectedIndex = 0;
  1117. g_Battle.UI.wPrevEnemyTarget = 0;
  1118. memset(g_Battle.UI.rgShowNum, 0, sizeof(g_Battle.UI.rgShowNum));
  1119. g_Battle.lpSummonSprite = NULL;
  1120. g_Battle.sBackgroundColorShift = 0;
  1121. gpGlobals->fInBattle = TRUE;
  1122. g_Battle.BattleResult = kBattleResultPreBattle;
  1123. PAL_BattleUpdateFighters();
  1124. //
  1125. // Load the battle effect sprite.
  1126. //
  1127. i = PAL_MKFGetChunkSize(10, gpGlobals->f.fpDATA);
  1128. g_Battle.lpEffectSprite = UTIL_malloc(i);
  1129. PAL_MKFReadChunk(g_Battle.lpEffectSprite, i, 10, gpGlobals->f.fpDATA);
  1130. #ifdef PAL_CLASSIC
  1131. g_Battle.Phase = kBattlePhaseSelectAction;
  1132. g_Battle.fRepeat = FALSE;
  1133. g_Battle.fForce = FALSE;
  1134. g_Battle.fFlee = FALSE;
  1135. #endif
  1136. #ifdef PAL_ALLOW_KEYREPEAT
  1137. SDL_EnableKeyRepeat(120, 75);
  1138. #endif
  1139. //
  1140. // Run the main battle routine.
  1141. //
  1142. i = PAL_BattleMain();
  1143. #ifdef PAL_ALLOW_KEYREPEAT
  1144. SDL_EnableKeyRepeat(0, 0);
  1145. PAL_ClearKeyState();
  1146. g_InputState.prevdir = kDirUnknown;
  1147. #endif
  1148. if (i == kBattleResultWon)
  1149. {
  1150. //
  1151. // Player won the battle. Add the Experience points.
  1152. //
  1153. PAL_BattleWon();
  1154. }
  1155. //
  1156. // Clear all item-using records
  1157. //
  1158. for (w = 0; w < MAX_INVENTORY; w++)
  1159. {
  1160. gpGlobals->rgInventory[w].nAmountInUse = 0;
  1161. }
  1162. //
  1163. // Clear all player status, poisons and temporary effects
  1164. //
  1165. PAL_ClearAllPlayerStatus();
  1166. for (w = 0; w < MAX_PLAYER_ROLES; w++)
  1167. {
  1168. PAL_CurePoisonByLevel(w, 3);
  1169. PAL_RemoveEquipmentEffect(w, kBodyPartExtra);
  1170. }
  1171. //
  1172. // Free all the battle sprites
  1173. //
  1174. PAL_FreeBattleSprites();
  1175. free(g_Battle.lpEffectSprite);
  1176. //
  1177. // Free the surfaces for the background picture and scene buffer
  1178. //
  1179. SDL_FreeSurface(g_Battle.lpBackground);
  1180. SDL_FreeSurface(g_Battle.lpSceneBuf);
  1181. g_Battle.lpBackground = NULL;
  1182. g_Battle.lpSceneBuf = NULL;
  1183. gpGlobals->fInBattle = FALSE;
  1184. AUDIO_PlayMusic(gpGlobals->wNumMusic, TRUE, 1);
  1185. //
  1186. // Restore the screen waving effects
  1187. //
  1188. gpGlobals->sWaveProgression = sPrevWaveProgression;
  1189. gpGlobals->wScreenWave = wPrevWaveLevel;
  1190. return i;
  1191. }