2 * blackbox.c: implementation of 'Black Box'.
14 #define PREFERRED_TILE_SIZE 32
15 #define FLASH_FRAME 0.2F
17 /* Terminology, for ease of reading various macros scattered about the place.
19 * The 'arena' is the inner area where the balls are placed. This is
20 * indexed from (0,0) to (w-1,h-1) but its offset in the grid is (1,1).
22 * The 'range' (firing range) is the bit around the edge where
23 * the lasers are fired from. This is indexed from 0 --> (2*(w+h) - 1),
24 * starting at the top left ((1,0) on the grid) and moving clockwise.
26 * The 'grid' is just the big array containing arena and range;
27 * locations (0,0), (0,w+1), (h+1,w+1) and (h+1,0) are unused.
31 COL_BACKGROUND, COL_COVER, COL_LOCK,
32 COL_TEXT, COL_FLASHTEXT,
33 COL_HIGHLIGHT, COL_LOWLIGHT, COL_GRID,
34 COL_BALL, COL_WRONG, COL_BUTTON,
35 COL_LASER, COL_DIMLASER,
41 int minballs, maxballs;
44 static game_params *default_params(void)
46 game_params *ret = snew(game_params);
49 ret->minballs = ret->maxballs = 5;
54 static const game_params blackbox_presets[] = {
62 static int game_fetch_preset(int i, char **name, game_params **params)
67 if (i < 0 || i >= lenof(blackbox_presets))
70 ret = snew(game_params);
71 *ret = blackbox_presets[i];
73 if (ret->minballs == ret->maxballs)
74 sprintf(str, "%dx%d, %d balls",
75 ret->w, ret->h, ret->minballs);
77 sprintf(str, "%dx%d, %d-%d balls",
78 ret->w, ret->h, ret->minballs, ret->maxballs);
85 static void free_params(game_params *params)
90 static game_params *dup_params(game_params *params)
92 game_params *ret = snew(game_params);
93 *ret = *params; /* structure copy */
97 static void decode_params(game_params *params, char const *string)
99 char const *p = string;
100 game_params *defs = default_params();
102 *params = *defs; free_params(defs);
108 while (*p && isdigit((unsigned char)*p)) p++;
113 while (*p && isdigit((unsigned char)*p)) p++;
117 params->minballs = atoi(p);
118 while (*p && isdigit((unsigned char)*p)) p++;
122 params->maxballs = atoi(p);
123 while (*p && isdigit((unsigned char)*p)) p++;
132 static char *encode_params(game_params *params, int full)
136 sprintf(str, "w%dh%dm%dM%d",
137 params->w, params->h, params->minballs, params->maxballs);
141 static config_item *game_configure(game_params *params)
146 ret = snewn(4, config_item);
148 ret[0].name = "Width";
149 ret[0].type = C_STRING;
150 sprintf(buf, "%d", params->w);
151 ret[0].sval = dupstr(buf);
154 ret[1].name = "Height";
155 ret[1].type = C_STRING;
156 sprintf(buf, "%d", params->h);
157 ret[1].sval = dupstr(buf);
160 ret[2].name = "No. of balls";
161 ret[2].type = C_STRING;
162 if (params->minballs == params->maxballs)
163 sprintf(buf, "%d", params->minballs);
165 sprintf(buf, "%d-%d", params->minballs, params->maxballs);
166 ret[2].sval = dupstr(buf);
177 static game_params *custom_params(config_item *cfg)
179 game_params *ret = snew(game_params);
181 ret->w = atoi(cfg[0].sval);
182 ret->h = atoi(cfg[1].sval);
184 /* Allow 'a-b' for a range, otherwise assume a single number. */
185 if (sscanf(cfg[2].sval, "%d-%d", &ret->minballs, &ret->maxballs) < 2)
186 ret->minballs = ret->maxballs = atoi(cfg[2].sval);
191 static char *validate_params(game_params *params, int full)
193 if (params->w < 2 || params->h < 2)
194 return "Grid must be at least 2 wide and 2 high";
195 /* next one is just for ease of coding stuff into 'char'
196 * types, and could be worked around if required. */
197 if (params->w > 255 || params->h > 255)
198 return "Grid must be < 255 in each direction";
199 if (params->minballs > params->maxballs)
200 return "Min. balls must be <= max. balls";
201 if (params->minballs >= params->w * params->h)
202 return "Too many balls for grid";
207 * We store: width | height | ball1x | ball1y | [ ball2x | ball2y | [...] ]
208 * all stored as unsigned chars; validate_params has already
209 * checked this won't overflow an 8-bit char.
210 * Then we obfuscate it.
213 static char *new_game_desc(game_params *params, random_state *rs,
214 char **aux, int interactive)
216 int nballs = params->minballs, i;
220 if (params->maxballs > params->minballs)
221 nballs += random_upto(rs, params->maxballs-params->minballs);
223 grid = snewn(params->w*params->h, char);
224 memset(grid, 0, params->w * params->h * sizeof(char));
226 bmp = snewn(nballs*2 + 2, unsigned char);
227 memset(bmp, 0, (nballs*2 + 2) * sizeof(unsigned char));
232 for (i = 0; i < nballs; i++) {
235 x = random_upto(rs, params->w);
236 y = random_upto(rs, params->h);
237 if (grid[y*params->h + x]) goto newball;
238 grid[y*params->h + x] = 1;
239 bmp[(i+1)*2 + 0] = x;
240 bmp[(i+1)*2 + 1] = y;
244 obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, FALSE);
245 ret = bin2hex(bmp, nballs*2 + 2);
251 static char *validate_desc(game_params *params, char *desc)
253 int nballs, dlen = strlen(desc), i;
257 /* the bitmap is 2+(nballs*2) long; the hex version is double that. */
258 nballs = ((dlen/2)-2)/2;
260 if (dlen < 4 || dlen % 4 ||
261 nballs < params->minballs || nballs > params->maxballs)
262 return "Game description is wrong length";
264 bmp = hex2bin(desc, nballs*2 + 2);
265 obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, TRUE);
266 ret = "Game description is corrupted";
267 /* check general grid size */
268 if (bmp[0] != params->w || bmp[1] != params->h)
270 /* check each ball will fit on that grid */
271 for (i = 0; i < nballs; i++) {
272 int x = bmp[(i+1)*2 + 0], y = bmp[(i+1)*2 + 1];
273 if (x < 0 || y < 0 || x > params->w || y > params->h)
283 #define BALL_CORRECT 0x01
284 #define BALL_GUESS 0x02
285 #define BALL_LOCK 0x04
287 #define LASER_FLAGMASK 0xf800
288 #define LASER_OMITTED 0x0800
289 #define LASER_REFLECT 0x1000
290 #define LASER_HIT 0x2000
291 #define LASER_WRONG 0x4000
292 #define LASER_FLASHED 0x8000
293 #define LASER_EMPTY (~0)
296 int w, h, minballs, maxballs, nballs, nlasers;
297 unsigned int *grid; /* (w+2)x(h+2), to allow for laser firing range */
298 unsigned int *exits; /* one per laser */
299 int done; /* user has finished placing his own balls. */
300 int laserno; /* number of next laser to be fired. */
301 int nguesses, reveal, nright, nwrong, nmissed;
304 #define GRID(s,x,y) ((s)->grid[(y)*((s)->h+2) + (x)])
306 /* specify numbers because they must match array indexes. */
307 enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 };
309 struct _off { int x, y; };
311 static const struct _off offsets[] = {
313 { 1, 0 }, /* right */
319 static const char *dirstrs[] = {
320 "UP", "RIGHT", "DOWN", "LEFT"
324 static int range2grid(game_state *state, int rangeno, int *x, int *y, int *direction)
329 if (rangeno < state->w) {
330 /* top row; from (1,0) to (w,0) */
333 *direction = DIR_DOWN;
337 if (rangeno < state->h) {
338 /* RHS; from (w+1, 1) to (w+1, h) */
341 *direction = DIR_LEFT;
345 if (rangeno < state->w) {
346 /* bottom row; from (1, h+1) to (w, h+1); counts backwards */
347 *x = (state->w - rangeno);
353 if (rangeno < state->h) {
354 /* LHS; from (0, 1) to (0, h); counts backwards */
356 *y = (state->h - rangeno);
357 *direction = DIR_RIGHT;
363 static int grid2range(game_state *state, int x, int y, int *rangeno)
365 int ret, x1 = state->w+1, y1 = state->h+1;
367 if (x > 0 && x < x1 && y > 0 && y < y1) return 0; /* in arena */
368 if (x < 0 || x > y1 || y < 0 || y > y1) return 0; /* outside grid */
370 if ((x == 0 || x == x1) && (y == 0 || y == y1))
371 return 0; /* one of 4 corners */
373 if (y == 0) { /* top line */
375 } else if (x == x1) { /* RHS */
376 ret = y - 1 + state->w;
377 } else if (y == y1) { /* Bottom [and counts backwards] */
378 ret = (state->w - x) + state->w + state->h;
379 } else { /* LHS [and counts backwards ] */
380 ret = (state->h-y) + state->w + state->w + state->h;
383 debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret));
387 static game_state *new_game(midend_data *me, game_params *params, char *desc)
389 game_state *state = snew(game_state);
390 int dlen = strlen(desc), i;
393 state->minballs = params->minballs;
394 state->maxballs = params->maxballs;
395 state->nballs = ((dlen/2)-2)/2;
397 bmp = hex2bin(desc, state->nballs*2 + 2);
398 obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, TRUE);
400 state->w = bmp[0]; state->h = bmp[1];
401 state->nlasers = 2 * (state->w + state->h);
403 state->grid = snewn((state->w+2)*(state->h+2), unsigned int);
404 memset(state->grid, 0, (state->w+2)*(state->h+2) * sizeof(unsigned int));
406 state->exits = snewn(state->nlasers, unsigned int);
407 memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int));
409 for (i = 0; i < state->nballs; i++) {
410 GRID(state, bmp[(i+1)*2 + 0]+1, bmp[(i+1)*2 + 1]+1) = BALL_CORRECT;
414 state->done = state->nguesses = state->reveal =
415 state->nright = state->nwrong = state->nmissed = 0;
421 #define XFER(x) ret->x = state->x
423 static game_state *dup_game(game_state *state)
425 game_state *ret = snew(game_state);
428 XFER(minballs); XFER(maxballs);
429 XFER(nballs); XFER(nlasers);
431 ret->grid = snewn((ret->w+2)*(ret->h+2), unsigned int);
432 memcpy(ret->grid, state->grid, (ret->w+2)*(ret->h+2) * sizeof(unsigned int));
433 ret->exits = snewn(ret->nlasers, unsigned int);
434 memcpy(ret->exits, state->exits, ret->nlasers * sizeof(unsigned int));
440 XFER(nright); XFER(nwrong); XFER(nmissed);
447 static void free_game(game_state *state)
454 static char *solve_game(game_state *state, game_state *currstate,
455 char *aux, char **error)
460 static char *game_text_format(game_state *state)
469 static game_ui *new_ui(game_state *state)
471 game_ui *ui = snew(struct game_ui);
472 ui->flash_laserno = LASER_EMPTY;
476 static void free_ui(game_ui *ui)
481 static char *encode_ui(game_ui *ui)
486 static void decode_ui(game_ui *ui, char *encoding)
490 static void game_changed_state(game_ui *ui, game_state *oldstate,
491 game_state *newstate)
495 #define OFFSET(gx,gy,o) do { \
496 int off = (o); while (off < 0) { off += 4; } off %= 4; \
497 (gx) += offsets[off].x; \
498 (gy) += offsets[off].y; \
501 enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT };
503 /* Given a position and a direction, check whether we can see a ball in front
504 * of us, or to our front-left or front-right. */
505 static int isball(game_state *state, int gx, int gy, int direction, int lookwhere)
507 debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction],
508 lookwhere == LOOK_LEFT ? "LEFT" :
509 lookwhere == LOOK_FORWARD ? "FORWARD" : "RIGHT"));
510 OFFSET(gx,gy,direction);
511 if (lookwhere == LOOK_LEFT)
512 OFFSET(gx,gy,direction-1);
513 else if (lookwhere == LOOK_RIGHT)
514 OFFSET(gx,gy,direction+1);
515 else if (lookwhere != LOOK_FORWARD)
516 assert(!"unknown lookwhere");
518 debug(("isball, new (%d, %d)\n", gx, gy));
520 /* if we're off the grid (into the firing range) there's never a ball. */
521 if (gx < 1 || gy < 1 || gx > state->h || gy > state->w)
524 if (GRID(state, gx,gy) & BALL_CORRECT)
530 static void fire_laser(game_state *state, int x, int y, int direction)
532 int xstart = x, ystart = y, unused, lno;
534 assert(grid2range(state, x, y, &lno));
536 /* deal with strange initial reflection rules (that stop
537 * you turning down the laser range) */
539 /* I've just chosen to prioritise instant-hit over instant-reflection;
540 * I can't find anywhere that gives me a definite algorithm for this. */
541 if (isball(state, x, y, direction, LOOK_FORWARD)) {
542 debug(("Instant hit at (%d, %d)\n", x, y));
543 GRID(state, x, y) = LASER_HIT;
544 state->exits[lno] = LASER_HIT;
548 if (isball(state, x, y, direction, LOOK_LEFT) ||
549 isball(state, x, y, direction, LOOK_RIGHT)) {
550 debug(("Instant reflection at (%d, %d)\n", x, y));
551 GRID(state, x, y) = LASER_REFLECT;
552 state->exits[lno] = LASER_REFLECT;
555 /* move us onto the grid. */
556 OFFSET(x, y, direction);
559 debug(("fire_laser: looping at (%d, %d) pointing %s\n",
560 x, y, dirstrs[direction]));
561 if (grid2range(state, x, y, &unused)) {
562 int newno = state->laserno++, exitno;
563 debug(("Back on range; (%d, %d) --> (%d, %d)\n",
564 xstart, ystart, x, y));
565 /* We're back out of the grid; the move is complete. */
566 if (xstart == x && ystart == y) {
567 GRID(state, x, y) = LASER_REFLECT;
568 state->exits[lno] = LASER_REFLECT;
570 /* it wasn't a reflection */
571 GRID(state, xstart, ystart) = newno;
572 GRID(state, x, y) = newno;
574 assert(grid2range(state, x, y, &exitno));
575 state->exits[lno] = exitno;
576 state->exits[exitno] = lno;
580 /* paranoia. This obviously should never happen */
581 assert(!(GRID(state, x, y) & BALL_CORRECT));
583 if (isball(state, x, y, direction, LOOK_FORWARD)) {
584 /* we're facing a ball; send back a reflection. */
585 GRID(state, xstart, ystart) = LASER_HIT;
586 state->exits[lno] = LASER_HIT;
587 debug(("Ball ahead of (%d, %d); HIT at (%d, %d), new grid 0x%x\n",
588 x, y, xstart, ystart, GRID(state, xstart, ystart)));
592 if (isball(state, x, y, direction, LOOK_LEFT)) {
593 /* ball to our left; rotate clockwise and look again. */
594 debug(("Ball to left; turning clockwise.\n"));
595 direction += 1; direction %= 4;
598 if (isball(state, x, y, direction, LOOK_RIGHT)) {
599 /* ball to our right; rotate anti-clockwise and look again. */
600 debug(("Ball to rightl turning anti-clockwise.\n"));
601 direction += 3; direction %= 4;
604 /* ... otherwise, we have no balls ahead of us so just move one step. */
605 debug(("No balls; moving forwards.\n"));
606 OFFSET(x, y, direction);
610 /* Checks that the guessed balls in the state match up with the real balls
611 * for all possible lasers (i.e. not just the ones that the player might
612 * have already guessed). This is required because any layout with >4 balls
613 * might have multiple valid solutions. Returns non-zero for a 'correct'
614 * (i.e. consistent) layout. */
615 static int check_guesses(game_state *state)
617 game_state *solution, *guesses;
618 int i, x, y, dir, unused;
621 /* duplicate the state (to solution) */
622 solution = dup_game(state);
624 /* clear out the lasers of solution */
625 for (i = 0; i < solution->nlasers; i++) {
626 assert(range2grid(solution, i, &x, &y, &unused));
627 GRID(solution, x, y) = 0;
628 solution->exits[i] = LASER_EMPTY;
631 /* duplicate solution to guess. */
632 guesses = dup_game(solution);
634 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
635 for (x = 1; x <= state->w; x++) {
636 for (y = 1; y <= state->h; y++) {
637 GRID(guesses, x, y) &= ~BALL_CORRECT;
638 if (GRID(guesses, x, y) & BALL_GUESS)
639 GRID(guesses, x, y) |= BALL_CORRECT;
643 /* for each laser (on both game_states), fire it if it hasn't been fired.
644 * If one has been fired (or received a hit) and another hasn't, we know
645 * the ball layouts didn't match and can short-circuit return. */
646 for (i = 0; i < solution->nlasers; i++) {
647 assert(range2grid(solution, i, &x, &y, &dir));
648 if (solution->exits[i] == LASER_EMPTY)
649 fire_laser(solution, x, y, dir);
650 if (guesses->exits[i] == LASER_EMPTY)
651 fire_laser(guesses, x, y, dir);
654 /* check each game_state's laser against the other; if any differ, return 0 */
656 for (i = 0; i < solution->nlasers; i++) {
657 assert(range2grid(solution, i, &x, &y, &unused));
659 if (solution->exits[i] != guesses->exits[i]) {
660 /* If the original state didn't have this shot fired,
661 * and it would be wrong between the guess and the solution,
663 if (state->exits[i] == LASER_EMPTY) {
664 state->exits[i] = solution->exits[i];
665 if (state->exits[i] == LASER_REFLECT ||
666 state->exits[i] == LASER_HIT)
667 GRID(state, x, y) = state->exits[i];
669 /* add a new shot, incrementing state's laser count. */
670 int ex, ey, newno = state->laserno++;
671 assert(range2grid(state, state->exits[i], &ex, &ey, &unused));
672 GRID(state, x, y) = newno;
673 GRID(state, ex, ey) = newno;
675 state->exits[i] |= LASER_OMITTED;
677 state->exits[i] |= LASER_WRONG;
682 if (ret == 0) goto done;
684 /* fix up original state so the 'correct' balls end up matching the guesses,
685 * as we've just proved that they were equivalent. */
686 for (x = 1; x <= state->w; x++) {
687 for (y = 1; y <= state->h; y++) {
688 if (GRID(state, x, y) & BALL_GUESS)
689 GRID(state, x, y) |= BALL_CORRECT;
691 GRID(state, x, y) &= ~BALL_CORRECT;
696 /* fill in nright and nwrong. */
697 state->nright = state->nwrong = state->nmissed = 0;
698 for (x = 1; x <= state->w; x++) {
699 for (y = 1; y <= state->h; y++) {
700 int bs = GRID(state, x, y) & (BALL_GUESS | BALL_CORRECT);
701 if (bs == (BALL_GUESS | BALL_CORRECT))
703 else if (bs == BALL_GUESS)
705 else if (bs == BALL_CORRECT)
714 #define TILE_SIZE (ds->tilesize)
716 #define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
717 #define FROMDRAW(x) (((x) - (TILE_SIZE / 2)) / TILE_SIZE)
719 struct game_drawstate {
720 int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
721 unsigned int *grid; /* as the game_state grid */
722 int started, canreveal, reveal;
726 static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
727 int x, int y, int button)
729 int gx = -1, gy = -1, rangeno = -1;
730 enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
731 TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
732 char buf[80], *nullret = NULL;
734 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
737 if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
739 if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
740 if (button == LEFT_BUTTON) {
741 if (!(GRID(state, gx,gy) & BALL_LOCK))
742 action = TOGGLE_BALL;
744 action = TOGGLE_LOCK;
746 if (grid2range(state, gx, gy, &rangeno)) {
747 if (button == LEFT_BUTTON)
749 else if (gy == 0 || gy > state->h)
750 action = TOGGLE_COLUMN_LOCK; /* and use gx */
752 action = TOGGLE_ROW_LOCK; /* and use gy */
754 } else if (button == LEFT_RELEASE) {
755 ui->flash_laserno = LASER_EMPTY;
761 sprintf(buf, "T%d,%d", gx, gy);
765 sprintf(buf, "LB%d,%d", gx, gy);
768 case TOGGLE_COLUMN_LOCK:
769 sprintf(buf, "LC%d", gx);
772 case TOGGLE_ROW_LOCK:
773 sprintf(buf, "LR%d", gy);
777 if (state->reveal && state->exits[rangeno] == LASER_EMPTY)
779 ui->flash_laserno = rangeno;
781 if (state->exits[rangeno] != LASER_EMPTY)
783 sprintf(buf, "F%d", rangeno);
787 if (!ds->canreveal) return nullret;
794 if (state->reveal) return nullret;
798 static game_state *execute_move(game_state *from, char *move)
800 game_state *ret = dup_game(from);
801 int gx = -1, gy = -1, rangeno = -1, direction;
803 if (!strcmp(move, "S")) {
808 if (from->reveal) goto badmove;
809 if (strlen(move) < 1) goto badmove;
813 sscanf(move+1, "%d,%d", &gx, &gy);
814 if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
816 if (GRID(ret, gx, gy) & BALL_GUESS) {
818 GRID(ret, gx, gy) &= ~BALL_GUESS;
821 GRID(ret, gx, gy) |= BALL_GUESS;
826 sscanf(move+1, "%d", &rangeno);
827 if (ret->exits[rangeno] != LASER_EMPTY)
829 if (!range2grid(ret, rangeno, &gx, &gy, &direction))
831 fire_laser(ret, gx, gy, direction);
835 if (ret->nguesses < ret->minballs ||
836 ret->nguesses > ret->maxballs)
845 if (strlen(move) < 2) goto badmove;
848 sscanf(move+2, "%d,%d", &gx, &gy);
849 if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
851 GRID(ret, gx, gy) ^= BALL_LOCK;
854 #define COUNTLOCK do { if (GRID(ret, gx, gy) & BALL_LOCK) lcount++; } while (0)
855 #define SETLOCKIF(c) do { \
856 if (lcount > (c)) GRID(ret, gx, gy) &= ~BALL_LOCK; \
857 else GRID(ret, gx, gy) |= BALL_LOCK; \
861 sscanf(move+2, "%d", &gx);
862 if (gx < 1 || gx > ret->w) goto badmove;
863 for (gy = 1; gy <= ret->h; gy++) { COUNTLOCK; }
864 for (gy = 1; gy <= ret->h; gy++) { SETLOCKIF(ret->h/2); }
868 sscanf(move+2, "%d", &gy);
869 if (gy < 1 || gy > ret->h) goto badmove;
870 for (gx = 1; gx <= ret->w; gx++) { COUNTLOCK; }
871 for (gx = 1; gx <= ret->w; gx++) { SETLOCKIF(ret->w/2); }
894 /* ----------------------------------------------------------------------
898 static void game_compute_size(game_params *params, int tilesize,
901 /* Border is ts/2, to make things easier.
902 * Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles
903 * across, and similarly height + 2 + 1 tiles down. */
904 *x = (params->w + 3) * tilesize;
905 *y = (params->h + 3) * tilesize;
908 static void game_set_size(game_drawstate *ds, game_params *params,
911 ds->tilesize = tilesize;
912 ds->crad = (tilesize-1)/2;
913 ds->rrad = (3*tilesize)/8;
916 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
918 float *ret = snewn(3 * NCOLOURS, float);
921 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
923 ret[COL_BALL * 3 + 0] = 0.0F;
924 ret[COL_BALL * 3 + 1] = 0.0F;
925 ret[COL_BALL * 3 + 2] = 0.0F;
927 ret[COL_WRONG * 3 + 0] = 1.0F;
928 ret[COL_WRONG * 3 + 1] = 0.0F;
929 ret[COL_WRONG * 3 + 2] = 0.0F;
931 ret[COL_BUTTON * 3 + 0] = 0.0F;
932 ret[COL_BUTTON * 3 + 1] = 1.0F;
933 ret[COL_BUTTON * 3 + 2] = 0.0F;
935 ret[COL_LASER * 3 + 0] = 1.0F;
936 ret[COL_LASER * 3 + 1] = 0.0F;
937 ret[COL_LASER * 3 + 2] = 0.0F;
939 ret[COL_DIMLASER * 3 + 0] = 0.5F;
940 ret[COL_DIMLASER * 3 + 1] = 0.0F;
941 ret[COL_DIMLASER * 3 + 2] = 0.0F;
943 for (i = 0; i < 3; i++) {
944 ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
945 ret[COL_LOCK * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.7F;
946 ret[COL_COVER * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.5F;
947 ret[COL_TEXT * 3 + i] = 0.0F;
950 ret[COL_FLASHTEXT * 3 + 0] = 0.0F;
951 ret[COL_FLASHTEXT * 3 + 1] = 1.0F;
952 ret[COL_FLASHTEXT * 3 + 2] = 0.0F;
954 *ncolours = NCOLOURS;
958 static game_drawstate *game_new_drawstate(game_state *state)
960 struct game_drawstate *ds = snew(struct game_drawstate);
963 ds->w = state->w; ds->h = state->h;
964 ds->grid = snewn((state->w+2)*(state->h+2), unsigned int);
965 memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
967 ds->flash_laserno = LASER_EMPTY;
972 static void game_free_drawstate(game_drawstate *ds)
978 static void draw_arena_tile(frontend *fe, game_state *gs, game_drawstate *ds,
979 int ax, int ay, int force, int isflash)
981 int gx = ax+1, gy = ay+1;
982 int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
983 int dx = TODRAW(gx), dy = TODRAW(gy);
985 if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) {
988 bg = (gs_tile & BALL_LOCK) ? COL_LOCK :
989 gs->reveal ? COL_BACKGROUND : COL_COVER;
991 draw_rect(fe, dx, dy, TILE_SIZE, TILE_SIZE, bg);
992 draw_rect_outline(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
995 /* Guessed balls are always black; if they're incorrect they'll
996 * have a red cross added later.
997 * Missing balls are red. */
998 if (gs_tile & BALL_GUESS) {
999 bcol = isflash ? bg : COL_BALL;
1000 } else if (gs_tile & BALL_CORRECT) {
1001 bcol = isflash ? bg : COL_WRONG;
1006 /* guesses are black/black, all else background. */
1007 if (gs_tile & BALL_GUESS) {
1014 draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
1018 (gs_tile & BALL_GUESS) &&
1019 !(gs_tile & BALL_CORRECT)) {
1020 int x1 = dx + 3, y1 = dy + 3;
1021 int x2 = dx + TILE_SIZE - 3, y2 = dy + TILE_SIZE-3;
1024 /* Incorrect guess; draw a red cross over the ball. */
1033 draw_polygon(fe, coords, 4, COL_WRONG, COL_WRONG);
1042 draw_polygon(fe, coords, 4, COL_WRONG, COL_WRONG);
1044 draw_update(fe, dx, dy, TILE_SIZE, TILE_SIZE);
1046 GRID(ds,gx,gy) = gs_tile;
1049 static void draw_laser_tile(frontend *fe, game_state *gs, game_drawstate *ds,
1050 game_ui *ui, int lno, int force)
1052 int gx, gy, dx, dy, unused;
1053 int wrong, omitted, reflect, hit, laserval, flash = 0;
1054 unsigned int gs_tile, ds_tile, exitno;
1056 assert(range2grid(gs, lno, &gx, &gy, &unused));
1057 gs_tile = GRID(gs, gx, gy);
1058 ds_tile = GRID(ds, gx, gy);
1062 wrong = gs->exits[lno] & LASER_WRONG;
1063 omitted = gs->exits[lno] & LASER_OMITTED;
1064 exitno = gs->exits[lno] & ~LASER_FLAGMASK;
1066 reflect = gs_tile & LASER_REFLECT;
1067 hit = gs_tile & LASER_HIT;
1068 laserval = gs_tile & ~LASER_FLAGMASK;
1070 if (lno == ui->flash_laserno)
1071 gs_tile |= LASER_FLASHED;
1072 else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) {
1073 if (exitno == ui->flash_laserno)
1074 gs_tile |= LASER_FLASHED;
1076 if (gs_tile & LASER_FLASHED) flash = 1;
1078 gs_tile |= wrong | omitted;
1080 if (gs_tile != ds_tile || force) {
1081 draw_rect(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
1082 draw_rect_outline(fe, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
1084 if (gs_tile &~ (LASER_WRONG | LASER_OMITTED)) {
1086 int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT;
1089 sprintf(str, "%s", reflect ? "R" : "H");
1091 sprintf(str, "%d", laserval);
1094 draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
1096 COL_WRONG, COL_WRONG);
1097 draw_circle(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
1098 ds->rrad - TILE_SIZE/16,
1099 COL_BACKGROUND, COL_WRONG);
1102 draw_text(fe, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
1103 FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
1106 draw_update(fe, dx, dy, TILE_SIZE, TILE_SIZE);
1108 GRID(ds, gx, gy) = gs_tile;
1112 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1113 game_state *state, int dir, game_ui *ui,
1114 float animtime, float flashtime)
1116 int i, x, y, ts = TILE_SIZE, isflash = 0, force = 0;
1118 if (flashtime > 0) {
1119 int frame = (int)(flashtime / FLASH_FRAME);
1120 isflash = (frame % 2) == 0;
1122 debug(("game_redraw: flashtime = %f", flashtime));
1126 int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1;
1127 int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2);
1130 TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3),
1133 /* clockwise around the outline starting at pt behind (1,1). */
1134 draw_line(fe, x0+ts, y0+ts, x0+ts, y0, COL_HIGHLIGHT);
1135 draw_line(fe, x0+ts, y0, x1-ts, y0, COL_HIGHLIGHT);
1136 draw_line(fe, x1-ts, y0, x1-ts, y0+ts, COL_LOWLIGHT);
1137 draw_line(fe, x1-ts, y0+ts, x1, y0+ts, COL_HIGHLIGHT);
1138 draw_line(fe, x1, y0+ts, x1, y1-ts, COL_LOWLIGHT);
1139 draw_line(fe, x1, y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
1140 draw_line(fe, x1-ts, y1-ts, x1-ts, y1, COL_LOWLIGHT);
1141 draw_line(fe, x1-ts, y1, x0+ts, y1, COL_LOWLIGHT);
1142 draw_line(fe, x0+ts, y1, x0+ts, y1-ts, COL_HIGHLIGHT);
1143 draw_line(fe, x0+ts, y1-ts, x0, y1-ts, COL_LOWLIGHT);
1144 draw_line(fe, x0, y1-ts, x0, y0+ts, COL_HIGHLIGHT);
1145 draw_line(fe, x0, y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
1148 draw_update(fe, 0, 0,
1149 TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
1154 /* draw the arena */
1155 for (x = 0; x < state->w; x++) {
1156 for (y = 0; y < state->h; y++) {
1157 draw_arena_tile(fe, state, ds, x, y, force, isflash);
1161 /* draw the lasers */
1162 for (i = 0; i < 2*(state->w+state->h); i++) {
1163 draw_laser_tile(fe, state, ds, ui, i, force);
1166 /* draw the 'finish' button */
1167 if (state->nguesses >= state->minballs &&
1168 state->nguesses <= state->maxballs &&
1170 clip(fe, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
1171 draw_circle(fe, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
1172 COL_BUTTON, COL_BALL);
1176 draw_rect(fe, TODRAW(0), TODRAW(0),
1177 TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND);
1180 draw_update(fe, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
1181 ds->reveal = state->reveal;
1182 ds->flash_laserno = ui->flash_laserno;
1188 if (state->nwrong == 0 &&
1189 state->nmissed == 0 &&
1190 state->nright >= state->minballs)
1191 sprintf(buf, "CORRECT!");
1193 sprintf(buf, "%d wrong and %d missed balls.",
1194 state->nwrong, state->nmissed);
1196 if (state->nguesses > state->maxballs)
1197 sprintf(buf, "%d too many balls marked.",
1198 state->nguesses - state->maxballs);
1199 else if (state->nguesses <= state->maxballs &&
1200 state->nguesses >= state->minballs)
1201 sprintf(buf, "Click button to verify guesses.");
1202 else if (state->maxballs == state->minballs)
1203 sprintf(buf, "Balls marked: %d / %d",
1204 state->nguesses, state->minballs);
1206 sprintf(buf, "Balls marked: %d / %d-%d.",
1207 state->nguesses, state->minballs, state->maxballs);
1209 status_bar(fe, buf);
1213 static float game_anim_length(game_state *oldstate, game_state *newstate,
1214 int dir, game_ui *ui)
1219 static float game_flash_length(game_state *oldstate, game_state *newstate,
1220 int dir, game_ui *ui)
1222 if (!oldstate->reveal && newstate->reveal)
1223 return 4.0F * FLASH_FRAME;
1228 static int game_wants_statusbar(void)
1233 static int game_timing_state(game_state *state, game_ui *ui)
1239 #define thegame blackbox
1242 const struct game thegame = {
1243 "Black Box", "games.blackbox",
1250 TRUE, game_configure, custom_params,
1258 FALSE, game_text_format,
1266 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1269 game_free_drawstate,
1273 game_wants_statusbar,
1274 FALSE, game_timing_state,
1275 0, /* mouse_priorities */
1278 /* vim: set shiftwidth=4 tabstop=8: */