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