scene.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  4. // Copyright (c) 2011-2017, SDLPAL development team.
  5. // All rights reserved.
  6. //
  7. // This file is part of SDLPAL.
  8. //
  9. // SDLPAL is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation, either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. // GNU General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. //
  22. // Portions based on PALx Project by palxex.
  23. // Copyright (c) 2006, Pal Lockheart <palxex@gmail.com>.
  24. //
  25. #include "main.h"
  26. #define MAX_SPRITE_TO_DRAW 2048
  27. typedef struct tagSPRITE_TO_DRAW
  28. {
  29. LPCBITMAPRLE lpSpriteFrame; // pointer to the frame bitmap
  30. PAL_POS pos; // position on the scene
  31. int iLayer; // logical layer
  32. } SPRITE_TO_DRAW;
  33. static SPRITE_TO_DRAW g_rgSpriteToDraw[MAX_SPRITE_TO_DRAW];
  34. static int g_nSpriteToDraw;
  35. static VOID
  36. PAL_AddSpriteToDraw(
  37. LPCBITMAPRLE lpSpriteFrame,
  38. int x,
  39. int y,
  40. int iLayer
  41. )
  42. /*++
  43. Purpose:
  44. Add a sprite to our list of drawing.
  45. Parameters:
  46. [IN] lpSpriteFrame - the bitmap of the sprite frame.
  47. [IN] x - the X coordinate on the screen.
  48. [IN] y - the Y coordinate on the screen.
  49. [IN] iLayer - the layer of the sprite.
  50. Return value:
  51. None.
  52. --*/
  53. {
  54. assert(g_nSpriteToDraw < MAX_SPRITE_TO_DRAW);
  55. g_rgSpriteToDraw[g_nSpriteToDraw].lpSpriteFrame = lpSpriteFrame;
  56. g_rgSpriteToDraw[g_nSpriteToDraw].pos = PAL_XY(x, y);
  57. g_rgSpriteToDraw[g_nSpriteToDraw].iLayer = iLayer;
  58. g_nSpriteToDraw++;
  59. }
  60. static VOID
  61. PAL_CalcCoverTiles(
  62. SPRITE_TO_DRAW *lpSpriteToDraw
  63. )
  64. /*++
  65. Purpose:
  66. Calculate all the tiles which may cover the specified sprite. Add the tiles
  67. into our list as well.
  68. Parameters:
  69. [IN] lpSpriteToDraw - pointer to SPRITE_TO_DRAW struct.
  70. Return value:
  71. None.
  72. --*/
  73. {
  74. int x, y, i, l, iTileHeight;
  75. LPCBITMAPRLE lpTile;
  76. const int sx = PAL_X(gpGlobals->viewport) + PAL_X(lpSpriteToDraw->pos);
  77. const int sy = PAL_Y(gpGlobals->viewport) + PAL_Y(lpSpriteToDraw->pos);
  78. const int sh = ((sx % 32) ? 1 : 0);
  79. const int width = PAL_RLEGetWidth(lpSpriteToDraw->lpSpriteFrame);
  80. const int height = PAL_RLEGetHeight(lpSpriteToDraw->lpSpriteFrame);
  81. int dx = 0;
  82. int dy = 0;
  83. int dh = 0;
  84. //
  85. // Loop through all the tiles in the area of the sprite.
  86. //
  87. for (y = (sy - height - 15) / 16; y <= sy / 16; y++)
  88. {
  89. for (x = (sx - width / 2) / 32; x <= (sx + width / 2) / 32; x++)
  90. {
  91. for (i = ((x == (sx - width / 2) / 32) ? 0 : 3); i < 5; i++)
  92. {
  93. //
  94. // Scan tiles in the following form (* = to scan):
  95. //
  96. // . . . * * * . . .
  97. // . . . * * . . . .
  98. //
  99. switch (i)
  100. {
  101. case 0:
  102. dx = x;
  103. dy = y;
  104. dh = sh;
  105. break;
  106. case 1:
  107. dx = x - 1;
  108. break;
  109. case 2:
  110. dx = (sh ? x : (x - 1));
  111. dy = (sh ? (y + 1) : y);
  112. dh = 1 - sh;
  113. break;
  114. case 3:
  115. dx = x + 1;
  116. dy = y;
  117. dh = sh;
  118. break;
  119. case 4:
  120. dx = (sh ? (x + 1) : x);
  121. dy = (sh ? (y + 1) : y);
  122. dh = 1 - sh;
  123. break;
  124. }
  125. for (l = 0; l < 2; l++)
  126. {
  127. lpTile = PAL_MapGetTileBitmap(dx, dy, dh, l, PAL_GetCurrentMap());
  128. iTileHeight = (signed char)PAL_MapGetTileHeight(dx, dy, dh, l, PAL_GetCurrentMap());
  129. //
  130. // Check if this tile may cover the sprites
  131. //
  132. if (lpTile != NULL && iTileHeight > 0 && (dy + iTileHeight) * 16 + dh * 8 >= sy)
  133. {
  134. //
  135. // This tile may cover the sprite
  136. //
  137. PAL_AddSpriteToDraw(lpTile,
  138. dx * 32 + dh * 16 - 16 - PAL_X(gpGlobals->viewport),
  139. dy * 16 + dh * 8 + 7 + l + iTileHeight * 8 - PAL_Y(gpGlobals->viewport),
  140. iTileHeight * 8 + l);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. static VOID
  148. PAL_SceneDrawSprites(
  149. VOID
  150. )
  151. /*++
  152. Purpose:
  153. Draw all the sprites to scene.
  154. Parameters:
  155. None.
  156. Return value:
  157. None.
  158. --*/
  159. {
  160. int i, x, y, vy;
  161. g_nSpriteToDraw = 0;
  162. //
  163. // Put all the sprites to be drawn into our array.
  164. //
  165. //
  166. // Players
  167. //
  168. for (i = 0; i <= (short)gpGlobals->wMaxPartyMemberIndex + gpGlobals->nFollower; i++)
  169. {
  170. LPCBITMAPRLE lpBitmap =
  171. PAL_SpriteGetFrame(PAL_GetPlayerSprite((BYTE)i), gpGlobals->rgParty[i].wFrame);
  172. if (lpBitmap == NULL)
  173. {
  174. continue;
  175. }
  176. //
  177. // Add it to our array
  178. //
  179. PAL_AddSpriteToDraw(lpBitmap,
  180. gpGlobals->rgParty[i].x - PAL_RLEGetWidth(lpBitmap) / 2,
  181. gpGlobals->rgParty[i].y + gpGlobals->wLayer + 10,
  182. gpGlobals->wLayer + 6);
  183. //
  184. // Calculate covering tiles on the map
  185. //
  186. PAL_CalcCoverTiles(&g_rgSpriteToDraw[g_nSpriteToDraw - 1]);
  187. }
  188. //
  189. // Event Objects (Monsters/NPCs/others)
  190. //
  191. for (i = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex;
  192. i < gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex; i++)
  193. {
  194. LPCBITMAPRLE lpFrame;
  195. LPCSPRITE lpSprite;
  196. LPEVENTOBJECT lpEvtObj = &(gpGlobals->g.lprgEventObject[i]);
  197. int iFrame;
  198. if (lpEvtObj->sState == kObjStateHidden || lpEvtObj->sVanishTime > 0 ||
  199. lpEvtObj->sState < 0)
  200. {
  201. continue;
  202. }
  203. //
  204. // Get the sprite
  205. //
  206. lpSprite = PAL_GetEventObjectSprite((WORD)i + 1);
  207. if (lpSprite == NULL)
  208. {
  209. continue;
  210. }
  211. iFrame = lpEvtObj->wCurrentFrameNum;
  212. if (lpEvtObj->nSpriteFrames == 3)
  213. {
  214. //
  215. // walking character
  216. //
  217. if (iFrame == 2)
  218. {
  219. iFrame = 0;
  220. }
  221. if (iFrame == 3)
  222. {
  223. iFrame = 2;
  224. }
  225. }
  226. lpFrame = PAL_SpriteGetFrame(lpSprite,
  227. lpEvtObj->wDirection * lpEvtObj->nSpriteFrames + iFrame);
  228. if (lpFrame == NULL)
  229. {
  230. continue;
  231. }
  232. //
  233. // Calculate the coordinate and check if outside the screen
  234. //
  235. x = (SHORT)lpEvtObj->x - PAL_X(gpGlobals->viewport);
  236. x -= PAL_RLEGetWidth(lpFrame) / 2;
  237. if (x >= 320 || x < -(int)PAL_RLEGetWidth(lpFrame))
  238. {
  239. //
  240. // outside the screen; skip it
  241. //
  242. continue;
  243. }
  244. y = (SHORT)lpEvtObj->y - PAL_Y(gpGlobals->viewport);
  245. y += lpEvtObj->sLayer * 8 + 9;
  246. vy = y - PAL_RLEGetHeight(lpFrame) - lpEvtObj->sLayer * 8 + 2;
  247. if (vy >= 200 || vy < -(int)PAL_RLEGetHeight(lpFrame))
  248. {
  249. //
  250. // outside the screen; skip it
  251. //
  252. continue;
  253. }
  254. //
  255. // Add it into the array
  256. //
  257. PAL_AddSpriteToDraw(lpFrame, x, y, lpEvtObj->sLayer * 8 + 2);
  258. //
  259. // Calculate covering map tiles
  260. //
  261. PAL_CalcCoverTiles(&g_rgSpriteToDraw[g_nSpriteToDraw - 1]);
  262. }
  263. //
  264. // All sprites are now in our array; sort them by their vertical positions.
  265. //
  266. for (x = 0; x < g_nSpriteToDraw - 1; x++)
  267. {
  268. SPRITE_TO_DRAW tmp;
  269. BOOL fSwap = FALSE;
  270. for (y = 0; y < g_nSpriteToDraw - 1 - x; y++)
  271. {
  272. if (PAL_Y(g_rgSpriteToDraw[y].pos) > PAL_Y(g_rgSpriteToDraw[y + 1].pos))
  273. {
  274. fSwap = TRUE;
  275. tmp = g_rgSpriteToDraw[y];
  276. g_rgSpriteToDraw[y] = g_rgSpriteToDraw[y + 1];
  277. g_rgSpriteToDraw[y + 1] = tmp;
  278. }
  279. }
  280. if (!fSwap)
  281. {
  282. break;
  283. }
  284. }
  285. //
  286. // Draw all the sprites to the screen.
  287. //
  288. for (i = 0; i < g_nSpriteToDraw; i++)
  289. {
  290. SPRITE_TO_DRAW *p = &g_rgSpriteToDraw[i];
  291. x = PAL_X(p->pos);
  292. y = PAL_Y(p->pos) - PAL_RLEGetHeight(p->lpSpriteFrame) - p->iLayer;
  293. PAL_RLEBlitToSurface(p->lpSpriteFrame, gpScreen, PAL_XY(x, y));
  294. }
  295. }
  296. VOID
  297. PAL_ApplyWave(
  298. SDL_Surface *lpSurface
  299. )
  300. /*++
  301. Purpose:
  302. Apply screen waving effect when needed.
  303. Parameters:
  304. [OUT] lpSurface - the surface to be proceed.
  305. Return value:
  306. None.
  307. --*/
  308. {
  309. int wave[32];
  310. int i, a, b;
  311. static int index = 0;
  312. LPBYTE p;
  313. BYTE buf[320];
  314. gpGlobals->wScreenWave += gpGlobals->sWaveProgression;
  315. if (gpGlobals->wScreenWave == 0 || gpGlobals->wScreenWave >= 256)
  316. {
  317. //
  318. // No need to wave the screen
  319. //
  320. gpGlobals->wScreenWave = 0;
  321. gpGlobals->sWaveProgression = 0;
  322. return;
  323. }
  324. //
  325. // Calculate the waving offsets.
  326. //
  327. a = 0;
  328. b = 60 + 8;
  329. for (i = 0; i < 16; i++)
  330. {
  331. b -= 8;
  332. a += b;
  333. //
  334. // WARNING: assuming the screen width is 320
  335. //
  336. wave[i] = a * gpGlobals->wScreenWave / 256;
  337. wave[i + 16] = 320 - wave[i];
  338. }
  339. //
  340. // Apply the effect.
  341. // WARNING: only works with 320x200 8-bit surface.
  342. //
  343. a = index;
  344. p = (LPBYTE)(lpSurface->pixels);
  345. //
  346. // Loop through all lines in the screen buffer.
  347. //
  348. for (i = 0; i < 200; i++)
  349. {
  350. b = wave[a];
  351. if (b > 0)
  352. {
  353. //
  354. // Do a shift on the current line with the calculated offset.
  355. //
  356. memcpy(buf, p, b);
  357. //memmove(p, p + b, 320 - b);
  358. memmove(p, &p[b], 320 - b);
  359. //memcpy(p + 320 - b, buf, b);
  360. memcpy(&p[320 - b], buf, b);
  361. }
  362. a = (a + 1) % 32;
  363. p += lpSurface->pitch;
  364. }
  365. index = (index + 1) % 32;
  366. }
  367. VOID
  368. PAL_MakeScene(
  369. VOID
  370. )
  371. /*++
  372. Purpose:
  373. Draw the scene of the current frame to the screen. Both the map and
  374. the sprites are handled here.
  375. Parameters:
  376. None.
  377. Return value:
  378. None.
  379. --*/
  380. {
  381. static SDL_Rect rect = {0, 0, 320, 200};
  382. //
  383. // Step 1: Draw the complete map, for both of the layers.
  384. //
  385. rect.x = PAL_X(gpGlobals->viewport);
  386. rect.y = PAL_Y(gpGlobals->viewport);
  387. PAL_MapBlitToSurface(PAL_GetCurrentMap(), gpScreen, &rect, 0);
  388. PAL_MapBlitToSurface(PAL_GetCurrentMap(), gpScreen, &rect, 1);
  389. //
  390. // Step 2: Apply screen waving effects.
  391. //
  392. PAL_ApplyWave(gpScreen);
  393. //
  394. // Step 3: Draw all the sprites.
  395. //
  396. PAL_SceneDrawSprites();
  397. //
  398. // Check if we need to fade in.
  399. //
  400. if (gpGlobals->fNeedToFadeIn)
  401. {
  402. VIDEO_UpdateScreen(NULL);
  403. PAL_FadeIn(gpGlobals->wNumPalette, gpGlobals->fNightPalette, 1);
  404. gpGlobals->fNeedToFadeIn = FALSE;
  405. }
  406. }
  407. BOOL
  408. PAL_CheckObstacle(
  409. PAL_POS pos,
  410. BOOL fCheckEventObjects,
  411. WORD wSelfObject
  412. )
  413. /*++
  414. Purpose:
  415. Check if the specified location has obstacle or not.
  416. Parameters:
  417. [IN] pos - the position to check.
  418. [IN] fCheckEventObjects - TRUE if check for event objects, FALSE if only
  419. check for the map.
  420. [IN] wSelfObject - the event object which will be skipped.
  421. Return value:
  422. TRUE if the location is obstacle, FALSE if not.
  423. --*/
  424. {
  425. int x, y, h, xr, yr;
  426. if (PAL_X(pos) < 0 || PAL_X(pos) >= 2048 || PAL_Y(pos) < 0 || PAL_Y(pos) >= 2048)
  427. {
  428. return TRUE;
  429. }
  430. //
  431. // Check if the map tile at the specified position is blocking
  432. //
  433. x = PAL_X(pos) / 32;
  434. y = PAL_Y(pos) / 16;
  435. h = 0;
  436. xr = PAL_X(pos) % 32;
  437. yr = PAL_Y(pos) % 16;
  438. if (xr + yr * 2 >= 16)
  439. {
  440. if (xr + yr * 2 >= 48)
  441. {
  442. x++;
  443. y++;
  444. }
  445. else if (32 - xr + yr * 2 < 16)
  446. {
  447. x++;
  448. }
  449. else if (32 - xr + yr * 2 < 48)
  450. {
  451. h = 1;
  452. }
  453. else
  454. {
  455. y++;
  456. }
  457. }
  458. if (PAL_MapTileIsBlocked(x, y, h, PAL_GetCurrentMap()))
  459. {
  460. return TRUE;
  461. }
  462. if (fCheckEventObjects)
  463. {
  464. //
  465. // Loop through all event objects in the current scene
  466. //
  467. int i;
  468. for (i = gpGlobals->g.rgScene[gpGlobals->wNumScene - 1].wEventObjectIndex;
  469. i < gpGlobals->g.rgScene[gpGlobals->wNumScene].wEventObjectIndex; i++)
  470. {
  471. LPEVENTOBJECT p = &(gpGlobals->g.lprgEventObject[i]);
  472. if (i == wSelfObject - 1)
  473. {
  474. //
  475. // Skip myself
  476. //
  477. continue;
  478. }
  479. //
  480. // Is this object a blocking one?
  481. //
  482. if (p->sState >= kObjStateBlocker)
  483. {
  484. //
  485. // Check for collision
  486. //
  487. if (abs(p->x - PAL_X(pos)) + abs(p->y - PAL_Y(pos)) * 2 < 16)
  488. {
  489. return TRUE;
  490. }
  491. }
  492. }
  493. }
  494. return FALSE;
  495. }
  496. VOID
  497. PAL_UpdatePartyGestures(
  498. BOOL fWalking
  499. )
  500. /*++
  501. Purpose:
  502. Update the gestures of all the party members.
  503. Parameters:
  504. [IN] fWalking - whether the party is walking or not.
  505. Return value:
  506. None.
  507. --*/
  508. {
  509. static int s_iThisStepFrame = 0;
  510. int iStepFrameFollower = 0, iStepFrameLeader = 0;
  511. int i;
  512. if (fWalking)
  513. {
  514. //
  515. // Update the gesture for party leader
  516. //
  517. s_iThisStepFrame = (s_iThisStepFrame + 1) % 4;
  518. if (s_iThisStepFrame & 1)
  519. {
  520. iStepFrameLeader = (s_iThisStepFrame + 1) / 2;
  521. iStepFrameFollower = 3 - iStepFrameLeader;
  522. }
  523. else
  524. {
  525. iStepFrameLeader = 0;
  526. iStepFrameFollower = 0;
  527. }
  528. gpGlobals->rgParty[0].x = PAL_X(gpGlobals->partyoffset);
  529. gpGlobals->rgParty[0].y = PAL_Y(gpGlobals->partyoffset);
  530. if (gpGlobals->g.PlayerRoles.rgwWalkFrames[gpGlobals->rgParty[0].wPlayerRole] == 4)
  531. {
  532. gpGlobals->rgParty[0].wFrame = gpGlobals->wPartyDirection * 4 + s_iThisStepFrame;
  533. }
  534. else
  535. {
  536. gpGlobals->rgParty[0].wFrame = gpGlobals->wPartyDirection * 3 + iStepFrameLeader;
  537. }
  538. //
  539. // Update the gestures and positions for other party members
  540. //
  541. for (i = 1; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  542. {
  543. gpGlobals->rgParty[i].x = gpGlobals->rgTrail[1].x - PAL_X(gpGlobals->viewport);
  544. gpGlobals->rgParty[i].y = gpGlobals->rgTrail[1].y - PAL_Y(gpGlobals->viewport);
  545. if (i == 2)
  546. {
  547. gpGlobals->rgParty[i].x +=
  548. (gpGlobals->rgTrail[1].wDirection == kDirEast || gpGlobals->rgTrail[1].wDirection == kDirWest) ? -16 : 16;
  549. gpGlobals->rgParty[i].y += 8;
  550. }
  551. else
  552. {
  553. gpGlobals->rgParty[i].x +=
  554. ((gpGlobals->rgTrail[1].wDirection == kDirWest || gpGlobals->rgTrail[1].wDirection == kDirSouth) ? 16 : -16);
  555. gpGlobals->rgParty[i].y +=
  556. ((gpGlobals->rgTrail[1].wDirection == kDirWest || gpGlobals->rgTrail[1].wDirection == kDirNorth) ? 8 : -8);
  557. }
  558. //
  559. // Adjust the position if there is obstacle
  560. //
  561. if (PAL_CheckObstacle(PAL_XY(gpGlobals->rgParty[i].x + PAL_X(gpGlobals->viewport),
  562. gpGlobals->rgParty[i].y + PAL_Y(gpGlobals->viewport)), TRUE, 0))
  563. {
  564. gpGlobals->rgParty[i].x = gpGlobals->rgTrail[1].x - PAL_X(gpGlobals->viewport);
  565. gpGlobals->rgParty[i].y = gpGlobals->rgTrail[1].y - PAL_Y(gpGlobals->viewport);
  566. }
  567. //
  568. // Update gesture for this party member
  569. //
  570. if (gpGlobals->g.PlayerRoles.rgwWalkFrames[gpGlobals->rgParty[i].wPlayerRole] == 4)
  571. {
  572. gpGlobals->rgParty[i].wFrame = gpGlobals->rgTrail[2].wDirection * 4 + s_iThisStepFrame;
  573. }
  574. else
  575. {
  576. gpGlobals->rgParty[i].wFrame = gpGlobals->rgTrail[2].wDirection * 3 + iStepFrameLeader;
  577. }
  578. }
  579. if (gpGlobals->nFollower > 0)
  580. {
  581. //
  582. // Update the position and gesture for the follower
  583. //
  584. gpGlobals->rgParty[gpGlobals->wMaxPartyMemberIndex + 1].x =
  585. gpGlobals->rgTrail[3].x - PAL_X(gpGlobals->viewport);
  586. gpGlobals->rgParty[gpGlobals->wMaxPartyMemberIndex + 1].y =
  587. gpGlobals->rgTrail[3].y - PAL_Y(gpGlobals->viewport);
  588. gpGlobals->rgParty[gpGlobals->wMaxPartyMemberIndex + 1].wFrame =
  589. gpGlobals->rgTrail[3].wDirection * 3 + iStepFrameFollower;
  590. }
  591. }
  592. else
  593. {
  594. //
  595. // Player is not moved. Use the "standing" gesture instead of "walking" one.
  596. //
  597. i = gpGlobals->g.PlayerRoles.rgwWalkFrames[gpGlobals->rgParty[0].wPlayerRole];
  598. if (i == 0)
  599. {
  600. i = 3;
  601. }
  602. gpGlobals->rgParty[0].wFrame = gpGlobals->wPartyDirection * i;
  603. for (i = 1; i <= (short)gpGlobals->wMaxPartyMemberIndex; i++)
  604. {
  605. int f = gpGlobals->g.PlayerRoles.rgwWalkFrames[gpGlobals->rgParty[i].wPlayerRole];
  606. if (f == 0)
  607. {
  608. f = 3;
  609. }
  610. gpGlobals->rgParty[i].wFrame = gpGlobals->rgTrail[2].wDirection * f;
  611. }
  612. if (gpGlobals->nFollower > 0)
  613. {
  614. gpGlobals->rgParty[gpGlobals->wMaxPartyMemberIndex + 1].wFrame =
  615. gpGlobals->rgTrail[3].wDirection * 3;
  616. }
  617. s_iThisStepFrame &= 2;
  618. s_iThisStepFrame ^= 2;
  619. }
  620. }
  621. VOID
  622. PAL_UpdateParty(
  623. VOID
  624. )
  625. /*++
  626. Purpose:
  627. Update the location and walking gesture of all the party members.
  628. Parameters:
  629. None.
  630. Return value:
  631. None.
  632. --*/
  633. {
  634. int xSource, ySource, xTarget, yTarget, xOffset, yOffset, i;
  635. //
  636. // Has user pressed one of the arrow keys?
  637. //
  638. if (g_InputState.dir != kDirUnknown)
  639. {
  640. xOffset = ((g_InputState.dir == kDirWest || g_InputState.dir == kDirSouth) ? -16 : 16);
  641. yOffset = ((g_InputState.dir == kDirWest || g_InputState.dir == kDirNorth) ? -8 : 8);
  642. xSource = PAL_X(gpGlobals->viewport) + PAL_X(gpGlobals->partyoffset);
  643. ySource = PAL_Y(gpGlobals->viewport) + PAL_Y(gpGlobals->partyoffset);
  644. xTarget = xSource + xOffset;
  645. yTarget = ySource + yOffset;
  646. gpGlobals->wPartyDirection = g_InputState.dir;
  647. //
  648. // Check for obstacles on the destination location
  649. //
  650. if (!PAL_CheckObstacle(PAL_XY(xTarget, yTarget), TRUE, 0))
  651. {
  652. //
  653. // Player will actually be moved. Store trail.
  654. //
  655. for (i = 3; i >= 0; i--)
  656. {
  657. gpGlobals->rgTrail[i + 1] = gpGlobals->rgTrail[i];
  658. }
  659. gpGlobals->rgTrail[0].wDirection = g_InputState.dir;
  660. gpGlobals->rgTrail[0].x = xSource;
  661. gpGlobals->rgTrail[0].y = ySource;
  662. //
  663. // Move the viewport
  664. //
  665. gpGlobals->viewport =
  666. PAL_XY(PAL_X(gpGlobals->viewport) + xOffset, PAL_Y(gpGlobals->viewport) + yOffset);
  667. //
  668. // Update gestures
  669. //
  670. PAL_UpdatePartyGestures(TRUE);
  671. return; // don't go further
  672. }
  673. }
  674. PAL_UpdatePartyGestures(FALSE);
  675. }
  676. VOID
  677. PAL_NPCWalkOneStep(
  678. WORD wEventObjectID,
  679. INT iSpeed
  680. )
  681. /*++
  682. Purpose:
  683. Move and animate the specified event object (NPC).
  684. Parameters:
  685. [IN] wEventObjectID - the event object to move.
  686. [IN] iSpeed - speed of the movement.
  687. Return value:
  688. None.
  689. --*/
  690. {
  691. LPEVENTOBJECT p;
  692. //
  693. // Check for invalid parameters
  694. //
  695. if (wEventObjectID == 0 || wEventObjectID > gpGlobals->g.nEventObject)
  696. {
  697. return;
  698. }
  699. p = &(gpGlobals->g.lprgEventObject[wEventObjectID - 1]);
  700. //
  701. // Move the event object by the specified direction
  702. //
  703. p->x += ((p->wDirection == kDirWest || p->wDirection == kDirSouth) ? -2 : 2) * iSpeed;
  704. p->y += ((p->wDirection == kDirWest || p->wDirection == kDirNorth) ? -1 : 1) * iSpeed;
  705. //
  706. // Update the gesture
  707. //
  708. if (p->nSpriteFrames > 0)
  709. {
  710. p->wCurrentFrameNum++;
  711. p->wCurrentFrameNum %= (p->nSpriteFrames == 3 ? 4 : p->nSpriteFrames);
  712. }
  713. else if (p->nSpriteFramesAuto > 0)
  714. {
  715. p->wCurrentFrameNum++;
  716. p->wCurrentFrameNum %= p->nSpriteFramesAuto;
  717. }
  718. }