compositor: fix the over operator
[helenos.git] / uspace / app / tetris / tetris.h
blobcae145cb992850325408b2899b4d1fc6a0251742
1 /*
2 * Copyright (c) 2011 Martin Decky
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
29 /** Attributations
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
35 * Based upon BSD Tetris
37 * Copyright (c) 1992, 1993
38 * The Regents of the University of California.
39 * Distributed under BSD license.
41 * This code is derived from software contributed to Berkeley by
42 * Chris Torek and Darren F. Provine.
46 /** @addtogroup tetris
47 * @{
49 /** @file
52 #include <stdint.h>
55 * Definitions for Tetris.
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.
68 /* The board */
69 #define B_COLS 12
70 #define B_ROWS 23
71 #define B_SIZE (B_ROWS * B_COLS)
73 typedef uint32_t cell;
75 extern cell board[B_SIZE]; /* 1 => occupied, 0 => empty */
77 /* The displayed area (rows) */
78 #define D_FIRST 1
79 #define D_LAST 22
81 /* The active area (rows) */
82 #define A_FIRST 1
83 #define A_LAST 21
86 * Minimum display size.
88 #define MINROWS 23
89 #define MINCOLS 40
91 /* Current screen size */
92 extern int Rows;
93 extern int Cols;
96 * Translations from board coordinates to display coordinates.
97 * As with board coordinates, display coordiates are zero origin.
99 #define RTOD(x) ((x) - 1)
100 #define CTOD(x) ((x) * 2 + (((Cols - 2 * B_COLS) >> 1) - 1))
103 * A `shape' is the fundamental thing that makes up the game. There
104 * are 7 basic shapes, each consisting of four `blots':
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
110 * 0 1 2 3 4 5 6
112 * Except for 3 and 6, the center of each shape is one of the blots.
113 * This blot is designated (0, 0). The other three blots can then be
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,
117 * all the blots are contained in a box going from (-1, -1) to (+1, +1);
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.
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
122 * there is only one row above, but two rows below, the display area.)
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.
132 * The game ends when the new shape will not fit at (1, 5).
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.
143 struct shape {
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;
150 extern const struct shape shapes[];
152 extern const struct shape *curshape;
153 extern const struct shape *nextshape;
156 * Shapes fall at a rate faster than once per second.
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.
165 extern long fallrate; /* less than 1 million; smaller => faster */
167 #define faster() (fallrate -= fallrate / 3000)
170 * Game level must be between 1 and 9. This controls the initial fall rate
171 * and affects scoring.
173 #define MINLEVEL 1
174 #define MAXLEVEL 9
177 * Scoring is as follows:
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).
186 * If previewing has been turned on, the score is multiplied by PRE_PENALTY.
188 #define PRE_PENALTY 0.75
190 extern int score; /* The obvious thing */
192 extern char key_msg[116];
193 extern int showpreview;
194 extern int classic;
196 extern int fits_in(const struct shape *, int);
197 extern void place(const struct shape *, int, int);
198 extern void stop(const char *);
200 /** @}