2000-01-21 05:10:56 +03:00
|
|
|
/* $NetBSD: tetris.c,v 1.14 2000/01/21 02:10:57 jsm Exp $ */
|
1995-04-22 11:42:31 +04:00
|
|
|
|
1994-05-06 10:50:50 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Chris Torek and Darren F. Provine.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* @(#)tetris.c 8.1 (Berkeley) 5/31/93
|
|
|
|
*/
|
|
|
|
|
1997-10-12 06:03:45 +04:00
|
|
|
#include <sys/cdefs.h>
|
1994-05-06 10:50:50 +04:00
|
|
|
#ifndef lint
|
1997-10-12 06:03:45 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1994-05-06 10:50:50 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tetris (or however it is spelled).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2000-01-21 05:10:56 +03:00
|
|
|
#include <err.h>
|
1999-09-12 13:02:20 +04:00
|
|
|
#include <fcntl.h>
|
1994-05-06 10:50:50 +04:00
|
|
|
#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"
|
|
|
|
|
2000-01-01 13:15:17 +03:00
|
|
|
cell board[B_SIZE]; /* 1 => occupied, 0 => empty */
|
|
|
|
|
|
|
|
int Rows, Cols; /* current screen size */
|
|
|
|
|
|
|
|
const struct shape *curshape;
|
|
|
|
const struct shape *nextshape;
|
|
|
|
|
|
|
|
long fallrate; /* less than 1 million; smaller => faster */
|
|
|
|
|
|
|
|
int score; /* the obvious thing */
|
1999-09-12 13:02:20 +04:00
|
|
|
gid_t gid, egid;
|
|
|
|
|
2000-01-01 13:15:17 +03:00
|
|
|
char key_msg[100];
|
|
|
|
int showpreview;
|
|
|
|
|
1997-10-12 06:03:45 +04:00
|
|
|
static void elide __P((void));
|
|
|
|
static void setup_board __P((void));
|
|
|
|
int main __P((int, char **));
|
1998-09-13 19:27:25 +04:00
|
|
|
void onintr __P((int)) __attribute__((__noreturn__));
|
|
|
|
void usage __P((void)) __attribute__((__noreturn__));
|
1994-05-06 10:50:50 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the initial board. The bottom display row is completely set,
|
|
|
|
* along with another (hidden) row underneath that. Also, the left and
|
|
|
|
* right edges are set.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
setup_board()
|
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
register cell *p;
|
|
|
|
|
|
|
|
p = board;
|
|
|
|
for (i = B_SIZE; i; i--)
|
|
|
|
*p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Elide any full active rows.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
elide()
|
|
|
|
{
|
|
|
|
register int i, j, base;
|
|
|
|
register 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) {
|
|
|
|
/* this row is to be elided */
|
1998-08-10 06:23:45 +04:00
|
|
|
memset(&board[base], 0, B_COLS - 2);
|
1994-05-06 10:50:50 +04:00
|
|
|
scr_update();
|
|
|
|
tsleep();
|
|
|
|
while (--base != 0)
|
|
|
|
board[base + B_COLS] = board[base];
|
|
|
|
scr_update();
|
|
|
|
tsleep();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
register int pos, c;
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
register const char *keys;
|
1994-05-06 10:50:50 +04:00
|
|
|
register int level = 2;
|
|
|
|
char key_write[6][10];
|
|
|
|
int ch, i, j;
|
1999-09-12 13:02:20 +04:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
gid = getgid();
|
|
|
|
egid = getegid();
|
|
|
|
setegid(gid);
|
|
|
|
|
|
|
|
fd = open("/dev/null", O_RDONLY);
|
|
|
|
if (fd < 3)
|
|
|
|
exit(1);
|
|
|
|
close(fd);
|
1994-05-06 10:50:50 +04:00
|
|
|
|
|
|
|
keys = "jkl pq";
|
|
|
|
|
1999-01-03 20:13:51 +03:00
|
|
|
while ((ch = getopt(argc, argv, "k:l:ps")) != -1)
|
1994-05-06 10:50:50 +04:00
|
|
|
switch(ch) {
|
|
|
|
case 'k':
|
|
|
|
if (strlen(keys = optarg) != 6)
|
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
level = atoi(optarg);
|
|
|
|
if (level < MINLEVEL || level > MAXLEVEL) {
|
2000-01-21 05:10:56 +03:00
|
|
|
errx(1, "level must be from %d to %d",
|
|
|
|
MINLEVEL, MAXLEVEL);
|
1994-05-06 10:50:50 +04:00
|
|
|
}
|
|
|
|
break;
|
1999-01-03 20:13:51 +03:00
|
|
|
case 'p':
|
|
|
|
showpreview = 1;
|
|
|
|
break;
|
1994-05-06 10:50:50 +04:00
|
|
|
case 's':
|
|
|
|
showscores(0);
|
|
|
|
exit(0);
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
fallrate = 1000000 / level;
|
|
|
|
|
|
|
|
for (i = 0; i <= 5; i++) {
|
|
|
|
for (j = i+1; j <= 5; j++) {
|
|
|
|
if (keys[i] == keys[j]) {
|
2000-01-21 05:10:56 +03:00
|
|
|
errx(1, "duplicate command keys specified.");
|
1994-05-06 10:50:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keys[i] == ' ')
|
|
|
|
strcpy(key_write[i], "<space>");
|
|
|
|
else {
|
|
|
|
key_write[i][0] = keys[i];
|
|
|
|
key_write[i][1] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(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();
|
|
|
|
setup_board();
|
|
|
|
|
|
|
|
srandom(getpid());
|
|
|
|
scr_set();
|
|
|
|
|
|
|
|
pos = A_FIRST*B_COLS + (B_COLS/2)-1;
|
1999-01-03 05:00:17 +03:00
|
|
|
nextshape = randshape();
|
1994-05-06 10:50:50 +04:00
|
|
|
curshape = randshape();
|
|
|
|
|
|
|
|
scr_msg(key_msg, 1);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
place(curshape, pos, 1);
|
|
|
|
scr_update();
|
|
|
|
place(curshape, pos, 0);
|
|
|
|
c = tgetchar();
|
|
|
|
if (c < 0) {
|
|
|
|
/*
|
|
|
|
* Timeout. Move down if possible.
|
|
|
|
*/
|
|
|
|
if (fits_in(curshape, pos + B_COLS)) {
|
|
|
|
pos += B_COLS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put up the current shape `permanently',
|
|
|
|
* bump score, and elide any full rows.
|
|
|
|
*/
|
|
|
|
place(curshape, pos, 1);
|
|
|
|
score++;
|
|
|
|
elide();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Choose a new shape. If it does not fit,
|
|
|
|
* the game is over.
|
|
|
|
*/
|
1999-01-03 05:00:17 +03:00
|
|
|
curshape = nextshape;
|
|
|
|
nextshape = randshape();
|
1994-05-06 10:50:50 +04:00
|
|
|
pos = A_FIRST*B_COLS + (B_COLS/2)-1;
|
|
|
|
if (!fits_in(curshape, pos))
|
|
|
|
break;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Handle command keys.
|
|
|
|
*/
|
|
|
|
if (c == keys[5]) {
|
|
|
|
/* quit */
|
|
|
|
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((struct timeval *)NULL) == -1);
|
|
|
|
scr_msg(msg, 0);
|
|
|
|
scr_msg(key_msg, 1);
|
|
|
|
place(curshape, pos, 0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == keys[0]) {
|
|
|
|
/* move left */
|
|
|
|
if (fits_in(curshape, pos - 1))
|
|
|
|
pos--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == keys[1]) {
|
|
|
|
/* turn */
|
Add use of `const' where appropriate to the games.
This merges in all such remaining changes from the Linux port of the
NetBSD games, except in hunt (where substantial changes from OpenBSD
need to be looked at).
Some such changes were previously covered in PRs bin/6041, bin/6146,
bin/6148, bin/6150, bin/6151, bin/6580, bin/6660, bin/7993, bin/7994,
bin/8039, bin/8057 and bin/8093.
1999-09-09 01:17:44 +04:00
|
|
|
const struct shape *new = &shapes[curshape->rot];
|
1994-05-06 10:50:50 +04:00
|
|
|
|
|
|
|
if (fits_in(new, pos))
|
|
|
|
curshape = new;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == keys[2]) {
|
|
|
|
/* move right */
|
|
|
|
if (fits_in(curshape, pos + 1))
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (c == keys[3]) {
|
|
|
|
/* move to bottom */
|
|
|
|
while (fits_in(curshape, pos + B_COLS)) {
|
|
|
|
pos += B_COLS;
|
|
|
|
score++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
1999-01-03 05:00:17 +03:00
|
|
|
if (c == '\f') {
|
1994-05-06 10:50:50 +04:00
|
|
|
scr_clear();
|
1999-01-03 05:00:17 +03:00
|
|
|
scr_msg(key_msg, 1);
|
|
|
|
}
|
1994-05-06 10:50:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
scr_clear();
|
|
|
|
scr_end();
|
|
|
|
|
|
|
|
(void)printf("Your score: %d point%s x level %d = %d\n",
|
|
|
|
score, score == 1 ? "" : "s", level, score * level);
|
|
|
|
savescore(level);
|
|
|
|
|
|
|
|
printf("\nHit RETURN to see high scores, ^C to skip.\n");
|
|
|
|
|
|
|
|
while ((i = getchar()) != '\n')
|
|
|
|
if (i == EOF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
showscores(level);
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
onintr(signo)
|
1999-09-09 01:45:25 +04:00
|
|
|
int signo __attribute__((__unused__));
|
1994-05-06 10:50:50 +04:00
|
|
|
{
|
|
|
|
scr_clear();
|
|
|
|
scr_end();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usage()
|
|
|
|
{
|
1999-03-22 09:12:23 +03:00
|
|
|
(void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n");
|
1994-05-06 10:50:50 +04:00
|
|
|
exit(1);
|
|
|
|
}
|