Added an automatic `Solve' feature to most games. This is useful for
[sgt/puzzles] / midend.c
1 /*
2 * midend.c: general middle fragment sitting between the
3 * platform-specific front end and game-specific back end.
4 * Maintains a move list, takes care of Undo and Redo commands, and
5 * processes standard keystrokes for undo/redo/new/restart/quit.
6 */
7
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11
12 #include "puzzles.h"
13
14 struct midend_data {
15 frontend *frontend;
16 random_state *random;
17 const game *ourgame;
18
19 char *seed;
20 game_aux_info *aux_info;
21 int fresh_seed;
22 int nstates, statesize, statepos;
23
24 game_params **presets;
25 char **preset_names;
26 int npresets, presetsize;
27
28 game_params *params;
29 game_state **states;
30 game_drawstate *drawstate;
31 game_state *oldstate;
32 game_ui *ui;
33 float anim_time, anim_pos;
34 float flash_time, flash_pos;
35 int dir;
36
37 int pressed_mouse_button;
38 };
39
40 #define ensure(me) do { \
41 if ((me)->nstates >= (me)->statesize) { \
42 (me)->statesize = (me)->nstates + 128; \
43 (me)->states = sresize((me)->states, (me)->statesize, game_state *); \
44 } \
45 } while (0)
46
47 midend_data *midend_new(frontend *fe, const game *ourgame)
48 {
49 midend_data *me = snew(midend_data);
50 void *randseed;
51 int randseedsize;
52
53 get_random_seed(&randseed, &randseedsize);
54
55 me->frontend = fe;
56 me->ourgame = ourgame;
57 me->random = random_init(randseed, randseedsize);
58 me->nstates = me->statesize = me->statepos = 0;
59 me->states = NULL;
60 me->params = ourgame->default_params();
61 me->seed = NULL;
62 me->aux_info = NULL;
63 me->fresh_seed = FALSE;
64 me->drawstate = NULL;
65 me->oldstate = NULL;
66 me->presets = NULL;
67 me->preset_names = NULL;
68 me->npresets = me->presetsize = 0;
69 me->anim_time = me->anim_pos = 0.0F;
70 me->flash_time = me->flash_pos = 0.0F;
71 me->dir = 0;
72 me->ui = NULL;
73 me->pressed_mouse_button = 0;
74
75 sfree(randseed);
76
77 return me;
78 }
79
80 void midend_free(midend_data *me)
81 {
82 sfree(me->states);
83 sfree(me->seed);
84 if (me->aux_info)
85 me->ourgame->free_aux_info(me->aux_info);
86 me->ourgame->free_params(me->params);
87 sfree(me);
88 }
89
90 void midend_size(midend_data *me, int *x, int *y)
91 {
92 me->ourgame->size(me->params, x, y);
93 }
94
95 void midend_set_params(midend_data *me, game_params *params)
96 {
97 me->ourgame->free_params(me->params);
98 me->params = me->ourgame->dup_params(params);
99 }
100
101 void midend_new_game(midend_data *me)
102 {
103 while (me->nstates > 0)
104 me->ourgame->free_game(me->states[--me->nstates]);
105
106 if (me->drawstate)
107 me->ourgame->free_drawstate(me->drawstate);
108
109 assert(me->nstates == 0);
110
111 if (!me->fresh_seed) {
112 sfree(me->seed);
113 if (me->aux_info)
114 me->ourgame->free_aux_info(me->aux_info);
115 me->aux_info = NULL;
116 me->seed = me->ourgame->new_seed(me->params, me->random,
117 &me->aux_info);
118 } else
119 me->fresh_seed = FALSE;
120
121 ensure(me);
122 me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed);
123 me->statepos = 1;
124 me->drawstate = me->ourgame->new_drawstate(me->states[0]);
125 if (me->ui)
126 me->ourgame->free_ui(me->ui);
127 me->ui = me->ourgame->new_ui(me->states[0]);
128 me->pressed_mouse_button = 0;
129 }
130
131 void midend_restart_game(midend_data *me)
132 {
133 while (me->nstates > 1)
134 me->ourgame->free_game(me->states[--me->nstates]);
135 me->statepos = me->nstates;
136 me->ourgame->free_ui(me->ui);
137 me->ui = me->ourgame->new_ui(me->states[0]);
138 }
139
140 static int midend_undo(midend_data *me)
141 {
142 if (me->statepos > 1) {
143 me->statepos--;
144 me->dir = -1;
145 return 1;
146 } else
147 return 0;
148 }
149
150 static int midend_redo(midend_data *me)
151 {
152 if (me->statepos < me->nstates) {
153 me->statepos++;
154 me->dir = +1;
155 return 1;
156 } else
157 return 0;
158 }
159
160 static void midend_finish_move(midend_data *me)
161 {
162 float flashtime;
163
164 if (me->oldstate || me->statepos > 1) {
165 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
166 me->states[me->statepos-2],
167 me->states[me->statepos-1],
168 me->oldstate ? me->dir : +1);
169 if (flashtime > 0) {
170 me->flash_pos = 0.0F;
171 me->flash_time = flashtime;
172 }
173 }
174
175 if (me->oldstate)
176 me->ourgame->free_game(me->oldstate);
177 me->oldstate = NULL;
178 me->anim_pos = me->anim_time = 0;
179 me->dir = 0;
180
181 if (me->flash_time == 0 && me->anim_time == 0)
182 deactivate_timer(me->frontend);
183 else
184 activate_timer(me->frontend);
185 }
186
187 static void midend_stop_anim(midend_data *me)
188 {
189 if (me->oldstate || me->anim_time) {
190 midend_finish_move(me);
191 midend_redraw(me);
192 }
193 }
194
195 static int midend_really_process_key(midend_data *me, int x, int y, int button)
196 {
197 game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]);
198 float anim_time;
199
200 if (button == 'n' || button == 'N' || button == '\x0E') {
201 midend_stop_anim(me);
202 midend_new_game(me);
203 midend_redraw(me);
204 return 1; /* never animate */
205 } else if (button == 'r' || button == 'R') {
206 midend_stop_anim(me);
207 midend_restart_game(me);
208 midend_redraw(me);
209 return 1; /* never animate */
210 } else if (button == 'u' || button == 'u' ||
211 button == '\x1A' || button == '\x1F') {
212 midend_stop_anim(me);
213 if (!midend_undo(me))
214 return 1;
215 } else if (button == '\x12') {
216 midend_stop_anim(me);
217 if (!midend_redo(me))
218 return 1;
219 } else if (button == 'q' || button == 'Q' || button == '\x11') {
220 me->ourgame->free_game(oldstate);
221 return 0;
222 } else {
223 game_state *s = me->ourgame->make_move(me->states[me->statepos-1],
224 me->ui, x, y, button);
225
226 if (s == me->states[me->statepos-1]) {
227 /*
228 * make_move() is allowed to return its input state to
229 * indicate that although no move has been made, the UI
230 * state has been updated and a redraw is called for.
231 */
232 midend_redraw(me);
233 return 1;
234 } else if (s) {
235 midend_stop_anim(me);
236 while (me->nstates > me->statepos)
237 me->ourgame->free_game(me->states[--me->nstates]);
238 ensure(me);
239 me->states[me->nstates] = s;
240 me->statepos = ++me->nstates;
241 me->dir = +1;
242 } else {
243 me->ourgame->free_game(oldstate);
244 return 1;
245 }
246 }
247
248 /*
249 * See if this move requires an animation.
250 */
251 anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1],
252 me->dir);
253
254 me->oldstate = oldstate;
255 if (anim_time > 0) {
256 me->anim_time = anim_time;
257 } else {
258 me->anim_time = 0.0;
259 midend_finish_move(me);
260 }
261 me->anim_pos = 0.0;
262
263 midend_redraw(me);
264
265 activate_timer(me->frontend);
266
267 return 1;
268 }
269
270 int midend_process_key(midend_data *me, int x, int y, int button)
271 {
272 int ret = 1;
273
274 /*
275 * Harmonise mouse drag and release messages.
276 *
277 * Some front ends might accidentally switch from sending, say,
278 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
279 * drag. (This can happen on the Mac, for example, since
280 * RIGHT_DRAG is usually done using Command+drag, and if the
281 * user accidentally releases Command half way through the drag
282 * then there will be trouble.)
283 *
284 * It would be an O(number of front ends) annoyance to fix this
285 * in the front ends, but an O(number of back ends) annoyance
286 * to have each game capable of dealing with it. Therefore, we
287 * fix it _here_ in the common midend code so that it only has
288 * to be done once.
289 *
290 * The possible ways in which things can go screwy in the front
291 * end are:
292 *
293 * - in a system containing multiple physical buttons button
294 * presses can inadvertently overlap. We can see ABab (caps
295 * meaning button-down and lowercase meaning button-up) when
296 * the user had semantically intended AaBb.
297 *
298 * - in a system where one button is simulated by means of a
299 * modifier key and another button, buttons can mutate
300 * between press and release (possibly during drag). So we
301 * can see Ab instead of Aa.
302 *
303 * Definite requirements are:
304 *
305 * - button _presses_ must never be invented or destroyed. If
306 * the user presses two buttons in succession, the button
307 * presses must be transferred to the backend unchanged. So
308 * if we see AaBb , that's fine; if we see ABab (the button
309 * presses inadvertently overlapped) we must somehow
310 * `correct' it to AaBb.
311 *
312 * - every mouse action must end up looking like a press, zero
313 * or more drags, then a release. This allows back ends to
314 * make the _assumption_ that incoming mouse data will be
315 * sane in this regard, and not worry about the details.
316 *
317 * So my policy will be:
318 *
319 * - treat any button-up as a button-up for the currently
320 * pressed button, or ignore it if there is no currently
321 * pressed button.
322 *
323 * - treat any drag as a drag for the currently pressed
324 * button, or ignore it if there is no currently pressed
325 * button.
326 *
327 * - if we see a button-down while another button is currently
328 * pressed, invent a button-up for the first one and then
329 * pass the button-down through as before.
330 *
331 */
332 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
333 if (me->pressed_mouse_button) {
334 if (IS_MOUSE_DRAG(button)) {
335 button = me->pressed_mouse_button +
336 (LEFT_DRAG - LEFT_BUTTON);
337 } else {
338 button = me->pressed_mouse_button +
339 (LEFT_RELEASE - LEFT_BUTTON);
340 }
341 } else
342 return ret; /* ignore it */
343 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
344 /*
345 * Fabricate a button-up for the previously pressed button.
346 */
347 ret = ret && midend_really_process_key
348 (me, x, y, (me->pressed_mouse_button +
349 (LEFT_RELEASE - LEFT_BUTTON)));
350 }
351
352 /*
353 * Now send on the event we originally received.
354 */
355 ret = ret && midend_really_process_key(me, x, y, button);
356
357 /*
358 * And update the currently pressed button.
359 */
360 if (IS_MOUSE_RELEASE(button))
361 me->pressed_mouse_button = 0;
362 else if (IS_MOUSE_DOWN(button))
363 me->pressed_mouse_button = button;
364
365 return ret;
366 }
367
368 void midend_redraw(midend_data *me)
369 {
370 if (me->statepos > 0 && me->drawstate) {
371 start_draw(me->frontend);
372 if (me->oldstate && me->anim_time > 0 &&
373 me->anim_pos < me->anim_time) {
374 assert(me->dir != 0);
375 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
376 me->states[me->statepos-1], me->dir,
377 me->ui, me->anim_pos, me->flash_pos);
378 } else {
379 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
380 me->states[me->statepos-1], +1 /*shrug*/,
381 me->ui, 0.0, me->flash_pos);
382 }
383 end_draw(me->frontend);
384 }
385 }
386
387 void midend_timer(midend_data *me, float tplus)
388 {
389 me->anim_pos += tplus;
390 if (me->anim_pos >= me->anim_time ||
391 me->anim_time == 0 || !me->oldstate) {
392 if (me->anim_time > 0)
393 midend_finish_move(me);
394 }
395 me->flash_pos += tplus;
396 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
397 me->flash_pos = me->flash_time = 0;
398 }
399 if (me->flash_time == 0 && me->anim_time == 0)
400 deactivate_timer(me->frontend);
401 midend_redraw(me);
402 }
403
404 float *midend_colours(midend_data *me, int *ncolours)
405 {
406 game_state *state = NULL;
407 float *ret;
408
409 if (me->nstates == 0) {
410 game_aux_info *aux = NULL;
411 char *seed = me->ourgame->new_seed(me->params, me->random, &aux);
412 state = me->ourgame->new_game(me->params, seed);
413 sfree(seed);
414 if (aux)
415 me->ourgame->free_aux_info(aux);
416 } else
417 state = me->states[0];
418
419 ret = me->ourgame->colours(me->frontend, state, ncolours);
420
421 if (me->nstates == 0)
422 me->ourgame->free_game(state);
423
424 return ret;
425 }
426
427 int midend_num_presets(midend_data *me)
428 {
429 if (!me->npresets) {
430 char *name;
431 game_params *preset;
432
433 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
434 if (me->presetsize <= me->npresets) {
435 me->presetsize = me->npresets + 10;
436 me->presets = sresize(me->presets, me->presetsize,
437 game_params *);
438 me->preset_names = sresize(me->preset_names, me->presetsize,
439 char *);
440 }
441
442 me->presets[me->npresets] = preset;
443 me->preset_names[me->npresets] = name;
444 me->npresets++;
445 }
446 }
447
448 return me->npresets;
449 }
450
451 void midend_fetch_preset(midend_data *me, int n,
452 char **name, game_params **params)
453 {
454 assert(n >= 0 && n < me->npresets);
455 *name = me->preset_names[n];
456 *params = me->presets[n];
457 }
458
459 int midend_wants_statusbar(midend_data *me)
460 {
461 return me->ourgame->wants_statusbar();
462 }
463
464 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
465 {
466 char *titlebuf, *parstr;
467 config_item *ret;
468
469 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
470
471 switch (which) {
472 case CFG_SETTINGS:
473 sprintf(titlebuf, "%s configuration", me->ourgame->name);
474 *wintitle = dupstr(titlebuf);
475 return me->ourgame->configure(me->params);
476 case CFG_SEED:
477 sprintf(titlebuf, "%s game selection", me->ourgame->name);
478 *wintitle = dupstr(titlebuf);
479
480 ret = snewn(2, config_item);
481
482 ret[0].type = C_STRING;
483 ret[0].name = "Game ID";
484 ret[0].ival = 0;
485 /*
486 * The text going in here will be a string encoding of the
487 * parameters, plus a colon, plus the game seed. This is a
488 * full game ID.
489 */
490 parstr = me->ourgame->encode_params(me->params);
491 ret[0].sval = snewn(strlen(parstr) + strlen(me->seed) + 2, char);
492 sprintf(ret[0].sval, "%s:%s", parstr, me->seed);
493 sfree(parstr);
494
495 ret[1].type = C_END;
496 ret[1].name = ret[1].sval = NULL;
497 ret[1].ival = 0;
498
499 return ret;
500 }
501
502 assert(!"We shouldn't be here");
503 return NULL;
504 }
505
506 char *midend_game_id(midend_data *me, char *id, int def_seed)
507 {
508 char *error, *par, *seed;
509 game_params *params;
510
511 seed = strchr(id, ':');
512
513 if (seed) {
514 /*
515 * We have a colon separating parameters from game seed. So
516 * `par' now points to the parameters string, and `seed' to
517 * the seed string.
518 */
519 *seed++ = '\0';
520 par = id;
521 } else {
522 /*
523 * We only have one string. Depending on `def_seed', we
524 * take it to be either parameters or seed.
525 */
526 if (def_seed) {
527 seed = id;
528 par = NULL;
529 } else {
530 seed = NULL;
531 par = id;
532 }
533 }
534
535 if (par) {
536 params = me->ourgame->decode_params(par);
537 error = me->ourgame->validate_params(params);
538 if (error) {
539 me->ourgame->free_params(params);
540 return error;
541 }
542 me->ourgame->free_params(me->params);
543 me->params = params;
544 }
545
546 if (seed) {
547 error = me->ourgame->validate_seed(me->params, seed);
548 if (error)
549 return error;
550
551 sfree(me->seed);
552 me->seed = dupstr(seed);
553 me->fresh_seed = TRUE;
554 if (me->aux_info)
555 me->ourgame->free_aux_info(me->aux_info);
556 me->aux_info = NULL;
557 }
558
559 return NULL;
560 }
561
562 char *midend_set_config(midend_data *me, int which, config_item *cfg)
563 {
564 char *error;
565 game_params *params;
566
567 switch (which) {
568 case CFG_SETTINGS:
569 params = me->ourgame->custom_params(cfg);
570 error = me->ourgame->validate_params(params);
571
572 if (error) {
573 me->ourgame->free_params(params);
574 return error;
575 }
576
577 me->ourgame->free_params(me->params);
578 me->params = params;
579 break;
580
581 case CFG_SEED:
582 error = midend_game_id(me, cfg[0].sval, TRUE);
583 if (error)
584 return error;
585 break;
586 }
587
588 return NULL;
589 }
590
591 char *midend_text_format(midend_data *me)
592 {
593 if (me->ourgame->can_format_as_text && me->statepos > 0)
594 return me->ourgame->text_format(me->states[me->statepos-1]);
595 else
596 return NULL;
597 }
598
599 char *midend_solve(midend_data *me)
600 {
601 game_state *s;
602 char *msg;
603
604 if (!me->ourgame->can_solve)
605 return "This game does not support the Solve operation";
606
607 if (me->statepos < 1)
608 return "No game set up to solve"; /* _shouldn't_ happen! */
609
610 msg = "Solve operation failed"; /* game _should_ overwrite on error */
611 s = me->ourgame->solve(me->states[0], me->aux_info, &msg);
612 if (!s)
613 return msg;
614
615 /*
616 * Now enter the solved state as the next move.~|~
617 */
618 midend_stop_anim(me);
619 while (me->nstates > me->statepos)
620 me->ourgame->free_game(me->states[--me->nstates]);
621 ensure(me);
622 me->states[me->nstates] = s;
623 me->statepos = ++me->nstates;
624 me->anim_time = 0.0;
625 midend_finish_move(me);
626 midend_redraw(me);
627 activate_timer(me->frontend);
628 return NULL;
629 }