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 GRID_SCALE 48.0F
161 #define ROLLTIME 0.13F
163 #define SQ(x) ( (x) * (x) )
165 #define MATMUL(ra,m,a) do { \
166 float rx, ry, rz, xx = (a)[0], yy = (a)[1], zz = (a)[2], *mat = (m); \
167 rx = mat[0] * xx + mat[3] * yy + mat[6] * zz; \
168 ry = mat[1] * xx + mat[4] * yy + mat[7] * zz; \
169 rz = mat[2] * xx + mat[5] * yy + mat[8] * zz; \
170 (ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \
173 #define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
178 float points
[8]; /* maximum */
179 int directions
[8]; /* bit masks showing point pairs */
188 * Grid dimensions. For a square grid these are width and
189 * height respectively; otherwise the grid is a hexagon, with
190 * the top side and the two lower diagonals having length d1
191 * and the remaining three sides having length d2 (so that
192 * d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
198 struct game_params params
;
199 const struct solid
*solid
;
201 struct grid_square
*squares
;
203 int current
; /* index of current grid square */
204 int sgkey
[2]; /* key-point indices into grid sq */
205 int dgkey
[2]; /* key-point indices into grid sq */
206 int spkey
[2]; /* key-point indices into polyhedron */
207 int dpkey
[2]; /* key-point indices into polyhedron */
214 static game_params
*default_params(void)
216 game_params
*ret
= snew(game_params
);
225 static int game_fetch_preset(int i
, char **name
, game_params
**params
)
227 game_params
*ret
= snew(game_params
);
239 ret
->solid
= TETRAHEDRON
;
245 ret
->solid
= OCTAHEDRON
;
251 ret
->solid
= ICOSAHEDRON
;
265 static void free_params(game_params
*params
)
270 static game_params
*dup_params(game_params
*params
)
272 game_params
*ret
= snew(game_params
);
273 *ret
= *params
; /* structure copy */
277 static game_params
*decode_params(char const *string
)
279 game_params
*ret
= default_params();
282 case 't': ret
->solid
= TETRAHEDRON
; string
++; break;
283 case 'c': ret
->solid
= CUBE
; string
++; break;
284 case 'o': ret
->solid
= OCTAHEDRON
; string
++; break;
285 case 'i': ret
->solid
= ICOSAHEDRON
; string
++; break;
288 ret
->d1
= ret
->d2
= atoi(string
);
289 while (*string
&& isdigit(*string
)) string
++;
290 if (*string
== 'x') {
292 ret
->d2
= atoi(string
);
298 static char *encode_params(game_params
*params
)
302 assert(params
->solid
>= 0 && params
->solid
< 4);
303 sprintf(data
, "%c%dx%d", "tcoi"[params
->solid
], params
->d1
, params
->d2
);
308 static void enum_grid_squares(game_params
*params
,
309 void (*callback
)(void *, struct grid_square
*),
312 const struct solid
*solid
= solids
[params
->solid
];
314 if (solid
->order
== 4) {
317 for (y
= 0; y
< params
->d2
; y
++)
318 for (x
= 0; x
< params
->d1
; x
++) {
319 struct grid_square sq
;
323 sq
.points
[0] = x
- 0.5F
;
324 sq
.points
[1] = y
- 0.5F
;
325 sq
.points
[2] = x
- 0.5F
;
326 sq
.points
[3] = y
+ 0.5F
;
327 sq
.points
[4] = x
+ 0.5F
;
328 sq
.points
[5] = y
+ 0.5F
;
329 sq
.points
[6] = x
+ 0.5F
;
330 sq
.points
[7] = y
- 0.5F
;
333 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
334 sq
.directions
[RIGHT
] = 0x0C; /* 2,3 */
335 sq
.directions
[UP
] = 0x09; /* 0,3 */
336 sq
.directions
[DOWN
] = 0x06; /* 1,2 */
337 sq
.directions
[UP_LEFT
] = 0; /* no diagonals in a square */
338 sq
.directions
[UP_RIGHT
] = 0; /* no diagonals in a square */
339 sq
.directions
[DOWN_LEFT
] = 0; /* no diagonals in a square */
340 sq
.directions
[DOWN_RIGHT
] = 0; /* no diagonals in a square */
345 * This is supremely irrelevant, but just to avoid
346 * having any uninitialised structure members...
353 int row
, rowlen
, other
, i
, firstix
= -1;
354 float theight
= (float)(sqrt(3) / 2.0);
356 for (row
= 0; row
< params
->d1
+ params
->d2
; row
++) {
357 if (row
< params
->d2
) {
359 rowlen
= row
+ params
->d1
;
362 rowlen
= 2*params
->d2
+ params
->d1
- row
;
366 * There are `rowlen' down-pointing triangles.
368 for (i
= 0; i
< rowlen
; i
++) {
369 struct grid_square sq
;
373 ix
= (2 * i
- (rowlen
-1));
377 sq
.y
= y
+ theight
/ 3;
378 sq
.points
[0] = x
- 0.5F
;
381 sq
.points
[3] = y
+ theight
;
382 sq
.points
[4] = x
+ 0.5F
;
386 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
387 sq
.directions
[RIGHT
] = 0x06; /* 1,2 */
388 sq
.directions
[UP
] = 0x05; /* 0,2 */
389 sq
.directions
[DOWN
] = 0; /* invalid move */
392 * Down-pointing triangle: both the up diagonals go
393 * up, and the down ones go left and right.
395 sq
.directions
[UP_LEFT
] = sq
.directions
[UP_RIGHT
] =
397 sq
.directions
[DOWN_LEFT
] = sq
.directions
[LEFT
];
398 sq
.directions
[DOWN_RIGHT
] = sq
.directions
[RIGHT
];
405 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
411 * There are `rowlen+other' up-pointing triangles.
413 for (i
= 0; i
< rowlen
+other
; i
++) {
414 struct grid_square sq
;
418 ix
= (2 * i
- (rowlen
+other
-1));
422 sq
.y
= y
+ 2*theight
/ 3;
423 sq
.points
[0] = x
+ 0.5F
;
424 sq
.points
[1] = y
+ theight
;
427 sq
.points
[4] = x
- 0.5F
;
428 sq
.points
[5] = y
+ theight
;
431 sq
.directions
[LEFT
] = 0x06; /* 1,2 */
432 sq
.directions
[RIGHT
] = 0x03; /* 0,1 */
433 sq
.directions
[DOWN
] = 0x05; /* 0,2 */
434 sq
.directions
[UP
] = 0; /* invalid move */
437 * Up-pointing triangle: both the down diagonals go
438 * down, and the up ones go left and right.
440 sq
.directions
[DOWN_LEFT
] = sq
.directions
[DOWN_RIGHT
] =
442 sq
.directions
[UP_LEFT
] = sq
.directions
[LEFT
];
443 sq
.directions
[UP_RIGHT
] = sq
.directions
[RIGHT
];
448 firstix
= (ix
- 1) & 3;
450 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
458 static int grid_area(int d1
, int d2
, int order
)
461 * An NxM grid of squares has NM squares in it.
463 * A grid of triangles with dimensions A and B has a total of
464 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
465 * a side-A triangle containing A^2 subtriangles, a side-B
466 * triangle containing B^2, and two congruent parallelograms,
467 * each with side lengths A and B, each therefore containing AB
468 * two-triangle rhombuses.)
473 return d1
*d1
+ d2
*d2
+ 4*d1
*d2
;
476 static config_item
*game_configure(game_params
*params
)
478 config_item
*ret
= snewn(4, config_item
);
481 ret
[0].name
= "Type of solid";
482 ret
[0].type
= C_CHOICES
;
483 ret
[0].sval
= ":Tetrahedron:Cube:Octahedron:Icosahedron";
484 ret
[0].ival
= params
->solid
;
486 ret
[1].name
= "Width / top";
487 ret
[1].type
= C_STRING
;
488 sprintf(buf
, "%d", params
->d1
);
489 ret
[1].sval
= dupstr(buf
);
492 ret
[2].name
= "Height / bottom";
493 ret
[2].type
= C_STRING
;
494 sprintf(buf
, "%d", params
->d2
);
495 ret
[2].sval
= dupstr(buf
);
506 static game_params
*custom_params(config_item
*cfg
)
508 game_params
*ret
= snew(game_params
);
510 ret
->solid
= cfg
[0].ival
;
511 ret
->d1
= atoi(cfg
[1].sval
);
512 ret
->d2
= atoi(cfg
[2].sval
);
517 static void count_grid_square_callback(void *ctx
, struct grid_square
*sq
)
519 int *classes
= (int *)ctx
;
523 thisclass
= sq
->tetra_class
;
524 else if (classes
[4] == 2)
525 thisclass
= sq
->flip
;
529 classes
[thisclass
]++;
532 static char *validate_params(game_params
*params
)
537 if (params
->solid
< 0 || params
->solid
>= lenof(solids
))
538 return "Unrecognised solid type";
540 if (solids
[params
->solid
]->order
== 4) {
541 if (params
->d1
<= 0 || params
->d2
<= 0)
542 return "Both grid dimensions must be greater than zero";
544 if (params
->d1
<= 0 && params
->d2
<= 0)
545 return "At least one grid dimension must be greater than zero";
548 for (i
= 0; i
< 4; i
++)
550 if (params
->solid
== TETRAHEDRON
)
552 else if (params
->solid
== OCTAHEDRON
)
556 enum_grid_squares(params
, count_grid_square_callback
, classes
);
558 for (i
= 0; i
< classes
[4]; i
++)
559 if (classes
[i
] < solids
[params
->solid
]->nfaces
/ classes
[4])
560 return "Not enough grid space to place all blue faces";
562 if (grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
) <
563 solids
[params
->solid
]->nfaces
+ 1)
564 return "Not enough space to place the solid on an empty square";
576 static void classify_grid_square_callback(void *ctx
, struct grid_square
*sq
)
578 struct grid_data
*data
= (struct grid_data
*)ctx
;
581 if (data
->nclasses
== 4)
582 thisclass
= sq
->tetra_class
;
583 else if (data
->nclasses
== 2)
584 thisclass
= sq
->flip
;
588 data
->gridptrs
[thisclass
][data
->nsquares
[thisclass
]++] =
592 static char *new_game_seed(game_params
*params
, random_state
*rs
,
595 struct grid_data data
;
596 int i
, j
, k
, m
, area
, facesperclass
;
601 * Enumerate the grid squares, dividing them into equivalence
602 * classes as appropriate. (For the tetrahedron, there is one
603 * equivalence class for each face; for the octahedron there
604 * are two classes; for the other two solids there's only one.)
607 area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
608 if (params
->solid
== TETRAHEDRON
)
610 else if (params
->solid
== OCTAHEDRON
)
614 data
.gridptrs
[0] = snewn(data
.nclasses
* area
, int);
615 for (i
= 0; i
< data
.nclasses
; i
++) {
616 data
.gridptrs
[i
] = data
.gridptrs
[0] + i
* area
;
617 data
.nsquares
[i
] = 0;
619 data
.squareindex
= 0;
620 enum_grid_squares(params
, classify_grid_square_callback
, &data
);
622 facesperclass
= solids
[params
->solid
]->nfaces
/ data
.nclasses
;
624 for (i
= 0; i
< data
.nclasses
; i
++)
625 assert(data
.nsquares
[i
] >= facesperclass
);
626 assert(data
.squareindex
== area
);
629 * So now we know how many faces to allocate in each class. Get
632 flags
= snewn(area
, int);
633 for (i
= 0; i
< area
; i
++)
636 for (i
= 0; i
< data
.nclasses
; i
++) {
637 for (j
= 0; j
< facesperclass
; j
++) {
638 int n
= random_upto(rs
, data
.nsquares
[i
]);
640 assert(!flags
[data
.gridptrs
[i
][n
]]);
641 flags
[data
.gridptrs
[i
][n
]] = TRUE
;
644 * Move everything else up the array. I ought to use a
645 * better data structure for this, but for such small
646 * numbers it hardly seems worth the effort.
648 while (n
< data
.nsquares
[i
]-1) {
649 data
.gridptrs
[i
][n
] = data
.gridptrs
[i
][n
+1];
657 * Now we know precisely which squares are blue. Encode this
658 * information in hex. While we're looping over this, collect
659 * the non-blue squares into a list in the now-unused gridptrs
662 seed
= snewn(area
/ 4 + 40, char);
667 for (i
= 0; i
< area
; i
++) {
671 data
.gridptrs
[0][m
++] = i
;
675 *p
++ = "0123456789ABCDEF"[j
];
681 *p
++ = "0123456789ABCDEF"[j
];
684 * Choose a non-blue square for the polyhedron.
686 sprintf(p
, ",%d", data
.gridptrs
[0][random_upto(rs
, m
)]);
688 sfree(data
.gridptrs
[0]);
694 static void game_free_aux_info(game_aux_info
*aux
)
696 assert(!"Shouldn't happen");
699 static void add_grid_square_callback(void *ctx
, struct grid_square
*sq
)
701 game_state
*state
= (game_state
*)ctx
;
703 state
->squares
[state
->nsquares
] = *sq
; /* structure copy */
704 state
->squares
[state
->nsquares
].blue
= FALSE
;
708 static int lowest_face(const struct solid
*solid
)
715 for (i
= 0; i
< solid
->nfaces
; i
++) {
718 for (j
= 0; j
< solid
->order
; j
++) {
719 int f
= solid
->faces
[i
*solid
->order
+ j
];
720 z
+= solid
->vertices
[f
*3+2];
723 if (i
== 0 || zmin
> z
) {
732 static int align_poly(const struct solid
*solid
, struct grid_square
*sq
,
737 int flip
= (sq
->flip ?
-1 : +1);
740 * First, find the lowest z-coordinate present in the solid.
743 for (i
= 0; i
< solid
->nvertices
; i
++)
744 if (zmin
> solid
->vertices
[i
*3+2])
745 zmin
= solid
->vertices
[i
*3+2];
748 * Now go round the grid square. For each point in the grid
749 * square, we're looking for a point of the polyhedron with the
750 * same x- and y-coordinates (relative to the square's centre),
751 * and z-coordinate equal to zmin (near enough).
753 for (j
= 0; j
< sq
->npoints
; j
++) {
759 for (i
= 0; i
< solid
->nvertices
; i
++) {
762 dist
+= SQ(solid
->vertices
[i
*3+0] * flip
- sq
->points
[j
*2+0] + sq
->x
);
763 dist
+= SQ(solid
->vertices
[i
*3+1] * flip
- sq
->points
[j
*2+1] + sq
->y
);
764 dist
+= SQ(solid
->vertices
[i
*3+2] - zmin
);
772 if (matches
!= 1 || index
< 0)
780 static void flip_poly(struct solid
*solid
, int flip
)
785 for (i
= 0; i
< solid
->nvertices
; i
++) {
786 solid
->vertices
[i
*3+0] *= -1;
787 solid
->vertices
[i
*3+1] *= -1;
789 for (i
= 0; i
< solid
->nfaces
; i
++) {
790 solid
->normals
[i
*3+0] *= -1;
791 solid
->normals
[i
*3+1] *= -1;
796 static struct solid
*transform_poly(const struct solid
*solid
, int flip
,
797 int key0
, int key1
, float angle
)
799 struct solid
*ret
= snew(struct solid
);
800 float vx
, vy
, ax
, ay
;
801 float vmatrix
[9], amatrix
[9], vmatrix2
[9];
804 *ret
= *solid
; /* structure copy */
806 flip_poly(ret
, flip
);
809 * Now rotate the polyhedron through the given angle. We must
810 * rotate about the Z-axis to bring the two vertices key0 and
811 * key1 into horizontal alignment, then rotate about the
812 * X-axis, then rotate back again.
814 vx
= ret
->vertices
[key1
*3+0] - ret
->vertices
[key0
*3+0];
815 vy
= ret
->vertices
[key1
*3+1] - ret
->vertices
[key0
*3+1];
816 assert(APPROXEQ(vx
*vx
+ vy
*vy
, 1.0));
818 vmatrix
[0] = vx
; vmatrix
[3] = vy
; vmatrix
[6] = 0;
819 vmatrix
[1] = -vy
; vmatrix
[4] = vx
; vmatrix
[7] = 0;
820 vmatrix
[2] = 0; vmatrix
[5] = 0; vmatrix
[8] = 1;
822 ax
= (float)cos(angle
);
823 ay
= (float)sin(angle
);
825 amatrix
[0] = 1; amatrix
[3] = 0; amatrix
[6] = 0;
826 amatrix
[1] = 0; amatrix
[4] = ax
; amatrix
[7] = ay
;
827 amatrix
[2] = 0; amatrix
[5] = -ay
; amatrix
[8] = ax
;
829 memcpy(vmatrix2
, vmatrix
, sizeof(vmatrix
));
833 for (i
= 0; i
< ret
->nvertices
; i
++) {
834 MATMUL(ret
->vertices
+ 3*i
, vmatrix
, ret
->vertices
+ 3*i
);
835 MATMUL(ret
->vertices
+ 3*i
, amatrix
, ret
->vertices
+ 3*i
);
836 MATMUL(ret
->vertices
+ 3*i
, vmatrix2
, ret
->vertices
+ 3*i
);
838 for (i
= 0; i
< ret
->nfaces
; i
++) {
839 MATMUL(ret
->normals
+ 3*i
, vmatrix
, ret
->normals
+ 3*i
);
840 MATMUL(ret
->normals
+ 3*i
, amatrix
, ret
->normals
+ 3*i
);
841 MATMUL(ret
->normals
+ 3*i
, vmatrix2
, ret
->normals
+ 3*i
);
847 static char *validate_seed(game_params
*params
, char *seed
)
849 int area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
853 for (j
= 0; j
< i
; j
++) {
855 if (c
>= '0' && c
<= '9') continue;
856 if (c
>= 'A' && c
<= 'F') continue;
857 if (c
>= 'a' && c
<= 'f') continue;
858 return "Not enough hex digits at start of string";
859 /* NB if seed[j]=='\0' that will also be caught here, so we're safe */
863 return "Expected ',' after hex digits";
867 if (seed
[i
] < '0' || seed
[i
] > '9')
868 return "Expected decimal integer after ','";
875 static game_state
*new_game(game_params
*params
, char *seed
)
877 game_state
*state
= snew(game_state
);
880 state
->params
= *params
; /* structure copy */
881 state
->solid
= solids
[params
->solid
];
883 area
= grid_area(params
->d1
, params
->d2
, state
->solid
->order
);
884 state
->squares
= snewn(area
, struct grid_square
);
886 enum_grid_squares(params
, add_grid_square_callback
, state
);
887 assert(state
->nsquares
== area
);
889 state
->facecolours
= snewn(state
->solid
->nfaces
, int);
890 memset(state
->facecolours
, 0, state
->solid
->nfaces
* sizeof(int));
893 * Set up the blue squares and polyhedron position according to
902 for (i
= 0; i
< state
->nsquares
; i
++) {
905 if (v
>= '0' && v
<= '9')
907 else if (v
>= 'A' && v
<= 'F')
909 else if (v
>= 'a' && v
<= 'f')
915 state
->squares
[i
].blue
= TRUE
;
924 state
->current
= atoi(p
);
925 if (state
->current
< 0 || state
->current
>= state
->nsquares
)
926 state
->current
= 0; /* got to do _something_ */
930 * Align the polyhedron with its grid square and determine
931 * initial key points.
937 ret
= align_poly(state
->solid
, &state
->squares
[state
->current
], pkey
);
940 state
->dpkey
[0] = state
->spkey
[0] = pkey
[0];
941 state
->dpkey
[1] = state
->spkey
[0] = pkey
[1];
942 state
->dgkey
[0] = state
->sgkey
[0] = 0;
943 state
->dgkey
[1] = state
->sgkey
[0] = 1;
946 state
->previous
= state
->current
;
948 state
->completed
= 0;
949 state
->movecount
= 0;
954 static game_state
*dup_game(game_state
*state
)
956 game_state
*ret
= snew(game_state
);
958 ret
->params
= state
->params
; /* structure copy */
959 ret
->solid
= state
->solid
;
960 ret
->facecolours
= snewn(ret
->solid
->nfaces
, int);
961 memcpy(ret
->facecolours
, state
->facecolours
,
962 ret
->solid
->nfaces
* sizeof(int));
963 ret
->nsquares
= state
->nsquares
;
964 ret
->squares
= snewn(ret
->nsquares
, struct grid_square
);
965 memcpy(ret
->squares
, state
->squares
,
966 ret
->nsquares
* sizeof(struct grid_square
));
967 ret
->dpkey
[0] = state
->dpkey
[0];
968 ret
->dpkey
[1] = state
->dpkey
[1];
969 ret
->dgkey
[0] = state
->dgkey
[0];
970 ret
->dgkey
[1] = state
->dgkey
[1];
971 ret
->spkey
[0] = state
->spkey
[0];
972 ret
->spkey
[1] = state
->spkey
[1];
973 ret
->sgkey
[0] = state
->sgkey
[0];
974 ret
->sgkey
[1] = state
->sgkey
[1];
975 ret
->previous
= state
->previous
;
976 ret
->angle
= state
->angle
;
977 ret
->completed
= state
->completed
;
978 ret
->movecount
= state
->movecount
;
983 static void free_game(game_state
*state
)
988 static game_state
*solve_game(game_state
*state
, game_aux_info
*aux
,
994 static char *game_text_format(game_state
*state
)
999 static game_ui
*new_ui(game_state
*state
)
1004 static void free_ui(game_ui
*ui
)
1008 static game_state
*make_move(game_state
*from
, game_ui
*ui
,
1009 int x
, int y
, int button
)
1012 int pkey
[2], skey
[2], dkey
[2];
1016 int i
, j
, dest
, mask
;
1020 * All moves are made with the cursor keys.
1022 if (button
== CURSOR_UP
)
1024 else if (button
== CURSOR_DOWN
)
1026 else if (button
== CURSOR_LEFT
)
1028 else if (button
== CURSOR_RIGHT
)
1030 else if (button
== CURSOR_UP_LEFT
)
1031 direction
= UP_LEFT
;
1032 else if (button
== CURSOR_DOWN_LEFT
)
1033 direction
= DOWN_LEFT
;
1034 else if (button
== CURSOR_UP_RIGHT
)
1035 direction
= UP_RIGHT
;
1036 else if (button
== CURSOR_DOWN_RIGHT
)
1037 direction
= DOWN_RIGHT
;
1042 * Find the two points in the current grid square which
1043 * correspond to this move.
1045 mask
= from
->squares
[from
->current
].directions
[direction
];
1048 for (i
= j
= 0; i
< from
->squares
[from
->current
].npoints
; i
++)
1049 if (mask
& (1 << i
)) {
1050 points
[j
*2] = from
->squares
[from
->current
].points
[i
*2];
1051 points
[j
*2+1] = from
->squares
[from
->current
].points
[i
*2+1];
1058 * Now find the other grid square which shares those points.
1059 * This is our move destination.
1062 for (i
= 0; i
< from
->nsquares
; i
++)
1063 if (i
!= from
->current
) {
1067 for (j
= 0; j
< from
->squares
[i
].npoints
; j
++) {
1068 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[0]) +
1069 SQ(from
->squares
[i
].points
[j
*2+1] - points
[1]));
1072 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[2]) +
1073 SQ(from
->squares
[i
].points
[j
*2+1] - points
[3]));
1087 ret
= dup_game(from
);
1091 * So we know what grid square we're aiming for, and we also
1092 * know the two key points (as indices in both the source and
1093 * destination grid squares) which are invariant between source
1096 * Next we must roll the polyhedron on to that square. So we
1097 * find the indices of the key points within the polyhedron's
1098 * vertex array, then use those in a call to transform_poly,
1099 * and align the result on the new grid square.
1103 align_poly(from
->solid
, &from
->squares
[from
->current
], all_pkey
);
1104 pkey
[0] = all_pkey
[skey
[0]];
1105 pkey
[1] = all_pkey
[skey
[1]];
1107 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1113 * Now find the angle through which to rotate the polyhedron.
1114 * Do this by finding the two faces that share the two vertices
1115 * we've found, and taking the dot product of their normals.
1121 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1123 for (j
= 0; j
< from
->solid
->order
; j
++)
1124 if (from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[0] ||
1125 from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[1])
1136 for (i
= 0; i
< 3; i
++)
1137 dp
+= (from
->solid
->normals
[f
[0]*3+i
] *
1138 from
->solid
->normals
[f
[1]*3+i
]);
1139 angle
= (float)acos(dp
);
1143 * Now transform the polyhedron. We aren't entirely sure
1144 * whether we need to rotate through angle or -angle, and the
1145 * simplest way round this is to try both and see which one
1146 * aligns successfully!
1148 * Unfortunately, _both_ will align successfully if this is a
1149 * cube, which won't tell us anything much. So for that
1150 * particular case, I resort to gross hackery: I simply negate
1151 * the angle before trying the alignment, depending on the
1152 * direction. Which directions work which way is determined by
1153 * pure trial and error. I said it was gross :-/
1159 if (from
->solid
->order
== 4 && direction
== UP
)
1160 angle
= -angle
; /* HACK */
1162 poly
= transform_poly(from
->solid
,
1163 from
->squares
[from
->current
].flip
,
1164 pkey
[0], pkey
[1], angle
);
1165 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1166 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1170 poly
= transform_poly(from
->solid
,
1171 from
->squares
[from
->current
].flip
,
1172 pkey
[0], pkey
[1], angle
);
1173 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1174 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1181 * Now we have our rotated polyhedron, which we expect to be
1182 * exactly congruent to the one we started with - but with the
1183 * faces permuted. So we map that congruence and thereby figure
1184 * out how to permute the faces as a result of the polyhedron
1188 int *newcolours
= snewn(from
->solid
->nfaces
, int);
1190 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1193 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1197 * Now go through the transformed polyhedron's faces
1198 * and figure out which one's normal is approximately
1199 * equal to this one.
1201 for (j
= 0; j
< poly
->nfaces
; j
++) {
1207 for (k
= 0; k
< 3; k
++)
1208 dist
+= SQ(poly
->normals
[j
*3+k
] -
1209 from
->solid
->normals
[i
*3+k
]);
1211 if (APPROXEQ(dist
, 0)) {
1213 newcolours
[i
] = ret
->facecolours
[j
];
1217 assert(nmatch
== 1);
1220 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1221 assert(newcolours
[i
] != -1);
1223 sfree(ret
->facecolours
);
1224 ret
->facecolours
= newcolours
;
1230 * And finally, swap the colour between the bottom face of the
1231 * polyhedron and the face we've just landed on.
1233 * We don't do this if the game is already complete, since we
1234 * allow the user to roll the fully blue polyhedron around the
1235 * grid as a feeble reward.
1237 if (!ret
->completed
) {
1238 i
= lowest_face(from
->solid
);
1239 j
= ret
->facecolours
[i
];
1240 ret
->facecolours
[i
] = ret
->squares
[ret
->current
].blue
;
1241 ret
->squares
[ret
->current
].blue
= j
;
1244 * Detect game completion.
1247 for (i
= 0; i
< ret
->solid
->nfaces
; i
++)
1248 if (ret
->facecolours
[i
])
1250 if (j
== ret
->solid
->nfaces
)
1251 ret
->completed
= ret
->movecount
;
1257 * Align the normal polyhedron with its grid square, to get key
1258 * points for non-animated display.
1264 success
= align_poly(ret
->solid
, &ret
->squares
[ret
->current
], pkey
);
1267 ret
->dpkey
[0] = pkey
[0];
1268 ret
->dpkey
[1] = pkey
[1];
1274 ret
->spkey
[0] = pkey
[0];
1275 ret
->spkey
[1] = pkey
[1];
1276 ret
->sgkey
[0] = skey
[0];
1277 ret
->sgkey
[1] = skey
[1];
1278 ret
->previous
= from
->current
;
1284 /* ----------------------------------------------------------------------
1292 struct game_drawstate
{
1293 int ox
, oy
; /* pixel position of float origin */
1296 static void find_bbox_callback(void *ctx
, struct grid_square
*sq
)
1298 struct bbox
*bb
= (struct bbox
*)ctx
;
1301 for (i
= 0; i
< sq
->npoints
; i
++) {
1302 if (bb
->l
> sq
->points
[i
*2]) bb
->l
= sq
->points
[i
*2];
1303 if (bb
->r
< sq
->points
[i
*2]) bb
->r
= sq
->points
[i
*2];
1304 if (bb
->u
> sq
->points
[i
*2+1]) bb
->u
= sq
->points
[i
*2+1];
1305 if (bb
->d
< sq
->points
[i
*2+1]) bb
->d
= sq
->points
[i
*2+1];
1309 static struct bbox
find_bbox(game_params
*params
)
1314 * These should be hugely more than the real bounding box will
1317 bb
.l
= 2.0F
* (params
->d1
+ params
->d2
);
1318 bb
.r
= -2.0F
* (params
->d1
+ params
->d2
);
1319 bb
.u
= 2.0F
* (params
->d1
+ params
->d2
);
1320 bb
.d
= -2.0F
* (params
->d1
+ params
->d2
);
1321 enum_grid_squares(params
, find_bbox_callback
, &bb
);
1326 static void game_size(game_params
*params
, int *x
, int *y
)
1328 struct bbox bb
= find_bbox(params
);
1329 *x
= (int)((bb
.r
- bb
.l
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
);
1330 *y
= (int)((bb
.d
- bb
.u
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
);
1333 static float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1335 float *ret
= snewn(3 * NCOLOURS
, float);
1337 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1339 ret
[COL_BORDER
* 3 + 0] = 0.0;
1340 ret
[COL_BORDER
* 3 + 1] = 0.0;
1341 ret
[COL_BORDER
* 3 + 2] = 0.0;
1343 ret
[COL_BLUE
* 3 + 0] = 0.0;
1344 ret
[COL_BLUE
* 3 + 1] = 0.0;
1345 ret
[COL_BLUE
* 3 + 2] = 1.0;
1347 *ncolours
= NCOLOURS
;
1351 static game_drawstate
*game_new_drawstate(game_state
*state
)
1353 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1354 struct bbox bb
= find_bbox(&state
->params
);
1356 ds
->ox
= (int)(-(bb
.l
- state
->solid
->border
) * GRID_SCALE
);
1357 ds
->oy
= (int)(-(bb
.u
- state
->solid
->border
) * GRID_SCALE
);
1362 static void game_free_drawstate(game_drawstate
*ds
)
1367 static void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1368 game_state
*state
, int dir
, game_ui
*ui
,
1369 float animtime
, float flashtime
)
1372 struct bbox bb
= find_bbox(&state
->params
);
1377 game_state
*newstate
;
1380 draw_rect(fe
, 0, 0, (int)((bb
.r
-bb
.l
+2.0F
) * GRID_SCALE
),
1381 (int)((bb
.d
-bb
.u
+2.0F
) * GRID_SCALE
), COL_BACKGROUND
);
1387 * This is an Undo. So reverse the order of the states, and
1388 * run the roll timer backwards.
1396 animtime
= ROLLTIME
- animtime
;
1402 square
= state
->current
;
1403 pkey
= state
->dpkey
;
1404 gkey
= state
->dgkey
;
1406 angle
= state
->angle
* animtime
/ ROLLTIME
;
1407 square
= state
->previous
;
1408 pkey
= state
->spkey
;
1409 gkey
= state
->sgkey
;
1414 for (i
= 0; i
< state
->nsquares
; i
++) {
1417 for (j
= 0; j
< state
->squares
[i
].npoints
; j
++) {
1418 coords
[2*j
] = ((int)(state
->squares
[i
].points
[2*j
] * GRID_SCALE
)
1420 coords
[2*j
+1] = ((int)(state
->squares
[i
].points
[2*j
+1]*GRID_SCALE
)
1424 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, TRUE
,
1425 state
->squares
[i
].blue ? COL_BLUE
: COL_BACKGROUND
);
1426 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, FALSE
, COL_BORDER
);
1430 * Now compute and draw the polyhedron.
1432 poly
= transform_poly(state
->solid
, state
->squares
[square
].flip
,
1433 pkey
[0], pkey
[1], angle
);
1436 * Compute the translation required to align the two key points
1437 * on the polyhedron with the same key points on the current
1440 for (i
= 0; i
< 3; i
++) {
1443 for (j
= 0; j
< 2; j
++) {
1448 state
->squares
[square
].points
[gkey
[j
]*2+i
];
1453 tc
+= (grid_coord
- poly
->vertices
[pkey
[j
]*3+i
]);
1458 for (i
= 0; i
< poly
->nvertices
; i
++)
1459 for (j
= 0; j
< 3; j
++)
1460 poly
->vertices
[i
*3+j
] += t
[j
];
1463 * Now actually draw each face.
1465 for (i
= 0; i
< poly
->nfaces
; i
++) {
1469 for (j
= 0; j
< poly
->order
; j
++) {
1470 int f
= poly
->faces
[i
*poly
->order
+ j
];
1471 points
[j
*2] = (poly
->vertices
[f
*3+0] -
1472 poly
->vertices
[f
*3+2] * poly
->shear
);
1473 points
[j
*2+1] = (poly
->vertices
[f
*3+1] -
1474 poly
->vertices
[f
*3+2] * poly
->shear
);
1477 for (j
= 0; j
< poly
->order
; j
++) {
1478 coords
[j
*2] = (int)floor(points
[j
*2] * GRID_SCALE
) + ds
->ox
;
1479 coords
[j
*2+1] = (int)floor(points
[j
*2+1] * GRID_SCALE
) + ds
->oy
;
1483 * Find out whether these points are in a clockwise or
1484 * anticlockwise arrangement. If the latter, discard the
1485 * face because it's facing away from the viewer.
1487 * This would involve fiddly winding-number stuff for a
1488 * general polygon, but for the simple parallelograms we'll
1489 * be seeing here, all we have to do is check whether the
1490 * corners turn right or left. So we'll take the vector
1491 * from point 0 to point 1, turn it right 90 degrees,
1492 * and check the sign of the dot product with that and the
1493 * next vector (point 1 to point 2).
1496 float v1x
= points
[2]-points
[0];
1497 float v1y
= points
[3]-points
[1];
1498 float v2x
= points
[4]-points
[2];
1499 float v2y
= points
[5]-points
[3];
1500 float dp
= v1x
* v2y
- v1y
* v2x
;
1506 draw_polygon(fe
, coords
, poly
->order
, TRUE
,
1507 state
->facecolours
[i
] ? COL_BLUE
: COL_BACKGROUND
);
1508 draw_polygon(fe
, coords
, poly
->order
, FALSE
, COL_BORDER
);
1512 draw_update(fe
, 0, 0, (int)((bb
.r
-bb
.l
+2.0F
) * GRID_SCALE
),
1513 (int)((bb
.d
-bb
.u
+2.0F
) * GRID_SCALE
));
1516 * Update the status bar.
1519 char statusbuf
[256];
1521 sprintf(statusbuf
, "%sMoves: %d",
1522 (state
->completed ?
"COMPLETED! " : ""),
1523 (state
->completed ? state
->completed
: state
->movecount
));
1525 status_bar(fe
, statusbuf
);
1529 static float game_anim_length(game_state
*oldstate
,
1530 game_state
*newstate
, int dir
)
1535 static float game_flash_length(game_state
*oldstate
,
1536 game_state
*newstate
, int dir
)
1541 static int game_wants_statusbar(void)
1547 #define thegame cube
1550 const struct game thegame
= {
1551 "Cube", "games.cube",
1558 TRUE
, game_configure
, custom_params
,
1567 FALSE
, game_text_format
,
1574 game_free_drawstate
,
1578 game_wants_statusbar
,