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