#include #include "pieces.h" #include "play.h" typedef struct { int startx[4]; int starty[4]; int rotx[4][4]; int roty[4][4]; } PIECE; PIECE pieces[7]; static int curpiece; //have this file like object static int curx[4]; static int cury[4]; static int currot; static int nextx[4]; static int nexty[4]; static int nextrot; extern int playarea[COLLEN][ROWLEN]; void initPieces(void) { PIECE box; PIECE str; PIECE tee; PIECE ll; PIECE lr; PIECE sl; PIECE sr; box.startx[0] = 4; box.starty[0] = 16; box.startx[1] = 5; box.starty[1] = 16; box.startx[2] = 5; box.starty[2] = 15; box.startx[3] = 4; box.starty[3] = 15; box.rotx[0][0] = 1; box.roty[0][0] = 0; box.rotx[0][1] = 0; box.roty[0][1] = -1; box.rotx[0][2] = -1; box.roty[0][2] = 0; box.rotx[0][3] = 0; box.roty[0][3] = 1; box.rotx[1][0] = 0; box.roty[1][0] = -1; box.rotx[1][1] = -1; box.roty[1][1] = 0; box.rotx[1][2] = 0; box.roty[1][2] = 1; box.rotx[1][3] = 1; box.roty[1][3] = 0; box.rotx[2][0] = -1; box.roty[2][0] = 0; box.rotx[2][1] = 0; box.roty[2][1] = 1; box.rotx[2][2] = 1; box.roty[2][2] = 0; box.rotx[2][3] = 0; box.roty[2][3] = -1; box.rotx[3][0] = 0; box.roty[3][0] = 1; box.rotx[3][1] = 1; box.roty[3][1] = 0; box.rotx[3][2] = 0; box.roty[3][2] = -1; box.rotx[3][3] = -1; box.roty[3][3] = 0; str.startx[0] = 3; str.starty[0] = 16; str.startx[1] = 4; str.starty[1] = 16; str.startx[2] = 5; str.starty[2] = 16; str.startx[3] = 6; str.starty[3] = 16; str.rotx[0][0] = 1; str.roty[0][0] = 2; str.rotx[0][1] = 0; str.roty[0][1] = 1; str.rotx[0][2] = -1; str.roty[0][2] = 0; str.rotx[0][3] = -2; str.roty[0][3] = -1; str.rotx[1][0] = 2; str.roty[1][0] = -2; str.rotx[1][1] = 1; str.roty[1][1] = -1; str.rotx[1][2] = 0; str.roty[1][2] = 0; str.rotx[1][3] = -1; str.roty[1][3] = 1; str.rotx[2][0] = -2; str.roty[2][0] = -1; str.rotx[2][1] = -1; str.roty[2][1] = 0; str.rotx[2][2] = 0; str.roty[2][2] = 1; str.rotx[2][3] = 1; str.roty[2][3] = 2; str.rotx[3][0] = -1; str.roty[3][0] = 1; str.rotx[3][1] = 0; str.roty[3][1] = 0; str.rotx[3][2] = 1; str.roty[3][2] = -1; str.rotx[3][3] = 2; str.roty[3][3] = -2; tee.startx[0] = 3; tee.starty[0] = 16; tee.startx[1] = 4; tee.starty[1] = 16; tee.startx[2] = 5; tee.starty[2] = 16; tee.startx[3] = 4; tee.starty[3] = 15; tee.rotx[0][0] = 1; tee.roty[0][0] = 1; tee.rotx[0][1] = 0; tee.roty[0][1] = 0; tee.rotx[0][2] = -1; tee.roty[0][2] = -1; tee.rotx[0][3] = -1; tee.roty[0][3] = 1; tee.rotx[1][0] = 1; tee.roty[1][0] = -1; tee.rotx[1][1] = 0; tee.roty[1][1] = 0; tee.rotx[1][2] = -1; tee.roty[1][2] = 1; tee.rotx[1][3] = 1; tee.roty[1][3] = 1; tee.rotx[2][0] = -1; tee.roty[2][0] = -1; tee.rotx[2][1] = 0; tee.roty[2][1] = 0; tee.rotx[2][2] = 1; tee.roty[2][2] = 1; tee.rotx[2][3] = 1; tee.roty[2][3] = -1; tee.rotx[3][0] = -1; tee.roty[3][0] = 1; tee.rotx[3][1] = 0; tee.roty[3][1] = 0; tee.rotx[3][2] = 1; tee.roty[3][2] = -1; tee.rotx[3][3] = -1; tee.roty[3][3] = -1; ll.startx[0] = 3; ll.starty[0] = 16; ll.startx[1] = 4; ll.starty[1] = 16; ll.startx[2] = 5; ll.starty[2] = 16; ll.startx[3] = 5; ll.starty[3] = 15; ll.rotx[0][0] = 1; ll.roty[0][0] = 1; ll.rotx[0][1] = 0; ll.roty[0][1] = 0; ll.rotx[0][2] = -1; ll.roty[0][2] = -1; ll.rotx[0][3] = -2; ll.roty[0][3] = 0; ll.rotx[1][0] = 1; ll.roty[1][0] = -1; ll.rotx[1][1] = 0; ll.roty[1][1] = 0; ll.rotx[1][2] = -1; ll.roty[1][2] = 1; ll.rotx[1][3] = 0; ll.roty[1][3] = 2; ll.rotx[2][0] = -1; ll.roty[2][0] = -1; ll.rotx[2][1] = 0; ll.roty[2][1] = 0; ll.rotx[2][2] = 1; ll.roty[2][2] = 1; ll.rotx[2][3] = 2; ll.roty[2][3] = 0; ll.rotx[3][0] = -1; ll.roty[3][0] = 1; ll.rotx[3][1] = 0; ll.roty[3][1] = 0; ll.rotx[3][2] = 1; ll.roty[3][2] = -1; ll.rotx[3][3] = 0; ll.roty[3][3] = -2; lr.startx[0] = 5; lr.starty[0] = 16; lr.startx[1] = 4; lr.starty[1] = 16; lr.startx[2] = 3; lr.starty[2] = 16; lr.startx[3] = 3; lr.starty[3] = 15; lr.rotx[0][0] = -1; lr.roty[0][0] = -1; lr.rotx[0][1] = 0; lr.roty[0][1] = 0; lr.rotx[0][2] = 1; lr.roty[0][2] = 1; lr.rotx[0][3] = 0; lr.roty[0][3] = 2; lr.rotx[1][0] = -1; lr.roty[1][0] = 1; lr.rotx[1][1] = 0; lr.roty[1][1] = 0; lr.rotx[1][2] = 1; lr.roty[1][2] = -1; lr.rotx[1][3] = 2; lr.roty[1][3] = 0; lr.rotx[2][0] = 1; lr.roty[2][0] = 1; lr.rotx[2][1] = 0; lr.roty[2][1] = 0; lr.rotx[2][2] = -1; lr.roty[2][2] = -1; lr.rotx[2][3] = 0; lr.roty[2][3] = -2; lr.rotx[3][0] = 1; lr.roty[3][0] = -1; lr.rotx[3][1] = 0; lr.roty[3][1] = 0; lr.rotx[3][2] = -1; lr.roty[3][2] = 1; lr.rotx[3][3] = -2; lr.roty[3][3] = 0; sl.startx[0] = 3; sl.starty[0] = 16; sl.startx[1] = 4; sl.starty[1] = 16; sl.startx[2] = 4; sl.starty[2] = 15; sl.startx[3] = 5; sl.starty[3] = 15; sl.rotx[0][0] = 1; sl.roty[0][0] = 1; sl.rotx[0][1] = 0; sl.roty[0][1] = 0; sl.rotx[0][2] = -1; sl.roty[0][2] = 1; sl.rotx[0][3] = -2; sl.roty[0][3] = 0; sl.rotx[1][0] = 1; sl.roty[1][0] = -2; sl.rotx[1][1] = 0; sl.roty[1][1] = -1; sl.rotx[1][2] = 1; sl.roty[1][2] = 0; sl.rotx[1][3] = 0; sl.roty[1][3] = 1; sl.rotx[2][0] = -2; sl.roty[2][0] = 0; sl.rotx[2][1] = -1; sl.roty[2][1] = 1; sl.rotx[2][2] = 0; sl.roty[2][2] = 0; sl.rotx[2][3] = 1; sl.roty[2][3] = 1; sl.rotx[3][0] = 0; sl.roty[3][0] = 1; sl.rotx[3][1] = 1; sl.roty[3][1] = 0; sl.rotx[3][2] = 0; sl.roty[3][2] = -1; sl.rotx[3][3] = 1; sl.roty[3][3] = -2; sr.startx[0] = 3; sr.starty[0] = 15; sr.startx[1] = 4; sr.starty[1] = 15; sr.startx[2] = 4; sr.starty[2] = 16; sr.startx[3] = 5; sr.starty[3] = 16; sr.rotx[0][0] = 0; sr.roty[0][0] = 2; sr.rotx[0][1] = -1; sr.roty[0][1] = 1; sr.rotx[0][2] = 0; sr.roty[0][2] = 0; sr.rotx[0][3] = -1; sr.roty[0][3] = -1; sr.rotx[1][0] = 2; sr.roty[1][0] = -1; sr.rotx[1][1] = 1; sr.roty[1][1] = 0; sr.rotx[1][2] = 0; sr.roty[1][2] = -1; sr.rotx[1][3] = -1; sr.roty[1][3] = 0; sr.rotx[2][0] = -1; sr.roty[2][0] = -1; sr.rotx[2][1] = 0; sr.roty[2][1] = 0; sr.rotx[2][2] = -1; sr.roty[2][2] = 1; sr.rotx[2][3] = 0; sr.roty[2][3] = 2; sr.rotx[3][0] = -1; sr.roty[3][0] = 0; sr.rotx[3][1] = 0; sr.roty[3][1] = -1; sr.rotx[3][2] = 1; sr.roty[3][2] = 0; sr.rotx[3][3] = 2; sr.roty[3][3] = -1; pieces[0] = box; pieces[1] = str; pieces[2] = tee; pieces[3] = ll; pieces[4] = lr; pieces[5] = sl; pieces[6] = sr; } int newPiece(int num) { int i; curpiece = num; currot = 0; nextrot = 0; for (i=0;i<4;i++) { curx[i] = pieces[num].startx[i]; cury[i] = pieces[num].starty[i]; nextx[i] = curx[i]; nexty[i] = cury[i]; if (playarea[cury[i]][curx[i]] == 0) playarea[cury[i]][curx[i]] = i+1; else return 1; } return 0; } void tryShift(int horz) { int i; for (i=0;i<4;i++) { nextx[i] = curx[i]+horz; nexty[i] = cury[i]; } nextrot = currot; } void tryDown(void) { int i; for (i=0;i<4;i++) { nextx[i] = curx[i]; nexty[i] = cury[i]-1; } nextrot = currot; } void tryRotClock(void) { int i; for (i=0;i<4;i++) { nextx[i] = curx[i]+pieces[curpiece].rotx[currot][i]; nexty[i] = cury[i]+pieces[curpiece].roty[currot][i]; } nextrot = currot+1; if (nextrot>3) nextrot -= 4; } void tryRotAnti(void) { int i; nextrot = currot-1; if (nextrot < 0) nextrot += 4; for (i=0;i<4;i++) { nextx[i] = curx[i]-pieces[curpiece].rotx[nextrot][i]; nexty[i] = cury[i]-pieces[curpiece].roty[nextrot][i]; } } int moveOK(void) { int i; for (i=0;i<4;i++) { if (nextx[i] < 0) return 0; if (nextx[i] > ROWLEN-1) return 0; if (nexty[i] < 0) return 0; if (nexty[i] > COLLEN-1) return 0; if (playarea[nexty[i]][nextx[i]] > 4) return 0; } return 1; } void makeMove(void) { int i; for (i=0;i<4;i++) { playarea[cury[i]][curx[i]] = 0; curx[i] = nextx[i]; cury[i] = nexty[i]; } for (i=0;i<4;i++) playarea[cury[i]][curx[i]] = i+1; currot = nextrot; } void fixPiece(void) { int i; for (i=0;i<4;i++) playarea[cury[i]][curx[i]] = curpiece+5; } void boxColour(GsBOXF *box,int val) { if ((val > 0)&&(val < 5)) val = curpiece + 5; switch (val) { case 5: box->r = 255; box->g = 0; box->b = 0; break; case 6: box->r = 0; box->g = 255; box->b = 0; break; case 7: box->r = 0; box->g = 0; box->b = 255; break; case 8: box->r = 255; box->g = 255; box->b = 0; break; case 9: box->r = 255; box->g = 0; box->b = 255; break; case 10: box->r = 0; box->g = 255; box->b = 255; break; case 11: box->r = 255; box->g = 120; box->b = 0; break; default: box->r = 0; box->g = 0; box->b = 0; break; } }