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 CleanupBoard(int scr)
42 ExtFunc BlockType GetBlock(int scr, int y, int x)
44 if (y < 0 || x < 0 || x >= Players[scr].boardWidth)
46 else if (y >= Players[scr].boardHeight)
49 return abs(board[scr][y][x]);
52 ExtFunc void SetBlock(int scr, int y, int x, BlockType type)
54 if (y >= 0 && y < Players[scr].boardHeight &&
55 x >= 0 && x < Players[scr].boardWidth) {
56 if (y < Players[scr].boardVisible)
57 falling[scr][x] += (type < 0) - (board[scr][y][x] < 0);
58 board[scr][y][x] = type;
59 changed[scr][y] |= 1 << x;
63 ExtFunc int RefreshBoard(int scr)
69 for (y = Players[scr].boardVisible - 1; y >= 0; --y)
70 if ((c = changed[scr][y])) {
72 RobotCmd(0, "RowUpdate %d %d", scr, y);
73 for (x = 0; x < Players[scr].boardWidth; ++x) {
77 RobotCmd(0, " %d", b);
83 for (x = 0; c; (c >>= 1), (++x))
84 if ((c & 1) && B_OLD(board[scr][y][x])!=oldBoard[scr][y][x]) {
85 PlotBlock(scr, y, x, B_OLD(board[scr][y][x]));
86 oldBoard[scr][y][x] = B_OLD(board[scr][y][x]);
91 for (x = 0; x < Players[scr].boardWidth; ++x)
92 if (oldFalling[scr][x] != !!falling[scr][x]) {
93 oldFalling[scr][x] = !!falling[scr][x];
94 PlotUnderline(scr, x, oldFalling[scr][x]);
100 ExtFunc int GlanceFunc(int scr, int y, int x, BlockType type, void *data)
102 PlotBlock1(scr, 20 - y, x * 2, type);
106 ExtFunc int PlotFunc(int scr, int y, int x, BlockType type, void *data)
108 SetBlock(scr, y, x, type);
112 ExtFunc int EraseFunc(int scr, int y, int x, BlockType type, void *data)
114 SetBlock(scr, y, x, BT_none);
118 ExtFunc int CollisionFunc(int scr, int y, int x, BlockType type, void *data)
120 return GetBlock(scr, y, x) != BT_none;
123 ExtFunc int VisibleFunc(int scr, int y, int x, BlockType type, void *data)
125 return (y >= 0 && y < Players[scr].boardVisible &&
126 x >= 0 && x < Players[scr].boardWidth);
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(Players[scr].curShape, scr,
154 Players[scr].curY, Players[scr].curX);
155 result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY,
156 Players[scr].curX + deltaX);
158 Players[scr].curY += deltaY;
159 Players[scr].curX += deltaX;
161 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1);
165 ExtFunc int RotatePiece(int scr, int dir)
169 EraseShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX);
170 result = ShapeFits(dir ? Players[scr].curShape->rotateTo
171 : Players[scr].curShape->rotateFrom,
172 scr, Players[scr].curY, Players[scr].curX);
174 Players[scr].curShape = dir ? Players[scr].curShape->rotateTo
175 : Players[scr].curShape->rotateFrom;
176 PlotShape(Players[scr].curShape, scr,
177 Players[scr].curY, Players[scr].curX, 1);
181 ExtFunc int DropPiece(int scr)
185 EraseShape(Players[scr].curShape, scr,
186 Players[scr].curY, Players[scr].curX);
187 while (ShapeFits(Players[scr].curShape, scr,
188 Players[scr].curY - 1, Players[scr].curX)) {
192 PlotShape(Players[scr].curShape, scr,
193 Players[scr].curY, Players[scr].curX, 1);
197 ExtFunc int LineIsFull(int scr, int y)
201 for (x = 0; x < Players[scr].boardWidth; ++x)
202 if (GetBlock(scr, y, x) == BT_none)
207 ExtFunc void CopyLine(int scr, int from, int to)
212 for (x = 0; x < Players[scr].boardWidth; ++x)
213 SetBlock(scr, to, x, GetBlock(scr, from, x));
216 ExtFunc int ClearFullLines(int scr)
221 while (to < Players[scr].boardHeight) {
222 while (LineIsFull(scr, from))
224 CopyLine(scr, from++, to++);
229 ExtFunc void FreezePiece(int scr)
234 for (y = 0; y < Players[scr].boardHeight; ++y)
235 for (x = 0; x < Players[scr].boardWidth; ++x)
236 if ((type = board[scr][y][x]) < 0)
237 SetBlock(scr, y, x, -type);
240 ExtFunc void InsertJunk(int scr, int count, int column)
244 EraseShape(Players[scr].curShape, scr,
245 Players[scr].curY, Players[scr].curX);
246 for (y = Players[scr].boardHeight - count - 1; y >= 0; --y)
247 CopyLine(scr, y, y + count);
248 for (y = 0; y < count; ++y)
249 for (x = 0; x < Players[scr].boardWidth; ++x)
250 SetBlock(scr, y, x, (x == column) ? BT_none : BT_white);
251 Players[scr].curY += count; //move piece up..
252 for (y = 0; y < count; ++y)
253 if (ShapeFits(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX))
254 Players[scr].curY--; //..and down as far as possible
256 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1);