2 * Netris -- A free networked version of T*tris
3 * Copyright (C) 1994-1996,1999 Mark H. Weaver <mhw@netris.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * $Id: board.c,v 1.15 1999/05/16 06:56:24 mhw Exp $
31 static BlockType board[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
32 static BlockType oldBoard[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
33 static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT];
34 static int falling[MAX_SCREENS][MAX_BOARD_WIDTH];
35 static int oldFalling[MAX_SCREENS][MAX_BOARD_WIDTH];
37 ExtFunc void InitBoard(int scr)
39 boardHeight[scr] = MAX_BOARD_HEIGHT;
40 boardVisible[scr] = 20;
45 ExtFunc void CleanupBoard(int scr)
50 ExtFunc BlockType GetBlock(int scr, int y, int x)
52 if (y < 0 || x < 0 || x >= boardWidth[scr])
54 else if (y >= boardHeight[scr])
57 return abs(board[scr][y][x]);
60 ExtFunc void SetBlock(int scr, int y, int x, BlockType type)
62 if (y >= 0 && y < boardHeight[scr] && x >= 0 && x < boardWidth[scr]) {
63 if (y < boardVisible[scr])
64 falling[scr][x] += (type < 0) - (board[scr][y][x] < 0);
65 board[scr][y][x] = type;
66 changed[scr][y] |= 1 << x;
70 ExtFunc int RefreshBoard(int scr)
76 for (y = boardVisible[scr] - 1; y >= 0; --y)
77 if ((c = changed[scr][y])) {
79 RobotCmd(0, "RowUpdate %d %d", scr, y);
80 for (x = 0; x < boardWidth[scr]; ++x) {
84 RobotCmd(0, " %d", b);
90 for (x = 0; c; (c >>= 1), (++x))
91 if ((c & 1) && B_OLD(board[scr][y][x])!=oldBoard[scr][y][x]) {
92 PlotBlock(scr, y, x, B_OLD(board[scr][y][x]));
93 oldBoard[scr][y][x] = B_OLD(board[scr][y][x]);
98 for (x = 0; x < boardWidth[scr]; ++x)
99 if (oldFalling[scr][x] != !!falling[scr][x]) {
100 oldFalling[scr][x] = !!falling[scr][x];
101 PlotUnderline(scr, x, oldFalling[scr][x]);
107 ExtFunc int PlotFunc(int scr, int y, int x, BlockType type, void *data)
109 SetBlock(scr, y, x, type);
113 ExtFunc int EraseFunc(int scr, int y, int x, BlockType type, void *data)
115 SetBlock(scr, y, x, BT_none);
119 ExtFunc int CollisionFunc(int scr, int y, int x, BlockType type, void *data)
121 return GetBlock(scr, y, x) != BT_none;
124 ExtFunc int VisibleFunc(int scr, int y, int x, BlockType type, void *data)
126 return (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr]);
129 ExtFunc void PlotShape(Shape *shape, int scr, int y, int x, int falling)
131 ShapeIterate(shape, scr, y, x, falling, PlotFunc, NULL);
134 ExtFunc void EraseShape(Shape *shape, int scr, int y, int x)
136 ShapeIterate(shape, scr, y, x, 0, EraseFunc, NULL);
139 ExtFunc int ShapeFits(Shape *shape, int scr, int y, int x)
141 return !ShapeIterate(shape, scr, y, x, 0, CollisionFunc, NULL);
144 ExtFunc int ShapeVisible(Shape *shape, int scr, int y, int x)
146 return ShapeIterate(shape, scr, y, x, 0, VisibleFunc, NULL);
149 ExtFunc int MovePiece(int scr, int deltaY, int deltaX)
153 EraseShape(curShape[scr], scr, curY[scr], curX[scr]);
154 result = ShapeFits(curShape[scr], scr, curY[scr] + deltaY,
160 PlotShape(curShape[scr], scr, curY[scr], curX[scr], 1);
164 ExtFunc int RotatePiece(int scr)
168 EraseShape(curShape[scr], scr, curY[scr], curX[scr]);
169 result = ShapeFits(curShape[scr]->rotateTo, scr, curY[scr], curX[scr]);
171 curShape[scr] = curShape[scr]->rotateTo;
172 PlotShape(curShape[scr], scr, curY[scr], curX[scr], 1);
176 ExtFunc int DropPiece(int scr)
180 EraseShape(curShape[scr], scr, curY[scr], curX[scr]);
181 while (ShapeFits(curShape[scr], scr, curY[scr] - 1, curX[scr])) {
185 PlotShape(curShape[scr], scr, curY[scr], curX[scr], 1);
189 ExtFunc int LineIsFull(int scr, int y)
193 for (x = 0; x < boardWidth[scr]; ++x)
194 if (GetBlock(scr, y, x) == BT_none)
199 ExtFunc void CopyLine(int scr, int from, int to)
204 for (x = 0; x < boardWidth[scr]; ++x)
205 SetBlock(scr, to, x, GetBlock(scr, from, x));
208 ExtFunc int ClearFullLines(int scr)
213 while (to < boardHeight[scr]) {
214 while (LineIsFull(scr, from))
216 CopyLine(scr, from++, to++);
221 ExtFunc void FreezePiece(int scr)
226 for (y = 0; y < boardHeight[scr]; ++y)
227 for (x = 0; x < boardWidth[scr]; ++x)
228 if ((type = board[scr][y][x]) < 0)
229 SetBlock(scr, y, x, -type);
232 ExtFunc void InsertJunk(int scr, int count, int column)
236 for (y = boardHeight[scr] - count - 1; y >= 0; --y)
237 CopyLine(scr, y, y + count);
238 for (y = 0; y < count; ++y)
239 for (x = 0; x < boardWidth[scr]; ++x)
240 SetBlock(scr, y, x, (x == column) ? BT_none : BT_white);