Ability to build a .CHM for Puzzles. I haven't yet arranged for it
[sgt/puzzles] / blackbox.c
CommitLineData
f17f85c5 1/*
2 * blackbox.c: implementation of 'Black Box'.
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <assert.h>
9#include <ctype.h>
10#include <math.h>
11
12#include "puzzles.h"
13
14#define PREFERRED_TILE_SIZE 32
15#define FLASH_FRAME 0.2F
16
17/* Terminology, for ease of reading various macros scattered about the place.
18 *
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).
21 *
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.
25 *
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.
28 */
29
30enum {
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,
36 NCOLOURS
37};
38
39struct game_params {
40 int w, h;
41 int minballs, maxballs;
42};
43
44static game_params *default_params(void)
45{
46 game_params *ret = snew(game_params);
47
48 ret->w = ret->h = 8;
49 ret->minballs = ret->maxballs = 5;
50
51 return ret;
52}
53
54static const game_params blackbox_presets[] = {
55 { 5, 5, 3, 3 },
56 { 8, 8, 5, 5 },
57 { 8, 8, 3, 6 },
58 { 10, 10, 5, 5 },
59 { 10, 10, 4, 10 }
60};
61
62static int game_fetch_preset(int i, char **name, game_params **params)
63{
64 char str[80];
65 game_params *ret;
66
67 if (i < 0 || i >= lenof(blackbox_presets))
68 return FALSE;
69
70 ret = snew(game_params);
71 *ret = blackbox_presets[i];
72
73 if (ret->minballs == ret->maxballs)
74 sprintf(str, "%dx%d, %d balls",
75 ret->w, ret->h, ret->minballs);
76 else
77 sprintf(str, "%dx%d, %d-%d balls",
78 ret->w, ret->h, ret->minballs, ret->maxballs);
79
80 *name = dupstr(str);
81 *params = ret;
82 return TRUE;
83}
84
85static void free_params(game_params *params)
86{
87 sfree(params);
88}
89
90static game_params *dup_params(game_params *params)
91{
92 game_params *ret = snew(game_params);
93 *ret = *params; /* structure copy */
94 return ret;
95}
96
97static void decode_params(game_params *params, char const *string)
98{
99 char const *p = string;
100 game_params *defs = default_params();
101
102 *params = *defs; free_params(defs);
103
104 while (*p) {
105 switch (*p++) {
106 case 'w':
107 params->w = atoi(p);
108 while (*p && isdigit((unsigned char)*p)) p++;
109 break;
110
111 case 'h':
112 params->h = atoi(p);
113 while (*p && isdigit((unsigned char)*p)) p++;
114 break;
115
116 case 'm':
117 params->minballs = atoi(p);
118 while (*p && isdigit((unsigned char)*p)) p++;
119 break;
120
121 case 'M':
122 params->maxballs = atoi(p);
123 while (*p && isdigit((unsigned char)*p)) p++;
124 break;
125
126 default:
127 ;
128 }
129 }
130}
131
132static char *encode_params(game_params *params, int full)
133{
134 char str[256];
135
136 sprintf(str, "w%dh%dm%dM%d",
137 params->w, params->h, params->minballs, params->maxballs);
138 return dupstr(str);
139}
140
141static config_item *game_configure(game_params *params)
142{
143 config_item *ret;
144 char buf[80];
145
146 ret = snewn(4, config_item);
147
148 ret[0].name = "Width";
149 ret[0].type = C_STRING;
150 sprintf(buf, "%d", params->w);
151 ret[0].sval = dupstr(buf);
152 ret[0].ival = 0;
153
154 ret[1].name = "Height";
155 ret[1].type = C_STRING;
156 sprintf(buf, "%d", params->h);
157 ret[1].sval = dupstr(buf);
158 ret[1].ival = 0;
159
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);
164 else
165 sprintf(buf, "%d-%d", params->minballs, params->maxballs);
166 ret[2].sval = dupstr(buf);
167 ret[2].ival = 0;
168
169 ret[3].name = NULL;
170 ret[3].type = C_END;
171 ret[3].sval = NULL;
172 ret[3].ival = 0;
173
174 return ret;
175}
176
177static game_params *custom_params(config_item *cfg)
178{
179 game_params *ret = snew(game_params);
180
181 ret->w = atoi(cfg[0].sval);
182 ret->h = atoi(cfg[1].sval);
183
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);
187
188 return ret;
189}
190
191static char *validate_params(game_params *params, int full)
192{
193 if (params->w < 2 || params->h < 2)
71dbfa3e 194 return "Width and height must both be at least two";
f17f85c5 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)
71dbfa3e 198 return "Widths and heights greater than 255 are not supported";
f17f85c5 199 if (params->minballs > params->maxballs)
71dbfa3e 200 return "Minimum number of balls may not be greater than maximum";
f17f85c5 201 if (params->minballs >= params->w * params->h)
71dbfa3e 202 return "Too many balls to fit in grid";
f17f85c5 203 return NULL;
204}
205
206/*
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.
211 */
212
213static char *new_game_desc(game_params *params, random_state *rs,
214 char **aux, int interactive)
215{
216 int nballs = params->minballs, i;
217 char *grid, *ret;
218 unsigned char *bmp;
219
220 if (params->maxballs > params->minballs)
71dbfa3e 221 nballs += random_upto(rs, params->maxballs - params->minballs + 1);
f17f85c5 222
223 grid = snewn(params->w*params->h, char);
224 memset(grid, 0, params->w * params->h * sizeof(char));
225
226 bmp = snewn(nballs*2 + 2, unsigned char);
227 memset(bmp, 0, (nballs*2 + 2) * sizeof(unsigned char));
228
229 bmp[0] = params->w;
230 bmp[1] = params->h;
231
232 for (i = 0; i < nballs; i++) {
233 int x, y;
71dbfa3e 234
235 do {
236 x = random_upto(rs, params->w);
237 y = random_upto(rs, params->h);
238 } while (grid[y*params->w + x]);
239
240 grid[y*params->w + x] = 1;
241
f17f85c5 242 bmp[(i+1)*2 + 0] = x;
243 bmp[(i+1)*2 + 1] = y;
244 }
245 sfree(grid);
246
247 obfuscate_bitmap(bmp, (nballs*2 + 2) * 8, FALSE);
248 ret = bin2hex(bmp, nballs*2 + 2);
249 sfree(bmp);
250
251 return ret;
252}
253
254static char *validate_desc(game_params *params, char *desc)
255{
256 int nballs, dlen = strlen(desc), i;
257 unsigned char *bmp;
258 char *ret;
259
260 /* the bitmap is 2+(nballs*2) long; the hex version is double that. */
261 nballs = ((dlen/2)-2)/2;
262
263 if (dlen < 4 || dlen % 4 ||
264 nballs < params->minballs || nballs > params->maxballs)
265 return "Game description is wrong length";
266
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)
272 goto done;
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];
a996ddc6 276 if (x < 0 || y < 0 || x >= params->w || y >= params->h)
f17f85c5 277 goto done;
278 }
279 ret = NULL;
280
281done:
282 sfree(bmp);
283 return ret;
284}
285
286#define BALL_CORRECT 0x01
287#define BALL_GUESS 0x02
288#define BALL_LOCK 0x04
289
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)
297
298struct game_state {
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. */
27388471 304 int nguesses, reveal, justwrong, nright, nwrong, nmissed;
f17f85c5 305};
306
71dbfa3e 307#define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)])
f17f85c5 308
27388471 309#define RANGECHECK(s,x) ((x) >= 0 && (x) <= (s)->nlasers)
310
f17f85c5 311/* specify numbers because they must match array indexes. */
312enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 };
313
71dbfa3e 314struct offset { int x, y; };
f17f85c5 315
71dbfa3e 316static const struct offset offsets[] = {
f17f85c5 317 { 0, -1 }, /* up */
318 { 1, 0 }, /* right */
319 { 0, 1 }, /* down */
320 { -1, 0 } /* left */
321};
322
323#ifdef DEBUGGING
324static const char *dirstrs[] = {
325 "UP", "RIGHT", "DOWN", "LEFT"
326};
327#endif
328
329static int range2grid(game_state *state, int rangeno, int *x, int *y, int *direction)
330{
331 if (rangeno < 0)
332 return 0;
333
334 if (rangeno < state->w) {
335 /* top row; from (1,0) to (w,0) */
336 *x = rangeno + 1;
337 *y = 0;
338 *direction = DIR_DOWN;
339 return 1;
340 }
341 rangeno -= state->w;
342 if (rangeno < state->h) {
343 /* RHS; from (w+1, 1) to (w+1, h) */
344 *x = state->w+1;
345 *y = rangeno + 1;
346 *direction = DIR_LEFT;
347 return 1;
348 }
349 rangeno -= state->h;
350 if (rangeno < state->w) {
351 /* bottom row; from (1, h+1) to (w, h+1); counts backwards */
352 *x = (state->w - rangeno);
353 *y = state->h+1;
354 *direction = DIR_UP;
355 return 1;
356 }
357 rangeno -= state->w;
358 if (rangeno < state->h) {
359 /* LHS; from (0, 1) to (0, h); counts backwards */
360 *x = 0;
361 *y = (state->h - rangeno);
362 *direction = DIR_RIGHT;
363 return 1;
364 }
365 return 0;
366}
367
368static int grid2range(game_state *state, int x, int y, int *rangeno)
369{
370 int ret, x1 = state->w+1, y1 = state->h+1;
371
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 */
374
375 if ((x == 0 || x == x1) && (y == 0 || y == y1))
376 return 0; /* one of 4 corners */
377
378 if (y == 0) { /* top line */
379 ret = x - 1;
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;
386 }
387 *rangeno = ret;
388 debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret));
389 return 1;
390}
391
dafd6cf6 392static game_state *new_game(midend *me, game_params *params, char *desc)
f17f85c5 393{
394 game_state *state = snew(game_state);
395 int dlen = strlen(desc), i;
396 unsigned char *bmp;
397
398 state->minballs = params->minballs;
399 state->maxballs = params->maxballs;
400 state->nballs = ((dlen/2)-2)/2;
401
402 bmp = hex2bin(desc, state->nballs*2 + 2);
403 obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, TRUE);
404
405 state->w = bmp[0]; state->h = bmp[1];
406 state->nlasers = 2 * (state->w + state->h);
407
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));
410
411 state->exits = snewn(state->nlasers, unsigned int);
412 memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int));
413
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;
416 }
417 sfree(bmp);
418
27388471 419 state->done = state->nguesses = state->reveal = state->justwrong =
f17f85c5 420 state->nright = state->nwrong = state->nmissed = 0;
421 state->laserno = 1;
422
423 return state;
424}
425
426#define XFER(x) ret->x = state->x
427
428static game_state *dup_game(game_state *state)
429{
430 game_state *ret = snew(game_state);
431
432 XFER(w); XFER(h);
433 XFER(minballs); XFER(maxballs);
434 XFER(nballs); XFER(nlasers);
435
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));
440
441 XFER(done);
442 XFER(laserno);
443 XFER(nguesses);
444 XFER(reveal);
27388471 445 XFER(justwrong);
f17f85c5 446 XFER(nright); XFER(nwrong); XFER(nmissed);
447
448 return ret;
449}
450
451#undef XFER
452
453static void free_game(game_state *state)
454{
455 sfree(state->exits);
456 sfree(state->grid);
457 sfree(state);
458}
459
460static char *solve_game(game_state *state, game_state *currstate,
461 char *aux, char **error)
462{
463 return dupstr("S");
464}
465
466static char *game_text_format(game_state *state)
467{
468 return NULL;
469}
470
471struct game_ui {
472 int flash_laserno;
27388471 473 int errors, newmove;
f17f85c5 474};
475
476static game_ui *new_ui(game_state *state)
477{
dafd6cf6 478 game_ui *ui = snew(game_ui);
f17f85c5 479 ui->flash_laserno = LASER_EMPTY;
27388471 480 ui->errors = 0;
481 ui->newmove = FALSE;
f17f85c5 482 return ui;
483}
484
485static void free_ui(game_ui *ui)
486{
487 sfree(ui);
488}
489
490static char *encode_ui(game_ui *ui)
491{
27388471 492 char buf[80];
493 /*
494 * The error counter needs preserving across a serialisation.
495 */
496 sprintf(buf, "E%d", ui->errors);
497 return dupstr(buf);
f17f85c5 498}
499
500static void decode_ui(game_ui *ui, char *encoding)
501{
27388471 502 sscanf(encoding, "E%d", &ui->errors);
f17f85c5 503}
504
505static void game_changed_state(game_ui *ui, game_state *oldstate,
506 game_state *newstate)
507{
27388471 508 /*
509 * If we've encountered a `justwrong' state as a result of
510 * actually making a move, increment the ui error counter.
511 */
512 if (newstate->justwrong && ui->newmove)
513 ui->errors++;
514 ui->newmove = FALSE;
f17f85c5 515}
516
517#define OFFSET(gx,gy,o) do { \
71dbfa3e 518 int off = (4 + (o) % 4) % 4; \
f17f85c5 519 (gx) += offsets[off].x; \
520 (gy) += offsets[off].y; \
521} while(0)
522
523enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT };
524
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. */
527static int isball(game_state *state, int gx, int gy, int direction, int lookwhere)
528{
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");
539
540 debug(("isball, new (%d, %d)\n", gx, gy));
541
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)
544 return 0;
545
546 if (GRID(state, gx,gy) & BALL_CORRECT)
547 return 1;
548
549 return 0;
550}
551
27388471 552static int fire_laser_internal(game_state *state, int x, int y, int direction)
f17f85c5 553{
27388471 554 int unused, lno, tmp;
f17f85c5 555
71dbfa3e 556 tmp = grid2range(state, x, y, &lno);
557 assert(tmp);
f17f85c5 558
559 /* deal with strange initial reflection rules (that stop
560 * you turning down the laser range) */
561
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));
27388471 566 return LASER_HIT; /* hit */
f17f85c5 567 }
568
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));
27388471 572 return LASER_REFLECT; /* reflection */
f17f85c5 573 }
574 /* move us onto the grid. */
575 OFFSET(x, y, direction);
576
577 while (1) {
578 debug(("fire_laser: looping at (%d, %d) pointing %s\n",
579 x, y, dirstrs[direction]));
580 if (grid2range(state, x, y, &unused)) {
27388471 581 int exitno;
582
583 tmp = grid2range(state, x, y, &exitno);
584 assert(tmp);
585
586 return (lno == exitno ? LASER_REFLECT : exitno);
f17f85c5 587 }
588 /* paranoia. This obviously should never happen */
589 assert(!(GRID(state, x, y) & BALL_CORRECT));
590
591 if (isball(state, x, y, direction, LOOK_FORWARD)) {
592 /* we're facing a ball; send back a reflection. */
986cc2de 593 debug(("Ball ahead of (%d, %d)", x, y));
27388471 594 return LASER_HIT; /* hit */
f17f85c5 595 }
596
597 if (isball(state, x, y, direction, LOOK_LEFT)) {
598 /* ball to our left; rotate clockwise and look again. */
599 debug(("Ball to left; turning clockwise.\n"));
600 direction += 1; direction %= 4;
601 continue;
602 }
603 if (isball(state, x, y, direction, LOOK_RIGHT)) {
604 /* ball to our right; rotate anti-clockwise and look again. */
605 debug(("Ball to rightl turning anti-clockwise.\n"));
606 direction += 3; direction %= 4;
607 continue;
608 }
609 /* ... otherwise, we have no balls ahead of us so just move one step. */
610 debug(("No balls; moving forwards.\n"));
611 OFFSET(x, y, direction);
612 }
613}
614
27388471 615static int laser_exit(game_state *state, int entryno)
616{
617 int tmp, x, y, direction;
618
619 tmp = range2grid(state, entryno, &x, &y, &direction);
620 assert(tmp);
621
622 return fire_laser_internal(state, x, y, direction);
623}
624
625static void fire_laser(game_state *state, int entryno)
626{
627 int tmp, exitno, x, y, direction;
628
629 tmp = range2grid(state, entryno, &x, &y, &direction);
630 assert(tmp);
631
632 exitno = fire_laser_internal(state, x, y, direction);
633
634 if (exitno == LASER_HIT || exitno == LASER_REFLECT) {
635 GRID(state, x, y) = state->exits[entryno] = exitno;
636 } else {
637 int newno = state->laserno++;
638 int xend, yend, unused;
639 tmp = range2grid(state, exitno, &xend, &yend, &unused);
640 assert(tmp);
641 GRID(state, x, y) = GRID(state, xend, yend) = newno;
642 state->exits[entryno] = exitno;
643 state->exits[exitno] = entryno;
644 }
645}
646
f17f85c5 647/* Checks that the guessed balls in the state match up with the real balls
648 * for all possible lasers (i.e. not just the ones that the player might
649 * have already guessed). This is required because any layout with >4 balls
650 * might have multiple valid solutions. Returns non-zero for a 'correct'
651 * (i.e. consistent) layout. */
27388471 652static int check_guesses(game_state *state, int cagey)
f17f85c5 653{
654 game_state *solution, *guesses;
27388471 655 int i, x, y, n, unused, tmp;
f17f85c5 656 int ret = 0;
657
27388471 658 if (cagey) {
659 /*
660 * First, check that each laser the player has already
661 * fired is consistent with the layout. If not, show them
662 * one error they've made and reveal no further
663 * information.
664 *
665 * Failing that, check to see whether the player would have
666 * been able to fire any laser which distinguished the real
667 * solution from their guess. If so, show them one such
668 * laser and reveal no further information.
669 */
670 guesses = dup_game(state);
671 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
672 for (x = 1; x <= state->w; x++) {
673 for (y = 1; y <= state->h; y++) {
674 GRID(guesses, x, y) &= ~BALL_CORRECT;
675 if (GRID(guesses, x, y) & BALL_GUESS)
676 GRID(guesses, x, y) |= BALL_CORRECT;
677 }
678 }
679 n = 0;
680 for (i = 0; i < guesses->nlasers; i++) {
681 if (guesses->exits[i] != LASER_EMPTY &&
682 guesses->exits[i] != laser_exit(guesses, i))
683 n++;
684 }
685 if (n) {
686 /*
687 * At least one of the player's existing lasers
688 * contradicts their ball placement. Pick a random one,
689 * highlight it, and return.
690 *
691 * A temporary random state is created from the current
692 * grid, so that repeating the same marking will give
693 * the same answer instead of a different one.
694 */
1fbb0680 695 random_state *rs = random_new((char *)guesses->grid,
696 (state->w+2)*(state->h+2) *
697 sizeof(unsigned int));
27388471 698 n = random_upto(rs, n);
699 random_free(rs);
700 for (i = 0; i < guesses->nlasers; i++) {
701 if (guesses->exits[i] != LASER_EMPTY &&
702 guesses->exits[i] != laser_exit(guesses, i) &&
703 n-- == 0) {
704 state->exits[i] |= LASER_WRONG;
705 tmp = laser_exit(state, i);
706 if (RANGECHECK(state, tmp))
707 state->exits[tmp] |= LASER_WRONG;
708 state->justwrong = TRUE;
709 free_game(guesses);
710 return 0;
711 }
712 }
713 }
714 n = 0;
715 for (i = 0; i < guesses->nlasers; i++) {
716 if (guesses->exits[i] == LASER_EMPTY &&
717 laser_exit(state, i) != laser_exit(guesses, i))
718 n++;
719 }
720 if (n) {
721 /*
722 * At least one of the player's unfired lasers would
723 * demonstrate their ball placement to be wrong. Pick a
724 * random one, highlight it, and return.
725 *
726 * A temporary random state is created from the current
727 * grid, so that repeating the same marking will give
728 * the same answer instead of a different one.
729 */
1fbb0680 730 random_state *rs = random_new((char *)guesses->grid,
731 (state->w+2)*(state->h+2) *
732 sizeof(unsigned int));
27388471 733 n = random_upto(rs, n);
734 random_free(rs);
735 for (i = 0; i < guesses->nlasers; i++) {
736 if (guesses->exits[i] == LASER_EMPTY &&
737 laser_exit(state, i) != laser_exit(guesses, i) &&
738 n-- == 0) {
739 fire_laser(state, i);
740 state->exits[i] |= LASER_OMITTED;
741 tmp = laser_exit(state, i);
742 if (RANGECHECK(state, tmp))
743 state->exits[tmp] |= LASER_OMITTED;
744 state->justwrong = TRUE;
745 free_game(guesses);
746 return 0;
747 }
748 }
749 }
750 free_game(guesses);
751 }
752
f17f85c5 753 /* duplicate the state (to solution) */
754 solution = dup_game(state);
755
756 /* clear out the lasers of solution */
757 for (i = 0; i < solution->nlasers; i++) {
71dbfa3e 758 tmp = range2grid(solution, i, &x, &y, &unused);
759 assert(tmp);
f17f85c5 760 GRID(solution, x, y) = 0;
761 solution->exits[i] = LASER_EMPTY;
762 }
763
764 /* duplicate solution to guess. */
765 guesses = dup_game(solution);
766
767 /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */
768 for (x = 1; x <= state->w; x++) {
769 for (y = 1; y <= state->h; y++) {
770 GRID(guesses, x, y) &= ~BALL_CORRECT;
771 if (GRID(guesses, x, y) & BALL_GUESS)
772 GRID(guesses, x, y) |= BALL_CORRECT;
773 }
774 }
775
776 /* for each laser (on both game_states), fire it if it hasn't been fired.
777 * If one has been fired (or received a hit) and another hasn't, we know
778 * the ball layouts didn't match and can short-circuit return. */
779 for (i = 0; i < solution->nlasers; i++) {
f17f85c5 780 if (solution->exits[i] == LASER_EMPTY)
27388471 781 fire_laser(solution, i);
f17f85c5 782 if (guesses->exits[i] == LASER_EMPTY)
27388471 783 fire_laser(guesses, i);
f17f85c5 784 }
785
786 /* check each game_state's laser against the other; if any differ, return 0 */
787 ret = 1;
788 for (i = 0; i < solution->nlasers; i++) {
71dbfa3e 789 tmp = range2grid(solution, i, &x, &y, &unused);
790 assert(tmp);
f17f85c5 791
792 if (solution->exits[i] != guesses->exits[i]) {
793 /* If the original state didn't have this shot fired,
794 * and it would be wrong between the guess and the solution,
795 * add it. */
796 if (state->exits[i] == LASER_EMPTY) {
797 state->exits[i] = solution->exits[i];
798 if (state->exits[i] == LASER_REFLECT ||
799 state->exits[i] == LASER_HIT)
800 GRID(state, x, y) = state->exits[i];
801 else {
802 /* add a new shot, incrementing state's laser count. */
803 int ex, ey, newno = state->laserno++;
71dbfa3e 804 tmp = range2grid(state, state->exits[i], &ex, &ey, &unused);
805 assert(tmp);
f17f85c5 806 GRID(state, x, y) = newno;
807 GRID(state, ex, ey) = newno;
808 }
809 state->exits[i] |= LASER_OMITTED;
810 } else {
811 state->exits[i] |= LASER_WRONG;
812 }
813 ret = 0;
814 }
815 }
f7d4bf1a 816 if (ret == 0 ||
817 state->nguesses < state->minballs ||
818 state->nguesses > state->maxballs) goto done;
f17f85c5 819
820 /* fix up original state so the 'correct' balls end up matching the guesses,
821 * as we've just proved that they were equivalent. */
822 for (x = 1; x <= state->w; x++) {
823 for (y = 1; y <= state->h; y++) {
824 if (GRID(state, x, y) & BALL_GUESS)
825 GRID(state, x, y) |= BALL_CORRECT;
826 else
827 GRID(state, x, y) &= ~BALL_CORRECT;
828 }
829 }
830
831done:
832 /* fill in nright and nwrong. */
833 state->nright = state->nwrong = state->nmissed = 0;
834 for (x = 1; x <= state->w; x++) {
835 for (y = 1; y <= state->h; y++) {
836 int bs = GRID(state, x, y) & (BALL_GUESS | BALL_CORRECT);
837 if (bs == (BALL_GUESS | BALL_CORRECT))
838 state->nright++;
839 else if (bs == BALL_GUESS)
840 state->nwrong++;
841 else if (bs == BALL_CORRECT)
842 state->nmissed++;
843 }
844 }
845 free_game(solution);
846 free_game(guesses);
27388471 847 state->reveal = 1;
f17f85c5 848 return ret;
849}
850
851#define TILE_SIZE (ds->tilesize)
852
853#define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2))
854#define FROMDRAW(x) (((x) - (TILE_SIZE / 2)) / TILE_SIZE)
855
27388471 856#define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \
857 (state)->nguesses <= (state)->maxballs && \
858 !(state)->reveal && !(state)->justwrong)
859
f17f85c5 860struct game_drawstate {
861 int tilesize, crad, rrad, w, h; /* w and h to make macros work... */
862 unsigned int *grid; /* as the game_state grid */
27388471 863 int started, reveal;
7dfe3b1f 864 int flash_laserno, isflash;
f17f85c5 865};
866
867static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds,
868 int x, int y, int button)
869{
870 int gx = -1, gy = -1, rangeno = -1;
871 enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL,
872 TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE;
873 char buf[80], *nullret = NULL;
874
875 if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
876 gx = FROMDRAW(x);
877 gy = FROMDRAW(y);
878 if (gx == 0 && gy == 0 && button == LEFT_BUTTON)
879 action = REVEAL;
880 if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) {
881 if (button == LEFT_BUTTON) {
882 if (!(GRID(state, gx,gy) & BALL_LOCK))
883 action = TOGGLE_BALL;
884 } else
885 action = TOGGLE_LOCK;
886 }
887 if (grid2range(state, gx, gy, &rangeno)) {
888 if (button == LEFT_BUTTON)
889 action = FIRE;
890 else if (gy == 0 || gy > state->h)
891 action = TOGGLE_COLUMN_LOCK; /* and use gx */
892 else
893 action = TOGGLE_ROW_LOCK; /* and use gy */
894 }
895 } else if (button == LEFT_RELEASE) {
896 ui->flash_laserno = LASER_EMPTY;
897 return "";
898 }
899
900 switch (action) {
901 case TOGGLE_BALL:
902 sprintf(buf, "T%d,%d", gx, gy);
903 break;
904
905 case TOGGLE_LOCK:
906 sprintf(buf, "LB%d,%d", gx, gy);
907 break;
908
909 case TOGGLE_COLUMN_LOCK:
910 sprintf(buf, "LC%d", gx);
911 break;
912
913 case TOGGLE_ROW_LOCK:
914 sprintf(buf, "LR%d", gy);
915 break;
916
917 case FIRE:
918 if (state->reveal && state->exits[rangeno] == LASER_EMPTY)
919 return nullret;
920 ui->flash_laserno = rangeno;
921 nullret = "";
922 if (state->exits[rangeno] != LASER_EMPTY)
923 return "";
924 sprintf(buf, "F%d", rangeno);
925 break;
926
927 case REVEAL:
27388471 928 if (!CAN_REVEAL(state)) return nullret;
f17f85c5 929 sprintf(buf, "R");
930 break;
931
932 default:
933 return nullret;
934 }
935 if (state->reveal) return nullret;
27388471 936 ui->newmove = TRUE;
f17f85c5 937 return dupstr(buf);
938}
939
940static game_state *execute_move(game_state *from, char *move)
941{
942 game_state *ret = dup_game(from);
27388471 943 int gx = -1, gy = -1, rangeno = -1;
944
945 if (ret->justwrong) {
946 int i;
947 ret->justwrong = FALSE;
948 for (i = 0; i < ret->nlasers; i++)
949 if (ret->exits[i] != LASER_EMPTY)
950 ret->exits[i] &= ~(LASER_OMITTED | LASER_WRONG);
951 }
f17f85c5 952
953 if (!strcmp(move, "S")) {
27388471 954 check_guesses(ret, FALSE);
f17f85c5 955 return ret;
956 }
957
958 if (from->reveal) goto badmove;
71dbfa3e 959 if (!*move) goto badmove;
f17f85c5 960
961 switch (move[0]) {
962 case 'T':
963 sscanf(move+1, "%d,%d", &gx, &gy);
964 if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
965 goto badmove;
966 if (GRID(ret, gx, gy) & BALL_GUESS) {
967 ret->nguesses--;
968 GRID(ret, gx, gy) &= ~BALL_GUESS;
969 } else {
970 ret->nguesses++;
971 GRID(ret, gx, gy) |= BALL_GUESS;
972 }
973 break;
974
975 case 'F':
976 sscanf(move+1, "%d", &rangeno);
977 if (ret->exits[rangeno] != LASER_EMPTY)
978 goto badmove;
27388471 979 if (!RANGECHECK(ret, rangeno))
f17f85c5 980 goto badmove;
27388471 981 fire_laser(ret, rangeno);
f17f85c5 982 break;
983
984 case 'R':
985 if (ret->nguesses < ret->minballs ||
986 ret->nguesses > ret->maxballs)
987 goto badmove;
27388471 988 check_guesses(ret, TRUE);
f17f85c5 989 break;
990
991 case 'L':
992 {
993 int lcount = 0;
994 if (strlen(move) < 2) goto badmove;
995 switch (move[1]) {
996 case 'B':
997 sscanf(move+2, "%d,%d", &gx, &gy);
998 if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h)
999 goto badmove;
1000 GRID(ret, gx, gy) ^= BALL_LOCK;
1001 break;
1002
1003#define COUNTLOCK do { if (GRID(ret, gx, gy) & BALL_LOCK) lcount++; } while (0)
1004#define SETLOCKIF(c) do { \
1005 if (lcount > (c)) GRID(ret, gx, gy) &= ~BALL_LOCK; \
1006 else GRID(ret, gx, gy) |= BALL_LOCK; \
1007} while(0)
1008
1009 case 'C':
1010 sscanf(move+2, "%d", &gx);
1011 if (gx < 1 || gx > ret->w) goto badmove;
1012 for (gy = 1; gy <= ret->h; gy++) { COUNTLOCK; }
1013 for (gy = 1; gy <= ret->h; gy++) { SETLOCKIF(ret->h/2); }
1014 break;
1015
1016 case 'R':
1017 sscanf(move+2, "%d", &gy);
1018 if (gy < 1 || gy > ret->h) goto badmove;
1019 for (gx = 1; gx <= ret->w; gx++) { COUNTLOCK; }
1020 for (gx = 1; gx <= ret->w; gx++) { SETLOCKIF(ret->w/2); }
1021 break;
1022
1023#undef COUNTLOCK
1024#undef SETLOCKIF
1025
1026 default:
1027 goto badmove;
1028 }
1029 }
1030 break;
1031
1032 default:
1033 goto badmove;
1034 }
1035
1036 return ret;
1037
1038badmove:
1039 free_game(ret);
1040 return NULL;
1041}
1042
1043/* ----------------------------------------------------------------------
1044 * Drawing routines.
1045 */
1046
1047static void game_compute_size(game_params *params, int tilesize,
1048 int *x, int *y)
1049{
1050 /* Border is ts/2, to make things easier.
1051 * Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles
1052 * across, and similarly height + 2 + 1 tiles down. */
1053 *x = (params->w + 3) * tilesize;
1054 *y = (params->h + 3) * tilesize;
1055}
1056
dafd6cf6 1057static void game_set_size(drawing *dr, game_drawstate *ds,
1058 game_params *params, int tilesize)
f17f85c5 1059{
1060 ds->tilesize = tilesize;
1061 ds->crad = (tilesize-1)/2;
1062 ds->rrad = (3*tilesize)/8;
1063}
1064
8266f3fc 1065static float *game_colours(frontend *fe, int *ncolours)
f17f85c5 1066{
1067 float *ret = snewn(3 * NCOLOURS, float);
1068 int i;
1069
1070 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
1071
1072 ret[COL_BALL * 3 + 0] = 0.0F;
1073 ret[COL_BALL * 3 + 1] = 0.0F;
1074 ret[COL_BALL * 3 + 2] = 0.0F;
1075
1076 ret[COL_WRONG * 3 + 0] = 1.0F;
1077 ret[COL_WRONG * 3 + 1] = 0.0F;
1078 ret[COL_WRONG * 3 + 2] = 0.0F;
1079
1080 ret[COL_BUTTON * 3 + 0] = 0.0F;
1081 ret[COL_BUTTON * 3 + 1] = 1.0F;
1082 ret[COL_BUTTON * 3 + 2] = 0.0F;
1083
1084 ret[COL_LASER * 3 + 0] = 1.0F;
1085 ret[COL_LASER * 3 + 1] = 0.0F;
1086 ret[COL_LASER * 3 + 2] = 0.0F;
1087
1088 ret[COL_DIMLASER * 3 + 0] = 0.5F;
1089 ret[COL_DIMLASER * 3 + 1] = 0.0F;
1090 ret[COL_DIMLASER * 3 + 2] = 0.0F;
1091
1092 for (i = 0; i < 3; i++) {
1093 ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F;
1094 ret[COL_LOCK * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.7F;
1095 ret[COL_COVER * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.5F;
1096 ret[COL_TEXT * 3 + i] = 0.0F;
1097 }
1098
1099 ret[COL_FLASHTEXT * 3 + 0] = 0.0F;
1100 ret[COL_FLASHTEXT * 3 + 1] = 1.0F;
1101 ret[COL_FLASHTEXT * 3 + 2] = 0.0F;
1102
1103 *ncolours = NCOLOURS;
1104 return ret;
1105}
1106
dafd6cf6 1107static game_drawstate *game_new_drawstate(drawing *dr, game_state *state)
f17f85c5 1108{
1109 struct game_drawstate *ds = snew(struct game_drawstate);
1110
1111 ds->tilesize = 0;
1112 ds->w = state->w; ds->h = state->h;
1113 ds->grid = snewn((state->w+2)*(state->h+2), unsigned int);
1114 memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int));
986cc2de 1115 ds->started = ds->reveal = 0;
f17f85c5 1116 ds->flash_laserno = LASER_EMPTY;
7dfe3b1f 1117 ds->isflash = 0;
f17f85c5 1118
1119 return ds;
1120}
1121
dafd6cf6 1122static void game_free_drawstate(drawing *dr, game_drawstate *ds)
f17f85c5 1123{
1124 sfree(ds->grid);
1125 sfree(ds);
1126}
1127
dafd6cf6 1128static void draw_arena_tile(drawing *dr, game_state *gs, game_drawstate *ds,
f17f85c5 1129 int ax, int ay, int force, int isflash)
1130{
1131 int gx = ax+1, gy = ay+1;
1132 int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy);
1133 int dx = TODRAW(gx), dy = TODRAW(gy);
1134
1135 if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) {
1136 int bcol, bg;
1137
a996ddc6 1138 bg = (gs->reveal ? COL_BACKGROUND :
1139 (gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER);
f17f85c5 1140
dafd6cf6 1141 draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, bg);
1142 draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
f17f85c5 1143
1144 if (gs->reveal) {
1145 /* Guessed balls are always black; if they're incorrect they'll
1146 * have a red cross added later.
1147 * Missing balls are red. */
1148 if (gs_tile & BALL_GUESS) {
1149 bcol = isflash ? bg : COL_BALL;
1150 } else if (gs_tile & BALL_CORRECT) {
1151 bcol = isflash ? bg : COL_WRONG;
1152 } else {
1153 bcol = bg;
1154 }
1155 } else {
1156 /* guesses are black/black, all else background. */
1157 if (gs_tile & BALL_GUESS) {
1158 bcol = COL_BALL;
1159 } else {
1160 bcol = bg;
1161 }
1162 }
1163
dafd6cf6 1164 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1,
f17f85c5 1165 bcol, bcol);
1166
1167 if (gs->reveal &&
1168 (gs_tile & BALL_GUESS) &&
1169 !(gs_tile & BALL_CORRECT)) {
1170 int x1 = dx + 3, y1 = dy + 3;
1171 int x2 = dx + TILE_SIZE - 3, y2 = dy + TILE_SIZE-3;
1172 int coords[8];
1173
1174 /* Incorrect guess; draw a red cross over the ball. */
1175 coords[0] = x1-1;
1176 coords[1] = y1+1;
1177 coords[2] = x1+1;
1178 coords[3] = y1-1;
1179 coords[4] = x2+1;
1180 coords[5] = y2-1;
1181 coords[6] = x2-1;
1182 coords[7] = y2+1;
dafd6cf6 1183 draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
f17f85c5 1184 coords[0] = x2+1;
1185 coords[1] = y1+1;
1186 coords[2] = x2-1;
1187 coords[3] = y1-1;
1188 coords[4] = x1-1;
1189 coords[5] = y2-1;
1190 coords[6] = x1+1;
1191 coords[7] = y2+1;
dafd6cf6 1192 draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG);
f17f85c5 1193 }
dafd6cf6 1194 draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
f17f85c5 1195 }
1196 GRID(ds,gx,gy) = gs_tile;
1197}
1198
dafd6cf6 1199static void draw_laser_tile(drawing *dr, game_state *gs, game_drawstate *ds,
f17f85c5 1200 game_ui *ui, int lno, int force)
1201{
1202 int gx, gy, dx, dy, unused;
71dbfa3e 1203 int wrong, omitted, reflect, hit, laserval, flash = 0, tmp;
f17f85c5 1204 unsigned int gs_tile, ds_tile, exitno;
1205
71dbfa3e 1206 tmp = range2grid(gs, lno, &gx, &gy, &unused);
1207 assert(tmp);
f17f85c5 1208 gs_tile = GRID(gs, gx, gy);
1209 ds_tile = GRID(ds, gx, gy);
1210 dx = TODRAW(gx);
1211 dy = TODRAW(gy);
1212
1213 wrong = gs->exits[lno] & LASER_WRONG;
1214 omitted = gs->exits[lno] & LASER_OMITTED;
1215 exitno = gs->exits[lno] & ~LASER_FLAGMASK;
1216
1217 reflect = gs_tile & LASER_REFLECT;
1218 hit = gs_tile & LASER_HIT;
1219 laserval = gs_tile & ~LASER_FLAGMASK;
1220
1221 if (lno == ui->flash_laserno)
1222 gs_tile |= LASER_FLASHED;
1223 else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) {
1224 if (exitno == ui->flash_laserno)
1225 gs_tile |= LASER_FLASHED;
1226 }
1227 if (gs_tile & LASER_FLASHED) flash = 1;
1228
1229 gs_tile |= wrong | omitted;
1230
1231 if (gs_tile != ds_tile || force) {
dafd6cf6 1232 draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND);
1233 draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID);
f17f85c5 1234
1235 if (gs_tile &~ (LASER_WRONG | LASER_OMITTED)) {
1236 char str[10];
1237 int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT;
1238
1239 if (reflect || hit)
1240 sprintf(str, "%s", reflect ? "R" : "H");
1241 else
1242 sprintf(str, "%d", laserval);
1243
1244 if (wrong) {
dafd6cf6 1245 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
f17f85c5 1246 ds->rrad,
1247 COL_WRONG, COL_WRONG);
dafd6cf6 1248 draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
f17f85c5 1249 ds->rrad - TILE_SIZE/16,
1250 COL_BACKGROUND, COL_WRONG);
1251 }
1252
dafd6cf6 1253 draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2,
f17f85c5 1254 FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE,
1255 tcol, str);
1256 }
dafd6cf6 1257 draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE);
f17f85c5 1258 }
1259 GRID(ds, gx, gy) = gs_tile;
1260}
1261
1262
dafd6cf6 1263static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate,
f17f85c5 1264 game_state *state, int dir, game_ui *ui,
1265 float animtime, float flashtime)
1266{
1267 int i, x, y, ts = TILE_SIZE, isflash = 0, force = 0;
1268
1269 if (flashtime > 0) {
1270 int frame = (int)(flashtime / FLASH_FRAME);
1271 isflash = (frame % 2) == 0;
f17f85c5 1272 debug(("game_redraw: flashtime = %f", flashtime));
1273 }
1274
1275 if (!ds->started) {
1276 int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1;
1277 int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2);
1278
dafd6cf6 1279 draw_rect(dr, 0, 0,
f17f85c5 1280 TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3),
1281 COL_BACKGROUND);
1282
1283 /* clockwise around the outline starting at pt behind (1,1). */
dafd6cf6 1284 draw_line(dr, x0+ts, y0+ts, x0+ts, y0, COL_HIGHLIGHT);
1285 draw_line(dr, x0+ts, y0, x1-ts, y0, COL_HIGHLIGHT);
1286 draw_line(dr, x1-ts, y0, x1-ts, y0+ts, COL_LOWLIGHT);
1287 draw_line(dr, x1-ts, y0+ts, x1, y0+ts, COL_HIGHLIGHT);
1288 draw_line(dr, x1, y0+ts, x1, y1-ts, COL_LOWLIGHT);
1289 draw_line(dr, x1, y1-ts, x1-ts, y1-ts, COL_LOWLIGHT);
1290 draw_line(dr, x1-ts, y1-ts, x1-ts, y1, COL_LOWLIGHT);
1291 draw_line(dr, x1-ts, y1, x0+ts, y1, COL_LOWLIGHT);
1292 draw_line(dr, x0+ts, y1, x0+ts, y1-ts, COL_HIGHLIGHT);
1293 draw_line(dr, x0+ts, y1-ts, x0, y1-ts, COL_LOWLIGHT);
1294 draw_line(dr, x0, y1-ts, x0, y0+ts, COL_HIGHLIGHT);
1295 draw_line(dr, x0, y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT);
f17f85c5 1296 /* phew... */
1297
dafd6cf6 1298 draw_update(dr, 0, 0,
f17f85c5 1299 TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3));
1300 force = 1;
1301 ds->started = 1;
1302 }
1303
7dfe3b1f 1304 if (isflash != ds->isflash) force = 1;
1305
f17f85c5 1306 /* draw the arena */
1307 for (x = 0; x < state->w; x++) {
1308 for (y = 0; y < state->h; y++) {
dafd6cf6 1309 draw_arena_tile(dr, state, ds, x, y, force, isflash);
f17f85c5 1310 }
1311 }
1312
1313 /* draw the lasers */
1314 for (i = 0; i < 2*(state->w+state->h); i++) {
dafd6cf6 1315 draw_laser_tile(dr, state, ds, ui, i, force);
f17f85c5 1316 }
1317
1318 /* draw the 'finish' button */
27388471 1319 if (CAN_REVEAL(state)) {
dafd6cf6 1320 clip(dr, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1);
1321 draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad,
f17f85c5 1322 COL_BUTTON, COL_BALL);
dafd6cf6 1323 unclip(dr);
f17f85c5 1324 } else {
dafd6cf6 1325 draw_rect(dr, TODRAW(0), TODRAW(0),
f17f85c5 1326 TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND);
f17f85c5 1327 }
dafd6cf6 1328 draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE);
f17f85c5 1329 ds->reveal = state->reveal;
1330 ds->flash_laserno = ui->flash_laserno;
7dfe3b1f 1331 ds->isflash = isflash;
f17f85c5 1332
1333 {
1334 char buf[256];
1335
1336 if (ds->reveal) {
1337 if (state->nwrong == 0 &&
1338 state->nmissed == 0 &&
1339 state->nright >= state->minballs)
1340 sprintf(buf, "CORRECT!");
1341 else
1342 sprintf(buf, "%d wrong and %d missed balls.",
1343 state->nwrong, state->nmissed);
27388471 1344 } else if (state->justwrong) {
1345 sprintf(buf, "Wrong! Guess again.");
1346 } else {
f17f85c5 1347 if (state->nguesses > state->maxballs)
1348 sprintf(buf, "%d too many balls marked.",
1349 state->nguesses - state->maxballs);
1350 else if (state->nguesses <= state->maxballs &&
1351 state->nguesses >= state->minballs)
1352 sprintf(buf, "Click button to verify guesses.");
1353 else if (state->maxballs == state->minballs)
1354 sprintf(buf, "Balls marked: %d / %d",
1355 state->nguesses, state->minballs);
1356 else
1357 sprintf(buf, "Balls marked: %d / %d-%d.",
1358 state->nguesses, state->minballs, state->maxballs);
1359 }
27388471 1360 if (ui->errors) {
1361 sprintf(buf + strlen(buf), " (%d error%s)",
1362 ui->errors, ui->errors > 1 ? "s" : "");
1363 }
dafd6cf6 1364 status_bar(dr, buf);
f17f85c5 1365 }
1366}
1367
1368static float game_anim_length(game_state *oldstate, game_state *newstate,
1369 int dir, game_ui *ui)
1370{
1371 return 0.0F;
1372}
1373
1374static float game_flash_length(game_state *oldstate, game_state *newstate,
1375 int dir, game_ui *ui)
1376{
1377 if (!oldstate->reveal && newstate->reveal)
1378 return 4.0F * FLASH_FRAME;
1379 else
1380 return 0.0F;
1381}
1382
f17f85c5 1383static int game_timing_state(game_state *state, game_ui *ui)
1384{
1385 return TRUE;
1386}
1387
dafd6cf6 1388static void game_print_size(game_params *params, float *x, float *y)
1389{
1390}
1391
1392static void game_print(drawing *dr, game_state *state, int tilesize)
1393{
1394}
1395
f17f85c5 1396#ifdef COMBINED
1397#define thegame blackbox
1398#endif
1399
1400const struct game thegame = {
1401 "Black Box", "games.blackbox",
1402 default_params,
1403 game_fetch_preset,
1404 decode_params,
1405 encode_params,
1406 free_params,
1407 dup_params,
1408 TRUE, game_configure, custom_params,
1409 validate_params,
1410 new_game_desc,
1411 validate_desc,
1412 new_game,
1413 dup_game,
1414 free_game,
1415 TRUE, solve_game,
1416 FALSE, game_text_format,
1417 new_ui,
1418 free_ui,
1419 encode_ui,
1420 decode_ui,
1421 game_changed_state,
1422 interpret_move,
1423 execute_move,
1424 PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
1425 game_colours,
1426 game_new_drawstate,
1427 game_free_drawstate,
1428 game_redraw,
1429 game_anim_length,
1430 game_flash_length,
dafd6cf6 1431 FALSE, FALSE, game_print_size, game_print,
ac9f41c4 1432 TRUE, /* wants_statusbar */
f17f85c5 1433 FALSE, game_timing_state,
2705d374 1434 0, /* flags */
f17f85c5 1435};
1436
1437/* vim: set shiftwidth=4 tabstop=8: */