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.
16 enum { DEF_PARAMS
, DEF_SEED
, DEF_DESC
}; /* for midend_game_id_int */
18 struct midend_state_entry
{
20 int special
; /* created by solve or restart */
29 * `desc' and `privdesc' deserve a comment.
31 * `desc' is the game description as presented to the user when
32 * they ask for Game -> Specific. `privdesc', if non-NULL, is a
33 * different game description used to reconstruct the initial
34 * game_state when de-serialising. If privdesc is NULL, `desc'
37 * For almost all games, `privdesc' is NULL and never used. The
38 * exception (as usual) is Mines: the initial game state has no
39 * squares open at all, but after the first click `desc' is
40 * rewritten to describe a game state with an initial click and
41 * thus a bunch of squares open. If we used that desc to
42 * serialise and deserialise, then the initial game state after
43 * deserialisation would look unlike the initial game state
44 * beforehand, and worse still execute_move() might fail on the
45 * attempted first click. So `privdesc' is also used in this
46 * case, to provide a game description describing the same
47 * fixed mine layout _but_ no initial click. (These game IDs
48 * may also be typed directly into Mines if you like.)
50 char *desc
, *privdesc
, *seedstr
;
52 enum { GOT_SEED
, GOT_DESC
, GOT_NOTHING
} genmode
;
53 int nstates
, statesize
, statepos
;
55 game_params
**presets
;
57 int npresets
, presetsize
;
59 game_params
*params
, *curparams
;
60 struct midend_state_entry
*states
;
61 game_drawstate
*drawstate
;
64 float anim_time
, anim_pos
;
65 float flash_time
, flash_pos
;
72 int pressed_mouse_button
;
74 int winwidth
, winheight
;
77 #define ensure(me) do { \
78 if ((me)->nstates >= (me)->statesize) { \
79 (me)->statesize = (me)->nstates + 128; \
80 (me)->states = sresize((me)->states, (me)->statesize, \
81 struct midend_state_entry); \
85 midend_data
*midend_new(frontend
*fe
, const game
*ourgame
)
87 midend_data
*me
= snew(midend_data
);
91 get_random_seed(&randseed
, &randseedsize
);
94 me
->ourgame
= ourgame
;
95 me
->random
= random_init(randseed
, randseedsize
);
96 me
->nstates
= me
->statesize
= me
->statepos
= 0;
98 me
->params
= ourgame
->default_params();
100 me
->desc
= me
->privdesc
= NULL
;
103 me
->genmode
= GOT_NOTHING
;
104 me
->drawstate
= NULL
;
107 me
->preset_names
= NULL
;
108 me
->npresets
= me
->presetsize
= 0;
109 me
->anim_time
= me
->anim_pos
= 0.0F
;
110 me
->flash_time
= me
->flash_pos
= 0.0F
;
113 me
->pressed_mouse_button
= 0;
114 me
->laststatus
= NULL
;
117 me
->winwidth
= me
->winheight
= 0;
124 static void midend_free_game(midend_data
*me
)
126 while (me
->nstates
> 0)
127 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
130 me
->ourgame
->free_drawstate(me
->drawstate
);
133 void midend_free(midend_data
*me
)
137 midend_free_game(me
);
139 random_free(me
->random
);
144 me
->ourgame
->free_params(me
->params
);
146 for (i
= 0; i
< me
->npresets
; i
++) {
147 sfree(me
->presets
[i
]);
148 sfree(me
->preset_names
[i
]);
151 sfree(me
->preset_names
);
154 me
->ourgame
->free_ui(me
->ui
);
156 me
->ourgame
->free_params(me
->curparams
);
157 sfree(me
->laststatus
);
161 void midend_size(midend_data
*me
, int *x
, int *y
, int expand
)
163 me
->ourgame
->size(me
->params
, me
->drawstate
, x
, y
, expand
);
168 void midend_set_params(midend_data
*me
, game_params
*params
)
170 me
->ourgame
->free_params(me
->params
);
171 me
->params
= me
->ourgame
->dup_params(params
);
174 static void midend_set_timer(midend_data
*me
)
176 me
->timing
= (me
->ourgame
->is_timed
&&
177 me
->ourgame
->timing_state(me
->states
[me
->statepos
-1].state
));
178 if (me
->timing
|| me
->flash_time
|| me
->anim_time
)
179 activate_timer(me
->frontend
);
181 deactivate_timer(me
->frontend
);
184 static void midend_size_new_drawstate(midend_data
*me
)
186 me
->ourgame
->size(me
->params
, me
->drawstate
, &me
->winwidth
, &me
->winheight
,
190 void midend_force_redraw(midend_data
*me
)
193 me
->ourgame
->free_drawstate(me
->drawstate
);
194 me
->drawstate
= me
->ourgame
->new_drawstate(me
->states
[0].state
);
195 midend_size_new_drawstate(me
);
199 void midend_new_game(midend_data
*me
)
201 midend_free_game(me
);
203 assert(me
->nstates
== 0);
205 if (me
->genmode
== GOT_DESC
) {
206 me
->genmode
= GOT_NOTHING
;
210 if (me
->genmode
== GOT_SEED
) {
211 me
->genmode
= GOT_NOTHING
;
214 * Generate a new random seed. 15 digits comes to about
215 * 48 bits, which should be more than enough.
217 * I'll avoid putting a leading zero on the number,
218 * just in case it confuses anybody who thinks it's
219 * processed as an integer rather than a string.
224 newseed
[0] = '1' + random_upto(me
->random
, 9);
225 for (i
= 1; i
< 15; i
++)
226 newseed
[i
] = '0' + random_upto(me
->random
, 10);
228 me
->seedstr
= dupstr(newseed
);
231 me
->ourgame
->free_params(me
->curparams
);
232 me
->curparams
= me
->ourgame
->dup_params(me
->params
);
240 rs
= random_init(me
->seedstr
, strlen(me
->seedstr
));
241 me
->desc
= me
->ourgame
->new_desc(me
->curparams
, rs
,
242 &me
->aux_info
, TRUE
);
248 me
->states
[me
->nstates
].state
=
249 me
->ourgame
->new_game(me
, me
->params
, me
->desc
);
250 me
->states
[me
->nstates
].special
= TRUE
;
253 me
->drawstate
= me
->ourgame
->new_drawstate(me
->states
[0].state
);
254 midend_size_new_drawstate(me
);
256 midend_set_timer(me
);
258 me
->ourgame
->free_ui(me
->ui
);
259 me
->ui
= me
->ourgame
->new_ui(me
->states
[0].state
);
260 me
->pressed_mouse_button
= 0;
263 static int midend_undo(midend_data
*me
)
265 if (me
->statepos
> 1) {
267 me
->ourgame
->changed_state(me
->ui
,
268 me
->states
[me
->statepos
-1].state
,
269 me
->states
[me
->statepos
-2].state
);
277 static int midend_redo(midend_data
*me
)
279 if (me
->statepos
< me
->nstates
) {
281 me
->ourgame
->changed_state(me
->ui
,
282 me
->states
[me
->statepos
-1].state
,
283 me
->states
[me
->statepos
].state
);
291 static void midend_finish_move(midend_data
*me
)
296 * We do not flash if the later of the two states is special.
297 * This covers both forward Solve moves and backward (undone)
300 if ((me
->oldstate
|| me
->statepos
> 1) &&
301 ((me
->dir
> 0 && !me
->states
[me
->statepos
-1].special
) ||
302 (me
->dir
< 0 && me
->statepos
< me
->nstates
&&
303 !me
->states
[me
->statepos
].special
))) {
304 flashtime
= me
->ourgame
->flash_length(me
->oldstate ? me
->oldstate
:
305 me
->states
[me
->statepos
-2].state
,
306 me
->states
[me
->statepos
-1].state
,
307 me
->oldstate ? me
->dir
: +1,
310 me
->flash_pos
= 0.0F
;
311 me
->flash_time
= flashtime
;
316 me
->ourgame
->free_game(me
->oldstate
);
318 me
->anim_pos
= me
->anim_time
= 0;
321 midend_set_timer(me
);
324 void midend_stop_anim(midend_data
*me
)
326 if (me
->oldstate
|| me
->anim_time
) {
327 midend_finish_move(me
);
332 void midend_restart_game(midend_data
*me
)
336 midend_stop_anim(me
);
338 assert(me
->statepos
>= 1);
339 if (me
->statepos
== 1)
340 return; /* no point doing anything at all! */
343 * During restart, we reconstruct the game from the (public)
344 * game description rather than from states[0], because that
345 * way Mines gets slightly more sensible behaviour (restart
346 * goes to _after_ the first click so you don't have to
347 * remember where you clicked).
349 s
= me
->ourgame
->new_game(me
, me
->params
, me
->desc
);
352 * Now enter the restarted state as the next move.
354 midend_stop_anim(me
);
355 while (me
->nstates
> me
->statepos
)
356 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
358 me
->states
[me
->nstates
].state
= s
;
359 me
->states
[me
->nstates
].special
= TRUE
; /* we just restarted */
360 me
->statepos
= ++me
->nstates
;
362 me
->ourgame
->changed_state(me
->ui
,
363 me
->states
[me
->statepos
-2].state
,
364 me
->states
[me
->statepos
-1].state
);
366 midend_finish_move(me
);
368 midend_set_timer(me
);
371 static int midend_really_process_key(midend_data
*me
, int x
, int y
, int button
)
373 game_state
*oldstate
=
374 me
->ourgame
->dup_game(me
->states
[me
->statepos
- 1].state
);
375 int special
= FALSE
, gotspecial
= FALSE
, ret
= 1;
378 if (button
== 'n' || button
== 'N' || button
== '\x0E') {
379 midend_stop_anim(me
);
382 goto done
; /* never animate */
383 } else if (button
== 'u' || button
== 'u' ||
384 button
== '\x1A' || button
== '\x1F') {
385 midend_stop_anim(me
);
386 special
= me
->states
[me
->statepos
-1].special
;
388 if (!midend_undo(me
))
390 } else if (button
== 'r' || button
== 'R' ||
391 button
== '\x12' || button
== '\x19') {
392 midend_stop_anim(me
);
393 if (!midend_redo(me
))
395 } else if (button
== 'q' || button
== 'Q' || button
== '\x11') {
403 me
->ourgame
->interpret_move(me
->states
[me
->statepos
-1].state
,
404 me
->ui
, me
->drawstate
, x
, y
, button
);
408 s
= me
->states
[me
->statepos
-1].state
;
410 s
= me
->ourgame
->execute_move(me
->states
[me
->statepos
-1].state
,
416 if (s
== me
->states
[me
->statepos
-1].state
) {
418 * make_move() is allowed to return its input state to
419 * indicate that although no move has been made, the UI
420 * state has been updated and a redraw is called for.
425 midend_stop_anim(me
);
426 while (me
->nstates
> me
->statepos
)
427 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
429 me
->states
[me
->nstates
].state
= s
;
430 me
->states
[me
->nstates
].special
= FALSE
; /* normal move */
431 me
->statepos
= ++me
->nstates
;
439 special
= me
->states
[me
->statepos
-1].special
;
442 * See if this move requires an animation.
447 anim_time
= me
->ourgame
->anim_length(oldstate
,
448 me
->states
[me
->statepos
-1].state
,
452 me
->oldstate
= oldstate
; oldstate
= NULL
;
454 me
->anim_time
= anim_time
;
457 midend_finish_move(me
);
463 midend_set_timer(me
);
466 if (oldstate
) me
->ourgame
->free_game(oldstate
);
470 int midend_process_key(midend_data
*me
, int x
, int y
, int button
)
475 * Harmonise mouse drag and release messages.
477 * Some front ends might accidentally switch from sending, say,
478 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
479 * drag. (This can happen on the Mac, for example, since
480 * RIGHT_DRAG is usually done using Command+drag, and if the
481 * user accidentally releases Command half way through the drag
482 * then there will be trouble.)
484 * It would be an O(number of front ends) annoyance to fix this
485 * in the front ends, but an O(number of back ends) annoyance
486 * to have each game capable of dealing with it. Therefore, we
487 * fix it _here_ in the common midend code so that it only has
490 * The possible ways in which things can go screwy in the front
493 * - in a system containing multiple physical buttons button
494 * presses can inadvertently overlap. We can see ABab (caps
495 * meaning button-down and lowercase meaning button-up) when
496 * the user had semantically intended AaBb.
498 * - in a system where one button is simulated by means of a
499 * modifier key and another button, buttons can mutate
500 * between press and release (possibly during drag). So we
501 * can see Ab instead of Aa.
503 * Definite requirements are:
505 * - button _presses_ must never be invented or destroyed. If
506 * the user presses two buttons in succession, the button
507 * presses must be transferred to the backend unchanged. So
508 * if we see AaBb , that's fine; if we see ABab (the button
509 * presses inadvertently overlapped) we must somehow
510 * `correct' it to AaBb.
512 * - every mouse action must end up looking like a press, zero
513 * or more drags, then a release. This allows back ends to
514 * make the _assumption_ that incoming mouse data will be
515 * sane in this regard, and not worry about the details.
517 * So my policy will be:
519 * - treat any button-up as a button-up for the currently
520 * pressed button, or ignore it if there is no currently
523 * - treat any drag as a drag for the currently pressed
524 * button, or ignore it if there is no currently pressed
527 * - if we see a button-down while another button is currently
528 * pressed, invent a button-up for the first one and then
529 * pass the button-down through as before.
531 * 2005-05-31: An addendum to the above. Some games might want
532 * a `priority order' among buttons, such that if one button is
533 * pressed while another is down then a fixed one of the
534 * buttons takes priority no matter what order they're pressed
535 * in. Mines, in particular, wants to treat a left+right click
536 * like a left click for the benefit of users of other
537 * implementations. So the last of the above points is modified
538 * in the presence of an (optional) button priority order.
540 if (IS_MOUSE_DRAG(button
) || IS_MOUSE_RELEASE(button
)) {
541 if (me
->pressed_mouse_button
) {
542 if (IS_MOUSE_DRAG(button
)) {
543 button
= me
->pressed_mouse_button
+
544 (LEFT_DRAG
- LEFT_BUTTON
);
546 button
= me
->pressed_mouse_button
+
547 (LEFT_RELEASE
- LEFT_BUTTON
);
550 return ret
; /* ignore it */
551 } else if (IS_MOUSE_DOWN(button
) && me
->pressed_mouse_button
) {
553 * If the new button has lower priority than the old one,
554 * don't bother doing this.
556 if (me
->ourgame
->mouse_priorities
&
557 BUTTON_BEATS(me
->pressed_mouse_button
, button
))
558 return ret
; /* just ignore it */
561 * Fabricate a button-up for the previously pressed button.
563 ret
= ret
&& midend_really_process_key
564 (me
, x
, y
, (me
->pressed_mouse_button
+
565 (LEFT_RELEASE
- LEFT_BUTTON
)));
569 * Now send on the event we originally received.
571 ret
= ret
&& midend_really_process_key(me
, x
, y
, button
);
574 * And update the currently pressed button.
576 if (IS_MOUSE_RELEASE(button
))
577 me
->pressed_mouse_button
= 0;
578 else if (IS_MOUSE_DOWN(button
))
579 me
->pressed_mouse_button
= button
;
584 void midend_redraw(midend_data
*me
)
586 if (me
->statepos
> 0 && me
->drawstate
) {
587 start_draw(me
->frontend
);
588 if (me
->oldstate
&& me
->anim_time
> 0 &&
589 me
->anim_pos
< me
->anim_time
) {
590 assert(me
->dir
!= 0);
591 me
->ourgame
->redraw(me
->frontend
, me
->drawstate
, me
->oldstate
,
592 me
->states
[me
->statepos
-1].state
, me
->dir
,
593 me
->ui
, me
->anim_pos
, me
->flash_pos
);
595 me
->ourgame
->redraw(me
->frontend
, me
->drawstate
, NULL
,
596 me
->states
[me
->statepos
-1].state
, +1 /*shrug*/,
597 me
->ui
, 0.0, me
->flash_pos
);
599 end_draw(me
->frontend
);
603 void midend_timer(midend_data
*me
, float tplus
)
605 me
->anim_pos
+= tplus
;
606 if (me
->anim_pos
>= me
->anim_time
||
607 me
->anim_time
== 0 || !me
->oldstate
) {
608 if (me
->anim_time
> 0)
609 midend_finish_move(me
);
612 me
->flash_pos
+= tplus
;
613 if (me
->flash_pos
>= me
->flash_time
|| me
->flash_time
== 0) {
614 me
->flash_pos
= me
->flash_time
= 0;
620 float oldelapsed
= me
->elapsed
;
621 me
->elapsed
+= tplus
;
622 if ((int)oldelapsed
!= (int)me
->elapsed
)
623 status_bar(me
->frontend
, me
->laststatus ? me
->laststatus
: "");
626 midend_set_timer(me
);
629 float *midend_colours(midend_data
*me
, int *ncolours
)
631 game_state
*state
= NULL
;
634 if (me
->nstates
== 0) {
636 char *desc
= me
->ourgame
->new_desc(me
->params
, me
->random
,
638 state
= me
->ourgame
->new_game(me
, me
->params
, desc
);
642 state
= me
->states
[0].state
;
644 ret
= me
->ourgame
->colours(me
->frontend
, state
, ncolours
);
650 * Allow environment-based overrides for the standard
651 * colours by defining variables along the lines of
652 * `NET_COLOUR_4=6000c0'.
655 for (i
= 0; i
< *ncolours
; i
++) {
657 unsigned int r
, g
, b
;
660 sprintf(buf
, "%s_COLOUR_%d", me
->ourgame
->name
, i
);
661 for (j
= 0; buf
[j
]; j
++)
662 buf
[j
] = toupper((unsigned char)buf
[j
]);
663 if ((e
= getenv(buf
)) != NULL
&&
664 sscanf(e
, "%2x%2x%2x", &r
, &g
, &b
) == 3) {
665 ret
[i
*3 + 0] = r
/ 255.0;
666 ret
[i
*3 + 1] = g
/ 255.0;
667 ret
[i
*3 + 2] = b
/ 255.0;
672 if (me
->nstates
== 0)
673 me
->ourgame
->free_game(state
);
678 int midend_num_presets(midend_data
*me
)
684 while (me
->ourgame
->fetch_preset(me
->npresets
, &name
, &preset
)) {
685 if (me
->presetsize
<= me
->npresets
) {
686 me
->presetsize
= me
->npresets
+ 10;
687 me
->presets
= sresize(me
->presets
, me
->presetsize
,
689 me
->preset_names
= sresize(me
->preset_names
, me
->presetsize
,
693 me
->presets
[me
->npresets
] = preset
;
694 me
->preset_names
[me
->npresets
] = name
;
701 * Allow environment-based extensions to the preset list by
702 * defining a variable along the lines of `SOLO_PRESETS=2x3
703 * Advanced:2x3da'. Colon-separated list of items,
704 * alternating between textual titles in the menu and
705 * encoded parameter strings.
707 char buf
[80], *e
, *p
;
710 sprintf(buf
, "%s_PRESETS", me
->ourgame
->name
);
711 for (j
= 0; buf
[j
]; j
++)
712 buf
[j
] = toupper((unsigned char)buf
[j
]);
714 if ((e
= getenv(buf
)) != NULL
) {
722 while (*p
&& *p
!= ':') p
++;
725 while (*p
&& *p
!= ':') p
++;
728 preset
= me
->ourgame
->default_params();
729 me
->ourgame
->decode_params(preset
, val
);
731 if (me
->ourgame
->validate_params(preset
)) {
732 /* Drop this one from the list. */
733 me
->ourgame
->free_params(preset
);
737 if (me
->presetsize
<= me
->npresets
) {
738 me
->presetsize
= me
->npresets
+ 10;
739 me
->presets
= sresize(me
->presets
, me
->presetsize
,
741 me
->preset_names
= sresize(me
->preset_names
,
742 me
->presetsize
, char *);
745 me
->presets
[me
->npresets
] = preset
;
746 me
->preset_names
[me
->npresets
] = name
;
755 void midend_fetch_preset(midend_data
*me
, int n
,
756 char **name
, game_params
**params
)
758 assert(n
>= 0 && n
< me
->npresets
);
759 *name
= me
->preset_names
[n
];
760 *params
= me
->presets
[n
];
763 int midend_wants_statusbar(midend_data
*me
)
765 return me
->ourgame
->wants_statusbar();
768 void midend_supersede_game_desc(midend_data
*me
, char *desc
, char *privdesc
)
772 me
->desc
= dupstr(desc
);
773 me
->privdesc
= privdesc ?
dupstr(privdesc
) : NULL
;
776 config_item
*midend_get_config(midend_data
*me
, int which
, char **wintitle
)
778 char *titlebuf
, *parstr
, *rest
;
783 titlebuf
= snewn(40 + strlen(me
->ourgame
->name
), char);
787 sprintf(titlebuf
, "%s configuration", me
->ourgame
->name
);
788 *wintitle
= titlebuf
;
789 return me
->ourgame
->configure(me
->params
);
792 if (!me
->curparams
) {
796 sprintf(titlebuf
, "%s %s selection", me
->ourgame
->name
,
797 which
== CFG_SEED ?
"random" : "game");
798 *wintitle
= titlebuf
;
800 ret
= snewn(2, config_item
);
802 ret
[0].type
= C_STRING
;
803 if (which
== CFG_SEED
)
804 ret
[0].name
= "Game random seed";
806 ret
[0].name
= "Game ID";
809 * For CFG_DESC the text going in here will be a string
810 * encoding of the restricted parameters, plus a colon,
811 * plus the game description. For CFG_SEED it will be the
812 * full parameters, plus a hash, plus the random seed data.
813 * Either of these is a valid full game ID (although only
814 * the former is likely to persist across many code
817 parstr
= me
->ourgame
->encode_params(me
->curparams
, which
== CFG_SEED
);
819 if (which
== CFG_DESC
) {
820 rest
= me
->desc ? me
->desc
: "";
823 rest
= me
->seedstr ? me
->seedstr
: "";
826 ret
[0].sval
= snewn(strlen(parstr
) + strlen(rest
) + 2, char);
827 sprintf(ret
[0].sval
, "%s%c%s", parstr
, sep
, rest
);
831 ret
[1].name
= ret
[1].sval
= NULL
;
837 assert(!"We shouldn't be here");
841 static char *midend_game_id_int(midend_data
*me
, char *id
, int defmode
)
843 char *error
, *par
, *desc
, *seed
;
845 seed
= strchr(id
, '#');
846 desc
= strchr(id
, ':');
848 if (desc
&& (!seed
|| desc
< seed
)) {
850 * We have a colon separating parameters from game
851 * description. So `par' now points to the parameters
852 * string, and `desc' to the description string.
857 } else if (seed
&& (!desc
|| seed
< desc
)) {
859 * We have a hash separating parameters from random seed.
860 * So `par' now points to the parameters string, and `seed'
861 * to the seed string.
868 * We only have one string. Depending on `defmode', we take
869 * it to be either parameters, seed or description.
871 if (defmode
== DEF_SEED
) {
874 } else if (defmode
== DEF_DESC
) {
884 game_params
*tmpparams
;
885 tmpparams
= me
->ourgame
->dup_params(me
->params
);
886 me
->ourgame
->decode_params(tmpparams
, par
);
887 error
= me
->ourgame
->validate_params(tmpparams
);
889 me
->ourgame
->free_params(tmpparams
);
893 me
->ourgame
->free_params(me
->curparams
);
894 me
->curparams
= tmpparams
;
897 * Now filter only the persistent parts of this state into
898 * the long-term params structure, unless we've _only_
899 * received a params string in which case the whole lot is
903 char *tmpstr
= me
->ourgame
->encode_params(tmpparams
, FALSE
);
904 me
->ourgame
->decode_params(me
->params
, tmpstr
);
907 me
->ourgame
->free_params(me
->params
);
908 me
->params
= me
->ourgame
->dup_params(tmpparams
);
914 me
->desc
= me
->privdesc
= NULL
;
919 error
= me
->ourgame
->validate_desc(me
->params
, desc
);
923 me
->desc
= dupstr(desc
);
924 me
->genmode
= GOT_DESC
;
930 me
->seedstr
= dupstr(seed
);
931 me
->genmode
= GOT_SEED
;
937 char *midend_game_id(midend_data
*me
, char *id
)
939 return midend_game_id_int(me
, id
, DEF_PARAMS
);
942 char *midend_set_config(midend_data
*me
, int which
, config_item
*cfg
)
949 params
= me
->ourgame
->custom_params(cfg
);
950 error
= me
->ourgame
->validate_params(params
);
953 me
->ourgame
->free_params(params
);
957 me
->ourgame
->free_params(me
->params
);
963 error
= midend_game_id_int(me
, cfg
[0].sval
,
964 (which
== CFG_SEED ? DEF_SEED
: DEF_DESC
));
973 char *midend_text_format(midend_data
*me
)
975 if (me
->ourgame
->can_format_as_text
&& me
->statepos
> 0)
976 return me
->ourgame
->text_format(me
->states
[me
->statepos
-1].state
);
981 char *midend_solve(midend_data
*me
)
986 if (!me
->ourgame
->can_solve
)
987 return "This game does not support the Solve operation";
989 if (me
->statepos
< 1)
990 return "No game set up to solve"; /* _shouldn't_ happen! */
992 msg
= "Solve operation failed"; /* game _should_ overwrite on error */
993 movestr
= me
->ourgame
->solve(me
->states
[0].state
,
994 me
->states
[me
->statepos
-1].state
,
998 s
= me
->ourgame
->execute_move(me
->states
[me
->statepos
-1].state
, movestr
);
1003 * Now enter the solved state as the next move.
1005 midend_stop_anim(me
);
1006 while (me
->nstates
> me
->statepos
)
1007 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
1009 me
->states
[me
->nstates
].state
= s
;
1010 me
->states
[me
->nstates
].special
= TRUE
; /* created using solve */
1011 me
->statepos
= ++me
->nstates
;
1013 me
->ourgame
->changed_state(me
->ui
,
1014 me
->states
[me
->statepos
-2].state
,
1015 me
->states
[me
->statepos
-1].state
);
1016 me
->anim_time
= 0.0;
1017 midend_finish_move(me
);
1019 midend_set_timer(me
);
1023 char *midend_rewrite_statusbar(midend_data
*me
, char *text
)
1026 * An important special case is that we are occasionally called
1027 * with our own laststatus, to update the timer.
1029 if (me
->laststatus
!= text
) {
1030 sfree(me
->laststatus
);
1031 me
->laststatus
= dupstr(text
);
1034 if (me
->ourgame
->is_timed
) {
1035 char timebuf
[100], *ret
;
1041 sprintf(timebuf
, "[%d:%02d] ", min
, sec
);
1043 ret
= snewn(strlen(timebuf
) + strlen(text
) + 1, char);
1044 strcpy(ret
, timebuf
);
1049 return dupstr(text
);