13 #define MAXVERTICES 20
18 float vertices
[MAXVERTICES
* 3]; /* 3*npoints coordinates */
21 int faces
[MAXFACES
* MAXORDER
]; /* order*nfaces point indices */
22 float normals
[MAXFACES
* 3]; /* 3*npoints vector components */
23 float shear
; /* isometric shear for nice drawing */
24 float border
; /* border required around arena */
27 static const struct solid tetrahedron
= {
30 0.0, -0.57735026919, -0.20412414523,
31 -0.5, 0.28867513459, -0.20412414523,
32 0.0, -0.0, 0.6123724357,
33 0.5, 0.28867513459, -0.20412414523,
37 0,2,1, 3,1,2, 2,0,3, 1,3,0
40 -0.816496580928, -0.471404520791, 0.333333333334,
41 0.0, 0.942809041583, 0.333333333333,
42 0.816496580928, -0.471404520791, 0.333333333334,
48 static const struct solid cube
= {
51 -0.5,-0.5,-0.5, -0.5,-0.5,+0.5, -0.5,+0.5,-0.5, -0.5,+0.5,+0.5,
52 +0.5,-0.5,-0.5, +0.5,-0.5,+0.5, +0.5,+0.5,-0.5, +0.5,+0.5,+0.5,
56 0,1,3,2, 1,5,7,3, 5,4,6,7, 4,0,2,6, 0,4,5,1, 3,7,6,2
59 -1,0,0, 0,0,+1, +1,0,0, 0,0,-1, 0,-1,0, 0,+1,0
64 static const struct solid octahedron
= {
67 -0.5, -0.28867513459472505, 0.4082482904638664,
68 0.5, 0.28867513459472505, -0.4082482904638664,
69 -0.5, 0.28867513459472505, -0.4082482904638664,
70 0.5, -0.28867513459472505, 0.4082482904638664,
71 0.0, -0.57735026918945009, -0.4082482904638664,
72 0.0, 0.57735026918945009, 0.4082482904638664,
76 4,0,2, 0,5,2, 0,4,3, 5,0,3, 1,4,2, 5,1,2, 4,1,3, 1,5,3
79 -0.816496580928, -0.471404520791, -0.333333333334,
80 -0.816496580928, 0.471404520791, 0.333333333334,
81 0.0, -0.942809041583, 0.333333333333,
84 0.0, 0.942809041583, -0.333333333333,
85 0.816496580928, -0.471404520791, -0.333333333334,
86 0.816496580928, 0.471404520791, 0.333333333334,
91 static const struct solid icosahedron
= {
94 0.0, 0.57735026919, 0.75576131408,
95 0.0, -0.93417235896, 0.17841104489,
96 0.0, 0.93417235896, -0.17841104489,
97 0.0, -0.57735026919, -0.75576131408,
98 -0.5, -0.28867513459, 0.75576131408,
99 -0.5, 0.28867513459, -0.75576131408,
100 0.5, -0.28867513459, 0.75576131408,
101 0.5, 0.28867513459, -0.75576131408,
102 -0.80901699437, 0.46708617948, 0.17841104489,
103 0.80901699437, 0.46708617948, 0.17841104489,
104 -0.80901699437, -0.46708617948, -0.17841104489,
105 0.80901699437, -0.46708617948, -0.17841104489,
109 8,0,2, 0,9,2, 1,10,3, 11,1,3, 0,4,6,
110 4,1,6, 5,2,7, 3,5,7, 4,8,10, 8,5,10,
111 9,6,11, 7,9,11, 0,8,4, 9,0,6, 10,1,4,
112 1,11,6, 8,2,5, 2,9,7, 3,10,5, 11,3,7,
115 -0.356822089773, 0.87267799625, 0.333333333333,
116 0.356822089773, 0.87267799625, 0.333333333333,
117 -0.356822089773, -0.87267799625, -0.333333333333,
118 0.356822089773, -0.87267799625, -0.333333333333,
120 0.0, -0.666666666667, 0.745355992501,
121 0.0, 0.666666666667, -0.745355992501,
123 -0.934172358963, -0.12732200375, 0.333333333333,
124 -0.934172358963, 0.12732200375, -0.333333333333,
125 0.934172358963, -0.12732200375, 0.333333333333,
126 0.934172358963, 0.12732200375, -0.333333333333,
127 -0.57735026919, 0.333333333334, 0.745355992501,
128 0.57735026919, 0.333333333334, 0.745355992501,
129 -0.57735026919, -0.745355992501, 0.333333333334,
130 0.57735026919, -0.745355992501, 0.333333333334,
131 -0.57735026919, 0.745355992501, -0.333333333334,
132 0.57735026919, 0.745355992501, -0.333333333334,
133 -0.57735026919, -0.333333333334, -0.745355992501,
134 0.57735026919, -0.333333333334, -0.745355992501,
140 TETRAHEDRON
, CUBE
, OCTAHEDRON
, ICOSAHEDRON
142 static const struct solid
*solids
[] = {
143 &tetrahedron
, &cube
, &octahedron
, &icosahedron
153 enum { LEFT
, RIGHT
, UP
, DOWN
};
155 #define GRID_SCALE 48
158 #define SQ(x) ( (x) * (x) )
160 #define MATMUL(ra,m,a) do { \
161 float rx, ry, rz, xx = (a)[0], yy = (a)[1], zz = (a)[2], *mat = (m); \
162 rx = mat[0] * xx + mat[3] * yy + mat[6] * zz; \
163 ry = mat[1] * xx + mat[4] * yy + mat[7] * zz; \
164 rz = mat[2] * xx + mat[5] * yy + mat[8] * zz; \
165 (ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \
168 #define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
173 float points
[8]; /* maximum */
174 int directions
[4]; /* bit masks showing point pairs */
183 * Grid dimensions. For a square grid these are width and
184 * height respectively; otherwise the grid is a hexagon, with
185 * the top side and the two lower diagonals having length d1
186 * and the remaining three sides having length d2 (so that
187 * d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
193 struct game_params params
;
194 const struct solid
*solid
;
196 struct grid_square
*squares
;
198 int current
; /* index of current grid square */
199 int sgkey
[2]; /* key-point indices into grid sq */
200 int dgkey
[2]; /* key-point indices into grid sq */
201 int spkey
[2]; /* key-point indices into polyhedron */
202 int dpkey
[2]; /* key-point indices into polyhedron */
209 game_params
*default_params(void)
211 game_params
*ret
= snew(game_params
);
220 int game_fetch_preset(int i
, char **name
, game_params
**params
)
222 game_params
*ret
= snew(game_params
);
234 ret
->solid
= TETRAHEDRON
;
240 ret
->solid
= OCTAHEDRON
;
246 ret
->solid
= ICOSAHEDRON
;
260 void free_params(game_params
*params
)
265 game_params
*dup_params(game_params
*params
)
267 game_params
*ret
= snew(game_params
);
268 *ret
= *params
; /* structure copy */
272 static void enum_grid_squares(game_params
*params
,
273 void (*callback
)(void *, struct grid_square
*),
276 const struct solid
*solid
= solids
[params
->solid
];
278 if (solid
->order
== 4) {
281 for (x
= 0; x
< params
->d1
; x
++)
282 for (y
= 0; y
< params
->d2
; y
++) {
283 struct grid_square sq
;
287 sq
.points
[0] = x
- 0.5;
288 sq
.points
[1] = y
- 0.5;
289 sq
.points
[2] = x
- 0.5;
290 sq
.points
[3] = y
+ 0.5;
291 sq
.points
[4] = x
+ 0.5;
292 sq
.points
[5] = y
+ 0.5;
293 sq
.points
[6] = x
+ 0.5;
294 sq
.points
[7] = y
- 0.5;
297 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
298 sq
.directions
[RIGHT
] = 0x0C; /* 2,3 */
299 sq
.directions
[UP
] = 0x09; /* 0,3 */
300 sq
.directions
[DOWN
] = 0x06; /* 1,2 */
305 * This is supremely irrelevant, but just to avoid
306 * having any uninitialised structure members...
313 int row
, rowlen
, other
, i
, firstix
= -1;
314 float theight
= sqrt(3) / 2.0;
316 for (row
= 0; row
< params
->d1
+ params
->d2
; row
++) {
317 if (row
< params
->d1
) {
319 rowlen
= row
+ params
->d2
;
322 rowlen
= 2*params
->d1
+ params
->d2
- row
;
326 * There are `rowlen' down-pointing triangles.
328 for (i
= 0; i
< rowlen
; i
++) {
329 struct grid_square sq
;
333 ix
= (2 * i
- (rowlen
-1));
337 sq
.y
= y
+ theight
/ 3;
338 sq
.points
[0] = x
- 0.5;
341 sq
.points
[3] = y
+ theight
;
342 sq
.points
[4] = x
+ 0.5;
346 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
347 sq
.directions
[RIGHT
] = 0x06; /* 1,2 */
348 sq
.directions
[UP
] = 0x05; /* 0,2 */
349 sq
.directions
[DOWN
] = 0; /* invalid move */
356 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
362 * There are `rowlen+other' up-pointing triangles.
364 for (i
= 0; i
< rowlen
+other
; i
++) {
365 struct grid_square sq
;
369 ix
= (2 * i
- (rowlen
+other
-1));
373 sq
.y
= y
+ 2*theight
/ 3;
374 sq
.points
[0] = x
+ 0.5;
375 sq
.points
[1] = y
+ theight
;
378 sq
.points
[4] = x
- 0.5;
379 sq
.points
[5] = y
+ theight
;
382 sq
.directions
[LEFT
] = 0x06; /* 1,2 */
383 sq
.directions
[RIGHT
] = 0x03; /* 0,1 */
384 sq
.directions
[DOWN
] = 0x05; /* 0,2 */
385 sq
.directions
[UP
] = 0; /* invalid move */
392 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
400 static int grid_area(int d1
, int d2
, int order
)
403 * An NxM grid of squares has NM squares in it.
405 * A grid of triangles with dimensions A and B has a total of
406 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
407 * a side-A triangle containing A^2 subtriangles, a side-B
408 * triangle containing B^2, and two congruent parallelograms,
409 * each with side lengths A and B, each therefore containing AB
410 * two-triangle rhombuses.)
415 return d1
*d1
+ d2
*d2
+ 4*d1
*d2
;
425 static void classify_grid_square_callback(void *ctx
, struct grid_square
*sq
)
427 struct grid_data
*data
= (struct grid_data
*)ctx
;
430 if (data
->nclasses
== 4)
431 thisclass
= sq
->tetra_class
;
432 else if (data
->nclasses
== 2)
433 thisclass
= sq
->flip
;
437 data
->gridptrs
[thisclass
][data
->nsquares
[thisclass
]++] =
441 char *new_game_seed(game_params
*params
)
443 struct grid_data data
;
444 int i
, j
, k
, m
, area
, facesperclass
;
449 * Enumerate the grid squares, dividing them into equivalence
450 * classes as appropriate. (For the tetrahedron, there is one
451 * equivalence class for each face; for the octahedron there
452 * are two classes; for the other two solids there's only one.)
455 area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
456 if (params
->solid
== TETRAHEDRON
)
458 else if (params
->solid
== OCTAHEDRON
)
462 data
.gridptrs
[0] = snewn(data
.nclasses
* area
, int);
463 for (i
= 0; i
< data
.nclasses
; i
++) {
464 data
.gridptrs
[i
] = data
.gridptrs
[0] + i
* area
;
465 data
.nsquares
[i
] = 0;
467 data
.squareindex
= 0;
468 enum_grid_squares(params
, classify_grid_square_callback
, &data
);
470 facesperclass
= solids
[params
->solid
]->nfaces
/ data
.nclasses
;
472 for (i
= 0; i
< data
.nclasses
; i
++)
473 assert(data
.nsquares
[i
] >= facesperclass
);
474 assert(data
.squareindex
== area
);
477 * So now we know how many faces to allocate in each class. Get
480 flags
= snewn(area
, int);
481 for (i
= 0; i
< area
; i
++)
484 for (i
= 0; i
< data
.nclasses
; i
++) {
485 for (j
= 0; j
< facesperclass
; j
++) {
486 unsigned long divisor
= RAND_MAX
/ data
.nsquares
[i
];
487 unsigned long max
= divisor
* data
.nsquares
[i
];
496 assert(!flags
[data
.gridptrs
[i
][n
]]);
497 flags
[data
.gridptrs
[i
][n
]] = TRUE
;
500 * Move everything else up the array. I ought to use a
501 * better data structure for this, but for such small
502 * numbers it hardly seems worth the effort.
504 while (n
< data
.nsquares
[i
]-1) {
505 data
.gridptrs
[i
][n
] = data
.gridptrs
[i
][n
+1];
513 * Now we know precisely which squares are blue. Encode this
514 * information in hex. While we're looping over this, collect
515 * the non-blue squares into a list in the now-unused gridptrs
518 seed
= snewn(area
/ 4 + 40, char);
523 for (i
= 0; i
< area
; i
++) {
527 data
.gridptrs
[0][m
++] = i
;
531 *p
++ = "0123456789ABCDEF"[j
];
537 *p
++ = "0123456789ABCDEF"[j
];
540 * Choose a non-blue square for the polyhedron.
543 unsigned long divisor
= RAND_MAX
/ m
;
544 unsigned long max
= divisor
* m
;
553 sprintf(p
, ":%d", data
.gridptrs
[0][n
]);
556 sfree(data
.gridptrs
[0]);
562 static void add_grid_square_callback(void *ctx
, struct grid_square
*sq
)
564 game_state
*state
= (game_state
*)ctx
;
566 state
->squares
[state
->nsquares
] = *sq
; /* structure copy */
567 state
->squares
[state
->nsquares
].blue
= FALSE
;
571 static int lowest_face(const struct solid
*solid
)
578 for (i
= 0; i
< solid
->nfaces
; i
++) {
581 for (j
= 0; j
< solid
->order
; j
++) {
582 int f
= solid
->faces
[i
*solid
->order
+ j
];
583 z
+= solid
->vertices
[f
*3+2];
586 if (i
== 0 || zmin
> z
) {
595 static int align_poly(const struct solid
*solid
, struct grid_square
*sq
,
600 int flip
= (sq
->flip ?
-1 : +1);
603 * First, find the lowest z-coordinate present in the solid.
606 for (i
= 0; i
< solid
->nvertices
; i
++)
607 if (zmin
> solid
->vertices
[i
*3+2])
608 zmin
= solid
->vertices
[i
*3+2];
611 * Now go round the grid square. For each point in the grid
612 * square, we're looking for a point of the polyhedron with the
613 * same x- and y-coordinates (relative to the square's centre),
614 * and z-coordinate equal to zmin (near enough).
616 for (j
= 0; j
< sq
->npoints
; j
++) {
622 for (i
= 0; i
< solid
->nvertices
; i
++) {
625 dist
+= SQ(solid
->vertices
[i
*3+0] * flip
- sq
->points
[j
*2+0] + sq
->x
);
626 dist
+= SQ(solid
->vertices
[i
*3+1] * flip
- sq
->points
[j
*2+1] + sq
->y
);
627 dist
+= SQ(solid
->vertices
[i
*3+2] - zmin
);
635 if (matches
!= 1 || index
< 0)
643 static void flip_poly(struct solid
*solid
, int flip
)
648 for (i
= 0; i
< solid
->nvertices
; i
++) {
649 solid
->vertices
[i
*3+0] *= -1;
650 solid
->vertices
[i
*3+1] *= -1;
652 for (i
= 0; i
< solid
->nfaces
; i
++) {
653 solid
->normals
[i
*3+0] *= -1;
654 solid
->normals
[i
*3+1] *= -1;
659 static struct solid
*transform_poly(const struct solid
*solid
, int flip
,
660 int key0
, int key1
, float angle
)
662 struct solid
*ret
= snew(struct solid
);
663 float vx
, vy
, ax
, ay
;
664 float vmatrix
[9], amatrix
[9], vmatrix2
[9];
667 *ret
= *solid
; /* structure copy */
669 flip_poly(ret
, flip
);
672 * Now rotate the polyhedron through the given angle. We must
673 * rotate about the Z-axis to bring the two vertices key0 and
674 * key1 into horizontal alignment, then rotate about the
675 * X-axis, then rotate back again.
677 vx
= ret
->vertices
[key1
*3+0] - ret
->vertices
[key0
*3+0];
678 vy
= ret
->vertices
[key1
*3+1] - ret
->vertices
[key0
*3+1];
679 assert(APPROXEQ(vx
*vx
+ vy
*vy
, 1.0));
681 vmatrix
[0] = vx
; vmatrix
[3] = vy
; vmatrix
[6] = 0;
682 vmatrix
[1] = -vy
; vmatrix
[4] = vx
; vmatrix
[7] = 0;
683 vmatrix
[2] = 0; vmatrix
[5] = 0; vmatrix
[8] = 1;
688 amatrix
[0] = 1; amatrix
[3] = 0; amatrix
[6] = 0;
689 amatrix
[1] = 0; amatrix
[4] = ax
; amatrix
[7] = ay
;
690 amatrix
[2] = 0; amatrix
[5] = -ay
; amatrix
[8] = ax
;
692 memcpy(vmatrix2
, vmatrix
, sizeof(vmatrix
));
696 for (i
= 0; i
< ret
->nvertices
; i
++) {
697 MATMUL(ret
->vertices
+ 3*i
, vmatrix
, ret
->vertices
+ 3*i
);
698 MATMUL(ret
->vertices
+ 3*i
, amatrix
, ret
->vertices
+ 3*i
);
699 MATMUL(ret
->vertices
+ 3*i
, vmatrix2
, ret
->vertices
+ 3*i
);
701 for (i
= 0; i
< ret
->nfaces
; i
++) {
702 MATMUL(ret
->normals
+ 3*i
, vmatrix
, ret
->normals
+ 3*i
);
703 MATMUL(ret
->normals
+ 3*i
, amatrix
, ret
->normals
+ 3*i
);
704 MATMUL(ret
->normals
+ 3*i
, vmatrix2
, ret
->normals
+ 3*i
);
710 game_state
*new_game(game_params
*params
, char *seed
)
712 game_state
*state
= snew(game_state
);
715 state
->params
= *params
; /* structure copy */
716 state
->solid
= solids
[params
->solid
];
718 area
= grid_area(params
->d1
, params
->d2
, state
->solid
->order
);
719 state
->squares
= snewn(area
, struct grid_square
);
721 enum_grid_squares(params
, add_grid_square_callback
, state
);
722 assert(state
->nsquares
== area
);
724 state
->facecolours
= snewn(state
->solid
->nfaces
, int);
725 memset(state
->facecolours
, 0, state
->solid
->nfaces
* sizeof(int));
728 * Set up the blue squares and polyhedron position according to
737 for (i
= 0; i
< state
->nsquares
; i
++) {
740 if (v
>= '0' && v
<= '9')
742 else if (v
>= 'A' && v
<= 'F')
744 else if (v
>= 'a' && v
<= 'f')
750 state
->squares
[i
].blue
= TRUE
;
759 state
->current
= atoi(p
);
760 if (state
->current
< 0 || state
->current
>= state
->nsquares
)
761 state
->current
= 0; /* got to do _something_ */
765 * Align the polyhedron with its grid square and determine
766 * initial key points.
772 ret
= align_poly(state
->solid
, &state
->squares
[state
->current
], pkey
);
775 state
->dpkey
[0] = state
->spkey
[0] = pkey
[0];
776 state
->dpkey
[1] = state
->spkey
[0] = pkey
[1];
777 state
->dgkey
[0] = state
->sgkey
[0] = 0;
778 state
->dgkey
[1] = state
->sgkey
[0] = 1;
781 state
->previous
= state
->current
;
783 state
->completed
= FALSE
;
784 state
->movecount
= 0;
789 game_state
*dup_game(game_state
*state
)
791 game_state
*ret
= snew(game_state
);
793 ret
->params
= state
->params
; /* structure copy */
794 ret
->solid
= state
->solid
;
795 ret
->facecolours
= snewn(ret
->solid
->nfaces
, int);
796 memcpy(ret
->facecolours
, state
->facecolours
,
797 ret
->solid
->nfaces
* sizeof(int));
798 ret
->nsquares
= state
->nsquares
;
799 ret
->squares
= snewn(ret
->nsquares
, struct grid_square
);
800 memcpy(ret
->squares
, state
->squares
,
801 ret
->nsquares
* sizeof(struct grid_square
));
802 ret
->dpkey
[0] = state
->dpkey
[0];
803 ret
->dpkey
[1] = state
->dpkey
[1];
804 ret
->dgkey
[0] = state
->dgkey
[0];
805 ret
->dgkey
[1] = state
->dgkey
[1];
806 ret
->spkey
[0] = state
->spkey
[0];
807 ret
->spkey
[1] = state
->spkey
[1];
808 ret
->sgkey
[0] = state
->sgkey
[0];
809 ret
->sgkey
[1] = state
->sgkey
[1];
810 ret
->previous
= state
->previous
;
811 ret
->angle
= state
->angle
;
812 ret
->completed
= state
->completed
;
813 ret
->movecount
= state
->movecount
;
818 void free_game(game_state
*state
)
823 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
826 int pkey
[2], skey
[2], dkey
[2];
830 int i
, j
, dest
, mask
;
834 * All moves are made with the cursor keys.
836 if (button
== CURSOR_UP
)
838 else if (button
== CURSOR_DOWN
)
840 else if (button
== CURSOR_LEFT
)
842 else if (button
== CURSOR_RIGHT
)
848 * Find the two points in the current grid square which
849 * correspond to this move.
851 mask
= from
->squares
[from
->current
].directions
[direction
];
854 for (i
= j
= 0; i
< from
->squares
[from
->current
].npoints
; i
++)
855 if (mask
& (1 << i
)) {
856 points
[j
*2] = from
->squares
[from
->current
].points
[i
*2];
857 points
[j
*2+1] = from
->squares
[from
->current
].points
[i
*2+1];
864 * Now find the other grid square which shares those points.
865 * This is our move destination.
868 for (i
= 0; i
< from
->nsquares
; i
++)
869 if (i
!= from
->current
) {
873 for (j
= 0; j
< from
->squares
[i
].npoints
; j
++) {
874 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[0]) +
875 SQ(from
->squares
[i
].points
[j
*2+1] - points
[1]));
878 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[2]) +
879 SQ(from
->squares
[i
].points
[j
*2+1] - points
[3]));
893 ret
= dup_game(from
);
897 * So we know what grid square we're aiming for, and we also
898 * know the two key points (as indices in both the source and
899 * destination grid squares) which are invariant between source
902 * Next we must roll the polyhedron on to that square. So we
903 * find the indices of the key points within the polyhedron's
904 * vertex array, then use those in a call to transform_poly,
905 * and align the result on the new grid square.
909 align_poly(from
->solid
, &from
->squares
[from
->current
], all_pkey
);
910 pkey
[0] = all_pkey
[skey
[0]];
911 pkey
[1] = all_pkey
[skey
[1]];
913 * Now pkey[0] corresponds to skey[0] and dkey[0], and
919 * Now find the angle through which to rotate the polyhedron.
920 * Do this by finding the two faces that share the two vertices
921 * we've found, and taking the dot product of their normals.
927 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
929 for (j
= 0; j
< from
->solid
->order
; j
++)
930 if (from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[0] ||
931 from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[1])
942 for (i
= 0; i
< 3; i
++)
943 dp
+= (from
->solid
->normals
[f
[0]*3+i
] *
944 from
->solid
->normals
[f
[1]*3+i
]);
949 * Now transform the polyhedron. We aren't entirely sure
950 * whether we need to rotate through angle or -angle, and the
951 * simplest way round this is to try both and see which one
952 * aligns successfully!
954 * Unfortunately, _both_ will align successfully if this is a
955 * cube, which won't tell us anything much. So for that
956 * particular case, I resort to gross hackery: I simply negate
957 * the angle before trying the alignment, depending on the
958 * direction. Which directions work which way is determined by
959 * pure trial and error. I said it was gross :-/
965 if (from
->solid
->order
== 4 && direction
== UP
)
966 angle
= -angle
; /* HACK */
968 poly
= transform_poly(from
->solid
,
969 from
->squares
[from
->current
].flip
,
970 pkey
[0], pkey
[1], angle
);
971 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
972 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
976 poly
= transform_poly(from
->solid
,
977 from
->squares
[from
->current
].flip
,
978 pkey
[0], pkey
[1], angle
);
979 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
980 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
987 * Now we have our rotated polyhedron, which we expect to be
988 * exactly congruent to the one we started with - but with the
989 * faces permuted. So we map that congruence and thereby figure
990 * out how to permute the faces as a result of the polyhedron
994 int *newcolours
= snewn(from
->solid
->nfaces
, int);
996 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
999 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1003 * Now go through the transformed polyhedron's faces
1004 * and figure out which one's normal is approximately
1005 * equal to this one.
1007 for (j
= 0; j
< poly
->nfaces
; j
++) {
1013 for (k
= 0; k
< 3; k
++)
1014 dist
+= SQ(poly
->normals
[j
*3+k
] -
1015 from
->solid
->normals
[i
*3+k
]);
1017 if (APPROXEQ(dist
, 0)) {
1019 newcolours
[i
] = ret
->facecolours
[j
];
1023 assert(nmatch
== 1);
1026 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1027 assert(newcolours
[i
] != -1);
1029 sfree(ret
->facecolours
);
1030 ret
->facecolours
= newcolours
;
1034 * And finally, swap the colour between the bottom face of the
1035 * polyhedron and the face we've just landed on.
1037 * We don't do this if the game is already complete, since we
1038 * allow the user to roll the fully blue polyhedron around the
1039 * grid as a feeble reward.
1041 if (!ret
->completed
) {
1042 i
= lowest_face(from
->solid
);
1043 j
= ret
->facecolours
[i
];
1044 ret
->facecolours
[i
] = ret
->squares
[ret
->current
].blue
;
1045 ret
->squares
[ret
->current
].blue
= j
;
1048 * Detect game completion.
1051 for (i
= 0; i
< ret
->solid
->nfaces
; i
++)
1052 if (ret
->facecolours
[i
])
1054 if (j
== ret
->solid
->nfaces
)
1055 ret
->completed
= TRUE
;
1061 * Align the normal polyhedron with its grid square, to get key
1062 * points for non-animated display.
1068 success
= align_poly(ret
->solid
, &ret
->squares
[ret
->current
], pkey
);
1071 ret
->dpkey
[0] = pkey
[0];
1072 ret
->dpkey
[1] = pkey
[1];
1078 ret
->spkey
[0] = pkey
[0];
1079 ret
->spkey
[1] = pkey
[1];
1080 ret
->sgkey
[0] = skey
[0];
1081 ret
->sgkey
[1] = skey
[1];
1082 ret
->previous
= from
->current
;
1089 /* ----------------------------------------------------------------------
1097 struct game_drawstate
{
1098 int ox
, oy
; /* pixel position of float origin */
1101 static void find_bbox_callback(void *ctx
, struct grid_square
*sq
)
1103 struct bbox
*bb
= (struct bbox
*)ctx
;
1106 for (i
= 0; i
< sq
->npoints
; i
++) {
1107 if (bb
->l
> sq
->points
[i
*2]) bb
->l
= sq
->points
[i
*2];
1108 if (bb
->r
< sq
->points
[i
*2]) bb
->r
= sq
->points
[i
*2];
1109 if (bb
->u
> sq
->points
[i
*2+1]) bb
->u
= sq
->points
[i
*2+1];
1110 if (bb
->d
< sq
->points
[i
*2+1]) bb
->d
= sq
->points
[i
*2+1];
1114 static struct bbox
find_bbox(game_params
*params
)
1119 * These should be hugely more than the real bounding box will
1122 bb
.l
= 2 * (params
->d1
+ params
->d2
);
1123 bb
.r
= -2 * (params
->d1
+ params
->d2
);
1124 bb
.u
= 2 * (params
->d1
+ params
->d2
);
1125 bb
.d
= -2 * (params
->d1
+ params
->d2
);
1126 enum_grid_squares(params
, find_bbox_callback
, &bb
);
1131 void game_size(game_params
*params
, int *x
, int *y
)
1133 struct bbox bb
= find_bbox(params
);
1134 *x
= (bb
.r
- bb
.l
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
;
1135 *y
= (bb
.d
- bb
.u
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
;
1138 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1140 float *ret
= snewn(3 * NCOLOURS
, float);
1142 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1144 ret
[COL_BORDER
* 3 + 0] = 0.0;
1145 ret
[COL_BORDER
* 3 + 1] = 0.0;
1146 ret
[COL_BORDER
* 3 + 2] = 0.0;
1148 ret
[COL_BLUE
* 3 + 0] = 0.0;
1149 ret
[COL_BLUE
* 3 + 1] = 0.0;
1150 ret
[COL_BLUE
* 3 + 2] = 1.0;
1152 *ncolours
= NCOLOURS
;
1156 game_drawstate
*game_new_drawstate(game_state
*state
)
1158 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1159 struct bbox bb
= find_bbox(&state
->params
);
1161 ds
->ox
= -(bb
.l
- state
->solid
->border
) * GRID_SCALE
;
1162 ds
->oy
= -(bb
.u
- state
->solid
->border
) * GRID_SCALE
;
1167 void game_free_drawstate(game_drawstate
*ds
)
1172 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1173 game_state
*state
, float animtime
)
1176 struct bbox bb
= find_bbox(&state
->params
);
1181 game_state
*newstate
;
1184 draw_rect(fe
, 0, 0, (bb
.r
-bb
.l
+2) * GRID_SCALE
,
1185 (bb
.d
-bb
.u
+2) * GRID_SCALE
, COL_BACKGROUND
);
1187 if (oldstate
&& oldstate
->movecount
> state
->movecount
) {
1191 * This is an Undo. So reverse the order of the states, and
1192 * run the roll timer backwards.
1198 animtime
= ROLLTIME
- animtime
;
1204 square
= state
->current
;
1205 pkey
= state
->dpkey
;
1206 gkey
= state
->dgkey
;
1208 angle
= state
->angle
* animtime
/ ROLLTIME
;
1209 square
= state
->previous
;
1210 pkey
= state
->spkey
;
1211 gkey
= state
->sgkey
;
1216 for (i
= 0; i
< state
->nsquares
; i
++) {
1219 for (j
= 0; j
< state
->squares
[i
].npoints
; j
++) {
1220 coords
[2*j
] = state
->squares
[i
].points
[2*j
]
1221 * GRID_SCALE
+ ds
->ox
;
1222 coords
[2*j
+1] = state
->squares
[i
].points
[2*j
+1]
1223 * GRID_SCALE
+ ds
->oy
;
1226 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, TRUE
,
1227 state
->squares
[i
].blue ? COL_BLUE
: COL_BACKGROUND
);
1228 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, FALSE
, COL_BORDER
);
1232 * Now compute and draw the polyhedron.
1234 poly
= transform_poly(state
->solid
, state
->squares
[square
].flip
,
1235 pkey
[0], pkey
[1], angle
);
1238 * Compute the translation required to align the two key points
1239 * on the polyhedron with the same key points on the current
1242 for (i
= 0; i
< 3; i
++) {
1245 for (j
= 0; j
< 2; j
++) {
1250 state
->squares
[square
].points
[gkey
[j
]*2+i
];
1255 tc
+= (grid_coord
- poly
->vertices
[pkey
[j
]*3+i
]);
1260 for (i
= 0; i
< poly
->nvertices
; i
++)
1261 for (j
= 0; j
< 3; j
++)
1262 poly
->vertices
[i
*3+j
] += t
[j
];
1265 * Now actually draw each face.
1267 for (i
= 0; i
< poly
->nfaces
; i
++) {
1271 for (j
= 0; j
< poly
->order
; j
++) {
1272 int f
= poly
->faces
[i
*poly
->order
+ j
];
1273 points
[j
*2] = (poly
->vertices
[f
*3+0] -
1274 poly
->vertices
[f
*3+2] * poly
->shear
);
1275 points
[j
*2+1] = (poly
->vertices
[f
*3+1] -
1276 poly
->vertices
[f
*3+2] * poly
->shear
);
1279 for (j
= 0; j
< poly
->order
; j
++) {
1280 coords
[j
*2] = points
[j
*2] * GRID_SCALE
+ ds
->ox
;
1281 coords
[j
*2+1] = points
[j
*2+1] * GRID_SCALE
+ ds
->oy
;
1285 * Find out whether these points are in a clockwise or
1286 * anticlockwise arrangement. If the latter, discard the
1287 * face because it's facing away from the viewer.
1289 * This would involve fiddly winding-number stuff for a
1290 * general polygon, but for the simple parallelograms we'll
1291 * be seeing here, all we have to do is check whether the
1292 * corners turn right or left. So we'll take the vector
1293 * from point 0 to point 1, turn it right 90 degrees,
1294 * and check the sign of the dot product with that and the
1295 * next vector (point 1 to point 2).
1298 float v1x
= points
[2]-points
[0];
1299 float v1y
= points
[3]-points
[1];
1300 float v2x
= points
[4]-points
[2];
1301 float v2y
= points
[5]-points
[3];
1302 float dp
= v1x
* v2y
- v1y
* v2x
;
1308 draw_polygon(fe
, coords
, poly
->order
, TRUE
,
1309 state
->facecolours
[i
] ? COL_BLUE
: COL_BACKGROUND
);
1310 draw_polygon(fe
, coords
, poly
->order
, FALSE
, COL_BORDER
);
1314 draw_update(fe
, 0, 0, (bb
.r
-bb
.l
+2) * GRID_SCALE
,
1315 (bb
.d
-bb
.u
+2) * GRID_SCALE
);
1318 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)