play.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 3; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2008, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // All rights reserved.
  5. //
  6. // Portions based on PALx Project by palxex.
  7. // Copyright (c) 2006, Pal Lockheart <palxex@gmail.com>.
  8. //
  9. // This file is part of SDLPAL.
  10. //
  11. // SDLPAL is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. #include "main.h"
  25. VOID
  26. PAL_GameUpdate(
  27. BOOL fTrigger
  28. )
  29. /*++
  30. Purpose:
  31. The main game logic routine. Update the status of everything.
  32. Parameters:
  33. [IN] fTrigger - whether to process trigger events or not.
  34. Return value:
  35. None.
  36. --*/
  37. {
  38. WORD wEventObjectID, wDir;
  39. int i;
  40. LPEVENTOBJECT p;
  41. //
  42. // Check for trigger events
  43. //
  44. if (fTrigger)
  45. {
  46. //
  47. // Check if we are entering a new scene
  48. //
  49. if (gpGlobals->fEnteringScene)
  50. {
  51. //
  52. // Run the script for entering the scene
  53. //
  54. gpGlobals->fEnteringScene = FALSE;
  55. i = gpGlobals->wNumScene - 1;
  56. gpGlobals->g.rgScene[i].wScriptOnEnter =
  57. PAL_RunTriggerScript(gpGlobals->g.rgScene[i].wScriptOnEnter, 0xFFFF);
  58. if (gpGlobals->fEnteringScene || gpGlobals->fGameStart)
  59. {
  60. //
  61. // Don't go further as we're switching to another scene
  62. //
  63. return;
  64. }
  65. PAL_ClearKeyState();
  66. PAL_MakeScene();
  67. }
  68. //
  69. // Update the vanish time for all event objects
  70. //
  71. for (wEventObjectID = 0; wEventObjectID < gpGlobals->g.nEventObject; wEventObjectID++)
  72. {
  73. p = &gpGlobals->g.lprgEventObject[wEventObjectID];
  74. if (p->sVanishTime != 0)
  75. {
  76. p->sVanishTime += ((p->sVanishTime < 0) ? 1 : -1);
  77. }
  78. }
  79. //
  80. // Loop through all event objects in the current scene
  81. //
  82. for (wEventObjectID = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex + 1;
  83. wEventObjectID <= gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex;
  84. wEventObjectID++)
  85. {
  86. p = &gpGlobals->g.lprgEventObject[wEventObjectID - 1];
  87. if (p->sVanishTime != 0)
  88. {
  89. continue;
  90. }
  91. if (p->sState < 0)
  92. {
  93. if (p->x < PAL_X(gpGlobals->viewport) ||
  94. p->x > PAL_X(gpGlobals->viewport) + 320 ||
  95. p->y < PAL_Y(gpGlobals->viewport) ||
  96. p->y > PAL_Y(gpGlobals->viewport) + 320)
  97. {
  98. p->sState = abs(p->sState);
  99. p->wCurrentFrameNum = 0;
  100. }
  101. }
  102. else if (p->sState > 0 && p->wTriggerMode >= kTriggerTouchNear)
  103. {
  104. //
  105. // This event object can be triggered without manually exploring
  106. //
  107. if (abs(PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset) - p->x) +
  108. abs(PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset) - p->y) * 2 <
  109. (p->wTriggerMode - kTriggerTouchNear) * 32 + 16)
  110. {
  111. //
  112. // Player is in the trigger zone.
  113. //
  114. if (p->nSpriteFrames)
  115. {
  116. //
  117. // The sprite has multiple frames. Try to adjust the direction.
  118. //
  119. int xOffset, yOffset;
  120. p->wCurrentFrameNum = 0;
  121. xOffset = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset) - p->x;
  122. yOffset = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset) - p->y;
  123. if (xOffset > 0)
  124. {
  125. p->wDirection = ((yOffset > 0) ? kDirEast : kDirNorth);
  126. }
  127. else
  128. {
  129. p->wDirection = ((yOffset > 0) ? kDirSouth : kDirWest);
  130. }
  131. //
  132. // Redraw the scene
  133. //
  134. PAL_UpdatePartyGestures(FALSE);
  135. PAL_MakeScene();
  136. VIDEO_UpdateScreen(NULL);
  137. }
  138. //
  139. // Execute the script.
  140. //
  141. p->wTriggerScript = PAL_RunTriggerScript(p->wTriggerScript, wEventObjectID);
  142. PAL_ClearKeyState();
  143. if (gpGlobals->fEnteringScene || gpGlobals->fGameStart)
  144. {
  145. //
  146. // Don't go further on scene switching
  147. //
  148. return;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. //
  155. // Run autoscript for each event objects
  156. //
  157. for (wEventObjectID = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex + 1;
  158. wEventObjectID <= gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex;
  159. wEventObjectID++)
  160. {
  161. p = &gpGlobals->g.lprgEventObject[wEventObjectID - 1];
  162. if (p->sState > 0 && p->sVanishTime == 0)
  163. {
  164. WORD wScriptEntry = p->wAutoScript;
  165. if (wScriptEntry != 0)
  166. {
  167. p->wAutoScript = PAL_RunAutoScript(wScriptEntry, wEventObjectID);
  168. if (gpGlobals->fEnteringScene || gpGlobals->fGameStart)
  169. {
  170. //
  171. // Don't go further on scene switching
  172. //
  173. return;
  174. }
  175. }
  176. }
  177. //
  178. // Check if the player is in the way
  179. //
  180. if (fTrigger && p->sState >= kObjStateBlocker && p->wSpriteNum != 0 &&
  181. abs(p->x - PAL_X(gpGlobals->viewport) - PAL_X(gpGlobals->partyoffset)) +
  182. abs(p->y - PAL_Y(gpGlobals->viewport) - PAL_Y(gpGlobals->partyoffset)) * 2 <= 12)
  183. {
  184. //
  185. // Player is in the way, try to move a step
  186. //
  187. wDir = (p->wDirection + 1) % 4;
  188. for (i = 0; i < 4; i++)
  189. {
  190. int x, y;
  191. PAL_POS pos;
  192. x = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset);
  193. y = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset);
  194. x += ((wDir == kDirWest || wDir == kDirSouth) ? -16 : 16);
  195. y += ((wDir == kDirWest || wDir == kDirNorth) ? -8 : 8);
  196. pos = PAL_XY(x, y);
  197. if (!PAL_CheckObstacle(pos, TRUE, 0))
  198. {
  199. //
  200. // move here
  201. //
  202. gpGlobals->viewport = PAL_XY(
  203. PAL_X(pos) - PAL_X(gpGlobals->partyoffset),
  204. PAL_Y(pos) - PAL_Y(gpGlobals->partyoffset));
  205. break;
  206. }
  207. wDir = (wDir + 1) % 4;
  208. }
  209. }
  210. }
  211. gpGlobals->dwFrameNum++;
  212. }
  213. VOID
  214. PAL_GameUseItem(
  215. VOID
  216. )
  217. /*++
  218. Purpose:
  219. Allow player use an item in the game.
  220. Parameters:
  221. None.
  222. Return value:
  223. None.
  224. --*/
  225. {
  226. WORD wObject;
  227. while (TRUE)
  228. {
  229. wObject = PAL_ItemSelectMenu(NULL, kItemFlagUsable);
  230. if (wObject == 0)
  231. {
  232. return;
  233. }
  234. if (!(gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagApplyToAll))
  235. {
  236. //
  237. // Select the player to use the item on
  238. //
  239. WORD wPlayer = 0;
  240. while (TRUE)
  241. {
  242. wPlayer = PAL_ItemUseMenu(wObject);
  243. if (wPlayer == MENUITEM_VALUE_CANCELLED)
  244. {
  245. break;
  246. }
  247. //
  248. // Run the script
  249. //
  250. gpGlobals->g.rgObject[wObject].item.wScriptOnUse =
  251. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].item.wScriptOnUse, wPlayer);
  252. //
  253. // Remove the item if the item is consuming and the script succeeded
  254. //
  255. if ((gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagConsuming) &&
  256. g_fScriptSuccess)
  257. {
  258. PAL_AddItemToInventory(wObject, -1);
  259. }
  260. }
  261. }
  262. else
  263. {
  264. //
  265. // Run the script
  266. //
  267. gpGlobals->g.rgObject[wObject].item.wScriptOnUse =
  268. PAL_RunTriggerScript(gpGlobals->g.rgObject[wObject].item.wScriptOnUse, 0xFFFF);
  269. //
  270. // Remove the item if the item is consuming and the script succeeded
  271. //
  272. if ((gpGlobals->g.rgObject[wObject].item.wFlags & kItemFlagConsuming) &&
  273. g_fScriptSuccess)
  274. {
  275. PAL_AddItemToInventory(wObject, -1);
  276. }
  277. return;
  278. }
  279. }
  280. }
  281. VOID
  282. PAL_GameEquipItem(
  283. VOID
  284. )
  285. /*++
  286. Purpose:
  287. Allow player equip an item in the game.
  288. Parameters:
  289. None.
  290. Return value:
  291. None.
  292. --*/
  293. {
  294. WORD wObject;
  295. while (TRUE)
  296. {
  297. wObject = PAL_ItemSelectMenu(NULL, kItemFlagEquipable);
  298. if (wObject == 0)
  299. {
  300. return;
  301. }
  302. PAL_EquipItemMenu(wObject);
  303. }
  304. }
  305. VOID
  306. PAL_Search(
  307. VOID
  308. )
  309. /*++
  310. Purpose:
  311. Process searching trigger events.
  312. Parameters:
  313. None.
  314. Return value:
  315. None.
  316. --*/
  317. {
  318. int x, y, xOffset, yOffset, dx, dy, dh, ex, ey, eh, i, k, l;
  319. LPEVENTOBJECT p;
  320. PAL_POS rgPos[13];
  321. //
  322. // Get the party location
  323. //
  324. x = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset);
  325. y = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset);
  326. if (gpGlobals->wPartyDirection == kDirNorth || gpGlobals->wPartyDirection == kDirEast)
  327. {
  328. xOffset = 16;
  329. }
  330. else
  331. {
  332. xOffset = -16;
  333. }
  334. if (gpGlobals->wPartyDirection == kDirEast || gpGlobals->wPartyDirection == kDirSouth)
  335. {
  336. yOffset = 8;
  337. }
  338. else
  339. {
  340. yOffset = -8;
  341. }
  342. rgPos[0] = PAL_XY(x, y);
  343. for (i = 0; i < 4; i++)
  344. {
  345. rgPos[i * 3 + 1] = PAL_XY(x + xOffset, y + yOffset);
  346. rgPos[i * 3 + 2] = PAL_XY(x, y + yOffset * 2);
  347. rgPos[i * 3 + 3] = PAL_XY(x + xOffset, y);
  348. x += xOffset;
  349. y += yOffset;
  350. }
  351. for (i = 0; i < 13; i++)
  352. {
  353. //
  354. // Convert to map location
  355. //
  356. dh = ((PAL_X(rgPos[i]) % 32) ? 1 : 0);
  357. dx = PAL_X(rgPos[i]) / 32;
  358. dy = PAL_Y(rgPos[i]) / 16;
  359. //
  360. // Loop through all event objects
  361. //
  362. for (k = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex;
  363. k < gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex; k++)
  364. {
  365. p = &(gpGlobals->g.lprgEventObject[k]);
  366. ex = p->x / 32;
  367. ey = p->y / 16;
  368. eh = ((p->x % 32) ? 1 : 0);
  369. if (p->sState <= 0 || p->wTriggerMode >= kTriggerTouchNear ||
  370. p->wTriggerMode * 6 - 4 < i || dx != ex || dy != ey || dh != eh)
  371. {
  372. continue;
  373. }
  374. //
  375. // Adjust direction/gesture for party members and the event object
  376. //
  377. if (p->nSpriteFrames * 4 > p->wCurrentFrameNum)
  378. {
  379. p->wCurrentFrameNum = 0; // use standing gesture
  380. p->wDirection = (gpGlobals->wPartyDirection + 2) % 4; // face the party
  381. for (l = 0; l <= gpGlobals->wMaxPartyMemberIndex; l++)
  382. {
  383. //
  384. // All party members should face the event object
  385. //
  386. gpGlobals->rgParty[l].wFrame = gpGlobals->wPartyDirection * 3;
  387. }
  388. //
  389. // Redraw everything
  390. //
  391. PAL_MakeScene();
  392. VIDEO_UpdateScreen(NULL);
  393. }
  394. //
  395. // Execute the script
  396. //
  397. p->wTriggerScript = PAL_RunTriggerScript(p->wTriggerScript, k + 1);
  398. //
  399. // Clear inputs and delay for a short time
  400. //
  401. UTIL_Delay(50);
  402. PAL_ClearKeyState();
  403. return; // don't go further
  404. }
  405. }
  406. }
  407. VOID
  408. PAL_StartFrame(
  409. VOID
  410. )
  411. /*++
  412. Purpose:
  413. Starts a video frame. Called once per video frame.
  414. Parameters:
  415. None.
  416. Return value:
  417. None.
  418. --*/
  419. {
  420. //
  421. // Run the game logic of one frame
  422. //
  423. PAL_GameUpdate(TRUE);
  424. if (gpGlobals->fEnteringScene)
  425. {
  426. return;
  427. }
  428. //
  429. // Update the positions and gestures of party members
  430. //
  431. PAL_UpdateParty();
  432. //
  433. // Update the scene
  434. //
  435. PAL_MakeScene();
  436. VIDEO_UpdateScreen(NULL);
  437. if (g_InputState.dwKeyPress & kKeyMenu)
  438. {
  439. //
  440. // Show the in-game menu
  441. //
  442. PAL_InGameMenu();
  443. }
  444. else if (g_InputState.dwKeyPress & kKeyUseItem)
  445. {
  446. //
  447. // Show the use item menu
  448. //
  449. PAL_GameUseItem();
  450. }
  451. else if (g_InputState.dwKeyPress & kKeyThrowItem)
  452. {
  453. //
  454. // Show the equipment menu
  455. //
  456. PAL_GameEquipItem();
  457. }
  458. else if (g_InputState.dwKeyPress & kKeyForce)
  459. {
  460. //
  461. // Show the magic menu
  462. //
  463. PAL_InGameMagicMenu();
  464. }
  465. else if (g_InputState.dwKeyPress & kKeyStatus)
  466. {
  467. //
  468. // Show the player status
  469. //
  470. PAL_PlayerStatus();
  471. }
  472. else if (g_InputState.dwKeyPress & kKeySearch)
  473. {
  474. //
  475. // Process search events
  476. //
  477. PAL_Search();
  478. }
  479. else if (g_InputState.dwKeyPress & kKeyFlee)
  480. {
  481. //
  482. // Quit Game
  483. //
  484. if (PAL_ConfirmMenu())
  485. {
  486. SOUND_PlayMUS(0, FALSE, 2);
  487. PAL_FadeOut(2);
  488. PAL_Shutdown();
  489. exit(0);
  490. }
  491. }
  492. if (--gpGlobals->wChasespeedChangeCycles == 0)
  493. {
  494. gpGlobals->wChaseRange = 1;
  495. }
  496. }
  497. VOID
  498. PAL_WaitForKey(
  499. WORD wTimeOut
  500. )
  501. /*++
  502. Purpose:
  503. Wait for any key.
  504. Parameters:
  505. [IN] wTimeOut - the maximum time of the waiting. 0 = wait forever.
  506. Return value:
  507. None.
  508. --*/
  509. {
  510. DWORD dwTimeOut = SDL_GetTicks() + wTimeOut;
  511. PAL_ClearKeyState();
  512. while (wTimeOut == 0 || !SDL_TICKS_PASSED(SDL_GetTicks(), dwTimeOut))
  513. {
  514. UTIL_Delay(5);
  515. if (g_InputState.dwKeyPress & (kKeySearch | kKeyMenu))
  516. {
  517. break;
  518. }
  519. }
  520. }