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