14 #define MAXVERTICES 20
19 float vertices[MAXVERTICES * 3]; /* 3*npoints coordinates */
22 int faces[MAXFACES * MAXORDER]; /* order*nfaces point indices */
23 float normals[MAXFACES * 3]; /* 3*npoints vector components */
24 float shear; /* isometric shear for nice drawing */
25 float border; /* border required around arena */
28 static const struct solid tetrahedron = {
31 0.0F, -0.57735026919F, -0.20412414523F,
32 -0.5F, 0.28867513459F, -0.20412414523F,
33 0.0F, -0.0F, 0.6123724357F,
34 0.5F, 0.28867513459F, -0.20412414523F,
38 0,2,1, 3,1,2, 2,0,3, 1,3,0
41 -0.816496580928F, -0.471404520791F, 0.333333333334F,
42 0.0F, 0.942809041583F, 0.333333333333F,
43 0.816496580928F, -0.471404520791F, 0.333333333334F,
49 static const struct solid cube = {
52 -0.5F,-0.5F,-0.5F, -0.5F,-0.5F,+0.5F,
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,
59 0,1,3,2, 1,5,7,3, 5,4,6,7, 4,0,2,6, 0,4,5,1, 3,7,6,2
62 -1.0F,0.0F,0.0F, 0.0F,0.0F,+1.0F,
63 +1.0F,0.0F,0.0F, 0.0F,0.0F,-1.0F,
64 0.0F,-1.0F,0.0F, 0.0F,+1.0F,0.0F
69 static const struct solid octahedron = {
72 -0.5F, -0.28867513459472505F, 0.4082482904638664F,
73 0.5F, 0.28867513459472505F, -0.4082482904638664F,
74 -0.5F, 0.28867513459472505F, -0.4082482904638664F,
75 0.5F, -0.28867513459472505F, 0.4082482904638664F,
76 0.0F, -0.57735026918945009F, -0.4082482904638664F,
77 0.0F, 0.57735026918945009F, 0.4082482904638664F,
81 4,0,2, 0,5,2, 0,4,3, 5,0,3, 1,4,2, 5,1,2, 4,1,3, 1,5,3
84 -0.816496580928F, -0.471404520791F, -0.333333333334F,
85 -0.816496580928F, 0.471404520791F, 0.333333333334F,
86 0.0F, -0.942809041583F, 0.333333333333F,
89 0.0F, 0.942809041583F, -0.333333333333F,
90 0.816496580928F, -0.471404520791F, -0.333333333334F,
91 0.816496580928F, 0.471404520791F, 0.333333333334F,
96 static const struct solid icosahedron = {
99 0.0F, 0.57735026919F, 0.75576131408F,
100 0.0F, -0.93417235896F, 0.17841104489F,
101 0.0F, 0.93417235896F, -0.17841104489F,
102 0.0F, -0.57735026919F, -0.75576131408F,
103 -0.5F, -0.28867513459F, 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.80901699437F, 0.46708617948F, 0.17841104489F,
108 0.80901699437F, 0.46708617948F, 0.17841104489F,
109 -0.80901699437F, -0.46708617948F, -0.17841104489F,
110 0.80901699437F, -0.46708617948F, -0.17841104489F,
114 8,0,2, 0,9,2, 1,10,3, 11,1,3, 0,4,6,
115 4,1,6, 5,2,7, 3,5,7, 4,8,10, 8,5,10,
116 9,6,11, 7,9,11, 0,8,4, 9,0,6, 10,1,4,
117 1,11,6, 8,2,5, 2,9,7, 3,10,5, 11,3,7,
120 -0.356822089773F, 0.87267799625F, 0.333333333333F,
121 0.356822089773F, 0.87267799625F, 0.333333333333F,
122 -0.356822089773F, -0.87267799625F, -0.333333333333F,
123 0.356822089773F, -0.87267799625F, -0.333333333333F,
125 0.0F, -0.666666666667F, 0.745355992501F,
126 0.0F, 0.666666666667F, -0.745355992501F,
128 -0.934172358963F, -0.12732200375F, 0.333333333333F,
129 -0.934172358963F, 0.12732200375F, -0.333333333333F,
130 0.934172358963F, -0.12732200375F, 0.333333333333F,
131 0.934172358963F, 0.12732200375F, -0.333333333333F,
132 -0.57735026919F, 0.333333333334F, 0.745355992501F,
133 0.57735026919F, 0.333333333334F, 0.745355992501F,
134 -0.57735026919F, -0.745355992501F, 0.333333333334F,
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.333333333334F, -0.745355992501F,
139 0.57735026919F, -0.333333333334F, -0.745355992501F,
145 TETRAHEDRON, CUBE, OCTAHEDRON, ICOSAHEDRON
147 static const struct solid *solids[] = {
148 &tetrahedron, &cube, &octahedron, &icosahedron
158 enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
160 #define GRID_SCALE 48.0F
161 #define ROLLTIME 0.13F
163 #define SQ(x) ( (x) * (x) )
165 #define MATMUL(ra,m,a) do { \
166 float rx, ry, rz, xx = (a)[0], yy = (a)[1], zz = (a)[2], *mat = (m); \
167 rx = mat[0] * xx + mat[3] * yy + mat[6] * zz; \
168 ry = mat[1] * xx + mat[4] * yy + mat[7] * zz; \
169 rz = mat[2] * xx + mat[5] * yy + mat[8] * zz; \
170 (ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \
173 #define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
178 float points[8]; /* maximum */
179 int directions[8]; /* bit masks showing point pairs */
188 * Grid dimensions. For a square grid these are width and
189 * height respectively; otherwise the grid is a hexagon, with
190 * the top side and the two lower diagonals having length d1
191 * and the remaining three sides having length d2 (so that
192 * d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
198 struct game_params params;
199 const struct solid *solid;
201 struct grid_square *squares;
203 int current; /* index of current grid square */
204 int sgkey[2]; /* key-point indices into grid sq */
205 int dgkey[2]; /* key-point indices into grid sq */
206 int spkey[2]; /* key-point indices into polyhedron */
207 int dpkey[2]; /* key-point indices into polyhedron */
214 static game_params *default_params(void)
216 game_params *ret = snew(game_params);
225 static int game_fetch_preset(int i, char **name, game_params **params)
227 game_params *ret = snew(game_params);
239 ret->solid = TETRAHEDRON;
245 ret->solid = OCTAHEDRON;
251 ret->solid = ICOSAHEDRON;
265 static void free_params(game_params *params)
270 static game_params *dup_params(game_params *params)
272 game_params *ret = snew(game_params);
273 *ret = *params; /* structure copy */
277 static game_params *decode_params(char const *string)
279 game_params *ret = default_params();
282 case 't': ret->solid = TETRAHEDRON; string++; break;
283 case 'c': ret->solid = CUBE; string++; break;
284 case 'o': ret->solid = OCTAHEDRON; string++; break;
285 case 'i': ret->solid = ICOSAHEDRON; string++; break;
288 ret->d1 = ret->d2 = atoi(string);
289 while (*string && isdigit(*string)) string++;
290 if (*string == 'x') {
292 ret->d2 = atoi(string);
298 static char *encode_params(game_params *params)
302 assert(params->solid >= 0 && params->solid < 4);
303 sprintf(data, "%c%dx%d", "tcoi"[params->solid], params->d1, params->d2);
308 static void enum_grid_squares(game_params *params,
309 void (*callback)(void *, struct grid_square *),
312 const struct solid *solid = solids[params->solid];
314 if (solid->order == 4) {
317 for (y = 0; y < params->d2; y++)
318 for (x = 0; x < params->d1; x++) {
319 struct grid_square sq;
323 sq.points[0] = x - 0.5F;
324 sq.points[1] = y - 0.5F;
325 sq.points[2] = x - 0.5F;
326 sq.points[3] = y + 0.5F;
327 sq.points[4] = x + 0.5F;
328 sq.points[5] = y + 0.5F;
329 sq.points[6] = x + 0.5F;
330 sq.points[7] = y - 0.5F;
333 sq.directions[LEFT] = 0x03; /* 0,1 */
334 sq.directions[RIGHT] = 0x0C; /* 2,3 */
335 sq.directions[UP] = 0x09; /* 0,3 */
336 sq.directions[DOWN] = 0x06; /* 1,2 */
337 sq.directions[UP_LEFT] = 0; /* no diagonals in a square */
338 sq.directions[UP_RIGHT] = 0; /* no diagonals in a square */
339 sq.directions[DOWN_LEFT] = 0; /* no diagonals in a square */
340 sq.directions[DOWN_RIGHT] = 0; /* no diagonals in a square */
345 * This is supremely irrelevant, but just to avoid
346 * having any uninitialised structure members...
353 int row, rowlen, other, i, firstix = -1;
354 float theight = (float)(sqrt(3) / 2.0);
356 for (row = 0; row < params->d1 + params->d2; row++) {
357 if (row < params->d2) {
359 rowlen = row + params->d1;
362 rowlen = 2*params->d2 + params->d1 - row;
366 * There are `rowlen' down-pointing triangles.
368 for (i = 0; i < rowlen; i++) {
369 struct grid_square sq;
373 ix = (2 * i - (rowlen-1));
377 sq.y = y + theight / 3;
378 sq.points[0] = x - 0.5F;
381 sq.points[3] = y + theight;
382 sq.points[4] = x + 0.5F;
386 sq.directions[LEFT] = 0x03; /* 0,1 */
387 sq.directions[RIGHT] = 0x06; /* 1,2 */
388 sq.directions[UP] = 0x05; /* 0,2 */
389 sq.directions[DOWN] = 0; /* invalid move */
392 * Down-pointing triangle: both the up diagonals go
393 * up, and the down ones go left and right.
395 sq.directions[UP_LEFT] = sq.directions[UP_RIGHT] =
397 sq.directions[DOWN_LEFT] = sq.directions[LEFT];
398 sq.directions[DOWN_RIGHT] = sq.directions[RIGHT];
405 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
411 * There are `rowlen+other' up-pointing triangles.
413 for (i = 0; i < rowlen+other; i++) {
414 struct grid_square sq;
418 ix = (2 * i - (rowlen+other-1));
422 sq.y = y + 2*theight / 3;
423 sq.points[0] = x + 0.5F;
424 sq.points[1] = y + theight;
427 sq.points[4] = x - 0.5F;
428 sq.points[5] = y + theight;
431 sq.directions[LEFT] = 0x06; /* 1,2 */
432 sq.directions[RIGHT] = 0x03; /* 0,1 */
433 sq.directions[DOWN] = 0x05; /* 0,2 */
434 sq.directions[UP] = 0; /* invalid move */
437 * Up-pointing triangle: both the down diagonals go
438 * down, and the up ones go left and right.
440 sq.directions[DOWN_LEFT] = sq.directions[DOWN_RIGHT] =
442 sq.directions[UP_LEFT] = sq.directions[LEFT];
443 sq.directions[UP_RIGHT] = sq.directions[RIGHT];
448 firstix = (ix - 1) & 3;
450 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
458 static int grid_area(int d1, int d2, int order)
461 * An NxM grid of squares has NM squares in it.
463 * A grid of triangles with dimensions A and B has a total of
464 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
465 * a side-A triangle containing A^2 subtriangles, a side-B
466 * triangle containing B^2, and two congruent parallelograms,
467 * each with side lengths A and B, each therefore containing AB
468 * two-triangle rhombuses.)
473 return d1*d1 + d2*d2 + 4*d1*d2;
476 static config_item *game_configure(game_params *params)
478 config_item *ret = snewn(4, config_item);
481 ret[0].name = "Type of solid";
482 ret[0].type = C_CHOICES;
483 ret[0].sval = ":Tetrahedron:Cube:Octahedron:Icosahedron";
484 ret[0].ival = params->solid;
486 ret[1].name = "Width / top";
487 ret[1].type = C_STRING;
488 sprintf(buf, "%d", params->d1);
489 ret[1].sval = dupstr(buf);
492 ret[2].name = "Height / bottom";
493 ret[2].type = C_STRING;
494 sprintf(buf, "%d", params->d2);
495 ret[2].sval = dupstr(buf);
506 static game_params *custom_params(config_item *cfg)
508 game_params *ret = snew(game_params);
510 ret->solid = cfg[0].ival;
511 ret->d1 = atoi(cfg[1].sval);
512 ret->d2 = atoi(cfg[2].sval);
517 static void count_grid_square_callback(void *ctx, struct grid_square *sq)
519 int *classes = (int *)ctx;
523 thisclass = sq->tetra_class;
524 else if (classes[4] == 2)
525 thisclass = sq->flip;
529 classes[thisclass]++;
532 static char *validate_params(game_params *params)
537 if (params->solid < 0 || params->solid >= lenof(solids))
538 return "Unrecognised solid type";
540 if (solids[params->solid]->order == 4) {
541 if (params->d1 <= 0 || params->d2 <= 0)
542 return "Both grid dimensions must be greater than zero";
544 if (params->d1 <= 0 && params->d2 <= 0)
545 return "At least one grid dimension must be greater than zero";
548 for (i = 0; i < 4; i++)
550 if (params->solid == TETRAHEDRON)
552 else if (params->solid == OCTAHEDRON)
556 enum_grid_squares(params, count_grid_square_callback, classes);
558 for (i = 0; i < classes[4]; i++)
559 if (classes[i] < solids[params->solid]->nfaces / classes[4])
560 return "Not enough grid space to place all blue faces";
562 if (grid_area(params->d1, params->d2, solids[params->solid]->order) <
563 solids[params->solid]->nfaces + 1)
564 return "Not enough space to place the solid on an empty square";
576 static void classify_grid_square_callback(void *ctx, struct grid_square *sq)
578 struct grid_data *data = (struct grid_data *)ctx;
581 if (data->nclasses == 4)
582 thisclass = sq->tetra_class;
583 else if (data->nclasses == 2)
584 thisclass = sq->flip;
588 data->gridptrs[thisclass][data->nsquares[thisclass]++] =
592 static char *new_game_seed(game_params *params, random_state *rs)
594 struct grid_data data;
595 int i, j, k, m, area, facesperclass;
600 * Enumerate the grid squares, dividing them into equivalence
601 * classes as appropriate. (For the tetrahedron, there is one
602 * equivalence class for each face; for the octahedron there
603 * are two classes; for the other two solids there's only one.)
606 area = grid_area(params->d1, params->d2, solids[params->solid]->order);
607 if (params->solid == TETRAHEDRON)
609 else if (params->solid == OCTAHEDRON)
613 data.gridptrs[0] = snewn(data.nclasses * area, int);
614 for (i = 0; i < data.nclasses; i++) {
615 data.gridptrs[i] = data.gridptrs[0] + i * area;
616 data.nsquares[i] = 0;
618 data.squareindex = 0;
619 enum_grid_squares(params, classify_grid_square_callback, &data);
621 facesperclass = solids[params->solid]->nfaces / data.nclasses;
623 for (i = 0; i < data.nclasses; i++)
624 assert(data.nsquares[i] >= facesperclass);
625 assert(data.squareindex == area);
628 * So now we know how many faces to allocate in each class. Get
631 flags = snewn(area, int);
632 for (i = 0; i < area; i++)
635 for (i = 0; i < data.nclasses; i++) {
636 for (j = 0; j < facesperclass; j++) {
637 int n = random_upto(rs, data.nsquares[i]);
639 assert(!flags[data.gridptrs[i][n]]);
640 flags[data.gridptrs[i][n]] = TRUE;
643 * Move everything else up the array. I ought to use a
644 * better data structure for this, but for such small
645 * numbers it hardly seems worth the effort.
647 while (n < data.nsquares[i]-1) {
648 data.gridptrs[i][n] = data.gridptrs[i][n+1];
656 * Now we know precisely which squares are blue. Encode this
657 * information in hex. While we're looping over this, collect
658 * the non-blue squares into a list in the now-unused gridptrs
661 seed = snewn(area / 4 + 40, char);
666 for (i = 0; i < area; i++) {
670 data.gridptrs[0][m++] = i;
674 *p++ = "0123456789ABCDEF"[j];
680 *p++ = "0123456789ABCDEF"[j];
683 * Choose a non-blue square for the polyhedron.
685 sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]);
687 sfree(data.gridptrs[0]);
693 static void add_grid_square_callback(void *ctx, struct grid_square *sq)
695 game_state *state = (game_state *)ctx;
697 state->squares[state->nsquares] = *sq; /* structure copy */
698 state->squares[state->nsquares].blue = FALSE;
702 static int lowest_face(const struct solid *solid)
709 for (i = 0; i < solid->nfaces; i++) {
712 for (j = 0; j < solid->order; j++) {
713 int f = solid->faces[i*solid->order + j];
714 z += solid->vertices[f*3+2];
717 if (i == 0 || zmin > z) {
726 static int align_poly(const struct solid *solid, struct grid_square *sq,
731 int flip = (sq->flip ? -1 : +1);
734 * First, find the lowest z-coordinate present in the solid.
737 for (i = 0; i < solid->nvertices; i++)
738 if (zmin > solid->vertices[i*3+2])
739 zmin = solid->vertices[i*3+2];
742 * Now go round the grid square. For each point in the grid
743 * square, we're looking for a point of the polyhedron with the
744 * same x- and y-coordinates (relative to the square's centre),
745 * and z-coordinate equal to zmin (near enough).
747 for (j = 0; j < sq->npoints; j++) {
753 for (i = 0; i < solid->nvertices; i++) {
756 dist += SQ(solid->vertices[i*3+0] * flip - sq->points[j*2+0] + sq->x);
757 dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y);
758 dist += SQ(solid->vertices[i*3+2] - zmin);
766 if (matches != 1 || index < 0)
774 static void flip_poly(struct solid *solid, int flip)
779 for (i = 0; i < solid->nvertices; i++) {
780 solid->vertices[i*3+0] *= -1;
781 solid->vertices[i*3+1] *= -1;
783 for (i = 0; i < solid->nfaces; i++) {
784 solid->normals[i*3+0] *= -1;
785 solid->normals[i*3+1] *= -1;
790 static struct solid *transform_poly(const struct solid *solid, int flip,
791 int key0, int key1, float angle)
793 struct solid *ret = snew(struct solid);
794 float vx, vy, ax, ay;
795 float vmatrix[9], amatrix[9], vmatrix2[9];
798 *ret = *solid; /* structure copy */
800 flip_poly(ret, flip);
803 * Now rotate the polyhedron through the given angle. We must
804 * rotate about the Z-axis to bring the two vertices key0 and
805 * key1 into horizontal alignment, then rotate about the
806 * X-axis, then rotate back again.
808 vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0];
809 vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1];
810 assert(APPROXEQ(vx*vx + vy*vy, 1.0));
812 vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0;
813 vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0;
814 vmatrix[2] = 0; vmatrix[5] = 0; vmatrix[8] = 1;
816 ax = (float)cos(angle);
817 ay = (float)sin(angle);
819 amatrix[0] = 1; amatrix[3] = 0; amatrix[6] = 0;
820 amatrix[1] = 0; amatrix[4] = ax; amatrix[7] = ay;
821 amatrix[2] = 0; amatrix[5] = -ay; amatrix[8] = ax;
823 memcpy(vmatrix2, vmatrix, sizeof(vmatrix));
827 for (i = 0; i < ret->nvertices; i++) {
828 MATMUL(ret->vertices + 3*i, vmatrix, ret->vertices + 3*i);
829 MATMUL(ret->vertices + 3*i, amatrix, ret->vertices + 3*i);
830 MATMUL(ret->vertices + 3*i, vmatrix2, ret->vertices + 3*i);
832 for (i = 0; i < ret->nfaces; i++) {
833 MATMUL(ret->normals + 3*i, vmatrix, ret->normals + 3*i);
834 MATMUL(ret->normals + 3*i, amatrix, ret->normals + 3*i);
835 MATMUL(ret->normals + 3*i, vmatrix2, ret->normals + 3*i);
841 static char *validate_seed(game_params *params, char *seed)
843 int area = grid_area(params->d1, params->d2, solids[params->solid]->order);
847 for (j = 0; j < i; j++) {
849 if (c >= '0' && c <= '9') continue;
850 if (c >= 'A' && c <= 'F') continue;
851 if (c >= 'a' && c <= 'f') continue;
852 return "Not enough hex digits at start of string";
853 /* NB if seed[j]=='\0' that will also be caught here, so we're safe */
857 return "Expected ',' after hex digits";
861 if (seed[i] < '0' || seed[i] > '9')
862 return "Expected decimal integer after ','";
869 static game_state *new_game(game_params *params, char *seed)
871 game_state *state = snew(game_state);
874 state->params = *params; /* structure copy */
875 state->solid = solids[params->solid];
877 area = grid_area(params->d1, params->d2, state->solid->order);
878 state->squares = snewn(area, struct grid_square);
880 enum_grid_squares(params, add_grid_square_callback, state);
881 assert(state->nsquares == area);
883 state->facecolours = snewn(state->solid->nfaces, int);
884 memset(state->facecolours, 0, state->solid->nfaces * sizeof(int));
887 * Set up the blue squares and polyhedron position according to
896 for (i = 0; i < state->nsquares; i++) {
899 if (v >= '0' && v <= '9')
901 else if (v >= 'A' && v <= 'F')
903 else if (v >= 'a' && v <= 'f')
909 state->squares[i].blue = TRUE;
918 state->current = atoi(p);
919 if (state->current < 0 || state->current >= state->nsquares)
920 state->current = 0; /* got to do _something_ */
924 * Align the polyhedron with its grid square and determine
925 * initial key points.
931 ret = align_poly(state->solid, &state->squares[state->current], pkey);
934 state->dpkey[0] = state->spkey[0] = pkey[0];
935 state->dpkey[1] = state->spkey[0] = pkey[1];
936 state->dgkey[0] = state->sgkey[0] = 0;
937 state->dgkey[1] = state->sgkey[0] = 1;
940 state->previous = state->current;
942 state->completed = 0;
943 state->movecount = 0;
948 static game_state *dup_game(game_state *state)
950 game_state *ret = snew(game_state);
952 ret->params = state->params; /* structure copy */
953 ret->solid = state->solid;
954 ret->facecolours = snewn(ret->solid->nfaces, int);
955 memcpy(ret->facecolours, state->facecolours,
956 ret->solid->nfaces * sizeof(int));
957 ret->nsquares = state->nsquares;
958 ret->squares = snewn(ret->nsquares, struct grid_square);
959 memcpy(ret->squares, state->squares,
960 ret->nsquares * sizeof(struct grid_square));
961 ret->dpkey[0] = state->dpkey[0];
962 ret->dpkey[1] = state->dpkey[1];
963 ret->dgkey[0] = state->dgkey[0];
964 ret->dgkey[1] = state->dgkey[1];
965 ret->spkey[0] = state->spkey[0];
966 ret->spkey[1] = state->spkey[1];
967 ret->sgkey[0] = state->sgkey[0];
968 ret->sgkey[1] = state->sgkey[1];
969 ret->previous = state->previous;
970 ret->angle = state->angle;
971 ret->completed = state->completed;
972 ret->movecount = state->movecount;
977 static void free_game(game_state *state)
982 static game_ui *new_ui(game_state *state)
987 static void free_ui(game_ui *ui)
991 static game_state *make_move(game_state *from, game_ui *ui,
992 int x, int y, int button)
995 int pkey[2], skey[2], dkey[2];
999 int i, j, dest, mask;
1003 * All moves are made with the cursor keys.
1005 if (button == CURSOR_UP)
1007 else if (button == CURSOR_DOWN)
1009 else if (button == CURSOR_LEFT)
1011 else if (button == CURSOR_RIGHT)
1013 else if (button == CURSOR_UP_LEFT)
1014 direction = UP_LEFT;
1015 else if (button == CURSOR_DOWN_LEFT)
1016 direction = DOWN_LEFT;
1017 else if (button == CURSOR_UP_RIGHT)
1018 direction = UP_RIGHT;
1019 else if (button == CURSOR_DOWN_RIGHT)
1020 direction = DOWN_RIGHT;
1025 * Find the two points in the current grid square which
1026 * correspond to this move.
1028 mask = from->squares[from->current].directions[direction];
1031 for (i = j = 0; i < from->squares[from->current].npoints; i++)
1032 if (mask & (1 << i)) {
1033 points[j*2] = from->squares[from->current].points[i*2];
1034 points[j*2+1] = from->squares[from->current].points[i*2+1];
1041 * Now find the other grid square which shares those points.
1042 * This is our move destination.
1045 for (i = 0; i < from->nsquares; i++)
1046 if (i != from->current) {
1050 for (j = 0; j < from->squares[i].npoints; j++) {
1051 dist = (SQ(from->squares[i].points[j*2] - points[0]) +
1052 SQ(from->squares[i].points[j*2+1] - points[1]));
1055 dist = (SQ(from->squares[i].points[j*2] - points[2]) +
1056 SQ(from->squares[i].points[j*2+1] - points[3]));
1070 ret = dup_game(from);
1074 * So we know what grid square we're aiming for, and we also
1075 * know the two key points (as indices in both the source and
1076 * destination grid squares) which are invariant between source
1079 * Next we must roll the polyhedron on to that square. So we
1080 * find the indices of the key points within the polyhedron's
1081 * vertex array, then use those in a call to transform_poly,
1082 * and align the result on the new grid square.
1086 align_poly(from->solid, &from->squares[from->current], all_pkey);
1087 pkey[0] = all_pkey[skey[0]];
1088 pkey[1] = all_pkey[skey[1]];
1090 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1096 * Now find the angle through which to rotate the polyhedron.
1097 * Do this by finding the two faces that share the two vertices
1098 * we've found, and taking the dot product of their normals.
1104 for (i = 0; i < from->solid->nfaces; i++) {
1106 for (j = 0; j < from->solid->order; j++)
1107 if (from->solid->faces[i*from->solid->order + j] == pkey[0] ||
1108 from->solid->faces[i*from->solid->order + j] == pkey[1])
1119 for (i = 0; i < 3; i++)
1120 dp += (from->solid->normals[f[0]*3+i] *
1121 from->solid->normals[f[1]*3+i]);
1122 angle = (float)acos(dp);
1126 * Now transform the polyhedron. We aren't entirely sure
1127 * whether we need to rotate through angle or -angle, and the
1128 * simplest way round this is to try both and see which one
1129 * aligns successfully!
1131 * Unfortunately, _both_ will align successfully if this is a
1132 * cube, which won't tell us anything much. So for that
1133 * particular case, I resort to gross hackery: I simply negate
1134 * the angle before trying the alignment, depending on the
1135 * direction. Which directions work which way is determined by
1136 * pure trial and error. I said it was gross :-/
1142 if (from->solid->order == 4 && direction == UP)
1143 angle = -angle; /* HACK */
1145 poly = transform_poly(from->solid,
1146 from->squares[from->current].flip,
1147 pkey[0], pkey[1], angle);
1148 flip_poly(poly, from->squares[ret->current].flip);
1149 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1153 poly = transform_poly(from->solid,
1154 from->squares[from->current].flip,
1155 pkey[0], pkey[1], angle);
1156 flip_poly(poly, from->squares[ret->current].flip);
1157 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1164 * Now we have our rotated polyhedron, which we expect to be
1165 * exactly congruent to the one we started with - but with the
1166 * faces permuted. So we map that congruence and thereby figure
1167 * out how to permute the faces as a result of the polyhedron
1171 int *newcolours = snewn(from->solid->nfaces, int);
1173 for (i = 0; i < from->solid->nfaces; i++)
1176 for (i = 0; i < from->solid->nfaces; i++) {
1180 * Now go through the transformed polyhedron's faces
1181 * and figure out which one's normal is approximately
1182 * equal to this one.
1184 for (j = 0; j < poly->nfaces; j++) {
1190 for (k = 0; k < 3; k++)
1191 dist += SQ(poly->normals[j*3+k] -
1192 from->solid->normals[i*3+k]);
1194 if (APPROXEQ(dist, 0)) {
1196 newcolours[i] = ret->facecolours[j];
1200 assert(nmatch == 1);
1203 for (i = 0; i < from->solid->nfaces; i++)
1204 assert(newcolours[i] != -1);
1206 sfree(ret->facecolours);
1207 ret->facecolours = newcolours;
1213 * And finally, swap the colour between the bottom face of the
1214 * polyhedron and the face we've just landed on.
1216 * We don't do this if the game is already complete, since we
1217 * allow the user to roll the fully blue polyhedron around the
1218 * grid as a feeble reward.
1220 if (!ret->completed) {
1221 i = lowest_face(from->solid);
1222 j = ret->facecolours[i];
1223 ret->facecolours[i] = ret->squares[ret->current].blue;
1224 ret->squares[ret->current].blue = j;
1227 * Detect game completion.
1230 for (i = 0; i < ret->solid->nfaces; i++)
1231 if (ret->facecolours[i])
1233 if (j == ret->solid->nfaces)
1234 ret->completed = ret->movecount;
1240 * Align the normal polyhedron with its grid square, to get key
1241 * points for non-animated display.
1247 success = align_poly(ret->solid, &ret->squares[ret->current], pkey);
1250 ret->dpkey[0] = pkey[0];
1251 ret->dpkey[1] = pkey[1];
1257 ret->spkey[0] = pkey[0];
1258 ret->spkey[1] = pkey[1];
1259 ret->sgkey[0] = skey[0];
1260 ret->sgkey[1] = skey[1];
1261 ret->previous = from->current;
1267 /* ----------------------------------------------------------------------
1275 struct game_drawstate {
1276 int ox, oy; /* pixel position of float origin */
1279 static void find_bbox_callback(void *ctx, struct grid_square *sq)
1281 struct bbox *bb = (struct bbox *)ctx;
1284 for (i = 0; i < sq->npoints; i++) {
1285 if (bb->l > sq->points[i*2]) bb->l = sq->points[i*2];
1286 if (bb->r < sq->points[i*2]) bb->r = sq->points[i*2];
1287 if (bb->u > sq->points[i*2+1]) bb->u = sq->points[i*2+1];
1288 if (bb->d < sq->points[i*2+1]) bb->d = sq->points[i*2+1];
1292 static struct bbox find_bbox(game_params *params)
1297 * These should be hugely more than the real bounding box will
1300 bb.l = 2.0F * (params->d1 + params->d2);
1301 bb.r = -2.0F * (params->d1 + params->d2);
1302 bb.u = 2.0F * (params->d1 + params->d2);
1303 bb.d = -2.0F * (params->d1 + params->d2);
1304 enum_grid_squares(params, find_bbox_callback, &bb);
1309 static void game_size(game_params *params, int *x, int *y)
1311 struct bbox bb = find_bbox(params);
1312 *x = (int)((bb.r - bb.l + 2*solids[params->solid]->border) * GRID_SCALE);
1313 *y = (int)((bb.d - bb.u + 2*solids[params->solid]->border) * GRID_SCALE);
1316 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1318 float *ret = snewn(3 * NCOLOURS, float);
1320 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1322 ret[COL_BORDER * 3 + 0] = 0.0;
1323 ret[COL_BORDER * 3 + 1] = 0.0;
1324 ret[COL_BORDER * 3 + 2] = 0.0;
1326 ret[COL_BLUE * 3 + 0] = 0.0;
1327 ret[COL_BLUE * 3 + 1] = 0.0;
1328 ret[COL_BLUE * 3 + 2] = 1.0;
1330 *ncolours = NCOLOURS;
1334 static game_drawstate *game_new_drawstate(game_state *state)
1336 struct game_drawstate *ds = snew(struct game_drawstate);
1337 struct bbox bb = find_bbox(&state->params);
1339 ds->ox = (int)(-(bb.l - state->solid->border) * GRID_SCALE);
1340 ds->oy = (int)(-(bb.u - state->solid->border) * GRID_SCALE);
1345 static void game_free_drawstate(game_drawstate *ds)
1350 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1351 game_state *state, int dir, game_ui *ui,
1352 float animtime, float flashtime)
1355 struct bbox bb = find_bbox(&state->params);
1360 game_state *newstate;
1363 draw_rect(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1364 (int)((bb.d-bb.u+2.0F) * GRID_SCALE), COL_BACKGROUND);
1370 * This is an Undo. So reverse the order of the states, and
1371 * run the roll timer backwards.
1379 animtime = ROLLTIME - animtime;
1385 square = state->current;
1386 pkey = state->dpkey;
1387 gkey = state->dgkey;
1389 angle = state->angle * animtime / ROLLTIME;
1390 square = state->previous;
1391 pkey = state->spkey;
1392 gkey = state->sgkey;
1397 for (i = 0; i < state->nsquares; i++) {
1400 for (j = 0; j < state->squares[i].npoints; j++) {
1401 coords[2*j] = ((int)(state->squares[i].points[2*j] * GRID_SCALE)
1403 coords[2*j+1] = ((int)(state->squares[i].points[2*j+1]*GRID_SCALE)
1407 draw_polygon(fe, coords, state->squares[i].npoints, TRUE,
1408 state->squares[i].blue ? COL_BLUE : COL_BACKGROUND);
1409 draw_polygon(fe, coords, state->squares[i].npoints, FALSE, COL_BORDER);
1413 * Now compute and draw the polyhedron.
1415 poly = transform_poly(state->solid, state->squares[square].flip,
1416 pkey[0], pkey[1], angle);
1419 * Compute the translation required to align the two key points
1420 * on the polyhedron with the same key points on the current
1423 for (i = 0; i < 3; i++) {
1426 for (j = 0; j < 2; j++) {
1431 state->squares[square].points[gkey[j]*2+i];
1436 tc += (grid_coord - poly->vertices[pkey[j]*3+i]);
1441 for (i = 0; i < poly->nvertices; i++)
1442 for (j = 0; j < 3; j++)
1443 poly->vertices[i*3+j] += t[j];
1446 * Now actually draw each face.
1448 for (i = 0; i < poly->nfaces; i++) {
1452 for (j = 0; j < poly->order; j++) {
1453 int f = poly->faces[i*poly->order + j];
1454 points[j*2] = (poly->vertices[f*3+0] -
1455 poly->vertices[f*3+2] * poly->shear);
1456 points[j*2+1] = (poly->vertices[f*3+1] -
1457 poly->vertices[f*3+2] * poly->shear);
1460 for (j = 0; j < poly->order; j++) {
1461 coords[j*2] = (int)floor(points[j*2] * GRID_SCALE) + ds->ox;
1462 coords[j*2+1] = (int)floor(points[j*2+1] * GRID_SCALE) + ds->oy;
1466 * Find out whether these points are in a clockwise or
1467 * anticlockwise arrangement. If the latter, discard the
1468 * face because it's facing away from the viewer.
1470 * This would involve fiddly winding-number stuff for a
1471 * general polygon, but for the simple parallelograms we'll
1472 * be seeing here, all we have to do is check whether the
1473 * corners turn right or left. So we'll take the vector
1474 * from point 0 to point 1, turn it right 90 degrees,
1475 * and check the sign of the dot product with that and the
1476 * next vector (point 1 to point 2).
1479 float v1x = points[2]-points[0];
1480 float v1y = points[3]-points[1];
1481 float v2x = points[4]-points[2];
1482 float v2y = points[5]-points[3];
1483 float dp = v1x * v2y - v1y * v2x;
1489 draw_polygon(fe, coords, poly->order, TRUE,
1490 state->facecolours[i] ? COL_BLUE : COL_BACKGROUND);
1491 draw_polygon(fe, coords, poly->order, FALSE, COL_BORDER);
1495 draw_update(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1496 (int)((bb.d-bb.u+2.0F) * GRID_SCALE));
1499 * Update the status bar.
1502 char statusbuf[256];
1504 sprintf(statusbuf, "%sMoves: %d",
1505 (state->completed ? "COMPLETED! " : ""),
1506 (state->completed ? state->completed : state->movecount));
1508 status_bar(fe, statusbuf);
1512 static float game_anim_length(game_state *oldstate,
1513 game_state *newstate, int dir)
1518 static float game_flash_length(game_state *oldstate,
1519 game_state *newstate, int dir)
1524 static int game_wants_statusbar(void)
1530 #define thegame cube
1533 const struct game thegame = {
1534 "Cube", "games.cube", TRUE,
1555 game_free_drawstate,
1559 game_wants_statusbar,