Rev 4515 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4515 | Rev 4680 | ||
---|---|---|---|
1 | /* $OpenBSD: shapes.c,v 1.8 2004/07/10 07:26:24 deraadt Exp $ */ |
1 | /* $OpenBSD: shapes.c,v 1.8 2004/07/10 07:26:24 deraadt Exp $ */ |
2 | /* $NetBSD: shapes.c,v 1.2 1995/04/22 07:42:44 cgd Exp $ */ |
2 | /* $NetBSD: shapes.c,v 1.2 1995/04/22 07:42:44 cgd Exp $ */ |
3 | 3 | ||
4 | /*- |
4 | /*- |
5 | * Copyright (c) 1992, 1993 |
5 | * Copyright (c) 1992, 1993 |
6 | * The Regents of the University of California. All rights reserved. |
6 | * The Regents of the University of California. All rights reserved. |
7 | * |
7 | * |
8 | * This code is derived from software contributed to Berkeley by |
8 | * This code is derived from software contributed to Berkeley by |
9 | * Chris Torek and Darren F. Provine. |
9 | * Chris Torek and Darren F. Provine. |
10 | * |
10 | * |
11 | * Redistribution and use in source and binary forms, with or without |
11 | * Redistribution and use in source and binary forms, with or without |
12 | * modification, are permitted provided that the following conditions |
12 | * modification, are permitted provided that the following conditions |
13 | * are met: |
13 | * are met: |
14 | * 1. Redistributions of source code must retain the above copyright |
14 | * 1. Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
16 | * 2. Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
18 | * documentation and/or other materials provided with the distribution. |
19 | * 3. Neither the name of the University nor the names of its contributors |
19 | * 3. Neither the name of the University nor the names of its contributors |
20 | * may be used to endorse or promote products derived from this software |
20 | * may be used to endorse or promote products derived from this software |
21 | * without specific prior written permission. |
21 | * without specific prior written permission. |
22 | * |
22 | * |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | * SUCH DAMAGE. |
33 | * SUCH DAMAGE. |
34 | * |
34 | * |
35 | * @(#)shapes.c 8.1 (Berkeley) 5/31/93 |
35 | * @(#)shapes.c 8.1 (Berkeley) 5/31/93 |
36 | */ |
36 | */ |
37 | 37 | ||
38 | /** @addtogroup tetris |
38 | /** @addtogroup tetris |
39 | * @{ |
39 | * @{ |
40 | */ |
40 | */ |
41 | /** @file |
41 | /** @file |
42 | */ |
42 | */ |
43 | 43 | ||
44 | /* |
44 | /* |
45 | * Tetris shapes and related routines. |
45 | * Tetris shapes and related routines. |
46 | * |
46 | * |
47 | * Note that the first 7 are `well known'. |
47 | * Note that the first 7 are `well known'. |
48 | */ |
48 | */ |
49 | 49 | ||
50 | #include <unistd.h> |
50 | #include <unistd.h> |
51 | #include "tetris.h" |
51 | #include "tetris.h" |
52 | 52 | ||
53 | #define TL (-B_COLS - 1) /* top left */ |
53 | #define TL (-B_COLS - 1) /* top left */ |
54 | #define TC (-B_COLS) /* top center */ |
54 | #define TC (-B_COLS) /* top center */ |
55 | #define TR (-B_COLS + 1) /* top right */ |
55 | #define TR (-B_COLS + 1) /* top right */ |
56 | #define ML -1 /* middle left */ |
56 | #define ML -1 /* middle left */ |
57 | #define MR 1 /* middle right */ |
57 | #define MR 1 /* middle right */ |
58 | #define BL (B_COLS - 1) /* bottom left */ |
58 | #define BL (B_COLS - 1) /* bottom left */ |
59 | #define BC B_COLS /* bottom center */ |
59 | #define BC B_COLS /* bottom center */ |
60 | #define BR (B_COLS + 1) /* bottom right */ |
60 | #define BR (B_COLS + 1) /* bottom right */ |
61 | 61 | ||
62 | const struct shape shapes[] = { |
62 | const struct shape shapes[] = { |
63 | /* 0 */ { 7, 7, { TL, TC, MR }, 0xff042d}, |
63 | /* 0 */ { 7, 7, { TL, TC, MR }, 0xff042d}, |
64 | /* 1 */ { 8, 8, { TC, TR, ML }, 0xff9304}, |
64 | /* 1 */ { 8, 8, { TC, TR, ML }, 0xff9304}, |
65 | /* 2 */ { 9, 11, { ML, MR, BC }, 0xbeff04}, |
65 | /* 2 */ { 9, 11, { ML, MR, BC }, 0xbeff04}, |
66 | /* 3 */ { 3, 3, { TL, TC, ML }, 0x63ff04}, |
66 | /* 3 */ { 3, 3, { TL, TC, ML }, 0x63ff04}, |
67 | /* 4 */ { 12, 14, { ML, BL, MR }, 0xce04ff}, |
67 | /* 4 */ { 12, 14, { ML, BL, MR }, 0xce04ff}, |
68 | /* 5 */ { 15, 17, { ML, BR, MR }, 0xff04cf}, |
68 | /* 5 */ { 15, 17, { ML, BR, MR }, 0xff04cf}, |
69 | /* 6 */ { 18, 18, { ML, MR, 2 }, 0x7604ff}, /* sticks out */ |
69 | /* 6 */ { 18, 18, { ML, MR, 2 }, 0x7604ff}, /* sticks out */ |
70 | /* 7 */ { 0, 0, { TC, ML, BL }, 0xff042d}, |
70 | /* 7 */ { 0, 0, { TC, ML, BL }, 0xff042d}, |
71 | /* 8 */ { 1, 1, { TC, MR, BR }, 0xff9304}, |
71 | /* 8 */ { 1, 1, { TC, MR, BR }, 0xff9304}, |
72 | /* 9 */ { 10, 2, { TC, MR, BC }, 0xbeff04}, |
72 | /* 9 */ { 10, 2, { TC, MR, BC }, 0xbeff04}, |
73 | /* 10 */ { 11, 9, { TC, ML, MR }, 0xbeff04}, |
73 | /* 10 */ { 11, 9, { TC, ML, MR }, 0xbeff04}, |
74 | /* 11 */ { 2, 10, { TC, ML, BC }, 0xbeff04}, |
74 | /* 11 */ { 2, 10, { TC, ML, BC }, 0xbeff04}, |
75 | /* 12 */ { 13, 4, { TC, BC, BR }, 0xce04ff}, |
75 | /* 12 */ { 13, 4, { TC, BC, BR }, 0xce04ff}, |
76 | /* 13 */ { 14, 12, { TR, ML, MR }, 0xce04ff}, |
76 | /* 13 */ { 14, 12, { TR, ML, MR }, 0xce04ff}, |
77 | /* 14 */ { 4, 13, { TL, TC, BC }, 0xce04ff}, |
77 | /* 14 */ { 4, 13, { TL, TC, BC }, 0xce04ff}, |
78 | /* 15 */ { 16, 5, { TR, TC, BC }, 0xff04cf}, |
78 | /* 15 */ { 16, 5, { TR, TC, BC }, 0xff04cf}, |
79 | /* 16 */ { 17, 15, { TL, MR, ML }, 0xff04cf}, |
79 | /* 16 */ { 17, 15, { TL, MR, ML }, 0xff04cf}, |
80 | /* 17 */ { 5, 16, { TC, BC, BL }, 0xff04cf}, |
80 | /* 17 */ { 5, 16, { TC, BC, BL }, 0xff04cf}, |
81 | /* 18 */ { 6, 6, { TC, BC, 2 * B_COLS }, 0x7604ff} /* sticks out */ |
81 | /* 18 */ { 6, 6, { TC, BC, 2 * B_COLS }, 0x7604ff} /* sticks out */ |
82 | }; |
82 | }; |
83 | 83 | ||
84 | /* |
84 | /* |
85 | * Return true iff the given shape fits in the given position, |
85 | * Return true iff the given shape fits in the given position, |
86 | * taking the current board into account. |
86 | * taking the current board into account. |
87 | */ |
87 | */ |
88 | int fits_in(const struct shape *shape, int pos) |
88 | int fits_in(const struct shape *shape, int pos) |
89 | { |
89 | { |
90 | int *o = shape->off; |
90 | const int *o = shape->off; |
91 | 91 | ||
92 | if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) || |
92 | if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) || |
93 | (board[pos + *o])) |
93 | (board[pos + *o])) |
94 | return 0; |
94 | return 0; |
95 | 95 | ||
96 | return 1; |
96 | return 1; |
97 | } |
97 | } |
98 | 98 | ||
99 | /* |
99 | /* |
100 | * Write the given shape into the current board, turning it on |
100 | * Write the given shape into the current board, turning it on |
101 | * if `onoff' is 1, and off if `onoff' is 0. |
101 | * if `onoff' is 1, and off if `onoff' is 0. |
102 | */ |
102 | */ |
103 | void place(const struct shape *shape, int pos, int onoff) |
103 | void place(const struct shape *shape, int pos, int onoff) |
104 | { |
104 | { |
105 | int *o = shape->off; |
105 | const int *o = shape->off; |
106 | 106 | ||
107 | board[pos] = onoff ? shape->color : 0x000000; |
107 | board[pos] = onoff ? shape->color : 0x000000; |
108 | board[pos + *o++] = onoff ? shape->color : 0x000000; |
108 | board[pos + *o++] = onoff ? shape->color : 0x000000; |
109 | board[pos + *o++] = onoff ? shape->color : 0x000000; |
109 | board[pos + *o++] = onoff ? shape->color : 0x000000; |
110 | board[pos + *o] = onoff ? shape->color : 0x000000; |
110 | board[pos + *o] = onoff ? shape->color : 0x000000; |
111 | } |
111 | } |
112 | 112 | ||
113 | /** @} |
113 | /** @} |
114 | */ |
114 | */ |
115 | 115 |