netslide.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887
  1. /*
  2. * netslide.c: cross between Net and Sixteen, courtesy of Richard
  3. * Boulton.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <ctype.h>
  10. #include <limits.h>
  11. #ifdef NO_TGMATH_H
  12. # include <math.h>
  13. #else
  14. # include <tgmath.h>
  15. #endif
  16. #include "puzzles.h"
  17. #include "tree234.h"
  18. #define MATMUL(xr,yr,m,x,y) do { \
  19. float rx, ry, xx = (x), yy = (y), *mat = (m); \
  20. rx = mat[0] * xx + mat[2] * yy; \
  21. ry = mat[1] * xx + mat[3] * yy; \
  22. (xr) = rx; (yr) = ry; \
  23. } while (0)
  24. /* Direction and other bitfields */
  25. #define R 0x01
  26. #define U 0x02
  27. #define L 0x04
  28. #define D 0x08
  29. #define FLASHING 0x10
  30. #define ACTIVE 0x20
  31. /* Corner flags go in the barriers array */
  32. #define RU 0x10
  33. #define UL 0x20
  34. #define LD 0x40
  35. #define DR 0x80
  36. /* Get tile at given coordinate */
  37. #define T(state, x, y) ( (y) * (state)->width + (x) )
  38. /* Rotations: Anticlockwise, Clockwise, Flip, general rotate */
  39. #define A(x) ( (((x) & 0x07) << 1) | (((x) & 0x08) >> 3) )
  40. #define C(x) ( (((x) & 0x0E) >> 1) | (((x) & 0x01) << 3) )
  41. #define F(x) ( (((x) & 0x0C) >> 2) | (((x) & 0x03) << 2) )
  42. #define ROT(x, n) ( ((n)&3) == 0 ? (x) : \
  43. ((n)&3) == 1 ? A(x) : \
  44. ((n)&3) == 2 ? F(x) : C(x) )
  45. /* X and Y displacements */
  46. #define X(x) ( (x) == R ? +1 : (x) == L ? -1 : 0 )
  47. #define Y(x) ( (x) == D ? +1 : (x) == U ? -1 : 0 )
  48. /* Bit count */
  49. #define COUNT(x) ( (((x) & 0x08) >> 3) + (((x) & 0x04) >> 2) + \
  50. (((x) & 0x02) >> 1) + ((x) & 0x01) )
  51. #define PREFERRED_TILE_SIZE 48
  52. #define TILE_SIZE (ds->tilesize)
  53. #define BORDER TILE_SIZE
  54. #define TILE_BORDER 1
  55. #define WINDOW_OFFSET 0
  56. #define ANIM_TIME 0.13F
  57. #define FLASH_FRAME 0.07F
  58. enum {
  59. COL_BACKGROUND,
  60. COL_FLASHING,
  61. COL_BORDER,
  62. COL_WIRE,
  63. COL_ENDPOINT,
  64. COL_POWERED,
  65. COL_BARRIER,
  66. COL_LOWLIGHT,
  67. COL_TEXT,
  68. NCOLOURS
  69. };
  70. struct game_params {
  71. int width;
  72. int height;
  73. bool wrapping;
  74. float barrier_probability;
  75. int movetarget;
  76. };
  77. struct game_state {
  78. int width, height, cx, cy, completed;
  79. bool wrapping, used_solve;
  80. int move_count, movetarget;
  81. /* position (row or col number, starting at 0) of last move. */
  82. int last_move_row, last_move_col;
  83. /* direction of last move: +1 or -1 */
  84. int last_move_dir;
  85. unsigned char *tiles;
  86. unsigned char *barriers;
  87. };
  88. #define OFFSET(x2,y2,x1,y1,dir,state) \
  89. ( (x2) = ((x1) + (state)->width + X((dir))) % (state)->width, \
  90. (y2) = ((y1) + (state)->height + Y((dir))) % (state)->height)
  91. #define index(state, a, x, y) ( a[(y) * (state)->width + (x)] )
  92. #define tile(state, x, y) index(state, (state)->tiles, x, y)
  93. #define barrier(state, x, y) index(state, (state)->barriers, x, y)
  94. struct xyd {
  95. int x, y, direction;
  96. };
  97. static int xyd_cmp(void *av, void *bv) {
  98. struct xyd *a = (struct xyd *)av;
  99. struct xyd *b = (struct xyd *)bv;
  100. if (a->x < b->x)
  101. return -1;
  102. if (a->x > b->x)
  103. return +1;
  104. if (a->y < b->y)
  105. return -1;
  106. if (a->y > b->y)
  107. return +1;
  108. if (a->direction < b->direction)
  109. return -1;
  110. if (a->direction > b->direction)
  111. return +1;
  112. return 0;
  113. }
  114. static struct xyd *new_xyd(int x, int y, int direction)
  115. {
  116. struct xyd *xyd = snew(struct xyd);
  117. xyd->x = x;
  118. xyd->y = y;
  119. xyd->direction = direction;
  120. return xyd;
  121. }
  122. static void slide_col(game_state *state, int dir, int col);
  123. static void slide_col_int(int w, int h, unsigned char *tiles, int dir, int col);
  124. static void slide_row(game_state *state, int dir, int row);
  125. static void slide_row_int(int w, int h, unsigned char *tiles, int dir, int row);
  126. /* ----------------------------------------------------------------------
  127. * Manage game parameters.
  128. */
  129. static game_params *default_params(void)
  130. {
  131. game_params *ret = snew(game_params);
  132. ret->width = 3;
  133. ret->height = 3;
  134. ret->wrapping = false;
  135. ret->barrier_probability = 1.0;
  136. ret->movetarget = 0;
  137. return ret;
  138. }
  139. static const struct { int x, y, wrap, bprob; const char* desc; }
  140. netslide_presets[] = {
  141. {3, 3, false, 1, " easy"},
  142. {3, 3, false, 0, " medium"},
  143. {3, 3, true, 0, " hard"},
  144. {4, 4, false, 1, " easy"},
  145. {4, 4, false, 0, " medium"},
  146. {4, 4, true, 0, " hard"},
  147. {5, 5, false, 1, " easy"},
  148. {5, 5, false, 0, " medium"},
  149. {5, 5, true, 0, " hard"},
  150. };
  151. static bool game_fetch_preset(int i, char **name, game_params **params)
  152. {
  153. game_params *ret;
  154. char str[80];
  155. if (i < 0 || i >= lenof(netslide_presets))
  156. return false;
  157. ret = snew(game_params);
  158. ret->width = netslide_presets[i].x;
  159. ret->height = netslide_presets[i].y;
  160. ret->wrapping = netslide_presets[i].wrap;
  161. ret->barrier_probability = (float)netslide_presets[i].bprob;
  162. ret->movetarget = 0;
  163. sprintf(str, "%dx%d%s", ret->width, ret->height, netslide_presets[i].desc);
  164. *name = dupstr(str);
  165. *params = ret;
  166. return true;
  167. }
  168. static void free_params(game_params *params)
  169. {
  170. sfree(params);
  171. }
  172. static game_params *dup_params(const game_params *params)
  173. {
  174. game_params *ret = snew(game_params);
  175. *ret = *params; /* structure copy */
  176. return ret;
  177. }
  178. static void decode_params(game_params *ret, char const *string)
  179. {
  180. char const *p = string;
  181. ret->wrapping = false;
  182. ret->barrier_probability = 0.0;
  183. ret->movetarget = 0;
  184. ret->width = atoi(p);
  185. while (*p && isdigit((unsigned char)*p)) p++;
  186. if (*p == 'x') {
  187. p++;
  188. ret->height = atoi(p);
  189. while (*p && isdigit((unsigned char)*p)) p++;
  190. ret->wrapping = (*p == 'w');
  191. if (ret->wrapping)
  192. p++;
  193. if (*p == 'b') {
  194. ret->barrier_probability = (float)atof(++p);
  195. while (*p && (isdigit((unsigned char)*p) || *p == '.')) p++;
  196. }
  197. if (*p == 'm') {
  198. ret->movetarget = atoi(++p);
  199. }
  200. } else {
  201. ret->height = ret->width;
  202. }
  203. }
  204. static char *encode_params(const game_params *params, bool full)
  205. {
  206. char ret[400];
  207. int len;
  208. len = sprintf(ret, "%dx%d", params->width, params->height);
  209. if (params->wrapping)
  210. ret[len++] = 'w';
  211. if (full && params->barrier_probability)
  212. len += sprintf(ret+len, "b%g", params->barrier_probability);
  213. /* Shuffle limit is part of the limited parameters, because we have to
  214. * provide the target move count. */
  215. if (params->movetarget)
  216. len += sprintf(ret+len, "m%d", params->movetarget);
  217. assert(len < lenof(ret));
  218. ret[len] = '\0';
  219. return dupstr(ret);
  220. }
  221. static config_item *game_configure(const game_params *params)
  222. {
  223. config_item *ret;
  224. char buf[80];
  225. ret = snewn(6, config_item);
  226. ret[0].name = "Width";
  227. ret[0].type = C_STRING;
  228. sprintf(buf, "%d", params->width);
  229. ret[0].u.string.sval = dupstr(buf);
  230. ret[1].name = "Height";
  231. ret[1].type = C_STRING;
  232. sprintf(buf, "%d", params->height);
  233. ret[1].u.string.sval = dupstr(buf);
  234. ret[2].name = "Walls wrap around";
  235. ret[2].type = C_BOOLEAN;
  236. ret[2].u.boolean.bval = params->wrapping;
  237. ret[3].name = "Barrier probability";
  238. ret[3].type = C_STRING;
  239. sprintf(buf, "%g", params->barrier_probability);
  240. ret[3].u.string.sval = dupstr(buf);
  241. ret[4].name = "Number of shuffling moves";
  242. ret[4].type = C_STRING;
  243. sprintf(buf, "%d", params->movetarget);
  244. ret[4].u.string.sval = dupstr(buf);
  245. ret[5].name = NULL;
  246. ret[5].type = C_END;
  247. return ret;
  248. }
  249. static game_params *custom_params(const config_item *cfg)
  250. {
  251. game_params *ret = snew(game_params);
  252. ret->width = atoi(cfg[0].u.string.sval);
  253. ret->height = atoi(cfg[1].u.string.sval);
  254. ret->wrapping = cfg[2].u.boolean.bval;
  255. ret->barrier_probability = (float)atof(cfg[3].u.string.sval);
  256. ret->movetarget = atoi(cfg[4].u.string.sval);
  257. return ret;
  258. }
  259. static const char *validate_params(const game_params *params, bool full)
  260. {
  261. if (params->width <= 1 || params->height <= 1)
  262. return "Width and height must both be greater than one";
  263. if (params->width > INT_MAX / params->height)
  264. return "Width times height must not be unreasonably large";
  265. if (params->barrier_probability < 0)
  266. return "Barrier probability may not be negative";
  267. if (params->barrier_probability > 1)
  268. return "Barrier probability may not be greater than 1";
  269. return NULL;
  270. }
  271. /* ----------------------------------------------------------------------
  272. * Randomly select a new game description.
  273. */
  274. static char *new_game_desc(const game_params *params, random_state *rs,
  275. char **aux, bool interactive)
  276. {
  277. tree234 *possibilities, *barriertree;
  278. int w, h, x, y, cx, cy, nbarriers;
  279. unsigned char *tiles, *barriers;
  280. char *desc, *p;
  281. w = params->width;
  282. h = params->height;
  283. tiles = snewn(w * h, unsigned char);
  284. memset(tiles, 0, w * h);
  285. barriers = snewn(w * h, unsigned char);
  286. memset(barriers, 0, w * h);
  287. cx = w / 2;
  288. cy = h / 2;
  289. /*
  290. * Construct the unshuffled grid.
  291. *
  292. * To do this, we simply start at the centre point, repeatedly
  293. * choose a random possibility out of the available ways to
  294. * extend a used square into an unused one, and do it. After
  295. * extending the third line out of a square, we remove the
  296. * fourth from the possibilities list to avoid any full-cross
  297. * squares (which would make the game too easy because they
  298. * only have one orientation).
  299. *
  300. * The slightly worrying thing is the avoidance of full-cross
  301. * squares. Can this cause our unsophisticated construction
  302. * algorithm to paint itself into a corner, by getting into a
  303. * situation where there are some unreached squares and the
  304. * only way to reach any of them is to extend a T-piece into a
  305. * full cross?
  306. *
  307. * Answer: no it can't, and here's a proof.
  308. *
  309. * Any contiguous group of such unreachable squares must be
  310. * surrounded on _all_ sides by T-pieces pointing away from the
  311. * group. (If not, then there is a square which can be extended
  312. * into one of the `unreachable' ones, and so it wasn't
  313. * unreachable after all.) In particular, this implies that
  314. * each contiguous group of unreachable squares must be
  315. * rectangular in shape (any deviation from that yields a
  316. * non-T-piece next to an `unreachable' square).
  317. *
  318. * So we have a rectangle of unreachable squares, with T-pieces
  319. * forming a solid border around the rectangle. The corners of
  320. * that border must be connected (since every tile connects all
  321. * the lines arriving in it), and therefore the border must
  322. * form a closed loop around the rectangle.
  323. *
  324. * But this can't have happened in the first place, since we
  325. * _know_ we've avoided creating closed loops! Hence, no such
  326. * situation can ever arise, and the naive grid construction
  327. * algorithm will guaranteeably result in a complete grid
  328. * containing no unreached squares, no full crosses _and_ no
  329. * closed loops. []
  330. */
  331. possibilities = newtree234(xyd_cmp);
  332. if (cx+1 < w)
  333. add234(possibilities, new_xyd(cx, cy, R));
  334. if (cy-1 >= 0)
  335. add234(possibilities, new_xyd(cx, cy, U));
  336. if (cx-1 >= 0)
  337. add234(possibilities, new_xyd(cx, cy, L));
  338. if (cy+1 < h)
  339. add234(possibilities, new_xyd(cx, cy, D));
  340. while (count234(possibilities) > 0) {
  341. int i;
  342. struct xyd *xyd;
  343. int x1, y1, d1, x2, y2, d2, d;
  344. /*
  345. * Extract a randomly chosen possibility from the list.
  346. */
  347. i = random_upto(rs, count234(possibilities));
  348. xyd = delpos234(possibilities, i);
  349. x1 = xyd->x;
  350. y1 = xyd->y;
  351. d1 = xyd->direction;
  352. sfree(xyd);
  353. OFFSET(x2, y2, x1, y1, d1, params);
  354. d2 = F(d1);
  355. #ifdef GENERATION_DIAGNOSTICS
  356. printf("picked (%d,%d,%c) <-> (%d,%d,%c)\n",
  357. x1, y1, "0RU3L567D9abcdef"[d1], x2, y2, "0RU3L567D9abcdef"[d2]);
  358. #endif
  359. /*
  360. * Make the connection. (We should be moving to an as yet
  361. * unused tile.)
  362. */
  363. index(params, tiles, x1, y1) |= d1;
  364. assert(index(params, tiles, x2, y2) == 0);
  365. index(params, tiles, x2, y2) |= d2;
  366. /*
  367. * If we have created a T-piece, remove its last
  368. * possibility.
  369. */
  370. if (COUNT(index(params, tiles, x1, y1)) == 3) {
  371. struct xyd xyd1, *xydp;
  372. xyd1.x = x1;
  373. xyd1.y = y1;
  374. xyd1.direction = 0x0F ^ index(params, tiles, x1, y1);
  375. xydp = find234(possibilities, &xyd1, NULL);
  376. if (xydp) {
  377. #ifdef GENERATION_DIAGNOSTICS
  378. printf("T-piece; removing (%d,%d,%c)\n",
  379. xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
  380. #endif
  381. del234(possibilities, xydp);
  382. sfree(xydp);
  383. }
  384. }
  385. /*
  386. * Remove all other possibilities that were pointing at the
  387. * tile we've just moved into.
  388. */
  389. for (d = 1; d < 0x10; d <<= 1) {
  390. int x3, y3, d3;
  391. struct xyd xyd1, *xydp;
  392. OFFSET(x3, y3, x2, y2, d, params);
  393. d3 = F(d);
  394. xyd1.x = x3;
  395. xyd1.y = y3;
  396. xyd1.direction = d3;
  397. xydp = find234(possibilities, &xyd1, NULL);
  398. if (xydp) {
  399. #ifdef GENERATION_DIAGNOSTICS
  400. printf("Loop avoidance; removing (%d,%d,%c)\n",
  401. xydp->x, xydp->y, "0RU3L567D9abcdef"[xydp->direction]);
  402. #endif
  403. del234(possibilities, xydp);
  404. sfree(xydp);
  405. }
  406. }
  407. /*
  408. * Add new possibilities to the list for moving _out_ of
  409. * the tile we have just moved into.
  410. */
  411. for (d = 1; d < 0x10; d <<= 1) {
  412. int x3, y3;
  413. if (d == d2)
  414. continue; /* we've got this one already */
  415. if (!params->wrapping) {
  416. if (d == U && y2 == 0)
  417. continue;
  418. if (d == D && y2 == h-1)
  419. continue;
  420. if (d == L && x2 == 0)
  421. continue;
  422. if (d == R && x2 == w-1)
  423. continue;
  424. }
  425. OFFSET(x3, y3, x2, y2, d, params);
  426. if (index(params, tiles, x3, y3))
  427. continue; /* this would create a loop */
  428. #ifdef GENERATION_DIAGNOSTICS
  429. printf("New frontier; adding (%d,%d,%c)\n",
  430. x2, y2, "0RU3L567D9abcdef"[d]);
  431. #endif
  432. add234(possibilities, new_xyd(x2, y2, d));
  433. }
  434. }
  435. /* Having done that, we should have no possibilities remaining. */
  436. assert(count234(possibilities) == 0);
  437. freetree234(possibilities);
  438. /*
  439. * Now compute a list of the possible barrier locations.
  440. */
  441. barriertree = newtree234(xyd_cmp);
  442. for (y = 0; y < h; y++) {
  443. for (x = 0; x < w; x++) {
  444. if (!(index(params, tiles, x, y) & R) &&
  445. (params->wrapping || x < w-1))
  446. add234(barriertree, new_xyd(x, y, R));
  447. if (!(index(params, tiles, x, y) & D) &&
  448. (params->wrapping || y < h-1))
  449. add234(barriertree, new_xyd(x, y, D));
  450. }
  451. }
  452. /*
  453. * Save the unshuffled grid in aux.
  454. */
  455. {
  456. char *solution;
  457. int i;
  458. /*
  459. * String format is exactly the same as a solve move, so we
  460. * can just dupstr this in solve_game().
  461. */
  462. solution = snewn(w * h + 2, char);
  463. solution[0] = 'S';
  464. for (i = 0; i < w * h; i++)
  465. solution[i+1] = "0123456789abcdef"[tiles[i] & 0xF];
  466. solution[w*h+1] = '\0';
  467. *aux = solution;
  468. }
  469. /*
  470. * Now shuffle the grid.
  471. * FIXME - this simply does a set of random moves to shuffle the pieces,
  472. * although we make a token effort to avoid boring cases by avoiding moves
  473. * that directly undo the previous one, or that repeat so often as to
  474. * turn into fewer moves.
  475. *
  476. * A better way would be to number all the pieces, generate a placement
  477. * for all the numbers as for "sixteen", observing parity constraints if
  478. * neccessary, and then place the pieces according to their numbering.
  479. * BUT - I'm not sure if this will work, since we disallow movement of
  480. * the middle row and column.
  481. */
  482. {
  483. int i;
  484. int cols = w - 1;
  485. int rows = h - 1;
  486. int moves = params->movetarget;
  487. int prevdir = -1, prevrowcol = -1, nrepeats = 0;
  488. if (!moves) moves = cols * rows * 2;
  489. for (i = 0; i < moves; /* incremented conditionally */) {
  490. /* Choose a direction: 0,1,2,3 = up, right, down, left. */
  491. int dir = random_upto(rs, 4);
  492. int rowcol;
  493. if (dir % 2 == 0) {
  494. int col = random_upto(rs, cols);
  495. if (col >= cx) col += 1; /* avoid centre */
  496. if (col == prevrowcol) {
  497. if (dir == 2-prevdir)
  498. continue; /* undoes last move */
  499. else if (dir == prevdir && (nrepeats+1)*2 > h)
  500. continue; /* makes fewer moves */
  501. }
  502. slide_col_int(w, h, tiles, 1 - dir, col);
  503. rowcol = col;
  504. } else {
  505. int row = random_upto(rs, rows);
  506. if (row >= cy) row += 1; /* avoid centre */
  507. if (row == prevrowcol) {
  508. if (dir == 4-prevdir)
  509. continue; /* undoes last move */
  510. else if (dir == prevdir && (nrepeats+1)*2 > w)
  511. continue; /* makes fewer moves */
  512. }
  513. slide_row_int(w, h, tiles, 2 - dir, row);
  514. rowcol = row;
  515. }
  516. if (dir == prevdir && rowcol == prevrowcol)
  517. nrepeats++;
  518. else
  519. nrepeats = 1;
  520. prevdir = dir;
  521. prevrowcol = rowcol;
  522. i++; /* if we got here, the move was accepted */
  523. }
  524. }
  525. /*
  526. * And now choose barrier locations. (We carefully do this
  527. * _after_ shuffling, so that changing the barrier rate in the
  528. * params while keeping the random seed the same will give the
  529. * same shuffled grid and _only_ change the barrier locations.
  530. * Also the way we choose barrier locations, by repeatedly
  531. * choosing one possibility from the list until we have enough,
  532. * is designed to ensure that raising the barrier rate while
  533. * keeping the seed the same will provide a superset of the
  534. * previous barrier set - i.e. if you ask for 10 barriers, and
  535. * then decide that's still too hard and ask for 20, you'll get
  536. * the original 10 plus 10 more, rather than getting 20 new
  537. * ones and the chance of remembering your first 10.)
  538. */
  539. nbarriers = (int)(params->barrier_probability * count234(barriertree));
  540. assert(nbarriers >= 0 && nbarriers <= count234(barriertree));
  541. while (nbarriers > 0) {
  542. int i;
  543. struct xyd *xyd;
  544. int x1, y1, d1, x2, y2, d2;
  545. /*
  546. * Extract a randomly chosen barrier from the list.
  547. */
  548. i = random_upto(rs, count234(barriertree));
  549. xyd = delpos234(barriertree, i);
  550. assert(xyd != NULL);
  551. x1 = xyd->x;
  552. y1 = xyd->y;
  553. d1 = xyd->direction;
  554. sfree(xyd);
  555. OFFSET(x2, y2, x1, y1, d1, params);
  556. d2 = F(d1);
  557. index(params, barriers, x1, y1) |= d1;
  558. index(params, barriers, x2, y2) |= d2;
  559. nbarriers--;
  560. }
  561. /*
  562. * Clean up the rest of the barrier list.
  563. */
  564. {
  565. struct xyd *xyd;
  566. while ( (xyd = delpos234(barriertree, 0)) != NULL)
  567. sfree(xyd);
  568. freetree234(barriertree);
  569. }
  570. /*
  571. * Finally, encode the grid into a string game description.
  572. *
  573. * My syntax is extremely simple: each square is encoded as a
  574. * hex digit in which bit 0 means a connection on the right,
  575. * bit 1 means up, bit 2 left and bit 3 down. (i.e. the same
  576. * encoding as used internally). Each digit is followed by
  577. * optional barrier indicators: `v' means a vertical barrier to
  578. * the right of it, and `h' means a horizontal barrier below
  579. * it.
  580. */
  581. desc = snewn(w * h * 3 + 1, char);
  582. p = desc;
  583. for (y = 0; y < h; y++) {
  584. for (x = 0; x < w; x++) {
  585. *p++ = "0123456789abcdef"[index(params, tiles, x, y)];
  586. if ((params->wrapping || x < w-1) &&
  587. (index(params, barriers, x, y) & R))
  588. *p++ = 'v';
  589. if ((params->wrapping || y < h-1) &&
  590. (index(params, barriers, x, y) & D))
  591. *p++ = 'h';
  592. }
  593. }
  594. assert(p - desc <= w*h*3);
  595. *p = '\0';
  596. sfree(tiles);
  597. sfree(barriers);
  598. return desc;
  599. }
  600. static const char *validate_desc(const game_params *params, const char *desc)
  601. {
  602. int w = params->width, h = params->height;
  603. int i;
  604. for (i = 0; i < w*h; i++) {
  605. if (*desc >= '0' && *desc <= '9')
  606. /* OK */;
  607. else if (*desc >= 'a' && *desc <= 'f')
  608. /* OK */;
  609. else if (*desc >= 'A' && *desc <= 'F')
  610. /* OK */;
  611. else if (!*desc)
  612. return "Game description shorter than expected";
  613. else
  614. return "Game description contained unexpected character";
  615. desc++;
  616. while (*desc == 'h' || *desc == 'v')
  617. desc++;
  618. }
  619. if (*desc)
  620. return "Game description longer than expected";
  621. return NULL;
  622. }
  623. /* ----------------------------------------------------------------------
  624. * Construct an initial game state, given a description and parameters.
  625. */
  626. static game_state *new_game(midend *me, const game_params *params,
  627. const char *desc)
  628. {
  629. game_state *state;
  630. int w, h, x, y;
  631. assert(params->width > 0 && params->height > 0);
  632. assert(params->width > 1 || params->height > 1);
  633. /*
  634. * Create a blank game state.
  635. */
  636. state = snew(game_state);
  637. w = state->width = params->width;
  638. h = state->height = params->height;
  639. state->cx = state->width / 2;
  640. state->cy = state->height / 2;
  641. state->wrapping = params->wrapping;
  642. state->movetarget = params->movetarget;
  643. state->completed = 0;
  644. state->used_solve = false;
  645. state->move_count = 0;
  646. state->last_move_row = -1;
  647. state->last_move_col = -1;
  648. state->last_move_dir = 0;
  649. state->tiles = snewn(state->width * state->height, unsigned char);
  650. memset(state->tiles, 0, state->width * state->height);
  651. state->barriers = snewn(state->width * state->height, unsigned char);
  652. memset(state->barriers, 0, state->width * state->height);
  653. /*
  654. * Parse the game description into the grid.
  655. */
  656. for (y = 0; y < h; y++) {
  657. for (x = 0; x < w; x++) {
  658. if (*desc >= '0' && *desc <= '9')
  659. tile(state, x, y) = *desc - '0';
  660. else if (*desc >= 'a' && *desc <= 'f')
  661. tile(state, x, y) = *desc - 'a' + 10;
  662. else if (*desc >= 'A' && *desc <= 'F')
  663. tile(state, x, y) = *desc - 'A' + 10;
  664. if (*desc)
  665. desc++;
  666. while (*desc == 'h' || *desc == 'v') {
  667. int x2, y2, d1, d2;
  668. if (*desc == 'v')
  669. d1 = R;
  670. else
  671. d1 = D;
  672. OFFSET(x2, y2, x, y, d1, state);
  673. d2 = F(d1);
  674. barrier(state, x, y) |= d1;
  675. barrier(state, x2, y2) |= d2;
  676. desc++;
  677. }
  678. }
  679. }
  680. /*
  681. * Set up border barriers if this is a non-wrapping game.
  682. */
  683. if (!state->wrapping) {
  684. for (x = 0; x < state->width; x++) {
  685. barrier(state, x, 0) |= U;
  686. barrier(state, x, state->height-1) |= D;
  687. }
  688. for (y = 0; y < state->height; y++) {
  689. barrier(state, 0, y) |= L;
  690. barrier(state, state->width-1, y) |= R;
  691. }
  692. }
  693. /*
  694. * Set up the barrier corner flags, for drawing barriers
  695. * prettily when they meet.
  696. */
  697. for (y = 0; y < state->height; y++) {
  698. for (x = 0; x < state->width; x++) {
  699. int dir;
  700. for (dir = 1; dir < 0x10; dir <<= 1) {
  701. int dir2 = A(dir);
  702. int x1, y1, x2, y2, x3, y3;
  703. bool corner = false;
  704. if (!(barrier(state, x, y) & dir))
  705. continue;
  706. if (barrier(state, x, y) & dir2)
  707. corner = true;
  708. x1 = x + X(dir), y1 = y + Y(dir);
  709. if (x1 >= 0 && x1 < state->width &&
  710. y1 >= 0 && y1 < state->height &&
  711. (barrier(state, x1, y1) & dir2))
  712. corner = true;
  713. x2 = x + X(dir2), y2 = y + Y(dir2);
  714. if (x2 >= 0 && x2 < state->width &&
  715. y2 >= 0 && y2 < state->height &&
  716. (barrier(state, x2, y2) & dir))
  717. corner = true;
  718. if (corner) {
  719. barrier(state, x, y) |= (dir << 4);
  720. if (x1 >= 0 && x1 < state->width &&
  721. y1 >= 0 && y1 < state->height)
  722. barrier(state, x1, y1) |= (A(dir) << 4);
  723. if (x2 >= 0 && x2 < state->width &&
  724. y2 >= 0 && y2 < state->height)
  725. barrier(state, x2, y2) |= (C(dir) << 4);
  726. x3 = x + X(dir) + X(dir2), y3 = y + Y(dir) + Y(dir2);
  727. if (x3 >= 0 && x3 < state->width &&
  728. y3 >= 0 && y3 < state->height)
  729. barrier(state, x3, y3) |= (F(dir) << 4);
  730. }
  731. }
  732. }
  733. }
  734. return state;
  735. }
  736. static game_state *dup_game(const game_state *state)
  737. {
  738. game_state *ret;
  739. ret = snew(game_state);
  740. ret->width = state->width;
  741. ret->height = state->height;
  742. ret->cx = state->cx;
  743. ret->cy = state->cy;
  744. ret->wrapping = state->wrapping;
  745. ret->movetarget = state->movetarget;
  746. ret->completed = state->completed;
  747. ret->used_solve = state->used_solve;
  748. ret->move_count = state->move_count;
  749. ret->last_move_row = state->last_move_row;
  750. ret->last_move_col = state->last_move_col;
  751. ret->last_move_dir = state->last_move_dir;
  752. ret->tiles = snewn(state->width * state->height, unsigned char);
  753. memcpy(ret->tiles, state->tiles, state->width * state->height);
  754. ret->barriers = snewn(state->width * state->height, unsigned char);
  755. memcpy(ret->barriers, state->barriers, state->width * state->height);
  756. return ret;
  757. }
  758. static void free_game(game_state *state)
  759. {
  760. sfree(state->tiles);
  761. sfree(state->barriers);
  762. sfree(state);
  763. }
  764. static char *solve_game(const game_state *state, const game_state *currstate,
  765. const char *aux, const char **error)
  766. {
  767. if (!aux) {
  768. *error = "Solution not known for this puzzle";
  769. return NULL;
  770. }
  771. return dupstr(aux);
  772. }
  773. /* ----------------------------------------------------------------------
  774. * Utility routine.
  775. */
  776. /*
  777. * Compute which squares are reachable from the centre square, as a
  778. * quick visual aid to determining how close the game is to
  779. * completion. This is also a simple way to tell if the game _is_
  780. * completed - just call this function and see whether every square
  781. * is marked active.
  782. *
  783. * squares in the moving_row and moving_col are always inactive - this
  784. * is so that "current" doesn't appear to jump across moving lines.
  785. */
  786. static unsigned char *compute_active(const game_state *state,
  787. int moving_row, int moving_col)
  788. {
  789. unsigned char *active;
  790. tree234 *todo;
  791. struct xyd *xyd;
  792. active = snewn(state->width * state->height, unsigned char);
  793. memset(active, 0, state->width * state->height);
  794. /*
  795. * We only store (x,y) pairs in todo, but it's easier to reuse
  796. * xyd_cmp and just store direction 0 every time.
  797. */
  798. todo = newtree234(xyd_cmp);
  799. index(state, active, state->cx, state->cy) = ACTIVE;
  800. add234(todo, new_xyd(state->cx, state->cy, 0));
  801. while ( (xyd = delpos234(todo, 0)) != NULL) {
  802. int x1, y1, d1, x2, y2, d2;
  803. x1 = xyd->x;
  804. y1 = xyd->y;
  805. sfree(xyd);
  806. for (d1 = 1; d1 < 0x10; d1 <<= 1) {
  807. OFFSET(x2, y2, x1, y1, d1, state);
  808. d2 = F(d1);
  809. /*
  810. * If the next tile in this direction is connected to
  811. * us, and there isn't a barrier in the way, and it
  812. * isn't already marked active, then mark it active and
  813. * add it to the to-examine list.
  814. */
  815. if ((x2 != moving_col && y2 != moving_row) &&
  816. (tile(state, x1, y1) & d1) &&
  817. (tile(state, x2, y2) & d2) &&
  818. !(barrier(state, x1, y1) & d1) &&
  819. !index(state, active, x2, y2)) {
  820. index(state, active, x2, y2) = ACTIVE;
  821. add234(todo, new_xyd(x2, y2, 0));
  822. }
  823. }
  824. }
  825. /* Now we expect the todo list to have shrunk to zero size. */
  826. assert(count234(todo) == 0);
  827. freetree234(todo);
  828. return active;
  829. }
  830. struct game_ui {
  831. int cur_x, cur_y;
  832. bool cur_visible;
  833. };
  834. static game_ui *new_ui(const game_state *state)
  835. {
  836. game_ui *ui = snew(game_ui);
  837. ui->cur_x = 0;
  838. ui->cur_y = -1;
  839. ui->cur_visible = getenv_bool("PUZZLES_SHOW_CURSOR", false);
  840. return ui;
  841. }
  842. static void free_ui(game_ui *ui)
  843. {
  844. sfree(ui);
  845. }
  846. /* ----------------------------------------------------------------------
  847. * Process a move.
  848. */
  849. static void slide_row_int(int w, int h, unsigned char *tiles, int dir, int row)
  850. {
  851. int x = dir > 0 ? -1 : w;
  852. int tx = x + dir;
  853. int n = w - 1;
  854. unsigned char endtile;
  855. assert(0 <= tx && tx < w);
  856. endtile = tiles[row * w + tx];
  857. do {
  858. x = tx;
  859. tx = (x + dir + w) % w;
  860. tiles[row * w + x] = tiles[row * w + tx];
  861. } while (--n > 0);
  862. tiles[row * w + tx] = endtile;
  863. }
  864. static void slide_col_int(int w, int h, unsigned char *tiles, int dir, int col)
  865. {
  866. int y = dir > 0 ? -1 : h;
  867. int ty = y + dir;
  868. int n = h - 1;
  869. unsigned char endtile;
  870. assert(0 <= ty && ty < h);
  871. endtile = tiles[ty * w + col];
  872. do {
  873. y = ty;
  874. ty = (y + dir + h) % h;
  875. tiles[y * w + col] = tiles[ty * w + col];
  876. } while (--n > 0);
  877. tiles[ty * w + col] = endtile;
  878. }
  879. static void slide_row(game_state *state, int dir, int row)
  880. {
  881. slide_row_int(state->width, state->height, state->tiles, dir, row);
  882. }
  883. static void slide_col(game_state *state, int dir, int col)
  884. {
  885. slide_col_int(state->width, state->height, state->tiles, dir, col);
  886. }
  887. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  888. const game_state *newstate)
  889. {
  890. }
  891. struct game_drawstate {
  892. bool started;
  893. int width, height;
  894. int tilesize;
  895. unsigned char *visible;
  896. int cur_x, cur_y;
  897. };
  898. static const char *current_key_label(const game_ui *ui,
  899. const game_state *state, int button)
  900. {
  901. if (IS_CURSOR_SELECT(button) && ui->cur_visible)
  902. return "Slide";
  903. return "";
  904. }
  905. static char *interpret_move(const game_state *state, game_ui *ui,
  906. const game_drawstate *ds,
  907. int x, int y, int button)
  908. {
  909. int cx, cy;
  910. int dx, dy;
  911. char buf[80];
  912. button &= ~MOD_MASK;
  913. if (IS_CURSOR_MOVE(button)) {
  914. int cpos, diff = 0;
  915. cpos = c2pos(state->width, state->height, ui->cur_x, ui->cur_y);
  916. diff = c2diff(state->width, state->height, ui->cur_x, ui->cur_y, button);
  917. if (diff != 0) {
  918. do { /* we might have to do this more than once to skip missing arrows */
  919. cpos += diff;
  920. pos2c(state->width, state->height, cpos, &ui->cur_x, &ui->cur_y);
  921. } while (ui->cur_x == state->cx || ui->cur_y == state->cy);
  922. }
  923. ui->cur_visible = true;
  924. return MOVE_UI_UPDATE;
  925. }
  926. if (button == LEFT_BUTTON || button == RIGHT_BUTTON) {
  927. cx = (x - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
  928. cy = (y - (BORDER + WINDOW_OFFSET + TILE_BORDER) + 2*TILE_SIZE) / TILE_SIZE - 2;
  929. ui->cur_visible = false;
  930. } else if (IS_CURSOR_SELECT(button)) {
  931. if (ui->cur_visible) {
  932. cx = ui->cur_x;
  933. cy = ui->cur_y;
  934. } else {
  935. /* 'click' when cursor is invisible just makes cursor visible. */
  936. ui->cur_visible = true;
  937. return MOVE_UI_UPDATE;
  938. }
  939. } else
  940. return NULL;
  941. if (cy >= 0 && cy < state->height && cy != state->cy)
  942. {
  943. if (cx == -1) dx = +1;
  944. else if (cx == state->width) dx = -1;
  945. else return NULL;
  946. dy = 0;
  947. }
  948. else if (cx >= 0 && cx < state->width && cx != state->cx)
  949. {
  950. if (cy == -1) dy = +1;
  951. else if (cy == state->height) dy = -1;
  952. else return NULL;
  953. dx = 0;
  954. }
  955. else
  956. return NULL;
  957. /* reverse direction if right hand button is pressed */
  958. if (button == RIGHT_BUTTON)
  959. {
  960. dx = -dx;
  961. dy = -dy;
  962. }
  963. if (dx == 0)
  964. sprintf(buf, "C%d,%d", cx, dy);
  965. else
  966. sprintf(buf, "R%d,%d", cy, dx);
  967. return dupstr(buf);
  968. }
  969. static game_state *execute_move(const game_state *from, const char *move)
  970. {
  971. game_state *ret;
  972. int c, d;
  973. bool col;
  974. if ((move[0] == 'C' || move[0] == 'R') &&
  975. sscanf(move+1, "%d,%d", &c, &d) == 2 &&
  976. c >= 0 && c < (move[0] == 'C' ? from->width : from->height) &&
  977. d <= (move[0] == 'C' ? from->height : from->width) &&
  978. d >= -(move[0] == 'C' ? from->height : from->width) && d != 0) {
  979. col = (move[0] == 'C');
  980. } else if (move[0] == 'S' &&
  981. strlen(move) == from->width * from->height + 1) {
  982. int i;
  983. ret = dup_game(from);
  984. ret->used_solve = true;
  985. ret->completed = ret->move_count = 1;
  986. for (i = 0; i < from->width * from->height; i++) {
  987. c = move[i+1];
  988. if (c >= '0' && c <= '9')
  989. c -= '0';
  990. else if (c >= 'A' && c <= 'F')
  991. c -= 'A' - 10;
  992. else if (c >= 'a' && c <= 'f')
  993. c -= 'a' - 10;
  994. else {
  995. free_game(ret);
  996. return NULL;
  997. }
  998. ret->tiles[i] = c;
  999. }
  1000. return ret;
  1001. } else
  1002. return NULL; /* can't parse move string */
  1003. ret = dup_game(from);
  1004. if (col)
  1005. slide_col(ret, d, c);
  1006. else
  1007. slide_row(ret, d, c);
  1008. ret->move_count++;
  1009. ret->last_move_row = col ? -1 : c;
  1010. ret->last_move_col = col ? c : -1;
  1011. ret->last_move_dir = d;
  1012. /*
  1013. * See if the game has been completed.
  1014. */
  1015. if (!ret->completed) {
  1016. unsigned char *active = compute_active(ret, -1, -1);
  1017. int x1, y1;
  1018. bool complete = true;
  1019. for (x1 = 0; x1 < ret->width; x1++)
  1020. for (y1 = 0; y1 < ret->height; y1++)
  1021. if (!index(ret, active, x1, y1)) {
  1022. complete = false;
  1023. goto break_label; /* break out of two loops at once */
  1024. }
  1025. break_label:
  1026. sfree(active);
  1027. if (complete)
  1028. ret->completed = ret->move_count;
  1029. }
  1030. return ret;
  1031. }
  1032. /* ----------------------------------------------------------------------
  1033. * Routines for drawing the game position on the screen.
  1034. */
  1035. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1036. {
  1037. game_drawstate *ds = snew(game_drawstate);
  1038. ds->started = false;
  1039. ds->width = state->width;
  1040. ds->height = state->height;
  1041. ds->visible = snewn(state->width * state->height, unsigned char);
  1042. ds->tilesize = 0; /* not decided yet */
  1043. memset(ds->visible, 0xFF, state->width * state->height);
  1044. ds->cur_x = ds->cur_y = -1;
  1045. return ds;
  1046. }
  1047. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  1048. {
  1049. sfree(ds->visible);
  1050. sfree(ds);
  1051. }
  1052. static void game_compute_size(const game_params *params, int tilesize,
  1053. const game_ui *ui, int *x, int *y)
  1054. {
  1055. /* Ick: fake up `ds->tilesize' for macro expansion purposes */
  1056. struct { int tilesize; } ads, *ds = &ads;
  1057. ads.tilesize = tilesize;
  1058. *x = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->width + TILE_BORDER;
  1059. *y = BORDER * 2 + WINDOW_OFFSET * 2 + TILE_SIZE * params->height + TILE_BORDER;
  1060. }
  1061. static void game_set_size(drawing *dr, game_drawstate *ds,
  1062. const game_params *params, int tilesize)
  1063. {
  1064. ds->tilesize = tilesize;
  1065. }
  1066. static float *game_colours(frontend *fe, int *ncolours)
  1067. {
  1068. float *ret;
  1069. ret = snewn(NCOLOURS * 3, float);
  1070. *ncolours = NCOLOURS;
  1071. /*
  1072. * Basic background colour is whatever the front end thinks is
  1073. * a sensible default.
  1074. */
  1075. frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
  1076. /*
  1077. * Wires are black.
  1078. */
  1079. ret[COL_WIRE * 3 + 0] = 0.0F;
  1080. ret[COL_WIRE * 3 + 1] = 0.0F;
  1081. ret[COL_WIRE * 3 + 2] = 0.0F;
  1082. /*
  1083. * Powered wires and powered endpoints are cyan.
  1084. */
  1085. ret[COL_POWERED * 3 + 0] = 0.0F;
  1086. ret[COL_POWERED * 3 + 1] = 1.0F;
  1087. ret[COL_POWERED * 3 + 2] = 1.0F;
  1088. /*
  1089. * Barriers are red.
  1090. */
  1091. ret[COL_BARRIER * 3 + 0] = 1.0F;
  1092. ret[COL_BARRIER * 3 + 1] = 0.0F;
  1093. ret[COL_BARRIER * 3 + 2] = 0.0F;
  1094. /*
  1095. * Unpowered endpoints are blue.
  1096. */
  1097. ret[COL_ENDPOINT * 3 + 0] = 0.0F;
  1098. ret[COL_ENDPOINT * 3 + 1] = 0.0F;
  1099. ret[COL_ENDPOINT * 3 + 2] = 1.0F;
  1100. /*
  1101. * Tile borders are a darker grey than the background.
  1102. */
  1103. ret[COL_BORDER * 3 + 0] = 0.5F * ret[COL_BACKGROUND * 3 + 0];
  1104. ret[COL_BORDER * 3 + 1] = 0.5F * ret[COL_BACKGROUND * 3 + 1];
  1105. ret[COL_BORDER * 3 + 2] = 0.5F * ret[COL_BACKGROUND * 3 + 2];
  1106. /*
  1107. * Flashing tiles are a grey in between those two.
  1108. */
  1109. ret[COL_FLASHING * 3 + 0] = 0.75F * ret[COL_BACKGROUND * 3 + 0];
  1110. ret[COL_FLASHING * 3 + 1] = 0.75F * ret[COL_BACKGROUND * 3 + 1];
  1111. ret[COL_FLASHING * 3 + 2] = 0.75F * ret[COL_BACKGROUND * 3 + 2];
  1112. ret[COL_LOWLIGHT * 3 + 0] = ret[COL_BACKGROUND * 3 + 0] * 0.8F;
  1113. ret[COL_LOWLIGHT * 3 + 1] = ret[COL_BACKGROUND * 3 + 1] * 0.8F;
  1114. ret[COL_LOWLIGHT * 3 + 2] = ret[COL_BACKGROUND * 3 + 2] * 0.8F;
  1115. ret[COL_TEXT * 3 + 0] = 0.0;
  1116. ret[COL_TEXT * 3 + 1] = 0.0;
  1117. ret[COL_TEXT * 3 + 2] = 0.0;
  1118. return ret;
  1119. }
  1120. static void draw_filled_line(drawing *dr, int x1, int y1, int x2, int y2,
  1121. int colour)
  1122. {
  1123. draw_line(dr, x1-1, y1, x2-1, y2, COL_WIRE);
  1124. draw_line(dr, x1+1, y1, x2+1, y2, COL_WIRE);
  1125. draw_line(dr, x1, y1-1, x2, y2-1, COL_WIRE);
  1126. draw_line(dr, x1, y1+1, x2, y2+1, COL_WIRE);
  1127. draw_line(dr, x1, y1, x2, y2, colour);
  1128. }
  1129. static void draw_rect_coords(drawing *dr, int x1, int y1, int x2, int y2,
  1130. int colour)
  1131. {
  1132. int mx = (x1 < x2 ? x1 : x2);
  1133. int my = (y1 < y2 ? y1 : y2);
  1134. int dx = (x2 + x1 - 2*mx + 1);
  1135. int dy = (y2 + y1 - 2*my + 1);
  1136. draw_rect(dr, mx, my, dx, dy, colour);
  1137. }
  1138. static void draw_barrier_corner(drawing *dr, game_drawstate *ds,
  1139. int x, int y, int dir, int phase)
  1140. {
  1141. int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
  1142. int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
  1143. int x1, y1, dx, dy, dir2;
  1144. dir >>= 4;
  1145. dir2 = A(dir);
  1146. dx = X(dir) + X(dir2);
  1147. dy = Y(dir) + Y(dir2);
  1148. x1 = (dx > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
  1149. y1 = (dy > 0 ? TILE_SIZE+TILE_BORDER-1 : 0);
  1150. if (phase == 0) {
  1151. draw_rect_coords(dr, bx+x1, by+y1,
  1152. bx+x1-TILE_BORDER*dx, by+y1-(TILE_BORDER-1)*dy,
  1153. COL_WIRE);
  1154. draw_rect_coords(dr, bx+x1, by+y1,
  1155. bx+x1-(TILE_BORDER-1)*dx, by+y1-TILE_BORDER*dy,
  1156. COL_WIRE);
  1157. } else {
  1158. draw_rect_coords(dr, bx+x1, by+y1,
  1159. bx+x1-(TILE_BORDER-1)*dx, by+y1-(TILE_BORDER-1)*dy,
  1160. COL_BARRIER);
  1161. }
  1162. }
  1163. static void draw_barrier(drawing *dr, game_drawstate *ds,
  1164. int x, int y, int dir, int phase)
  1165. {
  1166. int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
  1167. int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
  1168. int x1, y1, w, h;
  1169. x1 = (X(dir) > 0 ? TILE_SIZE : X(dir) == 0 ? TILE_BORDER : 0);
  1170. y1 = (Y(dir) > 0 ? TILE_SIZE : Y(dir) == 0 ? TILE_BORDER : 0);
  1171. w = (X(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
  1172. h = (Y(dir) ? TILE_BORDER : TILE_SIZE - TILE_BORDER);
  1173. if (phase == 0) {
  1174. draw_rect(dr, bx+x1-X(dir), by+y1-Y(dir), w, h, COL_WIRE);
  1175. } else {
  1176. draw_rect(dr, bx+x1, by+y1, w, h, COL_BARRIER);
  1177. }
  1178. }
  1179. static void draw_tile(drawing *dr, game_drawstate *ds, const game_state *state,
  1180. int x, int y, int tile, float xshift, float yshift)
  1181. {
  1182. int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x + (int)(xshift * TILE_SIZE);
  1183. int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y + (int)(yshift * TILE_SIZE);
  1184. float cx, cy, ex, ey;
  1185. int dir, col;
  1186. /*
  1187. * When we draw a single tile, we must draw everything up to
  1188. * and including the borders around the tile. This means that
  1189. * if the neighbouring tiles have connections to those borders,
  1190. * we must draw those connections on the borders themselves.
  1191. *
  1192. * This would be terribly fiddly if we ever had to draw a tile
  1193. * while its neighbour was in mid-rotate, because we'd have to
  1194. * arrange to _know_ that the neighbour was being rotated and
  1195. * hence had an anomalous effect on the redraw of this tile.
  1196. * Fortunately, the drawing algorithm avoids ever calling us in
  1197. * this circumstance: we're either drawing lots of straight
  1198. * tiles at game start or after a move is complete, or we're
  1199. * repeatedly drawing only the rotating tile. So no problem.
  1200. */
  1201. /*
  1202. * So. First blank the tile out completely: draw a big
  1203. * rectangle in border colour, and a smaller rectangle in
  1204. * background colour to fill it in.
  1205. */
  1206. draw_rect(dr, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER,
  1207. COL_BORDER);
  1208. draw_rect(dr, bx+TILE_BORDER, by+TILE_BORDER,
  1209. TILE_SIZE-TILE_BORDER, TILE_SIZE-TILE_BORDER,
  1210. tile & FLASHING ? COL_FLASHING : COL_BACKGROUND);
  1211. /*
  1212. * Draw the wires.
  1213. */
  1214. cx = cy = TILE_BORDER + (TILE_SIZE-TILE_BORDER) / 2.0F - 0.5F;
  1215. col = (tile & ACTIVE ? COL_POWERED : COL_WIRE);
  1216. for (dir = 1; dir < 0x10; dir <<= 1) {
  1217. if (tile & dir) {
  1218. ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
  1219. ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
  1220. draw_filled_line(dr, bx+(int)cx, by+(int)cy,
  1221. bx+(int)(cx+ex), by+(int)(cy+ey),
  1222. COL_WIRE);
  1223. }
  1224. }
  1225. for (dir = 1; dir < 0x10; dir <<= 1) {
  1226. if (tile & dir) {
  1227. ex = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * X(dir);
  1228. ey = (TILE_SIZE - TILE_BORDER - 1.0F) / 2.0F * Y(dir);
  1229. draw_line(dr, bx+(int)cx, by+(int)cy,
  1230. bx+(int)(cx+ex), by+(int)(cy+ey), col);
  1231. }
  1232. }
  1233. /*
  1234. * Draw the box in the middle. We do this in blue if the tile
  1235. * is an unpowered endpoint, in cyan if the tile is a powered
  1236. * endpoint, in black if the tile is the centrepiece, and
  1237. * otherwise not at all.
  1238. */
  1239. col = -1;
  1240. if (x == state->cx && y == state->cy)
  1241. col = COL_WIRE;
  1242. else if (COUNT(tile) == 1) {
  1243. col = (tile & ACTIVE ? COL_POWERED : COL_ENDPOINT);
  1244. }
  1245. if (col >= 0) {
  1246. int i, points[8];
  1247. points[0] = +1; points[1] = +1;
  1248. points[2] = +1; points[3] = -1;
  1249. points[4] = -1; points[5] = -1;
  1250. points[6] = -1; points[7] = +1;
  1251. for (i = 0; i < 8; i += 2) {
  1252. ex = (TILE_SIZE * 0.24F) * points[i];
  1253. ey = (TILE_SIZE * 0.24F) * points[i+1];
  1254. points[i] = bx+(int)(cx+ex);
  1255. points[i+1] = by+(int)(cy+ey);
  1256. }
  1257. draw_polygon(dr, points, 4, col, COL_WIRE);
  1258. }
  1259. /*
  1260. * Draw the points on the border if other tiles are connected
  1261. * to us.
  1262. */
  1263. for (dir = 1; dir < 0x10; dir <<= 1) {
  1264. int dx, dy, px, py, lx, ly, vx, vy, ox, oy;
  1265. dx = X(dir);
  1266. dy = Y(dir);
  1267. ox = x + dx;
  1268. oy = y + dy;
  1269. if (ox < 0 || ox >= state->width || oy < 0 || oy >= state->height)
  1270. continue;
  1271. if (!(tile(state, ox, oy) & F(dir)))
  1272. continue;
  1273. px = bx + (int)(dx>0 ? TILE_SIZE + TILE_BORDER - 1 : dx<0 ? 0 : cx);
  1274. py = by + (int)(dy>0 ? TILE_SIZE + TILE_BORDER - 1 : dy<0 ? 0 : cy);
  1275. lx = dx * (TILE_BORDER-1);
  1276. ly = dy * (TILE_BORDER-1);
  1277. vx = (dy ? 1 : 0);
  1278. vy = (dx ? 1 : 0);
  1279. if (xshift == 0.0F && yshift == 0.0F && (tile & dir)) {
  1280. /*
  1281. * If we are fully connected to the other tile, we must
  1282. * draw right across the tile border. (We can use our
  1283. * own ACTIVE state to determine what colour to do this
  1284. * in: if we are fully connected to the other tile then
  1285. * the two ACTIVE states will be the same.)
  1286. */
  1287. draw_rect_coords(dr, px-vx, py-vy, px+lx+vx, py+ly+vy, COL_WIRE);
  1288. draw_rect_coords(dr, px, py, px+lx, py+ly,
  1289. (tile & ACTIVE) ? COL_POWERED : COL_WIRE);
  1290. } else {
  1291. /*
  1292. * The other tile extends into our border, but isn't
  1293. * actually connected to us. Just draw a single black
  1294. * dot.
  1295. */
  1296. draw_rect_coords(dr, px, py, px, py, COL_WIRE);
  1297. }
  1298. }
  1299. draw_update(dr, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
  1300. }
  1301. static void draw_tile_barriers(drawing *dr, game_drawstate *ds,
  1302. const game_state *state, int x, int y)
  1303. {
  1304. int phase;
  1305. int dir;
  1306. int bx = BORDER + WINDOW_OFFSET + TILE_SIZE * x;
  1307. int by = BORDER + WINDOW_OFFSET + TILE_SIZE * y;
  1308. /*
  1309. * Draw barrier corners, and then barriers.
  1310. */
  1311. for (phase = 0; phase < 2; phase++) {
  1312. for (dir = 1; dir < 0x10; dir <<= 1)
  1313. if (barrier(state, x, y) & (dir << 4))
  1314. draw_barrier_corner(dr, ds, x, y, dir << 4, phase);
  1315. for (dir = 1; dir < 0x10; dir <<= 1)
  1316. if (barrier(state, x, y) & dir)
  1317. draw_barrier(dr, ds, x, y, dir, phase);
  1318. }
  1319. draw_update(dr, bx, by, TILE_SIZE+TILE_BORDER, TILE_SIZE+TILE_BORDER);
  1320. }
  1321. static void draw_arrow(drawing *dr, game_drawstate *ds,
  1322. int x, int y, int xdx, int xdy, bool cur)
  1323. {
  1324. int coords[14];
  1325. int ydy = -xdx, ydx = xdy;
  1326. x = x * TILE_SIZE + BORDER + WINDOW_OFFSET;
  1327. y = y * TILE_SIZE + BORDER + WINDOW_OFFSET;
  1328. #define POINT(n, xx, yy) ( \
  1329. coords[2*(n)+0] = x + (xx)*xdx + (yy)*ydx, \
  1330. coords[2*(n)+1] = y + (xx)*xdy + (yy)*ydy)
  1331. POINT(0, TILE_SIZE / 2, 3 * TILE_SIZE / 4); /* top of arrow */
  1332. POINT(1, 3 * TILE_SIZE / 4, TILE_SIZE / 2); /* right corner */
  1333. POINT(2, 5 * TILE_SIZE / 8, TILE_SIZE / 2); /* right concave */
  1334. POINT(3, 5 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom right */
  1335. POINT(4, 3 * TILE_SIZE / 8, TILE_SIZE / 4); /* bottom left */
  1336. POINT(5, 3 * TILE_SIZE / 8, TILE_SIZE / 2); /* left concave */
  1337. POINT(6, TILE_SIZE / 4, TILE_SIZE / 2); /* left corner */
  1338. draw_polygon(dr, coords, 7, cur ? COL_POWERED : COL_LOWLIGHT, COL_TEXT);
  1339. }
  1340. static void draw_arrow_for_cursor(drawing *dr, game_drawstate *ds,
  1341. int cur_x, int cur_y, bool cur)
  1342. {
  1343. if (cur_x == -1 && cur_y == -1)
  1344. return; /* 'no cursur here */
  1345. else if (cur_x == -1) /* LH column. */
  1346. draw_arrow(dr, ds, 0, cur_y+1, 0, -1, cur);
  1347. else if (cur_x == ds->width) /* RH column */
  1348. draw_arrow(dr, ds, ds->width, cur_y, 0, +1, cur);
  1349. else if (cur_y == -1) /* Top row */
  1350. draw_arrow(dr, ds, cur_x, 0, +1, 0, cur);
  1351. else if (cur_y == ds->height) /* Bottom row */
  1352. draw_arrow(dr, ds, cur_x+1, ds->height, -1, 0, cur);
  1353. else
  1354. assert(!"Invalid cursor position");
  1355. draw_update(dr,
  1356. cur_x * TILE_SIZE + BORDER + WINDOW_OFFSET,
  1357. cur_y * TILE_SIZE + BORDER + WINDOW_OFFSET,
  1358. TILE_SIZE, TILE_SIZE);
  1359. }
  1360. static void game_redraw(drawing *dr, game_drawstate *ds,
  1361. const game_state *oldstate, const game_state *state,
  1362. int dir, const game_ui *ui,
  1363. float t, float ft)
  1364. {
  1365. int x, y, frame;
  1366. unsigned char *active;
  1367. float xshift = 0.0;
  1368. float yshift = 0.0;
  1369. int cur_x = -1, cur_y = -1;
  1370. /*
  1371. * Draw the exterior barrier lines if this is our first call.
  1372. */
  1373. if (!ds->started) {
  1374. int phase;
  1375. ds->started = true;
  1376. for (phase = 0; phase < 2; phase++) {
  1377. for (x = 0; x < ds->width; x++) {
  1378. if (barrier(state, x, 0) & UL)
  1379. draw_barrier_corner(dr, ds, x, -1, LD, phase);
  1380. if (barrier(state, x, 0) & RU)
  1381. draw_barrier_corner(dr, ds, x, -1, DR, phase);
  1382. if (barrier(state, x, 0) & U)
  1383. draw_barrier(dr, ds, x, -1, D, phase);
  1384. if (barrier(state, x, ds->height-1) & DR)
  1385. draw_barrier_corner(dr, ds, x, ds->height, RU, phase);
  1386. if (barrier(state, x, ds->height-1) & LD)
  1387. draw_barrier_corner(dr, ds, x, ds->height, UL, phase);
  1388. if (barrier(state, x, ds->height-1) & D)
  1389. draw_barrier(dr, ds, x, ds->height, U, phase);
  1390. }
  1391. for (y = 0; y < ds->height; y++) {
  1392. if (barrier(state, 0, y) & UL)
  1393. draw_barrier_corner(dr, ds, -1, y, RU, phase);
  1394. if (barrier(state, 0, y) & LD)
  1395. draw_barrier_corner(dr, ds, -1, y, DR, phase);
  1396. if (barrier(state, 0, y) & L)
  1397. draw_barrier(dr, ds, -1, y, R, phase);
  1398. if (barrier(state, ds->width-1, y) & RU)
  1399. draw_barrier_corner(dr, ds, ds->width, y, UL, phase);
  1400. if (barrier(state, ds->width-1, y) & DR)
  1401. draw_barrier_corner(dr, ds, ds->width, y, LD, phase);
  1402. if (barrier(state, ds->width-1, y) & R)
  1403. draw_barrier(dr, ds, ds->width, y, L, phase);
  1404. }
  1405. }
  1406. /*
  1407. * Arrows for making moves.
  1408. */
  1409. for (x = 0; x < ds->width; x++) {
  1410. if (x == state->cx) continue;
  1411. draw_arrow(dr, ds, x, 0, +1, 0, false);
  1412. draw_arrow(dr, ds, x+1, ds->height, -1, 0, false);
  1413. }
  1414. for (y = 0; y < ds->height; y++) {
  1415. if (y == state->cy) continue;
  1416. draw_arrow(dr, ds, ds->width, y, 0, +1, false);
  1417. draw_arrow(dr, ds, 0, y+1, 0, -1, false);
  1418. }
  1419. }
  1420. if (ui->cur_visible) {
  1421. cur_x = ui->cur_x; cur_y = ui->cur_y;
  1422. }
  1423. if (cur_x != ds->cur_x || cur_y != ds->cur_y) {
  1424. /* Cursor has changed; redraw two (prev and curr) arrows. */
  1425. assert(cur_x != state->cx && cur_y != state->cy);
  1426. draw_arrow_for_cursor(dr, ds, cur_x, cur_y, true);
  1427. draw_arrow_for_cursor(dr, ds, ds->cur_x, ds->cur_y, false);
  1428. ds->cur_x = cur_x; ds->cur_y = cur_y;
  1429. }
  1430. /* Check if this is an undo. If so, we will need to run any animation
  1431. * backwards.
  1432. */
  1433. if (oldstate && oldstate->move_count > state->move_count) {
  1434. const game_state * tmpstate = state;
  1435. state = oldstate;
  1436. oldstate = tmpstate;
  1437. t = ANIM_TIME - t;
  1438. }
  1439. if (oldstate && (t < ANIM_TIME)) {
  1440. /*
  1441. * We're animating a slide, of row/column number
  1442. * state->last_move_pos, in direction
  1443. * state->last_move_dir
  1444. */
  1445. xshift = state->last_move_row == -1 ? 0.0F :
  1446. (1 - t / ANIM_TIME) * state->last_move_dir;
  1447. yshift = state->last_move_col == -1 ? 0.0F :
  1448. (1 - t / ANIM_TIME) * state->last_move_dir;
  1449. }
  1450. frame = -1;
  1451. if (ft > 0) {
  1452. /*
  1453. * We're animating a completion flash. Find which frame
  1454. * we're at.
  1455. */
  1456. frame = (int)(ft / FLASH_FRAME);
  1457. }
  1458. /*
  1459. * Draw any tile which differs from the way it was last drawn.
  1460. */
  1461. if (xshift != 0.0F || yshift != 0.0F) {
  1462. active = compute_active(state,
  1463. state->last_move_row, state->last_move_col);
  1464. } else {
  1465. active = compute_active(state, -1, -1);
  1466. }
  1467. clip(dr,
  1468. BORDER + WINDOW_OFFSET, BORDER + WINDOW_OFFSET,
  1469. TILE_SIZE * state->width + TILE_BORDER,
  1470. TILE_SIZE * state->height + TILE_BORDER);
  1471. for (x = 0; x < ds->width; x++)
  1472. for (y = 0; y < ds->height; y++) {
  1473. unsigned char c = tile(state, x, y) | index(state, active, x, y);
  1474. /*
  1475. * In a completion flash, we adjust the FLASHING bit
  1476. * depending on our distance from the centre point and
  1477. * the frame number.
  1478. */
  1479. if (frame >= 0) {
  1480. int xdist, ydist, dist;
  1481. xdist = (x < state->cx ? state->cx - x : x - state->cx);
  1482. ydist = (y < state->cy ? state->cy - y : y - state->cy);
  1483. dist = (xdist > ydist ? xdist : ydist);
  1484. if (frame >= dist && frame < dist+4) {
  1485. int flash = (frame - dist) & 1;
  1486. flash = flash ? FLASHING : 0;
  1487. c = (c &~ FLASHING) | flash;
  1488. }
  1489. }
  1490. if (index(state, ds->visible, x, y) != c ||
  1491. index(state, ds->visible, x, y) == 0xFF ||
  1492. (x == state->last_move_col || y == state->last_move_row))
  1493. {
  1494. float xs = (y == state->last_move_row ? xshift : (float)0.0);
  1495. float ys = (x == state->last_move_col ? yshift : (float)0.0);
  1496. draw_tile(dr, ds, state, x, y, c, xs, ys);
  1497. if (xs < 0 && x == 0)
  1498. draw_tile(dr, ds, state, state->width, y, c, xs, ys);
  1499. else if (xs > 0 && x == state->width - 1)
  1500. draw_tile(dr, ds, state, -1, y, c, xs, ys);
  1501. else if (ys < 0 && y == 0)
  1502. draw_tile(dr, ds, state, x, state->height, c, xs, ys);
  1503. else if (ys > 0 && y == state->height - 1)
  1504. draw_tile(dr, ds, state, x, -1, c, xs, ys);
  1505. if (x == state->last_move_col || y == state->last_move_row)
  1506. index(state, ds->visible, x, y) = 0xFF;
  1507. else
  1508. index(state, ds->visible, x, y) = c;
  1509. }
  1510. }
  1511. for (x = 0; x < ds->width; x++)
  1512. for (y = 0; y < ds->height; y++)
  1513. draw_tile_barriers(dr, ds, state, x, y);
  1514. unclip(dr);
  1515. /*
  1516. * Update the status bar.
  1517. */
  1518. {
  1519. char statusbuf[256];
  1520. int i, n, a;
  1521. n = state->width * state->height;
  1522. for (i = a = 0; i < n; i++)
  1523. if (active[i])
  1524. a++;
  1525. if (state->used_solve)
  1526. sprintf(statusbuf, "Moves since auto-solve: %d",
  1527. state->move_count - state->completed);
  1528. else
  1529. sprintf(statusbuf, "%sMoves: %d",
  1530. (state->completed ? "COMPLETED! " : ""),
  1531. (state->completed ? state->completed : state->move_count));
  1532. if (state->movetarget)
  1533. sprintf(statusbuf + strlen(statusbuf), " (target %d)",
  1534. state->movetarget);
  1535. sprintf(statusbuf + strlen(statusbuf), " Active: %d/%d", a, n);
  1536. status_bar(dr, statusbuf);
  1537. }
  1538. sfree(active);
  1539. }
  1540. static float game_anim_length(const game_state *oldstate,
  1541. const game_state *newstate, int dir, game_ui *ui)
  1542. {
  1543. return ANIM_TIME;
  1544. }
  1545. static float game_flash_length(const game_state *oldstate,
  1546. const game_state *newstate, int dir, game_ui *ui)
  1547. {
  1548. /*
  1549. * If the game has just been completed, we display a completion
  1550. * flash.
  1551. */
  1552. if (!oldstate->completed && newstate->completed &&
  1553. !oldstate->used_solve && !newstate->used_solve) {
  1554. int size;
  1555. size = 0;
  1556. if (size < newstate->cx+1)
  1557. size = newstate->cx+1;
  1558. if (size < newstate->cy+1)
  1559. size = newstate->cy+1;
  1560. if (size < newstate->width - newstate->cx)
  1561. size = newstate->width - newstate->cx;
  1562. if (size < newstate->height - newstate->cy)
  1563. size = newstate->height - newstate->cy;
  1564. return FLASH_FRAME * (size+4);
  1565. }
  1566. return 0.0F;
  1567. }
  1568. static void game_get_cursor_location(const game_ui *ui,
  1569. const game_drawstate *ds,
  1570. const game_state *state,
  1571. const game_params *params,
  1572. int *x, int *y, int *w, int *h)
  1573. {
  1574. if(ui->cur_visible) {
  1575. *x = BORDER + WINDOW_OFFSET + TILE_SIZE * ui->cur_x;
  1576. *y = BORDER + WINDOW_OFFSET + TILE_SIZE * ui->cur_y;
  1577. *w = *h = TILE_SIZE;
  1578. }
  1579. }
  1580. static int game_status(const game_state *state)
  1581. {
  1582. return state->completed ? +1 : 0;
  1583. }
  1584. #ifdef COMBINED
  1585. #define thegame netslide
  1586. #endif
  1587. const struct game thegame = {
  1588. "Netslide", "games.netslide", "netslide",
  1589. default_params,
  1590. game_fetch_preset, NULL,
  1591. decode_params,
  1592. encode_params,
  1593. free_params,
  1594. dup_params,
  1595. true, game_configure, custom_params,
  1596. validate_params,
  1597. new_game_desc,
  1598. validate_desc,
  1599. new_game,
  1600. dup_game,
  1601. free_game,
  1602. true, solve_game,
  1603. false, NULL, NULL, /* can_format_as_text_now, text_format */
  1604. NULL, NULL, /* get_prefs, set_prefs */
  1605. new_ui,
  1606. free_ui,
  1607. NULL, /* encode_ui */
  1608. NULL, /* decode_ui */
  1609. NULL, /* game_request_keys */
  1610. game_changed_state,
  1611. current_key_label,
  1612. interpret_move,
  1613. execute_move,
  1614. PREFERRED_TILE_SIZE, game_compute_size, game_set_size,
  1615. game_colours,
  1616. game_new_drawstate,
  1617. game_free_drawstate,
  1618. game_redraw,
  1619. game_anim_length,
  1620. game_flash_length,
  1621. game_get_cursor_location,
  1622. game_status,
  1623. false, false, NULL, NULL, /* print_size, print */
  1624. true, /* wants_statusbar */
  1625. false, NULL, /* timing_state */
  1626. 0, /* flags */
  1627. };
  1628. /* vim: set shiftwidth=4 tabstop=8: */