Prevent drags from being started outside the playing area, which was
[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);
871bf294 152 sfree(me->privdesc);
1185e3c5 153 sfree(me->seedstr);
c566778e 154 sfree(me->aux_info);
be8d5aa1 155 me->ourgame->free_params(me->params);
ab53eb64 156 if (me->npresets) {
157 for (i = 0; i < me->npresets; i++) {
158 sfree(me->presets[i]);
159 sfree(me->preset_names[i]);
160 }
161 sfree(me->presets);
162 sfree(me->preset_names);
163 }
164 if (me->ui)
165 me->ourgame->free_ui(me->ui);
f60f7e7c 166 if (me->curparams)
167 me->ourgame->free_params(me->curparams);
48dcdd62 168 sfree(me->laststatus);
7f77ea24 169 sfree(me);
170}
171
1e3e152d 172void midend_size(midend_data *me, int *x, int *y, int expand)
7f77ea24 173{
1e3e152d 174 me->ourgame->size(me->params, me->drawstate, x, y, expand);
175 me->winwidth = *x;
176 me->winheight = *y;
7f77ea24 177}
178
179void midend_set_params(midend_data *me, game_params *params)
180{
be8d5aa1 181 me->ourgame->free_params(me->params);
182 me->params = me->ourgame->dup_params(params);
7f77ea24 183}
184
48dcdd62 185static void midend_set_timer(midend_data *me)
186{
187 me->timing = (me->ourgame->is_timed &&
188 me->ourgame->timing_state(me->states[me->statepos-1].state));
189 if (me->timing || me->flash_time || me->anim_time)
190 activate_timer(me->frontend);
191 else
192 deactivate_timer(me->frontend);
193}
194
1e3e152d 195static void midend_size_new_drawstate(midend_data *me)
196{
197 me->ourgame->size(me->params, me->drawstate, &me->winwidth, &me->winheight,
198 TRUE);
199}
200
008b4378 201void midend_force_redraw(midend_data *me)
202{
203 if (me->drawstate)
204 me->ourgame->free_drawstate(me->drawstate);
205 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
1e3e152d 206 midend_size_new_drawstate(me);
008b4378 207 midend_redraw(me);
208}
209
5928817c 210void midend_new_game(midend_data *me)
7f77ea24 211{
ab53eb64 212 midend_free_game(me);
2ef96bd6 213
7f77ea24 214 assert(me->nstates == 0);
215
1185e3c5 216 if (me->genmode == GOT_DESC) {
217 me->genmode = GOT_NOTHING;
218 } else {
219 random_state *rs;
220
221 if (me->genmode == GOT_SEED) {
222 me->genmode = GOT_NOTHING;
223 } else {
224 /*
225 * Generate a new random seed. 15 digits comes to about
226 * 48 bits, which should be more than enough.
97c44af3 227 *
228 * I'll avoid putting a leading zero on the number,
229 * just in case it confuses anybody who thinks it's
230 * processed as an integer rather than a string.
1185e3c5 231 */
232 char newseed[16];
233 int i;
234 newseed[15] = '\0';
97c44af3 235 newseed[0] = '1' + random_upto(me->random, 9);
236 for (i = 1; i < 15; i++)
1185e3c5 237 newseed[i] = '0' + random_upto(me->random, 10);
238 sfree(me->seedstr);
239 me->seedstr = dupstr(newseed);
f60f7e7c 240
241 if (me->curparams)
242 me->ourgame->free_params(me->curparams);
243 me->curparams = me->ourgame->dup_params(me->params);
1185e3c5 244 }
245
246 sfree(me->desc);
0a6892db 247 sfree(me->privdesc);
c566778e 248 sfree(me->aux_info);
6f2d8d7c 249 me->aux_info = NULL;
1185e3c5 250
251 rs = random_init(me->seedstr, strlen(me->seedstr));
6aa6af4c 252 me->desc = me->ourgame->new_desc(me->curparams, rs,
253 &me->aux_info, TRUE);
0a6892db 254 me->privdesc = NULL;
1185e3c5 255 random_free(rs);
1185e3c5 256 }
7f77ea24 257
258 ensure(me);
c380832d 259 me->states[me->nstates].state =
260 me->ourgame->new_game(me, me->params, me->desc);
a4393230 261 me->states[me->nstates].movestr = NULL;
262 me->states[me->nstates].movetype = NEWGAME;
28d0118c 263 me->nstates++;
7f77ea24 264 me->statepos = 1;
28d0118c 265 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
1e3e152d 266 midend_size_new_drawstate(me);
48dcdd62 267 me->elapsed = 0.0F;
268 midend_set_timer(me);
74a4e547 269 if (me->ui)
be8d5aa1 270 me->ourgame->free_ui(me->ui);
28d0118c 271 me->ui = me->ourgame->new_ui(me->states[0].state);
6776a950 272 me->pressed_mouse_button = 0;
7f77ea24 273}
274
1482ee76 275static int midend_undo(midend_data *me)
7f77ea24 276{
1482ee76 277 if (me->statepos > 1) {
07dfb697 278 if (me->ui)
279 me->ourgame->changed_state(me->ui,
280 me->states[me->statepos-1].state,
281 me->states[me->statepos-2].state);
7f77ea24 282 me->statepos--;
c822de4a 283 me->dir = -1;
1482ee76 284 return 1;
285 } else
286 return 0;
7f77ea24 287}
288
1482ee76 289static int midend_redo(midend_data *me)
7f77ea24 290{
1482ee76 291 if (me->statepos < me->nstates) {
07dfb697 292 if (me->ui)
293 me->ourgame->changed_state(me->ui,
294 me->states[me->statepos-1].state,
295 me->states[me->statepos].state);
7f77ea24 296 me->statepos++;
c822de4a 297 me->dir = +1;
1482ee76 298 return 1;
299 } else
300 return 0;
7f77ea24 301}
302
87ed82be 303static void midend_finish_move(midend_data *me)
304{
305 float flashtime;
306
28d0118c 307 /*
308 * We do not flash if the later of the two states is special.
309 * This covers both forward Solve moves and backward (undone)
310 * Restart moves.
311 */
312 if ((me->oldstate || me->statepos > 1) &&
a4393230 313 ((me->dir > 0 && !special(me->states[me->statepos-1].movetype)) ||
28d0118c 314 (me->dir < 0 && me->statepos < me->nstates &&
a4393230 315 !special(me->states[me->statepos].movetype)))) {
be8d5aa1 316 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
28d0118c 317 me->states[me->statepos-2].state,
318 me->states[me->statepos-1].state,
e3f21163 319 me->oldstate ? me->dir : +1,
320 me->ui);
87ed82be 321 if (flashtime > 0) {
322 me->flash_pos = 0.0F;
323 me->flash_time = flashtime;
324 }
325 }
326
327 if (me->oldstate)
be8d5aa1 328 me->ourgame->free_game(me->oldstate);
87ed82be 329 me->oldstate = NULL;
330 me->anim_pos = me->anim_time = 0;
c822de4a 331 me->dir = 0;
87ed82be 332
48dcdd62 333 midend_set_timer(me);
87ed82be 334}
335
ab53eb64 336void midend_stop_anim(midend_data *me)
7f77ea24 337{
2ef96bd6 338 if (me->oldstate || me->anim_time) {
87ed82be 339 midend_finish_move(me);
2ef96bd6 340 midend_redraw(me);
341 }
dd216087 342}
343
28d0118c 344void midend_restart_game(midend_data *me)
345{
346 game_state *s;
347
7f89707c 348 midend_stop_anim(me);
349
28d0118c 350 assert(me->statepos >= 1);
351 if (me->statepos == 1)
352 return; /* no point doing anything at all! */
353
0a6892db 354 /*
355 * During restart, we reconstruct the game from the (public)
356 * game description rather than from states[0], because that
357 * way Mines gets slightly more sensible behaviour (restart
358 * goes to _after_ the first click so you don't have to
359 * remember where you clicked).
360 */
361 s = me->ourgame->new_game(me, me->params, me->desc);
28d0118c 362
363 /*
364 * Now enter the restarted state as the next move.
365 */
366 midend_stop_anim(me);
367 while (me->nstates > me->statepos)
368 me->ourgame->free_game(me->states[--me->nstates].state);
369 ensure(me);
370 me->states[me->nstates].state = s;
a4393230 371 me->states[me->nstates].movestr = dupstr(me->desc);
372 me->states[me->nstates].movetype = RESTART;
28d0118c 373 me->statepos = ++me->nstates;
07dfb697 374 if (me->ui)
375 me->ourgame->changed_state(me->ui,
376 me->states[me->statepos-2].state,
377 me->states[me->statepos-1].state);
28d0118c 378 me->anim_time = 0.0;
379 midend_finish_move(me);
380 midend_redraw(me);
48dcdd62 381 midend_set_timer(me);
28d0118c 382}
383
6776a950 384static int midend_really_process_key(midend_data *me, int x, int y, int button)
dd216087 385{
28d0118c 386 game_state *oldstate =
387 me->ourgame->dup_game(me->states[me->statepos - 1].state);
ab53eb64 388 int special = FALSE, gotspecial = FALSE, ret = 1;
dd216087 389 float anim_time;
7f77ea24 390
391 if (button == 'n' || button == 'N' || button == '\x0E') {
dd216087 392 midend_stop_anim(me);
5928817c 393 midend_new_game(me);
2ef96bd6 394 midend_redraw(me);
ab53eb64 395 goto done; /* never animate */
7f77ea24 396 } else if (button == 'u' || button == 'u' ||
1482ee76 397 button == '\x1A' || button == '\x1F') {
dd216087 398 midend_stop_anim(me);
a4393230 399 special = special(me->states[me->statepos-1].movetype);
28d0118c 400 gotspecial = TRUE;
1482ee76 401 if (!midend_undo(me))
ab53eb64 402 goto done;
7f89707c 403 } else if (button == 'r' || button == 'R' ||
31ad8194 404 button == '\x12' || button == '\x19') {
dd216087 405 midend_stop_anim(me);
1482ee76 406 if (!midend_redo(me))
ab53eb64 407 goto done;
7f77ea24 408 } else if (button == 'q' || button == 'Q' || button == '\x11') {
ab53eb64 409 ret = 0;
410 goto done;
2ef96bd6 411 } else {
df11cd4e 412 game_state *s;
413 char *movestr;
414
415 movestr =
416 me->ourgame->interpret_move(me->states[me->statepos-1].state,
417 me->ui, me->drawstate, x, y, button);
418 if (!movestr)
419 s = NULL;
420 else if (!*movestr)
421 s = me->states[me->statepos-1].state;
422 else {
423 s = me->ourgame->execute_move(me->states[me->statepos-1].state,
424 movestr);
425 assert(s != NULL);
df11cd4e 426 }
74a4e547 427
28d0118c 428 if (s == me->states[me->statepos-1].state) {
74a4e547 429 /*
430 * make_move() is allowed to return its input state to
431 * indicate that although no move has been made, the UI
432 * state has been updated and a redraw is called for.
433 */
434 midend_redraw(me);
ab53eb64 435 goto done;
74a4e547 436 } else if (s) {
dd216087 437 midend_stop_anim(me);
2ef96bd6 438 while (me->nstates > me->statepos)
28d0118c 439 me->ourgame->free_game(me->states[--me->nstates].state);
2ef96bd6 440 ensure(me);
a4393230 441 assert(movestr != NULL);
28d0118c 442 me->states[me->nstates].state = s;
a4393230 443 me->states[me->nstates].movestr = movestr;
444 me->states[me->nstates].movetype = MOVE;
2ef96bd6 445 me->statepos = ++me->nstates;
c822de4a 446 me->dir = +1;
2ef96bd6 447 } else {
a4393230 448 goto done;
2ef96bd6 449 }
7f77ea24 450 }
451
28d0118c 452 if (!gotspecial)
a4393230 453 special = special(me->states[me->statepos-1].movetype);
28d0118c 454
2ef96bd6 455 /*
456 * See if this move requires an animation.
457 */
28d0118c 458 if (special) {
459 anim_time = 0;
460 } else {
461 anim_time = me->ourgame->anim_length(oldstate,
462 me->states[me->statepos-1].state,
e3f21163 463 me->dir, me->ui);
28d0118c 464 }
2ef96bd6 465
ab53eb64 466 me->oldstate = oldstate; oldstate = NULL;
2ef96bd6 467 if (anim_time > 0) {
2ef96bd6 468 me->anim_time = anim_time;
469 } else {
2ef96bd6 470 me->anim_time = 0.0;
87ed82be 471 midend_finish_move(me);
7f77ea24 472 }
2ef96bd6 473 me->anim_pos = 0.0;
474
475 midend_redraw(me);
476
48dcdd62 477 midend_set_timer(me);
7f77ea24 478
ab53eb64 479 done:
480 if (oldstate) me->ourgame->free_game(oldstate);
481 return ret;
7f77ea24 482}
2ef96bd6 483
6776a950 484int midend_process_key(midend_data *me, int x, int y, int button)
485{
486 int ret = 1;
487
488 /*
489 * Harmonise mouse drag and release messages.
490 *
491 * Some front ends might accidentally switch from sending, say,
492 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
493 * drag. (This can happen on the Mac, for example, since
494 * RIGHT_DRAG is usually done using Command+drag, and if the
495 * user accidentally releases Command half way through the drag
496 * then there will be trouble.)
497 *
498 * It would be an O(number of front ends) annoyance to fix this
499 * in the front ends, but an O(number of back ends) annoyance
500 * to have each game capable of dealing with it. Therefore, we
501 * fix it _here_ in the common midend code so that it only has
502 * to be done once.
503 *
eeba2afe 504 * The possible ways in which things can go screwy in the front
505 * end are:
6776a950 506 *
eeba2afe 507 * - in a system containing multiple physical buttons button
508 * presses can inadvertently overlap. We can see ABab (caps
509 * meaning button-down and lowercase meaning button-up) when
510 * the user had semantically intended AaBb.
6776a950 511 *
eeba2afe 512 * - in a system where one button is simulated by means of a
513 * modifier key and another button, buttons can mutate
514 * between press and release (possibly during drag). So we
515 * can see Ab instead of Aa.
6776a950 516 *
eeba2afe 517 * Definite requirements are:
518 *
519 * - button _presses_ must never be invented or destroyed. If
520 * the user presses two buttons in succession, the button
521 * presses must be transferred to the backend unchanged. So
522 * if we see AaBb , that's fine; if we see ABab (the button
523 * presses inadvertently overlapped) we must somehow
524 * `correct' it to AaBb.
525 *
526 * - every mouse action must end up looking like a press, zero
527 * or more drags, then a release. This allows back ends to
528 * make the _assumption_ that incoming mouse data will be
529 * sane in this regard, and not worry about the details.
530 *
531 * So my policy will be:
532 *
533 * - treat any button-up as a button-up for the currently
534 * pressed button, or ignore it if there is no currently
535 * pressed button.
536 *
537 * - treat any drag as a drag for the currently pressed
538 * button, or ignore it if there is no currently pressed
539 * button.
540 *
541 * - if we see a button-down while another button is currently
542 * pressed, invent a button-up for the first one and then
543 * pass the button-down through as before.
6776a950 544 *
93b1da3d 545 * 2005-05-31: An addendum to the above. Some games might want
546 * a `priority order' among buttons, such that if one button is
547 * pressed while another is down then a fixed one of the
548 * buttons takes priority no matter what order they're pressed
549 * in. Mines, in particular, wants to treat a left+right click
550 * like a left click for the benefit of users of other
551 * implementations. So the last of the above points is modified
552 * in the presence of an (optional) button priority order.
6776a950 553 */
554 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
eeba2afe 555 if (me->pressed_mouse_button) {
556 if (IS_MOUSE_DRAG(button)) {
557 button = me->pressed_mouse_button +
558 (LEFT_DRAG - LEFT_BUTTON);
559 } else {
560 button = me->pressed_mouse_button +
561 (LEFT_RELEASE - LEFT_BUTTON);
6776a950 562 }
eeba2afe 563 } else
564 return ret; /* ignore it */
6776a950 565 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
93b1da3d 566 /*
567 * If the new button has lower priority than the old one,
568 * don't bother doing this.
569 */
570 if (me->ourgame->mouse_priorities &
571 BUTTON_BEATS(me->pressed_mouse_button, button))
572 return ret; /* just ignore it */
573
6776a950 574 /*
575 * Fabricate a button-up for the previously pressed button.
576 */
577 ret = ret && midend_really_process_key
578 (me, x, y, (me->pressed_mouse_button +
579 (LEFT_RELEASE - LEFT_BUTTON)));
580 }
581
582 /*
583 * Now send on the event we originally received.
584 */
585 ret = ret && midend_really_process_key(me, x, y, button);
586
587 /*
588 * And update the currently pressed button.
589 */
590 if (IS_MOUSE_RELEASE(button))
591 me->pressed_mouse_button = 0;
592 else if (IS_MOUSE_DOWN(button))
593 me->pressed_mouse_button = button;
594
595 return ret;
596}
597
2ef96bd6 598void midend_redraw(midend_data *me)
599{
600 if (me->statepos > 0 && me->drawstate) {
601 start_draw(me->frontend);
602 if (me->oldstate && me->anim_time > 0 &&
603 me->anim_pos < me->anim_time) {
c822de4a 604 assert(me->dir != 0);
be8d5aa1 605 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
28d0118c 606 me->states[me->statepos-1].state, me->dir,
be8d5aa1 607 me->ui, me->anim_pos, me->flash_pos);
2ef96bd6 608 } else {
be8d5aa1 609 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
28d0118c 610 me->states[me->statepos-1].state, +1 /*shrug*/,
be8d5aa1 611 me->ui, 0.0, me->flash_pos);
2ef96bd6 612 }
613 end_draw(me->frontend);
614 }
615}
616
617void midend_timer(midend_data *me, float tplus)
618{
619 me->anim_pos += tplus;
620 if (me->anim_pos >= me->anim_time ||
621 me->anim_time == 0 || !me->oldstate) {
87ed82be 622 if (me->anim_time > 0)
623 midend_finish_move(me);
624 }
48dcdd62 625
87ed82be 626 me->flash_pos += tplus;
627 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
628 me->flash_pos = me->flash_time = 0;
2ef96bd6 629 }
48dcdd62 630
2ef96bd6 631 midend_redraw(me);
48dcdd62 632
633 if (me->timing) {
634 float oldelapsed = me->elapsed;
635 me->elapsed += tplus;
636 if ((int)oldelapsed != (int)me->elapsed)
637 status_bar(me->frontend, me->laststatus ? me->laststatus : "");
638 }
639
640 midend_set_timer(me);
2ef96bd6 641}
642
643float *midend_colours(midend_data *me, int *ncolours)
644{
645 game_state *state = NULL;
646 float *ret;
647
648 if (me->nstates == 0) {
c566778e 649 char *aux = NULL;
6aa6af4c 650 char *desc = me->ourgame->new_desc(me->params, me->random,
651 &aux, TRUE);
c380832d 652 state = me->ourgame->new_game(me, me->params, desc);
1185e3c5 653 sfree(desc);
c566778e 654 sfree(aux);
2ef96bd6 655 } else
28d0118c 656 state = me->states[0].state;
2ef96bd6 657
be8d5aa1 658 ret = me->ourgame->colours(me->frontend, state, ncolours);
2ef96bd6 659
813593cc 660 {
661 int i;
662
663 /*
664 * Allow environment-based overrides for the standard
665 * colours by defining variables along the lines of
666 * `NET_COLOUR_4=6000c0'.
667 */
668
669 for (i = 0; i < *ncolours; i++) {
670 char buf[80], *e;
671 unsigned int r, g, b;
672 int j;
673
674 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
675 for (j = 0; buf[j]; j++)
676 buf[j] = toupper((unsigned char)buf[j]);
677 if ((e = getenv(buf)) != NULL &&
678 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
679 ret[i*3 + 0] = r / 255.0;
680 ret[i*3 + 1] = g / 255.0;
681 ret[i*3 + 2] = b / 255.0;
682 }
683 }
684 }
685
2ef96bd6 686 if (me->nstates == 0)
be8d5aa1 687 me->ourgame->free_game(state);
2ef96bd6 688
689 return ret;
690}
eb2ad6f1 691
692int midend_num_presets(midend_data *me)
693{
694 if (!me->npresets) {
695 char *name;
696 game_params *preset;
697
be8d5aa1 698 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
eb2ad6f1 699 if (me->presetsize <= me->npresets) {
700 me->presetsize = me->npresets + 10;
701 me->presets = sresize(me->presets, me->presetsize,
702 game_params *);
703 me->preset_names = sresize(me->preset_names, me->presetsize,
704 char *);
705 }
706
707 me->presets[me->npresets] = preset;
708 me->preset_names[me->npresets] = name;
709 me->npresets++;
710 }
711 }
712
813593cc 713 {
714 /*
715 * Allow environment-based extensions to the preset list by
716 * defining a variable along the lines of `SOLO_PRESETS=2x3
717 * Advanced:2x3da'. Colon-separated list of items,
718 * alternating between textual titles in the menu and
719 * encoded parameter strings.
720 */
721 char buf[80], *e, *p;
722 int j;
723
724 sprintf(buf, "%s_PRESETS", me->ourgame->name);
725 for (j = 0; buf[j]; j++)
726 buf[j] = toupper((unsigned char)buf[j]);
727
728 if ((e = getenv(buf)) != NULL) {
729 p = e = dupstr(e);
730
731 while (*p) {
732 char *name, *val;
733 game_params *preset;
734
735 name = p;
736 while (*p && *p != ':') p++;
737 if (*p) *p++ = '\0';
738 val = p;
739 while (*p && *p != ':') p++;
740 if (*p) *p++ = '\0';
741
742 preset = me->ourgame->default_params();
743 me->ourgame->decode_params(preset, val);
744
239ba6e6 745 if (me->ourgame->validate_params(preset)) {
746 /* Drop this one from the list. */
747 me->ourgame->free_params(preset);
748 continue;
749 }
750
813593cc 751 if (me->presetsize <= me->npresets) {
752 me->presetsize = me->npresets + 10;
753 me->presets = sresize(me->presets, me->presetsize,
754 game_params *);
755 me->preset_names = sresize(me->preset_names,
756 me->presetsize, char *);
757 }
758
759 me->presets[me->npresets] = preset;
cb64d2dd 760 me->preset_names[me->npresets] = dupstr(name);
813593cc 761 me->npresets++;
762 }
763 }
764 }
765
eb2ad6f1 766 return me->npresets;
767}
768
769void midend_fetch_preset(midend_data *me, int n,
770 char **name, game_params **params)
771{
772 assert(n >= 0 && n < me->npresets);
773 *name = me->preset_names[n];
774 *params = me->presets[n];
775}
fd1a1a2b 776
777int midend_wants_statusbar(midend_data *me)
778{
be8d5aa1 779 return me->ourgame->wants_statusbar();
fd1a1a2b 780}
c8230524 781
0a6892db 782void midend_supersede_game_desc(midend_data *me, char *desc, char *privdesc)
c380832d 783{
784 sfree(me->desc);
0a6892db 785 sfree(me->privdesc);
c380832d 786 me->desc = dupstr(desc);
0a6892db 787 me->privdesc = privdesc ? dupstr(privdesc) : NULL;
c380832d 788}
789
5928817c 790config_item *midend_get_config(midend_data *me, int which, char **wintitle)
c8230524 791{
ab53eb64 792 char *titlebuf, *parstr, *rest;
5928817c 793 config_item *ret;
ab53eb64 794 char sep;
5928817c 795
ab53eb64 796 assert(wintitle);
be8d5aa1 797 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
5928817c 798
799 switch (which) {
800 case CFG_SETTINGS:
be8d5aa1 801 sprintf(titlebuf, "%s configuration", me->ourgame->name);
ab53eb64 802 *wintitle = titlebuf;
be8d5aa1 803 return me->ourgame->configure(me->params);
5928817c 804 case CFG_SEED:
1185e3c5 805 case CFG_DESC:
ab53eb64 806 if (!me->curparams) {
807 sfree(titlebuf);
808 return NULL;
809 }
810 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
1185e3c5 811 which == CFG_SEED ? "random" : "game");
ab53eb64 812 *wintitle = titlebuf;
5928817c 813
814 ret = snewn(2, config_item);
815
816 ret[0].type = C_STRING;
1185e3c5 817 if (which == CFG_SEED)
818 ret[0].name = "Game random seed";
819 else
820 ret[0].name = "Game ID";
5928817c 821 ret[0].ival = 0;
b0e26073 822 /*
1185e3c5 823 * For CFG_DESC the text going in here will be a string
824 * encoding of the restricted parameters, plus a colon,
825 * plus the game description. For CFG_SEED it will be the
826 * full parameters, plus a hash, plus the random seed data.
827 * Either of these is a valid full game ID (although only
828 * the former is likely to persist across many code
829 * changes).
b0e26073 830 */
f60f7e7c 831 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
ab53eb64 832 assert(parstr);
1185e3c5 833 if (which == CFG_DESC) {
ab53eb64 834 rest = me->desc ? me->desc : "";
835 sep = ':';
1185e3c5 836 } else {
ab53eb64 837 rest = me->seedstr ? me->seedstr : "";
838 sep = '#';
1185e3c5 839 }
ab53eb64 840 ret[0].sval = snewn(strlen(parstr) + strlen(rest) + 2, char);
841 sprintf(ret[0].sval, "%s%c%s", parstr, sep, rest);
b0e26073 842 sfree(parstr);
5928817c 843
844 ret[1].type = C_END;
845 ret[1].name = ret[1].sval = NULL;
846 ret[1].ival = 0;
847
848 return ret;
849 }
850
851 assert(!"We shouldn't be here");
852 return NULL;
c8230524 853}
854
1185e3c5 855static char *midend_game_id_int(midend_data *me, char *id, int defmode)
8b7938e7 856{
1185e3c5 857 char *error, *par, *desc, *seed;
8b7938e7 858
1185e3c5 859 seed = strchr(id, '#');
860 desc = strchr(id, ':');
8b7938e7 861
1185e3c5 862 if (desc && (!seed || desc < seed)) {
863 /*
864 * We have a colon separating parameters from game
865 * description. So `par' now points to the parameters
866 * string, and `desc' to the description string.
867 */
868 *desc++ = '\0';
869 par = id;
870 seed = NULL;
871 } else if (seed && (!desc || seed < desc)) {
8b7938e7 872 /*
50ff5730 873 * We have a hash separating parameters from random seed.
1185e3c5 874 * So `par' now points to the parameters string, and `seed'
875 * to the seed string.
8b7938e7 876 */
877 *seed++ = '\0';
878 par = id;
1185e3c5 879 desc = NULL;
8b7938e7 880 } else {
881 /*
1185e3c5 882 * We only have one string. Depending on `defmode', we take
883 * it to be either parameters, seed or description.
8b7938e7 884 */
1185e3c5 885 if (defmode == DEF_SEED) {
8b7938e7 886 seed = id;
1185e3c5 887 par = desc = NULL;
888 } else if (defmode == DEF_DESC) {
889 desc = id;
890 par = seed = NULL;
8b7938e7 891 } else {
8b7938e7 892 par = id;
1185e3c5 893 seed = desc = NULL;
8b7938e7 894 }
895 }
896
897 if (par) {
1185e3c5 898 game_params *tmpparams;
899 tmpparams = me->ourgame->dup_params(me->params);
900 me->ourgame->decode_params(tmpparams, par);
901 error = me->ourgame->validate_params(tmpparams);
8b7938e7 902 if (error) {
1185e3c5 903 me->ourgame->free_params(tmpparams);
8b7938e7 904 return error;
905 }
f60f7e7c 906 if (me->curparams)
907 me->ourgame->free_params(me->curparams);
908 me->curparams = tmpparams;
1185e3c5 909
910 /*
911 * Now filter only the persistent parts of this state into
912 * the long-term params structure, unless we've _only_
913 * received a params string in which case the whole lot is
914 * persistent.
915 */
916 if (seed || desc) {
917 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
918 me->ourgame->decode_params(me->params, tmpstr);
3af70dd4 919 sfree(tmpstr);
1185e3c5 920 } else {
921 me->ourgame->free_params(me->params);
922 me->params = me->ourgame->dup_params(tmpparams);
923 }
8b7938e7 924 }
925
3af70dd4 926 sfree(me->desc);
0a6892db 927 sfree(me->privdesc);
928 me->desc = me->privdesc = NULL;
3af70dd4 929 sfree(me->seedstr);
930 me->seedstr = NULL;
931
1185e3c5 932 if (desc) {
933 error = me->ourgame->validate_desc(me->params, desc);
8b7938e7 934 if (error)
935 return error;
936
1185e3c5 937 me->desc = dupstr(desc);
938 me->genmode = GOT_DESC;
c566778e 939 sfree(me->aux_info);
6f2d8d7c 940 me->aux_info = NULL;
8b7938e7 941 }
942
1185e3c5 943 if (seed) {
1185e3c5 944 me->seedstr = dupstr(seed);
945 me->genmode = GOT_SEED;
946 }
947
8b7938e7 948 return NULL;
949}
950
1185e3c5 951char *midend_game_id(midend_data *me, char *id)
952{
953 return midend_game_id_int(me, id, DEF_PARAMS);
954}
955
5928817c 956char *midend_set_config(midend_data *me, int which, config_item *cfg)
c8230524 957{
8b7938e7 958 char *error;
c8230524 959 game_params *params;
960
5928817c 961 switch (which) {
962 case CFG_SETTINGS:
be8d5aa1 963 params = me->ourgame->custom_params(cfg);
964 error = me->ourgame->validate_params(params);
c8230524 965
5928817c 966 if (error) {
be8d5aa1 967 me->ourgame->free_params(params);
5928817c 968 return error;
969 }
c8230524 970
be8d5aa1 971 me->ourgame->free_params(me->params);
5928817c 972 me->params = params;
973 break;
974
975 case CFG_SEED:
1185e3c5 976 case CFG_DESC:
977 error = midend_game_id_int(me, cfg[0].sval,
978 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
5928817c 979 if (error)
980 return error;
5928817c 981 break;
982 }
c8230524 983
984 return NULL;
985}
9b4b03d3 986
987char *midend_text_format(midend_data *me)
988{
989 if (me->ourgame->can_format_as_text && me->statepos > 0)
28d0118c 990 return me->ourgame->text_format(me->states[me->statepos-1].state);
9b4b03d3 991 else
992 return NULL;
993}
2ac6d24e 994
995char *midend_solve(midend_data *me)
996{
997 game_state *s;
df11cd4e 998 char *msg, *movestr;
2ac6d24e 999
1000 if (!me->ourgame->can_solve)
1001 return "This game does not support the Solve operation";
1002
1003 if (me->statepos < 1)
1004 return "No game set up to solve"; /* _shouldn't_ happen! */
1005
1006 msg = "Solve operation failed"; /* game _should_ overwrite on error */
df11cd4e 1007 movestr = me->ourgame->solve(me->states[0].state,
1008 me->states[me->statepos-1].state,
1009 me->aux_info, &msg);
1010 if (!movestr)
2ac6d24e 1011 return msg;
df11cd4e 1012 s = me->ourgame->execute_move(me->states[me->statepos-1].state, movestr);
1013 assert(s);
2ac6d24e 1014
1015 /*
28d0118c 1016 * Now enter the solved state as the next move.
2ac6d24e 1017 */
1018 midend_stop_anim(me);
1019 while (me->nstates > me->statepos)
28d0118c 1020 me->ourgame->free_game(me->states[--me->nstates].state);
2ac6d24e 1021 ensure(me);
28d0118c 1022 me->states[me->nstates].state = s;
a4393230 1023 me->states[me->nstates].movestr = movestr;
1024 me->states[me->nstates].movetype = SOLVE;
2ac6d24e 1025 me->statepos = ++me->nstates;
07dfb697 1026 if (me->ui)
1027 me->ourgame->changed_state(me->ui,
1028 me->states[me->statepos-2].state,
1029 me->states[me->statepos-1].state);
2ac6d24e 1030 me->anim_time = 0.0;
1031 midend_finish_move(me);
1032 midend_redraw(me);
48dcdd62 1033 midend_set_timer(me);
2ac6d24e 1034 return NULL;
1035}
48dcdd62 1036
1037char *midend_rewrite_statusbar(midend_data *me, char *text)
1038{
1039 /*
1040 * An important special case is that we are occasionally called
1041 * with our own laststatus, to update the timer.
1042 */
1043 if (me->laststatus != text) {
1044 sfree(me->laststatus);
1045 me->laststatus = dupstr(text);
1046 }
1047
1048 if (me->ourgame->is_timed) {
1049 char timebuf[100], *ret;
1050 int min, sec;
1051
1052 sec = me->elapsed;
1053 min = sec / 60;
1054 sec %= 60;
1055 sprintf(timebuf, "[%d:%02d] ", min, sec);
1056
1057 ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
1058 strcpy(ret, timebuf);
1059 strcat(ret, text);
1060 return ret;
1061
1062 } else {
1063 return dupstr(text);
1064 }
1065}
a4393230 1066
1067#define SERIALISE_MAGIC "Simon Tatham's Portable Puzzle Collection"
1068#define SERIALISE_VERSION "1"
1069
1070void midend_serialise(midend_data *me,
1071 void (*write)(void *ctx, void *buf, int len),
1072 void *wctx)
1073{
1074 int i;
1075
1076 /*
1077 * Each line of the save file contains three components. First
1078 * exactly 8 characters of header word indicating what type of
1079 * data is contained on the line; then a colon followed by a
1080 * decimal integer giving the length of the main string on the
1081 * line; then a colon followed by the string itself (exactly as
1082 * many bytes as previously specified, no matter what they
1083 * contain). Then a newline (of reasonably flexible form).
1084 */
1085#define wr(h,s) do { \
1086 char hbuf[80]; \
1087 char *str = (s); \
48d4740c 1088 sprintf(hbuf, "%-8.8s:%d:", (h), (int)strlen(str)); \
a4393230 1089 write(wctx, hbuf, strlen(hbuf)); \
1090 write(wctx, str, strlen(str)); \
1091 write(wctx, "\n", 1); \
1092} while (0)
1093
1094 /*
1095 * Magic string identifying the file, and version number of the
1096 * file format.
1097 */
1098 wr("SAVEFILE", SERIALISE_MAGIC);
1099 wr("VERSION", SERIALISE_VERSION);
1100
1101 /*
1102 * The game name. (Copied locally to avoid const annoyance.)
1103 */
1104 {
1105 char *s = dupstr(me->ourgame->name);
1106 wr("GAME", s);
1107 sfree(s);
1108 }
1109
1110 /*
1111 * The current long-term parameters structure, in full.
1112 */
1113 if (me->params) {
1114 char *s = me->ourgame->encode_params(me->params, TRUE);
1115 wr("PARAMS", s);
1116 sfree(s);
1117 }
1118
1119 /*
1120 * The current short-term parameters structure, in full.
1121 */
1122 if (me->curparams) {
1123 char *s = me->ourgame->encode_params(me->curparams, TRUE);
1124 wr("CPARAMS", s);
1125 sfree(s);
1126 }
1127
1128 /*
1129 * The current game description, the privdesc, and the random seed.
1130 */
1131 if (me->seedstr)
1132 wr("SEED", me->seedstr);
1133 if (me->desc)
1134 wr("DESC", me->desc);
1135 if (me->privdesc)
1136 wr("PRIVDESC", me->privdesc);
1137
1138 /*
1139 * The game's aux_info. We obfuscate this to prevent spoilers
1140 * (people are likely to run `head' or similar on a saved game
1141 * file simply to find out what it is, and don't necessarily
1142 * want to be told the answer to the puzzle!)
1143 */
1144 if (me->aux_info) {
1145 unsigned char *s1;
1146 char *s2;
1147 int len;
1148
1149 len = strlen(me->aux_info);
1150 s1 = snewn(len, unsigned char);
1151 memcpy(s1, me->aux_info, len);
1152 obfuscate_bitmap(s1, len*8, FALSE);
1153 s2 = bin2hex(s1, len);
1154
1155 wr("AUXINFO", s2);
1156
1157 sfree(s2);
1158 sfree(s1);
1159 }
1160
1161 /*
1162 * Any required serialisation of the game_ui.
1163 */
1164 if (me->ui) {
1165 char *s = me->ourgame->encode_ui(me->ui);
1166 if (s) {
1167 wr("UI", s);
1168 sfree(s);
1169 }
1170 }
1171
1172 /*
1173 * The game time, if it's a timed game.
1174 */
1175 if (me->ourgame->is_timed) {
1176 char buf[80];
1177 sprintf(buf, "%g", me->elapsed);
1178 wr("TIME", buf);
1179 }
1180
1181 /*
1182 * The length of, and position in, the states list.
1183 */
1184 {
1185 char buf[80];
1186 sprintf(buf, "%d", me->nstates);
1187 wr("NSTATES", buf);
1188 sprintf(buf, "%d", me->statepos);
1189 wr("STATEPOS", buf);
1190 }
1191
1192 /*
1193 * For each state after the initial one (which we know is
1194 * constructed from either privdesc or desc), enough
1195 * information for execute_move() to reconstruct it from the
1196 * previous one.
1197 */
1198 for (i = 1; i < me->nstates; i++) {
1199 assert(me->states[i].movetype != NEWGAME); /* only state 0 */
1200 switch (me->states[i].movetype) {
1201 case MOVE:
1202 wr("MOVE", me->states[i].movestr);
1203 break;
1204 case SOLVE:
1205 wr("SOLVE", me->states[i].movestr);
1206 break;
1207 case RESTART:
1208 wr("RESTART", me->states[i].movestr);
1209 break;
1210 }
1211 }
1212
1213#undef wr
1214}
1215
1216/*
1217 * This function returns NULL on success, or an error message.
1218 */
1219char *midend_deserialise(midend_data *me,
1220 int (*read)(void *ctx, void *buf, int len),
1221 void *rctx)
1222{
1223 int nstates = 0, statepos = -1, gotstates = 0;
1224 int started = FALSE;
1225 int i;
1226
1227 char *val = NULL;
1228 /* Initially all errors give the same report */
1229 char *ret = "Data does not appear to be a saved game file";
1230
1231 /*
1232 * We construct all the new state in local variables while we
1233 * check its sanity. Only once we have finished reading the
1234 * serialised data and detected no errors at all do we start
1235 * modifying stuff in the midend_data passed in.
1236 */
1237 char *seed = NULL, *parstr = NULL, *desc = NULL, *privdesc = NULL;
1238 char *auxinfo = NULL, *uistr = NULL, *cparstr = NULL;
1239 float elapsed = 0.0F;
1240 game_params *params = NULL, *cparams = NULL;
1241 game_ui *ui = NULL;
1242 struct midend_state_entry *states = NULL;
1243
1244 /*
1245 * Loop round and round reading one key/value pair at a time
1246 * from the serialised stream, until we have enough game states
1247 * to finish.
1248 */
1249 while (nstates <= 0 || statepos < 0 || gotstates < nstates-1) {
1250 char key[9], c;
1251 int len;
1252
1253 do {
1254 if (!read(rctx, key, 1)) {
1255 /* unexpected EOF */
1256 goto cleanup;
1257 }
1258 } while (key[0] == '\r' || key[0] == '\n');
1259
1260 if (!read(rctx, key+1, 8)) {
1261 /* unexpected EOF */
1262 goto cleanup;
1263 }
1264
1265 if (key[8] != ':') {
1266 if (started)
1267 ret = "Data was incorrectly formatted for a saved game file";
1268 }
1269 len = strcspn(key, ": ");
1270 assert(len <= 8);
1271 key[len] = '\0';
1272
1273 len = 0;
1274 while (1) {
1275 if (!read(rctx, &c, 1)) {
1276 /* unexpected EOF */
1277 goto cleanup;
1278 }
1279
1280 if (c == ':') {
1281 break;
1282 } else if (c >= '0' && c <= '9') {
1283 len = (len * 10) + (c - '0');
1284 } else {
1285 if (started)
1286 ret = "Data was incorrectly formatted for a"
1287 " saved game file";
1288 goto cleanup;
1289 }
1290 }
1291
1292 val = snewn(len+1, char);
1293 if (!read(rctx, val, len)) {
1294 if (started)
1295 goto cleanup;
1296 }
1297 val[len] = '\0';
1298
1299 if (!started) {
1300 if (strcmp(key, "SAVEFILE") || strcmp(val, SERIALISE_MAGIC)) {
1301 /* ret already has the right message in it */
1302 goto cleanup;
1303 }
1304 /* Now most errors are this one, unless otherwise specified */
1305 ret = "Saved data ended unexpectedly";
1306 started = TRUE;
1307 } else {
1308 if (!strcmp(key, "VERSION")) {
1309 if (strcmp(val, SERIALISE_VERSION)) {
1310 ret = "Cannot handle this version of the saved game"
1311 " file format";
1312 goto cleanup;
1313 }
1314 } else if (!strcmp(key, "GAME")) {
1315 if (strcmp(val, me->ourgame->name)) {
1316 ret = "Save file is from a different game";
1317 goto cleanup;
1318 }
1319 } else if (!strcmp(key, "PARAMS")) {
1320 sfree(parstr);
1321 parstr = val;
1322 val = NULL;
1323 } else if (!strcmp(key, "CPARAMS")) {
1324 sfree(cparstr);
1325 cparstr = val;
1326 val = NULL;
1327 } else if (!strcmp(key, "SEED")) {
1328 sfree(seed);
1329 seed = val;
1330 val = NULL;
1331 } else if (!strcmp(key, "DESC")) {
1332 sfree(desc);
1333 desc = val;
1334 val = NULL;
1335 } else if (!strcmp(key, "PRIVDESC")) {
1336 sfree(privdesc);
1337 privdesc = val;
1338 val = NULL;
1339 } else if (!strcmp(key, "AUXINFO")) {
1340 unsigned char *tmp;
1341 int len = strlen(val) / 2; /* length in bytes */
1342 tmp = hex2bin(val, len);
1343 obfuscate_bitmap(tmp, len*8, TRUE);
1344
1345 sfree(auxinfo);
1346 auxinfo = snewn(len + 1, char);
1347 memcpy(auxinfo, tmp, len);
1348 auxinfo[len] = '\0';
1349 sfree(tmp);
1350 } else if (!strcmp(key, "UI")) {
1351 sfree(uistr);
1352 uistr = val;
1353 val = NULL;
1354 } else if (!strcmp(key, "TIME")) {
871bf294 1355 elapsed = atof(val);
a4393230 1356 } else if (!strcmp(key, "NSTATES")) {
1357 nstates = atoi(val);
1358 if (nstates <= 0) {
1359 ret = "Number of states in save file was negative";
1360 goto cleanup;
1361 }
1362 if (states) {
1363 ret = "Two state counts provided in save file";
1364 goto cleanup;
1365 }
1366 states = snewn(nstates, struct midend_state_entry);
1367 for (i = 0; i < nstates; i++) {
1368 states[i].state = NULL;
1369 states[i].movestr = NULL;
1370 states[i].movetype = NEWGAME;
1371 }
1372 } else if (!strcmp(key, "STATEPOS")) {
1373 statepos = atoi(val);
1374 } else if (!strcmp(key, "MOVE")) {
1375 gotstates++;
1376 states[gotstates].movetype = MOVE;
1377 states[gotstates].movestr = val;
1378 val = NULL;
1379 } else if (!strcmp(key, "SOLVE")) {
1380 gotstates++;
1381 states[gotstates].movetype = SOLVE;
1382 states[gotstates].movestr = val;
1383 val = NULL;
1384 } else if (!strcmp(key, "RESTART")) {
1385 gotstates++;
1386 states[gotstates].movetype = RESTART;
1387 states[gotstates].movestr = val;
1388 val = NULL;
1389 }
1390 }
1391
1392 sfree(val);
1393 val = NULL;
1394 }
1395
1396 params = me->ourgame->default_params();
1397 me->ourgame->decode_params(params, parstr);
1398 if (me->ourgame->validate_params(params)) {
1399 ret = "Long-term parameters in save file are invalid";
1400 goto cleanup;
1401 }
1402 cparams = me->ourgame->default_params();
1403 me->ourgame->decode_params(cparams, cparstr);
1404 if (me->ourgame->validate_params(cparams)) {
1405 ret = "Short-term parameters in save file are invalid";
1406 goto cleanup;
1407 }
1408 if (!desc) {
1409 ret = "Game description in save file is missing";
1410 goto cleanup;
1411 } else if (me->ourgame->validate_desc(params, desc)) {
1412 ret = "Game description in save file is invalid";
1413 goto cleanup;
1414 }
1415 if (privdesc && me->ourgame->validate_desc(params, privdesc)) {
1416 ret = "Game private description in save file is invalid";
1417 goto cleanup;
1418 }
1419 if (statepos < 0 || statepos >= nstates) {
1420 ret = "Game position in save file is out of range";
1421 }
1422
1423 states[0].state = me->ourgame->new_game(me, params,
1424 privdesc ? privdesc : desc);
1425 for (i = 1; i < nstates; i++) {
1426 assert(states[i].movetype != NEWGAME);
1427 switch (states[i].movetype) {
1428 case MOVE:
1429 case SOLVE:
1430 states[i].state = me->ourgame->execute_move(states[i-1].state,
1431 states[i].movestr);
1432 if (states[i].state == NULL) {
1433 ret = "Save file contained an invalid move";
1434 goto cleanup;
1435 }
1436 break;
1437 case RESTART:
1438 if (me->ourgame->validate_desc(params, states[i].movestr)) {
1439 ret = "Save file contained an invalid restart move";
1440 goto cleanup;
1441 }
1442 states[i].state = me->ourgame->new_game(me, params,
1443 states[i].movestr);
1444 break;
1445 }
1446 }
1447
1448 ui = me->ourgame->new_ui(states[0].state);
1449 me->ourgame->decode_ui(ui, uistr);
1450
1451 /*
1452 * Now we've run out of possible error conditions, so we're
1453 * ready to start overwriting the real data in the current
1454 * midend. We'll do this by swapping things with the local
1455 * variables, so that the same cleanup code will free the old
1456 * stuff.
1457 */
1458 {
1459 char *tmp;
1460
1461 tmp = me->desc;
1462 me->desc = desc;
1463 desc = tmp;
1464
1465 tmp = me->privdesc;
1466 me->privdesc = privdesc;
1467 privdesc = tmp;
1468
1469 tmp = me->seedstr;
1470 me->seedstr = seed;
1471 seed = tmp;
1472
1473 tmp = me->aux_info;
1474 me->aux_info = auxinfo;
1475 auxinfo = tmp;
1476 }
1477
1478 me->genmode = GOT_NOTHING;
1479
1480 me->statesize = nstates;
1481 nstates = me->nstates;
1482 me->nstates = me->statesize;
1483 {
1484 struct midend_state_entry *tmp;
1485 tmp = me->states;
1486 me->states = states;
1487 states = tmp;
1488 }
1489 me->statepos = statepos;
1490
1491 {
1492 game_params *tmp;
1493
1494 tmp = me->params;
1495 me->params = params;
1496 params = tmp;
1497
1498 tmp = me->curparams;
1499 me->curparams = cparams;
1500 cparams = tmp;
1501 }
1502
1503 me->oldstate = NULL;
1504 me->anim_time = me->anim_pos = me->flash_time = me->flash_pos = 0.0F;
1505 me->dir = 0;
1506
1507 {
1508 game_ui *tmp;
1509
1510 tmp = me->ui;
1511 me->ui = ui;
1512 ui = tmp;
1513 }
1514
1515 me->elapsed = elapsed;
1516 me->pressed_mouse_button = 0;
1517
1518 midend_set_timer(me);
1519
871bf294 1520 if (me->drawstate)
1521 me->ourgame->free_drawstate(me->drawstate);
1522 me->drawstate =
1523 me->ourgame->new_drawstate(me->states[me->statepos-1].state);
1524 midend_size_new_drawstate(me);
1525
a4393230 1526 ret = NULL; /* success! */
1527
1528 cleanup:
1529 sfree(val);
1530 sfree(seed);
1531 sfree(parstr);
1532 sfree(cparstr);
1533 sfree(desc);
1534 sfree(privdesc);
1535 sfree(auxinfo);
1536 sfree(uistr);
1537 if (params)
1538 me->ourgame->free_params(params);
1539 if (cparams)
1540 me->ourgame->free_params(cparams);
1541 if (ui)
1542 me->ourgame->free_ui(ui);
1543 if (states) {
1544 int i;
1545
1546 for (i = 0; i < nstates; i++) {
1547 if (states[i].state)
1548 me->ourgame->free_game(states[i].state);
1549 sfree(states[i].movestr);
1550 }
1551 sfree(states);
1552 }
1553
1554 return ret;
1555}