Move the colour configuration into midend.c so that it becomes
[sgt/puzzles] / midend.c
CommitLineData
720a8fb7 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
7f89707c 5 * processes standard keystrokes for undo/redo/new/quit.
720a8fb7 6 */
7f77ea24 7
8#include <stdio.h>
5928817c 9#include <string.h>
7f77ea24 10#include <assert.h>
813593cc 11#include <stdlib.h>
12#include <ctype.h>
7f77ea24 13
14#include "puzzles.h"
15
1185e3c5 16enum { DEF_PARAMS, DEF_SEED, DEF_DESC }; /* for midend_game_id_int */
17
28d0118c 18struct midend_state_entry {
19 game_state *state;
20 int special; /* created by solve or restart */
21};
22
7f77ea24 23struct midend_data {
2ef96bd6 24 frontend *frontend;
48d70ca9 25 random_state *random;
be8d5aa1 26 const game *ourgame;
48d70ca9 27
1185e3c5 28 char *desc, *seedstr;
6f2d8d7c 29 game_aux_info *aux_info;
1185e3c5 30 enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
7f77ea24 31 int nstates, statesize, statepos;
eb2ad6f1 32
33 game_params **presets;
34 char **preset_names;
35 int npresets, presetsize;
36
1185e3c5 37 game_params *params, *tmpparams;
28d0118c 38 struct midend_state_entry *states;
2ef96bd6 39 game_drawstate *drawstate;
40 game_state *oldstate;
74a4e547 41 game_ui *ui;
2ef96bd6 42 float anim_time, anim_pos;
87ed82be 43 float flash_time, flash_pos;
c822de4a 44 int dir;
6776a950 45
46 int pressed_mouse_button;
7f77ea24 47};
48
49#define ensure(me) do { \
50 if ((me)->nstates >= (me)->statesize) { \
51 (me)->statesize = (me)->nstates + 128; \
28d0118c 52 (me)->states = sresize((me)->states, (me)->statesize, \
53 struct midend_state_entry); \
7f77ea24 54 } \
55} while (0)
56
be8d5aa1 57midend_data *midend_new(frontend *fe, const game *ourgame)
7f77ea24 58{
59 midend_data *me = snew(midend_data);
cbb5549e 60 void *randseed;
61 int randseedsize;
62
63 get_random_seed(&randseed, &randseedsize);
7f77ea24 64
48d70ca9 65 me->frontend = fe;
be8d5aa1 66 me->ourgame = ourgame;
48d70ca9 67 me->random = random_init(randseed, randseedsize);
7f77ea24 68 me->nstates = me->statesize = me->statepos = 0;
69 me->states = NULL;
be8d5aa1 70 me->params = ourgame->default_params();
1185e3c5 71 me->tmpparams = NULL;
72 me->desc = NULL;
73 me->seedstr = NULL;
6f2d8d7c 74 me->aux_info = NULL;
1185e3c5 75 me->genmode = GOT_NOTHING;
2ef96bd6 76 me->drawstate = NULL;
77 me->oldstate = NULL;
eb2ad6f1 78 me->presets = NULL;
79 me->preset_names = NULL;
80 me->npresets = me->presetsize = 0;
87ed82be 81 me->anim_time = me->anim_pos = 0.0F;
82 me->flash_time = me->flash_pos = 0.0F;
c822de4a 83 me->dir = 0;
74a4e547 84 me->ui = NULL;
6776a950 85 me->pressed_mouse_button = 0;
7f77ea24 86
cbb5549e 87 sfree(randseed);
88
7f77ea24 89 return me;
90}
91
92void midend_free(midend_data *me)
93{
94 sfree(me->states);
1185e3c5 95 sfree(me->desc);
96 sfree(me->seedstr);
97 random_free(me->random);
6f2d8d7c 98 if (me->aux_info)
99 me->ourgame->free_aux_info(me->aux_info);
be8d5aa1 100 me->ourgame->free_params(me->params);
1185e3c5 101 if (me->tmpparams)
102 me->ourgame->free_params(me->tmpparams);
7f77ea24 103 sfree(me);
104}
105
106void midend_size(midend_data *me, int *x, int *y)
107{
be8d5aa1 108 me->ourgame->size(me->params, x, y);
7f77ea24 109}
110
111void midend_set_params(midend_data *me, game_params *params)
112{
be8d5aa1 113 me->ourgame->free_params(me->params);
114 me->params = me->ourgame->dup_params(params);
7f77ea24 115}
116
5928817c 117void midend_new_game(midend_data *me)
7f77ea24 118{
119 while (me->nstates > 0)
28d0118c 120 me->ourgame->free_game(me->states[--me->nstates].state);
7f77ea24 121
2ef96bd6 122 if (me->drawstate)
be8d5aa1 123 me->ourgame->free_drawstate(me->drawstate);
2ef96bd6 124
7f77ea24 125 assert(me->nstates == 0);
126
1185e3c5 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);
6f2d8d7c 149 if (me->aux_info)
150 me->ourgame->free_aux_info(me->aux_info);
151 me->aux_info = NULL;
1185e3c5 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 }
7f77ea24 163
164 ensure(me);
28d0118c 165 me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
166 me->states[me->nstates].special = TRUE;
167 me->nstates++;
7f77ea24 168 me->statepos = 1;
28d0118c 169 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
74a4e547 170 if (me->ui)
be8d5aa1 171 me->ourgame->free_ui(me->ui);
28d0118c 172 me->ui = me->ourgame->new_ui(me->states[0].state);
6776a950 173 me->pressed_mouse_button = 0;
7f77ea24 174}
175
1482ee76 176static int midend_undo(midend_data *me)
7f77ea24 177{
1482ee76 178 if (me->statepos > 1) {
7f77ea24 179 me->statepos--;
c822de4a 180 me->dir = -1;
1482ee76 181 return 1;
182 } else
183 return 0;
7f77ea24 184}
185
1482ee76 186static int midend_redo(midend_data *me)
7f77ea24 187{
1482ee76 188 if (me->statepos < me->nstates) {
7f77ea24 189 me->statepos++;
c822de4a 190 me->dir = +1;
1482ee76 191 return 1;
192 } else
193 return 0;
7f77ea24 194}
195
87ed82be 196static void midend_finish_move(midend_data *me)
197{
198 float flashtime;
199
28d0118c 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))) {
be8d5aa1 209 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
28d0118c 210 me->states[me->statepos-2].state,
211 me->states[me->statepos-1].state,
be8d5aa1 212 me->oldstate ? me->dir : +1);
87ed82be 213 if (flashtime > 0) {
214 me->flash_pos = 0.0F;
215 me->flash_time = flashtime;
216 }
217 }
218
219 if (me->oldstate)
be8d5aa1 220 me->ourgame->free_game(me->oldstate);
87ed82be 221 me->oldstate = NULL;
222 me->anim_pos = me->anim_time = 0;
c822de4a 223 me->dir = 0;
87ed82be 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
dd216087 231static void midend_stop_anim(midend_data *me)
7f77ea24 232{
2ef96bd6 233 if (me->oldstate || me->anim_time) {
87ed82be 234 midend_finish_move(me);
2ef96bd6 235 midend_redraw(me);
236 }
dd216087 237}
238
28d0118c 239void midend_restart_game(midend_data *me)
240{
241 game_state *s;
242
7f89707c 243 midend_stop_anim(me);
244
28d0118c 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
6776a950 267static int midend_really_process_key(midend_data *me, int x, int y, int button)
dd216087 268{
28d0118c 269 game_state *oldstate =
270 me->ourgame->dup_game(me->states[me->statepos - 1].state);
271 int special = FALSE, gotspecial = FALSE;
dd216087 272 float anim_time;
7f77ea24 273
274 if (button == 'n' || button == 'N' || button == '\x0E') {
dd216087 275 midend_stop_anim(me);
5928817c 276 midend_new_game(me);
2ef96bd6 277 midend_redraw(me);
278 return 1; /* never animate */
7f77ea24 279 } else if (button == 'u' || button == 'u' ||
1482ee76 280 button == '\x1A' || button == '\x1F') {
dd216087 281 midend_stop_anim(me);
28d0118c 282 special = me->states[me->statepos-1].special;
283 gotspecial = TRUE;
1482ee76 284 if (!midend_undo(me))
285 return 1;
7f89707c 286 } else if (button == 'r' || button == 'R' ||
287 button == '\x12') {
dd216087 288 midend_stop_anim(me);
1482ee76 289 if (!midend_redo(me))
290 return 1;
7f77ea24 291 } else if (button == 'q' || button == 'Q' || button == '\x11') {
be8d5aa1 292 me->ourgame->free_game(oldstate);
2ef96bd6 293 return 0;
294 } else {
28d0118c 295 game_state *s =
296 me->ourgame->make_move(me->states[me->statepos-1].state,
297 me->ui, x, y, button);
74a4e547 298
28d0118c 299 if (s == me->states[me->statepos-1].state) {
74a4e547 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) {
dd216087 308 midend_stop_anim(me);
2ef96bd6 309 while (me->nstates > me->statepos)
28d0118c 310 me->ourgame->free_game(me->states[--me->nstates].state);
2ef96bd6 311 ensure(me);
28d0118c 312 me->states[me->nstates].state = s;
313 me->states[me->nstates].special = FALSE; /* normal move */
2ef96bd6 314 me->statepos = ++me->nstates;
c822de4a 315 me->dir = +1;
2ef96bd6 316 } else {
be8d5aa1 317 me->ourgame->free_game(oldstate);
2ef96bd6 318 return 1;
319 }
7f77ea24 320 }
321
28d0118c 322 if (!gotspecial)
323 special = me->states[me->statepos-1].special;
324
2ef96bd6 325 /*
326 * See if this move requires an animation.
327 */
28d0118c 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 }
2ef96bd6 335
87ed82be 336 me->oldstate = oldstate;
2ef96bd6 337 if (anim_time > 0) {
2ef96bd6 338 me->anim_time = anim_time;
339 } else {
2ef96bd6 340 me->anim_time = 0.0;
87ed82be 341 midend_finish_move(me);
7f77ea24 342 }
2ef96bd6 343 me->anim_pos = 0.0;
344
345 midend_redraw(me);
346
347 activate_timer(me->frontend);
7f77ea24 348
349 return 1;
350}
2ef96bd6 351
6776a950 352int 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 *
eeba2afe 372 * The possible ways in which things can go screwy in the front
373 * end are:
6776a950 374 *
eeba2afe 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.
6776a950 379 *
eeba2afe 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.
6776a950 384 *
eeba2afe 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.
6776a950 412 *
6776a950 413 */
414 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
eeba2afe 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);
6776a950 422 }
eeba2afe 423 } else
424 return ret; /* ignore it */
6776a950 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
2ef96bd6 450void 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) {
c822de4a 456 assert(me->dir != 0);
be8d5aa1 457 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
28d0118c 458 me->states[me->statepos-1].state, me->dir,
be8d5aa1 459 me->ui, me->anim_pos, me->flash_pos);
2ef96bd6 460 } else {
be8d5aa1 461 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
28d0118c 462 me->states[me->statepos-1].state, +1 /*shrug*/,
be8d5aa1 463 me->ui, 0.0, me->flash_pos);
2ef96bd6 464 }
465 end_draw(me->frontend);
466 }
467}
468
469void 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) {
87ed82be 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;
2ef96bd6 480 }
87ed82be 481 if (me->flash_time == 0 && me->anim_time == 0)
482 deactivate_timer(me->frontend);
2ef96bd6 483 midend_redraw(me);
484}
485
486float *midend_colours(midend_data *me, int *ncolours)
487{
488 game_state *state = NULL;
489 float *ret;
490
491 if (me->nstates == 0) {
6f2d8d7c 492 game_aux_info *aux = NULL;
1185e3c5 493 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
494 state = me->ourgame->new_game(me->params, desc);
495 sfree(desc);
6f2d8d7c 496 if (aux)
497 me->ourgame->free_aux_info(aux);
2ef96bd6 498 } else
28d0118c 499 state = me->states[0].state;
2ef96bd6 500
be8d5aa1 501 ret = me->ourgame->colours(me->frontend, state, ncolours);
2ef96bd6 502
813593cc 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
2ef96bd6 529 if (me->nstates == 0)
be8d5aa1 530 me->ourgame->free_game(state);
2ef96bd6 531
532 return ret;
533}
eb2ad6f1 534
535int midend_num_presets(midend_data *me)
536{
537 if (!me->npresets) {
538 char *name;
539 game_params *preset;
540
be8d5aa1 541 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
eb2ad6f1 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
813593cc 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
eb2ad6f1 603 return me->npresets;
604}
605
606void 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}
fd1a1a2b 613
614int midend_wants_statusbar(midend_data *me)
615{
be8d5aa1 616 return me->ourgame->wants_statusbar();
fd1a1a2b 617}
c8230524 618
5928817c 619config_item *midend_get_config(midend_data *me, int which, char **wintitle)
c8230524 620{
b0e26073 621 char *titlebuf, *parstr;
5928817c 622 config_item *ret;
623
be8d5aa1 624 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
5928817c 625
626 switch (which) {
627 case CFG_SETTINGS:
be8d5aa1 628 sprintf(titlebuf, "%s configuration", me->ourgame->name);
5928817c 629 *wintitle = dupstr(titlebuf);
be8d5aa1 630 return me->ourgame->configure(me->params);
5928817c 631 case CFG_SEED:
1185e3c5 632 case CFG_DESC:
633 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
634 which == CFG_SEED ? "random" : "game");
5928817c 635 *wintitle = dupstr(titlebuf);
636
637 ret = snewn(2, config_item);
638
639 ret[0].type = C_STRING;
1185e3c5 640 if (which == CFG_SEED)
641 ret[0].name = "Game random seed";
642 else
643 ret[0].name = "Game ID";
5928817c 644 ret[0].ival = 0;
b0e26073 645 /*
1185e3c5 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).
b0e26073 653 */
1185e3c5 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 }
b0e26073 670 sfree(parstr);
5928817c 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;
c8230524 681}
682
1185e3c5 683static char *midend_game_id_int(midend_data *me, char *id, int defmode)
8b7938e7 684{
1185e3c5 685 char *error, *par, *desc, *seed;
8b7938e7 686
1185e3c5 687 seed = strchr(id, '#');
688 desc = strchr(id, ':');
8b7938e7 689
1185e3c5 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)) {
8b7938e7 700 /*
50ff5730 701 * We have a hash separating parameters from random seed.
1185e3c5 702 * So `par' now points to the parameters string, and `seed'
703 * to the seed string.
8b7938e7 704 */
705 *seed++ = '\0';
706 par = id;
1185e3c5 707 desc = NULL;
8b7938e7 708 } else {
709 /*
1185e3c5 710 * We only have one string. Depending on `defmode', we take
711 * it to be either parameters, seed or description.
8b7938e7 712 */
1185e3c5 713 if (defmode == DEF_SEED) {
8b7938e7 714 seed = id;
1185e3c5 715 par = desc = NULL;
716 } else if (defmode == DEF_DESC) {
717 desc = id;
718 par = seed = NULL;
8b7938e7 719 } else {
8b7938e7 720 par = id;
1185e3c5 721 seed = desc = NULL;
8b7938e7 722 }
723 }
724
725 if (par) {
1185e3c5 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);
8b7938e7 730 if (error) {
1185e3c5 731 me->ourgame->free_params(tmpparams);
8b7938e7 732 return error;
733 }
1185e3c5 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 }
8b7938e7 751 }
752
1185e3c5 753 if (desc) {
754 error = me->ourgame->validate_desc(me->params, desc);
8b7938e7 755 if (error)
756 return error;
757
1185e3c5 758 sfree(me->desc);
759 me->desc = dupstr(desc);
760 me->genmode = GOT_DESC;
6f2d8d7c 761 if (me->aux_info)
762 me->ourgame->free_aux_info(me->aux_info);
763 me->aux_info = NULL;
8b7938e7 764 }
765
1185e3c5 766 if (seed) {
767 sfree(me->seedstr);
768 me->seedstr = dupstr(seed);
769 me->genmode = GOT_SEED;
770 }
771
8b7938e7 772 return NULL;
773}
774
1185e3c5 775char *midend_game_id(midend_data *me, char *id)
776{
777 return midend_game_id_int(me, id, DEF_PARAMS);
778}
779
5928817c 780char *midend_set_config(midend_data *me, int which, config_item *cfg)
c8230524 781{
8b7938e7 782 char *error;
c8230524 783 game_params *params;
784
5928817c 785 switch (which) {
786 case CFG_SETTINGS:
be8d5aa1 787 params = me->ourgame->custom_params(cfg);
788 error = me->ourgame->validate_params(params);
c8230524 789
5928817c 790 if (error) {
be8d5aa1 791 me->ourgame->free_params(params);
5928817c 792 return error;
793 }
c8230524 794
be8d5aa1 795 me->ourgame->free_params(me->params);
5928817c 796 me->params = params;
797 break;
798
799 case CFG_SEED:
1185e3c5 800 case CFG_DESC:
801 error = midend_game_id_int(me, cfg[0].sval,
802 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
5928817c 803 if (error)
804 return error;
5928817c 805 break;
806 }
c8230524 807
808 return NULL;
809}
9b4b03d3 810
811char *midend_text_format(midend_data *me)
812{
813 if (me->ourgame->can_format_as_text && me->statepos > 0)
28d0118c 814 return me->ourgame->text_format(me->states[me->statepos-1].state);
9b4b03d3 815 else
816 return NULL;
817}
2ac6d24e 818
819char *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 */
28d0118c 831 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
2ac6d24e 832 if (!s)
833 return msg;
834
835 /*
28d0118c 836 * Now enter the solved state as the next move.
2ac6d24e 837 */
838 midend_stop_anim(me);
839 while (me->nstates > me->statepos)
28d0118c 840 me->ourgame->free_game(me->states[--me->nstates].state);
2ac6d24e 841 ensure(me);
28d0118c 842 me->states[me->nstates].state = s;
843 me->states[me->nstates].special = TRUE; /* created using solve */
2ac6d24e 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}