Remove outdated comment :-)
[sgt/puzzles] / cube.c
CommitLineData
720a8fb7 1/*
2 * cube.c: Cube game.
3 */
1482ee76 4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <assert.h>
b0e26073 9#include <ctype.h>
1482ee76 10#include <math.h>
11
12#include "puzzles.h"
13
14#define MAXVERTICES 20
15#define MAXFACES 20
16#define MAXORDER 4
17struct 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 */
eb2ad6f1 25 float border; /* border required around arena */
1482ee76 26};
27
19ef4855 28static const struct solid s_tetrahedron = {
1482ee76 29 4,
30 {
03f856c4 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,
1482ee76 35 },
36 3, 4,
37 {
38 0,2,1, 3,1,2, 2,0,3, 1,3,0
39 },
40 {
03f856c4 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,
1482ee76 45 },
03f856c4 46 0.0F, 0.3F
1482ee76 47};
48
19ef4855 49static const struct solid s_cube = {
1482ee76 50 8,
51 {
03f856c4 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,
1482ee76 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 {
03f856c4 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
1482ee76 65 },
03f856c4 66 0.3F, 0.5F
1482ee76 67};
68
19ef4855 69static const struct solid s_octahedron = {
1482ee76 70 6,
71 {
03f856c4 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,
1482ee76 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 {
03f856c4 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,
1482ee76 92 },
03f856c4 93 0.0F, 0.5F
1482ee76 94};
95
19ef4855 96static const struct solid s_icosahedron = {
1482ee76 97 12,
98 {
03f856c4 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,
1482ee76 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 {
03f856c4 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,
1482ee76 140 },
03f856c4 141 0.0F, 0.8F
1482ee76 142};
143
144enum {
145 TETRAHEDRON, CUBE, OCTAHEDRON, ICOSAHEDRON
146};
147static const struct solid *solids[] = {
19ef4855 148 &s_tetrahedron, &s_cube, &s_octahedron, &s_icosahedron
1482ee76 149};
150
151enum {
152 COL_BACKGROUND,
153 COL_BORDER,
154 COL_BLUE,
155 NCOLOURS
156};
157
c71454c0 158enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
1482ee76 159
03f856c4 160#define GRID_SCALE 48.0F
8c1fd974 161#define ROLLTIME 0.13F
1482ee76 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
175struct grid_square {
176 float x, y;
177 int npoints;
178 float points[8]; /* maximum */
c71454c0 179 int directions[8]; /* bit masks showing point pairs */
1482ee76 180 int flip;
181 int blue;
182 int tetra_class;
183};
184
185struct 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
197struct 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
be8d5aa1 214static game_params *default_params(void)
1482ee76 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
be8d5aa1 225static int game_fetch_preset(int i, char **name, game_params **params)
eb2ad6f1 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;
c8230524 240 ret->d1 = 1;
241 ret->d2 = 2;
eb2ad6f1 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
be8d5aa1 265static void free_params(game_params *params)
1482ee76 266{
267 sfree(params);
268}
269
be8d5aa1 270static game_params *dup_params(game_params *params)
eb2ad6f1 271{
272 game_params *ret = snew(game_params);
273 *ret = *params; /* structure copy */
274 return ret;
275}
276
be8d5aa1 277static game_params *decode_params(char const *string)
b0e26073 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
be8d5aa1 298static char *encode_params(game_params *params)
b0e26073 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
1482ee76 308static 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
5928817c 317 for (y = 0; y < params->d2; y++)
318 for (x = 0; x < params->d1; x++) {
1482ee76 319 struct grid_square sq;
320
03f856c4 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;
1482ee76 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 */
c71454c0 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 */
1482ee76 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;
03f856c4 354 float theight = (float)(sqrt(3) / 2.0);
1482ee76 355
356 for (row = 0; row < params->d1 + params->d2; row++) {
c8230524 357 if (row < params->d2) {
1482ee76 358 other = +1;
c8230524 359 rowlen = row + params->d1;
1482ee76 360 } else {
361 other = -1;
c8230524 362 rowlen = 2*params->d2 + params->d1 - row;
1482ee76 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));
03f856c4 374 x = ix * 0.5F;
1482ee76 375 y = theight * row;
376 sq.x = x;
377 sq.y = y + theight / 3;
03f856c4 378 sq.points[0] = x - 0.5F;
1482ee76 379 sq.points[1] = y;
380 sq.points[2] = x;
381 sq.points[3] = y + theight;
03f856c4 382 sq.points[4] = x + 0.5F;
1482ee76 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
c71454c0 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
1482ee76 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));
03f856c4 419 x = ix * 0.5F;
1482ee76 420 y = theight * row;
421 sq.x = x;
422 sq.y = y + 2*theight / 3;
03f856c4 423 sq.points[0] = x + 0.5F;
1482ee76 424 sq.points[1] = y + theight;
425 sq.points[2] = x;
426 sq.points[3] = y;
03f856c4 427 sq.points[4] = x - 0.5F;
1482ee76 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
c71454c0 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
1482ee76 445 sq.flip = FALSE;
446
447 if (firstix < 0)
c8230524 448 firstix = (ix - 1) & 3;
1482ee76 449 ix -= firstix;
450 sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
451
452 callback(ctx, &sq);
453 }
454 }
455 }
456}
457
458static 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
be8d5aa1 476static config_item *game_configure(game_params *params)
c8230524 477{
478 config_item *ret = snewn(4, config_item);
479 char buf[80];
480
481 ret[0].name = "Type of solid";
95709966 482 ret[0].type = C_CHOICES;
c8230524 483 ret[0].sval = ":Tetrahedron:Cube:Octahedron:Icosahedron";
484 ret[0].ival = params->solid;
485
486 ret[1].name = "Width / top";
95709966 487 ret[1].type = C_STRING;
c8230524 488 sprintf(buf, "%d", params->d1);
489 ret[1].sval = dupstr(buf);
490 ret[1].ival = 0;
491
492 ret[2].name = "Height / bottom";
95709966 493 ret[2].type = C_STRING;
c8230524 494 sprintf(buf, "%d", params->d2);
495 ret[2].sval = dupstr(buf);
496 ret[2].ival = 0;
497
498 ret[3].name = NULL;
95709966 499 ret[3].type = C_END;
c8230524 500 ret[3].sval = NULL;
501 ret[3].ival = 0;
502
503 return ret;
504}
505
be8d5aa1 506static game_params *custom_params(config_item *cfg)
c8230524 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
517static 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
be8d5aa1 532static char *validate_params(game_params *params)
c8230524 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
1482ee76 569struct grid_data {
570 int *gridptrs[4];
571 int nsquares[4];
572 int nclasses;
573 int squareindex;
574};
575
576static 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
be8d5aa1 592static char *new_game_seed(game_params *params, random_state *rs)
1482ee76 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++) {
48d70ca9 637 int n = random_upto(rs, data.nsquares[i]);
1482ee76 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 */
4efb3868 647 while (n < data.nsquares[i]-1) {
1482ee76 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 */
b0e26073 685 sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]);
1482ee76 686
687 sfree(data.gridptrs[0]);
688 sfree(flags);
689
690 return seed;
691}
692
693static 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
702static 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
726static 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
774static 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
790static 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
03f856c4 816 ax = (float)cos(angle);
817 ay = (float)sin(angle);
1482ee76 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
be8d5aa1 841static char *validate_seed(game_params *params, char *seed)
5928817c 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
b0e26073 856 if (seed[i] != ',')
857 return "Expected ',' after hex digits";
5928817c 858
859 i++;
860 do {
861 if (seed[i] < '0' || seed[i] > '9')
b0e26073 862 return "Expected decimal integer after ','";
5928817c 863 i++;
864 } while (seed[i]);
865
866 return NULL;
867}
868
be8d5aa1 869static game_state *new_game(game_params *params, char *seed)
1482ee76 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
b0e26073 915 if (*p == ',')
1482ee76 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;
fd1a1a2b 942 state->completed = 0;
1482ee76 943 state->movecount = 0;
944
945 return state;
946}
947
be8d5aa1 948static game_state *dup_game(game_state *state)
1482ee76 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
be8d5aa1 977static void free_game(game_state *state)
1482ee76 978{
979 sfree(state);
980}
981
be8d5aa1 982static game_ui *new_ui(game_state *state)
74a4e547 983{
984 return NULL;
985}
986
be8d5aa1 987static void free_ui(game_ui *ui)
74a4e547 988{
989}
990
be8d5aa1 991static game_state *make_move(game_state *from, game_ui *ui,
992 int x, int y, int button)
1482ee76 993{
994 int direction;
995 int pkey[2], skey[2], dkey[2];
996 float points[4];
997 game_state *ret;
998 float angle;
999 int i, j, dest, mask;
1000 struct solid *poly;
1001
1002 /*
1003 * All moves are made with the cursor keys.
1004 */
1005 if (button == CURSOR_UP)
1006 direction = UP;
1007 else if (button == CURSOR_DOWN)
1008 direction = DOWN;
1009 else if (button == CURSOR_LEFT)
1010 direction = LEFT;
1011 else if (button == CURSOR_RIGHT)
1012 direction = RIGHT;
c71454c0 1013 else if (button == CURSOR_UP_LEFT)
1014 direction = UP_LEFT;
1015 else if (button == CURSOR_DOWN_LEFT)
1016 direction = DOWN_LEFT;
1017 else if (button == CURSOR_UP_RIGHT)
1018 direction = UP_RIGHT;
1019 else if (button == CURSOR_DOWN_RIGHT)
1020 direction = DOWN_RIGHT;
1482ee76 1021 else
1022 return NULL;
1023
1024 /*
1025 * Find the two points in the current grid square which
1026 * correspond to this move.
1027 */
1028 mask = from->squares[from->current].directions[direction];
1029 if (mask == 0)
1030 return NULL;
1031 for (i = j = 0; i < from->squares[from->current].npoints; i++)
1032 if (mask & (1 << i)) {
1033 points[j*2] = from->squares[from->current].points[i*2];
1034 points[j*2+1] = from->squares[from->current].points[i*2+1];
1035 skey[j] = i;
1036 j++;
1037 }
1038 assert(j == 2);
1039
1040 /*
1041 * Now find the other grid square which shares those points.
1042 * This is our move destination.
1043 */
1044 dest = -1;
1045 for (i = 0; i < from->nsquares; i++)
1046 if (i != from->current) {
1047 int match = 0;
1048 float dist;
1049
1050 for (j = 0; j < from->squares[i].npoints; j++) {
1051 dist = (SQ(from->squares[i].points[j*2] - points[0]) +
1052 SQ(from->squares[i].points[j*2+1] - points[1]));
1053 if (dist < 0.1)
1054 dkey[match++] = j;
1055 dist = (SQ(from->squares[i].points[j*2] - points[2]) +
1056 SQ(from->squares[i].points[j*2+1] - points[3]));
1057 if (dist < 0.1)
1058 dkey[match++] = j;
1059 }
1060
1061 if (match == 2) {
1062 dest = i;
1063 break;
1064 }
1065 }
1066
1067 if (dest < 0)
1068 return NULL;
1069
1070 ret = dup_game(from);
1071 ret->current = i;
1072
1073 /*
1074 * So we know what grid square we're aiming for, and we also
1075 * know the two key points (as indices in both the source and
1076 * destination grid squares) which are invariant between source
1077 * and destination.
1078 *
1079 * Next we must roll the polyhedron on to that square. So we
1080 * find the indices of the key points within the polyhedron's
1081 * vertex array, then use those in a call to transform_poly,
1082 * and align the result on the new grid square.
1083 */
1084 {
1085 int all_pkey[4];
1086 align_poly(from->solid, &from->squares[from->current], all_pkey);
1087 pkey[0] = all_pkey[skey[0]];
1088 pkey[1] = all_pkey[skey[1]];
1089 /*
1090 * Now pkey[0] corresponds to skey[0] and dkey[0], and
1091 * likewise [1].
1092 */
1093 }
1094
1095 /*
1096 * Now find the angle through which to rotate the polyhedron.
1097 * Do this by finding the two faces that share the two vertices
1098 * we've found, and taking the dot product of their normals.
1099 */
1100 {
1101 int f[2], nf = 0;
1102 float dp;
1103
1104 for (i = 0; i < from->solid->nfaces; i++) {
1105 int match = 0;
1106 for (j = 0; j < from->solid->order; j++)
1107 if (from->solid->faces[i*from->solid->order + j] == pkey[0] ||
1108 from->solid->faces[i*from->solid->order + j] == pkey[1])
1109 match++;
1110 if (match == 2) {
1111 assert(nf < 2);
1112 f[nf++] = i;
1113 }
1114 }
1115
1116 assert(nf == 2);
1117
1118 dp = 0;
1119 for (i = 0; i < 3; i++)
1120 dp += (from->solid->normals[f[0]*3+i] *
1121 from->solid->normals[f[1]*3+i]);
03f856c4 1122 angle = (float)acos(dp);
1482ee76 1123 }
1124
1125 /*
1126 * Now transform the polyhedron. We aren't entirely sure
1127 * whether we need to rotate through angle or -angle, and the
1128 * simplest way round this is to try both and see which one
1129 * aligns successfully!
1130 *
1131 * Unfortunately, _both_ will align successfully if this is a
1132 * cube, which won't tell us anything much. So for that
1133 * particular case, I resort to gross hackery: I simply negate
1134 * the angle before trying the alignment, depending on the
1135 * direction. Which directions work which way is determined by
1136 * pure trial and error. I said it was gross :-/
1137 */
1138 {
1139 int all_pkey[4];
1140 int success;
1141
1142 if (from->solid->order == 4 && direction == UP)
1143 angle = -angle; /* HACK */
1144
1145 poly = transform_poly(from->solid,
1146 from->squares[from->current].flip,
1147 pkey[0], pkey[1], angle);
1148 flip_poly(poly, from->squares[ret->current].flip);
1149 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1150
1151 if (!success) {
1152 angle = -angle;
1153 poly = transform_poly(from->solid,
1154 from->squares[from->current].flip,
1155 pkey[0], pkey[1], angle);
1156 flip_poly(poly, from->squares[ret->current].flip);
1157 success = align_poly(poly, &from->squares[ret->current], all_pkey);
1158 }
1159
1160 assert(success);
1161 }
1162
1163 /*
1164 * Now we have our rotated polyhedron, which we expect to be
1165 * exactly congruent to the one we started with - but with the
1166 * faces permuted. So we map that congruence and thereby figure
1167 * out how to permute the faces as a result of the polyhedron
1168 * having rolled.
1169 */
1170 {
1171 int *newcolours = snewn(from->solid->nfaces, int);
1172
1173 for (i = 0; i < from->solid->nfaces; i++)
1174 newcolours[i] = -1;
1175
1176 for (i = 0; i < from->solid->nfaces; i++) {
1177 int nmatch = 0;
1178
1179 /*
1180 * Now go through the transformed polyhedron's faces
1181 * and figure out which one's normal is approximately
1182 * equal to this one.
1183 */
1184 for (j = 0; j < poly->nfaces; j++) {
1185 float dist;
1186 int k;
1187
1188 dist = 0;
1189
1190 for (k = 0; k < 3; k++)
1191 dist += SQ(poly->normals[j*3+k] -
1192 from->solid->normals[i*3+k]);
1193
1194 if (APPROXEQ(dist, 0)) {
1195 nmatch++;
1196 newcolours[i] = ret->facecolours[j];
1197 }
1198 }
1199
1200 assert(nmatch == 1);
1201 }
1202
1203 for (i = 0; i < from->solid->nfaces; i++)
1204 assert(newcolours[i] != -1);
1205
1206 sfree(ret->facecolours);
1207 ret->facecolours = newcolours;
1208 }
1209
ccd4e210 1210 ret->movecount++;
1211
1482ee76 1212 /*
1213 * And finally, swap the colour between the bottom face of the
1214 * polyhedron and the face we've just landed on.
1215 *
1216 * We don't do this if the game is already complete, since we
1217 * allow the user to roll the fully blue polyhedron around the
1218 * grid as a feeble reward.
1219 */
1220 if (!ret->completed) {
1221 i = lowest_face(from->solid);
1222 j = ret->facecolours[i];
1223 ret->facecolours[i] = ret->squares[ret->current].blue;
1224 ret->squares[ret->current].blue = j;
1225
1226 /*
1227 * Detect game completion.
1228 */
1229 j = 0;
1230 for (i = 0; i < ret->solid->nfaces; i++)
1231 if (ret->facecolours[i])
1232 j++;
1233 if (j == ret->solid->nfaces)
fd1a1a2b 1234 ret->completed = ret->movecount;
1482ee76 1235 }
1236
1237 sfree(poly);
1238
1239 /*
1240 * Align the normal polyhedron with its grid square, to get key
1241 * points for non-animated display.
1242 */
1243 {
1244 int pkey[4];
1245 int success;
1246
1247 success = align_poly(ret->solid, &ret->squares[ret->current], pkey);
1248 assert(success);
1249
1250 ret->dpkey[0] = pkey[0];
1251 ret->dpkey[1] = pkey[1];
1252 ret->dgkey[0] = 0;
1253 ret->dgkey[1] = 1;
1254 }
1255
1256
1257 ret->spkey[0] = pkey[0];
1258 ret->spkey[1] = pkey[1];
1259 ret->sgkey[0] = skey[0];
1260 ret->sgkey[1] = skey[1];
1261 ret->previous = from->current;
1262 ret->angle = angle;
1482ee76 1263
1264 return ret;
1265}
1266
1267/* ----------------------------------------------------------------------
1268 * Drawing routines.
1269 */
1270
1271struct bbox {
1272 float l, r, u, d;
1273};
1274
1275struct game_drawstate {
1276 int ox, oy; /* pixel position of float origin */
1277};
1278
1279static void find_bbox_callback(void *ctx, struct grid_square *sq)
1280{
1281 struct bbox *bb = (struct bbox *)ctx;
1282 int i;
1283
1284 for (i = 0; i < sq->npoints; i++) {
1285 if (bb->l > sq->points[i*2]) bb->l = sq->points[i*2];
1286 if (bb->r < sq->points[i*2]) bb->r = sq->points[i*2];
1287 if (bb->u > sq->points[i*2+1]) bb->u = sq->points[i*2+1];
1288 if (bb->d < sq->points[i*2+1]) bb->d = sq->points[i*2+1];
1289 }
1290}
1291
1292static struct bbox find_bbox(game_params *params)
1293{
1294 struct bbox bb;
1295
1296 /*
1297 * These should be hugely more than the real bounding box will
1298 * be.
1299 */
03f856c4 1300 bb.l = 2.0F * (params->d1 + params->d2);
1301 bb.r = -2.0F * (params->d1 + params->d2);
1302 bb.u = 2.0F * (params->d1 + params->d2);
1303 bb.d = -2.0F * (params->d1 + params->d2);
1482ee76 1304 enum_grid_squares(params, find_bbox_callback, &bb);
1305
1306 return bb;
1307}
1308
be8d5aa1 1309static void game_size(game_params *params, int *x, int *y)
1482ee76 1310{
1311 struct bbox bb = find_bbox(params);
03f856c4 1312 *x = (int)((bb.r - bb.l + 2*solids[params->solid]->border) * GRID_SCALE);
1313 *y = (int)((bb.d - bb.u + 2*solids[params->solid]->border) * GRID_SCALE);
1482ee76 1314}
1315
be8d5aa1 1316static float *game_colours(frontend *fe, game_state *state, int *ncolours)
1482ee76 1317{
1318 float *ret = snewn(3 * NCOLOURS, float);
1319
1320 frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]);
1321
1322 ret[COL_BORDER * 3 + 0] = 0.0;
1323 ret[COL_BORDER * 3 + 1] = 0.0;
1324 ret[COL_BORDER * 3 + 2] = 0.0;
1325
1326 ret[COL_BLUE * 3 + 0] = 0.0;
1327 ret[COL_BLUE * 3 + 1] = 0.0;
1328 ret[COL_BLUE * 3 + 2] = 1.0;
1329
1330 *ncolours = NCOLOURS;
1331 return ret;
1332}
1333
be8d5aa1 1334static game_drawstate *game_new_drawstate(game_state *state)
1482ee76 1335{
1336 struct game_drawstate *ds = snew(struct game_drawstate);
1337 struct bbox bb = find_bbox(&state->params);
1338
03f856c4 1339 ds->ox = (int)(-(bb.l - state->solid->border) * GRID_SCALE);
1340 ds->oy = (int)(-(bb.u - state->solid->border) * GRID_SCALE);
1482ee76 1341
1342 return ds;
1343}
1344
be8d5aa1 1345static void game_free_drawstate(game_drawstate *ds)
1482ee76 1346{
1347 sfree(ds);
1348}
1349
be8d5aa1 1350static void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate,
c822de4a 1351 game_state *state, int dir, game_ui *ui,
74a4e547 1352 float animtime, float flashtime)
1482ee76 1353{
1354 int i, j;
1355 struct bbox bb = find_bbox(&state->params);
1356 struct solid *poly;
1357 int *pkey, *gkey;
1358 float t[3];
1359 float angle;
1360 game_state *newstate;
1361 int square;
1362
03f856c4 1363 draw_rect(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1364 (int)((bb.d-bb.u+2.0F) * GRID_SCALE), COL_BACKGROUND);
1482ee76 1365
5b5c6b12 1366 if (dir < 0) {
1482ee76 1367 game_state *t;
1368
1369 /*
1370 * This is an Undo. So reverse the order of the states, and
1371 * run the roll timer backwards.
1372 */
5b5c6b12 1373 assert(oldstate);
1374
1482ee76 1375 t = oldstate;
1376 oldstate = state;
1377 state = t;
1378
1379 animtime = ROLLTIME - animtime;
1380 }
1381
1382 if (!oldstate) {
1383 oldstate = state;
1384 angle = 0.0;
1385 square = state->current;
1386 pkey = state->dpkey;
1387 gkey = state->dgkey;
1388 } else {
1389 angle = state->angle * animtime / ROLLTIME;
1390 square = state->previous;
1391 pkey = state->spkey;
1392 gkey = state->sgkey;
1393 }
1394 newstate = state;
1395 state = oldstate;
1396
1397 for (i = 0; i < state->nsquares; i++) {
1398 int coords[8];
1399
1400 for (j = 0; j < state->squares[i].npoints; j++) {
03f856c4 1401 coords[2*j] = ((int)(state->squares[i].points[2*j] * GRID_SCALE)
1402 + ds->ox);
1403 coords[2*j+1] = ((int)(state->squares[i].points[2*j+1]*GRID_SCALE)
1404 + ds->oy);
1482ee76 1405 }
1406
1407 draw_polygon(fe, coords, state->squares[i].npoints, TRUE,
1408 state->squares[i].blue ? COL_BLUE : COL_BACKGROUND);
1409 draw_polygon(fe, coords, state->squares[i].npoints, FALSE, COL_BORDER);
1410 }
1411
1412 /*
1413 * Now compute and draw the polyhedron.
1414 */
1415 poly = transform_poly(state->solid, state->squares[square].flip,
1416 pkey[0], pkey[1], angle);
1417
1418 /*
1419 * Compute the translation required to align the two key points
1420 * on the polyhedron with the same key points on the current
1421 * face.
1422 */
1423 for (i = 0; i < 3; i++) {
1424 float tc = 0.0;
1425
1426 for (j = 0; j < 2; j++) {
1427 float grid_coord;
1428
1429 if (i < 2) {
1430 grid_coord =
1431 state->squares[square].points[gkey[j]*2+i];
1432 } else {
1433 grid_coord = 0.0;
1434 }
1435
1436 tc += (grid_coord - poly->vertices[pkey[j]*3+i]);
1437 }
1438
1439 t[i] = tc / 2;
1440 }
1441 for (i = 0; i < poly->nvertices; i++)
1442 for (j = 0; j < 3; j++)
1443 poly->vertices[i*3+j] += t[j];
1444
1445 /*
1446 * Now actually draw each face.
1447 */
1448 for (i = 0; i < poly->nfaces; i++) {
1449 float points[8];
1450 int coords[8];
1451
1452 for (j = 0; j < poly->order; j++) {
1453 int f = poly->faces[i*poly->order + j];
1454 points[j*2] = (poly->vertices[f*3+0] -
1455 poly->vertices[f*3+2] * poly->shear);
1456 points[j*2+1] = (poly->vertices[f*3+1] -
1457 poly->vertices[f*3+2] * poly->shear);
1458 }
1459
1460 for (j = 0; j < poly->order; j++) {
962dcf9a 1461 coords[j*2] = (int)floor(points[j*2] * GRID_SCALE) + ds->ox;
1462 coords[j*2+1] = (int)floor(points[j*2+1] * GRID_SCALE) + ds->oy;
1482ee76 1463 }
1464
1465 /*
1466 * Find out whether these points are in a clockwise or
1467 * anticlockwise arrangement. If the latter, discard the
1468 * face because it's facing away from the viewer.
1469 *
1470 * This would involve fiddly winding-number stuff for a
1471 * general polygon, but for the simple parallelograms we'll
1472 * be seeing here, all we have to do is check whether the
1473 * corners turn right or left. So we'll take the vector
1474 * from point 0 to point 1, turn it right 90 degrees,
1475 * and check the sign of the dot product with that and the
1476 * next vector (point 1 to point 2).
1477 */
1478 {
1479 float v1x = points[2]-points[0];
1480 float v1y = points[3]-points[1];
1481 float v2x = points[4]-points[2];
1482 float v2y = points[5]-points[3];
1483 float dp = v1x * v2y - v1y * v2x;
1484
1485 if (dp <= 0)
1486 continue;
1487 }
1488
1489 draw_polygon(fe, coords, poly->order, TRUE,
1490 state->facecolours[i] ? COL_BLUE : COL_BACKGROUND);
1491 draw_polygon(fe, coords, poly->order, FALSE, COL_BORDER);
1492 }
1493 sfree(poly);
1494
03f856c4 1495 draw_update(fe, 0, 0, (int)((bb.r-bb.l+2.0F) * GRID_SCALE),
1496 (int)((bb.d-bb.u+2.0F) * GRID_SCALE));
fd1a1a2b 1497
1498 /*
1499 * Update the status bar.
1500 */
1501 {
1502 char statusbuf[256];
1503
1504 sprintf(statusbuf, "%sMoves: %d",
1505 (state->completed ? "COMPLETED! " : ""),
1506 (state->completed ? state->completed : state->movecount));
1507
1508 status_bar(fe, statusbuf);
1509 }
1482ee76 1510}
1511
be8d5aa1 1512static float game_anim_length(game_state *oldstate,
1513 game_state *newstate, int dir)
1482ee76 1514{
1515 return ROLLTIME;
1516}
87ed82be 1517
be8d5aa1 1518static float game_flash_length(game_state *oldstate,
1519 game_state *newstate, int dir)
87ed82be 1520{
1521 return 0.0F;
1522}
fd1a1a2b 1523
be8d5aa1 1524static int game_wants_statusbar(void)
fd1a1a2b 1525{
1526 return TRUE;
1527}
be8d5aa1 1528
1529#ifdef COMBINED
1530#define thegame cube
1531#endif
1532
1533const struct game thegame = {
1534 "Cube", "games.cube", TRUE,
1535 default_params,
1536 game_fetch_preset,
1537 decode_params,
1538 encode_params,
1539 free_params,
1540 dup_params,
1541 game_configure,
1542 custom_params,
1543 validate_params,
1544 new_game_seed,
1545 validate_seed,
1546 new_game,
1547 dup_game,
1548 free_game,
1549 new_ui,
1550 free_ui,
1551 make_move,
1552 game_size,
1553 game_colours,
1554 game_new_drawstate,
1555 game_free_drawstate,
1556 game_redraw,
1557 game_anim_length,
1558 game_flash_length,
1559 game_wants_statusbar,
1560};