Validation of random-state-type game descriptions was broken. This
[sgt/puzzles] / midend.c
CommitLineData
720a8fb7 1/*
2 * midend.c: general middle fragment sitting between the
3 * platform-specific front end and game-specific back end.
4 * Maintains a move list, takes care of Undo and Redo commands, and
7f89707c 5 * processes standard keystrokes for undo/redo/new/quit.
720a8fb7 6 */
7f77ea24 7
8#include <stdio.h>
5928817c 9#include <string.h>
7f77ea24 10#include <assert.h>
813593cc 11#include <stdlib.h>
12#include <ctype.h>
7f77ea24 13
14#include "puzzles.h"
15
1185e3c5 16enum { DEF_PARAMS, DEF_SEED, DEF_DESC }; /* for midend_game_id_int */
17
a4393230 18enum { NEWGAME, MOVE, SOLVE, RESTART };/* for midend_state_entry.movetype */
19
20#define special(type) ( (type) != MOVE )
21
28d0118c 22struct midend_state_entry {
23 game_state *state;
a4393230 24 char *movestr;
25 int movetype;
28d0118c 26};
27
7f77ea24 28struct midend_data {
2ef96bd6 29 frontend *frontend;
48d70ca9 30 random_state *random;
be8d5aa1 31 const game *ourgame;
48d70ca9 32
a4393230 33 game_params **presets;
34 char **preset_names;
35 int npresets, presetsize;
36
0a6892db 37 /*
38 * `desc' and `privdesc' deserve a comment.
39 *
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'
44 * is used for both.
45 *
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.)
58 */
59 char *desc, *privdesc, *seedstr;
c566778e 60 char *aux_info;
1185e3c5 61 enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
eb2ad6f1 62
a4393230 63 int nstates, statesize, statepos;
64 struct midend_state_entry *states;
eb2ad6f1 65
f60f7e7c 66 game_params *params, *curparams;
2ef96bd6 67 game_drawstate *drawstate;
74a4e547 68 game_ui *ui;
a4393230 69
70 game_state *oldstate;
2ef96bd6 71 float anim_time, anim_pos;
87ed82be 72 float flash_time, flash_pos;
c822de4a 73 int dir;
6776a950 74
48dcdd62 75 int timing;
76 float elapsed;
77 char *laststatus;
78
6776a950 79 int pressed_mouse_button;
1e3e152d 80
81 int winwidth, winheight;
7f77ea24 82};
83
84#define ensure(me) do { \
85 if ((me)->nstates >= (me)->statesize) { \
86 (me)->statesize = (me)->nstates + 128; \
28d0118c 87 (me)->states = sresize((me)->states, (me)->statesize, \
88 struct midend_state_entry); \
7f77ea24 89 } \
90} while (0)
91
be8d5aa1 92midend_data *midend_new(frontend *fe, const game *ourgame)
7f77ea24 93{
94 midend_data *me = snew(midend_data);
cbb5549e 95 void *randseed;
96 int randseedsize;
97
98 get_random_seed(&randseed, &randseedsize);
7f77ea24 99
48d70ca9 100 me->frontend = fe;
be8d5aa1 101 me->ourgame = ourgame;
48d70ca9 102 me->random = random_init(randseed, randseedsize);
7f77ea24 103 me->nstates = me->statesize = me->statepos = 0;
104 me->states = NULL;
be8d5aa1 105 me->params = ourgame->default_params();
f60f7e7c 106 me->curparams = NULL;
0a6892db 107 me->desc = me->privdesc = NULL;
1185e3c5 108 me->seedstr = NULL;
6f2d8d7c 109 me->aux_info = NULL;
1185e3c5 110 me->genmode = GOT_NOTHING;
2ef96bd6 111 me->drawstate = NULL;
112 me->oldstate = NULL;
eb2ad6f1 113 me->presets = NULL;
114 me->preset_names = NULL;
115 me->npresets = me->presetsize = 0;
87ed82be 116 me->anim_time = me->anim_pos = 0.0F;
117 me->flash_time = me->flash_pos = 0.0F;
c822de4a 118 me->dir = 0;
74a4e547 119 me->ui = NULL;
6776a950 120 me->pressed_mouse_button = 0;
48dcdd62 121 me->laststatus = NULL;
122 me->timing = FALSE;
123 me->elapsed = 0.0F;
1e3e152d 124 me->winwidth = me->winheight = 0;
7f77ea24 125
cbb5549e 126 sfree(randseed);
127
7f77ea24 128 return me;
129}
130
ab53eb64 131static void midend_free_game(midend_data *me)
132{
a4393230 133 while (me->nstates > 0) {
134 me->nstates--;
135 me->ourgame->free_game(me->states[me->nstates].state);
136 sfree(me->states[me->nstates].movestr);
137 }
ab53eb64 138
139 if (me->drawstate)
140 me->ourgame->free_drawstate(me->drawstate);
141}
142
7f77ea24 143void midend_free(midend_data *me)
144{
ab53eb64 145 int i;
146
147 midend_free_game(me);
148
149 random_free(me->random);
7f77ea24 150 sfree(me->states);
1185e3c5 151 sfree(me->desc);
152 sfree(me->seedstr);
c566778e 153 sfree(me->aux_info);
be8d5aa1 154 me->ourgame->free_params(me->params);
ab53eb64 155 if (me->npresets) {
156 for (i = 0; i < me->npresets; i++) {
157 sfree(me->presets[i]);
158 sfree(me->preset_names[i]);
159 }
160 sfree(me->presets);
161 sfree(me->preset_names);
162 }
163 if (me->ui)
164 me->ourgame->free_ui(me->ui);
f60f7e7c 165 if (me->curparams)
166 me->ourgame->free_params(me->curparams);
48dcdd62 167 sfree(me->laststatus);
7f77ea24 168 sfree(me);
169}
170
1e3e152d 171void midend_size(midend_data *me, int *x, int *y, int expand)
7f77ea24 172{
1e3e152d 173 me->ourgame->size(me->params, me->drawstate, x, y, expand);
174 me->winwidth = *x;
175 me->winheight = *y;
7f77ea24 176}
177
178void midend_set_params(midend_data *me, game_params *params)
179{
be8d5aa1 180 me->ourgame->free_params(me->params);
181 me->params = me->ourgame->dup_params(params);
7f77ea24 182}
183
48dcdd62 184static void midend_set_timer(midend_data *me)
185{
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);
190 else
191 deactivate_timer(me->frontend);
192}
193
1e3e152d 194static void midend_size_new_drawstate(midend_data *me)
195{
196 me->ourgame->size(me->params, me->drawstate, &me->winwidth, &me->winheight,
197 TRUE);
198}
199
008b4378 200void midend_force_redraw(midend_data *me)
201{
202 if (me->drawstate)
203 me->ourgame->free_drawstate(me->drawstate);
204 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
1e3e152d 205 midend_size_new_drawstate(me);
008b4378 206 midend_redraw(me);
207}
208
5928817c 209void midend_new_game(midend_data *me)
7f77ea24 210{
ab53eb64 211 midend_free_game(me);
2ef96bd6 212
7f77ea24 213 assert(me->nstates == 0);
214
1185e3c5 215 if (me->genmode == GOT_DESC) {
216 me->genmode = GOT_NOTHING;
217 } else {
218 random_state *rs;
219
220 if (me->genmode == GOT_SEED) {
221 me->genmode = GOT_NOTHING;
222 } else {
223 /*
224 * Generate a new random seed. 15 digits comes to about
225 * 48 bits, which should be more than enough.
97c44af3 226 *
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.
1185e3c5 230 */
231 char newseed[16];
232 int i;
233 newseed[15] = '\0';
97c44af3 234 newseed[0] = '1' + random_upto(me->random, 9);
235 for (i = 1; i < 15; i++)
1185e3c5 236 newseed[i] = '0' + random_upto(me->random, 10);
237 sfree(me->seedstr);
238 me->seedstr = dupstr(newseed);
f60f7e7c 239
240 if (me->curparams)
241 me->ourgame->free_params(me->curparams);
242 me->curparams = me->ourgame->dup_params(me->params);
1185e3c5 243 }
244
245 sfree(me->desc);
0a6892db 246 sfree(me->privdesc);
c566778e 247 sfree(me->aux_info);
6f2d8d7c 248 me->aux_info = NULL;
1185e3c5 249
250 rs = random_init(me->seedstr, strlen(me->seedstr));
6aa6af4c 251 me->desc = me->ourgame->new_desc(me->curparams, rs,
252 &me->aux_info, TRUE);
0a6892db 253 me->privdesc = NULL;
1185e3c5 254 random_free(rs);
1185e3c5 255 }
7f77ea24 256
257 ensure(me);
c380832d 258 me->states[me->nstates].state =
259 me->ourgame->new_game(me, me->params, me->desc);
a4393230 260 me->states[me->nstates].movestr = NULL;
261 me->states[me->nstates].movetype = NEWGAME;
28d0118c 262 me->nstates++;
7f77ea24 263 me->statepos = 1;
28d0118c 264 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
1e3e152d 265 midend_size_new_drawstate(me);
48dcdd62 266 me->elapsed = 0.0F;
267 midend_set_timer(me);
74a4e547 268 if (me->ui)
be8d5aa1 269 me->ourgame->free_ui(me->ui);
28d0118c 270 me->ui = me->ourgame->new_ui(me->states[0].state);
6776a950 271 me->pressed_mouse_button = 0;
7f77ea24 272}
273
1482ee76 274static int midend_undo(midend_data *me)
7f77ea24 275{
1482ee76 276 if (me->statepos > 1) {
07dfb697 277 if (me->ui)
278 me->ourgame->changed_state(me->ui,
279 me->states[me->statepos-1].state,
280 me->states[me->statepos-2].state);
7f77ea24 281 me->statepos--;
c822de4a 282 me->dir = -1;
1482ee76 283 return 1;
284 } else
285 return 0;
7f77ea24 286}
287
1482ee76 288static int midend_redo(midend_data *me)
7f77ea24 289{
1482ee76 290 if (me->statepos < me->nstates) {
07dfb697 291 if (me->ui)
292 me->ourgame->changed_state(me->ui,
293 me->states[me->statepos-1].state,
294 me->states[me->statepos].state);
7f77ea24 295 me->statepos++;
c822de4a 296 me->dir = +1;
1482ee76 297 return 1;
298 } else
299 return 0;
7f77ea24 300}
301
87ed82be 302static void midend_finish_move(midend_data *me)
303{
304 float flashtime;
305
28d0118c 306 /*
307 * We do not flash if the later of the two states is special.
308 * This covers both forward Solve moves and backward (undone)
309 * Restart moves.
310 */
311 if ((me->oldstate || me->statepos > 1) &&
a4393230 312 ((me->dir > 0 && !special(me->states[me->statepos-1].movetype)) ||
28d0118c 313 (me->dir < 0 && me->statepos < me->nstates &&
a4393230 314 !special(me->states[me->statepos].movetype)))) {
be8d5aa1 315 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
28d0118c 316 me->states[me->statepos-2].state,
317 me->states[me->statepos-1].state,
e3f21163 318 me->oldstate ? me->dir : +1,
319 me->ui);
87ed82be 320 if (flashtime > 0) {
321 me->flash_pos = 0.0F;
322 me->flash_time = flashtime;
323 }
324 }
325
326 if (me->oldstate)
be8d5aa1 327 me->ourgame->free_game(me->oldstate);
87ed82be 328 me->oldstate = NULL;
329 me->anim_pos = me->anim_time = 0;
c822de4a 330 me->dir = 0;
87ed82be 331
48dcdd62 332 midend_set_timer(me);
87ed82be 333}
334
ab53eb64 335void midend_stop_anim(midend_data *me)
7f77ea24 336{
2ef96bd6 337 if (me->oldstate || me->anim_time) {
87ed82be 338 midend_finish_move(me);
2ef96bd6 339 midend_redraw(me);
340 }
dd216087 341}
342
28d0118c 343void midend_restart_game(midend_data *me)
344{
345 game_state *s;
346
7f89707c 347 midend_stop_anim(me);
348
28d0118c 349 assert(me->statepos >= 1);
350 if (me->statepos == 1)
351 return; /* no point doing anything at all! */
352
0a6892db 353 /*
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).
359 */
360 s = me->ourgame->new_game(me, me->params, me->desc);
28d0118c 361
362 /*
363 * Now enter the restarted state as the next move.
364 */
365 midend_stop_anim(me);
366 while (me->nstates > me->statepos)
367 me->ourgame->free_game(me->states[--me->nstates].state);
368 ensure(me);
369 me->states[me->nstates].state = s;
a4393230 370 me->states[me->nstates].movestr = dupstr(me->desc);
371 me->states[me->nstates].movetype = RESTART;
28d0118c 372 me->statepos = ++me->nstates;
07dfb697 373 if (me->ui)
374 me->ourgame->changed_state(me->ui,
375 me->states[me->statepos-2].state,
376 me->states[me->statepos-1].state);
28d0118c 377 me->anim_time = 0.0;
378 midend_finish_move(me);
379 midend_redraw(me);
48dcdd62 380 midend_set_timer(me);
28d0118c 381}
382
6776a950 383static int midend_really_process_key(midend_data *me, int x, int y, int button)
dd216087 384{
28d0118c 385 game_state *oldstate =
386 me->ourgame->dup_game(me->states[me->statepos - 1].state);
ab53eb64 387 int special = FALSE, gotspecial = FALSE, ret = 1;
dd216087 388 float anim_time;
7f77ea24 389
390 if (button == 'n' || button == 'N' || button == '\x0E') {
dd216087 391 midend_stop_anim(me);
5928817c 392 midend_new_game(me);
2ef96bd6 393 midend_redraw(me);
ab53eb64 394 goto done; /* never animate */
7f77ea24 395 } else if (button == 'u' || button == 'u' ||
1482ee76 396 button == '\x1A' || button == '\x1F') {
dd216087 397 midend_stop_anim(me);
a4393230 398 special = special(me->states[me->statepos-1].movetype);
28d0118c 399 gotspecial = TRUE;
1482ee76 400 if (!midend_undo(me))
ab53eb64 401 goto done;
7f89707c 402 } else if (button == 'r' || button == 'R' ||
31ad8194 403 button == '\x12' || button == '\x19') {
dd216087 404 midend_stop_anim(me);
1482ee76 405 if (!midend_redo(me))
ab53eb64 406 goto done;
7f77ea24 407 } else if (button == 'q' || button == 'Q' || button == '\x11') {
ab53eb64 408 ret = 0;
409 goto done;
2ef96bd6 410 } else {
df11cd4e 411 game_state *s;
412 char *movestr;
413
414 movestr =
415 me->ourgame->interpret_move(me->states[me->statepos-1].state,
416 me->ui, me->drawstate, x, y, button);
417 if (!movestr)
418 s = NULL;
419 else if (!*movestr)
420 s = me->states[me->statepos-1].state;
421 else {
422 s = me->ourgame->execute_move(me->states[me->statepos-1].state,
423 movestr);
424 assert(s != NULL);
df11cd4e 425 }
74a4e547 426
28d0118c 427 if (s == me->states[me->statepos-1].state) {
74a4e547 428 /*
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.
432 */
433 midend_redraw(me);
ab53eb64 434 goto done;
74a4e547 435 } else if (s) {
dd216087 436 midend_stop_anim(me);
2ef96bd6 437 while (me->nstates > me->statepos)
28d0118c 438 me->ourgame->free_game(me->states[--me->nstates].state);
2ef96bd6 439 ensure(me);
a4393230 440 assert(movestr != NULL);
28d0118c 441 me->states[me->nstates].state = s;
a4393230 442 me->states[me->nstates].movestr = movestr;
443 me->states[me->nstates].movetype = MOVE;
2ef96bd6 444 me->statepos = ++me->nstates;
c822de4a 445 me->dir = +1;
2ef96bd6 446 } else {
a4393230 447 goto done;
2ef96bd6 448 }
7f77ea24 449 }
450
28d0118c 451 if (!gotspecial)
a4393230 452 special = special(me->states[me->statepos-1].movetype);
28d0118c 453
2ef96bd6 454 /*
455 * See if this move requires an animation.
456 */
28d0118c 457 if (special) {
458 anim_time = 0;
459 } else {
460 anim_time = me->ourgame->anim_length(oldstate,
461 me->states[me->statepos-1].state,
e3f21163 462 me->dir, me->ui);
28d0118c 463 }
2ef96bd6 464
ab53eb64 465 me->oldstate = oldstate; oldstate = NULL;
2ef96bd6 466 if (anim_time > 0) {
2ef96bd6 467 me->anim_time = anim_time;
468 } else {
2ef96bd6 469 me->anim_time = 0.0;
87ed82be 470 midend_finish_move(me);
7f77ea24 471 }
2ef96bd6 472 me->anim_pos = 0.0;
473
474 midend_redraw(me);
475
48dcdd62 476 midend_set_timer(me);
7f77ea24 477
ab53eb64 478 done:
479 if (oldstate) me->ourgame->free_game(oldstate);
480 return ret;
7f77ea24 481}
2ef96bd6 482
6776a950 483int midend_process_key(midend_data *me, int x, int y, int button)
484{
485 int ret = 1;
486
487 /*
488 * Harmonise mouse drag and release messages.
489 *
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.)
496 *
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
501 * to be done once.
502 *
eeba2afe 503 * The possible ways in which things can go screwy in the front
504 * end are:
6776a950 505 *
eeba2afe 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.
6776a950 510 *
eeba2afe 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.
6776a950 515 *
eeba2afe 516 * Definite requirements are:
517 *
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.
524 *
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.
529 *
530 * So my policy will be:
531 *
532 * - treat any button-up as a button-up for the currently
533 * pressed button, or ignore it if there is no currently
534 * pressed button.
535 *
536 * - treat any drag as a drag for the currently pressed
537 * button, or ignore it if there is no currently pressed
538 * button.
539 *
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.
6776a950 543 *
93b1da3d 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.
6776a950 552 */
553 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
eeba2afe 554 if (me->pressed_mouse_button) {
555 if (IS_MOUSE_DRAG(button)) {
556 button = me->pressed_mouse_button +
557 (LEFT_DRAG - LEFT_BUTTON);
558 } else {
559 button = me->pressed_mouse_button +
560 (LEFT_RELEASE - LEFT_BUTTON);
6776a950 561 }
eeba2afe 562 } else
563 return ret; /* ignore it */
6776a950 564 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
93b1da3d 565 /*
566 * If the new button has lower priority than the old one,
567 * don't bother doing this.
568 */
569 if (me->ourgame->mouse_priorities &
570 BUTTON_BEATS(me->pressed_mouse_button, button))
571 return ret; /* just ignore it */
572
6776a950 573 /*
574 * Fabricate a button-up for the previously pressed button.
575 */
576 ret = ret && midend_really_process_key
577 (me, x, y, (me->pressed_mouse_button +
578 (LEFT_RELEASE - LEFT_BUTTON)));
579 }
580
581 /*
582 * Now send on the event we originally received.
583 */
584 ret = ret && midend_really_process_key(me, x, y, button);
585
586 /*
587 * And update the currently pressed button.
588 */
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;
593
594 return ret;
595}
596
2ef96bd6 597void midend_redraw(midend_data *me)
598{
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) {
c822de4a 603 assert(me->dir != 0);
be8d5aa1 604 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
28d0118c 605 me->states[me->statepos-1].state, me->dir,
be8d5aa1 606 me->ui, me->anim_pos, me->flash_pos);
2ef96bd6 607 } else {
be8d5aa1 608 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
28d0118c 609 me->states[me->statepos-1].state, +1 /*shrug*/,
be8d5aa1 610 me->ui, 0.0, me->flash_pos);
2ef96bd6 611 }
612 end_draw(me->frontend);
613 }
614}
615
616void midend_timer(midend_data *me, float tplus)
617{
618 me->anim_pos += tplus;
619 if (me->anim_pos >= me->anim_time ||
620 me->anim_time == 0 || !me->oldstate) {
87ed82be 621 if (me->anim_time > 0)
622 midend_finish_move(me);
623 }
48dcdd62 624
87ed82be 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;
2ef96bd6 628 }
48dcdd62 629
2ef96bd6 630 midend_redraw(me);
48dcdd62 631
632 if (me->timing) {
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 : "");
637 }
638
639 midend_set_timer(me);
2ef96bd6 640}
641
642float *midend_colours(midend_data *me, int *ncolours)
643{
644 game_state *state = NULL;
645 float *ret;
646
647 if (me->nstates == 0) {
c566778e 648 char *aux = NULL;
6aa6af4c 649 char *desc = me->ourgame->new_desc(me->params, me->random,
650 &aux, TRUE);
c380832d 651 state = me->ourgame->new_game(me, me->params, desc);
1185e3c5 652 sfree(desc);
c566778e 653 sfree(aux);
2ef96bd6 654 } else
28d0118c 655 state = me->states[0].state;
2ef96bd6 656
be8d5aa1 657 ret = me->ourgame->colours(me->frontend, state, ncolours);
2ef96bd6 658
813593cc 659 {
660 int i;
661
662 /*
663 * Allow environment-based overrides for the standard
664 * colours by defining variables along the lines of
665 * `NET_COLOUR_4=6000c0'.
666 */
667
668 for (i = 0; i < *ncolours; i++) {
669 char buf[80], *e;
670 unsigned int r, g, b;
671 int j;
672
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;
681 }
682 }
683 }
684
2ef96bd6 685 if (me->nstates == 0)
be8d5aa1 686 me->ourgame->free_game(state);
2ef96bd6 687
688 return ret;
689}
eb2ad6f1 690
691int midend_num_presets(midend_data *me)
692{
693 if (!me->npresets) {
694 char *name;
695 game_params *preset;
696
be8d5aa1 697 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
eb2ad6f1 698 if (me->presetsize <= me->npresets) {
699 me->presetsize = me->npresets + 10;
700 me->presets = sresize(me->presets, me->presetsize,
701 game_params *);
702 me->preset_names = sresize(me->preset_names, me->presetsize,
703 char *);
704 }
705
706 me->presets[me->npresets] = preset;
707 me->preset_names[me->npresets] = name;
708 me->npresets++;
709 }
710 }
711
813593cc 712 {
713 /*
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.
719 */
720 char buf[80], *e, *p;
721 int j;
722
723 sprintf(buf, "%s_PRESETS", me->ourgame->name);
724 for (j = 0; buf[j]; j++)
725 buf[j] = toupper((unsigned char)buf[j]);
726
727 if ((e = getenv(buf)) != NULL) {
728 p = e = dupstr(e);
729
730 while (*p) {
731 char *name, *val;
732 game_params *preset;
733
734 name = p;
735 while (*p && *p != ':') p++;
736 if (*p) *p++ = '\0';
737 val = p;
738 while (*p && *p != ':') p++;
739 if (*p) *p++ = '\0';
740
741 preset = me->ourgame->default_params();
742 me->ourgame->decode_params(preset, val);
743
239ba6e6 744 if (me->ourgame->validate_params(preset)) {
745 /* Drop this one from the list. */
746 me->ourgame->free_params(preset);
747 continue;
748 }
749
813593cc 750 if (me->presetsize <= me->npresets) {
751 me->presetsize = me->npresets + 10;
752 me->presets = sresize(me->presets, me->presetsize,
753 game_params *);
754 me->preset_names = sresize(me->preset_names,
755 me->presetsize, char *);
756 }
757
758 me->presets[me->npresets] = preset;
759 me->preset_names[me->npresets] = name;
760 me->npresets++;
761 }
762 }
763 }
764
eb2ad6f1 765 return me->npresets;
766}
767
768void midend_fetch_preset(midend_data *me, int n,
769 char **name, game_params **params)
770{
771 assert(n >= 0 && n < me->npresets);
772 *name = me->preset_names[n];
773 *params = me->presets[n];
774}
fd1a1a2b 775
776int midend_wants_statusbar(midend_data *me)
777{
be8d5aa1 778 return me->ourgame->wants_statusbar();
fd1a1a2b 779}
c8230524 780
0a6892db 781void midend_supersede_game_desc(midend_data *me, char *desc, char *privdesc)
c380832d 782{
783 sfree(me->desc);
0a6892db 784 sfree(me->privdesc);
c380832d 785 me->desc = dupstr(desc);
0a6892db 786 me->privdesc = privdesc ? dupstr(privdesc) : NULL;
c380832d 787}
788
5928817c 789config_item *midend_get_config(midend_data *me, int which, char **wintitle)
c8230524 790{
ab53eb64 791 char *titlebuf, *parstr, *rest;
5928817c 792 config_item *ret;
ab53eb64 793 char sep;
5928817c 794
ab53eb64 795 assert(wintitle);
be8d5aa1 796 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
5928817c 797
798 switch (which) {
799 case CFG_SETTINGS:
be8d5aa1 800 sprintf(titlebuf, "%s configuration", me->ourgame->name);
ab53eb64 801 *wintitle = titlebuf;
be8d5aa1 802 return me->ourgame->configure(me->params);
5928817c 803 case CFG_SEED:
1185e3c5 804 case CFG_DESC:
ab53eb64 805 if (!me->curparams) {
806 sfree(titlebuf);
807 return NULL;
808 }
809 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
1185e3c5 810 which == CFG_SEED ? "random" : "game");
ab53eb64 811 *wintitle = titlebuf;
5928817c 812
813 ret = snewn(2, config_item);
814
815 ret[0].type = C_STRING;
1185e3c5 816 if (which == CFG_SEED)
817 ret[0].name = "Game random seed";
818 else
819 ret[0].name = "Game ID";
5928817c 820 ret[0].ival = 0;
b0e26073 821 /*
1185e3c5 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
828 * changes).
b0e26073 829 */
f60f7e7c 830 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
ab53eb64 831 assert(parstr);
1185e3c5 832 if (which == CFG_DESC) {
ab53eb64 833 rest = me->desc ? me->desc : "";
834 sep = ':';
1185e3c5 835 } else {
ab53eb64 836 rest = me->seedstr ? me->seedstr : "";
837 sep = '#';
1185e3c5 838 }
ab53eb64 839 ret[0].sval = snewn(strlen(parstr) + strlen(rest) + 2, char);
840 sprintf(ret[0].sval, "%s%c%s", parstr, sep, rest);
b0e26073 841 sfree(parstr);
5928817c 842
843 ret[1].type = C_END;
844 ret[1].name = ret[1].sval = NULL;
845 ret[1].ival = 0;
846
847 return ret;
848 }
849
850 assert(!"We shouldn't be here");
851 return NULL;
c8230524 852}
853
1185e3c5 854static char *midend_game_id_int(midend_data *me, char *id, int defmode)
8b7938e7 855{
1185e3c5 856 char *error, *par, *desc, *seed;
8b7938e7 857
1185e3c5 858 seed = strchr(id, '#');
859 desc = strchr(id, ':');
8b7938e7 860
1185e3c5 861 if (desc && (!seed || desc < seed)) {
862 /*
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.
866 */
867 *desc++ = '\0';
868 par = id;
869 seed = NULL;
870 } else if (seed && (!desc || seed < desc)) {
8b7938e7 871 /*
50ff5730 872 * We have a hash separating parameters from random seed.
1185e3c5 873 * So `par' now points to the parameters string, and `seed'
874 * to the seed string.
8b7938e7 875 */
876 *seed++ = '\0';
877 par = id;
1185e3c5 878 desc = NULL;
8b7938e7 879 } else {
880 /*
1185e3c5 881 * We only have one string. Depending on `defmode', we take
882 * it to be either parameters, seed or description.
8b7938e7 883 */
1185e3c5 884 if (defmode == DEF_SEED) {
8b7938e7 885 seed = id;
1185e3c5 886 par = desc = NULL;
887 } else if (defmode == DEF_DESC) {
888 desc = id;
889 par = seed = NULL;
8b7938e7 890 } else {
8b7938e7 891 par = id;
1185e3c5 892 seed = desc = NULL;
8b7938e7 893 }
894 }
895
896 if (par) {
1185e3c5 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);
8b7938e7 901 if (error) {
1185e3c5 902 me->ourgame->free_params(tmpparams);
8b7938e7 903 return error;
904 }
f60f7e7c 905 if (me->curparams)
906 me->ourgame->free_params(me->curparams);
907 me->curparams = tmpparams;
1185e3c5 908
909 /*
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
913 * persistent.
914 */
915 if (seed || desc) {
916 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
917 me->ourgame->decode_params(me->params, tmpstr);
3af70dd4 918 sfree(tmpstr);
1185e3c5 919 } else {
920 me->ourgame->free_params(me->params);
921 me->params = me->ourgame->dup_params(tmpparams);
922 }
8b7938e7 923 }
924
3af70dd4 925 sfree(me->desc);
0a6892db 926 sfree(me->privdesc);
927 me->desc = me->privdesc = NULL;
3af70dd4 928 sfree(me->seedstr);
929 me->seedstr = NULL;
930
1185e3c5 931 if (desc) {
932 error = me->ourgame->validate_desc(me->params, desc);
8b7938e7 933 if (error)
934 return error;
935
1185e3c5 936 me->desc = dupstr(desc);
937 me->genmode = GOT_DESC;
c566778e 938 sfree(me->aux_info);
6f2d8d7c 939 me->aux_info = NULL;
8b7938e7 940 }
941
1185e3c5 942 if (seed) {
1185e3c5 943 me->seedstr = dupstr(seed);
944 me->genmode = GOT_SEED;
945 }
946
8b7938e7 947 return NULL;
948}
949
1185e3c5 950char *midend_game_id(midend_data *me, char *id)
951{
952 return midend_game_id_int(me, id, DEF_PARAMS);
953}
954
5928817c 955char *midend_set_config(midend_data *me, int which, config_item *cfg)
c8230524 956{
8b7938e7 957 char *error;
c8230524 958 game_params *params;
959
5928817c 960 switch (which) {
961 case CFG_SETTINGS:
be8d5aa1 962 params = me->ourgame->custom_params(cfg);
963 error = me->ourgame->validate_params(params);
c8230524 964
5928817c 965 if (error) {
be8d5aa1 966 me->ourgame->free_params(params);
5928817c 967 return error;
968 }
c8230524 969
be8d5aa1 970 me->ourgame->free_params(me->params);
5928817c 971 me->params = params;
972 break;
973
974 case CFG_SEED:
1185e3c5 975 case CFG_DESC:
976 error = midend_game_id_int(me, cfg[0].sval,
977 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
5928817c 978 if (error)
979 return error;
5928817c 980 break;
981 }
c8230524 982
983 return NULL;
984}
9b4b03d3 985
986char *midend_text_format(midend_data *me)
987{
988 if (me->ourgame->can_format_as_text && me->statepos > 0)
28d0118c 989 return me->ourgame->text_format(me->states[me->statepos-1].state);
9b4b03d3 990 else
991 return NULL;
992}
2ac6d24e 993
994char *midend_solve(midend_data *me)
995{
996 game_state *s;
df11cd4e 997 char *msg, *movestr;
2ac6d24e 998
999 if (!me->ourgame->can_solve)
1000 return "This game does not support the Solve operation";
1001
1002 if (me->statepos < 1)
1003 return "No game set up to solve"; /* _shouldn't_ happen! */
1004
1005 msg = "Solve operation failed"; /* game _should_ overwrite on error */
df11cd4e 1006 movestr = me->ourgame->solve(me->states[0].state,
1007 me->states[me->statepos-1].state,
1008 me->aux_info, &msg);
1009 if (!movestr)
2ac6d24e 1010 return msg;
df11cd4e 1011 s = me->ourgame->execute_move(me->states[me->statepos-1].state, movestr);
1012 assert(s);
2ac6d24e 1013
1014 /*
28d0118c 1015 * Now enter the solved state as the next move.
2ac6d24e 1016 */
1017 midend_stop_anim(me);
1018 while (me->nstates > me->statepos)
28d0118c 1019 me->ourgame->free_game(me->states[--me->nstates].state);
2ac6d24e 1020 ensure(me);
28d0118c 1021 me->states[me->nstates].state = s;
a4393230 1022 me->states[me->nstates].movestr = movestr;
1023 me->states[me->nstates].movetype = SOLVE;
2ac6d24e 1024 me->statepos = ++me->nstates;
07dfb697 1025 if (me->ui)
1026 me->ourgame->changed_state(me->ui,
1027 me->states[me->statepos-2].state,
1028 me->states[me->statepos-1].state);
2ac6d24e 1029 me->anim_time = 0.0;
1030 midend_finish_move(me);
1031 midend_redraw(me);
48dcdd62 1032 midend_set_timer(me);
2ac6d24e 1033 return NULL;
1034}
48dcdd62 1035
1036char *midend_rewrite_statusbar(midend_data *me, char *text)
1037{
1038 /*
1039 * An important special case is that we are occasionally called
1040 * with our own laststatus, to update the timer.
1041 */
1042 if (me->laststatus != text) {
1043 sfree(me->laststatus);
1044 me->laststatus = dupstr(text);
1045 }
1046
1047 if (me->ourgame->is_timed) {
1048 char timebuf[100], *ret;
1049 int min, sec;
1050
1051 sec = me->elapsed;
1052 min = sec / 60;
1053 sec %= 60;
1054 sprintf(timebuf, "[%d:%02d] ", min, sec);
1055
1056 ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
1057 strcpy(ret, timebuf);
1058 strcat(ret, text);
1059 return ret;
1060
1061 } else {
1062 return dupstr(text);
1063 }
1064}
a4393230 1065
1066#define SERIALISE_MAGIC "Simon Tatham's Portable Puzzle Collection"
1067#define SERIALISE_VERSION "1"
1068
1069void midend_serialise(midend_data *me,
1070 void (*write)(void *ctx, void *buf, int len),
1071 void *wctx)
1072{
1073 int i;
1074
1075 /*
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).
1083 */
1084#define wr(h,s) do { \
1085 char hbuf[80]; \
1086 char *str = (s); \
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); \
1091} while (0)
1092
1093 /*
1094 * Magic string identifying the file, and version number of the
1095 * file format.
1096 */
1097 wr("SAVEFILE", SERIALISE_MAGIC);
1098 wr("VERSION", SERIALISE_VERSION);
1099
1100 /*
1101 * The game name. (Copied locally to avoid const annoyance.)
1102 */
1103 {
1104 char *s = dupstr(me->ourgame->name);
1105 wr("GAME", s);
1106 sfree(s);
1107 }
1108
1109 /*
1110 * The current long-term parameters structure, in full.
1111 */
1112 if (me->params) {
1113 char *s = me->ourgame->encode_params(me->params, TRUE);
1114 wr("PARAMS", s);
1115 sfree(s);
1116 }
1117
1118 /*
1119 * The current short-term parameters structure, in full.
1120 */
1121 if (me->curparams) {
1122 char *s = me->ourgame->encode_params(me->curparams, TRUE);
1123 wr("CPARAMS", s);
1124 sfree(s);
1125 }
1126
1127 /*
1128 * The current game description, the privdesc, and the random seed.
1129 */
1130 if (me->seedstr)
1131 wr("SEED", me->seedstr);
1132 if (me->desc)
1133 wr("DESC", me->desc);
1134 if (me->privdesc)
1135 wr("PRIVDESC", me->privdesc);
1136
1137 /*
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!)
1142 */
1143 if (me->aux_info) {
1144 unsigned char *s1;
1145 char *s2;
1146 int len;
1147
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);
1153
1154 wr("AUXINFO", s2);
1155
1156 sfree(s2);
1157 sfree(s1);
1158 }
1159
1160 /*
1161 * Any required serialisation of the game_ui.
1162 */
1163 if (me->ui) {
1164 char *s = me->ourgame->encode_ui(me->ui);
1165 if (s) {
1166 wr("UI", s);
1167 sfree(s);
1168 }
1169 }
1170
1171 /*
1172 * The game time, if it's a timed game.
1173 */
1174 if (me->ourgame->is_timed) {
1175 char buf[80];
1176 sprintf(buf, "%g", me->elapsed);
1177 wr("TIME", buf);
1178 }
1179
1180 /*
1181 * The length of, and position in, the states list.
1182 */
1183 {
1184 char buf[80];
1185 sprintf(buf, "%d", me->nstates);
1186 wr("NSTATES", buf);
1187 sprintf(buf, "%d", me->statepos);
1188 wr("STATEPOS", buf);
1189 }
1190
1191 /*
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
1195 * previous one.
1196 */
1197 for (i = 1; i < me->nstates; i++) {
1198 assert(me->states[i].movetype != NEWGAME); /* only state 0 */
1199 switch (me->states[i].movetype) {
1200 case MOVE:
1201 wr("MOVE", me->states[i].movestr);
1202 break;
1203 case SOLVE:
1204 wr("SOLVE", me->states[i].movestr);
1205 break;
1206 case RESTART:
1207 wr("RESTART", me->states[i].movestr);
1208 break;
1209 }
1210 }
1211
1212#undef wr
1213}
1214
1215/*
1216 * This function returns NULL on success, or an error message.
1217 */
1218char *midend_deserialise(midend_data *me,
1219 int (*read)(void *ctx, void *buf, int len),
1220 void *rctx)
1221{
1222 int nstates = 0, statepos = -1, gotstates = 0;
1223 int started = FALSE;
1224 int i;
1225
1226 char *val = NULL;
1227 /* Initially all errors give the same report */
1228 char *ret = "Data does not appear to be a saved game file";
1229
1230 /*
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.
1235 */
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;
1240 game_ui *ui = NULL;
1241 struct midend_state_entry *states = NULL;
1242
1243 /*
1244 * Loop round and round reading one key/value pair at a time
1245 * from the serialised stream, until we have enough game states
1246 * to finish.
1247 */
1248 while (nstates <= 0 || statepos < 0 || gotstates < nstates-1) {
1249 char key[9], c;
1250 int len;
1251
1252 do {
1253 if (!read(rctx, key, 1)) {
1254 /* unexpected EOF */
1255 goto cleanup;
1256 }
1257 } while (key[0] == '\r' || key[0] == '\n');
1258
1259 if (!read(rctx, key+1, 8)) {
1260 /* unexpected EOF */
1261 goto cleanup;
1262 }
1263
1264 if (key[8] != ':') {
1265 if (started)
1266 ret = "Data was incorrectly formatted for a saved game file";
1267 }
1268 len = strcspn(key, ": ");
1269 assert(len <= 8);
1270 key[len] = '\0';
1271
1272 len = 0;
1273 while (1) {
1274 if (!read(rctx, &c, 1)) {
1275 /* unexpected EOF */
1276 goto cleanup;
1277 }
1278
1279 if (c == ':') {
1280 break;
1281 } else if (c >= '0' && c <= '9') {
1282 len = (len * 10) + (c - '0');
1283 } else {
1284 if (started)
1285 ret = "Data was incorrectly formatted for a"
1286 " saved game file";
1287 goto cleanup;
1288 }
1289 }
1290
1291 val = snewn(len+1, char);
1292 if (!read(rctx, val, len)) {
1293 if (started)
1294 goto cleanup;
1295 }
1296 val[len] = '\0';
1297
1298 if (!started) {
1299 if (strcmp(key, "SAVEFILE") || strcmp(val, SERIALISE_MAGIC)) {
1300 /* ret already has the right message in it */
1301 goto cleanup;
1302 }
1303 /* Now most errors are this one, unless otherwise specified */
1304 ret = "Saved data ended unexpectedly";
1305 started = TRUE;
1306 } else {
1307 if (!strcmp(key, "VERSION")) {
1308 if (strcmp(val, SERIALISE_VERSION)) {
1309 ret = "Cannot handle this version of the saved game"
1310 " file format";
1311 goto cleanup;
1312 }
1313 } else if (!strcmp(key, "GAME")) {
1314 if (strcmp(val, me->ourgame->name)) {
1315 ret = "Save file is from a different game";
1316 goto cleanup;
1317 }
1318 } else if (!strcmp(key, "PARAMS")) {
1319 sfree(parstr);
1320 parstr = val;
1321 val = NULL;
1322 } else if (!strcmp(key, "CPARAMS")) {
1323 sfree(cparstr);
1324 cparstr = val;
1325 val = NULL;
1326 } else if (!strcmp(key, "SEED")) {
1327 sfree(seed);
1328 seed = val;
1329 val = NULL;
1330 } else if (!strcmp(key, "DESC")) {
1331 sfree(desc);
1332 desc = val;
1333 val = NULL;
1334 } else if (!strcmp(key, "PRIVDESC")) {
1335 sfree(privdesc);
1336 privdesc = val;
1337 val = NULL;
1338 } else if (!strcmp(key, "AUXINFO")) {
1339 unsigned char *tmp;
1340 int len = strlen(val) / 2; /* length in bytes */
1341 tmp = hex2bin(val, len);
1342 obfuscate_bitmap(tmp, len*8, TRUE);
1343
1344 sfree(auxinfo);
1345 auxinfo = snewn(len + 1, char);
1346 memcpy(auxinfo, tmp, len);
1347 auxinfo[len] = '\0';
1348 sfree(tmp);
1349 } else if (!strcmp(key, "UI")) {
1350 sfree(uistr);
1351 uistr = val;
1352 val = NULL;
1353 } else if (!strcmp(key, "TIME")) {
1354 elapsed = strtod(val, NULL);
1355 } else if (!strcmp(key, "NSTATES")) {
1356 nstates = atoi(val);
1357 if (nstates <= 0) {
1358 ret = "Number of states in save file was negative";
1359 goto cleanup;
1360 }
1361 if (states) {
1362 ret = "Two state counts provided in save file";
1363 goto cleanup;
1364 }
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;
1370 }
1371 } else if (!strcmp(key, "STATEPOS")) {
1372 statepos = atoi(val);
1373 } else if (!strcmp(key, "MOVE")) {
1374 gotstates++;
1375 states[gotstates].movetype = MOVE;
1376 states[gotstates].movestr = val;
1377 val = NULL;
1378 } else if (!strcmp(key, "SOLVE")) {
1379 gotstates++;
1380 states[gotstates].movetype = SOLVE;
1381 states[gotstates].movestr = val;
1382 val = NULL;
1383 } else if (!strcmp(key, "RESTART")) {
1384 gotstates++;
1385 states[gotstates].movetype = RESTART;
1386 states[gotstates].movestr = val;
1387 val = NULL;
1388 }
1389 }
1390
1391 sfree(val);
1392 val = NULL;
1393 }
1394
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";
1399 goto cleanup;
1400 }
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";
1405 goto cleanup;
1406 }
1407 if (!desc) {
1408 ret = "Game description in save file is missing";
1409 goto cleanup;
1410 } else if (me->ourgame->validate_desc(params, desc)) {
1411 ret = "Game description in save file is invalid";
1412 goto cleanup;
1413 }
1414 if (privdesc && me->ourgame->validate_desc(params, privdesc)) {
1415 ret = "Game private description in save file is invalid";
1416 goto cleanup;
1417 }
1418 if (statepos < 0 || statepos >= nstates) {
1419 ret = "Game position in save file is out of range";
1420 }
1421
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) {
1427 case MOVE:
1428 case SOLVE:
1429 states[i].state = me->ourgame->execute_move(states[i-1].state,
1430 states[i].movestr);
1431 if (states[i].state == NULL) {
1432 ret = "Save file contained an invalid move";
1433 goto cleanup;
1434 }
1435 break;
1436 case RESTART:
1437 if (me->ourgame->validate_desc(params, states[i].movestr)) {
1438 ret = "Save file contained an invalid restart move";
1439 goto cleanup;
1440 }
1441 states[i].state = me->ourgame->new_game(me, params,
1442 states[i].movestr);
1443 break;
1444 }
1445 }
1446
1447 ui = me->ourgame->new_ui(states[0].state);
1448 me->ourgame->decode_ui(ui, uistr);
1449
1450 /*
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
1455 * stuff.
1456 */
1457 {
1458 char *tmp;
1459
1460 tmp = me->desc;
1461 me->desc = desc;
1462 desc = tmp;
1463
1464 tmp = me->privdesc;
1465 me->privdesc = privdesc;
1466 privdesc = tmp;
1467
1468 tmp = me->seedstr;
1469 me->seedstr = seed;
1470 seed = tmp;
1471
1472 tmp = me->aux_info;
1473 me->aux_info = auxinfo;
1474 auxinfo = tmp;
1475 }
1476
1477 me->genmode = GOT_NOTHING;
1478
1479 me->statesize = nstates;
1480 nstates = me->nstates;
1481 me->nstates = me->statesize;
1482 {
1483 struct midend_state_entry *tmp;
1484 tmp = me->states;
1485 me->states = states;
1486 states = tmp;
1487 }
1488 me->statepos = statepos;
1489
1490 {
1491 game_params *tmp;
1492
1493 tmp = me->params;
1494 me->params = params;
1495 params = tmp;
1496
1497 tmp = me->curparams;
1498 me->curparams = cparams;
1499 cparams = tmp;
1500 }
1501
1502 me->oldstate = NULL;
1503 me->anim_time = me->anim_pos = me->flash_time = me->flash_pos = 0.0F;
1504 me->dir = 0;
1505
1506 {
1507 game_ui *tmp;
1508
1509 tmp = me->ui;
1510 me->ui = ui;
1511 ui = tmp;
1512 }
1513
1514 me->elapsed = elapsed;
1515 me->pressed_mouse_button = 0;
1516
1517 midend_set_timer(me);
1518
1519 ret = NULL; /* success! */
1520
1521 cleanup:
1522 sfree(val);
1523 sfree(seed);
1524 sfree(parstr);
1525 sfree(cparstr);
1526 sfree(desc);
1527 sfree(privdesc);
1528 sfree(auxinfo);
1529 sfree(uistr);
1530 if (params)
1531 me->ourgame->free_params(params);
1532 if (cparams)
1533 me->ourgame->free_params(cparams);
1534 if (ui)
1535 me->ourgame->free_ui(ui);
1536 if (states) {
1537 int i;
1538
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);
1543 }
1544 sfree(states);
1545 }
1546
1547 return ret;
1548}