X-Git-Url: http://git.shiar.nl/netris.git/blobdiff_plain/d548a7f09a0849ede6b8cd57747bb230a0eebb59..HEAD:/board.c diff --git a/board.c b/board.c index 22b200c..71ad1dc 100644 --- a/board.c +++ b/board.c @@ -156,7 +156,7 @@ static const char shapes[7][4][4][4] = { {0000, 0000, 0000, 0000} } } }; -int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func) +int shape_iterate(char s, int scr, int y, int x, ShapeDrawFunc func) { //Draw a certain shape using int i, j, result; char type, rotation; @@ -172,8 +172,6 @@ int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func) } -float stdOptions[7] = {1, 1, 1, 1, 1, 1, 1}; //stdOptions - char ChooseOption(float options[7]) { //Return a random piece with given piece weight int i; @@ -192,7 +190,7 @@ static unsigned char oldBoard[MAX_SCREENS][MAX_BOARD_HEIGHT][MAX_BOARD_WIDTH]; static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT]; static int shadowy; -void ClearField(int scr) +void player_empty(int scr) { //Empty the whole field (all blocks BT_none) int y, x; @@ -202,7 +200,7 @@ void ClearField(int scr) } } -unsigned char GetBlock(int scr, int y, int x) +unsigned char block_get(int scr, int y, int x) { //Returns the block on field at position (x,y) if (y < 0 || x < 0 || x >= Players[scr].boardWidth) return BT_wall; @@ -212,7 +210,7 @@ unsigned char GetBlock(int scr, int y, int x) return board[scr][y][x]; } -static void SetBlock(int scr, int y, int x, unsigned char type) +static void block_set(int scr, int y, int x, unsigned char type) { if (y >= 0 && y < Players[scr].boardHeight && x >= 0 && x < Players[scr].boardWidth) { @@ -221,7 +219,7 @@ static void SetBlock(int scr, int y, int x, unsigned char type) } } -int RefreshBoard(int scr) +int player_draw(int scr) { //draw changes to screen int y, x, any = 0; unsigned int c; @@ -230,7 +228,7 @@ int RefreshBoard(int scr) if ((c = changed[scr][y])) { // line changed for (x = 0; c; (c >>= 1), x++) if (c & 1 && board[scr][y][x] != oldBoard[scr][y][x]) { - PlotBlock(scr, y, x, board[scr][y][x]); + block_draw_window(scr, y, x, board[scr][y][x]); oldBoard[scr][y][x] = board[scr][y][x]; } changed[scr][y] = 0; // reset @@ -239,151 +237,154 @@ int RefreshBoard(int scr) return any; } -int GlanceFunc(int scr, int y, int x, unsigned char type) +int block_iter_set_status(int scr, int y, int x, unsigned char type) { - PlotBlockXY(y, x, type); + block_draw_status(y, x, type); return 0; } -int ShadowFunc(int scr, int y, int x, unsigned char type) +static int block_iter_shadow(int scr, int y, int x, unsigned char type) { //draw shadow - SetBlock(scr, y, x, BT_shadow); + block_set(scr, y, x, BT_shadow); return 0; } -int PlotFunc(int scr, int y, int x, unsigned char type) +static int block_iter_set(int scr, int y, int x, unsigned char type) { - SetBlock(scr, y, x, type); + block_set(scr, y, x, type); return 0; } -void PlotShape(char shape, int scr, int y, int x, int shadow) + +void shape_draw(char shape, int scr, int y, int x, int shadow) { //put shape on field if (shadow) { for (shadowy = y - 1; shadowy >= 0; shadowy--) - if (!ShapeFits(shape, scr, shadowy, x)) + if (!shape_get(shape, scr, shadowy, x)) break; - ShapeIterate(shape, scr, shadowy + 1, x, ShadowFunc); + shape_iterate(shape, scr, shadowy + 1, x, block_iter_shadow); } //draw shadow - ShapeIterate(shape, scr, y, x, PlotFunc); + shape_iterate(shape, scr, y, x, block_iter_set); } -int EraseFunc(int scr, int y, int x, unsigned char type) +static int block_iter_erase(int scr, int y, int x, unsigned char type) { - SetBlock(scr, y, x, BT_none); + block_set(scr, y, x, BT_none); return 0; } -void EraseShape(char shape, int scr, int y, int x, int shadow) + +void shape_erase(char shape, int scr, int y, int x, int shadow) { //remove block from field - ShapeIterate(shape, scr, y, x, EraseFunc); + shape_iterate(shape, scr, y, x, block_iter_erase); if (shadow && scr == me) // draw shadow - ShapeIterate(shape, scr, shadowy + 1, x, EraseFunc); + shape_iterate(shape, scr, shadowy + 1, x, block_iter_erase); } -int CollisionFunc(int scr, int y, int x, unsigned char type) +static int block_iter_get(int scr, int y, int x, unsigned char type) { - return GetBlock(scr, y, x) > BT_none; + return block_get(scr, y, x) > BT_none; } -int ShapeFits(char shape, int scr, int y, int x) + +int shape_get(char shape, int scr, int y, int x) { //check if there's nothing in the way - return !ShapeIterate(shape, scr, y, x, CollisionFunc); + return !shape_iterate(shape, scr, y, x, block_iter_get); } -int VisibleFunc(int scr, int y, int x, unsigned char type) +static int block_iter_visible(int scr, int y, int x, unsigned char type) { return (y >= 0 && y < Players[scr].boardVisible && x >= 0 && x < Players[scr].boardWidth); } -int ShapeVisible(char shape, int scr, int y, int x) +int shape_visible(char shape, int scr, int y, int x) { - return ShapeIterate(shape, scr, y, x, VisibleFunc); + return shape_iterate(shape, scr, y, x, block_iter_visible); } -int MovePiece(int scr, int deltaY, int deltaX) +int player_move(int scr, int deltaY, int deltaX) { int result; - EraseShape(Players[scr].curShape, scr, + shape_erase(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1); - result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY, + result = shape_get(Players[scr].curShape, scr, Players[scr].curY + deltaY, Players[scr].curX + deltaX); if (result) { Players[scr].curY += deltaY; Players[scr].curX += deltaX; } - PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, + shape_draw(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, scr == me); return result; } -int RotatePiece(int scr, int dir) +int player_rotate(int scr, int dir) { char newshape; int result; - EraseShape(Players[scr].curShape, scr, Players[scr].curY, + shape_erase(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1); /* (inc|dec)rement only 3 least significant bits which indicate rotation */ newshape = (Players[scr].curShape & 252) + (((Players[scr].curShape & 3) + dir) & 3); - result = ShapeFits(newshape, scr, Players[scr].curY, Players[scr].curX); + result = shape_get(newshape, scr, Players[scr].curY, Players[scr].curX); if (!result) { // move if it doesn't fit anymore short int slideX; for (slideX = 0; slideX < 2; slideX = -slideX) { // slide left and right if (slideX >= 0) slideX++; // slide more - if (result = ShapeFits(newshape, scr, Players[scr].curY, + if (result = shape_get(newshape, scr, Players[scr].curY, Players[scr].curX+slideX)) break; } if (result) Players[scr].curX += slideX; } if (result) Players[scr].curShape = newshape; - PlotShape(Players[scr].curShape, scr, + shape_draw(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, scr == me); return result; } -int DropPiece(int scr) +int player_drop(int scr) { int count = 0; - EraseShape(Players[scr].curShape, scr, + shape_erase(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1); - while (ShapeFits(Players[scr].curShape, scr, + while (shape_get(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX)) { Players[scr].curY--; count++; } - PlotShape(Players[scr].curShape, scr, + shape_draw(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 0); return count; } -int BlockFree(int scr, int x, int y, unsigned char z) +static int block_free(int scr, int x, int y, unsigned char z) { //Check if blocks are empty below block (x,y) and sticking to (x,y) mask unsigned char curblock; if (y == 0) return 0; // at bottom - curblock = GetBlock(scr, y, x) & z; - if (curblock & 0x10 && !BlockFree(scr, x, y-1, z & 0xD0)) return 0; - if (curblock & 0x20 && !BlockFree(scr, x, y+1, z & 0xE0)) return 0; - if (curblock & 0x40 && !BlockFree(scr, x+1, y, z & 0x70)) return 0; - if (curblock & 0x80 && !BlockFree(scr, x-1, y, z & 0xB0)) return 0; - if ((z = GetBlock(scr, y-1, x)) & 0x20) return 1; // stuck to block below + curblock = block_get(scr, y, x) & z; + if (curblock & 0x10 && !block_free(scr, x, y-1, z & 0xD0)) return 0; + if (curblock & 0x20 && !block_free(scr, x, y+1, z & 0xE0)) return 0; + if (curblock & 0x40 && !block_free(scr, x+1, y, z & 0x70)) return 0; + if (curblock & 0x80 && !block_free(scr, x-1, y, z & 0xB0)) return 0; + if ((z = block_get(scr, y-1, x)) & 0x20) return 1; // stuck to block below if (z > BT_none) return 0; // some other piece below return 1; // nothing below } -int BlockFall(int scr, int x, int y, unsigned char z) +static int block_down(int scr, int x, int y, unsigned char z) { //Drop down block (x,y) and those sticking to it mask - if (GetBlock(scr, y, x) & z & 0x10) BlockFall(scr, x, y-1, z & 0xD0); - if (GetBlock(scr, y, x) & z & 0x20) BlockFall(scr, x, y+1, z & 0xE0); - if (GetBlock(scr, y, x) & z & 0x40) BlockFall(scr, x+1, y, z & 0x70); - if (GetBlock(scr, y, x) & z & 0x80) BlockFall(scr, x-1, y, z & 0xB0); - SetBlock(scr, y-1, x, GetBlock(scr, y, x)); - SetBlock(scr, y, x, BT_none); + if (block_get(scr, y, x) & z & 0x10) block_down(scr, x, y-1, z & 0xD0); + if (block_get(scr, y, x) & z & 0x20) block_down(scr, x, y+1, z & 0xE0); + if (block_get(scr, y, x) & z & 0x40) block_down(scr, x+1, y, z & 0x70); + if (block_get(scr, y, x) & z & 0x80) block_down(scr, x-1, y, z & 0xB0); + block_set(scr, y-1, x, block_get(scr, y, x)); + block_set(scr, y, x, BT_none); } -int CheckFall(int scr) +int player_down(int scr) { //Drop any free blocks on field int xloop, x, x2, y, fallen = 0; unsigned char z; @@ -391,10 +392,10 @@ int CheckFall(int scr) if (!Game.gravity) return 0; for (y = Players[scr].boardHeight - 1; y > 0; y--) for (x = 0; x < Players[scr].boardWidth; x++) { - if ((z = GetBlock(scr, y, x)) > BT_none && (z & 0xA0) == 0) { + if ((z = block_get(scr, y, x)) > BT_none && (z & 0xA0) == 0) { // block present which doesn't stick left/up => topleft block - if (BlockFree(scr, x, y, 0xF0)) { - BlockFall(scr, x, y, 0xF0); // move blocks down + if (block_free(scr, x, y, 0xF0)) { + block_down(scr, x, y, 0xF0); // move blocks down fallen++; } } //block present @@ -402,67 +403,67 @@ int CheckFall(int scr) return fallen; } -int LineIsFull(int scr, int y) +static int player_linecheck(int scr, int y) { //return 0 if any blocks present on line int x; for (x = 0; x < Players[scr].boardWidth; x++) - if (GetBlock(scr, y, x) <= BT_none) + if (block_get(scr, y, x) <= BT_none) return 0; return 1; } -void CopyLine(int scr, int from, int to) +static void player_linecopy(int scr, int from, int to) { //move blocks on line to line int x; if (from != to) for (x = 0; x < Players[scr].boardWidth; ++x) - SetBlock(scr, to, x, GetBlock(scr, from, x)); + block_set(scr, to, x, block_get(scr, from, x)); } -int ClearFullLines(int scr) +int player_lineclear(int scr) { //remove full lines, return lines cleared int from, to, x, linescleared = 0; do { from = to = 0; while (to < Players[scr].boardHeight) { - while (LineIsFull(scr, from)) { + while (player_linecheck(scr, from)) { from++; // skip for (x = 0; x 1) - SetBlock(scr, from-2, x, GetBlock(scr, from-2, x) & 0xDF); + block_set(scr, from-2, x, block_get(scr, from-2, x) & 0xDF); } } - CopyLine(scr, from++, to++); + player_linecopy(scr, from++, to++); } linescleared += from - to; - } while (CheckFall(scr)); + } while (player_down(scr)); return linescleared; } -void InsertJunk(int scr, int color, int count, int column) +void player_lineadd(int scr, int color, int count, int column) { //add junklines with hole at to by team int y, x; - EraseShape(Players[scr].curShape, scr, + shape_erase(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1); for (y = Players[scr].boardHeight - count - 1; y >= 0; --y) - CopyLine(scr, y, y + count); + player_linecopy(scr, y, y + count); for (y = 0; y < count; ++y) for (x = 0; x < Players[scr].boardWidth; ++x) - SetBlock(scr, y, x, x == column ? BT_none : color + 1 + block_set(scr, y, x, x == column ? BT_none : color + 1 + 0x40 * (x != column-1 && x < Players[scr].boardWidth-1) + 0x80 * (x != column+1 && x > 0)); Players[scr].curY += count; // move piece up.. for (y = 0; y < count; ++y) - if (ShapeFits(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX)) + if (shape_get(Players[scr].curShape, scr, Players[scr].curY - 1, Players[scr].curX)) Players[scr].curY--; // ...and down again as far as possible else break; - PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, + shape_draw(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, scr == me); }