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