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
, UP_LEFT
, UP_RIGHT
, DOWN_LEFT
, DOWN_RIGHT
};
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
[8]; /* 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 */
301 sq
.directions
[UP_LEFT
] = 0; /* no diagonals in a square */
302 sq
.directions
[UP_RIGHT
] = 0; /* no diagonals in a square */
303 sq
.directions
[DOWN_LEFT
] = 0; /* no diagonals in a square */
304 sq
.directions
[DOWN_RIGHT
] = 0; /* no diagonals in a square */
309 * This is supremely irrelevant, but just to avoid
310 * having any uninitialised structure members...
317 int row
, rowlen
, other
, i
, firstix
= -1;
318 float theight
= sqrt(3) / 2.0;
320 for (row
= 0; row
< params
->d1
+ params
->d2
; row
++) {
321 if (row
< params
->d1
) {
323 rowlen
= row
+ params
->d2
;
326 rowlen
= 2*params
->d1
+ params
->d2
- row
;
330 * There are `rowlen' down-pointing triangles.
332 for (i
= 0; i
< rowlen
; i
++) {
333 struct grid_square sq
;
337 ix
= (2 * i
- (rowlen
-1));
341 sq
.y
= y
+ theight
/ 3;
342 sq
.points
[0] = x
- 0.5;
345 sq
.points
[3] = y
+ theight
;
346 sq
.points
[4] = x
+ 0.5;
350 sq
.directions
[LEFT
] = 0x03; /* 0,1 */
351 sq
.directions
[RIGHT
] = 0x06; /* 1,2 */
352 sq
.directions
[UP
] = 0x05; /* 0,2 */
353 sq
.directions
[DOWN
] = 0; /* invalid move */
356 * Down-pointing triangle: both the up diagonals go
357 * up, and the down ones go left and right.
359 sq
.directions
[UP_LEFT
] = sq
.directions
[UP_RIGHT
] =
361 sq
.directions
[DOWN_LEFT
] = sq
.directions
[LEFT
];
362 sq
.directions
[DOWN_RIGHT
] = sq
.directions
[RIGHT
];
369 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
375 * There are `rowlen+other' up-pointing triangles.
377 for (i
= 0; i
< rowlen
+other
; i
++) {
378 struct grid_square sq
;
382 ix
= (2 * i
- (rowlen
+other
-1));
386 sq
.y
= y
+ 2*theight
/ 3;
387 sq
.points
[0] = x
+ 0.5;
388 sq
.points
[1] = y
+ theight
;
391 sq
.points
[4] = x
- 0.5;
392 sq
.points
[5] = y
+ theight
;
395 sq
.directions
[LEFT
] = 0x06; /* 1,2 */
396 sq
.directions
[RIGHT
] = 0x03; /* 0,1 */
397 sq
.directions
[DOWN
] = 0x05; /* 0,2 */
398 sq
.directions
[UP
] = 0; /* invalid move */
401 * Up-pointing triangle: both the down diagonals go
402 * down, and the up ones go left and right.
404 sq
.directions
[DOWN_LEFT
] = sq
.directions
[DOWN_RIGHT
] =
406 sq
.directions
[UP_LEFT
] = sq
.directions
[LEFT
];
407 sq
.directions
[UP_RIGHT
] = sq
.directions
[RIGHT
];
414 sq
.tetra_class
= ((row
+(ix
&1)) & 2) ^ (ix
& 3);
422 static int grid_area(int d1
, int d2
, int order
)
425 * An NxM grid of squares has NM squares in it.
427 * A grid of triangles with dimensions A and B has a total of
428 * A^2 + B^2 + 4AB triangles in it. (You can divide it up into
429 * a side-A triangle containing A^2 subtriangles, a side-B
430 * triangle containing B^2, and two congruent parallelograms,
431 * each with side lengths A and B, each therefore containing AB
432 * two-triangle rhombuses.)
437 return d1
*d1
+ d2
*d2
+ 4*d1
*d2
;
447 static void classify_grid_square_callback(void *ctx
, struct grid_square
*sq
)
449 struct grid_data
*data
= (struct grid_data
*)ctx
;
452 if (data
->nclasses
== 4)
453 thisclass
= sq
->tetra_class
;
454 else if (data
->nclasses
== 2)
455 thisclass
= sq
->flip
;
459 data
->gridptrs
[thisclass
][data
->nsquares
[thisclass
]++] =
463 char *new_game_seed(game_params
*params
)
465 struct grid_data data
;
466 int i
, j
, k
, m
, area
, facesperclass
;
471 * Enumerate the grid squares, dividing them into equivalence
472 * classes as appropriate. (For the tetrahedron, there is one
473 * equivalence class for each face; for the octahedron there
474 * are two classes; for the other two solids there's only one.)
477 area
= grid_area(params
->d1
, params
->d2
, solids
[params
->solid
]->order
);
478 if (params
->solid
== TETRAHEDRON
)
480 else if (params
->solid
== OCTAHEDRON
)
484 data
.gridptrs
[0] = snewn(data
.nclasses
* area
, int);
485 for (i
= 0; i
< data
.nclasses
; i
++) {
486 data
.gridptrs
[i
] = data
.gridptrs
[0] + i
* area
;
487 data
.nsquares
[i
] = 0;
489 data
.squareindex
= 0;
490 enum_grid_squares(params
, classify_grid_square_callback
, &data
);
492 facesperclass
= solids
[params
->solid
]->nfaces
/ data
.nclasses
;
494 for (i
= 0; i
< data
.nclasses
; i
++)
495 assert(data
.nsquares
[i
] >= facesperclass
);
496 assert(data
.squareindex
== area
);
499 * So now we know how many faces to allocate in each class. Get
502 flags
= snewn(area
, int);
503 for (i
= 0; i
< area
; i
++)
506 for (i
= 0; i
< data
.nclasses
; i
++) {
507 for (j
= 0; j
< facesperclass
; j
++) {
508 unsigned long divisor
= RAND_MAX
/ data
.nsquares
[i
];
509 unsigned long max
= divisor
* data
.nsquares
[i
];
518 assert(!flags
[data
.gridptrs
[i
][n
]]);
519 flags
[data
.gridptrs
[i
][n
]] = TRUE
;
522 * Move everything else up the array. I ought to use a
523 * better data structure for this, but for such small
524 * numbers it hardly seems worth the effort.
526 while (n
< data
.nsquares
[i
]-1) {
527 data
.gridptrs
[i
][n
] = data
.gridptrs
[i
][n
+1];
535 * Now we know precisely which squares are blue. Encode this
536 * information in hex. While we're looping over this, collect
537 * the non-blue squares into a list in the now-unused gridptrs
540 seed
= snewn(area
/ 4 + 40, char);
545 for (i
= 0; i
< area
; i
++) {
549 data
.gridptrs
[0][m
++] = i
;
553 *p
++ = "0123456789ABCDEF"[j
];
559 *p
++ = "0123456789ABCDEF"[j
];
562 * Choose a non-blue square for the polyhedron.
565 unsigned long divisor
= RAND_MAX
/ m
;
566 unsigned long max
= divisor
* m
;
575 sprintf(p
, ":%d", data
.gridptrs
[0][n
]);
578 sfree(data
.gridptrs
[0]);
584 static void add_grid_square_callback(void *ctx
, struct grid_square
*sq
)
586 game_state
*state
= (game_state
*)ctx
;
588 state
->squares
[state
->nsquares
] = *sq
; /* structure copy */
589 state
->squares
[state
->nsquares
].blue
= FALSE
;
593 static int lowest_face(const struct solid
*solid
)
600 for (i
= 0; i
< solid
->nfaces
; i
++) {
603 for (j
= 0; j
< solid
->order
; j
++) {
604 int f
= solid
->faces
[i
*solid
->order
+ j
];
605 z
+= solid
->vertices
[f
*3+2];
608 if (i
== 0 || zmin
> z
) {
617 static int align_poly(const struct solid
*solid
, struct grid_square
*sq
,
622 int flip
= (sq
->flip ?
-1 : +1);
625 * First, find the lowest z-coordinate present in the solid.
628 for (i
= 0; i
< solid
->nvertices
; i
++)
629 if (zmin
> solid
->vertices
[i
*3+2])
630 zmin
= solid
->vertices
[i
*3+2];
633 * Now go round the grid square. For each point in the grid
634 * square, we're looking for a point of the polyhedron with the
635 * same x- and y-coordinates (relative to the square's centre),
636 * and z-coordinate equal to zmin (near enough).
638 for (j
= 0; j
< sq
->npoints
; j
++) {
644 for (i
= 0; i
< solid
->nvertices
; i
++) {
647 dist
+= SQ(solid
->vertices
[i
*3+0] * flip
- sq
->points
[j
*2+0] + sq
->x
);
648 dist
+= SQ(solid
->vertices
[i
*3+1] * flip
- sq
->points
[j
*2+1] + sq
->y
);
649 dist
+= SQ(solid
->vertices
[i
*3+2] - zmin
);
657 if (matches
!= 1 || index
< 0)
665 static void flip_poly(struct solid
*solid
, int flip
)
670 for (i
= 0; i
< solid
->nvertices
; i
++) {
671 solid
->vertices
[i
*3+0] *= -1;
672 solid
->vertices
[i
*3+1] *= -1;
674 for (i
= 0; i
< solid
->nfaces
; i
++) {
675 solid
->normals
[i
*3+0] *= -1;
676 solid
->normals
[i
*3+1] *= -1;
681 static struct solid
*transform_poly(const struct solid
*solid
, int flip
,
682 int key0
, int key1
, float angle
)
684 struct solid
*ret
= snew(struct solid
);
685 float vx
, vy
, ax
, ay
;
686 float vmatrix
[9], amatrix
[9], vmatrix2
[9];
689 *ret
= *solid
; /* structure copy */
691 flip_poly(ret
, flip
);
694 * Now rotate the polyhedron through the given angle. We must
695 * rotate about the Z-axis to bring the two vertices key0 and
696 * key1 into horizontal alignment, then rotate about the
697 * X-axis, then rotate back again.
699 vx
= ret
->vertices
[key1
*3+0] - ret
->vertices
[key0
*3+0];
700 vy
= ret
->vertices
[key1
*3+1] - ret
->vertices
[key0
*3+1];
701 assert(APPROXEQ(vx
*vx
+ vy
*vy
, 1.0));
703 vmatrix
[0] = vx
; vmatrix
[3] = vy
; vmatrix
[6] = 0;
704 vmatrix
[1] = -vy
; vmatrix
[4] = vx
; vmatrix
[7] = 0;
705 vmatrix
[2] = 0; vmatrix
[5] = 0; vmatrix
[8] = 1;
710 amatrix
[0] = 1; amatrix
[3] = 0; amatrix
[6] = 0;
711 amatrix
[1] = 0; amatrix
[4] = ax
; amatrix
[7] = ay
;
712 amatrix
[2] = 0; amatrix
[5] = -ay
; amatrix
[8] = ax
;
714 memcpy(vmatrix2
, vmatrix
, sizeof(vmatrix
));
718 for (i
= 0; i
< ret
->nvertices
; i
++) {
719 MATMUL(ret
->vertices
+ 3*i
, vmatrix
, ret
->vertices
+ 3*i
);
720 MATMUL(ret
->vertices
+ 3*i
, amatrix
, ret
->vertices
+ 3*i
);
721 MATMUL(ret
->vertices
+ 3*i
, vmatrix2
, ret
->vertices
+ 3*i
);
723 for (i
= 0; i
< ret
->nfaces
; i
++) {
724 MATMUL(ret
->normals
+ 3*i
, vmatrix
, ret
->normals
+ 3*i
);
725 MATMUL(ret
->normals
+ 3*i
, amatrix
, ret
->normals
+ 3*i
);
726 MATMUL(ret
->normals
+ 3*i
, vmatrix2
, ret
->normals
+ 3*i
);
732 game_state
*new_game(game_params
*params
, char *seed
)
734 game_state
*state
= snew(game_state
);
737 state
->params
= *params
; /* structure copy */
738 state
->solid
= solids
[params
->solid
];
740 area
= grid_area(params
->d1
, params
->d2
, state
->solid
->order
);
741 state
->squares
= snewn(area
, struct grid_square
);
743 enum_grid_squares(params
, add_grid_square_callback
, state
);
744 assert(state
->nsquares
== area
);
746 state
->facecolours
= snewn(state
->solid
->nfaces
, int);
747 memset(state
->facecolours
, 0, state
->solid
->nfaces
* sizeof(int));
750 * Set up the blue squares and polyhedron position according to
759 for (i
= 0; i
< state
->nsquares
; i
++) {
762 if (v
>= '0' && v
<= '9')
764 else if (v
>= 'A' && v
<= 'F')
766 else if (v
>= 'a' && v
<= 'f')
772 state
->squares
[i
].blue
= TRUE
;
781 state
->current
= atoi(p
);
782 if (state
->current
< 0 || state
->current
>= state
->nsquares
)
783 state
->current
= 0; /* got to do _something_ */
787 * Align the polyhedron with its grid square and determine
788 * initial key points.
794 ret
= align_poly(state
->solid
, &state
->squares
[state
->current
], pkey
);
797 state
->dpkey
[0] = state
->spkey
[0] = pkey
[0];
798 state
->dpkey
[1] = state
->spkey
[0] = pkey
[1];
799 state
->dgkey
[0] = state
->sgkey
[0] = 0;
800 state
->dgkey
[1] = state
->sgkey
[0] = 1;
803 state
->previous
= state
->current
;
805 state
->completed
= FALSE
;
806 state
->movecount
= 0;
811 game_state
*dup_game(game_state
*state
)
813 game_state
*ret
= snew(game_state
);
815 ret
->params
= state
->params
; /* structure copy */
816 ret
->solid
= state
->solid
;
817 ret
->facecolours
= snewn(ret
->solid
->nfaces
, int);
818 memcpy(ret
->facecolours
, state
->facecolours
,
819 ret
->solid
->nfaces
* sizeof(int));
820 ret
->nsquares
= state
->nsquares
;
821 ret
->squares
= snewn(ret
->nsquares
, struct grid_square
);
822 memcpy(ret
->squares
, state
->squares
,
823 ret
->nsquares
* sizeof(struct grid_square
));
824 ret
->dpkey
[0] = state
->dpkey
[0];
825 ret
->dpkey
[1] = state
->dpkey
[1];
826 ret
->dgkey
[0] = state
->dgkey
[0];
827 ret
->dgkey
[1] = state
->dgkey
[1];
828 ret
->spkey
[0] = state
->spkey
[0];
829 ret
->spkey
[1] = state
->spkey
[1];
830 ret
->sgkey
[0] = state
->sgkey
[0];
831 ret
->sgkey
[1] = state
->sgkey
[1];
832 ret
->previous
= state
->previous
;
833 ret
->angle
= state
->angle
;
834 ret
->completed
= state
->completed
;
835 ret
->movecount
= state
->movecount
;
840 void free_game(game_state
*state
)
845 game_state
*make_move(game_state
*from
, int x
, int y
, int button
)
848 int pkey
[2], skey
[2], dkey
[2];
852 int i
, j
, dest
, mask
;
856 * All moves are made with the cursor keys.
858 if (button
== CURSOR_UP
)
860 else if (button
== CURSOR_DOWN
)
862 else if (button
== CURSOR_LEFT
)
864 else if (button
== CURSOR_RIGHT
)
866 else if (button
== CURSOR_UP_LEFT
)
868 else if (button
== CURSOR_DOWN_LEFT
)
869 direction
= DOWN_LEFT
;
870 else if (button
== CURSOR_UP_RIGHT
)
871 direction
= UP_RIGHT
;
872 else if (button
== CURSOR_DOWN_RIGHT
)
873 direction
= DOWN_RIGHT
;
878 * Find the two points in the current grid square which
879 * correspond to this move.
881 mask
= from
->squares
[from
->current
].directions
[direction
];
884 for (i
= j
= 0; i
< from
->squares
[from
->current
].npoints
; i
++)
885 if (mask
& (1 << i
)) {
886 points
[j
*2] = from
->squares
[from
->current
].points
[i
*2];
887 points
[j
*2+1] = from
->squares
[from
->current
].points
[i
*2+1];
894 * Now find the other grid square which shares those points.
895 * This is our move destination.
898 for (i
= 0; i
< from
->nsquares
; i
++)
899 if (i
!= from
->current
) {
903 for (j
= 0; j
< from
->squares
[i
].npoints
; j
++) {
904 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[0]) +
905 SQ(from
->squares
[i
].points
[j
*2+1] - points
[1]));
908 dist
= (SQ(from
->squares
[i
].points
[j
*2] - points
[2]) +
909 SQ(from
->squares
[i
].points
[j
*2+1] - points
[3]));
923 ret
= dup_game(from
);
927 * So we know what grid square we're aiming for, and we also
928 * know the two key points (as indices in both the source and
929 * destination grid squares) which are invariant between source
932 * Next we must roll the polyhedron on to that square. So we
933 * find the indices of the key points within the polyhedron's
934 * vertex array, then use those in a call to transform_poly,
935 * and align the result on the new grid square.
939 align_poly(from
->solid
, &from
->squares
[from
->current
], all_pkey
);
940 pkey
[0] = all_pkey
[skey
[0]];
941 pkey
[1] = all_pkey
[skey
[1]];
943 * Now pkey[0] corresponds to skey[0] and dkey[0], and
949 * Now find the angle through which to rotate the polyhedron.
950 * Do this by finding the two faces that share the two vertices
951 * we've found, and taking the dot product of their normals.
957 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
959 for (j
= 0; j
< from
->solid
->order
; j
++)
960 if (from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[0] ||
961 from
->solid
->faces
[i
*from
->solid
->order
+ j
] == pkey
[1])
972 for (i
= 0; i
< 3; i
++)
973 dp
+= (from
->solid
->normals
[f
[0]*3+i
] *
974 from
->solid
->normals
[f
[1]*3+i
]);
979 * Now transform the polyhedron. We aren't entirely sure
980 * whether we need to rotate through angle or -angle, and the
981 * simplest way round this is to try both and see which one
982 * aligns successfully!
984 * Unfortunately, _both_ will align successfully if this is a
985 * cube, which won't tell us anything much. So for that
986 * particular case, I resort to gross hackery: I simply negate
987 * the angle before trying the alignment, depending on the
988 * direction. Which directions work which way is determined by
989 * pure trial and error. I said it was gross :-/
995 if (from
->solid
->order
== 4 && direction
== UP
)
996 angle
= -angle
; /* HACK */
998 poly
= transform_poly(from
->solid
,
999 from
->squares
[from
->current
].flip
,
1000 pkey
[0], pkey
[1], angle
);
1001 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1002 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1006 poly
= transform_poly(from
->solid
,
1007 from
->squares
[from
->current
].flip
,
1008 pkey
[0], pkey
[1], angle
);
1009 flip_poly(poly
, from
->squares
[ret
->current
].flip
);
1010 success
= align_poly(poly
, &from
->squares
[ret
->current
], all_pkey
);
1017 * Now we have our rotated polyhedron, which we expect to be
1018 * exactly congruent to the one we started with - but with the
1019 * faces permuted. So we map that congruence and thereby figure
1020 * out how to permute the faces as a result of the polyhedron
1024 int *newcolours
= snewn(from
->solid
->nfaces
, int);
1026 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1029 for (i
= 0; i
< from
->solid
->nfaces
; i
++) {
1033 * Now go through the transformed polyhedron's faces
1034 * and figure out which one's normal is approximately
1035 * equal to this one.
1037 for (j
= 0; j
< poly
->nfaces
; j
++) {
1043 for (k
= 0; k
< 3; k
++)
1044 dist
+= SQ(poly
->normals
[j
*3+k
] -
1045 from
->solid
->normals
[i
*3+k
]);
1047 if (APPROXEQ(dist
, 0)) {
1049 newcolours
[i
] = ret
->facecolours
[j
];
1053 assert(nmatch
== 1);
1056 for (i
= 0; i
< from
->solid
->nfaces
; i
++)
1057 assert(newcolours
[i
] != -1);
1059 sfree(ret
->facecolours
);
1060 ret
->facecolours
= newcolours
;
1064 * And finally, swap the colour between the bottom face of the
1065 * polyhedron and the face we've just landed on.
1067 * We don't do this if the game is already complete, since we
1068 * allow the user to roll the fully blue polyhedron around the
1069 * grid as a feeble reward.
1071 if (!ret
->completed
) {
1072 i
= lowest_face(from
->solid
);
1073 j
= ret
->facecolours
[i
];
1074 ret
->facecolours
[i
] = ret
->squares
[ret
->current
].blue
;
1075 ret
->squares
[ret
->current
].blue
= j
;
1078 * Detect game completion.
1081 for (i
= 0; i
< ret
->solid
->nfaces
; i
++)
1082 if (ret
->facecolours
[i
])
1084 if (j
== ret
->solid
->nfaces
)
1085 ret
->completed
= TRUE
;
1091 * Align the normal polyhedron with its grid square, to get key
1092 * points for non-animated display.
1098 success
= align_poly(ret
->solid
, &ret
->squares
[ret
->current
], pkey
);
1101 ret
->dpkey
[0] = pkey
[0];
1102 ret
->dpkey
[1] = pkey
[1];
1108 ret
->spkey
[0] = pkey
[0];
1109 ret
->spkey
[1] = pkey
[1];
1110 ret
->sgkey
[0] = skey
[0];
1111 ret
->sgkey
[1] = skey
[1];
1112 ret
->previous
= from
->current
;
1119 /* ----------------------------------------------------------------------
1127 struct game_drawstate
{
1128 int ox
, oy
; /* pixel position of float origin */
1131 static void find_bbox_callback(void *ctx
, struct grid_square
*sq
)
1133 struct bbox
*bb
= (struct bbox
*)ctx
;
1136 for (i
= 0; i
< sq
->npoints
; i
++) {
1137 if (bb
->l
> sq
->points
[i
*2]) bb
->l
= sq
->points
[i
*2];
1138 if (bb
->r
< sq
->points
[i
*2]) bb
->r
= sq
->points
[i
*2];
1139 if (bb
->u
> sq
->points
[i
*2+1]) bb
->u
= sq
->points
[i
*2+1];
1140 if (bb
->d
< sq
->points
[i
*2+1]) bb
->d
= sq
->points
[i
*2+1];
1144 static struct bbox
find_bbox(game_params
*params
)
1149 * These should be hugely more than the real bounding box will
1152 bb
.l
= 2 * (params
->d1
+ params
->d2
);
1153 bb
.r
= -2 * (params
->d1
+ params
->d2
);
1154 bb
.u
= 2 * (params
->d1
+ params
->d2
);
1155 bb
.d
= -2 * (params
->d1
+ params
->d2
);
1156 enum_grid_squares(params
, find_bbox_callback
, &bb
);
1161 void game_size(game_params
*params
, int *x
, int *y
)
1163 struct bbox bb
= find_bbox(params
);
1164 *x
= (bb
.r
- bb
.l
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
;
1165 *y
= (bb
.d
- bb
.u
+ 2*solids
[params
->solid
]->border
) * GRID_SCALE
;
1168 float *game_colours(frontend
*fe
, game_state
*state
, int *ncolours
)
1170 float *ret
= snewn(3 * NCOLOURS
, float);
1172 frontend_default_colour(fe
, &ret
[COL_BACKGROUND
* 3]);
1174 ret
[COL_BORDER
* 3 + 0] = 0.0;
1175 ret
[COL_BORDER
* 3 + 1] = 0.0;
1176 ret
[COL_BORDER
* 3 + 2] = 0.0;
1178 ret
[COL_BLUE
* 3 + 0] = 0.0;
1179 ret
[COL_BLUE
* 3 + 1] = 0.0;
1180 ret
[COL_BLUE
* 3 + 2] = 1.0;
1182 *ncolours
= NCOLOURS
;
1186 game_drawstate
*game_new_drawstate(game_state
*state
)
1188 struct game_drawstate
*ds
= snew(struct game_drawstate
);
1189 struct bbox bb
= find_bbox(&state
->params
);
1191 ds
->ox
= -(bb
.l
- state
->solid
->border
) * GRID_SCALE
;
1192 ds
->oy
= -(bb
.u
- state
->solid
->border
) * GRID_SCALE
;
1197 void game_free_drawstate(game_drawstate
*ds
)
1202 void game_redraw(frontend
*fe
, game_drawstate
*ds
, game_state
*oldstate
,
1203 game_state
*state
, float animtime
)
1206 struct bbox bb
= find_bbox(&state
->params
);
1211 game_state
*newstate
;
1214 draw_rect(fe
, 0, 0, (bb
.r
-bb
.l
+2) * GRID_SCALE
,
1215 (bb
.d
-bb
.u
+2) * GRID_SCALE
, COL_BACKGROUND
);
1217 if (oldstate
&& oldstate
->movecount
> state
->movecount
) {
1221 * This is an Undo. So reverse the order of the states, and
1222 * run the roll timer backwards.
1228 animtime
= ROLLTIME
- animtime
;
1234 square
= state
->current
;
1235 pkey
= state
->dpkey
;
1236 gkey
= state
->dgkey
;
1238 angle
= state
->angle
* animtime
/ ROLLTIME
;
1239 square
= state
->previous
;
1240 pkey
= state
->spkey
;
1241 gkey
= state
->sgkey
;
1246 for (i
= 0; i
< state
->nsquares
; i
++) {
1249 for (j
= 0; j
< state
->squares
[i
].npoints
; j
++) {
1250 coords
[2*j
] = state
->squares
[i
].points
[2*j
]
1251 * GRID_SCALE
+ ds
->ox
;
1252 coords
[2*j
+1] = state
->squares
[i
].points
[2*j
+1]
1253 * GRID_SCALE
+ ds
->oy
;
1256 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, TRUE
,
1257 state
->squares
[i
].blue ? COL_BLUE
: COL_BACKGROUND
);
1258 draw_polygon(fe
, coords
, state
->squares
[i
].npoints
, FALSE
, COL_BORDER
);
1262 * Now compute and draw the polyhedron.
1264 poly
= transform_poly(state
->solid
, state
->squares
[square
].flip
,
1265 pkey
[0], pkey
[1], angle
);
1268 * Compute the translation required to align the two key points
1269 * on the polyhedron with the same key points on the current
1272 for (i
= 0; i
< 3; i
++) {
1275 for (j
= 0; j
< 2; j
++) {
1280 state
->squares
[square
].points
[gkey
[j
]*2+i
];
1285 tc
+= (grid_coord
- poly
->vertices
[pkey
[j
]*3+i
]);
1290 for (i
= 0; i
< poly
->nvertices
; i
++)
1291 for (j
= 0; j
< 3; j
++)
1292 poly
->vertices
[i
*3+j
] += t
[j
];
1295 * Now actually draw each face.
1297 for (i
= 0; i
< poly
->nfaces
; i
++) {
1301 for (j
= 0; j
< poly
->order
; j
++) {
1302 int f
= poly
->faces
[i
*poly
->order
+ j
];
1303 points
[j
*2] = (poly
->vertices
[f
*3+0] -
1304 poly
->vertices
[f
*3+2] * poly
->shear
);
1305 points
[j
*2+1] = (poly
->vertices
[f
*3+1] -
1306 poly
->vertices
[f
*3+2] * poly
->shear
);
1309 for (j
= 0; j
< poly
->order
; j
++) {
1310 coords
[j
*2] = points
[j
*2] * GRID_SCALE
+ ds
->ox
;
1311 coords
[j
*2+1] = points
[j
*2+1] * GRID_SCALE
+ ds
->oy
;
1315 * Find out whether these points are in a clockwise or
1316 * anticlockwise arrangement. If the latter, discard the
1317 * face because it's facing away from the viewer.
1319 * This would involve fiddly winding-number stuff for a
1320 * general polygon, but for the simple parallelograms we'll
1321 * be seeing here, all we have to do is check whether the
1322 * corners turn right or left. So we'll take the vector
1323 * from point 0 to point 1, turn it right 90 degrees,
1324 * and check the sign of the dot product with that and the
1325 * next vector (point 1 to point 2).
1328 float v1x
= points
[2]-points
[0];
1329 float v1y
= points
[3]-points
[1];
1330 float v2x
= points
[4]-points
[2];
1331 float v2y
= points
[5]-points
[3];
1332 float dp
= v1x
* v2y
- v1y
* v2x
;
1338 draw_polygon(fe
, coords
, poly
->order
, TRUE
,
1339 state
->facecolours
[i
] ? COL_BLUE
: COL_BACKGROUND
);
1340 draw_polygon(fe
, coords
, poly
->order
, FALSE
, COL_BORDER
);
1344 draw_update(fe
, 0, 0, (bb
.r
-bb
.l
+2) * GRID_SCALE
,
1345 (bb
.d
-bb
.u
+2) * GRID_SCALE
);
1348 float game_anim_length(game_state
*oldstate
, game_state
*newstate
)