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 s_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 s_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 s_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 s_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 &s_tetrahedron, &s_cube, &s_octahedron, &s_icosahedron
158 enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
160 #define PREFERRED_GRID_SCALE 48
161 #define GRID_SCALE (ds->gridscale)
162 #define ROLLTIME 0.13F
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 */
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).
197 typedef struct game_grid game_grid;
200 struct grid_square *squares;
204 #define SET_SQUARE(state, i, val) \
205 ((state)->bluemask[(i)/32] &= ~(1 << ((i)%32)), \
206 (state)->bluemask[(i)/32] |= ((!!val) << ((i)%32)))
207 #define GET_SQUARE(state, i) \
208 (((state)->bluemask[(i)/32] >> ((i)%32)) & 1)
211 struct game_params params;
212 const struct solid *solid;
215 unsigned long *bluemask;
216 int current; /* index of current grid square */
217 int sgkey[2]; /* key-point indices into grid sq */
218 int dgkey[2]; /* key-point indices into grid sq */
219 int spkey[2]; /* key-point indices into polyhedron */
220 int dpkey[2]; /* key-point indices into polyhedron */
227 static game_params *default_params(void)
229 game_params *ret = snew(game_params);
238 static int game_fetch_preset(int i, char **name, game_params **params)
240 game_params *ret = snew(game_params);
252 ret->solid = TETRAHEDRON;
258 ret->solid = OCTAHEDRON;
264 ret->solid = ICOSAHEDRON;
278 static void free_params(game_params *params)
283 static game_params *dup_params(const game_params *params)
285 game_params *ret = snew(game_params);
286 *ret = *params; /* structure copy */
290 static void decode_params(game_params *ret, char const *string)
293 case 't': ret->solid = TETRAHEDRON; string++; break;
294 case 'c': ret->solid = CUBE; string++; break;
295 case 'o': ret->solid = OCTAHEDRON; string++; break;
296 case 'i': ret->solid = ICOSAHEDRON; string++; break;
299 ret->d1 = ret->d2 = atoi(string);
300 while (*string && isdigit((unsigned char)*string)) string++;
301 if (*string == 'x') {
303 ret->d2 = atoi(string);
307 static char *encode_params(const game_params *params, int full)
311 assert(params->solid >= 0 && params->solid < 4);
312 sprintf(data, "%c%dx%d", "tcoi"[params->solid], params->d1, params->d2);
316 typedef void (*egc_callback)(void *, struct grid_square *);
318 static void enum_grid_squares(const game_params *params, egc_callback callback,
321 const struct solid *solid = solids[params->solid];
323 if (solid->order == 4) {
326 for (y = 0; y < params->d2; y++)
327 for (x = 0; x < params->d1; x++) {
328 struct grid_square sq;
332 sq.points[0] = x - 0.5F;
333 sq.points[1] = y - 0.5F;
334 sq.points[2] = x - 0.5F;
335 sq.points[3] = y + 0.5F;
336 sq.points[4] = x + 0.5F;
337 sq.points[5] = y + 0.5F;
338 sq.points[6] = x + 0.5F;
339 sq.points[7] = y - 0.5F;
342 sq.directions[LEFT] = 0x03; /* 0,1 */
343 sq.directions[RIGHT] = 0x0C; /* 2,3 */
344 sq.directions[UP] = 0x09; /* 0,3 */
345 sq.directions[DOWN] = 0x06; /* 1,2 */
346 sq.directions[UP_LEFT] = 0; /* no diagonals in a square */
347 sq.directions[UP_RIGHT] = 0; /* no diagonals in a square */
348 sq.directions[DOWN_LEFT] = 0; /* no diagonals in a square */
349 sq.directions[DOWN_RIGHT] = 0; /* no diagonals in a square */
354 * This is supremely irrelevant, but just to avoid
355 * having any uninitialised structure members...
362 int row, rowlen, other, i, firstix = -1;
363 float theight = (float)(sqrt(3) / 2.0);
365 for (row = 0; row < params->d1 + params->d2; row++) {
366 if (row < params->d2) {
368 rowlen = row + params->d1;
371 rowlen = 2*params->d2 + params->d1 - row;
375 * There are `rowlen' down-pointing triangles.
377 for (i = 0; i < rowlen; i++) {
378 struct grid_square sq;
382 ix = (2 * i - (rowlen-1));
386 sq.y = y + theight / 3;
387 sq.points[0] = x - 0.5F;
390 sq.points[3] = y + theight;
391 sq.points[4] = x + 0.5F;
395 sq.directions[LEFT] = 0x03; /* 0,1 */
396 sq.directions[RIGHT] = 0x06; /* 1,2 */
397 sq.directions[UP] = 0x05; /* 0,2 */
398 sq.directions[DOWN] = 0; /* invalid move */
401 * Down-pointing triangle: both the up diagonals go
402 * up, and the down ones go left and right.
404 sq.directions[UP_LEFT] = sq.directions[UP_RIGHT] =
406 sq.directions[DOWN_LEFT] = sq.directions[LEFT];
407 sq.directions[DOWN_RIGHT] = sq.directions[RIGHT];
414 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
420 * There are `rowlen+other' up-pointing triangles.
422 for (i = 0; i < rowlen+other; i++) {
423 struct grid_square sq;
427 ix = (2 * i - (rowlen+other-1));
431 sq.y = y + 2*theight / 3;
432 sq.points[0] = x + 0.5F;
433 sq.points[1] = y + theight;
436 sq.points[4] = x - 0.5F;
437 sq.points[5] = y + theight;
440 sq.directions[LEFT] = 0x06; /* 1,2 */
441 sq.directions[RIGHT] = 0x03; /* 0,1 */
442 sq.directions[DOWN] = 0x05; /* 0,2 */
443 sq.directions[UP] = 0; /* invalid move */
446 * Up-pointing triangle: both the down diagonals go
447 * down, and the up ones go left and right.
449 sq.directions[DOWN_LEFT] = sq.directions[DOWN_RIGHT] =
451 sq.directions[UP_LEFT] = sq.directions[LEFT];
452 sq.directions[UP_RIGHT] = sq.directions[RIGHT];
457 firstix = (ix - 1) & 3;
459 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
467 static int grid_area(int d1, int d2, int order)
470 * An NxM grid of squares has NM squares in it.
472 * A grid of triangles with dimensions A and B has a total of
473 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
474 * a side-A triangle containing A^2 subtriangles, a side-B
475 * triangle containing B^2, and two congruent parallelograms,
476 * each with side lengths A and B, each therefore containing AB
477 * two-triangle rhombuses.)
482 return d1*d1 + d2*d2 + 4*d1*d2;
485 static config_item *game_configure(const game_params *params)
487 config_item *ret = snewn(4, config_item);
490 ret[0].name = "Type of solid";
491 ret[0].type = C_CHOICES;
492 ret[0].sval = ":Tetrahedron:Cube:Octahedron:Icosahedron";
493 ret[0].ival = params->solid;
495 ret[1].name = "Width / top";
496 ret[1].type = C_STRING;
497 sprintf(buf, "%d", params->d1);
498 ret[1].sval = dupstr(buf);
501 ret[2].name = "Height / bottom";
502 ret[2].type = C_STRING;
503 sprintf(buf, "%d", params->d2);
504 ret[2].sval = dupstr(buf);
515 static game_params *custom_params(const config_item *cfg)
517 game_params *ret = snew(game_params);
519 ret->solid = cfg[0].ival;
520 ret->d1 = atoi(cfg[1].sval);
521 ret->d2 = atoi(cfg[2].sval);
526 static void count_grid_square_callback(void *ctx, struct grid_square *sq)
528 int *classes = (int *)ctx;
532 thisclass = sq->tetra_class;
533 else if (classes[4] == 2)
534 thisclass = sq->flip;
538 classes[thisclass]++;
541 static char *validate_params(const game_params *params, int full)
546 if (params->solid < 0 || params->solid >= lenof(solids))
547 return "Unrecognised solid type";
549 if (solids[params->solid]->order == 4) {
550 if (params->d1 <= 0 || params->d2 <= 0)
551 return "Both grid dimensions must be greater than zero";
553 if (params->d1 <= 0 && params->d2 <= 0)
554 return "At least one grid dimension must be greater than zero";
557 for (i = 0; i < 4; i++)
559 if (params->solid == TETRAHEDRON)
561 else if (params->solid == OCTAHEDRON)
565 enum_grid_squares(params, count_grid_square_callback, classes);
567 for (i = 0; i < classes[4]; i++)
568 if (classes[i] < solids[params->solid]->nfaces / classes[4])
569 return "Not enough grid space to place all blue faces";
571 if (grid_area(params->d1, params->d2, solids[params->solid]->order) <
572 solids[params->solid]->nfaces + 1)
573 return "Not enough space to place the solid on an empty square";
585 static void classify_grid_square_callback(void *ctx, struct grid_square *sq)
587 struct grid_data *data = (struct grid_data *)ctx;
590 if (data->nclasses == 4)
591 thisclass = sq->tetra_class;
592 else if (data->nclasses == 2)
593 thisclass = sq->flip;
597 data->gridptrs[thisclass][data->nsquares[thisclass]++] =
601 static char *new_game_desc(const game_params *params, random_state *rs,
602 char **aux, int interactive)
604 struct grid_data data;
605 int i, j, k, m, area, facesperclass;
610 * Enumerate the grid squares, dividing them into equivalence
611 * classes as appropriate. (For the tetrahedron, there is one
612 * equivalence class for each face; for the octahedron there
613 * are two classes; for the other two solids there's only one.)
616 area = grid_area(params->d1, params->d2, solids[params->solid]->order);
617 if (params->solid == TETRAHEDRON)
619 else if (params->solid == OCTAHEDRON)
623 data.gridptrs[0] = snewn(data.nclasses * area, int);
624 for (i = 0; i < data.nclasses; i++) {
625 data.gridptrs[i] = data.gridptrs[0] + i * area;
626 data.nsquares[i] = 0;
628 data.squareindex = 0;
629 enum_grid_squares(params, classify_grid_square_callback, &data);
631 facesperclass = solids[params->solid]->nfaces / data.nclasses;
633 for (i = 0; i < data.nclasses; i++)
634 assert(data.nsquares[i] >= facesperclass);
635 assert(data.squareindex == area);
638 * So now we know how many faces to allocate in each class. Get
641 flags = snewn(area, int);
642 for (i = 0; i < area; i++)
645 for (i = 0; i < data.nclasses; i++) {
646 for (j = 0; j < facesperclass; j++) {
647 int n = random_upto(rs, data.nsquares[i]);
649 assert(!flags[data.gridptrs[i][n]]);
650 flags[data.gridptrs[i][n]] = TRUE;
653 * Move everything else up the array. I ought to use a
654 * better data structure for this, but for such small
655 * numbers it hardly seems worth the effort.
657 while (n < data.nsquares[i]-1) {
658 data.gridptrs[i][n] = data.gridptrs[i][n+1];
666 * Now we know precisely which squares are blue. Encode this
667 * information in hex. While we're looping over this, collect
668 * the non-blue squares into a list in the now-unused gridptrs
671 desc = snewn(area / 4 + 40, char);
676 for (i = 0; i < area; i++) {
680 data.gridptrs[0][m++] = i;
684 *p++ = "0123456789ABCDEF"[j];
690 *p++ = "0123456789ABCDEF"[j];
693 * Choose a non-blue square for the polyhedron.
695 sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]);
697 sfree(data.gridptrs[0]);
703 static void add_grid_square_callback(void *ctx, struct grid_square *sq)
705 game_grid *grid = (game_grid *)ctx;
707 grid->squares[grid->nsquares++] = *sq; /* structure copy */
710 static int lowest_face(const struct solid *solid)
717 for (i = 0; i < solid->nfaces; i++) {
720 for (j = 0; j < solid->order; j++) {
721 int f = solid->faces[i*solid->order + j];
722 z += solid->vertices[f*3+2];
725 if (i == 0 || zmin > z) {
734 static int align_poly(const struct solid *solid, struct grid_square *sq,
739 int flip = (sq->flip ? -1 : +1);
742 * First, find the lowest z-coordinate present in the solid.
745 for (i = 0; i < solid->nvertices; i++)
746 if (zmin > solid->vertices[i*3+2])
747 zmin = solid->vertices[i*3+2];
750 * Now go round the grid square. For each point in the grid
751 * square, we're looking for a point of the polyhedron with the
752 * same x- and y-coordinates (relative to the square's centre),
753 * and z-coordinate equal to zmin (near enough).
755 for (j = 0; j < sq->npoints; j++) {
761 for (i = 0; i < solid->nvertices; i++) {
764 dist += SQ(solid->vertices[i*3+0] * flip - sq->points[j*2+0] + sq->x);
765 dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y);
766 dist += SQ(solid->vertices[i*3+2] - zmin);
774 if (matches != 1 || index < 0)
782 static void flip_poly(struct solid *solid, int flip)
787 for (i = 0; i < solid->nvertices; i++) {
788 solid->vertices[i*3+0] *= -1;
789 solid->vertices[i*3+1] *= -1;
791 for (i = 0; i < solid->nfaces; i++) {
792 solid->normals[i*3+0] *= -1;
793 solid->normals[i*3+1] *= -1;
798 static struct solid *transform_poly(const struct solid *solid, int flip,
799 int key0, int key1, float angle)
801 struct solid *ret = snew(struct solid);
802 float vx, vy, ax, ay;
803 float vmatrix[9], amatrix[9], vmatrix2[9];
806 *ret = *solid; /* structure copy */
808 flip_poly(ret, flip);
811 * Now rotate the polyhedron through the given angle. We must
812 * rotate about the Z-axis to bring the two vertices key0 and
813 * key1 into horizontal alignment, then rotate about the
814 * X-axis, then rotate back again.
816 vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0];
817 vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1];
818 assert(APPROXEQ(vx*vx + vy*vy, 1.0));
820 vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0;
821 vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0;
822 vmatrix[2] = 0; vmatrix[5] = 0; vmatrix[8] = 1;
824 ax = (float)cos(angle);
825 ay = (float)sin(angle);
827 amatrix[0] = 1; amatrix[3] = 0; amatrix[6] = 0;
828 amatrix[1] = 0; amatrix[4] = ax; amatrix[7] = ay;
829 amatrix[2] = 0; amatrix[5] = -ay; amatrix[8] = ax;
831 memcpy(vmatrix2, vmatrix, sizeof(vmatrix));
835 for (i = 0; i < ret->nvertices; i++) {
836 MATMUL(ret->vertices + 3*i, vmatrix, ret->vertices + 3*i);
837 MATMUL(ret->vertices + 3*i, amatrix, ret->vertices + 3*i);
838 MATMUL(ret->vertices + 3*i, vmatrix2, ret->vertices + 3*i);
840 for (i = 0; i < ret->nfaces; i++) {
841 MATMUL(ret->normals + 3*i, vmatrix, ret->normals + 3*i);
842 MATMUL(ret->normals + 3*i, amatrix, ret->normals + 3*i);
843 MATMUL(ret->normals + 3*i, vmatrix2, ret->normals + 3*i);
849 static char *validate_desc(const game_params *params, const char *desc)
851 int area = grid_area(params->d1, params->d2, solids[params->solid]->order);
855 for (j = 0; j < i; j++) {
857 if (c >= '0' && c <= '9') continue;
858 if (c >= 'A' && c <= 'F') continue;
859 if (c >= 'a' && c <= 'f') continue;
860 return "Not enough hex digits at start of string";
861 /* NB if desc[j]=='\0' that will also be caught here, so we're safe */
865 return "Expected ',' after hex digits";
869 if (desc[i] < '0' || desc[i] > '9')
870 return "Expected decimal integer after ','";
877 static game_state *new_game(midend *me, const game_params *params,
880 game_grid *grid = snew(game_grid);
881 game_state *state = snew(game_state);
884 state->params = *params; /* structure copy */
885 state->solid = solids[params->solid];
887 area = grid_area(params->d1, params->d2, state->solid->order);
888 grid->squares = snewn(area, struct grid_square);
890 enum_grid_squares(params, add_grid_square_callback, grid);
891 assert(grid->nsquares == area);
895 state->facecolours = snewn(state->solid->nfaces, int);
896 memset(state->facecolours, 0, state->solid->nfaces * sizeof(int));
898 state->bluemask = snewn((state->grid->nsquares + 31) / 32, unsigned long);
899 memset(state->bluemask, 0, (state->grid->nsquares + 31) / 32 *
900 sizeof(unsigned long));
903 * Set up the blue squares and polyhedron position according to
904 * the game description.
907 const char *p = desc;
912 for (i = 0; i < state->grid->nsquares; i++) {
915 if (v >= '0' && v <= '9')
917 else if (v >= 'A' && v <= 'F')
919 else if (v >= 'a' && v <= 'f')
925 SET_SQUARE(state, i, TRUE);
934 state->current = atoi(p);
935 if (state->current < 0 || state->current >= state->grid->nsquares)
936 state->current = 0; /* got to do _something_ */
940 * Align the polyhedron with its grid square and determine
941 * initial key points.
947 ret = align_poly(state->solid, &state->grid->squares[state->current], pkey);
950 state->dpkey[0] = state->spkey[0] = pkey[0];
951 state->dpkey[1] = state->spkey[0] = pkey[1];
952 state->dgkey[0] = state->sgkey[0] = 0;
953 state->dgkey[1] = state->sgkey[0] = 1;
956 state->previous = state->current;
958 state->completed = 0;
959 state->movecount = 0;
964 static game_state *dup_game(const game_state *state)
966 game_state *ret = snew(game_state);
968 ret->params = state->params; /* structure copy */
969 ret->solid = state->solid;
970 ret->facecolours = snewn(ret->solid->nfaces, int);
971 memcpy(ret->facecolours, state->facecolours,
972 ret->solid->nfaces * sizeof(int));
973 ret->current = state->current;
974 ret->grid = state->grid;
975 ret->grid->refcount++;
976 ret->bluemask = snewn((ret->grid->nsquares + 31) / 32, unsigned long);
977 memcpy(ret->bluemask, state->bluemask, (ret->grid->nsquares + 31) / 32 *
978 sizeof(unsigned long));
979 ret->dpkey[0] = state->dpkey[0];
980 ret->dpkey[1] = state->dpkey[1];
981 ret->dgkey[0] = state->dgkey[0];
982 ret->dgkey[1] = state->dgkey[1];
983 ret->spkey[0] = state->spkey[0];
984 ret->spkey[1] = state->spkey[1];
985 ret->sgkey[0] = state->sgkey[0];
986 ret->sgkey[1] = state->sgkey[1];
987 ret->previous = state->previous;
988 ret->angle = state->angle;
989 ret->completed = state->completed;
990 ret->movecount = state->movecount;
995 static void free_game(game_state *state)
997 if (--state->grid->refcount <= 0) {
998 sfree(state->grid->squares);
1001 sfree(state->bluemask);
1002 sfree(state->facecolours);
1006 static char *solve_game(const game_state *state, const game_state *currstate,
1007 const char *aux, char **error)
1012 static int game_can_format_as_text_now(const game_params *params)
1017 static char *game_text_format(const game_state *state)
1022 static game_ui *new_ui(const game_state *state)
1027 static void free_ui(game_ui *ui)
1031 static char *encode_ui(const game_ui *ui)
1036 static void decode_ui(game_ui *ui, const char *encoding)
1040 static void game_changed_state(game_ui *ui, const game_state *oldstate,
1041 const game_state *newstate)
1045 struct game_drawstate {
1047 int ox, oy; /* pixel position of float origin */
1051 * Code shared between interpret_move() and execute_move().
1053 static int find_move_dest(const game_state *from, int direction,
1054 int *skey, int *dkey)
1056 int mask, dest, i, j;
1060 * Find the two points in the current grid square which
1061 * correspond to this move.
1063 mask = from->grid->squares[from->current].directions[direction];
1066 for (i = j = 0; i < from->grid->squares[from->current].npoints; i++)
1067 if (mask & (1 << i)) {
1068 points[j*2] = from->grid->squares[from->current].points[i*2];
1069 points[j*2+1] = from->grid->squares[from->current].points[i*2+1];
1076 * Now find the other grid square which shares those points.
1077 * This is our move destination.
1080 for (i = 0; i < from->grid->nsquares; i++)
1081 if (i != from->current) {
1085 for (j = 0; j < from->grid->squares[i].npoints; j++) {
1086 dist = (SQ(from->grid->squares[i].points[j*2] - points[0]) +
1087 SQ(from->grid->squares[i].points[j*2+1] - points[1]));
1090 dist = (SQ(from->grid->squares[i].points[j*2] - points[2]) +
1091 SQ(from->grid->squares[i].points[j*2+1] - points[3]));
1105 static char *interpret_move(const game_state *state, game_ui *ui,
1106 const game_drawstate *ds,
1107 int x, int y, int button)
1109 int direction, mask, i;
1110 int skey[2], dkey[2];
1112 button = button & (~MOD_MASK | MOD_NUM_KEYPAD);
1115 * Moves can be made with the cursor keys or numeric keypad, or
1116 * alternatively you can left-click and the polyhedron will
1117 * move in the general direction of the mouse pointer.
1119 if (button == CURSOR_UP || button == (MOD_NUM_KEYPAD | '8'))
1121 else if (button == CURSOR_DOWN || button == (MOD_NUM_KEYPAD | '2'))
1123 else if (button == CURSOR_LEFT || button == (MOD_NUM_KEYPAD | '4'))
1125 else if (button == CURSOR_RIGHT || button == (MOD_NUM_KEYPAD | '6'))
1127 else if (button == (MOD_NUM_KEYPAD | '7'))
1128 direction = UP_LEFT;
1129 else if (button == (MOD_NUM_KEYPAD | '1'))
1130 direction = DOWN_LEFT;
1131 else if (button == (MOD_NUM_KEYPAD | '9'))
1132 direction = UP_RIGHT;
1133 else if (button == (MOD_NUM_KEYPAD | '3'))
1134 direction = DOWN_RIGHT;
1135 else if (button == LEFT_BUTTON) {
1137 * Find the bearing of the click point from the current
1143 cx = (int)(state->grid->squares[state->current].x * GRID_SCALE) + ds->ox;
1144 cy = (int)(state->grid->squares[state->current].y * GRID_SCALE) + ds->oy;
1146 if (x == cx && y == cy)
1147 return NULL; /* clicked in exact centre! */
1148 angle = atan2(y - cy, x - cx);
1151 * There are three possibilities.
1153 * - This square is a square, so we choose between UP,
1154 * DOWN, LEFT and RIGHT by dividing the available angle
1155 * at the 45-degree points.
1157 * - This square is an up-pointing triangle, so we choose
1158 * between DOWN, LEFT and RIGHT by dividing into
1161 * - This square is a down-pointing triangle, so we choose
1162 * between UP, LEFT and RIGHT in the inverse manner.
1164 * Don't forget that since our y-coordinates increase
1165 * downwards, `angle' is measured _clockwise_ from the
1166 * x-axis, not anticlockwise as most mathematicians would
1167 * instinctively assume.
1169 if (state->grid->squares[state->current].npoints == 4) {
1171 if (fabs(angle) > 3*PI/4)
1173 else if (fabs(angle) < PI/4)
1179 } else if (state->grid->squares[state->current].directions[UP] == 0) {
1180 /* Up-pointing triangle. */
1181 if (angle < -PI/2 || angle > 5*PI/6)
1183 else if (angle > PI/6)
1188 /* Down-pointing triangle. */
1189 assert(state->grid->squares[state->current].directions[DOWN] == 0);
1190 if (angle > PI/2 || angle < -5*PI/6)
1192 else if (angle < -PI/6)
1200 mask = state->grid->squares[state->current].directions[direction];
1205 * Translate diagonal directions into orthogonal ones.
1207 if (direction > DOWN) {
1208 for (i = LEFT; i <= DOWN; i++)
1209 if (state->grid->squares[state->current].directions[i] == mask) {
1213 assert(direction <= DOWN);
1216 if (find_move_dest(state, direction, skey, dkey) < 0)
1219 if (direction == LEFT) return dupstr("L");
1220 if (direction == RIGHT) return dupstr("R");
1221 if (direction == UP) return dupstr("U");
1222 if (direction == DOWN) return dupstr("D");
1224 return NULL; /* should never happen */
1227 static game_state *execute_move(const game_state *from, const char *move)
1233 int skey[2], dkey[2];
1238 case 'L': direction = LEFT; break;
1239 case 'R': direction = RIGHT; break;
1240 case 'U': direction = UP; break;
1241 case 'D': direction = DOWN; break;
1242 default: return NULL;
1245 dest = find_move_dest(from, direction, skey, dkey);
1249 ret = dup_game(from);
1250 ret->current = dest;
1253 * So we know what grid square we're aiming for, and we also
1254 * know the two key points (as indices in both the source and
1255 * destination grid squares) which are invariant between source
1258 * Next we must roll the polyhedron on to that square. So we
1259 * find the indices of the key points within the polyhedron's
1260 * vertex array, then use those in a call to transform_poly,
1261 * and align the result on the new grid square.
1265 align_poly(from->solid, &from->grid->squares[from->current], all_pkey);
1266 pkey[0] = all_pkey[skey[0]];
1267 pkey[1] = all_pkey[skey[1]];
1269 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1275 * Now find the angle through which to rotate the polyhedron.
1276 * Do this by finding the two faces that share the two vertices
1277 * we've found, and taking the dot product of their normals.
1283 for (i = 0; i < from->solid->nfaces; i++) {
1285 for (j = 0; j < from->solid->order; j++)
1286 if (from->solid->faces[i*from->solid->order + j] == pkey[0] ||
1287 from->solid->faces[i*from->solid->order + j] == pkey[1])
1298 for (i = 0; i < 3; i++)
1299 dp += (from->solid->normals[f[0]*3+i] *
1300 from->solid->normals[f[1]*3+i]);
1301 angle = (float)acos(dp);
1305 * Now transform the polyhedron. We aren't entirely sure
1306 * whether we need to rotate through angle or -angle, and the
1307 * simplest way round this is to try both and see which one
1308 * aligns successfully!
1310 * Unfortunately, _both_ will align successfully if this is a
1311 * cube, which won't tell us anything much. So for that
1312 * particular case, I resort to gross hackery: I simply negate
1313 * the angle before trying the alignment, depending on the
1314 * direction. Which directions work which way is determined by
1315 * pure trial and error. I said it was gross :-/
1321 if (from->solid->order == 4 && direction == UP)
1322 angle = -angle; /* HACK */
1324 poly = transform_poly(from->solid,
1325 from->grid->squares[from->current].flip,
1326 pkey[0], pkey[1], angle);
1327 flip_poly(poly, from->grid->squares[ret->current].flip);
1328 success = align_poly(poly, &from->grid->squares[ret->current], all_pkey);
1333 poly = transform_poly(from->solid,
1334 from->grid->squares[from->current].flip,
1335 pkey[0], pkey[1], angle);
1336 flip_poly(poly, from->grid->squares[ret->current].flip);
1337 success = align_poly(poly, &from->grid->squares[ret->current], all_pkey);
1344 * Now we have our rotated polyhedron, which we expect to be
1345 * exactly congruent to the one we started with - but with the
1346 * faces permuted. So we map that congruence and thereby figure
1347 * out how to permute the faces as a result of the polyhedron
1351 int *newcolours = snewn(from->solid->nfaces, int);
1353 for (i = 0; i < from->solid->nfaces; i++)
1356 for (i = 0; i < from->solid->nfaces; i++) {
1360 * Now go through the transformed polyhedron's faces
1361 * and figure out which one's normal is approximately
1362 * equal to this one.
1364 for (j = 0; j < poly->nfaces; j++) {
1370 for (k = 0; k < 3; k++)
1371 dist += SQ(poly->normals[j*3+k] -
1372 from->solid->normals[i*3+k]);
1374 if (APPROXEQ(dist, 0)) {
1376 newcolours[i] = ret->facecolours[j];
1380 assert(nmatch == 1);
1383 for (i = 0; i < from->solid->nfaces; i++)
1384 assert(newcolours[i] != -1);
1386 sfree(ret->facecolours);
1387 ret->facecolours = newcolours;
1393 * And finally, swap the colour between the bottom face of the
1394 * polyhedron and the face we've just landed on.
1396 * We don't do this if the game is already complete, since we
1397 * allow the user to roll the fully blue polyhedron around the
1398 * grid as a feeble reward.
1400 if (!ret->completed) {
1401 i = lowest_face(from->solid);
1402 j = ret->facecolours[i];
1403 ret->facecolours[i] = GET_SQUARE(ret, ret->current);
1404 SET_SQUARE(ret, ret->current, j);
1407 * Detect game completion.
1410 for (i = 0; i < ret->solid->nfaces; i++)
1411 if (ret->facecolours[i])
1413 if (j == ret->solid->nfaces)
1414 ret->completed = ret->movecount;
1420 * Align the normal polyhedron with its grid square, to get key
1421 * points for non-animated display.
1427 success = align_poly(ret->solid, &ret->grid->squares[ret->current], pkey);
1430 ret->dpkey[0] = pkey[0];
1431 ret->dpkey[1] = pkey[1];
1437 ret->spkey[0] = pkey[0];
1438 ret->spkey[1] = pkey[1];
1439 ret->sgkey[0] = skey[0];
1440 ret->sgkey[1] = skey[1];
1441 ret->previous = from->current;
1447 /* ----------------------------------------------------------------------
1455 static void find_bbox_callback(void *ctx, struct grid_square *sq)
1457 struct bbox *bb = (struct bbox *)ctx;
1460 for (i = 0; i < sq->npoints; i++) {
1461 if (bb->l > sq->points[i*2]) bb->l = sq->points[i*2];
1462 if (bb->r < sq->points[i*2]) bb->r = sq->points[i*2];
1463 if (bb->u > sq->points[i*2+1]) bb->u = sq->points[i*2+1];
1464 if (bb->d < sq->points[i*2+1]) bb->d = sq->points[i*2+1];
1468 static struct bbox find_bbox(const game_params *params)
1473 * These should be hugely more than the real bounding box will
1476 bb.l = 2.0F * (params->d1 + params->d2);
1477 bb.r = -2.0F * (params->d1 + params->d2);
1478 bb.u = 2.0F * (params->d1 + params->d2);
1479 bb.d = -2.0F * (params->d1 + params->d2);
1480 enum_grid_squares(params, find_bbox_callback, &bb);
1485 #define XSIZE(gs, bb, solid) \
1486 ((int)(((bb).r - (bb).l + 2*(solid)->border) * gs))
1487 #define YSIZE(gs, bb, solid) \
1488 ((int)(((bb).d - (bb).u + 2*(solid)->border) * gs))
1490 static void game_compute_size(const game_params *params, int tilesize,
1493 struct bbox bb = find_bbox(params);
1495 *x = XSIZE(tilesize, bb, solids[params->solid]);
1496 *y = YSIZE(tilesize, bb, solids[params->solid]);
1499 static void game_set_size(drawing *dr, game_drawstate *ds,
1500 const game_params *params, int tilesize)
1502 struct bbox bb = find_bbox(params);
1504 ds->gridscale = (float)tilesize;
1505 ds->ox = (int)(-(bb.l - solids[params->solid]->border) * ds->gridscale);
1506 ds->oy = (int)(-(bb.u - solids[params->solid]->border) * ds->gridscale);
1509 static float *game_colours(frontend *fe, int *ncolours)
1511 float *ret = snewn(3 * NCOLOURS, float);
1513 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1515 ret[COL_BORDER * 3 + 0] = 0.0;
1516 ret[COL_BORDER * 3 + 1] = 0.0;
1517 ret[COL_BORDER * 3 + 2] = 0.0;
1519 ret[COL_BLUE * 3 + 0] = 0.0;
1520 ret[COL_BLUE * 3 + 1] = 0.0;
1521 ret[COL_BLUE * 3 + 2] = 1.0;
1523 *ncolours = NCOLOURS;
1527 static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state)
1529 struct game_drawstate *ds = snew(struct game_drawstate);
1531 ds->ox = ds->oy = 0;
1532 ds->gridscale = 0.0F; /* not decided yet */
1537 static void game_free_drawstate(drawing *dr, game_drawstate *ds)
1542 static void game_redraw(drawing *dr, game_drawstate *ds,
1543 const game_state *oldstate, const game_state *state,
1544 int dir, const game_ui *ui,
1545 float animtime, float flashtime)
1548 struct bbox bb = find_bbox(&state->params);
1550 const int *pkey, *gkey;
1555 draw_rect(dr, 0, 0, XSIZE(GRID_SCALE, bb, state->solid),
1556 YSIZE(GRID_SCALE, bb, state->solid), COL_BACKGROUND);
1559 const game_state *t;
1562 * This is an Undo. So reverse the order of the states, and
1563 * run the roll timer backwards.
1571 animtime = ROLLTIME - animtime;
1577 square = state->current;
1578 pkey = state->dpkey;
1579 gkey = state->dgkey;
1581 angle = state->angle * animtime / ROLLTIME;
1582 square = state->previous;
1583 pkey = state->spkey;
1584 gkey = state->sgkey;
1588 for (i = 0; i < state->grid->nsquares; i++) {
1591 for (j = 0; j < state->grid->squares[i].npoints; j++) {
1592 coords[2*j] = ((int)(state->grid->squares[i].points[2*j] * GRID_SCALE)
1594 coords[2*j+1] = ((int)(state->grid->squares[i].points[2*j+1]*GRID_SCALE)
1598 draw_polygon(dr, coords, state->grid->squares[i].npoints,
1599 GET_SQUARE(state, i) ? COL_BLUE : COL_BACKGROUND,
1604 * Now compute and draw the polyhedron.
1606 poly = transform_poly(state->solid, state->grid->squares[square].flip,
1607 pkey[0], pkey[1], angle);
1610 * Compute the translation required to align the two key points
1611 * on the polyhedron with the same key points on the current
1614 for (i = 0; i < 3; i++) {
1617 for (j = 0; j < 2; j++) {
1622 state->grid->squares[square].points[gkey[j]*2+i];
1627 tc += (grid_coord - poly->vertices[pkey[j]*3+i]);
1632 for (i = 0; i < poly->nvertices; i++)
1633 for (j = 0; j < 3; j++)
1634 poly->vertices[i*3+j] += t[j];
1637 * Now actually draw each face.
1639 for (i = 0; i < poly->nfaces; i++) {
1643 for (j = 0; j < poly->order; j++) {
1644 int f = poly->faces[i*poly->order + j];
1645 points[j*2] = (poly->vertices[f*3+0] -
1646 poly->vertices[f*3+2] * poly->shear);
1647 points[j*2+1] = (poly->vertices[f*3+1] -
1648 poly->vertices[f*3+2] * poly->shear);
1651 for (j = 0; j < poly->order; j++) {
1652 coords[j*2] = (int)floor(points[j*2] * GRID_SCALE) + ds->ox;
1653 coords[j*2+1] = (int)floor(points[j*2+1] * GRID_SCALE) + ds->oy;
1657 * Find out whether these points are in a clockwise or
1658 * anticlockwise arrangement. If the latter, discard the
1659 * face because it's facing away from the viewer.
1661 * This would involve fiddly winding-number stuff for a
1662 * general polygon, but for the simple parallelograms we'll
1663 * be seeing here, all we have to do is check whether the
1664 * corners turn right or left. So we'll take the vector
1665 * from point 0 to point 1, turn it right 90 degrees,
1666 * and check the sign of the dot product with that and the
1667 * next vector (point 1 to point 2).
1670 float v1x = points[2]-points[0];
1671 float v1y = points[3]-points[1];
1672 float v2x = points[4]-points[2];
1673 float v2y = points[5]-points[3];
1674 float dp = v1x * v2y - v1y * v2x;
1680 draw_polygon(dr, coords, poly->order,
1681 state->facecolours[i] ? COL_BLUE : COL_BACKGROUND,
1686 draw_update(dr, 0, 0, XSIZE(GRID_SCALE, bb, state->solid),
1687 YSIZE(GRID_SCALE, bb, state->solid));
1690 * Update the status bar.
1693 char statusbuf[256];
1695 sprintf(statusbuf, "%sMoves: %d",
1696 (state->completed ? "COMPLETED! " : ""),
1697 (state->completed ? state->completed : state->movecount));
1699 status_bar(dr, statusbuf);
1703 static float game_anim_length(const game_state *oldstate,
1704 const game_state *newstate, int dir, game_ui *ui)
1709 static float game_flash_length(const game_state *oldstate,
1710 const game_state *newstate, int dir, game_ui *ui)
1715 static int game_status(const game_state *state)
1717 return state->completed ? +1 : 0;
1720 static int game_timing_state(const game_state *state, game_ui *ui)
1725 static void game_print_size(const game_params *params, float *x, float *y)
1729 static void game_print(drawing *dr, const game_state *state, int tilesize)
1734 #define thegame cube
1737 const struct game thegame = {
1738 "Cube", "games.cube", "cube",
1740 game_fetch_preset, NULL,
1745 TRUE, game_configure, custom_params,
1753 FALSE, game_can_format_as_text_now, game_text_format,
1761 PREFERRED_GRID_SCALE, game_compute_size, game_set_size,
1764 game_free_drawstate,
1769 FALSE, FALSE, game_print_size, game_print,
1770 TRUE, /* wants_statusbar */
1771 FALSE, game_timing_state,