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.0F
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 */
189 * Grid dimensions. For a square grid these are width and
190 * height respectively; otherwise the grid is a hexagon, with
191 * the top side and the two lower diagonals having length d1
192 * and the remaining three sides having length d2 (so that
193 * d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
199 struct game_params params
;
200 const struct solid
*solid
;
202 struct grid_square
*squares
;
204 int current
; /* index of current grid square */
205 int sgkey
[2]; /* key-point indices into grid sq */
206 int dgkey
[2]; /* key-point indices into grid sq */
207 int spkey
[2]; /* key-point indices into polyhedron */
208 int dpkey
[2]; /* key-point indices into polyhedron */
215 static game_params
*default_params(void)
217 game_params
*ret
= snew(game_params
);
226 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
228 game_params
*ret
= snew(game_params
);
240 ret
->solid
= TETRAHEDRON
;
246 ret
->solid
= OCTAHEDRON
;
252 ret
->solid
= ICOSAHEDRON
;
266 static void free_params(game_params
*params
)
271 static game_params
*dup_params(game_params
*params
)
273 game_params
*ret
= snew(game_params
);
274 *ret
= *params
; /* structure copy */
278 static void decode_params(game_params
*ret
, char const *string
)
281 case 't': ret
->solid
= TETRAHEDRON
; string
++; break;
282 case 'c': ret
->solid
= CUBE
; string
++; break;
283 case 'o': ret
->solid
= OCTAHEDRON
; string
++; break;
284 case 'i': ret
->solid
= ICOSAHEDRON
; string
++; break;
287 ret
->d1
= ret
->d2
= atoi(string
);
288 while (*string
&& isdigit(*string
)) string
++;
289 if (*string
== 'x') {
291 ret
->d2
= atoi(string
);
295 static char *encode_params(game_params
*params
, int full
)
299 assert(params
->solid
>= 0 && params
->solid
< 4);
300 sprintf(data
, "%c%dx%d", "tcoi"[params
->solid
], params
->d1
, params
->d2
);
304 typedef void (*egc_callback
)(void *, struct grid_square
*);
306 static void enum_grid_squares(game_params
*params
, egc_callback callback
, void *ctx
)
308 const struct solid
*solid
= solids
[params
->solid
];
310 if (solid
->order
== 4) {
313 for (y
= 0; y
< params
->d2
; y
++)
314 for (x
= 0; x
< params
->d1
; x
++) {
315 struct grid_square sq
;
319 sq
.points
[0] = x
- 0.5F
;
320 sq
.points
[1] = y
- 0.5F
;
321 sq
.points
[2] = x
- 0.5F
;
322 sq
.points
[3] = y
+ 0.5F
;
323 sq
.points
[4] = x
+ 0.5F
;
324 sq
.points
[5] = y
+ 0.5F
;
325 sq
.points
[6] = x
+ 0.5F
;
326 sq
.points
[7] = y
- 0.5F
;
329 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
330 sq
.directions
[RIGHT
] = 0x0C; /* 2,3 */
331 sq
.directions
[UP
] = 0x09; /* 0,3 */
332 sq
.directions
[DOWN
] = 0x06; /* 1,2 */
333 sq
.directions
[UP_LEFT
] = 0; /* no diagonals in a square */
334 sq
.directions
[UP_RIGHT
] = 0; /* no diagonals in a square */
335 sq
.directions
[DOWN_LEFT
] = 0; /* no diagonals in a square */
336 sq
.directions
[DOWN_RIGHT
] = 0; /* no diagonals in a square */
341 * This is supremely irrelevant, but just to avoid
342 * having any uninitialised structure members...
349 int row
, rowlen
, other
, i
, firstix
= -1;
350 float theight
= (float)(sqrt(3) / 2.0);
352 for (row
= 0; row
< params
->d1
+ params
->d2
; row
++) {
353 if (row
< params
->d2
) {
355 rowlen
= row
+ params
->d1
;
358 rowlen
= 2*params
->d2
+ params
->d1
- row
;
362 * There are `rowlen' down-pointing triangles.
364 for (i
= 0; i
< rowlen
; i
++) {
365 struct grid_square sq
;
369 ix
= (2 * i
- (rowlen
-1));
373 sq
.y
= y
+ theight
/ 3;
374 sq
.points
[0] = x
- 0.5F
;
377 sq
.points
[3] = y
+ theight
;
378 sq
.points
[4] = x
+ 0.5F
;
382 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
383 sq
.directions
[RIGHT
] = 0x06; /* 1,2 */
384 sq
.directions
[UP
] = 0x05; /* 0,2 */
385 sq
.directions
[DOWN
] = 0; /* invalid move */
388 * Down-pointing triangle: both the up diagonals go
389 * up, and the down ones go left and right.
391 sq
.directions
[UP_LEFT
] = sq
.directions
[UP_RIGHT
] =
393 sq
.directions
[DOWN_LEFT
] = sq
.directions
[LEFT
];
394 sq
.directions
[DOWN_RIGHT
] = sq
.directions
[RIGHT
];
401 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
407 * There are `rowlen+other' up-pointing triangles.
409 for (i
= 0; i
< rowlen
+other
; i
++) {
410 struct grid_square sq
;
414 ix
= (2 * i
- (rowlen
+other
-1));
418 sq
.y
= y
+ 2*theight
/ 3;
419 sq
.points
[0] = x
+ 0.5F
;
420 sq
.points
[1] = y
+ theight
;
423 sq
.points
[4] = x
- 0.5F
;
424 sq
.points
[5] = y
+ theight
;
427 sq
.directions
[LEFT
] = 0x06; /* 1,2 */
428 sq
.directions
[RIGHT
] = 0x03; /* 0,1 */
429 sq
.directions
[DOWN
] = 0x05; /* 0,2 */
430 sq
.directions
[UP
] = 0; /* invalid move */
433 * Up-pointing triangle: both the down diagonals go
434 * down, and the up ones go left and right.
436 sq
.directions
[DOWN_LEFT
] = sq
.directions
[DOWN_RIGHT
] =
438 sq
.directions
[UP_LEFT
] = sq
.directions
[LEFT
];
439 sq
.directions
[UP_RIGHT
] = sq
.directions
[RIGHT
];
444 firstix
= (ix
- 1) & 3;
446 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
454 static int grid_area(int d1
, int d2
, int order
)
457 * An NxM grid of squares has NM squares in it.
459 * A grid of triangles with dimensions A and B has a total of
460 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
461 * a side-A triangle containing A^2 subtriangles, a side-B
462 * triangle containing B^2, and two congruent parallelograms,
463 * each with side lengths A and B, each therefore containing AB
464 * two-triangle rhombuses.)
469 return d1
*d1
+ d2
*d2
+ 4*d1
*d2
;
472 static config_item
*game_configure(game_params
*params
)
474 config_item
*ret
= snewn(4, config_item
);
477 ret
[0].name
= "Type of solid";
478 ret
[0].type
= C_CHOICES
;
479 ret
[0].sval
= ":Tetrahedron:Cube:Octahedron:Icosahedron";
480 ret
[0].ival
= params
->solid
;
482 ret
[1].name
= "Width / top";
483 ret
[1].type
= C_STRING
;
484 sprintf(buf
, "%d", params
->d1
);
485 ret
[1].sval
= dupstr(buf
);
488 ret
[2].name
= "Height / bottom";
489 ret
[2].type
= C_STRING
;
490 sprintf(buf
, "%d", params
->d2
);
491 ret
[2].sval
= dupstr(buf
);
502 static game_params
*custom_params(config_item
*cfg
)
504 game_params
*ret
= snew(game_params
);
506 ret
->solid
= cfg
[0].ival
;
507 ret
->d1
= atoi(cfg
[1].sval
);
508 ret
->d2
= atoi(cfg
[2].sval
);
513 static void count_grid_square_callback(void *ctx
, struct grid_square
*sq
)
515 int *classes
= (int *)ctx
;
519 thisclass
= sq
->tetra_class
;
520 else if (classes
[4] == 2)
521 thisclass
= sq
->flip
;
525 classes
[thisclass
]++;
528 static char *validate_params(game_params
*params
)
533 if (params
->solid
< 0 || params
->solid
>= lenof(solids
))
534 return "Unrecognised solid type";
536 if (solids
[params
->solid
]->order
== 4) {
537 if (params
->d1
<= 0 || params
->d2
<= 0)
538 return "Both grid dimensions must be greater than zero";
540 if (params
->d1
<= 0 && params
->d2
<= 0)
541 return "At least one grid dimension must be greater than zero";
544 for (i
= 0; i
< 4; i
++)
546 if (params
->solid
== TETRAHEDRON
)
548 else if (params
->solid
== OCTAHEDRON
)
552 enum_grid_squares(params
, count_grid_square_callback
, classes
);
554 for (i
= 0; i
< classes
[4]; i
++)
555 if (classes
[i
] < solids
[params
->solid
]->nfaces
/ classes
[4])
556 return "Not enough grid space to place all blue faces";
558 if (grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
) <
559 solids
[params
->solid
]->nfaces
+ 1)
560 return "Not enough space to place the solid on an empty square";
572 static void classify_grid_square_callback(void *ctx
, struct grid_square
*sq
)
574 struct grid_data
*data
= (struct grid_data
*)ctx
;
577 if (data
->nclasses
== 4)
578 thisclass
= sq
->tetra_class
;
579 else if (data
->nclasses
== 2)
580 thisclass
= sq
->flip
;
584 data
->gridptrs
[thisclass
][data
->nsquares
[thisclass
]++] =
588 static char *new_game_desc(game_params
*params
, random_state
*rs
,
589 game_aux_info
**aux
, int interactive
)
591 struct grid_data data
;
592 int i
, j
, k
, m
, area
, facesperclass
;
597 * Enumerate the grid squares, dividing them into equivalence
598 * classes as appropriate. (For the tetrahedron, there is one
599 * equivalence class for each face; for the octahedron there
600 * are two classes; for the other two solids there's only one.)
603 area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
604 if (params
->solid
== TETRAHEDRON
)
606 else if (params
->solid
== OCTAHEDRON
)
610 data
.gridptrs
[0] = snewn(data
.nclasses
* area
, int);
611 for (i
= 0; i
< data
.nclasses
; i
++) {
612 data
.gridptrs
[i
] = data
.gridptrs
[0] + i
* area
;
613 data
.nsquares
[i
] = 0;
615 data
.squareindex
= 0;
616 enum_grid_squares(params
, classify_grid_square_callback
, &data
);
618 facesperclass
= solids
[params
->solid
]->nfaces
/ data
.nclasses
;
620 for (i
= 0; i
< data
.nclasses
; i
++)
621 assert(data
.nsquares
[i
] >= facesperclass
);
622 assert(data
.squareindex
== area
);
625 * So now we know how many faces to allocate in each class. Get
628 flags
= snewn(area
, int);
629 for (i
= 0; i
< area
; i
++)
632 for (i
= 0; i
< data
.nclasses
; i
++) {
633 for (j
= 0; j
< facesperclass
; j
++) {
634 int n
= random_upto(rs
, data
.nsquares
[i
]);
636 assert(!flags
[data
.gridptrs
[i
][n
]]);
637 flags
[data
.gridptrs
[i
][n
]] = TRUE
;
640 * Move everything else up the array. I ought to use a
641 * better data structure for this, but for such small
642 * numbers it hardly seems worth the effort.
644 while (n
< data
.nsquares
[i
]-1) {
645 data
.gridptrs
[i
][n
] = data
.gridptrs
[i
][n
+1];
653 * Now we know precisely which squares are blue. Encode this
654 * information in hex. While we're looping over this, collect
655 * the non-blue squares into a list in the now-unused gridptrs
658 desc
= snewn(area
/ 4 + 40, char);
663 for (i
= 0; i
< area
; i
++) {
667 data
.gridptrs
[0][m
++] = i
;
671 *p
++ = "0123456789ABCDEF"[j
];
677 *p
++ = "0123456789ABCDEF"[j
];
680 * Choose a non-blue square for the polyhedron.
682 sprintf(p
, ",%d", data
.gridptrs
[0][random_upto(rs
, m
)]);
684 sfree(data
.gridptrs
[0]);
690 static void game_free_aux_info(game_aux_info
*aux
)
692 assert(!"Shouldn't happen");
695 static void add_grid_square_callback(void *ctx
, struct grid_square
*sq
)
697 game_state
*state
= (game_state
*)ctx
;
699 state
->squares
[state
->nsquares
] = *sq
; /* structure copy */
700 state
->squares
[state
->nsquares
].blue
= FALSE
;
704 static int lowest_face(const struct solid
*solid
)
711 for (i
= 0; i
< solid
->nfaces
; i
++) {
714 for (j
= 0; j
< solid
->order
; j
++) {
715 int f
= solid
->faces
[i
*solid
->order
+ j
];
716 z
+= solid
->vertices
[f
*3+2];
719 if (i
== 0 || zmin
> z
) {
728 static int align_poly(const struct solid
*solid
, struct grid_square
*sq
,
733 int flip
= (sq
->flip ?
-1 : +1);
736 * First, find the lowest z-coordinate present in the solid.
739 for (i
= 0; i
< solid
->nvertices
; i
++)
740 if (zmin
> solid
->vertices
[i
*3+2])
741 zmin
= solid
->vertices
[i
*3+2];
744 * Now go round the grid square. For each point in the grid
745 * square, we're looking for a point of the polyhedron with the
746 * same x- and y-coordinates (relative to the square's centre),
747 * and z-coordinate equal to zmin (near enough).
749 for (j
= 0; j
< sq
->npoints
; j
++) {
755 for (i
= 0; i
< solid
->nvertices
; i
++) {
758 dist
+= SQ(solid
->vertices
[i
*3+0] * flip
- sq
->points
[j
*2+0] + sq
->x
);
759 dist
+= SQ(solid
->vertices
[i
*3+1] * flip
- sq
->points
[j
*2+1] + sq
->y
);
760 dist
+= SQ(solid
->vertices
[i
*3+2] - zmin
);
768 if (matches
!= 1 || index
< 0)
776 static void flip_poly(struct solid
*solid
, int flip
)
781 for (i
= 0; i
< solid
->nvertices
; i
++) {
782 solid
->vertices
[i
*3+0] *= -1;
783 solid
->vertices
[i
*3+1] *= -1;
785 for (i
= 0; i
< solid
->nfaces
; i
++) {
786 solid
->normals
[i
*3+0] *= -1;
787 solid
->normals
[i
*3+1] *= -1;
792 static struct solid
*transform_poly(const struct solid
*solid
, int flip
,
793 int key0
, int key1
, float angle
)
795 struct solid
*ret
= snew(struct solid
);
796 float vx
, vy
, ax
, ay
;
797 float vmatrix
[9], amatrix
[9], vmatrix2
[9];
800 *ret
= *solid
; /* structure copy */
802 flip_poly(ret
, flip
);
805 * Now rotate the polyhedron through the given angle. We must
806 * rotate about the Z-axis to bring the two vertices key0 and
807 * key1 into horizontal alignment, then rotate about the
808 * X-axis, then rotate back again.
810 vx
= ret
->vertices
[key1
*3+0] - ret
->vertices
[key0
*3+0];
811 vy
= ret
->vertices
[key1
*3+1] - ret
->vertices
[key0
*3+1];
812 assert(APPROXEQ(vx
*vx
+ vy
*vy
, 1.0));
814 vmatrix
[0] = vx
; vmatrix
[3] = vy
; vmatrix
[6] = 0;
815 vmatrix
[1] = -vy
; vmatrix
[4] = vx
; vmatrix
[7] = 0;
816 vmatrix
[2] = 0; vmatrix
[5] = 0; vmatrix
[8] = 1;
818 ax
= (float)cos(angle
);
819 ay
= (float)sin(angle
);
821 amatrix
[0] = 1; amatrix
[3] = 0; amatrix
[6] = 0;
822 amatrix
[1] = 0; amatrix
[4] = ax
; amatrix
[7] = ay
;
823 amatrix
[2] = 0; amatrix
[5] = -ay
; amatrix
[8] = ax
;
825 memcpy(vmatrix2
, vmatrix
, sizeof(vmatrix
));
829 for (i
= 0; i
< ret
->nvertices
; i
++) {
830 MATMUL(ret
->vertices
+ 3*i
, vmatrix
, ret
->vertices
+ 3*i
);
831 MATMUL(ret
->vertices
+ 3*i
, amatrix
, ret
->vertices
+ 3*i
);
832 MATMUL(ret
->vertices
+ 3*i
, vmatrix2
, ret
->vertices
+ 3*i
);
834 for (i
= 0; i
< ret
->nfaces
; i
++) {
835 MATMUL(ret
->normals
+ 3*i
, vmatrix
, ret
->normals
+ 3*i
);
836 MATMUL(ret
->normals
+ 3*i
, amatrix
, ret
->normals
+ 3*i
);
837 MATMUL(ret
->normals
+ 3*i
, vmatrix2
, ret
->normals
+ 3*i
);
843 static char *validate_desc(game_params
*params
, char *desc
)
845 int area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
849 for (j
= 0; j
< i
; j
++) {
851 if (c
>= '0' && c
<= '9') continue;
852 if (c
>= 'A' && c
<= 'F') continue;
853 if (c
>= 'a' && c
<= 'f') continue;
854 return "Not enough hex digits at start of string";
855 /* NB if desc[j]=='\0' that will also be caught here, so we're safe */
859 return "Expected ',' after hex digits";
863 if (desc
[i
] < '0' || desc
[i
] > '9')
864 return "Expected decimal integer after ','";
871 static game_state
*new_game(midend_data
*me
, game_params
*params
, char *desc
)
873 game_state
*state
= snew(game_state
);
876 state
->params
= *params
; /* structure copy */
877 state
->solid
= solids
[params
->solid
];
879 area
= grid_area(params
->d1
, params
->d2
, state
->solid
->order
);
880 state
->squares
= snewn(area
, struct grid_square
);
882 enum_grid_squares(params
, add_grid_square_callback
, state
);
883 assert(state
->nsquares
== area
);
885 state
->facecolours
= snewn(state
->solid
->nfaces
, int);
886 memset(state
->facecolours
, 0, state
->solid
->nfaces
* sizeof(int));
889 * Set up the blue squares and polyhedron position according to
890 * the game description.
898 for (i
= 0; i
< state
->nsquares
; i
++) {
901 if (v
>= '0' && v
<= '9')
903 else if (v
>= 'A' && v
<= 'F')
905 else if (v
>= 'a' && v
<= 'f')
911 state
->squares
[i
].blue
= TRUE
;
920 state
->current
= atoi(p
);
921 if (state
->current
< 0 || state
->current
>= state
->nsquares
)
922 state
->current
= 0; /* got to do _something_ */
926 * Align the polyhedron with its grid square and determine
927 * initial key points.
933 ret
= align_poly(state
->solid
, &state
->squares
[state
->current
], pkey
);
936 state
->dpkey
[0] = state
->spkey
[0] = pkey
[0];
937 state
->dpkey
[1] = state
->spkey
[0] = pkey
[1];
938 state
->dgkey
[0] = state
->sgkey
[0] = 0;
939 state
->dgkey
[1] = state
->sgkey
[0] = 1;
942 state
->previous
= state
->current
;
944 state
->completed
= 0;
945 state
->movecount
= 0;
950 static game_state
*dup_game(game_state
*state
)
952 game_state
*ret
= snew(game_state
);
954 ret
->params
= state
->params
; /* structure copy */
955 ret
->solid
= state
->solid
;
956 ret
->facecolours
= snewn(ret
->solid
->nfaces
, int);
957 memcpy(ret
->facecolours
, state
->facecolours
,
958 ret
->solid
->nfaces
* sizeof(int));
959 ret
->nsquares
= state
->nsquares
;
960 ret
->current
= state
->current
;
961 ret
->squares
= snewn(ret
->nsquares
, struct grid_square
);
962 memcpy(ret
->squares
, state
->squares
,
963 ret
->nsquares
* sizeof(struct grid_square
));
964 ret
->dpkey
[0] = state
->dpkey
[0];
965 ret
->dpkey
[1] = state
->dpkey
[1];
966 ret
->dgkey
[0] = state
->dgkey
[0];
967 ret
->dgkey
[1] = state
->dgkey
[1];
968 ret
->spkey
[0] = state
->spkey
[0];
969 ret
->spkey
[1] = state
->spkey
[1];
970 ret
->sgkey
[0] = state
->sgkey
[0];
971 ret
->sgkey
[1] = state
->sgkey
[1];
972 ret
->previous
= state
->previous
;
973 ret
->angle
= state
->angle
;
974 ret
->completed
= state
->completed
;
975 ret
->movecount
= state
->movecount
;
980 static void free_game(game_state
*state
)
982 sfree(state
->squares
);
983 sfree(state
->facecolours
);
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 static void game_changed_state(game_ui
*ui
, game_state
*oldstate
,
1008 game_state
*newstate
)
1012 struct game_drawstate
{
1014 int ox
, oy
; /* pixel position of float origin */
1017 static game_state
*make_move(game_state
*from
, game_ui
*ui
, game_drawstate
*ds
,
1018 int x
, int y
, int button
)
1021 int pkey
[2], skey
[2], dkey
[2];
1025 int i
, j
, dest
, mask
;
1028 button
= button
& (~MOD_MASK
| MOD_NUM_KEYPAD
);
1031 * Moves can be made with the cursor keys or numeric keypad, or
1032 * alternatively you can left-click and the polyhedron will
1033 * move in the general direction of the mouse pointer.
1035 if (button
== CURSOR_UP
|| button
== (MOD_NUM_KEYPAD
| '8'))
1037 else if (button
== CURSOR_DOWN
|| button
== (MOD_NUM_KEYPAD
| '2'))
1039 else if (button
== CURSOR_LEFT
|| button
== (MOD_NUM_KEYPAD
| '4'))
1041 else if (button
== CURSOR_RIGHT
|| button
== (MOD_NUM_KEYPAD
| '6'))
1043 else if (button
== (MOD_NUM_KEYPAD
| '7'))
1044 direction
= UP_LEFT
;
1045 else if (button
== (MOD_NUM_KEYPAD
| '1'))
1046 direction
= DOWN_LEFT
;
1047 else if (button
== (MOD_NUM_KEYPAD
| '9'))
1048 direction
= UP_RIGHT
;
1049 else if (button
== (MOD_NUM_KEYPAD
| '3'))
1050 direction
= DOWN_RIGHT
;
1051 else if (button
== LEFT_BUTTON
) {
1053 * Find the bearing of the click point from the current
1059 cx
= from
->squares
[from
->current
].x
* GRID_SCALE
+ ds
->ox
;
1060 cy
= from
->squares
[from
->current
].y
* GRID_SCALE
+ ds
->oy
;
1062 if (x
== cx
&& y
== cy
)
1063 return NULL
; /* clicked in exact centre! */
1064 angle
= atan2(y
- cy
, x
- cx
);
1067 * There are three possibilities.
1069 * - This square is a square, so we choose between UP,
1070 * DOWN, LEFT and RIGHT by dividing the available angle
1071 * at the 45-degree points.
1073 * - This square is an up-pointing triangle, so we choose
1074 * between DOWN, LEFT and RIGHT by dividing into
1077 * - This square is a down-pointing triangle, so we choose
1078 * between UP, LEFT and RIGHT in the inverse manner.
1080 * Don't forget that since our y-coordinates increase
1081 * downwards, `angle' is measured _clockwise_ from the
1082 * x-axis, not anticlockwise as most mathematicians would
1083 * instinctively assume.
1085 if (from
->squares
[from
->current
].npoints
== 4) {
1087 if (fabs(angle
) > 3*PI
/4)
1089 else if (fabs(angle
) < PI
/4)
1095 } else if (from
->squares
[from
->current
].directions
[UP
] == 0) {
1096 /* Up-pointing triangle. */
1097 if (angle
< -PI
/2 || angle
> 5*PI
/6)
1099 else if (angle
> PI
/6)
1104 /* Down-pointing triangle. */
1105 assert(from
->squares
[from
->current
].directions
[DOWN
] == 0);
1106 if (angle
> PI
/2 || angle
< -5*PI
/6)
1108 else if (angle
< -PI
/6)
1117 * Find the two points in the current grid square which
1118 * correspond to this move.
1120 mask
= from
->squares
[from
->current
].directions
[direction
];
1123 for (i
= j
= 0; i
< from
->squares
[from
->current
].npoints
; i
++)
1124 if (mask
& (1 << i
)) {
1125 points
[j
*2] = from
->squares
[from
->current
].points
[i
*2];
1126 points
[j
*2+1] = from
->squares
[from
->current
].points
[i
*2+1];
1133 * Now find the other grid square which shares those points.
1134 * This is our move destination.
1137 for (i
= 0; i
< from
->nsquares
; i
++)
1138 if (i
!= from
->current
) {
1142 for (j
= 0; j
< from
->squares
[i
].npoints
; j
++) {
1143 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[0]) +
1144 SQ(from
->squares
[i
].points
[j
*2+1] - points
[1]));
1147 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[2]) +
1148 SQ(from
->squares
[i
].points
[j
*2+1] - points
[3]));
1162 ret
= dup_game(from
);
1166 * So we know what grid square we're aiming for, and we also
1167 * know the two key points (as indices in both the source and
1168 * destination grid squares) which are invariant between source
1171 * Next we must roll the polyhedron on to that square. So we
1172 * find the indices of the key points within the polyhedron's
1173 * vertex array, then use those in a call to transform_poly,
1174 * and align the result on the new grid square.
1178 align_poly(from
->solid
, &from
->squares
[from
->current
], all_pkey
);
1179 pkey
[0] = all_pkey
[skey
[0]];
1180 pkey
[1] = all_pkey
[skey
[1]];
1182 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1188 * Now find the angle through which to rotate the polyhedron.
1189 * Do this by finding the two faces that share the two vertices
1190 * we've found, and taking the dot product of their normals.
1196 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1198 for (j
= 0; j
< from
->solid
->order
; j
++)
1199 if (from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[0] ||
1200 from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[1])
1211 for (i
= 0; i
< 3; i
++)
1212 dp
+= (from
->solid
->normals
[f
[0]*3+i
] *
1213 from
->solid
->normals
[f
[1]*3+i
]);
1214 angle
= (float)acos(dp
);
1218 * Now transform the polyhedron. We aren't entirely sure
1219 * whether we need to rotate through angle or -angle, and the
1220 * simplest way round this is to try both and see which one
1221 * aligns successfully!
1223 * Unfortunately, _both_ will align successfully if this is a
1224 * cube, which won't tell us anything much. So for that
1225 * particular case, I resort to gross hackery: I simply negate
1226 * the angle before trying the alignment, depending on the
1227 * direction. Which directions work which way is determined by
1228 * pure trial and error. I said it was gross :-/
1234 if (from
->solid
->order
== 4 && direction
== UP
)
1235 angle
= -angle
; /* HACK */
1237 poly
= transform_poly(from
->solid
,
1238 from
->squares
[from
->current
].flip
,
1239 pkey
[0], pkey
[1], angle
);
1240 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1241 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1246 poly
= transform_poly(from
->solid
,
1247 from
->squares
[from
->current
].flip
,
1248 pkey
[0], pkey
[1], angle
);
1249 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1250 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1257 * Now we have our rotated polyhedron, which we expect to be
1258 * exactly congruent to the one we started with - but with the
1259 * faces permuted. So we map that congruence and thereby figure
1260 * out how to permute the faces as a result of the polyhedron
1264 int *newcolours
= snewn(from
->solid
->nfaces
, int);
1266 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1269 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1273 * Now go through the transformed polyhedron's faces
1274 * and figure out which one's normal is approximately
1275 * equal to this one.
1277 for (j
= 0; j
< poly
->nfaces
; j
++) {
1283 for (k
= 0; k
< 3; k
++)
1284 dist
+= SQ(poly
->normals
[j
*3+k
] -
1285 from
->solid
->normals
[i
*3+k
]);
1287 if (APPROXEQ(dist
, 0)) {
1289 newcolours
[i
] = ret
->facecolours
[j
];
1293 assert(nmatch
== 1);
1296 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1297 assert(newcolours
[i
] != -1);
1299 sfree(ret
->facecolours
);
1300 ret
->facecolours
= newcolours
;
1306 * And finally, swap the colour between the bottom face of the
1307 * polyhedron and the face we've just landed on.
1309 * We don't do this if the game is already complete, since we
1310 * allow the user to roll the fully blue polyhedron around the
1311 * grid as a feeble reward.
1313 if (!ret
->completed
) {
1314 i
= lowest_face(from
->solid
);
1315 j
= ret
->facecolours
[i
];
1316 ret
->facecolours
[i
] = ret
->squares
[ret
->current
].blue
;
1317 ret
->squares
[ret
->current
].blue
= j
;
1320 * Detect game completion.
1323 for (i
= 0; i
< ret
->solid
->nfaces
; i
++)
1324 if (ret
->facecolours
[i
])
1326 if (j
== ret
->solid
->nfaces
)
1327 ret
->completed
= ret
->movecount
;
1333 * Align the normal polyhedron with its grid square, to get key
1334 * points for non-animated display.
1340 success
= align_poly(ret
->solid
, &ret
->squares
[ret
->current
], pkey
);
1343 ret
->dpkey
[0] = pkey
[0];
1344 ret
->dpkey
[1] = pkey
[1];
1350 ret
->spkey
[0] = pkey
[0];
1351 ret
->spkey
[1] = pkey
[1];
1352 ret
->sgkey
[0] = skey
[0];
1353 ret
->sgkey
[1] = skey
[1];
1354 ret
->previous
= from
->current
;
1360 /* ----------------------------------------------------------------------
1368 static void find_bbox_callback(void *ctx
, struct grid_square
*sq
)
1370 struct bbox
*bb
= (struct bbox
*)ctx
;
1373 for (i
= 0; i
< sq
->npoints
; i
++) {
1374 if (bb
->l
> sq
->points
[i
*2]) bb
->l
= sq
->points
[i
*2];
1375 if (bb
->r
< sq
->points
[i
*2]) bb
->r
= sq
->points
[i
*2];
1376 if (bb
->u
> sq
->points
[i
*2+1]) bb
->u
= sq
->points
[i
*2+1];
1377 if (bb
->d
< sq
->points
[i
*2+1]) bb
->d
= sq
->points
[i
*2+1];
1381 static struct bbox
find_bbox(game_params
*params
)
1386 * These should be hugely more than the real bounding box will
1389 bb
.l
= 2.0F
* (params
->d1
+ params
->d2
);
1390 bb
.r
= -2.0F
* (params
->d1
+ params
->d2
);
1391 bb
.u
= 2.0F
* (params
->d1
+ params
->d2
);
1392 bb
.d
= -2.0F
* (params
->d1
+ params
->d2
);
1393 enum_grid_squares(params
, find_bbox_callback
, &bb
);
1398 #define XSIZE(bb, solid) \
1399 ((int)(((bb).r - (bb).l + 2*(solid)->border) * GRID_SCALE))
1400 #define YSIZE(bb, solid) \
1401 ((int)(((bb).d - (bb).u + 2*(solid)->border) * GRID_SCALE))
1403 static void game_size(game_params
*params
, game_drawstate
*ds
, int *x
, int *y
,
1406 struct bbox bb
= find_bbox(params
);
1409 gsx
= *x
/ (bb
.r
- bb
.l
+ 2*solids
[params
->solid
]->border
);
1410 gsy
= *y
/ (bb
.d
- bb
.u
+ 2*solids
[params
->solid
]->border
);
1416 ds
->gridscale
= min(gs
, PREFERRED_GRID_SCALE
);
1418 ds
->ox
= (int)(-(bb
.l
- solids
[params
->solid
]->border
) * GRID_SCALE
);
1419 ds
->oy
= (int)(-(bb
.u
- solids
[params
->solid
]->border
) * GRID_SCALE
);
1421 *x
= XSIZE(bb
, solids
[params
->solid
]);
1422 *y
= YSIZE(bb
, solids
[params
->solid
]);
1425 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1427 float *ret
= snewn(3 * NCOLOURS
, float);
1429 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1431 ret
[COL_BORDER
* 3 + 0] = 0.0;
1432 ret
[COL_BORDER
* 3 + 1] = 0.0;
1433 ret
[COL_BORDER
* 3 + 2] = 0.0;
1435 ret
[COL_BLUE
* 3 + 0] = 0.0;
1436 ret
[COL_BLUE
* 3 + 1] = 0.0;
1437 ret
[COL_BLUE
* 3 + 2] = 1.0;
1439 *ncolours
= NCOLOURS
;
1443 static game_drawstate
*game_new_drawstate(game_state
*state
)
1445 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1447 ds
->ox
= ds
->oy
= ds
->gridscale
= 0.0F
;/* not decided yet */
1452 static void game_free_drawstate(game_drawstate
*ds
)
1457 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1458 game_state
*state
, int dir
, game_ui
*ui
,
1459 float animtime
, float flashtime
)
1462 struct bbox bb
= find_bbox(&state
->params
);
1467 game_state
*newstate
;
1470 draw_rect(fe
, 0, 0, XSIZE(bb
, state
->solid
), YSIZE(bb
, state
->solid
),
1477 * This is an Undo. So reverse the order of the states, and
1478 * run the roll timer backwards.
1486 animtime
= ROLLTIME
- animtime
;
1492 square
= state
->current
;
1493 pkey
= state
->dpkey
;
1494 gkey
= state
->dgkey
;
1496 angle
= state
->angle
* animtime
/ ROLLTIME
;
1497 square
= state
->previous
;
1498 pkey
= state
->spkey
;
1499 gkey
= state
->sgkey
;
1504 for (i
= 0; i
< state
->nsquares
; i
++) {
1507 for (j
= 0; j
< state
->squares
[i
].npoints
; j
++) {
1508 coords
[2*j
] = ((int)(state
->squares
[i
].points
[2*j
] * GRID_SCALE
)
1510 coords
[2*j
+1] = ((int)(state
->squares
[i
].points
[2*j
+1]*GRID_SCALE
)
1514 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, TRUE
,
1515 state
->squares
[i
].blue ? COL_BLUE
: COL_BACKGROUND
);
1516 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, FALSE
, COL_BORDER
);
1520 * Now compute and draw the polyhedron.
1522 poly
= transform_poly(state
->solid
, state
->squares
[square
].flip
,
1523 pkey
[0], pkey
[1], angle
);
1526 * Compute the translation required to align the two key points
1527 * on the polyhedron with the same key points on the current
1530 for (i
= 0; i
< 3; i
++) {
1533 for (j
= 0; j
< 2; j
++) {
1538 state
->squares
[square
].points
[gkey
[j
]*2+i
];
1543 tc
+= (grid_coord
- poly
->vertices
[pkey
[j
]*3+i
]);
1548 for (i
= 0; i
< poly
->nvertices
; i
++)
1549 for (j
= 0; j
< 3; j
++)
1550 poly
->vertices
[i
*3+j
] += t
[j
];
1553 * Now actually draw each face.
1555 for (i
= 0; i
< poly
->nfaces
; i
++) {
1559 for (j
= 0; j
< poly
->order
; j
++) {
1560 int f
= poly
->faces
[i
*poly
->order
+ j
];
1561 points
[j
*2] = (poly
->vertices
[f
*3+0] -
1562 poly
->vertices
[f
*3+2] * poly
->shear
);
1563 points
[j
*2+1] = (poly
->vertices
[f
*3+1] -
1564 poly
->vertices
[f
*3+2] * poly
->shear
);
1567 for (j
= 0; j
< poly
->order
; j
++) {
1568 coords
[j
*2] = (int)floor(points
[j
*2] * GRID_SCALE
) + ds
->ox
;
1569 coords
[j
*2+1] = (int)floor(points
[j
*2+1] * GRID_SCALE
) + ds
->oy
;
1573 * Find out whether these points are in a clockwise or
1574 * anticlockwise arrangement. If the latter, discard the
1575 * face because it's facing away from the viewer.
1577 * This would involve fiddly winding-number stuff for a
1578 * general polygon, but for the simple parallelograms we'll
1579 * be seeing here, all we have to do is check whether the
1580 * corners turn right or left. So we'll take the vector
1581 * from point 0 to point 1, turn it right 90 degrees,
1582 * and check the sign of the dot product with that and the
1583 * next vector (point 1 to point 2).
1586 float v1x
= points
[2]-points
[0];
1587 float v1y
= points
[3]-points
[1];
1588 float v2x
= points
[4]-points
[2];
1589 float v2y
= points
[5]-points
[3];
1590 float dp
= v1x
* v2y
- v1y
* v2x
;
1596 draw_polygon(fe
, coords
, poly
->order
, TRUE
,
1597 state
->facecolours
[i
] ? COL_BLUE
: COL_BACKGROUND
);
1598 draw_polygon(fe
, coords
, poly
->order
, FALSE
, COL_BORDER
);
1602 draw_update(fe
, 0, 0, XSIZE(bb
, state
->solid
), YSIZE(bb
, state
->solid
));
1605 * Update the status bar.
1608 char statusbuf
[256];
1610 sprintf(statusbuf
, "%sMoves: %d",
1611 (state
->completed ?
"COMPLETED! " : ""),
1612 (state
->completed ? state
->completed
: state
->movecount
));
1614 status_bar(fe
, statusbuf
);
1618 static float game_anim_length(game_state
*oldstate
,
1619 game_state
*newstate
, int dir
, game_ui
*ui
)
1624 static float game_flash_length(game_state
*oldstate
,
1625 game_state
*newstate
, int dir
, game_ui
*ui
)
1630 static int game_wants_statusbar(void)
1635 static int game_timing_state(game_state
*state
)
1641 #define thegame cube
1644 const struct game thegame
= {
1645 "Cube", "games.cube",
1652 TRUE
, game_configure
, custom_params
,
1661 FALSE
, game_text_format
,
1669 game_free_drawstate
,
1673 game_wants_statusbar
,
1674 FALSE
, game_timing_state
,
1675 0, /* mouse_priorities */