aboutsummaryrefslogtreecommitdiff
path: root/blackbox.c
diff options
context:
space:
mode:
Diffstat (limited to 'blackbox.c')
-rw-r--r--blackbox.c116
1 files changed, 64 insertions, 52 deletions
diff --git a/blackbox.c b/blackbox.c
index 576fcfe..3067fbf 100644
--- a/blackbox.c
+++ b/blackbox.c
@@ -297,9 +297,10 @@ struct game_state {
int w, h, minballs, maxballs, nballs, nlasers;
unsigned int *grid; /* (w+2)x(h+2), to allow for laser firing range */
unsigned int *exits; /* one per laser */
- int done; /* user has finished placing his own balls. */
+ bool done; /* user has finished placing his own balls. */
int laserno; /* number of next laser to be fired. */
- int nguesses, reveal, justwrong, nright, nwrong, nmissed;
+ int nguesses, nright, nwrong, nmissed;
+ bool reveal, justwrong;
};
#define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
@@ -324,18 +325,18 @@ static const char *dirstrs[] = {
};
#endif
-static int range2grid(const game_state *state, int rangeno, int *x, int *y,
- int *direction)
+static bool range2grid(const game_state *state, int rangeno, int *x, int *y,
+ int *direction)
{
if (rangeno < 0)
- return 0;
+ return false;
if (rangeno < state->w) {
/* top row; from (1,0) to (w,0) */
*x = rangeno + 1;
*y = 0;
*direction = DIR_DOWN;
- return 1;
+ return true;
}
rangeno -= state->w;
if (rangeno < state->h) {
@@ -343,7 +344,7 @@ static int range2grid(const game_state *state, int rangeno, int *x, int *y,
*x = state->w+1;
*y = rangeno + 1;
*direction = DIR_LEFT;
- return 1;
+ return true;
}
rangeno -= state->h;
if (rangeno < state->w) {
@@ -351,7 +352,7 @@ static int range2grid(const game_state *state, int rangeno, int *x, int *y,
*x = (state->w - rangeno);
*y = state->h+1;
*direction = DIR_UP;
- return 1;
+ return true;
}
rangeno -= state->w;
if (rangeno < state->h) {
@@ -359,20 +360,20 @@ static int range2grid(const game_state *state, int rangeno, int *x, int *y,
*x = 0;
*y = (state->h - rangeno);
*direction = DIR_RIGHT;
- return 1;
+ return true;
}
- return 0;
+ return false;
}
-static int grid2range(const game_state *state, int x, int y, int *rangeno)
+static bool 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 > x1 || y < 0 || y > y1) return 0; /* outside grid */
+ if (x > 0 && x < x1 && y > 0 && y < y1) return false; /* in arena */
+ if (x < 0 || x > x1 || y < 0 || y > y1) return false; /* outside grid */
if ((x == 0 || x == x1) && (y == 0 || y == y1))
- return 0; /* one of 4 corners */
+ return false; /* one of 4 corners */
if (y == 0) { /* top line */
ret = x - 1;
@@ -385,7 +386,7 @@ static int grid2range(const game_state *state, int x, int y, int *rangeno)
}
*rangeno = ret;
debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret));
- return 1;
+ return true;
}
static game_state *new_game(midend *me, const game_params *params,
@@ -416,8 +417,10 @@ static game_state *new_game(midend *me, const game_params *params,
}
sfree(bmp);
- state->done = state->nguesses = state->reveal = state->justwrong =
- state->nright = state->nwrong = state->nmissed = 0;
+ state->done = false;
+ state->justwrong = false;
+ state->reveal = false;
+ state->nguesses = state->nright = state->nwrong = state->nmissed = 0;
state->laserno = 1;
return state;
@@ -475,8 +478,10 @@ static char *game_text_format(const game_state *state)
struct game_ui {
int flash_laserno;
- int errors, newmove;
- int cur_x, cur_y, cur_visible;
+ int errors;
+ bool newmove;
+ int cur_x, cur_y;
+ bool cur_visible;
int flash_laser; /* 0 = never, 1 = always, 2 = if anim. */
};
@@ -488,7 +493,7 @@ static game_ui *new_ui(const game_state *state)
ui->newmove = false;
ui->cur_x = ui->cur_y = 1;
- ui->cur_visible = 0;
+ ui->cur_visible = false;
ui->flash_laser = 0;
@@ -537,7 +542,7 @@ enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT };
/* Given a position and a direction, check whether we can see a ball in front
* of us, or to our front-left or front-right. */
-static int isball(game_state *state, int gx, int gy, int direction, int lookwhere)
+static bool isball(game_state *state, int gx, int gy, int direction, int lookwhere)
{
debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction],
lookwhere == LOOK_LEFT ? "LEFT" :
@@ -554,17 +559,18 @@ static int isball(game_state *state, int gx, int gy, int direction, int lookwher
/* if we're off the grid (into the firing range) there's never a ball. */
if (gx < 1 || gy < 1 || gx > state->w || gy > state->h)
- return 0;
+ return false;
if (GRID(state, gx,gy) & BALL_CORRECT)
- return 1;
+ return true;
- return 0;
+ return false;
}
static int fire_laser_internal(game_state *state, int x, int y, int direction)
{
- int unused, lno, tmp;
+ int unused, lno;
+ bool tmp;
tmp = grid2range(state, x, y, &lno);
assert(tmp);
@@ -627,7 +633,8 @@ static int fire_laser_internal(game_state *state, int x, int y, int direction)
static int laser_exit(game_state *state, int entryno)
{
- int tmp, x, y, direction;
+ int x, y, direction;
+ bool tmp;
tmp = range2grid(state, entryno, &x, &y, &direction);
assert(tmp);
@@ -637,7 +644,8 @@ static int laser_exit(game_state *state, int entryno)
static void fire_laser(game_state *state, int entryno)
{
- int tmp, exitno, x, y, direction;
+ int exitno, x, y, direction;
+ bool tmp;
tmp = range2grid(state, entryno, &x, &y, &direction);
assert(tmp);
@@ -662,10 +670,11 @@ static void fire_laser(game_state *state, int entryno)
* have already guessed). This is required because any layout with >4 balls
* might have multiple valid solutions. Returns non-zero for a 'correct'
* (i.e. consistent) layout. */
-static int check_guesses(game_state *state, int cagey)
+static int check_guesses(game_state *state, bool cagey)
{
game_state *solution, *guesses;
int i, x, y, n, unused, tmp;
+ bool tmpb;
int ret = 0;
if (cagey) {
@@ -768,8 +777,8 @@ static int check_guesses(game_state *state, int cagey)
/* clear out the lasers of solution */
for (i = 0; i < solution->nlasers; i++) {
- tmp = range2grid(solution, i, &x, &y, &unused);
- assert(tmp);
+ tmpb = range2grid(solution, i, &x, &y, &unused);
+ assert(tmpb);
GRID(solution, x, y) = 0;
solution->exits[i] = LASER_EMPTY;
}
@@ -799,8 +808,8 @@ static int check_guesses(game_state *state, int cagey)
/* check each game_state's laser against the other; if any differ, return 0 */
ret = 1;
for (i = 0; i < solution->nlasers; i++) {
- tmp = range2grid(solution, i, &x, &y, &unused);
- assert(tmp);
+ tmpb = range2grid(solution, i, &x, &y, &unused);
+ assert(tmpb);
if (solution->exits[i] != guesses->exits[i]) {
/* If the original state didn't have this shot fired,
@@ -814,8 +823,8 @@ static int check_guesses(game_state *state, int cagey)
else {
/* add a new shot, incrementing state's laser count. */
int ex, ey, newno = state->laserno++;
- tmp = range2grid(state, state->exits[i], &ex, &ey, &unused);
- assert(tmp);
+ tmpb = range2grid(state, state->exits[i], &ex, &ey, &unused);
+ assert(tmpb);
GRID(state, x, y) = newno;
GRID(state, ex, ey) = newno;
}
@@ -857,7 +866,7 @@ done:
}
free_game(solution);
free_game(guesses);
- state->reveal = 1;
+ state->reveal = true;
return ret;
}
@@ -873,8 +882,8 @@ done:
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, isflash;
+ bool started, reveal, isflash;
+ int flash_laserno;
};
static char *interpret_move(const game_state *state, game_ui *ui,
@@ -889,7 +898,7 @@ static char *interpret_move(const game_state *state, game_ui *ui,
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);
+ move_cursor(button, &cx, &cy, state->w+2, state->h+2, false);
if ((cx == 0 && cy == 0 && !CAN_REVEAL(state)) ||
(cx == 0 && cy == state->h+1) ||
(cx == state->w+1 && cy == 0) ||
@@ -897,14 +906,14 @@ static char *interpret_move(const game_state *state, game_ui *ui,
return NULL; /* disallow moving cursor to corners. */
ui->cur_x = cx;
ui->cur_y = cy;
- ui->cur_visible = 1;
+ ui->cur_visible = true;
return UI_UPDATE;
}
if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
gx = FROMDRAW(x);
gy = FROMDRAW(y);
- ui->cur_visible = 0;
+ ui->cur_visible = false;
wouldflash = 1;
} else if (button == LEFT_RELEASE) {
ui->flash_laser = 0;
@@ -916,7 +925,7 @@ static char *interpret_move(const game_state *state, game_ui *ui,
ui->flash_laser = 0;
wouldflash = 2;
} else {
- ui->cur_visible = 1;
+ ui->cur_visible = true;
return UI_UPDATE;
}
/* Fix up 'button' for the below logic. */
@@ -974,7 +983,7 @@ static char *interpret_move(const game_state *state, game_ui *ui,
case REVEAL:
if (!CAN_REVEAL(state)) return nullret;
- if (ui->cur_visible == 1) ui->cur_x = ui->cur_y = 1;
+ if (ui->cur_visible) ui->cur_x = ui->cur_y = 1;
sprintf(buf, "R");
break;
@@ -1157,9 +1166,10 @@ static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
ds->w = state->w; ds->h = state->h;
ds->grid = snewn((state->w+2)*(state->h+2), unsigned int);
memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
- ds->started = ds->reveal = 0;
+ ds->started = false;
+ ds->reveal = false;
ds->flash_laserno = LASER_EMPTY;
- ds->isflash = 0;
+ ds->isflash = false;
return ds;
}
@@ -1182,7 +1192,7 @@ static void draw_square_cursor(drawing *dr, game_drawstate *ds, int dx, int dy)
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 ax, int ay, bool force, bool isflash)
{
int gx = ax+1, gy = ay+1;
int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
@@ -1264,10 +1274,11 @@ static void draw_arena_tile(drawing *dr, const game_state *gs,
static void draw_laser_tile(drawing *dr, const game_state *gs,
game_drawstate *ds, const game_ui *ui,
- int lno, int force)
+ int lno, bool force)
{
int gx, gy, dx, dy, unused;
- int wrong, omitted, reflect, hit, laserval, flash = 0, tmp;
+ int wrong, omitted, laserval;
+ bool tmp, reflect, hit, flash = false;
unsigned int gs_tile, ds_tile, exitno;
tmp = range2grid(gs, lno, &gx, &gy, &unused);
@@ -1291,7 +1302,7 @@ static void draw_laser_tile(drawing *dr, const game_state *gs,
if (exitno == ds->flash_laserno)
gs_tile |= LASER_FLASHED;
}
- if (gs_tile & LASER_FLASHED) flash = 1;
+ if (gs_tile & LASER_FLASHED) flash = true;
gs_tile |= wrong | omitted;
@@ -1339,7 +1350,8 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
int dir, const game_ui *ui,
float animtime, float flashtime)
{
- int i, x, y, ts = TILE_SIZE, isflash = 0, force = 0;
+ int i, x, y, ts = TILE_SIZE;
+ bool isflash = false, force = false;
if (flashtime > 0) {
int frame = (int)(flashtime / FLASH_FRAME);
@@ -1372,11 +1384,11 @@ static void game_redraw(drawing *dr, game_drawstate *ds,
draw_update(dr, 0, 0,
TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
- force = 1;
- ds->started = 1;
+ force = true;
+ ds->started = true;
}
- if (isflash != ds->isflash) force = 1;
+ if (isflash != ds->isflash) force = true;
/* draw the arena */
for (x = 0; x < state->w; x++) {