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.
11 * - I think it would be better to ditch the `prev' array, and
12 * instead make the `dist' array strictly monotonic (by having
13 * each distance be something like I*A+S, where A is the grid
14 * area, I the number of INITIAL squares trampled on, and S the
15 * number of harmless spaces moved through). This would permit
16 * the path-tracing when a pull is actually made to choose
17 * randomly from all the possible shortest routes, which would
18 * be superior in terms of eliminating directional bias.
19 * + So when tracing the path back to the current px,py, we
20 * look at all four adjacent squares, find the minimum
21 * distance, check that it's _strictly smaller_ than that of
22 * the current square, and restrict our choice to precisely
23 * those squares with that minimum distance.
24 * + The other place `prev' is currently used is in the check
25 * for consistency of a pull. We would have to replace the
26 * check for whether prev[ny*w+nx]==oy*w+ox with a check that
27 * made sure there was at least one adjacent square with a
28 * smaller distance which _wasn't_ oy*w+ox. Then when we did
29 * the path-tracing we'd also have to take this special case
32 * - More discriminating choice of pull. (Snigger.)
33 * + favour putting targets in clumps
34 * + try to shoot for a reasonably consistent number of barrels
35 * (adjust willingness to generate a new barrel depending on
36 * how many are already present)
37 * + adjust willingness to break new ground depending on how
38 * much is already broken
40 * - generation time parameters:
41 * + enable NetHack mode (and find a better place for the hole)
42 * + decide how many of the remaining Is should be walls
44 * - at the end of generation, randomly position the starting
45 * player coordinates, probably by (somehow) reusing the same
46 * bfs currently inside the loop.
48 * - possible backtracking?
50 * - IWBNI we could spot completely unreachable bits of level at
51 * the outside, and not bother drawing grid lines for them. The
52 * NH levels currently look a bit weird with grid lines on the
53 * outside of the boundary.
66 * Various subsets of these constants are used during game
67 * generation, game play, game IDs and the game_drawstate.
69 #define INITIAL 'i' /* used only in game generation */
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 */
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.
89 * When resting on target squares, capital-letter barrels are
90 * replaced with their control-character value (A -> ^A).
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 )
102 #define DX(d) (d == 0 ? -1 : d == 2 ? +1 : 0)
103 #define DY(d) (d == 1 ? -1 : d == 3 ? +1 : 0)
105 #define FLASH_LENGTH 0.3F
126 * FIXME: a parameter involving degree of filling in?
137 static game_params *default_params(void)
139 game_params *ret = snew(game_params);
147 static void free_params(game_params *params)
152 static game_params *dup_params(const game_params *params)
154 game_params *ret = snew(game_params);
155 *ret = *params; /* structure copy */
159 static const struct game_params sokoban_presets[] = {
165 static int game_fetch_preset(int i, char **name, game_params **params)
171 if (i < 0 || i >= lenof(sokoban_presets))
174 p = sokoban_presets[i];
175 ret = dup_params(&p);
176 sprintf(namebuf, "%dx%d", ret->w, ret->h);
177 retname = dupstr(namebuf);
184 static void decode_params(game_params *params, char const *string)
186 params->w = params->h = atoi(string);
187 while (*string && isdigit((unsigned char)*string)) string++;
188 if (*string == 'x') {
190 params->h = atoi(string);
194 static char *encode_params(const game_params *params, int full)
198 sprintf(data, "%dx%d", params->w, params->h);
203 static config_item *game_configure(const game_params *params)
208 ret = snewn(3, config_item);
210 ret[0].name = "Width";
211 ret[0].type = C_STRING;
212 sprintf(buf, "%d", params->w);
213 ret[0].u.string.sval = dupstr(buf);
215 ret[1].name = "Height";
216 ret[1].type = C_STRING;
217 sprintf(buf, "%d", params->h);
218 ret[1].u.string.sval = dupstr(buf);
226 static game_params *custom_params(const config_item *cfg)
228 game_params *ret = snew(game_params);
230 ret->w = atoi(cfg[0].u.string.sval);
231 ret->h = atoi(cfg[1].u.string.sval);
236 static const char *validate_params(const game_params *params, int full)
238 if (params->w < 4 || params->h < 4)
239 return "Width and height must both be at least 4";
244 /* ----------------------------------------------------------------------
245 * Game generation mechanism.
247 * To generate a Sokoban level, we begin with a completely blank
248 * grid and make valid inverse moves. Grid squares can be in a
249 * number of states. The states are:
251 * - INITIAL: this square has not as yet been touched by any
252 * inverse move, which essentially means we haven't decided what
255 * - SPACE: this square is a space.
257 * - TARGET: this square is a space which is also the target for a
260 * - BARREL: this square contains a barrel.
262 * - BARRELTARGET: this square contains a barrel _on_ a target.
264 * - WALL: this square is a wall.
266 * - PLAYER: this square contains the player.
268 * - PLAYERTARGET: this square contains the player on a target.
270 * We begin with every square of the in state INITIAL, apart from a
271 * solid ring of WALLs around the edge. We randomly position the
272 * PLAYER somewhere. Thereafter our valid moves are:
274 * - to move the PLAYER in one direction _pulling_ a barrel after
275 * us. For this to work, we must have SPACE or INITIAL in the
276 * direction we're moving, and BARREL or BARRELTARGET in the
277 * direction we're moving away from. We leave SPACE or TARGET
278 * respectively in the vacated square.
280 * - to create a new barrel by transforming an INITIAL square into
283 * - to move the PLAYER freely through SPACE and TARGET squares,
284 * leaving SPACE or TARGET where it started.
286 * - to move the player through INITIAL squares, carving a tunnel
287 * of SPACEs as it goes.
289 * We try to avoid destroying INITIAL squares wherever possible (if
290 * there's a path to where we want to be using only SPACE, then we
291 * should always use that). At the end of generation, every square
292 * still in state INITIAL is one which was not required at any
293 * point during generation, which means we can randomly choose
294 * whether to make it SPACE or WALL.
296 * It's unclear as yet what the right strategy for wall placement
297 * should be. Too few WALLs will yield many alternative solutions
298 * to the puzzle, whereas too many might rule out so many
299 * possibilities that the intended solution becomes obvious.
302 static void sokoban_generate(int w, int h, unsigned char *grid, int moves,
303 int nethack, random_state *rs)
306 int ox, oy, nx, ny, score;
310 int *dist, *prev, *heap;
311 int x, y, px, py, i, j, d, heapsize, npulls;
313 pulls = snewn(w * h * 4, struct pull);
314 dist = snewn(w * h, int);
315 prev = snewn(w * h, int);
316 heap = snewn(w * h, int);
319 * Configure the initial grid.
321 for (y = 0; y < h; y++)
322 for (x = 0; x < w; x++)
323 grid[y*w+x] = (x == 0 || y == 0 || x == w-1 || y == h-1 ?
331 i = random_upto(rs, (w-2) * (h-2));
339 * Now loop around making random inverse Sokoban moves. In this
340 * loop we aim to make one actual barrel-pull per iteration,
341 * plus as many free moves as are necessary to get into
342 * position for that pull.
344 while (moves-- >= 0) {
346 * First enumerate all the viable barrel-pulls we can
347 * possibly make, counting two pulls of the same barrel in
348 * different directions as different. We also include pulls
349 * we can perform by creating a new barrel. Each pull is
350 * marked with the amount of violence it would have to do
354 for (y = 0; y < h; y++)
355 for (x = 0; x < w; x++)
356 for (d = 0; d < 4; d++) {
359 int nx = x + dx, ny = y + dy;
360 int npx = nx + dx, npy = ny + dy;
364 * The candidate move is to put the player at
365 * (nx,ny), and move him to (npx,npy), pulling
366 * a barrel at (x,y) to (nx,ny). So first we
367 * must check that all those squares are within
368 * the boundaries of the grid. For this it is
369 * sufficient to check npx,npy.
371 if (npx < 0 || npx >= w || npy < 0 || npy >= h)
375 * (x,y) must either be a barrel, or a square
376 * which we can convert into a barrel.
378 switch (grid[y*w+x]) {
379 case BARREL: case BARRELTARGET:
384 score += 10 /* new_barrel_score */;
395 * (nx,ny) must either be a space, or a square
396 * which we can convert into a space.
398 switch (grid[ny*w+nx]) {
399 case SPACE: case TARGET:
402 score += 3 /* new_space_score */;
409 * (npx,npy) must also either be a space, or a
410 * square which we can convert into a space.
412 switch (grid[npy*w+npx]) {
413 case SPACE: case TARGET:
416 score += 3 /* new_space_score */;
423 * That's sufficient to tag this as a possible
424 * pull right now. We still don't know if we
425 * can reach the required player position, but
426 * that's a job for the subsequent BFS phase to
429 pulls[npulls].ox = x;
430 pulls[npulls].oy = y;
431 pulls[npulls].nx = nx;
432 pulls[npulls].ny = ny;
433 pulls[npulls].score = score;
434 #ifdef GENERATION_DIAGNOSTICS
435 printf("found potential pull: (%d,%d)-(%d,%d) cost %d\n",
436 pulls[npulls].ox, pulls[npulls].oy,
437 pulls[npulls].nx, pulls[npulls].ny,
438 pulls[npulls].score);
442 #ifdef GENERATION_DIAGNOSTICS
443 printf("found %d potential pulls\n", npulls);
447 * If there are no pulls available at all, we give up.
449 * (FIXME: or perhaps backtrack?)
455 * Now we do a BFS from our current position, to find all
456 * the squares we can get the player into.
458 * This BFS is unusually tricky. We want to give a positive
459 * distance only to squares which we have to carve through
460 * INITIALs to get to, which means we can't just stick
461 * every square we reach on the end of our to-do list.
462 * Instead, we must maintain our list as a proper priority
465 for (i = 0; i < w*h; i++)
466 dist[i] = prev[i] = -1;
472 #define PARENT(n) ( ((n)-1)/2 )
473 #define LCHILD(n) ( 2*(n)+1 )
474 #define RCHILD(n) ( 2*(n)+2 )
475 #define SWAP(i,j) do { int swaptmp = (i); (i) = (j); (j) = swaptmp; } while (0)
477 while (heapsize > 0) {
479 * Pull the smallest element off the heap: it's at
480 * position 0. Move the arbitrary element from the very
481 * end of the heap into position 0.
487 heap[0] = heap[heapsize];
490 * Now repeatedly move that arbitrary element down the
491 * heap by swapping it with the more suitable of its
502 break; /* we've hit bottom */
504 if (rc >= heapsize) {
506 * Special case: there is only one child to
509 if (dist[heap[i]] > dist[heap[lc]])
510 SWAP(heap[i], heap[lc]);
512 /* _Now_ we've hit bottom. */
516 * The common case: there are two children and
517 * we must check them both.
519 if (dist[heap[i]] > dist[heap[lc]] ||
520 dist[heap[i]] > dist[heap[rc]]) {
522 * Pick the more appropriate child to swap with
523 * (i.e. the one which would want to be the
524 * parent if one were above the other - as one
527 if (dist[heap[lc]] > dist[heap[rc]]) {
528 SWAP(heap[i], heap[rc]);
531 SWAP(heap[i], heap[lc]);
535 /* This element is in the right place; we're done. */
542 * OK, that's given us (x,y) for this phase of the
543 * search. Now try all directions from here.
546 for (d = 0; d < 4; d++) {
549 int nx = x + dx, ny = y + dy;
550 if (nx < 0 || nx >= w || ny < 0 || ny >= h)
552 if (grid[ny*w+nx] != SPACE && grid[ny*w+nx] != TARGET &&
553 grid[ny*w+nx] != INITIAL)
555 if (dist[ny*w+nx] == -1) {
556 dist[ny*w+nx] = dist[y*w+x] + (grid[ny*w+nx] == INITIAL);
557 prev[ny*w+nx] = y*w+x;
560 * Now insert ny*w+nx at the end of the heap,
561 * and move it down to its appropriate resting
565 heap[heapsize++] = ny*w+nx;
568 * Swap element n with its parent repeatedly to
569 * preserve the heap property.
575 if (dist[heap[p]] > dist[heap[i]]) {
576 SWAP(heap[p], heap[i]);
590 #ifdef GENERATION_DIAGNOSTICS
591 printf("distance map:\n");
592 for (i = 0; i < h; i++) {
593 for (j = 0; j < w; j++) {
611 * Now we can go back through the `pulls' array, adjusting
612 * the score for each pull depending on how hard it is to
613 * reach its starting point, and also throwing out any
614 * whose starting points are genuinely unreachable even
615 * with the possibility of carving through INITIAL squares.
617 for (i = j = 0; i < npulls; i++) {
618 #ifdef GENERATION_DIAGNOSTICS
619 printf("potential pull (%d,%d)-(%d,%d)",
620 pulls[i].ox, pulls[i].oy,
621 pulls[i].nx, pulls[i].ny);
625 if (dist[y*w+x] < 0) {
626 #ifdef GENERATION_DIAGNOSTICS
627 printf(" unreachable\n");
629 continue; /* this pull isn't feasible at all */
632 * Another nasty special case we have to check is
633 * whether the initial barrel location (ox,oy) is
634 * on the path used to reach the square. This can
635 * occur if that square is in state INITIAL: the
636 * pull is initially considered valid on the basis
637 * that the INITIAL can become BARRELTARGET, and
638 * it's also considered reachable on the basis that
639 * INITIAL can be turned into SPACE, but it can't
642 * Fortunately, if (ox,oy) is on the path at all,
643 * it must be only one space from the end, so this
644 * is easy to spot and rule out.
646 if (prev[y*w+x] == pulls[i].oy*w+pulls[i].ox) {
647 #ifdef GENERATION_DIAGNOSTICS
648 printf(" goes through itself\n");
650 continue; /* this pull isn't feasible at all */
652 pulls[j] = pulls[i]; /* structure copy */
653 pulls[j].score += dist[y*w+x] * 3 /* new_space_score */;
654 #ifdef GENERATION_DIAGNOSTICS
655 printf(" reachable at distance %d (cost now %d)\n",
656 dist[y*w+x], pulls[j].score);
664 * Again, if there are no pulls available at all, we give
667 * (FIXME: or perhaps backtrack?)
673 * Now choose which pull to make. On the one hand we should
674 * prefer pulls which do less damage to the INITIAL squares
675 * (thus, ones for which we can already get into position
676 * via existing SPACEs, and for which the barrel already
677 * exists and doesn't have to be invented); on the other,
678 * we want to avoid _always_ preferring such pulls, on the
679 * grounds that that will lead to levels without very much
682 * When creating new barrels, we prefer creations which are
683 * next to existing TARGET squares.
685 * FIXME: for the moment I'll make this very simple indeed.
687 i = random_upto(rs, npulls);
690 * Actually make the pull, including carving a path to get
691 * to the site if necessary.
695 while (prev[y*w+x] >= 0) {
698 if (grid[y*w+x] == INITIAL)
705 px = 2*pulls[i].nx - pulls[i].ox;
706 py = 2*pulls[i].ny - pulls[i].oy;
707 if (grid[py*w+px] == INITIAL)
708 grid[py*w+px] = SPACE;
709 if (grid[pulls[i].ny*w+pulls[i].nx] == TARGET)
710 grid[pulls[i].ny*w+pulls[i].nx] = BARRELTARGET;
712 grid[pulls[i].ny*w+pulls[i].nx] = BARREL;
713 if (grid[pulls[i].oy*w+pulls[i].ox] == BARREL)
714 grid[pulls[i].oy*w+pulls[i].ox] = SPACE;
715 else if (grid[pulls[i].oy*w+pulls[i].ox] != DEEP_PIT)
716 grid[pulls[i].oy*w+pulls[i].ox] = TARGET;
724 if (grid[py*w+px] == TARGET)
725 grid[py*w+px] = PLAYERTARGET;
727 grid[py*w+px] = PLAYER;
730 static char *new_game_desc(const game_params *params, random_state *rs,
731 char **aux, int interactive)
733 int w = params->w, h = params->h;
735 int desclen, descpos, descsize, prev, count;
740 * FIXME: perhaps some more interesting means of choosing how
743 grid = snewn(w*h, unsigned char);
744 sokoban_generate(w, h, grid, w*h, FALSE, rs);
746 desclen = descpos = descsize = 0;
750 for (i = 0; i < w*h; i++) {
751 if (descsize < desclen + 40) {
752 descsize = desclen + 100;
753 desc = sresize(desc, descsize, char);
754 desc[desclen] = '\0';
758 j = 'w'; /* FIXME: make some of these 's'? */
796 desclen = descpos + sprintf(desc+descpos, "%d", count);
805 static const char *validate_desc(const game_params *params, const char *desc)
807 int w = params->w, h = params->h;
814 if (*desc && isdigit((unsigned char)*desc)) {
816 while (*desc && isdigit((unsigned char)*desc)) desc++;
821 if (c == PLAYER || c == PLAYERTARGET)
823 else if (c == INITIAL || c == SPACE || c == WALL || c == TARGET ||
824 c == PIT || c == DEEP_PIT || IS_BARREL(c))
827 return "Invalid character in game description";
831 return "Too much data in game description";
833 return "Too little data in game description";
835 return "No starting player position specified";
837 return "More than one starting player position specified";
842 static game_state *new_game(midend *me, const game_params *params,
845 int w = params->w, h = params->h;
846 game_state *state = snew(game_state);
849 state->p = *params; /* structure copy */
850 state->grid = snewn(w*h, unsigned char);
851 state->px = state->py = -1;
852 state->completed = FALSE;
859 if (*desc && isdigit((unsigned char)*desc)) {
861 while (*desc && isdigit((unsigned char)*desc)) desc++;
864 if (c == PLAYER || c == PLAYERTARGET) {
867 c = IS_ON_TARGET(c) ? TARGET : SPACE;
871 state->grid[i++] = c;
875 assert(state->px != -1 && state->py != -1);
880 static game_state *dup_game(const game_state *state)
882 int w = state->p.w, h = state->p.h;
883 game_state *ret = snew(game_state);
885 ret->p = state->p; /* structure copy */
886 ret->grid = snewn(w*h, unsigned char);
887 memcpy(ret->grid, state->grid, w*h);
890 ret->completed = state->completed;
895 static void free_game(game_state *state)
901 static char *solve_game(const game_state *state, const game_state *currstate,
902 const char *aux, const char **error)
907 static int game_can_format_as_text_now(const game_params *params)
912 static char *game_text_format(const game_state *state)
917 static game_ui *new_ui(const game_state *state)
922 static void free_ui(game_ui *ui)
926 static char *encode_ui(const game_ui *ui)
931 static void decode_ui(game_ui *ui, const char *encoding)
935 static void game_changed_state(game_ui *ui, const game_state *oldstate,
936 const game_state *newstate)
940 struct game_drawstate {
944 unsigned short *grid;
947 #define PREFERRED_TILESIZE 32
948 #define TILESIZE (ds->tilesize)
949 #define BORDER (TILESIZE)
950 #define HIGHLIGHT_WIDTH (TILESIZE / 10)
951 #define COORD(x) ( (x) * TILESIZE + BORDER )
952 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
955 * I'm going to need to do most of the move-type analysis in both
956 * interpret_move and execute_move, so I'll abstract it out into a
957 * subfunction. move_type() returns -1 for an illegal move, 0 for a
958 * movement, and 1 for a push.
960 int move_type(game_state *state, int dx, int dy)
962 int w = state->p.w, h = state->p.h;
963 int px = state->px, py = state->py;
964 int nx, ny, nbx, nby;
966 assert(dx >= -1 && dx <= +1);
967 assert(dy >= -1 && dy <= +1);
974 * Disallow any move that goes off the grid.
976 if (nx < 0 || nx >= w || ny < 0 || ny >= h)
980 * Examine the target square of the move to see whether it's a
981 * space, a barrel, or a wall.
984 if (state->grid[ny*w+nx] == WALL ||
985 state->grid[ny*w+nx] == PIT ||
986 state->grid[ny*w+nx] == DEEP_PIT)
987 return -1; /* this one's easy; just disallow it */
989 if (IS_BARREL(state->grid[ny*w+nx])) {
991 * This is a push move. For a start, that means it must not
998 * Now find the location of the third square involved in
999 * the push, and stop if it's off the edge.
1003 if (nbx < 0 || nbx >= w || nby < 0 || nby >= h)
1007 * That third square must be able to accept a barrel.
1009 if (state->grid[nby*w+nbx] == SPACE ||
1010 state->grid[nby*w+nbx] == TARGET ||
1011 state->grid[nby*w+nbx] == PIT ||
1012 state->grid[nby*w+nbx] == DEEP_PIT) {
1014 * The push is valid.
1022 * This is just an ordinary move. We've already checked the
1023 * target square, so the only thing left to check is that a
1024 * diagonal move has a space on one side to have notionally
1028 state->grid[(py+dy)*w+px] != SPACE &&
1029 state->grid[(py+dy)*w+px] != TARGET &&
1030 state->grid[py*w+(px+dx)] != SPACE &&
1031 state->grid[py*w+(px+dx)] != TARGET)
1035 * Otherwise, the move is valid.
1041 static char *interpret_move(const game_state *state, game_ui *ui,
1042 const game_drawstate *ds,
1043 int x, int y, int button)
1049 * Diagonal movement is supported as it is in NetHack: it's
1050 * for movement only (never pushing), and one of the two
1051 * squares adjacent to both the source and destination
1052 * squares must be free to move through. In other words, it
1053 * is only a shorthand for two orthogonal moves and cannot
1054 * change the nature of the actual puzzle game.
1056 if (button == CURSOR_UP || button == (MOD_NUM_KEYPAD | '8'))
1058 else if (button == CURSOR_DOWN || button == (MOD_NUM_KEYPAD | '2'))
1060 else if (button == CURSOR_LEFT || button == (MOD_NUM_KEYPAD | '4'))
1062 else if (button == CURSOR_RIGHT || button == (MOD_NUM_KEYPAD | '6'))
1064 else if (button == (MOD_NUM_KEYPAD | '7'))
1066 else if (button == (MOD_NUM_KEYPAD | '9'))
1068 else if (button == (MOD_NUM_KEYPAD | '1'))
1070 else if (button == (MOD_NUM_KEYPAD | '3'))
1072 else if (button == LEFT_BUTTON)
1074 if(x < COORD(state->px))
1076 else if (x > COORD(state->px + 1))
1078 if(y < COORD(state->py))
1080 else if (y > COORD(state->py + 1))
1086 if((dx == 0) && (dy == 0))
1089 if (move_type(state, dx, dy) < 0)
1092 move = snewn(2, char);
1094 move[0] = '5' - 3*dy + dx;
1098 static game_state *execute_move(const game_state *state, const char *move)
1100 int w = state->p.w, h = state->p.h;
1101 int px = state->px, py = state->py;
1102 int dx, dy, nx, ny, nbx, nby, type, m, i, freebarrels, freetargets;
1105 if (*move < '1' || *move == '5' || *move > '9' || move[1])
1106 return NULL; /* invalid move string */
1109 dx = (m + 2) % 3 - 1;
1110 dy = 2 - (m + 2) / 3;
1111 type = move_type(state, dx, dy);
1115 ret = dup_game(state);
1128 b = ret->grid[ny*w+nx];
1129 if (IS_ON_TARGET(b)) {
1130 ret->grid[ny*w+nx] = TARGET;
1133 ret->grid[ny*w+nx] = SPACE;
1135 if (ret->grid[nby*w+nbx] == PIT)
1136 ret->grid[nby*w+nbx] = SPACE;
1137 else if (ret->grid[nby*w+nbx] == DEEP_PIT)
1138 /* do nothing - the pit eats the barrel and remains there */;
1139 else if (ret->grid[nby*w+nbx] == TARGET)
1140 ret->grid[nby*w+nbx] = TARGETISE(b);
1142 ret->grid[nby*w+nbx] = b;
1149 * Check for completion. This is surprisingly complicated,
1150 * given the presence of pits and deep pits, and also the fact
1151 * that some Sokoban levels with pits have fewer pits than
1152 * barrels (due to providing spares, e.g. NetHack's). I think
1153 * the completion condition in fact must be that the game
1154 * cannot become any _more_ complete. That is, _either_ there
1155 * are no remaining barrels not on targets, _or_ there is a
1156 * good reason why any such barrels cannot be placed. The only
1157 * available good reason is that there are no remaining pits,
1158 * no free target squares, and no deep pits at all.
1160 if (!ret->completed) {
1161 freebarrels = FALSE;
1162 freetargets = FALSE;
1163 for (i = 0; i < w*h; i++) {
1164 int v = ret->grid[i];
1166 if (IS_BARREL(v) && !IS_ON_TARGET(v))
1168 if (v == DEEP_PIT || v == PIT ||
1169 (!IS_BARREL(v) && IS_ON_TARGET(v)))
1173 if (!freebarrels || !freetargets)
1174 ret->completed = TRUE;
1180 /* ----------------------------------------------------------------------
1184 static void game_compute_size(const game_params *params, int tilesize,
1187 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
1188 struct { int tilesize; } ads, *ds = &ads;
1189 ads.tilesize = tilesize;
1191 *x = 2 * BORDER + 1 + params->w * TILESIZE;
1192 *y = 2 * BORDER + 1 + params->h * TILESIZE;
1195 static void game_set_size(drawing *dr, game_drawstate *ds,
1196 const game_params *params, int tilesize)
1198 ds->tilesize = tilesize;
1201 static float *game_colours(frontend *fe, int *ncolours)
1203 float *ret = snewn(3 * NCOLOURS, float);
1206 game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT);
1208 ret[COL_OUTLINE * 3 + 0] = 0.0F;
1209 ret[COL_OUTLINE * 3 + 1] = 0.0F;
1210 ret[COL_OUTLINE * 3 + 2] = 0.0F;
1212 ret[COL_PLAYER * 3 + 0] = 0.0F;
1213 ret[COL_PLAYER * 3 + 1] = 1.0F;
1214 ret[COL_PLAYER * 3 + 2] = 0.0F;
1216 ret[COL_BARREL * 3 + 0] = 0.6F;
1217 ret[COL_BARREL * 3 + 1] = 0.3F;
1218 ret[COL_BARREL * 3 + 2] = 0.0F;
1220 ret[COL_TARGET * 3 + 0] = ret[COL_LOWLIGHT * 3 + 0];
1221 ret[COL_TARGET * 3 + 1] = ret[COL_LOWLIGHT * 3 + 1];
1222 ret[COL_TARGET * 3 + 2] = ret[COL_LOWLIGHT * 3 + 2];
1224 ret[COL_PIT * 3 + 0] = ret[COL_LOWLIGHT * 3 + 0] / 2;
1225 ret[COL_PIT * 3 + 1] = ret[COL_LOWLIGHT * 3 + 1] / 2;
1226 ret[COL_PIT * 3 + 2] = ret[COL_LOWLIGHT * 3 + 2] / 2;
1228 ret[COL_DEEP_PIT * 3 + 0] = 0.0F;
1229 ret[COL_DEEP_PIT * 3 + 1] = 0.0F;
1230 ret[COL_DEEP_PIT * 3 + 2] = 0.0F;
1232 ret[COL_TEXT * 3 + 0] = 1.0F;
1233 ret[COL_TEXT * 3 + 1] = 1.0F;
1234 ret[COL_TEXT * 3 + 2] = 1.0F;
1236 ret[COL_GRID * 3 + 0] = ret[COL_LOWLIGHT * 3 + 0];
1237 ret[COL_GRID * 3 + 1] = ret[COL_LOWLIGHT * 3 + 1];
1238 ret[COL_GRID * 3 + 2] = ret[COL_LOWLIGHT * 3 + 2];
1240 ret[COL_OUTLINE * 3 + 0] = 0.0F;
1241 ret[COL_OUTLINE * 3 + 1] = 0.0F;
1242 ret[COL_OUTLINE * 3 + 2] = 0.0F;
1244 for (i = 0; i < 3; i++) {
1245 ret[COL_WALL * 3 + i] = (3 * ret[COL_BACKGROUND * 3 + i] +
1246 1 * ret[COL_HIGHLIGHT * 3 + i]) / 4;
1249 *ncolours = NCOLOURS;
1253 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1255 int w = state->p.w, h = state->p.h;
1256 struct game_drawstate *ds = snew(struct game_drawstate);
1260 ds->p = state->p; /* structure copy */
1261 ds->grid = snewn(w*h, unsigned short);
1262 for (i = 0; i < w*h; i++)
1263 ds->grid[i] = INVALID;
1264 ds->started = FALSE;
1269 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1275 static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, int v)
1277 int tx = COORD(x), ty = COORD(y);
1278 int bg = (v & 0x100 ? COL_HIGHLIGHT : COL_BACKGROUND);
1282 clip(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1);
1283 draw_rect(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1, bg);
1288 coords[0] = tx + TILESIZE;
1289 coords[1] = ty + TILESIZE;
1290 coords[2] = tx + TILESIZE;
1293 coords[5] = ty + TILESIZE;
1294 draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT);
1298 draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT);
1300 draw_rect(dr, tx + 1 + HIGHLIGHT_WIDTH, ty + 1 + HIGHLIGHT_WIDTH,
1301 TILESIZE - 2*HIGHLIGHT_WIDTH,
1302 TILESIZE - 2*HIGHLIGHT_WIDTH, COL_WALL);
1303 } else if (v == PIT) {
1304 draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
1305 TILESIZE*3/7, COL_PIT, COL_OUTLINE);
1306 } else if (v == DEEP_PIT) {
1307 draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
1308 TILESIZE*3/7, COL_DEEP_PIT, COL_OUTLINE);
1310 if (IS_ON_TARGET(v)) {
1311 draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
1312 TILESIZE*3/7, COL_TARGET, COL_OUTLINE);
1315 draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
1316 TILESIZE/3, COL_PLAYER, COL_OUTLINE);
1317 } else if (IS_BARREL(v)) {
1320 draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2,
1321 TILESIZE/3, COL_BARREL, COL_OUTLINE);
1323 str[0] = BARREL_LABEL(v);
1325 draw_text(dr, tx + TILESIZE/2, ty + TILESIZE/2,
1326 FONT_VARIABLE, TILESIZE/2,
1327 ALIGN_VCENTRE | ALIGN_HCENTRE, COL_TEXT, str);
1333 draw_update(dr, tx, ty, TILESIZE, TILESIZE);
1336 static void game_redraw(drawing *dr, game_drawstate *ds,
1337 const game_state *oldstate, const game_state *state,
1338 int dir, const game_ui *ui,
1339 float animtime, float flashtime)
1341 int w = state->p.w, h = state->p.h /*, wh = w*h */;
1346 !((int)(flashtime * 3 / FLASH_LENGTH) % 2))
1352 * Initialise a fresh drawstate.
1358 * Blank out the window initially.
1360 game_compute_size(&ds->p, TILESIZE, &wid, &ht);
1361 draw_rect(dr, 0, 0, wid, ht, COL_BACKGROUND);
1362 draw_update(dr, 0, 0, wid, ht);
1365 * Draw the grid lines.
1367 for (y = 0; y <= h; y++)
1368 draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y),
1370 for (x = 0; x <= w; x++)
1371 draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h),
1378 * Draw the grid contents.
1380 for (y = 0; y < h; y++)
1381 for (x = 0; x < w; x++) {
1382 int v = state->grid[y*w+x];
1383 if (y == state->py && x == state->px) {
1394 if (ds->grid[y*w+x] != v) {
1395 draw_tile(dr, ds, x, y, v);
1396 ds->grid[y*w+x] = v;
1402 static float game_anim_length(const game_state *oldstate,
1403 const game_state *newstate, int dir, game_ui *ui)
1408 static float game_flash_length(const game_state *oldstate,
1409 const game_state *newstate, int dir, game_ui *ui)
1411 if (!oldstate->completed && newstate->completed)
1412 return FLASH_LENGTH;
1417 static int game_status(const game_state *state)
1419 return state->completed ? +1 : 0;
1422 static int game_timing_state(const game_state *state, game_ui *ui)
1427 static void game_print_size(const game_params *params, float *x, float *y)
1431 static void game_print(drawing *dr, const game_state *state, int tilesize)
1436 #define thegame sokoban
1439 const struct game thegame = {
1440 "Sokoban", NULL, NULL,
1442 game_fetch_preset, NULL,
1447 TRUE, game_configure, custom_params,
1455 FALSE, game_can_format_as_text_now, game_text_format,
1463 PREFERRED_TILESIZE, game_compute_size, game_set_size,
1466 game_free_drawstate,
1471 FALSE, FALSE, game_print_size, game_print,
1472 FALSE, /* wants_statusbar */
1473 FALSE, game_timing_state,