13 const char *const game_name = "Cube";
14 const int game_can_configure = TRUE;
16 #define MAXVERTICES 20
21 float vertices[MAXVERTICES * 3]; /* 3*npoints coordinates */
24 int faces[MAXFACES * MAXORDER]; /* order*nfaces point indices */
25 float normals[MAXFACES * 3]; /* 3*npoints vector components */
26 float shear; /* isometric shear for nice drawing */
27 float border; /* border required around arena */
30 static const struct solid tetrahedron = {
33 0.0F, -0.57735026919F, -0.20412414523F,
34 -0.5F, 0.28867513459F, -0.20412414523F,
35 0.0F, -0.0F, 0.6123724357F,
36 0.5F, 0.28867513459F, -0.20412414523F,
40 0,2,1, 3,1,2, 2,0,3, 1,3,0
43 -0.816496580928F, -0.471404520791F, 0.333333333334F,
44 0.0F, 0.942809041583F, 0.333333333333F,
45 0.816496580928F, -0.471404520791F, 0.333333333334F,
51 static const struct solid cube = {
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,
57 +0.5F,+0.5F,-0.5F, +0.5F,+0.5F,+0.5F,
61 0,1,3,2, 1,5,7,3, 5,4,6,7, 4,0,2,6, 0,4,5,1, 3,7,6,2
64 -1.0F,0.0F,0.0F, 0.0F,0.0F,+1.0F,
65 +1.0F,0.0F,0.0F, 0.0F,0.0F,-1.0F,
66 0.0F,-1.0F,0.0F, 0.0F,+1.0F,0.0F
71 static const struct solid octahedron = {
74 -0.5F, -0.28867513459472505F, 0.4082482904638664F,
75 0.5F, 0.28867513459472505F, -0.4082482904638664F,
76 -0.5F, 0.28867513459472505F, -0.4082482904638664F,
77 0.5F, -0.28867513459472505F, 0.4082482904638664F,
78 0.0F, -0.57735026918945009F, -0.4082482904638664F,
79 0.0F, 0.57735026918945009F, 0.4082482904638664F,
83 4,0,2, 0,5,2, 0,4,3, 5,0,3, 1,4,2, 5,1,2, 4,1,3, 1,5,3
86 -0.816496580928F, -0.471404520791F, -0.333333333334F,
87 -0.816496580928F, 0.471404520791F, 0.333333333334F,
88 0.0F, -0.942809041583F, 0.333333333333F,
91 0.0F, 0.942809041583F, -0.333333333333F,
92 0.816496580928F, -0.471404520791F, -0.333333333334F,
93 0.816496580928F, 0.471404520791F, 0.333333333334F,
98 static const struct solid icosahedron = {
101 0.0F, 0.57735026919F, 0.75576131408F,
102 0.0F, -0.93417235896F, 0.17841104489F,
103 0.0F, 0.93417235896F, -0.17841104489F,
104 0.0F, -0.57735026919F, -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.5F, 0.28867513459F, -0.75576131408F,
109 -0.80901699437F, 0.46708617948F, 0.17841104489F,
110 0.80901699437F, 0.46708617948F, 0.17841104489F,
111 -0.80901699437F, -0.46708617948F, -0.17841104489F,
112 0.80901699437F, -0.46708617948F, -0.17841104489F,
116 8,0,2, 0,9,2, 1,10,3, 11,1,3, 0,4,6,
117 4,1,6, 5,2,7, 3,5,7, 4,8,10, 8,5,10,
118 9,6,11, 7,9,11, 0,8,4, 9,0,6, 10,1,4,
119 1,11,6, 8,2,5, 2,9,7, 3,10,5, 11,3,7,
122 -0.356822089773F, 0.87267799625F, 0.333333333333F,
123 0.356822089773F, 0.87267799625F, 0.333333333333F,
124 -0.356822089773F, -0.87267799625F, -0.333333333333F,
125 0.356822089773F, -0.87267799625F, -0.333333333333F,
127 0.0F, -0.666666666667F, 0.745355992501F,
128 0.0F, 0.666666666667F, -0.745355992501F,
130 -0.934172358963F, -0.12732200375F, 0.333333333333F,
131 -0.934172358963F, 0.12732200375F, -0.333333333333F,
132 0.934172358963F, -0.12732200375F, 0.333333333333F,
133 0.934172358963F, 0.12732200375F, -0.333333333333F,
134 -0.57735026919F, 0.333333333334F, 0.745355992501F,
135 0.57735026919F, 0.333333333334F, 0.745355992501F,
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.745355992501F, -0.333333333334F,
140 -0.57735026919F, -0.333333333334F, -0.745355992501F,
141 0.57735026919F, -0.333333333334F, -0.745355992501F,
147 TETRAHEDRON, CUBE, OCTAHEDRON, ICOSAHEDRON
149 static const struct solid *solids[] = {
150 &tetrahedron, &cube, &octahedron, &icosahedron
160 enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
162 #define GRID_SCALE 48.0F
163 #define ROLLTIME 0.1F
165 #define SQ(x) ( (x) * (x) )
167 #define MATMUL(ra,m,a) do { \
168 float rx, ry, rz, xx = (a)[0], yy = (a)[1], zz = (a)[2], *mat = (m); \
169 rx = mat[0] * xx + mat[3] * yy + mat[6] * zz; \
170 ry = mat[1] * xx + mat[4] * yy + mat[7] * zz; \
171 rz = mat[2] * xx + mat[5] * yy + mat[8] * zz; \
172 (ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \
175 #define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
180 float points[8]; /* maximum */
181 int directions[8]; /* bit masks showing point pairs */
190 * Grid dimensions. For a square grid these are width and
191 * height respectively; otherwise the grid is a hexagon, with
192 * the top side and the two lower diagonals having length d1
193 * and the remaining three sides having length d2 (so that
194 * d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
200 struct game_params params;
201 const struct solid *solid;
203 struct grid_square *squares;
205 int current; /* index of current grid square */
206 int sgkey[2]; /* key-point indices into grid sq */
207 int dgkey[2]; /* key-point indices into grid sq */
208 int spkey[2]; /* key-point indices into polyhedron */
209 int dpkey[2]; /* key-point indices into polyhedron */
216 game_params *default_params(void)
218 game_params *ret = snew(game_params);
227 int game_fetch_preset(int i, char **name, game_params **params)
229 game_params *ret = snew(game_params);
241 ret->solid = TETRAHEDRON;
247 ret->solid = OCTAHEDRON;
253 ret->solid = ICOSAHEDRON;
267 void free_params(game_params *params)
272 game_params *dup_params(game_params *params)
274 game_params *ret = snew(game_params);
275 *ret = *params; /* structure copy */
279 static void enum_grid_squares(game_params *params,
280 void (*callback)(void *, struct grid_square *),
283 const struct solid *solid = solids[params->solid];
285 if (solid->order == 4) {
288 for (x = 0; x < params->d1; x++)
289 for (y = 0; y < params->d2; y++) {
290 struct grid_square sq;
294 sq.points[0] = x - 0.5F;
295 sq.points[1] = y - 0.5F;
296 sq.points[2] = x - 0.5F;
297 sq.points[3] = y + 0.5F;
298 sq.points[4] = x + 0.5F;
299 sq.points[5] = y + 0.5F;
300 sq.points[6] = x + 0.5F;
301 sq.points[7] = y - 0.5F;
304 sq.directions[LEFT] = 0x03; /* 0,1 */
305 sq.directions[RIGHT] = 0x0C; /* 2,3 */
306 sq.directions[UP] = 0x09; /* 0,3 */
307 sq.directions[DOWN] = 0x06; /* 1,2 */
308 sq.directions[UP_LEFT] = 0; /* no diagonals in a square */
309 sq.directions[UP_RIGHT] = 0; /* no diagonals in a square */
310 sq.directions[DOWN_LEFT] = 0; /* no diagonals in a square */
311 sq.directions[DOWN_RIGHT] = 0; /* no diagonals in a square */
316 * This is supremely irrelevant, but just to avoid
317 * having any uninitialised structure members...
324 int row, rowlen, other, i, firstix = -1;
325 float theight = (float)(sqrt(3) / 2.0);
327 for (row = 0; row < params->d1 + params->d2; row++) {
328 if (row < params->d2) {
330 rowlen = row + params->d1;
333 rowlen = 2*params->d2 + params->d1 - row;
337 * There are `rowlen' down-pointing triangles.
339 for (i = 0; i < rowlen; i++) {
340 struct grid_square sq;
344 ix = (2 * i - (rowlen-1));
348 sq.y = y + theight / 3;
349 sq.points[0] = x - 0.5F;
352 sq.points[3] = y + theight;
353 sq.points[4] = x + 0.5F;
357 sq.directions[LEFT] = 0x03; /* 0,1 */
358 sq.directions[RIGHT] = 0x06; /* 1,2 */
359 sq.directions[UP] = 0x05; /* 0,2 */
360 sq.directions[DOWN] = 0; /* invalid move */
363 * Down-pointing triangle: both the up diagonals go
364 * up, and the down ones go left and right.
366 sq.directions[UP_LEFT] = sq.directions[UP_RIGHT] =
368 sq.directions[DOWN_LEFT] = sq.directions[LEFT];
369 sq.directions[DOWN_RIGHT] = sq.directions[RIGHT];
376 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
382 * There are `rowlen+other' up-pointing triangles.
384 for (i = 0; i < rowlen+other; i++) {
385 struct grid_square sq;
389 ix = (2 * i - (rowlen+other-1));
393 sq.y = y + 2*theight / 3;
394 sq.points[0] = x + 0.5F;
395 sq.points[1] = y + theight;
398 sq.points[4] = x - 0.5F;
399 sq.points[5] = y + theight;
402 sq.directions[LEFT] = 0x06; /* 1,2 */
403 sq.directions[RIGHT] = 0x03; /* 0,1 */
404 sq.directions[DOWN] = 0x05; /* 0,2 */
405 sq.directions[UP] = 0; /* invalid move */
408 * Up-pointing triangle: both the down diagonals go
409 * down, and the up ones go left and right.
411 sq.directions[DOWN_LEFT] = sq.directions[DOWN_RIGHT] =
413 sq.directions[UP_LEFT] = sq.directions[LEFT];
414 sq.directions[UP_RIGHT] = sq.directions[RIGHT];
419 firstix = (ix - 1) & 3;
421 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
429 static int grid_area(int d1, int d2, int order)
432 * An NxM grid of squares has NM squares in it.
434 * A grid of triangles with dimensions A and B has a total of
435 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
436 * a side-A triangle containing A^2 subtriangles, a side-B
437 * triangle containing B^2, and two congruent parallelograms,
438 * each with side lengths A and B, each therefore containing AB
439 * two-triangle rhombuses.)
444 return d1*d1 + d2*d2 + 4*d1*d2;
447 config_item *game_configure(game_params *params)
449 config_item *ret = snewn(4, config_item);
452 ret[0].name = "Type of solid";
453 ret[0].type = C_CHOICES;
454 ret[0].sval = ":Tetrahedron:Cube:Octahedron:Icosahedron";
455 ret[0].ival = params->solid;
457 ret[1].name = "Width / top";
458 ret[1].type = C_STRING;
459 sprintf(buf, "%d", params->d1);
460 ret[1].sval = dupstr(buf);
463 ret[2].name = "Height / bottom";
464 ret[2].type = C_STRING;
465 sprintf(buf, "%d", params->d2);
466 ret[2].sval = dupstr(buf);
477 game_params *custom_params(config_item *cfg)
479 game_params *ret = snew(game_params);
481 ret->solid = cfg[0].ival;
482 ret->d1 = atoi(cfg[1].sval);
483 ret->d2 = atoi(cfg[2].sval);
488 static void count_grid_square_callback(void *ctx, struct grid_square *sq)
490 int *classes = (int *)ctx;
494 thisclass = sq->tetra_class;
495 else if (classes[4] == 2)
496 thisclass = sq->flip;
500 classes[thisclass]++;
503 char *validate_params(game_params *params)
508 if (params->solid < 0 || params->solid >= lenof(solids))
509 return "Unrecognised solid type";
511 if (solids[params->solid]->order == 4) {
512 if (params->d1 <= 0 || params->d2 <= 0)
513 return "Both grid dimensions must be greater than zero";
515 if (params->d1 <= 0 && params->d2 <= 0)
516 return "At least one grid dimension must be greater than zero";
519 for (i = 0; i < 4; i++)
521 if (params->solid == TETRAHEDRON)
523 else if (params->solid == OCTAHEDRON)
527 enum_grid_squares(params, count_grid_square_callback, classes);
529 for (i = 0; i < classes[4]; i++)
530 if (classes[i] < solids[params->solid]->nfaces / classes[4])
531 return "Not enough grid space to place all blue faces";
533 if (grid_area(params->d1, params->d2, solids[params->solid]->order) <
534 solids[params->solid]->nfaces + 1)
535 return "Not enough space to place the solid on an empty square";
547 static void classify_grid_square_callback(void *ctx, struct grid_square *sq)
549 struct grid_data *data = (struct grid_data *)ctx;
552 if (data->nclasses == 4)
553 thisclass = sq->tetra_class;
554 else if (data->nclasses == 2)
555 thisclass = sq->flip;
559 data->gridptrs[thisclass][data->nsquares[thisclass]++] =
563 char *new_game_seed(game_params *params)
565 struct grid_data data;
566 int i, j, k, m, area, facesperclass;
571 * Enumerate the grid squares, dividing them into equivalence
572 * classes as appropriate. (For the tetrahedron, there is one
573 * equivalence class for each face; for the octahedron there
574 * are two classes; for the other two solids there's only one.)
577 area = grid_area(params->d1, params->d2, solids[params->solid]->order);
578 if (params->solid == TETRAHEDRON)
580 else if (params->solid == OCTAHEDRON)
584 data.gridptrs[0] = snewn(data.nclasses * area, int);
585 for (i = 0; i < data.nclasses; i++) {
586 data.gridptrs[i] = data.gridptrs[0] + i * area;
587 data.nsquares[i] = 0;
589 data.squareindex = 0;
590 enum_grid_squares(params, classify_grid_square_callback, &data);
592 facesperclass = solids[params->solid]->nfaces / data.nclasses;
594 for (i = 0; i < data.nclasses; i++)
595 assert(data.nsquares[i] >= facesperclass);
596 assert(data.squareindex == area);
599 * So now we know how many faces to allocate in each class. Get
602 flags = snewn(area, int);
603 for (i = 0; i < area; i++)
606 for (i = 0; i < data.nclasses; i++) {
607 for (j = 0; j < facesperclass; j++) {
608 int n = rand_upto(data.nsquares[i]);
610 assert(!flags[data.gridptrs[i][n]]);
611 flags[data.gridptrs[i][n]] = TRUE;
614 * Move everything else up the array. I ought to use a
615 * better data structure for this, but for such small
616 * numbers it hardly seems worth the effort.
618 while (n < data.nsquares[i]-1) {
619 data.gridptrs[i][n] = data.gridptrs[i][n+1];
627 * Now we know precisely which squares are blue. Encode this
628 * information in hex. While we're looping over this, collect
629 * the non-blue squares into a list in the now-unused gridptrs
632 seed = snewn(area / 4 + 40, char);
637 for (i = 0; i < area; i++) {
641 data.gridptrs[0][m++] = i;
645 *p++ = "0123456789ABCDEF"[j];
651 *p++ = "0123456789ABCDEF"[j];
654 * Choose a non-blue square for the polyhedron.
656 sprintf(p, ":%d", data.gridptrs[0][rand_upto(m)]);
658 sfree(data.gridptrs[0]);
664 static void add_grid_square_callback(void *ctx, struct grid_square *sq)
666 game_state *state = (game_state *)ctx;
668 state->squares[state->nsquares] = *sq; /* structure copy */
669 state->squares[state->nsquares].blue = FALSE;
673 static int lowest_face(const struct solid *solid)
680 for (i = 0; i < solid->nfaces; i++) {
683 for (j = 0; j < solid->order; j++) {
684 int f = solid->faces[i*solid->order + j];
685 z += solid->vertices[f*3+2];
688 if (i == 0 || zmin > z) {
697 static int align_poly(const struct solid *solid, struct grid_square *sq,
702 int flip = (sq->flip ? -1 : +1);
705 * First, find the lowest z-coordinate present in the solid.
708 for (i = 0; i < solid->nvertices; i++)
709 if (zmin > solid->vertices[i*3+2])
710 zmin = solid->vertices[i*3+2];
713 * Now go round the grid square. For each point in the grid
714 * square, we're looking for a point of the polyhedron with the
715 * same x- and y-coordinates (relative to the square's centre),
716 * and z-coordinate equal to zmin (near enough).
718 for (j = 0; j < sq->npoints; j++) {
724 for (i = 0; i < solid->nvertices; i++) {
727 dist += SQ(solid->vertices[i*3+0] * flip - sq->points[j*2+0] + sq->x);
728 dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y);
729 dist += SQ(solid->vertices[i*3+2] - zmin);
737 if (matches != 1 || index < 0)
745 static void flip_poly(struct solid *solid, int flip)
750 for (i = 0; i < solid->nvertices; i++) {
751 solid->vertices[i*3+0] *= -1;
752 solid->vertices[i*3+1] *= -1;
754 for (i = 0; i < solid->nfaces; i++) {
755 solid->normals[i*3+0] *= -1;
756 solid->normals[i*3+1] *= -1;
761 static struct solid *transform_poly(const struct solid *solid, int flip,
762 int key0, int key1, float angle)
764 struct solid *ret = snew(struct solid);
765 float vx, vy, ax, ay;
766 float vmatrix[9], amatrix[9], vmatrix2[9];
769 *ret = *solid; /* structure copy */
771 flip_poly(ret, flip);
774 * Now rotate the polyhedron through the given angle. We must
775 * rotate about the Z-axis to bring the two vertices key0 and
776 * key1 into horizontal alignment, then rotate about the
777 * X-axis, then rotate back again.
779 vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0];
780 vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1];
781 assert(APPROXEQ(vx*vx + vy*vy, 1.0));
783 vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0;
784 vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0;
785 vmatrix[2] = 0; vmatrix[5] = 0; vmatrix[8] = 1;
787 ax = (float)cos(angle);
788 ay = (float)sin(angle);
790 amatrix[0] = 1; amatrix[3] = 0; amatrix[6] = 0;
791 amatrix[1] = 0; amatrix[4] = ax; amatrix[7] = ay;
792 amatrix[2] = 0; amatrix[5] = -ay; amatrix[8] = ax;
794 memcpy(vmatrix2, vmatrix, sizeof(vmatrix));
798 for (i = 0; i < ret->nvertices; i++) {
799 MATMUL(ret->vertices + 3*i, vmatrix, ret->vertices + 3*i);
800 MATMUL(ret->vertices + 3*i, amatrix, ret->vertices + 3*i);
801 MATMUL(ret->vertices + 3*i, vmatrix2, ret->vertices + 3*i);
803 for (i = 0; i < ret->nfaces; i++) {
804 MATMUL(ret->normals + 3*i, vmatrix, ret->normals + 3*i);
805 MATMUL(ret->normals + 3*i, amatrix, ret->normals + 3*i);
806 MATMUL(ret->normals + 3*i, vmatrix2, ret->normals + 3*i);
812 game_state *new_game(game_params *params, char *seed)
814 game_state *state = snew(game_state);
817 state->params = *params; /* structure copy */
818 state->solid = solids[params->solid];
820 area = grid_area(params->d1, params->d2, state->solid->order);
821 state->squares = snewn(area, struct grid_square);
823 enum_grid_squares(params, add_grid_square_callback, state);
824 assert(state->nsquares == area);
826 state->facecolours = snewn(state->solid->nfaces, int);
827 memset(state->facecolours, 0, state->solid->nfaces * sizeof(int));
830 * Set up the blue squares and polyhedron position according to
839 for (i = 0; i < state->nsquares; i++) {
842 if (v >= '0' && v <= '9')
844 else if (v >= 'A' && v <= 'F')
846 else if (v >= 'a' && v <= 'f')
852 state->squares[i].blue = TRUE;
861 state->current = atoi(p);
862 if (state->current < 0 || state->current >= state->nsquares)
863 state->current = 0; /* got to do _something_ */
867 * Align the polyhedron with its grid square and determine
868 * initial key points.
874 ret = align_poly(state->solid, &state->squares[state->current], pkey);
877 state->dpkey[0] = state->spkey[0] = pkey[0];
878 state->dpkey[1] = state->spkey[0] = pkey[1];
879 state->dgkey[0] = state->sgkey[0] = 0;
880 state->dgkey[1] = state->sgkey[0] = 1;
883 state->previous = state->current;
885 state->completed = 0;
886 state->movecount = 0;
891 game_state *dup_game(game_state *state)
893 game_state *ret = snew(game_state);
895 ret->params = state->params; /* structure copy */
896 ret->solid = state->solid;
897 ret->facecolours = snewn(ret->solid->nfaces, int);
898 memcpy(ret->facecolours, state->facecolours,
899 ret->solid->nfaces * sizeof(int));
900 ret->nsquares = state->nsquares;
901 ret->squares = snewn(ret->nsquares, struct grid_square);
902 memcpy(ret->squares, state->squares,
903 ret->nsquares * sizeof(struct grid_square));
904 ret->dpkey[0] = state->dpkey[0];
905 ret->dpkey[1] = state->dpkey[1];
906 ret->dgkey[0] = state->dgkey[0];
907 ret->dgkey[1] = state->dgkey[1];
908 ret->spkey[0] = state->spkey[0];
909 ret->spkey[1] = state->spkey[1];
910 ret->sgkey[0] = state->sgkey[0];
911 ret->sgkey[1] = state->sgkey[1];
912 ret->previous = state->previous;
913 ret->angle = state->angle;
914 ret->completed = state->completed;
915 ret->movecount = state->movecount;
920 void free_game(game_state *state)
925 game_state *make_move(game_state *from, int x, int y, int button)
928 int pkey[2], skey[2], dkey[2];
932 int i, j, dest, mask;
936 * All moves are made with the cursor keys.
938 if (button == CURSOR_UP)
940 else if (button == CURSOR_DOWN)
942 else if (button == CURSOR_LEFT)
944 else if (button == CURSOR_RIGHT)
946 else if (button == CURSOR_UP_LEFT)
948 else if (button == CURSOR_DOWN_LEFT)
949 direction = DOWN_LEFT;
950 else if (button == CURSOR_UP_RIGHT)
951 direction = UP_RIGHT;
952 else if (button == CURSOR_DOWN_RIGHT)
953 direction = DOWN_RIGHT;
958 * Find the two points in the current grid square which
959 * correspond to this move.
961 mask = from->squares[from->current].directions[direction];
964 for (i = j = 0; i < from->squares[from->current].npoints; i++)
965 if (mask & (1 << i)) {
966 points[j*2] = from->squares[from->current].points[i*2];
967 points[j*2+1] = from->squares[from->current].points[i*2+1];
974 * Now find the other grid square which shares those points.
975 * This is our move destination.
978 for (i = 0; i < from->nsquares; i++)
979 if (i != from->current) {
983 for (j = 0; j < from->squares[i].npoints; j++) {
984 dist = (SQ(from->squares[i].points[j*2] - points[0]) +
985 SQ(from->squares[i].points[j*2+1] - points[1]));
988 dist = (SQ(from->squares[i].points[j*2] - points[2]) +
989 SQ(from->squares[i].points[j*2+1] - points[3]));
1003 ret = dup_game(from);
1007 * So we know what grid square we're aiming for, and we also
1008 * know the two key points (as indices in both the source and
1009 * destination grid squares) which are invariant between source
1012 * Next we must roll the polyhedron on to that square. So we
1013 * find the indices of the key points within the polyhedron's
1014 * vertex array, then use those in a call to transform_poly,
1015 * and align the result on the new grid square.
1019 align_poly(from->solid, &from->squares[from->current], all_pkey);
1020 pkey[0] = all_pkey[skey[0]];
1021 pkey[1] = all_pkey[skey[1]];
1023 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1029 * Now find the angle through which to rotate the polyhedron.
1030 * Do this by finding the two faces that share the two vertices
1031 * we've found, and taking the dot product of their normals.
1037 for (i = 0; i < from->solid->nfaces; i++) {
1039 for (j = 0; j < from->solid->order; j++)
1040 if (from->solid->faces[i*from->solid->order + j] == pkey[0] ||
1041 from->solid->faces[i*from->solid->order + j] == pkey[1])
1052 for (i = 0; i < 3; i++)
1053 dp += (from->solid->normals[f[0]*3+i] *
1054 from->solid->normals[f[1]*3+i]);
1055 angle = (float)acos(dp);
1059 * Now transform the polyhedron. We aren't entirely sure
1060 * whether we need to rotate through angle or -angle, and the
1061 * simplest way round this is to try both and see which one
1062 * aligns successfully!
1064 * Unfortunately, _both_ will align successfully if this is a
1065 * cube, which won't tell us anything much. So for that
1066 * particular case, I resort to gross hackery: I simply negate
1067 * the angle before trying the alignment, depending on the
1068 * direction. Which directions work which way is determined by
1069 * pure trial and error. I said it was gross :-/
1075 if (from->solid->order == 4 && direction == UP)
1076 angle = -angle; /* HACK */
1078 poly = transform_poly(from->solid,
1079 from->squares[from->current].flip,
1080 pkey[0], pkey[1], angle);
1081 flip_poly(poly, from->squares[ret->current].flip);
1082 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1086 poly = transform_poly(from->solid,
1087 from->squares[from->current].flip,
1088 pkey[0], pkey[1], angle);
1089 flip_poly(poly, from->squares[ret->current].flip);
1090 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1097 * Now we have our rotated polyhedron, which we expect to be
1098 * exactly congruent to the one we started with - but with the
1099 * faces permuted. So we map that congruence and thereby figure
1100 * out how to permute the faces as a result of the polyhedron
1104 int *newcolours = snewn(from->solid->nfaces, int);
1106 for (i = 0; i < from->solid->nfaces; i++)
1109 for (i = 0; i < from->solid->nfaces; i++) {
1113 * Now go through the transformed polyhedron's faces
1114 * and figure out which one's normal is approximately
1115 * equal to this one.
1117 for (j = 0; j < poly->nfaces; j++) {
1123 for (k = 0; k < 3; k++)
1124 dist += SQ(poly->normals[j*3+k] -
1125 from->solid->normals[i*3+k]);
1127 if (APPROXEQ(dist, 0)) {
1129 newcolours[i] = ret->facecolours[j];
1133 assert(nmatch == 1);
1136 for (i = 0; i < from->solid->nfaces; i++)
1137 assert(newcolours[i] != -1);
1139 sfree(ret->facecolours);
1140 ret->facecolours = newcolours;
1146 * And finally, swap the colour between the bottom face of the
1147 * polyhedron and the face we've just landed on.
1149 * We don't do this if the game is already complete, since we
1150 * allow the user to roll the fully blue polyhedron around the
1151 * grid as a feeble reward.
1153 if (!ret->completed) {
1154 i = lowest_face(from->solid);
1155 j = ret->facecolours[i];
1156 ret->facecolours[i] = ret->squares[ret->current].blue;
1157 ret->squares[ret->current].blue = j;
1160 * Detect game completion.
1163 for (i = 0; i < ret->solid->nfaces; i++)
1164 if (ret->facecolours[i])
1166 if (j == ret->solid->nfaces)
1167 ret->completed = ret->movecount;
1173 * Align the normal polyhedron with its grid square, to get key
1174 * points for non-animated display.
1180 success = align_poly(ret->solid, &ret->squares[ret->current], pkey);
1183 ret->dpkey[0] = pkey[0];
1184 ret->dpkey[1] = pkey[1];
1190 ret->spkey[0] = pkey[0];
1191 ret->spkey[1] = pkey[1];
1192 ret->sgkey[0] = skey[0];
1193 ret->sgkey[1] = skey[1];
1194 ret->previous = from->current;
1200 /* ----------------------------------------------------------------------
1208 struct game_drawstate {
1209 int ox, oy; /* pixel position of float origin */
1212 static void find_bbox_callback(void *ctx, struct grid_square *sq)
1214 struct bbox *bb = (struct bbox *)ctx;
1217 for (i = 0; i < sq->npoints; i++) {
1218 if (bb->l > sq->points[i*2]) bb->l = sq->points[i*2];
1219 if (bb->r < sq->points[i*2]) bb->r = sq->points[i*2];
1220 if (bb->u > sq->points[i*2+1]) bb->u = sq->points[i*2+1];
1221 if (bb->d < sq->points[i*2+1]) bb->d = sq->points[i*2+1];
1225 static struct bbox find_bbox(game_params *params)
1230 * These should be hugely more than the real bounding box will
1233 bb.l = 2.0F * (params->d1 + params->d2);
1234 bb.r = -2.0F * (params->d1 + params->d2);
1235 bb.u = 2.0F * (params->d1 + params->d2);
1236 bb.d = -2.0F * (params->d1 + params->d2);
1237 enum_grid_squares(params, find_bbox_callback, &bb);
1242 void game_size(game_params *params, int *x, int *y)
1244 struct bbox bb = find_bbox(params);
1245 *x = (int)((bb.r - bb.l + 2*solids[params->solid]->border) * GRID_SCALE);
1246 *y = (int)((bb.d - bb.u + 2*solids[params->solid]->border) * GRID_SCALE);
1249 float *game_colours(frontend *fe, game_state *state, int *ncolours)
1251 float *ret = snewn(3 * NCOLOURS, float);
1253 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1255 ret[COL_BORDER * 3 + 0] = 0.0;
1256 ret[COL_BORDER * 3 + 1] = 0.0;
1257 ret[COL_BORDER * 3 + 2] = 0.0;
1259 ret[COL_BLUE * 3 + 0] = 0.0;
1260 ret[COL_BLUE * 3 + 1] = 0.0;
1261 ret[COL_BLUE * 3 + 2] = 1.0;
1263 *ncolours = NCOLOURS;
1267 game_drawstate *game_new_drawstate(game_state *state)
1269 struct game_drawstate *ds = snew(struct game_drawstate);
1270 struct bbox bb = find_bbox(&state->params);
1272 ds->ox = (int)(-(bb.l - state->solid->border) * GRID_SCALE);
1273 ds->oy = (int)(-(bb.u - state->solid->border) * GRID_SCALE);
1278 void game_free_drawstate(game_drawstate *ds)
1283 void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1284 game_state *state, float animtime, float flashtime)
1287 struct bbox bb = find_bbox(&state->params);
1292 game_state *newstate;
1295 draw_rect(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1296 (int)((bb.d-bb.u+2.0F) * GRID_SCALE), COL_BACKGROUND);
1298 if (oldstate && oldstate->movecount > state->movecount) {
1302 * This is an Undo. So reverse the order of the states, and
1303 * run the roll timer backwards.
1309 animtime = ROLLTIME - animtime;
1315 square = state->current;
1316 pkey = state->dpkey;
1317 gkey = state->dgkey;
1319 angle = state->angle * animtime / ROLLTIME;
1320 square = state->previous;
1321 pkey = state->spkey;
1322 gkey = state->sgkey;
1327 for (i = 0; i < state->nsquares; i++) {
1330 for (j = 0; j < state->squares[i].npoints; j++) {
1331 coords[2*j] = ((int)(state->squares[i].points[2*j] * GRID_SCALE)
1333 coords[2*j+1] = ((int)(state->squares[i].points[2*j+1]*GRID_SCALE)
1337 draw_polygon(fe, coords, state->squares[i].npoints, TRUE,
1338 state->squares[i].blue ? COL_BLUE : COL_BACKGROUND);
1339 draw_polygon(fe, coords, state->squares[i].npoints, FALSE, COL_BORDER);
1343 * Now compute and draw the polyhedron.
1345 poly = transform_poly(state->solid, state->squares[square].flip,
1346 pkey[0], pkey[1], angle);
1349 * Compute the translation required to align the two key points
1350 * on the polyhedron with the same key points on the current
1353 for (i = 0; i < 3; i++) {
1356 for (j = 0; j < 2; j++) {
1361 state->squares[square].points[gkey[j]*2+i];
1366 tc += (grid_coord - poly->vertices[pkey[j]*3+i]);
1371 for (i = 0; i < poly->nvertices; i++)
1372 for (j = 0; j < 3; j++)
1373 poly->vertices[i*3+j] += t[j];
1376 * Now actually draw each face.
1378 for (i = 0; i < poly->nfaces; i++) {
1382 for (j = 0; j < poly->order; j++) {
1383 int f = poly->faces[i*poly->order + j];
1384 points[j*2] = (poly->vertices[f*3+0] -
1385 poly->vertices[f*3+2] * poly->shear);
1386 points[j*2+1] = (poly->vertices[f*3+1] -
1387 poly->vertices[f*3+2] * poly->shear);
1390 for (j = 0; j < poly->order; j++) {
1391 coords[j*2] = (int)floor(points[j*2] * GRID_SCALE) + ds->ox;
1392 coords[j*2+1] = (int)floor(points[j*2+1] * GRID_SCALE) + ds->oy;
1396 * Find out whether these points are in a clockwise or
1397 * anticlockwise arrangement. If the latter, discard the
1398 * face because it's facing away from the viewer.
1400 * This would involve fiddly winding-number stuff for a
1401 * general polygon, but for the simple parallelograms we'll
1402 * be seeing here, all we have to do is check whether the
1403 * corners turn right or left. So we'll take the vector
1404 * from point 0 to point 1, turn it right 90 degrees,
1405 * and check the sign of the dot product with that and the
1406 * next vector (point 1 to point 2).
1409 float v1x = points[2]-points[0];
1410 float v1y = points[3]-points[1];
1411 float v2x = points[4]-points[2];
1412 float v2y = points[5]-points[3];
1413 float dp = v1x * v2y - v1y * v2x;
1419 draw_polygon(fe, coords, poly->order, TRUE,
1420 state->facecolours[i] ? COL_BLUE : COL_BACKGROUND);
1421 draw_polygon(fe, coords, poly->order, FALSE, COL_BORDER);
1425 draw_update(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1426 (int)((bb.d-bb.u+2.0F) * GRID_SCALE));
1429 * Update the status bar.
1432 char statusbuf[256];
1434 sprintf(statusbuf, "%sMoves: %d",
1435 (state->completed ? "COMPLETED! " : ""),
1436 (state->completed ? state->completed : state->movecount));
1438 status_bar(fe, statusbuf);
1442 float game_anim_length(game_state *oldstate, game_state *newstate)
1447 float game_flash_length(game_state *oldstate, game_state *newstate)
1452 int game_wants_statusbar(void)