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