tetris.h 6.7 KB

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