00001 /* $OpenBSD: shapes.c,v 1.8 2004/07/10 07:26:24 deraadt Exp $ */ 00002 /* $NetBSD: shapes.c,v 1.2 1995/04/22 07:42:44 cgd Exp $ */ 00003 00004 /*- 00005 * Copyright (c) 1992, 1993 00006 * The Regents of the University of California. All rights reserved. 00007 * 00008 * This code is derived from software contributed to Berkeley by 00009 * Chris Torek and Darren F. Provine. 00010 * 00011 * Redistribution and use in source and binary forms, with or without 00012 * modification, are permitted provided that the following conditions 00013 * are met: 00014 * 1. Redistributions of source code must retain the above copyright 00015 * notice, this list of conditions and the following disclaimer. 00016 * 2. Redistributions in binary form must reproduce the above copyright 00017 * notice, this list of conditions and the following disclaimer in the 00018 * documentation and/or other materials provided with the distribution. 00019 * 3. Neither the name of the University nor the names of its contributors 00020 * may be used to endorse or promote products derived from this software 00021 * without specific prior written permission. 00022 * 00023 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00024 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00027 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00028 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00029 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00032 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00033 * SUCH DAMAGE. 00034 * 00035 * @(#)shapes.c 8.1 (Berkeley) 5/31/93 00036 */ 00037 00044 /* 00045 * Tetris shapes and related routines. 00046 * 00047 * Note that the first 7 are `well known'. 00048 */ 00049 00050 #include <unistd.h> 00051 #include "tetris.h" 00052 00053 #define TL (-B_COLS - 1) /* top left */ 00054 #define TC (-B_COLS) /* top center */ 00055 #define TR (-B_COLS + 1) /* top right */ 00056 #define ML -1 /* middle left */ 00057 #define MR 1 /* middle right */ 00058 #define BL (B_COLS - 1) /* bottom left */ 00059 #define BC B_COLS /* bottom center */ 00060 #define BR (B_COLS + 1) /* bottom right */ 00061 00062 const struct shape shapes[] = { 00063 /* 0 */ { 7, 7, { TL, TC, MR }, 0xff042d}, 00064 /* 1 */ { 8, 8, { TC, TR, ML }, 0xff9304}, 00065 /* 2 */ { 9, 11, { ML, MR, BC }, 0xbeff04}, 00066 /* 3 */ { 3, 3, { TL, TC, ML }, 0x63ff04}, 00067 /* 4 */ { 12, 14, { ML, BL, MR }, 0xce04ff}, 00068 /* 5 */ { 15, 17, { ML, BR, MR }, 0xff04cf}, 00069 /* 6 */ { 18, 18, { ML, MR, 2 }, 0x7604ff}, /* sticks out */ 00070 /* 7 */ { 0, 0, { TC, ML, BL }, 0xff042d}, 00071 /* 8 */ { 1, 1, { TC, MR, BR }, 0xff9304}, 00072 /* 9 */ { 10, 2, { TC, MR, BC }, 0xbeff04}, 00073 /* 10 */ { 11, 9, { TC, ML, MR }, 0xbeff04}, 00074 /* 11 */ { 2, 10, { TC, ML, BC }, 0xbeff04}, 00075 /* 12 */ { 13, 4, { TC, BC, BR }, 0xce04ff}, 00076 /* 13 */ { 14, 12, { TR, ML, MR }, 0xce04ff}, 00077 /* 14 */ { 4, 13, { TL, TC, BC }, 0xce04ff}, 00078 /* 15 */ { 16, 5, { TR, TC, BC }, 0xff04cf}, 00079 /* 16 */ { 17, 15, { TL, MR, ML }, 0xff04cf}, 00080 /* 17 */ { 5, 16, { TC, BC, BL }, 0xff04cf}, 00081 /* 18 */ { 6, 6, { TC, BC, 2 * B_COLS }, 0x7604ff} /* sticks out */ 00082 }; 00083 00084 /* 00085 * Return true iff the given shape fits in the given position, 00086 * taking the current board into account. 00087 */ 00088 int fits_in(const struct shape *shape, int pos) 00089 { 00090 const int *o = shape->off; 00091 00092 if ((board[pos]) || (board[pos + *o++]) || (board[pos + *o++]) || 00093 (board[pos + *o])) 00094 return 0; 00095 00096 return 1; 00097 } 00098 00099 /* 00100 * Write the given shape into the current board, turning it on 00101 * if `onoff' is 1, and off if `onoff' is 0. 00102 */ 00103 void place(const struct shape *shape, int pos, int onoff) 00104 { 00105 const int *o = shape->off; 00106 00107 board[pos] = onoff ? shape->color : 0x000000; 00108 board[pos + *o++] = onoff ? shape->color : 0x000000; 00109 board[pos + *o++] = onoff ? shape->color : 0x000000; 00110 board[pos + *o] = onoff ? shape->color : 0x000000; 00111 } 00112