#include #include #include #include #include #include #include #include #include #include #include #include #define DICT_DIR "/usr/share/dict" #define N_STARS 40 int win_w = 80; int win_h = 24; int laser_x = 5; int laser_max_x = 75; int speed_th = 5; int laser_move_th = 4; double start_delay = 0.8; double delay_mult = 0.79; void version(void) { printf("banihstypos v" VERSION ", (C) 2007 by folkert@vanheusden.com\n\n"); } double get_ts(void) { struct timeval ts; struct timezone tz; if (gettimeofday(&ts, &tz) == -1) { endwin(); fprintf(stderr, "gettimeofday() failed with error %s\n", strerror(errno)); exit(1); } return (((double)ts.tv_sec) + ((double)ts.tv_usec)/1000000.0); } char * get_word(char **words, int n_words) { return strdup(words[lrand48() % n_words]); } void draw_screen(int *stars, int word_x, char *word, int word_cnt, double cur_delay, int points, int n_words) { int loop; static int laser_state = 0; static int stars_state = 0; char *buffer = strdup(word); laser_state = !laser_state; stars_state = !stars_state; if (strlen(buffer) > (win_w - word_x)) { buffer[win_w - word_x] = 0x00; } werase(stdscr); /* show stars */ wattron(stdscr, COLOR_PAIR(1)); for(loop=0; loop 0 && FD_ISSET(0, &fds)) { int c = getch(); if (c == 3) /* exit program */ { endwin(); version(); exit(0); } if (c == word[0]) { points += pointsadd; memcpy(&word[0], &word[1], strlen(word)); } else { points -= (pointsadd - 1); beep(); } } if (strlen(word) == 0) { free(word); word_x = win_w - 1; word = get_word(words, n_words); if (!nok) word_cnt++; nok = 0; if ((word_cnt % speed_th) == 0) { delay *= delay_mult; pointsadd++; } else if ((word_cnt % laser_move_th) == 0) { if (laser_x < laser_max_x) laser_x++; } } } } char * find_words_file(void) { static char filename_buffer[4096]; struct dirent *de; DIR *dir = opendir(DICT_DIR); if (!dir) { if (errno == ENOENT) return NULL; fprintf(stderr, "Default directory which is searched for words lists (" DICT_DIR ") cannot be opened: %s\n", strerror(errno)); exit(1); } while((de = readdir(dir)) != NULL) { if (strcmp(de -> d_name, ".") != 0 && strcmp(de -> d_name, "..") != 0) { snprintf(filename_buffer, sizeof(filename_buffer), DICT_DIR "/%s", de -> d_name); return filename_buffer; } } return NULL; } void load_words_file(char *file, char lowercase_only, char ***words, int *n_words) { FILE *fh = fopen(file, "r"); if (!fh) { fprintf(stderr, "Cannot open words file %s for read access: %s\n", file, strerror(errno)); exit(1); } while(!feof(fh)) { char read_buffer[4096], *start = read_buffer, *space, *lf; fgets(read_buffer, sizeof(read_buffer), fh); read_buffer[win_w] = 0x00; *words = realloc(*words, sizeof(char *) * (*n_words + 1)); if (!*words) { fprintf(stderr, "Out of memory.\n"); exit(1); } while(*start == ' ') start++; space = strchr(start, ' '); if (space) *space = 0x00; lf = strchr(start, '\n'); if (lf) *lf = 0x00; if (lowercase_only) { int loop, len = strlen(start); for(loop=0; loop