puzzles.h 32 KB

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