Fix GTK casts to restore correct compilation on GTK 2.0 after r6022.
[sgt/puzzles] / guess.c
CommitLineData
74476385 1/*
2 * guess.c: Mastermind clone.
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 FLASH_FRAME 0.5F
15
16enum {
17 COL_BACKGROUND,
c6203e43 18 COL_FRAME, COL_CURSOR, COL_FLASH, COL_HOLD,
74476385 19 COL_EMPTY, /* must be COL_1 - 1 */
20 COL_1, COL_2, COL_3, COL_4, COL_5, COL_6, COL_7, COL_8, COL_9, COL_10,
21 COL_CORRECTPLACE, COL_CORRECTCOLOUR,
22 NCOLOURS
23};
24
25struct game_params {
26 int ncolours, npegs, nguesses;
c6203e43 27 int allow_blank, allow_multiple;
74476385 28};
29
30#define FEEDBACK_CORRECTPLACE 1
31#define FEEDBACK_CORRECTCOLOUR 2
32
33typedef struct pegrow {
34 int npegs;
35 int *pegs; /* 0 is 'empty' */
36 int *feedback; /* may well be unused */
37} *pegrow;
38
39struct game_state {
40 game_params params;
41 pegrow *guesses; /* length params->nguesses */
42 pegrow solution;
43 int next_go; /* from 0 to nguesses-1;
44 if next_go == nguesses then they've lost. */
45 int solved;
46};
47
48static game_params *default_params(void)
49{
50 game_params *ret = snew(game_params);
51
52 /* AFAIK this is the canonical Mastermind ruleset. */
53 ret->ncolours = 6;
54 ret->npegs = 4;
55 ret->nguesses = 10;
56
c6203e43 57 ret->allow_blank = 0;
58 ret->allow_multiple = 1;
59
74476385 60 return ret;
61}
62
74476385 63static void free_params(game_params *params)
64{
65 sfree(params);
66}
67
68static game_params *dup_params(game_params *params)
69{
70 game_params *ret = snew(game_params);
71 *ret = *params; /* structure copy */
72 return ret;
73}
74
51cda7fe 75static const struct {
76 char *name;
77 game_params params;
78} guess_presets[] = {
79 {"Standard", {6, 4, 10, FALSE, TRUE}},
80 {"Super", {8, 5, 12, FALSE, TRUE}},
81};
82
83
84static int game_fetch_preset(int i, char **name, game_params **params)
85{
86 if (i < 0 || i >= lenof(guess_presets))
87 return FALSE;
88
89 *name = dupstr(guess_presets[i].name);
90 /*
91 * get round annoying const issues
92 */
93 {
94 game_params tmp = guess_presets[i].params;
95 *params = dup_params(&tmp);
96 }
97
98 return TRUE;
99}
100
74476385 101static void decode_params(game_params *params, char const *string)
102{
103 char const *p = string;
104 game_params *defs = default_params();
105
106 *params = *defs; free_params(defs);
107
108 while (*p) {
109 switch (*p++) {
110 case 'c':
111 params->ncolours = atoi(p);
112 while (*p && isdigit((unsigned char)*p)) p++;
113 break;
114
115 case 'p':
116 params->npegs = atoi(p);
117 while (*p && isdigit((unsigned char)*p)) p++;
118 break;
119
120 case 'g':
121 params->nguesses = atoi(p);
122 while (*p && isdigit((unsigned char)*p)) p++;
123 break;
124
c6203e43 125 case 'b':
126 params->allow_blank = 1;
127 break;
128
129 case 'B':
130 params->allow_blank = 0;
131 break;
132
133 case 'm':
134 params->allow_multiple = 1;
135 break;
136
137 case 'M':
138 params->allow_multiple = 0;
139 break;
140
74476385 141 default:
142 ;
143 }
144 }
145}
146
147static char *encode_params(game_params *params, int full)
148{
149 char data[256];
150
c6203e43 151 sprintf(data, "c%dp%dg%d%s%s",
152 params->ncolours, params->npegs, params->nguesses,
153 params->allow_blank ? "b" : "B", params->allow_multiple ? "m" : "M");
74476385 154
155 return dupstr(data);
156}
157
158static config_item *game_configure(game_params *params)
159{
160 config_item *ret;
161 char buf[80];
162
c6203e43 163 ret = snewn(6, config_item);
74476385 164
c6203e43 165 ret[0].name = "Colours";
74476385 166 ret[0].type = C_STRING;
167 sprintf(buf, "%d", params->ncolours);
168 ret[0].sval = dupstr(buf);
169 ret[0].ival = 0;
170
c6203e43 171 ret[1].name = "Pegs per guess";
74476385 172 ret[1].type = C_STRING;
173 sprintf(buf, "%d", params->npegs);
174 ret[1].sval = dupstr(buf);
175 ret[1].ival = 0;
176
c6203e43 177 ret[2].name = "Guesses";
74476385 178 ret[2].type = C_STRING;
179 sprintf(buf, "%d", params->nguesses);
180 ret[2].sval = dupstr(buf);
181 ret[2].ival = 0;
182
c6203e43 183 ret[3].name = "Allow blanks";
184 ret[3].type = C_BOOLEAN;
74476385 185 ret[3].sval = NULL;
c6203e43 186 ret[3].ival = params->allow_blank;
187
188 ret[4].name = "Allow duplicates";
189 ret[4].type = C_BOOLEAN;
190 ret[4].sval = NULL;
191 ret[4].ival = params->allow_multiple;
192
193 ret[5].name = NULL;
194 ret[5].type = C_END;
195 ret[5].sval = NULL;
196 ret[5].ival = 0;
74476385 197
198 return ret;
199}
200
201static game_params *custom_params(config_item *cfg)
202{
203 game_params *ret = snew(game_params);
204
205 ret->ncolours = atoi(cfg[0].sval);
206 ret->npegs = atoi(cfg[1].sval);
207 ret->nguesses = atoi(cfg[2].sval);
208
c6203e43 209 ret->allow_blank = cfg[3].ival;
210 ret->allow_multiple = cfg[4].ival;
211
74476385 212 return ret;
213}
214
215static char *validate_params(game_params *params)
216{
217 if (params->ncolours < 2 || params->npegs < 2)
218 return "Trivial solutions are uninteresting";
219 /* NB as well as the no. of colours we define, max(ncolours) must
220 * also fit in an unsigned char; see new_game_desc. */
221 if (params->ncolours > 10)
222 return "Too many colours";
223 if (params->nguesses < 1)
224 return "Must have at least one guess";
c6203e43 225 if (!params->allow_multiple && params->ncolours < params->npegs)
226 return "Disallowing multiple colours requires at least as many colours as pegs";
74476385 227 return NULL;
228}
229
230static pegrow new_pegrow(int npegs)
231{
232 pegrow pegs = snew(struct pegrow);
233
234 pegs->npegs = npegs;
235 pegs->pegs = snewn(pegs->npegs, int);
236 memset(pegs->pegs, 0, pegs->npegs * sizeof(int));
237 pegs->feedback = snewn(pegs->npegs, int);
238 memset(pegs->feedback, 0, pegs->npegs * sizeof(int));
239
240 return pegs;
241}
242
243static pegrow dup_pegrow(pegrow pegs)
244{
6b98d12c 245 pegrow newpegs = new_pegrow(pegs->npegs);
74476385 246
74476385 247 memcpy(newpegs->pegs, pegs->pegs, newpegs->npegs * sizeof(int));
74476385 248 memcpy(newpegs->feedback, pegs->feedback, newpegs->npegs * sizeof(int));
249
250 return newpegs;
251}
252
253static void invalidate_pegrow(pegrow pegs)
254{
255 memset(pegs->pegs, -1, pegs->npegs * sizeof(int));
256 memset(pegs->feedback, -1, pegs->npegs * sizeof(int));
257}
258
259static void free_pegrow(pegrow pegs)
260{
261 sfree(pegs->pegs);
262 sfree(pegs->feedback);
263 sfree(pegs);
264}
265
266static char *new_game_desc(game_params *params, random_state *rs,
267 game_aux_info **aux, int interactive)
268{
269 unsigned char *bmp = snewn(params->npegs, unsigned char);
270 char *ret;
c6203e43 271 int i, c;
272 pegrow colcount = new_pegrow(params->ncolours);
273
274 for (i = 0; i < params->npegs; i++) {
275newcol:
276 c = random_upto(rs, params->ncolours);
277 if (!params->allow_multiple && colcount->pegs[c]) goto newcol;
278 colcount->pegs[c]++;
279 bmp[i] = (unsigned char)(c+1);
280 }
74476385 281 obfuscate_bitmap(bmp, params->npegs*8, FALSE);
282
283 ret = bin2hex(bmp, params->npegs);
284 sfree(bmp);
c6203e43 285 free_pegrow(colcount);
74476385 286 return ret;
287}
288
289static void game_free_aux_info(game_aux_info *aux)
290{
291 assert(!"Shouldn't happen");
292}
293
294static char *validate_desc(game_params *params, char *desc)
295{
c6203e43 296 unsigned char *bmp;
297 int i;
298
299 /* desc is just an (obfuscated) bitmap of the solution; check that
300 * it's the correct length and (when unobfuscated) contains only
301 * sensible colours. */
74476385 302 if (strlen(desc) != params->npegs * 2)
303 return "Game description is wrong length";
c6203e43 304 bmp = hex2bin(desc, params->npegs);
305 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
306 for (i = 0; i < params->npegs; i++) {
307 if (bmp[i] < 1 || bmp[i] > params->ncolours) {
308 sfree(bmp);
309 return "Game description is corrupted";
310 }
311 }
312 sfree(bmp);
313
74476385 314 return NULL;
315}
316
317static game_state *new_game(midend_data *me, game_params *params, char *desc)
318{
319 game_state *state = snew(game_state);
320 unsigned char *bmp;
321 int i;
322
323 state->params = *params;
324 state->guesses = snewn(params->nguesses, pegrow);
325 for (i = 0; i < params->nguesses; i++)
326 state->guesses[i] = new_pegrow(params->npegs);
327 state->solution = new_pegrow(params->npegs);
328
329 bmp = hex2bin(desc, params->npegs);
330 obfuscate_bitmap(bmp, params->npegs*8, TRUE);
331 for (i = 0; i < params->npegs; i++)
332 state->solution->pegs[i] = (int)bmp[i];
333 sfree(bmp);
334
335 state->next_go = state->solved = 0;
336
337 return state;
338}
339
340static game_state *dup_game(game_state *state)
341{
342 game_state *ret = snew(game_state);
343 int i;
344
345 *ret = *state;
6b98d12c 346
74476385 347 ret->guesses = snewn(state->params.nguesses, pegrow);
348 for (i = 0; i < state->params.nguesses; i++)
349 ret->guesses[i] = dup_pegrow(state->guesses[i]);
350 ret->solution = dup_pegrow(state->solution);
351
352 return ret;
353}
354
355static void free_game(game_state *state)
356{
357 int i;
358
359 free_pegrow(state->solution);
360 for (i = 0; i < state->params.nguesses; i++)
361 free_pegrow(state->guesses[i]);
362 sfree(state->guesses);
363
364 sfree(state);
365}
366
367static game_state *solve_game(game_state *state, game_state *currstate,
368 game_aux_info *aux, char **error)
369{
370 game_state *ret = dup_game(currstate);
371 ret->solved = 1;
372 return ret;
373}
374
375static char *game_text_format(game_state *state)
376{
377 return NULL;
378}
379
380struct game_ui {
381 pegrow curr_pegs; /* half-finished current move */
382 int *holds;
383 int colour_cur; /* position of up-down colour picker cursor */
384 int peg_cur; /* position of left-right peg picker cursor */
385 int display_cur, markable;
386
387 int drag_col, drag_x, drag_y; /* x and y are *center* of peg! */
c6203e43 388 int drag_opeg; /* peg index, if dragged from a peg (from current guess), otherwise -1 */
74476385 389};
390
391static game_ui *new_ui(game_state *state)
392{
393 game_ui *ui = snew(struct game_ui);
394 memset(ui, 0, sizeof(struct game_ui));
395 ui->curr_pegs = new_pegrow(state->params.npegs);
396 ui->holds = snewn(state->params.npegs, int);
397 memset(ui->holds, 0, sizeof(int)*state->params.npegs);
c6203e43 398 ui->drag_opeg = -1;
74476385 399 return ui;
400}
401
402static void free_ui(game_ui *ui)
403{
404 free_pegrow(ui->curr_pegs);
405 sfree(ui->holds);
406 sfree(ui);
407}
408
409static void game_changed_state(game_ui *ui, game_state *oldstate,
410 game_state *newstate)
411{
412 int i;
413
414 /* just clear the row-in-progress when we have an undo/redo. */
415 for (i = 0; i < ui->curr_pegs->npegs; i++)
416 ui->curr_pegs->pegs[i] = 0;
75953cd4 417 ui->markable = FALSE;
74476385 418}
419
420#define PEGSZ (ds->pegsz)
421#define PEGOFF (ds->pegsz + ds->gapsz)
422#define HINTSZ (ds->hintsz)
423#define HINTOFF (ds->hintsz + ds->gapsz)
424
74b36224 425#define GAP (ds->gapsz)
66732e7a 426#define CGAP (ds->gapsz / 2)
427
74476385 428#define PEGRAD (ds->pegrad)
429#define HINTRAD (ds->hintrad)
430
431#define COL_OX (ds->colx)
432#define COL_OY (ds->coly)
433#define COL_X(c) (COL_OX)
434#define COL_Y(c) (COL_OY + (c)*PEGOFF)
435#define COL_W PEGOFF
436#define COL_H (ds->colours->npegs*PEGOFF)
437
438#define GUESS_OX (ds->guessx)
439#define GUESS_OY (ds->guessy)
440#define GUESS_X(g,p) (GUESS_OX + (p)*PEGOFF)
441#define GUESS_Y(g,p) (GUESS_OY + (g)*PEGOFF)
442#define GUESS_W (ds->solution->npegs*PEGOFF)
443#define GUESS_H (ds->nguesses*PEGOFF)
444
445#define HINT_OX (GUESS_OX + GUESS_W + ds->gapsz)
446#define HINT_OY (GUESS_OY + (PEGSZ - HINTOFF - HINTSZ) / 2)
447#define HINT_X(g) HINT_OX
448#define HINT_Y(g) (HINT_OY + (g)*PEGOFF)
74b36224 449#define HINT_W ((ds->hintw*HINTOFF) - GAP)
74476385 450#define HINT_H GUESS_H
451
452#define SOLN_OX GUESS_OX
453#define SOLN_OY (GUESS_OY + GUESS_H + ds->gapsz + 2)
454#define SOLN_W GUESS_W
455#define SOLN_H PEGOFF
456
457struct game_drawstate {
458 int nguesses;
459 pegrow *guesses; /* same size as state->guesses */
460 pegrow solution; /* only displayed if state->solved */
461 pegrow colours; /* length ncolours, not npegs */
462
74476385 463 int pegsz, hintsz, gapsz; /* peg size (diameter), etc. */
464 int pegrad, hintrad; /* radius of peg, hint */
465 int border;
466 int colx, coly; /* origin of colours vertical bar */
467 int guessx, guessy; /* origin of guesses */
468 int solnx, solny; /* origin of solution */
469 int hintw; /* no. of hint tiles we're wide per row */
470 int w, h, started, solved;
471
74476385 472 int next_go;
473
474 blitter *blit_peg;
475 int drag_col, blit_ox, blit_oy;
476};
477
c6203e43 478static int is_markable(game_params *params, pegrow pegs)
74476385 479{
c6203e43 480 int i, nset = 0, nrequired, ret = 0;
481 pegrow colcount = new_pegrow(params->ncolours);
74476385 482
c6203e43 483 nrequired = params->allow_blank ? 1 : params->npegs;
484
485 for (i = 0; i < params->npegs; i++) {
6b98d12c 486 int c = pegs->pegs[i];
487 if (c > 0) {
488 colcount->pegs[c-1]++;
c6203e43 489 nset++;
490 }
491 }
492 if (nset < nrequired) goto done;
74476385 493
c6203e43 494 if (!params->allow_multiple) {
495 for (i = 0; i < params->ncolours; i++) {
496 if (colcount->pegs[i] > 1) goto done;
497 }
74476385 498 }
c6203e43 499 ret = 1;
500done:
501 free_pegrow(colcount);
502 return ret;
503}
504
505static void set_peg(game_params *params, game_ui *ui, int peg, int col)
506{
507 ui->curr_pegs->pegs[peg] = col;
508 ui->markable = is_markable(params, ui->curr_pegs);
74476385 509}
510
511static int mark_pegs(pegrow guess, pegrow solution, int ncols)
512{
513 int nc_place = 0, nc_colour = 0, i, j;
514
515 assert(guess && solution && (guess->npegs == solution->npegs));
516
517 for (i = 0; i < guess->npegs; i++) {
518 if (guess->pegs[i] == solution->pegs[i]) nc_place++;
519 }
520
521 /* slight bit of cleverness: we have the following formula, from
522 * http://mathworld.wolfram.com/Mastermind.html that gives:
523 *
524 * nc_colour = sum(colours, min(#solution, #guess)) - nc_place
525 *
526 * I think this is due to Knuth.
527 */
528 for (i = 1; i <= ncols; i++) {
529 int n_guess = 0, n_solution = 0;
530 for (j = 0; j < guess->npegs; j++) {
531 if (guess->pegs[j] == i) n_guess++;
532 if (solution->pegs[j] == i) n_solution++;
533 }
534 nc_colour += min(n_guess, n_solution);
535 }
536 nc_colour -= nc_place;
537
538 debug(("mark_pegs, %d pegs, %d right place, %d right colour",
539 guess->npegs, nc_place, nc_colour));
540 assert((nc_colour + nc_place) <= guess->npegs);
541
542 memset(guess->feedback, 0, guess->npegs*sizeof(int));
543 for (i = 0, j = 0; i < nc_place; i++)
544 guess->feedback[j++] = FEEDBACK_CORRECTPLACE;
545 for (i = 0; i < nc_colour; i++)
546 guess->feedback[j++] = FEEDBACK_CORRECTCOLOUR;
547
548 return nc_place;
549}
550
551static game_state *mark_move(game_state *from, game_ui *ui)
552{
553 int i, ncleared = 0, nc_place;
554 game_state *to = dup_game(from);
555
556 for (i = 0; i < to->solution->npegs; i++) {
557 to->guesses[from->next_go]->pegs[i] = ui->curr_pegs->pegs[i];
558 }
559 nc_place = mark_pegs(to->guesses[from->next_go], to->solution, to->params.ncolours);
560
561 if (nc_place == to->solution->npegs) {
562 to->solved = 1; /* win! */
563 } else {
564 to->next_go = from->next_go + 1;
565 if (to->next_go >= to->params.nguesses)
566 to->solved = 1; /* 'lose' so we show the pegs. */
567 }
568
569 for (i = 0; i < to->solution->npegs; i++) {
570 if (!ui->holds[i] || to->solved) {
571 ui->curr_pegs->pegs[i] = 0;
572 ncleared++;
573 }
574 if (to->solved) ui->holds[i] = 0;
575 }
c6203e43 576 ui->markable = is_markable(&from->params, ui->curr_pegs);
577 if (!ui->markable && ui->peg_cur == to->solution->npegs)
578 ui->peg_cur--;
74476385 579
580 return to;
581}
582
583static game_state *make_move(game_state *from, game_ui *ui, game_drawstate *ds,
584 int x, int y, int button)
585{
586 int over_col = 0; /* one-indexed */
587 int over_guess = -1; /* zero-indexed */
e98cc9b7 588 int over_past_guess_y = -1; /* zero-indexed */
589 int over_past_guess_x = -1; /* zero-indexed */
74476385 590 int over_hint = 0; /* zero or one */
591 game_state *ret = NULL;
592
593 int guess_ox = GUESS_X(from->next_go, 0);
594 int guess_oy = GUESS_Y(from->next_go, 0);
595
596 if (from->solved) return NULL;
597
598 if (x >= COL_OX && x <= (COL_OX + COL_W) &&
599 y >= COL_OY && y <= (COL_OY + COL_H)) {
600 over_col = ((y - COL_OY) / PEGOFF) + 1;
601 } else if (x >= guess_ox &&
602 y >= guess_oy && y <= (guess_oy + GUESS_H)) {
603 if (x <= (guess_ox + GUESS_W)) {
604 over_guess = (x - guess_ox) / PEGOFF;
605 } else {
606 over_hint = 1;
607 }
e98cc9b7 608 } else if (x >= guess_ox &&
609 y >= GUESS_OY && y < guess_oy) {
610 over_past_guess_y = (y - GUESS_OY) / PEGOFF;
611 over_past_guess_x = (x - guess_ox) / PEGOFF;
74476385 612 }
e98cc9b7 613 debug(("make_move: over_col %d, over_guess %d, over_hint %d,"
c6203e43 614 " over_past_guess (%d,%d)", over_col, over_guess, over_hint,
615 over_past_guess_x, over_past_guess_y));
74476385 616
617 assert(ds->blit_peg);
618
619 /* mouse input */
620 if (button == LEFT_BUTTON) {
621 if (over_col > 0) {
622 ui->drag_col = over_col;
c6203e43 623 ui->drag_opeg = -1;
74476385 624 debug(("Start dragging from colours"));
625 } else if (over_guess > -1) {
626 int col = ui->curr_pegs->pegs[over_guess];
627 if (col) {
628 ui->drag_col = col;
c6203e43 629 ui->drag_opeg = over_guess;
74476385 630 debug(("Start dragging from a guess"));
631 }
e98cc9b7 632 } else if (over_past_guess_y > -1) {
633 int col =
634 from->guesses[over_past_guess_y]->pegs[over_past_guess_x];
635 if (col) {
636 ui->drag_col = col;
c6203e43 637 ui->drag_opeg = -1;
e98cc9b7 638 debug(("Start dragging from a past guess"));
639 }
74476385 640 }
641 if (ui->drag_col) {
642 ui->drag_x = x;
643 ui->drag_y = y;
644 debug(("Start dragging, col = %d, (%d,%d)",
645 ui->drag_col, ui->drag_x, ui->drag_y));
646 ret = from;
647 }
648 } else if (button == LEFT_DRAG && ui->drag_col) {
649 ui->drag_x = x;
650 ui->drag_y = y;
651 debug(("Keep dragging, (%d,%d)", ui->drag_x, ui->drag_y));
652 ret = from;
653 } else if (button == LEFT_RELEASE && ui->drag_col) {
654 if (over_guess > -1) {
655 debug(("Dropping colour %d onto guess peg %d",
656 ui->drag_col, over_guess));
c6203e43 657 set_peg(&from->params, ui, over_guess, ui->drag_col);
658 } else {
659 if (ui->drag_opeg > -1) {
660 debug(("Removing colour %d from peg %d",
661 ui->drag_col, ui->drag_opeg));
662 set_peg(&from->params, ui, ui->drag_opeg, 0);
663 }
74476385 664 }
665 ui->drag_col = 0;
c6203e43 666 ui->drag_opeg = -1;
eb6acfe7 667 ui->display_cur = 0;
74476385 668 debug(("Stop dragging."));
669 ret = from;
670 } else if (button == RIGHT_BUTTON) {
671 if (over_guess > -1) {
672 /* we use ths feedback in the game_ui to signify
673 * 'carry this peg to the next guess as well'. */
674 ui->holds[over_guess] = 1 - ui->holds[over_guess];
675 ret = from;
676 }
677 } else if (button == LEFT_RELEASE && over_hint && ui->markable) {
678 /* NB this won't trigger if on the end of a drag; that's on
679 * purpose, in case you drop by mistake... */
680 ret = mark_move(from, ui);
681 }
682
683 /* keyboard input */
684 if (button == CURSOR_UP || button == CURSOR_DOWN) {
685 ui->display_cur = 1;
686 if (button == CURSOR_DOWN && (ui->colour_cur+1) < from->params.ncolours)
687 ui->colour_cur++;
688 if (button == CURSOR_UP && ui->colour_cur > 0)
689 ui->colour_cur--;
690 ret = from;
691 } else if (button == CURSOR_LEFT || button == CURSOR_RIGHT) {
692 int maxcur = from->params.npegs;
693 if (ui->markable) maxcur++;
694
695 ui->display_cur = 1;
696 if (button == CURSOR_RIGHT && (ui->peg_cur+1) < maxcur)
697 ui->peg_cur++;
698 if (button == CURSOR_LEFT && ui->peg_cur > 0)
699 ui->peg_cur--;
700 ret = from;
701 } else if (button == CURSOR_SELECT || button == ' ' || button == '\r' ||
702 button == '\n') {
f467efad 703 ui->display_cur = 1;
74476385 704 if (ui->peg_cur == from->params.npegs) {
705 ret = mark_move(from, ui);
706 } else {
c6203e43 707 set_peg(&from->params, ui, ui->peg_cur, ui->colour_cur+1);
74476385 708 ret = from;
709 }
badb093d 710 } else if (button == 'H' || button == 'h') {
f467efad 711 ui->display_cur = 1;
badb093d 712 ui->holds[ui->peg_cur] = 1 - ui->holds[ui->peg_cur];
713 ret = from;
74476385 714 }
715 return ret;
716}
717
718/* ----------------------------------------------------------------------
719 * Drawing routines.
720 */
721
722#define PEG_PREFER_SZ 32
723
724/* next three are multipliers for pegsz. It will look much nicer if
725 * (2*PEG_HINT) + PEG_GAP = 1.0 as the hints are formatted like that. */
726#define PEG_GAP 0.10
727#define PEG_HINT 0.35
728
729#define BORDER 0.5
730
731static void game_size(game_params *params, game_drawstate *ds,
732 int *x, int *y, int expand)
733{
734 double hmul, vmul_c, vmul_g, vmul, szx, szy;
735 int sz, colh, guessh;
736
737 hmul = BORDER * 2.0 + /* border */
738 1.0 * 2.0 + /* vertical colour bar */
739 1.0 * params->npegs + /* guess pegs */
740 PEG_GAP * params->npegs + /* guess gaps */
741 PEG_HINT * ds->hintw + /* hint pegs */
742 PEG_GAP * (ds->hintw - 1); /* hint gaps */
743
744 vmul_c = BORDER * 2.0 + /* border */
745 1.0 * params->ncolours + /* colour pegs */
746 PEG_GAP * (params->ncolours - 1); /* colour gaps */
747
748 vmul_g = BORDER * 2.0 + /* border */
749 1.0 * (params->nguesses + 1) + /* guesses plus solution */
750 PEG_GAP * (params->nguesses + 1); /* gaps plus gap above soln */
751
752 vmul = max(vmul_c, vmul_g);
753
754 szx = *x / hmul;
755 szy = *y / vmul;
756 sz = max(min((int)szx, (int)szy), 1);
757 if (expand)
758 ds->pegsz = sz;
759 else
760 ds->pegsz = min(sz, PEG_PREFER_SZ);
761
762 ds->hintsz = (int)((double)ds->pegsz * PEG_HINT);
763 ds->gapsz = (int)((double)ds->pegsz * PEG_GAP);
764 ds->border = (int)((double)ds->pegsz * BORDER);
765
766 ds->pegrad = (ds->pegsz -1)/2; /* radius of peg to fit in pegsz (which is 2r+1) */
767 ds->hintrad = (ds->hintsz-1)/2;
768
813ca264 769 *x = (int)ceil((double)ds->pegsz * hmul);
770 *y = (int)ceil((double)ds->pegsz * vmul);
74476385 771 ds->w = *x; ds->h = *y;
772
773 colh = ((ds->pegsz + ds->gapsz) * params->ncolours) - ds->gapsz;
774 guessh = ((ds->pegsz + ds->gapsz) * params->nguesses); /* guesses */
775 guessh += ds->gapsz + ds->pegsz; /* solution */
776
777 ds->colx = ds->border;
778 ds->coly = (*y - colh) / 2;
779
780 ds->guessx = ds->solnx = ds->border + ds->pegsz * 2; /* border + colours */
781 ds->guessy = (*y - guessh) / 2;
782 ds->solny = ds->guessy + ((ds->pegsz + ds->gapsz) * params->nguesses) + ds->gapsz;
783
784 if (ds->pegsz > 0) {
785 if (ds->blit_peg) blitter_free(ds->blit_peg);
786 ds->blit_peg = blitter_new(ds->pegsz, ds->pegsz);
787 }
788}
789
790static float *game_colours(frontend *fe, game_state *state, int *ncolours)
791{
74b36224 792 float *ret = snewn(3 * NCOLOURS, float), max;
793 int i;
74476385 794
795 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
796
e98cc9b7 797 /* red */
798 ret[COL_1 * 3 + 0] = 1.0F;
74476385 799 ret[COL_1 * 3 + 1] = 0.0F;
e98cc9b7 800 ret[COL_1 * 3 + 2] = 0.0F;
74476385 801
e98cc9b7 802 /* yellow (toned down a bit due to pale grey background) */
803 ret[COL_2 * 3 + 0] = 0.7F;
804 ret[COL_2 * 3 + 1] = 0.7F;
74476385 805 ret[COL_2 * 3 + 2] = 0.0F;
806
e98cc9b7 807 /* green (also toned down) */
808 ret[COL_3 * 3 + 0] = 0.0F;
809 ret[COL_3 * 3 + 1] = 0.5F;
74476385 810 ret[COL_3 * 3 + 2] = 0.0F;
811
e98cc9b7 812 /* blue */
813 ret[COL_4 * 3 + 0] = 0.0F;
814 ret[COL_4 * 3 + 1] = 0.0F;
815 ret[COL_4 * 3 + 2] = 1.0F;
74476385 816
e98cc9b7 817 /* orange */
74476385 818 ret[COL_5 * 3 + 0] = 1.0F;
e98cc9b7 819 ret[COL_5 * 3 + 1] = 0.5F;
820 ret[COL_5 * 3 + 2] = 0.0F;
821
822 /* purple */
823 ret[COL_6 * 3 + 0] = 0.5F;
824 ret[COL_6 * 3 + 1] = 0.0F;
825 ret[COL_6 * 3 + 2] = 0.7F;
826
827 /* brown */
828 ret[COL_7 * 3 + 0] = 0.4F;
829 ret[COL_7 * 3 + 1] = 0.2F;
830 ret[COL_7 * 3 + 2] = 0.2F;
831
832 /* light blue */
833 ret[COL_8 * 3 + 0] = 0.4F;
834 ret[COL_8 * 3 + 1] = 0.7F;
835 ret[COL_8 * 3 + 2] = 1.0F;
836
837 /* light green */
838 ret[COL_9 * 3 + 0] = 0.5F;
839 ret[COL_9 * 3 + 1] = 0.8F;
74476385 840 ret[COL_9 * 3 + 2] = 0.5F;
841
e98cc9b7 842 /* pink */
74476385 843 ret[COL_10 * 3 + 0] = 1.0F;
e98cc9b7 844 ret[COL_10 * 3 + 1] = 0.6F;
74476385 845 ret[COL_10 * 3 + 2] = 1.0F;
846
847 ret[COL_FRAME * 3 + 0] = 0.0F;
848 ret[COL_FRAME * 3 + 1] = 0.0F;
849 ret[COL_FRAME * 3 + 2] = 0.0F;
850
c6203e43 851 ret[COL_CURSOR * 3 + 0] = 0.0F;
852 ret[COL_CURSOR * 3 + 1] = 0.0F;
853 ret[COL_CURSOR * 3 + 2] = 0.0F;
74476385 854
855 ret[COL_FLASH * 3 + 0] = 0.5F;
856 ret[COL_FLASH * 3 + 1] = 1.0F;
857 ret[COL_FLASH * 3 + 2] = 1.0F;
858
859 ret[COL_HOLD * 3 + 0] = 1.0F;
860 ret[COL_HOLD * 3 + 1] = 0.5F;
861 ret[COL_HOLD * 3 + 2] = 0.5F;
862
e98cc9b7 863 ret[COL_CORRECTPLACE*3 + 0] = 0.0F;
74476385 864 ret[COL_CORRECTPLACE*3 + 1] = 0.0F;
865 ret[COL_CORRECTPLACE*3 + 2] = 0.0F;
866
867 ret[COL_CORRECTCOLOUR*3 + 0] = 1.0F;
868 ret[COL_CORRECTCOLOUR*3 + 1] = 1.0F;
869 ret[COL_CORRECTCOLOUR*3 + 2] = 1.0F;
870
74b36224 871 /* We want to make sure we can distinguish COL_CORRECTCOLOUR
872 * (which we hard-code as white) from COL_BACKGROUND (which
873 * could default to white on some platforms).
874 * Code borrowed from fifteen.c. */
875 max = ret[COL_BACKGROUND*3];
876 for (i = 1; i < 3; i++)
877 if (ret[COL_BACKGROUND*3+i] > max)
878 max = ret[COL_BACKGROUND*3+i];
879 if (max * 1.2F > 1.0F) {
880 for (i = 0; i < 3; i++)
881 ret[COL_BACKGROUND*3+i] /= (max * 1.2F);
882 }
883
884 /* We also want to be able to tell the difference between BACKGROUND
885 * and EMPTY, for similar distinguishing-hint reasons. */
886 ret[COL_EMPTY * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 2.0 / 3.0;
887 ret[COL_EMPTY * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 2.0 / 3.0;
888 ret[COL_EMPTY * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 2.0 / 3.0;
889
74476385 890 *ncolours = NCOLOURS;
891 return ret;
892}
893
894static game_drawstate *game_new_drawstate(game_state *state)
895{
896 struct game_drawstate *ds = snew(struct game_drawstate);
897 int i;
898
899 memset(ds, 0, sizeof(struct game_drawstate));
900
901 ds->guesses = snewn(state->params.nguesses, pegrow);
902 ds->nguesses = state->params.nguesses;
903 for (i = 0; i < state->params.nguesses; i++) {
904 ds->guesses[i] = new_pegrow(state->params.npegs);
905 invalidate_pegrow(ds->guesses[i]);
906 }
907 ds->solution = new_pegrow(state->params.npegs);
908 invalidate_pegrow(ds->solution);
909 ds->colours = new_pegrow(state->params.ncolours);
910 invalidate_pegrow(ds->colours);
911
912 ds->hintw = (state->params.npegs+1)/2; /* must round up */
913
74476385 914 ds->blit_peg = NULL;
915
916 return ds;
917}
918
919static void game_free_drawstate(game_drawstate *ds)
920{
921 int i;
922
923 if (ds->blit_peg) blitter_free(ds->blit_peg);
924 free_pegrow(ds->colours);
925 free_pegrow(ds->solution);
926 for (i = 0; i < ds->nguesses; i++)
927 free_pegrow(ds->guesses[i]);
74476385 928 sfree(ds->guesses);
929 sfree(ds);
930}
931
385f402f 932static void draw_peg(frontend *fe, game_drawstate *ds, int cx, int cy,
933 int moving, int col)
74476385 934{
385f402f 935 /*
936 * Some platforms antialias circles, which means we shouldn't
937 * overwrite a circle of one colour with a circle of another
938 * colour without erasing the background first. However, if the
939 * peg is the one being dragged, we don't erase the background
940 * because we _want_ it to alpha-blend nicely into whatever's
941 * behind it.
942 */
943 if (!moving)
66732e7a 944 draw_rect(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2,
945 COL_BACKGROUND);
28094c81 946 if (PEGRAD > 0) {
74476385 947 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 1, COL_EMPTY + col);
28094c81 948 draw_circle(fe, cx+PEGRAD, cy+PEGRAD, PEGRAD, 0, COL_EMPTY + col);
949 } else
74476385 950 draw_rect(fe, cx, cy, PEGSZ, PEGSZ, COL_EMPTY + col);
66732e7a 951 draw_update(fe, cx-CGAP, cy-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
952}
953
954static void draw_cursor(frontend *fe, game_drawstate *ds, int x, int y)
955{
956 draw_circle(fe, x+PEGRAD, y+PEGRAD, PEGRAD+CGAP, 0, COL_CURSOR);
957
958 draw_update(fe, x-CGAP, y-CGAP, PEGSZ+CGAP*2, PEGSZ+CGAP*2);
74476385 959}
960
961static void guess_redraw(frontend *fe, game_drawstate *ds, int guess,
66732e7a 962 pegrow src, int *holds, int cur_col, int force)
74476385 963{
964 pegrow dest;
965 int rowx, rowy, i, scol;
966
967 if (guess == -1) {
968 dest = ds->solution;
969 rowx = SOLN_OX;
970 rowy = SOLN_OY;
971 } else {
972 dest = ds->guesses[guess];
973 rowx = GUESS_X(guess,0);
974 rowy = GUESS_Y(guess,0);
975 }
976 if (src) assert(src->npegs == dest->npegs);
977
978 for (i = 0; i < dest->npegs; i++) {
979 scol = src ? src->pegs[i] : 0;
66732e7a 980 if (i == cur_col)
981 scol |= 0x1000;
982 if (holds && holds[i])
983 scol |= 0x2000;
984 if ((dest->pegs[i] != scol) || force) {
985 draw_peg(fe, ds, rowx + PEGOFF * i, rowy, FALSE, scol &~ 0x3000);
986 /*
987 * Hold marker.
988 */
989 draw_rect(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
990 PEGSZ, 2, (scol & 0x2000 ? COL_HOLD : COL_BACKGROUND));
991 draw_update(fe, rowx + PEGOFF * i, rowy + PEGSZ + ds->gapsz/2,
992 PEGSZ, 2);
993 if (scol & 0x1000)
994 draw_cursor(fe, ds, rowx + PEGOFF * i, rowy);
995 }
74476385 996 dest->pegs[i] = scol;
997 }
998}
999
1000static void hint_redraw(frontend *fe, game_drawstate *ds, int guess,
75953cd4 1001 pegrow src, int force, int cursor, int markable)
74476385 1002{
1003 pegrow dest = ds->guesses[guess];
1004 int rowx, rowy, i, scol, col, hintlen;
66732e7a 1005 int need_redraw;
75953cd4 1006 int emptycol = (markable ? COL_FLASH : COL_EMPTY);
74476385 1007
1008 if (src) assert(src->npegs == dest->npegs);
1009
1010 hintlen = (dest->npegs + 1)/2;
1011
66732e7a 1012 /*
1013 * Because of the possible presence of the cursor around this
1014 * entire section, we redraw all or none of it but never part.
1015 */
1016 need_redraw = FALSE;
1017
74476385 1018 for (i = 0; i < dest->npegs; i++) {
1019 scol = src ? src->feedback[i] : 0;
66732e7a 1020 if (i == 0 && cursor)
1021 scol |= 0x1000;
75953cd4 1022 if (i == 0 && markable)
1023 scol |= 0x2000;
74476385 1024 if ((scol != dest->feedback[i]) || force) {
66732e7a 1025 need_redraw = TRUE;
1026 }
1027 dest->feedback[i] = scol;
1028 }
1029
1030 if (need_redraw) {
74b36224 1031 int hinth = HINTSZ + GAP + HINTSZ;
1032 int hx,hy,hw,hh;
1033
1034 hx = HINT_X(guess)-GAP; hy = HINT_Y(guess)-GAP;
1035 hw = HINT_W+GAP*2; hh = hinth+GAP*2;
1036
66732e7a 1037 /* erase a large background rectangle */
74b36224 1038 draw_rect(fe, hx, hy, hw, hh, COL_BACKGROUND);
66732e7a 1039
1040 for (i = 0; i < dest->npegs; i++) {
1041 scol = src ? src->feedback[i] : 0;
1042 col = ((scol == FEEDBACK_CORRECTPLACE) ? COL_CORRECTPLACE :
1043 (scol == FEEDBACK_CORRECTCOLOUR) ? COL_CORRECTCOLOUR :
1044 emptycol);
1045
74476385 1046 rowx = HINT_X(guess);
1047 rowy = HINT_Y(guess);
1048 if (i < hintlen) {
1049 rowx += HINTOFF * i;
1050 } else {
1051 rowx += HINTOFF * (i - hintlen);
1052 rowy += HINTOFF;
1053 }
28094c81 1054 if (HINTRAD > 0) {
74476385 1055 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 1, col);
28094c81 1056 draw_circle(fe, rowx+HINTRAD, rowy+HINTRAD, HINTRAD, 0, col);
1057 } else {
74476385 1058 draw_rect(fe, rowx, rowy, HINTSZ, HINTSZ, col);
28094c81 1059 }
74476385 1060 }
74b36224 1061 if (cursor) {
1062 int x1,y1,x2,y2;
1063 x1 = hx + CGAP; y1 = hy + CGAP;
1064 x2 = hx + hw - CGAP; y2 = hy + hh - CGAP;
1065 draw_line(fe, x1, y1, x2, y1, COL_CURSOR);
1066 draw_line(fe, x2, y1, x2, y2, COL_CURSOR);
1067 draw_line(fe, x2, y2, x1, y2, COL_CURSOR);
1068 draw_line(fe, x1, y2, x1, y1, COL_CURSOR);
1069 }
74476385 1070
74b36224 1071 draw_update(fe, hx, hy, hw, hh);
74476385 1072 }
1073}
1074
1075static void currmove_redraw(frontend *fe, game_drawstate *ds, int guess, int col)
1076{
1077 int ox = GUESS_X(guess, 0), oy = GUESS_Y(guess, 0), off = PEGSZ/4;
1078
1079 draw_rect(fe, ox-off-1, oy, 2, PEGSZ, col);
1080 draw_update(fe, ox-off-1, oy, 2, PEGSZ);
1081}
1082
74476385 1083static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1084 game_state *state, int dir, game_ui *ui,
1085 float animtime, float flashtime)
1086{
66732e7a 1087 int i, new_move, last_go;
74476385 1088
1089 new_move = (state->next_go != ds->next_go) || !ds->started;
1090 last_go = (state->next_go == state->params.nguesses-1);
1091
1092 if (!ds->started) {
1093 draw_rect(fe, 0, 0, ds->w, ds->h, COL_BACKGROUND);
1094 draw_rect(fe, SOLN_OX, SOLN_OY - ds->gapsz - 1, SOLN_W, 2, COL_FRAME);
1095 draw_update(fe, 0, 0, ds->w, ds->h);
1096 }
1097
1098 if (ds->drag_col != 0) {
1099 debug(("Loading from blitter."));
1100 blitter_load(fe, ds->blit_peg, ds->blit_ox, ds->blit_oy);
1101 draw_update(fe, ds->blit_ox, ds->blit_oy, PEGSZ, PEGSZ);
1102 }
1103
1104 /* draw the colours */
1105 for (i = 0; i < state->params.ncolours; i++) {
66732e7a 1106 int val = i+1;
1107 if (ui->display_cur && ui->colour_cur == i)
1108 val |= 0x1000;
1109 if (ds->colours->pegs[i] != val) {
385f402f 1110 draw_peg(fe, ds, COL_X(i), COL_Y(i), FALSE, i+1);
66732e7a 1111 if (val & 0x1000)
1112 draw_cursor(fe, ds, COL_X(i), COL_Y(i));
1113 ds->colours->pegs[i] = val;
74476385 1114 }
1115 }
1116
1117 /* draw the guesses (so far) and the hints */
1118 for (i = 0; i < state->params.nguesses; i++) {
1119 if (state->next_go > i || state->solved) {
1120 /* this info is stored in the game_state already */
66732e7a 1121 guess_redraw(fe, ds, i, state->guesses[i], NULL, -1, 0);
74476385 1122 hint_redraw(fe, ds, i, state->guesses[i],
75953cd4 1123 i == (state->next_go-1) ? 1 : 0, FALSE, FALSE);
74476385 1124 } else if (state->next_go == i) {
1125 /* this is the one we're on; the (incomplete) guess is
1126 * stored in the game_ui. */
66732e7a 1127 guess_redraw(fe, ds, i, ui->curr_pegs,
1128 ui->holds, ui->display_cur ? ui->peg_cur : -1, 0);
1129 hint_redraw(fe, ds, i, NULL, 1,
1130 ui->display_cur && ui->peg_cur == state->params.npegs,
75953cd4 1131 ui->markable);
74476385 1132 } else {
1133 /* we've not got here yet; it's blank. */
66732e7a 1134 guess_redraw(fe, ds, i, NULL, NULL, -1, 0);
75953cd4 1135 hint_redraw(fe, ds, i, NULL, 0, FALSE, FALSE);
74476385 1136 }
1137 }
1138
74476385 1139 /* draw the 'current move' and 'able to mark' sign. */
1140 if (new_move)
1141 currmove_redraw(fe, ds, ds->next_go, COL_BACKGROUND);
1142 if (!state->solved)
1143 currmove_redraw(fe, ds, state->next_go, COL_HOLD);
1144
1145 /* draw the solution (or the big rectangle) */
1146 if ((state->solved != ds->solved) || !ds->started) {
1147 draw_rect(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H,
1148 state->solved ? COL_BACKGROUND : COL_EMPTY);
1149 draw_update(fe, SOLN_OX, SOLN_OY, SOLN_W, SOLN_H);
1150 }
1151 if (state->solved)
66732e7a 1152 guess_redraw(fe, ds, -1, state->solution, NULL, -1, !ds->solved);
74476385 1153 ds->solved = state->solved;
1154
74476385 1155 ds->next_go = state->next_go;
1156
1157 /* if ui->drag_col != 0, save the screen to the blitter,
1158 * draw the peg where we saved, and set ds->drag_* == ui->drag_*. */
1159 if (ui->drag_col != 0) {
1160 int ox = ui->drag_x - (PEGSZ/2);
1161 int oy = ui->drag_y - (PEGSZ/2);
1162 debug(("Saving to blitter at (%d,%d)", ox, oy));
1163 blitter_save(fe, ds->blit_peg, ox, oy);
385f402f 1164 draw_peg(fe, ds, ox, oy, TRUE, ui->drag_col);
74476385 1165
1166 ds->blit_ox = ox; ds->blit_oy = oy;
1167 }
1168 ds->drag_col = ui->drag_col;
1169
1170 ds->started = 1;
1171}
1172
1173static float game_anim_length(game_state *oldstate, game_state *newstate,
1174 int dir, game_ui *ui)
1175{
1176 return 0.0F;
1177}
1178
1179static float game_flash_length(game_state *oldstate, game_state *newstate,
1180 int dir, game_ui *ui)
1181{
1182 return 0.0F;
1183}
1184
1185static int game_wants_statusbar(void)
1186{
1187 return FALSE;
1188}
1189
1190static int game_timing_state(game_state *state)
1191{
1192 return TRUE;
1193}
1194
1195#ifdef COMBINED
1196#define thegame guess
1197#endif
1198
1199const struct game thegame = {
61072f7b 1200 "Guess", "games.guess",
74476385 1201 default_params,
1202 game_fetch_preset,
1203 decode_params,
1204 encode_params,
1205 free_params,
1206 dup_params,
1207 TRUE, game_configure, custom_params,
1208 validate_params,
1209 new_game_desc,
1210 game_free_aux_info,
1211 validate_desc,
1212 new_game,
1213 dup_game,
1214 free_game,
1215 TRUE, solve_game,
1216 FALSE, game_text_format,
1217 new_ui,
1218 free_ui,
1219 game_changed_state,
1220 make_move,
1221 game_size,
1222 game_colours,
1223 game_new_drawstate,
1224 game_free_drawstate,
1225 game_redraw,
1226 game_anim_length,
1227 game_flash_length,
1228 game_wants_statusbar,
1229 FALSE, game_timing_state,
1230 0, /* mouse_priorities */
1231};
1232
1233/* vim: set shiftwidth=4 tabstop=8: */