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