1 /* $NetBSD: tetris.c,v 1.14 2000/01/21 02:10:57 jsm Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek and Darren F. Provine. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)tetris.c 8.1 (Berkeley) 5/31/93 39 */ 40 41 #include <sys/cdefs.h> 42 #ifndef lint 43 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\ 44 The Regents of the University of California. All rights reserved.\n"); 45 #endif /* not lint */ 46 47 /* 48 * Tetris (or however it is spelled). 49 */ 50 51 #include <sys/time.h> 52 53 #include <err.h> 54 #include <fcntl.h> 55 #include <signal.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include "input.h" 62 #include "scores.h" 63 #include "screen.h" 64 #include "tetris.h" 65 66 cell board[B_SIZE]; /* 1 => occupied, 0 => empty */ 67 68 int Rows, Cols; /* current screen size */ 69 70 const struct shape *curshape; 71 const struct shape *nextshape; 72 73 long fallrate; /* less than 1 million; smaller => faster */ 74 75 int score; /* the obvious thing */ 76 gid_t gid, egid; 77 78 char key_msg[100]; 79 int showpreview; 80 81 static void elide __P((void)); 82 static void setup_board __P((void)); 83 int main __P((int, char **)); 84 void onintr __P((int)) __attribute__((__noreturn__)); 85 void usage __P((void)) __attribute__((__noreturn__)); 86 87 /* 88 * Set up the initial board. The bottom display row is completely set, 89 * along with another (hidden) row underneath that. Also, the left and 90 * right edges are set. 91 */ 92 static void 93 setup_board() 94 { 95 register int i; 96 register cell *p; 97 98 p = board; 99 for (i = B_SIZE; i; i--) 100 *p++ = i <= (2 * B_COLS) || (i % B_COLS) < 2; 101 } 102 103 /* 104 * Elide any full active rows. 105 */ 106 static void 107 elide() 108 { 109 register int i, j, base; 110 register cell *p; 111 112 for (i = A_FIRST; i < A_LAST; i++) { 113 base = i * B_COLS + 1; 114 p = &board[base]; 115 for (j = B_COLS - 2; *p++ != 0;) { 116 if (--j <= 0) { 117 /* this row is to be elided */ 118 memset(&board[base], 0, B_COLS - 2); 119 scr_update(); 120 tsleep(); 121 while (--base != 0) 122 board[base + B_COLS] = board[base]; 123 scr_update(); 124 tsleep(); 125 break; 126 } 127 } 128 } 129 } 130 131 int 132 main(argc, argv) 133 int argc; 134 char *argv[]; 135 { 136 register int pos, c; 137 register const char *keys; 138 register int level = 2; 139 char key_write[6][10]; 140 int ch, i, j; 141 int fd; 142 143 gid = getgid(); 144 egid = getegid(); 145 setegid(gid); 146 147 fd = open("/dev/null", O_RDONLY); 148 if (fd < 3) 149 exit(1); 150 close(fd); 151 152 keys = "jkl pq"; 153 154 while ((ch = getopt(argc, argv, "k:l:ps")) != -1) 155 switch(ch) { 156 case 'k': 157 if (strlen(keys = optarg) != 6) 158 usage(); 159 break; 160 case 'l': 161 level = atoi(optarg); 162 if (level < MINLEVEL || level > MAXLEVEL) { 163 errx(1, "level must be from %d to %d", 164 MINLEVEL, MAXLEVEL); 165 } 166 break; 167 case 'p': 168 showpreview = 1; 169 break; 170 case 's': 171 showscores(0); 172 exit(0); 173 case '?': 174 default: 175 usage(); 176 } 177 178 argc -= optind; 179 argv += optind; 180 181 if (argc) 182 usage(); 183 184 fallrate = 1000000 / level; 185 186 for (i = 0; i <= 5; i++) { 187 for (j = i+1; j <= 5; j++) { 188 if (keys[i] == keys[j]) { 189 errx(1, "duplicate command keys specified."); 190 } 191 } 192 if (keys[i] == ' ') 193 strcpy(key_write[i], "<space>"); 194 else { 195 key_write[i][0] = keys[i]; 196 key_write[i][1] = '\0'; 197 } 198 } 199 200 sprintf(key_msg, 201 "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit", 202 key_write[0], key_write[1], key_write[2], key_write[3], 203 key_write[4], key_write[5]); 204 205 (void)signal(SIGINT, onintr); 206 scr_init(); 207 setup_board(); 208 209 srandom(getpid()); 210 scr_set(); 211 212 pos = A_FIRST*B_COLS + (B_COLS/2)-1; 213 nextshape = randshape(); 214 curshape = randshape(); 215 216 scr_msg(key_msg, 1); 217 218 for (;;) { 219 place(curshape, pos, 1); 220 scr_update(); 221 place(curshape, pos, 0); 222 c = tgetchar(); 223 if (c < 0) { 224 /* 225 * Timeout. Move down if possible. 226 */ 227 if (fits_in(curshape, pos + B_COLS)) { 228 pos += B_COLS; 229 continue; 230 } 231 232 /* 233 * Put up the current shape `permanently', 234 * bump score, and elide any full rows. 235 */ 236 place(curshape, pos, 1); 237 score++; 238 elide(); 239 240 /* 241 * Choose a new shape. If it does not fit, 242 * the game is over. 243 */ 244 curshape = nextshape; 245 nextshape = randshape(); 246 pos = A_FIRST*B_COLS + (B_COLS/2)-1; 247 if (!fits_in(curshape, pos)) 248 break; 249 continue; 250 } 251 252 /* 253 * Handle command keys. 254 */ 255 if (c == keys[5]) { 256 /* quit */ 257 break; 258 } 259 if (c == keys[4]) { 260 static char msg[] = 261 "paused - press RETURN to continue"; 262 263 place(curshape, pos, 1); 264 do { 265 scr_update(); 266 scr_msg(key_msg, 0); 267 scr_msg(msg, 1); 268 (void) fflush(stdout); 269 } while (rwait((struct timeval *)NULL) == -1); 270 scr_msg(msg, 0); 271 scr_msg(key_msg, 1); 272 place(curshape, pos, 0); 273 continue; 274 } 275 if (c == keys[0]) { 276 /* move left */ 277 if (fits_in(curshape, pos - 1)) 278 pos--; 279 continue; 280 } 281 if (c == keys[1]) { 282 /* turn */ 283 const struct shape *new = &shapes[curshape->rot]; 284 285 if (fits_in(new, pos)) 286 curshape = new; 287 continue; 288 } 289 if (c == keys[2]) { 290 /* move right */ 291 if (fits_in(curshape, pos + 1)) 292 pos++; 293 continue; 294 } 295 if (c == keys[3]) { 296 /* move to bottom */ 297 while (fits_in(curshape, pos + B_COLS)) { 298 pos += B_COLS; 299 score++; 300 } 301 continue; 302 } 303 if (c == '\f') { 304 scr_clear(); 305 scr_msg(key_msg, 1); 306 } 307 } 308 309 scr_clear(); 310 scr_end(); 311 312 (void)printf("Your score: %d point%s x level %d = %d\n", 313 score, score == 1 ? "" : "s", level, score * level); 314 savescore(level); 315 316 printf("\nHit RETURN to see high scores, ^C to skip.\n"); 317 318 while ((i = getchar()) != '\n') 319 if (i == EOF) 320 break; 321 322 showscores(level); 323 324 exit(0); 325 } 326 327 void 328 onintr(signo) 329 int signo __attribute__((__unused__)); 330 { 331 scr_clear(); 332 scr_end(); 333 exit(0); 334 } 335 336 void 337 usage() 338 { 339 (void)fprintf(stderr, "usage: tetris [-ps] [-k keys] [-l level]\n"); 340 exit(1); 341 } 342