25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

824 satır
24KB

  1. /*
  2. * STACKFALL - an original falling-block puzzle game for the Commodore 64.
  3. *
  4. * c64 build projects/stackfall
  5. * c64 run projects/stackfall (play it)
  6. * c64 test projects/stackfall (logic self-test + first piece lock)
  7. *
  8. * Timing assumes PAL (50 frames per second); on NTSC everything runs 20% faster.
  9. *
  10. * Controls
  11. * Joystick port 2: left/right move, down soft drop, up hard drop, fire rotate
  12. * Keyboard: A/D or cursor left/right move, S or cursor down soft drop,
  13. * W/X or cursor up rotate, Z rotate back, SPACE hard drop, P pause
  14. *
  15. * Memory: the program loads at $0801 and must end below $3800, where a copy of
  16. * the character ROM (with four redefined block glyphs) lives. The self-test
  17. * checks that.
  18. */
  19. #include <c64.h>
  20. #include <conio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include "c64test.h"
  24. #define SCREEN ((unsigned char *)0x0400)
  25. #define COLRAM ((unsigned char *)0xD800)
  26. #define CHARSET ((unsigned char *)0x3800)
  27. #define PEEK(a) (*(volatile unsigned char *)(a))
  28. #define POKE(a, v) (*(volatile unsigned char *)(a) = (v))
  29. /* Playfield */
  30. #define BW 10
  31. #define BH 20
  32. #define WELL_X 15 /* screen column of board x = 0 */
  33. #define WELL_Y 2 /* screen row of board y = 0 */
  34. /* Redefined screen codes (graphics characters unused by text) */
  35. #define CH_BLOCK 0x60
  36. #define CH_GHOST 0x61
  37. #define CH_WALL 0x62
  38. #define CH_DOT 0x63
  39. #define CH_SPACE 0x20
  40. #define C_DOT COLOR_GRAY1
  41. #define C_WALL COLOR_GRAY2
  42. #define C_LABEL COLOR_GRAY3
  43. #define C_VALUE COLOR_WHITE
  44. /* Input bits */
  45. #define IN_LEFT 0x01
  46. #define IN_RIGHT 0x02
  47. #define IN_DOWN 0x04
  48. #define IN_ROT 0x08
  49. #define IN_CCW 0x10
  50. #define IN_DROP 0x20
  51. #define IN_PAUSE 0x40
  52. #define IN_START 0x80
  53. #define LOCK_DELAY 30 /* frames a grounded piece may slide */
  54. #define LOCK_RESETS 12 /* moves that may restart that delay */
  55. #define DAS_DELAY 10 /* frames before horizontal auto-repeat */
  56. #define DAS_RATE 3 /* frames between repeats */
  57. /* ---------------------------------------------------------------- data */
  58. static const unsigned char glyphs[4][8] = {
  59. { 0xFE, 0x9E, 0xBE, 0xFE, 0xFE, 0xFE, 0xFE, 0x00 }, /* block, with a shine */
  60. { 0xFE, 0x82, 0x82, 0x82, 0x82, 0x82, 0xFE, 0x00 }, /* ghost outline */
  61. { 0xEF, 0xEF, 0xEF, 0x00, 0xFE, 0xFE, 0xFE, 0x00 }, /* brick wall */
  62. { 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00 }, /* empty cell dot */
  63. };
  64. /* 4x4 masks, bit 15 = row 0 column 0. Order: I J L O S T Z. */
  65. static const unsigned int shapes[7][4] = {
  66. { 0x0F00, 0x2222, 0x00F0, 0x4444 },
  67. { 0x8E00, 0x6440, 0x0E20, 0x44C0 },
  68. { 0x2E00, 0x4460, 0x0E80, 0xC440 },
  69. { 0x6600, 0x6600, 0x6600, 0x6600 },
  70. { 0x6C00, 0x4620, 0x06C0, 0x8C40 },
  71. { 0x4E00, 0x4640, 0x0E40, 0x4C40 },
  72. { 0xC600, 0x2640, 0x0C60, 0x4C80 },
  73. };
  74. static const unsigned char piece_color[7] = {
  75. COLOR_CYAN, COLOR_LIGHTBLUE, COLOR_ORANGE, COLOR_YELLOW,
  76. COLOR_GREEN, COLOR_PURPLE, COLOR_RED
  77. };
  78. /* Frames per row at each level (PAL). */
  79. static const unsigned char gravity[21] = {
  80. 48, 42, 37, 32, 27, 22, 18, 14, 10, 8, 7, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1
  81. };
  82. static const unsigned int line_points[5] = { 0, 40, 100, 300, 1200 };
  83. static const signed char kicks[5] = { 0, -1, 1, -2, 2 };
  84. /* Title logo: 3x5 letters, bit 2 = left column. */
  85. static const unsigned char logo[9][5] = {
  86. { 7, 4, 7, 1, 7 }, /* S */
  87. { 7, 2, 2, 2, 2 }, /* T */
  88. { 7, 5, 7, 5, 5 }, /* A */
  89. { 7, 4, 4, 4, 7 }, /* C */
  90. { 5, 5, 6, 5, 5 }, /* K */
  91. { 7, 4, 6, 4, 4 }, /* F */
  92. { 7, 5, 7, 5, 5 }, /* A */
  93. { 4, 4, 4, 4, 7 }, /* L */
  94. { 4, 4, 4, 4, 7 }, /* L */
  95. };
  96. /* Sound effects: frequency high bytes, 0 ends. */
  97. static const unsigned char sfx_move[] = { 0x30, 0 };
  98. static const unsigned char sfx_rotate[] = { 0x40, 0x56, 0 };
  99. static const unsigned char sfx_lock[] = { 0x0A, 0x06, 0 };
  100. static const unsigned char sfx_slam[] = { 0x14, 0x0C, 0x08, 0 };
  101. static const unsigned char sfx_line[] = { 0x22, 0x2C, 0x34, 0x46, 0 };
  102. static const unsigned char sfx_quad[] = { 0x22, 0x2C, 0x34, 0x46, 0x57, 0x68, 0x8B, 0 };
  103. static const unsigned char sfx_over[] = { 0x46, 0x3A, 0x34, 0x2C, 0x27, 0x22, 0x1A, 0x11, 0 };
  104. static const unsigned char sfx_level[] = { 0x34, 0x46, 0x34, 0x46, 0x68, 0 };
  105. /* --------------------------------------------------------------- state */
  106. static unsigned int rowoff[25];
  107. static unsigned char cell_dx[7][4][4];
  108. static unsigned char cell_dy[7][4][4];
  109. static unsigned char board[BH][BW]; /* 0 = empty, else color */
  110. static unsigned char cur_p, cur_r, next_p;
  111. static signed char cur_x, cur_y, ghost_y;
  112. static unsigned char dr_p, dr_r, drawn; /* what is on screen now */
  113. static signed char dr_x, dr_y, dr_gy;
  114. static unsigned char dirty;
  115. static unsigned char bag[7], bag_pos = 7;
  116. static unsigned int rng = 0xACE1;
  117. static unsigned long score, hiscore;
  118. static unsigned int lines;
  119. static unsigned char level, start_level;
  120. static unsigned char frame;
  121. static unsigned char reported; /* test hook fired */
  122. /* SFX channels 0 = voice 1, 1 = voice 2 */
  123. static const unsigned char *sfx_ptr[2];
  124. static unsigned char sfx_timer[2], sfx_len[2], sfx_wave[2];
  125. static char numbuf[20];
  126. /* ----------------------------------------------------------- utilities */
  127. static unsigned char rnd(void)
  128. {
  129. rng ^= rng << 7;
  130. rng ^= rng >> 9;
  131. rng ^= rng << 8;
  132. return (unsigned char)rng;
  133. }
  134. static unsigned char bag_next(void)
  135. {
  136. unsigned char i, j, t;
  137. if (bag_pos >= 7) {
  138. for (i = 0; i < 7; ++i) bag[i] = i;
  139. for (i = 6; i > 0; --i) {
  140. j = rnd() % (i + 1);
  141. t = bag[i]; bag[i] = bag[j]; bag[j] = t;
  142. }
  143. bag_pos = 0;
  144. }
  145. return bag[bag_pos++];
  146. }
  147. static void sfx_play(unsigned char ch, const unsigned char *seq,
  148. unsigned char wave, unsigned char ad, unsigned char sr,
  149. unsigned char len)
  150. {
  151. struct __sid_voice *v = ch ? &SID.v2 : &SID.v1;
  152. v->ctrl = 0;
  153. v->ad = ad;
  154. v->sr = sr;
  155. sfx_ptr[ch] = seq;
  156. sfx_wave[ch] = wave;
  157. sfx_len[ch] = len;
  158. sfx_timer[ch] = 0;
  159. }
  160. static void sfx_tick(void)
  161. {
  162. unsigned char ch, note;
  163. struct __sid_voice *v;
  164. for (ch = 0; ch < 2; ++ch) {
  165. if (!sfx_ptr[ch]) continue;
  166. if (sfx_timer[ch]) { --sfx_timer[ch]; continue; }
  167. v = ch ? &SID.v2 : &SID.v1;
  168. note = *sfx_ptr[ch]++;
  169. if (note == 0) {
  170. v->ctrl = sfx_wave[ch]; /* gate off: release */
  171. sfx_ptr[ch] = 0;
  172. } else {
  173. v->freq = (unsigned int)note << 8;
  174. v->ctrl = sfx_wave[ch];
  175. v->ctrl = sfx_wave[ch] | 1;
  176. sfx_timer[ch] = sfx_len[ch] - 1;
  177. }
  178. }
  179. }
  180. /* Wait for the raster to reach the lower border: one call per PAL frame. */
  181. static void wait_frame(void)
  182. {
  183. while (VIC.rasterline != 251) ;
  184. while (VIC.rasterline == 251) ;
  185. ++frame;
  186. sfx_tick();
  187. }
  188. static void wait_frames(unsigned char n)
  189. {
  190. while (n--) wait_frame();
  191. }
  192. static void put_screen(unsigned char x, unsigned char y, unsigned char ch, unsigned char col)
  193. {
  194. unsigned int o = rowoff[y] + x;
  195. SCREEN[o] = ch;
  196. COLRAM[o] = col;
  197. }
  198. static void text(unsigned char x, unsigned char y, unsigned char col, const char *s)
  199. {
  200. textcolor(col);
  201. cputsxy(x, y, s);
  202. }
  203. static void text_center(unsigned char y, unsigned char col, const char *s)
  204. {
  205. text((40 - strlen(s)) / 2, y, col, s);
  206. }
  207. static void number(unsigned char x, unsigned char y, unsigned long v, unsigned char width)
  208. {
  209. unsigned char n;
  210. ultoa(v, numbuf + 6, 10);
  211. n = strlen(numbuf + 6);
  212. memset(numbuf, '0', 6);
  213. text(x, y, C_VALUE, numbuf + 6 + n - width);
  214. }
  215. static unsigned char read_input(void)
  216. {
  217. unsigned char in = 0;
  218. unsigned char j = ~CIA1.pra;
  219. unsigned char k = PEEK(0xCB);
  220. unsigned char sh = PEEK(0x28D) & 1;
  221. if (j & 0x01) in |= IN_DROP;
  222. if (j & 0x02) in |= IN_DOWN;
  223. if (j & 0x04) in |= IN_LEFT;
  224. if (j & 0x08) in |= IN_RIGHT;
  225. if (j & 0x10) in |= IN_ROT | IN_START;
  226. switch (k) {
  227. case 10: in |= IN_LEFT; break; /* A */
  228. case 18: in |= IN_RIGHT; break; /* D */
  229. case 2: in |= sh ? IN_LEFT : IN_RIGHT; break; /* CRSR <-> */
  230. case 9: case 23: in |= IN_ROT; break; /* W, X */
  231. case 12: in |= IN_CCW; break; /* Z */
  232. case 13: in |= IN_DOWN; break; /* S */
  233. case 7: in |= sh ? IN_ROT : IN_DOWN; break; /* CRSR up/down */
  234. case 60: in |= IN_DROP | IN_START; break; /* SPACE */
  235. case 1: in |= IN_START; break; /* RETURN */
  236. case 41: in |= IN_PAUSE; break; /* P */
  237. }
  238. POKE(0xC6, 0); /* drop buffered keys */
  239. return in;
  240. }
  241. /* --------------------------------------------------------- game rules */
  242. static void build_cells(void)
  243. {
  244. unsigned char p, r, i, bit;
  245. unsigned int m;
  246. for (p = 0; p < 7; ++p)
  247. for (r = 0; r < 4; ++r) {
  248. m = shapes[p][r];
  249. i = 0;
  250. for (bit = 0; bit < 16; ++bit)
  251. if (m & (0x8000u >> bit)) {
  252. if (i < 4) {
  253. cell_dx[p][r][i] = bit & 3;
  254. cell_dy[p][r][i] = bit >> 2;
  255. }
  256. ++i;
  257. }
  258. }
  259. }
  260. static unsigned char fits(unsigned char p, unsigned char r, signed char x, signed char y)
  261. {
  262. unsigned char i;
  263. signed char cx, cy;
  264. const unsigned char *dx = cell_dx[p][r];
  265. const unsigned char *dy = cell_dy[p][r];
  266. for (i = 0; i < 4; ++i) {
  267. cx = x + dx[i];
  268. cy = y + dy[i];
  269. if (cx < 0 || cx >= BW || cy >= BH) return 0;
  270. if (cy >= 0 && board[cy][cx]) return 0;
  271. }
  272. return 1;
  273. }
  274. /* Rotate the current piece by dir (1 = clockwise, 3 = counter), with kicks. */
  275. static unsigned char try_rotate(unsigned char dir)
  276. {
  277. unsigned char r2 = (cur_r + dir) & 3, k;
  278. for (k = 0; k < 5; ++k)
  279. if (fits(cur_p, r2, cur_x + kicks[k], cur_y)) {
  280. cur_r = r2;
  281. cur_x += kicks[k];
  282. return 1;
  283. }
  284. if (fits(cur_p, r2, cur_x, cur_y - 1)) { /* floor kick */
  285. cur_r = r2;
  286. --cur_y;
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. static unsigned char row_full(unsigned char y)
  292. {
  293. unsigned char x;
  294. for (x = 0; x < BW; ++x)
  295. if (!board[y][x]) return 0;
  296. return 1;
  297. }
  298. /* Remove full rows; returns how many. */
  299. static unsigned char collapse(void)
  300. {
  301. unsigned char y, n = 0;
  302. for (y = 0; y < BH; ++y)
  303. if (row_full(y)) {
  304. memmove(board[1], board[0], y * BW);
  305. memset(board[0], 0, BW);
  306. ++n;
  307. }
  308. return n;
  309. }
  310. static unsigned long points_for(unsigned char n, unsigned char lvl)
  311. {
  312. return (unsigned long)line_points[n] * (lvl + 1);
  313. }
  314. static signed char drop_y(void)
  315. {
  316. signed char y = cur_y;
  317. while (fits(cur_p, cur_r, cur_x, y + 1)) ++y;
  318. return y;
  319. }
  320. /* ------------------------------------------------------------ drawing */
  321. static void draw_cell(signed char x, signed char y)
  322. {
  323. unsigned char c;
  324. if (y < 0) return;
  325. c = board[y][x];
  326. if (c) put_screen(WELL_X + x, WELL_Y + y, CH_BLOCK, c);
  327. else put_screen(WELL_X + x, WELL_Y + y, CH_DOT, C_DOT);
  328. }
  329. static void paint_cells(unsigned char p, unsigned char r, signed char x, signed char y,
  330. unsigned char ch, unsigned char col)
  331. {
  332. unsigned char i;
  333. signed char cy;
  334. for (i = 0; i < 4; ++i) {
  335. cy = y + cell_dy[p][r][i];
  336. if (cy >= 0)
  337. put_screen(WELL_X + x + cell_dx[p][r][i], WELL_Y + cy, ch, col);
  338. }
  339. }
  340. static void restore_cells(unsigned char p, unsigned char r, signed char x, signed char y)
  341. {
  342. unsigned char i;
  343. for (i = 0; i < 4; ++i)
  344. draw_cell(x + cell_dx[p][r][i], y + cell_dy[p][r][i]);
  345. }
  346. static void erase_piece(void)
  347. {
  348. if (!drawn) return;
  349. restore_cells(dr_p, dr_r, dr_x, dr_gy);
  350. restore_cells(dr_p, dr_r, dr_x, dr_y);
  351. drawn = 0;
  352. }
  353. static void show_piece(void)
  354. {
  355. erase_piece();
  356. paint_cells(cur_p, cur_r, cur_x, ghost_y, CH_GHOST, piece_color[cur_p]);
  357. paint_cells(cur_p, cur_r, cur_x, cur_y, CH_BLOCK, piece_color[cur_p]);
  358. dr_p = cur_p; dr_r = cur_r; dr_x = cur_x; dr_y = cur_y; dr_gy = ghost_y;
  359. drawn = 1;
  360. dirty = 0;
  361. }
  362. static void draw_well(void)
  363. {
  364. signed char x, y;
  365. for (y = 0; y < BH; ++y)
  366. for (x = 0; x < BW; ++x)
  367. draw_cell(x, y);
  368. }
  369. static void draw_frame(void)
  370. {
  371. unsigned char y;
  372. for (y = WELL_Y; y <= WELL_Y + BH; ++y) {
  373. put_screen(WELL_X - 1, y, CH_WALL, C_WALL);
  374. put_screen(WELL_X + BW, y, CH_WALL, C_WALL);
  375. }
  376. for (y = 0; y < BW; ++y)
  377. put_screen(WELL_X + y, WELL_Y + BH, CH_WALL, C_WALL);
  378. }
  379. static void draw_next(void)
  380. {
  381. unsigned char i, ox = 28, oy = 5;
  382. for (i = 0; i < 4; ++i) {
  383. put_screen(ox + i, oy, CH_SPACE, 0);
  384. put_screen(ox + i, oy + 1, CH_SPACE, 0);
  385. }
  386. for (i = 0; i < 4; ++i)
  387. put_screen(ox + cell_dx[next_p][0][i],
  388. oy + cell_dy[next_p][0][i] - (next_p == 0),
  389. CH_BLOCK, piece_color[next_p]);
  390. }
  391. static void draw_stats(void)
  392. {
  393. number(3, 4, score, 6);
  394. number(3, 8, lines, 3);
  395. number(3, 12, level, 2);
  396. number(3, 16, hiscore, 6);
  397. }
  398. static void draw_hud(void)
  399. {
  400. text(3, 3, C_LABEL, "SCORE");
  401. text(3, 7, C_LABEL, "LINES");
  402. text(3, 11, C_LABEL, "LEVEL");
  403. text(3, 15, C_LABEL, "BEST");
  404. text(28, 3, C_LABEL, "NEXT");
  405. text(28, 10, C_LABEL, "A D MOVE");
  406. text(28, 11, C_LABEL, "W TURN");
  407. text(28, 12, C_LABEL, "Z BACK");
  408. text(28, 13, C_LABEL, "S DOWN");
  409. text(28, 14, C_LABEL, "SPC DROP");
  410. text(28, 15, C_LABEL, "P PAUSE");
  411. text(28, 17, C_DOT, "OR JOY 2");
  412. text(28, 18, C_DOT, "FIRE TURN");
  413. text(28, 19, C_DOT, "UP DROP");
  414. draw_stats();
  415. }
  416. /* ------------------------------------------------------------- setup */
  417. static void install_charset(void)
  418. {
  419. __asm__("sei");
  420. POKE(0x01, 0x33); /* character ROM in at $D000 */
  421. memcpy(CHARSET, (void *)0xD800, 0x800); /* upper/lower case set */
  422. POKE(0x01, 0x37);
  423. __asm__("cli");
  424. memcpy(CHARSET + CH_BLOCK * 8, glyphs, sizeof glyphs);
  425. VIC.addr = 0x1E; /* screen $0400, chars $3800 */
  426. POKE(0x291, 0x80); /* lock out Shift+C= case switch */
  427. }
  428. static void setup(void)
  429. {
  430. unsigned char i;
  431. for (i = 0; i < 25; ++i) rowoff[i] = i * 40;
  432. build_cells();
  433. install_charset();
  434. bordercolor(COLOR_BLACK);
  435. bgcolor(COLOR_BLACK);
  436. cursor(0);
  437. SID.v1.pw = 0x0800;
  438. SID.v2.pw = 0x0800;
  439. SID.amp = 0x0F;
  440. }
  441. /* --------------------------------------------------------- self-test */
  442. extern unsigned char _BSS_RUN__[], _BSS_SIZE__[];
  443. static void self_test(void)
  444. {
  445. unsigned char p, r, i, a;
  446. /* Program, data and BSS must stay clear of the charset at $3800. */
  447. c64test_check((unsigned int)_BSS_RUN__ + (unsigned int)_BSS_SIZE__ <= 0x3800u, 1);
  448. /* Every shape has exactly four cells, all within its 4x4 box. */
  449. for (p = 0; p < 7; ++p)
  450. for (r = 0; r < 4; ++r) {
  451. a = 0;
  452. for (i = 0; i < 16; ++i)
  453. if (shapes[p][r] & (0x8000u >> i)) ++a;
  454. c64test_check(a == 4, 2);
  455. }
  456. memset(board, 0, sizeof board);
  457. /* Spawn fits; an I bar sticking out the right wall does not. */
  458. c64test_check(fits(0, 0, 3, -1), 3);
  459. c64test_check(!fits(0, 0, 7, 0), 4);
  460. c64test_check(fits(0, 0, 6, 0), 5);
  461. /* Vertical I against the left wall: rotating needs a kick. */
  462. cur_p = 0; cur_r = 1; cur_x = -2; cur_y = 5;
  463. c64test_check(fits(0, 1, -2, 5), 6);
  464. c64test_check(try_rotate(1) && cur_r == 2 && fits(0, 2, cur_x, cur_y), 7);
  465. /* Four rotations return to the start. */
  466. cur_p = 5; cur_r = 0; cur_x = 4; cur_y = 5;
  467. for (i = 0; i < 4; ++i) try_rotate(1);
  468. c64test_check(cur_r == 0 && cur_x == 4, 8);
  469. /* Hard drop lands on the floor: an O piece ends with its cells on row 19. */
  470. cur_p = 3; cur_r = 0; cur_x = 4; cur_y = 0;
  471. c64test_check(drop_y() == BH - 2, 9);
  472. /* Line clear: two full rows, one partial, a marker above. */
  473. memset(board[19], 2, BW);
  474. memset(board[18], 3, BW);
  475. board[17][0] = 4;
  476. board[16][5] = 7;
  477. c64test_check(collapse() == 2, 10);
  478. c64test_check(board[19][0] == 4 && board[18][5] == 7, 11);
  479. c64test_check(board[17][5] == 0 && !row_full(19), 12);
  480. /* Scoring. */
  481. c64test_check(points_for(4, 2) == 3600, 13);
  482. c64test_check(points_for(1, 0) == 40, 14);
  483. /* A bag deals each of the seven pieces once. */
  484. bag_pos = 7;
  485. a = 0;
  486. for (i = 0; i < 7; ++i) a |= 1 << bag_next();
  487. c64test_check(a == 0x7F, 15);
  488. memset(board, 0, sizeof board);
  489. }
  490. /* ------------------------------------------------------------- title */
  491. static void draw_logo(unsigned char shift)
  492. {
  493. unsigned char i, row, col, bits, c;
  494. for (i = 0; i < 9; ++i) {
  495. c = piece_color[(i + shift) % 7];
  496. for (row = 0; row < 5; ++row) {
  497. bits = logo[i][row];
  498. for (col = 0; col < 3; ++col)
  499. put_screen(3 + i * 4 + col, 3 + row,
  500. (bits & (4 >> col)) ? CH_BLOCK : CH_SPACE, c);
  501. }
  502. }
  503. }
  504. static void title(void)
  505. {
  506. unsigned char in, prev = 0xFF, shift = 0, edges;
  507. char k;
  508. clrscr();
  509. draw_logo(0);
  510. text_center(10, C_LABEL, "A FALLING BLOCK PUZZLE");
  511. text(11, 13, C_LABEL, "START LEVEL < >");
  512. text(11, 15, C_LABEL, "BEST SCORE");
  513. number(23, 15, hiscore, 6);
  514. text_center(18, C_DOT, "JOYSTICK PORT 2 OR KEYBOARD");
  515. text_center(19, C_DOT, "LEFT/RIGHT PICKS THE LEVEL");
  516. for (;;) {
  517. wait_frame();
  518. rnd(); /* stir the generator */
  519. number(26, 13, start_level, 1);
  520. if ((frame & 15) == 0) draw_logo(++shift);
  521. text_center(22, (frame & 32) ? COLOR_WHITE : C_DOT,
  522. "PRESS FIRE OR SPACE TO PLAY");
  523. k = kbhit() ? cgetc() : 0; /* keys typed into the buffer */
  524. in = read_input();
  525. edges = in & ~prev;
  526. prev = in;
  527. if (k == ' ' || k == '\n') edges |= IN_START;
  528. if ((edges & IN_LEFT) && start_level > 0) { --start_level; sfx_play(0, sfx_move, 0x10, 0x01, 0, 1); }
  529. if ((edges & IN_RIGHT) && start_level < 9) { ++start_level; sfx_play(0, sfx_move, 0x10, 0x01, 0, 1); }
  530. if (edges & IN_START) break;
  531. }
  532. rng ^= frame;
  533. if (!rng) rng = 1;
  534. }
  535. /* -------------------------------------------------------------- play */
  536. static unsigned char spawn(void)
  537. {
  538. cur_p = next_p;
  539. next_p = bag_next();
  540. cur_r = 0;
  541. cur_x = 3;
  542. cur_y = (cur_p == 0) ? -1 : 0;
  543. draw_next();
  544. if (!fits(cur_p, cur_r, cur_x, cur_y)) return 0;
  545. ghost_y = drop_y();
  546. dirty = 1;
  547. return 1;
  548. }
  549. static void clear_animation(void)
  550. {
  551. unsigned char y, x, step;
  552. for (y = 0; y < BH; ++y)
  553. if (row_full(y))
  554. for (x = 0; x < BW; ++x)
  555. put_screen(WELL_X + x, WELL_Y + y, CH_BLOCK, COLOR_WHITE);
  556. wait_frames(5);
  557. for (step = 0; step < 5; ++step) {
  558. for (y = 0; y < BH; ++y)
  559. if (row_full(y)) {
  560. put_screen(WELL_X + 4 - step, WELL_Y + y, CH_SPACE, 0);
  561. put_screen(WELL_X + 5 + step, WELL_Y + y, CH_SPACE, 0);
  562. }
  563. wait_frames(2);
  564. }
  565. }
  566. /* Lock the current piece. Returns 0 if the game is over. */
  567. static unsigned char lock_piece(void)
  568. {
  569. unsigned char i, n, old_level;
  570. signed char cy;
  571. erase_piece();
  572. for (i = 0; i < 4; ++i) {
  573. cy = cur_y + cell_dy[cur_p][cur_r][i];
  574. if (cy < 0) return 0;
  575. board[cy][cur_x + cell_dx[cur_p][cur_r][i]] = piece_color[cur_p];
  576. }
  577. restore_cells(cur_p, cur_r, cur_x, cur_y);
  578. n = 0;
  579. for (i = 0; i < BH; ++i) n += row_full(i);
  580. if (n) {
  581. if (n == 4) sfx_play(1, sfx_quad, 0x40, 0x09, 0x84, 3);
  582. else sfx_play(1, sfx_line, 0x40, 0x09, 0x84, 3);
  583. clear_animation();
  584. collapse();
  585. draw_well();
  586. score += points_for(n, level);
  587. lines += n;
  588. old_level = level;
  589. if (lines / 10 > level) level = lines / 10;
  590. if (level != old_level && n != 4) sfx_play(1, sfx_level, 0x20, 0x09, 0x84, 4);
  591. }
  592. if (score > hiscore) hiscore = score;
  593. draw_stats();
  594. if (!reported) { /* test hook: first piece has fallen and locked */
  595. reported = 1;
  596. c64test_pass();
  597. }
  598. return spawn();
  599. }
  600. static void game_over(void)
  601. {
  602. signed char y;
  603. unsigned char x, in, prev = 0xFF;
  604. erase_piece();
  605. sfx_play(1, sfx_over, 0x20, 0x09, 0x89, 6);
  606. for (y = BH - 1; y >= 0; --y) {
  607. for (x = 0; x < BW; ++x)
  608. put_screen(WELL_X + x, WELL_Y + y, CH_BLOCK, C_DOT);
  609. wait_frames(2);
  610. }
  611. for (y = 8; y <= 12; ++y)
  612. for (x = 0; x < BW; ++x)
  613. put_screen(WELL_X + x, WELL_Y + y, CH_SPACE, 0);
  614. text(WELL_X + 3, WELL_Y + 9, COLOR_WHITE, "GAME");
  615. text(WELL_X + 3, WELL_Y + 11, COLOR_WHITE, "OVER");
  616. wait_frames(50);
  617. for (;;) {
  618. wait_frame();
  619. in = read_input();
  620. if (in & ~prev & IN_START) break;
  621. prev = in;
  622. }
  623. }
  624. static void pause_game(void)
  625. {
  626. unsigned char x, in, prev = 0xFF;
  627. for (x = 0; x < BW; ++x) put_screen(WELL_X + x, WELL_Y + 9, CH_SPACE, 0);
  628. text(WELL_X + 2, WELL_Y + 9, COLOR_WHITE, "PAUSED");
  629. for (;;) {
  630. wait_frame();
  631. in = read_input();
  632. if (in & ~prev & (IN_PAUSE | IN_START)) break;
  633. prev = in;
  634. }
  635. draw_well();
  636. drawn = 0;
  637. dirty = 1;
  638. }
  639. static void play(void)
  640. {
  641. unsigned char in, prev = 0xFF, edges;
  642. unsigned char grav = 0, das = 0, soft = 0, lock_t = 0, resets = 0;
  643. signed char dir, last_dir = 0, rows;
  644. clrscr();
  645. memset(board, 0, sizeof board);
  646. score = 0;
  647. lines = 0;
  648. level = start_level;
  649. drawn = 0;
  650. bag_pos = 7;
  651. draw_frame();
  652. draw_well();
  653. draw_hud();
  654. next_p = bag_next();
  655. spawn();
  656. for (;;) {
  657. wait_frame();
  658. if (dirty) show_piece();
  659. in = read_input();
  660. edges = in & ~prev;
  661. prev = in;
  662. if (edges & IN_PAUSE) { pause_game(); continue; }
  663. /* Horizontal movement with delayed auto-repeat. */
  664. dir = (in & IN_LEFT) ? -1 : (in & IN_RIGHT) ? 1 : 0;
  665. if (dir != last_dir) das = 0;
  666. if (dir && (dir != last_dir || ++das >= DAS_DELAY)) {
  667. if (das >= DAS_DELAY) das = DAS_DELAY - DAS_RATE;
  668. if (fits(cur_p, cur_r, cur_x + dir, cur_y)) {
  669. cur_x += dir;
  670. dirty = 1;
  671. sfx_play(0, sfx_move, 0x10, 0x01, 0, 1);
  672. if (lock_t && resets < LOCK_RESETS) { lock_t = 0; ++resets; }
  673. }
  674. }
  675. last_dir = dir;
  676. /* Rotation. */
  677. if (edges & (IN_ROT | IN_CCW)) {
  678. if (try_rotate((edges & IN_ROT) ? 1 : 3)) {
  679. dirty = 1;
  680. sfx_play(0, sfx_rotate, 0x10, 0x02, 0, 1);
  681. if (lock_t && resets < LOCK_RESETS) { lock_t = 0; ++resets; }
  682. }
  683. }
  684. /* Hard drop. */
  685. if (edges & IN_DROP) {
  686. rows = ghost_y - cur_y;
  687. cur_y = ghost_y;
  688. score += rows * 2;
  689. sfx_play(1, sfx_slam, 0x80, 0x04, 0, 1);
  690. if (!lock_piece()) break;
  691. grav = lock_t = resets = 0;
  692. continue;
  693. }
  694. if (dirty) ghost_y = drop_y();
  695. /* Soft drop and gravity. */
  696. if ((in & IN_DOWN) && ++soft >= 2) {
  697. soft = 0;
  698. if (cur_y < ghost_y) {
  699. ++cur_y;
  700. ++score;
  701. grav = 0;
  702. dirty = 1;
  703. }
  704. }
  705. if (++grav >= gravity[level > 20 ? 20 : level]) {
  706. grav = 0;
  707. if (cur_y < ghost_y) { ++cur_y; dirty = 1; }
  708. }
  709. /* Lock once grounded long enough (at once while soft-dropping). */
  710. if (cur_y >= ghost_y) {
  711. if (++lock_t >= LOCK_DELAY || (in & IN_DOWN)) {
  712. sfx_play(1, sfx_lock, 0x80, 0x03, 0, 1);
  713. if (!lock_piece()) break;
  714. grav = lock_t = resets = 0;
  715. }
  716. } else {
  717. lock_t = 0;
  718. }
  719. }
  720. game_over();
  721. }
  722. int main(void)
  723. {
  724. setup();
  725. self_test();
  726. for (;;) {
  727. title();
  728. play();
  729. }
  730. }

Powered by TurnKey Linux.