#include <err.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "input.h"
#include "scores.h"
#include "screen.h"
#include "tetris.h"
#define NUMKEYS 6
cell board[B_SIZE];
int Rows, Cols;
const struct shape *curshape;
const struct shape *nextshape;
long fallrate;
int score;
char key_msg[100];
char scorepath[PATH_MAX];
int showpreview, classic;
static void elide(void);
void onintr(int);
const struct shape *randshape(void);
static void setup_board(void);
__dead void usage(void);
static void
setup_board(void)
{
int i;
cell *p;
p = board;
for (i = B_SIZE; i; i--)
*p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
}
static void
elide(void)
{
int rows = 0;
int i, j, base;
cell *p;
for (i = A_FIRST; i < A_LAST; i++) {
base = i * B_COLS + 1;
p = &board[base];
for (j = B_COLS - 2; *p++ != 0;) {
if (--j <= 0) {
rows++;
memset(&board[base], 0, B_COLS - 2);
scr_update();
tsleep();
while (--base != 0)
board[base + B_COLS] = board[base];
memset(&board[1], 0, B_COLS - 2);
scr_update();
tsleep();
break;
}
}
}
switch (rows) {
case 1:
score += 10;
break;
case 2:
score += 30;
break;
case 3:
score += 70;
break;
case 4:
score += 150;
break;
default:
break;
}
}
const struct shape *
randshape(void)
{
const struct shape *tmp;
int i, j;
tmp = &shapes[arc4random_uniform(7)];
j = arc4random_uniform(4);
for (i = 0; i < j; i++)
tmp = &shapes[classic? tmp->rotc : tmp->rot];
return (tmp);
}
int
main(int argc, char *argv[])
{
int pos, c;
char *keys;
int level = 2, ret;
char key_write[NUMKEYS][10];
char *home;
const char *errstr;
int ch, i, j;
home = getenv("HOME");
if (home == NULL || *home == '\0')
err(1, "getenv");
ret = snprintf(scorepath, sizeof(scorepath), "%s/%s", home,
".tetris.scores");
if (ret < 0 || ret >= PATH_MAX)
errc(1, ENAMETOOLONG, "%s/%s", home, ".tetris.scores");
if (pledge("stdio rpath wpath cpath tty unveil", NULL) == -1)
err(1, "pledge");
keys = "jkl pq";
classic = showpreview = 0;
while ((ch = getopt(argc, argv, "ck:l:ps")) != -1)
switch(ch) {
case 'c':
classic = 1;
break;
case 'k':
if (strlen(keys = optarg) != NUMKEYS)
usage();
break;
case 'l':
level = (int)strtonum(optarg, MINLEVEL, MAXLEVEL,
&errstr);
if (errstr)
errx(1, "level must be from %d to %d",
MINLEVEL, MAXLEVEL);
break;
case 'p':
showpreview = 1;
break;
case 's':
showscores(0);
return 0;
default:
usage();
}
argc -= optind;
argv += optind;
if (argc)
usage();
fallrate = 1000000000L / level;
for (i = 0; i < NUMKEYS; i++) {
for (j = i+1; j < NUMKEYS; j++) {
if (keys[i] == keys[j])
errx(1, "duplicate command keys specified.");
}
if (keys[i] == ' ')
strlcpy(key_write[i], "<space>", sizeof key_write[i]);
else {
key_write[i][0] = keys[i];
key_write[i][1] = '\0';
}
}
snprintf(key_msg, sizeof key_msg,
"%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
key_write[0], key_write[1], key_write[2], key_write[3],
key_write[4], key_write[5]);
(void)signal(SIGINT, onintr);
scr_init();
if (unveil(scorepath, "rwc") == -1)
err(1, "unveil %s", scorepath);
if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
err(1, "pledge");
setup_board();
scr_set();
pos = A_FIRST*B_COLS + (B_COLS/2)-1;
nextshape = randshape();
curshape = randshape();
scr_msg(key_msg, 1);
for (;;) {
place(curshape, pos, 1);
scr_update();
place(curshape, pos, 0);
c = tgetchar();
if (c < 0) {
if (fits_in(curshape, pos + B_COLS)) {
pos += B_COLS;
continue;
}
place(curshape, pos, 1);
score++;
elide();
curshape = nextshape;
nextshape = randshape();
pos = A_FIRST*B_COLS + (B_COLS/2)-1;
if (!fits_in(curshape, pos))
break;
continue;
}
if (c == keys[5]) {
break;
}
if (c == keys[4]) {
static char msg[] =
"paused - press RETURN to continue";
place(curshape, pos, 1);
do {
scr_update();
scr_msg(key_msg, 0);
scr_msg(msg, 1);
(void) fflush(stdout);
} while (rwait(NULL) == -1);
scr_msg(msg, 0);
scr_msg(key_msg, 1);
place(curshape, pos, 0);
continue;
}
if (c == keys[0]) {
if (fits_in(curshape, pos - 1))
pos--;
continue;
}
if (c == keys[1]) {
const struct shape *new = &shapes[
classic? curshape->rotc : curshape->rot];
if (fits_in(new, pos))
curshape = new;
continue;
}
if (c == keys[2]) {
if (fits_in(curshape, pos + 1))
pos++;
continue;
}
if (c == keys[3]) {
while (fits_in(curshape, pos + B_COLS)) {
pos += B_COLS;
score++;
}
continue;
}
if (c == '\f') {
scr_clear();
scr_msg(key_msg, 1);
}
}
scr_clear();
scr_end();
if (showpreview == 0)
(void)printf("Your score: %d point%s x level %d = %d\n",
score, score == 1 ? "" : "s", level, score * level);
else {
(void)printf("Your score: %d point%s x level %d x preview penalty %0.3f = %d\n",
score, score == 1 ? "" : "s", level, (double)PRE_PENALTY,
(int)(score * level * PRE_PENALTY));
score = score * PRE_PENALTY;
}
savescore(level);
printf("\nHit RETURN to see high scores, ^C to skip.\n");
while ((i = getchar()) != '\n')
if (i == EOF)
break;
showscores(level);
return 0;
}
void
onintr(int signo)
{
scr_clear();
scr_end();
_exit(0);
}
void
usage(void)
{
(void)fprintf(stderr, "usage: %s [-cps] [-k keys] "
"[-l level]\n", getprogname());
exit(1);
}