2 * pegs.c: the classic Peg Solitaire game.
28 * Grid shapes. I do some macro ickery here to ensure that my enum
29 * and the various forms of my name list always match up.
32 A(CROSS,Cross,cross) \
33 A(OCTAGON,Octagon,octagon) \
34 A(RANDOM,Random,random)
35 #define ENUM(upper,title,lower) TYPE_ ## upper,
36 #define TITLE(upper,title,lower) #title,
37 #define LOWER(upper,title,lower) #lower,
38 #define CONFIG(upper,title,lower) ":" #title
40 enum { TYPELIST(ENUM
) TYPECOUNT
};
41 static char const *const pegs_titletypes
[] = { TYPELIST(TITLE
) };
42 static char const *const pegs_lowertypes
[] = { TYPELIST(LOWER
) };
43 #define TYPECONFIG TYPELIST(CONFIG)
45 #define FLASH_FRAME 0.13F
58 static game_params
*default_params(void)
60 game_params
*ret
= snew(game_params
);
63 ret
->type
= TYPE_CROSS
;
68 static const struct game_params pegs_presets
[] = {
76 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
81 if (i
< 0 || i
>= lenof(pegs_presets
))
84 ret
= snew(game_params
);
85 *ret
= pegs_presets
[i
];
87 strcpy(str
, pegs_titletypes
[ret
->type
]);
88 if (ret
->type
== TYPE_RANDOM
)
89 sprintf(str
+ strlen(str
), " %dx%d", ret
->w
, ret
->h
);
96 static void free_params(game_params
*params
)
101 static game_params
*dup_params(game_params
*params
)
103 game_params
*ret
= snew(game_params
);
104 *ret
= *params
; /* structure copy */
108 static void decode_params(game_params
*params
, char const *string
)
110 char const *p
= string
;
114 while (*p
&& isdigit((unsigned char)*p
)) p
++;
118 while (*p
&& isdigit((unsigned char)*p
)) p
++;
120 params
->h
= params
->w
;
123 for (i
= 0; i
< lenof(pegs_lowertypes
); i
++)
124 if (!strcmp(p
, pegs_lowertypes
[i
]))
128 static char *encode_params(game_params
*params
, int full
)
132 sprintf(str
, "%dx%d", params
->w
, params
->h
);
134 assert(params
->type
>= 0 && params
->type
< lenof(pegs_lowertypes
));
135 strcat(str
, pegs_lowertypes
[params
->type
]);
140 static config_item
*game_configure(game_params
*params
)
142 config_item
*ret
= snewn(4, config_item
);
145 ret
[0].name
= "Width";
146 ret
[0].type
= C_STRING
;
147 sprintf(buf
, "%d", params
->w
);
148 ret
[0].sval
= dupstr(buf
);
151 ret
[1].name
= "Height";
152 ret
[1].type
= C_STRING
;
153 sprintf(buf
, "%d", params
->h
);
154 ret
[1].sval
= dupstr(buf
);
157 ret
[2].name
= "Board type";
158 ret
[2].type
= C_CHOICES
;
159 ret
[2].sval
= TYPECONFIG
;
160 ret
[2].ival
= params
->type
;
170 static game_params
*custom_params(config_item
*cfg
)
172 game_params
*ret
= snew(game_params
);
174 ret
->w
= atoi(cfg
[0].sval
);
175 ret
->h
= atoi(cfg
[1].sval
);
176 ret
->type
= cfg
[2].ival
;
181 static char *validate_params(game_params
*params
)
183 if (params
->w
<= 3 || params
->h
<= 3)
184 return "Width and height must both be greater than three";
187 * It might be possible to implement generalisations of Cross
188 * and Octagon, but only if I can find a proof that they're all
189 * soluble. For the moment, therefore, I'm going to disallow
190 * them at any size other than the standard one.
192 if (params
->type
== TYPE_CROSS
|| params
->type
== TYPE_OCTAGON
) {
193 if (params
->w
!= 7 || params
->h
!= 7)
194 return "This board type is only supported at 7x7";
199 /* ----------------------------------------------------------------------
200 * Beginning of code to generate random Peg Solitaire boards.
202 * This procedure is done with no aesthetic judgment, no effort at
203 * symmetry, no difficulty grading and generally no finesse
204 * whatsoever. We simply begin with an empty board containing a
205 * single peg, and repeatedly make random reverse moves until it's
206 * plausibly full. This typically yields a scrappy haphazard mess
207 * with several holes, an uneven shape, and no redeeming features
208 * except guaranteed solubility.
210 * My only concessions to sophistication are (a) to repeat the
211 * generation process until I at least get a grid that touches
212 * every edge of the specified board size, and (b) to try when
213 * selecting moves to reuse existing space rather than expanding
214 * into new space (so that non-rectangular board shape becomes a
215 * factor during play).
220 * x,y are the start point of the move during generation (hence
221 * its endpoint during normal play).
223 * dx,dy are the direction of the move during generation.
224 * Absolute value 1. Hence, for example, x=3,y=5,dx=1,dy=0
225 * means that the move during generation starts at (3,5) and
226 * ends at (5,5), and vice versa during normal play.
230 * cost is 0, 1 or 2, depending on how many GRID_OBSTs we must
231 * turn into GRID_HOLEs to play this move.
236 static int movecmp(void *av
, void *bv
)
238 struct move
*a
= (struct move
*)av
;
239 struct move
*b
= (struct move
*)bv
;
243 else if (a
->y
> b
->y
)
248 else if (a
->x
> b
->x
)
253 else if (a
->dy
> b
->dy
)
258 else if (a
->dx
> b
->dx
)
264 static int movecmpcost(void *av
, void *bv
)
266 struct move
*a
= (struct move
*)av
;
267 struct move
*b
= (struct move
*)bv
;
269 if (a
->cost
< b
->cost
)
271 else if (a
->cost
> b
->cost
)
274 return movecmp(av
, bv
);
278 tree234
*bymove
, *bycost
;
281 static void update_moves(unsigned char *grid
, int w
, int h
, int x
, int y
,
282 struct movetrees
*trees
)
288 * There are twelve moves that can include (x,y): three in each
289 * of four directions. Check each one to see if it's possible.
291 for (dir
= 0; dir
< 4; dir
++) {
295 dx
= 0, dy
= dir
- 2;
297 dy
= 0, dx
= dir
- 1;
299 assert(abs(dx
) + abs(dy
) == 1);
301 for (pos
= 0; pos
< 3; pos
++) {
309 if (move
.x
< 0 || move
.x
>= w
|| move
.y
< 0 || move
.y
>= h
)
310 continue; /* completely invalid move */
311 if (move
.x
+2*move
.dx
< 0 || move
.x
+2*move
.dx
>= w
||
312 move
.y
+2*move
.dy
< 0 || move
.y
+2*move
.dy
>= h
)
313 continue; /* completely invalid move */
315 v1
= grid
[move
.y
* w
+ move
.x
];
316 v2
= grid
[(move
.y
+move
.dy
) * w
+ (move
.x
+move
.dx
)];
317 v3
= grid
[(move
.y
+2*move
.dy
)*w
+ (move
.x
+2*move
.dx
)];
318 if (v1
== GRID_PEG
&& v2
!= GRID_PEG
&& v3
!= GRID_PEG
) {
321 move
.cost
= (v2
== GRID_OBST
) + (v3
== GRID_OBST
);
324 * This move is possible. See if it's already in
327 m
= find234(trees
->bymove
, &move
, NULL
);
328 if (m
&& m
->cost
!= move
.cost
) {
330 * It's in the tree but listed with the wrong
331 * cost. Remove the old version.
333 #ifdef GENERATION_DIAGNOSTICS
334 printf("correcting %d%+d,%d%+d at cost %d\n",
335 m
->x
, m
->dx
, m
->y
, m
->dy
, m
->cost
);
337 del234(trees
->bymove
, m
);
338 del234(trees
->bycost
, m
);
344 m
= snew(struct move
);
346 m2
= add234(trees
->bymove
, m
);
347 m2
= add234(trees
->bycost
, m
);
349 #ifdef GENERATION_DIAGNOSTICS
350 printf("adding %d%+d,%d%+d at cost %d\n",
351 move
.x
, move
.dx
, move
.y
, move
.dy
, move
.cost
);
354 #ifdef GENERATION_DIAGNOSTICS
355 printf("not adding %d%+d,%d%+d at cost %d\n",
356 move
.x
, move
.dx
, move
.y
, move
.dy
, move
.cost
);
361 * This move is impossible. If it is already in the
364 * (We make use here of the fact that del234
365 * doesn't have to be passed a pointer to the
366 * _actual_ element it's deleting: it merely needs
367 * one that compares equal to it, and it will
368 * return the one it deletes.)
370 struct move
*m
= del234(trees
->bymove
, &move
);
371 #ifdef GENERATION_DIAGNOSTICS
372 printf("%sdeleting %d%+d,%d%+d\n", m ?
"" : "not ",
373 move
.x
, move
.dx
, move
.y
, move
.dy
);
376 del234(trees
->bycost
, m
);
384 static void pegs_genmoves(unsigned char *grid
, int w
, int h
, random_state
*rs
)
386 struct movetrees atrees
, *trees
= &atrees
;
390 trees
->bymove
= newtree234(movecmp
);
391 trees
->bycost
= newtree234(movecmpcost
);
393 for (y
= 0; y
< h
; y
++)
394 for (x
= 0; x
< w
; x
++)
395 if (grid
[y
*w
+x
] == GRID_PEG
)
396 update_moves(grid
, w
, h
, x
, y
, trees
);
401 int limit
, maxcost
, index
;
402 struct move mtmp
, move
, *m
;
405 * See how many moves we can make at zero cost. Make one,
406 * if possible. Failing that, make a one-cost move, and
407 * then a two-cost one.
409 * After filling at least half the input grid, we no longer
410 * accept cost-2 moves: if that's our only option, we give
414 maxcost
= (nmoves
< w
*h
/2 ?
2 : 1);
415 m
= NULL
; /* placate optimiser */
416 for (mtmp
.cost
= 0; mtmp
.cost
<= maxcost
; mtmp
.cost
++) {
418 m
= findrelpos234(trees
->bycost
, &mtmp
, NULL
, REL234_LT
, &limit
);
419 #ifdef GENERATION_DIAGNOSTICS
420 printf("%d moves available with cost %d\n", limit
+1, mtmp
.cost
);
428 index
= random_upto(rs
, limit
+1);
429 move
= *(struct move
*)index234(trees
->bycost
, index
);
431 #ifdef GENERATION_DIAGNOSTICS
432 printf("selecting move %d%+d,%d%+d at cost %d\n",
433 move
.x
, move
.dx
, move
.y
, move
.dy
, move
.cost
);
436 grid
[move
.y
* w
+ move
.x
] = GRID_HOLE
;
437 grid
[(move
.y
+move
.dy
) * w
+ (move
.x
+move
.dx
)] = GRID_PEG
;
438 grid
[(move
.y
+2*move
.dy
)*w
+ (move
.x
+2*move
.dx
)] = GRID_PEG
;
440 for (i
= 0; i
<= 2; i
++) {
441 int tx
= move
.x
+ i
*move
.dx
;
442 int ty
= move
.y
+ i
*move
.dy
;
443 update_moves(grid
, w
, h
, tx
, ty
, trees
);
449 while ((m
= delpos234(trees
->bymove
, 0)) != NULL
) {
450 del234(trees
->bycost
, m
);
453 freetree234(trees
->bymove
);
454 freetree234(trees
->bycost
);
457 static void pegs_generate(unsigned char *grid
, int w
, int h
, random_state
*rs
)
462 memset(grid
, GRID_OBST
, w
*h
);
463 grid
[(h
/2) * w
+ (w
/2)] = GRID_PEG
;
464 #ifdef GENERATION_DIAGNOSTICS
465 printf("beginning move selection\n");
467 pegs_genmoves(grid
, w
, h
, rs
);
468 #ifdef GENERATION_DIAGNOSTICS
469 printf("finished move selection\n");
473 for (y
= 0; y
< h
; y
++) {
474 if (grid
[y
*w
+0] != GRID_OBST
)
476 if (grid
[y
*w
+w
-1] != GRID_OBST
)
479 for (x
= 0; x
< w
; x
++) {
480 if (grid
[0*w
+x
] != GRID_OBST
)
482 if (grid
[(h
-1)*w
+x
] != GRID_OBST
)
488 #ifdef GENERATION_DIAGNOSTICS
489 printf("insufficient extent; trying again\n");
492 #ifdef GENERATION_DIAGNOSTICS
497 /* ----------------------------------------------------------------------
498 * End of board generation code. Now for the client code which uses
499 * it as part of the puzzle.
502 static char *new_game_desc(game_params
*params
, random_state
*rs
,
503 char **aux
, int interactive
)
505 int w
= params
->w
, h
= params
->h
;
510 grid
= snewn(w
*h
, unsigned char);
511 if (params
->type
== TYPE_RANDOM
) {
512 pegs_generate(grid
, w
, h
, rs
);
516 for (y
= 0; y
< h
; y
++)
517 for (x
= 0; x
< w
; x
++) {
518 v
= GRID_OBST
; /* placate optimiser */
519 switch (params
->type
) {
523 if (cx
== 0 && cy
== 0)
525 else if (cx
> 1 && cy
> 1)
533 if (cx
== 0 && cy
== 0)
535 else if (cx
+ cy
> 1 + max(w
,h
)/2)
546 * Encode a game description which is simply a long list of P
547 * for peg, H for hole or O for obstacle.
549 ret
= snewn(w
*h
+1, char);
550 for (i
= 0; i
< w
*h
; i
++)
551 ret
[i
] = (grid
[i
] == GRID_PEG ?
'P' :
552 grid
[i
] == GRID_HOLE ?
'H' : 'O');
560 static char *validate_desc(game_params
*params
, char *desc
)
562 int len
= params
->w
* params
->h
;
564 if (len
!= strlen(desc
))
565 return "Game description is wrong length";
566 if (len
!= strspn(desc
, "PHO"))
567 return "Invalid character in game description";
572 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
574 int w
= params
->w
, h
= params
->h
;
575 game_state
*state
= snew(game_state
);
580 state
->completed
= 0;
581 state
->grid
= snewn(w
*h
, unsigned char);
582 for (i
= 0; i
< w
*h
; i
++)
583 state
->grid
[i
] = (desc
[i
] == 'P' ? GRID_PEG
:
584 desc
[i
] == 'H' ? GRID_HOLE
: GRID_OBST
);
589 static game_state
*dup_game(game_state
*state
)
591 int w
= state
->w
, h
= state
->h
;
592 game_state
*ret
= snew(game_state
);
596 ret
->completed
= state
->completed
;
597 ret
->grid
= snewn(w
*h
, unsigned char);
598 memcpy(ret
->grid
, state
->grid
, w
*h
);
603 static void free_game(game_state
*state
)
609 static char *solve_game(game_state
*state
, game_state
*currstate
,
610 char *aux
, char **error
)
615 static char *game_text_format(game_state
*state
)
617 int w
= state
->w
, h
= state
->h
;
621 ret
= snewn((w
+1)*h
+ 1, char);
623 for (y
= 0; y
< h
; y
++) {
624 for (x
= 0; x
< w
; x
++)
625 ret
[y
*(w
+1)+x
] = (state
->grid
[y
*w
+x
] == GRID_HOLE ?
'-' :
626 state
->grid
[y
*w
+x
] == GRID_PEG ?
'*' : ' ');
627 ret
[y
*(w
+1)+w
] = '\n';
635 int dragging
; /* boolean: is a drag in progress? */
636 int sx
, sy
; /* grid coords of drag start cell */
637 int dx
, dy
; /* pixel coords of current drag posn */
640 static game_ui
*new_ui(game_state
*state
)
642 game_ui
*ui
= snew(game_ui
);
644 ui
->sx
= ui
->sy
= ui
->dx
= ui
->dy
= 0;
645 ui
->dragging
= FALSE
;
650 static void free_ui(game_ui
*ui
)
655 static char *encode_ui(game_ui
*ui
)
660 static void decode_ui(game_ui
*ui
, char *encoding
)
664 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
665 game_state
*newstate
)
668 * Cancel a drag, in case the source square has become
671 ui
->dragging
= FALSE
;
674 #define PREFERRED_TILE_SIZE 33
675 #define TILESIZE (ds->tilesize)
676 #define BORDER (TILESIZE / 2)
678 #define HIGHLIGHT_WIDTH (TILESIZE / 16)
680 #define COORD(x) ( BORDER + (x) * TILESIZE )
681 #define FROMCOORD(x) ( ((x) + TILESIZE - BORDER) / TILESIZE - 1 )
683 struct game_drawstate
{
685 blitter
*drag_background
;
686 int dragging
, dragx
, dragy
;
693 static char *interpret_move(game_state
*state
, game_ui
*ui
, game_drawstate
*ds
,
694 int x
, int y
, int button
)
696 int w
= state
->w
, h
= state
->h
;
698 if (button
== LEFT_BUTTON
) {
702 * Left button down: we attempt to start a drag.
706 * There certainly shouldn't be a current drag in progress,
707 * unless the midend failed to send us button events in
708 * order; it has a responsibility to always get that right,
709 * so we can legitimately punish it by failing an
712 assert(!ui
->dragging
);
716 if (tx
>= 0 && tx
< w
&& ty
>= 0 && ty
< h
&&
717 state
->grid
[ty
*w
+tx
] == GRID_PEG
) {
723 return ""; /* ui modified */
725 } else if (button
== LEFT_DRAG
&& ui
->dragging
) {
727 * Mouse moved; just move the peg being dragged.
731 return ""; /* ui modified */
732 } else if (button
== LEFT_RELEASE
&& ui
->dragging
) {
737 * Button released. Identify the target square of the drag,
738 * see if it represents a valid move, and if so make it.
740 ui
->dragging
= FALSE
; /* cancel the drag no matter what */
743 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
)
744 return ""; /* target out of range */
747 if (max(abs(dx
),abs(dy
)) != 2 || min(abs(dx
),abs(dy
)) != 0)
748 return ""; /* move length was wrong */
752 if (state
->grid
[ty
*w
+tx
] != GRID_HOLE
||
753 state
->grid
[(ty
-dy
)*w
+(tx
-dx
)] != GRID_PEG
||
754 state
->grid
[ui
->sy
*w
+ui
->sx
] != GRID_PEG
)
755 return ""; /* grid contents were invalid */
758 * We have a valid move. Encode it simply as source and
759 * destination coordinate pairs.
761 sprintf(buf
, "%d,%d-%d,%d", ui
->sx
, ui
->sy
, tx
, ty
);
767 static game_state
*execute_move(game_state
*state
, char *move
)
769 int w
= state
->w
, h
= state
->h
;
773 if (sscanf(move
, "%d,%d-%d,%d", &sx
, &sy
, &tx
, &ty
)) {
776 if (sx
< 0 || sx
>= w
|| sy
< 0 || sy
>= h
)
777 return NULL
; /* source out of range */
778 if (tx
< 0 || tx
>= w
|| ty
< 0 || ty
>= h
)
779 return NULL
; /* target out of range */
783 if (max(abs(dx
),abs(dy
)) != 2 || min(abs(dx
),abs(dy
)) != 0)
784 return NULL
; /* move length was wrong */
788 if (state
->grid
[sy
*w
+sx
] != GRID_PEG
||
789 state
->grid
[my
*w
+mx
] != GRID_PEG
||
790 state
->grid
[ty
*w
+tx
] != GRID_HOLE
)
791 return NULL
; /* grid contents were invalid */
793 ret
= dup_game(state
);
794 ret
->grid
[sy
*w
+sx
] = GRID_HOLE
;
795 ret
->grid
[my
*w
+mx
] = GRID_HOLE
;
796 ret
->grid
[ty
*w
+tx
] = GRID_PEG
;
799 * Opinion varies on whether getting to a single peg counts as
800 * completing the game, or whether that peg has to be at a
801 * specific location (central in the classic cross game, for
802 * instance). For now we take the former, rather lax position.
804 if (!ret
->completed
) {
806 for (i
= 0; i
< w
*h
; i
++)
807 if (ret
->grid
[i
] == GRID_PEG
)
818 /* ----------------------------------------------------------------------
822 static void game_compute_size(game_params
*params
, int tilesize
,
825 /* Ick: fake up `ds->tilesize' for macro expansion purposes */
826 struct { int tilesize
; } ads
, *ds
= &ads
;
827 ads
.tilesize
= tilesize
;
829 *x
= TILESIZE
* params
->w
+ 2 * BORDER
;
830 *y
= TILESIZE
* params
->h
+ 2 * BORDER
;
833 static void game_set_size(game_drawstate
*ds
, game_params
*params
,
836 ds
->tilesize
= tilesize
;
838 assert(TILESIZE
> 0);
840 if (ds
->drag_background
)
841 blitter_free(ds
->drag_background
);
842 ds
->drag_background
= blitter_new(TILESIZE
, TILESIZE
);
845 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
847 float *ret
= snewn(3 * NCOLOURS
, float);
851 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
854 * Drop the background colour so that the highlight is
855 * noticeably brighter than it while still being under 1.
857 max
= ret
[COL_BACKGROUND
*3];
858 for (i
= 1; i
< 3; i
++)
859 if (ret
[COL_BACKGROUND
*3+i
] > max
)
860 max
= ret
[COL_BACKGROUND
*3+i
];
861 if (max
* 1.2F
> 1.0F
) {
862 for (i
= 0; i
< 3; i
++)
863 ret
[COL_BACKGROUND
*3+i
] /= (max
* 1.2F
);
866 for (i
= 0; i
< 3; i
++) {
867 ret
[COL_HIGHLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 1.2F
;
868 ret
[COL_LOWLIGHT
* 3 + i
] = ret
[COL_BACKGROUND
* 3 + i
] * 0.8F
;
871 ret
[COL_PEG
* 3 + 0] = 0.0F
;
872 ret
[COL_PEG
* 3 + 1] = 0.0F
;
873 ret
[COL_PEG
* 3 + 2] = 1.0F
;
875 *ncolours
= NCOLOURS
;
879 static game_drawstate
*game_new_drawstate(game_state
*state
)
881 int w
= state
->w
, h
= state
->h
;
882 struct game_drawstate
*ds
= snew(struct game_drawstate
);
884 ds
->tilesize
= 0; /* not decided yet */
886 /* We can't allocate the blitter rectangle for the drag background
887 * until we know what size to make it. */
888 ds
->drag_background
= NULL
;
889 ds
->dragging
= FALSE
;
893 ds
->grid
= snewn(w
*h
, unsigned char);
894 memset(ds
->grid
, 255, w
*h
);
902 static void game_free_drawstate(game_drawstate
*ds
)
904 if (ds
->drag_background
)
905 blitter_free(ds
->drag_background
);
910 static void draw_tile(frontend
*fe
, game_drawstate
*ds
,
911 int x
, int y
, int v
, int bgcolour
)
914 draw_rect(fe
, x
, y
, TILESIZE
, TILESIZE
, bgcolour
);
917 if (v
== GRID_HOLE
) {
918 draw_circle(fe
, x
+TILESIZE
/2, y
+TILESIZE
/2, TILESIZE
/4,
919 COL_LOWLIGHT
, COL_LOWLIGHT
);
920 } else if (v
== GRID_PEG
) {
921 draw_circle(fe
, x
+TILESIZE
/2, y
+TILESIZE
/2, TILESIZE
/3,
925 draw_update(fe
, x
, y
, TILESIZE
, TILESIZE
);
928 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
929 game_state
*state
, int dir
, game_ui
*ui
,
930 float animtime
, float flashtime
)
932 int w
= state
->w
, h
= state
->h
;
937 int frame
= (int)(flashtime
/ FLASH_FRAME
);
938 bgcolour
= (frame
% 2 ? COL_LOWLIGHT
: COL_HIGHLIGHT
);
940 bgcolour
= COL_BACKGROUND
;
943 * Erase the sprite currently being dragged, if any.
946 assert(ds
->drag_background
);
947 blitter_load(fe
, ds
->drag_background
, ds
->dragx
, ds
->dragy
);
948 draw_update(fe
, ds
->dragx
, ds
->dragy
, TILESIZE
, TILESIZE
);
949 ds
->dragging
= FALSE
;
954 TILESIZE
* state
->w
+ 2 * BORDER
,
955 TILESIZE
* state
->h
+ 2 * BORDER
, COL_BACKGROUND
);
958 * Draw relief marks around all the squares that aren't
961 for (y
= 0; y
< h
; y
++)
962 for (x
= 0; x
< w
; x
++)
963 if (state
->grid
[y
*w
+x
] != GRID_OBST
) {
965 * First pass: draw the full relief square.
968 coords
[0] = COORD(x
+1) + HIGHLIGHT_WIDTH
- 1;
969 coords
[1] = COORD(y
) - HIGHLIGHT_WIDTH
;
970 coords
[2] = COORD(x
) - HIGHLIGHT_WIDTH
;
971 coords
[3] = COORD(y
+1) + HIGHLIGHT_WIDTH
- 1;
972 coords
[4] = COORD(x
) - HIGHLIGHT_WIDTH
;
973 coords
[5] = COORD(y
) - HIGHLIGHT_WIDTH
;
974 draw_polygon(fe
, coords
, 3, COL_HIGHLIGHT
, COL_HIGHLIGHT
);
975 coords
[4] = COORD(x
+1) + HIGHLIGHT_WIDTH
- 1;
976 coords
[5] = COORD(y
+1) + HIGHLIGHT_WIDTH
- 1;
977 draw_polygon(fe
, coords
, 3, COL_LOWLIGHT
, COL_LOWLIGHT
);
979 for (y
= 0; y
< h
; y
++)
980 for (x
= 0; x
< w
; x
++)
981 if (state
->grid
[y
*w
+x
] != GRID_OBST
) {
983 * Second pass: draw everything but the two
986 draw_rect(fe
, COORD(x
) - HIGHLIGHT_WIDTH
,
987 COORD(y
) - HIGHLIGHT_WIDTH
,
988 TILESIZE
+ HIGHLIGHT_WIDTH
,
989 TILESIZE
+ HIGHLIGHT_WIDTH
, COL_HIGHLIGHT
);
990 draw_rect(fe
, COORD(x
),
992 TILESIZE
+ HIGHLIGHT_WIDTH
,
993 TILESIZE
+ HIGHLIGHT_WIDTH
, COL_LOWLIGHT
);
995 for (y
= 0; y
< h
; y
++)
996 for (x
= 0; x
< w
; x
++)
997 if (state
->grid
[y
*w
+x
] != GRID_OBST
) {
999 * Third pass: draw a trapezium on each edge.
1002 int dx
, dy
, s
, sn
, c
;
1004 for (dx
= 0; dx
< 2; dx
++) {
1006 for (s
= 0; s
< 2; s
++) {
1008 c
= s ? COL_LOWLIGHT
: COL_HIGHLIGHT
;
1010 coords
[0] = COORD(x
) + (s
*dx
)*(TILESIZE
-1);
1011 coords
[1] = COORD(y
) + (s
*dy
)*(TILESIZE
-1);
1012 coords
[2] = COORD(x
) + (s
*dx
+dy
)*(TILESIZE
-1);
1013 coords
[3] = COORD(y
) + (s
*dy
+dx
)*(TILESIZE
-1);
1014 coords
[4] = coords
[2] - HIGHLIGHT_WIDTH
* (dy
-sn
*dx
);
1015 coords
[5] = coords
[3] - HIGHLIGHT_WIDTH
* (dx
-sn
*dy
);
1016 coords
[6] = coords
[0] + HIGHLIGHT_WIDTH
* (dy
+sn
*dx
);
1017 coords
[7] = coords
[1] + HIGHLIGHT_WIDTH
* (dx
+sn
*dy
);
1018 draw_polygon(fe
, coords
, 4, c
, c
);
1022 for (y
= 0; y
< h
; y
++)
1023 for (x
= 0; x
< w
; x
++)
1024 if (state
->grid
[y
*w
+x
] != GRID_OBST
) {
1026 * Second pass: draw everything but the two
1029 draw_rect(fe
, COORD(x
),
1032 TILESIZE
, COL_BACKGROUND
);
1037 draw_update(fe
, 0, 0,
1038 TILESIZE
* state
->w
+ 2 * BORDER
,
1039 TILESIZE
* state
->h
+ 2 * BORDER
);
1043 * Loop over the grid redrawing anything that looks as if it
1046 for (y
= 0; y
< h
; y
++)
1047 for (x
= 0; x
< w
; x
++) {
1050 v
= state
->grid
[y
*w
+x
];
1052 * Blank the source of a drag so it looks as if the
1053 * user picked the peg up physically.
1055 if (ui
->dragging
&& ui
->sx
== x
&& ui
->sy
== y
&& v
== GRID_PEG
)
1057 if (v
!= GRID_OBST
&&
1058 (bgcolour
!= ds
->bgcolour
|| /* always redraw when flashing */
1059 v
!= ds
->grid
[y
*w
+x
])) {
1060 draw_tile(fe
, ds
, COORD(x
), COORD(y
), v
, bgcolour
);
1065 * Draw the dragging sprite if any.
1068 ds
->dragging
= TRUE
;
1069 ds
->dragx
= ui
->dx
- TILESIZE
/2;
1070 ds
->dragy
= ui
->dy
- TILESIZE
/2;
1071 blitter_save(fe
, ds
->drag_background
, ds
->dragx
, ds
->dragy
);
1072 draw_tile(fe
, ds
, ds
->dragx
, ds
->dragy
, GRID_PEG
, -1);
1075 ds
->bgcolour
= bgcolour
;
1078 static float game_anim_length(game_state
*oldstate
, game_state
*newstate
,
1079 int dir
, game_ui
*ui
)
1084 static float game_flash_length(game_state
*oldstate
, game_state
*newstate
,
1085 int dir
, game_ui
*ui
)
1087 if (!oldstate
->completed
&& newstate
->completed
)
1088 return 2 * FLASH_FRAME
;
1093 static int game_wants_statusbar(void)
1098 static int game_timing_state(game_state
*state
)
1104 #define thegame pegs
1107 const struct game thegame
= {
1108 "Pegs", "games.pegs",
1115 TRUE
, game_configure
, custom_params
,
1123 TRUE
, game_text_format
,
1131 PREFERRED_TILE_SIZE
, game_compute_size
, game_set_size
,
1134 game_free_drawstate
,
1138 game_wants_statusbar
,
1139 FALSE
, game_timing_state
,
1140 0, /* mouse_priorities */