slide.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445
  1. /*
  2. * slide.c: Implementation of the block-sliding puzzle `Klotski'.
  3. */
  4. /*
  5. * TODO:
  6. *
  7. * - Improve the generator.
  8. * * actually, we seem to be mostly sensible already now. I
  9. * want more choice over the type of main block and location
  10. * of the exit/target, and I think I probably ought to give
  11. * up on compactness and just bite the bullet and have the
  12. * target area right outside the main wall, but mostly I
  13. * think it's OK.
  14. * * the move limit tends to make the game _slower_ to
  15. * generate, which is odd. Perhaps investigate why.
  16. *
  17. * - Improve the graphics.
  18. * * All the colours are a bit wishy-washy. _Some_ dark
  19. * colours would surely not be excessive? Probably darken
  20. * the tiles, the walls and the main block, and leave the
  21. * target marker pale.
  22. * * The cattle grid effect is still disgusting. Think of
  23. * something completely different.
  24. * * The highlight for next-piece-to-move in the solver is
  25. * excessive, and the shadow blends in too well with the
  26. * piece lowlights. Adjust both.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <ctype.h>
  33. #ifdef NO_TGMATH_H
  34. # include <math.h>
  35. #else
  36. # include <tgmath.h>
  37. #endif
  38. #include "puzzles.h"
  39. #include "tree234.h"
  40. /*
  41. * The implementation of this game revolves around the insight
  42. * which makes an exhaustive-search solver feasible: although
  43. * there are many blocks which can be rearranged in many ways, any
  44. * two blocks of the same shape are _indistinguishable_ and hence
  45. * the number of _distinct_ board layouts is generally much
  46. * smaller. So we adopt a representation for board layouts which
  47. * is inherently canonical, i.e. there are no two distinct
  48. * representations which encode indistinguishable layouts.
  49. *
  50. * The way we do this is to encode each square of the board, in
  51. * the normal left-to-right top-to-bottom order, as being one of
  52. * the following things:
  53. * - the first square (in the given order) of a block (`anchor')
  54. * - special case of the above: the anchor for the _main_ block
  55. * (i.e. the one which the aim of the game is to get to the
  56. * target position)
  57. * - a subsequent square of a block whose previous square was N
  58. * squares ago
  59. * - an impassable wall
  60. *
  61. * (We also separately store data about which board positions are
  62. * forcefields only passable by the main block. We can't encode
  63. * that in the main board data, because then the main block would
  64. * destroy forcefields as it went over them.)
  65. *
  66. * Hence, for example, a 2x2 square block would be encoded as
  67. * ANCHOR, followed by DIST(1), and w-2 squares later on there
  68. * would be DIST(w-1) followed by DIST(1). So if you start at the
  69. * last of those squares, the DIST numbers give you a linked list
  70. * pointing back through all the other squares in the same block.
  71. *
  72. * So the solver simply does a bfs over all reachable positions,
  73. * encoding them in this format and storing them in a tree234 to
  74. * ensure it doesn't ever revisit an already-analysed position.
  75. */
  76. enum {
  77. /*
  78. * The colours are arranged here so that every base colour is
  79. * directly followed by its highlight colour and then its
  80. * lowlight colour. Do not break this, or draw_tile() will get
  81. * confused.
  82. */
  83. COL_BACKGROUND,
  84. COL_HIGHLIGHT,
  85. COL_LOWLIGHT,
  86. COL_DRAGGING,
  87. COL_DRAGGING_HIGHLIGHT,
  88. COL_DRAGGING_LOWLIGHT,
  89. COL_MAIN,
  90. COL_MAIN_HIGHLIGHT,
  91. COL_MAIN_LOWLIGHT,
  92. COL_MAIN_DRAGGING,
  93. COL_MAIN_DRAGGING_HIGHLIGHT,
  94. COL_MAIN_DRAGGING_LOWLIGHT,
  95. COL_TARGET,
  96. COL_TARGET_HIGHLIGHT,
  97. COL_TARGET_LOWLIGHT,
  98. NCOLOURS
  99. };
  100. /*
  101. * Board layout is a simple array of bytes. Each byte holds:
  102. */
  103. #define ANCHOR 255 /* top-left-most square of some piece */
  104. #define MAINANCHOR 254 /* anchor of _main_ piece */
  105. #define EMPTY 253 /* empty square */
  106. #define WALL 252 /* immovable wall */
  107. #define MAXDIST 251
  108. /* all other values indicate distance back to previous square of same block */
  109. #define ISDIST(x) ( (unsigned char)((x)-1) <= MAXDIST-1 )
  110. #define DIST(x) (x)
  111. #define ISANCHOR(x) ( (x)==ANCHOR || (x)==MAINANCHOR )
  112. #define ISBLOCK(x) ( ISANCHOR(x) || ISDIST(x) )
  113. /*
  114. * MAXDIST is the largest DIST value we can encode. This must
  115. * therefore also be the maximum puzzle width in theory (although
  116. * solver running time will dictate a much smaller limit in
  117. * practice).
  118. */
  119. #define MAXWID MAXDIST
  120. struct game_params {
  121. int w, h;
  122. int maxmoves;
  123. };
  124. struct game_immutable_state {
  125. int refcount;
  126. bool *forcefield;
  127. };
  128. struct game_solution {
  129. int nmoves;
  130. int *moves; /* just like from solve_board() */
  131. int refcount;
  132. };
  133. struct game_state {
  134. int w, h;
  135. unsigned char *board;
  136. int tx, ty; /* target coords for MAINANCHOR */
  137. int minmoves; /* for display only */
  138. int lastmoved, lastmoved_pos; /* for move counting */
  139. int movecount;
  140. int completed;
  141. bool cheated;
  142. struct game_immutable_state *imm;
  143. struct game_solution *soln;
  144. int soln_index;
  145. };
  146. static game_params *default_params(void)
  147. {
  148. game_params *ret = snew(game_params);
  149. ret->w = 7;
  150. ret->h = 6;
  151. ret->maxmoves = 40;
  152. return ret;
  153. }
  154. static const struct game_params slide_presets[] = {
  155. {7, 6, 25},
  156. {7, 6, -1},
  157. {8, 6, -1},
  158. };
  159. static bool game_fetch_preset(int i, char **name, game_params **params)
  160. {
  161. game_params *ret;
  162. char str[80];
  163. if (i < 0 || i >= lenof(slide_presets))
  164. return false;
  165. ret = snew(game_params);
  166. *ret = slide_presets[i];
  167. sprintf(str, "%dx%d", ret->w, ret->h);
  168. if (ret->maxmoves >= 0)
  169. sprintf(str + strlen(str), ", max %d moves", ret->maxmoves);
  170. else
  171. sprintf(str + strlen(str), ", no move limit");
  172. *name = dupstr(str);
  173. *params = ret;
  174. return true;
  175. }
  176. static void free_params(game_params *params)
  177. {
  178. sfree(params);
  179. }
  180. static game_params *dup_params(const game_params *params)
  181. {
  182. game_params *ret = snew(game_params);
  183. *ret = *params; /* structure copy */
  184. return ret;
  185. }
  186. static void decode_params(game_params *params, char const *string)
  187. {
  188. params->w = params->h = atoi(string);
  189. while (*string && isdigit((unsigned char)*string)) string++;
  190. if (*string == 'x') {
  191. string++;
  192. params->h = atoi(string);
  193. while (*string && isdigit((unsigned char)*string)) string++;
  194. }
  195. if (*string == 'm') {
  196. string++;
  197. params->maxmoves = atoi(string);
  198. while (*string && isdigit((unsigned char)*string)) string++;
  199. } else if (*string == 'u') {
  200. string++;
  201. params->maxmoves = -1;
  202. }
  203. }
  204. static char *encode_params(const game_params *params, bool full)
  205. {
  206. char data[256];
  207. sprintf(data, "%dx%d", params->w, params->h);
  208. if (params->maxmoves >= 0)
  209. sprintf(data + strlen(data), "m%d", params->maxmoves);
  210. else
  211. sprintf(data + strlen(data), "u");
  212. return dupstr(data);
  213. }
  214. static config_item *game_configure(const game_params *params)
  215. {
  216. config_item *ret;
  217. char buf[80];
  218. ret = snewn(4, config_item);
  219. ret[0].name = "Width";
  220. ret[0].type = C_STRING;
  221. sprintf(buf, "%d", params->w);
  222. ret[0].u.string.sval = dupstr(buf);
  223. ret[1].name = "Height";
  224. ret[1].type = C_STRING;
  225. sprintf(buf, "%d", params->h);
  226. ret[1].u.string.sval = dupstr(buf);
  227. ret[2].name = "Solution length limit";
  228. ret[2].type = C_STRING;
  229. sprintf(buf, "%d", params->maxmoves);
  230. ret[2].u.string.sval = dupstr(buf);
  231. ret[3].name = NULL;
  232. ret[3].type = C_END;
  233. return ret;
  234. }
  235. static game_params *custom_params(const config_item *cfg)
  236. {
  237. game_params *ret = snew(game_params);
  238. ret->w = atoi(cfg[0].u.string.sval);
  239. ret->h = atoi(cfg[1].u.string.sval);
  240. ret->maxmoves = atoi(cfg[2].u.string.sval);
  241. return ret;
  242. }
  243. static const char *validate_params(const game_params *params, bool full)
  244. {
  245. if (params->w > MAXWID)
  246. return "Width must be at most " STR(MAXWID);
  247. if (params->w < 5)
  248. return "Width must be at least 5";
  249. if (params->h < 4)
  250. return "Height must be at least 4";
  251. return NULL;
  252. }
  253. static char *board_text_format(int w, int h, unsigned char *data,
  254. bool *forcefield)
  255. {
  256. int wh = w*h;
  257. DSF *dsf = dsf_new(wh);
  258. int i, x, y;
  259. int retpos, retlen = (w*2+2)*(h*2+1)+1;
  260. char *ret = snewn(retlen, char);
  261. for (i = 0; i < wh; i++)
  262. if (ISDIST(data[i]))
  263. dsf_merge(dsf, i - data[i], i);
  264. retpos = 0;
  265. for (y = 0; y < 2*h+1; y++) {
  266. for (x = 0; x < 2*w+1; x++) {
  267. int v;
  268. int i = (y/2)*w+(x/2);
  269. #define dtype(i) (ISBLOCK(data[i]) ? \
  270. dsf_canonify(dsf, i) : data[i])
  271. #define dchar(t) ((t)==EMPTY ? ' ' : (t)==WALL ? '#' : \
  272. data[t] == MAINANCHOR ? '*' : '%')
  273. if (y % 2 && x % 2) {
  274. int j = dtype(i);
  275. v = dchar(j);
  276. } else if (y % 2 && !(x % 2)) {
  277. int j1 = (x > 0 ? dtype(i-1) : -1);
  278. int j2 = (x < 2*w ? dtype(i) : -1);
  279. if (j1 != j2)
  280. v = '|';
  281. else
  282. v = dchar(j1);
  283. } else if (!(y % 2) && (x % 2)) {
  284. int j1 = (y > 0 ? dtype(i-w) : -1);
  285. int j2 = (y < 2*h ? dtype(i) : -1);
  286. if (j1 != j2)
  287. v = '-';
  288. else
  289. v = dchar(j1);
  290. } else {
  291. int j1 = (x > 0 && y > 0 ? dtype(i-w-1) : -1);
  292. int j2 = (x > 0 && y < 2*h ? dtype(i-1) : -1);
  293. int j3 = (x < 2*w && y > 0 ? dtype(i-w) : -1);
  294. int j4 = (x < 2*w && y < 2*h ? dtype(i) : -1);
  295. if (j1 == j2 && j2 == j3 && j3 == j4)
  296. v = dchar(j1);
  297. else if (j1 == j2 && j3 == j4)
  298. v = '|';
  299. else if (j1 == j3 && j2 == j4)
  300. v = '-';
  301. else
  302. v = '+';
  303. }
  304. assert(retpos < retlen);
  305. ret[retpos++] = v;
  306. }
  307. assert(retpos < retlen);
  308. ret[retpos++] = '\n';
  309. }
  310. assert(retpos < retlen);
  311. ret[retpos++] = '\0';
  312. assert(retpos == retlen);
  313. return ret;
  314. }
  315. /* ----------------------------------------------------------------------
  316. * Solver.
  317. */
  318. /*
  319. * During solver execution, the set of visited board positions is
  320. * stored as a tree234 of the following structures. `w', `h' and
  321. * `data' are obvious in meaning; `dist' represents the minimum
  322. * distance to reach this position from the starting point.
  323. *
  324. * `prev' links each board to the board position from which it was
  325. * most efficiently derived.
  326. */
  327. struct board {
  328. int w, h;
  329. int dist;
  330. struct board *prev;
  331. unsigned char *data;
  332. };
  333. static int boardcmp(void *av, void *bv)
  334. {
  335. struct board *a = (struct board *)av;
  336. struct board *b = (struct board *)bv;
  337. return memcmp(a->data, b->data, a->w * a->h);
  338. }
  339. static struct board *newboard(int w, int h, unsigned char *data)
  340. {
  341. struct board *b = malloc(sizeof(struct board) + w*h);
  342. b->data = (unsigned char *)b + sizeof(struct board);
  343. memcpy(b->data, data, w*h);
  344. b->w = w;
  345. b->h = h;
  346. b->dist = -1;
  347. b->prev = NULL;
  348. return b;
  349. }
  350. /*
  351. * The actual solver. Given a board, attempt to find the minimum
  352. * length of move sequence which moves MAINANCHOR to (tx,ty), or
  353. * -1 if no solution exists. Returns that minimum length.
  354. *
  355. * Also, if `moveout' is provided, writes out the moves in the
  356. * form of a sequence of pairs of integers indicating the source
  357. * and destination points of the anchor of the moved piece in each
  358. * move. Exactly twice as many integers are written as the number
  359. * returned from solve_board(), and `moveout' receives an int *
  360. * which is a pointer to a dynamically allocated array.
  361. */
  362. static int solve_board(int w, int h, unsigned char *board,
  363. bool *forcefield, int tx, int ty,
  364. int movelimit, int **moveout)
  365. {
  366. int wh = w*h;
  367. struct board *b, *b2, *b3;
  368. int *next, *which;
  369. bool *anchors, *movereached;
  370. int *movequeue, mqhead, mqtail;
  371. tree234 *sorted, *queue;
  372. int i, j, dir;
  373. int qlen, lastdist;
  374. int ret;
  375. #ifdef SOLVER_DIAGNOSTICS
  376. {
  377. char *t = board_text_format(w, h, board);
  378. for (i = 0; i < h; i++) {
  379. for (j = 0; j < w; j++) {
  380. int c = board[i*w+j];
  381. if (ISDIST(c))
  382. printf("D%-3d", c);
  383. else if (c == MAINANCHOR)
  384. printf("M ");
  385. else if (c == ANCHOR)
  386. printf("A ");
  387. else if (c == WALL)
  388. printf("W ");
  389. else if (c == EMPTY)
  390. printf("E ");
  391. }
  392. printf("\n");
  393. }
  394. printf("Starting solver for:\n%s\n", t);
  395. sfree(t);
  396. }
  397. #endif
  398. sorted = newtree234(boardcmp);
  399. queue = newtree234(NULL);
  400. b = newboard(w, h, board);
  401. b->dist = 0;
  402. add234(sorted, b);
  403. addpos234(queue, b, 0);
  404. qlen = 1;
  405. next = snewn(wh, int);
  406. anchors = snewn(wh, bool);
  407. which = snewn(wh, int);
  408. movereached = snewn(wh, bool);
  409. movequeue = snewn(wh, int);
  410. lastdist = -1;
  411. while ((b = delpos234(queue, 0)) != NULL) {
  412. qlen--;
  413. if (movelimit >= 0 && b->dist >= movelimit) {
  414. /*
  415. * The problem is not soluble in under `movelimit'
  416. * moves, so we can quit right now.
  417. */
  418. b2 = NULL;
  419. goto done;
  420. }
  421. if (b->dist != lastdist) {
  422. #ifdef SOLVER_DIAGNOSTICS
  423. printf("dist %d (%d)\n", b->dist, count234(sorted));
  424. #endif
  425. lastdist = b->dist;
  426. }
  427. /*
  428. * Find all the anchors and form a linked list of the
  429. * squares within each block.
  430. */
  431. for (i = 0; i < wh; i++) {
  432. next[i] = -1;
  433. anchors[i] = false;
  434. which[i] = -1;
  435. if (ISANCHOR(b->data[i])) {
  436. anchors[i] = true;
  437. which[i] = i;
  438. } else if (ISDIST(b->data[i])) {
  439. j = i - b->data[i];
  440. next[j] = i;
  441. which[i] = which[j];
  442. }
  443. }
  444. /*
  445. * For each anchor, do an array-based BFS to find all the
  446. * places we can slide it to.
  447. */
  448. for (i = 0; i < wh; i++) {
  449. if (!anchors[i])
  450. continue;
  451. mqhead = mqtail = 0;
  452. for (j = 0; j < wh; j++)
  453. movereached[j] = false;
  454. movequeue[mqtail++] = i;
  455. while (mqhead < mqtail) {
  456. int pos = movequeue[mqhead++];
  457. /*
  458. * Try to move in each direction from here.
  459. */
  460. for (dir = 0; dir < 4; dir++) {
  461. int dx = (dir == 0 ? -1 : dir == 1 ? +1 : 0);
  462. int dy = (dir == 2 ? -1 : dir == 3 ? +1 : 0);
  463. int offset = dy*w + dx;
  464. int newpos = pos + offset;
  465. int d = newpos - i;
  466. /*
  467. * For each square involved in this block,
  468. * check to see if the square d spaces away
  469. * from it is either empty or part of the same
  470. * block.
  471. */
  472. for (j = i; j >= 0; j = next[j]) {
  473. int jy = (pos+j-i) / w + dy, jx = (pos+j-i) % w + dx;
  474. if (jy >= 0 && jy < h && jx >= 0 && jx < w &&
  475. ((b->data[j+d] == EMPTY || which[j+d] == i) &&
  476. (b->data[i] == MAINANCHOR || !forcefield[j+d])))
  477. /* ok */;
  478. else
  479. break;
  480. }
  481. if (j >= 0)
  482. continue; /* this direction wasn't feasible */
  483. /*
  484. * If we've already tried moving this piece
  485. * here, leave it.
  486. */
  487. if (movereached[newpos])
  488. continue;
  489. movereached[newpos] = true;
  490. movequeue[mqtail++] = newpos;
  491. /*
  492. * We have a viable move. Make it.
  493. */
  494. b2 = newboard(w, h, b->data);
  495. for (j = i; j >= 0; j = next[j])
  496. b2->data[j] = EMPTY;
  497. for (j = i; j >= 0; j = next[j])
  498. b2->data[j+d] = b->data[j];
  499. b3 = add234(sorted, b2);
  500. if (b3 != b2) {
  501. sfree(b2); /* we already got one */
  502. } else {
  503. b2->dist = b->dist + 1;
  504. b2->prev = b;
  505. addpos234(queue, b2, qlen++);
  506. if (b2->data[ty*w+tx] == MAINANCHOR)
  507. goto done; /* search completed! */
  508. }
  509. }
  510. }
  511. }
  512. }
  513. b2 = NULL;
  514. done:
  515. if (b2) {
  516. ret = b2->dist;
  517. if (moveout) {
  518. /*
  519. * Now b2 represents the solved position. Backtrack to
  520. * output the solution.
  521. */
  522. *moveout = snewn(ret * 2, int);
  523. j = ret * 2;
  524. while (b2->prev) {
  525. int from = -1, to = -1;
  526. b = b2->prev;
  527. /*
  528. * Scan b and b2 to find out which piece has
  529. * moved.
  530. */
  531. for (i = 0; i < wh; i++) {
  532. if (ISANCHOR(b->data[i]) && !ISANCHOR(b2->data[i])) {
  533. assert(from == -1);
  534. from = i;
  535. } else if (!ISANCHOR(b->data[i]) && ISANCHOR(b2->data[i])){
  536. assert(to == -1);
  537. to = i;
  538. }
  539. }
  540. assert(from >= 0 && to >= 0);
  541. assert(j >= 2);
  542. (*moveout)[--j] = to;
  543. (*moveout)[--j] = from;
  544. b2 = b;
  545. }
  546. assert(j == 0);
  547. }
  548. } else {
  549. ret = -1; /* no solution */
  550. if (moveout)
  551. *moveout = NULL;
  552. }
  553. freetree234(queue);
  554. while ((b = delpos234(sorted, 0)) != NULL)
  555. sfree(b);
  556. freetree234(sorted);
  557. sfree(next);
  558. sfree(anchors);
  559. sfree(movereached);
  560. sfree(movequeue);
  561. sfree(which);
  562. return ret;
  563. }
  564. /* ----------------------------------------------------------------------
  565. * Random board generation.
  566. */
  567. static void generate_board(int w, int h, int *rtx, int *rty, int *minmoves,
  568. random_state *rs, unsigned char **rboard,
  569. bool **rforcefield, int movelimit)
  570. {
  571. int wh = w*h;
  572. unsigned char *board, *board2;
  573. bool *forcefield;
  574. bool *tried_merge;
  575. DSF *dsf;
  576. int *list, nlist, pos;
  577. int tx, ty;
  578. int i, j;
  579. int moves = 0; /* placate optimiser */
  580. /*
  581. * Set up a board and fill it with singletons, except for a
  582. * border of walls.
  583. */
  584. board = snewn(wh, unsigned char);
  585. forcefield = snewn(wh, bool);
  586. board2 = snewn(wh, unsigned char);
  587. memset(board, ANCHOR, wh);
  588. memset(forcefield, 0, wh * sizeof(bool));
  589. for (i = 0; i < w; i++)
  590. board[i] = board[i+w*(h-1)] = WALL;
  591. for (i = 0; i < h; i++)
  592. board[i*w] = board[i*w+(w-1)] = WALL;
  593. tried_merge = snewn(wh * wh, bool);
  594. memset(tried_merge, 0, wh*wh * sizeof(bool));
  595. dsf = dsf_new(wh);
  596. /*
  597. * Invent a main piece at one extreme. (FIXME: vary the
  598. * extreme, and the piece.)
  599. */
  600. board[w+1] = MAINANCHOR;
  601. board[w+2] = DIST(1);
  602. board[w*2+1] = DIST(w-1);
  603. board[w*2+2] = DIST(1);
  604. /*
  605. * Invent a target position. (FIXME: vary this too.)
  606. */
  607. tx = w-2;
  608. ty = h-3;
  609. forcefield[ty*w+tx+1] = true;
  610. forcefield[(ty+1)*w+tx+1] = true;
  611. board[ty*w+tx+1] = board[(ty+1)*w+tx+1] = EMPTY;
  612. /*
  613. * Gradually remove singletons until the game becomes soluble.
  614. */
  615. for (j = w; j-- > 0 ;)
  616. for (i = h; i-- > 0 ;)
  617. if (board[i*w+j] == ANCHOR) {
  618. /*
  619. * See if the board is already soluble.
  620. */
  621. if ((moves = solve_board(w, h, board, forcefield,
  622. tx, ty, movelimit, NULL)) >= 0)
  623. goto soluble;
  624. /*
  625. * Otherwise, remove this piece.
  626. */
  627. board[i*w+j] = EMPTY;
  628. }
  629. assert(!"We shouldn't get here");
  630. soluble:
  631. /*
  632. * Make a list of all the inter-block edges on the board.
  633. */
  634. list = snewn(wh*2, int);
  635. nlist = 0;
  636. for (i = 0; i+1 < w; i++)
  637. for (j = 0; j < h; j++)
  638. list[nlist++] = (j*w+i) * 2 + 0; /* edge to the right of j*w+i */
  639. for (j = 0; j+1 < h; j++)
  640. for (i = 0; i < w; i++)
  641. list[nlist++] = (j*w+i) * 2 + 1; /* edge below j*w+i */
  642. /*
  643. * Now go through that list in random order, trying to merge
  644. * the blocks on each side of each edge.
  645. */
  646. shuffle(list, nlist, sizeof(*list), rs);
  647. while (nlist > 0) {
  648. int x1, y1, p1, c1;
  649. int x2, y2, p2, c2;
  650. pos = list[--nlist];
  651. y1 = y2 = pos / (w*2);
  652. x1 = x2 = (pos / 2) % w;
  653. if (pos % 2)
  654. y2++;
  655. else
  656. x2++;
  657. p1 = y1*w+x1;
  658. p2 = y2*w+x2;
  659. /*
  660. * Immediately abandon the attempt if we've already tried
  661. * to merge the same pair of blocks along a different
  662. * edge.
  663. */
  664. c1 = dsf_canonify(dsf, p1);
  665. c2 = dsf_canonify(dsf, p2);
  666. if (tried_merge[c1 * wh + c2])
  667. continue;
  668. /*
  669. * In order to be mergeable, these two squares must each
  670. * either be, or belong to, a non-main anchor, and their
  671. * anchors must also be distinct.
  672. */
  673. if (!ISBLOCK(board[p1]) || !ISBLOCK(board[p2]))
  674. continue;
  675. while (ISDIST(board[p1]))
  676. p1 -= board[p1];
  677. while (ISDIST(board[p2]))
  678. p2 -= board[p2];
  679. if (board[p1] == MAINANCHOR || board[p2] == MAINANCHOR || p1 == p2)
  680. continue;
  681. /*
  682. * We can merge these blocks. Try it, and see if the
  683. * puzzle remains soluble.
  684. */
  685. memcpy(board2, board, wh);
  686. j = -1;
  687. while (p1 < wh || p2 < wh) {
  688. /*
  689. * p1 and p2 are the squares at the head of each block
  690. * list. Pick the smaller one and put it on the output
  691. * block list.
  692. */
  693. i = min(p1, p2);
  694. if (j < 0) {
  695. board[i] = ANCHOR;
  696. } else {
  697. assert(i - j <= MAXDIST);
  698. board[i] = DIST(i - j);
  699. }
  700. j = i;
  701. /*
  702. * Now advance whichever list that came from.
  703. */
  704. if (i == p1) {
  705. do {
  706. p1++;
  707. } while (p1 < wh && board[p1] != DIST(p1-i));
  708. } else {
  709. do {
  710. p2++;
  711. } while (p2 < wh && board[p2] != DIST(p2-i));
  712. }
  713. }
  714. j = solve_board(w, h, board, forcefield, tx, ty, movelimit, NULL);
  715. if (j < 0) {
  716. /*
  717. * Didn't work. Revert the merge.
  718. */
  719. memcpy(board, board2, wh);
  720. tried_merge[c1 * wh + c2] = true;
  721. tried_merge[c2 * wh + c1] = true;
  722. } else {
  723. int c;
  724. moves = j;
  725. dsf_merge(dsf, c1, c2);
  726. c = dsf_canonify(dsf, c1);
  727. for (i = 0; i < wh; i++)
  728. tried_merge[c*wh+i] = (tried_merge[c1*wh+i] ||
  729. tried_merge[c2*wh+i]);
  730. for (i = 0; i < wh; i++)
  731. tried_merge[i*wh+c] = (tried_merge[i*wh+c1] ||
  732. tried_merge[i*wh+c2]);
  733. }
  734. }
  735. dsf_free(dsf);
  736. sfree(list);
  737. sfree(tried_merge);
  738. sfree(board2);
  739. *rtx = tx;
  740. *rty = ty;
  741. *rboard = board;
  742. *rforcefield = forcefield;
  743. *minmoves = moves;
  744. }
  745. /* ----------------------------------------------------------------------
  746. * End of solver/generator code.
  747. */
  748. static char *new_game_desc(const game_params *params, random_state *rs,
  749. char **aux, bool interactive)
  750. {
  751. int w = params->w, h = params->h, wh = w*h;
  752. int tx, ty, minmoves;
  753. unsigned char *board;
  754. bool *forcefield;
  755. char *ret, *p;
  756. int i;
  757. generate_board(params->w, params->h, &tx, &ty, &minmoves, rs,
  758. &board, &forcefield, params->maxmoves);
  759. #ifdef GENERATOR_DIAGNOSTICS
  760. {
  761. char *t = board_text_format(params->w, params->h, board);
  762. printf("%s\n", t);
  763. sfree(t);
  764. }
  765. #endif
  766. /*
  767. * Encode as a game ID.
  768. */
  769. ret = snewn(wh * 6 + 40, char);
  770. p = ret;
  771. i = 0;
  772. while (i < wh) {
  773. if (ISDIST(board[i])) {
  774. p += sprintf(p, "d%d", board[i]);
  775. i++;
  776. } else {
  777. int count = 1;
  778. int b = board[i];
  779. bool f = forcefield[i];
  780. int c = (b == ANCHOR ? 'a' :
  781. b == MAINANCHOR ? 'm' :
  782. b == EMPTY ? 'e' :
  783. /* b == WALL ? */ 'w');
  784. if (f) *p++ = 'f';
  785. *p++ = c;
  786. i++;
  787. while (i < wh && board[i] == b && forcefield[i] == f)
  788. i++, count++;
  789. if (count > 1)
  790. p += sprintf(p, "%d", count);
  791. }
  792. }
  793. p += sprintf(p, ",%d,%d,%d", tx, ty, minmoves);
  794. ret = sresize(ret, p+1 - ret, char);
  795. sfree(board);
  796. sfree(forcefield);
  797. return ret;
  798. }
  799. static const char *validate_desc(const game_params *params, const char *desc)
  800. {
  801. int w = params->w, h = params->h, wh = w*h;
  802. bool *active;
  803. int *link;
  804. int mains = 0;
  805. int i, tx, ty, minmoves;
  806. const char *ret;
  807. active = snewn(wh, bool);
  808. link = snewn(wh, int);
  809. i = 0;
  810. while (*desc && *desc != ',') {
  811. if (i >= wh) {
  812. ret = "Too much data in game description";
  813. goto done;
  814. }
  815. link[i] = -1;
  816. active[i] = false;
  817. if (*desc == 'f' || *desc == 'F') {
  818. desc++;
  819. if (!*desc) {
  820. ret = "Expected another character after 'f' in game "
  821. "description";
  822. goto done;
  823. }
  824. }
  825. if (*desc == 'd' || *desc == 'D') {
  826. int dist;
  827. desc++;
  828. if (!isdigit((unsigned char)*desc)) {
  829. ret = "Expected a number after 'd' in game description";
  830. goto done;
  831. }
  832. dist = atoi(desc);
  833. while (*desc && isdigit((unsigned char)*desc)) desc++;
  834. if (dist <= 0 || dist > i) {
  835. ret = "Out-of-range number after 'd' in game description";
  836. goto done;
  837. }
  838. if (!active[i - dist]) {
  839. ret = "Invalid back-reference in game description";
  840. goto done;
  841. }
  842. link[i] = i - dist;
  843. active[i] = true;
  844. active[link[i]] = false;
  845. i++;
  846. } else {
  847. int c = *desc++;
  848. int count = 1;
  849. if (!strchr("aAmMeEwW", c)) {
  850. ret = "Invalid character in game description";
  851. goto done;
  852. }
  853. if (isdigit((unsigned char)*desc)) {
  854. count = atoi(desc);
  855. while (*desc && isdigit((unsigned char)*desc)) desc++;
  856. }
  857. if (i + count > wh) {
  858. ret = "Too much data in game description";
  859. goto done;
  860. }
  861. while (count-- > 0) {
  862. active[i] = (strchr("aAmM", c) != NULL);
  863. link[i] = -1;
  864. if (strchr("mM", c) != NULL) {
  865. mains++;
  866. }
  867. i++;
  868. }
  869. }
  870. }
  871. if (mains != 1) {
  872. ret = (mains == 0 ? "No main piece specified in game description" :
  873. "More than one main piece specified in game description");
  874. goto done;
  875. }
  876. if (i < wh) {
  877. ret = "Not enough data in game description";
  878. goto done;
  879. }
  880. /*
  881. * Now read the target coordinates.
  882. */
  883. i = sscanf(desc, ",%d,%d,%d", &tx, &ty, &minmoves);
  884. if (i < 2) {
  885. ret = "No target coordinates specified";
  886. goto done;
  887. /*
  888. * (but minmoves is optional)
  889. */
  890. }
  891. ret = NULL;
  892. done:
  893. sfree(active);
  894. sfree(link);
  895. return ret;
  896. }
  897. static game_state *new_game(midend *me, const game_params *params,
  898. const char *desc)
  899. {
  900. int w = params->w, h = params->h, wh = w*h;
  901. game_state *state;
  902. int i;
  903. state = snew(game_state);
  904. state->w = w;
  905. state->h = h;
  906. state->board = snewn(wh, unsigned char);
  907. state->lastmoved = state->lastmoved_pos = -1;
  908. state->movecount = 0;
  909. state->imm = snew(struct game_immutable_state);
  910. state->imm->refcount = 1;
  911. state->imm->forcefield = snewn(wh, bool);
  912. i = 0;
  913. while (*desc && *desc != ',') {
  914. bool f = false;
  915. assert(i < wh);
  916. if (*desc == 'f') {
  917. f = true;
  918. desc++;
  919. assert(*desc);
  920. }
  921. if (*desc == 'd' || *desc == 'D') {
  922. int dist;
  923. desc++;
  924. dist = atoi(desc);
  925. while (*desc && isdigit((unsigned char)*desc)) desc++;
  926. state->board[i] = DIST(dist);
  927. state->imm->forcefield[i] = f;
  928. i++;
  929. } else {
  930. int c = *desc++;
  931. int count = 1;
  932. if (isdigit((unsigned char)*desc)) {
  933. count = atoi(desc);
  934. while (*desc && isdigit((unsigned char)*desc)) desc++;
  935. }
  936. assert(i + count <= wh);
  937. c = (c == 'a' || c == 'A' ? ANCHOR :
  938. c == 'm' || c == 'M' ? MAINANCHOR :
  939. c == 'e' || c == 'E' ? EMPTY :
  940. /* c == 'w' || c == 'W' ? */ WALL);
  941. while (count-- > 0) {
  942. state->board[i] = c;
  943. state->imm->forcefield[i] = f;
  944. i++;
  945. }
  946. }
  947. }
  948. /*
  949. * Now read the target coordinates.
  950. */
  951. state->tx = state->ty = 0;
  952. state->minmoves = -1;
  953. i = sscanf(desc, ",%d,%d,%d", &state->tx, &state->ty, &state->minmoves);
  954. if (state->board[state->ty*w+state->tx] == MAINANCHOR)
  955. state->completed = 0; /* already complete! */
  956. else
  957. state->completed = -1;
  958. state->cheated = false;
  959. state->soln = NULL;
  960. state->soln_index = -1;
  961. return state;
  962. }
  963. static game_state *dup_game(const game_state *state)
  964. {
  965. int w = state->w, h = state->h, wh = w*h;
  966. game_state *ret = snew(game_state);
  967. ret->w = state->w;
  968. ret->h = state->h;
  969. ret->board = snewn(wh, unsigned char);
  970. memcpy(ret->board, state->board, wh);
  971. ret->tx = state->tx;
  972. ret->ty = state->ty;
  973. ret->minmoves = state->minmoves;
  974. ret->lastmoved = state->lastmoved;
  975. ret->lastmoved_pos = state->lastmoved_pos;
  976. ret->movecount = state->movecount;
  977. ret->completed = state->completed;
  978. ret->cheated = state->cheated;
  979. ret->imm = state->imm;
  980. ret->imm->refcount++;
  981. ret->soln = state->soln;
  982. ret->soln_index = state->soln_index;
  983. if (ret->soln)
  984. ret->soln->refcount++;
  985. return ret;
  986. }
  987. static void free_game(game_state *state)
  988. {
  989. if (--state->imm->refcount <= 0) {
  990. sfree(state->imm->forcefield);
  991. sfree(state->imm);
  992. }
  993. if (state->soln && --state->soln->refcount <= 0) {
  994. sfree(state->soln->moves);
  995. sfree(state->soln);
  996. }
  997. sfree(state->board);
  998. sfree(state);
  999. }
  1000. static char *solve_game(const game_state *state, const game_state *currstate,
  1001. const char *aux, const char **error)
  1002. {
  1003. int *moves;
  1004. int nmoves;
  1005. int i;
  1006. char *ret, *p, sep;
  1007. /*
  1008. * Run the solver and attempt to find the shortest solution
  1009. * from the current position.
  1010. */
  1011. nmoves = solve_board(state->w, state->h, state->board,
  1012. state->imm->forcefield, state->tx, state->ty,
  1013. -1, &moves);
  1014. if (nmoves < 0) {
  1015. *error = "Unable to find a solution to this puzzle";
  1016. return NULL;
  1017. }
  1018. if (nmoves == 0) {
  1019. *error = "Puzzle is already solved";
  1020. return NULL;
  1021. }
  1022. /*
  1023. * Encode the resulting solution as a move string.
  1024. */
  1025. ret = snewn(nmoves * 40, char);
  1026. p = ret;
  1027. sep = 'S';
  1028. for (i = 0; i < nmoves; i++) {
  1029. p += sprintf(p, "%c%d-%d", sep, moves[i*2], moves[i*2+1]);
  1030. sep = ',';
  1031. }
  1032. sfree(moves);
  1033. assert(p - ret < nmoves * 40);
  1034. ret = sresize(ret, p+1 - ret, char);
  1035. return ret;
  1036. }
  1037. static bool game_can_format_as_text_now(const game_params *params)
  1038. {
  1039. return true;
  1040. }
  1041. static char *game_text_format(const game_state *state)
  1042. {
  1043. return board_text_format(state->w, state->h, state->board,
  1044. state->imm->forcefield);
  1045. }
  1046. struct game_ui {
  1047. bool dragging;
  1048. int drag_anchor;
  1049. int drag_offset_x, drag_offset_y;
  1050. int drag_currpos;
  1051. bool *reachable;
  1052. int *bfs_queue; /* used as scratch in interpret_move */
  1053. };
  1054. static game_ui *new_ui(const game_state *state)
  1055. {
  1056. int w = state->w, h = state->h, wh = w*h;
  1057. game_ui *ui = snew(game_ui);
  1058. ui->dragging = false;
  1059. ui->drag_anchor = ui->drag_currpos = -1;
  1060. ui->drag_offset_x = ui->drag_offset_y = -1;
  1061. ui->reachable = snewn(wh, bool);
  1062. memset(ui->reachable, 0, wh * sizeof(bool));
  1063. ui->bfs_queue = snewn(wh, int);
  1064. return ui;
  1065. }
  1066. static void free_ui(game_ui *ui)
  1067. {
  1068. sfree(ui->bfs_queue);
  1069. sfree(ui->reachable);
  1070. sfree(ui);
  1071. }
  1072. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  1073. const game_state *newstate)
  1074. {
  1075. }
  1076. #define PREFERRED_TILESIZE 32
  1077. #define TILESIZE (ds->tilesize)
  1078. #define BORDER (TILESIZE/2)
  1079. #define COORD(x) ( (x) * TILESIZE + BORDER )
  1080. #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
  1081. #define BORDER_WIDTH (1 + TILESIZE/20)
  1082. #define HIGHLIGHT_WIDTH (1 + TILESIZE/16)
  1083. #define FLASH_INTERVAL 0.10F
  1084. #define FLASH_TIME 3*FLASH_INTERVAL
  1085. struct game_drawstate {
  1086. int tilesize;
  1087. int w, h;
  1088. unsigned long *grid; /* what's currently displayed */
  1089. };
  1090. static char *interpret_move(const game_state *state, game_ui *ui,
  1091. const game_drawstate *ds,
  1092. int x, int y, int button)
  1093. {
  1094. int w = state->w, h = state->h, wh = w*h;
  1095. int tx, ty, i, j;
  1096. int qhead, qtail;
  1097. if (button == LEFT_BUTTON) {
  1098. tx = FROMCOORD(x);
  1099. ty = FROMCOORD(y);
  1100. if (tx < 0 || tx >= w || ty < 0 || ty >= h ||
  1101. !ISBLOCK(state->board[ty*w+tx]))
  1102. return NULL; /* this click has no effect */
  1103. /*
  1104. * User has clicked on a block. Find the block's anchor
  1105. * and register that we've started dragging it.
  1106. */
  1107. i = ty*w+tx;
  1108. while (ISDIST(state->board[i]))
  1109. i -= state->board[i];
  1110. assert(i >= 0 && i < wh);
  1111. ui->dragging = true;
  1112. ui->drag_anchor = i;
  1113. ui->drag_offset_x = tx - (i % w);
  1114. ui->drag_offset_y = ty - (i / w);
  1115. ui->drag_currpos = i;
  1116. /*
  1117. * Now we immediately bfs out from the current location of
  1118. * the anchor, to find all the places to which this block
  1119. * can be dragged.
  1120. */
  1121. memset(ui->reachable, 0, wh * sizeof(bool));
  1122. qhead = qtail = 0;
  1123. ui->reachable[i] = true;
  1124. ui->bfs_queue[qtail++] = i;
  1125. for (j = i; j < wh; j++)
  1126. if (state->board[j] == DIST(j - i))
  1127. i = j;
  1128. while (qhead < qtail) {
  1129. int pos = ui->bfs_queue[qhead++];
  1130. int x = pos % w, y = pos / w;
  1131. int dir;
  1132. for (dir = 0; dir < 4; dir++) {
  1133. int dx = (dir == 0 ? -1 : dir == 1 ? +1 : 0);
  1134. int dy = (dir == 2 ? -1 : dir == 3 ? +1 : 0);
  1135. int newpos;
  1136. if (x + dx < 0 || x + dx >= w ||
  1137. y + dy < 0 || y + dy >= h)
  1138. continue;
  1139. newpos = pos + dy*w + dx;
  1140. if (ui->reachable[newpos])
  1141. continue; /* already done this one */
  1142. /*
  1143. * Now search the grid to see if the block we're
  1144. * dragging could fit into this space.
  1145. */
  1146. for (j = i; j >= 0; j = (ISDIST(state->board[j]) ?
  1147. j - state->board[j] : -1)) {
  1148. int jx = (j+pos-ui->drag_anchor) % w;
  1149. int jy = (j+pos-ui->drag_anchor) / w;
  1150. int j2;
  1151. if (jx + dx < 0 || jx + dx >= w ||
  1152. jy + dy < 0 || jy + dy >= h)
  1153. break; /* this position isn't valid at all */
  1154. j2 = (j+pos-ui->drag_anchor) + dy*w + dx;
  1155. if (state->board[j2] == EMPTY &&
  1156. (!state->imm->forcefield[j2] ||
  1157. state->board[ui->drag_anchor] == MAINANCHOR))
  1158. continue;
  1159. while (ISDIST(state->board[j2]))
  1160. j2 -= state->board[j2];
  1161. assert(j2 >= 0 && j2 < wh);
  1162. if (j2 == ui->drag_anchor)
  1163. continue;
  1164. else
  1165. break;
  1166. }
  1167. if (j < 0) {
  1168. /*
  1169. * If we got to the end of that loop without
  1170. * disqualifying this position, mark it as
  1171. * reachable for this drag.
  1172. */
  1173. ui->reachable[newpos] = true;
  1174. ui->bfs_queue[qtail++] = newpos;
  1175. }
  1176. }
  1177. }
  1178. /*
  1179. * And that's it. Update the display to reflect the start
  1180. * of a drag.
  1181. */
  1182. return MOVE_UI_UPDATE;
  1183. } else if (button == LEFT_DRAG && ui->dragging) {
  1184. int dist, distlimit, dx, dy, s, px, py;
  1185. tx = FROMCOORD(x);
  1186. ty = FROMCOORD(y);
  1187. tx -= ui->drag_offset_x;
  1188. ty -= ui->drag_offset_y;
  1189. /*
  1190. * Now search outwards from (tx,ty), in order of Manhattan
  1191. * distance, until we find a reachable square.
  1192. */
  1193. distlimit = w+tx;
  1194. distlimit = max(distlimit, h+ty);
  1195. distlimit = max(distlimit, tx);
  1196. distlimit = max(distlimit, ty);
  1197. for (dist = 0; dist <= distlimit; dist++) {
  1198. for (dx = -dist; dx <= dist; dx++)
  1199. for (s = -1; s <= +1; s += 2) {
  1200. dy = s * (dist - abs(dx));
  1201. px = tx + dx;
  1202. py = ty + dy;
  1203. if (px >= 0 && px < w && py >= 0 && py < h &&
  1204. ui->reachable[py*w+px]) {
  1205. ui->drag_currpos = py*w+px;
  1206. return MOVE_UI_UPDATE;
  1207. }
  1208. }
  1209. }
  1210. return NULL; /* give up - this drag has no effect */
  1211. } else if (button == LEFT_RELEASE && ui->dragging) {
  1212. char data[256], *str;
  1213. /*
  1214. * Terminate the drag, and if the piece has actually moved
  1215. * then return a move string quoting the old and new
  1216. * locations of the piece's anchor.
  1217. */
  1218. if (ui->drag_anchor != ui->drag_currpos) {
  1219. sprintf(data, "M%d-%d", ui->drag_anchor, ui->drag_currpos);
  1220. str = dupstr(data);
  1221. } else
  1222. str = MOVE_UI_UPDATE;
  1223. ui->dragging = false;
  1224. ui->drag_anchor = ui->drag_currpos = -1;
  1225. ui->drag_offset_x = ui->drag_offset_y = -1;
  1226. memset(ui->reachable, 0, wh * sizeof(bool));
  1227. return str;
  1228. } else if (button == ' ' && state->soln) {
  1229. /*
  1230. * Make the next move in the stored solution.
  1231. */
  1232. char data[256];
  1233. int a1, a2;
  1234. a1 = state->soln->moves[state->soln_index*2];
  1235. a2 = state->soln->moves[state->soln_index*2+1];
  1236. if (a1 == state->lastmoved_pos)
  1237. a1 = state->lastmoved;
  1238. sprintf(data, "M%d-%d", a1, a2);
  1239. return dupstr(data);
  1240. }
  1241. return NULL;
  1242. }
  1243. static bool move_piece(int w, int h, const unsigned char *src,
  1244. unsigned char *dst, bool *ff, int from, int to)
  1245. {
  1246. int wh = w*h;
  1247. int i, j;
  1248. if (!ISANCHOR(dst[from]))
  1249. return false;
  1250. /*
  1251. * Scan to the far end of the piece's linked list.
  1252. */
  1253. for (i = j = from; j < wh; j++)
  1254. if (src[j] == DIST(j - i))
  1255. i = j;
  1256. /*
  1257. * Remove the piece from its old location in the new
  1258. * game state.
  1259. */
  1260. for (j = i; j >= 0; j = (ISDIST(src[j]) ? j - src[j] : -1))
  1261. dst[j] = EMPTY;
  1262. /*
  1263. * And put it back in at the new location.
  1264. */
  1265. for (j = i; j >= 0; j = (ISDIST(src[j]) ? j - src[j] : -1)) {
  1266. int jn = j + to - from;
  1267. if (jn < 0 || jn >= wh)
  1268. return false;
  1269. if (dst[jn] == EMPTY && (!ff[jn] || src[from] == MAINANCHOR)) {
  1270. dst[jn] = src[j];
  1271. } else {
  1272. return false;
  1273. }
  1274. }
  1275. return true;
  1276. }
  1277. static game_state *execute_move(const game_state *state, const char *move)
  1278. {
  1279. int w = state->w, h = state->h /* , wh = w*h */;
  1280. char c;
  1281. int a1, a2, n, movesize;
  1282. game_state *ret = dup_game(state);
  1283. while (*move) {
  1284. c = *move;
  1285. if (c == 'S') {
  1286. /*
  1287. * This is a solve move, so we just set up a stored
  1288. * solution path.
  1289. */
  1290. if (ret->soln && --ret->soln->refcount <= 0) {
  1291. sfree(ret->soln->moves);
  1292. sfree(ret->soln);
  1293. }
  1294. ret->soln = snew(struct game_solution);
  1295. ret->soln->nmoves = 0;
  1296. ret->soln->moves = NULL;
  1297. ret->soln->refcount = 1;
  1298. ret->soln_index = 0;
  1299. ret->cheated = true;
  1300. movesize = 0;
  1301. move++;
  1302. while (1) {
  1303. if (sscanf(move, "%d-%d%n", &a1, &a2, &n) != 2) {
  1304. free_game(ret);
  1305. return NULL;
  1306. }
  1307. /*
  1308. * Special case: if the first move in the solution
  1309. * involves the piece for which we already have a
  1310. * partial stored move, adjust the source point to
  1311. * the original starting point of that piece.
  1312. */
  1313. if (ret->soln->nmoves == 0 && a1 == ret->lastmoved)
  1314. a1 = ret->lastmoved_pos;
  1315. if (ret->soln->nmoves >= movesize) {
  1316. movesize = (ret->soln->nmoves + 48) * 4 / 3;
  1317. ret->soln->moves = sresize(ret->soln->moves,
  1318. 2*movesize, int);
  1319. }
  1320. ret->soln->moves[2*ret->soln->nmoves] = a1;
  1321. ret->soln->moves[2*ret->soln->nmoves+1] = a2;
  1322. ret->soln->nmoves++;
  1323. move += n;
  1324. if (*move != ',')
  1325. break;
  1326. move++; /* eat comma */
  1327. }
  1328. } else if (c == 'M') {
  1329. move++;
  1330. if (sscanf(move, "%d-%d%n", &a1, &a2, &n) != 2 ||
  1331. !move_piece(w, h, state->board, ret->board,
  1332. state->imm->forcefield, a1, a2)) {
  1333. free_game(ret);
  1334. return NULL;
  1335. }
  1336. if (a1 == ret->lastmoved) {
  1337. /*
  1338. * If the player has moved the same piece as they
  1339. * moved last time, don't increment the move
  1340. * count. In fact, if they've put the piece back
  1341. * where it started from, _decrement_ the move
  1342. * count.
  1343. */
  1344. if (a2 == ret->lastmoved_pos) {
  1345. ret->movecount--; /* reverted last move */
  1346. ret->lastmoved = ret->lastmoved_pos = -1;
  1347. } else {
  1348. ret->lastmoved = a2;
  1349. /* don't change lastmoved_pos */
  1350. }
  1351. } else {
  1352. ret->lastmoved = a2;
  1353. ret->lastmoved_pos = a1;
  1354. ret->movecount++;
  1355. }
  1356. /*
  1357. * If we have a stored solution path, see if we've
  1358. * strayed from it or successfully made the next move
  1359. * along it.
  1360. */
  1361. if (ret->soln && ret->lastmoved_pos >= 0) {
  1362. if (ret->lastmoved_pos !=
  1363. ret->soln->moves[ret->soln_index*2]) {
  1364. /* strayed from the path */
  1365. ret->soln->refcount--;
  1366. assert(ret->soln->refcount > 0);
  1367. /* `state' at least still exists */
  1368. ret->soln = NULL;
  1369. ret->soln_index = -1;
  1370. } else if (ret->lastmoved ==
  1371. ret->soln->moves[ret->soln_index*2+1]) {
  1372. /* advanced along the path */
  1373. ret->soln_index++;
  1374. if (ret->soln_index >= ret->soln->nmoves) {
  1375. /* finished the path! */
  1376. ret->soln->refcount--;
  1377. assert(ret->soln->refcount > 0);
  1378. /* `state' at least still exists */
  1379. ret->soln = NULL;
  1380. ret->soln_index = -1;
  1381. }
  1382. }
  1383. }
  1384. if (ret->board[a2] == MAINANCHOR &&
  1385. a2 == ret->ty * w + ret->tx && ret->completed < 0)
  1386. ret->completed = ret->movecount;
  1387. move += n;
  1388. } else {
  1389. free_game(ret);
  1390. return NULL;
  1391. }
  1392. if (*move == ';')
  1393. move++;
  1394. else if (*move) {
  1395. free_game(ret);
  1396. return NULL;
  1397. }
  1398. }
  1399. return ret;
  1400. }
  1401. /* ----------------------------------------------------------------------
  1402. * Drawing routines.
  1403. */
  1404. static void game_compute_size(const game_params *params, int tilesize,
  1405. const game_ui *ui, int *x, int *y)
  1406. {
  1407. /* fool the macros */
  1408. struct dummy { int tilesize; } dummy, *ds = &dummy;
  1409. dummy.tilesize = tilesize;
  1410. *x = params->w * TILESIZE + 2*BORDER;
  1411. *y = params->h * TILESIZE + 2*BORDER;
  1412. }
  1413. static void game_set_size(drawing *dr, game_drawstate *ds,
  1414. const game_params *params, int tilesize)
  1415. {
  1416. ds->tilesize = tilesize;
  1417. }
  1418. static void raise_colour(float *target, float *src, float *limit)
  1419. {
  1420. int i;
  1421. for (i = 0; i < 3; i++)
  1422. target[i] = (2*src[i] + limit[i]) / 3;
  1423. }
  1424. static float *game_colours(frontend *fe, int *ncolours)
  1425. {
  1426. float *ret = snewn(3 * NCOLOURS, float);
  1427. game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
  1428. /*
  1429. * When dragging a tile, we light it up a bit.
  1430. */
  1431. raise_colour(ret+3*COL_DRAGGING,
  1432. ret+3*COL_BACKGROUND, ret+3*COL_HIGHLIGHT);
  1433. raise_colour(ret+3*COL_DRAGGING_HIGHLIGHT,
  1434. ret+3*COL_HIGHLIGHT, ret+3*COL_HIGHLIGHT);
  1435. raise_colour(ret+3*COL_DRAGGING_LOWLIGHT,
  1436. ret+3*COL_LOWLIGHT, ret+3*COL_HIGHLIGHT);
  1437. /*
  1438. * The main tile is tinted blue.
  1439. */
  1440. ret[COL_MAIN * 3 + 0] = ret[COL_BACKGROUND * 3 + 0];
  1441. ret[COL_MAIN * 3 + 1] = ret[COL_BACKGROUND * 3 + 1];
  1442. ret[COL_MAIN * 3 + 2] = ret[COL_HIGHLIGHT * 3 + 2];
  1443. game_mkhighlight_specific(fe, ret, COL_MAIN,
  1444. COL_MAIN_HIGHLIGHT, COL_MAIN_LOWLIGHT);
  1445. /*
  1446. * And we light that up a bit too when dragging.
  1447. */
  1448. raise_colour(ret+3*COL_MAIN_DRAGGING,
  1449. ret+3*COL_MAIN, ret+3*COL_MAIN_HIGHLIGHT);
  1450. raise_colour(ret+3*COL_MAIN_DRAGGING_HIGHLIGHT,
  1451. ret+3*COL_MAIN_HIGHLIGHT, ret+3*COL_MAIN_HIGHLIGHT);
  1452. raise_colour(ret+3*COL_MAIN_DRAGGING_LOWLIGHT,
  1453. ret+3*COL_MAIN_LOWLIGHT, ret+3*COL_MAIN_HIGHLIGHT);
  1454. /*
  1455. * The target area on the floor is tinted green.
  1456. */
  1457. ret[COL_TARGET * 3 + 0] = ret[COL_BACKGROUND * 3 + 0];
  1458. ret[COL_TARGET * 3 + 1] = ret[COL_HIGHLIGHT * 3 + 1];
  1459. ret[COL_TARGET * 3 + 2] = ret[COL_BACKGROUND * 3 + 2];
  1460. game_mkhighlight_specific(fe, ret, COL_TARGET,
  1461. COL_TARGET_HIGHLIGHT, COL_TARGET_LOWLIGHT);
  1462. *ncolours = NCOLOURS;
  1463. return ret;
  1464. }
  1465. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1466. {
  1467. int w = state->w, h = state->h, wh = w*h;
  1468. struct game_drawstate *ds = snew(struct game_drawstate);
  1469. int i;
  1470. ds->tilesize = 0;
  1471. ds->w = w;
  1472. ds->h = h;
  1473. ds->grid = snewn(wh, unsigned long);
  1474. for (i = 0; i < wh; i++)
  1475. ds->grid[i] = ~(unsigned long)0;
  1476. return ds;
  1477. }
  1478. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  1479. {
  1480. sfree(ds->grid);
  1481. sfree(ds);
  1482. }
  1483. #define BG_NORMAL 0x00000001UL
  1484. #define BG_TARGET 0x00000002UL
  1485. #define BG_FORCEFIELD 0x00000004UL
  1486. #define FLASH_LOW 0x00000008UL
  1487. #define FLASH_HIGH 0x00000010UL
  1488. #define FG_WALL 0x00000020UL
  1489. #define FG_MAIN 0x00000040UL
  1490. #define FG_NORMAL 0x00000080UL
  1491. #define FG_DRAGGING 0x00000100UL
  1492. #define FG_SHADOW 0x00000200UL
  1493. #define FG_SOLVEPIECE 0x00000400UL
  1494. #define FG_MAINPIECESH 11
  1495. #define FG_SHADOWSH 19
  1496. #define PIECE_LBORDER 0x00000001UL
  1497. #define PIECE_TBORDER 0x00000002UL
  1498. #define PIECE_RBORDER 0x00000004UL
  1499. #define PIECE_BBORDER 0x00000008UL
  1500. #define PIECE_TLCORNER 0x00000010UL
  1501. #define PIECE_TRCORNER 0x00000020UL
  1502. #define PIECE_BLCORNER 0x00000040UL
  1503. #define PIECE_BRCORNER 0x00000080UL
  1504. #define PIECE_MASK 0x000000FFUL
  1505. /*
  1506. * Utility function.
  1507. */
  1508. #define TYPE_MASK 0xF000
  1509. #define COL_MASK 0x0FFF
  1510. #define TYPE_RECT 0x0000
  1511. #define TYPE_TLCIRC 0x4000
  1512. #define TYPE_TRCIRC 0x5000
  1513. #define TYPE_BLCIRC 0x6000
  1514. #define TYPE_BRCIRC 0x7000
  1515. static void maybe_rect(drawing *dr, int x, int y, int w, int h,
  1516. int coltype, int col2)
  1517. {
  1518. int colour = coltype & COL_MASK, type = coltype & TYPE_MASK;
  1519. if (colour > NCOLOURS)
  1520. return;
  1521. if (type == TYPE_RECT) {
  1522. draw_rect(dr, x, y, w, h, colour);
  1523. } else {
  1524. int cx, cy, r;
  1525. clip(dr, x, y, w, h);
  1526. cx = x;
  1527. cy = y;
  1528. r = w-1;
  1529. if (type & 0x1000)
  1530. cx += r;
  1531. if (type & 0x2000)
  1532. cy += r;
  1533. if (col2 == -1 || col2 == coltype) {
  1534. assert(w == h);
  1535. draw_circle(dr, cx, cy, r, colour, colour);
  1536. } else {
  1537. /*
  1538. * We aim to draw a quadrant of a circle in two
  1539. * different colours. We do this using Bresenham's
  1540. * algorithm directly, because the Puzzles drawing API
  1541. * doesn't have a draw-sector primitive.
  1542. */
  1543. int bx, by, bd, bd2;
  1544. int xm = (type & 0x1000 ? -1 : +1);
  1545. int ym = (type & 0x2000 ? -1 : +1);
  1546. by = r;
  1547. bx = 0;
  1548. bd = 0;
  1549. while (by >= bx) {
  1550. /*
  1551. * Plot the point.
  1552. */
  1553. {
  1554. int x1 = cx+xm*bx, y1 = cy+ym*bx;
  1555. int x2, y2;
  1556. x2 = cx+xm*by; y2 = y1;
  1557. draw_rect(dr, min(x1,x2), min(y1,y2),
  1558. abs(x1-x2)+1, abs(y1-y2)+1, colour);
  1559. x2 = x1; y2 = cy+ym*by;
  1560. draw_rect(dr, min(x1,x2), min(y1,y2),
  1561. abs(x1-x2)+1, abs(y1-y2)+1, col2);
  1562. }
  1563. bd += 2*bx + 1;
  1564. bd2 = bd - (2*by - 1);
  1565. if (abs(bd2) < abs(bd)) {
  1566. bd = bd2;
  1567. by--;
  1568. }
  1569. bx++;
  1570. }
  1571. }
  1572. unclip(dr);
  1573. }
  1574. }
  1575. static void draw_wallpart(drawing *dr, game_drawstate *ds,
  1576. int tx, int ty, unsigned long val,
  1577. int cl, int cc, int ch)
  1578. {
  1579. int coords[6];
  1580. draw_rect(dr, tx, ty, TILESIZE, TILESIZE, cc);
  1581. if (val & PIECE_LBORDER)
  1582. draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, TILESIZE,
  1583. ch);
  1584. if (val & PIECE_RBORDER)
  1585. draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
  1586. HIGHLIGHT_WIDTH, TILESIZE, cl);
  1587. if (val & PIECE_TBORDER)
  1588. draw_rect(dr, tx, ty, TILESIZE, HIGHLIGHT_WIDTH, ch);
  1589. if (val & PIECE_BBORDER)
  1590. draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
  1591. TILESIZE, HIGHLIGHT_WIDTH, cl);
  1592. if (!((PIECE_BBORDER | PIECE_LBORDER) &~ val)) {
  1593. draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
  1594. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl);
  1595. clip(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
  1596. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
  1597. coords[0] = tx - 1;
  1598. coords[1] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1599. coords[2] = tx + HIGHLIGHT_WIDTH;
  1600. coords[3] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1601. coords[4] = tx - 1;
  1602. coords[5] = ty + TILESIZE;
  1603. draw_polygon(dr, coords, 3, ch, ch);
  1604. unclip(dr);
  1605. } else if (val & PIECE_BLCORNER) {
  1606. draw_rect(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
  1607. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch);
  1608. clip(dr, tx, ty+TILESIZE-HIGHLIGHT_WIDTH,
  1609. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
  1610. coords[0] = tx - 1;
  1611. coords[1] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1612. coords[2] = tx + HIGHLIGHT_WIDTH;
  1613. coords[3] = ty + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1614. coords[4] = tx - 1;
  1615. coords[5] = ty + TILESIZE;
  1616. draw_polygon(dr, coords, 3, cl, cl);
  1617. unclip(dr);
  1618. }
  1619. if (!((PIECE_TBORDER | PIECE_RBORDER) &~ val)) {
  1620. draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
  1621. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl);
  1622. clip(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
  1623. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
  1624. coords[0] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1625. coords[1] = ty - 1;
  1626. coords[2] = tx + TILESIZE;
  1627. coords[3] = ty - 1;
  1628. coords[4] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1629. coords[5] = ty + HIGHLIGHT_WIDTH;
  1630. draw_polygon(dr, coords, 3, ch, ch);
  1631. unclip(dr);
  1632. } else if (val & PIECE_TRCORNER) {
  1633. draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
  1634. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch);
  1635. clip(dr, tx+TILESIZE-HIGHLIGHT_WIDTH, ty,
  1636. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH);
  1637. coords[0] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1638. coords[1] = ty - 1;
  1639. coords[2] = tx + TILESIZE;
  1640. coords[3] = ty - 1;
  1641. coords[4] = tx + TILESIZE - HIGHLIGHT_WIDTH - 1;
  1642. coords[5] = ty + HIGHLIGHT_WIDTH;
  1643. draw_polygon(dr, coords, 3, cl, cl);
  1644. unclip(dr);
  1645. }
  1646. if (val & PIECE_TLCORNER)
  1647. draw_rect(dr, tx, ty, HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, ch);
  1648. if (val & PIECE_BRCORNER)
  1649. draw_rect(dr, tx+TILESIZE-HIGHLIGHT_WIDTH,
  1650. ty+TILESIZE-HIGHLIGHT_WIDTH,
  1651. HIGHLIGHT_WIDTH, HIGHLIGHT_WIDTH, cl);
  1652. }
  1653. static void draw_piecepart(drawing *dr, game_drawstate *ds,
  1654. int tx, int ty, unsigned long val,
  1655. int cl, int cc, int ch)
  1656. {
  1657. int x[6], y[6];
  1658. /*
  1659. * Drawing the blocks is hellishly fiddly. The blocks don't
  1660. * stretch to the full size of the tile; there's a border
  1661. * around them of size BORDER_WIDTH. Then they have bevelled
  1662. * borders of size HIGHLIGHT_WIDTH, and also rounded corners.
  1663. *
  1664. * I tried for some time to find a clean and clever way to
  1665. * figure out what needed drawing from the corner and border
  1666. * flags, but in the end the cleanest way I could find was the
  1667. * following. We divide the grid square into 25 parts by
  1668. * ruling four horizontal and four vertical lines across it;
  1669. * those lines are at BORDER_WIDTH and BORDER_WIDTH +
  1670. * HIGHLIGHT_WIDTH from the top, from the bottom, from the
  1671. * left and from the right. Then we carefully consider each of
  1672. * the resulting 25 sections of square, and decide separately
  1673. * what needs to go in it based on the flags. In complicated
  1674. * cases there can be up to five possibilities affecting any
  1675. * given section (no corner or border flags, just the corner
  1676. * flag, one border flag, the other border flag, both border
  1677. * flags). So there's a lot of very fiddly logic here and all
  1678. * I could really think to do was give it my best shot and
  1679. * then test it and correct all the typos. Not fun to write,
  1680. * and I'm sure it isn't fun to read either, but it seems to
  1681. * work.
  1682. */
  1683. x[0] = tx;
  1684. x[1] = x[0] + BORDER_WIDTH;
  1685. x[2] = x[1] + HIGHLIGHT_WIDTH;
  1686. x[5] = tx + TILESIZE;
  1687. x[4] = x[5] - BORDER_WIDTH;
  1688. x[3] = x[4] - HIGHLIGHT_WIDTH;
  1689. y[0] = ty;
  1690. y[1] = y[0] + BORDER_WIDTH;
  1691. y[2] = y[1] + HIGHLIGHT_WIDTH;
  1692. y[5] = ty + TILESIZE;
  1693. y[4] = y[5] - BORDER_WIDTH;
  1694. y[3] = y[4] - HIGHLIGHT_WIDTH;
  1695. #define RECT(p,q) x[p], y[q], x[(p)+1]-x[p], y[(q)+1]-y[q]
  1696. maybe_rect(dr, RECT(0,0),
  1697. (val & (PIECE_TLCORNER | PIECE_TBORDER |
  1698. PIECE_LBORDER)) ? -1 : cc, -1);
  1699. maybe_rect(dr, RECT(1,0),
  1700. (val & PIECE_TLCORNER) ? ch : (val & PIECE_TBORDER) ? -1 :
  1701. (val & PIECE_LBORDER) ? ch : cc, -1);
  1702. maybe_rect(dr, RECT(2,0),
  1703. (val & PIECE_TBORDER) ? -1 : cc, -1);
  1704. maybe_rect(dr, RECT(3,0),
  1705. (val & PIECE_TRCORNER) ? cl : (val & PIECE_TBORDER) ? -1 :
  1706. (val & PIECE_RBORDER) ? cl : cc, -1);
  1707. maybe_rect(dr, RECT(4,0),
  1708. (val & (PIECE_TRCORNER | PIECE_TBORDER |
  1709. PIECE_RBORDER)) ? -1 : cc, -1);
  1710. maybe_rect(dr, RECT(0,1),
  1711. (val & PIECE_TLCORNER) ? ch : (val & PIECE_LBORDER) ? -1 :
  1712. (val & PIECE_TBORDER) ? ch : cc, -1);
  1713. maybe_rect(dr, RECT(1,1),
  1714. (val & PIECE_TLCORNER) ? cc : -1, -1);
  1715. maybe_rect(dr, RECT(1,1),
  1716. (val & PIECE_TLCORNER) ? ch | TYPE_TLCIRC :
  1717. !((PIECE_TBORDER | PIECE_LBORDER) &~ val) ? ch | TYPE_BRCIRC :
  1718. (val & (PIECE_TBORDER | PIECE_LBORDER)) ? ch : cc, -1);
  1719. maybe_rect(dr, RECT(2,1),
  1720. (val & PIECE_TBORDER) ? ch : cc, -1);
  1721. maybe_rect(dr, RECT(3,1),
  1722. (val & PIECE_TRCORNER) ? cc : -1, -1);
  1723. maybe_rect(dr, RECT(3,1),
  1724. (val & (PIECE_TBORDER | PIECE_RBORDER)) == PIECE_TBORDER ? ch :
  1725. (val & (PIECE_TBORDER | PIECE_RBORDER)) == PIECE_RBORDER ? cl :
  1726. !((PIECE_TBORDER|PIECE_RBORDER) &~ val) ? cl | TYPE_BLCIRC :
  1727. (val & PIECE_TRCORNER) ? cl | TYPE_TRCIRC :
  1728. cc, ch);
  1729. maybe_rect(dr, RECT(4,1),
  1730. (val & PIECE_TRCORNER) ? ch : (val & PIECE_RBORDER) ? -1 :
  1731. (val & PIECE_TBORDER) ? ch : cc, -1);
  1732. maybe_rect(dr, RECT(0,2),
  1733. (val & PIECE_LBORDER) ? -1 : cc, -1);
  1734. maybe_rect(dr, RECT(1,2),
  1735. (val & PIECE_LBORDER) ? ch : cc, -1);
  1736. maybe_rect(dr, RECT(2,2),
  1737. cc, -1);
  1738. maybe_rect(dr, RECT(3,2),
  1739. (val & PIECE_RBORDER) ? cl : cc, -1);
  1740. maybe_rect(dr, RECT(4,2),
  1741. (val & PIECE_RBORDER) ? -1 : cc, -1);
  1742. maybe_rect(dr, RECT(0,3),
  1743. (val & PIECE_BLCORNER) ? cl : (val & PIECE_LBORDER) ? -1 :
  1744. (val & PIECE_BBORDER) ? cl : cc, -1);
  1745. maybe_rect(dr, RECT(1,3),
  1746. (val & PIECE_BLCORNER) ? cc : -1, -1);
  1747. maybe_rect(dr, RECT(1,3),
  1748. (val & (PIECE_BBORDER | PIECE_LBORDER)) == PIECE_BBORDER ? cl :
  1749. (val & (PIECE_BBORDER | PIECE_LBORDER)) == PIECE_LBORDER ? ch :
  1750. !((PIECE_BBORDER|PIECE_LBORDER) &~ val) ? ch | TYPE_TRCIRC :
  1751. (val & PIECE_BLCORNER) ? ch | TYPE_BLCIRC :
  1752. cc, cl);
  1753. maybe_rect(dr, RECT(2,3),
  1754. (val & PIECE_BBORDER) ? cl : cc, -1);
  1755. maybe_rect(dr, RECT(3,3),
  1756. (val & PIECE_BRCORNER) ? cc : -1, -1);
  1757. maybe_rect(dr, RECT(3,3),
  1758. (val & PIECE_BRCORNER) ? cl | TYPE_BRCIRC :
  1759. !((PIECE_BBORDER | PIECE_RBORDER) &~ val) ? cl | TYPE_TLCIRC :
  1760. (val & (PIECE_BBORDER | PIECE_RBORDER)) ? cl : cc, -1);
  1761. maybe_rect(dr, RECT(4,3),
  1762. (val & PIECE_BRCORNER) ? cl : (val & PIECE_RBORDER) ? -1 :
  1763. (val & PIECE_BBORDER) ? cl : cc, -1);
  1764. maybe_rect(dr, RECT(0,4),
  1765. (val & (PIECE_BLCORNER | PIECE_BBORDER |
  1766. PIECE_LBORDER)) ? -1 : cc, -1);
  1767. maybe_rect(dr, RECT(1,4),
  1768. (val & PIECE_BLCORNER) ? ch : (val & PIECE_BBORDER) ? -1 :
  1769. (val & PIECE_LBORDER) ? ch : cc, -1);
  1770. maybe_rect(dr, RECT(2,4),
  1771. (val & PIECE_BBORDER) ? -1 : cc, -1);
  1772. maybe_rect(dr, RECT(3,4),
  1773. (val & PIECE_BRCORNER) ? cl : (val & PIECE_BBORDER) ? -1 :
  1774. (val & PIECE_RBORDER) ? cl : cc, -1);
  1775. maybe_rect(dr, RECT(4,4),
  1776. (val & (PIECE_BRCORNER | PIECE_BBORDER |
  1777. PIECE_RBORDER)) ? -1 : cc, -1);
  1778. #undef RECT
  1779. }
  1780. static void draw_tile(drawing *dr, game_drawstate *ds,
  1781. int x, int y, unsigned long val)
  1782. {
  1783. int tx = COORD(x), ty = COORD(y);
  1784. int cc, ch, cl;
  1785. /*
  1786. * Draw the tile background.
  1787. */
  1788. if (val & BG_TARGET)
  1789. cc = COL_TARGET;
  1790. else
  1791. cc = COL_BACKGROUND;
  1792. ch = cc+1;
  1793. cl = cc+2;
  1794. if (val & FLASH_LOW)
  1795. cc = cl;
  1796. else if (val & FLASH_HIGH)
  1797. cc = ch;
  1798. draw_rect(dr, tx, ty, TILESIZE, TILESIZE, cc);
  1799. if (val & BG_FORCEFIELD) {
  1800. /*
  1801. * Cattle-grid effect to indicate that nothing but the
  1802. * main block can slide over this square.
  1803. */
  1804. int n = 3 * (TILESIZE / (3*HIGHLIGHT_WIDTH));
  1805. int i;
  1806. for (i = 1; i < n; i += 3) {
  1807. draw_rect(dr, tx,ty+(TILESIZE*i/n), TILESIZE,HIGHLIGHT_WIDTH, cl);
  1808. draw_rect(dr, tx+(TILESIZE*i/n),ty, HIGHLIGHT_WIDTH,TILESIZE, cl);
  1809. }
  1810. }
  1811. /*
  1812. * Draw the tile midground: a shadow of a block, for
  1813. * displaying partial solutions.
  1814. */
  1815. if (val & FG_SHADOW) {
  1816. draw_piecepart(dr, ds, tx, ty, (val >> FG_SHADOWSH) & PIECE_MASK,
  1817. cl, cl, cl);
  1818. }
  1819. /*
  1820. * Draw the tile foreground, i.e. some section of a block or
  1821. * wall.
  1822. */
  1823. if (val & FG_WALL) {
  1824. cc = COL_BACKGROUND;
  1825. ch = cc+1;
  1826. cl = cc+2;
  1827. if (val & FLASH_LOW)
  1828. cc = cl;
  1829. else if (val & FLASH_HIGH)
  1830. cc = ch;
  1831. draw_wallpart(dr, ds, tx, ty, (val >> FG_MAINPIECESH) & PIECE_MASK,
  1832. cl, cc, ch);
  1833. } else if (val & (FG_MAIN | FG_NORMAL)) {
  1834. if (val & FG_DRAGGING)
  1835. cc = (val & FG_MAIN ? COL_MAIN_DRAGGING : COL_DRAGGING);
  1836. else
  1837. cc = (val & FG_MAIN ? COL_MAIN : COL_BACKGROUND);
  1838. ch = cc+1;
  1839. cl = cc+2;
  1840. if (val & FLASH_LOW)
  1841. cc = cl;
  1842. else if (val & (FLASH_HIGH | FG_SOLVEPIECE))
  1843. cc = ch;
  1844. draw_piecepart(dr, ds, tx, ty, (val >> FG_MAINPIECESH) & PIECE_MASK,
  1845. cl, cc, ch);
  1846. }
  1847. draw_update(dr, tx, ty, TILESIZE, TILESIZE);
  1848. }
  1849. static unsigned long find_piecepart(int w, int h, DSF *dsf, int x, int y)
  1850. {
  1851. int i = y*w+x;
  1852. int canon = dsf_canonify(dsf, i);
  1853. unsigned long val = 0;
  1854. if (x == 0 || canon != dsf_canonify(dsf, i-1))
  1855. val |= PIECE_LBORDER;
  1856. if (y== 0 || canon != dsf_canonify(dsf, i-w))
  1857. val |= PIECE_TBORDER;
  1858. if (x == w-1 || canon != dsf_canonify(dsf, i+1))
  1859. val |= PIECE_RBORDER;
  1860. if (y == h-1 || canon != dsf_canonify(dsf, i+w))
  1861. val |= PIECE_BBORDER;
  1862. if (!(val & (PIECE_TBORDER | PIECE_LBORDER)) &&
  1863. canon != dsf_canonify(dsf, i-1-w))
  1864. val |= PIECE_TLCORNER;
  1865. if (!(val & (PIECE_TBORDER | PIECE_RBORDER)) &&
  1866. canon != dsf_canonify(dsf, i+1-w))
  1867. val |= PIECE_TRCORNER;
  1868. if (!(val & (PIECE_BBORDER | PIECE_LBORDER)) &&
  1869. canon != dsf_canonify(dsf, i-1+w))
  1870. val |= PIECE_BLCORNER;
  1871. if (!(val & (PIECE_BBORDER | PIECE_RBORDER)) &&
  1872. canon != dsf_canonify(dsf, i+1+w))
  1873. val |= PIECE_BRCORNER;
  1874. return val;
  1875. }
  1876. static void game_redraw(drawing *dr, game_drawstate *ds,
  1877. const game_state *oldstate, const game_state *state,
  1878. int dir, const game_ui *ui,
  1879. float animtime, float flashtime)
  1880. {
  1881. int w = state->w, h = state->h, wh = w*h;
  1882. unsigned char *board;
  1883. DSF *dsf;
  1884. int x, y, mainanchor, mainpos, dragpos, solvepos, solvesrc, solvedst;
  1885. /*
  1886. * Construct the board we'll be displaying (which may be
  1887. * different from the one in state if ui describes a drag in
  1888. * progress).
  1889. */
  1890. board = snewn(wh, unsigned char);
  1891. memcpy(board, state->board, wh);
  1892. if (ui->dragging) {
  1893. bool mpret = move_piece(w, h, state->board, board,
  1894. state->imm->forcefield,
  1895. ui->drag_anchor, ui->drag_currpos);
  1896. assert(mpret);
  1897. }
  1898. if (state->soln) {
  1899. solvesrc = state->soln->moves[state->soln_index*2];
  1900. solvedst = state->soln->moves[state->soln_index*2+1];
  1901. if (solvesrc == state->lastmoved_pos)
  1902. solvesrc = state->lastmoved;
  1903. if (solvesrc == ui->drag_anchor)
  1904. solvesrc = ui->drag_currpos;
  1905. } else
  1906. solvesrc = solvedst = -1;
  1907. /*
  1908. * Build a dsf out of that board, so we can conveniently tell
  1909. * which edges are connected and which aren't.
  1910. */
  1911. dsf = dsf_new(wh);
  1912. mainanchor = -1;
  1913. for (y = 0; y < h; y++)
  1914. for (x = 0; x < w; x++) {
  1915. int i = y*w+x;
  1916. if (ISDIST(board[i]))
  1917. dsf_merge(dsf, i, i - board[i]);
  1918. if (board[i] == MAINANCHOR)
  1919. mainanchor = i;
  1920. if (board[i] == WALL) {
  1921. if (x > 0 && board[i-1] == WALL)
  1922. dsf_merge(dsf, i, i-1);
  1923. if (y > 0 && board[i-w] == WALL)
  1924. dsf_merge(dsf, i, i-w);
  1925. }
  1926. }
  1927. assert(mainanchor >= 0);
  1928. mainpos = dsf_canonify(dsf, mainanchor);
  1929. dragpos = ui->drag_currpos > 0 ? dsf_canonify(dsf, ui->drag_currpos) : -1;
  1930. solvepos = solvesrc >= 0 ? dsf_canonify(dsf, solvesrc) : -1;
  1931. /*
  1932. * Now we can construct the data about what we want to draw.
  1933. */
  1934. for (y = 0; y < h; y++)
  1935. for (x = 0; x < w; x++) {
  1936. int i = y*w+x;
  1937. int j;
  1938. unsigned long val;
  1939. int canon;
  1940. /*
  1941. * See if this square is part of the target area.
  1942. */
  1943. j = i + mainanchor - (state->ty * w + state->tx);
  1944. while (j >= 0 && j < wh && ISDIST(board[j]))
  1945. j -= board[j];
  1946. if (j == mainanchor)
  1947. val = BG_TARGET;
  1948. else
  1949. val = BG_NORMAL;
  1950. if (state->imm->forcefield[i])
  1951. val |= BG_FORCEFIELD;
  1952. if (flashtime > 0) {
  1953. int flashtype = (int)(flashtime / FLASH_INTERVAL) & 1;
  1954. val |= (flashtype ? FLASH_LOW : FLASH_HIGH);
  1955. }
  1956. if (board[i] != EMPTY) {
  1957. canon = dsf_canonify(dsf, i);
  1958. if (board[i] == WALL)
  1959. val |= FG_WALL;
  1960. else if (canon == mainpos)
  1961. val |= FG_MAIN;
  1962. else
  1963. val |= FG_NORMAL;
  1964. if (canon == dragpos)
  1965. val |= FG_DRAGGING;
  1966. if (canon == solvepos)
  1967. val |= FG_SOLVEPIECE;
  1968. /*
  1969. * Now look around to see if other squares
  1970. * belonging to the same block are adjacent to us.
  1971. */
  1972. val |= find_piecepart(w, h, dsf, x, y) << FG_MAINPIECESH;
  1973. }
  1974. /*
  1975. * If we're in the middle of showing a solution,
  1976. * display a shadow piece for the target of the
  1977. * current move.
  1978. */
  1979. if (solvepos >= 0) {
  1980. int si = i - solvedst + solvesrc;
  1981. if (si >= 0 && si < wh && dsf_canonify(dsf, si) == solvepos) {
  1982. val |= find_piecepart(w, h, dsf,
  1983. si % w, si / w) << FG_SHADOWSH;
  1984. val |= FG_SHADOW;
  1985. }
  1986. }
  1987. if (val != ds->grid[i]) {
  1988. draw_tile(dr, ds, x, y, val);
  1989. ds->grid[i] = val;
  1990. }
  1991. }
  1992. /*
  1993. * Update the status bar.
  1994. */
  1995. {
  1996. char statusbuf[256];
  1997. sprintf(statusbuf, "%sMoves: %d",
  1998. (state->completed >= 0 ?
  1999. (state->cheated ? "Auto-solved. " : "COMPLETED! ") :
  2000. (state->cheated ? "Auto-solver used. " : "")),
  2001. (state->completed >= 0 ? state->completed : state->movecount));
  2002. if (state->minmoves >= 0)
  2003. sprintf(statusbuf+strlen(statusbuf), " (min %d)",
  2004. state->minmoves);
  2005. status_bar(dr, statusbuf);
  2006. }
  2007. dsf_free(dsf);
  2008. sfree(board);
  2009. }
  2010. static float game_anim_length(const game_state *oldstate,
  2011. const game_state *newstate, int dir, game_ui *ui)
  2012. {
  2013. return 0.0F;
  2014. }
  2015. static float game_flash_length(const game_state *oldstate,
  2016. const game_state *newstate, int dir, game_ui *ui)
  2017. {
  2018. if (oldstate->completed < 0 && newstate->completed >= 0)
  2019. return FLASH_TIME;
  2020. return 0.0F;
  2021. }
  2022. static void game_get_cursor_location(const game_ui *ui,
  2023. const game_drawstate *ds,
  2024. const game_state *state,
  2025. const game_params *params,
  2026. int *x, int *y, int *w, int *h)
  2027. {
  2028. }
  2029. static int game_status(const game_state *state)
  2030. {
  2031. return state->completed ? +1 : 0;
  2032. }
  2033. static bool game_timing_state(const game_state *state, game_ui *ui)
  2034. {
  2035. return true;
  2036. }
  2037. static void game_print_size(const game_params *params, const game_ui *ui,
  2038. float *x, float *y)
  2039. {
  2040. }
  2041. static void game_print(drawing *dr, const game_state *state, const game_ui *ui,
  2042. int tilesize)
  2043. {
  2044. }
  2045. #ifdef COMBINED
  2046. #define thegame slide
  2047. #endif
  2048. const struct game thegame = {
  2049. "Slide", NULL, NULL,
  2050. default_params,
  2051. game_fetch_preset, NULL,
  2052. decode_params,
  2053. encode_params,
  2054. free_params,
  2055. dup_params,
  2056. true, game_configure, custom_params,
  2057. validate_params,
  2058. new_game_desc,
  2059. validate_desc,
  2060. new_game,
  2061. dup_game,
  2062. free_game,
  2063. true, solve_game,
  2064. true, game_can_format_as_text_now, game_text_format,
  2065. NULL, NULL, /* get_prefs, set_prefs */
  2066. new_ui,
  2067. free_ui,
  2068. NULL, /* encode_ui */
  2069. NULL, /* decode_ui */
  2070. NULL, /* game_request_keys */
  2071. game_changed_state,
  2072. NULL, /* current_key_label */
  2073. interpret_move,
  2074. execute_move,
  2075. PREFERRED_TILESIZE, game_compute_size, game_set_size,
  2076. game_colours,
  2077. game_new_drawstate,
  2078. game_free_drawstate,
  2079. game_redraw,
  2080. game_anim_length,
  2081. game_flash_length,
  2082. game_get_cursor_location,
  2083. game_status,
  2084. false, false, game_print_size, game_print,
  2085. true, /* wants_statusbar */
  2086. false, game_timing_state,
  2087. 0, /* flags */
  2088. };
  2089. #ifdef STANDALONE_SOLVER
  2090. #include <stdarg.h>
  2091. int main(int argc, char **argv)
  2092. {
  2093. game_params *p;
  2094. game_state *s;
  2095. char *id = NULL, *desc;
  2096. const char *err;
  2097. bool count = false;
  2098. int ret;
  2099. int *moves;
  2100. while (--argc > 0) {
  2101. char *p = *++argv;
  2102. /*
  2103. if (!strcmp(p, "-v")) {
  2104. verbose = true;
  2105. } else
  2106. */
  2107. if (!strcmp(p, "-c")) {
  2108. count = true;
  2109. } else if (*p == '-') {
  2110. fprintf(stderr, "%s: unrecognised option `%s'\n", argv[0], p);
  2111. return 1;
  2112. } else {
  2113. id = p;
  2114. }
  2115. }
  2116. if (!id) {
  2117. fprintf(stderr, "usage: %s [-c | -v] <game_id>\n", argv[0]);
  2118. return 1;
  2119. }
  2120. desc = strchr(id, ':');
  2121. if (!desc) {
  2122. fprintf(stderr, "%s: game id expects a colon in it\n", argv[0]);
  2123. return 1;
  2124. }
  2125. *desc++ = '\0';
  2126. p = default_params();
  2127. decode_params(p, id);
  2128. err = validate_desc(p, desc);
  2129. if (err) {
  2130. fprintf(stderr, "%s: %s\n", argv[0], err);
  2131. return 1;
  2132. }
  2133. s = new_game(NULL, p, desc);
  2134. ret = solve_board(s->w, s->h, s->board, s->imm->forcefield,
  2135. s->tx, s->ty, -1, &moves);
  2136. if (ret < 0) {
  2137. printf("No solution found\n");
  2138. } else {
  2139. int index = 0;
  2140. if (count) {
  2141. printf("%d moves required\n", ret);
  2142. return 0;
  2143. }
  2144. while (1) {
  2145. bool moveret;
  2146. char *text = board_text_format(s->w, s->h, s->board,
  2147. s->imm->forcefield);
  2148. game_state *s2;
  2149. printf("position %d:\n%s", index, text);
  2150. if (index >= ret)
  2151. break;
  2152. s2 = dup_game(s);
  2153. moveret = move_piece(s->w, s->h, s->board,
  2154. s2->board, s->imm->forcefield,
  2155. moves[index*2], moves[index*2+1]);
  2156. assert(moveret);
  2157. free_game(s);
  2158. s = s2;
  2159. index++;
  2160. }
  2161. }
  2162. return 0;
  2163. }
  2164. #endif