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 | |
30 | enum { |
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, |
316908ca |
35 | COL_CURSOR, |
f17f85c5 |
36 | NCOLOURS |
37 | }; |
38 | |
39 | struct game_params { |
40 | int w, h; |
41 | int minballs, maxballs; |
42 | }; |
43 | |
44 | static 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 | |
54 | static 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 | |
62 | static 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 | |
85 | static void free_params(game_params *params) |
86 | { |
87 | sfree(params); |
88 | } |
89 | |
90 | static 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 | |
97 | static 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 | |
132 | static 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 | |
141 | static 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 | |
177 | static 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 | |
191 | static 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 | |
213 | static 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 | |
254 | static 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 | |
281 | done: |
282 | sfree(bmp); |
283 | return ret; |
284 | } |
285 | |
286 | #define BALL_CORRECT 0x01 |
287 | #define BALL_GUESS 0x02 |
288 | #define BALL_LOCK 0x04 |
289 | |
316908ca |
290 | #define LASER_FLAGMASK 0x1f800 |
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 | |
298 | #define FLAG_CURSOR 0x10000 /* needs to be disjoint from both sets */ |
f17f85c5 |
299 | |
300 | struct game_state { |
301 | int w, h, minballs, maxballs, nballs, nlasers; |
302 | unsigned int *grid; /* (w+2)x(h+2), to allow for laser firing range */ |
303 | unsigned int *exits; /* one per laser */ |
304 | int done; /* user has finished placing his own balls. */ |
305 | int laserno; /* number of next laser to be fired. */ |
27388471 |
306 | int nguesses, reveal, justwrong, nright, nwrong, nmissed; |
f17f85c5 |
307 | }; |
308 | |
71dbfa3e |
309 | #define GRID(s,x,y) ((s)->grid[(y)*((s)->w+2) + (x)]) |
f17f85c5 |
310 | |
27388471 |
311 | #define RANGECHECK(s,x) ((x) >= 0 && (x) <= (s)->nlasers) |
312 | |
f17f85c5 |
313 | /* specify numbers because they must match array indexes. */ |
314 | enum { DIR_UP = 0, DIR_RIGHT = 1, DIR_DOWN = 2, DIR_LEFT = 3 }; |
315 | |
71dbfa3e |
316 | struct offset { int x, y; }; |
f17f85c5 |
317 | |
71dbfa3e |
318 | static const struct offset offsets[] = { |
f17f85c5 |
319 | { 0, -1 }, /* up */ |
320 | { 1, 0 }, /* right */ |
321 | { 0, 1 }, /* down */ |
322 | { -1, 0 } /* left */ |
323 | }; |
324 | |
325 | #ifdef DEBUGGING |
326 | static const char *dirstrs[] = { |
327 | "UP", "RIGHT", "DOWN", "LEFT" |
328 | }; |
329 | #endif |
330 | |
331 | static int range2grid(game_state *state, int rangeno, int *x, int *y, int *direction) |
332 | { |
333 | if (rangeno < 0) |
334 | return 0; |
335 | |
336 | if (rangeno < state->w) { |
337 | /* top row; from (1,0) to (w,0) */ |
338 | *x = rangeno + 1; |
339 | *y = 0; |
340 | *direction = DIR_DOWN; |
341 | return 1; |
342 | } |
343 | rangeno -= state->w; |
344 | if (rangeno < state->h) { |
345 | /* RHS; from (w+1, 1) to (w+1, h) */ |
346 | *x = state->w+1; |
347 | *y = rangeno + 1; |
348 | *direction = DIR_LEFT; |
349 | return 1; |
350 | } |
351 | rangeno -= state->h; |
352 | if (rangeno < state->w) { |
353 | /* bottom row; from (1, h+1) to (w, h+1); counts backwards */ |
354 | *x = (state->w - rangeno); |
355 | *y = state->h+1; |
356 | *direction = DIR_UP; |
357 | return 1; |
358 | } |
359 | rangeno -= state->w; |
360 | if (rangeno < state->h) { |
361 | /* LHS; from (0, 1) to (0, h); counts backwards */ |
362 | *x = 0; |
363 | *y = (state->h - rangeno); |
364 | *direction = DIR_RIGHT; |
365 | return 1; |
366 | } |
367 | return 0; |
368 | } |
369 | |
370 | static int grid2range(game_state *state, int x, int y, int *rangeno) |
371 | { |
372 | int ret, x1 = state->w+1, y1 = state->h+1; |
373 | |
374 | if (x > 0 && x < x1 && y > 0 && y < y1) return 0; /* in arena */ |
375 | if (x < 0 || x > y1 || y < 0 || y > y1) return 0; /* outside grid */ |
376 | |
377 | if ((x == 0 || x == x1) && (y == 0 || y == y1)) |
378 | return 0; /* one of 4 corners */ |
379 | |
380 | if (y == 0) { /* top line */ |
381 | ret = x - 1; |
382 | } else if (x == x1) { /* RHS */ |
383 | ret = y - 1 + state->w; |
384 | } else if (y == y1) { /* Bottom [and counts backwards] */ |
385 | ret = (state->w - x) + state->w + state->h; |
386 | } else { /* LHS [and counts backwards ] */ |
387 | ret = (state->h-y) + state->w + state->w + state->h; |
388 | } |
389 | *rangeno = ret; |
390 | debug(("grid2range: (%d,%d) rangeno = %d\n", x, y, ret)); |
391 | return 1; |
392 | } |
393 | |
dafd6cf6 |
394 | static game_state *new_game(midend *me, game_params *params, char *desc) |
f17f85c5 |
395 | { |
396 | game_state *state = snew(game_state); |
397 | int dlen = strlen(desc), i; |
398 | unsigned char *bmp; |
399 | |
400 | state->minballs = params->minballs; |
401 | state->maxballs = params->maxballs; |
402 | state->nballs = ((dlen/2)-2)/2; |
403 | |
404 | bmp = hex2bin(desc, state->nballs*2 + 2); |
405 | obfuscate_bitmap(bmp, (state->nballs*2 + 2) * 8, TRUE); |
406 | |
407 | state->w = bmp[0]; state->h = bmp[1]; |
408 | state->nlasers = 2 * (state->w + state->h); |
409 | |
410 | state->grid = snewn((state->w+2)*(state->h+2), unsigned int); |
411 | memset(state->grid, 0, (state->w+2)*(state->h+2) * sizeof(unsigned int)); |
412 | |
413 | state->exits = snewn(state->nlasers, unsigned int); |
414 | memset(state->exits, LASER_EMPTY, state->nlasers * sizeof(unsigned int)); |
415 | |
416 | for (i = 0; i < state->nballs; i++) { |
417 | GRID(state, bmp[(i+1)*2 + 0]+1, bmp[(i+1)*2 + 1]+1) = BALL_CORRECT; |
418 | } |
419 | sfree(bmp); |
420 | |
27388471 |
421 | state->done = state->nguesses = state->reveal = state->justwrong = |
f17f85c5 |
422 | state->nright = state->nwrong = state->nmissed = 0; |
423 | state->laserno = 1; |
424 | |
425 | return state; |
426 | } |
427 | |
428 | #define XFER(x) ret->x = state->x |
429 | |
430 | static game_state *dup_game(game_state *state) |
431 | { |
432 | game_state *ret = snew(game_state); |
433 | |
434 | XFER(w); XFER(h); |
435 | XFER(minballs); XFER(maxballs); |
436 | XFER(nballs); XFER(nlasers); |
437 | |
438 | ret->grid = snewn((ret->w+2)*(ret->h+2), unsigned int); |
439 | memcpy(ret->grid, state->grid, (ret->w+2)*(ret->h+2) * sizeof(unsigned int)); |
440 | ret->exits = snewn(ret->nlasers, unsigned int); |
441 | memcpy(ret->exits, state->exits, ret->nlasers * sizeof(unsigned int)); |
442 | |
443 | XFER(done); |
444 | XFER(laserno); |
445 | XFER(nguesses); |
446 | XFER(reveal); |
27388471 |
447 | XFER(justwrong); |
f17f85c5 |
448 | XFER(nright); XFER(nwrong); XFER(nmissed); |
449 | |
450 | return ret; |
451 | } |
452 | |
453 | #undef XFER |
454 | |
455 | static void free_game(game_state *state) |
456 | { |
457 | sfree(state->exits); |
458 | sfree(state->grid); |
459 | sfree(state); |
460 | } |
461 | |
462 | static char *solve_game(game_state *state, game_state *currstate, |
463 | char *aux, char **error) |
464 | { |
465 | return dupstr("S"); |
466 | } |
467 | |
fa3abef5 |
468 | static int game_can_format_as_text_now(game_params *params) |
469 | { |
470 | return TRUE; |
471 | } |
472 | |
f17f85c5 |
473 | static char *game_text_format(game_state *state) |
474 | { |
475 | return NULL; |
476 | } |
477 | |
478 | struct game_ui { |
479 | int flash_laserno; |
27388471 |
480 | int errors, newmove; |
316908ca |
481 | int cur_x, cur_y, cur_visible; |
482 | int flash_laser; /* 0 = never, 1 = always, 2 = if anim. */ |
f17f85c5 |
483 | }; |
484 | |
485 | static game_ui *new_ui(game_state *state) |
486 | { |
dafd6cf6 |
487 | game_ui *ui = snew(game_ui); |
f17f85c5 |
488 | ui->flash_laserno = LASER_EMPTY; |
27388471 |
489 | ui->errors = 0; |
490 | ui->newmove = FALSE; |
316908ca |
491 | |
492 | ui->cur_x = ui->cur_y = 1; |
493 | ui->cur_visible = 0; |
494 | |
495 | ui->flash_laser = 0; |
496 | |
f17f85c5 |
497 | return ui; |
498 | } |
499 | |
500 | static void free_ui(game_ui *ui) |
501 | { |
502 | sfree(ui); |
503 | } |
504 | |
505 | static char *encode_ui(game_ui *ui) |
506 | { |
27388471 |
507 | char buf[80]; |
508 | /* |
509 | * The error counter needs preserving across a serialisation. |
510 | */ |
511 | sprintf(buf, "E%d", ui->errors); |
512 | return dupstr(buf); |
f17f85c5 |
513 | } |
514 | |
515 | static void decode_ui(game_ui *ui, char *encoding) |
516 | { |
27388471 |
517 | sscanf(encoding, "E%d", &ui->errors); |
f17f85c5 |
518 | } |
519 | |
520 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
521 | game_state *newstate) |
522 | { |
27388471 |
523 | /* |
524 | * If we've encountered a `justwrong' state as a result of |
525 | * actually making a move, increment the ui error counter. |
526 | */ |
527 | if (newstate->justwrong && ui->newmove) |
528 | ui->errors++; |
529 | ui->newmove = FALSE; |
f17f85c5 |
530 | } |
531 | |
532 | #define OFFSET(gx,gy,o) do { \ |
71dbfa3e |
533 | int off = (4 + (o) % 4) % 4; \ |
f17f85c5 |
534 | (gx) += offsets[off].x; \ |
535 | (gy) += offsets[off].y; \ |
536 | } while(0) |
537 | |
538 | enum { LOOK_LEFT, LOOK_FORWARD, LOOK_RIGHT }; |
539 | |
540 | /* Given a position and a direction, check whether we can see a ball in front |
541 | * of us, or to our front-left or front-right. */ |
542 | static int isball(game_state *state, int gx, int gy, int direction, int lookwhere) |
543 | { |
544 | debug(("isball, (%d, %d), dir %s, lookwhere %s\n", gx, gy, dirstrs[direction], |
545 | lookwhere == LOOK_LEFT ? "LEFT" : |
546 | lookwhere == LOOK_FORWARD ? "FORWARD" : "RIGHT")); |
547 | OFFSET(gx,gy,direction); |
548 | if (lookwhere == LOOK_LEFT) |
549 | OFFSET(gx,gy,direction-1); |
550 | else if (lookwhere == LOOK_RIGHT) |
551 | OFFSET(gx,gy,direction+1); |
552 | else if (lookwhere != LOOK_FORWARD) |
553 | assert(!"unknown lookwhere"); |
554 | |
555 | debug(("isball, new (%d, %d)\n", gx, gy)); |
556 | |
557 | /* if we're off the grid (into the firing range) there's never a ball. */ |
558 | if (gx < 1 || gy < 1 || gx > state->h || gy > state->w) |
559 | return 0; |
560 | |
561 | if (GRID(state, gx,gy) & BALL_CORRECT) |
562 | return 1; |
563 | |
564 | return 0; |
565 | } |
566 | |
27388471 |
567 | static int fire_laser_internal(game_state *state, int x, int y, int direction) |
f17f85c5 |
568 | { |
27388471 |
569 | int unused, lno, tmp; |
f17f85c5 |
570 | |
71dbfa3e |
571 | tmp = grid2range(state, x, y, &lno); |
572 | assert(tmp); |
f17f85c5 |
573 | |
574 | /* deal with strange initial reflection rules (that stop |
575 | * you turning down the laser range) */ |
576 | |
577 | /* I've just chosen to prioritise instant-hit over instant-reflection; |
578 | * I can't find anywhere that gives me a definite algorithm for this. */ |
579 | if (isball(state, x, y, direction, LOOK_FORWARD)) { |
580 | debug(("Instant hit at (%d, %d)\n", x, y)); |
27388471 |
581 | return LASER_HIT; /* hit */ |
f17f85c5 |
582 | } |
583 | |
584 | if (isball(state, x, y, direction, LOOK_LEFT) || |
585 | isball(state, x, y, direction, LOOK_RIGHT)) { |
586 | debug(("Instant reflection at (%d, %d)\n", x, y)); |
27388471 |
587 | return LASER_REFLECT; /* reflection */ |
f17f85c5 |
588 | } |
589 | /* move us onto the grid. */ |
590 | OFFSET(x, y, direction); |
591 | |
592 | while (1) { |
593 | debug(("fire_laser: looping at (%d, %d) pointing %s\n", |
594 | x, y, dirstrs[direction])); |
595 | if (grid2range(state, x, y, &unused)) { |
27388471 |
596 | int exitno; |
597 | |
598 | tmp = grid2range(state, x, y, &exitno); |
599 | assert(tmp); |
600 | |
601 | return (lno == exitno ? LASER_REFLECT : exitno); |
f17f85c5 |
602 | } |
603 | /* paranoia. This obviously should never happen */ |
604 | assert(!(GRID(state, x, y) & BALL_CORRECT)); |
605 | |
606 | if (isball(state, x, y, direction, LOOK_FORWARD)) { |
607 | /* we're facing a ball; send back a reflection. */ |
986cc2de |
608 | debug(("Ball ahead of (%d, %d)", x, y)); |
27388471 |
609 | return LASER_HIT; /* hit */ |
f17f85c5 |
610 | } |
611 | |
612 | if (isball(state, x, y, direction, LOOK_LEFT)) { |
613 | /* ball to our left; rotate clockwise and look again. */ |
614 | debug(("Ball to left; turning clockwise.\n")); |
615 | direction += 1; direction %= 4; |
616 | continue; |
617 | } |
618 | if (isball(state, x, y, direction, LOOK_RIGHT)) { |
619 | /* ball to our right; rotate anti-clockwise and look again. */ |
620 | debug(("Ball to rightl turning anti-clockwise.\n")); |
621 | direction += 3; direction %= 4; |
622 | continue; |
623 | } |
624 | /* ... otherwise, we have no balls ahead of us so just move one step. */ |
625 | debug(("No balls; moving forwards.\n")); |
626 | OFFSET(x, y, direction); |
627 | } |
628 | } |
629 | |
27388471 |
630 | static int laser_exit(game_state *state, int entryno) |
631 | { |
632 | int tmp, x, y, direction; |
633 | |
634 | tmp = range2grid(state, entryno, &x, &y, &direction); |
635 | assert(tmp); |
636 | |
637 | return fire_laser_internal(state, x, y, direction); |
638 | } |
639 | |
640 | static void fire_laser(game_state *state, int entryno) |
641 | { |
642 | int tmp, exitno, x, y, direction; |
643 | |
644 | tmp = range2grid(state, entryno, &x, &y, &direction); |
645 | assert(tmp); |
646 | |
647 | exitno = fire_laser_internal(state, x, y, direction); |
648 | |
649 | if (exitno == LASER_HIT || exitno == LASER_REFLECT) { |
650 | GRID(state, x, y) = state->exits[entryno] = exitno; |
651 | } else { |
652 | int newno = state->laserno++; |
653 | int xend, yend, unused; |
654 | tmp = range2grid(state, exitno, &xend, ¥d, &unused); |
655 | assert(tmp); |
656 | GRID(state, x, y) = GRID(state, xend, yend) = newno; |
657 | state->exits[entryno] = exitno; |
658 | state->exits[exitno] = entryno; |
659 | } |
660 | } |
661 | |
f17f85c5 |
662 | /* Checks that the guessed balls in the state match up with the real balls |
663 | * for all possible lasers (i.e. not just the ones that the player might |
664 | * have already guessed). This is required because any layout with >4 balls |
665 | * might have multiple valid solutions. Returns non-zero for a 'correct' |
666 | * (i.e. consistent) layout. */ |
27388471 |
667 | static int check_guesses(game_state *state, int cagey) |
f17f85c5 |
668 | { |
669 | game_state *solution, *guesses; |
27388471 |
670 | int i, x, y, n, unused, tmp; |
f17f85c5 |
671 | int ret = 0; |
672 | |
27388471 |
673 | if (cagey) { |
674 | /* |
675 | * First, check that each laser the player has already |
676 | * fired is consistent with the layout. If not, show them |
677 | * one error they've made and reveal no further |
678 | * information. |
679 | * |
680 | * Failing that, check to see whether the player would have |
681 | * been able to fire any laser which distinguished the real |
682 | * solution from their guess. If so, show them one such |
683 | * laser and reveal no further information. |
684 | */ |
685 | guesses = dup_game(state); |
686 | /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */ |
687 | for (x = 1; x <= state->w; x++) { |
688 | for (y = 1; y <= state->h; y++) { |
689 | GRID(guesses, x, y) &= ~BALL_CORRECT; |
690 | if (GRID(guesses, x, y) & BALL_GUESS) |
691 | GRID(guesses, x, y) |= BALL_CORRECT; |
692 | } |
693 | } |
694 | n = 0; |
695 | for (i = 0; i < guesses->nlasers; i++) { |
696 | if (guesses->exits[i] != LASER_EMPTY && |
697 | guesses->exits[i] != laser_exit(guesses, i)) |
698 | n++; |
699 | } |
700 | if (n) { |
701 | /* |
702 | * At least one of the player's existing lasers |
703 | * contradicts their ball placement. Pick a random one, |
704 | * highlight it, and return. |
705 | * |
706 | * A temporary random state is created from the current |
707 | * grid, so that repeating the same marking will give |
708 | * the same answer instead of a different one. |
709 | */ |
1fbb0680 |
710 | random_state *rs = random_new((char *)guesses->grid, |
711 | (state->w+2)*(state->h+2) * |
712 | sizeof(unsigned int)); |
27388471 |
713 | n = random_upto(rs, n); |
714 | random_free(rs); |
715 | for (i = 0; i < guesses->nlasers; i++) { |
716 | if (guesses->exits[i] != LASER_EMPTY && |
717 | guesses->exits[i] != laser_exit(guesses, i) && |
718 | n-- == 0) { |
719 | state->exits[i] |= LASER_WRONG; |
720 | tmp = laser_exit(state, i); |
721 | if (RANGECHECK(state, tmp)) |
722 | state->exits[tmp] |= LASER_WRONG; |
723 | state->justwrong = TRUE; |
724 | free_game(guesses); |
725 | return 0; |
726 | } |
727 | } |
728 | } |
729 | n = 0; |
730 | for (i = 0; i < guesses->nlasers; i++) { |
731 | if (guesses->exits[i] == LASER_EMPTY && |
732 | laser_exit(state, i) != laser_exit(guesses, i)) |
733 | n++; |
734 | } |
735 | if (n) { |
736 | /* |
737 | * At least one of the player's unfired lasers would |
738 | * demonstrate their ball placement to be wrong. Pick a |
739 | * random one, highlight it, and return. |
740 | * |
741 | * A temporary random state is created from the current |
742 | * grid, so that repeating the same marking will give |
743 | * the same answer instead of a different one. |
744 | */ |
1fbb0680 |
745 | random_state *rs = random_new((char *)guesses->grid, |
746 | (state->w+2)*(state->h+2) * |
747 | sizeof(unsigned int)); |
27388471 |
748 | n = random_upto(rs, n); |
749 | random_free(rs); |
750 | for (i = 0; i < guesses->nlasers; i++) { |
751 | if (guesses->exits[i] == LASER_EMPTY && |
752 | laser_exit(state, i) != laser_exit(guesses, i) && |
753 | n-- == 0) { |
754 | fire_laser(state, i); |
755 | state->exits[i] |= LASER_OMITTED; |
756 | tmp = laser_exit(state, i); |
757 | if (RANGECHECK(state, tmp)) |
758 | state->exits[tmp] |= LASER_OMITTED; |
759 | state->justwrong = TRUE; |
760 | free_game(guesses); |
761 | return 0; |
762 | } |
763 | } |
764 | } |
765 | free_game(guesses); |
766 | } |
767 | |
f17f85c5 |
768 | /* duplicate the state (to solution) */ |
769 | solution = dup_game(state); |
770 | |
771 | /* clear out the lasers of solution */ |
772 | for (i = 0; i < solution->nlasers; i++) { |
71dbfa3e |
773 | tmp = range2grid(solution, i, &x, &y, &unused); |
774 | assert(tmp); |
f17f85c5 |
775 | GRID(solution, x, y) = 0; |
776 | solution->exits[i] = LASER_EMPTY; |
777 | } |
778 | |
779 | /* duplicate solution to guess. */ |
780 | guesses = dup_game(solution); |
781 | |
782 | /* clear out BALL_CORRECT on guess, make BALL_GUESS BALL_CORRECT. */ |
783 | for (x = 1; x <= state->w; x++) { |
784 | for (y = 1; y <= state->h; y++) { |
785 | GRID(guesses, x, y) &= ~BALL_CORRECT; |
786 | if (GRID(guesses, x, y) & BALL_GUESS) |
787 | GRID(guesses, x, y) |= BALL_CORRECT; |
788 | } |
789 | } |
790 | |
791 | /* for each laser (on both game_states), fire it if it hasn't been fired. |
792 | * If one has been fired (or received a hit) and another hasn't, we know |
793 | * the ball layouts didn't match and can short-circuit return. */ |
794 | for (i = 0; i < solution->nlasers; i++) { |
f17f85c5 |
795 | if (solution->exits[i] == LASER_EMPTY) |
27388471 |
796 | fire_laser(solution, i); |
f17f85c5 |
797 | if (guesses->exits[i] == LASER_EMPTY) |
27388471 |
798 | fire_laser(guesses, i); |
f17f85c5 |
799 | } |
800 | |
801 | /* check each game_state's laser against the other; if any differ, return 0 */ |
802 | ret = 1; |
803 | for (i = 0; i < solution->nlasers; i++) { |
71dbfa3e |
804 | tmp = range2grid(solution, i, &x, &y, &unused); |
805 | assert(tmp); |
f17f85c5 |
806 | |
807 | if (solution->exits[i] != guesses->exits[i]) { |
808 | /* If the original state didn't have this shot fired, |
809 | * and it would be wrong between the guess and the solution, |
810 | * add it. */ |
811 | if (state->exits[i] == LASER_EMPTY) { |
812 | state->exits[i] = solution->exits[i]; |
813 | if (state->exits[i] == LASER_REFLECT || |
814 | state->exits[i] == LASER_HIT) |
815 | GRID(state, x, y) = state->exits[i]; |
816 | else { |
817 | /* add a new shot, incrementing state's laser count. */ |
818 | int ex, ey, newno = state->laserno++; |
71dbfa3e |
819 | tmp = range2grid(state, state->exits[i], &ex, &ey, &unused); |
820 | assert(tmp); |
f17f85c5 |
821 | GRID(state, x, y) = newno; |
822 | GRID(state, ex, ey) = newno; |
823 | } |
824 | state->exits[i] |= LASER_OMITTED; |
825 | } else { |
826 | state->exits[i] |= LASER_WRONG; |
827 | } |
828 | ret = 0; |
829 | } |
830 | } |
f7d4bf1a |
831 | if (ret == 0 || |
832 | state->nguesses < state->minballs || |
833 | state->nguesses > state->maxballs) goto done; |
f17f85c5 |
834 | |
835 | /* fix up original state so the 'correct' balls end up matching the guesses, |
836 | * as we've just proved that they were equivalent. */ |
837 | for (x = 1; x <= state->w; x++) { |
838 | for (y = 1; y <= state->h; y++) { |
839 | if (GRID(state, x, y) & BALL_GUESS) |
840 | GRID(state, x, y) |= BALL_CORRECT; |
841 | else |
842 | GRID(state, x, y) &= ~BALL_CORRECT; |
843 | } |
844 | } |
845 | |
846 | done: |
847 | /* fill in nright and nwrong. */ |
848 | state->nright = state->nwrong = state->nmissed = 0; |
849 | for (x = 1; x <= state->w; x++) { |
850 | for (y = 1; y <= state->h; y++) { |
851 | int bs = GRID(state, x, y) & (BALL_GUESS | BALL_CORRECT); |
852 | if (bs == (BALL_GUESS | BALL_CORRECT)) |
853 | state->nright++; |
854 | else if (bs == BALL_GUESS) |
855 | state->nwrong++; |
856 | else if (bs == BALL_CORRECT) |
857 | state->nmissed++; |
858 | } |
859 | } |
860 | free_game(solution); |
861 | free_game(guesses); |
27388471 |
862 | state->reveal = 1; |
f17f85c5 |
863 | return ret; |
864 | } |
865 | |
866 | #define TILE_SIZE (ds->tilesize) |
867 | |
868 | #define TODRAW(x) ((TILE_SIZE * (x)) + (TILE_SIZE / 2)) |
869 | #define FROMDRAW(x) (((x) - (TILE_SIZE / 2)) / TILE_SIZE) |
870 | |
27388471 |
871 | #define CAN_REVEAL(state) ((state)->nguesses >= (state)->minballs && \ |
872 | (state)->nguesses <= (state)->maxballs && \ |
873 | !(state)->reveal && !(state)->justwrong) |
874 | |
f17f85c5 |
875 | struct game_drawstate { |
876 | int tilesize, crad, rrad, w, h; /* w and h to make macros work... */ |
877 | unsigned int *grid; /* as the game_state grid */ |
27388471 |
878 | int started, reveal; |
7dfe3b1f |
879 | int flash_laserno, isflash; |
f17f85c5 |
880 | }; |
881 | |
882 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
883 | int x, int y, int button) |
884 | { |
316908ca |
885 | int gx = -1, gy = -1, rangeno = -1, wouldflash = 0; |
f17f85c5 |
886 | enum { NONE, TOGGLE_BALL, TOGGLE_LOCK, FIRE, REVEAL, |
887 | TOGGLE_COLUMN_LOCK, TOGGLE_ROW_LOCK} action = NONE; |
888 | char buf[80], *nullret = NULL; |
889 | |
316908ca |
890 | if (IS_CURSOR_MOVE(button)) { |
891 | int cx = ui->cur_x, cy = ui->cur_y; |
892 | |
893 | move_cursor(button, &cx, &cy, state->w+2, state->h+2, 0); |
894 | if ((cx == 0 && cy == 0 && !CAN_REVEAL(state)) || |
895 | (cx == 0 && cy == state->h+1) || |
896 | (cx == state->w+1 && cy == 0) || |
897 | (cx == state->w+1 && cy == state->h+1)) |
898 | return NULL; /* disallow moving cursor to corners. */ |
899 | ui->cur_x = cx; |
900 | ui->cur_y = cy; |
901 | ui->cur_visible = 1; |
902 | return ""; |
903 | } |
904 | |
f17f85c5 |
905 | if (button == LEFT_BUTTON || button == RIGHT_BUTTON) { |
906 | gx = FROMDRAW(x); |
907 | gy = FROMDRAW(y); |
316908ca |
908 | ui->cur_visible = 0; |
909 | wouldflash = 1; |
910 | } else if (button == LEFT_RELEASE) { |
911 | ui->flash_laser = 0; |
912 | return ""; |
913 | } else if (IS_CURSOR_SELECT(button)) { |
914 | if (ui->cur_visible) { |
915 | gx = ui->cur_x; |
916 | gy = ui->cur_y; |
917 | ui->flash_laser = 0; |
918 | wouldflash = 2; |
919 | } else { |
920 | ui->cur_visible = 1; |
921 | return ""; |
922 | } |
923 | /* Fix up 'button' for the below logic. */ |
924 | if (button == CURSOR_SELECT2) button = RIGHT_BUTTON; |
925 | else button = LEFT_BUTTON; |
926 | } |
927 | |
928 | if (gx != -1 && gy != -1) { |
f17f85c5 |
929 | if (gx == 0 && gy == 0 && button == LEFT_BUTTON) |
930 | action = REVEAL; |
931 | if (gx >= 1 && gx <= state->w && gy >= 1 && gy <= state->h) { |
932 | if (button == LEFT_BUTTON) { |
933 | if (!(GRID(state, gx,gy) & BALL_LOCK)) |
934 | action = TOGGLE_BALL; |
935 | } else |
936 | action = TOGGLE_LOCK; |
937 | } |
938 | if (grid2range(state, gx, gy, &rangeno)) { |
939 | if (button == LEFT_BUTTON) |
940 | action = FIRE; |
941 | else if (gy == 0 || gy > state->h) |
942 | action = TOGGLE_COLUMN_LOCK; /* and use gx */ |
943 | else |
944 | action = TOGGLE_ROW_LOCK; /* and use gy */ |
945 | } |
f17f85c5 |
946 | } |
947 | |
948 | switch (action) { |
949 | case TOGGLE_BALL: |
950 | sprintf(buf, "T%d,%d", gx, gy); |
951 | break; |
952 | |
953 | case TOGGLE_LOCK: |
954 | sprintf(buf, "LB%d,%d", gx, gy); |
955 | break; |
956 | |
957 | case TOGGLE_COLUMN_LOCK: |
958 | sprintf(buf, "LC%d", gx); |
959 | break; |
960 | |
961 | case TOGGLE_ROW_LOCK: |
962 | sprintf(buf, "LR%d", gy); |
963 | break; |
964 | |
965 | case FIRE: |
966 | if (state->reveal && state->exits[rangeno] == LASER_EMPTY) |
967 | return nullret; |
968 | ui->flash_laserno = rangeno; |
316908ca |
969 | ui->flash_laser = wouldflash; |
f17f85c5 |
970 | nullret = ""; |
971 | if (state->exits[rangeno] != LASER_EMPTY) |
972 | return ""; |
973 | sprintf(buf, "F%d", rangeno); |
974 | break; |
975 | |
976 | case REVEAL: |
27388471 |
977 | if (!CAN_REVEAL(state)) return nullret; |
316908ca |
978 | if (ui->cur_visible == 1) ui->cur_x = ui->cur_y = 1; |
f17f85c5 |
979 | sprintf(buf, "R"); |
980 | break; |
981 | |
982 | default: |
983 | return nullret; |
984 | } |
985 | if (state->reveal) return nullret; |
27388471 |
986 | ui->newmove = TRUE; |
f17f85c5 |
987 | return dupstr(buf); |
988 | } |
989 | |
990 | static game_state *execute_move(game_state *from, char *move) |
991 | { |
992 | game_state *ret = dup_game(from); |
27388471 |
993 | int gx = -1, gy = -1, rangeno = -1; |
994 | |
995 | if (ret->justwrong) { |
996 | int i; |
997 | ret->justwrong = FALSE; |
998 | for (i = 0; i < ret->nlasers; i++) |
999 | if (ret->exits[i] != LASER_EMPTY) |
1000 | ret->exits[i] &= ~(LASER_OMITTED | LASER_WRONG); |
1001 | } |
f17f85c5 |
1002 | |
1003 | if (!strcmp(move, "S")) { |
27388471 |
1004 | check_guesses(ret, FALSE); |
f17f85c5 |
1005 | return ret; |
1006 | } |
1007 | |
1008 | if (from->reveal) goto badmove; |
71dbfa3e |
1009 | if (!*move) goto badmove; |
f17f85c5 |
1010 | |
1011 | switch (move[0]) { |
1012 | case 'T': |
1013 | sscanf(move+1, "%d,%d", &gx, &gy); |
1014 | if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h) |
1015 | goto badmove; |
1016 | if (GRID(ret, gx, gy) & BALL_GUESS) { |
1017 | ret->nguesses--; |
1018 | GRID(ret, gx, gy) &= ~BALL_GUESS; |
1019 | } else { |
1020 | ret->nguesses++; |
1021 | GRID(ret, gx, gy) |= BALL_GUESS; |
1022 | } |
1023 | break; |
1024 | |
1025 | case 'F': |
1026 | sscanf(move+1, "%d", &rangeno); |
1027 | if (ret->exits[rangeno] != LASER_EMPTY) |
1028 | goto badmove; |
27388471 |
1029 | if (!RANGECHECK(ret, rangeno)) |
f17f85c5 |
1030 | goto badmove; |
27388471 |
1031 | fire_laser(ret, rangeno); |
f17f85c5 |
1032 | break; |
1033 | |
1034 | case 'R': |
1035 | if (ret->nguesses < ret->minballs || |
1036 | ret->nguesses > ret->maxballs) |
1037 | goto badmove; |
27388471 |
1038 | check_guesses(ret, TRUE); |
f17f85c5 |
1039 | break; |
1040 | |
1041 | case 'L': |
1042 | { |
1043 | int lcount = 0; |
1044 | if (strlen(move) < 2) goto badmove; |
1045 | switch (move[1]) { |
1046 | case 'B': |
1047 | sscanf(move+2, "%d,%d", &gx, &gy); |
1048 | if (gx < 1 || gy < 1 || gx > ret->w || gy > ret->h) |
1049 | goto badmove; |
1050 | GRID(ret, gx, gy) ^= BALL_LOCK; |
1051 | break; |
1052 | |
1053 | #define COUNTLOCK do { if (GRID(ret, gx, gy) & BALL_LOCK) lcount++; } while (0) |
1054 | #define SETLOCKIF(c) do { \ |
1055 | if (lcount > (c)) GRID(ret, gx, gy) &= ~BALL_LOCK; \ |
1056 | else GRID(ret, gx, gy) |= BALL_LOCK; \ |
1057 | } while(0) |
1058 | |
1059 | case 'C': |
1060 | sscanf(move+2, "%d", &gx); |
1061 | if (gx < 1 || gx > ret->w) goto badmove; |
1062 | for (gy = 1; gy <= ret->h; gy++) { COUNTLOCK; } |
1063 | for (gy = 1; gy <= ret->h; gy++) { SETLOCKIF(ret->h/2); } |
1064 | break; |
1065 | |
1066 | case 'R': |
1067 | sscanf(move+2, "%d", &gy); |
1068 | if (gy < 1 || gy > ret->h) goto badmove; |
1069 | for (gx = 1; gx <= ret->w; gx++) { COUNTLOCK; } |
1070 | for (gx = 1; gx <= ret->w; gx++) { SETLOCKIF(ret->w/2); } |
1071 | break; |
1072 | |
1073 | #undef COUNTLOCK |
1074 | #undef SETLOCKIF |
1075 | |
1076 | default: |
1077 | goto badmove; |
1078 | } |
1079 | } |
1080 | break; |
1081 | |
1082 | default: |
1083 | goto badmove; |
1084 | } |
1085 | |
1086 | return ret; |
1087 | |
1088 | badmove: |
1089 | free_game(ret); |
1090 | return NULL; |
1091 | } |
1092 | |
1093 | /* ---------------------------------------------------------------------- |
1094 | * Drawing routines. |
1095 | */ |
1096 | |
1097 | static void game_compute_size(game_params *params, int tilesize, |
1098 | int *x, int *y) |
1099 | { |
1100 | /* Border is ts/2, to make things easier. |
1101 | * Thus we have (width) + 2 (firing range*2) + 1 (border*2) tiles |
1102 | * across, and similarly height + 2 + 1 tiles down. */ |
1103 | *x = (params->w + 3) * tilesize; |
1104 | *y = (params->h + 3) * tilesize; |
1105 | } |
1106 | |
dafd6cf6 |
1107 | static void game_set_size(drawing *dr, game_drawstate *ds, |
1108 | game_params *params, int tilesize) |
f17f85c5 |
1109 | { |
1110 | ds->tilesize = tilesize; |
1111 | ds->crad = (tilesize-1)/2; |
1112 | ds->rrad = (3*tilesize)/8; |
1113 | } |
1114 | |
8266f3fc |
1115 | static float *game_colours(frontend *fe, int *ncolours) |
f17f85c5 |
1116 | { |
1117 | float *ret = snewn(3 * NCOLOURS, float); |
1118 | int i; |
1119 | |
1120 | game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT); |
1121 | |
1122 | ret[COL_BALL * 3 + 0] = 0.0F; |
1123 | ret[COL_BALL * 3 + 1] = 0.0F; |
1124 | ret[COL_BALL * 3 + 2] = 0.0F; |
1125 | |
1126 | ret[COL_WRONG * 3 + 0] = 1.0F; |
1127 | ret[COL_WRONG * 3 + 1] = 0.0F; |
1128 | ret[COL_WRONG * 3 + 2] = 0.0F; |
1129 | |
1130 | ret[COL_BUTTON * 3 + 0] = 0.0F; |
1131 | ret[COL_BUTTON * 3 + 1] = 1.0F; |
1132 | ret[COL_BUTTON * 3 + 2] = 0.0F; |
1133 | |
316908ca |
1134 | ret[COL_CURSOR * 3 + 0] = 1.0F; |
1135 | ret[COL_CURSOR * 3 + 1] = 0.0F; |
1136 | ret[COL_CURSOR * 3 + 2] = 0.0F; |
f17f85c5 |
1137 | |
1138 | for (i = 0; i < 3; i++) { |
1139 | ret[COL_GRID * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.9F; |
1140 | ret[COL_LOCK * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.7F; |
1141 | ret[COL_COVER * 3 + i] = ret[COL_BACKGROUND * 3 + i] * 0.5F; |
1142 | ret[COL_TEXT * 3 + i] = 0.0F; |
1143 | } |
1144 | |
1145 | ret[COL_FLASHTEXT * 3 + 0] = 0.0F; |
1146 | ret[COL_FLASHTEXT * 3 + 1] = 1.0F; |
1147 | ret[COL_FLASHTEXT * 3 + 2] = 0.0F; |
1148 | |
1149 | *ncolours = NCOLOURS; |
1150 | return ret; |
1151 | } |
1152 | |
dafd6cf6 |
1153 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
f17f85c5 |
1154 | { |
1155 | struct game_drawstate *ds = snew(struct game_drawstate); |
1156 | |
1157 | ds->tilesize = 0; |
1158 | ds->w = state->w; ds->h = state->h; |
1159 | ds->grid = snewn((state->w+2)*(state->h+2), unsigned int); |
1160 | memset(ds->grid, 0, (state->w+2)*(state->h+2)*sizeof(unsigned int)); |
986cc2de |
1161 | ds->started = ds->reveal = 0; |
f17f85c5 |
1162 | ds->flash_laserno = LASER_EMPTY; |
7dfe3b1f |
1163 | ds->isflash = 0; |
f17f85c5 |
1164 | |
1165 | return ds; |
1166 | } |
1167 | |
dafd6cf6 |
1168 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
f17f85c5 |
1169 | { |
1170 | sfree(ds->grid); |
1171 | sfree(ds); |
1172 | } |
1173 | |
316908ca |
1174 | static void draw_square_cursor(drawing *dr, game_drawstate *ds, int dx, int dy) |
1175 | { |
1176 | int coff = TILE_SIZE/8; |
1177 | draw_rect_outline(dr, dx + coff, dy + coff, |
1178 | TILE_SIZE - coff*2, |
1179 | TILE_SIZE - coff*2, |
1180 | COL_CURSOR); |
1181 | } |
1182 | |
1183 | |
dafd6cf6 |
1184 | static void draw_arena_tile(drawing *dr, game_state *gs, game_drawstate *ds, |
316908ca |
1185 | game_ui *ui, int ax, int ay, int force, int isflash) |
f17f85c5 |
1186 | { |
1187 | int gx = ax+1, gy = ay+1; |
1188 | int gs_tile = GRID(gs, gx, gy), ds_tile = GRID(ds, gx, gy); |
1189 | int dx = TODRAW(gx), dy = TODRAW(gy); |
1190 | |
316908ca |
1191 | if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy) |
1192 | gs_tile |= FLAG_CURSOR; |
1193 | |
f17f85c5 |
1194 | if (gs_tile != ds_tile || gs->reveal != ds->reveal || force) { |
316908ca |
1195 | int bcol, ocol, bg; |
f17f85c5 |
1196 | |
a996ddc6 |
1197 | bg = (gs->reveal ? COL_BACKGROUND : |
1198 | (gs_tile & BALL_LOCK) ? COL_LOCK : COL_COVER); |
f17f85c5 |
1199 | |
dafd6cf6 |
1200 | draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, bg); |
1201 | draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID); |
f17f85c5 |
1202 | |
1203 | if (gs->reveal) { |
1204 | /* Guessed balls are always black; if they're incorrect they'll |
1205 | * have a red cross added later. |
1206 | * Missing balls are red. */ |
1207 | if (gs_tile & BALL_GUESS) { |
1208 | bcol = isflash ? bg : COL_BALL; |
1209 | } else if (gs_tile & BALL_CORRECT) { |
1210 | bcol = isflash ? bg : COL_WRONG; |
1211 | } else { |
1212 | bcol = bg; |
1213 | } |
1214 | } else { |
1215 | /* guesses are black/black, all else background. */ |
1216 | if (gs_tile & BALL_GUESS) { |
1217 | bcol = COL_BALL; |
1218 | } else { |
1219 | bcol = bg; |
1220 | } |
1221 | } |
316908ca |
1222 | ocol = (gs_tile & FLAG_CURSOR && bcol != bg) ? COL_CURSOR : bcol; |
f17f85c5 |
1223 | |
dafd6cf6 |
1224 | draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-1, |
316908ca |
1225 | ocol, ocol); |
1226 | draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, ds->crad-3, |
f17f85c5 |
1227 | bcol, bcol); |
1228 | |
316908ca |
1229 | |
1230 | if (gs_tile & FLAG_CURSOR && bcol == bg) |
1231 | draw_square_cursor(dr, ds, dx, dy); |
1232 | |
f17f85c5 |
1233 | if (gs->reveal && |
1234 | (gs_tile & BALL_GUESS) && |
1235 | !(gs_tile & BALL_CORRECT)) { |
1236 | int x1 = dx + 3, y1 = dy + 3; |
1237 | int x2 = dx + TILE_SIZE - 3, y2 = dy + TILE_SIZE-3; |
1238 | int coords[8]; |
1239 | |
1240 | /* Incorrect guess; draw a red cross over the ball. */ |
1241 | coords[0] = x1-1; |
1242 | coords[1] = y1+1; |
1243 | coords[2] = x1+1; |
1244 | coords[3] = y1-1; |
1245 | coords[4] = x2+1; |
1246 | coords[5] = y2-1; |
1247 | coords[6] = x2-1; |
1248 | coords[7] = y2+1; |
dafd6cf6 |
1249 | draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG); |
f17f85c5 |
1250 | coords[0] = x2+1; |
1251 | coords[1] = y1+1; |
1252 | coords[2] = x2-1; |
1253 | coords[3] = y1-1; |
1254 | coords[4] = x1-1; |
1255 | coords[5] = y2-1; |
1256 | coords[6] = x1+1; |
1257 | coords[7] = y2+1; |
dafd6cf6 |
1258 | draw_polygon(dr, coords, 4, COL_WRONG, COL_WRONG); |
f17f85c5 |
1259 | } |
dafd6cf6 |
1260 | draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE); |
f17f85c5 |
1261 | } |
1262 | GRID(ds,gx,gy) = gs_tile; |
1263 | } |
1264 | |
dafd6cf6 |
1265 | static void draw_laser_tile(drawing *dr, game_state *gs, game_drawstate *ds, |
f17f85c5 |
1266 | game_ui *ui, int lno, int force) |
1267 | { |
1268 | int gx, gy, dx, dy, unused; |
71dbfa3e |
1269 | int wrong, omitted, reflect, hit, laserval, flash = 0, tmp; |
f17f85c5 |
1270 | unsigned int gs_tile, ds_tile, exitno; |
1271 | |
71dbfa3e |
1272 | tmp = range2grid(gs, lno, &gx, &gy, &unused); |
1273 | assert(tmp); |
f17f85c5 |
1274 | gs_tile = GRID(gs, gx, gy); |
1275 | ds_tile = GRID(ds, gx, gy); |
1276 | dx = TODRAW(gx); |
1277 | dy = TODRAW(gy); |
1278 | |
1279 | wrong = gs->exits[lno] & LASER_WRONG; |
1280 | omitted = gs->exits[lno] & LASER_OMITTED; |
1281 | exitno = gs->exits[lno] & ~LASER_FLAGMASK; |
1282 | |
1283 | reflect = gs_tile & LASER_REFLECT; |
1284 | hit = gs_tile & LASER_HIT; |
1285 | laserval = gs_tile & ~LASER_FLAGMASK; |
1286 | |
316908ca |
1287 | if (lno == ds->flash_laserno) |
f17f85c5 |
1288 | gs_tile |= LASER_FLASHED; |
1289 | else if (!(gs->exits[lno] & (LASER_HIT | LASER_REFLECT))) { |
316908ca |
1290 | if (exitno == ds->flash_laserno) |
f17f85c5 |
1291 | gs_tile |= LASER_FLASHED; |
1292 | } |
1293 | if (gs_tile & LASER_FLASHED) flash = 1; |
1294 | |
1295 | gs_tile |= wrong | omitted; |
1296 | |
316908ca |
1297 | if (ui->cur_visible && ui->cur_x == gx && ui->cur_y == gy) |
1298 | gs_tile |= FLAG_CURSOR; |
1299 | |
f17f85c5 |
1300 | if (gs_tile != ds_tile || force) { |
dafd6cf6 |
1301 | draw_rect(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_BACKGROUND); |
1302 | draw_rect_outline(dr, dx, dy, TILE_SIZE, TILE_SIZE, COL_GRID); |
f17f85c5 |
1303 | |
316908ca |
1304 | if (gs_tile &~ (LASER_WRONG | LASER_OMITTED | FLAG_CURSOR)) { |
092e9395 |
1305 | char str[32]; |
f17f85c5 |
1306 | int tcol = flash ? COL_FLASHTEXT : omitted ? COL_WRONG : COL_TEXT; |
1307 | |
1308 | if (reflect || hit) |
1309 | sprintf(str, "%s", reflect ? "R" : "H"); |
1310 | else |
1311 | sprintf(str, "%d", laserval); |
1312 | |
1313 | if (wrong) { |
dafd6cf6 |
1314 | draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, |
f17f85c5 |
1315 | ds->rrad, |
1316 | COL_WRONG, COL_WRONG); |
dafd6cf6 |
1317 | draw_circle(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, |
f17f85c5 |
1318 | ds->rrad - TILE_SIZE/16, |
1319 | COL_BACKGROUND, COL_WRONG); |
1320 | } |
1321 | |
dafd6cf6 |
1322 | draw_text(dr, dx + TILE_SIZE/2, dy + TILE_SIZE/2, |
f17f85c5 |
1323 | FONT_VARIABLE, TILE_SIZE/2, ALIGN_VCENTRE | ALIGN_HCENTRE, |
1324 | tcol, str); |
1325 | } |
316908ca |
1326 | if (gs_tile & FLAG_CURSOR) |
1327 | draw_square_cursor(dr, ds, dx, dy); |
1328 | |
dafd6cf6 |
1329 | draw_update(dr, dx, dy, TILE_SIZE, TILE_SIZE); |
f17f85c5 |
1330 | } |
1331 | GRID(ds, gx, gy) = gs_tile; |
1332 | } |
1333 | |
316908ca |
1334 | #define CUR_ANIM 0.2F |
f17f85c5 |
1335 | |
dafd6cf6 |
1336 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
f17f85c5 |
1337 | game_state *state, int dir, game_ui *ui, |
1338 | float animtime, float flashtime) |
1339 | { |
1340 | int i, x, y, ts = TILE_SIZE, isflash = 0, force = 0; |
1341 | |
1342 | if (flashtime > 0) { |
1343 | int frame = (int)(flashtime / FLASH_FRAME); |
1344 | isflash = (frame % 2) == 0; |
f17f85c5 |
1345 | debug(("game_redraw: flashtime = %f", flashtime)); |
1346 | } |
1347 | |
1348 | if (!ds->started) { |
1349 | int x0 = TODRAW(0)-1, y0 = TODRAW(0)-1; |
1350 | int x1 = TODRAW(state->w+2), y1 = TODRAW(state->h+2); |
1351 | |
dafd6cf6 |
1352 | draw_rect(dr, 0, 0, |
f17f85c5 |
1353 | TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3), |
1354 | COL_BACKGROUND); |
1355 | |
1356 | /* clockwise around the outline starting at pt behind (1,1). */ |
dafd6cf6 |
1357 | draw_line(dr, x0+ts, y0+ts, x0+ts, y0, COL_HIGHLIGHT); |
1358 | draw_line(dr, x0+ts, y0, x1-ts, y0, COL_HIGHLIGHT); |
1359 | draw_line(dr, x1-ts, y0, x1-ts, y0+ts, COL_LOWLIGHT); |
1360 | draw_line(dr, x1-ts, y0+ts, x1, y0+ts, COL_HIGHLIGHT); |
1361 | draw_line(dr, x1, y0+ts, x1, y1-ts, COL_LOWLIGHT); |
1362 | draw_line(dr, x1, y1-ts, x1-ts, y1-ts, COL_LOWLIGHT); |
1363 | draw_line(dr, x1-ts, y1-ts, x1-ts, y1, COL_LOWLIGHT); |
1364 | draw_line(dr, x1-ts, y1, x0+ts, y1, COL_LOWLIGHT); |
1365 | draw_line(dr, x0+ts, y1, x0+ts, y1-ts, COL_HIGHLIGHT); |
1366 | draw_line(dr, x0+ts, y1-ts, x0, y1-ts, COL_LOWLIGHT); |
1367 | draw_line(dr, x0, y1-ts, x0, y0+ts, COL_HIGHLIGHT); |
1368 | draw_line(dr, x0, y0+ts, x0+ts, y0+ts, COL_HIGHLIGHT); |
f17f85c5 |
1369 | /* phew... */ |
1370 | |
dafd6cf6 |
1371 | draw_update(dr, 0, 0, |
f17f85c5 |
1372 | TILE_SIZE * (state->w+3), TILE_SIZE * (state->h+3)); |
1373 | force = 1; |
1374 | ds->started = 1; |
1375 | } |
1376 | |
7dfe3b1f |
1377 | if (isflash != ds->isflash) force = 1; |
1378 | |
f17f85c5 |
1379 | /* draw the arena */ |
1380 | for (x = 0; x < state->w; x++) { |
1381 | for (y = 0; y < state->h; y++) { |
316908ca |
1382 | draw_arena_tile(dr, state, ds, ui, x, y, force, isflash); |
f17f85c5 |
1383 | } |
1384 | } |
1385 | |
1386 | /* draw the lasers */ |
316908ca |
1387 | ds->flash_laserno = LASER_EMPTY; |
1388 | if (ui->flash_laser == 1) |
1389 | ds->flash_laserno = ui->flash_laserno; |
1390 | else if (ui->flash_laser == 2 && animtime > 0) |
1391 | ds->flash_laserno = ui->flash_laserno; |
1392 | |
f17f85c5 |
1393 | for (i = 0; i < 2*(state->w+state->h); i++) { |
dafd6cf6 |
1394 | draw_laser_tile(dr, state, ds, ui, i, force); |
f17f85c5 |
1395 | } |
1396 | |
1397 | /* draw the 'finish' button */ |
27388471 |
1398 | if (CAN_REVEAL(state)) { |
316908ca |
1399 | int outline = (ui->cur_visible && ui->cur_x == 0 && ui->cur_y == 0) |
1400 | ? COL_CURSOR : COL_BALL; |
dafd6cf6 |
1401 | clip(dr, TODRAW(0), TODRAW(0), TILE_SIZE-1, TILE_SIZE-1); |
1402 | draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad, |
316908ca |
1403 | outline, outline); |
1404 | draw_circle(dr, TODRAW(0) + ds->crad, TODRAW(0) + ds->crad, ds->crad-2, |
1405 | COL_BUTTON, COL_BUTTON); |
dafd6cf6 |
1406 | unclip(dr); |
f17f85c5 |
1407 | } else { |
dafd6cf6 |
1408 | draw_rect(dr, TODRAW(0), TODRAW(0), |
f17f85c5 |
1409 | TILE_SIZE-1, TILE_SIZE-1, COL_BACKGROUND); |
f17f85c5 |
1410 | } |
dafd6cf6 |
1411 | draw_update(dr, TODRAW(0), TODRAW(0), TILE_SIZE, TILE_SIZE); |
f17f85c5 |
1412 | ds->reveal = state->reveal; |
7dfe3b1f |
1413 | ds->isflash = isflash; |
f17f85c5 |
1414 | |
1415 | { |
1416 | char buf[256]; |
1417 | |
1418 | if (ds->reveal) { |
1419 | if (state->nwrong == 0 && |
1420 | state->nmissed == 0 && |
1421 | state->nright >= state->minballs) |
1422 | sprintf(buf, "CORRECT!"); |
1423 | else |
1424 | sprintf(buf, "%d wrong and %d missed balls.", |
1425 | state->nwrong, state->nmissed); |
27388471 |
1426 | } else if (state->justwrong) { |
1427 | sprintf(buf, "Wrong! Guess again."); |
1428 | } else { |
f17f85c5 |
1429 | if (state->nguesses > state->maxballs) |
1430 | sprintf(buf, "%d too many balls marked.", |
1431 | state->nguesses - state->maxballs); |
1432 | else if (state->nguesses <= state->maxballs && |
1433 | state->nguesses >= state->minballs) |
1434 | sprintf(buf, "Click button to verify guesses."); |
1435 | else if (state->maxballs == state->minballs) |
1436 | sprintf(buf, "Balls marked: %d / %d", |
1437 | state->nguesses, state->minballs); |
1438 | else |
1439 | sprintf(buf, "Balls marked: %d / %d-%d.", |
1440 | state->nguesses, state->minballs, state->maxballs); |
1441 | } |
27388471 |
1442 | if (ui->errors) { |
1443 | sprintf(buf + strlen(buf), " (%d error%s)", |
1444 | ui->errors, ui->errors > 1 ? "s" : ""); |
1445 | } |
dafd6cf6 |
1446 | status_bar(dr, buf); |
f17f85c5 |
1447 | } |
1448 | } |
1449 | |
1450 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
1451 | int dir, game_ui *ui) |
1452 | { |
316908ca |
1453 | return (ui->flash_laser == 2) ? CUR_ANIM : 0.0F; |
f17f85c5 |
1454 | } |
1455 | |
1456 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
1457 | int dir, game_ui *ui) |
1458 | { |
1459 | if (!oldstate->reveal && newstate->reveal) |
1460 | return 4.0F * FLASH_FRAME; |
1461 | else |
1462 | return 0.0F; |
1463 | } |
1464 | |
f17f85c5 |
1465 | static int game_timing_state(game_state *state, game_ui *ui) |
1466 | { |
1467 | return TRUE; |
1468 | } |
1469 | |
dafd6cf6 |
1470 | static void game_print_size(game_params *params, float *x, float *y) |
1471 | { |
1472 | } |
1473 | |
1474 | static void game_print(drawing *dr, game_state *state, int tilesize) |
1475 | { |
1476 | } |
1477 | |
f17f85c5 |
1478 | #ifdef COMBINED |
1479 | #define thegame blackbox |
1480 | #endif |
1481 | |
1482 | const struct game thegame = { |
750037d7 |
1483 | "Black Box", "games.blackbox", "blackbox", |
f17f85c5 |
1484 | default_params, |
1485 | game_fetch_preset, |
1486 | decode_params, |
1487 | encode_params, |
1488 | free_params, |
1489 | dup_params, |
1490 | TRUE, game_configure, custom_params, |
1491 | validate_params, |
1492 | new_game_desc, |
1493 | validate_desc, |
1494 | new_game, |
1495 | dup_game, |
1496 | free_game, |
1497 | TRUE, solve_game, |
fa3abef5 |
1498 | FALSE, game_can_format_as_text_now, game_text_format, |
f17f85c5 |
1499 | new_ui, |
1500 | free_ui, |
1501 | encode_ui, |
1502 | decode_ui, |
1503 | game_changed_state, |
1504 | interpret_move, |
1505 | execute_move, |
1506 | PREFERRED_TILE_SIZE, game_compute_size, game_set_size, |
1507 | game_colours, |
1508 | game_new_drawstate, |
1509 | game_free_drawstate, |
1510 | game_redraw, |
1511 | game_anim_length, |
1512 | game_flash_length, |
dafd6cf6 |
1513 | FALSE, FALSE, game_print_size, game_print, |
ac9f41c4 |
1514 | TRUE, /* wants_statusbar */ |
f17f85c5 |
1515 | FALSE, game_timing_state, |
cb0c7d4a |
1516 | REQUIRE_RBUTTON, /* flags */ |
f17f85c5 |
1517 | }; |
1518 | |
1519 | /* vim: set shiftwidth=4 tabstop=8: */ |