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 | * shapes.c 8.1 (Berkeley) 5/31/93
|
---|
32 | * NetBSD: shapes.c,v 1.2 1995/04/22 07:42:44 cgd
|
---|
33 | * OpenBSD: shapes.c,v 1.8 2004/07/10 07:26:24 deraadt
|
---|
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 | /*
|
---|
53 | * Tetris shapes and related routines.
|
---|
54 | *
|
---|
55 | * Note that the first 7 are `well known'.
|
---|
56 | */
|
---|
57 |
|
---|
58 | #include "tetris.h"
|
---|
59 |
|
---|
60 | #define TL (-B_COLS - 1) /* top left */
|
---|
61 | #define TC (-B_COLS) /* top center */
|
---|
62 | #define TR (-B_COLS + 1) /* top right */
|
---|
63 | #define ML -1 /* middle left */
|
---|
64 | #define MR 1 /* middle right */
|
---|
65 | #define BL (B_COLS - 1) /* bottom left */
|
---|
66 | #define BC B_COLS /* bottom center */
|
---|
67 | #define BR (B_COLS + 1) /* bottom right */
|
---|
68 |
|
---|
69 | const struct shape shapes[] = {
|
---|
70 | /* 0 */ { 7, 7, { TL, TC, MR }, 0x00aaaa },
|
---|
71 | /* 1 */ { 8, 8, { TC, TR, ML }, 0x00aa00 },
|
---|
72 | /* 2 */ { 9, 11, { ML, MR, BC }, 0xaa5500 },
|
---|
73 | /* 3 */ { 3, 3, { TL, TC, ML }, 0x0000aa },
|
---|
74 | /* 4 */ { 12, 14, { ML, BL, MR }, 0xaa00aa },
|
---|
75 | /* 5 */ { 15, 17, { ML, BR, MR }, 0xffa500 },
|
---|
76 | /* 6 */ { 18, 18, { ML, MR, 2 }, 0xaa0000 }, /* sticks out */
|
---|
77 | /* 7 */ { 0, 0, { TC, ML, BL }, 0x00aaaa },
|
---|
78 | /* 8 */ { 1, 1, { TC, MR, BR }, 0x00aa00 },
|
---|
79 | /* 9 */ { 10, 2, { TC, MR, BC }, 0xaa5500 },
|
---|
80 | /* 10 */ { 11, 9, { TC, ML, MR }, 0xaa5500 },
|
---|
81 | /* 11 */ { 2, 10, { TC, ML, BC }, 0xaa5500 },
|
---|
82 | /* 12 */ { 13, 4, { TC, BC, BR }, 0xaa00aa },
|
---|
83 | /* 13 */ { 14, 12, { TR, ML, MR }, 0xaa00aa },
|
---|
84 | /* 14 */ { 4, 13, { TL, TC, BC }, 0xaa00aa },
|
---|
85 | /* 15 */ { 16, 5, { TR, TC, BC }, 0xffa500 },
|
---|
86 | /* 16 */ { 17, 15, { TL, MR, ML }, 0xffa500 },
|
---|
87 | /* 17 */ { 5, 16, { TC, BC, BL }, 0xffa500 },
|
---|
88 | /* 18 */ { 6, 6, { TC, BC, 2 * B_COLS }, 0xaa0000 } /* sticks out */
|
---|
89 | };
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Return true iff the given shape fits in the given position,
|
---|
93 | * taking the current board into account.
|
---|
94 | */
|
---|
95 | errno_t fits_in(const struct shape *shape, int pos)
|
---|
96 | {
|
---|
97 | const int *o = shape->off;
|
---|
98 |
|
---|
99 | if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) ||
|
---|
100 | (board[pos + *o]))
|
---|
101 | return 0;
|
---|
102 |
|
---|
103 | return 1;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /*
|
---|
107 | * Write the given shape into the current board, turning it on
|
---|
108 | * if `onoff' is 1, and off if `onoff' is 0.
|
---|
109 | */
|
---|
110 | void place(const struct shape *shape, int pos, int onoff)
|
---|
111 | {
|
---|
112 | const int *o = shape->off;
|
---|
113 |
|
---|
114 | board[pos] = onoff ? shape->color : 0x000000;
|
---|
115 | board[pos + *o++] = onoff ? shape->color : 0x000000;
|
---|
116 | board[pos + *o++] = onoff ? shape->color : 0x000000;
|
---|
117 | board[pos + *o] = onoff ? shape->color : 0x000000;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /** @}
|
---|
121 | */
|
---|