play.c 14 KB

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