document shapes array
[netris.git] / board.c
diff --git a/board.c b/board.c
index 7946d571fc82ce14212f4505eb4ed4939c72963c..538a0dfe406f1d4aec0ceee05596dd18705d08de 100644 (file)
--- a/board.c
+++ b/board.c
@@ -15,8 +15,6 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * $Id: board.c,v 1.15 1999/05/16 06:56:24 mhw Exp $
  */
 
 #include "netris.h"
 #include "board.h"
 
 static const char shapes[7][4][4][4] = {
+       /*
+        * 4 rotations of 4x4 pixels per shape
+        * high nibble signifies joinage (left, right, top, bottom, from MSB)
+        * low nibble identifies block type (typically 2..8)
+        */
+
        { { {0x00, 0x00, 0x00, 0x00}, {0x47, 0xC7, 0x97, 0x00},
            {0x00, 0x00, 0x27, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //sharp horizontal
          { {0x00, 0x17, 0x00, 0x00}, {0x00, 0x37, 0x00, 0x00},
@@ -53,18 +57,18 @@ static const char shapes[7][4][4][4] = {
            {0x00, 0x28, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //T (white)
 
        { { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x52, 0x82, 0x00},
-           {0x42, 0xA2, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} },
+           {0x42, 0xA2, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //lieing
          { {0x12, 0x00, 0x00, 0x00}, {0x62, 0x92, 0x00, 0x00},
-           {0x00, 0x22, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} },
+           {0x00, 0x22, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //standing
          { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x52, 0x82, 0x00},
            {0x42, 0xA2, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
          { {0x12, 0x00, 0x00, 0x00}, {0x62, 0x92, 0x00, 0x00},
            {0x00, 0x22, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} } }, //S (green)
 
        { { {0x00, 0x00, 0x00, 0x00}, {0x46, 0x96, 0x00, 0x00},
-           {0x00, 0x66, 0x86, 0x00}, {0x00, 0x00, 0x00, 0x00} },
+           {0x00, 0x66, 0x86, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //lieing
          { {0x00, 0x16, 0x00, 0x00}, {0x56, 0xA6, 0x00, 0x00},
-           {0x26, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} },
+           {0x26, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //standing
          { {0x00, 0x00, 0x00, 0x00}, {0x46, 0x96, 0x00, 0x00},
            {0x00, 0x66, 0x86, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
          { {0x00, 0x16, 0x00, 0x00}, {0x56, 0xA6, 0x00, 0x00},
@@ -77,7 +81,7 @@ static const char shapes[7][4][4][4] = {
          { {0x00, 0x00, 0x00, 0x00}, {0x44, 0xC4, 0xC4, 0x84},
            {0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
          { {0x00, 0x14, 0x00, 0x00}, {0x00, 0x34, 0x00, 0x00},
-           {0x00, 0x34, 0x00, 0x00}, {0x00, 0x24, 0x00, 0x00} } }, //stick (blue)
+           {0x00, 0x34, 0x00, 0x00}, {0x00, 0x24, 0x00, 0x00} } }, //I, stick (blue)
 
        { { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
            {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} },
@@ -86,7 +90,7 @@ static const char shapes[7][4][4][4] = {
          { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
            {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} }, //rep
          { {0x00, 0x00, 0x00, 0x00}, {0x00, 0x55, 0x95, 0x00},
-           {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} } } //square (purple)
+           {0x00, 0x65, 0xA5, 0x00}, {0x00, 0x00, 0x00, 0x00} } } //O, square (purple)
 };
 
 int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func)
@@ -94,10 +98,10 @@ int ShapeIterate(char s, int scr, int y, int x, ShapeDrawFunc func)
        int i, j, result;
        char type, rotation;
 
-       type = s/4;
-       rotation = s&3;
-       for (i = 0; i<4; i++)
-               for (j = 0; j<4; j++)
+       type = s / 4;
+       rotation = s & 3;
+       for (i = 0; i < 4; i++)
+               for (j = 0; j < 4; j++)
                        if (shapes[type][rotation][i][j])
                                if (result = func(scr, y-i, x+j, shapes[type][rotation][i][j]))
                                        return result;
@@ -112,9 +116,10 @@ char ChooseOption(float options[7])
        int i;
        float total = 0, val;
 
-       for (i = 0; i<7; i++) total += options[i];
-       val = Random(0, 32767)/32768.0*total;
-       for (i = 0; i<7; i++) if ((val -= options[i])<0) return i<<2;
+       for (i = 0; i < 7; i++) total += options[i];
+       val = Random(0, 32767) / 32768.0 * total;
+       for (i = 0; i < 7; i++) if ((val -= options[i]) < 0)
+               return i << 2;
        return 0;
 }
 
@@ -132,7 +137,7 @@ void ClearField(int scr)
                for (x = 0; x < Players[scr].boardWidth; ++x) {
                        oldBoard[scr][y][x] = board[scr][y][x] = BT_none;
                }
-} //ClearField
+}
 
 unsigned char GetBlock(int scr, int y, int x)
 { //Returns the block on field at position (x,y)
@@ -142,7 +147,7 @@ unsigned char GetBlock(int scr, int y, int x)
                return BT_none;
        else
                return board[scr][y][x];
-} //GetBlock
+}
 
 void SetBlock(int scr, int y, int x, unsigned char type)
 {
@@ -151,7 +156,7 @@ void SetBlock(int scr, int y, int x, unsigned char type)
                board[scr][y][x] = type;
                changed[scr][y] |= 1 << x;
        }
-} //SetBlock
+}
 
 int RefreshBoard(int scr)
 { //draw changes to screen
@@ -161,7 +166,7 @@ int RefreshBoard(int scr)
        for (y = Players[scr].boardVisible - 1; y >= 0; y--)
                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]) {
+                               if (c & 1 && board[scr][y][x] != oldBoard[scr][y][x]) {
                                        PlotBlock(scr, y, x, board[scr][y][x]);
                                        oldBoard[scr][y][x] = board[scr][y][x];
                                }
@@ -169,19 +174,19 @@ int RefreshBoard(int scr)
                        any = 1;
                } //changed row
        return any;
-} //RefreshBoard
+}
 
 int GlanceFunc(int scr, int y, int x, unsigned char type)
 {
        PlotBlockXY(y, x, type);
        return 0;
-} //GlanceFunc
+}
 
 int ShadowFunc(int scr, int y, int x, unsigned char type)
 { //draw shadow
        SetBlock(scr, y, x, BT_shadow);
        return 0;
-} //ShadowFunc
+}
 
 int PlotFunc(int scr, int y, int x, unsigned char type)
 {
@@ -197,7 +202,7 @@ void PlotShape(char shape, int scr, int y, int x, int shadow)
                ShapeIterate(shape, scr, shadowy + 1, x, ShadowFunc);
        } //draw shadow
        ShapeIterate(shape, scr, y, x, PlotFunc);
-} //PlotShape
+}
 
 int EraseFunc(int scr, int y, int x, unsigned char type)
 {
@@ -209,7 +214,7 @@ void EraseShape(char shape, int scr, int y, int x, int shadow)
        ShapeIterate(shape, scr, y, x, EraseFunc);
        if (shadow && scr == me) //draw shadow
                ShapeIterate(shape, scr, shadowy + 1, x, EraseFunc);
-} //EraseShape
+}
 
 int CollisionFunc(int scr, int y, int x, unsigned char type)
 {
@@ -218,7 +223,7 @@ int CollisionFunc(int scr, int y, int x, unsigned char type)
 int ShapeFits(char shape, int scr, int y, int x)
 { //check if there's nothing in the way
        return !ShapeIterate(shape, scr, y, x, CollisionFunc);
-} //ShapeFits
+}
 
 int VisibleFunc(int scr, int y, int x, unsigned char type)
 {
@@ -228,7 +233,7 @@ int VisibleFunc(int scr, int y, int x, unsigned char type)
 int ShapeVisible(char shape, int scr, int y, int x)
 {
        return ShapeIterate(shape, scr, y, x, VisibleFunc);
-} //ShapeVisible
+}
 
 int MovePiece(int scr, int deltaY, int deltaX)
 {
@@ -245,7 +250,7 @@ int MovePiece(int scr, int deltaY, int deltaX)
        PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
                scr == me);
        return result;
