Neat idea from Gareth: if you put a % on the end of the mine count
[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
f60f7e7c 37 game_params *params, *curparams;
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();
f60f7e7c 71 me->curparams = NULL;
1185e3c5 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);
f60f7e7c 101 if (me->curparams)
102 me->ourgame->free_params(me->curparams);
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.
97c44af3 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.
1185e3c5 142 */
143 char newseed[16];
144 int i;
145 newseed[15] = '\0';
97c44af3 146 newseed[0] = '1' + random_upto(me->random, 9);
147 for (i = 1; i < 15; i++)
1185e3c5 148 newseed[i] = '0' + random_upto(me->random, 10);
149 sfree(me->seedstr);
150 me->seedstr = dupstr(newseed);
f60f7e7c 151
152 if (me->curparams)
153 me->ourgame->free_params(me->curparams);
154 me->curparams = me->ourgame->dup_params(me->params);
1185e3c5 155 }
156
157 sfree(me->desc);
6f2d8d7c 158 if (me->aux_info)
159 me->ourgame->free_aux_info(me->aux_info);
160 me->aux_info = NULL;
1185e3c5 161
162 rs = random_init(me->seedstr, strlen(me->seedstr));
f60f7e7c 163 me->desc = me->ourgame->new_desc(me->curparams, rs, &me->aux_info);
1185e3c5 164 random_free(rs);
1185e3c5 165 }
7f77ea24 166
167 ensure(me);
28d0118c 168 me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
169 me->states[me->nstates].special = TRUE;
170 me->nstates++;
7f77ea24 171 me->statepos = 1;
28d0118c 172 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
74a4e547 173 if (me->ui)
be8d5aa1 174 me->ourgame->free_ui(me->ui);
28d0118c 175 me->ui = me->ourgame->new_ui(me->states[0].state);
6776a950 176 me->pressed_mouse_button = 0;
7f77ea24 177}
178
1482ee76 179static int midend_undo(midend_data *me)
7f77ea24 180{
1482ee76 181 if (me->statepos > 1) {
7f77ea24 182 me->statepos--;
c822de4a 183 me->dir = -1;
1482ee76 184 return 1;
185 } else
186 return 0;
7f77ea24 187}
188
1482ee76 189static int midend_redo(midend_data *me)
7f77ea24 190{
1482ee76 191 if (me->statepos < me->nstates) {
7f77ea24 192 me->statepos++;
c822de4a 193 me->dir = +1;
1482ee76 194 return 1;
195 } else
196 return 0;
7f77ea24 197}
198
87ed82be 199static void midend_finish_move(midend_data *me)
200{
201 float flashtime;
202
28d0118c 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))) {
be8d5aa1 212 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
28d0118c 213 me->states[me->statepos-2].state,
214 me->states[me->statepos-1].state,
e3f21163 215 me->oldstate ? me->dir : +1,
216 me->ui);
87ed82be 217 if (flashtime > 0) {
218 me->flash_pos = 0.0F;
219 me->flash_time = flashtime;
220 }
221 }
222
223 if (me->oldstate)
be8d5aa1 224 me->ourgame->free_game(me->oldstate);
87ed82be 225 me->oldstate = NULL;
226 me->anim_pos = me->anim_time = 0;
c822de4a 227 me->dir = 0;
87ed82be 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
dd216087 235static void midend_stop_anim(midend_data *me)
7f77ea24 236{
2ef96bd6 237 if (me->oldstate || me->anim_time) {
87ed82be 238 midend_finish_move(me);
2ef96bd6 239 midend_redraw(me);
240 }
dd216087 241}
242
28d0118c 243void midend_restart_game(midend_data *me)
244{
245 game_state *s;
246
7f89707c 247 midend_stop_anim(me);
248
28d0118c 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
6776a950 271static int midend_really_process_key(midend_data *me, int x, int y, int button)
dd216087 272{
28d0118c 273 game_state *oldstate =
274 me->ourgame->dup_game(me->states[me->statepos - 1].state);
275 int special = FALSE, gotspecial = FALSE;
dd216087 276 float anim_time;
7f77ea24 277
278 if (button == 'n' || button == 'N' || button == '\x0E') {
dd216087 279 midend_stop_anim(me);
5928817c 280 midend_new_game(me);
2ef96bd6 281 midend_redraw(me);
282 return 1; /* never animate */
7f77ea24 283 } else if (button == 'u' || button == 'u' ||
1482ee76 284 button == '\x1A' || button == '\x1F') {
dd216087 285 midend_stop_anim(me);
28d0118c 286 special = me->states[me->statepos-1].special;
287 gotspecial = TRUE;
1482ee76 288 if (!midend_undo(me))
289 return 1;
7f89707c 290 } else if (button == 'r' || button == 'R' ||
291 button == '\x12') {
dd216087 292 midend_stop_anim(me);
1482ee76 293 if (!midend_redo(me))
294 return 1;
7f77ea24 295 } else if (button == 'q' || button == 'Q' || button == '\x11') {
be8d5aa1 296 me->ourgame->free_game(oldstate);
2ef96bd6 297 return 0;
298 } else {
28d0118c 299 game_state *s =
300 me->ourgame->make_move(me->states[me->statepos-1].state,
301 me->ui, x, y, button);
74a4e547 302
28d0118c 303 if (s == me->states[me->statepos-1].state) {
74a4e547 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) {
dd216087 312 midend_stop_anim(me);
2ef96bd6 313 while (me->nstates > me->statepos)
28d0118c 314 me->ourgame->free_game(me->states[--me->nstates].state);
2ef96bd6 315 ensure(me);
28d0118c 316 me->states[me->nstates].state = s;
317 me->states[me->nstates].special = FALSE; /* normal move */
2ef96bd6 318 me->statepos = ++me->nstates;
c822de4a 319 me->dir = +1;
2ef96bd6 320 } else {
be8d5aa1 321 me->ourgame->free_game(oldstate);
2ef96bd6 322 return 1;
323 }
7f77ea24 324 }
325
28d0118c 326 if (!gotspecial)
327 special = me->states[me->statepos-1].special;
328
2ef96bd6 329 /*
330 * See if this move requires an animation.
331 */
28d0118c 332 if (special) {
333 anim_time = 0;
334 } else {
335 anim_time = me->ourgame->anim_length(oldstate,
336 me->states[me->statepos-1].state,
e3f21163 337 me->dir, me->ui);
28d0118c 338 }
2ef96bd6 339
87ed82be 340 me->oldstate = oldstate;
2ef96bd6 341 if (anim_time > 0) {
2ef96bd6 342 me->anim_time = anim_time;
343 } else {
2ef96bd6 344 me->anim_time = 0.0;
87ed82be 345 midend_finish_move(me);
7f77ea24 346 }
2ef96bd6 347 me->anim_pos = 0.0;
348
349 midend_redraw(me);
350
351 activate_timer(me->frontend);
7f77ea24 352
353 return 1;
354}
2ef96bd6 355
6776a950 356int 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 *
eeba2afe 376 * The possible ways in which things can go screwy in the front
377 * end are:
6776a950 378 *
eeba2afe 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.
6776a950 383 *
eeba2afe 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.
6776a950 388 *
eeba2afe 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.
6776a950 416 *
6776a950 417 */
418 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
eeba2afe 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);
6776a950 426 }
eeba2afe 427 } else
428 return ret; /* ignore it */
6776a950 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
2ef96bd6 454void 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) {
c822de4a 460 assert(me->dir != 0);
be8d5aa1 461 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
28d0118c 462 me->states[me->statepos-1].state, me->dir,
be8d5aa1 463 me->ui, me->anim_pos, me->flash_pos);
2ef96bd6 464 } else {
be8d5aa1 465 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
28d0118c 466 me->states[me->statepos-1].state, +1 /*shrug*/,
be8d5aa1 467 me->ui, 0.0, me->flash_pos);
2ef96bd6 468 }
469 end_draw(me->frontend);
470 }
471}
472
473void 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) {
87ed82be 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;
2ef96bd6 484 }
87ed82be 485 if (me->flash_time == 0 && me->anim_time == 0)
486 deactivate_timer(me->frontend);
2ef96bd6 487 midend_redraw(me);
488}
489
490float *midend_colours(midend_data *me, int *ncolours)
491{
492 game_state *state = NULL;
493 float *ret;
494
495 if (me->nstates == 0) {
6f2d8d7c 496 game_aux_info *aux = NULL;
1185e3c5 497 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
498 state = me->ourgame->new_game(me->params, desc);
499 sfree(desc);
6f2d8d7c 500 if (aux)
501 me->ourgame->free_aux_info(aux);
2ef96bd6 502 } else
28d0118c 503 state = me->states[0].state;
2ef96bd6 504
be8d5aa1 505 ret = me->ourgame->colours(me->frontend, state, ncolours);
2ef96bd6 506
813593cc 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
2ef96bd6 533 if (me->nstates == 0)
be8d5aa1 534 me->ourgame->free_game(state);
2ef96bd6 535
536 return ret;
537}
eb2ad6f1 538
539int midend_num_presets(midend_data *me)
540{
541 if (!me->npresets) {
542 char *name;
543 game_params *preset;
544
be8d5aa1 545 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
eb2ad6f1 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
813593cc 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
239ba6e6 592 if (me->ourgame->validate_params(preset)) {
593 /* Drop this one from the list. */
594 me->ourgame->free_params(preset);
595 continue;
596 }
597
813593cc 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
eb2ad6f1 613 return me->npresets;
614}
615
616void 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}
fd1a1a2b 623
624int midend_wants_statusbar(midend_data *me)
625{
be8d5aa1 626 return me->ourgame->wants_statusbar();
fd1a1a2b 627}
c8230524 628
5928817c 629config_item *midend_get_config(midend_data *me, int which, char **wintitle)
c8230524 630{
b0e26073 631 char *titlebuf, *parstr;
5928817c 632 config_item *ret;
633
be8d5aa1 634 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
5928817c 635
636 switch (which) {
637 case CFG_SETTINGS:
be8d5aa1 638 sprintf(titlebuf, "%s configuration", me->ourgame->name);
5928817c 639 *wintitle = dupstr(titlebuf);
be8d5aa1 640 return me->ourgame->configure(me->params);
5928817c 641 case CFG_SEED:
1185e3c5 642 case CFG_DESC:
643 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
644 which == CFG_SEED ? "random" : "game");
5928817c 645 *wintitle = dupstr(titlebuf);
646
647 ret = snewn(2, config_item);
648
649 ret[0].type = C_STRING;
1185e3c5 650 if (which == CFG_SEED)
651 ret[0].name = "Game random seed";
652 else
653 ret[0].name = "Game ID";
5928817c 654 ret[0].ival = 0;
b0e26073 655 /*
1185e3c5 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).
b0e26073 663 */
f60f7e7c 664 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
1185e3c5 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 }
b0e26073 680 sfree(parstr);
5928817c 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;
c8230524 691}
692
1185e3c5 693static char *midend_game_id_int(midend_data *me, char *id, int defmode)
8b7938e7 694{
1185e3c5 695 char *error, *par, *desc, *seed;
8b7938e7 696
1185e3c5 697 seed = strchr(id, '#');
698 desc = strchr(id, ':');
8b7938e7 699
1185e3c5 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)) {
8b7938e7 710 /*
50ff5730 711 * We have a hash separating parameters from random seed.
1185e3c5 712 * So `par' now points to the parameters string, and `seed'
713 * to the seed string.
8b7938e7 714 */
715 *seed++ = '\0';
716 par = id;
1185e3c5 717 desc = NULL;
8b7938e7 718 } else {
719 /*
1185e3c5 720 * We only have one string. Depending on `defmode', we take
721 * it to be either parameters, seed or description.
8b7938e7 722 */
1185e3c5 723 if (defmode == DEF_SEED) {
8b7938e7 724 seed = id;
1185e3c5 725 par = desc = NULL;
726 } else if (defmode == DEF_DESC) {
727 desc = id;
728 par = seed = NULL;
8b7938e7 729 } else {
8b7938e7 730 par = id;
1185e3c5 731 seed = desc = NULL;
8b7938e7 732 }
733 }
734
735 if (par) {
1185e3c5 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);
8b7938e7 740 if (error) {
1185e3c5 741 me->ourgame->free_params(tmpparams);
8b7938e7 742 return error;
743 }
f60f7e7c 744 if (me->curparams)
745 me->ourgame->free_params(me->curparams);
746 me->curparams = tmpparams;
1185e3c5 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);
3af70dd4 757 sfree(tmpstr);
1185e3c5 758 } else {
759 me->ourgame->free_params(me->params);
760 me->params = me->ourgame->dup_params(tmpparams);
761 }
8b7938e7 762 }
763
3af70dd4 764 sfree(me->desc);
765 me->desc = NULL;
766 sfree(me->seedstr);
767 me->seedstr = NULL;
768
1185e3c5 769 if (desc) {
770 error = me->ourgame->validate_desc(me->params, desc);
8b7938e7 771 if (error)
772 return error;
773
1185e3c5 774 me->desc = dupstr(desc);
775 me->genmode = GOT_DESC;
6f2d8d7c 776 if (me->aux_info)
777 me->ourgame->free_aux_info(me->aux_info);
778 me->aux_info = NULL;
8b7938e7 779 }
780
1185e3c5 781 if (seed) {
1185e3c5 782 me->seedstr = dupstr(seed);
783 me->genmode = GOT_SEED;
784 }
785
8b7938e7 786 return NULL;
787}
788
1185e3c5 789char *midend_game_id(midend_data *me, char *id)
790{
791 return midend_game_id_int(me, id, DEF_PARAMS);
792}
793
5928817c 794char *midend_set_config(midend_data *me, int which, config_item *cfg)
c8230524 795{
8b7938e7 796 char *error;
c8230524 797 game_params *params;
798
5928817c 799 switch (which) {
800 case CFG_SETTINGS:
be8d5aa1 801 params = me->ourgame->custom_params(cfg);
802 error = me->ourgame->validate_params(params);
c8230524 803
5928817c 804 if (error) {
be8d5aa1 805 me->ourgame->free_params(params);
5928817c 806 return error;
807 }
c8230524 808
be8d5aa1 809 me->ourgame->free_params(me->params);
5928817c 810 me->params = params;
811 break;
812
813 case CFG_SEED:
1185e3c5 814 case CFG_DESC:
815 error = midend_game_id_int(me, cfg[0].sval,
816 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
5928817c 817 if (error)
818 return error;
5928817c 819 break;
820 }
c8230524 821
822 return NULL;
823}
9b4b03d3 824
825char *midend_text_format(midend_data *me)
826{
827 if (me->ourgame->can_format_as_text && me->statepos > 0)
28d0118c 828 return me->ourgame->text_format(me->states[me->statepos-1].state);
9b4b03d3 829 else
830 return NULL;
831}
2ac6d24e 832
833char *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 */
28d0118c 845 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
2ac6d24e 846 if (!s)
847 return msg;
848
849 /*
28d0118c 850 * Now enter the solved state as the next move.
2ac6d24e 851 */
852 midend_stop_anim(me);
853 while (me->nstates > me->statepos)
28d0118c 854 me->ourgame->free_game(me->states[--me->nstates].state);
2ac6d24e 855 ensure(me);
28d0118c 856 me->states[me->nstates].state = s;
857 me->states[me->nstates].special = TRUE; /* created using solve */
2ac6d24e 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}