/************************************************ pentix v2.4 by Andrey Tchoritch ************************************************/ #include "defs.h" int timings[] = {450000, 400000, 350000, 300000, 250000, 200000, 150000, 100000}; int main(void) { initialise(); setjmp(rest); init_all(); main_loop(); exit(0); } void initialise(void) { struct passwd *who; init_curses(); srandom(time(0)); print_authors(); print_help(); define_shapes(); who = (struct passwd *)getpwuid(getuid()); name = who->pw_name; read_quotations(); read_high_scores(); } void init_all(void) { int i, j; score_position = -1; lines = score = shape_no = rot = xpos = ypos = next = 0; /* grid setup */ for (i = 0; i < UWIDTH; i++) for (j = 0; j < UHEIGHT; j++) grid[i][j] = 0; create_shape(); /* Set up 1st shape */ create_shape(); /* Set up next shape */ clear(); /* bucket setup */ attrset(COLOR_PAIR(WHITE)); for (i=0; i < UHEIGHT; i++) { scmove(NORM_OFFSET-1, i); addstr("||"); scmove(NORM_OFFSET+UWIDTH, i); addstr("||"); } scmove(NORM_OFFSET-1,UHEIGHT); addstr("+-"); scmove(NORM_OFFSET+UWIDTH, UHEIGHT); addstr("-+"); for (j=NORM_OFFSET; j < NORM_OFFSET+UWIDTH; j++) { scmove(j, UHEIGHT); addstr("--"); } scmove(NORM_OFFSET+3, 23); attrset(COLOR_PAIR(GREEN)|A_BOLD); addstr("Play PENTIX!!!"); show_score(1); show_next(1); } void init_curses(void) { initscr(); start_color(); init_pair(7,COLOR_WHITE,COLOR_BLACK); init_pair(1,COLOR_BLUE,COLOR_BLACK); init_pair(2,COLOR_GREEN,COLOR_BLACK); init_pair(3,COLOR_CYAN,COLOR_BLACK); init_pair(4,COLOR_RED,COLOR_BLACK); init_pair(5,COLOR_MAGENTA,COLOR_BLACK); init_pair(6,COLOR_YELLOW,COLOR_BLACK); init_pair(8,COLOR_BLACK,COLOR_BLACK); leaveok(stdscr,FALSE); nodelay(stdscr, TRUE); raw(); noecho(); keypad(stdscr,TRUE); } void exit_curses(void) { clear(); refresh(); nocbreak(); echo(); endwin(); } void main_loop(void) { int drop_delay = timings[level]; struct timeval tval_prev_drop, tval_prev_leap, tval_next; struct timezone tzone; gettimeofday(&tval_prev_drop, &tzone); tval_prev_leap = tval_prev_drop; for ( ; ; ) { gettimeofday(&tval_next, &tzone); /* drop delay */ if (usec_delta(tval_next.tv_usec, tval_prev_drop.tv_usec) > drop_delay) { drop_block(); tval_prev_drop = tval_next; } /* leap level delay */ if ((tval_next.tv_sec - tval_prev_leap.tv_sec) > LEAP_LEVEL_DELAY) { drop_delay = timings[level = ++level & 0x7]; show_score(1); tval_prev_leap = tval_next; } switch ( getch() ) { case KEY_LEFT: left_proc(); break; case KEY_DOWN: rot_proc(); break; case KEY_RIGHT: right_proc(); break; case ' ' : fast_proc(); break; case '1' : /* switch "show next" hint */ next = next ? 0 : 1; show_next(1); break; case '2' : end_proc(); break; case '6' : /* switch level */ drop_delay = timings[level = ++level & 0x7]; show_score(1); tval_prev_leap = tval_next; break; default: break; } move(24, 79); } } void drop_block(void) { if (block_can_drop(shape_no, xpos, ypos, rot)) { print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, BLACK); ypos++; print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, shape[shape_no].color); } else { if (ypos < 0) end_game(); else { score += ((shape[shape_no].pointv[rot] + (UHEIGHT - ypos)) * (level - next + 1)); store_shape(shape_no, xpos, ypos, rot); remove_full_lines(ypos); if (score > high_scores[0].score) { scmove(0, 23); attrset(COLOR_PAIR(YELLOW)|A_BOLD); addstr("You're in PENTIX top 16 !"); }; create_shape(); show_score(1); show_next(1); } } } int waituser(void) { int c; refresh(); nodelay(stdscr, FALSE); c = getch(); nodelay(stdscr, TRUE); return c; } void end_game(void) { int c; char str[32]; attrset(COLOR_PAIR(YELLOW)|A_BOLD); sprintf((char *) &str, "SCORE: %d", score); scmove(NORM_OFFSET+3, UHEIGHT/2); addstr((char *) &str); update_highscore_table(); if (score_position < 0) print_quotation(); beep(); waituser(); print_high_scores(); scmove(17, 23); attrset(COLOR_PAIR(WHITE)|A_BOLD); addstr("New game ?"); c = waituser(); if ( c == 'n' || c == 'N' || c == 'q' || c == 'Q' ) end_proc(); setup_level(); longjmp(rest,1); } void end_proc(void) { clear(); print_quotation(); refresh(); endwin(); exit(0); } void left_proc(void) { if (block_can_left(shape_no, xpos, ypos, rot)) { print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, BLACK); xpos--; print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, shape[shape_no].color); } } void right_proc(void) { if (block_can_right(shape_no, xpos, ypos, rot)) { print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, BLACK); xpos++; print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, shape[shape_no].color); } } void rot_proc(void) { register int newrot = rot; newrot = ++newrot & 0x3; if (check_rot(shape_no, xpos, ypos, newrot)) { print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, BLACK); rot = newrot; print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, shape[shape_no].color); } } void fast_proc(void) { while (block_can_drop(shape_no, xpos, ypos, rot)) { print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, BLACK); ypos++; print_shape(shape_no, xpos + NORM_OFFSET, ypos, rot, shape[shape_no].color); } } void show_next(int on) { int x, y; attrset(COLOR_PAIR(BLACK)); for ( y=10; y < 18; y++ ) { for ( x=29; x < 35; x++ ) { scmove(x, y); addstr(" "); } } if ( on && next) { scmove(31, 10); attrset(COLOR_PAIR(WHITE)); addstr("Next:"); print_shape(next_no, 29, 12, next_rot, shape[next_no].color); } } void print_shape(int shape_no, int x, int y, int rot, int col) { int x1, x2, x3, x4, x5, y1; int t0, t1, t2, t3, t4; t0 = shape[shape_no].table[0][rot]; /* Bit map of 1st Row */ t1 = shape[shape_no].table[1][rot]; /* Bit map of 2nd Row */ t2 = shape[shape_no].table[2][rot]; /* Bit map of 3rd Row */ t3 = shape[shape_no].table[3][rot]; /* Bit map of 4th Row */ t4 = shape[shape_no].table[4][rot]; /* Bit map of 5th Row */ x1 = x; /* Position of 1st column of block grid */ x2 = x1 + 1; /* Position of 2nd column of block grid */ x3 = x2 + 1; /* Position of 3rd column of block grid */ x4 = x3 + 1; /* Position of 4th column of block grid */ x5 = x4 + 1; /* Position of 5th column of block grid */ y1 = y; /* Position of 1st row of block grid */ if (y > -1) { if (t0 & 16) addbl(x1, y1, col); if (t0 & 8) addbl(x2, y1, col); if (t0 & 4) addbl(x3, y1, col); if (t0 & 2) addbl(x4, y1, col); if (t0 & 1) addbl(x5, y1, col); } y1 += 1; /* Position of next row */ if (y > -2) { if (t1 & 16) addbl(x1, y1, col); if (t1 & 8) addbl(x2, y1, col); if (t1 & 4) addbl(x3, y1, col); if (t1 & 2) addbl(x4, y1, col); if (t1 & 1) addbl(x5, y1, col); } y1 += 1; /* Position of next row */ if (y > -3) { if (t2 & 16) addbl(x1, y1, col); if (t2 & 8) addbl(x2, y1, col); if (t2 & 4) addbl(x3, y1, col); if (t2 & 2) addbl(x4, y1, col); if (t2 & 1) addbl(x5, y1, col); } y1 += 1; /* Position of next row */ if (y > -4) { if (t3 & 16) addbl(x1, y1, col); if (t3 & 8) addbl(x2, y1, col); if (t3 & 4) addbl(x3, y1, col); if (t3 & 2) addbl(x4, y1, col); if (t3 & 1) addbl(x5, y1, col); } y1 += 1; /* Position of next row */ if (y > -5) { if (t4 & 16) addbl(x1, y1, col); if (t4 & 8) addbl(x2, y1, col); if (t4 & 4) addbl(x3, y1, col); if (t4 & 2) addbl(x4, y1, col); if (t4 & 1) addbl(x5, y1, col); } } void addbl(int x, int y, int col) { scmove(x, y); attrset(COLOR_PAIR(col)|A_REVERSE); addstr(" "); } void scinsert(int y) { scmove(0,y); insertln(); attrset(COLOR_PAIR(WHITE)); scmove(NORM_OFFSET-1, 0); addstr("||"); scmove(NORM_OFFSET+UWIDTH, 0); addstr("||"); } void scdelete(int y) { show_score(0); show_next(0); scmove(0, y); deleteln(); } void scmove(int x, int y) { move(y*VUNIT, x*HUNIT); }