/* _ |_) | IPES Authored by abakh To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see . */ #include "common.h" #define UP 1 #define RIGHT 2 #define DOWN 4 #define LEFT 8 #define CROSSOVER 15 #define FILLED 16 #define FLOWDELAY 5 #define DELAY 3 #define SAVE_TO_NUM 10 #define SY 0 #define SX 7 typedef unsigned char bitbox; /* The Plan9 compiler can not handle VLAs */ //#ifdef NO_VLA who uses that len wid arguments for this one? removed them #define wid 20 #define len 14 //#else //int len,wid; //#endif int py,px,fy,fx;//p: pointer f: fluid bitbox tocome[5]={0};//the row of pipes in the left side chtype green=A_BOLD;//will use bold font instead of green if colors are not available long score; void logo(void){ mvprintw(0,0," _ "); mvprintw(1,0,"|_)"); mvprintw(2,0,"| IPES"); } byte save_score(void){ return fallback_to_home("pipes_scores",score,SAVE_TO_NUM); } void show_scores(byte playerrank){ erase(); logo(); if(playerrank==FOPEN_FAIL){ mvaddstr(SY,SX,"Couldn't open scorefile"); mvprintw(SY+1,SX,"However, your score is %ld.",score); refresh(); return; } if(playerrank == 0){ char formername[60]={0}; long formerscore=0; rewind(score_file); fscanf(score_file,"%*s : %*d"); if ( fscanf(score_file,"%s : %ld",formername,&formerscore)==2 && formerscore>0){ byte a = (len-9)/2; attron(A_BOLD); mvprintw(SY,SX, "**** ***"); mvprintw(SY+len+1,SX,"***********************"); attroff(A_BOLD); attron(green); mvprintw(SY,SX+4,"CONGRATULATIONS!"); attroff(green); mvprintw(SY+a+1,SX," _____You beat the"); mvprintw(SY+a+2,SX," .' | previous"); mvprintw(SY+a+3,SX," .' | record"); mvprintw(SY+a+4,SX," | .| | of"); mvprintw(SY+a+5,SX," |.' | |%11ld",formerscore); mvprintw(SY+a+6,SX," | | held by"); mvprintw(SY+a+7,SX," ___| |___%7s!",formername); mvprintw(SY+a+8,SX," | |"); mvprintw(SY+a+9,SX," |____________|"); mvprintw(len+2,0,"Game over! Press a key to proceed:"); refresh(); getch(); erase(); logo(); } } attron(A_BOLD); mvprintw(3,0," HIGH"); mvprintw(4,0,"SCORES"); attroff(A_BOLD); //scorefile is still open with w+ char pname[60] = {0}; long pscore=0; byte rank=0; rewind(score_file); while( rank>>"); printw("%d",rank+1); attroff(green); printw(") %s : %ld",pname,pscore); ++rank; } fclose(score_file); refresh(); } //move in direction void MID(bitbox direction){ switch(direction){ case UP: --fy; break; case DOWN: ++fy; break; case LEFT: --fx; break; case RIGHT: ++fx; break; } } bitbox opposite(bitbox direction){ switch(direction){ case UP: return DOWN; case DOWN: return UP; case LEFT: return RIGHT; case RIGHT: return LEFT; } return 0; } void rectangle(void){ for(int y=0;y<=len;++y){ mvaddch(SY+y,SX,ACS_VLINE); mvaddch(SY+y,SX+wid+1,ACS_VLINE); } for(int x=0;x<=wid;++x){ mvaddch(SY,SX+x,ACS_HLINE); mvaddch(SY+len+1,SX+x,ACS_HLINE); } mvaddch(SY,SX,ACS_ULCORNER); mvaddch(SY+len+1,SX,ACS_LLCORNER); mvaddch(SY,SX+wid+1,ACS_URCORNER); mvaddch(SY+len+1,SX+wid+1,ACS_LRCORNER); } //this generates the pipes... bitbox pipegen(void){ if(rand()%17){//17 so all forms have the same chance byte a=rand()%4; byte b; do{ b=rand()%4; }while(b==a); return (1 << a) | ( 1 << b); } else return CROSSOVER;//could not be generated like that } //.. and this is only for display void addpipe(int y,int x,bitbox pipe , bool highlight){ bitbox p= pipe & ~FILLED; chtype foo ; switch(p){ case UP|RIGHT : foo= ACS_LLCORNER; break; case UP|DOWN : foo=ACS_VLINE; break; case UP|LEFT : foo=ACS_LRCORNER; break; case DOWN|RIGHT : foo =ACS_ULCORNER; break; case DOWN|LEFT : foo=ACS_URCORNER; break; case LEFT|RIGHT: foo=ACS_HLINE; break; case RIGHT: foo = '>'; break; case LEFT: foo = '<'; break; case UP: foo = '^'; break; case DOWN: foo = 'v'; break; case CROSSOVER: //all foo = ACS_PLUS; break; default: foo = ' '; break; } if( pipe & FILLED ) foo |= green; mvaddch(y,x, foo|(highlight*A_REVERSE) ); } //display void draw(bitbox board[len][wid]){ int y,x; for(y=0;y= now-tstart){ mvprintw(4,0,"Time:%ld",giventime-(now-tstart)); mvprintw(5,0,"Score:"); mvprintw(6,0,"%ld",score); } else{ mvprintw(4,0,"Score:"); mvprintw(5,0,"%ld",score); } for(foo=0;foo<5;++foo) addpipe(11-foo,4,tocome[foo],0); draw(board); refresh(); if(now-tstart == giventime){ flow=1; } if(flow && (fast || ( !(now%FLOWDELAY)&& now!=lasttime ) )){ lasttime = now; MID(direction); if(fy=0&& fx>=0 && ( board[fy][fx]&opposite(direction) ) ){ if(board[fy][fx] != CROSSOVER && board[fy][fx] != (CROSSOVER|FILLED) ) direction = board[fy][fx] & ~opposite(direction); ++score; if(fast) ++score; } else goto End; board[fy][fx]|=FILLED; } input = getch(); if( input == KEY_F(1) || input=='?' ){ help(); if(!flow) tstart += time(NULL)-now; } if( (input==KEY_F(2)||input=='!') ){ gameplay(); if(!flow) tstart += time(NULL)-now; } if( input == KEY_MOUSE ) mouseinput(); if( (input=='k' || (input==KEY_UP||input=='w')) && py>0 ) --py; if( (input=='j' || (input==KEY_DOWN||input=='s')) && py0 ) --px; if( (input=='l' || (input==KEY_RIGHT||input=='d')) && px