Extra utility function.
[sgt/puzzles] / 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
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14 #ifndef FALSE
15 #define FALSE 0
16 #endif
17
18 #define PI 3.141592653589793238462643383279502884197169399
19
20 #define lenof(array) ( sizeof(array) / sizeof(*(array)) )
21
22 #define STR_INT(x) #x
23 #define STR(x) STR_INT(x)
24
25 /* NB not perfect because they evaluate arguments multiple times. */
26 #ifndef max
27 #define max(x,y) ( (x)>(y) ? (x) : (y) )
28 #endif /* max */
29 #ifndef min
30 #define min(x,y) ( (x)<(y) ? (x) : (y) )
31 #endif /* min */
32
33 enum {
34 LEFT_BUTTON = 0x0200,
35 MIDDLE_BUTTON,
36 RIGHT_BUTTON,
37 LEFT_DRAG,
38 MIDDLE_DRAG,
39 RIGHT_DRAG,
40 LEFT_RELEASE,
41 MIDDLE_RELEASE,
42 RIGHT_RELEASE,
43 CURSOR_UP,
44 CURSOR_DOWN,
45 CURSOR_LEFT,
46 CURSOR_RIGHT,
47 CURSOR_SELECT,
48
49 /* made smaller because of 'limited range of datatype' errors. */
50 MOD_CTRL = 0x1000,
51 MOD_SHFT = 0x2000,
52 MOD_NUM_KEYPAD = 0x4000,
53 MOD_MASK = 0x7000 /* mask for all modifiers */
54 };
55
56 #define IS_MOUSE_DOWN(m) ( (unsigned)((m) - LEFT_BUTTON) <= \
57 (unsigned)(RIGHT_BUTTON - LEFT_BUTTON))
58 #define IS_MOUSE_DRAG(m) ( (unsigned)((m) - LEFT_DRAG) <= \
59 (unsigned)(RIGHT_DRAG - LEFT_DRAG))
60 #define IS_MOUSE_RELEASE(m) ( (unsigned)((m) - LEFT_RELEASE) <= \
61 (unsigned)(RIGHT_RELEASE - LEFT_RELEASE))
62
63 /*
64 * Flags in the back end's `flags' word.
65 */
66 /* Bit flags indicating mouse button priorities */
67 #define BUTTON_BEATS(x,y) ( 1 << (((x)-LEFT_BUTTON)*3+(y)-LEFT_BUTTON) )
68 /* Flag indicating that Solve operations should be animated */
69 #define SOLVE_ANIMATES ( 1 << 9 )
70 /* end of `flags' word definitions */
71
72 #define IGNOREARG(x) ( (x) = (x) )
73
74 typedef struct frontend frontend;
75 typedef struct config_item config_item;
76 typedef struct midend midend;
77 typedef struct random_state random_state;
78 typedef struct game_params game_params;
79 typedef struct game_state game_state;
80 typedef struct game_ui game_ui;
81 typedef struct game_drawstate game_drawstate;
82 typedef struct game game;
83 typedef struct blitter blitter;
84 typedef struct document document;
85 typedef struct drawing_api drawing_api;
86 typedef struct drawing drawing;
87 typedef struct psdata psdata;
88
89 #define ALIGN_VNORMAL 0x000
90 #define ALIGN_VCENTRE 0x100
91
92 #define ALIGN_HLEFT 0x000
93 #define ALIGN_HCENTRE 0x001
94 #define ALIGN_HRIGHT 0x002
95
96 #define FONT_FIXED 0
97 #define FONT_VARIABLE 1
98
99 /* For printing colours */
100 #define HATCH_SOLID 0
101 #define HATCH_CLEAR 1
102 #define HATCH_SLASH 2
103 #define HATCH_BACKSLASH 3
104 #define HATCH_HORIZ 4
105 #define HATCH_VERT 5
106 #define HATCH_PLUS 6
107 #define HATCH_X 7
108
109 /*
110 * Structure used to pass configuration data between frontend and
111 * game
112 */
113 enum { C_STRING, C_CHOICES, C_BOOLEAN, C_END };
114 struct config_item {
115 /*
116 * `name' is never dynamically allocated.
117 */
118 char *name;
119 /*
120 * `type' contains one of the above values.
121 */
122 int type;
123 /*
124 * For C_STRING, `sval' is always dynamically allocated and
125 * non-NULL. For C_BOOLEAN and C_END, `sval' is always NULL.
126 * For C_CHOICES, `sval' is non-NULL, _not_ dynamically
127 * allocated, and contains a set of option strings separated by
128 * a delimiter. The delimeter is also the first character in
129 * the string, so for example ":Foo:Bar:Baz" gives three
130 * options `Foo', `Bar' and `Baz'.
131 */
132 char *sval;
133 /*
134 * For C_BOOLEAN, this is TRUE or FALSE. For C_CHOICES, it
135 * indicates the chosen index from the `sval' list. In the
136 * above example, 0==Foo, 1==Bar and 2==Baz.
137 */
138 int ival;
139 };
140
141 /*
142 * Platform routines
143 */
144
145 /* We can't use #ifdef DEBUG, because Cygwin defines it by default. */
146 #ifdef DEBUGGING
147 #define debug(x) (debug_printf x)
148 void debug_printf(char *fmt, ...);
149 #else
150 #define debug(x)
151 #endif
152
153 void fatal(char *fmt, ...);
154 void frontend_default_colour(frontend *fe, float *output);
155 void deactivate_timer(frontend *fe);
156 void activate_timer(frontend *fe);
157 void get_random_seed(void **randseed, int *randseedsize);
158
159 /*
160 * drawing.c
161 */
162 drawing *drawing_new(const drawing_api *api, midend *me, void *handle);
163 void drawing_free(drawing *dr);
164 void draw_text(drawing *dr, int x, int y, int fonttype, int fontsize,
165 int align, int colour, char *text);
166 void draw_rect(drawing *dr, int x, int y, int w, int h, int colour);
167 void draw_line(drawing *dr, int x1, int y1, int x2, int y2, int colour);
168 void draw_polygon(drawing *dr, int *coords, int npoints,
169 int fillcolour, int outlinecolour);
170 void draw_circle(drawing *dr, int cx, int cy, int radius,
171 int fillcolour, int outlinecolour);
172 void clip(drawing *dr, int x, int y, int w, int h);
173 void unclip(drawing *dr);
174 void start_draw(drawing *dr);
175 void draw_update(drawing *dr, int x, int y, int w, int h);
176 void end_draw(drawing *dr);
177 void status_bar(drawing *dr, char *text);
178 blitter *blitter_new(drawing *dr, int w, int h);
179 void blitter_free(drawing *dr, blitter *bl);
180 /* save puts the portion of the current display with top-left corner
181 * (x,y) to the blitter. load puts it back again to the specified
182 * coords, or else wherever it was saved from
183 * (if x = y = BLITTER_FROMSAVED). */
184 void blitter_save(drawing *dr, blitter *bl, int x, int y);
185 #define BLITTER_FROMSAVED (-1)
186 void blitter_load(drawing *dr, blitter *bl, int x, int y);
187 void print_begin_doc(drawing *dr, int pages);
188 void print_begin_page(drawing *dr, int number);
189 void print_begin_puzzle(drawing *dr, float xm, float xc,
190 float ym, float yc, int pw, int ph, float wmm,
191 float scale);
192 void print_end_puzzle(drawing *dr);
193 void print_end_page(drawing *dr, int number);
194 void print_end_doc(drawing *dr);
195 void print_get_colour(drawing *dr, int colour, int *hatch,
196 float *r, float *g, float *b);
197 int print_mono_colour(drawing *dr, int grey); /* 0==black, 1==white */
198 int print_grey_colour(drawing *dr, int hatch, float grey);
199 int print_rgb_colour(drawing *dr, int hatch, float r, float g, float b);
200 void print_line_width(drawing *dr, int width);
201
202 /*
203 * midend.c
204 */
205 midend *midend_new(frontend *fe, const game *ourgame,
206 const drawing_api *drapi, void *drhandle);
207 void midend_free(midend *me);
208 void midend_set_params(midend *me, game_params *params);
209 game_params *midend_get_params(midend *me);
210 void midend_size(midend *me, int *x, int *y, int expand);
211 void midend_new_game(midend *me);
212 void midend_restart_game(midend *me);
213 void midend_stop_anim(midend *me);
214 int midend_process_key(midend *me, int x, int y, int button);
215 void midend_force_redraw(midend *me);
216 void midend_redraw(midend *me);
217 float *midend_colours(midend *me, int *ncolours);
218 void midend_timer(midend *me, float tplus);
219 int midend_num_presets(midend *me);
220 void midend_fetch_preset(midend *me, int n,
221 char **name, game_params **params);
222 int midend_wants_statusbar(midend *me);
223 enum { CFG_SETTINGS, CFG_SEED, CFG_DESC, CFG_FRONTEND_SPECIFIC };
224 config_item *midend_get_config(midend *me, int which, char **wintitle);
225 char *midend_set_config(midend *me, int which, config_item *cfg);
226 char *midend_game_id(midend *me, char *id);
227 char *midend_get_game_id(midend *me);
228 char *midend_text_format(midend *me);
229 char *midend_solve(midend *me);
230 void midend_supersede_game_desc(midend *me, char *desc, char *privdesc);
231 char *midend_rewrite_statusbar(midend *me, char *text);
232 void midend_serialise(midend *me,
233 void (*write)(void *ctx, void *buf, int len),
234 void *wctx);
235 char *midend_deserialise(midend *me,
236 int (*read)(void *ctx, void *buf, int len),
237 void *rctx);
238 /* Printing functions supplied by the mid-end */
239 char *midend_print_puzzle(midend *me, document *doc, int with_soln);
240
241 /*
242 * malloc.c
243 */
244 void *smalloc(size_t size);
245 void *srealloc(void *p, size_t size);
246 void sfree(void *p);
247 char *dupstr(const char *s);
248 #define snew(type) \
249 ( (type *) smalloc (sizeof (type)) )
250 #define snewn(number, type) \
251 ( (type *) smalloc ((number) * sizeof (type)) )
252 #define sresize(array, number, type) \
253 ( (type *) srealloc ((array), (number) * sizeof (type)) )
254
255 /*
256 * misc.c
257 */
258 void free_cfg(config_item *cfg);
259 void obfuscate_bitmap(unsigned char *bmp, int bits, int decode);
260
261 /* allocates output each time. len is always in bytes of binary data.
262 * May assert (or just go wrong) if lengths are unchecked. */
263 char *bin2hex(const unsigned char *in, int inlen);
264 unsigned char *hex2bin(const char *in, int outlen);
265
266 /* Sets (and possibly dims) background from frontend default colour,
267 * and auto-generates highlight and lowlight colours too. */
268 void game_mkhighlight(frontend *fe, float *ret,
269 int background, int highlight, int lowlight);
270
271 /* Randomly shuffles an array of items. */
272 void shuffle(void *array, int nelts, int eltsize, random_state *rs);
273
274 /* Draw a rectangle outline, using the drawing API's draw_line. */
275 void draw_rect_outline(drawing *dr, int x, int y, int w, int h,
276 int colour);
277
278 /*
279 * dsf.c
280 */
281 int dsf_canonify(int *dsf, int val);
282 void dsf_merge(int *dsf, int v1, int v2);
283 void dsf_init(int *dsf, int len);
284
285 /*
286 * version.c
287 */
288 extern char ver[];
289
290 /*
291 * random.c
292 */
293 random_state *random_new(char *seed, int len);
294 random_state *random_copy(random_state *tocopy);
295 unsigned long random_bits(random_state *state, int bits);
296 unsigned long random_upto(random_state *state, unsigned long limit);
297 void random_free(random_state *state);
298 char *random_state_encode(random_state *state);
299 random_state *random_state_decode(char *input);
300 /* random.c also exports SHA, which occasionally comes in useful. */
301 typedef unsigned long uint32;
302 typedef struct {
303 uint32 h[5];
304 unsigned char block[64];
305 int blkused;
306 uint32 lenhi, lenlo;
307 } SHA_State;
308 void SHA_Init(SHA_State *s);
309 void SHA_Bytes(SHA_State *s, void *p, int len);
310 void SHA_Final(SHA_State *s, unsigned char *output);
311 void SHA_Simple(void *p, int len, unsigned char *output);
312
313 /*
314 * printing.c
315 */
316 document *document_new(int pw, int ph, float userscale);
317 void document_free(document *doc);
318 void document_add_puzzle(document *doc, const game *game, game_params *par,
319 game_state *st, game_state *st2);
320 void document_print(document *doc, drawing *dr);
321
322 /*
323 * ps.c
324 */
325 psdata *ps_init(FILE *outfile, int colour);
326 void ps_free(psdata *ps);
327 drawing *ps_drawing_api(psdata *ps);
328
329 /*
330 * combi.c: provides a structure and functions for iterating over
331 * combinations (i.e. choosing r things out of n).
332 */
333 typedef struct _combi_ctx {
334 int r, n, nleft, total;
335 int *a;
336 } combi_ctx;
337
338 combi_ctx *new_combi(int r, int n);
339 void reset_combi(combi_ctx *combi);
340 combi_ctx *next_combi(combi_ctx *combi); /* returns NULL for end */
341 void free_combi(combi_ctx *combi);
342
343 /*
344 * Data structure containing the function calls and data specific
345 * to a particular game. This is enclosed in a data structure so
346 * that a particular platform can choose, if it wishes, to compile
347 * all the games into a single combined executable rather than
348 * having lots of little ones.
349 */
350 struct game {
351 const char *name;
352 const char *winhelp_topic;
353 game_params *(*default_params)(void);
354 int (*fetch_preset)(int i, char **name, game_params **params);
355 void (*decode_params)(game_params *, char const *string);
356 char *(*encode_params)(game_params *, int full);
357 void (*free_params)(game_params *params);
358 game_params *(*dup_params)(game_params *params);
359 int can_configure;
360 config_item *(*configure)(game_params *params);
361 game_params *(*custom_params)(config_item *cfg);
362 char *(*validate_params)(game_params *params, int full);
363 char *(*new_desc)(game_params *params, random_state *rs,
364 char **aux, int interactive);
365 char *(*validate_desc)(game_params *params, char *desc);
366 game_state *(*new_game)(midend *me, game_params *params, char *desc);
367 game_state *(*dup_game)(game_state *state);
368 void (*free_game)(game_state *state);
369 int can_solve;
370 char *(*solve)(game_state *orig, game_state *curr,
371 char *aux, char **error);
372 int can_format_as_text;
373 char *(*text_format)(game_state *state);
374 game_ui *(*new_ui)(game_state *state);
375 void (*free_ui)(game_ui *ui);
376 char *(*encode_ui)(game_ui *ui);
377 void (*decode_ui)(game_ui *ui, char *encoding);
378 void (*changed_state)(game_ui *ui, game_state *oldstate,
379 game_state *newstate);
380 char *(*interpret_move)(game_state *state, game_ui *ui, game_drawstate *ds,
381 int x, int y, int button);
382 game_state *(*execute_move)(game_state *state, char *move);
383 int preferred_tilesize;
384 void (*compute_size)(game_params *params, int tilesize, int *x, int *y);
385 void (*set_size)(drawing *dr, game_drawstate *ds,
386 game_params *params, int tilesize);
387 float *(*colours)(frontend *fe, int *ncolours);
388 game_drawstate *(*new_drawstate)(drawing *dr, game_state *state);
389 void (*free_drawstate)(drawing *dr, game_drawstate *ds);
390 void (*redraw)(drawing *dr, game_drawstate *ds, game_state *oldstate,
391 game_state *newstate, int dir, game_ui *ui, float anim_time,
392 float flash_time);
393 float (*anim_length)(game_state *oldstate, game_state *newstate, int dir,
394 game_ui *ui);
395 float (*flash_length)(game_state *oldstate, game_state *newstate, int dir,
396 game_ui *ui);
397 int can_print, can_print_in_colour;
398 void (*print_size)(game_params *params, float *x, float *y);
399 void (*print)(drawing *dr, game_state *state, int tilesize);
400 int wants_statusbar;
401 int is_timed;
402 int (*timing_state)(game_state *state, game_ui *ui);
403 int flags;
404 };
405
406 /*
407 * Data structure containing the drawing API implemented by the
408 * front end and also by cross-platform printing modules such as
409 * PostScript.
410 */
411 struct drawing_api {
412 void (*draw_text)(void *handle, int x, int y, int fonttype, int fontsize,
413 int align, int colour, char *text);
414 void (*draw_rect)(void *handle, int x, int y, int w, int h, int colour);
415 void (*draw_line)(void *handle, int x1, int y1, int x2, int y2,
416 int colour);
417 void (*draw_polygon)(void *handle, int *coords, int npoints,
418 int fillcolour, int outlinecolour);
419 void (*draw_circle)(void *handle, int cx, int cy, int radius,
420 int fillcolour, int outlinecolour);
421 void (*draw_update)(void *handle, int x, int y, int w, int h);
422 void (*clip)(void *handle, int x, int y, int w, int h);
423 void (*unclip)(void *handle);
424 void (*start_draw)(void *handle);
425 void (*end_draw)(void *handle);
426 void (*status_bar)(void *handle, char *text);
427 blitter *(*blitter_new)(void *handle, int w, int h);
428 void (*blitter_free)(void *handle, blitter *bl);
429 void (*blitter_save)(void *handle, blitter *bl, int x, int y);
430 void (*blitter_load)(void *handle, blitter *bl, int x, int y);
431 void (*begin_doc)(void *handle, int pages);
432 void (*begin_page)(void *handle, int number);
433 void (*begin_puzzle)(void *handle, float xm, float xc,
434 float ym, float yc, int pw, int ph, float wmm);
435 void (*end_puzzle)(void *handle);
436 void (*end_page)(void *handle, int number);
437 void (*end_doc)(void *handle);
438 void (*line_width)(void *handle, float width);
439 };
440
441 /*
442 * For one-game-at-a-time platforms, there's a single structure
443 * like the above, under a fixed name. For all-at-once platforms,
444 * there's a list of all available puzzles in array form.
445 */
446 #ifdef COMBINED
447 extern const game *gamelist[];
448 extern const int gamecount;
449 #else
450 extern const game thegame;
451 #endif
452
453 #endif /* PUZZLES_PUZZLES_H */