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 "Width and height must both be at least two";
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 "Widths and heights greater than 255 are not supported";
199 if (params
->minballs
> params
->maxballs
)
200 return "Minimum number of balls may not be greater than maximum";
201 if (params
->minballs
>= params
->w
* params
->h
)
202 return "Too many balls to fit in 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
+ 1);
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
++) {
236 x
= random_upto(rs
, params
->w
);
237 y
= random_upto(rs
, params
->h
);
238 } while (grid
[y
*params
->w
+ x
]);
240 grid
[y
*params
->w
+ x
] = 1;
242 bmp
[(i
+1)*2 + 0] = x
;
243 bmp
[(i
+1)*2 + 1] = y
;
247 obfuscate_bitmap(bmp
, (nballs
*2 + 2) * 8, FALSE
);
248 ret
= bin2hex(bmp
, nballs
*2 + 2);
254 static char *validate_desc(game_params
*params
, char *desc
)
256 int nballs
, dlen
= strlen(desc
), i
;
260 /* the bitmap is 2+(nballs*2) long; the hex version is double that. */
261 nballs
= ((dlen
/2)-2)/2;
263 if (dlen
< 4 || dlen
% 4 ||
264 nballs
< params
->minballs
|| nballs
> params
->maxballs
)
265 return "Game description is wrong length";
267 bmp
= hex2bin(desc
, nballs
*2 + 2);
268 obfuscate_bitmap(bmp
, (nballs
*2 + 2) * 8, TRUE
);
269 ret
= "Game description is corrupted";
270 /* check general grid size */
271 if (bmp
[0] != params
->w
|| bmp
[1] != params
->h
)
273 /* check each ball will fit on that grid */
274 for (i
= 0; i
< nballs
; i
++) {
275 int x
= bmp
[(i
+1)*2 + 0], y
= bmp
[(i
+1)*2 + 1];
276 if (x
< 0 || y
< 0 || x
>= params
->w
|| y
>= params
->h
)
286 #define BALL_CORRECT 0x01
287 #define BALL_GUESS 0x02
288 #define BALL_LOCK 0x04
290 #define LASER_FLAGMASK 0xf800
291 #define LASER_OMITTED 0x0800
292 #define LASER_REFLECT 0x1000
293 #define LASER_HIT 0x2000
294 #define LASER_WRONG 0x4000
295 #define LASER_FLASHED 0x8000
296 #define LASER_EMPTY (~0)
299 int w
, h
, minballs
, maxballs
, nballs
, nlasers
;
300 unsigned int *grid
; /* (w+2)x(h+2), to allow for laser firing range */
301 unsigned int *exits
; /* one per laser */
302 int done
; /* user has finished placing his own balls. */
303 int laserno
; /* number of next laser to be fired. */
304 int nguesses
, reveal
, justwrong
, nright
, nwrong
, nmissed
;
307 #define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
309 #define RANGECHECK(s,x) ((x) >= 0 && (x) <= (s)->nlasers)
311 /* specify numbers because they must match array indexes. */
312 enum { DIR_UP
= 0, DIR_RIGHT
= 1, DIR_DOWN
= 2, DIR_LEFT
= 3 };
314 struct offset
{ int x
, y
; };
316 static const struct offset offsets
[] = {
318 { 1, 0 }, /* right */
324 static const char *dirstrs
[] = {
325 "UP", "RIGHT", "DOWN", "LEFT"
329 static int range2grid(game_state
*state
, int rangeno
, int *x
, int *y
, int *direction
)
334 if (rangeno
< state
->w
) {
335 /* top row; from (1,0) to (w,0) */
338 *direction
= DIR_DOWN
;
342 if (rangeno
< state
->h
) {
343 /* RHS; from (w+1, 1) to (w+1, h) */
346 *direction
= DIR_LEFT
;
350 if (rangeno
< state
->w
) {
351 /* bottom row; from (1, h+1) to (w, h+1); counts backwards */
352 *x
= (state
->w
- rangeno
);
358 if (rangeno
< state
->h
) {
359 /* LHS; from (0, 1) to (0, h); counts backwards */
361 *y
= (state
->h
- rangeno
);
362 *direction
= DIR_RIGHT
;
368 static int grid2range(game_state
*state
, int x
, int y
, int *rangeno
)
370 int ret
, x1
= state
->w
+1, y1
= state
->h
+1;
372 if (x
> 0 && x
< x1
&& y
> 0 && y
< y1
) return 0; /* in arena */
373 if (x
< 0 || x
> y1
|| y
< 0 || y
> y1
) return 0; /* outside grid */
375 if ((x
== 0 || x
== x1
) && (y
== 0 || y
== y1
))
376 return 0; /* one of 4 corners */
378 if (y
== 0) { /* top line */
380 } else if (x
== x1
) { /* RHS */
381 ret
= y
- 1 + state
->w
;
382 } else if (y
== y1
) { /* Bottom [and counts backwards] */
383 ret
= (state
->w
- x
) + state
->w
+ state
->h
;
384 } else { /* LHS [and counts backwards ] */
385 ret
= (state
->h
-y
) + state
->w
+ state
->w
+ state
->h
;
388 debug(("grid2range: (%d,%d) rangeno = %d\n", x
, y
, ret
));
392 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
394 game_state
*state
= snew(game_state
);
395 int dlen
= strlen(desc
), i
;
398 state
->minballs
= params
->minballs
;
399 state
->maxballs
= params
->maxballs
;
400 state
->nballs
= ((dlen
/2)-2)/2;
402 bmp
= hex2bin(desc
, state
->nballs
*2 + 2);
403 obfuscate_bitmap(bmp
, (state
->nballs
*2 + 2) * 8, TRUE
);
405 state
->w
= bmp
[0]; state
->h
= bmp
[1];
406 state
->nlasers
= 2 * (state
->w
+ state
->h
);
408 state
->grid
= snewn((state
->w
+2)*(state
->h
+2), unsigned int);
409 memset(state
->grid
, 0, (state
->w
+2)*(state
->h
+2) * sizeof(unsigned int));
411 state
->exits
= snewn(state
->nlasers
, unsigned int);
412 memset(state
->exits
, LASER_EMPTY
, state
->nlasers
* sizeof(unsigned int));
414 for (i
= 0; i
< state
->nballs
; i
++) {
415 GRID(state
, bmp
[(i
+1)*2 + 0]+1, bmp
[(i
+1)*2 + 1]+1) = BALL_CORRECT
;
419 state
->done
= state
->nguesses
= state
->reveal
= state
->justwrong
=
420 state
->nright
= state
->nwrong
= state
->nmissed
= 0;
426 #define XFER(x) ret->x = state->x
428 static game_state
*dup_game(game_state
*state
)
430 game_state
*ret
= snew(game_state
);
433 XFER(minballs
); XFER(maxballs
);
434 XFER(nballs
); XFER(nlasers
);
436 ret
->grid
= snewn((ret
->w
+2)*(ret
->h
+2), unsigned int);
437 memcpy(ret
->grid
, state
->grid
, (ret
->w
+2)*(ret
->h
+2) * sizeof(unsigned int));
438 ret
->exits
= snewn(ret
->nlasers
, unsigned int);
439 memcpy(ret
->exits
, state
->exits
, ret
->nlasers
* sizeof(unsigned int));
446 XFER(nright
); XFER(nwrong
); XFER(nmissed
);
453 static void free_game(game_state
*state
)
460 static char *solve_game(game_state
*state
, game_state
*currstate
,
461 char *aux
, char **error
)
466 static char *game_text_format(game_state
*state
)
476 static game_ui
*new_ui(game_state
*state
)
478 game_ui
*ui
= snew(struct game_ui
);
479 ui
->flash_laserno
= LASER_EMPTY
;
485 static void free_ui(game_ui
*ui
)
490 static char *encode_ui(game_ui
*ui
)
494 * The error counter needs preserving across a serialisation.
496 sprintf(buf
, "E%d", ui
->errors
);
500 static void decode_ui(game_ui
*ui
, char *encoding
)
502 sscanf(encoding
, "E%d", &ui
->errors
);
505 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
506 game_state
*newstate
)
509 * If we've encountered a `justwrong' state as a result of
510 * actually making a move, increment the ui error counter.
512 if (newstate
->justwrong
&& ui
->newmove
)
517 #define OFFSET(gx,gy,o) do { \
518 int off = (4 + (o) % 4) % 4; \
519 (gx) += offsets[off].x; \
520 (gy) += offsets[off].y; \
523 enum { LOOK_LEFT
, LOOK_FORWARD
, LOOK_RIGHT
};
525 /* Given a position and a direction, check whether we can see a ball in front
526 * of us, or to our front-left or front-right. */
527 static int isball(game_state
*state
, int gx
, int gy
, int direction
, int lookwhere
)
529 debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx
, gy
, dirstrs
[direction
],
530 lookwhere
== LOOK_LEFT ?
"LEFT" :
531 lookwhere
== LOOK_FORWARD ?
"FORWARD" : "RIGHT"));
532 OFFSET(gx
,gy
,direction
);
533 if (lookwhere
== LOOK_LEFT
)
534 OFFSET(gx
,gy
,direction
-1);
535 else if (lookwhere
== LOOK_RIGHT
)
536 OFFSET(gx
,gy
,direction
+1);
537 else if (lookwhere
!= LOOK_FORWARD
)
538 assert(!"unknown lookwhere");
540 debug(("isball, new (%d, %d)\n", gx
, gy
));
542 /* if we're off the grid (into the firing range) there's never a ball. */
543 if (gx
< 1 || gy
< 1 || gx
> state
->h
|| gy
> state
->w
)
546 if (GRID(state
, gx
,gy
) & BALL_CORRECT
)
552 static int fire_laser_internal(game_state
*state
, int x
, int y
, int direction
)
554 int unused
, lno
, tmp
;
556 tmp
= grid2range(state
, x
, y
, &lno
);
559 /* deal with strange initial reflection rules (that stop
560 * you turning down the laser range) */
562 /* I've just chosen to prioritise instant-hit over instant-reflection;
563 * I can't find anywhere that gives me a definite algorithm for this. */
564 if (isball(state
, x
, y
, direction
, LOOK_FORWARD
)) {
565 debug(("Instant hit at (%d, %d)\n", x
, y
));
566 return LASER_HIT
; /* hit */
569 if (isball(state
, x
, y
, direction
, LOOK_LEFT
) ||
570 isball(state
, x
, y
, direction
, LOOK_RIGHT
)) {
571 debug(("Instant reflection at (%d, %d)\n", x
, y
));
572 return LASER_REFLECT
; /* reflection */
574 /* move us onto the grid. */
575 OFFSET(x
, y
, direction
);
578 debug(("fire_laser: looping at (%d, %d) pointing %s\n",
579 x
, y
, dirstrs
[direction
]));
580 if (grid2range(state
, x
, y
, &unused
)) {
583 tmp
= grid2range(state
, x
, y
, &exitno
);
586 return (lno
== exitno ? LASER_REFLECT
: exitno
);
588 /* paranoia. This obviously should never happen */
589 assert(!(GRID(state
, x
, y
) & BALL_CORRECT
));
591 if (isball(state
, x
, y
, direction
, LOOK_FORWARD
)) {
592 /* we're facing a ball; send back a reflection. */
593 debug(("Ball ahead of (%d, %d); HIT at (%d, %d), new grid 0x%x\n",
594 x
, y
, xstart
, ystart
, GRID(state
, xstart
, ystart
)));
595 return LASER_HIT
; /* hit */
598 if (isball(state
, x
, y
, direction
, LOOK_LEFT
)) {
599 /* ball to our left; rotate clockwise and look again. */
600 debug(("Ball to left; turning clockwise.\n"));
601 direction
+= 1; direction
%= 4;
604 if (isball(state
, x
, y
, direction
, LOOK_RIGHT
)) {
605 /* ball to our right; rotate anti-clockwise and look again. */
606 debug(("Ball to rightl turning anti-clockwise.\n"));
607 direction
+= 3; direction
%= 4;
610 /* ... otherwise, we have no balls ahead of us so just move one step. */
611 debug(("No balls; moving forwards.\n"));
612 OFFSET(x
, y
, direction
);
616 static int laser_exit(game_state
*state
, int entryno
)
618 int tmp
, x
, y
, direction
;
620 tmp
= range2grid(state
, entryno
, &x
, &y
, &direction
);
623 return fire_laser_internal(state
, x
, y
, direction
);
626 static void fire_laser(game_state
*state
, int entryno
)
628 int tmp
, exitno
, x
, y
, direction
;
630 tmp
= range2grid(state
, entryno
, &x
, &y
, &direction
);
633 exitno
= fire_laser_internal(state
, x
, y
, direction
);
635 if (exitno
== LASER_HIT
|| exitno
== LASER_REFLECT
) {
636 GRID(state
, x
, y
) = state
->exits
[entryno
] = exitno
;
638 int newno
= state
->laserno
++;
639 int xend
, yend
, unused
;
640 tmp
= range2grid(state
, exitno
, &xend
, ¥d
, &unused
);
642 GRID(state
, x
, y
) = GRID(state
, xend
, yend
) = newno
;
643 state
->exits
[entryno
] = exitno
;
644 state
->exits
[exitno
] = entryno
;
648 /* Checks that the guessed balls in the state match up with the real balls
649 * for all possible lasers (i.e. not just the ones that the player might
650 * have already guessed). This is required because any layout with >4 balls
651 * might have multiple valid solutions. Returns non-zero for a 'correct'
652 * (i.e. consistent) layout. */
653 static int check_guesses(game_state
*state
, int cagey
)
655 game_state
*solution
, *guesses
;
656 int i
, x
, y
, n
, unused
, tmp
;
661 * First, check that each laser the player has already
662 * fired is consistent with the layout. If not, show them
663 * one error they've made and reveal no further
666 * Failing that, check to see whether the player would have
667 * been able to fire any laser which distinguished the real
668 * solution from their guess. If so, show them one such
669 * laser and reveal no further information.
671 guesses
= dup_game(state
);
672 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
673 for (x
= 1; x
<= state
->w
; x
++) {
674 for (y
= 1; y
<= state
->h
; y
++) {
675 GRID(guesses
, x
, y
) &= ~BALL_CORRECT
;
676 if (GRID(guesses
, x
, y
) & BALL_GUESS
)
677 GRID(guesses
, x
, y
) |= BALL_CORRECT
;
681 for (i
= 0; i
< guesses
->nlasers
; i
++) {
682 if (guesses
->exits
[i
] != LASER_EMPTY
&&
683 guesses
->exits
[i
] != laser_exit(guesses
, i
))
688 * At least one of the player's existing lasers
689 * contradicts their ball placement. Pick a random one,
690 * highlight it, and return.
692 * A temporary random state is created from the current
693 * grid, so that repeating the same marking will give
694 * the same answer instead of a different one.
696 random_state
*rs
= random_init((char *)guesses
->grid
,
697 (state
->w
+2)*(state
->h
+2) *
698 sizeof(unsigned int));
699 n
= random_upto(rs
, n
);
701 for (i
= 0; i
< guesses
->nlasers
; i
++) {
702 if (guesses
->exits
[i
] != LASER_EMPTY
&&
703 guesses
->exits
[i
] != laser_exit(guesses
, i
) &&
705 state
->exits
[i
] |= LASER_WRONG
;
706 tmp
= laser_exit(state
, i
);
707 if (RANGECHECK(state
, tmp
))
708 state
->exits
[tmp
] |= LASER_WRONG
;
709 state
->justwrong
= TRUE
;
716 for (i
= 0; i
< guesses
->nlasers
; i
++) {
717 if (guesses
->exits
[i
] == LASER_EMPTY
&&
718 laser_exit(state
, i
) != laser_exit(guesses
, i
))
723 * At least one of the player's unfired lasers would
724 * demonstrate their ball placement to be wrong. Pick a
725 * random one, highlight it, and return.
727 * A temporary random state is created from the current
728 * grid, so that repeating the same marking will give
729 * the same answer instead of a different one.
731 random_state
*rs
= random_init((char *)guesses
->grid
,
732 (state
->w
+2)*(state
->h
+2) *
733 sizeof(unsigned int));
734 n
= random_upto(rs
, n
);
736 for (i
= 0; i
< guesses
->nlasers
; i
++) {
737 if (guesses
->exits
[i
] == LASER_EMPTY
&&
738 laser_exit(state
, i
) != laser_exit(guesses
, i
) &&
740 fire_laser(state
, i
);
741 state
->exits
[i
] |= LASER_OMITTED
;
742 tmp
= laser_exit(state
, i
);
743 if (RANGECHECK(state
, tmp
))
744 state
->exits
[tmp
] |= LASER_OMITTED
;
745 state
->justwrong
= TRUE
;
754 /* duplicate the state (to solution) */
755 solution
= dup_game(state
);
757 /* clear out the lasers of solution */
758 for (i
= 0; i
< solution
->nlasers
; i
++) {
759 tmp
= range2grid(solution
, i
, &x
, &y
, &unused
);
761 GRID(solution
, x
, y
) = 0;
762 solution
->exits
[i
] = LASER_EMPTY
;
765 /* duplicate solution to guess. */
766 guesses
= dup_game(solution
);
768 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
769 for (x
= 1; x
<= state
->w
; x
++) {
770 for (y
= 1; y
<= state
->h
; y
++) {
771 GRID(guesses
, x
, y
) &= ~BALL_CORRECT
;
772 if (GRID(guesses
, x
, y
) & BALL_GUESS
)
773 GRID(guesses
, x
, y
) |= BALL_CORRECT
;
777 /* for each laser (on both game_states), fire it if it hasn't been fired.
778 * If one has been fired (or received a hit) and another hasn't, we know
779 * the ball layouts didn't match and can short-circuit return. */
780 for (i
= 0; i
< solution
->nlasers
; i
++) {
781 if (solution
->exits
[i
] == LASER_EMPTY
)
782 fire_laser(solution
, i
);
783 if (guesses
->exits
[i
] == LASER_EMPTY
)
784 fire_laser(guesses
, i
);
787 /* check each game_state's laser against the other; if any differ, return 0 */
789 for (i
= 0; i
< solution
->nlasers
; i
++) {
790 tmp
= range2grid(solution
, i
, &x
, &y
, &unused
);
793 if (solution
->exits
[i
] != guesses
->exits
[i
]) {
794 /* If the original state didn't have this shot fired,
795 * and it would be wrong between the guess and the solution,
797 if (state
->exits
[i
] == LASER_EMPTY
) {
798 state
->exits
[i
] = solution
->exits
[i
];
799 if (state
->exits
[i
] == LASER_REFLECT
||
800 state
->exits
[i
] == LASER_HIT
)
801 GRID(state
, x
, y
) = state
->exits
[i
];
803 /* add a new shot, incrementing state's laser count. */
804 int ex
, ey
, newno
= state
->laserno
++;
805 tmp
= range2grid(state
, state
->exits
[i
], &ex
, &ey
, &unused
);
807 GRID(state
, x
, y
) = newno
;
808 GRID(state
, ex
, ey
) = newno
;
810 state
->exits
[i
] |= LASER_OMITTED
;
812 state
->exits
[i
] |= LASER_WRONG
;
817 if (ret
== 0) goto done
;
819 /* fix up original state so the 'correct' balls end up matching the guesses,
820 * as we've just proved that they were equivalent. */
821 for (x
= 1; x
<= state
->w
; x
++) {
822 for (y
= 1; y
<= state
->h
; y
++) {
823 if (GRID(state
, x
, y
) & BALL_GUESS
)
824 GRID(state
, x
, y
) |= BALL_CORRECT
;
826 GRID(state
, x
, y
) &= ~BALL_CORRECT
;
831 /* fill in nright and nwrong. */
832 state
->nright
= state
->nwrong
= state
->nmissed
= 0;
833 for (x
= 1; x
<= state
->w
; x
++) {
834 for (y
= 1; y
<= state
->h
; y
++) {
835 int bs
= GRID(state
, x
, y
) & (BALL_GUESS
| BALL_CORRECT
);
836 if (bs
== (BALL_GUESS
| BALL_CORRECT
))
838 else if (bs
== BALL_GUESS
)
840 else if (bs
== BALL_CORRECT
)
850 #define TILE_SIZE (ds->tilesize)
852 #define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
853 #define FROMDRAW(x) (((x) - (TILE_SIZE / 2)) / TILE_SIZE)
855 #define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \
856 (state)->nguesses <= (state)->maxballs && \
857 !(state)->reveal && !(state)->justwrong)
859 struct game_drawstate
{
860 int tilesize
, crad
, rrad
, w
, h
; /* w and h to make macros work... */
861 unsigned int *grid
; /* as the game_state grid */
866 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
867 int x
, int y
, int button
)
869 int gx
= -1, gy
= -1, rangeno
= -1;
870 enum { NONE
, TOGGLE_BALL
, TOGGLE_LOCK
, FIRE
, REVEAL
,
871 TOGGLE_COLUMN_LOCK
, TOGGLE_ROW_LOCK
} action
= NONE
;
872 char buf
[80], *nullret
= NULL
;
874 if (button
== LEFT_BUTTON
|| button
== RIGHT_BUTTON
) {
877 if (gx
== 0 && gy
== 0 && button
== LEFT_BUTTON
)
879 if (gx
>= 1 && gx
<= state
->w
&& gy
>= 1 && gy
<= state
->h
) {
880 if (button
== LEFT_BUTTON
) {
881 if (!(GRID(state
, gx
,gy
) & BALL_LOCK
))
882 action
= TOGGLE_BALL
;
884 action
= TOGGLE_LOCK
;
886 if (grid2range(state
, gx
, gy
, &rangeno
)) {
887 if (button
== LEFT_BUTTON
)
889 else if (gy
== 0 || gy
> state
->h
)
890 action
= TOGGLE_COLUMN_LOCK
; /* and use gx */
892 action
= TOGGLE_ROW_LOCK
; /* and use gy */
894 } else if (button
== LEFT_RELEASE
) {
895 ui
->flash_laserno
= LASER_EMPTY
;
901 sprintf(buf
, "T%d,%d", gx
, gy
);
905 sprintf(buf
, "LB%d,%d", gx
, gy
);
908 case TOGGLE_COLUMN_LOCK
:
909 sprintf(buf
, "LC%d", gx
);
912 case TOGGLE_ROW_LOCK
:
913 sprintf(buf
, "LR%d", gy
);
917 if (state
->reveal
&& state
->exits
[rangeno
] == LASER_EMPTY
)
919 ui
->flash_laserno
= rangeno
;
921 if (state
->exits
[rangeno
] != LASER_EMPTY
)
923 sprintf(buf
, "F%d", rangeno
);
927 if (!CAN_REVEAL(state
)) return nullret
;
934 if (state
->reveal
) return nullret
;
939 static game_state
*execute_move(game_state
*from
, char *move
)
941 game_state
*ret
= dup_game(from
);
942 int gx
= -1, gy
= -1, rangeno
= -1;
944 if (ret
->justwrong
) {
946 ret
->justwrong
= FALSE
;
947 for (i
= 0; i
< ret
->nlasers
; i
++)
948 if (ret
->exits
[i
] != LASER_EMPTY
)
949 ret
->exits
[i
] &= ~(LASER_OMITTED
| LASER_WRONG
);
952 if (!strcmp(move
, "S")) {
953 check_guesses(ret
, FALSE
);
957 if (from
->reveal
) goto badmove
;
958 if (!*move
) goto badmove
;
962 sscanf(move
+1, "%d,%d", &gx
, &gy
);
963 if (gx
< 1 || gy
< 1 || gx
> ret
->w
|| gy
> ret
->h
)
965 if (GRID(ret
, gx
, gy
) & BALL_GUESS
) {
967 GRID(ret
, gx
, gy
) &= ~BALL_GUESS
;
970 GRID(ret
, gx
, gy
) |= BALL_GUESS
;
975 sscanf(move
+1, "%d", &rangeno
);
976 if (ret
->exits
[rangeno
] != LASER_EMPTY
)
978 if (!RANGECHECK(ret
, rangeno
))
980 fire_laser(ret
, rangeno
);
984 if (ret
->nguesses
< ret
->minballs
||
985 ret
->nguesses
> ret
->maxballs
)
987 check_guesses(ret
, TRUE
);
993 if (strlen(move
) < 2) goto badmove
;
996 sscanf(move
+2, "%d,%d", &gx
, &gy
);
997 if (gx
< 1 || gy
< 1 || gx
> ret
->w
|| gy
> ret
->h
)
999 GRID(ret
, gx
, gy
) ^= BALL_LOCK
;
1002 #define COUNTLOCK do { if (GRID(ret, gx, gy) & BALL_LOCK) lcount++; } while (0)
1003 #define SETLOCKIF(c) do { \
1004 if (lcount > (c)) GRID(ret, gx, gy) &= ~BALL_LOCK; \
1005 else GRID(ret, gx, gy) |= BALL_LOCK; \
1009 sscanf(move
+2, "%d", &gx
);
1010 if (gx
< 1 || gx
> ret
->w
) goto badmove
;
1011 for (gy
= 1; gy
<= ret
->h
; gy
++) { COUNTLOCK
; }
1012 for (gy
= 1; gy
<= ret
->h
; gy
++) { SETLOCKIF(ret
->h
/2); }
1016 sscanf(move
+2, "%d", &gy
);
1017 if (gy
< 1 || gy
> ret
->h
) goto badmove
;
1018 for (gx
= 1; gx
<= ret
->w
; gx
++) { COUNTLOCK
; }
1019 for (gx
= 1; gx
<= ret
->w
; gx
++) { SETLOCKIF(ret
->w
/2); }
1042 /* ----------------------------------------------------------------------
1046 static void game_compute_size(game_params
*params
, int tilesize
,
1049 /* Border is ts/2, to make things easier.
1050 * Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles
1051 * across, and similarly height + 2 + 1 tiles down. */
1052 *x
= (params
->w
+ 3) * tilesize
;
1053 *y
= (params
->h
+ 3) * tilesize
;
1056 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
1059 ds
->tilesize
= tilesize
;
1060 ds
->crad
= (tilesize
-1)/2;
1061 ds
->rrad
= (3*tilesize
)/8;
1064 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1066 float *ret
= snewn(3 * NCOLOURS
, float);
1069 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_HIGHLIGHT
, COL_LOWLIGHT
);
1071 ret
[COL_BALL
* 3 + 0] = 0.0F
;
1072 ret
[COL_BALL
* 3 + 1] = 0.0F
;
1073 ret
[COL_BALL
* 3 + 2] = 0.0F
;
1075 ret
[COL_WRONG
* 3 + 0] = 1.0F
;
1076 ret
[COL_WRONG
* 3 + 1] = 0.0F
;
1077 ret
[COL_WRONG
* 3 + 2] = 0.0F
;
1079 ret
[COL_BUTTON
* 3 + 0] = 0.0F
;
1080 ret
[COL_BUTTON
* 3 + 1] = 1.0F
;
1081 ret
[COL_BUTTON
* 3 + 2] = 0.0F
;
1083 ret
[COL_LASER
* 3 + 0] = 1.0F
;
1084 ret
[COL_LASER
* 3 + 1] = 0.0F
;
1085 ret
[COL_LASER
* 3 + 2] = 0.0F
;
1087 ret
[COL_DIMLASER
* 3 + 0] = 0.5F
;
1088 ret
[COL_DIMLASER
* 3 + 1] = 0.0F
;
1089 ret
[COL_DIMLASER
* 3 + 2] = 0.0F
;
1091 for (i
= 0; i
< 3; i
++) {
1092 ret
[COL_GRID
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.9F
;
1093 ret
[COL_LOCK
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.7F
;
1094 ret
[COL_COVER
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.5F
;
1095 ret
[COL_TEXT
* 3 + i
] = 0.0F
;
1098 ret
[COL_FLASHTEXT
* 3 + 0] = 0.0F
;
1099 ret
[COL_FLASHTEXT
* 3 + 1] = 1.0F
;
1100 ret
[COL_FLASHTEXT
* 3 + 2] = 0.0F
;
1102 *ncolours
= NCOLOURS
;
1106 static game_drawstate
*game_new_drawstate(game_state
*state
)
1108 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1111 ds
->w
= state
->w
; ds
->h
= state
->h
;
1112 ds
->grid
= snewn((state
->w
+2)*(state
->h
+2), unsigned int);
1113 memset(ds
->grid
, 0, (state
->w
+2)*(state
->h
+2)*sizeof(unsigned int));
1115 ds
->flash_laserno
= LASER_EMPTY
;
1120 static void game_free_drawstate(game_drawstate
*ds
)
1126 static void draw_arena_tile(frontend
*fe
, game_state
*gs
, game_drawstate
*ds
,
1127 int ax
, int ay
, int force
, int isflash
)
1129 int gx
= ax
+1, gy
= ay
+1;
1130 int gs_tile
= GRID(gs
, gx
, gy
), ds_tile
= GRID(ds
, gx
, gy
);
1131 int dx
= TODRAW(gx
), dy
= TODRAW(gy
);
1133 if (gs_tile
!= ds_tile
|| gs
->reveal
!= ds
->reveal
|| force
) {
1136 bg
= (gs
->reveal ? COL_BACKGROUND
:
1137 (gs_tile
& BALL_LOCK
) ? COL_LOCK
: COL_COVER
);
1139 draw_rect(fe
, dx
, dy
, TILE_SIZE
, TILE_SIZE
, bg
);
1140 draw_rect_outline(fe
, dx
, dy
, TILE_SIZE
, TILE_SIZE
, COL_GRID
);
1143 /* Guessed balls are always black; if they're incorrect they'll
1144 * have a red cross added later.
1145 * Missing balls are red. */
1146 if (gs_tile
& BALL_GUESS
) {
1147 bcol
= isflash ? bg
: COL_BALL
;
1148 } else if (gs_tile
& BALL_CORRECT
) {
1149 bcol
= isflash ? bg
: COL_WRONG
;
1154 /* guesses are black/black, all else background. */
1155 if (gs_tile
& BALL_GUESS
) {
1162 draw_circle(fe
, dx
+ TILE_SIZE
/2, dy
+ TILE_SIZE
/2, ds
->crad
-1,
1166 (gs_tile
& BALL_GUESS
) &&
1167 !(gs_tile
& BALL_CORRECT
)) {
1168 int x1
= dx
+ 3, y1
= dy
+ 3;
1169 int x2
= dx
+ TILE_SIZE
- 3, y2
= dy
+ TILE_SIZE
-3;
1172 /* Incorrect guess; draw a red cross over the ball. */
1181 draw_polygon(fe
, coords
, 4, COL_WRONG
, COL_WRONG
);
1190 draw_polygon(fe
, coords
, 4, COL_WRONG
, COL_WRONG
);
1192 draw_update(fe
, dx
, dy
, TILE_SIZE
, TILE_SIZE
);
1194 GRID(ds
,gx
,gy
) = gs_tile
;
1197 static void draw_laser_tile(frontend
*fe
, game_state
*gs
, game_drawstate
*ds
,
1198 game_ui
*ui
, int lno
, int force
)
1200 int gx
, gy
, dx
, dy
, unused
;
1201 int wrong
, omitted
, reflect
, hit
, laserval
, flash
= 0, tmp
;
1202 unsigned int gs_tile
, ds_tile
, exitno
;
1204 tmp
= range2grid(gs
, lno
, &gx
, &gy
, &unused
);
1206 gs_tile
= GRID(gs
, gx
, gy
);
1207 ds_tile
= GRID(ds
, gx
, gy
);
1211 wrong
= gs
->exits
[lno
] & LASER_WRONG
;
1212 omitted
= gs
->exits
[lno
] & LASER_OMITTED
;
1213 exitno
= gs
->exits
[lno
] & ~LASER_FLAGMASK
;
1215 reflect
= gs_tile
& LASER_REFLECT
;
1216 hit
= gs_tile
& LASER_HIT
;
1217 laserval
= gs_tile
& ~LASER_FLAGMASK
;
1219 if (lno
== ui
->flash_laserno
)
1220 gs_tile
|= LASER_FLASHED
;
1221 else if (!(gs
->exits
[lno
] & (LASER_HIT
| LASER_REFLECT
))) {
1222 if (exitno
== ui
->flash_laserno
)
1223 gs_tile
|= LASER_FLASHED
;
1225 if (gs_tile
& LASER_FLASHED
) flash
= 1;
1227 gs_tile
|= wrong
| omitted
;
1229 if (gs_tile
!= ds_tile
|| force
) {
1230 draw_rect(fe
, dx
, dy
, TILE_SIZE
, TILE_SIZE
, COL_BACKGROUND
);
1231 draw_rect_outline(fe
, dx
, dy
, TILE_SIZE
, TILE_SIZE
, COL_GRID
);
1233 if (gs_tile
&~ (LASER_WRONG
| LASER_OMITTED
)) {
1235 int tcol
= flash ? COL_FLASHTEXT
: omitted ? COL_WRONG
: COL_TEXT
;
1238 sprintf(str
, "%s", reflect ?
"R" : "H");
1240 sprintf(str
, "%d", laserval
);
1243 draw_circle(fe
, dx
+ TILE_SIZE
/2, dy
+ TILE_SIZE
/2,
1245 COL_WRONG
, COL_WRONG
);
1246 draw_circle(fe
, dx
+ TILE_SIZE
/2, dy
+ TILE_SIZE
/2,
1247 ds
->rrad
- TILE_SIZE
/16,
1248 COL_BACKGROUND
, COL_WRONG
);
1251 draw_text(fe
, dx
+ TILE_SIZE
/2, dy
+ TILE_SIZE
/2,
1252 FONT_VARIABLE
, TILE_SIZE
/2, ALIGN_VCENTRE
| ALIGN_HCENTRE
,
1255 draw_update(fe
, dx
, dy
, TILE_SIZE
, TILE_SIZE
);
1257 GRID(ds
, gx
, gy
) = gs_tile
;
1261 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1262 game_state
*state
, int dir
, game_ui
*ui
,
1263 float animtime
, float flashtime
)
1265 int i
, x
, y
, ts
= TILE_SIZE
, isflash
= 0, force
= 0;
1267 if (flashtime
> 0) {
1268 int frame
= (int)(flashtime
/ FLASH_FRAME
);
1269 isflash
= (frame
% 2) == 0;
1271 debug(("game_redraw: flashtime = %f", flashtime
));
1275 int x0
= TODRAW(0)-1, y0
= TODRAW(0)-1;
1276 int x1
= TODRAW(state
->w
+2), y1
= TODRAW(state
->h
+2);
1279 TILE_SIZE
* (state
->w
+3), TILE_SIZE
* (state
->h
+3),
1282 /* clockwise around the outline starting at pt behind (1,1). */
1283 draw_line(fe
, x0
+ts
, y0
+ts
, x0
+ts
, y0
, COL_HIGHLIGHT
);
1284 draw_line(fe
, x0
+ts
, y0
, x1
-ts
, y0
, COL_HIGHLIGHT
);
1285 draw_line(fe
, x1
-ts
, y0
, x1
-ts
, y0
+ts
, COL_LOWLIGHT
);
1286 draw_line(fe
, x1
-ts
, y0
+ts
, x1
, y0
+ts
, COL_HIGHLIGHT
);
1287 draw_line(fe
, x1
, y0
+ts
, x1
, y1
-ts
, COL_LOWLIGHT
);
1288 draw_line(fe
, x1
, y1
-ts
, x1
-ts
, y1
-ts
, COL_LOWLIGHT
);
1289 draw_line(fe
, x1
-ts
, y1
-ts
, x1
-ts
, y1
, COL_LOWLIGHT
);
1290 draw_line(fe
, x1
-ts
, y1
, x0
+ts
, y1
, COL_LOWLIGHT
);
1291 draw_line(fe
, x0
+ts
, y1
, x0
+ts
, y1
-ts
, COL_HIGHLIGHT
);
1292 draw_line(fe
, x0
+ts
, y1
-ts
, x0
, y1
-ts
, COL_LOWLIGHT
);
1293 draw_line(fe
, x0
, y1
-ts
, x0
, y0
+ts
, COL_HIGHLIGHT
);
1294 draw_line(fe
, x0
, y0
+ts
, x0
+ts
, y0
+ts
, COL_HIGHLIGHT
);
1297 draw_update(fe
, 0, 0,
1298 TILE_SIZE
* (state
->w
+3), TILE_SIZE
* (state
->h
+3));
1303 /* draw the arena */
1304 for (x
= 0; x
< state
->w
; x
++) {
1305 for (y
= 0; y
< state
->h
; y
++) {
1306 draw_arena_tile(fe
, state
, ds
, x
, y
, force
, isflash
);
1310 /* draw the lasers */
1311 for (i
= 0; i
< 2*(state
->w
+state
->h
); i
++) {
1312 draw_laser_tile(fe
, state
, ds
, ui
, i
, force
);
1315 /* draw the 'finish' button */
1316 if (CAN_REVEAL(state
)) {
1317 clip(fe
, TODRAW(0), TODRAW(0), TILE_SIZE
-1, TILE_SIZE
-1);
1318 draw_circle(fe
, TODRAW(0) + ds
->crad
, TODRAW(0) + ds
->crad
, ds
->crad
,
1319 COL_BUTTON
, COL_BALL
);
1322 draw_rect(fe
, TODRAW(0), TODRAW(0),
1323 TILE_SIZE
-1, TILE_SIZE
-1, COL_BACKGROUND
);
1325 draw_update(fe
, TODRAW(0), TODRAW(0), TILE_SIZE
, TILE_SIZE
);
1326 ds
->reveal
= state
->reveal
;
1327 ds
->flash_laserno
= ui
->flash_laserno
;
1333 if (state
->nwrong
== 0 &&
1334 state
->nmissed
== 0 &&
1335 state
->nright
>= state
->minballs
)
1336 sprintf(buf
, "CORRECT!");
1338 sprintf(buf
, "%d wrong and %d missed balls.",
1339 state
->nwrong
, state
->nmissed
);
1340 } else if (state
->justwrong
) {
1341 sprintf(buf
, "Wrong! Guess again.");
1343 if (state
->nguesses
> state
->maxballs
)
1344 sprintf(buf
, "%d too many balls marked.",
1345 state
->nguesses
- state
->maxballs
);
1346 else if (state
->nguesses
<= state
->maxballs
&&
1347 state
->nguesses
>= state
->minballs
)
1348 sprintf(buf
, "Click button to verify guesses.");
1349 else if (state
->maxballs
== state
->minballs
)
1350 sprintf(buf
, "Balls marked: %d / %d",
1351 state
->nguesses
, state
->minballs
);
1353 sprintf(buf
, "Balls marked: %d / %d-%d.",
1354 state
->nguesses
, state
->minballs
, state
->maxballs
);
1357 sprintf(buf
+ strlen(buf
), " (%d error%s)",
1358 ui
->errors
, ui
->errors
> 1 ?
"s" : "");
1360 status_bar(fe
, buf
);
1364 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1365 int dir
, game_ui
*ui
)
1370 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1371 int dir
, game_ui
*ui
)
1373 if (!oldstate
->reveal
&& newstate
->reveal
)
1374 return 4.0F
* FLASH_FRAME
;
1379 static int game_wants_statusbar(void)
1384 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1390 #define thegame blackbox
1393 const struct game thegame
= {
1394 "Black Box", "games.blackbox",
1401 TRUE
, game_configure
, custom_params
,
1409 FALSE
, game_text_format
,
1417 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
1420 game_free_drawstate
,
1424 game_wants_statusbar
,
1425 FALSE
, game_timing_state
,
1426 0, /* mouse_priorities */
1429 /* vim: set shiftwidth=4 tabstop=8: */