Home | History | Annotate | Line # | Download | only in tetris
tetris.h revision 1.7
      1  1.7      jsm /*	$NetBSD: tetris.h,v 1.7 1999/09/12 09:02:24 jsm Exp $	*/
      2  1.2      cgd 
      3  1.1      cgd /*-
      4  1.1      cgd  * Copyright (c) 1992, 1993
      5  1.1      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * This code is derived from software contributed to Berkeley by
      8  1.1      cgd  * Chris Torek and Darren F. Provine.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  *
     38  1.1      cgd  *	@(#)tetris.h	8.1 (Berkeley) 5/31/93
     39  1.1      cgd  */
     40  1.1      cgd 
     41  1.7      jsm #include <sys/types.h>
     42  1.7      jsm 
     43  1.1      cgd /*
     44  1.1      cgd  * Definitions for Tetris.
     45  1.1      cgd  */
     46  1.1      cgd 
     47  1.1      cgd /*
     48  1.1      cgd  * The display (`board') is composed of 23 rows of 12 columns of characters
     49  1.1      cgd  * (numbered 0..22 and 0..11), stored in a single array for convenience.
     50  1.1      cgd  * Columns 1 to 10 of rows 1 to 20 are the actual playing area, where
     51  1.1      cgd  * shapes appear.  Columns 0 and 11 are always occupied, as are all
     52  1.1      cgd  * columns of rows 21 and 22.  Rows 0 and 22 exist as boundary areas
     53  1.1      cgd  * so that regions `outside' the visible area can be examined without
     54  1.1      cgd  * worrying about addressing problems.
     55  1.1      cgd  */
     56  1.1      cgd 
     57  1.1      cgd 	/* the board */
     58  1.1      cgd #define	B_COLS	12
     59  1.1      cgd #define	B_ROWS	23
     60  1.1      cgd #define	B_SIZE	(B_ROWS * B_COLS)
     61  1.1      cgd 
     62  1.1      cgd typedef unsigned char cell;
     63  1.1      cgd cell	board[B_SIZE];		/* 1 => occupied, 0 => empty */
     64  1.1      cgd 
     65  1.1      cgd 	/* the displayed area (rows) */
     66  1.1      cgd #define	D_FIRST	1
     67  1.1      cgd #define	D_LAST	22
     68  1.1      cgd 
     69  1.1      cgd 	/* the active area (rows) */
     70  1.1      cgd #define	A_FIRST	1
     71  1.1      cgd #define	A_LAST	21
     72  1.1      cgd 
     73  1.1      cgd /*
     74  1.1      cgd  * Minimum display size.
     75  1.1      cgd  */
     76  1.1      cgd #define	MINROWS	23
     77  1.1      cgd #define	MINCOLS	40
     78  1.1      cgd 
     79  1.1      cgd int	Rows, Cols;		/* current screen size */
     80  1.1      cgd 
     81  1.1      cgd /*
     82  1.1      cgd  * Translations from board coordinates to display coordinates.
     83  1.1      cgd  * As with board coordinates, display coordiates are zero origin.
     84  1.1      cgd  */
     85  1.1      cgd #define	RTOD(x)	((x) - 1)
     86  1.1      cgd #define	CTOD(x)	((x) * 2 + (((Cols - 2 * B_COLS) >> 1) - 1))
     87  1.1      cgd 
     88  1.1      cgd /*
     89  1.1      cgd  * A `shape' is the fundamental thing that makes up the game.  There
     90  1.1      cgd  * are 7 basic shapes, each consisting of four `blots':
     91  1.1      cgd  *
     92  1.1      cgd  *	X.X	  X.X		X.X
     93  1.1      cgd  *	  X.X	X.X	X.X.X	X.X	X.X.X	X.X.X	X.X.X.X
     94  1.1      cgd  *			  X		X	    X
     95  1.1      cgd  *
     96  1.1      cgd  *	  0	  1	  2	  3	  4	  5	  6
     97  1.1      cgd  *
     98  1.1      cgd  * Except for 3 and 6, the center of each shape is one of the blots.
     99  1.1      cgd  * This blot is designated (0,0).  The other three blots can then be
    100  1.1      cgd  * described as offsets from the center.  Shape 3 is the same under
    101  1.1      cgd  * rotation, so its center is effectively irrelevant; it has been chosen
    102  1.1      cgd  * so that it `sticks out' upward and leftward.  Except for shape 6,
    103  1.1      cgd  * all the blots are contained in a box going from (-1,-1) to (+1,+1);
    104  1.1      cgd  * shape 6's center `wobbles' as it rotates, so that while it `sticks out'
    105  1.1      cgd  * rightward, its rotation---a vertical line---`sticks out' downward.
    106  1.1      cgd  * The containment box has to include the offset (2,0), making the overall
    107  1.1      cgd  * containment box range from offset (-1,-1) to (+2,+1).  (This is why
    108  1.1      cgd  * there is only one row above, but two rows below, the display area.)
    109  1.1      cgd  *
    110  1.1      cgd  * The game works by choosing one of these shapes at random and putting
    111  1.1      cgd  * its center at the middle of the first display row (row 1, column 5).
    112  1.1      cgd  * The shape is moved steadily downward until it collides with something:
    113  1.1      cgd  * either  another shape, or the bottom of the board.  When the shape can
    114  1.1      cgd  * no longer be moved downwards, it is merged into the current board.
    115  1.1      cgd  * At this time, any completely filled rows are elided, and blots above
    116  1.1      cgd  * these rows move down to make more room.  A new random shape is again
    117  1.1      cgd  * introduced at the top of the board, and the whole process repeats.
    118  1.1      cgd  * The game ends when the new shape will not fit at (1,5).
    119  1.1      cgd  *
    120  1.1      cgd  * While the shapes are falling, the user can rotate them counterclockwise
    121  1.1      cgd  * 90 degrees (in addition to moving them left or right), provided that the
    122  1.1      cgd  * rotation puts the blots in empty spaces.  The table of shapes is set up
    123  1.1      cgd  * so that each shape contains the index of the new shape obtained by
    124  1.1      cgd  * rotating the current shape.  Due to symmetry, each shape has exactly
    125  1.1      cgd  * 1, 2, or 4 rotations total; the first 7 entries in the table represent
    126  1.1      cgd  * the primary shapes, and the remaining 12 represent their various
    127  1.1      cgd  * rotated forms.
    128  1.1      cgd  */
    129  1.1      cgd struct shape {
    130  1.1      cgd 	int	rot;	/* index of rotated version of this shape */
    131  1.1      cgd 	int	off[3];	/* offsets to other blots if center is at (0,0) */
    132  1.1      cgd };
    133  1.1      cgd 
    134  1.6      jsm extern const struct shape shapes[];
    135  1.1      cgd #define	randshape() (&shapes[random() % 7])
    136  1.4  hubertf 
    137  1.6      jsm const struct shape *curshape;
    138  1.6      jsm const struct shape *nextshape;
    139  1.1      cgd 
    140  1.1      cgd /*
    141  1.1      cgd  * Shapes fall at a rate faster than once per second.
    142  1.1      cgd  *
    143  1.1      cgd  * The initial rate is determined by dividing 1 million microseconds
    144  1.1      cgd  * by the game `level'.  (This is at most 1 million, or one second.)
    145  1.1      cgd  * Each time the fall-rate is used, it is decreased a little bit,
    146  1.1      cgd  * depending on its current value, via the `faster' macro below.
    147  1.1      cgd  * The value eventually reaches a limit, and things stop going faster,
    148  1.1      cgd  * but by then the game is utterly impossible.
    149  1.1      cgd  */
    150  1.1      cgd long	fallrate;		/* less than 1 million; smaller => faster */
    151  1.1      cgd #define	faster() (fallrate -= fallrate / 3000)
    152  1.1      cgd 
    153  1.1      cgd /*
    154  1.1      cgd  * Game level must be between 1 and 9.  This controls the initial fall rate
    155  1.1      cgd  * and affects scoring.
    156  1.1      cgd  */
    157  1.1      cgd #define	MINLEVEL	1
    158  1.1      cgd #define	MAXLEVEL	9
    159  1.1      cgd 
    160  1.1      cgd /*
    161  1.1      cgd  * Scoring is as follows:
    162  1.1      cgd  *
    163  1.1      cgd  * When the shape comes to rest, and is integrated into the board,
    164  1.1      cgd  * we score one point.  If the shape is high up (at a low-numbered row),
    165  1.1      cgd  * and the user hits the space bar, the shape plummets all the way down,
    166  1.1      cgd  * and we score a point for each row it falls (plus one more as soon as
    167  1.1      cgd  * we find that it is at rest and integrate it---until then, it can
    168  1.1      cgd  * still be moved or rotated).
    169  1.1      cgd  */
    170  1.1      cgd int	score;			/* the obvious thing */
    171  1.7      jsm extern gid_t	gid, egid;
    172  1.1      cgd 
    173  1.1      cgd char	key_msg[100];
    174  1.5  hubertf int	showpreview;
    175  1.1      cgd 
    176  1.6      jsm int	fits_in __P((const struct shape *, int));
    177  1.6      jsm void	place __P((const struct shape *, int, int));
    178  1.6      jsm void	stop __P((const char *)) __attribute__((__noreturn__));
    179