14 #define PI 3.14159265358979323846264338327950884197169399
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 s_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 s_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 s_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 s_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 &s_tetrahedron
, &s_cube
, &s_octahedron
, &s_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.13F
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 static game_params
*default_params(void)
218 game_params
*ret
= snew(game_params
);
227 static 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 static void free_params(game_params
*params
)
272 static game_params
*dup_params(game_params
*params
)
274 game_params
*ret
= snew(game_params
);
275 *ret
= *params
; /* structure copy */
279 static void decode_params(game_params
*ret
, char const *string
)
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
);
296 static char *encode_params(game_params
*params
, int full
)
300 assert(params
->solid
>= 0 && params
->solid
< 4);
301 sprintf(data
, "%c%dx%d", "tcoi"[params
->solid
], params
->d1
, params
->d2
);
306 static void enum_grid_squares(game_params
*params
,
307 void (*callback
)(void *, struct grid_square
*),
310 const struct solid
*solid
= solids
[params
->solid
];
312 if (solid
->order
== 4) {
315 for (y
= 0; y
< params
->d2
; y
++)
316 for (x
= 0; x
< params
->d1
; x
++) {
317 struct grid_square sq
;
321 sq
.points
[0] = x
- 0.5F
;
322 sq
.points
[1] = y
- 0.5F
;
323 sq
.points
[2] = x
- 0.5F
;
324 sq
.points
[3] = y
+ 0.5F
;
325 sq
.points
[4] = x
+ 0.5F
;
326 sq
.points
[5] = y
+ 0.5F
;
327 sq
.points
[6] = x
+ 0.5F
;
328 sq
.points
[7] = y
- 0.5F
;
331 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
332 sq
.directions
[RIGHT
] = 0x0C; /* 2,3 */
333 sq
.directions
[UP
] = 0x09; /* 0,3 */
334 sq
.directions
[DOWN
] = 0x06; /* 1,2 */
335 sq
.directions
[UP_LEFT
] = 0; /* no diagonals in a square */
336 sq
.directions
[UP_RIGHT
] = 0; /* no diagonals in a square */
337 sq
.directions
[DOWN_LEFT
] = 0; /* no diagonals in a square */
338 sq
.directions
[DOWN_RIGHT
] = 0; /* no diagonals in a square */
343 * This is supremely irrelevant, but just to avoid
344 * having any uninitialised structure members...
351 int row
, rowlen
, other
, i
, firstix
= -1;
352 float theight
= (float)(sqrt(3) / 2.0);
354 for (row
= 0; row
< params
->d1
+ params
->d2
; row
++) {
355 if (row
< params
->d2
) {
357 rowlen
= row
+ params
->d1
;
360 rowlen
= 2*params
->d2
+ params
->d1
- row
;
364 * There are `rowlen' down-pointing triangles.
366 for (i
= 0; i
< rowlen
; i
++) {
367 struct grid_square sq
;
371 ix
= (2 * i
- (rowlen
-1));
375 sq
.y
= y
+ theight
/ 3;
376 sq
.points
[0] = x
- 0.5F
;
379 sq
.points
[3] = y
+ theight
;
380 sq
.points
[4] = x
+ 0.5F
;
384 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
385 sq
.directions
[RIGHT
] = 0x06; /* 1,2 */
386 sq
.directions
[UP
] = 0x05; /* 0,2 */
387 sq
.directions
[DOWN
] = 0; /* invalid move */
390 * Down-pointing triangle: both the up diagonals go
391 * up, and the down ones go left and right.
393 sq
.directions
[UP_LEFT
] = sq
.directions
[UP_RIGHT
] =
395 sq
.directions
[DOWN_LEFT
] = sq
.directions
[LEFT
];
396 sq
.directions
[DOWN_RIGHT
] = sq
.directions
[RIGHT
];
403 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
409 * There are `rowlen+other' up-pointing triangles.
411 for (i
= 0; i
< rowlen
+other
; i
++) {
412 struct grid_square sq
;
416 ix
= (2 * i
- (rowlen
+other
-1));
420 sq
.y
= y
+ 2*theight
/ 3;
421 sq
.points
[0] = x
+ 0.5F
;
422 sq
.points
[1] = y
+ theight
;
425 sq
.points
[4] = x
- 0.5F
;
426 sq
.points
[5] = y
+ theight
;
429 sq
.directions
[LEFT
] = 0x06; /* 1,2 */
430 sq
.directions
[RIGHT
] = 0x03; /* 0,1 */
431 sq
.directions
[DOWN
] = 0x05; /* 0,2 */
432 sq
.directions
[UP
] = 0; /* invalid move */
435 * Up-pointing triangle: both the down diagonals go
436 * down, and the up ones go left and right.
438 sq
.directions
[DOWN_LEFT
] = sq
.directions
[DOWN_RIGHT
] =
440 sq
.directions
[UP_LEFT
] = sq
.directions
[LEFT
];
441 sq
.directions
[UP_RIGHT
] = sq
.directions
[RIGHT
];
446 firstix
= (ix
- 1) & 3;
448 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
456 static int grid_area(int d1
, int d2
, int order
)
459 * An NxM grid of squares has NM squares in it.
461 * A grid of triangles with dimensions A and B has a total of
462 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
463 * a side-A triangle containing A^2 subtriangles, a side-B
464 * triangle containing B^2, and two congruent parallelograms,
465 * each with side lengths A and B, each therefore containing AB
466 * two-triangle rhombuses.)
471 return d1
*d1
+ d2
*d2
+ 4*d1
*d2
;
474 static config_item
*game_configure(game_params
*params
)
476 config_item
*ret
= snewn(4, config_item
);
479 ret
[0].name
= "Type of solid";
480 ret
[0].type
= C_CHOICES
;
481 ret
[0].sval
= ":Tetrahedron:Cube:Octahedron:Icosahedron";
482 ret
[0].ival
= params
->solid
;
484 ret
[1].name
= "Width / top";
485 ret
[1].type
= C_STRING
;
486 sprintf(buf
, "%d", params
->d1
);
487 ret
[1].sval
= dupstr(buf
);
490 ret
[2].name
= "Height / bottom";
491 ret
[2].type
= C_STRING
;
492 sprintf(buf
, "%d", params
->d2
);
493 ret
[2].sval
= dupstr(buf
);
504 static game_params
*custom_params(config_item
*cfg
)
506 game_params
*ret
= snew(game_params
);
508 ret
->solid
= cfg
[0].ival
;
509 ret
->d1
= atoi(cfg
[1].sval
);
510 ret
->d2
= atoi(cfg
[2].sval
);
515 static void count_grid_square_callback(void *ctx
, struct grid_square
*sq
)
517 int *classes
= (int *)ctx
;
521 thisclass
= sq
->tetra_class
;
522 else if (classes
[4] == 2)
523 thisclass
= sq
->flip
;
527 classes
[thisclass
]++;
530 static char *validate_params(game_params
*params
)
535 if (params
->solid
< 0 || params
->solid
>= lenof(solids
))
536 return "Unrecognised solid type";
538 if (solids
[params
->solid
]->order
== 4) {
539 if (params
->d1
<= 0 || params
->d2
<= 0)
540 return "Both grid dimensions must be greater than zero";
542 if (params
->d1
<= 0 && params
->d2
<= 0)
543 return "At least one grid dimension must be greater than zero";
546 for (i
= 0; i
< 4; i
++)
548 if (params
->solid
== TETRAHEDRON
)
550 else if (params
->solid
== OCTAHEDRON
)
554 enum_grid_squares(params
, count_grid_square_callback
, classes
);
556 for (i
= 0; i
< classes
[4]; i
++)
557 if (classes
[i
] < solids
[params
->solid
]->nfaces
/ classes
[4])
558 return "Not enough grid space to place all blue faces";
560 if (grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
) <
561 solids
[params
->solid
]->nfaces
+ 1)
562 return "Not enough space to place the solid on an empty square";
574 static void classify_grid_square_callback(void *ctx
, struct grid_square
*sq
)
576 struct grid_data
*data
= (struct grid_data
*)ctx
;
579 if (data
->nclasses
== 4)
580 thisclass
= sq
->tetra_class
;
581 else if (data
->nclasses
== 2)
582 thisclass
= sq
->flip
;
586 data
->gridptrs
[thisclass
][data
->nsquares
[thisclass
]++] =
590 static char *new_game_desc(game_params
*params
, random_state
*rs
,
591 game_aux_info
**aux
, int interactive
)
593 struct grid_data data
;
594 int i
, j
, k
, m
, area
, facesperclass
;
599 * Enumerate the grid squares, dividing them into equivalence
600 * classes as appropriate. (For the tetrahedron, there is one
601 * equivalence class for each face; for the octahedron there
602 * are two classes; for the other two solids there's only one.)
605 area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
606 if (params
->solid
== TETRAHEDRON
)
608 else if (params
->solid
== OCTAHEDRON
)
612 data
.gridptrs
[0] = snewn(data
.nclasses
* area
, int);
613 for (i
= 0; i
< data
.nclasses
; i
++) {
614 data
.gridptrs
[i
] = data
.gridptrs
[0] + i
* area
;
615 data
.nsquares
[i
] = 0;
617 data
.squareindex
= 0;
618 enum_grid_squares(params
, classify_grid_square_callback
, &data
);
620 facesperclass
= solids
[params
->solid
]->nfaces
/ data
.nclasses
;
622 for (i
= 0; i
< data
.nclasses
; i
++)
623 assert(data
.nsquares
[i
] >= facesperclass
);
624 assert(data
.squareindex
== area
);
627 * So now we know how many faces to allocate in each class. Get
630 flags
= snewn(area
, int);
631 for (i
= 0; i
< area
; i
++)
634 for (i
= 0; i
< data
.nclasses
; i
++) {
635 for (j
= 0; j
< facesperclass
; j
++) {
636 int n
= random_upto(rs
, data
.nsquares
[i
]);
638 assert(!flags
[data
.gridptrs
[i
][n
]]);
639 flags
[data
.gridptrs
[i
][n
]] = TRUE
;
642 * Move everything else up the array. I ought to use a
643 * better data structure for this, but for such small
644 * numbers it hardly seems worth the effort.
646 while (n
< data
.nsquares
[i
]-1) {
647 data
.gridptrs
[i
][n
] = data
.gridptrs
[i
][n
+1];
655 * Now we know precisely which squares are blue. Encode this
656 * information in hex. While we're looping over this, collect
657 * the non-blue squares into a list in the now-unused gridptrs
660 desc
= snewn(area
/ 4 + 40, char);
665 for (i
= 0; i
< area
; i
++) {
669 data
.gridptrs
[0][m
++] = i
;
673 *p
++ = "0123456789ABCDEF"[j
];
679 *p
++ = "0123456789ABCDEF"[j
];
682 * Choose a non-blue square for the polyhedron.
684 sprintf(p
, ",%d", data
.gridptrs
[0][random_upto(rs
, m
)]);
686 sfree(data
.gridptrs
[0]);
692 static void game_free_aux_info(game_aux_info
*aux
)
694 assert(!"Shouldn't happen");
697 static void add_grid_square_callback(void *ctx
, struct grid_square
*sq
)
699 game_state
*state
= (game_state
*)ctx
;
701 state
->squares
[state
->nsquares
] = *sq
; /* structure copy */
702 state
->squares
[state
->nsquares
].blue
= FALSE
;
706 static int lowest_face(const struct solid
*solid
)
713 for (i
= 0; i
< solid
->nfaces
; i
++) {
716 for (j
= 0; j
< solid
->order
; j
++) {
717 int f
= solid
->faces
[i
*solid
->order
+ j
];
718 z
+= solid
->vertices
[f
*3+2];
721 if (i
== 0 || zmin
> z
) {
730 static int align_poly(const struct solid
*solid
, struct grid_square
*sq
,
735 int flip
= (sq
->flip ?
-1 : +1);
738 * First, find the lowest z-coordinate present in the solid.
741 for (i
= 0; i
< solid
->nvertices
; i
++)
742 if (zmin
> solid
->vertices
[i
*3+2])
743 zmin
= solid
->vertices
[i
*3+2];
746 * Now go round the grid square. For each point in the grid
747 * square, we're looking for a point of the polyhedron with the
748 * same x- and y-coordinates (relative to the square's centre),
749 * and z-coordinate equal to zmin (near enough).
751 for (j
= 0; j
< sq
->npoints
; j
++) {
757 for (i
= 0; i
< solid
->nvertices
; i
++) {
760 dist
+= SQ(solid
->vertices
[i
*3+0] * flip
- sq
->points
[j
*2+0] + sq
->x
);
761 dist
+= SQ(solid
->vertices
[i
*3+1] * flip
- sq
->points
[j
*2+1] + sq
->y
);
762 dist
+= SQ(solid
->vertices
[i
*3+2] - zmin
);
770 if (matches
!= 1 || index
< 0)
778 static void flip_poly(struct solid
*solid
, int flip
)
783 for (i
= 0; i
< solid
->nvertices
; i
++) {
784 solid
->vertices
[i
*3+0] *= -1;
785 solid
->vertices
[i
*3+1] *= -1;
787 for (i
= 0; i
< solid
->nfaces
; i
++) {
788 solid
->normals
[i
*3+0] *= -1;
789 solid
->normals
[i
*3+1] *= -1;
794 static struct solid
*transform_poly(const struct solid
*solid
, int flip
,
795 int key0
, int key1
, float angle
)
797 struct solid
*ret
= snew(struct solid
);
798 float vx
, vy
, ax
, ay
;
799 float vmatrix
[9], amatrix
[9], vmatrix2
[9];
802 *ret
= *solid
; /* structure copy */
804 flip_poly(ret
, flip
);
807 * Now rotate the polyhedron through the given angle. We must
808 * rotate about the Z-axis to bring the two vertices key0 and
809 * key1 into horizontal alignment, then rotate about the
810 * X-axis, then rotate back again.
812 vx
= ret
->vertices
[key1
*3+0] - ret
->vertices
[key0
*3+0];
813 vy
= ret
->vertices
[key1
*3+1] - ret
->vertices
[key0
*3+1];
814 assert(APPROXEQ(vx
*vx
+ vy
*vy
, 1.0));
816 vmatrix
[0] = vx
; vmatrix
[3] = vy
; vmatrix
[6] = 0;
817 vmatrix
[1] = -vy
; vmatrix
[4] = vx
; vmatrix
[7] = 0;
818 vmatrix
[2] = 0; vmatrix
[5] = 0; vmatrix
[8] = 1;
820 ax
= (float)cos(angle
);
821 ay
= (float)sin(angle
);
823 amatrix
[0] = 1; amatrix
[3] = 0; amatrix
[6] = 0;
824 amatrix
[1] = 0; amatrix
[4] = ax
; amatrix
[7] = ay
;
825 amatrix
[2] = 0; amatrix
[5] = -ay
; amatrix
[8] = ax
;
827 memcpy(vmatrix2
, vmatrix
, sizeof(vmatrix
));
831 for (i
= 0; i
< ret
->nvertices
; i
++) {
832 MATMUL(ret
->vertices
+ 3*i
, vmatrix
, ret
->vertices
+ 3*i
);
833 MATMUL(ret
->vertices
+ 3*i
, amatrix
, ret
->vertices
+ 3*i
);
834 MATMUL(ret
->vertices
+ 3*i
, vmatrix2
, ret
->vertices
+ 3*i
);
836 for (i
= 0; i
< ret
->nfaces
; i
++) {
837 MATMUL(ret
->normals
+ 3*i
, vmatrix
, ret
->normals
+ 3*i
);
838 MATMUL(ret
->normals
+ 3*i
, amatrix
, ret
->normals
+ 3*i
);
839 MATMUL(ret
->normals
+ 3*i
, vmatrix2
, ret
->normals
+ 3*i
);
845 static char *validate_desc(game_params
*params
, char *desc
)
847 int area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
851 for (j
= 0; j
< i
; j
++) {
853 if (c
>= '0' && c
<= '9') continue;
854 if (c
>= 'A' && c
<= 'F') continue;
855 if (c
>= 'a' && c
<= 'f') continue;
856 return "Not enough hex digits at start of string";
857 /* NB if desc[j]=='\0' that will also be caught here, so we're safe */
861 return "Expected ',' after hex digits";
865 if (desc
[i
] < '0' || desc
[i
] > '9')
866 return "Expected decimal integer after ','";
873 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
875 game_state
*state
= snew(game_state
);
878 state
->params
= *params
; /* structure copy */
879 state
->solid
= solids
[params
->solid
];
881 area
= grid_area(params
->d1
, params
->d2
, state
->solid
->order
);
882 state
->squares
= snewn(area
, struct grid_square
);
884 enum_grid_squares(params
, add_grid_square_callback
, state
);
885 assert(state
->nsquares
== area
);
887 state
->facecolours
= snewn(state
->solid
->nfaces
, int);
888 memset(state
->facecolours
, 0, state
->solid
->nfaces
* sizeof(int));
891 * Set up the blue squares and polyhedron position according to
892 * the game description.
900 for (i
= 0; i
< state
->nsquares
; i
++) {
903 if (v
>= '0' && v
<= '9')
905 else if (v
>= 'A' && v
<= 'F')
907 else if (v
>= 'a' && v
<= 'f')
913 state
->squares
[i
].blue
= TRUE
;
922 state
->current
= atoi(p
);
923 if (state
->current
< 0 || state
->current
>= state
->nsquares
)
924 state
->current
= 0; /* got to do _something_ */
928 * Align the polyhedron with its grid square and determine
929 * initial key points.
935 ret
= align_poly(state
->solid
, &state
->squares
[state
->current
], pkey
);
938 state
->dpkey
[0] = state
->spkey
[0] = pkey
[0];
939 state
->dpkey
[1] = state
->spkey
[0] = pkey
[1];
940 state
->dgkey
[0] = state
->sgkey
[0] = 0;
941 state
->dgkey
[1] = state
->sgkey
[0] = 1;
944 state
->previous
= state
->current
;
946 state
->completed
= 0;
947 state
->movecount
= 0;
952 static game_state
*dup_game(game_state
*state
)
954 game_state
*ret
= snew(game_state
);
956 ret
->params
= state
->params
; /* structure copy */
957 ret
->solid
= state
->solid
;
958 ret
->facecolours
= snewn(ret
->solid
->nfaces
, int);
959 memcpy(ret
->facecolours
, state
->facecolours
,
960 ret
->solid
->nfaces
* sizeof(int));
961 ret
->nsquares
= state
->nsquares
;
962 ret
->current
= state
->current
;
963 ret
->squares
= snewn(ret
->nsquares
, struct grid_square
);
964 memcpy(ret
->squares
, state
->squares
,
965 ret
->nsquares
* sizeof(struct grid_square
));
966 ret
->dpkey
[0] = state
->dpkey
[0];
967 ret
->dpkey
[1] = state
->dpkey
[1];
968 ret
->dgkey
[0] = state
->dgkey
[0];
969 ret
->dgkey
[1] = state
->dgkey
[1];
970 ret
->spkey
[0] = state
->spkey
[0];
971 ret
->spkey
[1] = state
->spkey
[1];
972 ret
->sgkey
[0] = state
->sgkey
[0];
973 ret
->sgkey
[1] = state
->sgkey
[1];
974 ret
->previous
= state
->previous
;
975 ret
->angle
= state
->angle
;
976 ret
->completed
= state
->completed
;
977 ret
->movecount
= state
->movecount
;
982 static void free_game(game_state
*state
)
987 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
993 static char *game_text_format(game_state
*state
)
998 static game_ui
*new_ui(game_state
*state
)
1003 static void free_ui(game_ui
*ui
)
1007 struct game_drawstate
{
1008 int ox
, oy
; /* pixel position of float origin */
1011 static game_state
*make_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
1012 int x
, int y
, int button
)
1015 int pkey
[2], skey
[2], dkey
[2];
1019 int i
, j
, dest
, mask
;
1022 button
= button
& (~MOD_MASK
| MOD_NUM_KEYPAD
);
1025 * Moves can be made with the cursor keys or numeric keypad, or
1026 * alternatively you can left-click and the polyhedron will
1027 * move in the general direction of the mouse pointer.
1029 if (button
== CURSOR_UP
|| button
== (MOD_NUM_KEYPAD
| '8'))
1031 else if (button
== CURSOR_DOWN
|| button
== (MOD_NUM_KEYPAD
| '2'))
1033 else if (button
== CURSOR_LEFT
|| button
== (MOD_NUM_KEYPAD
| '4'))
1035 else if (button
== CURSOR_RIGHT
|| button
== (MOD_NUM_KEYPAD
| '6'))
1037 else if (button
== (MOD_NUM_KEYPAD
| '7'))
1038 direction
= UP_LEFT
;
1039 else if (button
== (MOD_NUM_KEYPAD
| '1'))
1040 direction
= DOWN_LEFT
;
1041 else if (button
== (MOD_NUM_KEYPAD
| '9'))
1042 direction
= UP_RIGHT
;
1043 else if (button
== (MOD_NUM_KEYPAD
| '3'))
1044 direction
= DOWN_RIGHT
;
1045 else if (button
== LEFT_BUTTON
) {
1047 * Find the bearing of the click point from the current
1053 cx
= from
->squares
[from
->current
].x
* GRID_SCALE
+ ds
->ox
;
1054 cy
= from
->squares
[from
->current
].y
* GRID_SCALE
+ ds
->oy
;
1056 if (x
== cx
&& y
== cy
)
1057 return NULL
; /* clicked in exact centre! */
1058 angle
= atan2(y
- cy
, x
- cx
);
1061 * There are three possibilities.
1063 * - This square is a square, so we choose between UP,
1064 * DOWN, LEFT and RIGHT by dividing the available angle
1065 * at the 45-degree points.
1067 * - This square is an up-pointing triangle, so we choose
1068 * between DOWN, LEFT and RIGHT by dividing into
1071 * - This square is a down-pointing triangle, so we choose
1072 * between UP, LEFT and RIGHT in the inverse manner.
1074 * Don't forget that since our y-coordinates increase
1075 * downwards, `angle' is measured _clockwise_ from the
1076 * x-axis, not anticlockwise as most mathematicians would
1077 * instinctively assume.
1079 if (from
->squares
[from
->current
].npoints
== 4) {
1081 if (fabs(angle
) > 3*PI
/4)
1083 else if (fabs(angle
) < PI
/4)
1089 } else if (from
->squares
[from
->current
].directions
[UP
] == 0) {
1090 /* Up-pointing triangle. */
1091 if (angle
< -PI
/2 || angle
> 5*PI
/6)
1093 else if (angle
> PI
/6)
1098 /* Down-pointing triangle. */
1099 assert(from
->squares
[from
->current
].directions
[DOWN
] == 0);
1100 if (angle
> PI
/2 || angle
< -5*PI
/6)
1102 else if (angle
< -PI
/6)
1111 * Find the two points in the current grid square which
1112 * correspond to this move.
1114 mask
= from
->squares
[from
->current
].directions
[direction
];
1117 for (i
= j
= 0; i
< from
->squares
[from
->current
].npoints
; i
++)
1118 if (mask
& (1 << i
)) {
1119 points
[j
*2] = from
->squares
[from
->current
].points
[i
*2];
1120 points
[j
*2+1] = from
->squares
[from
->current
].points
[i
*2+1];
1127 * Now find the other grid square which shares those points.
1128 * This is our move destination.
1131 for (i
= 0; i
< from
->nsquares
; i
++)
1132 if (i
!= from
->current
) {
1136 for (j
= 0; j
< from
->squares
[i
].npoints
; j
++) {
1137 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[0]) +
1138 SQ(from
->squares
[i
].points
[j
*2+1] - points
[1]));
1141 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[2]) +
1142 SQ(from
->squares
[i
].points
[j
*2+1] - points
[3]));
1156 ret
= dup_game(from
);
1160 * So we know what grid square we're aiming for, and we also
1161 * know the two key points (as indices in both the source and
1162 * destination grid squares) which are invariant between source
1165 * Next we must roll the polyhedron on to that square. So we
1166 * find the indices of the key points within the polyhedron's
1167 * vertex array, then use those in a call to transform_poly,
1168 * and align the result on the new grid square.
1172 align_poly(from
->solid
, &from
->squares
[from
->current
], all_pkey
);
1173 pkey
[0] = all_pkey
[skey
[0]];
1174 pkey
[1] = all_pkey
[skey
[1]];
1176 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1182 * Now find the angle through which to rotate the polyhedron.
1183 * Do this by finding the two faces that share the two vertices
1184 * we've found, and taking the dot product of their normals.
1190 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1192 for (j
= 0; j
< from
->solid
->order
; j
++)
1193 if (from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[0] ||
1194 from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[1])
1205 for (i
= 0; i
< 3; i
++)
1206 dp
+= (from
->solid
->normals
[f
[0]*3+i
] *
1207 from
->solid
->normals
[f
[1]*3+i
]);
1208 angle
= (float)acos(dp
);
1212 * Now transform the polyhedron. We aren't entirely sure
1213 * whether we need to rotate through angle or -angle, and the
1214 * simplest way round this is to try both and see which one
1215 * aligns successfully!
1217 * Unfortunately, _both_ will align successfully if this is a
1218 * cube, which won't tell us anything much. So for that
1219 * particular case, I resort to gross hackery: I simply negate
1220 * the angle before trying the alignment, depending on the
1221 * direction. Which directions work which way is determined by
1222 * pure trial and error. I said it was gross :-/
1228 if (from
->solid
->order
== 4 && direction
== UP
)
1229 angle
= -angle
; /* HACK */
1231 poly
= transform_poly(from
->solid
,
1232 from
->squares
[from
->current
].flip
,
1233 pkey
[0], pkey
[1], angle
);
1234 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1235 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1239 poly
= transform_poly(from
->solid
,
1240 from
->squares
[from
->current
].flip
,
1241 pkey
[0], pkey
[1], angle
);
1242 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1243 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1250 * Now we have our rotated polyhedron, which we expect to be
1251 * exactly congruent to the one we started with - but with the
1252 * faces permuted. So we map that congruence and thereby figure
1253 * out how to permute the faces as a result of the polyhedron
1257 int *newcolours
= snewn(from
->solid
->nfaces
, int);
1259 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1262 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1266 * Now go through the transformed polyhedron's faces
1267 * and figure out which one's normal is approximately
1268 * equal to this one.
1270 for (j
= 0; j
< poly
->nfaces
; j
++) {
1276 for (k
= 0; k
< 3; k
++)
1277 dist
+= SQ(poly
->normals
[j
*3+k
] -
1278 from
->solid
->normals
[i
*3+k
]);
1280 if (APPROXEQ(dist
, 0)) {
1282 newcolours
[i
] = ret
->facecolours
[j
];
1286 assert(nmatch
== 1);
1289 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1290 assert(newcolours
[i
] != -1);
1292 sfree(ret
->facecolours
);
1293 ret
->facecolours
= newcolours
;
1299 * And finally, swap the colour between the bottom face of the
1300 * polyhedron and the face we've just landed on.
1302 * We don't do this if the game is already complete, since we
1303 * allow the user to roll the fully blue polyhedron around the
1304 * grid as a feeble reward.
1306 if (!ret
->completed
) {
1307 i
= lowest_face(from
->solid
);
1308 j
= ret
->facecolours
[i
];
1309 ret
->facecolours
[i
] = ret
->squares
[ret
->current
].blue
;
1310 ret
->squares
[ret
->current
].blue
= j
;
1313 * Detect game completion.
1316 for (i
= 0; i
< ret
->solid
->nfaces
; i
++)
1317 if (ret
->facecolours
[i
])
1319 if (j
== ret
->solid
->nfaces
)
1320 ret
->completed
= ret
->movecount
;
1326 * Align the normal polyhedron with its grid square, to get key
1327 * points for non-animated display.
1333 success
= align_poly(ret
->solid
, &ret
->squares
[ret
->current
], pkey
);
1336 ret
->dpkey
[0] = pkey
[0];
1337 ret
->dpkey
[1] = pkey
[1];
1343 ret
->spkey
[0] = pkey
[0];
1344 ret
->spkey
[1] = pkey
[1];
1345 ret
->sgkey
[0] = skey
[0];
1346 ret
->sgkey
[1] = skey
[1];
1347 ret
->previous
= from
->current
;
1353 /* ----------------------------------------------------------------------
1361 static void find_bbox_callback(void *ctx
, struct grid_square
*sq
)
1363 struct bbox
*bb
= (struct bbox
*)ctx
;
1366 for (i
= 0; i
< sq
->npoints
; i
++) {
1367 if (bb
->l
> sq
->points
[i
*2]) bb
->l
= sq
->points
[i
*2];
1368 if (bb
->r
< sq
->points
[i
*2]) bb
->r
= sq
->points
[i
*2];
1369 if (bb
->u
> sq
->points
[i
*2+1]) bb
->u
= sq
->points
[i
*2+1];
1370 if (bb
->d
< sq
->points
[i
*2+1]) bb
->d
= sq
->points
[i
*2+1];
1374 static struct bbox
find_bbox(game_params
*params
)
1379 * These should be hugely more than the real bounding box will
1382 bb
.l
= 2.0F
* (params
->d1
+ params
->d2
);
1383 bb
.r
= -2.0F
* (params
->d1
+ params
->d2
);
1384 bb
.u
= 2.0F
* (params
->d1
+ params
->d2
);
1385 bb
.d
= -2.0F
* (params
->d1
+ params
->d2
);
1386 enum_grid_squares(params
, find_bbox_callback
, &bb
);
1391 static void game_size(game_params
*params
, int *x
, int *y
)
1393 struct bbox bb
= find_bbox(params
);
1394 *x
= (int)((bb
.r
- bb
.l
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
);
1395 *y
= (int)((bb
.d
- bb
.u
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
);
1398 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1400 float *ret
= snewn(3 * NCOLOURS
, float);
1402 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1404 ret
[COL_BORDER
* 3 + 0] = 0.0;
1405 ret
[COL_BORDER
* 3 + 1] = 0.0;
1406 ret
[COL_BORDER
* 3 + 2] = 0.0;
1408 ret
[COL_BLUE
* 3 + 0] = 0.0;
1409 ret
[COL_BLUE
* 3 + 1] = 0.0;
1410 ret
[COL_BLUE
* 3 + 2] = 1.0;
1412 *ncolours
= NCOLOURS
;
1416 static game_drawstate
*game_new_drawstate(game_state
*state
)
1418 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1419 struct bbox bb
= find_bbox(&state
->params
);
1421 ds
->ox
= (int)(-(bb
.l
- state
->solid
->border
) * GRID_SCALE
);
1422 ds
->oy
= (int)(-(bb
.u
- state
->solid
->border
) * GRID_SCALE
);
1427 static void game_free_drawstate(game_drawstate
*ds
)
1432 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1433 game_state
*state
, int dir
, game_ui
*ui
,
1434 float animtime
, float flashtime
)
1437 struct bbox bb
= find_bbox(&state
->params
);
1442 game_state
*newstate
;
1445 draw_rect(fe
, 0, 0, (int)((bb
.r
-bb
.l
+2.0F
) * GRID_SCALE
),
1446 (int)((bb
.d
-bb
.u
+2.0F
) * GRID_SCALE
), COL_BACKGROUND
);
1452 * This is an Undo. So reverse the order of the states, and
1453 * run the roll timer backwards.
1461 animtime
= ROLLTIME
- animtime
;
1467 square
= state
->current
;
1468 pkey
= state
->dpkey
;
1469 gkey
= state
->dgkey
;
1471 angle
= state
->angle
* animtime
/ ROLLTIME
;
1472 square
= state
->previous
;
1473 pkey
= state
->spkey
;
1474 gkey
= state
->sgkey
;
1479 for (i
= 0; i
< state
->nsquares
; i
++) {
1482 for (j
= 0; j
< state
->squares
[i
].npoints
; j
++) {
1483 coords
[2*j
] = ((int)(state
->squares
[i
].points
[2*j
] * GRID_SCALE
)
1485 coords
[2*j
+1] = ((int)(state
->squares
[i
].points
[2*j
+1]*GRID_SCALE
)
1489 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, TRUE
,
1490 state
->squares
[i
].blue ? COL_BLUE
: COL_BACKGROUND
);
1491 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, FALSE
, COL_BORDER
);
1495 * Now compute and draw the polyhedron.
1497 poly
= transform_poly(state
->solid
, state
->squares
[square
].flip
,
1498 pkey
[0], pkey
[1], angle
);
1501 * Compute the translation required to align the two key points
1502 * on the polyhedron with the same key points on the current
1505 for (i
= 0; i
< 3; i
++) {
1508 for (j
= 0; j
< 2; j
++) {
1513 state
->squares
[square
].points
[gkey
[j
]*2+i
];
1518 tc
+= (grid_coord
- poly
->vertices
[pkey
[j
]*3+i
]);
1523 for (i
= 0; i
< poly
->nvertices
; i
++)
1524 for (j
= 0; j
< 3; j
++)
1525 poly
->vertices
[i
*3+j
] += t
[j
];
1528 * Now actually draw each face.
1530 for (i
= 0; i
< poly
->nfaces
; i
++) {
1534 for (j
= 0; j
< poly
->order
; j
++) {
1535 int f
= poly
->faces
[i
*poly
->order
+ j
];
1536 points
[j
*2] = (poly
->vertices
[f
*3+0] -
1537 poly
->vertices
[f
*3+2] * poly
->shear
);
1538 points
[j
*2+1] = (poly
->vertices
[f
*3+1] -
1539 poly
->vertices
[f
*3+2] * poly
->shear
);
1542 for (j
= 0; j
< poly
->order
; j
++) {
1543 coords
[j
*2] = (int)floor(points
[j
*2] * GRID_SCALE
) + ds
->ox
;
1544 coords
[j
*2+1] = (int)floor(points
[j
*2+1] * GRID_SCALE
) + ds
->oy
;
1548 * Find out whether these points are in a clockwise or
1549 * anticlockwise arrangement. If the latter, discard the
1550 * face because it's facing away from the viewer.
1552 * This would involve fiddly winding-number stuff for a
1553 * general polygon, but for the simple parallelograms we'll
1554 * be seeing here, all we have to do is check whether the
1555 * corners turn right or left. So we'll take the vector
1556 * from point 0 to point 1, turn it right 90 degrees,
1557 * and check the sign of the dot product with that and the
1558 * next vector (point 1 to point 2).
1561 float v1x
= points
[2]-points
[0];
1562 float v1y
= points
[3]-points
[1];
1563 float v2x
= points
[4]-points
[2];
1564 float v2y
= points
[5]-points
[3];
1565 float dp
= v1x
* v2y
- v1y
* v2x
;
1571 draw_polygon(fe
, coords
, poly
->order
, TRUE
,
1572 state
->facecolours
[i
] ? COL_BLUE
: COL_BACKGROUND
);
1573 draw_polygon(fe
, coords
, poly
->order
, FALSE
, COL_BORDER
);
1577 draw_update(fe
, 0, 0, (int)((bb
.r
-bb
.l
+2.0F
) * GRID_SCALE
),
1578 (int)((bb
.d
-bb
.u
+2.0F
) * GRID_SCALE
));
1581 * Update the status bar.
1584 char statusbuf
[256];
1586 sprintf(statusbuf
, "%sMoves: %d",
1587 (state
->completed ?
"COMPLETED! " : ""),
1588 (state
->completed ? state
->completed
: state
->movecount
));
1590 status_bar(fe
, statusbuf
);
1594 static float game_anim_length(game_state
*oldstate
,
1595 game_state
*newstate
, int dir
, game_ui
*ui
)
1600 static float game_flash_length(game_state
*oldstate
,
1601 game_state
*newstate
, int dir
, game_ui
*ui
)
1606 static int game_wants_statusbar(void)
1611 static int game_timing_state(game_state
*state
)
1617 #define thegame cube
1620 const struct game thegame
= {
1621 "Cube", "games.cube",
1628 TRUE
, game_configure
, custom_params
,
1637 FALSE
, game_text_format
,
1644 game_free_drawstate
,
1648 game_wants_statusbar
,
1649 FALSE
, game_timing_state
,