unofficial version 0.6: first major updates
[netris.git] / board.c
diff --git a/board.c b/board.c
index 245cea1d43eb6194f697f82c8488a840f0d59864..8524cc5b7adeceff018baa0c2b8df445fca8880a 100644 (file)
--- a/board.c
+++ b/board.c
@@ -34,14 +34,6 @@ static unsigned int changed[MAX_SCREENS][MAX_BOARD_HEIGHT];
 static int falling[MAX_SCREENS][MAX_BOARD_WIDTH];
 static int oldFalling[MAX_SCREENS][MAX_BOARD_WIDTH];
 
-ExtFunc void InitBoard(int scr)
-{
-       boardHeight[scr] = MAX_BOARD_HEIGHT;
-       boardVisible[scr] = 20;
-       boardWidth[scr] = 10;
-       InitScreen(scr);
-}
-
 ExtFunc void CleanupBoard(int scr)
 {
        CleanupScreen(scr);
@@ -49,9 +41,9 @@ ExtFunc void CleanupBoard(int scr)
 
 ExtFunc BlockType GetBlock(int scr, int y, int x)
 {
-       if (y < 0 || x < 0 || x >= boardWidth[scr])
+       if (y < 0 || x < 0 || x >= Players[scr].boardWidth)
                return BT_wall;
-       else if (y >= boardHeight[scr])
+       else if (y >= Players[scr].boardHeight)
                return BT_none;
        else
                return abs(board[scr][y][x]);
@@ -59,8 +51,9 @@ ExtFunc BlockType GetBlock(int scr, int y, int x)
 
 ExtFunc void SetBlock(int scr, int y, int x, BlockType type)
 {
-       if (y >= 0 && y < boardHeight[scr] && x >= 0 && x < boardWidth[scr]) {
-               if (y < boardVisible[scr])
+       if (y >= 0 && y < Players[scr].boardHeight &&
+               x >= 0 && x < Players[scr].boardWidth) {
+               if (y < Players[scr].boardVisible)
                        falling[scr][x] += (type < 0) - (board[scr][y][x] < 0);
                board[scr][y][x] = type;
                changed[scr][y] |= 1 << x;
@@ -73,11 +66,11 @@ ExtFunc int RefreshBoard(int scr)
        unsigned int c;
        BlockType b;
 
-       for (y = boardVisible[scr] - 1; y >= 0; --y)
+       for (y = Players[scr].boardVisible - 1; y >= 0; --y)
                if ((c = changed[scr][y])) {
                        if (robotEnable) {
                                RobotCmd(0, "RowUpdate %d %d", scr, y);
-                               for (x = 0; x < boardWidth[scr]; ++x) {
+                               for (x = 0; x < Players[scr].boardWidth; ++x) {
                                        b = board[scr][y][x];
                                        if (fairRobot)
                                                b = abs(b);
@@ -95,7 +88,7 @@ ExtFunc int RefreshBoard(int scr)
                }
        if (robotEnable)
                RobotTimeStamp();
-       for (x = 0; x < boardWidth[scr]; ++x)
+       for (x = 0; x < Players[scr].boardWidth; ++x)
                if (oldFalling[scr][x] != !!falling[scr][x]) {
                        oldFalling[scr][x] = !!falling[scr][x];
                        PlotUnderline(scr, x, oldFalling[scr][x]);
@@ -106,7 +99,7 @@ ExtFunc int RefreshBoard(int scr)
 
 ExtFunc int GlanceFunc(int scr, int y, int x, BlockType type, void *data)
 {
-       PlotBlock1(scr, y, x, type);
+       PlotBlock1(scr, 20 - y, x * 2, type);
        return 0;
 }
 
@@ -129,7 +122,8 @@ ExtFunc int CollisionFunc(int scr, int y, int x, BlockType type, void *data)
 
 ExtFunc int VisibleFunc(int scr, int y, int x, BlockType type, void *data)
 {
-       return (y >= 0 && y < boardVisible[scr] && x >= 0 && x < boardWidth[scr]);
+       return (y >= 0 && y < Players[scr].boardVisible &&
+                       x >= 0 && x < Players[scr].boardWidth);
 }
 
 ExtFunc void PlotShape(Shape *shape, int scr, int y, int x, int falling)
@@ -156,14 +150,15 @@ ExtFunc int MovePiece(int scr, int deltaY, int deltaX)
 {
        int result;
 
-       EraseShape(curShape[scr], scr, curY[scr], curX[scr]);
-       result = ShapeFits(curShape[scr], scr, curY[scr] + deltaY,
-                               curX[scr] + deltaX);
+       EraseShape(Players[scr].curShape, scr,
+               Players[scr].curY, Players[scr].curX);
+       result = ShapeFits(Players[scr].curShape, scr, Players[scr].curY + deltaY,
+                               Players[scr].curX + deltaX);
        if (result) {
-               curY[scr] += deltaY;
-               curX[scr] += deltaX;
+               Players[scr].curY += deltaY;
+               Players[scr].curX += deltaX;
        }
-       PlotShape(curShape[scr], scr, curY[scr], curX[scr], 1);
+       PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1);
        return result;
 }
 
@@ -171,12 +166,15 @@ ExtFunc int RotatePiece(int scr, int dir)
 {
        int result;
 
-       EraseShape(curShape[scr], scr, curY[scr], curX[scr]);
-       result = ShapeFits(dir ? curShape[scr]->rotateTo : curShape[scr]->rotateFrom,
-               scr, curY[scr], curX[scr]);
+       EraseShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX);
+       result = ShapeFits(dir ? Players[scr].curShape->rotateTo
+                                                  : Players[scr].curShape->rotateFrom,
+               scr, Players[scr].curY, Players[scr].curX);
        if (result)
-               curShape[scr] = dir ? curShape[scr]->rotateTo : curShape[scr]->rotateFrom;
-       PlotShape(curShape[scr], scr, curY[scr], curX[scr], 1);
+               Players[scr].curShape = dir ? Players[scr].curShape->rotateTo
+                                                                       : Players[scr].curShape->rotateFrom;
+       PlotShape(Players[scr].curShape, scr,
+                       Players[scr].curY, Players[scr].curX, 1);
        return result;
 }
 
@@ -184,12 +182,15 @@ ExtFunc int DropPiece(int scr)
 {
        int count = 0;
 
-       EraseShape(curShape[scr], scr, curY[scr], curX[scr]);
-       while (ShapeFits(curShape[scr], scr, curY[scr] - 1, curX[scr])) {
-               --curY[scr];
+       EraseShape(Players[scr].curShape, scr,
+               Players[scr].curY, Players[scr].curX);
+       while (ShapeFits(Players[scr].curShape, scr,
+                       Players[scr].curY - 1, Players[scr].curX)) {
+               --Players[scr].curY;
                ++count;
        }
-       PlotShape(curShape[scr], scr, curY[scr], curX[scr], 1);
+       PlotShape(Players[scr].curShape, scr,
+               Players[scr].curY, Players[scr].curX, 1);
        return count;
 }
 
@@ -197,7 +198,7 @@ ExtFunc int LineIsFull(int scr, int y)
 {
        int x;
 
-       for (x = 0; x < boardWidth[scr]; ++x)
+       for (x = 0; x < Players[scr].boardWidth; ++x)
                if (GetBlock(scr, y, x) == BT_none)
                        return 0;
        return 1;
@@ -208,7 +209,7 @@ ExtFunc void CopyLine(int scr, int from, int to)
        int x;
 
        if (from != to)
-               for (x = 0; x < boardWidth[scr]; ++x)
+               for (x = 0; x < Players[scr].boardWidth; ++x)
                        SetBlock(scr, to, x, GetBlock(scr, from, x));
 }
 
@@ -217,7 +218,7 @@ ExtFunc int ClearFullLines(int scr)
        int from, to;
 
        from = to = 0;
-       while (to < boardHeight[scr]) {
+       while (to < Players[scr].boardHeight) {
                while (LineIsFull(scr, from))
                        ++from;
                CopyLine(scr, from++, to++);
@@ -230,8 +231,8 @@ ExtFunc void FreezePiece(int scr)
        int y, x;
        BlockType type;
 
-       for (y = 0; y < boardHeight[scr]; ++y)
-               for (x = 0; x < boardWidth[scr]; ++x)
+       for (y = 0; y < Players[scr].boardHeight; ++y)
+               for (x = 0; x < Players[scr].boardWidth; ++x)
                        if ((type = board[scr][y][x]) < 0)
                                SetBlock(scr, y, x, -type);
 }
@@ -240,12 +241,19 @@ ExtFunc void InsertJunk(int scr, int count, int column)
 {
        int y, x;
 
-       for (y = boardHeight[scr] - count - 1; y >= 0; --y)
+       EraseShape(Players[scr].curShape, scr,
+               Players[scr].curY, Players[scr].curX);
+       for (y = Players[scr].boardHeight - count - 1; y >= 0; --y)
                CopyLine(scr, y, y + count);
        for (y = 0; y < count; ++y)
-               for (x = 0; x < boardWidth[scr]; ++x)
+               for (x = 0; x < Players[scr].boardWidth; ++x)
                        SetBlock(scr, y, x, (x == column) ? BT_none : BT_white);
-       curY[scr] += count;
+       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))
+                       Players[scr].curY--; //..and down as far as possible
+               else break;
+       PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX, 1);
 }
 
 /*