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