| 1 | /* |
| 2 | * inertia.c: Game involving navigating round a grid picking up |
| 3 | * gems. |
| 4 | * |
| 5 | * Game rules and basic generator design by Ben Olmstead. |
| 6 | * This re-implementation was written by Simon Tatham. |
| 7 | */ |
| 8 | |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | #include <assert.h> |
| 13 | #include <ctype.h> |
| 14 | #include <math.h> |
| 15 | |
| 16 | #include "puzzles.h" |
| 17 | |
| 18 | /* Used in the game_state */ |
| 19 | #define BLANK 'b' |
| 20 | #define GEM 'g' |
| 21 | #define MINE 'm' |
| 22 | #define STOP 's' |
| 23 | #define WALL 'w' |
| 24 | |
| 25 | /* Used in the game IDs */ |
| 26 | #define START 'S' |
| 27 | |
| 28 | /* Used in the game generation */ |
| 29 | #define POSSGEM 'G' |
| 30 | |
| 31 | /* Used only in the game_drawstate*/ |
| 32 | #define UNDRAWN '?' |
| 33 | |
| 34 | #define DIRECTIONS 8 |
| 35 | #define DP1 (DIRECTIONS+1) |
| 36 | #define DX(dir) ( (dir) & 3 ? (((dir) & 7) > 4 ? -1 : +1) : 0 ) |
| 37 | #define DY(dir) ( DX((dir)+6) ) |
| 38 | |
| 39 | /* |
| 40 | * Lvalue macro which expects x and y to be in range. |
| 41 | */ |
| 42 | #define LV_AT(w, h, grid, x, y) ( (grid)[(y)*(w)+(x)] ) |
| 43 | |
| 44 | /* |
| 45 | * Rvalue macro which can cope with x and y being out of range. |
| 46 | */ |
| 47 | #define AT(w, h, grid, x, y) ( (x)<0 || (x)>=(w) || (y)<0 || (y)>=(h) ? \ |
| 48 | WALL : LV_AT(w, h, grid, x, y) ) |
| 49 | |
| 50 | enum { |
| 51 | COL_BACKGROUND, |
| 52 | COL_OUTLINE, |
| 53 | COL_HIGHLIGHT, |
| 54 | COL_LOWLIGHT, |
| 55 | COL_PLAYER, |
| 56 | COL_DEAD_PLAYER, |
| 57 | COL_MINE, |
| 58 | COL_GEM, |
| 59 | COL_WALL, |
| 60 | COL_HINT, |
| 61 | NCOLOURS |
| 62 | }; |
| 63 | |
| 64 | struct game_params { |
| 65 | int w, h; |
| 66 | }; |
| 67 | |
| 68 | typedef struct soln { |
| 69 | int refcount; |
| 70 | int len; |
| 71 | unsigned char *list; |
| 72 | } soln; |
| 73 | |
| 74 | struct game_state { |
| 75 | game_params p; |
| 76 | int px, py; |
| 77 | int gems; |
| 78 | char *grid; |
| 79 | int distance_moved; |
| 80 | int dead; |
| 81 | int cheated; |
| 82 | int solnpos; |
| 83 | soln *soln; |
| 84 | }; |
| 85 | |
| 86 | static game_params *default_params(void) |
| 87 | { |
| 88 | game_params *ret = snew(game_params); |
| 89 | |
| 90 | ret->w = 10; |
| 91 | #ifdef PORTRAIT_SCREEN |
| 92 | ret->h = 10; |
| 93 | #else |
| 94 | ret->h = 8; |
| 95 | #endif |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | static void free_params(game_params *params) |
| 100 | { |
| 101 | sfree(params); |
| 102 | } |
| 103 | |
| 104 | static game_params *dup_params(game_params *params) |
| 105 | { |
| 106 | game_params *ret = snew(game_params); |
| 107 | *ret = *params; /* structure copy */ |
| 108 | return ret; |
| 109 | } |
| 110 | |
| 111 | static const struct game_params inertia_presets[] = { |
| 112 | #ifdef PORTRAIT_SCREEN |
| 113 | { 10, 10 }, |
| 114 | { 12, 12 }, |
| 115 | { 16, 16 }, |
| 116 | #else |
| 117 | { 10, 8 }, |
| 118 | { 15, 12 }, |
| 119 | { 20, 16 }, |
| 120 | #endif |
| 121 | }; |
| 122 | |
| 123 | static int game_fetch_preset(int i, char **name, game_params **params) |
| 124 | { |
| 125 | game_params p, *ret; |
| 126 | char *retname; |
| 127 | char namebuf[80]; |
| 128 | |
| 129 | if (i < 0 || i >= lenof(inertia_presets)) |
| 130 | return FALSE; |
| 131 | |
| 132 | p = inertia_presets[i]; |
| 133 | ret = dup_params(&p); |
| 134 | sprintf(namebuf, "%dx%d", ret->w, ret->h); |
| 135 | retname = dupstr(namebuf); |
| 136 | |
| 137 | *params = ret; |
| 138 | *name = retname; |
| 139 | return TRUE; |
| 140 | } |
| 141 | |
| 142 | static void decode_params(game_params *params, char const *string) |
| 143 | { |
| 144 | params->w = params->h = atoi(string); |
| 145 | while (*string && isdigit((unsigned char)*string)) string++; |
| 146 | if (*string == 'x') { |
| 147 | string++; |
| 148 | params->h = atoi(string); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | static char *encode_params(game_params *params, int full) |
| 153 | { |
| 154 | char data[256]; |
| 155 | |
| 156 | sprintf(data, "%dx%d", params->w, params->h); |
| 157 | |
| 158 | return dupstr(data); |
| 159 | } |
| 160 | |
| 161 | static config_item *game_configure(game_params *params) |
| 162 | { |
| 163 | config_item *ret; |
| 164 | char buf[80]; |
| 165 | |
| 166 | ret = snewn(3, config_item); |
| 167 | |
| 168 | ret[0].name = "Width"; |
| 169 | ret[0].type = C_STRING; |
| 170 | sprintf(buf, "%d", params->w); |
| 171 | ret[0].sval = dupstr(buf); |
| 172 | ret[0].ival = 0; |
| 173 | |
| 174 | ret[1].name = "Height"; |
| 175 | ret[1].type = C_STRING; |
| 176 | sprintf(buf, "%d", params->h); |
| 177 | ret[1].sval = dupstr(buf); |
| 178 | ret[1].ival = 0; |
| 179 | |
| 180 | ret[2].name = NULL; |
| 181 | ret[2].type = C_END; |
| 182 | ret[2].sval = NULL; |
| 183 | ret[2].ival = 0; |
| 184 | |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | static game_params *custom_params(config_item *cfg) |
| 189 | { |
| 190 | game_params *ret = snew(game_params); |
| 191 | |
| 192 | ret->w = atoi(cfg[0].sval); |
| 193 | ret->h = atoi(cfg[1].sval); |
| 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | static char *validate_params(game_params *params, int full) |
| 199 | { |
| 200 | /* |
| 201 | * Avoid completely degenerate cases which only have one |
| 202 | * row/column. We probably could generate completable puzzles |
| 203 | * of that shape, but they'd be forced to be extremely boring |
| 204 | * and at large sizes would take a while to happen upon at |
| 205 | * random as well. |
| 206 | */ |
| 207 | if (params->w < 2 || params->h < 2) |
| 208 | return "Width and height must both be at least two"; |
| 209 | |
| 210 | /* |
| 211 | * The grid construction algorithm creates 1/5 as many gems as |
| 212 | * grid squares, and must create at least one gem to have an |
| 213 | * actual puzzle. However, an area-five grid is ruled out by |
| 214 | * the above constraint, so the practical minimum is six. |
| 215 | */ |
| 216 | if (params->w * params->h < 6) |
| 217 | return "Grid area must be at least six squares"; |
| 218 | |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | /* ---------------------------------------------------------------------- |
| 223 | * Solver used by grid generator. |
| 224 | */ |
| 225 | |
| 226 | struct solver_scratch { |
| 227 | unsigned char *reachable_from, *reachable_to; |
| 228 | int *positions; |
| 229 | }; |
| 230 | |
| 231 | static struct solver_scratch *new_scratch(int w, int h) |
| 232 | { |
| 233 | struct solver_scratch *sc = snew(struct solver_scratch); |
| 234 | |
| 235 | sc->reachable_from = snewn(w * h * DIRECTIONS, unsigned char); |
| 236 | sc->reachable_to = snewn(w * h * DIRECTIONS, unsigned char); |
| 237 | sc->positions = snewn(w * h * DIRECTIONS, int); |
| 238 | |
| 239 | return sc; |
| 240 | } |
| 241 | |
| 242 | static void free_scratch(struct solver_scratch *sc) |
| 243 | { |
| 244 | sfree(sc->reachable_from); |
| 245 | sfree(sc->reachable_to); |
| 246 | sfree(sc->positions); |
| 247 | sfree(sc); |
| 248 | } |
| 249 | |
| 250 | static int can_go(int w, int h, char *grid, |
| 251 | int x1, int y1, int dir1, int x2, int y2, int dir2) |
| 252 | { |
| 253 | /* |
| 254 | * Returns TRUE if we can transition directly from (x1,y1) |
| 255 | * going in direction dir1, to (x2,y2) going in direction dir2. |
| 256 | */ |
| 257 | |
| 258 | /* |
| 259 | * If we're actually in the middle of an unoccupyable square, |
| 260 | * we cannot make any move. |
| 261 | */ |
| 262 | if (AT(w, h, grid, x1, y1) == WALL || |
| 263 | AT(w, h, grid, x1, y1) == MINE) |
| 264 | return FALSE; |
| 265 | |
| 266 | /* |
| 267 | * If a move is capable of stopping at x1,y1,dir1, and x2,y2 is |
| 268 | * the same coordinate as x1,y1, then we can make the |
| 269 | * transition (by stopping and changing direction). |
| 270 | * |
| 271 | * For this to be the case, we have to either have a wall |
| 272 | * beyond x1,y1,dir1, or have a stop on x1,y1. |
| 273 | */ |
| 274 | if (x2 == x1 && y2 == y1 && |
| 275 | (AT(w, h, grid, x1, y1) == STOP || |
| 276 | AT(w, h, grid, x1, y1) == START || |
| 277 | AT(w, h, grid, x1+DX(dir1), y1+DY(dir1)) == WALL)) |
| 278 | return TRUE; |
| 279 | |
| 280 | /* |
| 281 | * If a move is capable of continuing here, then x1,y1,dir1 can |
| 282 | * move one space further on. |
| 283 | */ |
| 284 | if (x2 == x1+DX(dir1) && y2 == y1+DY(dir1) && dir1 == dir2 && |
| 285 | (AT(w, h, grid, x2, y2) == BLANK || |
| 286 | AT(w, h, grid, x2, y2) == GEM || |
| 287 | AT(w, h, grid, x2, y2) == STOP || |
| 288 | AT(w, h, grid, x2, y2) == START)) |
| 289 | return TRUE; |
| 290 | |
| 291 | /* |
| 292 | * That's it. |
| 293 | */ |
| 294 | return FALSE; |
| 295 | } |
| 296 | |
| 297 | static int find_gem_candidates(int w, int h, char *grid, |
| 298 | struct solver_scratch *sc) |
| 299 | { |
| 300 | int wh = w*h; |
| 301 | int head, tail; |
| 302 | int sx, sy, gx, gy, gd, pass, possgems; |
| 303 | |
| 304 | /* |
| 305 | * This function finds all the candidate gem squares, which are |
| 306 | * precisely those squares which can be picked up on a loop |
| 307 | * from the starting point back to the starting point. Doing |
| 308 | * this may involve passing through such a square in the middle |
| 309 | * of a move; so simple breadth-first search over the _squares_ |
| 310 | * of the grid isn't quite adequate, because it might be that |
| 311 | * we can only reach a gem from the start by moving over it in |
| 312 | * one direction, but can only return to the start if we were |
| 313 | * moving over it in another direction. |
| 314 | * |
| 315 | * Instead, we BFS over a space which mentions each grid square |
| 316 | * eight times - once for each direction. We also BFS twice: |
| 317 | * once to find out what square+direction pairs we can reach |
| 318 | * _from_ the start point, and once to find out what pairs we |
| 319 | * can reach the start point from. Then a square is reachable |
| 320 | * if any of the eight directions for that square has both |
| 321 | * flags set. |
| 322 | */ |
| 323 | |
| 324 | memset(sc->reachable_from, 0, wh * DIRECTIONS); |
| 325 | memset(sc->reachable_to, 0, wh * DIRECTIONS); |
| 326 | |
| 327 | /* |
| 328 | * Find the starting square. |
| 329 | */ |
| 330 | sx = -1; /* placate optimiser */ |
| 331 | for (sy = 0; sy < h; sy++) { |
| 332 | for (sx = 0; sx < w; sx++) |
| 333 | if (AT(w, h, grid, sx, sy) == START) |
| 334 | break; |
| 335 | if (sx < w) |
| 336 | break; |
| 337 | } |
| 338 | assert(sy < h); |
| 339 | |
| 340 | for (pass = 0; pass < 2; pass++) { |
| 341 | unsigned char *reachable = (pass == 0 ? sc->reachable_from : |
| 342 | sc->reachable_to); |
| 343 | int sign = (pass == 0 ? +1 : -1); |
| 344 | int dir; |
| 345 | |
| 346 | #ifdef SOLVER_DIAGNOSTICS |
| 347 | printf("starting pass %d\n", pass); |
| 348 | #endif |
| 349 | |
| 350 | /* |
| 351 | * `head' and `tail' are indices within sc->positions which |
| 352 | * track the list of board positions left to process. |
| 353 | */ |
| 354 | head = tail = 0; |
| 355 | for (dir = 0; dir < DIRECTIONS; dir++) { |
| 356 | int index = (sy*w+sx)*DIRECTIONS+dir; |
| 357 | sc->positions[tail++] = index; |
| 358 | reachable[index] = TRUE; |
| 359 | #ifdef SOLVER_DIAGNOSTICS |
| 360 | printf("starting point %d,%d,%d\n", sx, sy, dir); |
| 361 | #endif |
| 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Now repeatedly pick an element off the list and process |
| 366 | * it. |
| 367 | */ |
| 368 | while (head < tail) { |
| 369 | int index = sc->positions[head++]; |
| 370 | int dir = index % DIRECTIONS; |
| 371 | int x = (index / DIRECTIONS) % w; |
| 372 | int y = index / (w * DIRECTIONS); |
| 373 | int n, x2, y2, d2, i2; |
| 374 | |
| 375 | #ifdef SOLVER_DIAGNOSTICS |
| 376 | printf("processing point %d,%d,%d\n", x, y, dir); |
| 377 | #endif |
| 378 | /* |
| 379 | * The places we attempt to switch to here are: |
| 380 | * - each possible direction change (all the other |
| 381 | * directions in this square) |
| 382 | * - one step further in the direction we're going (or |
| 383 | * one step back, if we're in the reachable_to pass). |
| 384 | */ |
| 385 | for (n = -1; n < DIRECTIONS; n++) { |
| 386 | if (n < 0) { |
| 387 | x2 = x + sign * DX(dir); |
| 388 | y2 = y + sign * DY(dir); |
| 389 | d2 = dir; |
| 390 | } else { |
| 391 | x2 = x; |
| 392 | y2 = y; |
| 393 | d2 = n; |
| 394 | } |
| 395 | i2 = (y2*w+x2)*DIRECTIONS+d2; |
| 396 | if (x2 >= 0 && x2 < w && |
| 397 | y2 >= 0 && y2 < h && |
| 398 | !reachable[i2]) { |
| 399 | int ok; |
| 400 | #ifdef SOLVER_DIAGNOSTICS |
| 401 | printf(" trying point %d,%d,%d", x2, y2, d2); |
| 402 | #endif |
| 403 | if (pass == 0) |
| 404 | ok = can_go(w, h, grid, x, y, dir, x2, y2, d2); |
| 405 | else |
| 406 | ok = can_go(w, h, grid, x2, y2, d2, x, y, dir); |
| 407 | #ifdef SOLVER_DIAGNOSTICS |
| 408 | printf(" - %sok\n", ok ? "" : "not "); |
| 409 | #endif |
| 410 | if (ok) { |
| 411 | sc->positions[tail++] = i2; |
| 412 | reachable[i2] = TRUE; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * And that should be it. Now all we have to do is find the |
| 421 | * squares for which there exists _some_ direction such that |
| 422 | * the square plus that direction form a tuple which is both |
| 423 | * reachable from the start and reachable to the start. |
| 424 | */ |
| 425 | possgems = 0; |
| 426 | for (gy = 0; gy < h; gy++) |
| 427 | for (gx = 0; gx < w; gx++) |
| 428 | if (AT(w, h, grid, gx, gy) == BLANK) { |
| 429 | for (gd = 0; gd < DIRECTIONS; gd++) { |
| 430 | int index = (gy*w+gx)*DIRECTIONS+gd; |
| 431 | if (sc->reachable_from[index] && sc->reachable_to[index]) { |
| 432 | #ifdef SOLVER_DIAGNOSTICS |
| 433 | printf("space at %d,%d is reachable via" |
| 434 | " direction %d\n", gx, gy, gd); |
| 435 | #endif |
| 436 | LV_AT(w, h, grid, gx, gy) = POSSGEM; |
| 437 | possgems++; |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | return possgems; |
| 444 | } |
| 445 | |
| 446 | /* ---------------------------------------------------------------------- |
| 447 | * Grid generation code. |
| 448 | */ |
| 449 | |
| 450 | static char *gengrid(int w, int h, random_state *rs) |
| 451 | { |
| 452 | int wh = w*h; |
| 453 | char *grid = snewn(wh+1, char); |
| 454 | struct solver_scratch *sc = new_scratch(w, h); |
| 455 | int maxdist_threshold, tries; |
| 456 | |
| 457 | maxdist_threshold = 2; |
| 458 | tries = 0; |
| 459 | |
| 460 | while (1) { |
| 461 | int i, j; |
| 462 | int possgems; |
| 463 | int *dist, *list, head, tail, maxdist; |
| 464 | |
| 465 | /* |
| 466 | * We're going to fill the grid with the five basic piece |
| 467 | * types in about 1/5 proportion. For the moment, though, |
| 468 | * we leave out the gems, because we'll put those in |
| 469 | * _after_ we run the solver to tell us where the viable |
| 470 | * locations are. |
| 471 | */ |
| 472 | i = 0; |
| 473 | for (j = 0; j < wh/5; j++) |
| 474 | grid[i++] = WALL; |
| 475 | for (j = 0; j < wh/5; j++) |
| 476 | grid[i++] = STOP; |
| 477 | for (j = 0; j < wh/5; j++) |
| 478 | grid[i++] = MINE; |
| 479 | assert(i < wh); |
| 480 | grid[i++] = START; |
| 481 | while (i < wh) |
| 482 | grid[i++] = BLANK; |
| 483 | shuffle(grid, wh, sizeof(*grid), rs); |
| 484 | |
| 485 | /* |
| 486 | * Find the viable gem locations, and immediately give up |
| 487 | * and try again if there aren't enough of them. |
| 488 | */ |
| 489 | possgems = find_gem_candidates(w, h, grid, sc); |
| 490 | if (possgems < wh/5) |
| 491 | continue; |
| 492 | |
| 493 | /* |
| 494 | * We _could_ now select wh/5 of the POSSGEMs and set them |
| 495 | * to GEM, and have a viable level. However, there's a |
| 496 | * chance that a large chunk of the level will turn out to |
| 497 | * be unreachable, so first we test for that. |
| 498 | * |
| 499 | * We do this by finding the largest distance from any |
| 500 | * square to the nearest POSSGEM, by breadth-first search. |
| 501 | * If this is above a critical threshold, we abort and try |
| 502 | * again. |
| 503 | * |
| 504 | * (This search is purely geometric, without regard to |
| 505 | * walls and long ways round.) |
| 506 | */ |
| 507 | dist = sc->positions; |
| 508 | list = sc->positions + wh; |
| 509 | for (i = 0; i < wh; i++) |
| 510 | dist[i] = -1; |
| 511 | head = tail = 0; |
| 512 | for (i = 0; i < wh; i++) |
| 513 | if (grid[i] == POSSGEM) { |
| 514 | dist[i] = 0; |
| 515 | list[tail++] = i; |
| 516 | } |
| 517 | maxdist = 0; |
| 518 | while (head < tail) { |
| 519 | int pos, x, y, d; |
| 520 | |
| 521 | pos = list[head++]; |
| 522 | if (maxdist < dist[pos]) |
| 523 | maxdist = dist[pos]; |
| 524 | |
| 525 | x = pos % w; |
| 526 | y = pos / w; |
| 527 | |
| 528 | for (d = 0; d < DIRECTIONS; d++) { |
| 529 | int x2, y2, p2; |
| 530 | |
| 531 | x2 = x + DX(d); |
| 532 | y2 = y + DY(d); |
| 533 | |
| 534 | if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h) { |
| 535 | p2 = y2*w+x2; |
| 536 | if (dist[p2] < 0) { |
| 537 | dist[p2] = dist[pos] + 1; |
| 538 | list[tail++] = p2; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | assert(head == wh && tail == wh); |
| 544 | |
| 545 | /* |
| 546 | * Now abandon this grid and go round again if maxdist is |
| 547 | * above the required threshold. |
| 548 | * |
| 549 | * We can safely start the threshold as low as 2. As we |
| 550 | * accumulate failed generation attempts, we gradually |
| 551 | * raise it as we get more desperate. |
| 552 | */ |
| 553 | if (maxdist > maxdist_threshold) { |
| 554 | tries++; |
| 555 | if (tries == 50) { |
| 556 | maxdist_threshold++; |
| 557 | tries = 0; |
| 558 | } |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | /* |
| 563 | * Now our reachable squares are plausibly evenly |
| 564 | * distributed over the grid. I'm not actually going to |
| 565 | * _enforce_ that I place the gems in such a way as not to |
| 566 | * increase that maxdist value; I'm now just going to trust |
| 567 | * to the RNG to pick a sensible subset of the POSSGEMs. |
| 568 | */ |
| 569 | j = 0; |
| 570 | for (i = 0; i < wh; i++) |
| 571 | if (grid[i] == POSSGEM) |
| 572 | list[j++] = i; |
| 573 | shuffle(list, j, sizeof(*list), rs); |
| 574 | for (i = 0; i < j; i++) |
| 575 | grid[list[i]] = (i < wh/5 ? GEM : BLANK); |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | free_scratch(sc); |
| 580 | |
| 581 | grid[wh] = '\0'; |
| 582 | |
| 583 | return grid; |
| 584 | } |
| 585 | |
| 586 | static char *new_game_desc(game_params *params, random_state *rs, |
| 587 | char **aux, int interactive) |
| 588 | { |
| 589 | return gengrid(params->w, params->h, rs); |
| 590 | } |
| 591 | |
| 592 | static char *validate_desc(game_params *params, char *desc) |
| 593 | { |
| 594 | int w = params->w, h = params->h, wh = w*h; |
| 595 | int starts = 0, gems = 0, i; |
| 596 | |
| 597 | for (i = 0; i < wh; i++) { |
| 598 | if (!desc[i]) |
| 599 | return "Not enough data to fill grid"; |
| 600 | if (desc[i] != WALL && desc[i] != START && desc[i] != STOP && |
| 601 | desc[i] != GEM && desc[i] != MINE && desc[i] != BLANK) |
| 602 | return "Unrecognised character in game description"; |
| 603 | if (desc[i] == START) |
| 604 | starts++; |
| 605 | if (desc[i] == GEM) |
| 606 | gems++; |
| 607 | } |
| 608 | if (desc[i]) |
| 609 | return "Too much data to fill grid"; |
| 610 | if (starts < 1) |
| 611 | return "No starting square specified"; |
| 612 | if (starts > 1) |
| 613 | return "More than one starting square specified"; |
| 614 | if (gems < 1) |
| 615 | return "No gems specified"; |
| 616 | |
| 617 | return NULL; |
| 618 | } |
| 619 | |
| 620 | static game_state *new_game(midend *me, game_params *params, char *desc) |
| 621 | { |
| 622 | int w = params->w, h = params->h, wh = w*h; |
| 623 | int i; |
| 624 | game_state *state = snew(game_state); |
| 625 | |
| 626 | state->p = *params; /* structure copy */ |
| 627 | |
| 628 | state->grid = snewn(wh, char); |
| 629 | assert(strlen(desc) == wh); |
| 630 | memcpy(state->grid, desc, wh); |
| 631 | |
| 632 | state->px = state->py = -1; |
| 633 | state->gems = 0; |
| 634 | for (i = 0; i < wh; i++) { |
| 635 | if (state->grid[i] == START) { |
| 636 | state->grid[i] = STOP; |
| 637 | state->px = i % w; |
| 638 | state->py = i / w; |
| 639 | } else if (state->grid[i] == GEM) { |
| 640 | state->gems++; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | assert(state->gems > 0); |
| 645 | assert(state->px >= 0 && state->py >= 0); |
| 646 | |
| 647 | state->distance_moved = 0; |
| 648 | state->dead = FALSE; |
| 649 | |
| 650 | state->cheated = FALSE; |
| 651 | state->solnpos = 0; |
| 652 | state->soln = NULL; |
| 653 | |
| 654 | return state; |
| 655 | } |
| 656 | |
| 657 | static game_state *dup_game(game_state *state) |
| 658 | { |
| 659 | int w = state->p.w, h = state->p.h, wh = w*h; |
| 660 | game_state *ret = snew(game_state); |
| 661 | |
| 662 | ret->p = state->p; |
| 663 | ret->px = state->px; |
| 664 | ret->py = state->py; |
| 665 | ret->gems = state->gems; |
| 666 | ret->grid = snewn(wh, char); |
| 667 | ret->distance_moved = state->distance_moved; |
| 668 | ret->dead = FALSE; |
| 669 | memcpy(ret->grid, state->grid, wh); |
| 670 | ret->cheated = state->cheated; |
| 671 | ret->soln = state->soln; |
| 672 | if (ret->soln) |
| 673 | ret->soln->refcount++; |
| 674 | ret->solnpos = state->solnpos; |
| 675 | |
| 676 | return ret; |
| 677 | } |
| 678 | |
| 679 | static void free_game(game_state *state) |
| 680 | { |
| 681 | if (state->soln && --state->soln->refcount == 0) { |
| 682 | sfree(state->soln->list); |
| 683 | sfree(state->soln); |
| 684 | } |
| 685 | sfree(state->grid); |
| 686 | sfree(state); |
| 687 | } |
| 688 | |
| 689 | /* |
| 690 | * Internal function used by solver. |
| 691 | */ |
| 692 | static int move_goes_to(int w, int h, char *grid, int x, int y, int d) |
| 693 | { |
| 694 | int dr; |
| 695 | |
| 696 | /* |
| 697 | * See where we'd get to if we made this move. |
| 698 | */ |
| 699 | dr = -1; /* placate optimiser */ |
| 700 | while (1) { |
| 701 | if (AT(w, h, grid, x+DX(d), y+DY(d)) == WALL) { |
| 702 | dr = DIRECTIONS; /* hit a wall, so end up stationary */ |
| 703 | break; |
| 704 | } |
| 705 | x += DX(d); |
| 706 | y += DY(d); |
| 707 | if (AT(w, h, grid, x, y) == STOP) { |
| 708 | dr = DIRECTIONS; /* hit a stop, so end up stationary */ |
| 709 | break; |
| 710 | } |
| 711 | if (AT(w, h, grid, x, y) == GEM) { |
| 712 | dr = d; /* hit a gem, so we're still moving */ |
| 713 | break; |
| 714 | } |
| 715 | if (AT(w, h, grid, x, y) == MINE) |
| 716 | return -1; /* hit a mine, so move is invalid */ |
| 717 | } |
| 718 | assert(dr >= 0); |
| 719 | return (y*w+x)*DP1+dr; |
| 720 | } |
| 721 | |
| 722 | static int compare_integers(const void *av, const void *bv) |
| 723 | { |
| 724 | const int *a = (const int *)av; |
| 725 | const int *b = (const int *)bv; |
| 726 | if (*a < *b) |
| 727 | return -1; |
| 728 | else if (*a > *b) |
| 729 | return +1; |
| 730 | else |
| 731 | return 0; |
| 732 | } |
| 733 | |
| 734 | static char *solve_game(game_state *state, game_state *currstate, |
| 735 | char *aux, char **error) |
| 736 | { |
| 737 | int w = state->p.w, h = state->p.h, wh = w*h; |
| 738 | int *nodes, *nodeindex, *edges, *backedges, *edgei, *backedgei, *circuit; |
| 739 | int nedges; |
| 740 | int *dist, *dist2, *list; |
| 741 | int *unvisited; |
| 742 | int circuitlen, circuitsize; |
| 743 | int head, tail, pass, i, j, n, x, y, d, dd; |
| 744 | char *err, *soln, *p; |
| 745 | |
| 746 | /* |
| 747 | * Before anything else, deal with the special case in which |
| 748 | * all the gems are already collected. |
| 749 | */ |
| 750 | for (i = 0; i < wh; i++) |
| 751 | if (currstate->grid[i] == GEM) |
| 752 | break; |
| 753 | if (i == wh) { |
| 754 | *error = "Game is already solved"; |
| 755 | return NULL; |
| 756 | } |
| 757 | |
| 758 | /* |
| 759 | * Solving Inertia is a question of first building up the graph |
| 760 | * of where you can get to from where, and secondly finding a |
| 761 | * tour of the graph which takes in every gem. |
| 762 | * |
| 763 | * This is of course a close cousin of the travelling salesman |
| 764 | * problem, which is NP-complete; so I rather doubt that any |
| 765 | * _optimal_ tour can be found in plausible time. Hence I'll |
| 766 | * restrict myself to merely finding a not-too-bad one. |
| 767 | * |
| 768 | * First construct the graph, by bfsing out move by move from |
| 769 | * the current player position. Graph vertices will be |
| 770 | * - every endpoint of a move (place the ball can be |
| 771 | * stationary) |
| 772 | * - every gem (place the ball can go through in motion). |
| 773 | * Vertices of this type have an associated direction, since |
| 774 | * if a gem can be collected by sliding through it in two |
| 775 | * different directions it doesn't follow that you can |
| 776 | * change direction at it. |
| 777 | * |
| 778 | * I'm going to refer to a non-directional vertex as |
| 779 | * (y*w+x)*DP1+DIRECTIONS, and a directional one as |
| 780 | * (y*w+x)*DP1+d. |
| 781 | */ |
| 782 | |
| 783 | /* |
| 784 | * nodeindex[] maps node codes as shown above to numeric |
| 785 | * indices in the nodes[] array. |
| 786 | */ |
| 787 | nodeindex = snewn(DP1*wh, int); |
| 788 | for (i = 0; i < DP1*wh; i++) |
| 789 | nodeindex[i] = -1; |
| 790 | |
| 791 | /* |
| 792 | * Do the bfs to find all the interesting graph nodes. |
| 793 | */ |
| 794 | nodes = snewn(DP1*wh, int); |
| 795 | head = tail = 0; |
| 796 | |
| 797 | nodes[tail] = (currstate->py * w + currstate->px) * DP1 + DIRECTIONS; |
| 798 | nodeindex[nodes[0]] = tail; |
| 799 | tail++; |
| 800 | |
| 801 | while (head < tail) { |
| 802 | int nc = nodes[head++], nnc; |
| 803 | |
| 804 | d = nc % DP1; |
| 805 | |
| 806 | /* |
| 807 | * Plot all possible moves from this node. If the node is |
| 808 | * directed, there's only one. |
| 809 | */ |
| 810 | for (dd = 0; dd < DIRECTIONS; dd++) { |
| 811 | x = nc / DP1; |
| 812 | y = x / w; |
| 813 | x %= w; |
| 814 | |
| 815 | if (d < DIRECTIONS && d != dd) |
| 816 | continue; |
| 817 | |
| 818 | nnc = move_goes_to(w, h, currstate->grid, x, y, dd); |
| 819 | if (nnc >= 0 && nnc != nc) { |
| 820 | if (nodeindex[nnc] < 0) { |
| 821 | nodes[tail] = nnc; |
| 822 | nodeindex[nnc] = tail; |
| 823 | tail++; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | n = head; |
| 829 | |
| 830 | /* |
| 831 | * Now we know how many nodes we have, allocate the edge array |
| 832 | * and go through setting up the edges. |
| 833 | */ |
| 834 | edges = snewn(DIRECTIONS*n, int); |
| 835 | edgei = snewn(n+1, int); |
| 836 | nedges = 0; |
| 837 | |
| 838 | for (i = 0; i < n; i++) { |
| 839 | int nc = nodes[i]; |
| 840 | |
| 841 | edgei[i] = nedges; |
| 842 | |
| 843 | d = nc % DP1; |
| 844 | x = nc / DP1; |
| 845 | y = x / w; |
| 846 | x %= w; |
| 847 | |
| 848 | for (dd = 0; dd < DIRECTIONS; dd++) { |
| 849 | int nnc; |
| 850 | |
| 851 | if (d >= DIRECTIONS || d == dd) { |
| 852 | nnc = move_goes_to(w, h, currstate->grid, x, y, dd); |
| 853 | |
| 854 | if (nnc >= 0 && nnc != nc) |
| 855 | edges[nedges++] = nodeindex[nnc]; |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | edgei[n] = nedges; |
| 860 | |
| 861 | /* |
| 862 | * Now set up the backedges array. |
| 863 | */ |
| 864 | backedges = snewn(nedges, int); |
| 865 | backedgei = snewn(n+1, int); |
| 866 | for (i = j = 0; i < nedges; i++) { |
| 867 | while (j+1 < n && i >= edgei[j+1]) |
| 868 | j++; |
| 869 | backedges[i] = edges[i] * n + j; |
| 870 | } |
| 871 | qsort(backedges, nedges, sizeof(int), compare_integers); |
| 872 | backedgei[0] = 0; |
| 873 | for (i = j = 0; i < nedges; i++) { |
| 874 | int k = backedges[i] / n; |
| 875 | backedges[i] %= n; |
| 876 | while (j < k) |
| 877 | backedgei[++j] = i; |
| 878 | } |
| 879 | backedgei[n] = nedges; |
| 880 | |
| 881 | /* |
| 882 | * Set up the initial tour. At all times, our tour is a circuit |
| 883 | * of graph vertices (which may, and probably will often, |
| 884 | * repeat vertices). To begin with, it's got exactly one vertex |
| 885 | * in it, which is the player's current starting point. |
| 886 | */ |
| 887 | circuitsize = 256; |
| 888 | circuit = snewn(circuitsize, int); |
| 889 | circuitlen = 0; |
| 890 | circuit[circuitlen++] = 0; /* node index 0 is the starting posn */ |
| 891 | |
| 892 | /* |
| 893 | * Track which gems are as yet unvisited. |
| 894 | */ |
| 895 | unvisited = snewn(wh, int); |
| 896 | for (i = 0; i < wh; i++) |
| 897 | unvisited[i] = FALSE; |
| 898 | for (i = 0; i < wh; i++) |
| 899 | if (currstate->grid[i] == GEM) |
| 900 | unvisited[i] = TRUE; |
| 901 | |
| 902 | /* |
| 903 | * Allocate space for doing bfses inside the main loop. |
| 904 | */ |
| 905 | dist = snewn(n, int); |
| 906 | dist2 = snewn(n, int); |
| 907 | list = snewn(n, int); |
| 908 | |
| 909 | err = NULL; |
| 910 | soln = NULL; |
| 911 | |
| 912 | /* |
| 913 | * Now enter the main loop, in each iteration of which we |
| 914 | * extend the tour to take in an as yet uncollected gem. |
| 915 | */ |
| 916 | while (1) { |
| 917 | int target, n1, n2, bestdist, extralen, targetpos; |
| 918 | |
| 919 | #ifdef TSP_DIAGNOSTICS |
| 920 | printf("circuit is"); |
| 921 | for (i = 0; i < circuitlen; i++) { |
| 922 | int nc = nodes[circuit[i]]; |
| 923 | printf(" (%d,%d,%d)", nc/DP1%w, nc/(DP1*w), nc%DP1); |
| 924 | } |
| 925 | printf("\n"); |
| 926 | printf("moves are "); |
| 927 | x = nodes[circuit[0]] / DP1 % w; |
| 928 | y = nodes[circuit[0]] / DP1 / w; |
| 929 | for (i = 1; i < circuitlen; i++) { |
| 930 | int x2, y2, dx, dy; |
| 931 | if (nodes[circuit[i]] % DP1 != DIRECTIONS) |
| 932 | continue; |
| 933 | x2 = nodes[circuit[i]] / DP1 % w; |
| 934 | y2 = nodes[circuit[i]] / DP1 / w; |
| 935 | dx = (x2 > x ? +1 : x2 < x ? -1 : 0); |
| 936 | dy = (y2 > y ? +1 : y2 < y ? -1 : 0); |
| 937 | for (d = 0; d < DIRECTIONS; d++) |
| 938 | if (DX(d) == dx && DY(d) == dy) |
| 939 | printf("%c", "89632147"[d]); |
| 940 | x = x2; |
| 941 | y = y2; |
| 942 | } |
| 943 | printf("\n"); |
| 944 | #endif |
| 945 | |
| 946 | /* |
| 947 | * First, start a pair of bfses at _every_ vertex currently |
| 948 | * in the tour, and extend them outwards to find the |
| 949 | * nearest as yet unreached gem vertex. |
| 950 | * |
| 951 | * This is largely a heuristic: we could pick _any_ doubly |
| 952 | * reachable node here and still get a valid tour as |
| 953 | * output. I hope that picking a nearby one will result in |
| 954 | * generally good tours. |
| 955 | */ |
| 956 | for (pass = 0; pass < 2; pass++) { |
| 957 | int *ep = (pass == 0 ? edges : backedges); |
| 958 | int *ei = (pass == 0 ? edgei : backedgei); |
| 959 | int *dp = (pass == 0 ? dist : dist2); |
| 960 | head = tail = 0; |
| 961 | for (i = 0; i < n; i++) |
| 962 | dp[i] = -1; |
| 963 | for (i = 0; i < circuitlen; i++) { |
| 964 | int ni = circuit[i]; |
| 965 | if (dp[ni] < 0) { |
| 966 | dp[ni] = 0; |
| 967 | list[tail++] = ni; |
| 968 | } |
| 969 | } |
| 970 | while (head < tail) { |
| 971 | int ni = list[head++]; |
| 972 | for (i = ei[ni]; i < ei[ni+1]; i++) { |
| 973 | int ti = ep[i]; |
| 974 | if (ti >= 0 && dp[ti] < 0) { |
| 975 | dp[ti] = dp[ni] + 1; |
| 976 | list[tail++] = ti; |
| 977 | } |
| 978 | } |
| 979 | } |
| 980 | } |
| 981 | /* Now find the nearest unvisited gem. */ |
| 982 | bestdist = -1; |
| 983 | target = -1; |
| 984 | for (i = 0; i < n; i++) { |
| 985 | if (unvisited[nodes[i] / DP1] && |
| 986 | dist[i] >= 0 && dist2[i] >= 0) { |
| 987 | int thisdist = dist[i] + dist2[i]; |
| 988 | if (bestdist < 0 || bestdist > thisdist) { |
| 989 | bestdist = thisdist; |
| 990 | target = i; |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | |
| 995 | if (target < 0) { |
| 996 | /* |
| 997 | * If we get to here, we haven't found a gem we can get |
| 998 | * at all, which means we terminate this loop. |
| 999 | */ |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * Now we have a graph vertex at list[tail-1] which is an |
| 1005 | * unvisited gem. We want to add that vertex to our tour. |
| 1006 | * So we run two more breadth-first searches: one starting |
| 1007 | * from that vertex and following forward edges, and |
| 1008 | * another starting from the same vertex and following |
| 1009 | * backward edges. This allows us to determine, for each |
| 1010 | * node on the current tour, how quickly we can get both to |
| 1011 | * and from the target vertex from that node. |
| 1012 | */ |
| 1013 | #ifdef TSP_DIAGNOSTICS |
| 1014 | printf("target node is %d (%d,%d,%d)\n", target, nodes[target]/DP1%w, |
| 1015 | nodes[target]/DP1/w, nodes[target]%DP1); |
| 1016 | #endif |
| 1017 | |
| 1018 | for (pass = 0; pass < 2; pass++) { |
| 1019 | int *ep = (pass == 0 ? edges : backedges); |
| 1020 | int *ei = (pass == 0 ? edgei : backedgei); |
| 1021 | int *dp = (pass == 0 ? dist : dist2); |
| 1022 | |
| 1023 | for (i = 0; i < n; i++) |
| 1024 | dp[i] = -1; |
| 1025 | head = tail = 0; |
| 1026 | |
| 1027 | dp[target] = 0; |
| 1028 | list[tail++] = target; |
| 1029 | |
| 1030 | while (head < tail) { |
| 1031 | int ni = list[head++]; |
| 1032 | for (i = ei[ni]; i < ei[ni+1]; i++) { |
| 1033 | int ti = ep[i]; |
| 1034 | if (ti >= 0 && dp[ti] < 0) { |
| 1035 | dp[ti] = dp[ni] + 1; |
| 1036 | /*printf("pass %d: set dist of vertex %d to %d (via %d)\n", pass, ti, dp[ti], ni);*/ |
| 1037 | list[tail++] = ti; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /* |
| 1044 | * Now for every node n, dist[n] gives the length of the |
| 1045 | * shortest path from the target vertex to n, and dist2[n] |
| 1046 | * gives the length of the shortest path from n to the |
| 1047 | * target vertex. |
| 1048 | * |
| 1049 | * Our next step is to search linearly along the tour to |
| 1050 | * find the optimum place to insert a trip to the target |
| 1051 | * vertex and back. Our two options are either |
| 1052 | * (a) to find two adjacent vertices A,B in the tour and |
| 1053 | * replace the edge A->B with the path A->target->B |
| 1054 | * (b) to find a single vertex X in the tour and replace |
| 1055 | * it with the complete round trip X->target->X. |
| 1056 | * We do whichever takes the fewest moves. |
| 1057 | */ |
| 1058 | n1 = n2 = -1; |
| 1059 | bestdist = -1; |
| 1060 | for (i = 0; i < circuitlen; i++) { |
| 1061 | int thisdist; |
| 1062 | |
| 1063 | /* |
| 1064 | * Try a round trip from vertex i. |
| 1065 | */ |
| 1066 | if (dist[circuit[i]] >= 0 && |
| 1067 | dist2[circuit[i]] >= 0) { |
| 1068 | thisdist = dist[circuit[i]] + dist2[circuit[i]]; |
| 1069 | if (bestdist < 0 || thisdist < bestdist) { |
| 1070 | bestdist = thisdist; |
| 1071 | n1 = n2 = i; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Try a trip from vertex i via target to vertex i+1. |
| 1077 | */ |
| 1078 | if (i+1 < circuitlen && |
| 1079 | dist2[circuit[i]] >= 0 && |
| 1080 | dist[circuit[i+1]] >= 0) { |
| 1081 | thisdist = dist2[circuit[i]] + dist[circuit[i+1]]; |
| 1082 | if (bestdist < 0 || thisdist < bestdist) { |
| 1083 | bestdist = thisdist; |
| 1084 | n1 = i; |
| 1085 | n2 = i+1; |
| 1086 | } |
| 1087 | } |
| 1088 | } |
| 1089 | if (bestdist < 0) { |
| 1090 | /* |
| 1091 | * We couldn't find a round trip taking in this gem _at |
| 1092 | * all_. Give up. |
| 1093 | */ |
| 1094 | err = "Unable to find a solution from this starting point"; |
| 1095 | break; |
| 1096 | } |
| 1097 | #ifdef TSP_DIAGNOSTICS |
| 1098 | printf("insertion point: n1=%d, n2=%d, dist=%d\n", n1, n2, bestdist); |
| 1099 | #endif |
| 1100 | |
| 1101 | #ifdef TSP_DIAGNOSTICS |
| 1102 | printf("circuit before lengthening is"); |
| 1103 | for (i = 0; i < circuitlen; i++) { |
| 1104 | printf(" %d", circuit[i]); |
| 1105 | } |
| 1106 | printf("\n"); |
| 1107 | #endif |
| 1108 | |
| 1109 | /* |
| 1110 | * Now actually lengthen the tour to take in this round |
| 1111 | * trip. |
| 1112 | */ |
| 1113 | extralen = dist2[circuit[n1]] + dist[circuit[n2]]; |
| 1114 | if (n1 != n2) |
| 1115 | extralen--; |
| 1116 | circuitlen += extralen; |
| 1117 | if (circuitlen >= circuitsize) { |
| 1118 | circuitsize = circuitlen + 256; |
| 1119 | circuit = sresize(circuit, circuitsize, int); |
| 1120 | } |
| 1121 | memmove(circuit + n2 + extralen, circuit + n2, |
| 1122 | (circuitlen - n2 - extralen) * sizeof(int)); |
| 1123 | n2 += extralen; |
| 1124 | |
| 1125 | #ifdef TSP_DIAGNOSTICS |
| 1126 | printf("circuit in middle of lengthening is"); |
| 1127 | for (i = 0; i < circuitlen; i++) { |
| 1128 | printf(" %d", circuit[i]); |
| 1129 | } |
| 1130 | printf("\n"); |
| 1131 | #endif |
| 1132 | |
| 1133 | /* |
| 1134 | * Find the shortest-path routes to and from the target, |
| 1135 | * and write them into the circuit. |
| 1136 | */ |
| 1137 | targetpos = n1 + dist2[circuit[n1]]; |
| 1138 | assert(targetpos - dist2[circuit[n1]] == n1); |
| 1139 | assert(targetpos + dist[circuit[n2]] == n2); |
| 1140 | for (pass = 0; pass < 2; pass++) { |
| 1141 | int dir = (pass == 0 ? -1 : +1); |
| 1142 | int *ep = (pass == 0 ? backedges : edges); |
| 1143 | int *ei = (pass == 0 ? backedgei : edgei); |
| 1144 | int *dp = (pass == 0 ? dist : dist2); |
| 1145 | int nn = (pass == 0 ? n2 : n1); |
| 1146 | int ni = circuit[nn], ti, dest = nn; |
| 1147 | |
| 1148 | while (1) { |
| 1149 | circuit[dest] = ni; |
| 1150 | if (dp[ni] == 0) |
| 1151 | break; |
| 1152 | dest += dir; |
| 1153 | ti = -1; |
| 1154 | /*printf("pass %d: looking at vertex %d\n", pass, ni);*/ |
| 1155 | for (i = ei[ni]; i < ei[ni+1]; i++) { |
| 1156 | ti = ep[i]; |
| 1157 | if (ti >= 0 && dp[ti] == dp[ni] - 1) |
| 1158 | break; |
| 1159 | } |
| 1160 | assert(i < ei[ni+1] && ti >= 0); |
| 1161 | ni = ti; |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | #ifdef TSP_DIAGNOSTICS |
| 1166 | printf("circuit after lengthening is"); |
| 1167 | for (i = 0; i < circuitlen; i++) { |
| 1168 | printf(" %d", circuit[i]); |
| 1169 | } |
| 1170 | printf("\n"); |
| 1171 | #endif |
| 1172 | |
| 1173 | /* |
| 1174 | * Finally, mark all gems that the new piece of circuit |
| 1175 | * passes through as visited. |
| 1176 | */ |
| 1177 | for (i = n1; i <= n2; i++) { |
| 1178 | int pos = nodes[circuit[i]] / DP1; |
| 1179 | assert(pos >= 0 && pos < wh); |
| 1180 | unvisited[pos] = FALSE; |
| 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | #ifdef TSP_DIAGNOSTICS |
| 1185 | printf("before reduction, moves are "); |
| 1186 | x = nodes[circuit[0]] / DP1 % w; |
| 1187 | y = nodes[circuit[0]] / DP1 / w; |
| 1188 | for (i = 1; i < circuitlen; i++) { |
| 1189 | int x2, y2, dx, dy; |
| 1190 | if (nodes[circuit[i]] % DP1 != DIRECTIONS) |
| 1191 | continue; |
| 1192 | x2 = nodes[circuit[i]] / DP1 % w; |
| 1193 | y2 = nodes[circuit[i]] / DP1 / w; |
| 1194 | dx = (x2 > x ? +1 : x2 < x ? -1 : 0); |
| 1195 | dy = (y2 > y ? +1 : y2 < y ? -1 : 0); |
| 1196 | for (d = 0; d < DIRECTIONS; d++) |
| 1197 | if (DX(d) == dx && DY(d) == dy) |
| 1198 | printf("%c", "89632147"[d]); |
| 1199 | x = x2; |
| 1200 | y = y2; |
| 1201 | } |
| 1202 | printf("\n"); |
| 1203 | #endif |
| 1204 | |
| 1205 | /* |
| 1206 | * That's got a basic solution. Now optimise it by removing |
| 1207 | * redundant sections of the circuit: it's entirely possible |
| 1208 | * that a piece of circuit we carefully inserted at one stage |
| 1209 | * to collect a gem has become pointless because the steps |
| 1210 | * required to collect some _later_ gem necessarily passed |
| 1211 | * through the same one. |
| 1212 | * |
| 1213 | * So first we go through and work out how many times each gem |
| 1214 | * is collected. Then we look for maximal sections of circuit |
| 1215 | * which are redundant in the sense that their removal would |
| 1216 | * not reduce any gem's collection count to zero, and replace |
| 1217 | * each one with a bfs-derived fastest path between their |
| 1218 | * endpoints. |
| 1219 | */ |
| 1220 | while (1) { |
| 1221 | int oldlen = circuitlen; |
| 1222 | int dir; |
| 1223 | |
| 1224 | for (dir = +1; dir >= -1; dir -= 2) { |
| 1225 | |
| 1226 | for (i = 0; i < wh; i++) |
| 1227 | unvisited[i] = 0; |
| 1228 | for (i = 0; i < circuitlen; i++) { |
| 1229 | int xy = nodes[circuit[i]] / DP1; |
| 1230 | if (currstate->grid[xy] == GEM) |
| 1231 | unvisited[xy]++; |
| 1232 | } |
| 1233 | |
| 1234 | /* |
| 1235 | * If there's any gem we didn't end up visiting at all, |
| 1236 | * give up. |
| 1237 | */ |
| 1238 | for (i = 0; i < wh; i++) { |
| 1239 | if (currstate->grid[i] == GEM && unvisited[i] == 0) { |
| 1240 | err = "Unable to find a solution from this starting point"; |
| 1241 | break; |
| 1242 | } |
| 1243 | } |
| 1244 | if (i < wh) |
| 1245 | break; |
| 1246 | |
| 1247 | for (i = j = (dir > 0 ? 0 : circuitlen-1); |
| 1248 | i < circuitlen && i >= 0; |
| 1249 | i += dir) { |
| 1250 | int xy = nodes[circuit[i]] / DP1; |
| 1251 | if (currstate->grid[xy] == GEM && unvisited[xy] > 1) { |
| 1252 | unvisited[xy]--; |
| 1253 | } else if (currstate->grid[xy] == GEM || i == circuitlen-1) { |
| 1254 | /* |
| 1255 | * circuit[i] collects a gem for the only time, |
| 1256 | * or is the last node in the circuit. |
| 1257 | * Therefore it cannot be removed; so we now |
| 1258 | * want to replace the path from circuit[j] to |
| 1259 | * circuit[i] with a bfs-shortest path. |
| 1260 | */ |
| 1261 | int p, q, k, dest, ni, ti, thisdist; |
| 1262 | |
| 1263 | /* |
| 1264 | * Set up the upper and lower bounds of the |
| 1265 | * reduced section. |
| 1266 | */ |
| 1267 | p = min(i, j); |
| 1268 | q = max(i, j); |
| 1269 | |
| 1270 | #ifdef TSP_DIAGNOSTICS |
| 1271 | printf("optimising section from %d - %d\n", p, q); |
| 1272 | #endif |
| 1273 | |
| 1274 | for (k = 0; k < n; k++) |
| 1275 | dist[k] = -1; |
| 1276 | head = tail = 0; |
| 1277 | |
| 1278 | dist[circuit[p]] = 0; |
| 1279 | list[tail++] = circuit[p]; |
| 1280 | |
| 1281 | while (head < tail && dist[circuit[q]] < 0) { |
| 1282 | int ni = list[head++]; |
| 1283 | for (k = edgei[ni]; k < edgei[ni+1]; k++) { |
| 1284 | int ti = edges[k]; |
| 1285 | if (ti >= 0 && dist[ti] < 0) { |
| 1286 | dist[ti] = dist[ni] + 1; |
| 1287 | list[tail++] = ti; |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | |
| 1292 | thisdist = dist[circuit[q]]; |
| 1293 | assert(thisdist >= 0 && thisdist <= q-p); |
| 1294 | |
| 1295 | memmove(circuit+p+thisdist, circuit+q, |
| 1296 | (circuitlen - q) * sizeof(int)); |
| 1297 | circuitlen -= q-p; |
| 1298 | q = p + thisdist; |
| 1299 | circuitlen += q-p; |
| 1300 | |
| 1301 | if (dir > 0) |
| 1302 | i = q; /* resume loop from the right place */ |
| 1303 | |
| 1304 | #ifdef TSP_DIAGNOSTICS |
| 1305 | printf("new section runs from %d - %d\n", p, q); |
| 1306 | #endif |
| 1307 | |
| 1308 | dest = q; |
| 1309 | assert(dest >= 0); |
| 1310 | ni = circuit[q]; |
| 1311 | |
| 1312 | while (1) { |
| 1313 | /* printf("dest=%d circuitlen=%d ni=%d dist[ni]=%d\n", dest, circuitlen, ni, dist[ni]); */ |
| 1314 | circuit[dest] = ni; |
| 1315 | if (dist[ni] == 0) |
| 1316 | break; |
| 1317 | dest--; |
| 1318 | ti = -1; |
| 1319 | for (k = backedgei[ni]; k < backedgei[ni+1]; k++) { |
| 1320 | ti = backedges[k]; |
| 1321 | if (ti >= 0 && dist[ti] == dist[ni] - 1) |
| 1322 | break; |
| 1323 | } |
| 1324 | assert(k < backedgei[ni+1] && ti >= 0); |
| 1325 | ni = ti; |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Now re-increment the visit counts for the |
| 1330 | * new path. |
| 1331 | */ |
| 1332 | while (++p < q) { |
| 1333 | int xy = nodes[circuit[p]] / DP1; |
| 1334 | if (currstate->grid[xy] == GEM) |
| 1335 | unvisited[xy]++; |
| 1336 | } |
| 1337 | |
| 1338 | j = i; |
| 1339 | |
| 1340 | #ifdef TSP_DIAGNOSTICS |
| 1341 | printf("during reduction, circuit is"); |
| 1342 | for (k = 0; k < circuitlen; k++) { |
| 1343 | int nc = nodes[circuit[k]]; |
| 1344 | printf(" (%d,%d,%d)", nc/DP1%w, nc/(DP1*w), nc%DP1); |
| 1345 | } |
| 1346 | printf("\n"); |
| 1347 | printf("moves are "); |
| 1348 | x = nodes[circuit[0]] / DP1 % w; |
| 1349 | y = nodes[circuit[0]] / DP1 / w; |
| 1350 | for (k = 1; k < circuitlen; k++) { |
| 1351 | int x2, y2, dx, dy; |
| 1352 | if (nodes[circuit[k]] % DP1 != DIRECTIONS) |
| 1353 | continue; |
| 1354 | x2 = nodes[circuit[k]] / DP1 % w; |
| 1355 | y2 = nodes[circuit[k]] / DP1 / w; |
| 1356 | dx = (x2 > x ? +1 : x2 < x ? -1 : 0); |
| 1357 | dy = (y2 > y ? +1 : y2 < y ? -1 : 0); |
| 1358 | for (d = 0; d < DIRECTIONS; d++) |
| 1359 | if (DX(d) == dx && DY(d) == dy) |
| 1360 | printf("%c", "89632147"[d]); |
| 1361 | x = x2; |
| 1362 | y = y2; |
| 1363 | } |
| 1364 | printf("\n"); |
| 1365 | #endif |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | #ifdef TSP_DIAGNOSTICS |
| 1370 | printf("after reduction, moves are "); |
| 1371 | x = nodes[circuit[0]] / DP1 % w; |
| 1372 | y = nodes[circuit[0]] / DP1 / w; |
| 1373 | for (i = 1; i < circuitlen; i++) { |
| 1374 | int x2, y2, dx, dy; |
| 1375 | if (nodes[circuit[i]] % DP1 != DIRECTIONS) |
| 1376 | continue; |
| 1377 | x2 = nodes[circuit[i]] / DP1 % w; |
| 1378 | y2 = nodes[circuit[i]] / DP1 / w; |
| 1379 | dx = (x2 > x ? +1 : x2 < x ? -1 : 0); |
| 1380 | dy = (y2 > y ? +1 : y2 < y ? -1 : 0); |
| 1381 | for (d = 0; d < DIRECTIONS; d++) |
| 1382 | if (DX(d) == dx && DY(d) == dy) |
| 1383 | printf("%c", "89632147"[d]); |
| 1384 | x = x2; |
| 1385 | y = y2; |
| 1386 | } |
| 1387 | printf("\n"); |
| 1388 | #endif |
| 1389 | } |
| 1390 | |
| 1391 | /* |
| 1392 | * If we've managed an entire reduction pass in each |
| 1393 | * direction and not made the solution any shorter, we're |
| 1394 | * _really_ done. |
| 1395 | */ |
| 1396 | if (circuitlen == oldlen) |
| 1397 | break; |
| 1398 | } |
| 1399 | |
| 1400 | /* |
| 1401 | * Encode the solution as a move string. |
| 1402 | */ |
| 1403 | if (!err) { |
| 1404 | soln = snewn(circuitlen+2, char); |
| 1405 | p = soln; |
| 1406 | *p++ = 'S'; |
| 1407 | x = nodes[circuit[0]] / DP1 % w; |
| 1408 | y = nodes[circuit[0]] / DP1 / w; |
| 1409 | for (i = 1; i < circuitlen; i++) { |
| 1410 | int x2, y2, dx, dy; |
| 1411 | if (nodes[circuit[i]] % DP1 != DIRECTIONS) |
| 1412 | continue; |
| 1413 | x2 = nodes[circuit[i]] / DP1 % w; |
| 1414 | y2 = nodes[circuit[i]] / DP1 / w; |
| 1415 | dx = (x2 > x ? +1 : x2 < x ? -1 : 0); |
| 1416 | dy = (y2 > y ? +1 : y2 < y ? -1 : 0); |
| 1417 | for (d = 0; d < DIRECTIONS; d++) |
| 1418 | if (DX(d) == dx && DY(d) == dy) { |
| 1419 | *p++ = '0' + d; |
| 1420 | break; |
| 1421 | } |
| 1422 | assert(d < DIRECTIONS); |
| 1423 | x = x2; |
| 1424 | y = y2; |
| 1425 | } |
| 1426 | *p++ = '\0'; |
| 1427 | assert(p - soln < circuitlen+2); |
| 1428 | } |
| 1429 | |
| 1430 | sfree(list); |
| 1431 | sfree(dist); |
| 1432 | sfree(dist2); |
| 1433 | sfree(unvisited); |
| 1434 | sfree(circuit); |
| 1435 | sfree(backedgei); |
| 1436 | sfree(backedges); |
| 1437 | sfree(edgei); |
| 1438 | sfree(edges); |
| 1439 | sfree(nodeindex); |
| 1440 | sfree(nodes); |
| 1441 | |
| 1442 | if (err) |
| 1443 | *error = err; |
| 1444 | |
| 1445 | return soln; |
| 1446 | } |
| 1447 | |
| 1448 | static char *game_text_format(game_state *state) |
| 1449 | { |
| 1450 | return NULL; |
| 1451 | } |
| 1452 | |
| 1453 | struct game_ui { |
| 1454 | float anim_length; |
| 1455 | int flashtype; |
| 1456 | int deaths; |
| 1457 | int just_made_move; |
| 1458 | int just_died; |
| 1459 | }; |
| 1460 | |
| 1461 | static game_ui *new_ui(game_state *state) |
| 1462 | { |
| 1463 | game_ui *ui = snew(game_ui); |
| 1464 | ui->anim_length = 0.0F; |
| 1465 | ui->flashtype = 0; |
| 1466 | ui->deaths = 0; |
| 1467 | ui->just_made_move = FALSE; |
| 1468 | ui->just_died = FALSE; |
| 1469 | return ui; |
| 1470 | } |
| 1471 | |
| 1472 | static void free_ui(game_ui *ui) |
| 1473 | { |
| 1474 | sfree(ui); |
| 1475 | } |
| 1476 | |
| 1477 | static char *encode_ui(game_ui *ui) |
| 1478 | { |
| 1479 | char buf[80]; |
| 1480 | /* |
| 1481 | * The deaths counter needs preserving across a serialisation. |
| 1482 | */ |
| 1483 | sprintf(buf, "D%d", ui->deaths); |
| 1484 | return dupstr(buf); |
| 1485 | } |
| 1486 | |
| 1487 | static void decode_ui(game_ui *ui, char *encoding) |
| 1488 | { |
| 1489 | int p = 0; |
| 1490 | sscanf(encoding, "D%d%n", &ui->deaths, &p); |
| 1491 | } |
| 1492 | |
| 1493 | static void game_changed_state(game_ui *ui, game_state *oldstate, |
| 1494 | game_state *newstate) |
| 1495 | { |
| 1496 | /* |
| 1497 | * Increment the deaths counter. We only do this if |
| 1498 | * ui->just_made_move is set (redoing a suicide move doesn't |
| 1499 | * kill you _again_), and also we only do it if the game wasn't |
| 1500 | * already completed (once you're finished, you can play). |
| 1501 | */ |
| 1502 | if (!oldstate->dead && newstate->dead && ui->just_made_move && |
| 1503 | oldstate->gems) { |
| 1504 | ui->deaths++; |
| 1505 | ui->just_died = TRUE; |
| 1506 | } else { |
| 1507 | ui->just_died = FALSE; |
| 1508 | } |
| 1509 | ui->just_made_move = FALSE; |
| 1510 | } |
| 1511 | |
| 1512 | struct game_drawstate { |
| 1513 | game_params p; |
| 1514 | int tilesize; |
| 1515 | int started; |
| 1516 | unsigned short *grid; |
| 1517 | blitter *player_background; |
| 1518 | int player_bg_saved, pbgx, pbgy; |
| 1519 | }; |
| 1520 | |
| 1521 | #define PREFERRED_TILESIZE 32 |
| 1522 | #define TILESIZE (ds->tilesize) |
| 1523 | #ifdef SMALL_SCREEN |
| 1524 | #define BORDER (TILESIZE / 4) |
| 1525 | #else |
| 1526 | #define BORDER (TILESIZE) |
| 1527 | #endif |
| 1528 | #define HIGHLIGHT_WIDTH (TILESIZE / 10) |
| 1529 | #define COORD(x) ( (x) * TILESIZE + BORDER ) |
| 1530 | #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 ) |
| 1531 | |
| 1532 | static char *interpret_move(game_state *state, game_ui *ui, game_drawstate *ds, |
| 1533 | int x, int y, int button) |
| 1534 | { |
| 1535 | int w = state->p.w, h = state->p.h /*, wh = w*h */; |
| 1536 | int dir; |
| 1537 | char buf[80]; |
| 1538 | |
| 1539 | dir = -1; |
| 1540 | |
| 1541 | if (button == LEFT_BUTTON) { |
| 1542 | /* |
| 1543 | * Mouse-clicking near the target point (or, more |
| 1544 | * accurately, in the appropriate octant) is an alternative |
| 1545 | * way to input moves. |
| 1546 | */ |
| 1547 | |
| 1548 | if (FROMCOORD(x) != state->px || FROMCOORD(y) != state->py) { |
| 1549 | int dx, dy; |
| 1550 | float angle; |
| 1551 | |
| 1552 | dx = FROMCOORD(x) - state->px; |
| 1553 | dy = FROMCOORD(y) - state->py; |
| 1554 | /* I pass dx,dy rather than dy,dx so that the octants |
| 1555 | * end up the right way round. */ |
| 1556 | angle = atan2(dx, -dy); |
| 1557 | |
| 1558 | angle = (angle + (PI/8)) / (PI/4); |
| 1559 | assert(angle > -16.0F); |
| 1560 | dir = (int)(angle + 16.0F) & 7; |
| 1561 | } |
| 1562 | } else if (button == CURSOR_UP || button == (MOD_NUM_KEYPAD | '8')) |
| 1563 | dir = 0; |
| 1564 | else if (button == CURSOR_DOWN || button == (MOD_NUM_KEYPAD | '2')) |
| 1565 | dir = 4; |
| 1566 | else if (button == CURSOR_LEFT || button == (MOD_NUM_KEYPAD | '4')) |
| 1567 | dir = 6; |
| 1568 | else if (button == CURSOR_RIGHT || button == (MOD_NUM_KEYPAD | '6')) |
| 1569 | dir = 2; |
| 1570 | else if (button == (MOD_NUM_KEYPAD | '7')) |
| 1571 | dir = 7; |
| 1572 | else if (button == (MOD_NUM_KEYPAD | '1')) |
| 1573 | dir = 5; |
| 1574 | else if (button == (MOD_NUM_KEYPAD | '9')) |
| 1575 | dir = 1; |
| 1576 | else if (button == (MOD_NUM_KEYPAD | '3')) |
| 1577 | dir = 3; |
| 1578 | else if (button == ' ' && state->soln && state->solnpos < state->soln->len) |
| 1579 | dir = state->soln->list[state->solnpos]; |
| 1580 | |
| 1581 | if (dir < 0) |
| 1582 | return NULL; |
| 1583 | |
| 1584 | /* |
| 1585 | * Reject the move if we can't make it at all due to a wall |
| 1586 | * being in the way. |
| 1587 | */ |
| 1588 | if (AT(w, h, state->grid, state->px+DX(dir), state->py+DY(dir)) == WALL) |
| 1589 | return NULL; |
| 1590 | |
| 1591 | /* |
| 1592 | * Reject the move if we're dead! |
| 1593 | */ |
| 1594 | if (state->dead) |
| 1595 | return NULL; |
| 1596 | |
| 1597 | /* |
| 1598 | * Otherwise, we can make the move. All we need to specify is |
| 1599 | * the direction. |
| 1600 | */ |
| 1601 | ui->just_made_move = TRUE; |
| 1602 | sprintf(buf, "%d", dir); |
| 1603 | return dupstr(buf); |
| 1604 | } |
| 1605 | |
| 1606 | static game_state *execute_move(game_state *state, char *move) |
| 1607 | { |
| 1608 | int w = state->p.w, h = state->p.h /*, wh = w*h */; |
| 1609 | int dir; |
| 1610 | game_state *ret; |
| 1611 | |
| 1612 | if (*move == 'S') { |
| 1613 | int len, i; |
| 1614 | soln *sol; |
| 1615 | |
| 1616 | /* |
| 1617 | * This is a solve move, so we don't actually _change_ the |
| 1618 | * grid but merely set up a stored solution path. |
| 1619 | */ |
| 1620 | move++; |
| 1621 | len = strlen(move); |
| 1622 | sol = snew(soln); |
| 1623 | sol->len = len; |
| 1624 | sol->list = snewn(len, unsigned char); |
| 1625 | for (i = 0; i < len; i++) |
| 1626 | sol->list[i] = move[i] - '0'; |
| 1627 | ret = dup_game(state); |
| 1628 | ret->cheated = TRUE; |
| 1629 | ret->soln = sol; |
| 1630 | ret->solnpos = 0; |
| 1631 | sol->refcount = 1; |
| 1632 | return ret; |
| 1633 | } |
| 1634 | |
| 1635 | dir = atoi(move); |
| 1636 | if (dir < 0 || dir >= DIRECTIONS) |
| 1637 | return NULL; /* huh? */ |
| 1638 | |
| 1639 | if (state->dead) |
| 1640 | return NULL; |
| 1641 | |
| 1642 | if (AT(w, h, state->grid, state->px+DX(dir), state->py+DY(dir)) == WALL) |
| 1643 | return NULL; /* wall in the way! */ |
| 1644 | |
| 1645 | /* |
| 1646 | * Now make the move. |
| 1647 | */ |
| 1648 | ret = dup_game(state); |
| 1649 | ret->distance_moved = 0; |
| 1650 | while (1) { |
| 1651 | ret->px += DX(dir); |
| 1652 | ret->py += DY(dir); |
| 1653 | ret->distance_moved++; |
| 1654 | |
| 1655 | if (AT(w, h, ret->grid, ret->px, ret->py) == GEM) { |
| 1656 | LV_AT(w, h, ret->grid, ret->px, ret->py) = BLANK; |
| 1657 | ret->gems--; |
| 1658 | } |
| 1659 | |
| 1660 | if (AT(w, h, ret->grid, ret->px, ret->py) == MINE) { |
| 1661 | ret->dead = TRUE; |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | if (AT(w, h, ret->grid, ret->px, ret->py) == STOP || |
| 1666 | AT(w, h, ret->grid, ret->px+DX(dir), |
| 1667 | ret->py+DY(dir)) == WALL) |
| 1668 | break; |
| 1669 | } |
| 1670 | |
| 1671 | if (ret->soln) { |
| 1672 | /* |
| 1673 | * If this move is the correct next one in the stored |
| 1674 | * solution path, advance solnpos. |
| 1675 | */ |
| 1676 | if (ret->soln->list[ret->solnpos] == dir && |
| 1677 | ret->solnpos+1 < ret->soln->len) { |
| 1678 | ret->solnpos++; |
| 1679 | } else { |
| 1680 | /* |
| 1681 | * Otherwise, the user has strayed from the path, so |
| 1682 | * the path is no longer valid. |
| 1683 | */ |
| 1684 | ret->soln->refcount--; |
| 1685 | assert(ret->soln->refcount > 0);/* `state' at least still exists */ |
| 1686 | ret->soln = NULL; |
| 1687 | ret->solnpos = 0; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | return ret; |
| 1692 | } |
| 1693 | |
| 1694 | /* ---------------------------------------------------------------------- |
| 1695 | * Drawing routines. |
| 1696 | */ |
| 1697 | |
| 1698 | static void game_compute_size(game_params *params, int tilesize, |
| 1699 | int *x, int *y) |
| 1700 | { |
| 1701 | /* Ick: fake up `ds->tilesize' for macro expansion purposes */ |
| 1702 | struct { int tilesize; } ads, *ds = &ads; |
| 1703 | ads.tilesize = tilesize; |
| 1704 | |
| 1705 | *x = 2 * BORDER + 1 + params->w * TILESIZE; |
| 1706 | *y = 2 * BORDER + 1 + params->h * TILESIZE; |
| 1707 | } |
| 1708 | |
| 1709 | static void game_set_size(drawing *dr, game_drawstate *ds, |
| 1710 | game_params *params, int tilesize) |
| 1711 | { |
| 1712 | ds->tilesize = tilesize; |
| 1713 | |
| 1714 | assert(!ds->player_background); /* set_size is never called twice */ |
| 1715 | assert(!ds->player_bg_saved); |
| 1716 | |
| 1717 | ds->player_background = blitter_new(dr, TILESIZE, TILESIZE); |
| 1718 | } |
| 1719 | |
| 1720 | static float *game_colours(frontend *fe, int *ncolours) |
| 1721 | { |
| 1722 | float *ret = snewn(3 * NCOLOURS, float); |
| 1723 | int i; |
| 1724 | |
| 1725 | game_mkhighlight(fe, ret, COL_BACKGROUND, COL_HIGHLIGHT, COL_LOWLIGHT); |
| 1726 | |
| 1727 | ret[COL_OUTLINE * 3 + 0] = 0.0F; |
| 1728 | ret[COL_OUTLINE * 3 + 1] = 0.0F; |
| 1729 | ret[COL_OUTLINE * 3 + 2] = 0.0F; |
| 1730 | |
| 1731 | ret[COL_PLAYER * 3 + 0] = 0.0F; |
| 1732 | ret[COL_PLAYER * 3 + 1] = 1.0F; |
| 1733 | ret[COL_PLAYER * 3 + 2] = 0.0F; |
| 1734 | |
| 1735 | ret[COL_DEAD_PLAYER * 3 + 0] = 1.0F; |
| 1736 | ret[COL_DEAD_PLAYER * 3 + 1] = 0.0F; |
| 1737 | ret[COL_DEAD_PLAYER * 3 + 2] = 0.0F; |
| 1738 | |
| 1739 | ret[COL_MINE * 3 + 0] = 0.0F; |
| 1740 | ret[COL_MINE * 3 + 1] = 0.0F; |
| 1741 | ret[COL_MINE * 3 + 2] = 0.0F; |
| 1742 | |
| 1743 | ret[COL_GEM * 3 + 0] = 0.6F; |
| 1744 | ret[COL_GEM * 3 + 1] = 1.0F; |
| 1745 | ret[COL_GEM * 3 + 2] = 1.0F; |
| 1746 | |
| 1747 | for (i = 0; i < 3; i++) { |
| 1748 | ret[COL_WALL * 3 + i] = (3 * ret[COL_BACKGROUND * 3 + i] + |
| 1749 | 1 * ret[COL_HIGHLIGHT * 3 + i]) / 4; |
| 1750 | } |
| 1751 | |
| 1752 | ret[COL_HINT * 3 + 0] = 1.0F; |
| 1753 | ret[COL_HINT * 3 + 1] = 1.0F; |
| 1754 | ret[COL_HINT * 3 + 2] = 0.0F; |
| 1755 | |
| 1756 | *ncolours = NCOLOURS; |
| 1757 | return ret; |
| 1758 | } |
| 1759 | |
| 1760 | static game_drawstate *game_new_drawstate(drawing *dr, game_state *state) |
| 1761 | { |
| 1762 | int w = state->p.w, h = state->p.h, wh = w*h; |
| 1763 | struct game_drawstate *ds = snew(struct game_drawstate); |
| 1764 | int i; |
| 1765 | |
| 1766 | ds->tilesize = 0; |
| 1767 | |
| 1768 | /* We can't allocate the blitter rectangle for the player background |
| 1769 | * until we know what size to make it. */ |
| 1770 | ds->player_background = NULL; |
| 1771 | ds->player_bg_saved = FALSE; |
| 1772 | ds->pbgx = ds->pbgy = -1; |
| 1773 | |
| 1774 | ds->p = state->p; /* structure copy */ |
| 1775 | ds->started = FALSE; |
| 1776 | ds->grid = snewn(wh, unsigned short); |
| 1777 | for (i = 0; i < wh; i++) |
| 1778 | ds->grid[i] = UNDRAWN; |
| 1779 | |
| 1780 | return ds; |
| 1781 | } |
| 1782 | |
| 1783 | static void game_free_drawstate(drawing *dr, game_drawstate *ds) |
| 1784 | { |
| 1785 | if (ds->player_background) |
| 1786 | blitter_free(dr, ds->player_background); |
| 1787 | sfree(ds->grid); |
| 1788 | sfree(ds); |
| 1789 | } |
| 1790 | |
| 1791 | static void draw_player(drawing *dr, game_drawstate *ds, int x, int y, |
| 1792 | int dead, int hintdir) |
| 1793 | { |
| 1794 | if (dead) { |
| 1795 | int coords[DIRECTIONS*4]; |
| 1796 | int d; |
| 1797 | |
| 1798 | for (d = 0; d < DIRECTIONS; d++) { |
| 1799 | float x1, y1, x2, y2, x3, y3, len; |
| 1800 | |
| 1801 | x1 = DX(d); |
| 1802 | y1 = DY(d); |
| 1803 | len = sqrt(x1*x1+y1*y1); x1 /= len; y1 /= len; |
| 1804 | |
| 1805 | x3 = DX(d+1); |
| 1806 | y3 = DY(d+1); |
| 1807 | len = sqrt(x3*x3+y3*y3); x3 /= len; y3 /= len; |
| 1808 | |
| 1809 | x2 = (x1+x3) / 4; |
| 1810 | y2 = (y1+y3) / 4; |
| 1811 | |
| 1812 | coords[d*4+0] = x + TILESIZE/2 + (int)((TILESIZE*3/7) * x1); |
| 1813 | coords[d*4+1] = y + TILESIZE/2 + (int)((TILESIZE*3/7) * y1); |
| 1814 | coords[d*4+2] = x + TILESIZE/2 + (int)((TILESIZE*3/7) * x2); |
| 1815 | coords[d*4+3] = y + TILESIZE/2 + (int)((TILESIZE*3/7) * y2); |
| 1816 | } |
| 1817 | draw_polygon(dr, coords, DIRECTIONS*2, COL_DEAD_PLAYER, COL_OUTLINE); |
| 1818 | } else { |
| 1819 | draw_circle(dr, x + TILESIZE/2, y + TILESIZE/2, |
| 1820 | TILESIZE/3, COL_PLAYER, COL_OUTLINE); |
| 1821 | } |
| 1822 | |
| 1823 | if (!dead && hintdir >= 0) { |
| 1824 | float scale = (DX(hintdir) && DY(hintdir) ? 0.8F : 1.0F); |
| 1825 | int ax = (TILESIZE*2/5) * scale * DX(hintdir); |
| 1826 | int ay = (TILESIZE*2/5) * scale * DY(hintdir); |
| 1827 | int px = -ay, py = ax; |
| 1828 | int ox = x + TILESIZE/2, oy = y + TILESIZE/2; |
| 1829 | int coords[14], *c; |
| 1830 | |
| 1831 | c = coords; |
| 1832 | *c++ = ox + px/9; |
| 1833 | *c++ = oy + py/9; |
| 1834 | *c++ = ox + px/9 + ax*2/3; |
| 1835 | *c++ = oy + py/9 + ay*2/3; |
| 1836 | *c++ = ox + px/3 + ax*2/3; |
| 1837 | *c++ = oy + py/3 + ay*2/3; |
| 1838 | *c++ = ox + ax; |
| 1839 | *c++ = oy + ay; |
| 1840 | *c++ = ox - px/3 + ax*2/3; |
| 1841 | *c++ = oy - py/3 + ay*2/3; |
| 1842 | *c++ = ox - px/9 + ax*2/3; |
| 1843 | *c++ = oy - py/9 + ay*2/3; |
| 1844 | *c++ = ox - px/9; |
| 1845 | *c++ = oy - py/9; |
| 1846 | draw_polygon(dr, coords, 7, COL_HINT, COL_OUTLINE); |
| 1847 | } |
| 1848 | |
| 1849 | draw_update(dr, x, y, TILESIZE, TILESIZE); |
| 1850 | } |
| 1851 | |
| 1852 | #define FLASH_DEAD 0x100 |
| 1853 | #define FLASH_WIN 0x200 |
| 1854 | #define FLASH_MASK 0x300 |
| 1855 | |
| 1856 | static void draw_tile(drawing *dr, game_drawstate *ds, int x, int y, int v) |
| 1857 | { |
| 1858 | int tx = COORD(x), ty = COORD(y); |
| 1859 | int bg = (v & FLASH_DEAD ? COL_DEAD_PLAYER : |
| 1860 | v & FLASH_WIN ? COL_HIGHLIGHT : COL_BACKGROUND); |
| 1861 | |
| 1862 | v &= ~FLASH_MASK; |
| 1863 | |
| 1864 | clip(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1); |
| 1865 | draw_rect(dr, tx+1, ty+1, TILESIZE-1, TILESIZE-1, bg); |
| 1866 | |
| 1867 | if (v == WALL) { |
| 1868 | int coords[6]; |
| 1869 | |
| 1870 | coords[0] = tx + TILESIZE; |
| 1871 | coords[1] = ty + TILESIZE; |
| 1872 | coords[2] = tx + TILESIZE; |
| 1873 | coords[3] = ty + 1; |
| 1874 | coords[4] = tx + 1; |
| 1875 | coords[5] = ty + TILESIZE; |
| 1876 | draw_polygon(dr, coords, 3, COL_LOWLIGHT, COL_LOWLIGHT); |
| 1877 | |
| 1878 | coords[0] = tx + 1; |
| 1879 | coords[1] = ty + 1; |
| 1880 | draw_polygon(dr, coords, 3, COL_HIGHLIGHT, COL_HIGHLIGHT); |
| 1881 | |
| 1882 | draw_rect(dr, tx + 1 + HIGHLIGHT_WIDTH, ty + 1 + HIGHLIGHT_WIDTH, |
| 1883 | TILESIZE - 2*HIGHLIGHT_WIDTH, |
| 1884 | TILESIZE - 2*HIGHLIGHT_WIDTH, COL_WALL); |
| 1885 | } else if (v == MINE) { |
| 1886 | int cx = tx + TILESIZE / 2; |
| 1887 | int cy = ty + TILESIZE / 2; |
| 1888 | int r = TILESIZE / 2 - 3; |
| 1889 | int coords[4*5*2]; |
| 1890 | int xdx = 1, xdy = 0, ydx = 0, ydy = 1; |
| 1891 | int tdx, tdy, i; |
| 1892 | |
| 1893 | for (i = 0; i < 4*5*2; i += 5*2) { |
| 1894 | coords[i+2*0+0] = cx - r/6*xdx + r*4/5*ydx; |
| 1895 | coords[i+2*0+1] = cy - r/6*xdy + r*4/5*ydy; |
| 1896 | coords[i+2*1+0] = cx - r/6*xdx + r*ydx; |
| 1897 | coords[i+2*1+1] = cy - r/6*xdy + r*ydy; |
| 1898 | coords[i+2*2+0] = cx + r/6*xdx + r*ydx; |
| 1899 | coords[i+2*2+1] = cy + r/6*xdy + r*ydy; |
| 1900 | coords[i+2*3+0] = cx + r/6*xdx + r*4/5*ydx; |
| 1901 | coords[i+2*3+1] = cy + r/6*xdy + r*4/5*ydy; |
| 1902 | coords[i+2*4+0] = cx + r*3/5*xdx + r*3/5*ydx; |
| 1903 | coords[i+2*4+1] = cy + r*3/5*xdy + r*3/5*ydy; |
| 1904 | |
| 1905 | tdx = ydx; |
| 1906 | tdy = ydy; |
| 1907 | ydx = xdx; |
| 1908 | ydy = xdy; |
| 1909 | xdx = -tdx; |
| 1910 | xdy = -tdy; |
| 1911 | } |
| 1912 | |
| 1913 | draw_polygon(dr, coords, 5*4, COL_MINE, COL_MINE); |
| 1914 | |
| 1915 | draw_rect(dr, cx-r/3, cy-r/3, r/3, r/4, COL_HIGHLIGHT); |
| 1916 | } else if (v == STOP) { |
| 1917 | draw_circle(dr, tx + TILESIZE/2, ty + TILESIZE/2, |
| 1918 | TILESIZE*3/7, -1, COL_OUTLINE); |
| 1919 | draw_rect(dr, tx + TILESIZE*3/7, ty+1, |
| 1920 | TILESIZE - 2*(TILESIZE*3/7) + 1, TILESIZE-1, bg); |
| 1921 | draw_rect(dr, tx+1, ty + TILESIZE*3/7, |
| 1922 | TILESIZE-1, TILESIZE - 2*(TILESIZE*3/7) + 1, bg); |
| 1923 | } else if (v == GEM) { |
| 1924 | int coords[8]; |
| 1925 | |
| 1926 | coords[0] = tx+TILESIZE/2; |
| 1927 | coords[1] = ty+TILESIZE/2-TILESIZE*5/14; |
| 1928 | coords[2] = tx+TILESIZE/2-TILESIZE*5/14; |
| 1929 | coords[3] = ty+TILESIZE/2; |
| 1930 | coords[4] = tx+TILESIZE/2; |
| 1931 | coords[5] = ty+TILESIZE/2+TILESIZE*5/14; |
| 1932 | coords[6] = tx+TILESIZE/2+TILESIZE*5/14; |
| 1933 | coords[7] = ty+TILESIZE/2; |
| 1934 | |
| 1935 | draw_polygon(dr, coords, 4, COL_GEM, COL_OUTLINE); |
| 1936 | } |
| 1937 | |
| 1938 | unclip(dr); |
| 1939 | draw_update(dr, tx, ty, TILESIZE, TILESIZE); |
| 1940 | } |
| 1941 | |
| 1942 | #define BASE_ANIM_LENGTH 0.1F |
| 1943 | #define FLASH_LENGTH 0.3F |
| 1944 | |
| 1945 | static void game_redraw(drawing *dr, game_drawstate *ds, game_state *oldstate, |
| 1946 | game_state *state, int dir, game_ui *ui, |
| 1947 | float animtime, float flashtime) |
| 1948 | { |
| 1949 | int w = state->p.w, h = state->p.h /*, wh = w*h */; |
| 1950 | int x, y; |
| 1951 | float ap; |
| 1952 | int player_dist; |
| 1953 | int flashtype; |
| 1954 | int gems, deaths; |
| 1955 | char status[256]; |
| 1956 | |
| 1957 | if (flashtime && |
| 1958 | !((int)(flashtime * 3 / FLASH_LENGTH) % 2)) |
| 1959 | flashtype = ui->flashtype; |
| 1960 | else |
| 1961 | flashtype = 0; |
| 1962 | |
| 1963 | /* |
| 1964 | * Erase the player sprite. |
| 1965 | */ |
| 1966 | if (ds->player_bg_saved) { |
| 1967 | assert(ds->player_background); |
| 1968 | blitter_load(dr, ds->player_background, ds->pbgx, ds->pbgy); |
| 1969 | draw_update(dr, ds->pbgx, ds->pbgy, TILESIZE, TILESIZE); |
| 1970 | ds->player_bg_saved = FALSE; |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * Initialise a fresh drawstate. |
| 1975 | */ |
| 1976 | if (!ds->started) { |
| 1977 | int wid, ht; |
| 1978 | |
| 1979 | /* |
| 1980 | * Blank out the window initially. |
| 1981 | */ |
| 1982 | game_compute_size(&ds->p, TILESIZE, &wid, &ht); |
| 1983 | draw_rect(dr, 0, 0, wid, ht, COL_BACKGROUND); |
| 1984 | draw_update(dr, 0, 0, wid, ht); |
| 1985 | |
| 1986 | /* |
| 1987 | * Draw the grid lines. |
| 1988 | */ |
| 1989 | for (y = 0; y <= h; y++) |
| 1990 | draw_line(dr, COORD(0), COORD(y), COORD(w), COORD(y), |
| 1991 | COL_LOWLIGHT); |
| 1992 | for (x = 0; x <= w; x++) |
| 1993 | draw_line(dr, COORD(x), COORD(0), COORD(x), COORD(h), |
| 1994 | COL_LOWLIGHT); |
| 1995 | |
| 1996 | ds->started = TRUE; |
| 1997 | } |
| 1998 | |
| 1999 | /* |
| 2000 | * If we're in the process of animating a move, let's start by |
| 2001 | * working out how far the player has moved from their _older_ |
| 2002 | * state. |
| 2003 | */ |
| 2004 | if (oldstate) { |
| 2005 | ap = animtime / ui->anim_length; |
| 2006 | player_dist = ap * (dir > 0 ? state : oldstate)->distance_moved; |
| 2007 | } else { |
| 2008 | player_dist = 0; |
| 2009 | ap = 0.0F; |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * Draw the grid contents. |
| 2014 | * |
| 2015 | * We count the gems as we go round this loop, for the purposes |
| 2016 | * of the status bar. Of course we have a gems counter in the |
| 2017 | * game_state already, but if we do the counting in this loop |
| 2018 | * then it tracks gems being picked up in a sliding move, and |
| 2019 | * updates one by one. |
| 2020 | */ |
| 2021 | gems = 0; |
| 2022 | for (y = 0; y < h; y++) |
| 2023 | for (x = 0; x < w; x++) { |
| 2024 | unsigned short v = (unsigned char)state->grid[y*w+x]; |
| 2025 | |
| 2026 | /* |
| 2027 | * Special case: if the player is in the process of |
| 2028 | * moving over a gem, we draw the gem iff they haven't |
| 2029 | * gone past it yet. |
| 2030 | */ |
| 2031 | if (oldstate && oldstate->grid[y*w+x] != state->grid[y*w+x]) { |
| 2032 | /* |
| 2033 | * Compute the distance from this square to the |
| 2034 | * original player position. |
| 2035 | */ |
| 2036 | int dist = max(abs(x - oldstate->px), abs(y - oldstate->py)); |
| 2037 | |
| 2038 | /* |
| 2039 | * If the player has reached here, use the new grid |
| 2040 | * element. Otherwise use the old one. |
| 2041 | */ |
| 2042 | if (player_dist < dist) |
| 2043 | v = oldstate->grid[y*w+x]; |
| 2044 | else |
| 2045 | v = state->grid[y*w+x]; |
| 2046 | } |
| 2047 | |
| 2048 | /* |
| 2049 | * Special case: erase the mine the dead player is |
| 2050 | * sitting on. Only at the end of the move. |
| 2051 | */ |
| 2052 | if (v == MINE && !oldstate && state->dead && |
| 2053 | x == state->px && y == state->py) |
| 2054 | v = BLANK; |
| 2055 | |
| 2056 | if (v == GEM) |
| 2057 | gems++; |
| 2058 | |
| 2059 | v |= flashtype; |
| 2060 | |
| 2061 | if (ds->grid[y*w+x] != v) { |
| 2062 | draw_tile(dr, ds, x, y, v); |
| 2063 | ds->grid[y*w+x] = v; |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /* |
| 2068 | * Gem counter in the status bar. We replace it with |
| 2069 | * `COMPLETED!' when it reaches zero ... or rather, when the |
| 2070 | * _current state_'s gem counter is zero. (Thus, `Gems: 0' is |
| 2071 | * shown between the collection of the last gem and the |
| 2072 | * completion of the move animation that did it.) |
| 2073 | */ |
| 2074 | if (state->dead && (!oldstate || oldstate->dead)) { |
| 2075 | sprintf(status, "DEAD!"); |
| 2076 | } else if (state->gems || (oldstate && oldstate->gems)) { |
| 2077 | if (state->cheated) |
| 2078 | sprintf(status, "Auto-solver used. "); |
| 2079 | else |
| 2080 | *status = '\0'; |
| 2081 | sprintf(status + strlen(status), "Gems: %d", gems); |
| 2082 | } else if (state->cheated) { |
| 2083 | sprintf(status, "Auto-solved."); |
| 2084 | } else { |
| 2085 | sprintf(status, "COMPLETED!"); |
| 2086 | } |
| 2087 | /* We subtract one from the visible death counter if we're still |
| 2088 | * animating the move at the end of which the death took place. */ |
| 2089 | deaths = ui->deaths; |
| 2090 | if (oldstate && ui->just_died) { |
| 2091 | assert(deaths > 0); |
| 2092 | deaths--; |
| 2093 | } |
| 2094 | if (deaths) |
| 2095 | sprintf(status + strlen(status), " Deaths: %d", deaths); |
| 2096 | status_bar(dr, status); |
| 2097 | |
| 2098 | /* |
| 2099 | * Draw the player sprite. |
| 2100 | */ |
| 2101 | assert(!ds->player_bg_saved); |
| 2102 | assert(ds->player_background); |
| 2103 | { |
| 2104 | int ox, oy, nx, ny; |
| 2105 | nx = COORD(state->px); |
| 2106 | ny = COORD(state->py); |
| 2107 | if (oldstate) { |
| 2108 | ox = COORD(oldstate->px); |
| 2109 | oy = COORD(oldstate->py); |
| 2110 | } else { |
| 2111 | ox = nx; |
| 2112 | oy = ny; |
| 2113 | } |
| 2114 | ds->pbgx = ox + ap * (nx - ox); |
| 2115 | ds->pbgy = oy + ap * (ny - oy); |
| 2116 | } |
| 2117 | blitter_save(dr, ds->player_background, ds->pbgx, ds->pbgy); |
| 2118 | draw_player(dr, ds, ds->pbgx, ds->pbgy, |
| 2119 | (state->dead && !oldstate), |
| 2120 | (!oldstate && state->soln ? |
| 2121 | state->soln->list[state->solnpos] : -1)); |
| 2122 | ds->player_bg_saved = TRUE; |
| 2123 | } |
| 2124 | |
| 2125 | static float game_anim_length(game_state *oldstate, game_state *newstate, |
| 2126 | int dir, game_ui *ui) |
| 2127 | { |
| 2128 | int dist; |
| 2129 | if (dir > 0) |
| 2130 | dist = newstate->distance_moved; |
| 2131 | else |
| 2132 | dist = oldstate->distance_moved; |
| 2133 | ui->anim_length = sqrt(dist) * BASE_ANIM_LENGTH; |
| 2134 | return ui->anim_length; |
| 2135 | } |
| 2136 | |
| 2137 | static float game_flash_length(game_state *oldstate, game_state *newstate, |
| 2138 | int dir, game_ui *ui) |
| 2139 | { |
| 2140 | if (!oldstate->dead && newstate->dead) { |
| 2141 | ui->flashtype = FLASH_DEAD; |
| 2142 | return FLASH_LENGTH; |
| 2143 | } else if (oldstate->gems && !newstate->gems) { |
| 2144 | ui->flashtype = FLASH_WIN; |
| 2145 | return FLASH_LENGTH; |
| 2146 | } |
| 2147 | return 0.0F; |
| 2148 | } |
| 2149 | |
| 2150 | static int game_timing_state(game_state *state, game_ui *ui) |
| 2151 | { |
| 2152 | return TRUE; |
| 2153 | } |
| 2154 | |
| 2155 | static void game_print_size(game_params *params, float *x, float *y) |
| 2156 | { |
| 2157 | } |
| 2158 | |
| 2159 | static void game_print(drawing *dr, game_state *state, int tilesize) |
| 2160 | { |
| 2161 | } |
| 2162 | |
| 2163 | #ifdef COMBINED |
| 2164 | #define thegame inertia |
| 2165 | #endif |
| 2166 | |
| 2167 | const struct game thegame = { |
| 2168 | "Inertia", "games.inertia", "inertia", |
| 2169 | default_params, |
| 2170 | game_fetch_preset, |
| 2171 | decode_params, |
| 2172 | encode_params, |
| 2173 | free_params, |
| 2174 | dup_params, |
| 2175 | TRUE, game_configure, custom_params, |
| 2176 | validate_params, |
| 2177 | new_game_desc, |
| 2178 | validate_desc, |
| 2179 | new_game, |
| 2180 | dup_game, |
| 2181 | free_game, |
| 2182 | TRUE, solve_game, |
| 2183 | FALSE, game_text_format, |
| 2184 | new_ui, |
| 2185 | free_ui, |
| 2186 | encode_ui, |
| 2187 | decode_ui, |
| 2188 | game_changed_state, |
| 2189 | interpret_move, |
| 2190 | execute_move, |
| 2191 | PREFERRED_TILESIZE, game_compute_size, game_set_size, |
| 2192 | game_colours, |
| 2193 | game_new_drawstate, |
| 2194 | game_free_drawstate, |
| 2195 | game_redraw, |
| 2196 | game_anim_length, |
| 2197 | game_flash_length, |
| 2198 | FALSE, FALSE, game_print_size, game_print, |
| 2199 | TRUE, /* wants_statusbar */ |
| 2200 | FALSE, game_timing_state, |
| 2201 | 0, /* flags */ |
| 2202 | }; |