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