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