sokoban.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * sokoban.c: An implementation of the well-known Sokoban barrel-
  3. * pushing game. Random generation is too simplistic to be
  4. * credible, but the rest of the gameplay works well enough to use
  5. * it with hand-written level descriptions.
  6. */
  7. /*
  8. * TODO:
  9. *
  10. * - I think it would be better to ditch the `prev' array, and
  11. * instead make the `dist' array strictly monotonic (by having
  12. * each distance be something like I*A+S, where A is the grid
  13. * area, I the number of INITIAL squares trampled on, and S the
  14. * number of harmless spaces moved through). This would permit
  15. * the path-tracing when a pull is actually made to choose
  16. * randomly from all the possible shortest routes, which would
  17. * be superior in terms of eliminating directional bias.
  18. * + So when tracing the path back to the current px,py, we
  19. * look at all four adjacent squares, find the minimum
  20. * distance, check that it's _strictly smaller_ than that of
  21. * the current square, and restrict our choice to precisely
  22. * those squares with that minimum distance.
  23. * + The other place `prev' is currently used is in the check
  24. * for consistency of a pull. We would have to replace the
  25. * check for whether prev[ny*w+nx]==oy*w+ox with a check that
  26. * made sure there was at least one adjacent square with a
  27. * smaller distance which _wasn't_ oy*w+ox. Then when we did
  28. * the path-tracing we'd also have to take this special case
  29. * into account.
  30. *
  31. * - More discriminating choice of pull. (Snigger.)
  32. * + favour putting targets in clumps
  33. * + try to shoot for a reasonably consistent number of barrels
  34. * (adjust willingness to generate a new barrel depending on
  35. * how many are already present)
  36. * + adjust willingness to break new ground depending on how
  37. * much is already broken
  38. *
  39. * - generation time parameters:
  40. * + enable NetHack mode (and find a better place for the hole)
  41. * + decide how many of the remaining Is should be walls
  42. *
  43. * - at the end of generation, randomly position the starting
  44. * player coordinates, probably by (somehow) reusing the same
  45. * bfs currently inside the loop.
  46. *
  47. * - possible backtracking?
  48. *
  49. * - IWBNI we could spot completely unreachable bits of level at
  50. * the outside, and not bother drawing grid lines for them. The
  51. * NH levels currently look a bit weird with grid lines on the
  52. * outside of the boundary.
  53. */
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #include <assert.h>
  58. #include <ctype.h>
  59. #ifdef NO_TGMATH_H
  60. # include <math.h>
  61. #else
  62. # include <tgmath.h>
  63. #endif
  64. #include "puzzles.h"
  65. /*
  66. * Various subsets of these constants are used during game
  67. * generation, game play, game IDs and the game_drawstate.
  68. */
  69. #define INITIAL 'i' /* used only in game generation */
  70. #define SPACE 's'
  71. #define WALL 'w'
  72. #define PIT 'p'
  73. #define DEEP_PIT 'd'
  74. #define TARGET 't'
  75. #define BARREL 'b'
  76. #define BARRELTARGET 'f' /* target is 'f'illed */
  77. #define PLAYER 'u' /* yo'u'; used in game IDs */
  78. #define PLAYERTARGET 'v' /* bad letter: v is to u as t is to s */
  79. #define INVALID '!' /* used in drawstate to force redraw */
  80. /*
  81. * We also support the use of any capital letter as a barrel, which
  82. * will be displayed with that letter as a label. (This facilitates
  83. * people distributing annotated game IDs for particular Sokoban
  84. * levels, so they can accompany them with verbal instructions
  85. * about pushing particular barrels in particular ways.) Therefore,
  86. * to find out whether something is a barrel, we need a test
  87. * function which does a bit more than just comparing to BARREL.
  88. *
  89. * When resting on target squares, capital-letter barrels are
  90. * replaced with their control-character value (A -> ^A).
  91. */
  92. #define IS_PLAYER(c) ( (c)==PLAYER || (c)==PLAYERTARGET )
  93. #define IS_BARREL(c) ( (c)==BARREL || (c)==BARRELTARGET || \
  94. ((c)>='A' && (c)<='Z') || ((c)>=1 && (c)<=26) )
  95. #define IS_ON_TARGET(c) ( (c)==TARGET || (c)==BARRELTARGET || \
  96. (c)==PLAYERTARGET || ((c)>=1 && (c)<=26) )
  97. #define TARGETISE(b) ( (b)==BARREL ? BARRELTARGET : (b)-('A'-1) )
  98. #define DETARGETISE(b) ( (b)==BARRELTARGET ? BARREL : (b)+('A'-1) )
  99. #define BARREL_LABEL(b) ( (b)>='A'&&(b)<='Z' ? (b) : \
  100. (b)>=1 && (b)<=26 ? (b)+('A'-1) : 0 )
  101. #define DX(d) (d == 0 ? -1 : d == 2 ? +1 : 0)
  102. #define DY(d) (d == 1 ? -1 : d == 3 ? +1 : 0)
  103. #define FLASH_LENGTH 0.3F
  104. enum {
  105. COL_BACKGROUND,
  106. COL_TARGET,
  107. COL_PIT,
  108. COL_DEEP_PIT,
  109. COL_BARREL,
  110. COL_PLAYER,
  111. COL_TEXT,
  112. COL_GRID,
  113. COL_OUTLINE,
  114. COL_HIGHLIGHT,
  115. COL_LOWLIGHT,
  116. COL_WALL,
  117. NCOLOURS
  118. };
  119. struct game_params {
  120. int w, h;
  121. /*
  122. * FIXME: a parameter involving degree of filling in?
  123. */
  124. };
  125. struct game_state {
  126. game_params p;
  127. unsigned char *grid;
  128. int px, py;
  129. bool completed;
  130. };
  131. static game_params *default_params(void)
  132. {
  133. game_params *ret = snew(game_params);
  134. ret->w = 12;
  135. ret->h = 10;
  136. return ret;
  137. }
  138. static void free_params(game_params *params)
  139. {
  140. sfree(params);
  141. }
  142. static game_params *dup_params(const game_params *params)
  143. {
  144. game_params *ret = snew(game_params);
  145. *ret = *params; /* structure copy */
  146. return ret;
  147. }
  148. static const struct game_params sokoban_presets[] = {
  149. { 12, 10 },
  150. { 16, 12 },
  151. { 20, 16 },
  152. };
  153. static bool game_fetch_preset(int i, char **name, game_params **params)
  154. {
  155. game_params p, *ret;
  156. char *retname;
  157. char namebuf[80];
  158. if (i < 0 || i >= lenof(sokoban_presets))
  159. return false;
  160. p = sokoban_presets[i];
  161. ret = dup_params(&p);
  162. sprintf(namebuf, "%dx%d", ret->w, ret->h);
  163. retname = dupstr(namebuf);
  164. *params = ret;
  165. *name = retname;
  166. return true;
  167. }
  168. static void decode_params(game_params *params, char const *string)
  169. {
  170. params->w = params->h = atoi(string);
  171. while (*string && isdigit((unsigned char)*string)) string++;
  172. if (*string == 'x') {
  173. string++;
  174. params->h = atoi(string);
  175. }
  176. }
  177. static char *encode_params(const game_params *params, bool full)
  178. {
  179. char data[256];
  180. sprintf(data, "%dx%d", params->w, params->h);
  181. return dupstr(data);
  182. }
  183. static config_item *game_configure(const game_params *params)
  184. {
  185. config_item *ret;
  186. char buf[80];
  187. ret = snewn(3, config_item);
  188. ret[0].name = "Width";
  189. ret[0].type = C_STRING;
  190. sprintf(buf, "%d", params->w);
  191. ret[0].u.string.sval = dupstr(buf);
  192. ret[1].name = "Height";
  193. ret[1].type = C_STRING;
  194. sprintf(buf, "%d", params->h);
  195. ret[1].u.string.sval = dupstr(buf);
  196. ret[2].name = NULL;
  197. ret[2].type = C_END;
  198. return ret;
  199. }
  200. static game_params *custom_params(const config_item *cfg)
  201. {
  202. game_params *ret = snew(game_params);
  203. ret->w = atoi(cfg[0].u.string.sval);
  204. ret->h = atoi(cfg[1].u.string.sval);
  205. return ret;
  206. }
  207. static const char *validate_params(const game_params *params, bool full)
  208. {
  209. if (params->w < 4 || params->h < 4)
  210. return "Width and height must both be at least 4";
  211. return NULL;
  212. }
  213. /* ----------------------------------------------------------------------
  214. * Game generation mechanism.
  215. *
  216. * To generate a Sokoban level, we begin with a completely blank
  217. * grid and make valid inverse moves. Grid squares can be in a
  218. * number of states. The states are:
  219. *
  220. * - INITIAL: this square has not as yet been touched by any
  221. * inverse move, which essentially means we haven't decided what
  222. * it is yet.
  223. *
  224. * - SPACE: this square is a space.
  225. *
  226. * - TARGET: this square is a space which is also the target for a
  227. * barrel.
  228. *
  229. * - BARREL: this square contains a barrel.
  230. *
  231. * - BARRELTARGET: this square contains a barrel _on_ a target.
  232. *
  233. * - WALL: this square is a wall.
  234. *
  235. * - PLAYER: this square contains the player.
  236. *
  237. * - PLAYERTARGET: this square contains the player on a target.
  238. *
  239. * We begin with every square of the in state INITIAL, apart from a
  240. * solid ring of WALLs around the edge. We randomly position the
  241. * PLAYER somewhere. Thereafter our valid moves are:
  242. *
  243. * - to move the PLAYER in one direction _pulling_ a barrel after
  244. * us. For this to work, we must have SPACE or INITIAL in the
  245. * direction we're moving, and BARREL or BARRELTARGET in the
  246. * direction we're moving away from. We leave SPACE or TARGET
  247. * respectively in the vacated square.
  248. *
  249. * - to create a new barrel by transforming an INITIAL square into
  250. * BARRELTARGET.
  251. *
  252. * - to move the PLAYER freely through SPACE and TARGET squares,
  253. * leaving SPACE or TARGET where it started.
  254. *
  255. * - to move the player through INITIAL squares, carving a tunnel
  256. * of SPACEs as it goes.
  257. *
  258. * We try to avoid destroying INITIAL squares wherever possible (if
  259. * there's a path to where we want to be using only SPACE, then we
  260. * should always use that). At the end of generation, every square
  261. * still in state INITIAL is one which was not required at any
  262. * point during generation, which means we can randomly choose
  263. * whether to make it SPACE or WALL.
  264. *
  265. * It's unclear as yet what the right strategy for wall placement
  266. * should be. Too few WALLs will yield many alternative solutions
  267. * to the puzzle, whereas too many might rule out so many
  268. * possibilities that the intended solution becomes obvious.
  269. */
  270. static void sokoban_generate(int w, int h, unsigned char *grid, int moves,
  271. bool nethack, random_state *rs)
  272. {
  273. struct pull {
  274. int ox, oy, nx, ny, score;
  275. };
  276. struct pull *pulls;
  277. int *dist, *prev, *heap;
  278. int x, y, px, py, i, j, d, heapsize, npulls;
  279. pulls = snewn(w * h * 4, struct pull);
  280. dist = snewn(w * h, int);
  281. prev = snewn(w * h, int);
  282. heap = snewn(w * h, int);
  283. /*
  284. * Configure the initial grid.
  285. */
  286. for (y = 0; y < h; y++)
  287. for (x = 0; x < w; x++)
  288. grid[y*w+x] = (x == 0 || y == 0 || x == w-1 || y == h-1 ?
  289. WALL : INITIAL);
  290. if (nethack)
  291. grid[1] = DEEP_PIT;
  292. /*
  293. * Place the player.
  294. */
  295. i = random_upto(rs, (w-2) * (h-2));
  296. x = 1 + i % (w-2);
  297. y = 1 + i / (w-2);
  298. grid[y*w+x] = SPACE;
  299. px = x;
  300. py = y;
  301. /*
  302. * Now loop around making random inverse Sokoban moves. In this
  303. * loop we aim to make one actual barrel-pull per iteration,
  304. * plus as many free moves as are necessary to get into
  305. * position for that pull.
  306. */
  307. while (moves-- >= 0) {
  308. /*
  309. * First enumerate all the viable barrel-pulls we can
  310. * possibly make, counting two pulls of the same barrel in
  311. * different directions as different. We also include pulls
  312. * we can perform by creating a new barrel. Each pull is
  313. * marked with the amount of violence it would have to do
  314. * to the grid.
  315. */
  316. npulls = 0;
  317. for (y = 0; y < h; y++)
  318. for (x = 0; x < w; x++)
  319. for (d = 0; d < 4; d++) {
  320. int dx = DX(d);
  321. int dy = DY(d);
  322. int nx = x + dx, ny = y + dy;
  323. int npx = nx + dx, npy = ny + dy;
  324. int score = 0;
  325. /*
  326. * The candidate move is to put the player at
  327. * (nx,ny), and move him to (npx,npy), pulling
  328. * a barrel at (x,y) to (nx,ny). So first we
  329. * must check that all those squares are within
  330. * the boundaries of the grid. For this it is
  331. * sufficient to check npx,npy.
  332. */
  333. if (npx < 0 || npx >= w || npy < 0 || npy >= h)
  334. continue;
  335. /*
  336. * (x,y) must either be a barrel, or a square
  337. * which we can convert into a barrel.
  338. */
  339. switch (grid[y*w+x]) {
  340. case BARREL: case BARRELTARGET:
  341. break;
  342. case INITIAL:
  343. if (nethack)
  344. continue;
  345. score += 10 /* new_barrel_score */;
  346. break;
  347. case DEEP_PIT:
  348. if (!nethack)
  349. continue;
  350. break;
  351. default:
  352. continue;
  353. }
  354. /*
  355. * (nx,ny) must either be a space, or a square
  356. * which we can convert into a space.
  357. */
  358. switch (grid[ny*w+nx]) {
  359. case SPACE: case TARGET:
  360. break;
  361. case INITIAL:
  362. score += 3 /* new_space_score */;
  363. break;
  364. default:
  365. continue;
  366. }
  367. /*
  368. * (npx,npy) must also either be a space, or a
  369. * square which we can convert into a space.
  370. */
  371. switch (grid[npy*w+npx]) {
  372. case SPACE: case TARGET:
  373. break;
  374. case INITIAL:
  375. score += 3 /* new_space_score */;
  376. break;
  377. default:
  378. continue;
  379. }
  380. /*
  381. * That's sufficient to tag this as a possible
  382. * pull right now. We still don't know if we
  383. * can reach the required player position, but
  384. * that's a job for the subsequent BFS phase to
  385. * tell us.
  386. */
  387. pulls[npulls].ox = x;
  388. pulls[npulls].oy = y;
  389. pulls[npulls].nx = nx;
  390. pulls[npulls].ny = ny;
  391. pulls[npulls].score = score;
  392. #ifdef GENERATION_DIAGNOSTICS
  393. printf("found potential pull: (%d,%d)-(%d,%d) cost %d\n",
  394. pulls[npulls].ox, pulls[npulls].oy,
  395. pulls[npulls].nx, pulls[npulls].ny,
  396. pulls[npulls].score);
  397. #endif
  398. npulls++;
  399. }
  400. #ifdef GENERATION_DIAGNOSTICS
  401. printf("found %d potential pulls\n", npulls);
  402. #endif
  403. /*
  404. * If there are no pulls available at all, we give up.
  405. *
  406. * (FIXME: or perhaps backtrack?)
  407. */
  408. if (npulls == 0)
  409. break;
  410. /*
  411. * Now we do a BFS from our current position, to find all
  412. * the squares we can get the player into.
  413. *
  414. * This BFS is unusually tricky. We want to give a positive
  415. * distance only to squares which we have to carve through
  416. * INITIALs to get to, which means we can't just stick
  417. * every square we reach on the end of our to-do list.
  418. * Instead, we must maintain our list as a proper priority
  419. * queue.
  420. */
  421. for (i = 0; i < w*h; i++)
  422. dist[i] = prev[i] = -1;
  423. heap[0] = py*w+px;
  424. heapsize = 1;
  425. dist[py*w+px] = 0;
  426. #define PARENT(n) ( ((n)-1)/2 )
  427. #define LCHILD(n) ( 2*(n)+1 )
  428. #define RCHILD(n) ( 2*(n)+2 )
  429. #define SWAP(i,j) do { int swaptmp = (i); (i) = (j); (j) = swaptmp; } while (0)
  430. while (heapsize > 0) {
  431. /*
  432. * Pull the smallest element off the heap: it's at
  433. * position 0. Move the arbitrary element from the very
  434. * end of the heap into position 0.
  435. */
  436. y = heap[0] / w;
  437. x = heap[0] % w;
  438. heapsize--;
  439. heap[0] = heap[heapsize];
  440. /*
  441. * Now repeatedly move that arbitrary element down the
  442. * heap by swapping it with the more suitable of its
  443. * children.
  444. */
  445. i = 0;
  446. while (1) {
  447. int lc, rc;
  448. lc = LCHILD(i);
  449. rc = RCHILD(i);
  450. if (lc >= heapsize)
  451. break; /* we've hit bottom */
  452. if (rc >= heapsize) {
  453. /*
  454. * Special case: there is only one child to
  455. * check.
  456. */
  457. if (dist[heap[i]] > dist[heap[lc]])
  458. SWAP(heap[i], heap[lc]);
  459. /* _Now_ we've hit bottom. */
  460. break;
  461. } else {
  462. /*
  463. * The common case: there are two children and
  464. * we must check them both.
  465. */
  466. if (dist[heap[i]] > dist[heap[lc]] ||
  467. dist[heap[i]] > dist[heap[rc]]) {
  468. /*
  469. * Pick the more appropriate child to swap with
  470. * (i.e. the one which would want to be the
  471. * parent if one were above the other - as one
  472. * is about to be).
  473. */
  474. if (dist[heap[lc]] > dist[heap[rc]]) {
  475. SWAP(heap[i], heap[rc]);
  476. i = rc;
  477. } else {
  478. SWAP(heap[i], heap[lc]);
  479. i = lc;
  480. }
  481. } else {
  482. /* This element is in the right place; we're done. */
  483. break;
  484. }
  485. }
  486. }
  487. /*
  488. * OK, that's given us (x,y) for this phase of the
  489. * search. Now try all directions from here.
  490. */
  491. for (d = 0; d < 4; d++) {
  492. int dx = DX(d);
  493. int dy = DY(d);
  494. int nx = x + dx, ny = y + dy;
  495. if (nx < 0 || nx >= w || ny < 0 || ny >= h)
  496. continue;
  497. if (grid[ny*w+nx] != SPACE && grid[ny*w+nx] != TARGET &&
  498. grid[ny*w+nx] != INITIAL)
  499. continue;
  500. if (dist[ny*w+nx] == -1) {
  501. dist[ny*w+nx] = dist[y*w+x] + (grid[ny*w+nx] == INITIAL);
  502. prev[ny*w+nx] = y*w+x;
  503. /*
  504. * Now insert ny*w+nx at the end of the heap,
  505. * and move it down to its appropriate resting
  506. * place.
  507. */
  508. i = heapsize;
  509. heap[heapsize++] = ny*w+nx;
  510. /*
  511. * Swap element n with its parent repeatedly to
  512. * preserve the heap property.
  513. */
  514. while (i > 0) {
  515. int p = PARENT(i);
  516. if (dist[heap[p]] > dist[heap[i]]) {
  517. SWAP(heap[p], heap[i]);
  518. i = p;
  519. } else
  520. break;
  521. }
  522. }
  523. }
  524. }
  525. #undef PARENT
  526. #undef LCHILD
  527. #undef RCHILD
  528. #undef SWAP
  529. #ifdef GENERATION_DIAGNOSTICS
  530. printf("distance map:\n");
  531. for (i = 0; i < h; i++) {
  532. for (j = 0; j < w; j++) {
  533. int d = dist[i*w+j];
  534. int c;
  535. if (d < 0)
  536. c = '#';
  537. else if (d >= 36)
  538. c = '!';
  539. else if (d >= 10)
  540. c = 'A' - 10 + d;
  541. else
  542. c = '0' + d;
  543. putchar(c);
  544. }
  545. putchar('\n');
  546. }
  547. #endif
  548. /*
  549. * Now we can go back through the `pulls' array, adjusting
  550. * the score for each pull depending on how hard it is to
  551. * reach its starting point, and also throwing out any
  552. * whose starting points are genuinely unreachable even
  553. * with the possibility of carving through INITIAL squares.
  554. */
  555. for (i = j = 0; i < npulls; i++) {
  556. #ifdef GENERATION_DIAGNOSTICS
  557. printf("potential pull (%d,%d)-(%d,%d)",
  558. pulls[i].ox, pulls[i].oy,
  559. pulls[i].nx, pulls[i].ny);
  560. #endif
  561. x = pulls[i].nx;
  562. y = pulls[i].ny;
  563. if (dist[y*w+x] < 0) {
  564. #ifdef GENERATION_DIAGNOSTICS
  565. printf(" unreachable\n");
  566. #endif
  567. continue; /* this pull isn't feasible at all */
  568. } else {
  569. /*
  570. * Another nasty special case we have to check is
  571. * whether the initial barrel location (ox,oy) is
  572. * on the path used to reach the square. This can
  573. * occur if that square is in state INITIAL: the
  574. * pull is initially considered valid on the basis
  575. * that the INITIAL can become BARRELTARGET, and
  576. * it's also considered reachable on the basis that
  577. * INITIAL can be turned into SPACE, but it can't
  578. * be both at once.
  579. *
  580. * Fortunately, if (ox,oy) is on the path at all,
  581. * it must be only one space from the end, so this
  582. * is easy to spot and rule out.
  583. */
  584. if (prev[y*w+x] == pulls[i].oy*w+pulls[i].ox) {
  585. #ifdef GENERATION_DIAGNOSTICS
  586. printf(" goes through itself\n");
  587. #endif
  588. continue; /* this pull isn't feasible at all */
  589. }
  590. pulls[j] = pulls[i]; /* structure copy */
  591. pulls[j].score += dist[y*w+x] * 3 /* new_space_score */;
  592. #ifdef GENERATION_DIAGNOSTICS
  593. printf(" reachable at distance %d (cost now %d)\n",
  594. dist[y*w+x], pulls[j].score);
  595. #endif
  596. j++;
  597. }
  598. }
  599. npulls = j;
  600. /*
  601. * Again, if there are no pulls available at all, we give
  602. * up.
  603. *
  604. * (FIXME: or perhaps backtrack?)
  605. */
  606. if (npulls == 0)
  607. break;
  608. /*
  609. * Now choose which pull to make. On the one hand we should
  610. * prefer pulls which do less damage to the INITIAL squares
  611. * (thus, ones for which we can already get into position
  612. * via existing SPACEs, and for which the barrel already
  613. * exists and doesn't have to be invented); on the other,
  614. * we want to avoid _always_ preferring such pulls, on the
  615. * grounds that that will lead to levels without very much
  616. * stuff in.
  617. *
  618. * When creating new barrels, we prefer creations which are
  619. * next to existing TARGET squares.
  620. *
  621. * FIXME: for the moment I'll make this very simple indeed.
  622. */
  623. i = random_upto(rs, npulls);
  624. /*
  625. * Actually make the pull, including carving a path to get
  626. * to the site if necessary.
  627. */
  628. x = pulls[i].nx;
  629. y = pulls[i].ny;
  630. while (prev[y*w+x] >= 0) {
  631. int p;
  632. if (grid[y*w+x] == INITIAL)
  633. grid[y*w+x] = SPACE;
  634. p = prev[y*w+x];
  635. y = p / w;
  636. x = p % w;
  637. }
  638. px = 2*pulls[i].nx - pulls[i].ox;
  639. py = 2*pulls[i].ny - pulls[i].oy;
  640. if (grid[py*w+px] == INITIAL)
  641. grid[py*w+px] = SPACE;
  642. if (grid[pulls[i].ny*w+pulls[i].nx] == TARGET)
  643. grid[pulls[i].ny*w+pulls[i].nx] = BARRELTARGET;
  644. else
  645. grid[pulls[i].ny*w+pulls[i].nx] = BARREL;
  646. if (grid[pulls[i].oy*w+pulls[i].ox] == BARREL)
  647. grid[pulls[i].oy*w+pulls[i].ox] = SPACE;
  648. else if (grid[pulls[i].oy*w+pulls[i].ox] != DEEP_PIT)
  649. grid[pulls[i].oy*w+pulls[i].ox] = TARGET;
  650. }
  651. sfree(heap);
  652. sfree(prev);
  653. sfree(dist);
  654. sfree(pulls);
  655. if (grid[py*w+px] == TARGET)
  656. grid[py*w+px] = PLAYERTARGET;
  657. else
  658. grid[py*w+px] = PLAYER;
  659. }
  660. static char *new_game_desc(const game_params *params, random_state *rs,
  661. char **aux, bool interactive)
  662. {
  663. int w = params->w, h = params->h;
  664. char *desc;
  665. int desclen, descpos, descsize, prev, count;
  666. unsigned char *grid;
  667. int i, j;
  668. /*
  669. * FIXME: perhaps some more interesting means of choosing how
  670. * many moves to try?
  671. */
  672. grid = snewn(w*h, unsigned char);
  673. sokoban_generate(w, h, grid, w*h, false, rs);
  674. desclen = descpos = descsize = 0;
  675. desc = NULL;
  676. prev = -1;
  677. count = 0;
  678. for (i = 0; i < w*h; i++) {
  679. if (descsize < desclen + 40) {
  680. descsize = desclen + 100;
  681. desc = sresize(desc, descsize, char);
  682. desc[desclen] = '\0';
  683. }
  684. switch (grid[i]) {
  685. case INITIAL:
  686. j = 'w'; /* FIXME: make some of these 's'? */
  687. break;
  688. case SPACE:
  689. j = 's';
  690. break;
  691. case WALL:
  692. j = 'w';
  693. break;
  694. case TARGET:
  695. j = 't';
  696. break;
  697. case BARREL:
  698. j = 'b';
  699. break;
  700. case BARRELTARGET:
  701. j = 'f';
  702. break;
  703. case DEEP_PIT:
  704. j = 'd';
  705. break;
  706. case PLAYER:
  707. j = 'u';
  708. break;
  709. case PLAYERTARGET:
  710. j = 'v';
  711. break;
  712. default:
  713. j = '?';
  714. break;
  715. }
  716. assert(j != '?');
  717. if (j != prev) {
  718. desc[desclen++] = j;
  719. descpos = desclen;
  720. prev = j;
  721. count = 1;
  722. } else {
  723. count++;
  724. desclen = descpos + sprintf(desc+descpos, "%d", count);
  725. }
  726. }
  727. sfree(grid);
  728. return desc;
  729. }
  730. static const char *validate_desc(const game_params *params, const char *desc)
  731. {
  732. int w = params->w, h = params->h;
  733. int area = 0;
  734. int nplayers = 0;
  735. while (*desc) {
  736. int c = *desc++;
  737. int n = 1;
  738. if (*desc && isdigit((unsigned char)*desc)) {
  739. n = atoi(desc);
  740. while (*desc && isdigit((unsigned char)*desc)) desc++;
  741. }
  742. area += n;
  743. if (c == PLAYER || c == PLAYERTARGET)
  744. nplayers += n;
  745. else if (c == INITIAL || c == SPACE || c == WALL || c == TARGET ||
  746. c == PIT || c == DEEP_PIT || IS_BARREL(c))
  747. /* ok */;
  748. else
  749. return "Invalid character in game description";
  750. }
  751. if (area > w*h)
  752. return "Too much data in game description";
  753. if (area < w*h)
  754. return "Too little data in game description";
  755. if (nplayers < 1)
  756. return "No starting player position specified";
  757. if (nplayers > 1)
  758. return "More than one starting player position specified";
  759. return NULL;
  760. }
  761. static game_state *new_game(midend *me, const game_params *params,
  762. const char *desc)
  763. {
  764. int w = params->w, h = params->h;
  765. game_state *state = snew(game_state);
  766. int i;
  767. state->p = *params; /* structure copy */
  768. state->grid = snewn(w*h, unsigned char);
  769. state->px = state->py = -1;
  770. state->completed = false;
  771. i = 0;
  772. while (*desc) {
  773. int c = *desc++;
  774. int n = 1;
  775. if (*desc && isdigit((unsigned char)*desc)) {
  776. n = atoi(desc);
  777. while (*desc && isdigit((unsigned char)*desc)) desc++;
  778. }
  779. if (c == PLAYER || c == PLAYERTARGET) {
  780. state->py = i / w;
  781. state->px = i % w;
  782. c = IS_ON_TARGET(c) ? TARGET : SPACE;
  783. }
  784. while (n-- > 0)
  785. state->grid[i++] = c;
  786. }
  787. assert(i == w*h);
  788. assert(state->px != -1 && state->py != -1);
  789. return state;
  790. }
  791. static game_state *dup_game(const game_state *state)
  792. {
  793. int w = state->p.w, h = state->p.h;
  794. game_state *ret = snew(game_state);
  795. ret->p = state->p; /* structure copy */
  796. ret->grid = snewn(w*h, unsigned char);
  797. memcpy(ret->grid, state->grid, w*h);
  798. ret->px = state->px;
  799. ret->py = state->py;
  800. ret->completed = state->completed;
  801. return ret;
  802. }
  803. static void free_game(game_state *state)
  804. {
  805. sfree(state->grid);
  806. sfree(state);
  807. }
  808. static char *solve_game(const game_state *state, const game_state *currstate,
  809. const char *aux, const char **error)
  810. {
  811. return NULL;
  812. }
  813. static bool game_can_format_as_text_now(const game_params *params)
  814. {
  815. return true;
  816. }
  817. static char *game_text_format(const game_state *state)
  818. {
  819. return NULL;
  820. }
  821. static game_ui *new_ui(const game_state *state)
  822. {
  823. return NULL;
  824. }
  825. static void free_ui(game_ui *ui)
  826. {
  827. }
  828. static void game_changed_state(game_ui *ui, const game_state *oldstate,
  829. const game_state *newstate)
  830. {
  831. }
  832. struct game_drawstate {
  833. game_params p;
  834. int tilesize;
  835. bool started;
  836. unsigned short *grid;
  837. };
  838. #define PREFERRED_TILESIZE 32
  839. #define TILESIZE (ds->tilesize)
  840. #define BORDER (TILESIZE)
  841. #define HIGHLIGHT_WIDTH (TILESIZE / 10)
  842. #define COORD(x) ( (x) * TILESIZE + BORDER )
  843. #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
  844. /*
  845. * I'm going to need to do most of the move-type analysis in both
  846. * interpret_move and execute_move, so I'll abstract it out into a
  847. * subfunction. move_type() returns -1 for an illegal move, 0 for a
  848. * movement, and 1 for a push.
  849. */
  850. static int move_type(const game_state *state, int dx, int dy)
  851. {
  852. int w = state->p.w, h = state->p.h;
  853. int px = state->px, py = state->py;
  854. int nx, ny, nbx, nby;
  855. assert(dx >= -1 && dx <= +1);
  856. assert(dy >= -1 && dy <= +1);
  857. assert(dx || dy);
  858. nx = px + dx;
  859. ny = py + dy;
  860. /*
  861. * Disallow any move that goes off the grid.
  862. */
  863. if (nx < 0 || nx >= w || ny < 0 || ny >= h)
  864. return -1;
  865. /*
  866. * Examine the target square of the move to see whether it's a
  867. * space, a barrel, or a wall.
  868. */
  869. if (state->grid[ny*w+nx] == WALL ||
  870. state->grid[ny*w+nx] == PIT ||
  871. state->grid[ny*w+nx] == DEEP_PIT)
  872. return -1; /* this one's easy; just disallow it */
  873. if (IS_BARREL(state->grid[ny*w+nx])) {
  874. /*
  875. * This is a push move. For a start, that means it must not
  876. * be diagonal.
  877. */
  878. if (dy && dx)
  879. return -1;
  880. /*
  881. * Now find the location of the third square involved in
  882. * the push, and stop if it's off the edge.
  883. */
  884. nbx = nx + dx;
  885. nby = ny + dy;
  886. if (nbx < 0 || nbx >= w || nby < 0 || nby >= h)
  887. return -1;
  888. /*
  889. * That third square must be able to accept a barrel.
  890. */
  891. if (state->grid[nby*w+nbx] == SPACE ||
  892. state->grid[nby*w+nbx] == TARGET ||
  893. state->grid[nby*w+nbx] == PIT ||
  894. state->grid[nby*w+nbx] == DEEP_PIT) {
  895. /*
  896. * The push is valid.
  897. */
  898. return 1;
  899. } else {
  900. return -1;
  901. }
  902. } else {
  903. /*
  904. * This is just an ordinary move. We've already checked the
  905. * target square, so the only thing left to check is that a
  906. * diagonal move has a space on one side to have notionally
  907. * gone through.
  908. */
  909. if (dx && dy &&
  910. state->grid[(py+dy)*w+px] != SPACE &&
  911. state->grid[(py+dy)*w+px] != TARGET &&
  912. state->grid[py*w+(px+dx)] != SPACE &&
  913. state->grid[py*w+(px+dx)] != TARGET)
  914. return -1;
  915. /*
  916. * Otherwise, the move is valid.
  917. */
  918. return 0;
  919. }
  920. }
  921. static char *interpret_move(const game_state *state, game_ui *ui,
  922. const game_drawstate *ds,
  923. int x, int y, int button)
  924. {
  925. int dx=0, dy=0;
  926. char *move;
  927. /*
  928. * Diagonal movement is supported as it is in NetHack: it's
  929. * for movement only (never pushing), and one of the two
  930. * squares adjacent to both the source and destination
  931. * squares must be free to move through. In other words, it
  932. * is only a shorthand for two orthogonal moves and cannot
  933. * change the nature of the actual puzzle game.
  934. */
  935. if (button == CURSOR_UP || button == (MOD_NUM_KEYPAD | '8'))
  936. dx = 0, dy = -1;
  937. else if (button == CURSOR_DOWN || button == (MOD_NUM_KEYPAD | '2'))
  938. dx = 0, dy = +1;
  939. else if (button == CURSOR_LEFT || button == (MOD_NUM_KEYPAD | '4'))
  940. dx = -1, dy = 0;
  941. else if (button == CURSOR_RIGHT || button == (MOD_NUM_KEYPAD | '6'))
  942. dx = +1, dy = 0;
  943. else if (button == (MOD_NUM_KEYPAD | '7'))
  944. dx = -1, dy = -1;
  945. else if (button == (MOD_NUM_KEYPAD | '9'))
  946. dx = +1, dy = -1;
  947. else if (button == (MOD_NUM_KEYPAD | '1'))
  948. dx = -1, dy = +1;
  949. else if (button == (MOD_NUM_KEYPAD | '3'))
  950. dx = +1, dy = +1;
  951. else if (button == LEFT_BUTTON)
  952. {
  953. if(x < COORD(state->px))
  954. dx = -1;
  955. else if (x > COORD(state->px + 1))
  956. dx = 1;
  957. if(y < COORD(state->py))
  958. dy = -1;
  959. else if (y > COORD(state->py + 1))
  960. dy = 1;
  961. }
  962. else
  963. return NULL;
  964. if((dx == 0) && (dy == 0))
  965. return(NULL);
  966. if (move_type(state, dx, dy) < 0)
  967. return NULL;
  968. move = snewn(2, char);
  969. move[1] = '\0';
  970. move[0] = '5' - 3*dy + dx;
  971. return move;
  972. }
  973. static game_state *execute_move(const game_state *state, const char *move)
  974. {
  975. int w = state->p.w, h = state->p.h;
  976. int px = state->px, py = state->py;
  977. int dx, dy, nx, ny, nbx, nby, type, m, i;
  978. bool freebarrels, freetargets;
  979. game_state *ret;
  980. if (*move < '1' || *move == '5' || *move > '9' || move[1])
  981. return NULL; /* invalid move string */
  982. m = *move - '0';
  983. dx = (m + 2) % 3 - 1;
  984. dy = 2 - (m + 2) / 3;
  985. type = move_type(state, dx, dy);
  986. if (type < 0)
  987. return NULL;
  988. ret = dup_game(state);
  989. nx = px + dx;
  990. ny = py + dy;
  991. nbx = nx + dx;
  992. nby = ny + dy;
  993. if (type) {
  994. int b;
  995. /*
  996. * Push.
  997. */
  998. b = ret->grid[ny*w+nx];
  999. if (IS_ON_TARGET(b)) {
  1000. ret->grid[ny*w+nx] = TARGET;
  1001. b = DETARGETISE(b);
  1002. } else
  1003. ret->grid[ny*w+nx] = SPACE;
  1004. if (ret->grid[nby*w+nbx] == PIT)
  1005. ret->grid[nby*w+nbx] = SPACE;
  1006. else if (ret->grid[nby*w+nbx] == DEEP_PIT)
  1007. /* do nothing - the pit eats the barrel and remains there */;
  1008. else if (ret->grid[nby*w+nbx] == TARGET)
  1009. ret->grid[nby*w+nbx] = TARGETISE(b);
  1010. else
  1011. ret->grid[nby*w+nbx] = b;
  1012. }
  1013. ret->px = nx;
  1014. ret->py = ny;
  1015. /*
  1016. * Check for completion. This is surprisingly complicated,
  1017. * given the presence of pits and deep pits, and also the fact
  1018. * that some Sokoban levels with pits have fewer pits than
  1019. * barrels (due to providing spares, e.g. NetHack's). I think
  1020. * the completion condition in fact must be that the game
  1021. * cannot become any _more_ complete. That is, _either_ there
  1022. * are no remaining barrels not on targets, _or_ there is a
  1023. * good reason why any such barrels cannot be placed. The only
  1024. * available good reason is that there are no remaining pits,
  1025. * no free target squares, and no deep pits at all.
  1026. */
  1027. if (!ret->completed) {
  1028. freebarrels = false;
  1029. freetargets = false;
  1030. for (i = 0; i < w*h; i++) {
  1031. int v = ret->grid[i];
  1032. if (IS_BARREL(v) && !IS_ON_TARGET(v))
  1033. freebarrels = true;
  1034. if (v == DEEP_PIT || v == PIT ||
  1035. (!IS_BARREL(v) && IS_ON_TARGET(v)))
  1036. freetargets = true;
  1037. }
  1038. if (!freebarrels || !freetargets)
  1039. ret->completed = true;
  1040. }
  1041. return ret;
  1042. }
  1043. /* ----------------------------------------------------------------------
  1044. * Drawing routines.
  1045. */
  1046. static void game_compute_size(const game_params *params, int tilesize,
  1047. const game_ui *ui, int *x, int *y)
  1048. {
  1049. /* Ick: fake up `ds->tilesize' for macro expansion purposes */
  1050. struct { int tilesize; } ads, *ds = &ads;
  1051. ads.tilesize = tilesize;
  1052. *x = 2 * BORDER + 1 + params->w * TILESIZE;
  1053. *y = 2 * BORDER + 1 + params->h * TILESIZE;
  1054. }
  1055. static void game_set_size(drawing *dr, game_drawstate *ds,
  1056. const game_params *params, int tilesize)
  1057. {
  1058. ds->tilesize = tilesize;
  1059. }
  1060. static float *game_colours(frontend *fe, int *ncolours)
  1061. {
  1062. float *ret = snewn(3 * NCOLOURS, float);
  1063. int i;
  1064. game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
  1065. ret[COL_OUTLINE * 3 + 0] = 0.0F;
  1066. ret[COL_OUTLINE * 3 + 1] = 0.0F;
  1067. ret[COL_OUTLINE * 3 + 2] = 0.0F;
  1068. ret[COL_PLAYER * 3 + 0] = 0.0F;
  1069. ret[COL_PLAYER * 3 + 1] = 1.0F;
  1070. ret[COL_PLAYER * 3 + 2] = 0.0F;
  1071. ret[COL_BARREL * 3 + 0] = 0.6F;
  1072. ret[COL_BARREL * 3 + 1] = 0.3F;
  1073. ret[COL_BARREL * 3 + 2] = 0.0F;
  1074. ret[COL_TARGET * 3 + 0] = ret[COL_LOWLIGHT * 3 + 0];
  1075. ret[COL_TARGET * 3 + 1] = ret[COL_LOWLIGHT * 3 + 1];
  1076. ret[COL_TARGET * 3 + 2] = ret[COL_LOWLIGHT * 3 + 2];
  1077. ret[COL_PIT * 3 + 0] = ret[COL_LOWLIGHT * 3 + 0] / 2;
  1078. ret[COL_PIT * 3 + 1] = ret[COL_LOWLIGHT * 3 + 1] / 2;
  1079. ret[COL_PIT * 3 + 2] = ret[COL_LOWLIGHT * 3 + 2] / 2;
  1080. ret[COL_DEEP_PIT * 3 + 0] = 0.0F;
  1081. ret[COL_DEEP_PIT * 3 + 1] = 0.0F;
  1082. ret[COL_DEEP_PIT * 3 + 2] = 0.0F;
  1083. ret[COL_TEXT * 3 + 0] = 1.0F;
  1084. ret[COL_TEXT * 3 + 1] = 1.0F;
  1085. ret[COL_TEXT * 3 + 2] = 1.0F;
  1086. ret[COL_GRID * 3 + 0] = ret[COL_LOWLIGHT * 3 + 0];
  1087. ret[COL_GRID * 3 + 1] = ret[COL_LOWLIGHT * 3 + 1];
  1088. ret[COL_GRID * 3 + 2] = ret[COL_LOWLIGHT * 3 + 2];
  1089. ret[COL_OUTLINE * 3 + 0] = 0.0F;
  1090. ret[COL_OUTLINE * 3 + 1] = 0.0F;
  1091. ret[COL_OUTLINE * 3 + 2] = 0.0F;
  1092. for (i = 0; i < 3; i++) {
  1093. ret[COL_WALL * 3 + i] = (3 * ret[COL_BACKGROUND * 3 + i] +
  1094. 1 * ret[COL_HIGHLIGHT * 3 + i]) / 4;
  1095. }
  1096. *ncolours = NCOLOURS;
  1097. return ret;
  1098. }
  1099. static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
  1100. {
  1101. int w = state->p.w, h = state->p.h;
  1102. struct game_drawstate *ds = snew(struct game_drawstate);
  1103. int i;
  1104. ds->tilesize = 0;
  1105. ds->p = state->p; /* structure copy */
  1106. ds->grid = snewn(w*h, unsigned short);
  1107. for (i = 0; i < w*h; i++)
  1108. ds->grid[i] = INVALID;
  1109. ds->started = false;
  1110. return ds;
  1111. }
  1112. static void game_free_drawstate(drawing *dr, game_drawstate *ds)
  1113. {
  1114. sfree(ds->grid);
  1115. sfree(ds);
  1116. }
  1117. static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, int v)
  1118. {
  1119. int tx = COORD(x), ty = COORD(y);
  1120. int bg = (v & 0x100 ? COL_HIGHLIGHT : COL_BACKGROUND);
  1121. v &= 0xFF;
  1122. clip(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1);
  1123. draw_rect(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1, bg);
  1124. if (v == WALL) {
  1125. int coords[6];
  1126. coords[0] = tx + TILESIZE;
  1127. coords[1] = ty + TILESIZE;
  1128. coords[2] = tx + TILESIZE;
  1129. coords[3] = ty + 1;
  1130. coords[4] = tx + 1;
  1131. coords[5] = ty + TILESIZE;
  1132. draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
  1133. coords[0] = tx + 1;
  1134. coords[1] = ty + 1;
  1135. draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
  1136. draw_rect(dr, tx + 1 + HIGHLIGHT_WIDTH, ty + 1 + HIGHLIGHT_WIDTH,
  1137. TILESIZE - 2*HIGHLIGHT_WIDTH,
  1138. TILESIZE - 2*HIGHLIGHT_WIDTH, COL_WALL);
  1139. } else if (v == PIT) {
  1140. draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
  1141. TILESIZE*3/7, COL_PIT, COL_OUTLINE);
  1142. } else if (v == DEEP_PIT) {
  1143. draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
  1144. TILESIZE*3/7, COL_DEEP_PIT, COL_OUTLINE);
  1145. } else {
  1146. if (IS_ON_TARGET(v)) {
  1147. draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
  1148. TILESIZE*3/7, COL_TARGET, COL_OUTLINE);
  1149. }
  1150. if (IS_PLAYER(v)) {
  1151. draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
  1152. TILESIZE/3, COL_PLAYER, COL_OUTLINE);
  1153. } else if (IS_BARREL(v)) {
  1154. char str[2];
  1155. draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
  1156. TILESIZE/3, COL_BARREL, COL_OUTLINE);
  1157. str[1] = '\0';
  1158. str[0] = BARREL_LABEL(v);
  1159. if (str[0]) {
  1160. draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2,
  1161. FONT_VARIABLE, TILESIZE/2,
  1162. ALIGN_VCENTRE | ALIGN_HCENTRE, COL_TEXT, str);
  1163. }
  1164. }
  1165. }
  1166. unclip(dr);
  1167. draw_update(dr, tx, ty, TILESIZE, TILESIZE);
  1168. }
  1169. static void game_redraw(drawing *dr, game_drawstate *ds,
  1170. const game_state *oldstate, const game_state *state,
  1171. int dir, const game_ui *ui,
  1172. float animtime, float flashtime)
  1173. {
  1174. int w = state->p.w, h = state->p.h /*, wh = w*h */;
  1175. int x, y;
  1176. int flashtype;
  1177. if (flashtime &&
  1178. !((int)(flashtime * 3 / FLASH_LENGTH) % 2))
  1179. flashtype = 0x100;
  1180. else
  1181. flashtype = 0;
  1182. /*
  1183. * Initialise a fresh drawstate.
  1184. */
  1185. if (!ds->started) {
  1186. /*
  1187. * Draw the grid lines.
  1188. */
  1189. for (y = 0; y <= h; y++)
  1190. draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y),
  1191. COL_LOWLIGHT);
  1192. for (x = 0; x <= w; x++)
  1193. draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h),
  1194. COL_LOWLIGHT);
  1195. ds->started = true;
  1196. }
  1197. /*
  1198. * Draw the grid contents.
  1199. */
  1200. for (y = 0; y < h; y++)
  1201. for (x = 0; x < w; x++) {
  1202. int v = state->grid[y*w+x];
  1203. if (y == state->py && x == state->px) {
  1204. if (v == TARGET)
  1205. v = PLAYERTARGET;
  1206. else {
  1207. assert(v == SPACE);
  1208. v = PLAYER;
  1209. }
  1210. }
  1211. v |= flashtype;
  1212. if (ds->grid[y*w+x] != v) {
  1213. draw_tile(dr, ds, x, y, v);
  1214. ds->grid[y*w+x] = v;
  1215. }
  1216. }
  1217. }
  1218. static float game_anim_length(const game_state *oldstate,
  1219. const game_state *newstate, int dir, game_ui *ui)
  1220. {
  1221. return 0.0F;
  1222. }
  1223. static float game_flash_length(const game_state *oldstate,
  1224. const game_state *newstate, int dir, game_ui *ui)
  1225. {
  1226. if (!oldstate->completed && newstate->completed)
  1227. return FLASH_LENGTH;
  1228. else
  1229. return 0.0F;
  1230. }
  1231. static void game_get_cursor_location(const game_ui *ui,
  1232. const game_drawstate *ds,
  1233. const game_state *state,
  1234. const game_params *params,
  1235. int *x, int *y, int *w, int *h)
  1236. {
  1237. }
  1238. static int game_status(const game_state *state)
  1239. {
  1240. return state->completed ? +1 : 0;
  1241. }
  1242. static bool game_timing_state(const game_state *state, game_ui *ui)
  1243. {
  1244. return true;
  1245. }
  1246. static void game_print_size(const game_params *params, const game_ui *ui,
  1247. float *x, float *y)
  1248. {
  1249. }
  1250. static void game_print(drawing *dr, const game_state *state, const game_ui *ui,
  1251. int tilesize)
  1252. {
  1253. }
  1254. #ifdef COMBINED
  1255. #define thegame sokoban
  1256. #endif
  1257. const struct game thegame = {
  1258. "Sokoban", NULL, NULL,
  1259. default_params,
  1260. game_fetch_preset, NULL,
  1261. decode_params,
  1262. encode_params,
  1263. free_params,
  1264. dup_params,
  1265. true, game_configure, custom_params,
  1266. validate_params,
  1267. new_game_desc,
  1268. validate_desc,
  1269. new_game,
  1270. dup_game,
  1271. free_game,
  1272. false, solve_game,
  1273. false, game_can_format_as_text_now, game_text_format,
  1274. NULL, NULL, /* get_prefs, set_prefs */
  1275. new_ui,
  1276. free_ui,
  1277. NULL, /* encode_ui */
  1278. NULL, /* decode_ui */
  1279. NULL, /* game_request_keys */
  1280. game_changed_state,
  1281. NULL, /* current_key_label */
  1282. interpret_move,
  1283. execute_move,
  1284. PREFERRED_TILESIZE, game_compute_size, game_set_size,
  1285. game_colours,
  1286. game_new_drawstate,
  1287. game_free_drawstate,
  1288. game_redraw,
  1289. game_anim_length,
  1290. game_flash_length,
  1291. game_get_cursor_location,
  1292. game_status,
  1293. false, false, game_print_size, game_print,
  1294. false, /* wants_statusbar */
  1295. false, game_timing_state,
  1296. 0, /* flags */
  1297. };