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