13 const char *const game_name
= "Cube";
15 #define MAXVERTICES 20
20 float vertices
[MAXVERTICES
* 3]; /* 3*npoints coordinates */
23 int faces
[MAXFACES
* MAXORDER
]; /* order*nfaces point indices */
24 float normals
[MAXFACES
* 3]; /* 3*npoints vector components */
25 float shear
; /* isometric shear for nice drawing */
26 float border
; /* border required around arena */
29 static const struct solid tetrahedron
= {
32 0.0F
, -0.57735026919F
, -0.20412414523F
,
33 -0.5F
, 0.28867513459F
, -0.20412414523F
,
34 0.0F
, -0.0F
, 0.6123724357F
,
35 0.5F
, 0.28867513459F
, -0.20412414523F
,
39 0,2,1, 3,1,2, 2,0,3, 1,3,0
42 -0.816496580928F
, -0.471404520791F
, 0.333333333334F
,
43 0.0F
, 0.942809041583F
, 0.333333333333F
,
44 0.816496580928F
, -0.471404520791F
, 0.333333333334F
,
50 static const struct solid cube
= {
53 -0.5F
,-0.5F
,-0.5F
, -0.5F
,-0.5F
,+0.5F
,
54 -0.5F
,+0.5F
,-0.5F
, -0.5F
,+0.5F
,+0.5F
,
55 +0.5F
,-0.5F
,-0.5F
, +0.5F
,-0.5F
,+0.5F
,
56 +0.5F
,+0.5F
,-0.5F
, +0.5F
,+0.5F
,+0.5F
,
60 0,1,3,2, 1,5,7,3, 5,4,6,7, 4,0,2,6, 0,4,5,1, 3,7,6,2
63 -1.0F
,0.0F
,0.0F
, 0.0F
,0.0F
,+1.0F
,
64 +1.0F
,0.0F
,0.0F
, 0.0F
,0.0F
,-1.0F
,
65 0.0F
,-1.0F
,0.0F
, 0.0F
,+1.0F
,0.0F
70 static const struct solid octahedron
= {
73 -0.5F
, -0.28867513459472505F
, 0.4082482904638664F
,
74 0.5F
, 0.28867513459472505F
, -0.4082482904638664F
,
75 -0.5F
, 0.28867513459472505F
, -0.4082482904638664F
,
76 0.5F
, -0.28867513459472505F
, 0.4082482904638664F
,
77 0.0F
, -0.57735026918945009F
, -0.4082482904638664F
,
78 0.0F
, 0.57735026918945009F
, 0.4082482904638664F
,
82 4,0,2, 0,5,2, 0,4,3, 5,0,3, 1,4,2, 5,1,2, 4,1,3, 1,5,3
85 -0.816496580928F
, -0.471404520791F
, -0.333333333334F
,
86 -0.816496580928F
, 0.471404520791F
, 0.333333333334F
,
87 0.0F
, -0.942809041583F
, 0.333333333333F
,
90 0.0F
, 0.942809041583F
, -0.333333333333F
,
91 0.816496580928F
, -0.471404520791F
, -0.333333333334F
,
92 0.816496580928F
, 0.471404520791F
, 0.333333333334F
,
97 static const struct solid icosahedron
= {
100 0.0F
, 0.57735026919F
, 0.75576131408F
,
101 0.0F
, -0.93417235896F
, 0.17841104489F
,
102 0.0F
, 0.93417235896F
, -0.17841104489F
,
103 0.0F
, -0.57735026919F
, -0.75576131408F
,
104 -0.5F
, -0.28867513459F
, 0.75576131408F
,
105 -0.5F
, 0.28867513459F
, -0.75576131408F
,
106 0.5F
, -0.28867513459F
, 0.75576131408F
,
107 0.5F
, 0.28867513459F
, -0.75576131408F
,
108 -0.80901699437F
, 0.46708617948F
, 0.17841104489F
,
109 0.80901699437F
, 0.46708617948F
, 0.17841104489F
,
110 -0.80901699437F
, -0.46708617948F
, -0.17841104489F
,
111 0.80901699437F
, -0.46708617948F
, -0.17841104489F
,
115 8,0,2, 0,9,2, 1,10,3, 11,1,3, 0,4,6,
116 4,1,6, 5,2,7, 3,5,7, 4,8,10, 8,5,10,
117 9,6,11, 7,9,11, 0,8,4, 9,0,6, 10,1,4,
118 1,11,6, 8,2,5, 2,9,7, 3,10,5, 11,3,7,
121 -0.356822089773F
, 0.87267799625F
, 0.333333333333F
,
122 0.356822089773F
, 0.87267799625F
, 0.333333333333F
,
123 -0.356822089773F
, -0.87267799625F
, -0.333333333333F
,
124 0.356822089773F
, -0.87267799625F
, -0.333333333333F
,
126 0.0F
, -0.666666666667F
, 0.745355992501F
,
127 0.0F
, 0.666666666667F
, -0.745355992501F
,
129 -0.934172358963F
, -0.12732200375F
, 0.333333333333F
,
130 -0.934172358963F
, 0.12732200375F
, -0.333333333333F
,
131 0.934172358963F
, -0.12732200375F
, 0.333333333333F
,
132 0.934172358963F
, 0.12732200375F
, -0.333333333333F
,
133 -0.57735026919F
, 0.333333333334F
, 0.745355992501F
,
134 0.57735026919F
, 0.333333333334F
, 0.745355992501F
,
135 -0.57735026919F
, -0.745355992501F
, 0.333333333334F
,
136 0.57735026919F
, -0.745355992501F
, 0.333333333334F
,
137 -0.57735026919F
, 0.745355992501F
, -0.333333333334F
,
138 0.57735026919F
, 0.745355992501F
, -0.333333333334F
,
139 -0.57735026919F
, -0.333333333334F
, -0.745355992501F
,
140 0.57735026919F
, -0.333333333334F
, -0.745355992501F
,
146 TETRAHEDRON
, CUBE
, OCTAHEDRON
, ICOSAHEDRON
148 static const struct solid
*solids
[] = {
149 &tetrahedron
, &cube
, &octahedron
, &icosahedron
159 enum { LEFT
, RIGHT
, UP
, DOWN
, UP_LEFT
, UP_RIGHT
, DOWN_LEFT
, DOWN_RIGHT
};
161 #define GRID_SCALE 48.0F
162 #define ROLLTIME 0.1F
164 #define SQ(x) ( (x) * (x) )
166 #define MATMUL(ra,m,a) do { \
167 float rx, ry, rz, xx = (a)[0], yy = (a)[1], zz = (a)[2], *mat = (m); \
168 rx = mat[0] * xx + mat[3] * yy + mat[6] * zz; \
169 ry = mat[1] * xx + mat[4] * yy + mat[7] * zz; \
170 rz = mat[2] * xx + mat[5] * yy + mat[8] * zz; \
171 (ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \
174 #define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
179 float points
[8]; /* maximum */
180 int directions
[8]; /* bit masks showing point pairs */
189 * Grid dimensions. For a square grid these are width and
190 * height respectively; otherwise the grid is a hexagon, with
191 * the top side and the two lower diagonals having length d1
192 * and the remaining three sides having length d2 (so that
193 * d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
199 struct game_params params
;
200 const struct solid
*solid
;
202 struct grid_square
*squares
;
204 int current
; /* index of current grid square */
205 int sgkey
[2]; /* key-point indices into grid sq */
206 int dgkey
[2]; /* key-point indices into grid sq */
207 int spkey
[2]; /* key-point indices into polyhedron */
208 int dpkey
[2]; /* key-point indices into polyhedron */
215 game_params
*default_params(void)
217 game_params
*ret
= snew(game_params
);
226 int game_fetch_preset(int i
, char **name
, game_params
**params
)
228 game_params
*ret
= snew(game_params
);
240 ret
->solid
= TETRAHEDRON
;
246 ret
->solid
= OCTAHEDRON
;
252 ret
->solid
= ICOSAHEDRON
;
266 void free_params(game_params
*params
)
271 game_params
*dup_params(game_params
*params
)
273 game_params
*ret
= snew(game_params
);
274 *ret
= *params
; /* structure copy */
278 static void enum_grid_squares(game_params
*params
,
279 void (*callback
)(void *, struct grid_square
*),
282 const struct solid
*solid
= solids
[params
->solid
];
284 if (solid
->order
== 4) {
287 for (x
= 0; x
< params
->d1
; x
++)
288 for (y
= 0; y
< params
->d2
; y
++) {
289 struct grid_square sq
;
293 sq
.points
[0] = x
- 0.5F
;
294 sq
.points
[1] = y
- 0.5F
;
295 sq
.points
[2] = x
- 0.5F
;
296 sq
.points
[3] = y
+ 0.5F
;
297 sq
.points
[4] = x
+ 0.5F
;
298 sq
.points
[5] = y
+ 0.5F
;
299 sq
.points
[6] = x
+ 0.5F
;
300 sq
.points
[7] = y
- 0.5F
;
303 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
304 sq
.directions
[RIGHT
] = 0x0C; /* 2,3 */
305 sq
.directions
[UP
] = 0x09; /* 0,3 */
306 sq
.directions
[DOWN
] = 0x06; /* 1,2 */
307 sq
.directions
[UP_LEFT
] = 0; /* no diagonals in a square */
308 sq
.directions
[UP_RIGHT
] = 0; /* no diagonals in a square */
309 sq
.directions
[DOWN_LEFT
] = 0; /* no diagonals in a square */
310 sq
.directions
[DOWN_RIGHT
] = 0; /* no diagonals in a square */
315 * This is supremely irrelevant, but just to avoid
316 * having any uninitialised structure members...
323 int row
, rowlen
, other
, i
, firstix
= -1;
324 float theight
= (float)(sqrt(3) / 2.0);
326 for (row
= 0; row
< params
->d1
+ params
->d2
; row
++) {
327 if (row
< params
->d1
) {
329 rowlen
= row
+ params
->d2
;
332 rowlen
= 2*params
->d1
+ params
->d2
- row
;
336 * There are `rowlen' down-pointing triangles.
338 for (i
= 0; i
< rowlen
; i
++) {
339 struct grid_square sq
;
343 ix
= (2 * i
- (rowlen
-1));
347 sq
.y
= y
+ theight
/ 3;
348 sq
.points
[0] = x
- 0.5F
;
351 sq
.points
[3] = y
+ theight
;
352 sq
.points
[4] = x
+ 0.5F
;
356 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
357 sq
.directions
[RIGHT
] = 0x06; /* 1,2 */
358 sq
.directions
[UP
] = 0x05; /* 0,2 */
359 sq
.directions
[DOWN
] = 0; /* invalid move */
362 * Down-pointing triangle: both the up diagonals go
363 * up, and the down ones go left and right.
365 sq
.directions
[UP_LEFT
] = sq
.directions
[UP_RIGHT
] =
367 sq
.directions
[DOWN_LEFT
] = sq
.directions
[LEFT
];
368 sq
.directions
[DOWN_RIGHT
] = sq
.directions
[RIGHT
];
375 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
381 * There are `rowlen+other' up-pointing triangles.
383 for (i
= 0; i
< rowlen
+other
; i
++) {
384 struct grid_square sq
;
388 ix
= (2 * i
- (rowlen
+other
-1));
392 sq
.y
= y
+ 2*theight
/ 3;
393 sq
.points
[0] = x
+ 0.5F
;
394 sq
.points
[1] = y
+ theight
;
397 sq
.points
[4] = x
- 0.5F
;
398 sq
.points
[5] = y
+ theight
;
401 sq
.directions
[LEFT
] = 0x06; /* 1,2 */
402 sq
.directions
[RIGHT
] = 0x03; /* 0,1 */
403 sq
.directions
[DOWN
] = 0x05; /* 0,2 */
404 sq
.directions
[UP
] = 0; /* invalid move */
407 * Up-pointing triangle: both the down diagonals go
408 * down, and the up ones go left and right.
410 sq
.directions
[DOWN_LEFT
] = sq
.directions
[DOWN_RIGHT
] =
412 sq
.directions
[UP_LEFT
] = sq
.directions
[LEFT
];
413 sq
.directions
[UP_RIGHT
] = sq
.directions
[RIGHT
];
420 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
428 static int grid_area(int d1
, int d2
, int order
)
431 * An NxM grid of squares has NM squares in it.
433 * A grid of triangles with dimensions A and B has a total of
434 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
435 * a side-A triangle containing A^2 subtriangles, a side-B
436 * triangle containing B^2, and two congruent parallelograms,
437 * each with side lengths A and B, each therefore containing AB
438 * two-triangle rhombuses.)
443 return d1
*d1
+ d2
*d2
+ 4*d1
*d2
;
453 static void classify_grid_square_callback(void *ctx
, struct grid_square
*sq
)
455 struct grid_data
*data
= (struct grid_data
*)ctx
;
458 if (data
->nclasses
== 4)
459 thisclass
= sq
->tetra_class
;
460 else if (data
->nclasses
== 2)
461 thisclass
= sq
->flip
;
465 data
->gridptrs
[thisclass
][data
->nsquares
[thisclass
]++] =
469 char *new_game_seed(game_params
*params
)
471 struct grid_data data
;
472 int i
, j
, k
, m
, area
, facesperclass
;
477 * Enumerate the grid squares, dividing them into equivalence
478 * classes as appropriate. (For the tetrahedron, there is one
479 * equivalence class for each face; for the octahedron there
480 * are two classes; for the other two solids there's only one.)
483 area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
484 if (params
->solid
== TETRAHEDRON
)
486 else if (params
->solid
== OCTAHEDRON
)
490 data
.gridptrs
[0] = snewn(data
.nclasses
* area
, int);
491 for (i
= 0; i
< data
.nclasses
; i
++) {
492 data
.gridptrs
[i
] = data
.gridptrs
[0] + i
* area
;
493 data
.nsquares
[i
] = 0;
495 data
.squareindex
= 0;
496 enum_grid_squares(params
, classify_grid_square_callback
, &data
);
498 facesperclass
= solids
[params
->solid
]->nfaces
/ data
.nclasses
;
500 for (i
= 0; i
< data
.nclasses
; i
++)
501 assert(data
.nsquares
[i
] >= facesperclass
);
502 assert(data
.squareindex
== area
);
505 * So now we know how many faces to allocate in each class. Get
508 flags
= snewn(area
, int);
509 for (i
= 0; i
< area
; i
++)
512 for (i
= 0; i
< data
.nclasses
; i
++) {
513 for (j
= 0; j
< facesperclass
; j
++) {
514 int n
= rand_upto(data
.nsquares
[i
]);
516 assert(!flags
[data
.gridptrs
[i
][n
]]);
517 flags
[data
.gridptrs
[i
][n
]] = TRUE
;
520 * Move everything else up the array. I ought to use a
521 * better data structure for this, but for such small
522 * numbers it hardly seems worth the effort.
524 while (n
< data
.nsquares
[i
]-1) {
525 data
.gridptrs
[i
][n
] = data
.gridptrs
[i
][n
+1];
533 * Now we know precisely which squares are blue. Encode this
534 * information in hex. While we're looping over this, collect
535 * the non-blue squares into a list in the now-unused gridptrs
538 seed
= snewn(area
/ 4 + 40, char);
543 for (i
= 0; i
< area
; i
++) {
547 data
.gridptrs
[0][m
++] = i
;
551 *p
++ = "0123456789ABCDEF"[j
];
557 *p
++ = "0123456789ABCDEF"[j
];
560 * Choose a non-blue square for the polyhedron.
562 sprintf(p
, ":%d", data
.gridptrs
[0][rand_upto(m
)]);
564 sfree(data
.gridptrs
[0]);
570 static void add_grid_square_callback(void *ctx
, struct grid_square
*sq
)
572 game_state
*state
= (game_state
*)ctx
;
574 state
->squares
[state
->nsquares
] = *sq
; /* structure copy */
575 state
->squares
[state
->nsquares
].blue
= FALSE
;
579 static int lowest_face(const struct solid
*solid
)
586 for (i
= 0; i
< solid
->nfaces
; i
++) {
589 for (j
= 0; j
< solid
->order
; j
++) {
590 int f
= solid
->faces
[i
*solid
->order
+ j
];
591 z
+= solid
->vertices
[f
*3+2];
594 if (i
== 0 || zmin
> z
) {
603 static int align_poly(const struct solid
*solid
, struct grid_square
*sq
,
608 int flip
= (sq
->flip ?
-1 : +1);
611 * First, find the lowest z-coordinate present in the solid.
614 for (i
= 0; i
< solid
->nvertices
; i
++)
615 if (zmin
> solid
->vertices
[i
*3+2])
616 zmin
= solid
->vertices
[i
*3+2];
619 * Now go round the grid square. For each point in the grid
620 * square, we're looking for a point of the polyhedron with the
621 * same x- and y-coordinates (relative to the square's centre),
622 * and z-coordinate equal to zmin (near enough).
624 for (j
= 0; j
< sq
->npoints
; j
++) {
630 for (i
= 0; i
< solid
->nvertices
; i
++) {
633 dist
+= SQ(solid
->vertices
[i
*3+0] * flip
- sq
->points
[j
*2+0] + sq
->x
);
634 dist
+= SQ(solid
->vertices
[i
*3+1] * flip
- sq
->points
[j
*2+1] + sq
->y
);
635 dist
+= SQ(solid
->vertices
[i
*3+2] - zmin
);
643 if (matches
!= 1 || index
< 0)
651 static void flip_poly(struct solid
*solid
, int flip
)
656 for (i
= 0; i
< solid
->nvertices
; i
++) {
657 solid
->vertices
[i
*3+0] *= -1;
658 solid
->vertices
[i
*3+1] *= -1;
660 for (i
= 0; i
< solid
->nfaces
; i
++) {
661 solid
->normals
[i
*3+0] *= -1;
662 solid
->normals
[i
*3+1] *= -1;
667 static struct solid
*transform_poly(const struct solid
*solid
, int flip
,
668 int key0
, int key1
, float angle
)
670 struct solid
*ret
= snew(struct solid
);
671 float vx
, vy
, ax
, ay
;
672 float vmatrix
[9], amatrix
[9], vmatrix2
[9];
675 *ret
= *solid
; /* structure copy */
677 flip_poly(ret
, flip
);
680 * Now rotate the polyhedron through the given angle. We must
681 * rotate about the Z-axis to bring the two vertices key0 and
682 * key1 into horizontal alignment, then rotate about the
683 * X-axis, then rotate back again.
685 vx
= ret
->vertices
[key1
*3+0] - ret
->vertices
[key0
*3+0];
686 vy
= ret
->vertices
[key1
*3+1] - ret
->vertices
[key0
*3+1];
687 assert(APPROXEQ(vx
*vx
+ vy
*vy
, 1.0));
689 vmatrix
[0] = vx
; vmatrix
[3] = vy
; vmatrix
[6] = 0;
690 vmatrix
[1] = -vy
; vmatrix
[4] = vx
; vmatrix
[7] = 0;
691 vmatrix
[2] = 0; vmatrix
[5] = 0; vmatrix
[8] = 1;
693 ax
= (float)cos(angle
);
694 ay
= (float)sin(angle
);
696 amatrix
[0] = 1; amatrix
[3] = 0; amatrix
[6] = 0;
697 amatrix
[1] = 0; amatrix
[4] = ax
; amatrix
[7] = ay
;
698 amatrix
[2] = 0; amatrix
[5] = -ay
; amatrix
[8] = ax
;
700 memcpy(vmatrix2
, vmatrix
, sizeof(vmatrix
));
704 for (i
= 0; i
< ret
->nvertices
; i
++) {
705 MATMUL(ret
->vertices
+ 3*i
, vmatrix
, ret
->vertices
+ 3*i
);
706 MATMUL(ret
->vertices
+ 3*i
, amatrix
, ret
->vertices
+ 3*i
);
707 MATMUL(ret
->vertices
+ 3*i
, vmatrix2
, ret
->vertices
+ 3*i
);
709 for (i
= 0; i
< ret
->nfaces
; i
++) {
710 MATMUL(ret
->normals
+ 3*i
, vmatrix
, ret
->normals
+ 3*i
);
711 MATMUL(ret
->normals
+ 3*i
, amatrix
, ret
->normals
+ 3*i
);
712 MATMUL(ret
->normals
+ 3*i
, vmatrix2
, ret
->normals
+ 3*i
);
718 game_state
*new_game(game_params
*params
, char *seed
)
720 game_state
*state
= snew(game_state
);
723 state
->params
= *params
; /* structure copy */
724 state
->solid
= solids
[params
->solid
];
726 area
= grid_area(params
->d1
, params
->d2
, state
->solid
->order
);
727 state
->squares
= snewn(area
, struct grid_square
);
729 enum_grid_squares(params
, add_grid_square_callback
, state
);
730 assert(state
->nsquares
== area
);
732 state
->facecolours
= snewn(state
->solid
->nfaces
, int);
733 memset(state
->facecolours
, 0, state
->solid
->nfaces
* sizeof(int));
736 * Set up the blue squares and polyhedron position according to
745 for (i
= 0; i
< state
->nsquares
; i
++) {
748 if (v
>= '0' && v
<= '9')
750 else if (v
>= 'A' && v
<= 'F')
752 else if (v
>= 'a' && v
<= 'f')
758 state
->squares
[i
].blue
= TRUE
;
767 state
->current
= atoi(p
);
768 if (state
->current
< 0 || state
->current
>= state
->nsquares
)
769 state
->current
= 0; /* got to do _something_ */
773 * Align the polyhedron with its grid square and determine
774 * initial key points.
780 ret
= align_poly(state
->solid
, &state
->squares
[state
->current
], pkey
);
783 state
->dpkey
[0] = state
->spkey
[0] = pkey
[0];
784 state
->dpkey
[1] = state
->spkey
[0] = pkey
[1];
785 state
->dgkey
[0] = state
->sgkey
[0] = 0;
786 state
->dgkey
[1] = state
->sgkey
[0] = 1;
789 state
->previous
= state
->current
;
791 state
->completed
= 0;
792 state
->movecount
= 0;
797 game_state
*dup_game(game_state
*state
)
799 game_state
*ret
= snew(game_state
);
801 ret
->params
= state
->params
; /* structure copy */
802 ret
->solid
= state
->solid
;
803 ret
->facecolours
= snewn(ret
->solid
->nfaces
, int);
804 memcpy(ret
->facecolours
, state
->facecolours
,
805 ret
->solid
->nfaces
* sizeof(int));
806 ret
->nsquares
= state
->nsquares
;
807 ret
->squares
= snewn(ret
->nsquares
, struct grid_square
);
808 memcpy(ret
->squares
, state
->squares
,
809 ret
->nsquares
* sizeof(struct grid_square
));
810 ret
->dpkey
[0] = state
->dpkey
[0];
811 ret
->dpkey
[1] = state
->dpkey
[1];
812 ret
->dgkey
[0] = state
->dgkey
[0];
813 ret
->dgkey
[1] = state
->dgkey
[1];
814 ret
->spkey
[0] = state
->spkey
[0];
815 ret
->spkey
[1] = state
->spkey
[1];
816 ret
->sgkey
[0] = state
->sgkey
[0];
817 ret
->sgkey
[1] = state
->sgkey
[1];
818 ret
->previous
= state
->previous
;
819 ret
->angle
= state
->angle
;
820 ret
->completed
= state
->completed
;
821 ret
->movecount
= state
->movecount
;
826 void free_game(game_state
*state
)
831 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
834 int pkey
[2], skey
[2], dkey
[2];
838 int i
, j
, dest
, mask
;
842 * All moves are made with the cursor keys.
844 if (button
== CURSOR_UP
)
846 else if (button
== CURSOR_DOWN
)
848 else if (button
== CURSOR_LEFT
)
850 else if (button
== CURSOR_RIGHT
)
852 else if (button
== CURSOR_UP_LEFT
)
854 else if (button
== CURSOR_DOWN_LEFT
)
855 direction
= DOWN_LEFT
;
856 else if (button
== CURSOR_UP_RIGHT
)
857 direction
= UP_RIGHT
;
858 else if (button
== CURSOR_DOWN_RIGHT
)
859 direction
= DOWN_RIGHT
;
864 * Find the two points in the current grid square which
865 * correspond to this move.
867 mask
= from
->squares
[from
->current
].directions
[direction
];
870 for (i
= j
= 0; i
< from
->squares
[from
->current
].npoints
; i
++)
871 if (mask
& (1 << i
)) {
872 points
[j
*2] = from
->squares
[from
->current
].points
[i
*2];
873 points
[j
*2+1] = from
->squares
[from
->current
].points
[i
*2+1];
880 * Now find the other grid square which shares those points.
881 * This is our move destination.
884 for (i
= 0; i
< from
->nsquares
; i
++)
885 if (i
!= from
->current
) {
889 for (j
= 0; j
< from
->squares
[i
].npoints
; j
++) {
890 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[0]) +
891 SQ(from
->squares
[i
].points
[j
*2+1] - points
[1]));
894 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[2]) +
895 SQ(from
->squares
[i
].points
[j
*2+1] - points
[3]));
909 ret
= dup_game(from
);
913 * So we know what grid square we're aiming for, and we also
914 * know the two key points (as indices in both the source and
915 * destination grid squares) which are invariant between source
918 * Next we must roll the polyhedron on to that square. So we
919 * find the indices of the key points within the polyhedron's
920 * vertex array, then use those in a call to transform_poly,
921 * and align the result on the new grid square.
925 align_poly(from
->solid
, &from
->squares
[from
->current
], all_pkey
);
926 pkey
[0] = all_pkey
[skey
[0]];
927 pkey
[1] = all_pkey
[skey
[1]];
929 * Now pkey[0] corresponds to skey[0] and dkey[0], and
935 * Now find the angle through which to rotate the polyhedron.
936 * Do this by finding the two faces that share the two vertices
937 * we've found, and taking the dot product of their normals.
943 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
945 for (j
= 0; j
< from
->solid
->order
; j
++)
946 if (from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[0] ||
947 from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[1])
958 for (i
= 0; i
< 3; i
++)
959 dp
+= (from
->solid
->normals
[f
[0]*3+i
] *
960 from
->solid
->normals
[f
[1]*3+i
]);
961 angle
= (float)acos(dp
);
965 * Now transform the polyhedron. We aren't entirely sure
966 * whether we need to rotate through angle or -angle, and the
967 * simplest way round this is to try both and see which one
968 * aligns successfully!
970 * Unfortunately, _both_ will align successfully if this is a
971 * cube, which won't tell us anything much. So for that
972 * particular case, I resort to gross hackery: I simply negate
973 * the angle before trying the alignment, depending on the
974 * direction. Which directions work which way is determined by
975 * pure trial and error. I said it was gross :-/
981 if (from
->solid
->order
== 4 && direction
== UP
)
982 angle
= -angle
; /* HACK */
984 poly
= transform_poly(from
->solid
,
985 from
->squares
[from
->current
].flip
,
986 pkey
[0], pkey
[1], angle
);
987 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
988 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
992 poly
= transform_poly(from
->solid
,
993 from
->squares
[from
->current
].flip
,
994 pkey
[0], pkey
[1], angle
);
995 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
996 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1003 * Now we have our rotated polyhedron, which we expect to be
1004 * exactly congruent to the one we started with - but with the
1005 * faces permuted. So we map that congruence and thereby figure
1006 * out how to permute the faces as a result of the polyhedron
1010 int *newcolours
= snewn(from
->solid
->nfaces
, int);
1012 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1015 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1019 * Now go through the transformed polyhedron's faces
1020 * and figure out which one's normal is approximately
1021 * equal to this one.
1023 for (j
= 0; j
< poly
->nfaces
; j
++) {
1029 for (k
= 0; k
< 3; k
++)
1030 dist
+= SQ(poly
->normals
[j
*3+k
] -
1031 from
->solid
->normals
[i
*3+k
]);
1033 if (APPROXEQ(dist
, 0)) {
1035 newcolours
[i
] = ret
->facecolours
[j
];
1039 assert(nmatch
== 1);
1042 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1043 assert(newcolours
[i
] != -1);
1045 sfree(ret
->facecolours
);
1046 ret
->facecolours
= newcolours
;
1052 * And finally, swap the colour between the bottom face of the
1053 * polyhedron and the face we've just landed on.
1055 * We don't do this if the game is already complete, since we
1056 * allow the user to roll the fully blue polyhedron around the
1057 * grid as a feeble reward.
1059 if (!ret
->completed
) {
1060 i
= lowest_face(from
->solid
);
1061 j
= ret
->facecolours
[i
];
1062 ret
->facecolours
[i
] = ret
->squares
[ret
->current
].blue
;
1063 ret
->squares
[ret
->current
].blue
= j
;
1066 * Detect game completion.
1069 for (i
= 0; i
< ret
->solid
->nfaces
; i
++)
1070 if (ret
->facecolours
[i
])
1072 if (j
== ret
->solid
->nfaces
)
1073 ret
->completed
= ret
->movecount
;
1079 * Align the normal polyhedron with its grid square, to get key
1080 * points for non-animated display.
1086 success
= align_poly(ret
->solid
, &ret
->squares
[ret
->current
], pkey
);
1089 ret
->dpkey
[0] = pkey
[0];
1090 ret
->dpkey
[1] = pkey
[1];
1096 ret
->spkey
[0] = pkey
[0];
1097 ret
->spkey
[1] = pkey
[1];
1098 ret
->sgkey
[0] = skey
[0];
1099 ret
->sgkey
[1] = skey
[1];
1100 ret
->previous
= from
->current
;
1106 /* ----------------------------------------------------------------------
1114 struct game_drawstate
{
1115 int ox
, oy
; /* pixel position of float origin */
1118 static void find_bbox_callback(void *ctx
, struct grid_square
*sq
)
1120 struct bbox
*bb
= (struct bbox
*)ctx
;
1123 for (i
= 0; i
< sq
->npoints
; i
++) {
1124 if (bb
->l
> sq
->points
[i
*2]) bb
->l
= sq
->points
[i
*2];
1125 if (bb
->r
< sq
->points
[i
*2]) bb
->r
= sq
->points
[i
*2];
1126 if (bb
->u
> sq
->points
[i
*2+1]) bb
->u
= sq
->points
[i
*2+1];
1127 if (bb
->d
< sq
->points
[i
*2+1]) bb
->d
= sq
->points
[i
*2+1];
1131 static struct bbox
find_bbox(game_params
*params
)
1136 * These should be hugely more than the real bounding box will
1139 bb
.l
= 2.0F
* (params
->d1
+ params
->d2
);
1140 bb
.r
= -2.0F
* (params
->d1
+ params
->d2
);
1141 bb
.u
= 2.0F
* (params
->d1
+ params
->d2
);
1142 bb
.d
= -2.0F
* (params
->d1
+ params
->d2
);
1143 enum_grid_squares(params
, find_bbox_callback
, &bb
);
1148 void game_size(game_params
*params
, int *x
, int *y
)
1150 struct bbox bb
= find_bbox(params
);
1151 *x
= (int)((bb
.r
- bb
.l
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
);
1152 *y
= (int)((bb
.d
- bb
.u
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
);
1155 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1157 float *ret
= snewn(3 * NCOLOURS
, float);
1159 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1161 ret
[COL_BORDER
* 3 + 0] = 0.0;
1162 ret
[COL_BORDER
* 3 + 1] = 0.0;
1163 ret
[COL_BORDER
* 3 + 2] = 0.0;
1165 ret
[COL_BLUE
* 3 + 0] = 0.0;
1166 ret
[COL_BLUE
* 3 + 1] = 0.0;
1167 ret
[COL_BLUE
* 3 + 2] = 1.0;
1169 *ncolours
= NCOLOURS
;
1173 game_drawstate
*game_new_drawstate(game_state
*state
)
1175 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1176 struct bbox bb
= find_bbox(&state
->params
);
1178 ds
->ox
= (int)(-(bb
.l
- state
->solid
->border
) * GRID_SCALE
);
1179 ds
->oy
= (int)(-(bb
.u
- state
->solid
->border
) * GRID_SCALE
);
1184 void game_free_drawstate(game_drawstate
*ds
)
1189 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1190 game_state
*state
, float animtime
, float flashtime
)
1193 struct bbox bb
= find_bbox(&state
->params
);
1198 game_state
*newstate
;
1201 draw_rect(fe
, 0, 0, (int)((bb
.r
-bb
.l
+2.0F
) * GRID_SCALE
),
1202 (int)((bb
.d
-bb
.u
+2.0F
) * GRID_SCALE
), COL_BACKGROUND
);
1204 if (oldstate
&& oldstate
->movecount
> state
->movecount
) {
1208 * This is an Undo. So reverse the order of the states, and
1209 * run the roll timer backwards.
1215 animtime
= ROLLTIME
- animtime
;
1221 square
= state
->current
;
1222 pkey
= state
->dpkey
;
1223 gkey
= state
->dgkey
;
1225 angle
= state
->angle
* animtime
/ ROLLTIME
;
1226 square
= state
->previous
;
1227 pkey
= state
->spkey
;
1228 gkey
= state
->sgkey
;
1233 for (i
= 0; i
< state
->nsquares
; i
++) {
1236 for (j
= 0; j
< state
->squares
[i
].npoints
; j
++) {
1237 coords
[2*j
] = ((int)(state
->squares
[i
].points
[2*j
] * GRID_SCALE
)
1239 coords
[2*j
+1] = ((int)(state
->squares
[i
].points
[2*j
+1]*GRID_SCALE
)
1243 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, TRUE
,
1244 state
->squares
[i
].blue ? COL_BLUE
: COL_BACKGROUND
);
1245 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, FALSE
, COL_BORDER
);
1249 * Now compute and draw the polyhedron.
1251 poly
= transform_poly(state
->solid
, state
->squares
[square
].flip
,
1252 pkey
[0], pkey
[1], angle
);
1255 * Compute the translation required to align the two key points
1256 * on the polyhedron with the same key points on the current
1259 for (i
= 0; i
< 3; i
++) {
1262 for (j
= 0; j
< 2; j
++) {
1267 state
->squares
[square
].points
[gkey
[j
]*2+i
];
1272 tc
+= (grid_coord
- poly
->vertices
[pkey
[j
]*3+i
]);
1277 for (i
= 0; i
< poly
->nvertices
; i
++)
1278 for (j
= 0; j
< 3; j
++)
1279 poly
->vertices
[i
*3+j
] += t
[j
];
1282 * Now actually draw each face.
1284 for (i
= 0; i
< poly
->nfaces
; i
++) {
1288 for (j
= 0; j
< poly
->order
; j
++) {
1289 int f
= poly
->faces
[i
*poly
->order
+ j
];
1290 points
[j
*2] = (poly
->vertices
[f
*3+0] -
1291 poly
->vertices
[f
*3+2] * poly
->shear
);
1292 points
[j
*2+1] = (poly
->vertices
[f
*3+1] -
1293 poly
->vertices
[f
*3+2] * poly
->shear
);
1296 for (j
= 0; j
< poly
->order
; j
++) {
1297 coords
[j
*2] = (int)floor(points
[j
*2] * GRID_SCALE
) + ds
->ox
;
1298 coords
[j
*2+1] = (int)floor(points
[j
*2+1] * GRID_SCALE
) + ds
->oy
;
1302 * Find out whether these points are in a clockwise or
1303 * anticlockwise arrangement. If the latter, discard the
1304 * face because it's facing away from the viewer.
1306 * This would involve fiddly winding-number stuff for a
1307 * general polygon, but for the simple parallelograms we'll
1308 * be seeing here, all we have to do is check whether the
1309 * corners turn right or left. So we'll take the vector
1310 * from point 0 to point 1, turn it right 90 degrees,
1311 * and check the sign of the dot product with that and the
1312 * next vector (point 1 to point 2).
1315 float v1x
= points
[2]-points
[0];
1316 float v1y
= points
[3]-points
[1];
1317 float v2x
= points
[4]-points
[2];
1318 float v2y
= points
[5]-points
[3];
1319 float dp
= v1x
* v2y
- v1y
* v2x
;
1325 draw_polygon(fe
, coords
, poly
->order
, TRUE
,
1326 state
->facecolours
[i
] ? COL_BLUE
: COL_BACKGROUND
);
1327 draw_polygon(fe
, coords
, poly
->order
, FALSE
, COL_BORDER
);
1331 draw_update(fe
, 0, 0, (int)((bb
.r
-bb
.l
+2.0F
) * GRID_SCALE
),
1332 (int)((bb
.d
-bb
.u
+2.0F
) * GRID_SCALE
));
1335 * Update the status bar.
1338 char statusbuf
[256];
1340 sprintf(statusbuf
, "%sMoves: %d",
1341 (state
->completed ?
"COMPLETED! " : ""),
1342 (state
->completed ? state
->completed
: state
->movecount
));
1344 status_bar(fe
, statusbuf
);
1348 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)
1353 float game_flash_length(game_state
*oldstate
, game_state
*newstate
)
1358 int game_wants_statusbar(void)