-} //MovePiece
+}
 
 int RotatePiece(int scr, int dir)
 {
@@ -270,7 +275,7 @@ int RotatePiece(int scr, int dir)
        PlotShape(Players[scr].curShape, scr,
                Players[scr].curY, Players[scr].curX, scr == me);
        return result;
-} //RotatePiece
+}
 
 int DropPiece(int scr)
 {
@@ -286,7 +291,7 @@ int DropPiece(int scr)
        PlotShape(Players[scr].curShape, scr,
                Players[scr].curY, Players[scr].curX, 0);
        return count;
-} //DropPiece
+}
 
 int BlockFree(int scr, int x, int y, unsigned char z)
 { //Check if blocks are empty below block (x,y) and sticking to (x,y) mask <z>
@@ -294,21 +299,21 @@ int BlockFree(int scr, int x, int y, unsigned char z)
 
        if (y == 0) return 0; //at bottom
        curblock = GetBlock(scr, y, x) & z;
-       if (curblock & 16  && !BlockFree(scr, x, y-1, z & 208)) return 0;
-       if (curblock & 32  && !BlockFree(scr, x, y+1, z & 224)) return 0;
-       if (curblock & 64  && !BlockFree(scr, x+1, y, z & 112)) return 0;
-       if (curblock & 128 && !BlockFree(scr, x-1, y, z & 176)) return 0;
-       if ((z = GetBlock(scr, y-1, x)) & 32) return 1; //stuck to block below
+       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
        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)
 { //Drop down block (x,y) and those sticking to it mask <z>
-       if (GetBlock(scr, y, x) & z & 16)  BlockFall(scr, x, y-1, z & 208);
-       if (GetBlock(scr, y, x) & z & 32)  BlockFall(scr, x, y+1, z & 224);
-       if (GetBlock(scr, y, x) & z & 64)  BlockFall(scr, x+1, y, z & 112);
-       if (GetBlock(scr, y, x) & z & 128) BlockFall(scr, x-1, y, z & 174);
+       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);
 }
