1 /*	$OpenBSD: shapes.c,v 1.7 2003/06/03 03:01:41 millert Exp $	*/
2 /*	$NetBSD: shapes.c,v 1.2 1995/04/22 07:42:44 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Chris Torek and Darren F. Provine.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
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
21  *    without specific prior written permission.
22  *
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
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
33  * SUCH DAMAGE.
34  *
35  *	@(#)shapes.c	8.1 (Berkeley) 5/31/93
36  */
37 
38 /*
39  * Tetris shapes and related routines.
40  *
41  * Note that the first 7 are `well known'.
42  */
43 
44 #include <unistd.h>
45 #include "tetris.h"
46 
47 #define	TL	-B_COLS-1	/* top left */
48 #define	TC	-B_COLS		/* top center */
49 #define	TR	-B_COLS+1	/* top right */
50 #define	ML	-1		/* middle left */
51 #define	MR	1		/* middle right */
52 #define	BL	B_COLS-1	/* bottom left */
53 #define	BC	B_COLS		/* bottom center */
54 #define	BR	B_COLS+1	/* bottom right */
55 
56 const struct shape shapes[] = {
57 	/* 0*/	{ 7,	7,	{ TL, TC, MR } },
58 	/* 1*/	{ 8,	8,	{ TC, TR, ML } },
59 	/* 2*/	{ 9,	11,	{ ML, MR, BC } },
60 	/* 3*/	{ 3,	3,	{ TL, TC, ML } },
61 	/* 4*/	{ 12,	14,	{ ML, BL, MR } },
62 	/* 5*/	{ 15,	17,	{ ML, BR, MR } },
63 	/* 6*/	{ 18,	18,	{ ML, MR, 2  } }, /* sticks out */
64 	/* 7*/	{ 0,	0,	{ TC, ML, BL } },
65 	/* 8*/	{ 1,	1,	{ TC, MR, BR } },
66 	/* 9*/	{ 10,	2,	{ TC, MR, BC } },
67 	/*10*/	{ 11,	9,	{ TC, ML, MR } },
68 	/*11*/	{ 2,	10,	{ TC, ML, BC } },
69 	/*12*/	{ 13,	4,	{ TC, BC, BR } },
70 	/*13*/	{ 14,	12,	{ TR, ML, MR } },
71 	/*14*/	{ 4,	13,	{ TL, TC, BC } },
72 	/*15*/	{ 16,	5,	{ TR, TC, BC } },
73 	/*16*/	{ 17,	15,	{ TL, MR, ML } },
74 	/*17*/	{ 5,	16,	{ TC, BC, BL } },
75 	/*18*/	{ 6,	6,	{ TC, BC, 2*B_COLS } }/* sticks out */
76 };
77 
78 /*
79  * Return true iff the given shape fits in the given position,
80  * taking the current board into account.
81  */
82 int
fits_in(shape,pos)83 fits_in(shape, pos)
84 	const struct shape *shape;
85 	int pos;
86 {
87 	int *o = shape->off;
88 
89 	if (board[pos] || board[pos + *o++] || board[pos + *o++] ||
90 	    board[pos + *o])
91 		return 0;
92 	return 1;
93 }
94 
95 /*
96  * Write the given shape into the current board, turning it on
97  * if `onoff' is 1, and off if `onoff' is 0.
98  */
99 void
place(shape,pos,onoff)100 place(shape, pos, onoff)
101 	const struct shape *shape;
102 	int pos, onoff;
103 {
104 	int *o = shape->off;
105 
106 	board[pos] = onoff;
107 	board[pos + *o++] = onoff;
108 	board[pos + *o++] = onoff;
109 	board[pos + *o] = onoff;
110 }
111