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 game_aux_info *aux_info;
30 enum { GOT_SEED, GOT_DESC, GOT_NOTHING } genmode;
31 int nstates, statesize, statepos;
33 game_params **presets;
35 int npresets, presetsize;
37 game_params *params, *curparams;
38 struct midend_state_entry *states;
39 game_drawstate *drawstate;
42 float anim_time, anim_pos;
43 float flash_time, flash_pos;
46 int pressed_mouse_button;
49 #define ensure(me) do { \
50 if ((me)->nstates >= (me)->statesize) { \
51 (me)->statesize = (me)->nstates + 128; \
52 (me)->states = sresize((me)->states, (me)->statesize, \
53 struct midend_state_entry); \
57 midend_data *midend_new(frontend *fe, const game *ourgame)
59 midend_data *me = snew(midend_data);
63 get_random_seed(&randseed, &randseedsize);
66 me->ourgame = ourgame;
67 me->random = random_init(randseed, randseedsize);
68 me->nstates = me->statesize = me->statepos = 0;
70 me->params = ourgame->default_params();
75 me->genmode = GOT_NOTHING;
79 me->preset_names = NULL;
80 me->npresets = me->presetsize = 0;
81 me->anim_time = me->anim_pos = 0.0F;
82 me->flash_time = me->flash_pos = 0.0F;
85 me->pressed_mouse_button = 0;
92 void midend_free(midend_data *me)
97 random_free(me->random);
99 me->ourgame->free_aux_info(me->aux_info);
100 me->ourgame->free_params(me->params);
102 me->ourgame->free_params(me->curparams);
106 void midend_size(midend_data *me, int *x, int *y)
108 me->ourgame->size(me->params, x, y);
111 void midend_set_params(midend_data *me, game_params *params)
113 me->ourgame->free_params(me->params);
114 me->params = me->ourgame->dup_params(params);
117 void midend_new_game(midend_data *me)
119 while (me->nstates > 0)
120 me->ourgame->free_game(me->states[--me->nstates].state);
123 me->ourgame->free_drawstate(me->drawstate);
125 assert(me->nstates == 0);
127 if (me->genmode == GOT_DESC) {
128 me->genmode = GOT_NOTHING;
132 if (me->genmode == GOT_SEED) {
133 me->genmode = GOT_NOTHING;
136 * Generate a new random seed. 15 digits comes to about
137 * 48 bits, which should be more than enough.
139 * I'll avoid putting a leading zero on the number,
140 * just in case it confuses anybody who thinks it's
141 * processed as an integer rather than a string.
146 newseed[0] = '1' + random_upto(me->random, 9);
147 for (i = 1; i < 15; i++)
148 newseed[i] = '0' + random_upto(me->random, 10);
150 me->seedstr = dupstr(newseed);
153 me->ourgame->free_params(me->curparams);
154 me->curparams = me->ourgame->dup_params(me->params);
159 me->ourgame->free_aux_info(me->aux_info);
162 rs = random_init(me->seedstr, strlen(me->seedstr));
163 me->desc = me->ourgame->new_desc(me->curparams, rs, &me->aux_info);
168 me->states[me->nstates].state = me->ourgame->new_game(me->params, me->desc);
169 me->states[me->nstates].special = TRUE;
172 me->drawstate = me->ourgame->new_drawstate(me->states[0].state);
174 me->ourgame->free_ui(me->ui);
175 me->ui = me->ourgame->new_ui(me->states[0].state);
176 me->pressed_mouse_button = 0;
179 static int midend_undo(midend_data *me)
181 if (me->statepos > 1) {
189 static int midend_redo(midend_data *me)
191 if (me->statepos < me->nstates) {
199 static void midend_finish_move(midend_data *me)
204 * We do not flash if the later of the two states is special.
205 * This covers both forward Solve moves and backward (undone)
208 if ((me->oldstate || me->statepos > 1) &&
209 ((me->dir > 0 && !me->states[me->statepos-1].special) ||
210 (me->dir < 0 && me->statepos < me->nstates &&
211 !me->states[me->statepos].special))) {
212 flashtime = me->ourgame->flash_length(me->oldstate ? me->oldstate :
213 me->states[me->statepos-2].state,
214 me->states[me->statepos-1].state,
215 me->oldstate ? me->dir : +1,
218 me->flash_pos = 0.0F;
219 me->flash_time = flashtime;
224 me->ourgame->free_game(me->oldstate);
226 me->anim_pos = me->anim_time = 0;
229 if (me->flash_time == 0 && me->anim_time == 0)
230 deactivate_timer(me->frontend);
232 activate_timer(me->frontend);
235 static void midend_stop_anim(midend_data *me)
237 if (me->oldstate || me->anim_time) {
238 midend_finish_move(me);
243 void midend_restart_game(midend_data *me)
247 midend_stop_anim(me);
249 assert(me->statepos >= 1);
250 if (me->statepos == 1)
251 return; /* no point doing anything at all! */
253 s = me->ourgame->dup_game(me->states[0].state);
256 * Now enter the restarted state as the next move.
258 midend_stop_anim(me);
259 while (me->nstates > me->statepos)
260 me->ourgame->free_game(me->states[--me->nstates].state);
262 me->states[me->nstates].state = s;
263 me->states[me->nstates].special = TRUE; /* we just restarted */
264 me->statepos = ++me->nstates;
266 midend_finish_move(me);
268 activate_timer(me->frontend);
271 static int midend_really_process_key(midend_data *me, int x, int y, int button)
273 game_state *oldstate =
274 me->ourgame->dup_game(me->states[me->statepos - 1].state);
275 int special = FALSE, gotspecial = FALSE;
278 if (button == 'n' || button == 'N' || button == '\x0E') {
279 midend_stop_anim(me);
282 return 1; /* never animate */
283 } else if (button == 'u' || button == 'u' ||
284 button == '\x1A' || button == '\x1F') {
285 midend_stop_anim(me);
286 special = me->states[me->statepos-1].special;
288 if (!midend_undo(me))
290 } else if (button == 'r' || button == 'R' ||
292 midend_stop_anim(me);
293 if (!midend_redo(me))
295 } else if (button == 'q' || button == 'Q' || button == '\x11') {
296 me->ourgame->free_game(oldstate);
300 me->ourgame->make_move(me->states[me->statepos-1].state,
301 me->ui, x, y, button);
303 if (s == me->states[me->statepos-1].state) {
305 * make_move() is allowed to return its input state to
306 * indicate that although no move has been made, the UI
307 * state has been updated and a redraw is called for.
312 midend_stop_anim(me);
313 while (me->nstates > me->statepos)
314 me->ourgame->free_game(me->states[--me->nstates].state);
316 me->states[me->nstates].state = s;
317 me->states[me->nstates].special = FALSE; /* normal move */
318 me->statepos = ++me->nstates;
321 me->ourgame->free_game(oldstate);
327 special = me->states[me->statepos-1].special;
330 * See if this move requires an animation.
335 anim_time = me->ourgame->anim_length(oldstate,
336 me->states[me->statepos-1].state,
340 me->oldstate = oldstate;
342 me->anim_time = anim_time;
345 midend_finish_move(me);
351 activate_timer(me->frontend);
356 int midend_process_key(midend_data *me, int x, int y, int button)
361 * Harmonise mouse drag and release messages.
363 * Some front ends might accidentally switch from sending, say,
364 * RIGHT_DRAG messages to sending LEFT_DRAG, half way through a
365 * drag. (This can happen on the Mac, for example, since
366 * RIGHT_DRAG is usually done using Command+drag, and if the
367 * user accidentally releases Command half way through the drag
368 * then there will be trouble.)
370 * It would be an O(number of front ends) annoyance to fix this
371 * in the front ends, but an O(number of back ends) annoyance
372 * to have each game capable of dealing with it. Therefore, we
373 * fix it _here_ in the common midend code so that it only has
376 * The possible ways in which things can go screwy in the front
379 * - in a system containing multiple physical buttons button
380 * presses can inadvertently overlap. We can see ABab (caps
381 * meaning button-down and lowercase meaning button-up) when
382 * the user had semantically intended AaBb.
384 * - in a system where one button is simulated by means of a
385 * modifier key and another button, buttons can mutate
386 * between press and release (possibly during drag). So we
387 * can see Ab instead of Aa.
389 * Definite requirements are:
391 * - button _presses_ must never be invented or destroyed. If
392 * the user presses two buttons in succession, the button
393 * presses must be transferred to the backend unchanged. So
394 * if we see AaBb , that's fine; if we see ABab (the button
395 * presses inadvertently overlapped) we must somehow
396 * `correct' it to AaBb.
398 * - every mouse action must end up looking like a press, zero
399 * or more drags, then a release. This allows back ends to
400 * make the _assumption_ that incoming mouse data will be
401 * sane in this regard, and not worry about the details.
403 * So my policy will be:
405 * - treat any button-up as a button-up for the currently
406 * pressed button, or ignore it if there is no currently
409 * - treat any drag as a drag for the currently pressed
410 * button, or ignore it if there is no currently pressed
413 * - if we see a button-down while another button is currently
414 * pressed, invent a button-up for the first one and then
415 * pass the button-down through as before.
418 if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) {
419 if (me->pressed_mouse_button) {
420 if (IS_MOUSE_DRAG(button)) {
421 button = me->pressed_mouse_button +
422 (LEFT_DRAG - LEFT_BUTTON);
424 button = me->pressed_mouse_button +
425 (LEFT_RELEASE - LEFT_BUTTON);
428 return ret; /* ignore it */
429 } else if (IS_MOUSE_DOWN(button) && me->pressed_mouse_button) {
431 * Fabricate a button-up for the previously pressed button.
433 ret = ret && midend_really_process_key
434 (me, x, y, (me->pressed_mouse_button +
435 (LEFT_RELEASE - LEFT_BUTTON)));
439 * Now send on the event we originally received.
441 ret = ret && midend_really_process_key(me, x, y, button);
444 * And update the currently pressed button.
446 if (IS_MOUSE_RELEASE(button))
447 me->pressed_mouse_button = 0;
448 else if (IS_MOUSE_DOWN(button))
449 me->pressed_mouse_button = button;
454 void midend_redraw(midend_data *me)
456 if (me->statepos > 0 && me->drawstate) {
457 start_draw(me->frontend);
458 if (me->oldstate && me->anim_time > 0 &&
459 me->anim_pos < me->anim_time) {
460 assert(me->dir != 0);
461 me->ourgame->redraw(me->frontend, me->drawstate, me->oldstate,
462 me->states[me->statepos-1].state, me->dir,
463 me->ui, me->anim_pos, me->flash_pos);
465 me->ourgame->redraw(me->frontend, me->drawstate, NULL,
466 me->states[me->statepos-1].state, +1 /*shrug*/,
467 me->ui, 0.0, me->flash_pos);
469 end_draw(me->frontend);
473 void midend_timer(midend_data *me, float tplus)
475 me->anim_pos += tplus;
476 if (me->anim_pos >= me->anim_time ||
477 me->anim_time == 0 || !me->oldstate) {
478 if (me->anim_time > 0)
479 midend_finish_move(me);
481 me->flash_pos += tplus;
482 if (me->flash_pos >= me->flash_time || me->flash_time == 0) {
483 me->flash_pos = me->flash_time = 0;
485 if (me->flash_time == 0 && me->anim_time == 0)
486 deactivate_timer(me->frontend);
490 float *midend_colours(midend_data *me, int *ncolours)
492 game_state *state = NULL;
495 if (me->nstates == 0) {
496 game_aux_info *aux = NULL;
497 char *desc = me->ourgame->new_desc(me->params, me->random, &aux);
498 state = me->ourgame->new_game(me->params, desc);
501 me->ourgame->free_aux_info(aux);
503 state = me->states[0].state;
505 ret = me->ourgame->colours(me->frontend, state, ncolours);
511 * Allow environment-based overrides for the standard
512 * colours by defining variables along the lines of
513 * `NET_COLOUR_4=6000c0'.
516 for (i = 0; i < *ncolours; i++) {
518 unsigned int r, g, b;
521 sprintf(buf, "%s_COLOUR_%d", me->ourgame->name, i);
522 for (j = 0; buf[j]; j++)
523 buf[j] = toupper((unsigned char)buf[j]);
524 if ((e = getenv(buf)) != NULL &&
525 sscanf(e, "%2x%2x%2x", &r, &g, &b) == 3) {
526 ret[i*3 + 0] = r / 255.0;
527 ret[i*3 + 1] = g / 255.0;
528 ret[i*3 + 2] = b / 255.0;
533 if (me->nstates == 0)
534 me->ourgame->free_game(state);
539 int midend_num_presets(midend_data *me)
545 while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) {
546 if (me->presetsize <= me->npresets) {
547 me->presetsize = me->npresets + 10;
548 me->presets = sresize(me->presets, me->presetsize,
550 me->preset_names = sresize(me->preset_names, me->presetsize,
554 me->presets[me->npresets] = preset;
555 me->preset_names[me->npresets] = name;
562 * Allow environment-based extensions to the preset list by
563 * defining a variable along the lines of `SOLO_PRESETS=2x3
564 * Advanced:2x3da'. Colon-separated list of items,
565 * alternating between textual titles in the menu and
566 * encoded parameter strings.
568 char buf[80], *e, *p;
571 sprintf(buf, "%s_PRESETS", me->ourgame->name);
572 for (j = 0; buf[j]; j++)
573 buf[j] = toupper((unsigned char)buf[j]);
575 if ((e = getenv(buf)) != NULL) {
583 while (*p && *p != ':') p++;
586 while (*p && *p != ':') p++;
589 preset = me->ourgame->default_params();
590 me->ourgame->decode_params(preset, val);
592 if (me->ourgame->validate_params(preset)) {
593 /* Drop this one from the list. */
594 me->ourgame->free_params(preset);
598 if (me->presetsize <= me->npresets) {
599 me->presetsize = me->npresets + 10;
600 me->presets = sresize(me->presets, me->presetsize,
602 me->preset_names = sresize(me->preset_names,
603 me->presetsize, char *);
606 me->presets[me->npresets] = preset;
607 me->preset_names[me->npresets] = name;
616 void midend_fetch_preset(midend_data *me, int n,
617 char **name, game_params **params)
619 assert(n >= 0 && n < me->npresets);
620 *name = me->preset_names[n];
621 *params = me->presets[n];
624 int midend_wants_statusbar(midend_data *me)
626 return me->ourgame->wants_statusbar();
629 config_item *midend_get_config(midend_data *me, int which, char **wintitle)
631 char *titlebuf, *parstr;
634 titlebuf = snewn(40 + strlen(me->ourgame->name), char);
638 sprintf(titlebuf, "%s configuration", me->ourgame->name);
639 *wintitle = dupstr(titlebuf);
640 return me->ourgame->configure(me->params);
643 sprintf(titlebuf, "%s %s selection", me->ourgame->name,
644 which == CFG_SEED ? "random" : "game");
645 *wintitle = dupstr(titlebuf);
647 ret = snewn(2, config_item);
649 ret[0].type = C_STRING;
650 if (which == CFG_SEED)
651 ret[0].name = "Game random seed";
653 ret[0].name = "Game ID";
656 * For CFG_DESC the text going in here will be a string
657 * encoding of the restricted parameters, plus a colon,
658 * plus the game description. For CFG_SEED it will be the
659 * full parameters, plus a hash, plus the random seed data.
660 * Either of these is a valid full game ID (although only
661 * the former is likely to persist across many code
664 parstr = me->ourgame->encode_params(me->curparams, which == CFG_SEED);
665 if (which == CFG_DESC) {
666 ret[0].sval = snewn(strlen(parstr) + strlen(me->desc) + 2, char);
667 sprintf(ret[0].sval, "%s:%s", parstr, me->desc);
668 } else if (me->seedstr) {
669 ret[0].sval = snewn(strlen(parstr) + strlen(me->seedstr) + 2, char);
670 sprintf(ret[0].sval, "%s#%s", parstr, me->seedstr);
673 * If the current game was not randomly generated, the
674 * best we can do is to give a template for typing a
677 ret[0].sval = snewn(strlen(parstr) + 2, char);
678 sprintf(ret[0].sval, "%s#", parstr);
683 ret[1].name = ret[1].sval = NULL;
689 assert(!"We shouldn't be here");
693 static char *midend_game_id_int(midend_data *me, char *id, int defmode)
695 char *error, *par, *desc, *seed;
697 seed = strchr(id, '#');
698 desc = strchr(id, ':');
700 if (desc && (!seed || desc < seed)) {
702 * We have a colon separating parameters from game
703 * description. So `par' now points to the parameters
704 * string, and `desc' to the description string.
709 } else if (seed && (!desc || seed < desc)) {
711 * We have a hash separating parameters from random seed.
712 * So `par' now points to the parameters string, and `seed'
713 * to the seed string.
720 * We only have one string. Depending on `defmode', we take
721 * it to be either parameters, seed or description.
723 if (defmode == DEF_SEED) {
726 } else if (defmode == DEF_DESC) {
736 game_params *tmpparams;
737 tmpparams = me->ourgame->dup_params(me->params);
738 me->ourgame->decode_params(tmpparams, par);
739 error = me->ourgame->validate_params(tmpparams);
741 me->ourgame->free_params(tmpparams);
745 me->ourgame->free_params(me->curparams);
746 me->curparams = tmpparams;
749 * Now filter only the persistent parts of this state into
750 * the long-term params structure, unless we've _only_
751 * received a params string in which case the whole lot is
755 char *tmpstr = me->ourgame->encode_params(tmpparams, FALSE);
756 me->ourgame->decode_params(me->params, tmpstr);
759 me->ourgame->free_params(me->params);
760 me->params = me->ourgame->dup_params(tmpparams);
770 error = me->ourgame->validate_desc(me->params, desc);
774 me->desc = dupstr(desc);
775 me->genmode = GOT_DESC;
777 me->ourgame->free_aux_info(me->aux_info);
782 me->seedstr = dupstr(seed);
783 me->genmode = GOT_SEED;
789 char *midend_game_id(midend_data *me, char *id)
791 return midend_game_id_int(me, id, DEF_PARAMS);
794 char *midend_set_config(midend_data *me, int which, config_item *cfg)
801 params = me->ourgame->custom_params(cfg);
802 error = me->ourgame->validate_params(params);
805 me->ourgame->free_params(params);
809 me->ourgame->free_params(me->params);
815 error = midend_game_id_int(me, cfg[0].sval,
816 (which == CFG_SEED ? DEF_SEED : DEF_DESC));
825 char *midend_text_format(midend_data *me)
827 if (me->ourgame->can_format_as_text && me->statepos > 0)
828 return me->ourgame->text_format(me->states[me->statepos-1].state);
833 char *midend_solve(midend_data *me)
838 if (!me->ourgame->can_solve)
839 return "This game does not support the Solve operation";
841 if (me->statepos < 1)
842 return "No game set up to solve"; /* _shouldn't_ happen! */
844 msg = "Solve operation failed"; /* game _should_ overwrite on error */
845 s = me->ourgame->solve(me->states[0].state, me->aux_info, &msg);
850 * Now enter the solved state as the next move.
852 midend_stop_anim(me);
853 while (me->nstates > me->statepos)
854 me->ourgame->free_game(me->states[--me->nstates].state);
856 me->states[me->nstates].state = s;
857 me->states[me->nstates].special = TRUE; /* created using solve */
858 me->statepos = ++me->nstates;
860 midend_finish_move(me);
862 activate_timer(me->frontend);