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 enum { NEWGAME
, MOVE
, SOLVE
, RESTART
};/* for midend_state_entry.movetype */
20 #define special(type) ( (type) != MOVE )
22 struct midend_state_entry
{
33 game_params
**presets
;
35 int npresets
, presetsize
;
38 * `desc' and `privdesc' deserve a comment.
40 * `desc' is the game description as presented to the user when
41 * they ask for Game -> Specific. `privdesc', if non-NULL, is a
42 * different game description used to reconstruct the initial
43 * game_state when de-serialising. If privdesc is NULL, `desc'
46 * For almost all games, `privdesc' is NULL and never used. The
47 * exception (as usual) is Mines: the initial game state has no
48 * squares open at all, but after the first click `desc' is
49 * rewritten to describe a game state with an initial click and
50 * thus a bunch of squares open. If we used that desc to
51 * serialise and deserialise, then the initial game state after
52 * deserialisation would look unlike the initial game state
53 * beforehand, and worse still execute_move() might fail on the
54 * attempted first click. So `privdesc' is also used in this
55 * case, to provide a game description describing the same
56 * fixed mine layout _but_ no initial click. (These game IDs
57 * may also be typed directly into Mines if you like.)
59 char *desc
, *privdesc
, *seedstr
;
61 enum { GOT_SEED
, GOT_DESC
, GOT_NOTHING
} genmode
;
63 int nstates
, statesize
, statepos
;
64 struct midend_state_entry
*states
;
66 game_params
*params
, *curparams
;
67 game_drawstate
*drawstate
;
71 float anim_time
, anim_pos
;
72 float flash_time
, flash_pos
;
79 int pressed_mouse_button
;
81 int winwidth
, winheight
;
84 #define ensure(me) do { \
85 if ((me)->nstates >= (me)->statesize) { \
86 (me)->statesize = (me)->nstates + 128; \
87 (me)->states = sresize((me)->states, (me)->statesize, \
88 struct midend_state_entry); \
92 midend_data
*midend_new(frontend
*fe
, const game
*ourgame
)
94 midend_data
*me
= snew(midend_data
);
98 get_random_seed(&randseed
, &randseedsize
);
101 me
->ourgame
= ourgame
;
102 me
->random
= random_init(randseed
, randseedsize
);
103 me
->nstates
= me
->statesize
= me
->statepos
= 0;
105 me
->params
= ourgame
->default_params();
106 me
->curparams
= NULL
;
107 me
->desc
= me
->privdesc
= NULL
;
110 me
->genmode
= GOT_NOTHING
;
111 me
->drawstate
= NULL
;
114 me
->preset_names
= NULL
;
115 me
->npresets
= me
->presetsize
= 0;
116 me
->anim_time
= me
->anim_pos
= 0.0F
;
117 me
->flash_time
= me
->flash_pos
= 0.0F
;
120 me
->pressed_mouse_button
= 0;
121 me
->laststatus
= NULL
;
124 me
->winwidth
= me
->winheight
= 0;
131 static void midend_free_game(midend_data
*me
)
133 while (me
->nstates
> 0) {
135 me
->ourgame
->free_game(me
->states
[me
->nstates
].state
);
136 sfree(me
->states
[me
->nstates
].movestr
);
140 me
->ourgame
->free_drawstate(me
->drawstate
);
143 void midend_free(midend_data
*me
)
147 midend_free_game(me
);
149 random_free(me
->random
);
154 me
->ourgame
->free_params(me
->params
);
156 for (i
= 0; i
< me
->npresets
; i
++) {
157 sfree(me
->presets
[i
]);
158 sfree(me
->preset_names
[i
]);
161 sfree(me
->preset_names
);
164 me
->ourgame
->free_ui(me
->ui
);
166 me
->ourgame
->free_params(me
->curparams
);
167 sfree(me
->laststatus
);
171 void midend_size(midend_data
*me
, int *x
, int *y
, int expand
)
173 me
->ourgame
->size(me
->params
, me
->drawstate
, x
, y
, expand
);
178 void midend_set_params(midend_data
*me
, game_params
*params
)
180 me
->ourgame
->free_params(me
->params
);
181 me
->params
= me
->ourgame
->dup_params(params
);
184 static void midend_set_timer(midend_data
*me
)
186 me
->timing
= (me
->ourgame
->is_timed
&&
187 me
->ourgame
->timing_state(me
->states
[me
->statepos
-1].state
));
188 if (me
->timing
|| me
->flash_time
|| me
->anim_time
)
189 activate_timer(me
->frontend
);
191 deactivate_timer(me
->frontend
);
194 static void midend_size_new_drawstate(midend_data
*me
)
196 me
->ourgame
->size(me
->params
, me
->drawstate
, &me
->winwidth
, &me
->winheight
,
200 void midend_force_redraw(midend_data
*me
)
203 me
->ourgame
->free_drawstate(me
->drawstate
);
204 me
->drawstate
= me
->ourgame
->new_drawstate(me
->states
[0].state
);
205 midend_size_new_drawstate(me
);
209 void midend_new_game(midend_data
*me
)
211 midend_free_game(me
);
213 assert(me
->nstates
== 0);
215 if (me
->genmode
== GOT_DESC
) {
216 me
->genmode
= GOT_NOTHING
;
220 if (me
->genmode
== GOT_SEED
) {
221 me
->genmode
= GOT_NOTHING
;
224 * Generate a new random seed. 15 digits comes to about
225 * 48 bits, which should be more than enough.
227 * I'll avoid putting a leading zero on the number,
228 * just in case it confuses anybody who thinks it's
229 * processed as an integer rather than a string.
234 newseed
[0] = '1' + random_upto(me
->random
, 9);
235 for (i
= 1; i
< 15; i
++)
236 newseed
[i
] = '0' + random_upto(me
->random
, 10);
238 me
->seedstr
= dupstr(newseed
);
241 me
->ourgame
->free_params(me
->curparams
);
242 me
->curparams
= me
->ourgame
->dup_params(me
->params
);
250 rs
= random_init(me
->seedstr
, strlen(me
->seedstr
));
251 me
->desc
= me
->ourgame
->new_desc(me
->curparams
, rs
,
252 &me
->aux_info
, TRUE
);
258 me
->states
[me
->nstates
].state
=
259 me
->ourgame
->new_game(me
, me
->params
, me
->desc
);
260 me
->states
[me
->nstates
].movestr
= NULL
;
261 me
->states
[me
->nstates
].movetype
= NEWGAME
;
264 me
->drawstate
= me
->ourgame
->new_drawstate(me
->states
[0].state
);
265 midend_size_new_drawstate(me
);
267 midend_set_timer(me
);
269 me
->ourgame
->free_ui(me
->ui
);
270 me
->ui
= me
->ourgame
->new_ui(me
->states
[0].state
);
271 me
->pressed_mouse_button
= 0;
274 static int midend_undo(midend_data
*me
)
276 if (me
->statepos
> 1) {
278 me
->ourgame
->changed_state(me
->ui
,
279 me
->states
[me
->statepos
-1].state
,
280 me
->states
[me
->statepos
-2].state
);
288 static int midend_redo(midend_data
*me
)
290 if (me
->statepos
< me
->nstates
) {
292 me
->ourgame
->changed_state(me
->ui
,
293 me
->states
[me
->statepos
-1].state
,
294 me
->states
[me
->statepos
].state
);
302 static void midend_finish_move(midend_data
*me
)
307 * We do not flash if the later of the two states is special.
308 * This covers both forward Solve moves and backward (undone)
311 if ((me
->oldstate
|| me
->statepos
> 1) &&
312 ((me
->dir
> 0 && !special(me
->states
[me
->statepos
-1].movetype
)) ||
313 (me
->dir
< 0 && me
->statepos
< me
->nstates
&&
314 !special(me
->states
[me
->statepos
].movetype
)))) {
315 flashtime
= me
->ourgame
->flash_length(me
->oldstate ? me
->oldstate
:
316 me
->states
[me
->statepos
-2].state
,
317 me
->states
[me
->statepos
-1].state
,
318 me
->oldstate ? me
->dir
: +1,
321 me
->flash_pos
= 0.0F
;
322 me
->flash_time
= flashtime
;
327 me
->ourgame
->free_game(me
->oldstate
);
329 me
->anim_pos
= me
->anim_time
= 0;
332 midend_set_timer(me
);
335 void midend_stop_anim(midend_data
*me
)
337 if (me
->oldstate
|| me
->anim_time
) {
338 midend_finish_move(me
);
343 void midend_restart_game(midend_data
*me
)
347 midend_stop_anim(me
);
349 assert(me
->statepos
>= 1);
350 if (me
->statepos
== 1)
351 return; /* no point doing anything at all! */
354 * During restart, we reconstruct the game from the (public)
355 * game description rather than from states[0], because that
356 * way Mines gets slightly more sensible behaviour (restart
357 * goes to _after_ the first click so you don't have to
358 * remember where you clicked).
360 s
= me
->ourgame
->new_game(me
, me
->params
, me
->desc
);
363 * Now enter the restarted state as the next move.
365 midend_stop_anim(me
);
366 while (me
->nstates
> me
->statepos
)
367 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
369 me
->states
[me
->nstates
].state
= s
;
370 me
->states
[me
->nstates
].movestr
= dupstr(me
->desc
);
371 me
->states
[me
->nstates
].movetype
= RESTART
;
372 me
->statepos
= ++me
->nstates
;
374 me
->ourgame
->changed_state(me
->ui
,
375 me
->states
[me
->statepos
-2].state
,
376 me
->states
[me
->statepos
-1].state
);
378 midend_finish_move(me
);
380 midend_set_timer(me
);
383 static int midend_really_process_key(midend_data
*me
, int x
, int y
, int button
)
385 game_state
*oldstate
=
386 me
->ourgame
->dup_game(me
->states
[me
->statepos
- 1].state
);
387 int special
= FALSE
, gotspecial
= FALSE
, ret
= 1;
390 if (button
== 'n' || button
== 'N' || button
== '\x0E') {
391 midend_stop_anim(me
);
394 goto done
; /* never animate */
395 } else if (button
== 'u' || button
== 'u' ||
396 button
== '\x1A' || button
== '\x1F') {
397 midend_stop_anim(me
);
398 special
= special(me
->states
[me
->statepos
-1].movetype
);
400 if (!midend_undo(me
))
402 } else if (button
== 'r' || button
== 'R' ||
403 button
== '\x12' || button
== '\x19') {
404 midend_stop_anim(me
);
405 if (!midend_redo(me
))
407 } else if (button
== 'q' || button
== 'Q' || button
== '\x11') {
415 me
->ourgame
->interpret_move(me
->states
[me
->statepos
-1].state
,
416 me
->ui
, me
->drawstate
, x
, y
, button
);
420 s
= me
->states
[me
->statepos
-1].state
;
422 s
= me
->ourgame
->execute_move(me
->states
[me
->statepos
-1].state
,
427 if (s
== me
->states
[me
->statepos
-1].state
) {
429 * make_move() is allowed to return its input state to
430 * indicate that although no move has been made, the UI
431 * state has been updated and a redraw is called for.
436 midend_stop_anim(me
);
437 while (me
->nstates
> me
->statepos
)
438 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
440 assert(movestr
!= NULL
);
441 me
->states
[me
->nstates
].state
= s
;
442 me
->states
[me
->nstates
].movestr
= movestr
;
443 me
->states
[me
->nstates
].movetype
= MOVE
;
444 me
->statepos
= ++me
->nstates
;
452 special
= special(me
->states
[me
->statepos
-1].movetype
);
455 * See if this move requires an animation.
460 anim_time
= me
->ourgame
->anim_length(oldstate
,
461 me
->states
[me
->statepos
-1].state
,
465 me
->oldstate
= oldstate
; oldstate
= NULL
;
467 me
->anim_time
= anim_time
;
470 midend_finish_move(me
);
476 midend_set_timer(me
);
479 if (oldstate
) me
->ourgame
->free_game(oldstate
);
483 int midend_process_key(midend_data
*me
, int x
, int y
, int button
)
488 * Harmonise mouse drag and release messages.
490 * Some front ends might accidentally switch from sending, say,
491 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
492 * drag. (This can happen on the Mac, for example, since
493 * RIGHT_DRAG is usually done using Command+drag, and if the
494 * user accidentally releases Command half way through the drag
495 * then there will be trouble.)
497 * It would be an O(number of front ends) annoyance to fix this
498 * in the front ends, but an O(number of back ends) annoyance
499 * to have each game capable of dealing with it. Therefore, we
500 * fix it _here_ in the common midend code so that it only has
503 * The possible ways in which things can go screwy in the front
506 * - in a system containing multiple physical buttons button
507 * presses can inadvertently overlap. We can see ABab (caps
508 * meaning button-down and lowercase meaning button-up) when
509 * the user had semantically intended AaBb.
511 * - in a system where one button is simulated by means of a
512 * modifier key and another button, buttons can mutate
513 * between press and release (possibly during drag). So we
514 * can see Ab instead of Aa.
516 * Definite requirements are:
518 * - button _presses_ must never be invented or destroyed. If
519 * the user presses two buttons in succession, the button
520 * presses must be transferred to the backend unchanged. So
521 * if we see AaBb , that's fine; if we see ABab (the button
522 * presses inadvertently overlapped) we must somehow
523 * `correct' it to AaBb.
525 * - every mouse action must end up looking like a press, zero
526 * or more drags, then a release. This allows back ends to
527 * make the _assumption_ that incoming mouse data will be
528 * sane in this regard, and not worry about the details.
530 * So my policy will be:
532 * - treat any button-up as a button-up for the currently
533 * pressed button, or ignore it if there is no currently
536 * - treat any drag as a drag for the currently pressed
537 * button, or ignore it if there is no currently pressed
540 * - if we see a button-down while another button is currently
541 * pressed, invent a button-up for the first one and then
542 * pass the button-down through as before.
544 * 2005-05-31: An addendum to the above. Some games might want
545 * a `priority order' among buttons, such that if one button is
546 * pressed while another is down then a fixed one of the
547 * buttons takes priority no matter what order they're pressed
548 * in. Mines, in particular, wants to treat a left+right click
549 * like a left click for the benefit of users of other
550 * implementations. So the last of the above points is modified
551 * in the presence of an (optional) button priority order.
553 if (IS_MOUSE_DRAG(button
) || IS_MOUSE_RELEASE(button
)) {
554 if (me
->pressed_mouse_button
) {
555 if (IS_MOUSE_DRAG(button
)) {
556 button
= me
->pressed_mouse_button
+
557 (LEFT_DRAG
- LEFT_BUTTON
);
559 button
= me
->pressed_mouse_button
+
560 (LEFT_RELEASE
- LEFT_BUTTON
);
563 return ret
; /* ignore it */
564 } else if (IS_MOUSE_DOWN(button
) && me
->pressed_mouse_button
) {
566 * If the new button has lower priority than the old one,
567 * don't bother doing this.
569 if (me
->ourgame
->mouse_priorities
&
570 BUTTON_BEATS(me
->pressed_mouse_button
, button
))
571 return ret
; /* just ignore it */
574 * Fabricate a button-up for the previously pressed button.
576 ret
= ret
&& midend_really_process_key
577 (me
, x
, y
, (me
->pressed_mouse_button
+
578 (LEFT_RELEASE
- LEFT_BUTTON
)));
582 * Now send on the event we originally received.
584 ret
= ret
&& midend_really_process_key(me
, x
, y
, button
);
587 * And update the currently pressed button.
589 if (IS_MOUSE_RELEASE(button
))
590 me
->pressed_mouse_button
= 0;
591 else if (IS_MOUSE_DOWN(button
))
592 me
->pressed_mouse_button
= button
;
597 void midend_redraw(midend_data
*me
)
599 if (me
->statepos
> 0 && me
->drawstate
) {
600 start_draw(me
->frontend
);
601 if (me
->oldstate
&& me
->anim_time
> 0 &&
602 me
->anim_pos
< me
->anim_time
) {
603 assert(me
->dir
!= 0);
604 me
->ourgame
->redraw(me
->frontend
, me
->drawstate
, me
->oldstate
,
605 me
->states
[me
->statepos
-1].state
, me
->dir
,
606 me
->ui
, me
->anim_pos
, me
->flash_pos
);
608 me
->ourgame
->redraw(me
->frontend
, me
->drawstate
, NULL
,
609 me
->states
[me
->statepos
-1].state
, +1 /*shrug*/,
610 me
->ui
, 0.0, me
->flash_pos
);
612 end_draw(me
->frontend
);
616 void midend_timer(midend_data
*me
, float tplus
)
618 me
->anim_pos
+= tplus
;
619 if (me
->anim_pos
>= me
->anim_time
||
620 me
->anim_time
== 0 || !me
->oldstate
) {
621 if (me
->anim_time
> 0)
622 midend_finish_move(me
);
625 me
->flash_pos
+= tplus
;
626 if (me
->flash_pos
>= me
->flash_time
|| me
->flash_time
== 0) {
627 me
->flash_pos
= me
->flash_time
= 0;
633 float oldelapsed
= me
->elapsed
;
634 me
->elapsed
+= tplus
;
635 if ((int)oldelapsed
!= (int)me
->elapsed
)
636 status_bar(me
->frontend
, me
->laststatus ? me
->laststatus
: "");
639 midend_set_timer(me
);
642 float *midend_colours(midend_data
*me
, int *ncolours
)
644 game_state
*state
= NULL
;
647 if (me
->nstates
== 0) {
649 char *desc
= me
->ourgame
->new_desc(me
->params
, me
->random
,
651 state
= me
->ourgame
->new_game(me
, me
->params
, desc
);
655 state
= me
->states
[0].state
;
657 ret
= me
->ourgame
->colours(me
->frontend
, state
, ncolours
);
663 * Allow environment-based overrides for the standard
664 * colours by defining variables along the lines of
665 * `NET_COLOUR_4=6000c0'.
668 for (i
= 0; i
< *ncolours
; i
++) {
670 unsigned int r
, g
, b
;
673 sprintf(buf
, "%s_COLOUR_%d", me
->ourgame
->name
, i
);
674 for (j
= 0; buf
[j
]; j
++)
675 buf
[j
] = toupper((unsigned char)buf
[j
]);
676 if ((e
= getenv(buf
)) != NULL
&&
677 sscanf(e
, "%2x%2x%2x", &r
, &g
, &b
) == 3) {
678 ret
[i
*3 + 0] = r
/ 255.0;
679 ret
[i
*3 + 1] = g
/ 255.0;
680 ret
[i
*3 + 2] = b
/ 255.0;
685 if (me
->nstates
== 0)
686 me
->ourgame
->free_game(state
);
691 int midend_num_presets(midend_data
*me
)
697 while (me
->ourgame
->fetch_preset(me
->npresets
, &name
, &preset
)) {
698 if (me
->presetsize
<= me
->npresets
) {
699 me
->presetsize
= me
->npresets
+ 10;
700 me
->presets
= sresize(me
->presets
, me
->presetsize
,
702 me
->preset_names
= sresize(me
->preset_names
, me
->presetsize
,
706 me
->presets
[me
->npresets
] = preset
;
707 me
->preset_names
[me
->npresets
] = name
;
714 * Allow environment-based extensions to the preset list by
715 * defining a variable along the lines of `SOLO_PRESETS=2x3
716 * Advanced:2x3da'. Colon-separated list of items,
717 * alternating between textual titles in the menu and
718 * encoded parameter strings.
720 char buf
[80], *e
, *p
;
723 sprintf(buf
, "%s_PRESETS", me
->ourgame
->name
);
724 for (j
= 0; buf
[j
]; j
++)
725 buf
[j
] = toupper((unsigned char)buf
[j
]);
727 if ((e
= getenv(buf
)) != NULL
) {
735 while (*p
&& *p
!= ':') p
++;
738 while (*p
&& *p
!= ':') p
++;
741 preset
= me
->ourgame
->default_params();
742 me
->ourgame
->decode_params(preset
, val
);
744 if (me
->ourgame
->validate_params(preset
)) {
745 /* Drop this one from the list. */
746 me
->ourgame
->free_params(preset
);
750 if (me
->presetsize
<= me
->npresets
) {
751 me
->presetsize
= me
->npresets
+ 10;
752 me
->presets
= sresize(me
->presets
, me
->presetsize
,
754 me
->preset_names
= sresize(me
->preset_names
,
755 me
->presetsize
, char *);
758 me
->presets
[me
->npresets
] = preset
;
759 me
->preset_names
[me
->npresets
] = name
;
768 void midend_fetch_preset(midend_data
*me
, int n
,
769 char **name
, game_params
**params
)
771 assert(n
>= 0 && n
< me
->npresets
);
772 *name
= me
->preset_names
[n
];
773 *params
= me
->presets
[n
];
776 int midend_wants_statusbar(midend_data
*me
)
778 return me
->ourgame
->wants_statusbar();
781 void midend_supersede_game_desc(midend_data
*me
, char *desc
, char *privdesc
)
785 me
->desc
= dupstr(desc
);
786 me
->privdesc
= privdesc ?
dupstr(privdesc
) : NULL
;
789 config_item
*midend_get_config(midend_data
*me
, int which
, char **wintitle
)
791 char *titlebuf
, *parstr
, *rest
;
796 titlebuf
= snewn(40 + strlen(me
->ourgame
->name
), char);
800 sprintf(titlebuf
, "%s configuration", me
->ourgame
->name
);
801 *wintitle
= titlebuf
;
802 return me
->ourgame
->configure(me
->params
);
805 if (!me
->curparams
) {
809 sprintf(titlebuf
, "%s %s selection", me
->ourgame
->name
,
810 which
== CFG_SEED ?
"random" : "game");
811 *wintitle
= titlebuf
;
813 ret
= snewn(2, config_item
);
815 ret
[0].type
= C_STRING
;
816 if (which
== CFG_SEED
)
817 ret
[0].name
= "Game random seed";
819 ret
[0].name
= "Game ID";
822 * For CFG_DESC the text going in here will be a string
823 * encoding of the restricted parameters, plus a colon,
824 * plus the game description. For CFG_SEED it will be the
825 * full parameters, plus a hash, plus the random seed data.
826 * Either of these is a valid full game ID (although only
827 * the former is likely to persist across many code
830 parstr
= me
->ourgame
->encode_params(me
->curparams
, which
== CFG_SEED
);
832 if (which
== CFG_DESC
) {
833 rest
= me
->desc ? me
->desc
: "";
836 rest
= me
->seedstr ? me
->seedstr
: "";
839 ret
[0].sval
= snewn(strlen(parstr
) + strlen(rest
) + 2, char);
840 sprintf(ret
[0].sval
, "%s%c%s", parstr
, sep
, rest
);
844 ret
[1].name
= ret
[1].sval
= NULL
;
850 assert(!"We shouldn't be here");
854 static char *midend_game_id_int(midend_data
*me
, char *id
, int defmode
)
856 char *error
, *par
, *desc
, *seed
;
858 seed
= strchr(id
, '#');
859 desc
= strchr(id
, ':');
861 if (desc
&& (!seed
|| desc
< seed
)) {
863 * We have a colon separating parameters from game
864 * description. So `par' now points to the parameters
865 * string, and `desc' to the description string.
870 } else if (seed
&& (!desc
|| seed
< desc
)) {
872 * We have a hash separating parameters from random seed.
873 * So `par' now points to the parameters string, and `seed'
874 * to the seed string.
881 * We only have one string. Depending on `defmode', we take
882 * it to be either parameters, seed or description.
884 if (defmode
== DEF_SEED
) {
887 } else if (defmode
== DEF_DESC
) {
897 game_params
*tmpparams
;
898 tmpparams
= me
->ourgame
->dup_params(me
->params
);
899 me
->ourgame
->decode_params(tmpparams
, par
);
900 error
= me
->ourgame
->validate_params(tmpparams
);
902 me
->ourgame
->free_params(tmpparams
);
906 me
->ourgame
->free_params(me
->curparams
);
907 me
->curparams
= tmpparams
;
910 * Now filter only the persistent parts of this state into
911 * the long-term params structure, unless we've _only_
912 * received a params string in which case the whole lot is
916 char *tmpstr
= me
->ourgame
->encode_params(tmpparams
, FALSE
);
917 me
->ourgame
->decode_params(me
->params
, tmpstr
);
920 me
->ourgame
->free_params(me
->params
);
921 me
->params
= me
->ourgame
->dup_params(tmpparams
);
927 me
->desc
= me
->privdesc
= NULL
;
932 error
= me
->ourgame
->validate_desc(me
->params
, desc
);
936 me
->desc
= dupstr(desc
);
937 me
->genmode
= GOT_DESC
;
943 me
->seedstr
= dupstr(seed
);
944 me
->genmode
= GOT_SEED
;
950 char *midend_game_id(midend_data
*me
, char *id
)
952 return midend_game_id_int(me
, id
, DEF_PARAMS
);
955 char *midend_set_config(midend_data
*me
, int which
, config_item
*cfg
)
962 params
= me
->ourgame
->custom_params(cfg
);
963 error
= me
->ourgame
->validate_params(params
);
966 me
->ourgame
->free_params(params
);
970 me
->ourgame
->free_params(me
->params
);
976 error
= midend_game_id_int(me
, cfg
[0].sval
,
977 (which
== CFG_SEED ? DEF_SEED
: DEF_DESC
));
986 char *midend_text_format(midend_data
*me
)
988 if (me
->ourgame
->can_format_as_text
&& me
->statepos
> 0)
989 return me
->ourgame
->text_format(me
->states
[me
->statepos
-1].state
);
994 char *midend_solve(midend_data
*me
)
999 if (!me
->ourgame
->can_solve
)
1000 return "This game does not support the Solve operation";
1002 if (me
->statepos
< 1)
1003 return "No game set up to solve"; /* _shouldn't_ happen! */
1005 msg
= "Solve operation failed"; /* game _should_ overwrite on error */
1006 movestr
= me
->ourgame
->solve(me
->states
[0].state
,
1007 me
->states
[me
->statepos
-1].state
,
1008 me
->aux_info
, &msg
);
1011 s
= me
->ourgame
->execute_move(me
->states
[me
->statepos
-1].state
, movestr
);
1015 * Now enter the solved state as the next move.
1017 midend_stop_anim(me
);
1018 while (me
->nstates
> me
->statepos
)
1019 me
->ourgame
->free_game(me
->states
[--me
->nstates
].state
);
1021 me
->states
[me
->nstates
].state
= s
;
1022 me
->states
[me
->nstates
].movestr
= movestr
;
1023 me
->states
[me
->nstates
].movetype
= SOLVE
;
1024 me
->statepos
= ++me
->nstates
;
1026 me
->ourgame
->changed_state(me
->ui
,
1027 me
->states
[me
->statepos
-2].state
,
1028 me
->states
[me
->statepos
-1].state
);
1029 me
->anim_time
= 0.0;
1030 midend_finish_move(me
);
1032 midend_set_timer(me
);
1036 char *midend_rewrite_statusbar(midend_data
*me
, char *text
)
1039 * An important special case is that we are occasionally called
1040 * with our own laststatus, to update the timer.
1042 if (me
->laststatus
!= text
) {
1043 sfree(me
->laststatus
);
1044 me
->laststatus
= dupstr(text
);
1047 if (me
->ourgame
->is_timed
) {
1048 char timebuf
[100], *ret
;
1054 sprintf(timebuf
, "[%d:%02d] ", min
, sec
);
1056 ret
= snewn(strlen(timebuf
) + strlen(text
) + 1, char);
1057 strcpy(ret
, timebuf
);
1062 return dupstr(text
);
1066 #define SERIALISE_MAGIC "Simon Tatham's Portable Puzzle Collection"
1067 #define SERIALISE_VERSION "1"
1069 void midend_serialise(midend_data
*me
,
1070 void (*write
)(void *ctx
, void *buf
, int len
),
1076 * Each line of the save file contains three components. First
1077 * exactly 8 characters of header word indicating what type of
1078 * data is contained on the line; then a colon followed by a
1079 * decimal integer giving the length of the main string on the
1080 * line; then a colon followed by the string itself (exactly as
1081 * many bytes as previously specified, no matter what they
1082 * contain). Then a newline (of reasonably flexible form).
1084 #define wr(h,s) do { \
1087 sprintf(hbuf, "%-8.8s:%d:", (h), strlen(str)); \
1088 write(wctx, hbuf, strlen(hbuf)); \
1089 write(wctx, str, strlen(str)); \
1090 write(wctx, "\n", 1); \
1094 * Magic string identifying the file, and version number of the
1097 wr("SAVEFILE", SERIALISE_MAGIC
);
1098 wr("VERSION", SERIALISE_VERSION
);
1101 * The game name. (Copied locally to avoid const annoyance.)
1104 char *s
= dupstr(me
->ourgame
->name
);
1110 * The current long-term parameters structure, in full.
1113 char *s
= me
->ourgame
->encode_params(me
->params
, TRUE
);
1119 * The current short-term parameters structure, in full.
1121 if (me
->curparams
) {
1122 char *s
= me
->ourgame
->encode_params(me
->curparams
, TRUE
);
1128 * The current game description, the privdesc, and the random seed.
1131 wr("SEED", me
->seedstr
);
1133 wr("DESC", me
->desc
);
1135 wr("PRIVDESC", me
->privdesc
);
1138 * The game's aux_info. We obfuscate this to prevent spoilers
1139 * (people are likely to run `head' or similar on a saved game
1140 * file simply to find out what it is, and don't necessarily
1141 * want to be told the answer to the puzzle!)
1148 len
= strlen(me
->aux_info
);
1149 s1
= snewn(len
, unsigned char);
1150 memcpy(s1
, me
->aux_info
, len
);
1151 obfuscate_bitmap(s1
, len
*8, FALSE
);
1152 s2
= bin2hex(s1
, len
);
1161 * Any required serialisation of the game_ui.
1164 char *s
= me
->ourgame
->encode_ui(me
->ui
);
1172 * The game time, if it's a timed game.
1174 if (me
->ourgame
->is_timed
) {
1176 sprintf(buf
, "%g", me
->elapsed
);
1181 * The length of, and position in, the states list.
1185 sprintf(buf
, "%d", me
->nstates
);
1187 sprintf(buf
, "%d", me
->statepos
);
1188 wr("STATEPOS", buf
);
1192 * For each state after the initial one (which we know is
1193 * constructed from either privdesc or desc), enough
1194 * information for execute_move() to reconstruct it from the
1197 for (i
= 1; i
< me
->nstates
; i
++) {
1198 assert(me
->states
[i
].movetype
!= NEWGAME
); /* only state 0 */
1199 switch (me
->states
[i
].movetype
) {
1201 wr("MOVE", me
->states
[i
].movestr
);
1204 wr("SOLVE", me
->states
[i
].movestr
);
1207 wr("RESTART", me
->states
[i
].movestr
);
1216 * This function returns NULL on success, or an error message.
1218 char *midend_deserialise(midend_data
*me
,
1219 int (*read
)(void *ctx
, void *buf
, int len
),
1222 int nstates
= 0, statepos
= -1, gotstates
= 0;
1223 int started
= FALSE
;
1227 /* Initially all errors give the same report */
1228 char *ret
= "Data does not appear to be a saved game file";
1231 * We construct all the new state in local variables while we
1232 * check its sanity. Only once we have finished reading the
1233 * serialised data and detected no errors at all do we start
1234 * modifying stuff in the midend_data passed in.
1236 char *seed
= NULL
, *parstr
= NULL
, *desc
= NULL
, *privdesc
= NULL
;
1237 char *auxinfo
= NULL
, *uistr
= NULL
, *cparstr
= NULL
;
1238 float elapsed
= 0.0F
;
1239 game_params
*params
= NULL
, *cparams
= NULL
;
1241 struct midend_state_entry
*states
= NULL
;
1244 * Loop round and round reading one key/value pair at a time
1245 * from the serialised stream, until we have enough game states
1248 while (nstates
<= 0 || statepos
< 0 || gotstates
< nstates
-1) {
1253 if (!read(rctx
, key
, 1)) {
1254 /* unexpected EOF */
1257 } while (key
[0] == '\r' || key
[0] == '\n');
1259 if (!read(rctx
, key
+1, 8)) {
1260 /* unexpected EOF */
1264 if (key
[8] != ':') {
1266 ret
= "Data was incorrectly formatted for a saved game file";
1268 len
= strcspn(key
, ": ");
1274 if (!read(rctx
, &c
, 1)) {
1275 /* unexpected EOF */
1281 } else if (c
>= '0' && c
<= '9') {
1282 len
= (len
* 10) + (c
- '0');
1285 ret
= "Data was incorrectly formatted for a"
1291 val
= snewn(len
+1, char);
1292 if (!read(rctx
, val
, len
)) {
1299 if (strcmp(key
, "SAVEFILE") || strcmp(val
, SERIALISE_MAGIC
)) {
1300 /* ret already has the right message in it */
1303 /* Now most errors are this one, unless otherwise specified */
1304 ret
= "Saved data ended unexpectedly";
1307 if (!strcmp(key
, "VERSION")) {
1308 if (strcmp(val
, SERIALISE_VERSION
)) {
1309 ret
= "Cannot handle this version of the saved game"
1313 } else if (!strcmp(key
, "GAME")) {
1314 if (strcmp(val
, me
->ourgame
->name
)) {
1315 ret
= "Save file is from a different game";
1318 } else if (!strcmp(key
, "PARAMS")) {
1322 } else if (!strcmp(key
, "CPARAMS")) {
1326 } else if (!strcmp(key
, "SEED")) {
1330 } else if (!strcmp(key
, "DESC")) {
1334 } else if (!strcmp(key
, "PRIVDESC")) {
1338 } else if (!strcmp(key
, "AUXINFO")) {
1340 int len
= strlen(val
) / 2; /* length in bytes */
1341 tmp
= hex2bin(val
, len
);
1342 obfuscate_bitmap(tmp
, len
*8, TRUE
);
1345 auxinfo
= snewn(len
+ 1, char);
1346 memcpy(auxinfo
, tmp
, len
);
1347 auxinfo
[len
] = '\0';
1349 } else if (!strcmp(key
, "UI")) {
1353 } else if (!strcmp(key
, "TIME")) {
1354 elapsed
= strtod(val
, NULL
);
1355 } else if (!strcmp(key
, "NSTATES")) {
1356 nstates
= atoi(val
);
1358 ret
= "Number of states in save file was negative";
1362 ret
= "Two state counts provided in save file";
1365 states
= snewn(nstates
, struct midend_state_entry
);
1366 for (i
= 0; i
< nstates
; i
++) {
1367 states
[i
].state
= NULL
;
1368 states
[i
].movestr
= NULL
;
1369 states
[i
].movetype
= NEWGAME
;
1371 } else if (!strcmp(key
, "STATEPOS")) {
1372 statepos
= atoi(val
);
1373 } else if (!strcmp(key
, "MOVE")) {
1375 states
[gotstates
].movetype
= MOVE
;
1376 states
[gotstates
].movestr
= val
;
1378 } else if (!strcmp(key
, "SOLVE")) {
1380 states
[gotstates
].movetype
= SOLVE
;
1381 states
[gotstates
].movestr
= val
;
1383 } else if (!strcmp(key
, "RESTART")) {
1385 states
[gotstates
].movetype
= RESTART
;
1386 states
[gotstates
].movestr
= val
;
1395 params
= me
->ourgame
->default_params();
1396 me
->ourgame
->decode_params(params
, parstr
);
1397 if (me
->ourgame
->validate_params(params
)) {
1398 ret
= "Long-term parameters in save file are invalid";
1401 cparams
= me
->ourgame
->default_params();
1402 me
->ourgame
->decode_params(cparams
, cparstr
);
1403 if (me
->ourgame
->validate_params(cparams
)) {
1404 ret
= "Short-term parameters in save file are invalid";
1408 ret
= "Game description in save file is missing";
1410 } else if (me
->ourgame
->validate_desc(params
, desc
)) {
1411 ret
= "Game description in save file is invalid";
1414 if (privdesc
&& me
->ourgame
->validate_desc(params
, privdesc
)) {
1415 ret
= "Game private description in save file is invalid";
1418 if (statepos
< 0 || statepos
>= nstates
) {
1419 ret
= "Game position in save file is out of range";
1422 states
[0].state
= me
->ourgame
->new_game(me
, params
,
1423 privdesc ? privdesc
: desc
);
1424 for (i
= 1; i
< nstates
; i
++) {
1425 assert(states
[i
].movetype
!= NEWGAME
);
1426 switch (states
[i
].movetype
) {
1429 states
[i
].state
= me
->ourgame
->execute_move(states
[i
-1].state
,
1431 if (states
[i
].state
== NULL
) {
1432 ret
= "Save file contained an invalid move";
1437 if (me
->ourgame
->validate_desc(params
, states
[i
].movestr
)) {
1438 ret
= "Save file contained an invalid restart move";
1441 states
[i
].state
= me
->ourgame
->new_game(me
, params
,
1447 ui
= me
->ourgame
->new_ui(states
[0].state
);
1448 me
->ourgame
->decode_ui(ui
, uistr
);
1451 * Now we've run out of possible error conditions, so we're
1452 * ready to start overwriting the real data in the current
1453 * midend. We'll do this by swapping things with the local
1454 * variables, so that the same cleanup code will free the old
1465 me
->privdesc
= privdesc
;
1473 me
->aux_info
= auxinfo
;
1477 me
->genmode
= GOT_NOTHING
;
1479 me
->statesize
= nstates
;
1480 nstates
= me
->nstates
;
1481 me
->nstates
= me
->statesize
;
1483 struct midend_state_entry
*tmp
;
1485 me
->states
= states
;
1488 me
->statepos
= statepos
;
1494 me
->params
= params
;
1497 tmp
= me
->curparams
;
1498 me
->curparams
= cparams
;
1502 me
->oldstate
= NULL
;
1503 me
->anim_time
= me
->anim_pos
= me
->flash_time
= me
->flash_pos
= 0.0F
;
1514 me
->elapsed
= elapsed
;
1515 me
->pressed_mouse_button
= 0;
1517 midend_set_timer(me
);
1519 ret
= NULL
; /* success! */
1531 me
->ourgame
->free_params(params
);
1533 me
->ourgame
->free_params(cparams
);
1535 me
->ourgame
->free_ui(ui
);
1539 for (i
= 0; i
< nstates
; i
++) {
1540 if (states
[i
].state
)
1541 me
->ourgame
->free_game(states
[i
].state
);
1542 sfree(states
[i
].movestr
);