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.
25 static const char shapes[7][4][4][4] = {
27 * 4 rotations of 4x4 pixels per shape
28 * high nibble signifies joinage (left, right, top, bottom, from MSB)
29 * low nibble identifies block type (typically 2..8)
34 {0x47, 0xC7, 0x97, 0000},
36 {0000, 0000, 0000, 0000} },
37 { { 0, 0x17, 0, 0000},
39 {0x47, 0xA7, 0, 0000},
40 {0000, 0000, 0000, 0000} },
42 {0x67, 0xC7, 0x87, 0000},
44 {0000, 0000, 0000, 0000} },
45 { { 0, 0x57, 0x87, 0000},
48 {0000, 0000, 0000, 0000} } },
52 {0x53, 0xC3, 0x83, 0000},
54 {0000, 0000, 0000, 0000} },
55 { {0x43, 0x93, 0, 0000},
58 {0000, 0000, 0000, 0000} },
59 { { 0, 0, 0x13, 0000},
60 {0x43, 0xC3, 0xA3, 0000},
62 {0000, 0000, 0000, 0000} },
63 { { 0, 0x13, 0, 0000},
65 { 0, 0x63, 0x83, 0000},
66 {0000, 0000, 0000, 0000} } },
70 {0x48, 0xD8, 0x88, 0000},
72 {0000, 0000, 0000, 0000} },
73 { { 0, 0x18, 0, 0000},
74 {0x48, 0xB8, 0, 0000},
76 {0000, 0000, 0000, 0000} },
77 { { 0, 0x18, 0, 0000},
78 {0x48, 0xE8, 0x88, 0000},
80 {0000, 0000, 0000, 0000} },
81 { { 0, 0x18, 0, 0000},
82 { 0, 0x78, 0x88, 0000},
84 {0000, 0000, 0000, 0000} } },
88 { 0, 0x52, 0x82, 0000},
89 {0x42, 0xA2, 0, 0000},
90 {0000, 0000, 0000, 0000} },
92 {0x62, 0x92, 0, 0000},
94 {0000, 0000, 0000, 0000} },
96 { 0, 0x52, 0x82, 0000},
97 {0x42, 0xA2, 0, 0000},
98 {0000, 0000, 0000, 0000} },
100 {0x62, 0x92, 0, 0000},
102 {0000, 0000, 0000, 0000} } },
105 { { { 0, 0, 0, 0000},
106 {0x46, 0x96, 0, 0000},
107 { 0, 0x66, 0x86, 0000},
108 {0000, 0000, 0000, 0000} },
109 { { 0, 0x16, 0, 0000},
110 {0x56, 0xA6, 0, 0000},
112 {0000, 0000, 0000, 0000} },
114 {0x46, 0x96, 0, 0000},
115 { 0, 0x66, 0x86, 0000},
116 {0000, 0000, 0000, 0000} },
117 { { 0, 0x16, 0, 0000},
118 {0x56, 0xA6, 0, 0000},
120 {0000, 0000, 0000, 0000} } },
124 {0x44, 0xC4, 0xC4, 0x84},
132 {0x44, 0xC4, 0xC4, 0x84},
138 { 0, 0x24, 0, 0} } },
141 { { { 0, 0, 0, 0000},
142 { 0, 0x55, 0x95, 0000},
143 { 0, 0x65, 0xA5, 0000},
144 {0000, 0000, 0000, 0000} },
146 { 0, 0x55, 0x95, 0000},
147 { 0, 0x65, 0xA5, 0000},
148 {0000, 0000, 0000, 0000} },
150 { 0, 0x55, 0x95, 0000},
151 { 0, 0x65, 0xA5, 0000},
152 {0000, 0000, 0000, 0000} },
154 { 0, 0x55, 0x95, 0000},
155 { 0, 0x65, 0xA5, 0000},
156 {0000, 0000, 0000, 0000} } }
159 int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func)
160 { //Draw a certain shape using <ShapeDrawFunc>
166 for (i = 0; i < 4; i++)
167 for (j = 0; j < 4; j++)
168 if (shapes[type][rotation][i][j])
169 if (result = func(scr, y-i, x+j, shapes[type][rotation][i][j]))
175 float stdOptions[7] = {1, 1, 1, 1, 1, 1, 1}; //stdOptions
177 char ChooseOption(float options[7])
178 { //Return a random piece with given piece weight
180 float total = 0, val;
182 for (i = 0; i < 7; i++) total += options[i];
183 val = Random(0, 32767) / 32768.0 * total;
184 for (i = 0; i < 7; i++) if ((val -= options[i]) < 0)
190 static unsigned char board[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
191 static unsigned char oldBoard[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH];
192 static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT];
195 void ClearField(int scr)
196 { //Empty the whole field (all blocks BT_none)
199 for (y = Players[scr].boardHeight - 1; y >= 0; --y)
200 for (x = 0; x < Players[scr].boardWidth; ++x) {
201 oldBoard[scr][y][x] = board[scr][y][x] = BT_none;
205 unsigned char GetBlock(int scr, int y, int x)
206 { //Returns the block on field at position (x,y)
207 if (y < 0 || x < 0 || x >= Players[scr].boardWidth)
209 else if (y >= Players[scr].boardHeight)
212 return board[scr][y][x];
215 static void SetBlock(int scr, int y, int x, unsigned char type)
217 if (y >= 0 && y < Players[scr].boardHeight
218 && x >= 0 && x < Players[scr].boardWidth) {
219 board[scr][y][x] = type;
220 changed[scr][y] |= 1 << x;
224 int RefreshBoard(int scr)
225 { //draw changes to screen
229 for (y = Players[scr].boardVisible - 1; y >= 0; y--)
230 if ((c = changed[scr][y])) { // line changed
231 for (x = 0; c; (c >>= 1), x++)
232 if (c & 1 && board[scr][y][x] != oldBoard[scr][y][x]) {
233 PlotBlock(scr, y, x, board[scr][y][x]);
234 oldBoard[scr][y][x] = board[scr][y][x];
236 changed[scr][y] = 0; // reset
242 int GlanceFunc(int scr, int y, int x, unsigned char type)
244 PlotBlockXY(y, x, type);
248 int ShadowFunc(int scr, int y, int x, unsigned char type)
250 SetBlock(scr, y, x, BT_shadow);
254 int PlotFunc(int scr, int y, int x, unsigned char type)
256 SetBlock(scr, y, x, type);
259 void PlotShape(char shape, int scr, int y, int x, int shadow)
260 { //put shape on field
262 for (shadowy = y - 1; shadowy >= 0; shadowy--)
263 if (!ShapeFits(shape, scr, shadowy, x))
265 ShapeIterate(shape, scr, shadowy + 1, x, ShadowFunc);
267 ShapeIterate(shape, scr, y, x, PlotFunc);
270 int EraseFunc(int scr, int y, int x, unsigned char type)
272 SetBlock(scr, y, x, BT_none);
275 void EraseShape(char shape, int scr, int y, int x, int shadow)
276 { //remove block from field
277 ShapeIterate(shape, scr, y, x, EraseFunc);
278 if (shadow && scr == me) // draw shadow
279 ShapeIterate(shape, scr, shadowy + 1, x, EraseFunc);
282 int CollisionFunc(int scr, int y, int x, unsigned char type)
284 return GetBlock(scr, y, x) > BT_none;
286 int ShapeFits(char shape, int scr, int y, int x)
287 { //check if there's nothing in the way
288 return !ShapeIterate(shape, scr, y, x, CollisionFunc);
291 int VisibleFunc(int scr, int y, int x, unsigned char type)
293 return (y >= 0 && y < Players[scr].boardVisible &&
294 x >= 0 && x < Players[scr].boardWidth);
296 int ShapeVisible(char shape, int scr, int y, int x)
298 return ShapeIterate(shape, scr, y, x, VisibleFunc);
301 int MovePiece(int scr, int deltaY, int deltaX)
305 EraseShape(Players[scr].curShape, scr,
306 Players[scr].curY, Players[scr].curX, 1);
307 result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY,
308 Players[scr].curX + deltaX);
310 Players[scr].curY += deltaY;
311 Players[scr].curX += deltaX;
313 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
318 int RotatePiece(int scr, int dir)
323 EraseShape(Players[scr].curShape, scr, Players[scr].curY,
324 Players[scr].curX, 1);
325 /* (inc|dec)rement only 3 least significant bits which indicate rotation */
326 newshape = (Players[scr].curShape & 252) + (((Players[scr].curShape & 3) + dir) & 3);
327 result = ShapeFits(newshape, scr, Players[scr].curY, Players[scr].curX);
329 // move if it doesn't fit anymore
331 for (slideX = 0; slideX < 2; slideX = -slideX) {
332 // slide left and right
333 if (slideX >= 0) slideX++; // slide more
334 if (result = ShapeFits(newshape, scr, Players[scr].curY,
335 Players[scr].curX+slideX)) break;
337 if (result) Players[scr].curX += slideX;
339 if (result) Players[scr].curShape = newshape;
340 PlotShape(Players[scr].curShape, scr,
341 Players[scr].curY, Players[scr].curX, scr == me);
345 int DropPiece(int scr)
349 EraseShape(Players[scr].curShape, scr,
350 Players[scr].curY, Players[scr].curX, 1);
351 while (ShapeFits(Players[scr].curShape, scr,
352 Players[scr].curY - 1, Players[scr].curX)) {
356 PlotShape(Players[scr].curShape, scr,
357 Players[scr].curY, Players[scr].curX, 0);
361 int BlockFree(int scr, int x, int y, unsigned char z)
362 { //Check if blocks are empty below block (x,y) and sticking to (x,y) mask <z>
363 unsigned char curblock;
365 if (y == 0) return 0; // at bottom
366 curblock = GetBlock(scr, y, x) & z;
367 if (curblock & 0x10 && !BlockFree(scr, x, y-1, z & 0xD0)) return 0;
368 if (curblock & 0x20 && !BlockFree(scr, x, y+1, z & 0xE0)) return 0;
369 if (curblock & 0x40 && !BlockFree(scr, x+1, y, z & 0x70)) return 0;
370 if (curblock & 0x80 && !BlockFree(scr, x-1, y, z & 0xB0)) return 0;
371 if ((z = GetBlock(scr, y-1, x)) & 0x20) return 1; // stuck to block below
372 if (z > BT_none) return 0; // some other piece below
373 return 1; // nothing below
376 int BlockFall(int scr, int x, int y, unsigned char z)
377 { //Drop down block (x,y) and those sticking to it mask <z>
378 if (GetBlock(scr, y, x) & z & 0x10) BlockFall(scr, x, y-1, z & 0xD0);
379 if (GetBlock(scr, y, x) & z & 0x20) BlockFall(scr, x, y+1, z & 0xE0);
380 if (GetBlock(scr, y, x) & z & 0x40) BlockFall(scr, x+1, y, z & 0x70);
381 if (GetBlock(scr, y, x) & z & 0x80) BlockFall(scr, x-1, y, z & 0xB0);
382 SetBlock(scr, y-1, x, GetBlock(scr, y, x));
383 SetBlock(scr, y, x, BT_none);
386 int CheckFall(int scr)
387 { //Drop any free blocks on field
388 int xloop, x, x2, y, fallen = 0;
391 if (!Game.gravity) return 0;
392 for (y = Players[scr].boardHeight - 1; y > 0; y--)
393 for (x = 0; x < Players[scr].boardWidth; x++) {
394 if ((z = GetBlock(scr, y, x)) > BT_none && (z & 0xA0) == 0) {
395 // block present which doesn't stick left/up => topleft block
396 if (BlockFree(scr, x, y, 0xF0)) {
397 BlockFall(scr, x, y, 0xF0); // move blocks down
405 int LineIsFull(int scr, int y)
406 { //return 0 if any blocks present on line <y>
409 for (x = 0; x < Players[scr].boardWidth; x++)
410 if (GetBlock(scr, y, x) <= BT_none)
415 void CopyLine(int scr, int from, int to)
416 { //move blocks on line <from> to line <to>
420 for (x = 0; x < Players[scr].boardWidth; ++x)
421 SetBlock(scr, to, x, GetBlock(scr, from, x));
424 int ClearFullLines(int scr)
425 { //remove full lines, return lines cleared
426 int from, to, x, linescleared = 0;
430 while (to < Players[scr].boardHeight) {
431 while (LineIsFull(scr, from)) {
433 for (x = 0; x<Players[scr].boardWidth; x++) {
434 // don't stick blocks to line which we'll remove
435 SetBlock(scr, from, x, GetBlock(scr, from, x) & 0xEF);
437 SetBlock(scr, from-2, x, GetBlock(scr, from-2, x) & 0xDF);
440 CopyLine(scr, from++, to++);
442 linescleared += from - to;
443 } while (CheckFall(scr));
447 void InsertJunk(int scr, int color, int count, int column)
448 { //add <count> junklines with hole at <column> to <scr> by team <color>
451 EraseShape(Players[scr].curShape, scr,
452 Players[scr].curY, Players[scr].curX, 1);
453 for (y = Players[scr].boardHeight - count - 1; y >= 0; --y)
454 CopyLine(scr, y, y + count);
455 for (y = 0; y < count; ++y)
456 for (x = 0; x < Players[scr].boardWidth; ++x)
457 SetBlock(scr, y, x, x == column ? BT_none : color + 1
458 + 0x40 * (x != column-1 && x < Players[scr].boardWidth-1)
459 + 0x80 * (x != column+1 && x > 0));
460 Players[scr].curY += count; // move piece up..
461 for (y = 0; y < count; ++y)
462 if (ShapeFits(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX))
463 Players[scr].curY--; // ...and down again as far as possible
465 PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,