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>
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) return i<<2;
122 static unsigned char board[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
123 static unsigned char oldBoard[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
124 static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT];
127 void ClearField(int scr)
128 { //Empty the whole field (all blocks BT_none)
131 for (y = Players[scr].boardHeight - 1; y >= 0; --y)
132 for (x = 0; x < Players[scr].boardWidth; ++x) {
133 oldBoard[scr][y][x] = board[scr][y][x] = BT_none;
137 unsigned char GetBlock(int scr, int y, int x)
138 { //Returns the block on field at position (x,y)
139 if (y < 0 || x < 0 || x >= Players[scr].boardWidth)
141 else if (y >= Players[scr].boardHeight)
144 return board[scr][y][x];
147 void SetBlock(int scr, int y, int x, unsigned char type)
149 if (y >= 0 && y < Players[scr].boardHeight
150 && x >= 0 && x < Players[scr].boardWidth) {
151 board[scr][y][x] = type;
152 changed[scr][y] |= 1 << x;
156 int RefreshBoard(int scr)
157 { //draw changes to screen
161 for (y = Players[scr].boardVisible - 1; y >= 0; y--)
162 if ((c = changed[scr][y])) { //line changed
163 for (x = 0; c; (c >>= 1), x++)
164 if ((c & 1) && board[scr][y][x] != oldBoard[scr][y][x]) {
165 PlotBlock(scr, y, x, board[scr][y][x]);
166 oldBoard[scr][y][x] = board[scr][y][x];
168 changed[scr][y] = 0; //reset
174 int GlanceFunc(int scr, int y, int x, unsigned char type)
176 PlotBlockXY(y, x, type);
180 int ShadowFunc(int scr, int y, int x, unsigned char type)
182 SetBlock(scr, y, x, BT_shadow);
186 int PlotFunc(int scr, int y, int x, unsigned char type)
188 SetBlock(scr, y, x, type);
191 void PlotShape(char shape, int scr, int y, int x, int shadow)
192 { //put shape on field
194 for (shadowy = y - 1; shadowy >= 0; shadowy--)
195 if (!ShapeFits(shape, scr, shadowy, x))
197 ShapeIterate(shape, scr, shadowy + 1, x, ShadowFunc);
199 ShapeIterate(shape, scr, y, x, PlotFunc);
202 int EraseFunc(int scr, int y, int x, unsigned char type)
204 SetBlock(scr, y, x, BT_none);
207 void EraseShape(char shape, int scr, int y, int x, int shadow)
208 { //remove block from field
209 ShapeIterate(shape, scr, y, x, EraseFunc);
210 if (shadow && scr == me) //draw shadow
211 ShapeIterate(shape, scr, shadowy + 1, x, EraseFunc);
214 int CollisionFunc(int scr, int y, int x, unsigned char type)
216 return GetBlock(scr, y, x) > BT_none;
218 int ShapeFits(char shape, int scr, int y, int x)
219 { //check if there's nothing in the way
220 return !ShapeIterate(shape, scr, y, x, CollisionFunc);
223 int VisibleFunc(int scr, int y, int x, unsigned char type)
225 return (y >= 0 && y < Players[scr].boardVisible &&
226 x >= 0 && x < Players[scr].boardWidth);
228 int ShapeVisible(char shape, int scr, int y, int x)
230 return ShapeIterate(shape, scr, y, x, VisibleFunc);
233 int MovePiece(int scr, int deltaY, int deltaX)
237 EraseShape(Players[scr].curShape, scr,
238 Players[scr].curY, Players[scr].curX, 1);
239 result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY,
240 Players[scr].curX + deltaX);
242 Players[scr].curY += deltaY;
243 Players[scr].curX += deltaX;
245 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
250 int RotatePiece(int scr, int dir)
255 EraseShape(Players[scr].curShape, scr, Players[scr].curY,
256 Players[scr].curX, 1);
257 /* (inc|dec)rement only 3 least significant bits which indicate rotation */
258 newshape = (Players[scr].curShape & 252) + (((Players[scr].curShape & 3) + dir) & 3);
259 result = ShapeFits(newshape, scr, Players[scr].curY, Players[scr].curX);
262 for (slideX = 0; slideX < 2; slideX = -slideX) {
263 if (slideX >= 0) slideX++; //slide more
264 if (result = ShapeFits(newshape, scr, Players[scr].curY,
265 Players[scr].curX+slideX)) break;
266 } //slide left and right
267 if (result) Players[scr].curX += slideX;
268 } //try to fit if it doesn't
269 if (result) Players[scr].curShape = newshape;
270 PlotShape(Players[scr].curShape, scr,
271 Players[scr].curY, Players[scr].curX, scr == me);
275 int DropPiece(int scr)
279 EraseShape(Players[scr].curShape, scr,
280 Players[scr].curY, Players[scr].curX, 1);
281 while (ShapeFits(Players[scr].curShape, scr,
282 Players[scr].curY - 1, Players[scr].curX)) {
286 PlotShape(Players[scr].curShape, scr,
287 Players[scr].curY, Players[scr].curX, 0);
291 int BlockFree(int scr, int x, int y, unsigned char z)
292 { //Check if blocks are empty below block (x,y) and sticking to (x,y) mask <z>
293 unsigned char curblock;
295 if (y == 0) return 0; //at bottom
296 curblock = GetBlock(scr, y, x) & z;
297 if (curblock & 16 && !BlockFree(scr, x, y-1, z & 208)) return 0;
298 if (curblock & 32 && !BlockFree(scr, x, y+1, z & 224)) return 0;
299 if (curblock & 64 && !BlockFree(scr, x+1, y, z & 112)) return 0;
300 if (curblock & 128 && !BlockFree(scr, x-1, y, z & 176)) return 0;
301 if ((z = GetBlock(scr, y-1, x)) & 32) return 1; //stuck to block below
302 if (z > BT_none) return 0; //some other piece below
303 return 1; //nothing below
306 int BlockFall(int scr, int x, int y, unsigned char z)
307 { //Drop down block (x,y) and those sticking to it mask <z>
308 if (GetBlock(scr, y, x) & z & 16) BlockFall(scr, x, y-1, z & 208);
309 if (GetBlock(scr, y, x) & z & 32) BlockFall(scr, x, y+1, z & 224);
310 if (GetBlock(scr, y, x) & z & 64) BlockFall(scr, x+1, y, z & 112);
311 if (GetBlock(scr, y, x) & z & 128) BlockFall(scr, x-1, y, z & 174);
312 SetBlock(scr, y-1, x, GetBlock(scr, y, x));
313 SetBlock(scr, y, x, BT_none);
316 int CheckFall(int scr)
317 { //Drop any free blocks on field
318 int xloop, x, x2, y, fallen = 0;
321 if (!Game.gravity) return 0;
322 for (y = Players[scr].boardHeight-1; y > 0; y--)
323 for (x = 0; x < Players[scr].boardWidth; x++) {
324 if (((z = GetBlock(scr, y, x)) > BT_none) && ((z & 160) == 0)) {
325 //doesn't stick left/up => topleft block
326 if (BlockFree(scr, x, y, 240)) {
327 BlockFall(scr, x, y, 240);
335 int LineIsFull(int scr, int y)
336 { //return 0 if any blocks present on line <y>
339 for (x = 0; x < Players[scr].boardWidth; x++)
340 if (GetBlock(scr, y, x) <= BT_none)
345 void CopyLine(int scr, int from, int to)
346 { //move blocks on line <from> to line <to>
350 for (x = 0; x < Players[scr].boardWidth; ++x)
351 SetBlock(scr, to, x, GetBlock(scr, from, x));
354 int ClearFullLines(int scr)
355 { //remove full lines, return lines cleared
356 int from, to, x, linescleared = 0;
360 while (to < Players[scr].boardHeight) {
361 while (LineIsFull(scr, from)) {
363 for (x = 0; x<Players[scr].boardWidth; x++) {
364 SetBlock(scr, from, x, GetBlock(scr, from, x)&239);
366 SetBlock(scr, from-2, x, GetBlock(scr, from-2, x)&223);
367 } //don't stick blocks to line which we'll remove
369 CopyLine(scr, from++, to++);
371 linescleared += from - to;
372 } while (CheckFall(scr));
376 void FreezePiece(int scr)
381 void InsertJunk(int scr, int color, int count, int column)
382 { //add <count> junklines with hole at <column> to <scr> by team <color>
385 EraseShape(Players[scr].curShape, scr,
386 Players[scr].curY, Players[scr].curX, 1);
387 for (y = Players[scr].boardHeight - count - 1; y >= 0; --y)
388 CopyLine(scr, y, y + count);
389 for (y = 0; y < count; ++y)
390 for (x = 0; x < Players[scr].boardWidth; ++x)
391 SetBlock(scr, y, x, (x == column) ? BT_none : color + 1
392 + 64 * (x != column-1 && x < Players[scr].boardWidth-1)
393 + 128 * (x != column+1 && x > 0));
394 Players[scr].curY += count; //move piece up..
395 for (y = 0; y < count; ++y)
396 if (ShapeFits(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX))
397 Players[scr].curY--; //..and down again as far as possible
399 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,