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 $
27 static const char shapes[7][4][4][4] = {
28 { { {0x00, 0x00, 0x00, 0x00}, {0x47, 0xC7, 0x97, 0x00},
29 {0x00, 0x00, 0x27, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //sharp horizontal
30 { {0x00, 0x17, 0x00, 0x00}, {0x00, 0x37, 0x00, 0x00},
31 {0x47, 0xA7, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //blunt vertical J
32 { {0x17, 0x00, 0x00, 0x00}, {0x67, 0xC7, 0x87, 0x00},
33 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //blunt horizontal
34 { {0x00, 0x57, 0x87, 0x00}, {0x00, 0x37, 0x00, 0x00},
35 {0x00, 0x27, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //J (yellow)
37 { { {0x00, 0x00, 0x00, 0x00}, {0x53, 0xC3, 0x83, 0x00},
38 {0x23, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //sharp horizontal
39 { {0x43, 0x93, 0x00, 0x00}, {0x00, 0x33, 0x00, 0x00},
40 {0x00, 0x23, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //sharp vertical
41 { {0x00, 0x00, 0x13, 0x00}, {0x43, 0xC3, 0xA3, 0x00},
42 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //blunt horizontal
43 { {0x00, 0x13, 0x00, 0x00}, {0x00, 0x33, 0x00, 0x00},
44 {0x00, 0x63, 0x83, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //L (cyan)
46 { { {0x00, 0x00, 0x00, 0x00}, {0x48, 0xD8, 0x88, 0x00},
47 {0x00, 0x28, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //pointing down
48 { {0x00, 0x18, 0x00, 0x00}, {0x48, 0xB8, 0x00, 0x00},
49 {0x00, 0x28, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //pointing left
50 { {0x00, 0x18, 0x00, 0x00}, {0x48, 0xE8, 0x88, 0x00},
51 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //pointing up
52 { {0x00, 0x18, 0x00, 0x00}, {0x00, 0x78, 0x88, 0x00},
53 {0x00, 0x28, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //T (white)
55 { { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x52, 0x82, 0x00},
56 {0x42, 0xA2, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} },
57 { {0x12, 0x00, 0x00, 0x00}, {0x62, 0x92, 0x00, 0x00},
58 {0x00, 0x22, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} },
59 { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x52, 0x82, 0x00},
60 {0x42, 0xA2, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
61 { {0x12, 0x00, 0x00, 0x00}, {0x62, 0x92, 0x00, 0x00},
62 {0x00, 0x22, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //S (green)
64 { { {0x00, 0x00, 0x00, 0x00}, {0x46, 0x96, 0x00, 0x00},
65 {0x00, 0x66, 0x86, 0x00}, {0x00, 0x00, 0x00, 0x00} },
66 { {0x00, 0x16, 0x00, 0x00}, {0x56, 0xA6, 0x00, 0x00},
67 {0x26, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} },
68 { {0x00, 0x00, 0x00, 0x00}, {0x46, 0x96, 0x00, 0x00},
69 {0x00, 0x66, 0x86, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
70 { {0x00, 0x16, 0x00, 0x00}, {0x56, 0xA6, 0x00, 0x00},
71 {0x26, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //Z (red)
73 { { {0x00, 0x00, 0x00, 0x00}, {0x44, 0xC4, 0xC4, 0x84},
74 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //lieing
75 { {0x00, 0x14, 0x00, 0x00}, {0x00, 0x34, 0x00, 0x00},
76 {0x00, 0x34, 0x00, 0x00}, {0x00, 0x24, 0x00, 0x00} }, //standing
77 { {0x00, 0x00, 0x00, 0x00}, {0x44, 0xC4, 0xC4, 0x84},
78 {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
79 { {0x00, 0x14, 0x00, 0x00}, {0x00, 0x34, 0x00, 0x00},
80 {0x00, 0x34, 0x00, 0x00}, {0x00, 0x24, 0x00, 0x00} } }, //stick (blue)
82 { { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
83 {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} },
84 { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
85 {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
86 { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
87 {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
88 { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
89 {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} } } //square (purple)
92 int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func)
93 { //Draw a certain shape using <ShapeDrawFunc>
99 for (i = 0; i < 4; i++)
100 for (j = 0; j < 4; j++)
101 if (shapes[type][rotation][i][j])
102 if (result = func(scr, y-i, x+j, shapes[type][rotation][i][j]))
108 float stdOptions[7] = {1, 1, 1, 1, 1, 1, 1}; //stdOptions
110 char ChooseOption(float options[7])
111 { //Return a random piece with given piece weight
113 float total = 0, val;
115 for (i = 0; i < 7; i++) total += options[i];
116 val = Random(0, 32767) / 32768.0 * total;
117 for (i = 0; i < 7; i++) if ((val -= options[i]) < 0)
123 static unsigned char board[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
124 static unsigned char oldBoard[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
125 static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT];
128 void ClearField(int scr)
129 { //Empty the whole field (all blocks BT_none)
132 for (y = Players[scr].boardHeight - 1; y >= 0; --y)
133 for (x = 0; x < Players[scr].boardWidth; ++x) {
134 oldBoard[scr][y][x] = board[scr][y][x] = BT_none;
138 unsigned char GetBlock(int scr, int y, int x)
139 { //Returns the block on field at position (x,y)
140 if (y < 0 || x < 0 || x >= Players[scr].boardWidth)
142 else if (y >= Players[scr].boardHeight)
145 return board[scr][y][x];
148 void SetBlock(int scr, int y, int x, unsigned char type)
150 if (y >= 0 && y < Players[scr].boardHeight
151 && x >= 0 && x < Players[scr].boardWidth) {
152 board[scr][y][x] = type;
153 changed[scr][y] |= 1 << x;
157 int RefreshBoard(int scr)
158 { //draw changes to screen
162 for (y = Players[scr].boardVisible - 1; y >= 0; y--)
163 if ((c = changed[scr][y])) { //line changed
164 for (x = 0; c; (c >>= 1), x++)
165 if ((c & 1) && board[scr][y][x] != oldBoard[scr][y][x]) {
166 PlotBlock(scr, y, x, board[scr][y][x]);
167 oldBoard[scr][y][x] = board[scr][y][x];
169 changed[scr][y] = 0; //reset
175 int GlanceFunc(int scr, int y, int x, unsigned char type)
177 PlotBlockXY(y, x, type);
181 int ShadowFunc(int scr, int y, int x, unsigned char type)
183 SetBlock(scr, y, x, BT_shadow);
187 int PlotFunc(int scr, int y, int x, unsigned char type)
189 SetBlock(scr, y, x, type);
192 void PlotShape(char shape, int scr, int y, int x, int shadow)
193 { //put shape on field
195 for (shadowy = y - 1; shadowy >= 0; shadowy--)
196 if (!ShapeFits(shape, scr, shadowy, x))
198 ShapeIterate(shape, scr, shadowy + 1, x, ShadowFunc);
200 ShapeIterate(shape, scr, y, x, PlotFunc);
203 int EraseFunc(int scr, int y, int x, unsigned char type)
205 SetBlock(scr, y, x, BT_none);
208 void EraseShape(char shape, int scr, int y, int x, int shadow)
209 { //remove block from field
210 ShapeIterate(shape, scr, y, x, EraseFunc);
211 if (shadow && scr == me) //draw shadow
212 ShapeIterate(shape, scr, shadowy + 1, x, EraseFunc);
215 int CollisionFunc(int scr, int y, int x, unsigned char type)
217 return GetBlock(scr, y, x) > BT_none;
219 int ShapeFits(char shape, int scr, int y, int x)
220 { //check if there's nothing in the way
221 return !ShapeIterate(shape, scr, y, x, CollisionFunc);
224 int VisibleFunc(int scr, int y, int x, unsigned char type)
226 return (y >= 0 && y < Players[scr].boardVisible &&
227 x >= 0 && x < Players[scr].boardWidth);
229 int ShapeVisible(char shape, int scr, int y, int x)
231 return ShapeIterate(shape, scr, y, x, VisibleFunc);
234 int MovePiece(int scr, int deltaY, int deltaX)
238 EraseShape(Players[scr].curShape, scr,
239 Players[scr].curY, Players[scr].curX, 1);
240 result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY,
241 Players[scr].curX + deltaX);
243 Players[scr].curY += deltaY;
244 Players[scr].curX += deltaX;
246 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
251 int RotatePiece(int scr, int dir)
256 EraseShape(Players[scr].curShape, scr, Players[scr].curY,
257 Players[scr].curX, 1);
258 /* (inc|dec)rement only 3 least significant bits which indicate rotation */
259 newshape = (Players[scr].curShape & 252) + (((Players[scr].curShape & 3) + dir) & 3);
260 result = ShapeFits(newshape, scr, Players[scr].curY, Players[scr].curX);
263 for (slideX = 0; slideX < 2; slideX = -slideX) {
264 if (slideX >= 0) slideX++; //slide more
265 if (result = ShapeFits(newshape, scr, Players[scr].curY,
266 Players[scr].curX+slideX)) break;
267 } //slide left and right
268 if (result) Players[scr].curX += slideX;
269 } //try to fit if it doesn't
270 if (result) Players[scr].curShape = newshape;
271 PlotShape(Players[scr].curShape, scr,
272 Players[scr].curY, Players[scr].curX, scr == me);
276 int DropPiece(int scr)
280 EraseShape(Players[scr].curShape, scr,
281 Players[scr].curY, Players[scr].curX, 1);
282 while (ShapeFits(Players[scr].curShape, scr,
283 Players[scr].curY - 1, Players[scr].curX)) {
287 PlotShape(Players[scr].curShape, scr,
288 Players[scr].curY, Players[scr].curX, 0);
292 int BlockFree(int scr, int x, int y, unsigned char z)
293 { //Check if blocks are empty below block (x,y) and sticking to (x,y) mask <z>
294 unsigned char curblock;
296 if (y == 0) return 0; //at bottom
297 curblock = GetBlock(scr, y, x) & z;
298 if (curblock & 16 && !BlockFree(scr, x, y-1, z & 208)) return 0;
299 if (curblock & 32 && !BlockFree(scr, x, y+1, z & 224)) return 0;
300 if (curblock & 64 && !BlockFree(scr, x+1, y, z & 112)) return 0;
301 if (curblock & 128 && !BlockFree(scr, x-1, y, z & 176)) return 0;
302 if ((z = GetBlock(scr, y-1, x)) & 32) return 1; //stuck to block below
303 if (z > BT_none) return 0; //some other piece below
304 return 1; //nothing below
307 int BlockFall(int scr, int x, int y, unsigned char z)
308 { //Drop down block (x,y) and those sticking to it mask <z>
309 if (GetBlock(scr, y, x) & z & 16) BlockFall(scr, x, y-1, z & 208);
310 if (GetBlock(scr, y, x) & z & 32) BlockFall(scr, x, y+1, z & 224);
311 if (GetBlock(scr, y, x) & z & 64) BlockFall(scr, x+1, y, z & 112);
312 if (GetBlock(scr, y, x) & z & 128) BlockFall(scr, x-1, y, z & 174);
313 SetBlock(scr, y-1, x, GetBlock(scr, y, x));
314 SetBlock(scr, y, x, BT_none);
317 int CheckFall(int scr)
318 { //Drop any free blocks on field
319 int xloop, x, x2, y, fallen = 0;
322 if (!Game.gravity) return 0;
323 for (y = Players[scr].boardHeight - 1; y > 0; y--)
324 for (x = 0; x < Players[scr].boardWidth; x++) {
325 if (((z = GetBlock(scr, y, x)) > BT_none) && ((z & 160) == 0)) {
326 //doesn't stick left/up => topleft block
327 if (BlockFree(scr, x, y, 240)) {
328 BlockFall(scr, x, y, 240);
336 int LineIsFull(int scr, int y)
337 { //return 0 if any blocks present on line <y>
340 for (x = 0; x < Players[scr].boardWidth; x++)
341 if (GetBlock(scr, y, x) <= BT_none)
346 void CopyLine(int scr, int from, int to)
347 { //move blocks on line <from> to line <to>
351 for (x = 0; x < Players[scr].boardWidth; ++x)
352 SetBlock(scr, to, x, GetBlock(scr, from, x));
355 int ClearFullLines(int scr)
356 { //remove full lines, return lines cleared
357 int from, to, x, linescleared = 0;
361 while (to < Players[scr].boardHeight) {
362 while (LineIsFull(scr, from)) {
364 for (x = 0; x<Players[scr].boardWidth; x++) {
365 SetBlock(scr, from, x, GetBlock(scr, from, x)&239);
367 SetBlock(scr, from-2, x, GetBlock(scr, from-2, x)&223);
368 } //don't stick blocks to line which we'll remove
370 CopyLine(scr, from++, to++);
372 linescleared += from - to;
373 } while (CheckFall(scr));
377 void FreezePiece(int scr)
382 void InsertJunk(int scr, int color, int count, int column)
383 { //add <count> junklines with hole at <column> to <scr> by team <color>
386 EraseShape(Players[scr].curShape, scr,
387 Players[scr].curY, Players[scr].curX, 1);
388 for (y = Players[scr].boardHeight - count - 1; y >= 0; --y)
389 CopyLine(scr, y, y + count);
390 for (y = 0; y < count; ++y)
391 for (x = 0; x < Players[scr].boardWidth; ++x)
392 SetBlock(scr, y, x, (x == column) ? BT_none : color + 1
393 + 64 * (x != column-1 && x < Players[scr].boardWidth-1)
394 + 128 * (x != column+1 && x > 0));
395 Players[scr].curY += count; //move piece up..
396 for (y = 0; y < count; ++y)
397 if (ShapeFits(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX))
398 Players[scr].curY--; //..and down again as far as possible
400 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,