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