]> git.draconx.ca Git - rrace.git/blobdiff - src/curses.c
curses: Make the game winnable.
[rrace.git] / src / curses.c
index d5b1cc4be149681d9a383183adb3493c97f4bd79..06ec3519c87e530bcaa4837c7ff7276c882bab47 100644 (file)
@@ -49,7 +49,19 @@ static struct app_state {
        struct board board;
 
        WINDOW *gamewin[WINDOW_MAX], *goalwin[WINDOW_MAX];
-       int cursor;
+       WINDOW *timer;
+
+       /* Most recently displayed timer value, for screen redraw. */
+       uint_least32_t timer_ms;
+
+       /* Location of the keyboard cursor */
+       int_least8_t cursor;
+
+       /* If true, the goal will be displayed over the main play area. */
+       uint_least8_t view_goal_on_game;
+
+       /* Clicked toolbar item */
+       uint_least8_t toolbar_click;
 } state;
 
 static void print_version(void)
@@ -170,12 +182,20 @@ static void redraw_area_border(WINDOW **win, unsigned x, unsigned sz)
 
 static void curs_redraw_game(struct app_state *state, uint_fast32_t mask)
 {
-       uint_least32_t *gp = state->board.game;
+       uint_least32_t buf[3], *gp = state->board.game;
        int i;
 
        if (mask == -1)
                redraw_area_border(state->gamewin, 2, 5);
 
+       if (state->view_goal_on_game) {
+               for (i = 0; i < 3; i++) {
+                       buf[i] = state->board.goal[i];
+                       buf[i] = (gp[i] & ~GOAL_MASK) | (buf[i] << GOAL_SHIFT);
+               }
+               gp = buf;
+       }
+
        for (i = 0; i < 25; i++) {
                if (mask & 1) {
                        redraw_tile(state->gamewin, i%5, i/5,
@@ -288,6 +308,10 @@ static void setup_mainwin(struct app_state *state)
        w = MIN(scr_w-split, 3+6*goalsz);
        h = MIN(scr_h-GAME_YPOS, 2+3*goalsz);
        realloc_area(&state->goalwin[WINDOW_AREA], h, w, GAME_YPOS, split);
+
+       /* Status area */
+       w = MAX(0, scr_w-split-1);
+       realloc_area(&state->timer, 1, w, GAME_YPOS+h, split+1);
 }
 
 static void app_initialize(int argc, char **argv)
@@ -319,7 +343,7 @@ static void app_initialize(int argc, char **argv)
        }
 
        game_reset(&state.board);
-       state.cursor = 5*state.board.y + state.board.x;
+       state.cursor = -1;
 
        initscr();
        start_color();
@@ -328,14 +352,36 @@ static void app_initialize(int argc, char **argv)
        cbreak();
        keypad(stdscr, TRUE);
        if (enable_mouse) {
+               /*
+                * While we only care about a subset of these events, for
+                * some reason with ncurses failing to enable all of them
+                * causes timeout to stop working when disabled buttons are
+                * pressed (or released).
+                *
+                * It is not known if other curses have this problem; at least
+                * pdcurses does not, but the extra events should be harmless
+                * in any case.
+                */
+#if HAVE_CURSES_MOUSE_SUPPORT
+               unsigned long mask = BUTTON1_PRESSED | BUTTON1_RELEASED
+                                   | BUTTON2_PRESSED | BUTTON2_RELEASED
+                                   | BUTTON3_PRESSED | BUTTON3_RELEASED
+#ifdef BUTTON4_PRESSED
+                                   | BUTTON4_PRESSED | BUTTON4_RELEASED
+#endif
+#ifdef BUTTON5_PRESSED
+                                   | BUTTON5_PRESSED | BUTTON5_RELEASED
+#endif
+                                   ;
 #if HAVE_CURSES_MOUSE_SET
-               mouse_set(BUTTON1_PRESSED);
+               mouse_set(mask);
 #elif HAVE_CURSES_MOUSEMASK
-               mousemask(BUTTON1_PRESSED, NULL);
+               mousemask(mask, NULL);
 #endif
 #if HAVE_CURSES_MOUSEINTERVAL
                mouseinterval(0);
 #endif
+#endif /* HAVE_CURSES_MOUSE_SUPPORT */
        }
 
        noecho();
@@ -352,18 +398,65 @@ static void app_initialize(int argc, char **argv)
        refresh();
 }
 
+static void update_timer(struct app_state *state, uint_fast32_t ms)
+{
+       unsigned sec, min;
+
+       state->timer_ms = ms;
+       sec = ms / 1000; ms %= 1000;
+       min = sec / 60; sec %= 60;
+       mvwprintw(state->timer, 0, 0, "Time: %u:%.2u.%.3u",
+                 min, sec, (unsigned)ms);
+       wclrtoeol(state->timer);
+       wrefresh(state->timer);
+}
+
 static uint_fast32_t do_move(struct app_state *state, int x, int y)
 {
        uint_fast32_t mask;
 
        if ((mask = game_do_move(&state->board, x, y)) != 0) {
+               uint_fast32_t goal = game_check_goal(&state->board);
+
+               if (state->view_goal_on_game) {
+                       state->view_goal_on_game = 0;
+                       mask |= goal;
+               }
+
+               if (goal == 0) {
+                       /* Solved! */
+                       update_timer(state, game_finish(&state->board));
+                       mask |= ~GOAL_MASK;
+                       state->cursor = -1;
+                       timeout(-1);
+               }
+
                curs_redraw_game(state, mask);
-               refresh();
+               doupdate();
        }
 
        return mask;
 }
 
+static void do_reset_cursor(struct app_state *state)
+{
+       state->cursor = 5*state->board.y + state->board.x;
+}
+
+static void do_new_game(struct app_state *state)
+{
+       game_reset(&state->board);
+
+       do_reset_cursor(state);
+       curs_redraw_game(state, -1);
+       curs_redraw_goal(state, -1);
+       update_timer(state, 0);
+       doupdate();
+
+       timeout(33);
+       game_begin(&state->board);
+}
+
 #if HAVE_CURSES_MOUSE_SUPPORT
 static void do_mouse(struct app_state *state)
 {
@@ -384,14 +477,30 @@ static void do_mouse(struct app_state *state)
        y = MOUSE_Y_POS;
 
        bstate = 0;
-       if (BUTTON_CHANGED(1)) {
-               switch (BUTTON_STATUS(1)) {
-               case BUTTON_RELEASED: bstate |= BUTTON1_RELEASED;
-               case BUTTON_PRESSED:  bstate |= BUTTON1_PRESSED;
-               }
-       }
+
+#define set_bstate_helper(button) do { if (BUTTON_CHANGED(button)) \
+       switch (BUTTON_STATUS(button)) { \
+       case BUTTON_RELEASED: bstate |= BUTTON ## button ##  _RELEASED; break; \
+       case BUTTON_PRESSED:  bstate |= BUTTON ## button ##  _PRESSED; break; \
+       } \
+} while (0);
+
+       set_bstate_helper(1);
+       set_bstate_helper(3);
 #endif
-       if (bstate == BUTTON1_PRESSED) {
+       if (bstate & BUTTON3_PRESSED) {
+               state->view_goal_on_game = 1;
+               curs_redraw_game(state, game_check_goal(&state->board));
+               doupdate();
+       }
+
+       if (bstate & BUTTON3_RELEASED) {
+               state->view_goal_on_game = 0;
+               curs_redraw_game(state, game_check_goal(&state->board));
+               doupdate();
+       }
+
+       if (!state->view_goal_on_game && bstate & BUTTON1_PRESSED) {
                uint_fast32_t cursor_mask, move_mask;
                int w, h;
 
@@ -409,7 +518,7 @@ static void do_mouse(struct app_state *state)
                move_mask = do_move(state, x/w, y/h);
                if ((cursor_mask & move_mask) == 0) {
                        curs_redraw_game(state, cursor_mask);
-                       refresh();
+                       doupdate();
                }
        }
 }
@@ -417,11 +526,18 @@ static void do_mouse(struct app_state *state)
 
 static void do_move_cursor(struct app_state *state, int c)
 {
-       uint_fast32_t mask = 1ul << state->cursor;
+       uint_fast32_t mask = 0;
 
        if (state->cursor < 0) {
-               /* Reset keyboard cursor to the empty position */
-               state->cursor = 5*state->board.y + state->board.x;
+               /* Cursor was hidden; reset it */
+               do_reset_cursor(state);
+       } else {
+               mask = 1ul << state->cursor;
+       }
+
+       if (state->view_goal_on_game) {
+               state->view_goal_on_game = 0;
+               mask |= game_check_goal(&state->board);
        }
 
        switch (c) {
@@ -444,7 +560,7 @@ static void do_move_cursor(struct app_state *state, int c)
        }
 
        curs_redraw_game(state, mask | 1ul << state->cursor);
-       refresh();
+       doupdate();
 }
 
 static void do_keystroke(struct app_state *state, int c)
@@ -453,8 +569,13 @@ static void do_keystroke(struct app_state *state, int c)
        case KEY_DOWN: case KEY_UP: case KEY_LEFT: case KEY_RIGHT:
                do_move_cursor(state, c);
                break;
+       case '\t':
+               state->view_goal_on_game ^= 2u;
+               curs_redraw_game(state, game_check_goal(&state->board));
+               doupdate();
+               break;
        case ' ':
-               if (state->cursor >= 0)
+               if (!(state->view_goal_on_game & 1) && state->cursor >= 0)
                        do_move(state, state->cursor%5, state->cursor/5);
                break;
        }
@@ -465,9 +586,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        app_initialize(argc, argv);
 
-       curs_redraw_game(&state, -1);
-       curs_redraw_goal(&state, -1);
-       refresh();
+       do_new_game(&state);
 
        while (1) {
                int c = getch();
@@ -480,7 +599,7 @@ int main(int argc, char **argv)
                        refresh();
                        curs_redraw_game(&state, -1);
                        curs_redraw_goal(&state, -1);
-                       refresh();
+                       update_timer(&state, state.timer_ms);
                        break;
 #endif
 #if HAVE_CURSES_MOUSE_SUPPORT
@@ -490,6 +609,10 @@ int main(int argc, char **argv)
 #endif
                default:
                        do_keystroke(&state, c);
+               case ERR:;
                }
+
+               if (state.board.x <= 4)
+                       update_timer(&state, game_elapsed(&state.board));
        }
 }