scene.c 21 KB

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