Avoid leading zeroes on internally generated random seeds, _just_ in
[sgt/puzzles] / midend.c
1 /*
2 * midend.c: general middle fragment sitting between the
3 * platform-specific front end and game-specific back end.
4 * Maintains a move list, takes care of Undo and Redo commands, and
5 * processes standard keystrokes for undo/redo/new/quit.
6 */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13
14 #include "puzzles.h"
15
16 enum { DEF_PARAMS, DEF_SEED, DEF_DESC }; /* for midend_game_id_int */
17
18 struct midend_state_entry {
19 game_state *state;
20 int special; /* created by solve or restart */
21 };
22
23 struct midend_data {
24 frontend *frontend;
25 random_state *random;
26 const game *ourgame;
27
28 char *desc, *seedstr;
29 game_aux_info *aux_info;
30 enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
31 int nstates, statesize, statepos;
32
33 game_params **presets;
34 char **preset_names;
35 int npresets, presetsize;
36
37 game_params *params, *curparams;
38 struct midend_state_entry *states;
39 game_drawstate *drawstate;
40 game_state *oldstate;
41 game_ui *ui;
42 float anim_time, anim_pos;
43 float flash_time, flash_pos;
44 int dir;
45
46 int pressed_mouse_button;
47 };
48
49 #define ensure(me) do { \
50 if ((me)->nstates >= (me)->statesize) { \
51 (me)->statesize = (me)->nstates + 128; \
52 (me)->states = sresize((me)->states, (me)->statesize, \
53 struct midend_state_entry); \
54 } \
55 } while (0)
56
57 midend_data *midend_new(frontend *fe, const game *ourgame)
58 {
59 midend_data *me = snew(midend_data);
60 void *randseed;
61 int randseedsize;
62
63 get_random_seed(&randseed, &randseedsize);
64
65 me->frontend = fe;
66 me->ourgame = ourgame;
67 me->random = random_init(randseed, randseedsize);
68 me->nstates = me->statesize = me->statepos = 0;
69 me->states = NULL;
70 me->params = ourgame->default_params();
71 me->curparams = NULL;
72 me->desc = NULL;
73 me->seedstr = NULL;
74 me->aux_info = NULL;
75 me->genmode = GOT_NOTHING;
76 me->drawstate = NULL;
77 me->oldstate = NULL;
78 me->presets = NULL;
79 me->preset_names = NULL;
80 me->npresets = me->presetsize = 0;
81 me->anim_time = me->anim_pos = 0.0F;
82 me->flash_time = me->flash_pos = 0.0F;
83 me->dir = 0;
84 me->ui = NULL;
85 me->pressed_mouse_button = 0;
86
87 sfree(randseed);
88
89 return me;
90 }
91
92 void midend_free(midend_data *me)
93 {
94 sfree(me->states);
95 sfree(me->desc);
96 sfree(me->seedstr);
97 random_free(me->random);
98 if (me->aux_info)
99 me->ourgame->free_aux_info(me->aux_info);
100 me->ourgame->free_params(me->params);
101 if (me->curparams)
102 me->ourgame->free_params(me->curparams);
103 sfree(me);
104 }
105
106 void midend_size(midend_data *me, int *x, int *y)
107 {
108 me->ourgame->size(me->params, x, y);
109 }
110
111 void midend_set_params(midend_data *me, game_params *params)
112 {
113 me->ourgame->free_params(me->params);
114 me->params = me->ourgame->dup_params(params);
115 }
116
117 void midend_new_game(midend_data *me)
118 {
119 while (me->nstates > 0)
120 me->ourgame->free_game(me->states[--me->nstates].state);
121
122 if (me->drawstate)
123 me->ourgame->free_drawstate(me->drawstate);
124
125 assert(me->nstates == 0);
126
127 if (me->genmode == GOT_DESC) {
128 me->genmode = GOT_NOTHING;
129 } else {
130 random_state *rs;
131
132 if (me->genmode == GOT_SEED) {
133 me->genmode = GOT_NOTHING;
134 } else {
135 /*
136 * Generate a new random seed. 15 digits comes to about
137 * 48 bits, which should be more than enough.
138 *
139 * I'll avoid putting a leading zero on the number,
140 * just in case it confuses anybody who thinks it's
141 * processed as an integer rather than a string.
142 */
143 char newseed[16];
144 int i;
145 newseed[15] = '\0';
146 newseed[0] = '1' + random_upto(me->random, 9);
147 for (i = 1; i < 15; i++)
148 newseed[i] = '0' + random_upto(me->random, 10);
149 sfree(me->seedstr);
150 me->seedstr = dupstr(newseed);
151
152 if (me->curparams)
153 me->ourgame->free_params(me->curparams);
154 me->curparams = me->ourgame->dup_params(me->params);
155 }
156
157 sfree(me->desc);
158 if (me->aux_info)
159 me->ourgame->free_aux_info(me->aux_info);
160 me->aux_info = NULL;
161
162 rs = random_init(me->seedstr, strlen(me->seedstr));
163 me->desc = me->ourgame->new_desc(me->curparams, rs, &me->aux_info);
164 random_free(rs);
165 }
166
167 ensure(me);
168 me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
169 me->states[me->nstates].special = TRUE;
170 me->nstates++;
171 me->statepos = 1;
172 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
173 if (me->ui)
174 me->ourgame->free_ui(me->ui);
175 me->ui = me->ourgame->new_ui(me->states[0].state);
176 me->pressed_mouse_button = 0;
177 }
178
179 static int midend_undo(midend_data *me)
180 {
181 if (me->statepos > 1) {
182 me->statepos--;
183 me->dir = -1;
184 return 1;
185 } else
186 return 0;
187 }
188
189 static int midend_redo(midend_data *me)
190 {
191 if (me->statepos < me->nstates) {
192 me->statepos++;
193 me->dir = +1;
194 return 1;
195 } else
196 return 0;
197 }
198
199 static void midend_finish_move(midend_data *me)
200 {
201 float flashtime;
202
203 /*
204 * We do not flash if the later of the two states is special.
205 * This covers both forward Solve moves and backward (undone)
206 * Restart moves.
207 */
208 if ((me->oldstate || me->statepos > 1) &&
209 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
210 (me->dir < 0 && me->statepos < me->nstates &&
211 !me->states[me->statepos].special))) {
212 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
213 me->states[me->statepos-2].state,
214 me->states[me->statepos-1].state,
215 me->oldstate ? me->dir : +1);
216 if (flashtime > 0) {
217 me->flash_pos = 0.0F;
218 me->flash_time = flashtime;
219 }
220 }
221
222 if (me->oldstate)
223 me->ourgame->free_game(me->oldstate);
224 me->oldstate = NULL;
225 me->anim_pos = me->anim_time = 0;
226 me->dir = 0;
227
228 if (me->flash_time == 0 && me->anim_time == 0)
229 deactivate_timer(me->frontend);
230 else
231 activate_timer(me->frontend);
232 }
233
234 static void midend_stop_anim(midend_data *me)
235 {
236 if (me->oldstate || me->anim_time) {
237 midend_finish_move(me);
238 midend_redraw(me);
239 }
240 }
241
242 void midend_restart_game(midend_data *me)
243 {
244 game_state *s;
245
246 midend_stop_anim(me);
247
248 assert(me->statepos >= 1);
249 if (me->statepos == 1)
250 return; /* no point doing anything at all! */
251
252 s = me->ourgame->dup_game(me->states[0].state);
253
254 /*
255 * Now enter the restarted state as the next move.
256 */
257 midend_stop_anim(me);
258 while (me->nstates > me->statepos)
259 me->ourgame->free_game(me->states[--me->nstates].state);
260 ensure(me);
261 me->states[me->nstates].state = s;
262 me->states[me->nstates].special = TRUE; /* we just restarted */
263 me->statepos = ++me->nstates;
264 me->anim_time = 0.0;
265 midend_finish_move(me);
266 midend_redraw(me);
267 activate_timer(me->frontend);
268 }
269
270 static int midend_really_process_key(midend_data *me, int x, int y, int button)
271 {
272 game_state *oldstate =
273 me->ourgame->dup_game(me->states[me->statepos - 1].state);
274 int special = FALSE, gotspecial = FALSE;
275 float anim_time;
276
277 if (button == 'n' || button == 'N' || button == '\x0E') {
278 midend_stop_anim(me);
279 midend_new_game(me);
280 midend_redraw(me);
281 return 1; /* never animate */
282 } else if (button == 'u' || button == 'u' ||
283 button == '\x1A' || button == '\x1F') {
284 midend_stop_anim(me);
285 special = me->states[me->statepos-1].special;
286 gotspecial = TRUE;
287 if (!midend_undo(me))
288 return 1;
289 } else if (button == 'r' || button == 'R' ||
290 button == '\x12') {
291 midend_stop_anim(me);
292 if (!midend_redo(me))
293 return 1;
294 } else if (button == 'q' || button == 'Q' || button == '\x11') {
295 me->ourgame->free_game(oldstate);
296 return 0;
297 } else {
298 game_state *s =
299 me->ourgame->make_move(me->states[me->statepos-1].state,
300 me->ui, x, y, button);
301
302 if (s == me->states[me->statepos-1].state) {
303 /*
304 * make_move() is allowed to return its input state to
305 * indicate that although no move has been made, the UI
306 * state has been updated and a redraw is called for.
307 */
308 midend_redraw(me);
309 return 1;
310 } else if (s) {
311 midend_stop_anim(me);
312 while (me->nstates > me->statepos)
313 me->ourgame->free_game(me->states[--me->nstates].state);
314 ensure(me);
315 me->states[me->nstates].state = s;
316 me->states[me->nstates].special = FALSE; /* normal move */
317 me->statepos = ++me->nstates;
318 me->dir = +1;
319 } else {
320 me->ourgame->free_game(oldstate);
321 return 1;
322 }
323 }
324
325 if (!gotspecial)
326 special = me->states[me->statepos-1].special;
327
328 /*
329 * See if this move requires an animation.
330 */
331 if (special) {
332 anim_time = 0;
333 } else {
334 anim_time = me->ourgame->anim_length(oldstate,
335 me->states[me->statepos-1].state,
336 me->dir);
337 }
338
339 me->oldstate = oldstate;
340 if (anim_time > 0) {
341 me->anim_time = anim_time;
342 } else {
343 me->anim_time = 0.0;
344 midend_finish_move(me);
345 }
346 me->anim_pos = 0.0;
347
348 midend_redraw(me);
349
350 activate_timer(me->frontend);
351
352 return 1;
353 }
354
355 int midend_process_key(midend_data *me, int x, int y, int button)
356 {
357 int ret = 1;
358
359 /*
360 * Harmonise mouse drag and release messages.
361 *
362 * Some front ends might accidentally switch from sending, say,
363 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
364 * drag. (This can happen on the Mac, for example, since
365 * RIGHT_DRAG is usually done using Command+drag, and if the
366 * user accidentally releases Command half way through the drag
367 * then there will be trouble.)
368 *
369 * It would be an O(number of front ends) annoyance to fix this
370 * in the front ends, but an O(number of back ends) annoyance
371 * to have each game capable of dealing with it. Therefore, we
372 * fix it _here_ in the common midend code so that it only has
373 * to be done once.
374 *
375 * The possible ways in which things can go screwy in the front
376 * end are:
377 *
378 * - in a system containing multiple physical buttons button
379 * presses can inadvertently overlap. We can see ABab (caps
380 * meaning button-down and lowercase meaning button-up) when
381 * the user had semantically intended AaBb.
382 *
383 * - in a system where one button is simulated by means of a
384 * modifier key and another button, buttons can mutate
385 * between press and release (possibly during drag). So we
386 * can see Ab instead of Aa.
387 *
388 * Definite requirements are:
389 *
390 * - button _presses_ must never be invented or destroyed. If
391 * the user presses two buttons in succession, the button
392 * presses must be transferred to the backend unchanged. So
393 * if we see AaBb , that's fine; if we see ABab (the button
394 * presses inadvertently overlapped) we must somehow
395 * `correct' it to AaBb.
396 *
397 * - every mouse action must end up looking like a press, zero
398 * or more drags, then a release. This allows back ends to
399 * make the _assumption_ that incoming mouse data will be
400 * sane in this regard, and not worry about the details.
401 *
402 * So my policy will be:
403 *
404 * - treat any button-up as a button-up for the currently
405 * pressed button, or ignore it if there is no currently
406 * pressed button.
407 *
408 * - treat any drag as a drag for the currently pressed
409 * button, or ignore it if there is no currently pressed
410 * button.
411 *
412 * - if we see a button-down while another button is currently
413 * pressed, invent a button-up for the first one and then
414 * pass the button-down through as before.
415 *
416 */
417 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
418 if (me->pressed_mouse_button) {
419 if (IS_MOUSE_DRAG(button)) {
420 button = me->pressed_mouse_button +
421 (LEFT_DRAG - LEFT_BUTTON);
422 } else {
423 button = me->pressed_mouse_button +
424 (LEFT_RELEASE - LEFT_BUTTON);
425 }
426 } else
427 return ret; /* ignore it */
428 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
429 /*
430 * Fabricate a button-up for the previously pressed button.
431 */
432 ret = ret && midend_really_process_key
433 (me, x, y, (me->pressed_mouse_button +
434 (LEFT_RELEASE - LEFT_BUTTON)));
435 }
436
437 /*
438 * Now send on the event we originally received.
439 */
440 ret = ret && midend_really_process_key(me, x, y, button);
441
442 /*
443 * And update the currently pressed button.
444 */
445 if (IS_MOUSE_RELEASE(button))
446 me->pressed_mouse_button = 0;
447 else if (IS_MOUSE_DOWN(button))
448 me->pressed_mouse_button = button;
449
450 return ret;
451 }
452
453 void midend_redraw(midend_data *me)
454 {
455 if (me->statepos > 0 && me->drawstate) {
456 start_draw(me->frontend);
457 if (me->oldstate && me->anim_time > 0 &&
458 me->anim_pos < me->anim_time) {
459 assert(me->dir != 0);
460 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
461 me->states[me->statepos-1].state, me->dir,
462 me->ui, me->anim_pos, me->flash_pos);
463 } else {
464 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
465 me->states[me->statepos-1].state, +1 /*shrug*/,
466 me->ui, 0.0, me->flash_pos);
467 }
468 end_draw(me->frontend);
469 }
470 }
471
472 void midend_timer(midend_data *me, float tplus)
473 {
474 me->anim_pos += tplus;
475 if (me->anim_pos >= me->anim_time ||
476 me->anim_time == 0 || !me->oldstate) {
477 if (me->anim_time > 0)
478 midend_finish_move(me);
479 }
480 me->flash_pos += tplus;
481 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
482 me->flash_pos = me->flash_time = 0;
483 }
484 if (me->flash_time == 0 && me->anim_time == 0)
485 deactivate_timer(me->frontend);
486 midend_redraw(me);
487 }
488
489 float *midend_colours(midend_data *me, int *ncolours)
490 {
491 game_state *state = NULL;
492 float *ret;
493
494 if (me->nstates == 0) {
495 game_aux_info *aux = NULL;
496 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
497 state = me->ourgame->new_game(me->params, desc);
498 sfree(desc);
499 if (aux)
500 me->ourgame->free_aux_info(aux);
501 } else
502 state = me->states[0].state;
503
504 ret = me->ourgame->colours(me->frontend, state, ncolours);
505
506 {
507 int i;
508
509 /*
510 * Allow environment-based overrides for the standard
511 * colours by defining variables along the lines of
512 * `NET_COLOUR_4=6000c0'.
513 */
514
515 for (i = 0; i < *ncolours; i++) {
516 char buf[80], *e;
517 unsigned int r, g, b;
518 int j;
519
520 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
521 for (j = 0; buf[j]; j++)
522 buf[j] = toupper((unsigned char)buf[j]);
523 if ((e = getenv(buf)) != NULL &&
524 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
525 ret[i*3 + 0] = r / 255.0;
526 ret[i*3 + 1] = g / 255.0;
527 ret[i*3 + 2] = b / 255.0;
528 }
529 }
530 }
531
532 if (me->nstates == 0)
533 me->ourgame->free_game(state);
534
535 return ret;
536 }
537
538 int midend_num_presets(midend_data *me)
539 {
540 if (!me->npresets) {
541 char *name;
542 game_params *preset;
543
544 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
545 if (me->presetsize <= me->npresets) {
546 me->presetsize = me->npresets + 10;
547 me->presets = sresize(me->presets, me->presetsize,
548 game_params *);
549 me->preset_names = sresize(me->preset_names, me->presetsize,
550 char *);
551 }
552
553 me->presets[me->npresets] = preset;
554 me->preset_names[me->npresets] = name;
555 me->npresets++;
556 }
557 }
558
559 {
560 /*
561 * Allow environment-based extensions to the preset list by
562 * defining a variable along the lines of `SOLO_PRESETS=2x3
563 * Advanced:2x3da'. Colon-separated list of items,
564 * alternating between textual titles in the menu and
565 * encoded parameter strings.
566 */
567 char buf[80], *e, *p;
568 int j;
569
570 sprintf(buf, "%s_PRESETS", me->ourgame->name);
571 for (j = 0; buf[j]; j++)
572 buf[j] = toupper((unsigned char)buf[j]);
573
574 if ((e = getenv(buf)) != NULL) {
575 p = e = dupstr(e);
576
577 while (*p) {
578 char *name, *val;
579 game_params *preset;
580
581 name = p;
582 while (*p && *p != ':') p++;
583 if (*p) *p++ = '\0';
584 val = p;
585 while (*p && *p != ':') p++;
586 if (*p) *p++ = '\0';
587
588 preset = me->ourgame->default_params();
589 me->ourgame->decode_params(preset, val);
590
591 if (me->ourgame->validate_params(preset)) {
592 /* Drop this one from the list. */
593 me->ourgame->free_params(preset);
594 continue;
595 }
596
597 if (me->presetsize <= me->npresets) {
598 me->presetsize = me->npresets + 10;
599 me->presets = sresize(me->presets, me->presetsize,
600 game_params *);
601 me->preset_names = sresize(me->preset_names,
602 me->presetsize, char *);
603 }
604
605 me->presets[me->npresets] = preset;
606 me->preset_names[me->npresets] = name;
607 me->npresets++;
608 }
609 }
610 }
611
612 return me->npresets;
613 }
614
615 void midend_fetch_preset(midend_data *me, int n,
616 char **name, game_params **params)
617 {
618 assert(n >= 0 && n < me->npresets);
619 *name = me->preset_names[n];
620 *params = me->presets[n];
621 }
622
623 int midend_wants_statusbar(midend_data *me)
624 {
625 return me->ourgame->wants_statusbar();
626 }
627
628 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
629 {
630 char *titlebuf, *parstr;
631 config_item *ret;
632
633 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
634
635 switch (which) {
636 case CFG_SETTINGS:
637 sprintf(titlebuf, "%s configuration", me->ourgame->name);
638 *wintitle = dupstr(titlebuf);
639 return me->ourgame->configure(me->params);
640 case CFG_SEED:
641 case CFG_DESC:
642 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
643 which == CFG_SEED ? "random" : "game");
644 *wintitle = dupstr(titlebuf);
645
646 ret = snewn(2, config_item);
647
648 ret[0].type = C_STRING;
649 if (which == CFG_SEED)
650 ret[0].name = "Game random seed";
651 else
652 ret[0].name = "Game ID";
653 ret[0].ival = 0;
654 /*
655 * For CFG_DESC the text going in here will be a string
656 * encoding of the restricted parameters, plus a colon,
657 * plus the game description. For CFG_SEED it will be the
658 * full parameters, plus a hash, plus the random seed data.
659 * Either of these is a valid full game ID (although only
660 * the former is likely to persist across many code
661 * changes).
662 */
663 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
664 if (which == CFG_DESC) {
665 ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
666 sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
667 } else if (me->seedstr) {
668 ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
669 sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
670 } else {
671 /*
672 * If the current game was not randomly generated, the
673 * best we can do is to give a template for typing a
674 * new seed in.
675 */
676 ret[0].sval = snewn(strlen(parstr) + 2, char);
677 sprintf(ret[0].sval, "%s#", parstr);
678 }
679 sfree(parstr);
680
681 ret[1].type = C_END;
682 ret[1].name = ret[1].sval = NULL;
683 ret[1].ival = 0;
684
685 return ret;
686 }
687
688 assert(!"We shouldn't be here");
689 return NULL;
690 }
691
692 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
693 {
694 char *error, *par, *desc, *seed;
695
696 seed = strchr(id, '#');
697 desc = strchr(id, ':');
698
699 if (desc && (!seed || desc < seed)) {
700 /*
701 * We have a colon separating parameters from game
702 * description. So `par' now points to the parameters
703 * string, and `desc' to the description string.
704 */
705 *desc++ = '\0';
706 par = id;
707 seed = NULL;
708 } else if (seed && (!desc || seed < desc)) {
709 /*
710 * We have a hash separating parameters from random seed.
711 * So `par' now points to the parameters string, and `seed'
712 * to the seed string.
713 */
714 *seed++ = '\0';
715 par = id;
716 desc = NULL;
717 } else {
718 /*
719 * We only have one string. Depending on `defmode', we take
720 * it to be either parameters, seed or description.
721 */
722 if (defmode == DEF_SEED) {
723 seed = id;
724 par = desc = NULL;
725 } else if (defmode == DEF_DESC) {
726 desc = id;
727 par = seed = NULL;
728 } else {
729 par = id;
730 seed = desc = NULL;
731 }
732 }
733
734 if (par) {
735 game_params *tmpparams;
736 tmpparams = me->ourgame->dup_params(me->params);
737 me->ourgame->decode_params(tmpparams, par);
738 error = me->ourgame->validate_params(tmpparams);
739 if (error) {
740 me->ourgame->free_params(tmpparams);
741 return error;
742 }
743 if (me->curparams)
744 me->ourgame->free_params(me->curparams);
745 me->curparams = tmpparams;
746
747 /*
748 * Now filter only the persistent parts of this state into
749 * the long-term params structure, unless we've _only_
750 * received a params string in which case the whole lot is
751 * persistent.
752 */
753 if (seed || desc) {
754 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
755 me->ourgame->decode_params(me->params, tmpstr);
756 sfree(tmpstr);
757 } else {
758 me->ourgame->free_params(me->params);
759 me->params = me->ourgame->dup_params(tmpparams);
760 }
761 }
762
763 sfree(me->desc);
764 me->desc = NULL;
765 sfree(me->seedstr);
766 me->seedstr = NULL;
767
768 if (desc) {
769 error = me->ourgame->validate_desc(me->params, desc);
770 if (error)
771 return error;
772
773 me->desc = dupstr(desc);
774 me->genmode = GOT_DESC;
775 if (me->aux_info)
776 me->ourgame->free_aux_info(me->aux_info);
777 me->aux_info = NULL;
778 }
779
780 if (seed) {
781 me->seedstr = dupstr(seed);
782 me->genmode = GOT_SEED;
783 }
784
785 return NULL;
786 }
787
788 char *midend_game_id(midend_data *me, char *id)
789 {
790 return midend_game_id_int(me, id, DEF_PARAMS);
791 }
792
793 char *midend_set_config(midend_data *me, int which, config_item *cfg)
794 {
795 char *error;
796 game_params *params;
797
798 switch (which) {
799 case CFG_SETTINGS:
800 params = me->ourgame->custom_params(cfg);
801 error = me->ourgame->validate_params(params);
802
803 if (error) {
804 me->ourgame->free_params(params);
805 return error;
806 }
807
808 me->ourgame->free_params(me->params);
809 me->params = params;
810 break;
811
812 case CFG_SEED:
813 case CFG_DESC:
814 error = midend_game_id_int(me, cfg[0].sval,
815 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
816 if (error)
817 return error;
818 break;
819 }
820
821 return NULL;
822 }
823
824 char *midend_text_format(midend_data *me)
825 {
826 if (me->ourgame->can_format_as_text && me->statepos > 0)
827 return me->ourgame->text_format(me->states[me->statepos-1].state);
828 else
829 return NULL;
830 }
831
832 char *midend_solve(midend_data *me)
833 {
834 game_state *s;
835 char *msg;
836
837 if (!me->ourgame->can_solve)
838 return "This game does not support the Solve operation";
839
840 if (me->statepos < 1)
841 return "No game set up to solve"; /* _shouldn't_ happen! */
842
843 msg = "Solve operation failed"; /* game _should_ overwrite on error */
844 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
845 if (!s)
846 return msg;
847
848 /*
849 * Now enter the solved state as the next move.
850 */
851 midend_stop_anim(me);
852 while (me->nstates > me->statepos)
853 me->ourgame->free_game(me->states[--me->nstates].state);
854 ensure(me);
855 me->states[me->nstates].state = s;
856 me->states[me->nstates].special = TRUE; /* created using solve */
857 me->statepos = ++me->nstates;
858 me->anim_time = 0.0;
859 midend_finish_move(me);
860 midend_redraw(me);
861 activate_timer(me->frontend);
862 return NULL;
863 }