Blame


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