1 /* $NetBSD: tetris.h,v 1.2 1995/04/22 07:42:48 cgd 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.h 8.1 (Berkeley) 5/31/93 39 */ 40 41 /* 42 * Definitions for Tetris. 43 */ 44 45 /* 46 * The display (`board') is composed of 23 rows of 12 columns of characters 47 * (numbered 0..22 and 0..11), stored in a single array for convenience. 48 * Columns 1 to 10 of rows 1 to 20 are the actual playing area, where 49 * shapes appear. Columns 0 and 11 are always occupied, as are all 50 * columns of rows 21 and 22. Rows 0 and 22 exist as boundary areas 51 * so that regions `outside' the visible area can be examined without 52 * worrying about addressing problems. 53 */ 54 55 /* the board */ 56 #define B_COLS 12 57 #define B_ROWS 23 58 #define B_SIZE (B_ROWS * B_COLS) 59 60 typedef unsigned char cell; 61 cell board[B_SIZE]; /* 1 => occupied, 0 => empty */ 62 63 /* the displayed area (rows) */ 64 #define D_FIRST 1 65 #define D_LAST 22 66 67 /* the active area (rows) */ 68 #define A_FIRST 1 69 #define A_LAST 21 70 71 /* 72 * Minimum display size. 73 */ 74 #define MINROWS 23 75 #define MINCOLS 40 76 77 int Rows, Cols; /* current screen size */ 78 79 /* 80 * Translations from board coordinates to display coordinates. 81 * As with board coordinates, display coordiates are zero origin. 82 */ 83 #define RTOD(x) ((x) - 1) 84 #define CTOD(x) ((x) * 2 + (((Cols - 2 * B_COLS) >> 1) - 1)) 85 86 /* 87 * A `shape' is the fundamental thing that makes up the game. There 88 * are 7 basic shapes, each consisting of four `blots': 89 * 90 * X.X X.X X.X 91 * X.X X.X X.X.X X.X X.X.X X.X.X X.X.X.X 92 * X X X 93 * 94 * 0 1 2 3 4 5 6 95 * 96 * Except for 3 and 6, the center of each shape is one of the blots. 97 * This blot is designated (0,0). The other three blots can then be 98 * described as offsets from the center. Shape 3 is the same under 99 * rotation, so its center is effectively irrelevant; it has been chosen 100 * so that it `sticks out' upward and leftward. Except for shape 6, 101 * all the blots are contained in a box going from (-1,-1) to (+1,+1); 102 * shape 6's center `wobbles' as it rotates, so that while it `sticks out' 103 * rightward, its rotation---a vertical line---`sticks out' downward. 104 * The containment box has to include the offset (2,0), making the overall 105 * containment box range from offset (-1,-1) to (+2,+1). (This is why 106 * there is only one row above, but two rows below, the display area.) 107 * 108 * The game works by choosing one of these shapes at random and putting 109 * its center at the middle of the first display row (row 1, column 5). 110 * The shape is moved steadily downward until it collides with something: 111 * either another shape, or the bottom of the board. When the shape can 112 * no longer be moved downwards, it is merged into the current board. 113 * At this time, any completely filled rows are elided, and blots above 114 * these rows move down to make more room. A new random shape is again 115 * introduced at the top of the board, and the whole process repeats. 116 * The game ends when the new shape will not fit at (1,5). 117 * 118 * While the shapes are falling, the user can rotate them counterclockwise 119 * 90 degrees (in addition to moving them left or right), provided that the 120 * rotation puts the blots in empty spaces. The table of shapes is set up 121 * so that each shape contains the index of the new shape obtained by 122 * rotating the current shape. Due to symmetry, each shape has exactly 123 * 1, 2, or 4 rotations total; the first 7 entries in the table represent 124 * the primary shapes, and the remaining 12 represent their various 125 * rotated forms. 126 */ 127 struct shape { 128 int rot; /* index of rotated version of this shape */ 129 int off[3]; /* offsets to other blots if center is at (0,0) */ 130 }; 131 132 extern struct shape shapes[]; 133 #define randshape() (&shapes[random() % 7]) 134 135 /* 136 * Shapes fall at a rate faster than once per second. 137 * 138 * The initial rate is determined by dividing 1 million microseconds 139 * by the game `level'. (This is at most 1 million, or one second.) 140 * Each time the fall-rate is used, it is decreased a little bit, 141 * depending on its current value, via the `faster' macro below. 142 * The value eventually reaches a limit, and things stop going faster, 143 * but by then the game is utterly impossible. 144 */ 145 long fallrate; /* less than 1 million; smaller => faster */ 146 #define faster() (fallrate -= fallrate / 3000) 147 148 /* 149 * Game level must be between 1 and 9. This controls the initial fall rate 150 * and affects scoring. 151 */ 152 #define MINLEVEL 1 153 #define MAXLEVEL 9 154 155 /* 156 * Scoring is as follows: 157 * 158 * When the shape comes to rest, and is integrated into the board, 159 * we score one point. If the shape is high up (at a low-numbered row), 160 * and the user hits the space bar, the shape plummets all the way down, 161 * and we score a point for each row it falls (plus one more as soon as 162 * we find that it is at rest and integrate it---until then, it can 163 * still be moved or rotated). 164 */ 165 int score; /* the obvious thing */ 166 gid_t gid, egid; 167 168 char key_msg[100]; 169 170 int fits_in __P((struct shape *, int)); 171 void place __P((struct shape *, int, int)); 172 void stop __P((char *)); 173