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.c 8.1 (Berkeley) 5/31/93
|
---|
32 | * NetBSD: tetris.c,v 1.2 1995/04/22 07:42:47 cgd
|
---|
33 | * OpenBSD: tetris.c,v 1.21 2006/04/20 03:24:12 ray
|
---|
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 | static volatile const char copyright[] =
|
---|
53 | "@(#) Copyright (c) 1992, 1993\n"
|
---|
54 | "\tThe Regents of the University of California. All rights reserved.\n";
|
---|
55 |
|
---|
56 | #include <time.h>
|
---|
57 | #include <errno.h>
|
---|
58 | #include <stdbool.h>
|
---|
59 | #include <stdio.h>
|
---|
60 | #include <stdlib.h>
|
---|
61 | #include <stdint.h>
|
---|
62 | #include <str.h>
|
---|
63 | #include <getopt.h>
|
---|
64 | #include "scores.h"
|
---|
65 | #include "screen.h"
|
---|
66 | #include "tetris.h"
|
---|
67 |
|
---|
68 | cell board[B_SIZE];
|
---|
69 |
|
---|
70 | int Rows;
|
---|
71 | int Cols;
|
---|
72 |
|
---|
73 | const struct shape *curshape;
|
---|
74 | const struct shape *nextshape;
|
---|
75 |
|
---|
76 | long fallrate;
|
---|
77 | int score;
|
---|
78 | char key_msg[116];
|
---|
79 | int showpreview;
|
---|
80 | int classic;
|
---|
81 |
|
---|
82 | static void elide(void);
|
---|
83 | static void setup_board(void);
|
---|
84 | static const struct shape *randshape(void);
|
---|
85 |
|
---|
86 | static void usage(void);
|
---|
87 |
|
---|
88 | static int firstgame = 1;
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * Set up the initial board. The bottom display row is completely set,
|
---|
92 | * along with another (hidden) row underneath that. Also, the left and
|
---|
93 | * right edges are set.
|
---|
94 | */
|
---|
95 | static void setup_board(void)
|
---|
96 | {
|
---|
97 | int i;
|
---|
98 | cell *p = board;
|
---|
99 |
|
---|
100 | for (i = B_SIZE; i; i--)
|
---|
101 | *p++ = (i <= (2 * B_COLS) || (i % B_COLS) < 2) ? 0x0000ff : 0x000000;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Elide any full active rows.
|
---|
106 | */
|
---|
107 | static void elide(void)
|
---|
108 | {
|
---|
109 | int rows = 0;
|
---|
110 | int i;
|
---|
111 | int j;
|
---|
112 | int base;
|
---|
113 | cell *p;
|
---|
114 |
|
---|
115 | for (i = A_FIRST; i < A_LAST; i++) {
|
---|
116 | base = i * B_COLS + 1;
|
---|
117 | p = &board[base];
|
---|
118 | j = B_COLS - 2;
|
---|
119 | while (*p++ != 0) {
|
---|
120 | if (--j <= 0) {
|
---|
121 | /* This row is to be elided */
|
---|
122 | rows++;
|
---|
123 | memset(&board[base], 0, sizeof(cell) * (B_COLS - 2));
|
---|
124 |
|
---|
125 | scr_update();
|
---|
126 | tsleep();
|
---|
127 |
|
---|
128 | while (--base != 0)
|
---|
129 | board[base + B_COLS] = board[base];
|
---|
130 |
|
---|
131 | scr_update();
|
---|
132 | tsleep();
|
---|
133 |
|
---|
134 | break;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | switch (rows) {
|
---|
140 | case 1:
|
---|
141 | score += 10;
|
---|
142 | break;
|
---|
143 | case 2:
|
---|
144 | score += 30;
|
---|
145 | break;
|
---|
146 | case 3:
|
---|
147 | score += 70;
|
---|
148 | break;
|
---|
149 | case 4:
|
---|
150 | score += 150;
|
---|
151 | break;
|
---|
152 | default:
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | const struct shape *randshape(void)
|
---|
158 | {
|
---|
159 | const struct shape *tmp = &shapes[rand() % 7];
|
---|
160 | int i;
|
---|
161 | int j = rand() % 4;
|
---|
162 |
|
---|
163 | for (i = 0; i < j; i++)
|
---|
164 | tmp = &shapes[classic ? tmp->rotc : tmp->rot];
|
---|
165 |
|
---|
166 | return (tmp);
|
---|
167 | }
|
---|
168 |
|
---|
169 | static void srandomdev(void)
|
---|
170 | {
|
---|
171 | struct timespec ts;
|
---|
172 |
|
---|
173 | getrealtime(&ts);
|
---|
174 | srand(ts.tv_sec + ts.tv_nsec / 100000000);
|
---|
175 | }
|
---|
176 |
|
---|
177 | static void tetris_menu_draw(int level)
|
---|
178 | {
|
---|
179 | clear_screen();
|
---|
180 | moveto(5, 10);
|
---|
181 | puts("Tetris\n");
|
---|
182 |
|
---|
183 | moveto(8, 10);
|
---|
184 | printf("Level = %d (press keys 1 - 9 to change)", level);
|
---|
185 | moveto(9, 10);
|
---|
186 | printf("Preview is %s (press 'p' to change)", (showpreview ? "on " : "off"));
|
---|
187 | moveto(12, 10);
|
---|
188 | printf("Press 'h' to show hiscore table.");
|
---|
189 | moveto(13, 10);
|
---|
190 | printf("Press 's' to start game.");
|
---|
191 | moveto(14, 10);
|
---|
192 | printf("Press 'q' to quit game.");
|
---|
193 | moveto(20, 10);
|
---|
194 | printf("In game controls:");
|
---|
195 | moveto(21, 0);
|
---|
196 | printf("%s", key_msg);
|
---|
197 | }
|
---|
198 |
|
---|
199 | static int tetris_menu(int *level)
|
---|
200 | {
|
---|
201 | tetris_menu_draw(*level);
|
---|
202 | while (true) {
|
---|
203 | int i = getchar();
|
---|
204 | if (i < 0)
|
---|
205 | return 0;
|
---|
206 |
|
---|
207 | switch (i) {
|
---|
208 | case 'p':
|
---|
209 | showpreview = !showpreview;
|
---|
210 | moveto(9, 21);
|
---|
211 | if (showpreview)
|
---|
212 | printf("on ");
|
---|
213 | else
|
---|
214 | printf("off");
|
---|
215 | break;
|
---|
216 | case 'h':
|
---|
217 | loadscores();
|
---|
218 | showscores(firstgame);
|
---|
219 | tetris_menu_draw(*level);
|
---|
220 | break;
|
---|
221 | case 's':
|
---|
222 | firstgame = 0;
|
---|
223 | return 1;
|
---|
224 | case 'q':
|
---|
225 | return 0;
|
---|
226 | case '1':
|
---|
227 | case '2':
|
---|
228 | case '3':
|
---|
229 | case '4':
|
---|
230 | case '5':
|
---|
231 | case '6':
|
---|
232 | case '7':
|
---|
233 | case '8':
|
---|
234 | case '9':
|
---|
235 | *level = i - '0';
|
---|
236 | moveto(8, 18);
|
---|
237 | printf("%d", *level);
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | int main(int argc, char *argv[])
|
---|
244 | {
|
---|
245 | int pos;
|
---|
246 | int c;
|
---|
247 | const char *keys;
|
---|
248 | int level = 2;
|
---|
249 | char key_write[6][10];
|
---|
250 | int i;
|
---|
251 | int j;
|
---|
252 | int ch;
|
---|
253 |
|
---|
254 | console = console_init(stdin, stdout);
|
---|
255 |
|
---|
256 | keys = "jkl pq";
|
---|
257 |
|
---|
258 | classic = 0;
|
---|
259 | showpreview = 1;
|
---|
260 |
|
---|
261 | while ((ch = getopt(argc, argv, "ck:ps")) != -1)
|
---|
262 | switch (ch) {
|
---|
263 | case 'c':
|
---|
264 | /*
|
---|
265 | * this means:
|
---|
266 | * - rotate the other way
|
---|
267 | * - no reverse video
|
---|
268 | */
|
---|
269 | classic = 1;
|
---|
270 | break;
|
---|
271 | case 'k':
|
---|
272 | if (str_size(keys = optarg) != 6)
|
---|
273 | usage();
|
---|
274 | break;
|
---|
275 | case 'p':
|
---|
276 | showpreview = 1;
|
---|
277 | break;
|
---|
278 | case 's':
|
---|
279 | showscores(0);
|
---|
280 | exit(0);
|
---|
281 | default:
|
---|
282 | usage();
|
---|
283 | }
|
---|
284 |
|
---|
285 | argc -= optind;
|
---|
286 | argv += optind;
|
---|
287 |
|
---|
288 | if (argc)
|
---|
289 | usage();
|
---|
290 |
|
---|
291 | for (i = 0; i <= 5; i++) {
|
---|
292 | for (j = i + 1; j <= 5; j++) {
|
---|
293 | if (keys[i] == keys[j]) {
|
---|
294 | fprintf(stderr, "duplicate command keys specified.");
|
---|
295 | abort();
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | if (keys[i] == ' ')
|
---|
300 | str_cpy(key_write[i], sizeof(key_write[i]), "<space>");
|
---|
301 | else {
|
---|
302 | key_write[i][0] = keys[i];
|
---|
303 | key_write[i][1] = '\0';
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | snprintf(key_msg, sizeof(key_msg),
|
---|
308 | "%s - left %s - rotate %s - right %s - drop %s - pause %s - quit",
|
---|
309 | key_write[0], key_write[1], key_write[2], key_write[3],
|
---|
310 | key_write[4], key_write[5]);
|
---|
311 |
|
---|
312 | scr_init();
|
---|
313 | if (loadscores() != EOK)
|
---|
314 | initscores();
|
---|
315 |
|
---|
316 | while (tetris_menu(&level)) {
|
---|
317 | fallrate = 1000000 / level;
|
---|
318 |
|
---|
319 | scr_clear();
|
---|
320 | setup_board();
|
---|
321 |
|
---|
322 | srandomdev();
|
---|
323 | scr_set();
|
---|
324 |
|
---|
325 | pos = A_FIRST * B_COLS + (B_COLS / 2) - 1;
|
---|
326 | nextshape = randshape();
|
---|
327 | curshape = randshape();
|
---|
328 |
|
---|
329 | scr_msg(key_msg, 1);
|
---|
330 |
|
---|
331 | while (true) {
|
---|
332 | place(curshape, pos, 1);
|
---|
333 | scr_update();
|
---|
334 | place(curshape, pos, 0);
|
---|
335 | c = tgetchar();
|
---|
336 | if (c < 0) {
|
---|
337 | /*
|
---|
338 | * Timeout. Move down if possible.
|
---|
339 | */
|
---|
340 | if (fits_in(curshape, pos + B_COLS)) {
|
---|
341 | pos += B_COLS;
|
---|
342 | continue;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /*
|
---|
346 | * Put up the current shape `permanently',
|
---|
347 | * bump score, and elide any full rows.
|
---|
348 | */
|
---|
349 | place(curshape, pos, 1);
|
---|
350 | score++;
|
---|
351 | elide();
|
---|
352 |
|
---|
353 | /*
|
---|
354 | * Choose a new shape. If it does not fit,
|
---|
355 | * the game is over.
|
---|
356 | */
|
---|
357 | curshape = nextshape;
|
---|
358 | nextshape = randshape();
|
---|
359 | pos = A_FIRST * B_COLS + (B_COLS / 2) - 1;
|
---|
360 |
|
---|
361 | if (!fits_in(curshape, pos))
|
---|
362 | break;
|
---|
363 |
|
---|
364 | continue;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /*
|
---|
368 | * Handle command keys.
|
---|
369 | */
|
---|
370 | if (c == keys[5]) {
|
---|
371 | /* quit */
|
---|
372 | break;
|
---|
373 | }
|
---|
374 |
|
---|
375 | if (c == keys[4]) {
|
---|
376 | static char msg[] =
|
---|
377 | "paused - press RETURN to continue";
|
---|
378 |
|
---|
379 | place(curshape, pos, 1);
|
---|
380 | do {
|
---|
381 | scr_update();
|
---|
382 | scr_msg(key_msg, 0);
|
---|
383 | scr_msg(msg, 1);
|
---|
384 | console_flush(console);
|
---|
385 | } while (!twait());
|
---|
386 |
|
---|
387 | scr_msg(msg, 0);
|
---|
388 | scr_msg(key_msg, 1);
|
---|
389 | place(curshape, pos, 0);
|
---|
390 | continue;
|
---|
391 | }
|
---|
392 |
|
---|
393 | if (c == keys[0]) {
|
---|
394 | /* move left */
|
---|
395 | if (fits_in(curshape, pos - 1))
|
---|
396 | pos--;
|
---|
397 | continue;
|
---|
398 | }
|
---|
399 |
|
---|
400 | if (c == keys[1]) {
|
---|
401 | /* turn */
|
---|
402 | const struct shape *new =
|
---|
403 | &shapes[classic ? curshape->rotc : curshape->rot];
|
---|
404 |
|
---|
405 | if (fits_in(new, pos))
|
---|
406 | curshape = new;
|
---|
407 | continue;
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (c == keys[2]) {
|
---|
411 | /* move right */
|
---|
412 | if (fits_in(curshape, pos + 1))
|
---|
413 | pos++;
|
---|
414 | continue;
|
---|
415 | }
|
---|
416 |
|
---|
417 | if (c == keys[3]) {
|
---|
418 | /* move to bottom */
|
---|
419 | while (fits_in(curshape, pos + B_COLS)) {
|
---|
420 | pos += B_COLS;
|
---|
421 | score++;
|
---|
422 | }
|
---|
423 | continue;
|
---|
424 | }
|
---|
425 |
|
---|
426 | if (c == '\f') {
|
---|
427 | scr_clear();
|
---|
428 | scr_msg(key_msg, 1);
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | scr_clear();
|
---|
433 | loadscores();
|
---|
434 | insertscore(score, level);
|
---|
435 | savescores();
|
---|
436 | score = 0;
|
---|
437 | }
|
---|
438 |
|
---|
439 | scr_clear();
|
---|
440 | printf("\nGame over.\n");
|
---|
441 | scr_end();
|
---|
442 |
|
---|
443 | return 0;
|
---|
444 | }
|
---|
445 |
|
---|
446 | void usage(void)
|
---|
447 | {
|
---|
448 | fprintf(stderr, "%s", copyright);
|
---|
449 | fprintf(stderr, "usage: tetris [-ps] [-k keys]\n");
|
---|
450 | exit(1);
|
---|
451 | }
|
---|
452 |
|
---|
453 | /** @}
|
---|
454 | */
|
---|