2 * inertia.c: Game involving navigating round a grid picking up
5 * Game rules and basic generator design by Ben Olmstead.
6 * This re-implementation was written by Simon Tatham.
18 /* Used in the game_state */
25 /* Used in the game IDs */
28 /* Used in the game generation */
31 /* Used only in the game_drawstate*/
35 #define DX(dir) ( (dir) & 3 ? (((dir) & 7) > 4 ? -1 : +1) : 0 )
36 #define DY(dir) ( DX((dir)+6) )
39 * Lvalue macro which expects x and y to be in range.
41 #define LV_AT(w, h, grid, x, y) ( (grid)[(y)*(w)+(x)] )
44 * Rvalue macro which can cope with x and y being out of range.
46 #define AT(w, h, grid, x, y) ( (x)<0 || (x)>=(w) || (y)<0 || (y)>=(h) ? \
47 WALL : LV_AT(w, h, grid, x, y) )
75 static game_params
*default_params(void)
77 game_params
*ret
= snew(game_params
);
85 static void free_params(game_params
*params
)
90 static game_params
*dup_params(game_params
*params
)
92 game_params
*ret
= snew(game_params
);
93 *ret
= *params
; /* structure copy */
97 static const struct game_params inertia_presets
[] = {
103 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
109 if (i
< 0 || i
>= lenof(inertia_presets
))
112 p
= inertia_presets
[i
];
113 ret
= dup_params(&p
);
114 sprintf(namebuf
, "%dx%d", ret
->w
, ret
->h
);
115 retname
= dupstr(namebuf
);
122 static void decode_params(game_params
*params
, char const *string
)
124 params
->w
= params
->h
= atoi(string
);
125 while (*string
&& isdigit((unsigned char)*string
)) string
++;
126 if (*string
== 'x') {
128 params
->h
= atoi(string
);
132 static char *encode_params(game_params
*params
, int full
)
136 sprintf(data
, "%dx%d", params
->w
, params
->h
);
141 static config_item
*game_configure(game_params
*params
)
146 ret
= snewn(3, config_item
);
148 ret
[0].name
= "Width";
149 ret
[0].type
= C_STRING
;
150 sprintf(buf
, "%d", params
->w
);
151 ret
[0].sval
= dupstr(buf
);
154 ret
[1].name
= "Height";
155 ret
[1].type
= C_STRING
;
156 sprintf(buf
, "%d", params
->h
);
157 ret
[1].sval
= dupstr(buf
);
168 static game_params
*custom_params(config_item
*cfg
)
170 game_params
*ret
= snew(game_params
);
172 ret
->w
= atoi(cfg
[0].sval
);
173 ret
->h
= atoi(cfg
[1].sval
);
178 static char *validate_params(game_params
*params
, int full
)
181 * Avoid completely degenerate cases which only have one
182 * row/column. We probably could generate completable puzzles
183 * of that shape, but they'd be forced to be extremely boring
184 * and at large sizes would take a while to happen upon at
187 if (params
->w
< 2 || params
->h
< 2)
188 return "Width and height must both be at least two";
191 * The grid construction algorithm creates 1/5 as many gems as
192 * grid squares, and must create at least one gem to have an
193 * actual puzzle. However, an area-five grid is ruled out by
194 * the above constraint, so the practical minimum is six.
196 if (params
->w
* params
->h
< 6)
197 return "Grid area must be at least six squares";
202 /* ----------------------------------------------------------------------
203 * Solver used by grid generator.
206 struct solver_scratch
{
207 unsigned char *reachable_from
, *reachable_to
;
211 static struct solver_scratch
*new_scratch(int w
, int h
)
213 struct solver_scratch
*sc
= snew(struct solver_scratch
);
215 sc
->reachable_from
= snewn(w
* h
* DIRECTIONS
, unsigned char);
216 sc
->reachable_to
= snewn(w
* h
* DIRECTIONS
, unsigned char);
217 sc
->positions
= snewn(w
* h
* DIRECTIONS
, int);
222 static void free_scratch(struct solver_scratch
*sc
)
227 static int can_go(int w
, int h
, char *grid
,
228 int x1
, int y1
, int dir1
, int x2
, int y2
, int dir2
)
231 * Returns TRUE if we can transition directly from (x1,y1)
232 * going in direction dir1, to (x2,y2) going in direction dir2.
236 * If we're actually in the middle of an unoccupyable square,
237 * we cannot make any move.
239 if (AT(w
, h
, grid
, x1
, y1
) == WALL
||
240 AT(w
, h
, grid
, x1
, y1
) == MINE
)
244 * If a move is capable of stopping at x1,y1,dir1, and x2,y2 is
245 * the same coordinate as x1,y1, then we can make the
246 * transition (by stopping and changing direction).
248 * For this to be the case, we have to either have a wall
249 * beyond x1,y1,dir1, or have a stop on x1,y1.
251 if (x2
== x1
&& y2
== y1
&&
252 (AT(w
, h
, grid
, x1
, y1
) == STOP
||
253 AT(w
, h
, grid
, x1
, y1
) == START
||
254 AT(w
, h
, grid
, x1
+DX(dir1
), y1
+DY(dir1
)) == WALL
))
258 * If a move is capable of continuing here, then x1,y1,dir1 can
259 * move one space further on.
261 if (x2
== x1
+DX(dir1
) && y2
== y1
+DY(dir1
) && dir1
== dir2
&&
262 (AT(w
, h
, grid
, x2
, y2
) == BLANK
||
263 AT(w
, h
, grid
, x2
, y2
) == GEM
||
264 AT(w
, h
, grid
, x2
, y2
) == STOP
||
265 AT(w
, h
, grid
, x2
, y2
) == START
))
274 static int find_gem_candidates(int w
, int h
, char *grid
,
275 struct solver_scratch
*sc
)
279 int sx
, sy
, gx
, gy
, gd
, pass
, possgems
;
282 * This function finds all the candidate gem squares, which are
283 * precisely those squares which can be picked up on a loop
284 * from the starting point back to the starting point. Doing
285 * this may involve passing through such a square in the middle
286 * of a move; so simple breadth-first search over the _squares_
287 * of the grid isn't quite adequate, because it might be that
288 * we can only reach a gem from the start by moving over it in
289 * one direction, but can only return to the start if we were
290 * moving over it in another direction.
292 * Instead, we BFS over a space which mentions each grid square
293 * eight times - once for each direction. We also BFS twice:
294 * once to find out what square+direction pairs we can reach
295 * _from_ the start point, and once to find out what pairs we
296 * can reach the start point from. Then a square is reachable
297 * if any of the eight directions for that square has both
301 memset(sc
->reachable_from
, 0, wh
* DIRECTIONS
);
302 memset(sc
->reachable_to
, 0, wh
* DIRECTIONS
);
305 * Find the starting square.
307 sx
= -1; /* placate optimiser */
308 for (sy
= 0; sy
< h
; sy
++) {
309 for (sx
= 0; sx
< w
; sx
++)
310 if (AT(w
, h
, grid
, sx
, sy
) == START
)
317 for (pass
= 0; pass
< 2; pass
++) {
318 unsigned char *reachable
= (pass
== 0 ? sc
->reachable_from
:
320 int sign
= (pass
== 0 ?
+1 : -1);
323 #ifdef SOLVER_DIAGNOSTICS
324 printf("starting pass %d\n", pass
);
328 * `head' and `tail' are indices within sc->positions which
329 * track the list of board positions left to process.
332 for (dir
= 0; dir
< DIRECTIONS
; dir
++) {
333 int index
= (sy
*w
+sx
)*DIRECTIONS
+dir
;
334 sc
->positions
[tail
++] = index
;
335 reachable
[index
] = TRUE
;
336 #ifdef SOLVER_DIAGNOSTICS
337 printf("starting point %d,%d,%d\n", sx
, sy
, dir
);
342 * Now repeatedly pick an element off the list and process
345 while (head
< tail
) {
346 int index
= sc
->positions
[head
++];
347 int dir
= index
% DIRECTIONS
;
348 int x
= (index
/ DIRECTIONS
) % w
;
349 int y
= index
/ (w
* DIRECTIONS
);
350 int n
, x2
, y2
, d2
, i2
;
352 #ifdef SOLVER_DIAGNOSTICS
353 printf("processing point %d,%d,%d\n", x
, y
, dir
);
356 * The places we attempt to switch to here are:
357 * - each possible direction change (all the other
358 * directions in this square)
359 * - one step further in the direction we're going (or
360 * one step back, if we're in the reachable_to pass).
362 for (n
= -1; n
< DIRECTIONS
; n
++) {
364 x2
= x
+ sign
* DX(dir
);
365 y2
= y
+ sign
* DY(dir
);
372 i2
= (y2
*w
+x2
)*DIRECTIONS
+d2
;
373 if (x2
>= 0 && x2
< w
&&
377 #ifdef SOLVER_DIAGNOSTICS
378 printf(" trying point %d,%d,%d", x2
, y2
, d2
);
381 ok
= can_go(w
, h
, grid
, x
, y
, dir
, x2
, y2
, d2
);
383 ok
= can_go(w
, h
, grid
, x2
, y2
, d2
, x
, y
, dir
);
384 #ifdef SOLVER_DIAGNOSTICS
385 printf(" - %sok\n", ok ?
"" : "not ");
388 sc
->positions
[tail
++] = i2
;
389 reachable
[i2
] = TRUE
;
397 * And that should be it. Now all we have to do is find the
398 * squares for which there exists _some_ direction such that
399 * the square plus that direction form a tuple which is both
400 * reachable from the start and reachable to the start.
403 for (gy
= 0; gy
< h
; gy
++)
404 for (gx
= 0; gx
< w
; gx
++)
405 if (AT(w
, h
, grid
, gx
, gy
) == BLANK
) {
406 for (gd
= 0; gd
< DIRECTIONS
; gd
++) {
407 int index
= (gy
*w
+gx
)*DIRECTIONS
+gd
;
408 if (sc
->reachable_from
[index
] && sc
->reachable_to
[index
]) {
409 #ifdef SOLVER_DIAGNOSTICS
410 printf("space at %d,%d is reachable via"
411 " direction %d\n", gx
, gy
, gd
);
413 LV_AT(w
, h
, grid
, gx
, gy
) = POSSGEM
;
423 /* ----------------------------------------------------------------------
424 * Grid generation code.
427 static char *gengrid(int w
, int h
, random_state
*rs
)
430 char *grid
= snewn(wh
+1, char);
431 struct solver_scratch
*sc
= new_scratch(w
, h
);
432 int maxdist_threshold
, tries
;
434 maxdist_threshold
= 2;
440 int *dist
, *list
, head
, tail
, maxdist
;
443 * We're going to fill the grid with the five basic piece
444 * types in about 1/5 proportion. For the moment, though,
445 * we leave out the gems, because we'll put those in
446 * _after_ we run the solver to tell us where the viable
450 for (j
= 0; j
< wh
/5; j
++)
452 for (j
= 0; j
< wh
/5; j
++)
454 for (j
= 0; j
< wh
/5; j
++)
460 shuffle(grid
, wh
, sizeof(*grid
), rs
);
463 * Find the viable gem locations, and immediately give up
464 * and try again if there aren't enough of them.
466 possgems
= find_gem_candidates(w
, h
, grid
, sc
);
471 * We _could_ now select wh/5 of the POSSGEMs and set them
472 * to GEM, and have a viable level. However, there's a
473 * chance that a large chunk of the level will turn out to
474 * be unreachable, so first we test for that.
476 * We do this by finding the largest distance from any
477 * square to the nearest POSSGEM, by breadth-first search.
478 * If this is above a critical threshold, we abort and try
481 * (This search is purely geometric, without regard to
482 * walls and long ways round.)
484 dist
= sc
->positions
;
485 list
= sc
->positions
+ wh
;
486 for (i
= 0; i
< wh
; i
++)
489 for (i
= 0; i
< wh
; i
++)
490 if (grid
[i
] == POSSGEM
) {
495 while (head
< tail
) {
499 if (maxdist
< dist
[pos
])
505 for (d
= 0; d
< DIRECTIONS
; d
++) {
511 if (x2
>= 0 && x2
< w
&& y2
>= 0 && y2
< h
) {
514 dist
[p2
] = dist
[pos
] + 1;
520 assert(head
== wh
&& tail
== wh
);
523 * Now abandon this grid and go round again if maxdist is
524 * above the required threshold.
526 * We can safely start the threshold as low as 2. As we
527 * accumulate failed generation attempts, we gradually
528 * raise it as we get more desperate.
530 if (maxdist
> maxdist_threshold
) {
540 * Now our reachable squares are plausibly evenly
541 * distributed over the grid. I'm not actually going to
542 * _enforce_ that I place the gems in such a way as not to
543 * increase that maxdist value; I'm now just going to trust
544 * to the RNG to pick a sensible subset of the POSSGEMs.
547 for (i
= 0; i
< wh
; i
++)
548 if (grid
[i
] == POSSGEM
)
550 shuffle(list
, j
, sizeof(*list
), rs
);
551 for (i
= 0; i
< j
; i
++)
552 grid
[list
[i
]] = (i
< wh
/5 ? GEM
: BLANK
);
563 static char *new_game_desc(game_params
*params
, random_state
*rs
,
564 char **aux
, int interactive
)
566 return gengrid(params
->w
, params
->h
, rs
);
569 static char *validate_desc(game_params
*params
, char *desc
)
571 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
572 int starts
= 0, gems
= 0, i
;
574 for (i
= 0; i
< wh
; i
++) {
576 return "Not enough data to fill grid";
577 if (desc
[i
] != WALL
&& desc
[i
] != START
&& desc
[i
] != STOP
&&
578 desc
[i
] != GEM
&& desc
[i
] != MINE
&& desc
[i
] != BLANK
)
579 return "Unrecognised character in game description";
580 if (desc
[i
] == START
)
586 return "Too much data to fill grid";
588 return "No starting square specified";
590 return "More than one starting square specified";
592 return "No gems specified";
597 static game_state
*new_game(midend
*me
, game_params
*params
, char *desc
)
599 int w
= params
->w
, h
= params
->h
, wh
= w
*h
;
601 game_state
*state
= snew(game_state
);
603 state
->p
= *params
; /* structure copy */
605 state
->grid
= snewn(wh
, char);
606 assert(strlen(desc
) == wh
);
607 memcpy(state
->grid
, desc
, wh
);
609 state
->px
= state
->py
= -1;
611 for (i
= 0; i
< wh
; i
++) {
612 if (state
->grid
[i
] == START
) {
613 state
->grid
[i
] = STOP
;
616 } else if (state
->grid
[i
] == GEM
) {
621 assert(state
->gems
> 0);
622 assert(state
->px
>= 0 && state
->py
>= 0);
624 state
->distance_moved
= 0;
630 static game_state
*dup_game(game_state
*state
)
632 int w
= state
->p
.w
, h
= state
->p
.h
, wh
= w
*h
;
633 game_state
*ret
= snew(game_state
);
638 ret
->gems
= state
->gems
;
639 ret
->grid
= snewn(wh
, char);
640 ret
->distance_moved
= state
->distance_moved
;
642 memcpy(ret
->grid
, state
->grid
, wh
);
647 static void free_game(game_state
*state
)
653 static char *solve_game(game_state
*state
, game_state
*currstate
,
654 char *aux
, char **error
)
659 static char *game_text_format(game_state
*state
)
672 static game_ui
*new_ui(game_state
*state
)
674 game_ui
*ui
= snew(game_ui
);
675 ui
->anim_length
= 0.0F
;
678 ui
->just_made_move
= FALSE
;
679 ui
->just_died
= FALSE
;
683 static void free_ui(game_ui
*ui
)
688 static char *encode_ui(game_ui
*ui
)
692 * The deaths counter needs preserving across a serialisation.
694 sprintf(buf
, "D%d", ui
->deaths
);
698 static void decode_ui(game_ui
*ui
, char *encoding
)
701 sscanf(encoding
, "D%d%n", &ui
->deaths
, &p
);
704 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
705 game_state
*newstate
)
708 * Increment the deaths counter. We only do this if
709 * ui->just_made_move is set (redoing a suicide move doesn't
710 * kill you _again_), and also we only do it if the game isn't
711 * completed (once you're finished, you can play).
713 if (!oldstate
->dead
&& newstate
->dead
&& ui
->just_made_move
&&
716 ui
->just_died
= TRUE
;
718 ui
->just_died
= FALSE
;
720 ui
->just_made_move
= FALSE
;
723 struct game_drawstate
{
727 unsigned short *grid
;
728 blitter
*player_background
;
729 int player_bg_saved
, pbgx
, pbgy
;
732 #define PREFERRED_TILESIZE 32
733 #define TILESIZE (ds->tilesize)
734 #define BORDER (TILESIZE)
735 #define HIGHLIGHT_WIDTH (TILESIZE / 10)
736 #define COORD(x) ( (x) * TILESIZE + BORDER )
737 #define FROMCOORD(x) ( ((x) - BORDER + TILESIZE) / TILESIZE - 1 )
739 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
740 int x
, int y
, int button
)
742 int w
= state
->p
.w
, h
= state
->p
.h
/*, wh = w*h */;
748 if (button
== LEFT_BUTTON
) {
750 * Mouse-clicking near the target point (or, more
751 * accurately, in the appropriate octant) is an alternative
752 * way to input moves.
755 if (FROMCOORD(x
) != state
->px
|| FROMCOORD(y
) != state
->py
) {
759 dx
= FROMCOORD(x
) - state
->px
;
760 dy
= FROMCOORD(y
) - state
->py
;
761 /* I pass dx,dy rather than dy,dx so that the octants
762 * end up the right way round. */
763 angle
= atan2(dx
, -dy
);
765 angle
= (angle
+ (PI
/8)) / (PI
/4);
766 assert(angle
> -16.0F
);
767 dir
= (int)(angle
+ 16.0F
) & 7;
769 } else if (button
== CURSOR_UP
|| button
== (MOD_NUM_KEYPAD
| '8'))
771 else if (button
== CURSOR_DOWN
|| button
== (MOD_NUM_KEYPAD
| '2'))
773 else if (button
== CURSOR_LEFT
|| button
== (MOD_NUM_KEYPAD
| '4'))
775 else if (button
== CURSOR_RIGHT
|| button
== (MOD_NUM_KEYPAD
| '6'))
777 else if (button
== (MOD_NUM_KEYPAD
| '7'))
779 else if (button
== (MOD_NUM_KEYPAD
| '1'))
781 else if (button
== (MOD_NUM_KEYPAD
| '9'))
783 else if (button
== (MOD_NUM_KEYPAD
| '3'))
790 * Reject the move if we can't make it at all due to a wall
793 if (AT(w
, h
, state
->grid
, state
->px
+DX(dir
), state
->py
+DY(dir
)) == WALL
)
797 * Reject the move if we're dead!
803 * Otherwise, we can make the move. All we need to specify is
806 ui
->just_made_move
= TRUE
;
807 sprintf(buf
, "%d", dir
);
811 static game_state
*execute_move(game_state
*state
, char *move
)
813 int w
= state
->p
.w
, h
= state
->p
.h
/*, wh = w*h */;
814 int dir
= atoi(move
);
817 if (dir
< 0 || dir
>= DIRECTIONS
)
818 return NULL
; /* huh? */
823 if (AT(w
, h
, state
->grid
, state
->px
+DX(dir
), state
->py
+DY(dir
)) == WALL
)
824 return NULL
; /* wall in the way! */
829 ret
= dup_game(state
);
830 ret
->distance_moved
= 0;
834 ret
->distance_moved
++;
836 if (AT(w
, h
, ret
->grid
, ret
->px
, ret
->py
) == GEM
) {
837 LV_AT(w
, h
, ret
->grid
, ret
->px
, ret
->py
) = BLANK
;
841 if (AT(w
, h
, ret
->grid
, ret
->px
, ret
->py
) == MINE
) {
846 if (AT(w
, h
, ret
->grid
, ret
->px
, ret
->py
) == STOP
||
847 AT(w
, h
, ret
->grid
, ret
->px
+DX(dir
),
848 ret
->py
+DY(dir
)) == WALL
)
855 /* ----------------------------------------------------------------------
859 static void game_compute_size(game_params
*params
, int tilesize
,
862 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
863 struct { int tilesize
; } ads
, *ds
= &ads
;
864 ads
.tilesize
= tilesize
;
866 *x
= 2 * BORDER
+ 1 + params
->w
* TILESIZE
;
867 *y
= 2 * BORDER
+ 1 + params
->h
* TILESIZE
;
870 static void game_set_size(drawing
*dr
, game_drawstate
*ds
,
871 game_params
*params
, int tilesize
)
873 ds
->tilesize
= tilesize
;
875 assert(!ds
->player_bg_saved
);
877 if (ds
->player_background
)
878 blitter_free(dr
, ds
->player_background
);
879 ds
->player_background
= blitter_new(dr
, TILESIZE
, TILESIZE
);
882 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
884 float *ret
= snewn(3 * NCOLOURS
, float);
887 game_mkhighlight(fe
, ret
, COL_BACKGROUND
, COL_HIGHLIGHT
, COL_LOWLIGHT
);
889 ret
[COL_OUTLINE
* 3 + 0] = 0.0F
;
890 ret
[COL_OUTLINE
* 3 + 1] = 0.0F
;
891 ret
[COL_OUTLINE
* 3 + 2] = 0.0F
;
893 ret
[COL_PLAYER
* 3 + 0] = 0.0F
;
894 ret
[COL_PLAYER
* 3 + 1] = 1.0F
;
895 ret
[COL_PLAYER
* 3 + 2] = 0.0F
;
897 ret
[COL_DEAD_PLAYER
* 3 + 0] = 1.0F
;
898 ret
[COL_DEAD_PLAYER
* 3 + 1] = 0.0F
;
899 ret
[COL_DEAD_PLAYER
* 3 + 2] = 0.0F
;
901 ret
[COL_MINE
* 3 + 0] = 0.0F
;
902 ret
[COL_MINE
* 3 + 1] = 0.0F
;
903 ret
[COL_MINE
* 3 + 2] = 0.0F
;
905 ret
[COL_GEM
* 3 + 0] = 0.6F
;
906 ret
[COL_GEM
* 3 + 1] = 1.0F
;
907 ret
[COL_GEM
* 3 + 2] = 1.0F
;
909 for (i
= 0; i
< 3; i
++) {
910 ret
[COL_WALL
* 3 + i
] = (3 * ret
[COL_BACKGROUND
* 3 + i
] +
911 1 * ret
[COL_HIGHLIGHT
* 3 + i
]) / 4;
914 *ncolours
= NCOLOURS
;
918 static game_drawstate
*game_new_drawstate(drawing
*dr
, game_state
*state
)
920 int w
= state
->p
.w
, h
= state
->p
.h
, wh
= w
*h
;
921 struct game_drawstate
*ds
= snew(struct game_drawstate
);
926 /* We can't allocate the blitter rectangle for the player background
927 * until we know what size to make it. */
928 ds
->player_background
= NULL
;
929 ds
->player_bg_saved
= FALSE
;
930 ds
->pbgx
= ds
->pbgy
= -1;
932 ds
->p
= state
->p
; /* structure copy */
934 ds
->grid
= snewn(wh
, unsigned short);
935 for (i
= 0; i
< wh
; i
++)
936 ds
->grid
[i
] = UNDRAWN
;
941 static void game_free_drawstate(drawing
*dr
, game_drawstate
*ds
)
947 static void draw_player(drawing
*dr
, game_drawstate
*ds
, int x
, int y
,
951 int coords
[DIRECTIONS
*4];
954 for (d
= 0; d
< DIRECTIONS
; d
++) {
955 float x1
, y1
, x2
, y2
, x3
, y3
, len
;
959 len
= sqrt(x1
*x1
+y1
*y1
); x1
/= len
; y1
/= len
;
963 len
= sqrt(x3
*x3
+y3
*y3
); x3
/= len
; y3
/= len
;
968 coords
[d
*4+0] = x
+ TILESIZE
/2 + (int)((TILESIZE
*3/7) * x1
);
969 coords
[d
*4+1] = y
+ TILESIZE
/2 + (int)((TILESIZE
*3/7) * y1
);
970 coords
[d
*4+2] = x
+ TILESIZE
/2 + (int)((TILESIZE
*3/7) * x2
);
971 coords
[d
*4+3] = y
+ TILESIZE
/2 + (int)((TILESIZE
*3/7) * y2
);
973 draw_polygon(dr
, coords
, DIRECTIONS
*2, COL_DEAD_PLAYER
, COL_OUTLINE
);
975 draw_circle(dr
, x
+ TILESIZE
/2, y
+ TILESIZE
/2,
976 TILESIZE
/3, COL_PLAYER
, COL_OUTLINE
);
978 draw_update(dr
, x
, y
, TILESIZE
, TILESIZE
);
981 #define FLASH_DEAD 0x100
982 #define FLASH_WIN 0x200
983 #define FLASH_MASK 0x300
985 static void draw_tile(drawing
*dr
, game_drawstate
*ds
, int x
, int y
, int v
)
987 int tx
= COORD(x
), ty
= COORD(y
);
988 int bg
= (v
& FLASH_DEAD ? COL_DEAD_PLAYER
:
989 v
& FLASH_WIN ? COL_HIGHLIGHT
: COL_BACKGROUND
);
993 clip(dr
, tx
+1, ty
+1, TILESIZE
-1, TILESIZE
-1);
994 draw_rect(dr
, tx
+1, ty
+1, TILESIZE
-1, TILESIZE
-1, bg
);
999 coords
[0] = tx
+ TILESIZE
;
1000 coords
[1] = ty
+ TILESIZE
;
1001 coords
[2] = tx
+ TILESIZE
;
1004 coords
[5] = ty
+ TILESIZE
;
1005 draw_polygon(dr
, coords
, 3, COL_LOWLIGHT
, COL_LOWLIGHT
);
1009 draw_polygon(dr
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
1011 draw_rect(dr
, tx
+ 1 + HIGHLIGHT_WIDTH
, ty
+ 1 + HIGHLIGHT_WIDTH
,
1012 TILESIZE
- 2*HIGHLIGHT_WIDTH
,
1013 TILESIZE
- 2*HIGHLIGHT_WIDTH
, COL_WALL
);
1014 } else if (v
== MINE
) {
1015 int cx
= tx
+ TILESIZE
/ 2;
1016 int cy
= ty
+ TILESIZE
/ 2;
1017 int r
= TILESIZE
/ 2 - 3;
1019 int xdx
= 1, xdy
= 0, ydx
= 0, ydy
= 1;
1022 for (i
= 0; i
< 4*5*2; i
+= 5*2) {
1023 coords
[i
+2*0+0] = cx
- r
/6*xdx
+ r
*4/5*ydx
;
1024 coords
[i
+2*0+1] = cy
- r
/6*xdy
+ r
*4/5*ydy
;
1025 coords
[i
+2*1+0] = cx
- r
/6*xdx
+ r
*ydx
;
1026 coords
[i
+2*1+1] = cy
- r
/6*xdy
+ r
*ydy
;
1027 coords
[i
+2*2+0] = cx
+ r
/6*xdx
+ r
*ydx
;
1028 coords
[i
+2*2+1] = cy
+ r
/6*xdy
+ r
*ydy
;
1029 coords
[i
+2*3+0] = cx
+ r
/6*xdx
+ r
*4/5*ydx
;
1030 coords
[i
+2*3+1] = cy
+ r
/6*xdy
+ r
*4/5*ydy
;
1031 coords
[i
+2*4+0] = cx
+ r
*3/5*xdx
+ r
*3/5*ydx
;
1032 coords
[i
+2*4+1] = cy
+ r
*3/5*xdy
+ r
*3/5*ydy
;
1042 draw_polygon(dr
, coords
, 5*4, COL_MINE
, COL_MINE
);
1044 draw_rect(dr
, cx
-r
/3, cy
-r
/3, r
/3, r
/4, COL_HIGHLIGHT
);
1045 } else if (v
== STOP
) {
1046 draw_circle(dr
, tx
+ TILESIZE
/2, ty
+ TILESIZE
/2,
1047 TILESIZE
*3/7, -1, COL_OUTLINE
);
1048 draw_rect(dr
, tx
+ TILESIZE
*3/7, ty
+1,
1049 TILESIZE
- 2*(TILESIZE
*3/7) + 1, TILESIZE
-1, bg
);
1050 draw_rect(dr
, tx
+1, ty
+ TILESIZE
*3/7,
1051 TILESIZE
-1, TILESIZE
- 2*(TILESIZE
*3/7) + 1, bg
);
1052 } else if (v
== GEM
) {
1055 coords
[0] = tx
+TILESIZE
/2;
1056 coords
[1] = ty
+TILESIZE
*1/7;
1057 coords
[2] = tx
+TILESIZE
*1/7;
1058 coords
[3] = ty
+TILESIZE
/2;
1059 coords
[4] = tx
+TILESIZE
/2;
1060 coords
[5] = ty
+TILESIZE
-TILESIZE
*1/7;
1061 coords
[6] = tx
+TILESIZE
-TILESIZE
*1/7;
1062 coords
[7] = ty
+TILESIZE
/2;
1064 draw_polygon(dr
, coords
, 4, COL_GEM
, COL_OUTLINE
);
1068 draw_update(dr
, tx
, ty
, TILESIZE
, TILESIZE
);
1071 #define BASE_ANIM_LENGTH 0.1F
1072 #define FLASH_LENGTH 0.3F
1074 static void game_redraw(drawing
*dr
, game_drawstate
*ds
, game_state
*oldstate
,
1075 game_state
*state
, int dir
, game_ui
*ui
,
1076 float animtime
, float flashtime
)
1078 int w
= state
->p
.w
, h
= state
->p
.h
/*, wh = w*h */;
1087 !((int)(flashtime
* 3 / FLASH_LENGTH
) % 2))
1088 flashtype
= ui
->flashtype
;
1093 * Erase the player sprite.
1095 if (ds
->player_bg_saved
) {
1096 assert(ds
->player_background
);
1097 blitter_load(dr
, ds
->player_background
, ds
->pbgx
, ds
->pbgy
);
1098 draw_update(dr
, ds
->pbgx
, ds
->pbgy
, TILESIZE
, TILESIZE
);
1099 ds
->player_bg_saved
= FALSE
;
1103 * Initialise a fresh drawstate.
1109 * Blank out the window initially.
1111 game_compute_size(&ds
->p
, TILESIZE
, &wid
, &ht
);
1112 draw_rect(dr
, 0, 0, wid
, ht
, COL_BACKGROUND
);
1113 draw_update(dr
, 0, 0, wid
, ht
);
1116 * Draw the grid lines.
1118 for (y
= 0; y
<= h
; y
++)
1119 draw_line(dr
, COORD(0), COORD(y
), COORD(w
), COORD(y
),
1121 for (x
= 0; x
<= w
; x
++)
1122 draw_line(dr
, COORD(x
), COORD(0), COORD(x
), COORD(h
),
1129 * If we're in the process of animating a move, let's start by
1130 * working out how far the player has moved from their _older_
1134 ap
= animtime
/ ui
->anim_length
;
1135 player_dist
= ap
* (dir
> 0 ? state
: oldstate
)->distance_moved
;
1142 * Draw the grid contents.
1144 * We count the gems as we go round this loop, for the purposes
1145 * of the status bar. Of course we have a gems counter in the
1146 * game_state already, but if we do the counting in this loop
1147 * then it tracks gems being picked up in a sliding move, and
1148 * updates one by one.
1151 for (y
= 0; y
< h
; y
++)
1152 for (x
= 0; x
< w
; x
++) {
1153 unsigned short v
= (unsigned char)state
->grid
[y
*w
+x
];
1156 * Special case: if the player is in the process of
1157 * moving over a gem, we draw the gem iff they haven't
1160 if (oldstate
&& oldstate
->grid
[y
*w
+x
] != state
->grid
[y
*w
+x
]) {
1162 * Compute the distance from this square to the
1163 * original player position.
1165 int dist
= max(abs(x
- oldstate
->px
), abs(y
- oldstate
->py
));
1168 * If the player has reached here, use the new grid
1169 * element. Otherwise use the old one.
1171 if (player_dist
< dist
)
1172 v
= oldstate
->grid
[y
*w
+x
];
1174 v
= state
->grid
[y
*w
+x
];
1178 * Special case: erase the mine the dead player is
1179 * sitting on. Only at the end of the move.
1181 if (v
== MINE
&& !oldstate
&& state
->dead
&&
1182 x
== state
->px
&& y
== state
->py
)
1190 if (ds
->grid
[y
*w
+x
] != v
) {
1191 draw_tile(dr
, ds
, x
, y
, v
);
1192 ds
->grid
[y
*w
+x
] = v
;
1197 * Gem counter in the status bar. We replace it with
1198 * `COMPLETED!' when it reaches zero ... or rather, when the
1199 * _current state_'s gem counter is zero. (Thus, `Gems: 0' is
1200 * shown between the collection of the last gem and the
1201 * completion of the move animation that did it.)
1203 if (state
->dead
&& (!oldstate
|| oldstate
->dead
))
1204 sprintf(status
, "DEAD!");
1205 else if (state
->gems
|| (oldstate
&& oldstate
->gems
))
1206 sprintf(status
, "Gems: %d", gems
);
1208 sprintf(status
, "COMPLETED!");
1209 /* We subtract one from the visible death counter if we're still
1210 * animating the move at the end of which the death took place. */
1211 deaths
= ui
->deaths
;
1212 if (oldstate
&& ui
->just_died
) {
1217 sprintf(status
+ strlen(status
), " Deaths: %d", deaths
);
1218 status_bar(dr
, status
);
1221 * Draw the player sprite.
1223 assert(!ds
->player_bg_saved
);
1224 assert(ds
->player_background
);
1227 nx
= COORD(state
->px
);
1228 ny
= COORD(state
->py
);
1230 ox
= COORD(oldstate
->px
);
1231 oy
= COORD(oldstate
->py
);
1236 ds
->pbgx
= ox
+ ap
* (nx
- ox
);
1237 ds
->pbgy
= oy
+ ap
* (ny
- oy
);
1239 blitter_save(dr
, ds
->player_background
, ds
->pbgx
, ds
->pbgy
);
1240 draw_player(dr
, ds
, ds
->pbgx
, ds
->pbgy
, (state
->dead
&& !oldstate
));
1241 ds
->player_bg_saved
= TRUE
;
1244 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1245 int dir
, game_ui
*ui
)
1249 dist
= newstate
->distance_moved
;
1251 dist
= oldstate
->distance_moved
;
1252 ui
->anim_length
= sqrt(dist
) * BASE_ANIM_LENGTH
;
1253 return ui
->anim_length
;
1256 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1257 int dir
, game_ui
*ui
)
1259 if (!oldstate
->dead
&& newstate
->dead
) {
1260 ui
->flashtype
= FLASH_DEAD
;
1261 return FLASH_LENGTH
;
1262 } else if (oldstate
->gems
&& !newstate
->gems
) {
1263 ui
->flashtype
= FLASH_WIN
;
1264 return FLASH_LENGTH
;
1269 static int game_wants_statusbar(void)
1274 static int game_timing_state(game_state
*state
, game_ui
*ui
)
1279 static void game_print_size(game_params
*params
, float *x
, float *y
)
1283 static void game_print(drawing
*dr
, game_state
*state
, int tilesize
)
1288 #define thegame inertia
1291 const struct game thegame
= {
1292 "Inertia", "games.inertia",
1299 TRUE
, game_configure
, custom_params
,
1307 FALSE
, game_text_format
,
1315 PREFERRED_TILESIZE
, game_compute_size
, game_set_size
,
1318 game_free_drawstate
,
1322 FALSE
, FALSE
, game_print_size
, game_print
,
1323 game_wants_statusbar
,
1324 FALSE
, game_timing_state
,
1325 0, /* mouse_priorities */