@@ -319,12 +324,12 @@ int CheckFall(int scr)
        unsigned char z;
 
        if (!Game.gravity) return 0;
-       for (y = Players[scr].boardHeight-1; y > 0; y--)
+       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 & 160) == 0)) {
+                       if ((z = GetBlock(scr, y, x)) > BT_none && (z & 0xA0) == 0) {
                        //doesn't stick left/up => topleft block
-                               if (BlockFree(scr, x, y, 240)) {
-                                       BlockFall(scr, x, y, 240);
+                               if (BlockFree(scr, x, y, 0xF0)) {
+                                       BlockFall(scr, x, y, 0xF0);
                                        fallen++;
                                } //move blocks down
                        } //block present
@@ -361,9 +366,9 @@ int ClearFullLines(int scr)
                while (LineIsFull(scr, from)) {
                        from++; //skip
                        for (x = 0; x<Players[scr].boardWidth; x++) {
-                               SetBlock(scr, from, x, GetBlock(scr, from, x)&239);
-                               if (from>1)
-                                       SetBlock(scr, from-2, x, GetBlock(scr, from-2, x)&223);
+                               SetBlock(scr, from, x, GetBlock(scr, from, x) & 0xEF);
+                               if (from > 1)
+                                       SetBlock(scr, from-2, x, GetBlock(scr, from-2, x) & 0xDF);
                        } //don't stick blocks to line which we'll remove
                } //full lines
                CopyLine(scr, from++, to++);
@@ -388,9 +393,9 @@ void InsertJunk(int scr, int color, int count, int column)
                CopyLine(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
-                               + 64 * (x != column-1 && x < Players[scr].boardWidth-1)
-                               + 128 * (x != column+1 && x > 0));
+                       SetBlock(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))
@@ -398,9 +403,5 @@ void InsertJunk(int scr, int color, int count, int column)
                else break;
        PlotShape(Players[scr].curShape, scr, Players[scr].curY, Players[scr].curX,
                scr == me);
-} //InoertJunk
+}
 
-/*
- * vi: ts=4 ai
- * vim: noai si
- */