Introduced a new function in every game which formats a game_state
[sgt/puzzles] / cube.c
1 /*
2 * cube.c: Cube game.
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <assert.h>
9 #include <ctype.h>
10 #include <math.h>
11
12 #include "puzzles.h"
13
14 #define MAXVERTICES 20
15 #define MAXFACES 20
16 #define MAXORDER 4
17 struct solid {
18 int nvertices;
19 float vertices[MAXVERTICES * 3]; /* 3*npoints coordinates */
20 int order;
21 int nfaces;
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 */
26 };
27
28 static const struct solid s_tetrahedron = {
29 4,
30 {
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,
35 },
36 3, 4,
37 {
38 0,2,1, 3,1,2, 2,0,3, 1,3,0
39 },
40 {
41 -0.816496580928F, -0.471404520791F, 0.333333333334F,
42 0.0F, 0.942809041583F, 0.333333333333F,
43 0.816496580928F, -0.471404520791F, 0.333333333334F,
44 0.0F, 0.0F, -1.0F,
45 },
46 0.0F, 0.3F
47 };
48
49 static const struct solid s_cube = {
50 8,
51 {
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,
56 },
57 4, 6,
58 {
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
60 },
61 {
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
65 },
66 0.3F, 0.5F
67 };
68
69 static const struct solid s_octahedron = {
70 6,
71 {
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,
78 },
79 3, 8,
80 {
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
82 },
83 {
84 -0.816496580928F, -0.471404520791F, -0.333333333334F,
85 -0.816496580928F, 0.471404520791F, 0.333333333334F,
86 0.0F, -0.942809041583F, 0.333333333333F,
87 0.0F, 0.0F, 1.0F,
88 0.0F, 0.0F, -1.0F,
89 0.0F, 0.942809041583F, -0.333333333333F,
90 0.816496580928F, -0.471404520791F, -0.333333333334F,
91 0.816496580928F, 0.471404520791F, 0.333333333334F,
92 },
93 0.0F, 0.5F
94 };
95
96 static const struct solid s_icosahedron = {
97 12,
98 {
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,
111 },
112 3, 20,
113 {
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,
118 },
119 {
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,
124 -0.0F, 0.0F, 1.0F,
125 0.0F, -0.666666666667F, 0.745355992501F,
126 0.0F, 0.666666666667F, -0.745355992501F,
127 0.0F, 0.0F, -1.0F,
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,
140 },
141 0.0F, 0.8F
142 };
143
144 enum {
145 TETRAHEDRON, CUBE, OCTAHEDRON, ICOSAHEDRON
146 };
147 static const struct solid *solids[] = {
148 &s_tetrahedron, &s_cube, &s_octahedron, &s_icosahedron
149 };
150
151 enum {
152 COL_BACKGROUND,
153 COL_BORDER,
154 COL_BLUE,
155 NCOLOURS
156 };
157
158 enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
159
160 #define GRID_SCALE 48.0F
161 #define ROLLTIME 0.13F
162
163 #define SQ(x) ( (x) * (x) )
164
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; \
171 } while (0)
172
173 #define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
174
175 struct grid_square {
176 float x, y;
177 int npoints;
178 float points[8]; /* maximum */
179 int directions[8]; /* bit masks showing point pairs */
180 int flip;
181 int blue;
182 int tetra_class;
183 };
184
185 struct game_params {
186 int solid;
187 /*
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).
193 */
194 int d1, d2;
195 };
196
197 struct game_state {
198 struct game_params params;
199 const struct solid *solid;
200 int *facecolours;
201 struct grid_square *squares;
202 int nsquares;
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 */
208 int previous;
209 float angle;
210 int completed;
211 int movecount;
212 };
213
214 static game_params *default_params(void)
215 {
216 game_params *ret = snew(game_params);
217
218 ret->solid = CUBE;
219 ret->d1 = 4;
220 ret->d2 = 4;
221
222 return ret;
223 }
224
225 static int game_fetch_preset(int i, char **name, game_params **params)
226 {
227 game_params *ret = snew(game_params);
228 char *str;
229
230 switch (i) {
231 case 0:
232 str = "Cube";
233 ret->solid = CUBE;
234 ret->d1 = 4;
235 ret->d2 = 4;
236 break;
237 case 1:
238 str = "Tetrahedron";
239 ret->solid = TETRAHEDRON;
240 ret->d1 = 1;
241 ret->d2 = 2;
242 break;
243 case 2:
244 str = "Octahedron";
245 ret->solid = OCTAHEDRON;
246 ret->d1 = 2;
247 ret->d2 = 2;
248 break;
249 case 3:
250 str = "Icosahedron";
251 ret->solid = ICOSAHEDRON;
252 ret->d1 = 3;
253 ret->d2 = 3;
254 break;
255 default:
256 sfree(ret);
257 return FALSE;
258 }
259
260 *name = dupstr(str);
261 *params = ret;
262 return TRUE;
263 }
264
265 static void free_params(game_params *params)
266 {
267 sfree(params);
268 }
269
270 static game_params *dup_params(game_params *params)
271 {
272 game_params *ret = snew(game_params);
273 *ret = *params; /* structure copy */
274 return ret;
275 }
276
277 static game_params *decode_params(char const *string)
278 {
279 game_params *ret = default_params();
280
281 switch (*string) {
282 case 't': ret->solid = TETRAHEDRON; string++; break;
283 case 'c': ret->solid = CUBE; string++; break;
284 case 'o': ret->solid = OCTAHEDRON; string++; break;
285 case 'i': ret->solid = ICOSAHEDRON; string++; break;
286 default: break;
287 }
288 ret->d1 = ret->d2 = atoi(string);
289 while (*string && isdigit(*string)) string++;
290 if (*string == 'x') {
291 string++;
292 ret->d2 = atoi(string);
293 }
294
295 return ret;
296 }
297
298 static char *encode_params(game_params *params)
299 {
300 char data[256];
301
302 assert(params->solid >= 0 && params->solid < 4);
303 sprintf(data, "%c%dx%d", "tcoi"[params->solid], params->d1, params->d2);
304
305 return dupstr(data);
306 }
307
308 static void enum_grid_squares(game_params *params,
309 void (*callback)(void *, struct grid_square *),
310 void *ctx)
311 {
312 const struct solid *solid = solids[params->solid];
313
314 if (solid->order == 4) {
315 int x, y;
316
317 for (y = 0; y < params->d2; y++)
318 for (x = 0; x < params->d1; x++) {
319 struct grid_square sq;
320
321 sq.x = (float)x;
322 sq.y = (float)y;
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;
331 sq.npoints = 4;
332
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 */
341
342 sq.flip = FALSE;
343
344 /*
345 * This is supremely irrelevant, but just to avoid
346 * having any uninitialised structure members...
347 */
348 sq.tetra_class = 0;
349
350 callback(ctx, &sq);
351 }
352 } else {
353 int row, rowlen, other, i, firstix = -1;
354 float theight = (float)(sqrt(3) / 2.0);
355
356 for (row = 0; row < params->d1 + params->d2; row++) {
357 if (row < params->d2) {
358 other = +1;
359 rowlen = row + params->d1;
360 } else {
361 other = -1;
362 rowlen = 2*params->d2 + params->d1 - row;
363 }
364
365 /*
366 * There are `rowlen' down-pointing triangles.
367 */
368 for (i = 0; i < rowlen; i++) {
369 struct grid_square sq;
370 int ix;
371 float x, y;
372
373 ix = (2 * i - (rowlen-1));
374 x = ix * 0.5F;
375 y = theight * row;
376 sq.x = x;
377 sq.y = y + theight / 3;
378 sq.points[0] = x - 0.5F;
379 sq.points[1] = y;
380 sq.points[2] = x;
381 sq.points[3] = y + theight;
382 sq.points[4] = x + 0.5F;
383 sq.points[5] = y;
384 sq.npoints = 3;
385
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 */
390
391 /*
392 * Down-pointing triangle: both the up diagonals go
393 * up, and the down ones go left and right.
394 */
395 sq.directions[UP_LEFT] = sq.directions[UP_RIGHT] =
396 sq.directions[UP];
397 sq.directions[DOWN_LEFT] = sq.directions[LEFT];
398 sq.directions[DOWN_RIGHT] = sq.directions[RIGHT];
399
400 sq.flip = TRUE;
401
402 if (firstix < 0)
403 firstix = ix & 3;
404 ix -= firstix;
405 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
406
407 callback(ctx, &sq);
408 }
409
410 /*
411 * There are `rowlen+other' up-pointing triangles.
412 */
413 for (i = 0; i < rowlen+other; i++) {
414 struct grid_square sq;
415 int ix;
416 float x, y;
417
418 ix = (2 * i - (rowlen+other-1));
419 x = ix * 0.5F;
420 y = theight * row;
421 sq.x = x;
422 sq.y = y + 2*theight / 3;
423 sq.points[0] = x + 0.5F;
424 sq.points[1] = y + theight;
425 sq.points[2] = x;
426 sq.points[3] = y;
427 sq.points[4] = x - 0.5F;
428 sq.points[5] = y + theight;
429 sq.npoints = 3;
430
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 */
435
436 /*
437 * Up-pointing triangle: both the down diagonals go
438 * down, and the up ones go left and right.
439 */
440 sq.directions[DOWN_LEFT] = sq.directions[DOWN_RIGHT] =
441 sq.directions[DOWN];
442 sq.directions[UP_LEFT] = sq.directions[LEFT];
443 sq.directions[UP_RIGHT] = sq.directions[RIGHT];
444
445 sq.flip = FALSE;
446
447 if (firstix < 0)
448 firstix = (ix - 1) & 3;
449 ix -= firstix;
450 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
451
452 callback(ctx, &sq);
453 }
454 }
455 }
456 }
457
458 static int grid_area(int d1, int d2, int order)
459 {
460 /*
461 * An NxM grid of squares has NM squares in it.
462 *
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.)
469 */
470 if (order == 4)
471 return d1 * d2;
472 else
473 return d1*d1 + d2*d2 + 4*d1*d2;
474 }
475
476 static config_item *game_configure(game_params *params)
477 {
478 config_item *ret = snewn(4, config_item);
479 char buf[80];
480
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;
485
486 ret[1].name = "Width / top";
487 ret[1].type = C_STRING;
488 sprintf(buf, "%d", params->d1);
489 ret[1].sval = dupstr(buf);
490 ret[1].ival = 0;
491
492 ret[2].name = "Height / bottom";
493 ret[2].type = C_STRING;
494 sprintf(buf, "%d", params->d2);
495 ret[2].sval = dupstr(buf);
496 ret[2].ival = 0;
497
498 ret[3].name = NULL;
499 ret[3].type = C_END;
500 ret[3].sval = NULL;
501 ret[3].ival = 0;
502
503 return ret;
504 }
505
506 static game_params *custom_params(config_item *cfg)
507 {
508 game_params *ret = snew(game_params);
509
510 ret->solid = cfg[0].ival;
511 ret->d1 = atoi(cfg[1].sval);
512 ret->d2 = atoi(cfg[2].sval);
513
514 return ret;
515 }
516
517 static void count_grid_square_callback(void *ctx, struct grid_square *sq)
518 {
519 int *classes = (int *)ctx;
520 int thisclass;
521
522 if (classes[4] == 4)
523 thisclass = sq->tetra_class;
524 else if (classes[4] == 2)
525 thisclass = sq->flip;
526 else
527 thisclass = 0;
528
529 classes[thisclass]++;
530 }
531
532 static char *validate_params(game_params *params)
533 {
534 int classes[5];
535 int i;
536
537 if (params->solid < 0 || params->solid >= lenof(solids))
538 return "Unrecognised solid type";
539
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";
543 } else {
544 if (params->d1 <= 0 && params->d2 <= 0)
545 return "At least one grid dimension must be greater than zero";
546 }
547
548 for (i = 0; i < 4; i++)
549 classes[i] = 0;
550 if (params->solid == TETRAHEDRON)
551 classes[4] = 4;
552 else if (params->solid == OCTAHEDRON)
553 classes[4] = 2;
554 else
555 classes[4] = 1;
556 enum_grid_squares(params, count_grid_square_callback, classes);
557
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";
561
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";
565
566 return NULL;
567 }
568
569 struct grid_data {
570 int *gridptrs[4];
571 int nsquares[4];
572 int nclasses;
573 int squareindex;
574 };
575
576 static void classify_grid_square_callback(void *ctx, struct grid_square *sq)
577 {
578 struct grid_data *data = (struct grid_data *)ctx;
579 int thisclass;
580
581 if (data->nclasses == 4)
582 thisclass = sq->tetra_class;
583 else if (data->nclasses == 2)
584 thisclass = sq->flip;
585 else
586 thisclass = 0;
587
588 data->gridptrs[thisclass][data->nsquares[thisclass]++] =
589 data->squareindex++;
590 }
591
592 static char *new_game_seed(game_params *params, random_state *rs)
593 {
594 struct grid_data data;
595 int i, j, k, m, area, facesperclass;
596 int *flags;
597 char *seed, *p;
598
599 /*
600 * Enumerate the grid squares, dividing them into equivalence
601 * classes as appropriate. (For the tetrahedron, there is one
602 * equivalence class for each face; for the octahedron there
603 * are two classes; for the other two solids there's only one.)
604 */
605
606 area = grid_area(params->d1, params->d2, solids[params->solid]->order);
607 if (params->solid == TETRAHEDRON)
608 data.nclasses = 4;
609 else if (params->solid == OCTAHEDRON)
610 data.nclasses = 2;
611 else
612 data.nclasses = 1;
613 data.gridptrs[0] = snewn(data.nclasses * area, int);
614 for (i = 0; i < data.nclasses; i++) {
615 data.gridptrs[i] = data.gridptrs[0] + i * area;
616 data.nsquares[i] = 0;
617 }
618 data.squareindex = 0;
619 enum_grid_squares(params, classify_grid_square_callback, &data);
620
621 facesperclass = solids[params->solid]->nfaces / data.nclasses;
622
623 for (i = 0; i < data.nclasses; i++)
624 assert(data.nsquares[i] >= facesperclass);
625 assert(data.squareindex == area);
626
627 /*
628 * So now we know how many faces to allocate in each class. Get
629 * on with it.
630 */
631 flags = snewn(area, int);
632 for (i = 0; i < area; i++)
633 flags[i] = FALSE;
634
635 for (i = 0; i < data.nclasses; i++) {
636 for (j = 0; j < facesperclass; j++) {
637 int n = random_upto(rs, data.nsquares[i]);
638
639 assert(!flags[data.gridptrs[i][n]]);
640 flags[data.gridptrs[i][n]] = TRUE;
641
642 /*
643 * Move everything else up the array. I ought to use a
644 * better data structure for this, but for such small
645 * numbers it hardly seems worth the effort.
646 */
647 while (n < data.nsquares[i]-1) {
648 data.gridptrs[i][n] = data.gridptrs[i][n+1];
649 n++;
650 }
651 data.nsquares[i]--;
652 }
653 }
654
655 /*
656 * Now we know precisely which squares are blue. Encode this
657 * information in hex. While we're looping over this, collect
658 * the non-blue squares into a list in the now-unused gridptrs
659 * array.
660 */
661 seed = snewn(area / 4 + 40, char);
662 p = seed;
663 j = 0;
664 k = 8;
665 m = 0;
666 for (i = 0; i < area; i++) {
667 if (flags[i]) {
668 j |= k;
669 } else {
670 data.gridptrs[0][m++] = i;
671 }
672 k >>= 1;
673 if (!k) {
674 *p++ = "0123456789ABCDEF"[j];
675 k = 8;
676 j = 0;
677 }
678 }
679 if (k != 8)
680 *p++ = "0123456789ABCDEF"[j];
681
682 /*
683 * Choose a non-blue square for the polyhedron.
684 */
685 sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]);
686
687 sfree(data.gridptrs[0]);
688 sfree(flags);
689
690 return seed;
691 }
692
693 static void add_grid_square_callback(void *ctx, struct grid_square *sq)
694 {
695 game_state *state = (game_state *)ctx;
696
697 state->squares[state->nsquares] = *sq; /* structure copy */
698 state->squares[state->nsquares].blue = FALSE;
699 state->nsquares++;
700 }
701
702 static int lowest_face(const struct solid *solid)
703 {
704 int i, j, best;
705 float zmin;
706
707 best = 0;
708 zmin = 0.0;
709 for (i = 0; i < solid->nfaces; i++) {
710 float z = 0;
711
712 for (j = 0; j < solid->order; j++) {
713 int f = solid->faces[i*solid->order + j];
714 z += solid->vertices[f*3+2];
715 }
716
717 if (i == 0 || zmin > z) {
718 zmin = z;
719 best = i;
720 }
721 }
722
723 return best;
724 }
725
726 static int align_poly(const struct solid *solid, struct grid_square *sq,
727 int *pkey)
728 {
729 float zmin;
730 int i, j;
731 int flip = (sq->flip ? -1 : +1);
732
733 /*
734 * First, find the lowest z-coordinate present in the solid.
735 */
736 zmin = 0.0;
737 for (i = 0; i < solid->nvertices; i++)
738 if (zmin > solid->vertices[i*3+2])
739 zmin = solid->vertices[i*3+2];
740
741 /*
742 * Now go round the grid square. For each point in the grid
743 * square, we're looking for a point of the polyhedron with the
744 * same x- and y-coordinates (relative to the square's centre),
745 * and z-coordinate equal to zmin (near enough).
746 */
747 for (j = 0; j < sq->npoints; j++) {
748 int matches, index;
749
750 matches = 0;
751 index = -1;
752
753 for (i = 0; i < solid->nvertices; i++) {
754 float dist = 0;
755
756 dist += SQ(solid->vertices[i*3+0] * flip - sq->points[j*2+0] + sq->x);
757 dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y);
758 dist += SQ(solid->vertices[i*3+2] - zmin);
759
760 if (dist < 0.1) {
761 matches++;
762 index = i;
763 }
764 }
765
766 if (matches != 1 || index < 0)
767 return FALSE;
768 pkey[j] = index;
769 }
770
771 return TRUE;
772 }
773
774 static void flip_poly(struct solid *solid, int flip)
775 {
776 int i;
777
778 if (flip) {
779 for (i = 0; i < solid->nvertices; i++) {
780 solid->vertices[i*3+0] *= -1;
781 solid->vertices[i*3+1] *= -1;
782 }
783 for (i = 0; i < solid->nfaces; i++) {
784 solid->normals[i*3+0] *= -1;
785 solid->normals[i*3+1] *= -1;
786 }
787 }
788 }
789
790 static struct solid *transform_poly(const struct solid *solid, int flip,
791 int key0, int key1, float angle)
792 {
793 struct solid *ret = snew(struct solid);
794 float vx, vy, ax, ay;
795 float vmatrix[9], amatrix[9], vmatrix2[9];
796 int i;
797
798 *ret = *solid; /* structure copy */
799
800 flip_poly(ret, flip);
801
802 /*
803 * Now rotate the polyhedron through the given angle. We must
804 * rotate about the Z-axis to bring the two vertices key0 and
805 * key1 into horizontal alignment, then rotate about the
806 * X-axis, then rotate back again.
807 */
808 vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0];
809 vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1];
810 assert(APPROXEQ(vx*vx + vy*vy, 1.0));
811
812 vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0;
813 vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0;
814 vmatrix[2] = 0; vmatrix[5] = 0; vmatrix[8] = 1;
815
816 ax = (float)cos(angle);
817 ay = (float)sin(angle);
818
819 amatrix[0] = 1; amatrix[3] = 0; amatrix[6] = 0;
820 amatrix[1] = 0; amatrix[4] = ax; amatrix[7] = ay;
821 amatrix[2] = 0; amatrix[5] = -ay; amatrix[8] = ax;
822
823 memcpy(vmatrix2, vmatrix, sizeof(vmatrix));
824 vmatrix2[1] = vy;
825 vmatrix2[3] = -vy;
826
827 for (i = 0; i < ret->nvertices; i++) {
828 MATMUL(ret->vertices + 3*i, vmatrix, ret->vertices + 3*i);
829 MATMUL(ret->vertices + 3*i, amatrix, ret->vertices + 3*i);
830 MATMUL(ret->vertices + 3*i, vmatrix2, ret->vertices + 3*i);
831 }
832 for (i = 0; i < ret->nfaces; i++) {
833 MATMUL(ret->normals + 3*i, vmatrix, ret->normals + 3*i);
834 MATMUL(ret->normals + 3*i, amatrix, ret->normals + 3*i);
835 MATMUL(ret->normals + 3*i, vmatrix2, ret->normals + 3*i);
836 }
837
838 return ret;
839 }
840
841 static char *validate_seed(game_params *params, char *seed)
842 {
843 int area = grid_area(params->d1, params->d2, solids[params->solid]->order);
844 int i, j;
845
846 i = (area + 3) / 4;
847 for (j = 0; j < i; j++) {
848 int c = seed[j];
849 if (c >= '0' && c <= '9') continue;
850 if (c >= 'A' && c <= 'F') continue;
851 if (c >= 'a' && c <= 'f') continue;
852 return "Not enough hex digits at start of string";
853 /* NB if seed[j]=='\0' that will also be caught here, so we're safe */
854 }
855
856 if (seed[i] != ',')
857 return "Expected ',' after hex digits";
858
859 i++;
860 do {
861 if (seed[i] < '0' || seed[i] > '9')
862 return "Expected decimal integer after ','";
863 i++;
864 } while (seed[i]);
865
866 return NULL;
867 }
868
869 static game_state *new_game(game_params *params, char *seed)
870 {
871 game_state *state = snew(game_state);
872 int area;
873
874 state->params = *params; /* structure copy */
875 state->solid = solids[params->solid];
876
877 area = grid_area(params->d1, params->d2, state->solid->order);
878 state->squares = snewn(area, struct grid_square);
879 state->nsquares = 0;
880 enum_grid_squares(params, add_grid_square_callback, state);
881 assert(state->nsquares == area);
882
883 state->facecolours = snewn(state->solid->nfaces, int);
884 memset(state->facecolours, 0, state->solid->nfaces * sizeof(int));
885
886 /*
887 * Set up the blue squares and polyhedron position according to
888 * the game seed.
889 */
890 {
891 char *p = seed;
892 int i, j, v;
893
894 j = 8;
895 v = 0;
896 for (i = 0; i < state->nsquares; i++) {
897 if (j == 8) {
898 v = *p++;
899 if (v >= '0' && v <= '9')
900 v -= '0';
901 else if (v >= 'A' && v <= 'F')
902 v -= 'A' - 10;
903 else if (v >= 'a' && v <= 'f')
904 v -= 'a' - 10;
905 else
906 break;
907 }
908 if (v & j)
909 state->squares[i].blue = TRUE;
910 j >>= 1;
911 if (j == 0)
912 j = 8;
913 }
914
915 if (*p == ',')
916 p++;
917
918 state->current = atoi(p);
919 if (state->current < 0 || state->current >= state->nsquares)
920 state->current = 0; /* got to do _something_ */
921 }
922
923 /*
924 * Align the polyhedron with its grid square and determine
925 * initial key points.
926 */
927 {
928 int pkey[4];
929 int ret;
930
931 ret = align_poly(state->solid, &state->squares[state->current], pkey);
932 assert(ret);
933
934 state->dpkey[0] = state->spkey[0] = pkey[0];
935 state->dpkey[1] = state->spkey[0] = pkey[1];
936 state->dgkey[0] = state->sgkey[0] = 0;
937 state->dgkey[1] = state->sgkey[0] = 1;
938 }
939
940 state->previous = state->current;
941 state->angle = 0.0;
942 state->completed = 0;
943 state->movecount = 0;
944
945 return state;
946 }
947
948 static game_state *dup_game(game_state *state)
949 {
950 game_state *ret = snew(game_state);
951
952 ret->params = state->params; /* structure copy */
953 ret->solid = state->solid;
954 ret->facecolours = snewn(ret->solid->nfaces, int);
955 memcpy(ret->facecolours, state->facecolours,
956 ret->solid->nfaces * sizeof(int));
957 ret->nsquares = state->nsquares;
958 ret->squares = snewn(ret->nsquares, struct grid_square);
959 memcpy(ret->squares, state->squares,
960 ret->nsquares * sizeof(struct grid_square));
961 ret->dpkey[0] = state->dpkey[0];
962 ret->dpkey[1] = state->dpkey[1];
963 ret->dgkey[0] = state->dgkey[0];
964 ret->dgkey[1] = state->dgkey[1];
965 ret->spkey[0] = state->spkey[0];
966 ret->spkey[1] = state->spkey[1];
967 ret->sgkey[0] = state->sgkey[0];
968 ret->sgkey[1] = state->sgkey[1];
969 ret->previous = state->previous;
970 ret->angle = state->angle;
971 ret->completed = state->completed;
972 ret->movecount = state->movecount;
973
974 return ret;
975 }
976
977 static void free_game(game_state *state)
978 {
979 sfree(state);
980 }
981
982 static char *game_text_format(game_state *state)
983 {
984 return NULL;
985 }
986
987 static game_ui *new_ui(game_state *state)
988 {
989 return NULL;
990 }
991
992 static void free_ui(game_ui *ui)
993 {
994 }
995
996 static game_state *make_move(game_state *from, game_ui *ui,
997 int x, int y, int button)
998 {
999 int direction;
1000 int pkey[2], skey[2], dkey[2];
1001 float points[4];
1002 game_state *ret;
1003 float angle;
1004 int i, j, dest, mask;
1005 struct solid *poly;
1006
1007 /*
1008 * All moves are made with the cursor keys.
1009 */
1010 if (button == CURSOR_UP)
1011 direction = UP;
1012 else if (button == CURSOR_DOWN)
1013 direction = DOWN;
1014 else if (button == CURSOR_LEFT)
1015 direction = LEFT;
1016 else if (button == CURSOR_RIGHT)
1017 direction = RIGHT;
1018 else if (button == CURSOR_UP_LEFT)
1019 direction = UP_LEFT;
1020 else if (button == CURSOR_DOWN_LEFT)
1021 direction = DOWN_LEFT;
1022 else if (button == CURSOR_UP_RIGHT)
1023 direction = UP_RIGHT;
1024 else if (button == CURSOR_DOWN_RIGHT)
1025 direction = DOWN_RIGHT;
1026 else
1027 return NULL;
1028
1029 /*
1030 * Find the two points in the current grid square which
1031 * correspond to this move.
1032 */
1033 mask = from->squares[from->current].directions[direction];
1034 if (mask == 0)
1035 return NULL;
1036 for (i = j = 0; i < from->squares[from->current].npoints; i++)
1037 if (mask & (1 << i)) {
1038 points[j*2] = from->squares[from->current].points[i*2];
1039 points[j*2+1] = from->squares[from->current].points[i*2+1];
1040 skey[j] = i;
1041 j++;
1042 }
1043 assert(j == 2);
1044
1045 /*
1046 * Now find the other grid square which shares those points.
1047 * This is our move destination.
1048 */
1049 dest = -1;
1050 for (i = 0; i < from->nsquares; i++)
1051 if (i != from->current) {
1052 int match = 0;
1053 float dist;
1054
1055 for (j = 0; j < from->squares[i].npoints; j++) {
1056 dist = (SQ(from->squares[i].points[j*2] - points[0]) +
1057 SQ(from->squares[i].points[j*2+1] - points[1]));
1058 if (dist < 0.1)
1059 dkey[match++] = j;
1060 dist = (SQ(from->squares[i].points[j*2] - points[2]) +
1061 SQ(from->squares[i].points[j*2+1] - points[3]));
1062 if (dist < 0.1)
1063 dkey[match++] = j;
1064 }
1065
1066 if (match == 2) {
1067 dest = i;
1068 break;
1069 }
1070 }
1071
1072 if (dest < 0)
1073 return NULL;
1074
1075 ret = dup_game(from);
1076 ret->current = i;
1077
1078 /*
1079 * So we know what grid square we're aiming for, and we also
1080 * know the two key points (as indices in both the source and
1081 * destination grid squares) which are invariant between source
1082 * and destination.
1083 *
1084 * Next we must roll the polyhedron on to that square. So we
1085 * find the indices of the key points within the polyhedron's
1086 * vertex array, then use those in a call to transform_poly,
1087 * and align the result on the new grid square.
1088 */
1089 {
1090 int all_pkey[4];
1091 align_poly(from->solid, &from->squares[from->current], all_pkey);
1092 pkey[0] = all_pkey[skey[0]];
1093 pkey[1] = all_pkey[skey[1]];
1094 /*
1095 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1096 * likewise [1].
1097 */
1098 }
1099
1100 /*
1101 * Now find the angle through which to rotate the polyhedron.
1102 * Do this by finding the two faces that share the two vertices
1103 * we've found, and taking the dot product of their normals.
1104 */
1105 {
1106 int f[2], nf = 0;
1107 float dp;
1108
1109 for (i = 0; i < from->solid->nfaces; i++) {
1110 int match = 0;
1111 for (j = 0; j < from->solid->order; j++)
1112 if (from->solid->faces[i*from->solid->order + j] == pkey[0] ||
1113 from->solid->faces[i*from->solid->order + j] == pkey[1])
1114 match++;
1115 if (match == 2) {
1116 assert(nf < 2);
1117 f[nf++] = i;
1118 }
1119 }
1120
1121 assert(nf == 2);
1122
1123 dp = 0;
1124 for (i = 0; i < 3; i++)
1125 dp += (from->solid->normals[f[0]*3+i] *
1126 from->solid->normals[f[1]*3+i]);
1127 angle = (float)acos(dp);
1128 }
1129
1130 /*
1131 * Now transform the polyhedron. We aren't entirely sure
1132 * whether we need to rotate through angle or -angle, and the
1133 * simplest way round this is to try both and see which one
1134 * aligns successfully!
1135 *
1136 * Unfortunately, _both_ will align successfully if this is a
1137 * cube, which won't tell us anything much. So for that
1138 * particular case, I resort to gross hackery: I simply negate
1139 * the angle before trying the alignment, depending on the
1140 * direction. Which directions work which way is determined by
1141 * pure trial and error. I said it was gross :-/
1142 */
1143 {
1144 int all_pkey[4];
1145 int success;
1146
1147 if (from->solid->order == 4 && direction == UP)
1148 angle = -angle; /* HACK */
1149
1150 poly = transform_poly(from->solid,
1151 from->squares[from->current].flip,
1152 pkey[0], pkey[1], angle);
1153 flip_poly(poly, from->squares[ret->current].flip);
1154 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1155
1156 if (!success) {
1157 angle = -angle;
1158 poly = transform_poly(from->solid,
1159 from->squares[from->current].flip,
1160 pkey[0], pkey[1], angle);
1161 flip_poly(poly, from->squares[ret->current].flip);
1162 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1163 }
1164
1165 assert(success);
1166 }
1167
1168 /*
1169 * Now we have our rotated polyhedron, which we expect to be
1170 * exactly congruent to the one we started with - but with the
1171 * faces permuted. So we map that congruence and thereby figure
1172 * out how to permute the faces as a result of the polyhedron
1173 * having rolled.
1174 */
1175 {
1176 int *newcolours = snewn(from->solid->nfaces, int);
1177
1178 for (i = 0; i < from->solid->nfaces; i++)
1179 newcolours[i] = -1;
1180
1181 for (i = 0; i < from->solid->nfaces; i++) {
1182 int nmatch = 0;
1183
1184 /*
1185 * Now go through the transformed polyhedron's faces
1186 * and figure out which one's normal is approximately
1187 * equal to this one.
1188 */
1189 for (j = 0; j < poly->nfaces; j++) {
1190 float dist;
1191 int k;
1192
1193 dist = 0;
1194
1195 for (k = 0; k < 3; k++)
1196 dist += SQ(poly->normals[j*3+k] -
1197 from->solid->normals[i*3+k]);
1198
1199 if (APPROXEQ(dist, 0)) {
1200 nmatch++;
1201 newcolours[i] = ret->facecolours[j];
1202 }
1203 }
1204
1205 assert(nmatch == 1);
1206 }
1207
1208 for (i = 0; i < from->solid->nfaces; i++)
1209 assert(newcolours[i] != -1);
1210
1211 sfree(ret->facecolours);
1212 ret->facecolours = newcolours;
1213 }
1214
1215 ret->movecount++;
1216
1217 /*
1218 * And finally, swap the colour between the bottom face of the
1219 * polyhedron and the face we've just landed on.
1220 *
1221 * We don't do this if the game is already complete, since we
1222 * allow the user to roll the fully blue polyhedron around the
1223 * grid as a feeble reward.
1224 */
1225 if (!ret->completed) {
1226 i = lowest_face(from->solid);
1227 j = ret->facecolours[i];
1228 ret->facecolours[i] = ret->squares[ret->current].blue;
1229 ret->squares[ret->current].blue = j;
1230
1231 /*
1232 * Detect game completion.
1233 */
1234 j = 0;
1235 for (i = 0; i < ret->solid->nfaces; i++)
1236 if (ret->facecolours[i])
1237 j++;
1238 if (j == ret->solid->nfaces)
1239 ret->completed = ret->movecount;
1240 }
1241
1242 sfree(poly);
1243
1244 /*
1245 * Align the normal polyhedron with its grid square, to get key
1246 * points for non-animated display.
1247 */
1248 {
1249 int pkey[4];
1250 int success;
1251
1252 success = align_poly(ret->solid, &ret->squares[ret->current], pkey);
1253 assert(success);
1254
1255 ret->dpkey[0] = pkey[0];
1256 ret->dpkey[1] = pkey[1];
1257 ret->dgkey[0] = 0;
1258 ret->dgkey[1] = 1;
1259 }
1260
1261
1262 ret->spkey[0] = pkey[0];
1263 ret->spkey[1] = pkey[1];
1264 ret->sgkey[0] = skey[0];
1265 ret->sgkey[1] = skey[1];
1266 ret->previous = from->current;
1267 ret->angle = angle;
1268
1269 return ret;
1270 }
1271
1272 /* ----------------------------------------------------------------------
1273 * Drawing routines.
1274 */
1275
1276 struct bbox {
1277 float l, r, u, d;
1278 };
1279
1280 struct game_drawstate {
1281 int ox, oy; /* pixel position of float origin */
1282 };
1283
1284 static void find_bbox_callback(void *ctx, struct grid_square *sq)
1285 {
1286 struct bbox *bb = (struct bbox *)ctx;
1287 int i;
1288
1289 for (i = 0; i < sq->npoints; i++) {
1290 if (bb->l > sq->points[i*2]) bb->l = sq->points[i*2];
1291 if (bb->r < sq->points[i*2]) bb->r = sq->points[i*2];
1292 if (bb->u > sq->points[i*2+1]) bb->u = sq->points[i*2+1];
1293 if (bb->d < sq->points[i*2+1]) bb->d = sq->points[i*2+1];
1294 }
1295 }
1296
1297 static struct bbox find_bbox(game_params *params)
1298 {
1299 struct bbox bb;
1300
1301 /*
1302 * These should be hugely more than the real bounding box will
1303 * be.
1304 */
1305 bb.l = 2.0F * (params->d1 + params->d2);
1306 bb.r = -2.0F * (params->d1 + params->d2);
1307 bb.u = 2.0F * (params->d1 + params->d2);
1308 bb.d = -2.0F * (params->d1 + params->d2);
1309 enum_grid_squares(params, find_bbox_callback, &bb);
1310
1311 return bb;
1312 }
1313
1314 static void game_size(game_params *params, int *x, int *y)
1315 {
1316 struct bbox bb = find_bbox(params);
1317 *x = (int)((bb.r - bb.l + 2*solids[params->solid]->border) * GRID_SCALE);
1318 *y = (int)((bb.d - bb.u + 2*solids[params->solid]->border) * GRID_SCALE);
1319 }
1320
1321 static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1322 {
1323 float *ret = snewn(3 * NCOLOURS, float);
1324
1325 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1326
1327 ret[COL_BORDER * 3 + 0] = 0.0;
1328 ret[COL_BORDER * 3 + 1] = 0.0;
1329 ret[COL_BORDER * 3 + 2] = 0.0;
1330
1331 ret[COL_BLUE * 3 + 0] = 0.0;
1332 ret[COL_BLUE * 3 + 1] = 0.0;
1333 ret[COL_BLUE * 3 + 2] = 1.0;
1334
1335 *ncolours = NCOLOURS;
1336 return ret;
1337 }
1338
1339 static game_drawstate *game_new_drawstate(game_state *state)
1340 {
1341 struct game_drawstate *ds = snew(struct game_drawstate);
1342 struct bbox bb = find_bbox(&state->params);
1343
1344 ds->ox = (int)(-(bb.l - state->solid->border) * GRID_SCALE);
1345 ds->oy = (int)(-(bb.u - state->solid->border) * GRID_SCALE);
1346
1347 return ds;
1348 }
1349
1350 static void game_free_drawstate(game_drawstate *ds)
1351 {
1352 sfree(ds);
1353 }
1354
1355 static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
1356 game_state *state, int dir, game_ui *ui,
1357 float animtime, float flashtime)
1358 {
1359 int i, j;
1360 struct bbox bb = find_bbox(&state->params);
1361 struct solid *poly;
1362 int *pkey, *gkey;
1363 float t[3];
1364 float angle;
1365 game_state *newstate;
1366 int square;
1367
1368 draw_rect(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1369 (int)((bb.d-bb.u+2.0F) * GRID_SCALE), COL_BACKGROUND);
1370
1371 if (dir < 0) {
1372 game_state *t;
1373
1374 /*
1375 * This is an Undo. So reverse the order of the states, and
1376 * run the roll timer backwards.
1377 */
1378 assert(oldstate);
1379
1380 t = oldstate;
1381 oldstate = state;
1382 state = t;
1383
1384 animtime = ROLLTIME - animtime;
1385 }
1386
1387 if (!oldstate) {
1388 oldstate = state;
1389 angle = 0.0;
1390 square = state->current;
1391 pkey = state->dpkey;
1392 gkey = state->dgkey;
1393 } else {
1394 angle = state->angle * animtime / ROLLTIME;
1395 square = state->previous;
1396 pkey = state->spkey;
1397 gkey = state->sgkey;
1398 }
1399 newstate = state;
1400 state = oldstate;
1401
1402 for (i = 0; i < state->nsquares; i++) {
1403 int coords[8];
1404
1405 for (j = 0; j < state->squares[i].npoints; j++) {
1406 coords[2*j] = ((int)(state->squares[i].points[2*j] * GRID_SCALE)
1407 + ds->ox);
1408 coords[2*j+1] = ((int)(state->squares[i].points[2*j+1]*GRID_SCALE)
1409 + ds->oy);
1410 }
1411
1412 draw_polygon(fe, coords, state->squares[i].npoints, TRUE,
1413 state->squares[i].blue ? COL_BLUE : COL_BACKGROUND);
1414 draw_polygon(fe, coords, state->squares[i].npoints, FALSE, COL_BORDER);
1415 }
1416
1417 /*
1418 * Now compute and draw the polyhedron.
1419 */
1420 poly = transform_poly(state->solid, state->squares[square].flip,
1421 pkey[0], pkey[1], angle);
1422
1423 /*
1424 * Compute the translation required to align the two key points
1425 * on the polyhedron with the same key points on the current
1426 * face.
1427 */
1428 for (i = 0; i < 3; i++) {
1429 float tc = 0.0;
1430
1431 for (j = 0; j < 2; j++) {
1432 float grid_coord;
1433
1434 if (i < 2) {
1435 grid_coord =
1436 state->squares[square].points[gkey[j]*2+i];
1437 } else {
1438 grid_coord = 0.0;
1439 }
1440
1441 tc += (grid_coord - poly->vertices[pkey[j]*3+i]);
1442 }
1443
1444 t[i] = tc / 2;
1445 }
1446 for (i = 0; i < poly->nvertices; i++)
1447 for (j = 0; j < 3; j++)
1448 poly->vertices[i*3+j] += t[j];
1449
1450 /*
1451 * Now actually draw each face.
1452 */
1453 for (i = 0; i < poly->nfaces; i++) {
1454 float points[8];
1455 int coords[8];
1456
1457 for (j = 0; j < poly->order; j++) {
1458 int f = poly->faces[i*poly->order + j];
1459 points[j*2] = (poly->vertices[f*3+0] -
1460 poly->vertices[f*3+2] * poly->shear);
1461 points[j*2+1] = (poly->vertices[f*3+1] -
1462 poly->vertices[f*3+2] * poly->shear);
1463 }
1464
1465 for (j = 0; j < poly->order; j++) {
1466 coords[j*2] = (int)floor(points[j*2] * GRID_SCALE) + ds->ox;
1467 coords[j*2+1] = (int)floor(points[j*2+1] * GRID_SCALE) + ds->oy;
1468 }
1469
1470 /*
1471 * Find out whether these points are in a clockwise or
1472 * anticlockwise arrangement. If the latter, discard the
1473 * face because it's facing away from the viewer.
1474 *
1475 * This would involve fiddly winding-number stuff for a
1476 * general polygon, but for the simple parallelograms we'll
1477 * be seeing here, all we have to do is check whether the
1478 * corners turn right or left. So we'll take the vector
1479 * from point 0 to point 1, turn it right 90 degrees,
1480 * and check the sign of the dot product with that and the
1481 * next vector (point 1 to point 2).
1482 */
1483 {
1484 float v1x = points[2]-points[0];
1485 float v1y = points[3]-points[1];
1486 float v2x = points[4]-points[2];
1487 float v2y = points[5]-points[3];
1488 float dp = v1x * v2y - v1y * v2x;
1489
1490 if (dp <= 0)
1491 continue;
1492 }
1493
1494 draw_polygon(fe, coords, poly->order, TRUE,
1495 state->facecolours[i] ? COL_BLUE : COL_BACKGROUND);
1496 draw_polygon(fe, coords, poly->order, FALSE, COL_BORDER);
1497 }
1498 sfree(poly);
1499
1500 draw_update(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1501 (int)((bb.d-bb.u+2.0F) * GRID_SCALE));
1502
1503 /*
1504 * Update the status bar.
1505 */
1506 {
1507 char statusbuf[256];
1508
1509 sprintf(statusbuf, "%sMoves: %d",
1510 (state->completed ? "COMPLETED! " : ""),
1511 (state->completed ? state->completed : state->movecount));
1512
1513 status_bar(fe, statusbuf);
1514 }
1515 }
1516
1517 static float game_anim_length(game_state *oldstate,
1518 game_state *newstate, int dir)
1519 {
1520 return ROLLTIME;
1521 }
1522
1523 static float game_flash_length(game_state *oldstate,
1524 game_state *newstate, int dir)
1525 {
1526 return 0.0F;
1527 }
1528
1529 static int game_wants_statusbar(void)
1530 {
1531 return TRUE;
1532 }
1533
1534 #ifdef COMBINED
1535 #define thegame cube
1536 #endif
1537
1538 const struct game thegame = {
1539 "Cube", "games.cube",
1540 default_params,
1541 game_fetch_preset,
1542 decode_params,
1543 encode_params,
1544 free_params,
1545 dup_params,
1546 TRUE, game_configure, custom_params,
1547 validate_params,
1548 new_game_seed,
1549 validate_seed,
1550 new_game,
1551 dup_game,
1552 free_game,
1553 NULL, game_text_format,
1554 new_ui,
1555 free_ui,
1556 make_move,
1557 game_size,
1558 game_colours,
1559 game_new_drawstate,
1560 game_free_drawstate,
1561 game_redraw,
1562 game_anim_length,
1563 game_flash_length,
1564 game_wants_statusbar,
1565 };