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 | |
14 | struct midend_data { |
2ef96bd6 |
15 | frontend *frontend; |
48d70ca9 |
16 | random_state *random; |
be8d5aa1 |
17 | const game *ourgame; |
48d70ca9 |
18 | |
7f77ea24 |
19 | char *seed; |
6f2d8d7c |
20 | game_aux_info *aux_info; |
5928817c |
21 | int fresh_seed; |
7f77ea24 |
22 | int nstates, statesize, statepos; |
eb2ad6f1 |
23 | |
24 | game_params **presets; |
25 | char **preset_names; |
26 | int npresets, presetsize; |
27 | |
7f77ea24 |
28 | game_params *params; |
29 | game_state **states; |
2ef96bd6 |
30 | game_drawstate *drawstate; |
31 | game_state *oldstate; |
74a4e547 |
32 | game_ui *ui; |
2ef96bd6 |
33 | float anim_time, anim_pos; |
87ed82be |
34 | float flash_time, flash_pos; |
c822de4a |
35 | int dir; |
6776a950 |
36 | |
37 | int pressed_mouse_button; |
7f77ea24 |
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 | |
be8d5aa1 |
47 | midend_data *midend_new(frontend *fe, const game *ourgame) |
7f77ea24 |
48 | { |
49 | midend_data *me = snew(midend_data); |
cbb5549e |
50 | void *randseed; |
51 | int randseedsize; |
52 | |
53 | get_random_seed(&randseed, &randseedsize); |
7f77ea24 |
54 | |
48d70ca9 |
55 | me->frontend = fe; |
be8d5aa1 |
56 | me->ourgame = ourgame; |
48d70ca9 |
57 | me->random = random_init(randseed, randseedsize); |
7f77ea24 |
58 | me->nstates = me->statesize = me->statepos = 0; |
59 | me->states = NULL; |
be8d5aa1 |
60 | me->params = ourgame->default_params(); |
7f77ea24 |
61 | me->seed = NULL; |
6f2d8d7c |
62 | me->aux_info = NULL; |
5928817c |
63 | me->fresh_seed = FALSE; |
2ef96bd6 |
64 | me->drawstate = NULL; |
65 | me->oldstate = NULL; |
eb2ad6f1 |
66 | me->presets = NULL; |
67 | me->preset_names = NULL; |
68 | me->npresets = me->presetsize = 0; |
87ed82be |
69 | me->anim_time = me->anim_pos = 0.0F; |
70 | me->flash_time = me->flash_pos = 0.0F; |
c822de4a |
71 | me->dir = 0; |
74a4e547 |
72 | me->ui = NULL; |
6776a950 |
73 | me->pressed_mouse_button = 0; |
7f77ea24 |
74 | |
cbb5549e |
75 | sfree(randseed); |
76 | |
7f77ea24 |
77 | return me; |
78 | } |
79 | |
80 | void midend_free(midend_data *me) |
81 | { |
82 | sfree(me->states); |
83 | sfree(me->seed); |
6f2d8d7c |
84 | if (me->aux_info) |
85 | me->ourgame->free_aux_info(me->aux_info); |
be8d5aa1 |
86 | me->ourgame->free_params(me->params); |
7f77ea24 |
87 | sfree(me); |
88 | } |
89 | |
90 | void midend_size(midend_data *me, int *x, int *y) |
91 | { |
be8d5aa1 |
92 | me->ourgame->size(me->params, x, y); |
7f77ea24 |
93 | } |
94 | |
95 | void midend_set_params(midend_data *me, game_params *params) |
96 | { |
be8d5aa1 |
97 | me->ourgame->free_params(me->params); |
98 | me->params = me->ourgame->dup_params(params); |
7f77ea24 |
99 | } |
100 | |
5928817c |
101 | void midend_new_game(midend_data *me) |
7f77ea24 |
102 | { |
103 | while (me->nstates > 0) |
be8d5aa1 |
104 | me->ourgame->free_game(me->states[--me->nstates]); |
7f77ea24 |
105 | |
2ef96bd6 |
106 | if (me->drawstate) |
be8d5aa1 |
107 | me->ourgame->free_drawstate(me->drawstate); |
2ef96bd6 |
108 | |
7f77ea24 |
109 | assert(me->nstates == 0); |
110 | |
5928817c |
111 | if (!me->fresh_seed) { |
112 | sfree(me->seed); |
6f2d8d7c |
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); |
5928817c |
118 | } else |
119 | me->fresh_seed = FALSE; |
7f77ea24 |
120 | |
121 | ensure(me); |
be8d5aa1 |
122 | me->states[me->nstates++] = me->ourgame->new_game(me->params, me->seed); |
7f77ea24 |
123 | me->statepos = 1; |
be8d5aa1 |
124 | me->drawstate = me->ourgame->new_drawstate(me->states[0]); |
74a4e547 |
125 | if (me->ui) |
be8d5aa1 |
126 | me->ourgame->free_ui(me->ui); |
127 | me->ui = me->ourgame->new_ui(me->states[0]); |
6776a950 |
128 | me->pressed_mouse_button = 0; |
7f77ea24 |
129 | } |
130 | |
131 | void midend_restart_game(midend_data *me) |
132 | { |
133 | while (me->nstates > 1) |
be8d5aa1 |
134 | me->ourgame->free_game(me->states[--me->nstates]); |
7f77ea24 |
135 | me->statepos = me->nstates; |
be8d5aa1 |
136 | me->ourgame->free_ui(me->ui); |
137 | me->ui = me->ourgame->new_ui(me->states[0]); |
7f77ea24 |
138 | } |
139 | |
1482ee76 |
140 | static int midend_undo(midend_data *me) |
7f77ea24 |
141 | { |
1482ee76 |
142 | if (me->statepos > 1) { |
7f77ea24 |
143 | me->statepos--; |
c822de4a |
144 | me->dir = -1; |
1482ee76 |
145 | return 1; |
146 | } else |
147 | return 0; |
7f77ea24 |
148 | } |
149 | |
1482ee76 |
150 | static int midend_redo(midend_data *me) |
7f77ea24 |
151 | { |
1482ee76 |
152 | if (me->statepos < me->nstates) { |
7f77ea24 |
153 | me->statepos++; |
c822de4a |
154 | me->dir = +1; |
1482ee76 |
155 | return 1; |
156 | } else |
157 | return 0; |
7f77ea24 |
158 | } |
159 | |
87ed82be |
160 | static void midend_finish_move(midend_data *me) |
161 | { |
162 | float flashtime; |
163 | |
164 | if (me->oldstate || me->statepos > 1) { |
be8d5aa1 |
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); |
87ed82be |
169 | if (flashtime > 0) { |
170 | me->flash_pos = 0.0F; |
171 | me->flash_time = flashtime; |
172 | } |
173 | } |
174 | |
175 | if (me->oldstate) |
be8d5aa1 |
176 | me->ourgame->free_game(me->oldstate); |
87ed82be |
177 | me->oldstate = NULL; |
178 | me->anim_pos = me->anim_time = 0; |
c822de4a |
179 | me->dir = 0; |
87ed82be |
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 | |
dd216087 |
187 | static void midend_stop_anim(midend_data *me) |
7f77ea24 |
188 | { |
2ef96bd6 |
189 | if (me->oldstate || me->anim_time) { |
87ed82be |
190 | midend_finish_move(me); |
2ef96bd6 |
191 | midend_redraw(me); |
192 | } |
dd216087 |
193 | } |
194 | |
6776a950 |
195 | static int midend_really_process_key(midend_data *me, int x, int y, int button) |
dd216087 |
196 | { |
be8d5aa1 |
197 | game_state *oldstate = me->ourgame->dup_game(me->states[me->statepos - 1]); |
dd216087 |
198 | float anim_time; |
7f77ea24 |
199 | |
200 | if (button == 'n' || button == 'N' || button == '\x0E') { |
dd216087 |
201 | midend_stop_anim(me); |
5928817c |
202 | midend_new_game(me); |
2ef96bd6 |
203 | midend_redraw(me); |
204 | return 1; /* never animate */ |
7f77ea24 |
205 | } else if (button == 'r' || button == 'R') { |
dd216087 |
206 | midend_stop_anim(me); |
7f77ea24 |
207 | midend_restart_game(me); |
2ef96bd6 |
208 | midend_redraw(me); |
209 | return 1; /* never animate */ |
7f77ea24 |
210 | } else if (button == 'u' || button == 'u' || |
1482ee76 |
211 | button == '\x1A' || button == '\x1F') { |
dd216087 |
212 | midend_stop_anim(me); |
1482ee76 |
213 | if (!midend_undo(me)) |
214 | return 1; |
7f77ea24 |
215 | } else if (button == '\x12') { |
dd216087 |
216 | midend_stop_anim(me); |
1482ee76 |
217 | if (!midend_redo(me)) |
218 | return 1; |
7f77ea24 |
219 | } else if (button == 'q' || button == 'Q' || button == '\x11') { |
be8d5aa1 |
220 | me->ourgame->free_game(oldstate); |
2ef96bd6 |
221 | return 0; |
222 | } else { |
be8d5aa1 |
223 | game_state *s = me->ourgame->make_move(me->states[me->statepos-1], |
224 | me->ui, x, y, button); |
74a4e547 |
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) { |
dd216087 |
235 | midend_stop_anim(me); |
2ef96bd6 |
236 | while (me->nstates > me->statepos) |
be8d5aa1 |
237 | me->ourgame->free_game(me->states[--me->nstates]); |
2ef96bd6 |
238 | ensure(me); |
239 | me->states[me->nstates] = s; |
240 | me->statepos = ++me->nstates; |
c822de4a |
241 | me->dir = +1; |
2ef96bd6 |
242 | } else { |
be8d5aa1 |
243 | me->ourgame->free_game(oldstate); |
2ef96bd6 |
244 | return 1; |
245 | } |
7f77ea24 |
246 | } |
247 | |
2ef96bd6 |
248 | /* |
249 | * See if this move requires an animation. |
250 | */ |
be8d5aa1 |
251 | anim_time = me->ourgame->anim_length(oldstate, me->states[me->statepos-1], |
252 | me->dir); |
2ef96bd6 |
253 | |
87ed82be |
254 | me->oldstate = oldstate; |
2ef96bd6 |
255 | if (anim_time > 0) { |
2ef96bd6 |
256 | me->anim_time = anim_time; |
257 | } else { |
2ef96bd6 |
258 | me->anim_time = 0.0; |
87ed82be |
259 | midend_finish_move(me); |
7f77ea24 |
260 | } |
2ef96bd6 |
261 | me->anim_pos = 0.0; |
262 | |
263 | midend_redraw(me); |
264 | |
265 | activate_timer(me->frontend); |
7f77ea24 |
266 | |
267 | return 1; |
268 | } |
2ef96bd6 |
269 | |
6776a950 |
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 | * |
eeba2afe |
290 | * The possible ways in which things can go screwy in the front |
291 | * end are: |
6776a950 |
292 | * |
eeba2afe |
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. |
6776a950 |
297 | * |
eeba2afe |
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. |
6776a950 |
302 | * |
eeba2afe |
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. |
6776a950 |
330 | * |
6776a950 |
331 | */ |
332 | if (IS_MOUSE_DRAG(button) || IS_MOUSE_RELEASE(button)) { |
eeba2afe |
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); |
6776a950 |
340 | } |
eeba2afe |
341 | } else |
342 | return ret; /* ignore it */ |
6776a950 |
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 | |
2ef96bd6 |
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) { |
c822de4a |
374 | assert(me->dir != 0); |
be8d5aa1 |
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); |
2ef96bd6 |
378 | } else { |
be8d5aa1 |
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); |
2ef96bd6 |
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) { |
87ed82be |
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; |
2ef96bd6 |
398 | } |
87ed82be |
399 | if (me->flash_time == 0 && me->anim_time == 0) |
400 | deactivate_timer(me->frontend); |
2ef96bd6 |
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) { |
6f2d8d7c |
410 | game_aux_info *aux = NULL; |
411 | char *seed = me->ourgame->new_seed(me->params, me->random, &aux); |
be8d5aa1 |
412 | state = me->ourgame->new_game(me->params, seed); |
2ef96bd6 |
413 | sfree(seed); |
6f2d8d7c |
414 | if (aux) |
415 | me->ourgame->free_aux_info(aux); |
2ef96bd6 |
416 | } else |
417 | state = me->states[0]; |
418 | |
be8d5aa1 |
419 | ret = me->ourgame->colours(me->frontend, state, ncolours); |
2ef96bd6 |
420 | |
421 | if (me->nstates == 0) |
be8d5aa1 |
422 | me->ourgame->free_game(state); |
2ef96bd6 |
423 | |
424 | return ret; |
425 | } |
eb2ad6f1 |
426 | |
427 | int midend_num_presets(midend_data *me) |
428 | { |
429 | if (!me->npresets) { |
430 | char *name; |
431 | game_params *preset; |
432 | |
be8d5aa1 |
433 | while (me->ourgame->fetch_preset(me->npresets, &name, &preset)) { |
eb2ad6f1 |
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 | } |
fd1a1a2b |
458 | |
459 | int midend_wants_statusbar(midend_data *me) |
460 | { |
be8d5aa1 |
461 | return me->ourgame->wants_statusbar(); |
fd1a1a2b |
462 | } |
c8230524 |
463 | |
5928817c |
464 | config_item *midend_get_config(midend_data *me, int which, char **wintitle) |
c8230524 |
465 | { |
b0e26073 |
466 | char *titlebuf, *parstr; |
5928817c |
467 | config_item *ret; |
468 | |
be8d5aa1 |
469 | titlebuf = snewn(40 + strlen(me->ourgame->name), char); |
5928817c |
470 | |
471 | switch (which) { |
472 | case CFG_SETTINGS: |
be8d5aa1 |
473 | sprintf(titlebuf, "%s configuration", me->ourgame->name); |
5928817c |
474 | *wintitle = dupstr(titlebuf); |
be8d5aa1 |
475 | return me->ourgame->configure(me->params); |
5928817c |
476 | case CFG_SEED: |
be8d5aa1 |
477 | sprintf(titlebuf, "%s game selection", me->ourgame->name); |
5928817c |
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; |
b0e26073 |
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 | */ |
be8d5aa1 |
490 | parstr = me->ourgame->encode_params(me->params); |
b0e26073 |
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); |
5928817c |
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; |
c8230524 |
504 | } |
505 | |
8b7938e7 |
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) { |
be8d5aa1 |
536 | params = me->ourgame->decode_params(par); |
537 | error = me->ourgame->validate_params(params); |
8b7938e7 |
538 | if (error) { |
be8d5aa1 |
539 | me->ourgame->free_params(params); |
8b7938e7 |
540 | return error; |
541 | } |
be8d5aa1 |
542 | me->ourgame->free_params(me->params); |
8b7938e7 |
543 | me->params = params; |
544 | } |
545 | |
546 | if (seed) { |
be8d5aa1 |
547 | error = me->ourgame->validate_seed(me->params, seed); |
8b7938e7 |
548 | if (error) |
549 | return error; |
550 | |
551 | sfree(me->seed); |
552 | me->seed = dupstr(seed); |
553 | me->fresh_seed = TRUE; |
6f2d8d7c |
554 | if (me->aux_info) |
555 | me->ourgame->free_aux_info(me->aux_info); |
556 | me->aux_info = NULL; |
8b7938e7 |
557 | } |
558 | |
559 | return NULL; |
560 | } |
561 | |
5928817c |
562 | char *midend_set_config(midend_data *me, int which, config_item *cfg) |
c8230524 |
563 | { |
8b7938e7 |
564 | char *error; |
c8230524 |
565 | game_params *params; |
566 | |
5928817c |
567 | switch (which) { |
568 | case CFG_SETTINGS: |
be8d5aa1 |
569 | params = me->ourgame->custom_params(cfg); |
570 | error = me->ourgame->validate_params(params); |
c8230524 |
571 | |
5928817c |
572 | if (error) { |
be8d5aa1 |
573 | me->ourgame->free_params(params); |
5928817c |
574 | return error; |
575 | } |
c8230524 |
576 | |
be8d5aa1 |
577 | me->ourgame->free_params(me->params); |
5928817c |
578 | me->params = params; |
579 | break; |
580 | |
581 | case CFG_SEED: |
8b7938e7 |
582 | error = midend_game_id(me, cfg[0].sval, TRUE); |
5928817c |
583 | if (error) |
584 | return error; |
5928817c |
585 | break; |
586 | } |
c8230524 |
587 | |
588 | return NULL; |
589 | } |
9b4b03d3 |
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 | } |
2ac6d24e |
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 | } |