Remove unistd.h
[helenos.git] / uspace / app / tetris / shapes.c
blob3c97af7d4be72e4c11a38c2b43664d3212ceb427
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 * 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
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
53 * Tetris shapes and related routines.
55 * Note that the first 7 are `well known'.
58 #include "tetris.h"
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 */
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 */
92 * Return true iff the given shape fits in the given position,
93 * taking the current board into account.
95 int fits_in(const struct shape *shape, int pos)
97 const int *o = shape->off;
99 if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) ||
100 (board[pos + *o]))
101 return 0;
103 return 1;
107 * Write the given shape into the current board, turning it on
108 * if `onoff' is 1, and off if `onoff' is 0.
110 void place(const struct shape *shape, int pos, int onoff)
112 const int *o = shape->off;
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;
120 /** @}