/* io.c - handling keyboard input and screen output Copyright 2008 Ido Yehieli This file is part of CryptRover. CryptRover is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. CryptRover is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with CryptRover. If not, see . */ #include #include #include "map.h" #include "utils.h" #include "entities.h" #include "items.h" #include "io.h" #include "mdport.h" WINDOW *message_win; int message_win_height; int messages; int init_curses(void) { int error_lines=0; stdscr=initscr(); if (ERR==keypad(stdscr,true)) mvaddstr(error_lines++,X_+1,"Cannot enable keypad"); if (ERR==noecho()) mvaddstr(error_lines++,X_+1,"Cannot set noecho mode"); if (ERR==start_color() || !has_colors()) mvaddstr(error_lines++,X_+1,"Cannot enable colors"); else { for (int c=0; c<=8; c++) init_pair(c,c,0); } if (ERR==curs_set(0)) mvaddstr(error_lines++,X_+1,"Cannot set invisible cursor"); if (error_lines) { attron(A_STANDOUT); mvaddstr(error_lines++,X_+1,"Please try a different terminal"); attroff(A_STANDOUT); } return error_lines; } int end_curses(void) { delwin(message_win); delwin(stdscr); return endwin(); } void show_help(void) { WINDOW *help_win=newwin(Y_*3/4,X_*3/4,Y_/8,X_/8); PANEL *help_panel=new_panel(help_win); box(help_win,0,0); int lines=1; mvwaddstr(help_win,lines++,1,"To move or attack use wasd,"); mvwaddstr(help_win,lines++,1,"vi keys or the numpad:"); mvwaddstr(help_win,lines++,1," q w e y k u 7 8 9"); mvwaddstr(help_win,lines++,1," \\|/ \\|/ \\|/ "); mvwaddstr(help_win,lines++,1," a-s-d h-.-l 4-5-6"); mvwaddstr(help_win,lines++,1," /|\\ /|\\ /|\\ "); mvwaddstr(help_win,lines++,1," z x c b j n 1 2 3"); lines++; mvwaddstr(help_win,lines++,1,"To go up a staircase to the next "); mvwaddstr(help_win,lines++,1,"level press '<' or ','."); lines++; mvwaddstr(help_win,lines++,1,"To flip the flashlight on and off"); mvwaddstr(help_win,lines++,1,"press 'f'."); lines++; mvwaddstr(help_win,lines++,1,"To quit the game press ESC or"); mvwaddstr(help_win,lines++,1,"Ctrl+c."); lines++; mvwaddstr(help_win,lines++,1,"To come back to this help screen "); mvwaddstr(help_win,lines++,1,"press '?'."); wrefresh(help_win); show_panel(help_panel); update_panels(); readchar(); hide_panel(help_panel); delwin(help_win); del_panel(help_panel); refresh(); } int compare_scores(const void* s1, const void* s2) { char score_s1[80];//(char*)s1; char score_s2[80];//(char*)s2; strcpy(score_s1 ,(char*)s1+strlen("Gold: ")*sizeof(char)); strcpy(score_s2 ,(char*)s2+strlen("Gold: ")*sizeof(char)); int score1=atoi(score_s1); int score2=atoi(score_s2); if (score1>score2) return -1; else if (score1==score2) return 0; else return 1; } void show_highscore(void){ WINDOW *highscore_win=newwin(LINES*3/4,COLS*3/4,LINES/8-1,COLS/8); PANEL *highscore_panel=new_panel(highscore_win); show_panel(highscore_panel); int lines=0; int llength=80; char line[llength]; FILE *score_f = fopen("scores.dat", "r"); while(fgets(line, llength, score_f)!=NULL) lines++; fclose(score_f); char scores[lines][llength]; score_f = fopen("scores.dat", "r"); int l=0; while(fgets(line, llength, score_f)!=NULL) strcpy(scores[l++],line); fclose(score_f); qsort(scores,lines,sizeof(char)*llength,compare_scores); for(int l=0;l=message_win_height-1) { mvwaddstr(message_win,messages,0,msg); scroll(message_win); } else mvwaddstr(message_win,messages++,0,msg); wrefresh(message_win); wattroff(message_win,attr); } int readchar(void) { return md_readchar(stdscr); } int print_info(int errs,int level) { int msgs=errs; char msg[COLS-X_-1]; sprintf(msg, "Hit points: %d%% ",100*ent_l[0].hp/PLAYER_HP); mvaddstr(msgs++,X_+1,msg); mvaddstr(msgs,X_+1,"[----------]"); move(msgs++,X_+2); for(int i=0;i<10*ent_l[0].hp/PLAYER_HP;i++) addch('*'|C_MED); sprintf(msg, "Air: %d%% ",100*ent_l[0].air/PLAYER_AIR); mvaddstr(msgs++,X_+1,msg); mvaddstr(msgs,X_+1,"[----------]"); move(msgs++,X_+2); for(int i=0;i<10*ent_l[0].air/PLAYER_AIR;i++) addch('*'|C_AIR); sprintf(msg, "Battery: %d%% ",100*ent_l[0].battery/PLAYER_BATTERY); mvaddstr(msgs++,X_+1,msg); mvaddstr(msgs,X_+1,"[----------]"); move(msgs++,X_+2); for(int i=0;i<10*ent_l[0].battery/PLAYER_BATTERY;i++) addch('*'|C_BAT); if (1==ent_l[0].coins) sprintf(msg, "Gold: %d coin ",ent_l[0].coins); else sprintf(msg, "Gold: %d coins ",ent_l[0].coins); mvaddstr(msgs++,X_+1,msg); sprintf(msg, "Dungeon level: %d/%d ",level,LAST_LEVEL); mvaddstr(msgs++,X_+1,msg); mvaddstr(msgs,X_+1,"[------------]"); move(msgs++,X_+2); for(int i=0;i0 && ent_l[e].air>0 && view_m[ent_l[e].y][ent_l[e].x]==IN_SIGHT) mvaddch(ent_l[e].y,ent_l[e].x,ent_l[e].type|ent_l[e].color); } }