|
- /*
- * STACKFALL - an original falling-block puzzle game for the Commodore 64.
- *
- * c64 build projects/stackfall
- * c64 run projects/stackfall (play it)
- * c64 test projects/stackfall (logic self-test + first piece lock)
- *
- * Timing assumes PAL (50 frames per second); on NTSC everything runs 20% faster.
- *
- * Controls
- * Joystick port 2: left/right move, down soft drop, up hard drop, fire rotate
- * Keyboard: A/D or cursor left/right move, S or cursor down soft drop,
- * W/X or cursor up rotate, Z rotate back, SPACE hard drop, P pause
- *
- * Memory: the program loads at $0801 and must end below $3800, where a copy of
- * the character ROM (with four redefined block glyphs) lives. The self-test
- * checks that.
- */
- #include <c64.h>
- #include <conio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "c64test.h"
-
- #define SCREEN ((unsigned char *)0x0400)
- #define COLRAM ((unsigned char *)0xD800)
- #define CHARSET ((unsigned char *)0x3800)
- #define PEEK(a) (*(volatile unsigned char *)(a))
- #define POKE(a, v) (*(volatile unsigned char *)(a) = (v))
-
- /* Playfield */
- #define BW 10
- #define BH 20
- #define WELL_X 15 /* screen column of board x = 0 */
- #define WELL_Y 2 /* screen row of board y = 0 */
-
- /* Redefined screen codes (graphics characters unused by text) */
- #define CH_BLOCK 0x60
- #define CH_GHOST 0x61
- #define CH_WALL 0x62
- #define CH_DOT 0x63
- #define CH_SPACE 0x20
-
- #define C_DOT COLOR_GRAY1
- #define C_WALL COLOR_GRAY2
- #define C_LABEL COLOR_GRAY3
- #define C_VALUE COLOR_WHITE
-
- /* Input bits */
- #define IN_LEFT 0x01
- #define IN_RIGHT 0x02
- #define IN_DOWN 0x04
- #define IN_ROT 0x08
- #define IN_CCW 0x10
- #define IN_DROP 0x20
- #define IN_PAUSE 0x40
- #define IN_START 0x80
-
- #define LOCK_DELAY 30 /* frames a grounded piece may slide */
- #define LOCK_RESETS 12 /* moves that may restart that delay */
- #define DAS_DELAY 10 /* frames before horizontal auto-repeat */
- #define DAS_RATE 3 /* frames between repeats */
-
- /* ---------------------------------------------------------------- data */
-
- static const unsigned char glyphs[4][8] = {
- { 0xFE, 0x9E, 0xBE, 0xFE, 0xFE, 0xFE, 0xFE, 0x00 }, /* block, with a shine */
- { 0xFE, 0x82, 0x82, 0x82, 0x82, 0x82, 0xFE, 0x00 }, /* ghost outline */
- { 0xEF, 0xEF, 0xEF, 0x00, 0xFE, 0xFE, 0xFE, 0x00 }, /* brick wall */
- { 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00 }, /* empty cell dot */
- };
-
- /* 4x4 masks, bit 15 = row 0 column 0. Order: I J L O S T Z. */
- static const unsigned int shapes[7][4] = {
- { 0x0F00, 0x2222, 0x00F0, 0x4444 },
- { 0x8E00, 0x6440, 0x0E20, 0x44C0 },
- { 0x2E00, 0x4460, 0x0E80, 0xC440 },
- { 0x6600, 0x6600, 0x6600, 0x6600 },
- { 0x6C00, 0x4620, 0x06C0, 0x8C40 },
- { 0x4E00, 0x4640, 0x0E40, 0x4C40 },
- { 0xC600, 0x2640, 0x0C60, 0x4C80 },
- };
- static const unsigned char piece_color[7] = {
- COLOR_CYAN, COLOR_LIGHTBLUE, COLOR_ORANGE, COLOR_YELLOW,
- COLOR_GREEN, COLOR_PURPLE, COLOR_RED
- };
-
- /* Frames per row at each level (PAL). */
- static const unsigned char gravity[21] = {
- 48, 42, 37, 32, 27, 22, 18, 14, 10, 8, 7, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1
- };
- static const unsigned int line_points[5] = { 0, 40, 100, 300, 1200 };
- static const signed char kicks[5] = { 0, -1, 1, -2, 2 };
-
- /* Title logo: 3x5 letters, bit 2 = left column. */
- static const unsigned char logo[9][5] = {
- { 7, 4, 7, 1, 7 }, /* S */
- { 7, 2, 2, 2, 2 }, /* T */
- { 7, 5, 7, 5, 5 }, /* A */
- { 7, 4, 4, 4, 7 }, /* C */
- { 5, 5, 6, 5, 5 }, /* K */
- { 7, 4, 6, 4, 4 }, /* F */
- { 7, 5, 7, 5, 5 }, /* A */
- { 4, 4, 4, 4, 7 }, /* L */
- { 4, 4, 4, 4, 7 }, /* L */
- };
-
- /* Sound effects: frequency high bytes, 0 ends. */
- static const unsigned char sfx_move[] = { 0x30, 0 };
- static const unsigned char sfx_rotate[] = { 0x40, 0x56, 0 };
- static const unsigned char sfx_lock[] = { 0x0A, 0x06, 0 };
- static const unsigned char sfx_slam[] = { 0x14, 0x0C, 0x08, 0 };
- static const unsigned char sfx_line[] = { 0x22, 0x2C, 0x34, 0x46, 0 };
- static const unsigned char sfx_quad[] = { 0x22, 0x2C, 0x34, 0x46, 0x57, 0x68, 0x8B, 0 };
- static const unsigned char sfx_over[] = { 0x46, 0x3A, 0x34, 0x2C, 0x27, 0x22, 0x1A, 0x11, 0 };
- static const unsigned char sfx_level[] = { 0x34, 0x46, 0x34, 0x46, 0x68, 0 };
-
- /* --------------------------------------------------------------- state */
-
- static unsigned int rowoff[25];
- static unsigned char cell_dx[7][4][4];
- static unsigned char cell_dy[7][4][4];
- static unsigned char board[BH][BW]; /* 0 = empty, else color */
-
- static unsigned char cur_p, cur_r, next_p;
- static signed char cur_x, cur_y, ghost_y;
- static unsigned char dr_p, dr_r, drawn; /* what is on screen now */
- static signed char dr_x, dr_y, dr_gy;
- static unsigned char dirty;
-
- static unsigned char bag[7], bag_pos = 7;
- static unsigned int rng = 0xACE1;
-
- static unsigned long score, hiscore;
- static unsigned int lines;
- static unsigned char level, start_level;
- static unsigned char frame;
- static unsigned char reported; /* test hook fired */
-
- /* SFX channels 0 = voice 1, 1 = voice 2 */
- static const unsigned char *sfx_ptr[2];
- static unsigned char sfx_timer[2], sfx_len[2], sfx_wave[2];
-
- static char numbuf[20];
-
- /* ----------------------------------------------------------- utilities */
-
- static unsigned char rnd(void)
- {
- rng ^= rng << 7;
- rng ^= rng >> 9;
- rng ^= rng << 8;
- return (unsigned char)rng;
- }
-
- static unsigned char bag_next(void)
- {
- unsigned char i, j, t;
- if (bag_pos >= 7) {
- for (i = 0; i < 7; ++i) bag[i] = i;
- for (i = 6; i > 0; --i) {
- j = rnd() % (i + 1);
- t = bag[i]; bag[i] = bag[j]; bag[j] = t;
- }
- bag_pos = 0;
- }
- return bag[bag_pos++];
- }
-
- static void sfx_play(unsigned char ch, const unsigned char *seq,
- unsigned char wave, unsigned char ad, unsigned char sr,
- unsigned char len)
- {
- struct __sid_voice *v = ch ? &SID.v2 : &SID.v1;
- v->ctrl = 0;
- v->ad = ad;
- v->sr = sr;
- sfx_ptr[ch] = seq;
- sfx_wave[ch] = wave;
- sfx_len[ch] = len;
- sfx_timer[ch] = 0;
- }
-
- static void sfx_tick(void)
- {
- unsigned char ch, note;
- struct __sid_voice *v;
- for (ch = 0; ch < 2; ++ch) {
- if (!sfx_ptr[ch]) continue;
- if (sfx_timer[ch]) { --sfx_timer[ch]; continue; }
- v = ch ? &SID.v2 : &SID.v1;
- note = *sfx_ptr[ch]++;
- if (note == 0) {
- v->ctrl = sfx_wave[ch]; /* gate off: release */
- sfx_ptr[ch] = 0;
- } else {
- v->freq = (unsigned int)note << 8;
- v->ctrl = sfx_wave[ch];
- v->ctrl = sfx_wave[ch] | 1;
- sfx_timer[ch] = sfx_len[ch] - 1;
- }
- }
- }
-
- /* Wait for the raster to reach the lower border: one call per PAL frame. */
- static void wait_frame(void)
- {
- while (VIC.rasterline != 251) ;
- while (VIC.rasterline == 251) ;
- ++frame;
- sfx_tick();
- }
-
- static void wait_frames(unsigned char n)
- {
- while (n--) wait_frame();
- }
-
- static void put_screen(unsigned char x, unsigned char y, unsigned char ch, unsigned char col)
- {
- unsigned int o = rowoff[y] + x;
- SCREEN[o] = ch;
- COLRAM[o] = col;
- }
-
- static void text(unsigned char x, unsigned char y, unsigned char col, const char *s)
- {
- textcolor(col);
- cputsxy(x, y, s);
- }
-
- static void text_center(unsigned char y, unsigned char col, const char *s)
- {
- text((40 - strlen(s)) / 2, y, col, s);
- }
-
- static void number(unsigned char x, unsigned char y, unsigned long v, unsigned char width)
- {
- unsigned char n;
- ultoa(v, numbuf + 6, 10);
- n = strlen(numbuf + 6);
- memset(numbuf, '0', 6);
- text(x, y, C_VALUE, numbuf + 6 + n - width);
- }
-
- static unsigned char read_input(void)
- {
- unsigned char in = 0;
- unsigned char j = ~CIA1.pra;
- unsigned char k = PEEK(0xCB);
- unsigned char sh = PEEK(0x28D) & 1;
-
- if (j & 0x01) in |= IN_DROP;
- if (j & 0x02) in |= IN_DOWN;
- if (j & 0x04) in |= IN_LEFT;
- if (j & 0x08) in |= IN_RIGHT;
- if (j & 0x10) in |= IN_ROT | IN_START;
-
- switch (k) {
- case 10: in |= IN_LEFT; break; /* A */
- case 18: in |= IN_RIGHT; break; /* D */
- case 2: in |= sh ? IN_LEFT : IN_RIGHT; break; /* CRSR <-> */
- case 9: case 23: in |= IN_ROT; break; /* W, X */
- case 12: in |= IN_CCW; break; /* Z */
- case 13: in |= IN_DOWN; break; /* S */
- case 7: in |= sh ? IN_ROT : IN_DOWN; break; /* CRSR up/down */
- case 60: in |= IN_DROP | IN_START; break; /* SPACE */
- case 1: in |= IN_START; break; /* RETURN */
- case 41: in |= IN_PAUSE; break; /* P */
- }
- POKE(0xC6, 0); /* drop buffered keys */
- return in;
- }
-
- /* --------------------------------------------------------- game rules */
-
- static void build_cells(void)
- {
- unsigned char p, r, i, bit;
- unsigned int m;
- for (p = 0; p < 7; ++p)
- for (r = 0; r < 4; ++r) {
- m = shapes[p][r];
- i = 0;
- for (bit = 0; bit < 16; ++bit)
- if (m & (0x8000u >> bit)) {
- if (i < 4) {
- cell_dx[p][r][i] = bit & 3;
- cell_dy[p][r][i] = bit >> 2;
- }
- ++i;
- }
- }
- }
-
- static unsigned char fits(unsigned char p, unsigned char r, signed char x, signed char y)
- {
- unsigned char i;
- signed char cx, cy;
- const unsigned char *dx = cell_dx[p][r];
- const unsigned char *dy = cell_dy[p][r];
- for (i = 0; i < 4; ++i) {
- cx = x + dx[i];
- cy = y + dy[i];
- if (cx < 0 || cx >= BW || cy >= BH) return 0;
- if (cy >= 0 && board[cy][cx]) return 0;
- }
- return 1;
- }
-
- /* Rotate the current piece by dir (1 = clockwise, 3 = counter), with kicks. */
- static unsigned char try_rotate(unsigned char dir)
- {
- unsigned char r2 = (cur_r + dir) & 3, k;
- for (k = 0; k < 5; ++k)
- if (fits(cur_p, r2, cur_x + kicks[k], cur_y)) {
- cur_r = r2;
- cur_x += kicks[k];
- return 1;
- }
- if (fits(cur_p, r2, cur_x, cur_y - 1)) { /* floor kick */
- cur_r = r2;
- --cur_y;
- return 1;
- }
- return 0;
- }
-
- static unsigned char row_full(unsigned char y)
- {
- unsigned char x;
- for (x = 0; x < BW; ++x)
- if (!board[y][x]) return 0;
- return 1;
- }
-
- /* Remove full rows; returns how many. */
- static unsigned char collapse(void)
- {
- unsigned char y, n = 0;
- for (y = 0; y < BH; ++y)
- if (row_full(y)) {
- memmove(board[1], board[0], y * BW);
- memset(board[0], 0, BW);
- ++n;
- }
- return n;
- }
-
- static unsigned long points_for(unsigned char n, unsigned char lvl)
- {
- return (unsigned long)line_points[n] * (lvl + 1);
- }
-
- static signed char drop_y(void)
- {
- signed char y = cur_y;
- while (fits(cur_p, cur_r, cur_x, y + 1)) ++y;
- return y;
- }
-
- /* ------------------------------------------------------------ drawing */
-
- static void draw_cell(signed char x, signed char y)
- {
- unsigned char c;
- if (y < 0) return;
- c = board[y][x];
- if (c) put_screen(WELL_X + x, WELL_Y + y, CH_BLOCK, c);
- else put_screen(WELL_X + x, WELL_Y + y, CH_DOT, C_DOT);
- }
-
- static void paint_cells(unsigned char p, unsigned char r, signed char x, signed char y,
- unsigned char ch, unsigned char col)
- {
- unsigned char i;
- signed char cy;
- for (i = 0; i < 4; ++i) {
- cy = y + cell_dy[p][r][i];
- if (cy >= 0)
- put_screen(WELL_X + x + cell_dx[p][r][i], WELL_Y + cy, ch, col);
- }
- }
-
- static void restore_cells(unsigned char p, unsigned char r, signed char x, signed char y)
- {
- unsigned char i;
- for (i = 0; i < 4; ++i)
- draw_cell(x + cell_dx[p][r][i], y + cell_dy[p][r][i]);
- }
-
- static void erase_piece(void)
- {
- if (!drawn) return;
- restore_cells(dr_p, dr_r, dr_x, dr_gy);
- restore_cells(dr_p, dr_r, dr_x, dr_y);
- drawn = 0;
- }
-
- static void show_piece(void)
- {
- erase_piece();
- paint_cells(cur_p, cur_r, cur_x, ghost_y, CH_GHOST, piece_color[cur_p]);
- paint_cells(cur_p, cur_r, cur_x, cur_y, CH_BLOCK, piece_color[cur_p]);
- dr_p = cur_p; dr_r = cur_r; dr_x = cur_x; dr_y = cur_y; dr_gy = ghost_y;
- drawn = 1;
- dirty = 0;
- }
-
- static void draw_well(void)
- {
- signed char x, y;
- for (y = 0; y < BH; ++y)
- for (x = 0; x < BW; ++x)
- draw_cell(x, y);
- }
-
- static void draw_frame(void)
- {
- unsigned char y;
- for (y = WELL_Y; y <= WELL_Y + BH; ++y) {
- put_screen(WELL_X - 1, y, CH_WALL, C_WALL);
- put_screen(WELL_X + BW, y, CH_WALL, C_WALL);
- }
- for (y = 0; y < BW; ++y)
- put_screen(WELL_X + y, WELL_Y + BH, CH_WALL, C_WALL);
- }
-
- static void draw_next(void)
- {
- unsigned char i, ox = 28, oy = 5;
- for (i = 0; i < 4; ++i) {
- put_screen(ox + i, oy, CH_SPACE, 0);
- put_screen(ox + i, oy + 1, CH_SPACE, 0);
- }
- for (i = 0; i < 4; ++i)
- put_screen(ox + cell_dx[next_p][0][i],
- oy + cell_dy[next_p][0][i] - (next_p == 0),
- CH_BLOCK, piece_color[next_p]);
- }
-
- static void draw_stats(void)
- {
- number(3, 4, score, 6);
- number(3, 8, lines, 3);
- number(3, 12, level, 2);
- number(3, 16, hiscore, 6);
- }
-
- static void draw_hud(void)
- {
- text(3, 3, C_LABEL, "SCORE");
- text(3, 7, C_LABEL, "LINES");
- text(3, 11, C_LABEL, "LEVEL");
- text(3, 15, C_LABEL, "BEST");
- text(28, 3, C_LABEL, "NEXT");
- text(28, 10, C_LABEL, "A D MOVE");
- text(28, 11, C_LABEL, "W TURN");
- text(28, 12, C_LABEL, "Z BACK");
- text(28, 13, C_LABEL, "S DOWN");
- text(28, 14, C_LABEL, "SPC DROP");
- text(28, 15, C_LABEL, "P PAUSE");
- text(28, 17, C_DOT, "OR JOY 2");
- text(28, 18, C_DOT, "FIRE TURN");
- text(28, 19, C_DOT, "UP DROP");
- draw_stats();
- }
-
- /* ------------------------------------------------------------- setup */
-
- static void install_charset(void)
- {
- __asm__("sei");
- POKE(0x01, 0x33); /* character ROM in at $D000 */
- memcpy(CHARSET, (void *)0xD800, 0x800); /* upper/lower case set */
- POKE(0x01, 0x37);
- __asm__("cli");
- memcpy(CHARSET + CH_BLOCK * 8, glyphs, sizeof glyphs);
- VIC.addr = 0x1E; /* screen $0400, chars $3800 */
- POKE(0x291, 0x80); /* lock out Shift+C= case switch */
- }
-
- static void setup(void)
- {
- unsigned char i;
- for (i = 0; i < 25; ++i) rowoff[i] = i * 40;
- build_cells();
- install_charset();
- bordercolor(COLOR_BLACK);
- bgcolor(COLOR_BLACK);
- cursor(0);
- SID.v1.pw = 0x0800;
- SID.v2.pw = 0x0800;
- SID.amp = 0x0F;
- }
-
- /* --------------------------------------------------------- self-test */
-
- extern unsigned char _BSS_RUN__[], _BSS_SIZE__[];
-
- static void self_test(void)
- {
- unsigned char p, r, i, a;
-
- /* Program, data and BSS must stay clear of the charset at $3800. */
- c64test_check((unsigned int)_BSS_RUN__ + (unsigned int)_BSS_SIZE__ <= 0x3800u, 1);
-
- /* Every shape has exactly four cells, all within its 4x4 box. */
- for (p = 0; p < 7; ++p)
- for (r = 0; r < 4; ++r) {
- a = 0;
- for (i = 0; i < 16; ++i)
- if (shapes[p][r] & (0x8000u >> i)) ++a;
- c64test_check(a == 4, 2);
- }
-
- memset(board, 0, sizeof board);
- /* Spawn fits; an I bar sticking out the right wall does not. */
- c64test_check(fits(0, 0, 3, -1), 3);
- c64test_check(!fits(0, 0, 7, 0), 4);
- c64test_check(fits(0, 0, 6, 0), 5);
- /* Vertical I against the left wall: rotating needs a kick. */
- cur_p = 0; cur_r = 1; cur_x = -2; cur_y = 5;
- c64test_check(fits(0, 1, -2, 5), 6);
- c64test_check(try_rotate(1) && cur_r == 2 && fits(0, 2, cur_x, cur_y), 7);
- /* Four rotations return to the start. */
- cur_p = 5; cur_r = 0; cur_x = 4; cur_y = 5;
- for (i = 0; i < 4; ++i) try_rotate(1);
- c64test_check(cur_r == 0 && cur_x == 4, 8);
- /* Hard drop lands on the floor: an O piece ends with its cells on row 19. */
- cur_p = 3; cur_r = 0; cur_x = 4; cur_y = 0;
- c64test_check(drop_y() == BH - 2, 9);
-
- /* Line clear: two full rows, one partial, a marker above. */
- memset(board[19], 2, BW);
- memset(board[18], 3, BW);
- board[17][0] = 4;
- board[16][5] = 7;
- c64test_check(collapse() == 2, 10);
- c64test_check(board[19][0] == 4 && board[18][5] == 7, 11);
- c64test_check(board[17][5] == 0 && !row_full(19), 12);
-
- /* Scoring. */
- c64test_check(points_for(4, 2) == 3600, 13);
- c64test_check(points_for(1, 0) == 40, 14);
-
- /* A bag deals each of the seven pieces once. */
- bag_pos = 7;
- a = 0;
- for (i = 0; i < 7; ++i) a |= 1 << bag_next();
- c64test_check(a == 0x7F, 15);
-
- memset(board, 0, sizeof board);
- }
-
- /* ------------------------------------------------------------- title */
-
- static void draw_logo(unsigned char shift)
- {
- unsigned char i, row, col, bits, c;
- for (i = 0; i < 9; ++i) {
- c = piece_color[(i + shift) % 7];
- for (row = 0; row < 5; ++row) {
- bits = logo[i][row];
- for (col = 0; col < 3; ++col)
- put_screen(3 + i * 4 + col, 3 + row,
- (bits & (4 >> col)) ? CH_BLOCK : CH_SPACE, c);
- }
- }
- }
-
- static void title(void)
- {
- unsigned char in, prev = 0xFF, shift = 0, edges;
- char k;
-
- clrscr();
- draw_logo(0);
- text_center(10, C_LABEL, "A FALLING BLOCK PUZZLE");
- text(11, 13, C_LABEL, "START LEVEL < >");
- text(11, 15, C_LABEL, "BEST SCORE");
- number(23, 15, hiscore, 6);
- text_center(18, C_DOT, "JOYSTICK PORT 2 OR KEYBOARD");
- text_center(19, C_DOT, "LEFT/RIGHT PICKS THE LEVEL");
-
- for (;;) {
- wait_frame();
- rnd(); /* stir the generator */
- number(26, 13, start_level, 1);
- if ((frame & 15) == 0) draw_logo(++shift);
- text_center(22, (frame & 32) ? COLOR_WHITE : C_DOT,
- "PRESS FIRE OR SPACE TO PLAY");
-
- k = kbhit() ? cgetc() : 0; /* keys typed into the buffer */
- in = read_input();
- edges = in & ~prev;
- prev = in;
- if (k == ' ' || k == '\n') edges |= IN_START;
- if ((edges & IN_LEFT) && start_level > 0) { --start_level; sfx_play(0, sfx_move, 0x10, 0x01, 0, 1); }
- if ((edges & IN_RIGHT) && start_level < 9) { ++start_level; sfx_play(0, sfx_move, 0x10, 0x01, 0, 1); }
- if (edges & IN_START) break;
- }
- rng ^= frame;
- if (!rng) rng = 1;
- }
-
- /* -------------------------------------------------------------- play */
-
- static unsigned char spawn(void)
- {
- cur_p = next_p;
- next_p = bag_next();
- cur_r = 0;
- cur_x = 3;
- cur_y = (cur_p == 0) ? -1 : 0;
- draw_next();
- if (!fits(cur_p, cur_r, cur_x, cur_y)) return 0;
- ghost_y = drop_y();
- dirty = 1;
- return 1;
- }
-
- static void clear_animation(void)
- {
- unsigned char y, x, step;
- for (y = 0; y < BH; ++y)
- if (row_full(y))
- for (x = 0; x < BW; ++x)
- put_screen(WELL_X + x, WELL_Y + y, CH_BLOCK, COLOR_WHITE);
- wait_frames(5);
- for (step = 0; step < 5; ++step) {
- for (y = 0; y < BH; ++y)
- if (row_full(y)) {
- put_screen(WELL_X + 4 - step, WELL_Y + y, CH_SPACE, 0);
- put_screen(WELL_X + 5 + step, WELL_Y + y, CH_SPACE, 0);
- }
- wait_frames(2);
- }
- }
-
- /* Lock the current piece. Returns 0 if the game is over. */
- static unsigned char lock_piece(void)
- {
- unsigned char i, n, old_level;
- signed char cy;
-
- erase_piece();
- for (i = 0; i < 4; ++i) {
- cy = cur_y + cell_dy[cur_p][cur_r][i];
- if (cy < 0) return 0;
- board[cy][cur_x + cell_dx[cur_p][cur_r][i]] = piece_color[cur_p];
- }
- restore_cells(cur_p, cur_r, cur_x, cur_y);
-
- n = 0;
- for (i = 0; i < BH; ++i) n += row_full(i);
- if (n) {
- if (n == 4) sfx_play(1, sfx_quad, 0x40, 0x09, 0x84, 3);
- else sfx_play(1, sfx_line, 0x40, 0x09, 0x84, 3);
- clear_animation();
- collapse();
- draw_well();
- score += points_for(n, level);
- lines += n;
- old_level = level;
- if (lines / 10 > level) level = lines / 10;
- if (level != old_level && n != 4) sfx_play(1, sfx_level, 0x20, 0x09, 0x84, 4);
- }
- if (score > hiscore) hiscore = score;
- draw_stats();
-
- if (!reported) { /* test hook: first piece has fallen and locked */
- reported = 1;
- c64test_pass();
- }
- return spawn();
- }
-
- static void game_over(void)
- {
- signed char y;
- unsigned char x, in, prev = 0xFF;
-
- erase_piece();
- sfx_play(1, sfx_over, 0x20, 0x09, 0x89, 6);
- for (y = BH - 1; y >= 0; --y) {
- for (x = 0; x < BW; ++x)
- put_screen(WELL_X + x, WELL_Y + y, CH_BLOCK, C_DOT);
- wait_frames(2);
- }
- for (y = 8; y <= 12; ++y)
- for (x = 0; x < BW; ++x)
- put_screen(WELL_X + x, WELL_Y + y, CH_SPACE, 0);
- text(WELL_X + 3, WELL_Y + 9, COLOR_WHITE, "GAME");
- text(WELL_X + 3, WELL_Y + 11, COLOR_WHITE, "OVER");
- wait_frames(50);
- for (;;) {
- wait_frame();
- in = read_input();
- if (in & ~prev & IN_START) break;
- prev = in;
- }
- }
-
- static void pause_game(void)
- {
- unsigned char x, in, prev = 0xFF;
- for (x = 0; x < BW; ++x) put_screen(WELL_X + x, WELL_Y + 9, CH_SPACE, 0);
- text(WELL_X + 2, WELL_Y + 9, COLOR_WHITE, "PAUSED");
- for (;;) {
- wait_frame();
- in = read_input();
- if (in & ~prev & (IN_PAUSE | IN_START)) break;
- prev = in;
- }
- draw_well();
- drawn = 0;
- dirty = 1;
- }
-
- static void play(void)
- {
- unsigned char in, prev = 0xFF, edges;
- unsigned char grav = 0, das = 0, soft = 0, lock_t = 0, resets = 0;
- signed char dir, last_dir = 0, rows;
-
- clrscr();
- memset(board, 0, sizeof board);
- score = 0;
- lines = 0;
- level = start_level;
- drawn = 0;
- bag_pos = 7;
- draw_frame();
- draw_well();
- draw_hud();
- next_p = bag_next();
- spawn();
-
- for (;;) {
- wait_frame();
- if (dirty) show_piece();
-
- in = read_input();
- edges = in & ~prev;
- prev = in;
-
- if (edges & IN_PAUSE) { pause_game(); continue; }
-
- /* Horizontal movement with delayed auto-repeat. */
- dir = (in & IN_LEFT) ? -1 : (in & IN_RIGHT) ? 1 : 0;
- if (dir != last_dir) das = 0;
- if (dir && (dir != last_dir || ++das >= DAS_DELAY)) {
- if (das >= DAS_DELAY) das = DAS_DELAY - DAS_RATE;
- if (fits(cur_p, cur_r, cur_x + dir, cur_y)) {
- cur_x += dir;
- dirty = 1;
- sfx_play(0, sfx_move, 0x10, 0x01, 0, 1);
- if (lock_t && resets < LOCK_RESETS) { lock_t = 0; ++resets; }
- }
- }
- last_dir = dir;
-
- /* Rotation. */
- if (edges & (IN_ROT | IN_CCW)) {
- if (try_rotate((edges & IN_ROT) ? 1 : 3)) {
- dirty = 1;
- sfx_play(0, sfx_rotate, 0x10, 0x02, 0, 1);
- if (lock_t && resets < LOCK_RESETS) { lock_t = 0; ++resets; }
- }
- }
-
- /* Hard drop. */
- if (edges & IN_DROP) {
- rows = ghost_y - cur_y;
- cur_y = ghost_y;
- score += rows * 2;
- sfx_play(1, sfx_slam, 0x80, 0x04, 0, 1);
- if (!lock_piece()) break;
- grav = lock_t = resets = 0;
- continue;
- }
-
- if (dirty) ghost_y = drop_y();
-
- /* Soft drop and gravity. */
- if ((in & IN_DOWN) && ++soft >= 2) {
- soft = 0;
- if (cur_y < ghost_y) {
- ++cur_y;
- ++score;
- grav = 0;
- dirty = 1;
- }
- }
- if (++grav >= gravity[level > 20 ? 20 : level]) {
- grav = 0;
- if (cur_y < ghost_y) { ++cur_y; dirty = 1; }
- }
-
- /* Lock once grounded long enough (at once while soft-dropping). */
- if (cur_y >= ghost_y) {
- if (++lock_t >= LOCK_DELAY || (in & IN_DOWN)) {
- sfx_play(1, sfx_lock, 0x80, 0x03, 0, 1);
- if (!lock_piece()) break;
- grav = lock_t = resets = 0;
- }
- } else {
- lock_t = 0;
- }
- }
- game_over();
- }
-
- int main(void)
- {
- setup();
- self_test();
- for (;;) {
- title();
- play();
- }
- }
|