chiark / gitweb /
Infrastructure change: game_anim_length and game_flash_length now
[sgt-puzzles.git] / 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                                               me->ui);
217         if (flashtime > 0) {
218             me->flash_pos = 0.0F;
219             me->flash_time = flashtime;
220         }
221     }
222
223     if (me->oldstate)
224         me->ourgame->free_game(me->oldstate);
225     me->oldstate = NULL;
226     me->anim_pos = me->anim_time = 0;
227     me->dir = 0;
228
229     if (me->flash_time == 0 && me->anim_time == 0)
230         deactivate_timer(me->frontend);
231     else
232         activate_timer(me->frontend);
233 }
234
235 static void midend_stop_anim(midend_data *me)
236 {
237     if (me->oldstate || me->anim_time) {
238         midend_finish_move(me);
239         midend_redraw(me);
240     }
241 }
242
243 void midend_restart_game(midend_data *me)
244 {
245     game_state *s;
246
247     midend_stop_anim(me);
248
249     assert(me->statepos >= 1);
250     if (me->statepos == 1)
251         return;                        /* no point doing anything at all! */
252
253     s = me->ourgame->dup_game(me->states[0].state);
254
255     /*
256      * Now enter the restarted state as the next move.
257      */
258     midend_stop_anim(me);
259     while (me->nstates > me->statepos)
260         me->ourgame->free_game(me->states[--me->nstates].state);
261     ensure(me);
262     me->states[me->nstates].state = s;
263     me->states[me->nstates].special = TRUE;   /* we just restarted */
264     me->statepos = ++me->nstates;
265     me->anim_time = 0.0;
266     midend_finish_move(me);
267     midend_redraw(me);
268     activate_timer(me->frontend);
269 }
270
271 static int midend_really_process_key(midend_data *me, int x, int y, int button)
272 {
273     game_state *oldstate =
274         me->ourgame->dup_game(me->states[me->statepos - 1].state);
275     int special = FALSE, gotspecial = FALSE;
276     float anim_time;
277
278     if (button == 'n' || button == 'N' || button == '\x0E') {
279         midend_stop_anim(me);
280         midend_new_game(me);
281         midend_redraw(me);
282         return 1;                      /* never animate */
283     } else if (button == 'u' || button == 'u' ||
284                button == '\x1A' || button == '\x1F') {
285         midend_stop_anim(me);
286         special = me->states[me->statepos-1].special;
287         gotspecial = TRUE;
288         if (!midend_undo(me))
289             return 1;
290     } else if (button == 'r' || button == 'R' ||
291                button == '\x12') {
292         midend_stop_anim(me);
293         if (!midend_redo(me))
294             return 1;
295     } else if (button == 'q' || button == 'Q' || button == '\x11') {
296         me->ourgame->free_game(oldstate);
297         return 0;
298     } else {
299         game_state *s =
300             me->ourgame->make_move(me->states[me->statepos-1].state,
301                                    me->ui, x, y, button);
302
303         if (s == me->states[me->statepos-1].state) {
304             /*
305              * make_move() is allowed to return its input state to
306              * indicate that although no move has been made, the UI
307              * state has been updated and a redraw is called for.
308              */
309             midend_redraw(me);
310             return 1;
311         } else if (s) {
312             midend_stop_anim(me);
313             while (me->nstates > me->statepos)
314                 me->ourgame->free_game(me->states[--me->nstates].state);
315             ensure(me);
316             me->states[me->nstates].state = s;
317             me->states[me->nstates].special = FALSE;   /* normal move */
318             me->statepos = ++me->nstates;
319             me->dir = +1;
320         } else {
321             me->ourgame->free_game(oldstate);
322             return 1;
323         }
324     }
325
326     if (!gotspecial)
327         special = me->states[me->statepos-1].special;
328
329     /*
330      * See if this move requires an animation.
331      */
332     if (special) {
333         anim_time = 0;
334     } else {
335         anim_time = me->ourgame->anim_length(oldstate,
336                                              me->states[me->statepos-1].state,
337                                              me->dir, me->ui);
338     }
339
340     me->oldstate = oldstate;
341     if (anim_time > 0) {
342         me->anim_time = anim_time;
343     } else {
344         me->anim_time = 0.0;
345         midend_finish_move(me);
346     }
347     me->anim_pos = 0.0;
348
349     midend_redraw(me);
350
351     activate_timer(me->frontend);
352
353     return 1;
354 }
355
356 int midend_process_key(midend_data *me, int x, int y, int button)
357 {
358     int ret = 1;
359
360     /*
361      * Harmonise mouse drag and release messages.
362      * 
363      * Some front ends might accidentally switch from sending, say,
364      * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
365      * drag. (This can happen on the Mac, for example, since
366      * RIGHT_DRAG is usually done using Command+drag, and if the
367      * user accidentally releases Command half way through the drag
368      * then there will be trouble.)
369      * 
370      * It would be an O(number of front ends) annoyance to fix this
371      * in the front ends, but an O(number of back ends) annoyance
372      * to have each game capable of dealing with it. Therefore, we
373      * fix it _here_ in the common midend code so that it only has
374      * to be done once.
375      * 
376      * The possible ways in which things can go screwy in the front
377      * end are:
378      * 
379      *  - in a system containing multiple physical buttons button
380      *    presses can inadvertently overlap. We can see ABab (caps
381      *    meaning button-down and lowercase meaning button-up) when
382      *    the user had semantically intended AaBb.
383      * 
384      *  - in a system where one button is simulated by means of a
385      *    modifier key and another button, buttons can mutate
386      *    between press and release (possibly during drag). So we
387      *    can see Ab instead of Aa.
388      * 
389      * Definite requirements are:
390      * 
391      *  - button _presses_ must never be invented or destroyed. If
392      *    the user presses two buttons in succession, the button
393      *    presses must be transferred to the backend unchanged. So
394      *    if we see AaBb , that's fine; if we see ABab (the button
395      *    presses inadvertently overlapped) we must somehow
396      *    `correct' it to AaBb.
397      * 
398      *  - every mouse action must end up looking like a press, zero
399      *    or more drags, then a release. This allows back ends to
400      *    make the _assumption_ that incoming mouse data will be
401      *    sane in this regard, and not worry about the details.
402      * 
403      * So my policy will be:
404      * 
405      *  - treat any button-up as a button-up for the currently
406      *    pressed button, or ignore it if there is no currently
407      *    pressed button.
408      * 
409      *  - treat any drag as a drag for the currently pressed
410      *    button, or ignore it if there is no currently pressed
411      *    button.
412      * 
413      *  - if we see a button-down while another button is currently
414      *    pressed, invent a button-up for the first one and then
415      *    pass the button-down through as before.
416      * 
417      */
418     if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
419         if (me->pressed_mouse_button) {
420             if (IS_MOUSE_DRAG(button)) {
421                 button = me->pressed_mouse_button +
422                     (LEFT_DRAG - LEFT_BUTTON);
423             } else {
424                 button = me->pressed_mouse_button +
425                     (LEFT_RELEASE - LEFT_BUTTON);
426             }
427         } else
428             return ret;                /* ignore it */
429     } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
430         /*
431          * Fabricate a button-up for the previously pressed button.
432          */
433         ret = ret && midend_really_process_key
434             (me, x, y, (me->pressed_mouse_button +
435                         (LEFT_RELEASE - LEFT_BUTTON)));
436     }
437
438     /*
439      * Now send on the event we originally received.
440      */
441     ret = ret && midend_really_process_key(me, x, y, button);
442
443     /*
444      * And update the currently pressed button.
445      */
446     if (IS_MOUSE_RELEASE(button))
447         me->pressed_mouse_button = 0;
448     else if (IS_MOUSE_DOWN(button))
449         me->pressed_mouse_button = button;
450
451     return ret;
452 }
453
454 void midend_redraw(midend_data *me)
455 {
456     if (me->statepos > 0 && me->drawstate) {
457         start_draw(me->frontend);
458         if (me->oldstate && me->anim_time > 0 &&
459             me->anim_pos < me->anim_time) {
460             assert(me->dir != 0);
461             me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
462                                 me->states[me->statepos-1].state, me->dir,
463                                 me->ui, me->anim_pos, me->flash_pos);
464         } else {
465             me->ourgame->redraw(me->frontend, me->drawstate, NULL,
466                                 me->states[me->statepos-1].state, +1 /*shrug*/,
467                                 me->ui, 0.0, me->flash_pos);
468         }
469         end_draw(me->frontend);
470     }
471 }
472
473 void midend_timer(midend_data *me, float tplus)
474 {
475     me->anim_pos += tplus;
476     if (me->anim_pos >= me->anim_time ||
477         me->anim_time == 0 || !me->oldstate) {
478         if (me->anim_time > 0)
479             midend_finish_move(me);
480     }
481     me->flash_pos += tplus;
482     if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
483         me->flash_pos = me->flash_time = 0;
484     }
485     if (me->flash_time == 0 && me->anim_time == 0)
486         deactivate_timer(me->frontend);
487     midend_redraw(me);
488 }
489
490 float *midend_colours(midend_data *me, int *ncolours)
491 {
492     game_state *state = NULL;
493     float *ret;
494
495     if (me->nstates == 0) {
496         game_aux_info *aux = NULL;
497         char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
498         state = me->ourgame->new_game(me->params, desc);
499         sfree(desc);
500         if (aux)
501             me->ourgame->free_aux_info(aux);
502     } else
503         state = me->states[0].state;
504
505     ret = me->ourgame->colours(me->frontend, state, ncolours);
506
507     {
508         int i;
509
510         /*
511          * Allow environment-based overrides for the standard
512          * colours by defining variables along the lines of
513          * `NET_COLOUR_4=6000c0'.
514          */
515
516         for (i = 0; i < *ncolours; i++) {
517             char buf[80], *e;
518             unsigned int r, g, b;
519             int j;
520
521             sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
522             for (j = 0; buf[j]; j++)
523                 buf[j] = toupper((unsigned char)buf[j]);
524             if ((e = getenv(buf)) != NULL &&
525                 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
526                 ret[i*3 + 0] = r / 255.0;
527                 ret[i*3 + 1] = g / 255.0;
528                 ret[i*3 + 2] = b / 255.0;
529             }
530         }
531     }
532
533     if (me->nstates == 0)
534         me->ourgame->free_game(state);
535
536     return ret;
537 }
538
539 int midend_num_presets(midend_data *me)
540 {
541     if (!me->npresets) {
542         char *name;
543         game_params *preset;
544
545         while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
546             if (me->presetsize <= me->npresets) {
547                 me->presetsize = me->npresets + 10;
548                 me->presets = sresize(me->presets, me->presetsize,
549                                       game_params *);
550                 me->preset_names = sresize(me->preset_names, me->presetsize,
551                                            char *);
552             }
553
554             me->presets[me->npresets] = preset;
555             me->preset_names[me->npresets] = name;
556             me->npresets++;
557         }
558     }
559
560     {
561         /*
562          * Allow environment-based extensions to the preset list by
563          * defining a variable along the lines of `SOLO_PRESETS=2x3
564          * Advanced:2x3da'. Colon-separated list of items,
565          * alternating between textual titles in the menu and
566          * encoded parameter strings.
567          */
568         char buf[80], *e, *p;
569         int j;
570
571         sprintf(buf, "%s_PRESETS", me->ourgame->name);
572         for (j = 0; buf[j]; j++)
573             buf[j] = toupper((unsigned char)buf[j]);
574
575         if ((e = getenv(buf)) != NULL) {
576             p = e = dupstr(e);
577
578             while (*p) {
579                 char *name, *val;
580                 game_params *preset;
581
582                 name = p;
583                 while (*p && *p != ':') p++;
584                 if (*p) *p++ = '\0';
585                 val = p;
586                 while (*p && *p != ':') p++;
587                 if (*p) *p++ = '\0';
588
589                 preset = me->ourgame->default_params();
590                 me->ourgame->decode_params(preset, val);
591
592                 if (me->ourgame->validate_params(preset)) {
593                     /* Drop this one from the list. */
594                     me->ourgame->free_params(preset);
595                     continue;
596                 }
597
598                 if (me->presetsize <= me->npresets) {
599                     me->presetsize = me->npresets + 10;
600                     me->presets = sresize(me->presets, me->presetsize,
601                                           game_params *);
602                     me->preset_names = sresize(me->preset_names,
603                                                me->presetsize, char *);
604                 }
605
606                 me->presets[me->npresets] = preset;
607                 me->preset_names[me->npresets] = name;
608                 me->npresets++;
609             }
610         }
611     }
612
613     return me->npresets;
614 }
615
616 void midend_fetch_preset(midend_data *me, int n,
617                          char **name, game_params **params)
618 {
619     assert(n >= 0 && n < me->npresets);
620     *name = me->preset_names[n];
621     *params = me->presets[n];
622 }
623
624 int midend_wants_statusbar(midend_data *me)
625 {
626     return me->ourgame->wants_statusbar();
627 }
628
629 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
630 {
631     char *titlebuf, *parstr;
632     config_item *ret;
633
634     titlebuf = snewn(40 + strlen(me->ourgame->name), char);
635
636     switch (which) {
637       case CFG_SETTINGS:
638         sprintf(titlebuf, "%s configuration", me->ourgame->name);
639         *wintitle = dupstr(titlebuf);
640         return me->ourgame->configure(me->params);
641       case CFG_SEED:
642       case CFG_DESC:
643         sprintf(titlebuf, "%s %s selection", me->ourgame->name,
644                 which == CFG_SEED ? "random" : "game");
645         *wintitle = dupstr(titlebuf);
646
647         ret = snewn(2, config_item);
648
649         ret[0].type = C_STRING;
650         if (which == CFG_SEED)
651             ret[0].name = "Game random seed";
652         else
653             ret[0].name = "Game ID";
654         ret[0].ival = 0;
655         /*
656          * For CFG_DESC the text going in here will be a string
657          * encoding of the restricted parameters, plus a colon,
658          * plus the game description. For CFG_SEED it will be the
659          * full parameters, plus a hash, plus the random seed data.
660          * Either of these is a valid full game ID (although only
661          * the former is likely to persist across many code
662          * changes).
663          */
664         parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
665         if (which == CFG_DESC) {
666             ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
667             sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
668         } else if (me->seedstr) {
669             ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
670             sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
671         } else {
672             /*
673              * If the current game was not randomly generated, the
674              * best we can do is to give a template for typing a
675              * new seed in.
676              */
677             ret[0].sval = snewn(strlen(parstr) + 2, char);
678             sprintf(ret[0].sval, "%s#", parstr);
679         }
680         sfree(parstr);
681
682         ret[1].type = C_END;
683         ret[1].name = ret[1].sval = NULL;
684         ret[1].ival = 0;
685
686         return ret;
687     }
688
689     assert(!"We shouldn't be here");
690     return NULL;
691 }
692
693 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
694 {
695     char *error, *par, *desc, *seed;
696
697     seed = strchr(id, '#');
698     desc = strchr(id, ':');
699
700     if (desc && (!seed || desc < seed)) {
701         /*
702          * We have a colon separating parameters from game
703          * description. So `par' now points to the parameters
704          * string, and `desc' to the description string.
705          */
706         *desc++ = '\0';
707         par = id;
708         seed = NULL;
709     } else if (seed && (!desc || seed < desc)) {
710         /*
711          * We have a hash separating parameters from random seed.
712          * So `par' now points to the parameters string, and `seed'
713          * to the seed string.
714          */
715         *seed++ = '\0';
716         par = id;
717         desc = NULL;
718     } else {
719         /*
720          * We only have one string. Depending on `defmode', we take
721          * it to be either parameters, seed or description.
722          */
723         if (defmode == DEF_SEED) {
724             seed = id;
725             par = desc = NULL;
726         } else if (defmode == DEF_DESC) {
727             desc = id;
728             par = seed = NULL;
729         } else {
730             par = id;
731             seed = desc = NULL;
732         }
733     }
734
735     if (par) {
736         game_params *tmpparams;
737         tmpparams = me->ourgame->dup_params(me->params);
738         me->ourgame->decode_params(tmpparams, par);
739         error = me->ourgame->validate_params(tmpparams);
740         if (error) {
741             me->ourgame->free_params(tmpparams);
742             return error;
743         }
744         if (me->curparams)
745             me->ourgame->free_params(me->curparams);
746         me->curparams = tmpparams;
747
748         /*
749          * Now filter only the persistent parts of this state into
750          * the long-term params structure, unless we've _only_
751          * received a params string in which case the whole lot is
752          * persistent.
753          */
754         if (seed || desc) {
755             char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
756             me->ourgame->decode_params(me->params, tmpstr);
757             sfree(tmpstr);
758         } else {
759             me->ourgame->free_params(me->params);
760             me->params = me->ourgame->dup_params(tmpparams);
761         }
762     }
763
764     sfree(me->desc);
765     me->desc = NULL;
766     sfree(me->seedstr);
767     me->seedstr = NULL;
768
769     if (desc) {
770         error = me->ourgame->validate_desc(me->params, desc);
771         if (error)
772             return error;
773
774         me->desc = dupstr(desc);
775         me->genmode = GOT_DESC;
776         if (me->aux_info)
777             me->ourgame->free_aux_info(me->aux_info);
778         me->aux_info = NULL;
779     }
780
781     if (seed) {
782         me->seedstr = dupstr(seed);
783         me->genmode = GOT_SEED;
784     }
785
786     return NULL;
787 }
788
789 char *midend_game_id(midend_data *me, char *id)
790 {
791     return midend_game_id_int(me, id, DEF_PARAMS);
792 }
793
794 char *midend_set_config(midend_data *me, int which, config_item *cfg)
795 {
796     char *error;
797     game_params *params;
798
799     switch (which) {
800       case CFG_SETTINGS:
801         params = me->ourgame->custom_params(cfg);
802         error = me->ourgame->validate_params(params);
803
804         if (error) {
805             me->ourgame->free_params(params);
806             return error;
807         }
808
809         me->ourgame->free_params(me->params);
810         me->params = params;
811         break;
812
813       case CFG_SEED:
814       case CFG_DESC:
815         error = midend_game_id_int(me, cfg[0].sval,
816                                    (which == CFG_SEED ? DEF_SEED : DEF_DESC));
817         if (error)
818             return error;
819         break;
820     }
821
822     return NULL;
823 }
824
825 char *midend_text_format(midend_data *me)
826 {
827     if (me->ourgame->can_format_as_text && me->statepos > 0)
828         return me->ourgame->text_format(me->states[me->statepos-1].state);
829     else
830         return NULL;
831 }
832
833 char *midend_solve(midend_data *me)
834 {
835     game_state *s;
836     char *msg;
837
838     if (!me->ourgame->can_solve)
839         return "This game does not support the Solve operation";
840
841     if (me->statepos < 1)
842         return "No game set up to solve";   /* _shouldn't_ happen! */
843
844     msg = "Solve operation failed";    /* game _should_ overwrite on error */
845     s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
846     if (!s)
847         return msg;
848
849     /*
850      * Now enter the solved state as the next move.
851      */
852     midend_stop_anim(me);
853     while (me->nstates > me->statepos)
854         me->ourgame->free_game(me->states[--me->nstates].state);
855     ensure(me);
856     me->states[me->nstates].state = s;
857     me->states[me->nstates].special = TRUE;   /* created using solve */
858     me->statepos = ++me->nstates;
859     me->anim_time = 0.0;
860     midend_finish_move(me);
861     midend_redraw(me);
862     activate_timer(me->frontend);
863     return NULL;
864 }