chiark / gitweb /
Forbid undo of new-game if it would change the params.
[sgt-puzzles.git] / puzzles.h
1 /*
2  * puzzles.h: header file for my puzzle collection
3  */
4
5 #ifndef PUZZLES_PUZZLES_H
6 #define PUZZLES_PUZZLES_H
7
8 #include <stdio.h>  /* for FILE */
9 #include <stdlib.h> /* for size_t */
10 #include <limits.h> /* for UINT_MAX */
11
12 #ifndef TRUE
13 #define TRUE 1
14 #endif
15 #ifndef FALSE
16 #define FALSE 0
17 #endif
18
19 #define PI 3.141592653589793238462643383279502884197169399
20
21 #define lenof(array) ( sizeof(array) / sizeof(*(array)) )
22
23 #define STR_INT(x) #x
24 #define STR(x) STR_INT(x)
25
26 /* NB not perfect because they evaluate arguments multiple times. */
27 #ifndef max
28 #define max(x,y) ( (x)>(y) ? (x) : (y) )
29 #endif /* max */
30 #ifndef min
31 #define min(x,y) ( (x)<(y) ? (x) : (y) )
32 #endif /* min */
33
34 enum {
35     LEFT_BUTTON = 0x0200,
36     MIDDLE_BUTTON,
37     RIGHT_BUTTON,
38     LEFT_DRAG,
39     MIDDLE_DRAG,
40     RIGHT_DRAG,
41     LEFT_RELEASE,
42     MIDDLE_RELEASE,
43     RIGHT_RELEASE,
44     CURSOR_UP,
45     CURSOR_DOWN,
46     CURSOR_LEFT,
47     CURSOR_RIGHT,
48     CURSOR_SELECT,
49     CURSOR_SELECT2,
50     /* UI_* are special keystrokes generated by front ends in response
51      * to menu actions, never passed to back ends */
52     UI_LOWER_BOUND,
53     UI_QUIT,
54     UI_NEWGAME,
55     UI_SOLVE,
56     UI_UNDO,
57     UI_REDO,
58     UI_UPPER_BOUND,
59     
60     /* made smaller because of 'limited range of datatype' errors. */
61     MOD_CTRL       = 0x1000,
62     MOD_SHFT       = 0x2000,
63     MOD_NUM_KEYPAD = 0x4000,
64     MOD_MASK       = 0x7000 /* mask for all modifiers */
65 };
66
67 #define IS_MOUSE_DOWN(m) ( (unsigned)((m) - LEFT_BUTTON) <= \
68                                (unsigned)(RIGHT_BUTTON - LEFT_BUTTON))
69 #define IS_MOUSE_DRAG(m) ( (unsigned)((m) - LEFT_DRAG) <= \
70                                (unsigned)(RIGHT_DRAG - LEFT_DRAG))
71 #define IS_MOUSE_RELEASE(m) ( (unsigned)((m) - LEFT_RELEASE) <= \
72                                (unsigned)(RIGHT_RELEASE - LEFT_RELEASE))
73 #define IS_CURSOR_MOVE(m) ( (m) == CURSOR_UP || (m) == CURSOR_DOWN || \
74                             (m) == CURSOR_RIGHT || (m) == CURSOR_LEFT )
75 #define IS_CURSOR_SELECT(m) ( (m) == CURSOR_SELECT || (m) == CURSOR_SELECT2)
76 #define IS_UI_FAKE_KEY(m) ( (m) > UI_LOWER_BOUND && (m) < UI_UPPER_BOUND )
77
78 /*
79  * Flags in the back end's `flags' word.
80  */
81 /* Bit flags indicating mouse button priorities */
82 #define BUTTON_BEATS(x,y) ( 1 << (((x)-LEFT_BUTTON)*3+(y)-LEFT_BUTTON) )
83 /* Flag indicating that Solve operations should be animated */
84 #define SOLVE_ANIMATES ( 1 << 9 )
85 /* Pocket PC: Game requires right mouse button emulation */
86 #define REQUIRE_RBUTTON ( 1 << 10 )
87 /* Pocket PC: Game requires numeric input */
88 #define REQUIRE_NUMPAD ( 1 << 11 )
89 /* end of `flags' word definitions */
90
91 #ifdef _WIN32_WCE
92   /* Pocket PC devices have small, portrait screen that requires more vivid colours */
93   #define SMALL_SCREEN
94   #define PORTRAIT_SCREEN
95   #define VIVID_COLOURS
96   #define STYLUS_BASED
97 #endif
98
99 #define IGNOREARG(x) ( (x) = (x) )
100
101 typedef struct frontend frontend;
102 typedef struct config_item config_item;
103 typedef struct midend midend;
104 typedef struct random_state random_state;
105 typedef struct game_params game_params;
106 typedef struct game_state game_state;
107 typedef struct game_ui game_ui;
108 typedef struct game_drawstate game_drawstate;
109 typedef struct game game;
110 typedef struct blitter blitter;
111 typedef struct document document;
112 typedef struct drawing_api drawing_api;
113 typedef struct drawing drawing;
114 typedef struct psdata psdata;
115
116 #define ALIGN_VNORMAL 0x000
117 #define ALIGN_VCENTRE 0x100
118
119 #define ALIGN_HLEFT   0x000
120 #define ALIGN_HCENTRE 0x001
121 #define ALIGN_HRIGHT  0x002
122
123 #define FONT_FIXED    0
124 #define FONT_VARIABLE 1
125
126 /* For printing colours */
127 #define HATCH_SLASH     1
128 #define HATCH_BACKSLASH 2
129 #define HATCH_HORIZ     3
130 #define HATCH_VERT      4
131 #define HATCH_PLUS      5
132 #define HATCH_X         6
133
134 /*
135  * Structure used to pass configuration data between frontend and
136  * game
137  */
138 enum { C_STRING, C_CHOICES, C_BOOLEAN, C_END };
139 struct config_item {
140     /*
141      * `name' is never dynamically allocated.
142      */
143     char *name;
144     /*
145      * `type' contains one of the above values.
146      */
147     int type;
148     /*
149      * For C_STRING, `sval' is always dynamically allocated and
150      * non-NULL. For C_BOOLEAN and C_END, `sval' is always NULL.
151      * For C_CHOICES, `sval' is non-NULL, _not_ dynamically
152      * allocated, and contains a set of option strings separated by
153      * a delimiter. The delimeter is also the first character in
154      * the string, so for example ":Foo:Bar:Baz" gives three
155      * options `Foo', `Bar' and `Baz'.
156      */
157     char *sval;
158     /*
159      * For C_BOOLEAN, this is TRUE or FALSE. For C_CHOICES, it
160      * indicates the chosen index from the `sval' list. In the
161      * above example, 0==Foo, 1==Bar and 2==Baz.
162      */
163     int ival;
164 };
165
166 /*
167  * Structure used to communicate the presets menu from midend to
168  * frontend. In principle, it's also used to pass the same information
169  * from game to midend, though games that don't specify a menu
170  * hierarchy (i.e. most of them) will use the simpler fetch_preset()
171  * function to return an unstructured list.
172  *
173  * A tree of these structures always belongs to the midend, and only
174  * the midend should ever need to free it. The front end should treat
175  * them as read-only.
176  */
177 struct preset_menu_entry {
178     char *title;
179     /* Exactly one of the next two fields is NULL, depending on
180      * whether this entry is a submenu title or an actual preset */
181     game_params *params;
182     struct preset_menu *submenu;
183     /* Every preset menu entry has a number allocated by the mid-end,
184      * so that midend_which_preset() can return a value that
185      * identifies an entry anywhere in the menu hierarchy. The values
186      * will be allocated reasonably densely from 1 upwards (so it's
187      * reasonable for the front end to use them as array indices if it
188      * needs to store GUI state per menu entry), but no other
189      * guarantee is given about their ordering.
190      *
191      * Entries containing submenus have ids too - not only the actual
192      * presets are numbered. */
193     int id;
194 };
195 struct preset_menu {
196     int n_entries;             /* number of entries actually in use */
197     int entries_size;          /* space currently allocated in this array */
198     struct preset_menu_entry *entries;
199 };
200 /* For games which do want to directly return a tree of these, here
201  * are convenience routines (in midend.c) for constructing one. These
202  * assume that 'title' and 'encoded_params' are already dynamically
203  * allocated by the caller; the resulting preset_menu tree takes
204  * ownership of them. */
205 struct preset_menu *preset_menu_new(void);
206 struct preset_menu *preset_menu_add_submenu(struct preset_menu *parent,
207                                             char *title);
208 void preset_menu_add_preset(struct preset_menu *menu,
209                             char *title, game_params *params);
210 /* Helper routine front ends can use for one of the ways they might
211  * want to organise their preset menu usage */
212 game_params *preset_menu_lookup_by_id(struct preset_menu *menu, int id);
213
214 /*
215  * Platform routines
216  */
217
218 /* We can't use #ifdef DEBUG, because Cygwin defines it by default. */
219 #ifdef DEBUGGING
220 #define debug(x) (debug_printf x)
221 void debug_printf(char *fmt, ...);
222 #else
223 #define debug(x)
224 #endif
225
226 void fatal(char *fmt, ...);
227 void frontend_default_colour(frontend *fe, float *output);
228 void deactivate_timer(frontend *fe);
229 void activate_timer(frontend *fe);
230 void get_random_seed(void **randseed, int *randseedsize);
231
232 /*
233  * drawing.c
234  */
235 drawing *drawing_new(const drawing_api *api, midend *me, void *handle);
236 void drawing_free(drawing *dr);
237 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
238                int align, int colour, char *text);
239 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour);
240 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour);
241 void draw_polygon(drawing *dr, int *coords, int npoints,
242                   int fillcolour, int outlinecolour);
243 void draw_circle(drawing *dr, int cx, int cy, int radius,
244                  int fillcolour, int outlinecolour);
245 void draw_thick_line(drawing *dr, float thickness,
246                      float x1, float y1, float x2, float y2, int colour);
247 void clip(drawing *dr, int x, int y, int w, int h);
248 void unclip(drawing *dr);
249 void start_draw(drawing *dr);
250 void draw_update(drawing *dr, int x, int y, int w, int h);
251 void end_draw(drawing *dr);
252 char *text_fallback(drawing *dr, const char *const *strings, int nstrings);
253 void status_bar(drawing *dr, char *text);
254 blitter *blitter_new(drawing *dr, int w, int h);
255 void blitter_free(drawing *dr, blitter *bl);
256 /* save puts the portion of the current display with top-left corner
257  * (x,y) to the blitter. load puts it back again to the specified
258  * coords, or else wherever it was saved from
259  * (if x = y = BLITTER_FROMSAVED). */
260 void blitter_save(drawing *dr, blitter *bl, int x, int y);
261 #define BLITTER_FROMSAVED (-1)
262 void blitter_load(drawing *dr, blitter *bl, int x, int y);
263 void print_begin_doc(drawing *dr, int pages);
264 void print_begin_page(drawing *dr, int number);
265 void print_begin_puzzle(drawing *dr, float xm, float xc,
266                         float ym, float yc, int pw, int ph, float wmm,
267                         float scale);
268 void print_end_puzzle(drawing *dr);
269 void print_end_page(drawing *dr, int number);
270 void print_end_doc(drawing *dr);
271 void print_get_colour(drawing *dr, int colour, int printing_in_colour,
272                       int *hatch, float *r, float *g, float *b);
273 int print_mono_colour(drawing *dr, int grey); /* 0==black, 1==white */
274 int print_grey_colour(drawing *dr, float grey);
275 int print_hatched_colour(drawing *dr, int hatch);
276 int print_rgb_mono_colour(drawing *dr, float r, float g, float b, int mono);
277 int print_rgb_grey_colour(drawing *dr, float r, float g, float b, float grey);
278 int print_rgb_hatched_colour(drawing *dr, float r, float g, float b,
279                              int hatch);
280 void print_line_width(drawing *dr, int width);
281 void print_line_dotted(drawing *dr, int dotted);
282
283 /*
284  * midend.c
285  */
286 midend *midend_new(frontend *fe, const game *ourgame,
287                    const drawing_api *drapi, void *drhandle);
288 void midend_free(midend *me);
289 const game *midend_which_game(midend *me);
290 void midend_set_params(midend *me, game_params *params);
291 game_params *midend_get_params(midend *me);
292 void midend_size(midend *me, int *x, int *y, int user_size);
293 void midend_reset_tilesize(midend *me);
294 void midend_new_game(midend *me);
295 void midend_restart_game(midend *me);
296 void midend_stop_anim(midend *me);
297 int midend_process_key(midend *me, int x, int y, int button);
298 void midend_force_redraw(midend *me);
299 void midend_redraw(midend *me);
300 float *midend_colours(midend *me, int *ncolours);
301 void midend_freeze_timer(midend *me, float tprop);
302 void midend_timer(midend *me, float tplus);
303 struct preset_menu *midend_get_presets(midend *me, int *id_limit);
304 int midend_which_preset(midend *me);
305 int midend_wants_statusbar(midend *me);
306 enum { CFG_SETTINGS, CFG_SEED, CFG_DESC, CFG_FRONTEND_SPECIFIC };
307 config_item *midend_get_config(midend *me, int which, char **wintitle);
308 char *midend_set_config(midend *me, int which, config_item *cfg);
309 char *midend_game_id(midend *me, char *id);
310 char *midend_get_game_id(midend *me);
311 char *midend_get_random_seed(midend *me);
312 int midend_can_format_as_text_now(midend *me);
313 char *midend_text_format(midend *me);
314 char *midend_solve(midend *me);
315 int midend_status(midend *me);
316 int midend_can_undo(midend *me);
317 int midend_can_redo(midend *me);
318 void midend_supersede_game_desc(midend *me, char *desc, char *privdesc);
319 char *midend_rewrite_statusbar(midend *me, char *text);
320 void midend_serialise(midend *me,
321                       void (*write)(void *ctx, void *buf, int len),
322                       void *wctx);
323 char *midend_deserialise(midend *me,
324                          int (*read)(void *ctx, void *buf, int len),
325                          void *rctx);
326 char *identify_game(char **name, int (*read)(void *ctx, void *buf, int len),
327                     void *rctx);
328 void midend_request_id_changes(midend *me, void (*notify)(void *), void *ctx);
329 /* Printing functions supplied by the mid-end */
330 char *midend_print_puzzle(midend *me, document *doc, int with_soln);
331 int midend_tilesize(midend *me);
332
333 /*
334  * malloc.c
335  */
336 void *smalloc(size_t size);
337 void *srealloc(void *p, size_t size);
338 void sfree(void *p);
339 char *dupstr(const char *s);
340 #define snew(type) \
341     ( (type *) smalloc (sizeof (type)) )
342 #define snewn(number, type) \
343     ( (type *) smalloc ((number) * sizeof (type)) )
344 #define sresize(array, number, type) \
345     ( (type *) srealloc ((array), (number) * sizeof (type)) )
346
347 /*
348  * misc.c
349  */
350 void free_cfg(config_item *cfg);
351 void obfuscate_bitmap(unsigned char *bmp, int bits, int decode);
352
353 /* allocates output each time. len is always in bytes of binary data.
354  * May assert (or just go wrong) if lengths are unchecked. */
355 char *bin2hex(const unsigned char *in, int inlen);
356 unsigned char *hex2bin(const char *in, int outlen);
357
358 /* Sets (and possibly dims) background from frontend default colour,
359  * and auto-generates highlight and lowlight colours too. */
360 void game_mkhighlight(frontend *fe, float *ret,
361                       int background, int highlight, int lowlight);
362 /* As above, but starts from a provided background colour rather
363  * than the frontend default. */
364 void game_mkhighlight_specific(frontend *fe, float *ret,
365                                int background, int highlight, int lowlight);
366
367 /* Randomly shuffles an array of items. */
368 void shuffle(void *array, int nelts, int eltsize, random_state *rs);
369
370 /* Draw a rectangle outline, using the drawing API's draw_line. */
371 void draw_rect_outline(drawing *dr, int x, int y, int w, int h,
372                        int colour);
373
374 /* Draw a set of rectangle corners (e.g. for a cursor display). */
375 void draw_rect_corners(drawing *dr, int cx, int cy, int r, int col);
376
377 void move_cursor(int button, int *x, int *y, int maxw, int maxh, int wrap);
378
379 /* Used in netslide.c and sixteen.c for cursor movement around edge. */
380 int c2pos(int w, int h, int cx, int cy);
381 int c2diff(int w, int h, int cx, int cy, int button);
382 void pos2c(int w, int h, int pos, int *cx, int *cy);
383
384 /* Draws text with an 'outline' formed by offsetting the text
385  * by one pixel; useful for highlighting. Outline is omitted if -1. */
386 void draw_text_outline(drawing *dr, int x, int y, int fonttype,
387                        int fontsize, int align,
388                        int text_colour, int outline_colour, char *text);
389
390 /* Copies text left-justified with spaces. Length of string must be
391  * less than buffer size. */
392 void copy_left_justified(char *buf, size_t sz, const char *str);
393
394 /*
395  * dsf.c
396  */
397 int *snew_dsf(int size);
398
399 void print_dsf(int *dsf, int size);
400
401 /* Return the canonical element of the equivalence class containing element
402  * val.  If 'inverse' is non-NULL, this function will put into it a flag
403  * indicating whether the canonical element is inverse to val. */
404 int edsf_canonify(int *dsf, int val, int *inverse);
405 int dsf_canonify(int *dsf, int val);
406 int dsf_size(int *dsf, int val);
407
408 /* Allow the caller to specify that two elements should be in the same
409  * equivalence class.  If 'inverse' is TRUE, the elements are actually opposite
410  * to one another in some sense.  This function will fail an assertion if the
411  * caller gives it self-contradictory data, ie if two elements are claimed to
412  * be both opposite and non-opposite. */
413 void edsf_merge(int *dsf, int v1, int v2, int inverse);
414 void dsf_merge(int *dsf, int v1, int v2);
415 void dsf_init(int *dsf, int len);
416
417 /*
418  * tdq.c
419  */
420
421 /*
422  * Data structure implementing a 'to-do queue', a simple
423  * de-duplicating to-do list mechanism.
424  *
425  * Specification: a tdq is a queue which can hold integers from 0 to
426  * n-1, where n was some constant specified at tdq creation time. No
427  * integer may appear in the queue's current contents more than once;
428  * an attempt to add an already-present integer again will do nothing,
429  * so that that integer is removed from the queue at the position
430  * where it was _first_ inserted. The add and remove operations take
431  * constant time.
432  *
433  * The idea is that you might use this in applications like solvers:
434  * keep a tdq listing the indices of grid squares that you currently
435  * need to process in some way. Whenever you modify a square in a way
436  * that will require you to re-scan its neighbours, add them to the
437  * list with tdq_add; meanwhile you're constantly taking elements off
438  * the list when you need another square to process. In solvers where
439  * deductions are mostly localised, this should prevent repeated
440  * O(N^2) loops over the whole grid looking for something to do. (But
441  * if only _most_ of the deductions are localised, then you should
442  * respond to an empty to-do list by re-adding everything using
443  * tdq_fill, so _then_ you rescan the whole grid looking for newly
444  * enabled non-local deductions. Only if you've done that and emptied
445  * the list again finding nothing new to do are you actually done.)
446  */
447 typedef struct tdq tdq;
448 tdq *tdq_new(int n);
449 void tdq_free(tdq *tdq);
450 void tdq_add(tdq *tdq, int k);
451 int tdq_remove(tdq *tdq);        /* returns -1 if nothing available */
452 void tdq_fill(tdq *tdq);         /* add everything to the tdq at once */
453
454 /*
455  * laydomino.c
456  */
457 int *domino_layout(int w, int h, random_state *rs);
458 void domino_layout_prealloc(int w, int h, random_state *rs,
459                             int *grid, int *grid2, int *list);
460 /*
461  * version.c
462  */
463 extern char ver[];
464
465 /*
466  * random.c
467  */
468 random_state *random_new(const char *seed, int len);
469 random_state *random_copy(random_state *tocopy);
470 unsigned long random_bits(random_state *state, int bits);
471 unsigned long random_upto(random_state *state, unsigned long limit);
472 void random_free(random_state *state);
473 char *random_state_encode(random_state *state);
474 random_state *random_state_decode(const char *input);
475 /* random.c also exports SHA, which occasionally comes in useful. */
476 #if __STDC_VERSION__ >= 199901L
477 #include <stdint.h>
478 typedef uint32_t uint32;
479 #elif UINT_MAX >= 4294967295L
480 typedef unsigned int uint32;
481 #else
482 typedef unsigned long uint32;
483 #endif
484 typedef struct {
485     uint32 h[5];
486     unsigned char block[64];
487     int blkused;
488     uint32 lenhi, lenlo;
489 } SHA_State;
490 void SHA_Init(SHA_State *s);
491 void SHA_Bytes(SHA_State *s, const void *p, int len);
492 void SHA_Final(SHA_State *s, unsigned char *output);
493 void SHA_Simple(const void *p, int len, unsigned char *output);
494
495 /*
496  * printing.c
497  */
498 document *document_new(int pw, int ph, float userscale);
499 void document_free(document *doc);
500 void document_add_puzzle(document *doc, const game *game, game_params *par,
501                          game_state *st, game_state *st2);
502 void document_print(document *doc, drawing *dr);
503
504 /*
505  * ps.c
506  */
507 psdata *ps_init(FILE *outfile, int colour);
508 void ps_free(psdata *ps);
509 drawing *ps_drawing_api(psdata *ps);
510
511 /*
512  * combi.c: provides a structure and functions for iterating over
513  * combinations (i.e. choosing r things out of n).
514  */
515 typedef struct _combi_ctx {
516   int r, n, nleft, total;
517   int *a;
518 } combi_ctx;
519
520 combi_ctx *new_combi(int r, int n);
521 void reset_combi(combi_ctx *combi);
522 combi_ctx *next_combi(combi_ctx *combi); /* returns NULL for end */
523 void free_combi(combi_ctx *combi);
524
525 /*
526  * divvy.c
527  */
528 /* divides w*h rectangle into pieces of size k. Returns w*h dsf. */
529 int *divvy_rectangle(int w, int h, int k, random_state *rs);
530
531 /*
532  * findloop.c
533  */
534 struct findloopstate;
535 struct findloopstate *findloop_new_state(int nvertices);
536 void findloop_free_state(struct findloopstate *);
537 /*
538  * Callback provided by the client code to enumerate the graph
539  * vertices joined directly to a given vertex.
540  *
541  * Semantics: if vertex >= 0, return one of its neighbours; if vertex
542  * < 0, return a previously unmentioned neighbour of whatever vertex
543  * was last passed as input. Write to 'ctx' as necessary to store
544  * state. In either case, return < 0 if no such vertex can be found.
545  */
546 typedef int (*neighbour_fn_t)(int vertex, void *ctx);
547 /*
548  * Actual function to find loops. 'ctx' will be passed unchanged to
549  * the 'neighbour' function to query graph edges. Returns FALSE if no
550  * loop was found, or TRUE if one was.
551  */
552 int findloop_run(struct findloopstate *state, int nvertices,
553                  neighbour_fn_t neighbour, void *ctx);
554 /*
555  * Query whether an edge is part of a loop, in the output of
556  * find_loops.
557  *
558  * Due to the internal storage format, if you pass u,v which are not
559  * connected at all, the output will be TRUE. (The algorithm actually
560  * stores an exhaustive list of *non*-loop edges, because there are
561  * fewer of those, so really it's querying whether the edge is on that
562  * list.)
563  */
564 int findloop_is_loop_edge(struct findloopstate *state, int u, int v);
565
566 /*
567  * Data structure containing the function calls and data specific
568  * to a particular game. This is enclosed in a data structure so
569  * that a particular platform can choose, if it wishes, to compile
570  * all the games into a single combined executable rather than
571  * having lots of little ones.
572  */
573 struct game {
574     const char *name;
575     const char *winhelp_topic, *htmlhelp_topic;
576     game_params *(*default_params)(void);
577     int (*fetch_preset)(int i, char **name, game_params **params);
578     struct preset_menu *(*preset_menu)(void);
579     void (*decode_params)(game_params *, char const *string);
580     char *(*encode_params)(const game_params *, int full);
581     void (*free_params)(game_params *params);
582     game_params *(*dup_params)(const game_params *params);
583     int can_configure;
584     config_item *(*configure)(const game_params *params);
585     game_params *(*custom_params)(const config_item *cfg);
586     char *(*validate_params)(const game_params *params, int full);
587     char *(*new_desc)(const game_params *params, random_state *rs,
588                       char **aux, int interactive);
589     char *(*validate_desc)(const game_params *params, const char *desc);
590     game_state *(*new_game)(midend *me, const game_params *params,
591                             const char *desc);
592     game_state *(*dup_game)(const game_state *state);
593     void (*free_game)(game_state *state);
594     int can_solve;
595     char *(*solve)(const game_state *orig, const game_state *curr,
596                    const char *aux, char **error);
597     int can_format_as_text_ever;
598     int (*can_format_as_text_now)(const game_params *params);
599     char *(*text_format)(const game_state *state);
600     game_ui *(*new_ui)(const game_state *state);
601     void (*free_ui)(game_ui *ui);
602     char *(*encode_ui)(const game_ui *ui);
603     void (*decode_ui)(game_ui *ui, const char *encoding);
604     void (*changed_state)(game_ui *ui, const game_state *oldstate,
605                           const game_state *newstate);
606     char *(*interpret_move)(const game_state *state, game_ui *ui,
607                             const game_drawstate *ds, int x, int y, int button);
608     game_state *(*execute_move)(const game_state *state, const char *move);
609     int preferred_tilesize;
610     void (*compute_size)(const game_params *params, int tilesize,
611                          int *x, int *y);
612     void (*set_size)(drawing *dr, game_drawstate *ds,
613                      const game_params *params, int tilesize);
614     float *(*colours)(frontend *fe, int *ncolours);
615     game_drawstate *(*new_drawstate)(drawing *dr, const game_state *state);
616     void (*free_drawstate)(drawing *dr, game_drawstate *ds);
617     void (*redraw)(drawing *dr, game_drawstate *ds, const game_state *oldstate,
618                    const game_state *newstate, int dir, const game_ui *ui,
619                    float anim_time, float flash_time);
620     float (*anim_length)(const game_state *oldstate,
621                          const game_state *newstate, int dir, game_ui *ui);
622     float (*flash_length)(const game_state *oldstate,
623                           const game_state *newstate, int dir, game_ui *ui);
624     int (*status)(const game_state *state);
625     int can_print, can_print_in_colour;
626     void (*print_size)(const game_params *params, float *x, float *y);
627     void (*print)(drawing *dr, const game_state *state, int tilesize);
628     int wants_statusbar;
629     int is_timed;
630     int (*timing_state)(const game_state *state, game_ui *ui);
631     int flags;
632 };
633
634 /*
635  * Data structure containing the drawing API implemented by the
636  * front end and also by cross-platform printing modules such as
637  * PostScript.
638  */
639 struct drawing_api {
640     void (*draw_text)(void *handle, int x, int y, int fonttype, int fontsize,
641                       int align, int colour, char *text);
642     void (*draw_rect)(void *handle, int x, int y, int w, int h, int colour);
643     void (*draw_line)(void *handle, int x1, int y1, int x2, int y2,
644                       int colour);
645     void (*draw_polygon)(void *handle, int *coords, int npoints,
646                          int fillcolour, int outlinecolour);
647     void (*draw_circle)(void *handle, int cx, int cy, int radius,
648                         int fillcolour, int outlinecolour);
649     void (*draw_update)(void *handle, int x, int y, int w, int h);
650     void (*clip)(void *handle, int x, int y, int w, int h);
651     void (*unclip)(void *handle);
652     void (*start_draw)(void *handle);
653     void (*end_draw)(void *handle);
654     void (*status_bar)(void *handle, char *text);
655     blitter *(*blitter_new)(void *handle, int w, int h);
656     void (*blitter_free)(void *handle, blitter *bl);
657     void (*blitter_save)(void *handle, blitter *bl, int x, int y);
658     void (*blitter_load)(void *handle, blitter *bl, int x, int y);
659     void (*begin_doc)(void *handle, int pages);
660     void (*begin_page)(void *handle, int number);
661     void (*begin_puzzle)(void *handle, float xm, float xc,
662                          float ym, float yc, int pw, int ph, float wmm);
663     void (*end_puzzle)(void *handle);
664     void (*end_page)(void *handle, int number);
665     void (*end_doc)(void *handle);
666     void (*line_width)(void *handle, float width);
667     void (*line_dotted)(void *handle, int dotted);
668     char *(*text_fallback)(void *handle, const char *const *strings,
669                            int nstrings);
670     void (*draw_thick_line)(void *handle, float thickness,
671                             float x1, float y1, float x2, float y2,
672                             int colour);
673 };
674
675 /*
676  * For one-game-at-a-time platforms, there's a single structure
677  * like the above, under a fixed name. For all-at-once platforms,
678  * there's a list of all available puzzles in array form.
679  */
680 #ifdef COMBINED
681 extern const game *gamelist[];
682 extern const int gamecount;
683 #else
684 extern const game thegame;
685 #endif
686
687 /* A little bit of help to lazy developers */
688 #define DEFAULT_STATUSBAR_TEXT "Use status_bar() to fill this in."
689
690 #endif /* PUZZLES_PUZZLES_H */