COL_TEXT, COL_FLASHTEXT,
COL_HIGHLIGHT, COL_LOWLIGHT, COL_GRID,
COL_BALL, COL_WRONG, COL_BUTTON,
- COL_LASER, COL_DIMLASER,
+ COL_CURSOR,
NCOLOURS
};
sfree(params);
}
-static game_params *dup_params(game_params *params)
+static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
}
}
-static char *encode_params(game_params *params, int full)
+static char *encode_params(const game_params *params, int full)
{
char str[256];
return dupstr(str);
}
-static config_item *game_configure(game_params *params)
+static config_item *game_configure(const game_params *params)
{
config_item *ret;
char buf[80];
return ret;
}
-static game_params *custom_params(config_item *cfg)
+static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
return ret;
}
-static char *validate_params(game_params *params, int full)
+static char *validate_params(const game_params *params, int full)
{
if (params->w < 2 || params->h < 2)
return "Width and height must both be at least two";
* Then we obfuscate it.
*/
-static char *new_game_desc(game_params *params, random_state *rs,
+static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, int interactive)
{
int nballs = params->minballs, i;
return ret;
}
-static char *validate_desc(game_params *params, char *desc)
+static char *validate_desc(const game_params *params, const char *desc)
{
int nballs, dlen = strlen(desc), i;
unsigned char *bmp;
#define BALL_GUESS 0x02
#define BALL_LOCK 0x04
-#define LASER_FLAGMASK 0xf800
-#define LASER_OMITTED 0x0800
-#define LASER_REFLECT 0x1000
-#define LASER_HIT 0x2000
-#define LASER_WRONG 0x4000
-#define LASER_FLASHED 0x8000
-#define LASER_EMPTY (~0)
+#define LASER_FLAGMASK 0x1f800
+#define LASER_OMITTED 0x0800
+#define LASER_REFLECT 0x1000
+#define LASER_HIT 0x2000
+#define LASER_WRONG 0x4000
+#define LASER_FLASHED 0x8000
+#define LASER_EMPTY (~0)
+
+#define FLAG_CURSOR 0x10000 /* needs to be disjoint from both sets */
struct game_state {
int w, h, minballs, maxballs, nballs, nlasers;
};
#endif
-static int range2grid(game_state *state, int rangeno, int *x, int *y, int *direction)
+static int range2grid(const game_state *state, int rangeno, int *x, int *y,
+ int *direction)
{
if (rangeno < 0)
return 0;
return 0;
}
-static int grid2range(game_state *state, int x, int y, int *rangeno)
+static int grid2range(const game_state *state, int x, int y, int *rangeno)
{
int ret, x1 = state->w+1, y1 = state->h+1;
if (x > 0 && x < x1 && y > 0 && y < y1) return 0; /* in arena */
- if (x < 0 || x > y1 || y < 0 || y > y1) return 0; /* outside grid */
+ if (x < 0 || x > x1 || y < 0 || y > y1) return 0; /* outside grid */
if ((x == 0 || x == x1) && (y == 0 || y == y1))
return 0; /* one of 4 corners */
return 1;
}
-static game_state *new_game(midend *me, game_params *params, char *desc)
+static game_state *new_game(midend *me, const game_params *params,
+ const char *desc)
{
game_state *state = snew(game_state);
int dlen = strlen(desc), i;
#define XFER(x) ret->x = state->x
-static game_state *dup_game(game_state *state)
+static game_state *dup_game(const game_state *state)
{
game_state *ret = snew(game_state);
sfree(state);
}
-static char *solve_game(game_state *state, game_state *currstate,
- char *aux, char **error)
+static char *solve_game(const game_state *state, const game_state *currstate,
+ const char *aux, char **error)
{
return dupstr("S");
}
-static char *game_text_format(game_state *state)
+static int game_can_format_as_text_now(const game_params *params)
+{
+ return TRUE;
+}
+
+static char *game_text_format(const game_state *state)
{
return NULL;
}
struct game_ui {
int flash_laserno;
int errors, newmove;
+ int cur_x, cur_y, cur_visible;
+ int flash_laser; /* 0 = never, 1 = always, 2 = if anim. */
};
-static game_ui *new_ui(game_state *state)
+static game_ui *new_ui(const game_state *state)
{
game_ui *ui = snew(game_ui);
ui->flash_laserno = LASER_EMPTY;
ui->errors = 0;
ui->newmove = FALSE;
+
+ ui->cur_x = ui->cur_y = 1;
+ ui->cur_visible = 0;
+
+ ui->flash_laser = 0;
+
return ui;
}
sfree(ui);
}
-static char *encode_ui(game_ui *ui)
+static char *encode_ui(const game_ui *ui)
{
char buf[80];
/*
return dupstr(buf);
}
-static void decode_ui(game_ui *ui, char *encoding)
+static void decode_ui(game_ui *ui, const char *encoding)
{
sscanf(encoding, "E%d", &ui->errors);
}
-static void game_changed_state(game_ui *ui, game_state *oldstate,
- game_state *newstate)
+static void game_changed_state(game_ui *ui, const game_state *oldstate,
+ const game_state *newstate)
{
/*
* If we've encountered a `justwrong' state as a result of
debug(("isball, new (%d, %d)\n", gx, gy));
/* if we're off the grid (into the firing range) there's never a ball. */
- if (gx < 1 || gy < 1 || gx > state->h || gy > state->w)
+ if (gx < 1 || gy < 1 || gx > state->w || gy > state->h)
return 0;
if (GRID(state, gx,gy) & BALL_CORRECT)
int flash_laserno, isflash;
};
-static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
- int x, int y, int button)
+static char *interpret_move(const game_state *state, game_ui *ui,
+ const game_drawstate *ds,
+ int x, int y, int button)
{
- int gx = -1, gy = -1, rangeno = -1;
+ int gx = -1, gy = -1, rangeno = -1, wouldflash = 0;
enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
char buf[80], *nullret = NULL;
+ if (IS_CURSOR_MOVE(button)) {
+ int cx = ui->cur_x, cy = ui->cur_y;
+
+ move_cursor(button, &cx, &cy, state->w+2, state->h+2, 0);
+ if ((cx == 0 && cy == 0 && !CAN_REVEAL(state)) ||
+ (cx == 0 && cy == state->h+1) ||
+ (cx == state->w+1 && cy == 0) ||
+ (cx == state->w+1 && cy == state->h+1))
+ return NULL; /* disallow moving cursor to corners. */
+ ui->cur_x = cx;
+ ui->cur_y = cy;
+ ui->cur_visible = 1;
+ return "";
+ }
+
if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
gx = FROMDRAW(x);
gy = FROMDRAW(y);
+ ui->cur_visible = 0;
+ wouldflash = 1;
+ } else if (button == LEFT_RELEASE) {
+ ui->flash_laser = 0;
+ return "";
+ } else if (IS_CURSOR_SELECT(button)) {
+ if (ui->cur_visible) {
+ gx = ui->cur_x;
+ gy = ui->cur_y;
+ ui->flash_laser = 0;
+ wouldflash = 2;
+ } else {
+ ui->cur_visible = 1;
+ return "";
+ }
+ /* Fix up 'button' for the below logic. */
+ if (button == CURSOR_SELECT2) button = RIGHT_BUTTON;
+ else button = LEFT_BUTTON;
+ }
+
+ if (gx != -1 && gy != -1) {
if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
action = REVEAL;
if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
else
action = TOGGLE_ROW_LOCK; /* and use gy */
}
- } else if (button == LEFT_RELEASE) {
- ui->flash_laserno = LASER_EMPTY;
- return "";
}
switch (action) {
if (state->reveal && state->exits[rangeno] == LASER_EMPTY)
return nullret;
ui->flash_laserno = rangeno;
+ ui->flash_laser = wouldflash;
nullret = "";
if (state->exits[rangeno] != LASER_EMPTY)
return "";
case REVEAL:
if (!CAN_REVEAL(state)) return nullret;
+ if (ui->cur_visible == 1) ui->cur_x = ui->cur_y = 1;
sprintf(buf, "R");
break;
return dupstr(buf);
}
-static game_state *execute_move(game_state *from, char *move)
+static game_state *execute_move(const game_state *from, const char *move)
{
game_state *ret = dup_game(from);
int gx = -1, gy = -1, rangeno = -1;
* Drawing routines.
*/
-static void game_compute_size(game_params *params, int tilesize,
- int *x, int *y)
+static void game_compute_size(const game_params *params, int tilesize,
+ int *x, int *y)
{
/* Border is ts/2, to make things easier.
* Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles
}
static void game_set_size(drawing *dr, game_drawstate *ds,
- game_params *params, int tilesize)
+ const game_params *params, int tilesize)
{
ds->tilesize = tilesize;
ds->crad = (tilesize-1)/2;
ret[COL_BUTTON * 3 + 1] = 1.0F;
ret[COL_BUTTON * 3 + 2] = 0.0F;
- ret[COL_LASER * 3 + 0] = 1.0F;
- ret[COL_LASER * 3 + 1] = 0.0F;
- ret[COL_LASER * 3 + 2] = 0.0F;
-
- ret[COL_DIMLASER * 3 + 0] = 0.5F;
- ret[COL_DIMLASER * 3 + 1] = 0.0F;
- ret[COL_DIMLASER * 3 + 2] = 0.0F;
+ ret[COL_CURSOR * 3 + 0] = 1.0F;
+ ret[COL_CURSOR * 3 + 1] = 0.0F;
+ ret[COL_CURSOR * 3 + 2] = 0.0F;
for (i = 0; i < 3; i++) {
ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
return ret;
}
-static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
+static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
{
struct game_drawstate *ds = snew(struct game_drawstate);
sfree(ds);
}
-static void draw_arena_tile(drawing *dr, game_state *gs, game_drawstate *ds,
+static void draw_square_cursor(drawing *dr, game_drawstate *ds, int dx, int dy)
+{
+ int coff = TILE_SIZE/8;
+ draw_rect_outline(dr, dx + coff, dy + coff,
+ TILE_SIZE - coff*2,
+ TILE_SIZE - coff*2,
+ COL_CURSOR);
+}
+
+
+static void draw_arena_tile(drawing *dr, const game_state *gs,
+ game_drawstate *ds, const game_ui *ui,
int ax, int ay, int force, int isflash)
{
int gx = ax+1, gy = ay+1;
int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
int dx = TODRAW(gx), dy = TODRAW(gy);
+ if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy)
+ gs_tile |= FLAG_CURSOR;
+
if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) {
- int bcol, bg;
+ int bcol, ocol, bg;
bg = (gs->reveal ? COL_BACKGROUND :
(gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER);
bcol = bg;
}
}
+ ocol = (gs_tile & FLAG_CURSOR && bcol != bg) ? COL_CURSOR : bcol;
draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
+ ocol, ocol);
+ draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-3,
bcol, bcol);
+
+ if (gs_tile & FLAG_CURSOR && bcol == bg)
+ draw_square_cursor(dr, ds, dx, dy);
+
if (gs->reveal &&
(gs_tile & BALL_GUESS) &&
!(gs_tile & BALL_CORRECT)) {
GRID(ds,gx,gy) = gs_tile;
}
-static void draw_laser_tile(drawing *dr, game_state *gs, game_drawstate *ds,
- game_ui *ui, int lno, int force)
+static void draw_laser_tile(drawing *dr, const game_state *gs,
+ game_drawstate *ds, const game_ui *ui,
+ int lno, int force)
{
int gx, gy, dx, dy, unused;
int wrong, omitted, reflect, hit, laserval, flash = 0, tmp;
hit = gs_tile & LASER_HIT;
laserval = gs_tile & ~LASER_FLAGMASK;
- if (lno == ui->flash_laserno)
+ if (lno == ds->flash_laserno)
gs_tile |= LASER_FLASHED;
else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) {
- if (exitno == ui->flash_laserno)
+ if (exitno == ds->flash_laserno)
gs_tile |= LASER_FLASHED;
}
if (gs_tile & LASER_FLASHED) flash = 1;
gs_tile |= wrong | omitted;
+ if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy)
+ gs_tile |= FLAG_CURSOR;
+
if (gs_tile != ds_tile || force) {
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];
+ if (gs_tile &~ (LASER_WRONG | LASER_OMITTED | FLAG_CURSOR)) {
+ char str[32];
int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT;
if (reflect || hit)
FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
tcol, str);
}
+ if (gs_tile & FLAG_CURSOR)
+ draw_square_cursor(dr, ds, dx, dy);
+
draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
}
GRID(ds, gx, gy) = gs_tile;
}
+#define CUR_ANIM 0.2F
-static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
- game_state *state, int dir, game_ui *ui,
- float animtime, float flashtime)
+static void game_redraw(drawing *dr, game_drawstate *ds,
+ const game_state *oldstate, const game_state *state,
+ int dir, const game_ui *ui,
+ float animtime, float flashtime)
{
int i, x, y, ts = TILE_SIZE, isflash = 0, force = 0;
/* draw the arena */
for (x = 0; x < state->w; x++) {
for (y = 0; y < state->h; y++) {
- draw_arena_tile(dr, state, ds, x, y, force, isflash);
+ draw_arena_tile(dr, state, ds, ui, x, y, force, isflash);
}
}
/* draw the lasers */
+ ds->flash_laserno = LASER_EMPTY;
+ if (ui->flash_laser == 1)
+ ds->flash_laserno = ui->flash_laserno;
+ else if (ui->flash_laser == 2 && animtime > 0)
+ ds->flash_laserno = ui->flash_laserno;
+
for (i = 0; i < 2*(state->w+state->h); i++) {
draw_laser_tile(dr, state, ds, ui, i, force);
}
/* draw the 'finish' button */
if (CAN_REVEAL(state)) {
- clip(dr, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
+ int outline = (ui->cur_visible && ui->cur_x == 0 && ui->cur_y == 0)
+ ? COL_CURSOR : COL_BALL;
+ clip(dr, TODRAW(0)-1, TODRAW(0)-1, TILE_SIZE+1, TILE_SIZE+1);
draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
- COL_BUTTON, COL_BALL);
+ outline, outline);
+ draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad-2,
+ COL_BUTTON, COL_BUTTON);
unclip(dr);
} else {
- draw_rect(dr, TODRAW(0), TODRAW(0),
- TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND);
+ draw_rect(dr, TODRAW(0)-1, TODRAW(0)-1,
+ TILE_SIZE+1, TILE_SIZE+1, COL_BACKGROUND);
}
draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
ds->reveal = state->reveal;
- ds->flash_laserno = ui->flash_laserno;
ds->isflash = isflash;
{
}
}
-static float game_anim_length(game_state *oldstate, game_state *newstate,
- int dir, game_ui *ui)
+static float game_anim_length(const game_state *oldstate,
+ const game_state *newstate, int dir, game_ui *ui)
{
- return 0.0F;
+ return (ui->flash_laser == 2) ? CUR_ANIM : 0.0F;
}
-static float game_flash_length(game_state *oldstate, game_state *newstate,
- int dir, game_ui *ui)
+static float game_flash_length(const game_state *oldstate,
+ const game_state *newstate, int dir, game_ui *ui)
{
if (!oldstate->reveal && newstate->reveal)
return 4.0F * FLASH_FRAME;
return 0.0F;
}
-static int game_timing_state(game_state *state, game_ui *ui)
+static int game_status(const game_state *state)
+{
+ if (state->reveal) {
+ /*
+ * We return nonzero whenever the solution has been revealed,
+ * even (on spoiler grounds) if it wasn't guessed correctly.
+ */
+ if (state->nwrong == 0 &&
+ state->nmissed == 0 &&
+ state->nright >= state->minballs)
+ return +1;
+ else
+ return -1;
+ }
+ return 0;
+}
+
+static int game_timing_state(const game_state *state, game_ui *ui)
{
return TRUE;
}
-static void game_print_size(game_params *params, float *x, float *y)
+static void game_print_size(const game_params *params, float *x, float *y)
{
}
-static void game_print(drawing *dr, game_state *state, int tilesize)
+static void game_print(drawing *dr, const game_state *state, int tilesize)
{
}
#endif
const struct game thegame = {
- "Black Box", "games.blackbox",
+ "Black Box", "games.blackbox", "blackbox",
default_params,
game_fetch_preset,
decode_params,
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,
game_redraw,
game_anim_length,
game_flash_length,
+ game_status,
FALSE, FALSE, game_print_size, game_print,
TRUE, /* wants_statusbar */
FALSE, game_timing_state,
- 0, /* flags */
+ REQUIRE_RBUTTON, /* flags */
};
/* vim: set shiftwidth=4 tabstop=8: */