2 * midend.c: general middle fragment sitting between the
3 * platform-specific front end and game-specific back end.
4 * Maintains a move list, takes care of Undo and Redo commands, and
5 * processes standard keystrokes for undo/redo/new/quit.
16 enum { DEF_PARAMS, DEF_SEED, DEF_DESC }; /* for midend_game_id_int */
18 struct midend_state_entry {
20 int special; /* created by solve or restart */
29 * `desc' and `privdesc' deserve a comment.
31 * `desc' is the game description as presented to the user when
32 * they ask for Game -> Specific. `privdesc', if non-NULL, is a
33 * different game description used to reconstruct the initial
34 * game_state when de-serialising. If privdesc is NULL, `desc'
37 * For almost all games, `privdesc' is NULL and never used. The
38 * exception (as usual) is Mines: the initial game state has no
39 * squares open at all, but after the first click `desc' is
40 * rewritten to describe a game state with an initial click and
41 * thus a bunch of squares open. If we used that desc to
42 * serialise and deserialise, then the initial game state after
43 * deserialisation would look unlike the initial game state
44 * beforehand, and worse still execute_move() might fail on the
45 * attempted first click. So `privdesc' is also used in this
46 * case, to provide a game description describing the same
47 * fixed mine layout _but_ no initial click. (These game IDs
48 * may also be typed directly into Mines if you like.)
50 char *desc, *privdesc, *seedstr;
51 game_aux_info *aux_info;
52 enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
53 int nstates, statesize, statepos;
55 game_params **presets;
57 int npresets, presetsize;
59 game_params *params, *curparams;
60 struct midend_state_entry *states;
61 game_drawstate *drawstate;
64 float anim_time, anim_pos;
65 float flash_time, flash_pos;
72 int pressed_mouse_button;
74 int winwidth, winheight;
77 #define ensure(me) do { \
78 if ((me)->nstates >= (me)->statesize) { \
79 (me)->statesize = (me)->nstates + 128; \
80 (me)->states = sresize((me)->states, (me)->statesize, \
81 struct midend_state_entry); \
85 midend_data *midend_new(frontend *fe, const game *ourgame)
87 midend_data *me = snew(midend_data);
91 get_random_seed(&randseed, &randseedsize);
94 me->ourgame = ourgame;
95 me->random = random_init(randseed, randseedsize);
96 me->nstates = me->statesize = me->statepos = 0;
98 me->params = ourgame->default_params();
100 me->desc = me->privdesc = NULL;
103 me->genmode = GOT_NOTHING;
104 me->drawstate = NULL;
107 me->preset_names = NULL;
108 me->npresets = me->presetsize = 0;
109 me->anim_time = me->anim_pos = 0.0F;
110 me->flash_time = me->flash_pos = 0.0F;
113 me->pressed_mouse_button = 0;
114 me->laststatus = NULL;
117 me->winwidth = me->winheight = 0;
124 static void midend_free_game(midend_data *me)
126 while (me->nstates > 0)
127 me->ourgame->free_game(me->states[--me->nstates].state);
130 me->ourgame->free_drawstate(me->drawstate);
133 void midend_free(midend_data *me)
137 midend_free_game(me);
139 random_free(me->random);
144 me->ourgame->free_aux_info(me->aux_info);
145 me->ourgame->free_params(me->params);
147 for (i = 0; i < me->npresets; i++) {
148 sfree(me->presets[i]);
149 sfree(me->preset_names[i]);
152 sfree(me->preset_names);
155 me->ourgame->free_ui(me->ui);
157 me->ourgame->free_params(me->curparams);
158 sfree(me->laststatus);
162 void midend_size(midend_data *me, int *x, int *y, int expand)
164 me->ourgame->size(me->params, me->drawstate, x, y, expand);
169 void midend_set_params(midend_data *me, game_params *params)
171 me->ourgame->free_params(me->params);
172 me->params = me->ourgame->dup_params(params);
175 static void midend_set_timer(midend_data *me)
177 me->timing = (me->ourgame->is_timed &&
178 me->ourgame->timing_state(me->states[me->statepos-1].state));
179 if (me->timing || me->flash_time || me->anim_time)
180 activate_timer(me->frontend);
182 deactivate_timer(me->frontend);
185 static void midend_size_new_drawstate(midend_data *me)
187 me->ourgame->size(me->params, me->drawstate, &me->winwidth, &me->winheight,
191 void midend_force_redraw(midend_data *me)
194 me->ourgame->free_drawstate(me->drawstate);
195 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
196 midend_size_new_drawstate(me);
200 void midend_new_game(midend_data *me)
202 midend_free_game(me);
204 assert(me->nstates == 0);
206 if (me->genmode == GOT_DESC) {
207 me->genmode = GOT_NOTHING;
211 if (me->genmode == GOT_SEED) {
212 me->genmode = GOT_NOTHING;
215 * Generate a new random seed. 15 digits comes to about
216 * 48 bits, which should be more than enough.
218 * I'll avoid putting a leading zero on the number,
219 * just in case it confuses anybody who thinks it's
220 * processed as an integer rather than a string.
225 newseed[0] = '1' + random_upto(me->random, 9);
226 for (i = 1; i < 15; i++)
227 newseed[i] = '0' + random_upto(me->random, 10);
229 me->seedstr = dupstr(newseed);
232 me->ourgame->free_params(me->curparams);
233 me->curparams = me->ourgame->dup_params(me->params);
239 me->ourgame->free_aux_info(me->aux_info);
242 rs = random_init(me->seedstr, strlen(me->seedstr));
243 me->desc = me->ourgame->new_desc(me->curparams, rs,
244 &me->aux_info, TRUE);
250 me->states[me->nstates].state =
251 me->ourgame->new_game(me, me->params, me->desc);
252 me->states[me->nstates].special = TRUE;
255 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
256 midend_size_new_drawstate(me);
258 midend_set_timer(me);
260 me->ourgame->free_ui(me->ui);
261 me->ui = me->ourgame->new_ui(me->states[0].state);
262 me->pressed_mouse_button = 0;
265 static int midend_undo(midend_data *me)
267 if (me->statepos > 1) {
269 me->ourgame->changed_state(me->ui,
270 me->states[me->statepos-1].state,
271 me->states[me->statepos-2].state);
279 static int midend_redo(midend_data *me)
281 if (me->statepos < me->nstates) {
283 me->ourgame->changed_state(me->ui,
284 me->states[me->statepos-1].state,
285 me->states[me->statepos].state);
293 static void midend_finish_move(midend_data *me)
298 * We do not flash if the later of the two states is special.
299 * This covers both forward Solve moves and backward (undone)
302 if ((me->oldstate || me->statepos > 1) &&
303 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
304 (me->dir < 0 && me->statepos < me->nstates &&
305 !me->states[me->statepos].special))) {
306 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
307 me->states[me->statepos-2].state,
308 me->states[me->statepos-1].state,
309 me->oldstate ? me->dir : +1,
312 me->flash_pos = 0.0F;
313 me->flash_time = flashtime;
318 me->ourgame->free_game(me->oldstate);
320 me->anim_pos = me->anim_time = 0;
323 midend_set_timer(me);
326 void midend_stop_anim(midend_data *me)
328 if (me->oldstate || me->anim_time) {
329 midend_finish_move(me);
334 void midend_restart_game(midend_data *me)
338 midend_stop_anim(me);
340 assert(me->statepos >= 1);
341 if (me->statepos == 1)
342 return; /* no point doing anything at all! */
345 * During restart, we reconstruct the game from the (public)
346 * game description rather than from states[0], because that
347 * way Mines gets slightly more sensible behaviour (restart
348 * goes to _after_ the first click so you don't have to
349 * remember where you clicked).
351 s = me->ourgame->new_game(me, me->params, me->desc);
354 * Now enter the restarted state as the next move.
356 midend_stop_anim(me);
357 while (me->nstates > me->statepos)
358 me->ourgame->free_game(me->states[--me->nstates].state);
360 me->states[me->nstates].state = s;
361 me->states[me->nstates].special = TRUE; /* we just restarted */
362 me->statepos = ++me->nstates;
364 me->ourgame->changed_state(me->ui,
365 me->states[me->statepos-2].state,
366 me->states[me->statepos-1].state);
368 midend_finish_move(me);
370 midend_set_timer(me);
373 static int midend_really_process_key(midend_data *me, int x, int y, int button)
375 game_state *oldstate =
376 me->ourgame->dup_game(me->states[me->statepos - 1].state);
377 int special = FALSE, gotspecial = FALSE, ret = 1;
380 if (button == 'n' || button == 'N' || button == '\x0E') {
381 midend_stop_anim(me);
384 goto done; /* never animate */
385 } else if (button == 'u' || button == 'u' ||
386 button == '\x1A' || button == '\x1F') {
387 midend_stop_anim(me);
388 special = me->states[me->statepos-1].special;
390 if (!midend_undo(me))
392 } else if (button == 'r' || button == 'R' ||
393 button == '\x12' || button == '\x19') {
394 midend_stop_anim(me);
395 if (!midend_redo(me))
397 } else if (button == 'q' || button == 'Q' || button == '\x11') {
405 me->ourgame->interpret_move(me->states[me->statepos-1].state,
406 me->ui, me->drawstate, x, y, button);
410 s = me->states[me->statepos-1].state;
412 s = me->ourgame->execute_move(me->states[me->statepos-1].state,
418 if (s == me->states[me->statepos-1].state) {
420 * make_move() is allowed to return its input state to
421 * indicate that although no move has been made, the UI
422 * state has been updated and a redraw is called for.
427 midend_stop_anim(me);
428 while (me->nstates > me->statepos)
429 me->ourgame->free_game(me->states[--me->nstates].state);
431 me->states[me->nstates].state = s;
432 me->states[me->nstates].special = FALSE; /* normal move */
433 me->statepos = ++me->nstates;
441 special = me->states[me->statepos-1].special;
444 * See if this move requires an animation.
449 anim_time = me->ourgame->anim_length(oldstate,
450 me->states[me->statepos-1].state,
454 me->oldstate = oldstate; oldstate = NULL;
456 me->anim_time = anim_time;
459 midend_finish_move(me);
465 midend_set_timer(me);
468 if (oldstate) me->ourgame->free_game(oldstate);
472 int midend_process_key(midend_data *me, int x, int y, int button)
477 * Harmonise mouse drag and release messages.
479 * Some front ends might accidentally switch from sending, say,
480 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
481 * drag. (This can happen on the Mac, for example, since
482 * RIGHT_DRAG is usually done using Command+drag, and if the
483 * user accidentally releases Command half way through the drag
484 * then there will be trouble.)
486 * It would be an O(number of front ends) annoyance to fix this
487 * in the front ends, but an O(number of back ends) annoyance
488 * to have each game capable of dealing with it. Therefore, we
489 * fix it _here_ in the common midend code so that it only has
492 * The possible ways in which things can go screwy in the front
495 * - in a system containing multiple physical buttons button
496 * presses can inadvertently overlap. We can see ABab (caps
497 * meaning button-down and lowercase meaning button-up) when
498 * the user had semantically intended AaBb.
500 * - in a system where one button is simulated by means of a
501 * modifier key and another button, buttons can mutate
502 * between press and release (possibly during drag). So we
503 * can see Ab instead of Aa.
505 * Definite requirements are:
507 * - button _presses_ must never be invented or destroyed. If
508 * the user presses two buttons in succession, the button
509 * presses must be transferred to the backend unchanged. So
510 * if we see AaBb , that's fine; if we see ABab (the button
511 * presses inadvertently overlapped) we must somehow
512 * `correct' it to AaBb.
514 * - every mouse action must end up looking like a press, zero
515 * or more drags, then a release. This allows back ends to
516 * make the _assumption_ that incoming mouse data will be
517 * sane in this regard, and not worry about the details.
519 * So my policy will be:
521 * - treat any button-up as a button-up for the currently
522 * pressed button, or ignore it if there is no currently
525 * - treat any drag as a drag for the currently pressed
526 * button, or ignore it if there is no currently pressed
529 * - if we see a button-down while another button is currently
530 * pressed, invent a button-up for the first one and then
531 * pass the button-down through as before.
533 * 2005-05-31: An addendum to the above. Some games might want
534 * a `priority order' among buttons, such that if one button is
535 * pressed while another is down then a fixed one of the
536 * buttons takes priority no matter what order they're pressed
537 * in. Mines, in particular, wants to treat a left+right click
538 * like a left click for the benefit of users of other
539 * implementations. So the last of the above points is modified
540 * in the presence of an (optional) button priority order.
542 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
543 if (me->pressed_mouse_button) {
544 if (IS_MOUSE_DRAG(button)) {
545 button = me->pressed_mouse_button +
546 (LEFT_DRAG - LEFT_BUTTON);
548 button = me->pressed_mouse_button +
549 (LEFT_RELEASE - LEFT_BUTTON);
552 return ret; /* ignore it */
553 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
555 * If the new button has lower priority than the old one,
556 * don't bother doing this.
558 if (me->ourgame->mouse_priorities &
559 BUTTON_BEATS(me->pressed_mouse_button, button))
560 return ret; /* just ignore it */
563 * Fabricate a button-up for the previously pressed button.
565 ret = ret && midend_really_process_key
566 (me, x, y, (me->pressed_mouse_button +
567 (LEFT_RELEASE - LEFT_BUTTON)));
571 * Now send on the event we originally received.
573 ret = ret && midend_really_process_key(me, x, y, button);
576 * And update the currently pressed button.
578 if (IS_MOUSE_RELEASE(button))
579 me->pressed_mouse_button = 0;
580 else if (IS_MOUSE_DOWN(button))
581 me->pressed_mouse_button = button;
586 void midend_redraw(midend_data *me)
588 if (me->statepos > 0 && me->drawstate) {
589 start_draw(me->frontend);
590 if (me->oldstate && me->anim_time > 0 &&
591 me->anim_pos < me->anim_time) {
592 assert(me->dir != 0);
593 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
594 me->states[me->statepos-1].state, me->dir,
595 me->ui, me->anim_pos, me->flash_pos);
597 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
598 me->states[me->statepos-1].state, +1 /*shrug*/,
599 me->ui, 0.0, me->flash_pos);
601 end_draw(me->frontend);
605 void midend_timer(midend_data *me, float tplus)
607 me->anim_pos += tplus;
608 if (me->anim_pos >= me->anim_time ||
609 me->anim_time == 0 || !me->oldstate) {
610 if (me->anim_time > 0)
611 midend_finish_move(me);
614 me->flash_pos += tplus;
615 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
616 me->flash_pos = me->flash_time = 0;
622 float oldelapsed = me->elapsed;
623 me->elapsed += tplus;
624 if ((int)oldelapsed != (int)me->elapsed)
625 status_bar(me->frontend, me->laststatus ? me->laststatus : "");
628 midend_set_timer(me);
631 float *midend_colours(midend_data *me, int *ncolours)
633 game_state *state = NULL;
636 if (me->nstates == 0) {
637 game_aux_info *aux = NULL;
638 char *desc = me->ourgame->new_desc(me->params, me->random,
640 state = me->ourgame->new_game(me, me->params, desc);
643 me->ourgame->free_aux_info(aux);
645 state = me->states[0].state;
647 ret = me->ourgame->colours(me->frontend, state, ncolours);
653 * Allow environment-based overrides for the standard
654 * colours by defining variables along the lines of
655 * `NET_COLOUR_4=6000c0'.
658 for (i = 0; i < *ncolours; i++) {
660 unsigned int r, g, b;
663 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
664 for (j = 0; buf[j]; j++)
665 buf[j] = toupper((unsigned char)buf[j]);
666 if ((e = getenv(buf)) != NULL &&
667 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
668 ret[i*3 + 0] = r / 255.0;
669 ret[i*3 + 1] = g / 255.0;
670 ret[i*3 + 2] = b / 255.0;
675 if (me->nstates == 0)
676 me->ourgame->free_game(state);
681 int midend_num_presets(midend_data *me)
687 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
688 if (me->presetsize <= me->npresets) {
689 me->presetsize = me->npresets + 10;
690 me->presets = sresize(me->presets, me->presetsize,
692 me->preset_names = sresize(me->preset_names, me->presetsize,
696 me->presets[me->npresets] = preset;
697 me->preset_names[me->npresets] = name;
704 * Allow environment-based extensions to the preset list by
705 * defining a variable along the lines of `SOLO_PRESETS=2x3
706 * Advanced:2x3da'. Colon-separated list of items,
707 * alternating between textual titles in the menu and
708 * encoded parameter strings.
710 char buf[80], *e, *p;
713 sprintf(buf, "%s_PRESETS", me->ourgame->name);
714 for (j = 0; buf[j]; j++)
715 buf[j] = toupper((unsigned char)buf[j]);
717 if ((e = getenv(buf)) != NULL) {
725 while (*p && *p != ':') p++;
728 while (*p && *p != ':') p++;
731 preset = me->ourgame->default_params();
732 me->ourgame->decode_params(preset, val);
734 if (me->ourgame->validate_params(preset)) {
735 /* Drop this one from the list. */
736 me->ourgame->free_params(preset);
740 if (me->presetsize <= me->npresets) {
741 me->presetsize = me->npresets + 10;
742 me->presets = sresize(me->presets, me->presetsize,
744 me->preset_names = sresize(me->preset_names,
745 me->presetsize, char *);
748 me->presets[me->npresets] = preset;
749 me->preset_names[me->npresets] = name;
758 void midend_fetch_preset(midend_data *me, int n,
759 char **name, game_params **params)
761 assert(n >= 0 && n < me->npresets);
762 *name = me->preset_names[n];
763 *params = me->presets[n];
766 int midend_wants_statusbar(midend_data *me)
768 return me->ourgame->wants_statusbar();
771 void midend_supersede_game_desc(midend_data *me, char *desc, char *privdesc)
775 printf("%s\n%s\n", desc, privdesc);
776 me->desc = dupstr(desc);
777 me->privdesc = privdesc ? dupstr(privdesc) : NULL;
780 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
782 char *titlebuf, *parstr, *rest;
787 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
791 sprintf(titlebuf, "%s configuration", me->ourgame->name);
792 *wintitle = titlebuf;
793 return me->ourgame->configure(me->params);
796 if (!me->curparams) {
800 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
801 which == CFG_SEED ? "random" : "game");
802 *wintitle = titlebuf;
804 ret = snewn(2, config_item);
806 ret[0].type = C_STRING;
807 if (which == CFG_SEED)
808 ret[0].name = "Game random seed";
810 ret[0].name = "Game ID";
813 * For CFG_DESC the text going in here will be a string
814 * encoding of the restricted parameters, plus a colon,
815 * plus the game description. For CFG_SEED it will be the
816 * full parameters, plus a hash, plus the random seed data.
817 * Either of these is a valid full game ID (although only
818 * the former is likely to persist across many code
821 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
823 if (which == CFG_DESC) {
824 rest = me->desc ? me->desc : "";
827 rest = me->seedstr ? me->seedstr : "";
830 ret[0].sval = snewn(strlen(parstr) + strlen(rest) + 2, char);
831 sprintf(ret[0].sval, "%s%c%s", parstr, sep, rest);
835 ret[1].name = ret[1].sval = NULL;
841 assert(!"We shouldn't be here");
845 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
847 char *error, *par, *desc, *seed;
849 seed = strchr(id, '#');
850 desc = strchr(id, ':');
852 if (desc && (!seed || desc < seed)) {
854 * We have a colon separating parameters from game
855 * description. So `par' now points to the parameters
856 * string, and `desc' to the description string.
861 } else if (seed && (!desc || seed < desc)) {
863 * We have a hash separating parameters from random seed.
864 * So `par' now points to the parameters string, and `seed'
865 * to the seed string.
872 * We only have one string. Depending on `defmode', we take
873 * it to be either parameters, seed or description.
875 if (defmode == DEF_SEED) {
878 } else if (defmode == DEF_DESC) {
888 game_params *tmpparams;
889 tmpparams = me->ourgame->dup_params(me->params);
890 me->ourgame->decode_params(tmpparams, par);
891 error = me->ourgame->validate_params(tmpparams);
893 me->ourgame->free_params(tmpparams);
897 me->ourgame->free_params(me->curparams);
898 me->curparams = tmpparams;
901 * Now filter only the persistent parts of this state into
902 * the long-term params structure, unless we've _only_
903 * received a params string in which case the whole lot is
907 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
908 me->ourgame->decode_params(me->params, tmpstr);
911 me->ourgame->free_params(me->params);
912 me->params = me->ourgame->dup_params(tmpparams);
918 me->desc = me->privdesc = NULL;
923 error = me->ourgame->validate_desc(me->params, desc);
927 me->desc = dupstr(desc);
928 me->genmode = GOT_DESC;
930 me->ourgame->free_aux_info(me->aux_info);
935 me->seedstr = dupstr(seed);
936 me->genmode = GOT_SEED;
942 char *midend_game_id(midend_data *me, char *id)
944 return midend_game_id_int(me, id, DEF_PARAMS);
947 char *midend_set_config(midend_data *me, int which, config_item *cfg)
954 params = me->ourgame->custom_params(cfg);
955 error = me->ourgame->validate_params(params);
958 me->ourgame->free_params(params);
962 me->ourgame->free_params(me->params);
968 error = midend_game_id_int(me, cfg[0].sval,
969 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
978 char *midend_text_format(midend_data *me)
980 if (me->ourgame->can_format_as_text && me->statepos > 0)
981 return me->ourgame->text_format(me->states[me->statepos-1].state);
986 char *midend_solve(midend_data *me)
991 if (!me->ourgame->can_solve)
992 return "This game does not support the Solve operation";
994 if (me->statepos < 1)
995 return "No game set up to solve"; /* _shouldn't_ happen! */
997 msg = "Solve operation failed"; /* game _should_ overwrite on error */
998 movestr = me->ourgame->solve(me->states[0].state,
999 me->states[me->statepos-1].state,
1000 me->aux_info, &msg);
1003 s = me->ourgame->execute_move(me->states[me->statepos-1].state, movestr);
1008 * Now enter the solved state as the next move.
1010 midend_stop_anim(me);
1011 while (me->nstates > me->statepos)
1012 me->ourgame->free_game(me->states[--me->nstates].state);
1014 me->states[me->nstates].state = s;
1015 me->states[me->nstates].special = TRUE; /* created using solve */
1016 me->statepos = ++me->nstates;
1018 me->ourgame->changed_state(me->ui,
1019 me->states[me->statepos-2].state,
1020 me->states[me->statepos-1].state);
1021 me->anim_time = 0.0;
1022 midend_finish_move(me);
1024 midend_set_timer(me);
1028 char *midend_rewrite_statusbar(midend_data *me, char *text)
1031 * An important special case is that we are occasionally called
1032 * with our own laststatus, to update the timer.
1034 if (me->laststatus != text) {
1035 sfree(me->laststatus);
1036 me->laststatus = dupstr(text);
1039 if (me->ourgame->is_timed) {
1040 char timebuf[100], *ret;
1046 sprintf(timebuf, "[%d:%02d] ", min, sec);
1048 ret = snewn(strlen(timebuf) + strlen(text) + 1, char);
1049 strcpy(ret, timebuf);
1054 return dupstr(text);