chiark / gitweb /
New infrastructure feature. Games are now permitted to be
[sgt-puzzles.git] / blackbox.c
index 4f2270448057308c75deb49995d8f43cedd52011..a8c6d5a2cb5c0c303452585a77aaec855e09f782 100644 (file)
@@ -389,7 +389,7 @@ static int grid2range(game_state *state, int x, int y, int *rangeno)
     return 1;
 }
 
-static game_state *new_game(midend_data *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, game_params *params, char *desc)
 {
     game_state *state = snew(game_state);
     int dlen = strlen(desc), i;
@@ -463,6 +463,11 @@ static char *solve_game(game_state *state, game_state *currstate,
     return dupstr("S");
 }
 
+static int game_can_format_as_text_now(game_params *params)
+{
+    return TRUE;
+}
+
 static char *game_text_format(game_state *state)
 {
     return NULL;
@@ -475,7 +480,7 @@ struct game_ui {
 
 static game_ui *new_ui(game_state *state)
 {
-    game_ui *ui = snew(struct game_ui);
+    game_ui *ui = snew(game_ui);
     ui->flash_laserno = LASER_EMPTY;
     ui->errors = 0;
     ui->newmove = FALSE;
@@ -692,9 +697,9 @@ static int check_guesses(game_state *state, int cagey)
             * grid, so that repeating the same marking will give
             * the same answer instead of a different one.
             */
-           random_state *rs = random_init((char *)guesses->grid,
-                                          (state->w+2)*(state->h+2) *
-                                          sizeof(unsigned int));
+           random_state *rs = random_new((char *)guesses->grid,
+                                         (state->w+2)*(state->h+2) *
+                                         sizeof(unsigned int));
            n = random_upto(rs, n);
            random_free(rs);
            for (i = 0; i < guesses->nlasers; i++) {
@@ -727,9 +732,9 @@ static int check_guesses(game_state *state, int cagey)
             * grid, so that repeating the same marking will give
             * the same answer instead of a different one.
             */
-           random_state *rs = random_init((char *)guesses->grid,
-                                          (state->w+2)*(state->h+2) *
-                                          sizeof(unsigned int));
+           random_state *rs = random_new((char *)guesses->grid,
+                                         (state->w+2)*(state->h+2) *
+                                         sizeof(unsigned int));
            n = random_upto(rs, n);
            random_free(rs);
            for (i = 0; i < guesses->nlasers; i++) {
@@ -813,7 +818,9 @@ static int check_guesses(game_state *state, int cagey)
             ret = 0;
         }
     }
-    if (ret == 0) goto done;
+    if (ret == 0 ||
+       state->nguesses < state->minballs ||
+       state->nguesses > state->maxballs) goto done;
 
     /* fix up original state so the 'correct' balls end up matching the guesses,
      * as we've just proved that they were equivalent. */
@@ -859,7 +866,7 @@ struct game_drawstate {
     int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
     unsigned int *grid;          /* as the game_state grid */
     int started, reveal;
-    int flash_laserno;
+    int flash_laserno, isflash;
 };
 
 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
@@ -1052,15 +1059,15 @@ static void game_compute_size(game_params *params, int tilesize,
     *y = (params->h + 3) * tilesize;
 }
 
-static void game_set_size(game_drawstate *ds, game_params *params,
-                         int tilesize)
+static void game_set_size(drawing *dr, game_drawstate *ds,
+                         game_params *params, int tilesize)
 {
     ds->tilesize = tilesize;
     ds->crad = (tilesize-1)/2;
     ds->rrad = (3*tilesize)/8;
 }
 
-static float *game_colours(frontend *fe, game_state *state, int *ncolours)
+static float *game_colours(frontend *fe, int *ncolours)
 {
     float *ret = snewn(3 * NCOLOURS, float);
     int i;
@@ -1102,7 +1109,7 @@ static float *game_colours(frontend *fe, game_state *state, int *ncolours)
     return ret;
 }
 
-static game_drawstate *game_new_drawstate(game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
 {
     struct game_drawstate *ds = snew(struct game_drawstate);
 
@@ -1112,17 +1119,18 @@ static game_drawstate *game_new_drawstate(game_state *state)
     memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
     ds->started = ds->reveal = 0;
     ds->flash_laserno = LASER_EMPTY;
+    ds->isflash = 0;
 
     return ds;
 }
 
-static void game_free_drawstate(game_drawstate *ds)
+static void game_free_drawstate(drawing *dr, game_drawstate *ds)
 {
     sfree(ds->grid);
     sfree(ds);
 }
 
-static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
+static void draw_arena_tile(drawing *dr, game_state *gs, game_drawstate *ds,
                             int ax, int ay, int force, int isflash)
 {
     int gx = ax+1, gy = ay+1;
@@ -1135,8 +1143,8 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
         bg = (gs->reveal ? COL_BACKGROUND :
               (gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER);
 
-        draw_rect(fe, dx, dy, TILE_SIZE, TILE_SIZE, bg);
-        draw_rect_outline(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
+        draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, bg);
+        draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
 
         if (gs->reveal) {
             /* Guessed balls are always black; if they're incorrect they'll
@@ -1158,7 +1166,7 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
             }
         }
 
-        draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
+        draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
                     bcol, bcol);
 
         if (gs->reveal &&
@@ -1177,7 +1185,7 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
            coords[5] = y2-1;
            coords[6] = x2-1;
            coords[7] = y2+1;
-           draw_polygon(fe, coords, 4, COL_WRONG, COL_WRONG);
+           draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
            coords[0] = x2+1;
            coords[1] = y1+1;
            coords[2] = x2-1;
@@ -1186,14 +1194,14 @@ static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
            coords[5] = y2-1;
            coords[6] = x1+1;
            coords[7] = y2+1;
-           draw_polygon(fe, coords, 4, COL_WRONG, COL_WRONG);
+           draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
         }
-        draw_update(fe, dx, dy, TILE_SIZE, TILE_SIZE);
+        draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
     }
     GRID(ds,gx,gy) = gs_tile;
 }
 
-static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
+static void draw_laser_tile(drawing *dr, game_state *gs, game_drawstate *ds,
                             game_ui *ui, int lno, int force)
 {
     int gx, gy, dx, dy, unused;
@@ -1226,8 +1234,8 @@ static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
     gs_tile |= wrong | omitted;
 
     if (gs_tile != ds_tile || force) {
-        draw_rect(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
-        draw_rect_outline(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
+        draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
+        draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
 
         if (gs_tile &~ (LASER_WRONG | LASER_OMITTED)) {
             char str[10];
@@ -1239,25 +1247,25 @@ static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
                 sprintf(str, "%d", laserval);
 
             if (wrong) {
-                draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
+                draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
                             ds->rrad,
                             COL_WRONG, COL_WRONG);
-                draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
+                draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
                             ds->rrad - TILE_SIZE/16,
                             COL_BACKGROUND, COL_WRONG);
             }
 
-            draw_text(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
+            draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
                       FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
                       tcol, str);
         }
-        draw_update(fe, dx, dy, TILE_SIZE, TILE_SIZE);
+        draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
     }
     GRID(ds, gx, gy) = gs_tile;
 }
 
 
-static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
+static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
                        game_state *state, int dir, game_ui *ui,
                        float animtime, float flashtime)
 {
@@ -1266,7 +1274,6 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
     if (flashtime > 0) {
         int frame = (int)(flashtime / FLASH_FRAME);
         isflash = (frame % 2) == 0;
-        force = 1;
         debug(("game_redraw: flashtime = %f", flashtime));
     }
 
@@ -1274,56 +1281,59 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
         int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1;
         int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2);
 
-        draw_rect(fe, 0, 0,
+        draw_rect(dr, 0, 0,
                   TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3),
                   COL_BACKGROUND);
 
         /* clockwise around the outline starting at pt behind (1,1). */
-        draw_line(fe, x0+ts, y0+ts, x0+ts, y0,    COL_HIGHLIGHT);
-        draw_line(fe, x0+ts, y0,    x1-ts, y0,    COL_HIGHLIGHT);
-        draw_line(fe, x1-ts, y0,    x1-ts, y0+ts, COL_LOWLIGHT);
-        draw_line(fe, x1-ts, y0+ts, x1,    y0+ts, COL_HIGHLIGHT);
-        draw_line(fe, x1,    y0+ts, x1,    y1-ts, COL_LOWLIGHT);
-        draw_line(fe, x1,    y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
-        draw_line(fe, x1-ts, y1-ts, x1-ts, y1,    COL_LOWLIGHT);
-        draw_line(fe, x1-ts, y1,    x0+ts, y1,    COL_LOWLIGHT);
-        draw_line(fe, x0+ts, y1,    x0+ts, y1-ts, COL_HIGHLIGHT);
-        draw_line(fe, x0+ts, y1-ts, x0,    y1-ts, COL_LOWLIGHT);
-        draw_line(fe, x0,    y1-ts, x0,    y0+ts, COL_HIGHLIGHT);
-        draw_line(fe, x0,    y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
+        draw_line(dr, x0+ts, y0+ts, x0+ts, y0,    COL_HIGHLIGHT);
+        draw_line(dr, x0+ts, y0,    x1-ts, y0,    COL_HIGHLIGHT);
+        draw_line(dr, x1-ts, y0,    x1-ts, y0+ts, COL_LOWLIGHT);
+        draw_line(dr, x1-ts, y0+ts, x1,    y0+ts, COL_HIGHLIGHT);
+        draw_line(dr, x1,    y0+ts, x1,    y1-ts, COL_LOWLIGHT);
+        draw_line(dr, x1,    y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
+        draw_line(dr, x1-ts, y1-ts, x1-ts, y1,    COL_LOWLIGHT);
+        draw_line(dr, x1-ts, y1,    x0+ts, y1,    COL_LOWLIGHT);
+        draw_line(dr, x0+ts, y1,    x0+ts, y1-ts, COL_HIGHLIGHT);
+        draw_line(dr, x0+ts, y1-ts, x0,    y1-ts, COL_LOWLIGHT);
+        draw_line(dr, x0,    y1-ts, x0,    y0+ts, COL_HIGHLIGHT);
+        draw_line(dr, x0,    y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
         /* phew... */
 
-        draw_update(fe, 0, 0,
+        draw_update(dr, 0, 0,
                     TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
         force = 1;
         ds->started = 1;
     }
 
+    if (isflash != ds->isflash) force = 1;
+
     /* draw the arena */
     for (x = 0; x < state->w; x++) {
         for (y = 0; y < state->h; y++) {
-            draw_arena_tile(fe, state, ds, x, y, force, isflash);
+            draw_arena_tile(dr, state, ds, x, y, force, isflash);
         }
     }
 
     /* draw the lasers */
     for (i = 0; i < 2*(state->w+state->h); i++) {
-        draw_laser_tile(fe, state, ds, ui, i, force);
+        draw_laser_tile(dr, state, ds, ui, i, force);
     }
 
     /* draw the 'finish' button */
     if (CAN_REVEAL(state)) {
-        clip(fe, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
-        draw_circle(fe, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
+        clip(dr, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
+        draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
                     COL_BUTTON, COL_BALL);
-       unclip(fe);
+       unclip(dr);
     } else {
-        draw_rect(fe, TODRAW(0), TODRAW(0),
+        draw_rect(dr, TODRAW(0), TODRAW(0),
                  TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND);
     }
-    draw_update(fe, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
+    draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
     ds->reveal = state->reveal;
     ds->flash_laserno = ui->flash_laserno;
+    ds->isflash = isflash;
 
     {
         char buf[256];
@@ -1356,7 +1366,7 @@ static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
            sprintf(buf + strlen(buf), " (%d error%s)",
                    ui->errors, ui->errors > 1 ? "s" : "");
        }
-        status_bar(fe, buf);
+        status_bar(dr, buf);
     }
 }
 
@@ -1375,14 +1385,17 @@ static float game_flash_length(game_state *oldstate, game_state *newstate,
         return 0.0F;
 }
 
-static int game_wants_statusbar(void)
+static int game_timing_state(game_state *state, game_ui *ui)
 {
     return TRUE;
 }
 
-static int game_timing_state(game_state *state, game_ui *ui)
+static void game_print_size(game_params *params, float *x, float *y)
+{
+}
+
+static void game_print(drawing *dr, game_state *state, int tilesize)
 {
-    return TRUE;
 }
 
 #ifdef COMBINED
@@ -1390,7 +1403,7 @@ static int game_timing_state(game_state *state, game_ui *ui)
 #endif
 
 const struct game thegame = {
-    "Black Box", "games.blackbox",
+    "Black Box", "games.blackbox", "blackbox",
     default_params,
     game_fetch_preset,
     decode_params,
@@ -1405,7 +1418,7 @@ const struct game thegame = {
     dup_game,
     free_game,
     TRUE, solve_game,
-    FALSE, game_text_format,
+    FALSE, game_can_format_as_text_now, game_text_format,
     new_ui,
     free_ui,
     encode_ui,
@@ -1420,9 +1433,10 @@ const struct game thegame = {
     game_redraw,
     game_anim_length,
     game_flash_length,
-    game_wants_statusbar,
+    FALSE, FALSE, game_print_size, game_print,
+    TRUE,                             /* wants_statusbar */
     FALSE, game_timing_state,
-    0,                                /* mouse_priorities */
+    REQUIRE_RBUTTON,                  /* flags */
 };
 
 /* vim: set shiftwidth=4 tabstop=8: */