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 | |
c71454c0 |
153 | enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT }; |
1482ee76 |
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 */ |
c71454c0 |
174 | int directions[8]; /* bit masks showing point pairs */ |
1482ee76 |
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 */ |
c71454c0 |
301 | sq.directions[UP_LEFT] = 0; /* no diagonals in a square */ |
302 | sq.directions[UP_RIGHT] = 0; /* no diagonals in a square */ |
303 | sq.directions[DOWN_LEFT] = 0; /* no diagonals in a square */ |
304 | sq.directions[DOWN_RIGHT] = 0; /* no diagonals in a square */ |
1482ee76 |
305 | |
306 | sq.flip = FALSE; |
307 | |
308 | /* |
309 | * This is supremely irrelevant, but just to avoid |
310 | * having any uninitialised structure members... |
311 | */ |
312 | sq.tetra_class = 0; |
313 | |
314 | callback(ctx, &sq); |
315 | } |
316 | } else { |
317 | int row, rowlen, other, i, firstix = -1; |
318 | float theight = sqrt(3) / 2.0; |
319 | |
320 | for (row = 0; row < params->d1 + params->d2; row++) { |
321 | if (row < params->d1) { |
322 | other = +1; |
323 | rowlen = row + params->d2; |
324 | } else { |
325 | other = -1; |
326 | rowlen = 2*params->d1 + params->d2 - row; |
327 | } |
328 | |
329 | /* |
330 | * There are `rowlen' down-pointing triangles. |
331 | */ |
332 | for (i = 0; i < rowlen; i++) { |
333 | struct grid_square sq; |
334 | int ix; |
335 | float x, y; |
336 | |
337 | ix = (2 * i - (rowlen-1)); |
338 | x = ix * 0.5; |
339 | y = theight * row; |
340 | sq.x = x; |
341 | sq.y = y + theight / 3; |
342 | sq.points[0] = x - 0.5; |
343 | sq.points[1] = y; |
344 | sq.points[2] = x; |
345 | sq.points[3] = y + theight; |
346 | sq.points[4] = x + 0.5; |
347 | sq.points[5] = y; |
348 | sq.npoints = 3; |
349 | |
350 | sq.directions[LEFT] = 0x03; /* 0,1 */ |
351 | sq.directions[RIGHT] = 0x06; /* 1,2 */ |
352 | sq.directions[UP] = 0x05; /* 0,2 */ |
353 | sq.directions[DOWN] = 0; /* invalid move */ |
354 | |
c71454c0 |
355 | /* |
356 | * Down-pointing triangle: both the up diagonals go |
357 | * up, and the down ones go left and right. |
358 | */ |
359 | sq.directions[UP_LEFT] = sq.directions[UP_RIGHT] = |
360 | sq.directions[UP]; |
361 | sq.directions[DOWN_LEFT] = sq.directions[LEFT]; |
362 | sq.directions[DOWN_RIGHT] = sq.directions[RIGHT]; |
363 | |
1482ee76 |
364 | sq.flip = TRUE; |
365 | |
366 | if (firstix < 0) |
367 | firstix = ix & 3; |
368 | ix -= firstix; |
369 | sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3); |
370 | |
371 | callback(ctx, &sq); |
372 | } |
373 | |
374 | /* |
375 | * There are `rowlen+other' up-pointing triangles. |
376 | */ |
377 | for (i = 0; i < rowlen+other; i++) { |
378 | struct grid_square sq; |
379 | int ix; |
380 | float x, y; |
381 | |
382 | ix = (2 * i - (rowlen+other-1)); |
383 | x = ix * 0.5; |
384 | y = theight * row; |
385 | sq.x = x; |
386 | sq.y = y + 2*theight / 3; |
387 | sq.points[0] = x + 0.5; |
388 | sq.points[1] = y + theight; |
389 | sq.points[2] = x; |
390 | sq.points[3] = y; |
391 | sq.points[4] = x - 0.5; |
392 | sq.points[5] = y + theight; |
393 | sq.npoints = 3; |
394 | |
395 | sq.directions[LEFT] = 0x06; /* 1,2 */ |
396 | sq.directions[RIGHT] = 0x03; /* 0,1 */ |
397 | sq.directions[DOWN] = 0x05; /* 0,2 */ |
398 | sq.directions[UP] = 0; /* invalid move */ |
399 | |
c71454c0 |
400 | /* |
401 | * Up-pointing triangle: both the down diagonals go |
402 | * down, and the up ones go left and right. |
403 | */ |
404 | sq.directions[DOWN_LEFT] = sq.directions[DOWN_RIGHT] = |
405 | sq.directions[DOWN]; |
406 | sq.directions[UP_LEFT] = sq.directions[LEFT]; |
407 | sq.directions[UP_RIGHT] = sq.directions[RIGHT]; |
408 | |
1482ee76 |
409 | sq.flip = FALSE; |
410 | |
411 | if (firstix < 0) |
412 | firstix = ix; |
413 | ix -= firstix; |
414 | sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3); |
415 | |
416 | callback(ctx, &sq); |
417 | } |
418 | } |
419 | } |
420 | } |
421 | |
422 | static int grid_area(int d1, int d2, int order) |
423 | { |
424 | /* |
425 | * An NxM grid of squares has NM squares in it. |
426 | * |
427 | * A grid of triangles with dimensions A and B has a total of |
428 | * A^2 + B^2 + 4AB triangles in it. (You can divide it up into |
429 | * a side-A triangle containing A^2 subtriangles, a side-B |
430 | * triangle containing B^2, and two congruent parallelograms, |
431 | * each with side lengths A and B, each therefore containing AB |
432 | * two-triangle rhombuses.) |
433 | */ |
434 | if (order == 4) |
435 | return d1 * d2; |
436 | else |
437 | return d1*d1 + d2*d2 + 4*d1*d2; |
438 | } |
439 | |
440 | struct grid_data { |
441 | int *gridptrs[4]; |
442 | int nsquares[4]; |
443 | int nclasses; |
444 | int squareindex; |
445 | }; |
446 | |
447 | static void classify_grid_square_callback(void *ctx, struct grid_square *sq) |
448 | { |
449 | struct grid_data *data = (struct grid_data *)ctx; |
450 | int thisclass; |
451 | |
452 | if (data->nclasses == 4) |
453 | thisclass = sq->tetra_class; |
454 | else if (data->nclasses == 2) |
455 | thisclass = sq->flip; |
456 | else |
457 | thisclass = 0; |
458 | |
459 | data->gridptrs[thisclass][data->nsquares[thisclass]++] = |
460 | data->squareindex++; |
461 | } |
462 | |
463 | char *new_game_seed(game_params *params) |
464 | { |
465 | struct grid_data data; |
466 | int i, j, k, m, area, facesperclass; |
467 | int *flags; |
468 | char *seed, *p; |
469 | |
470 | /* |
471 | * Enumerate the grid squares, dividing them into equivalence |
472 | * classes as appropriate. (For the tetrahedron, there is one |
473 | * equivalence class for each face; for the octahedron there |
474 | * are two classes; for the other two solids there's only one.) |
475 | */ |
476 | |
477 | area = grid_area(params->d1, params->d2, solids[params->solid]->order); |
478 | if (params->solid == TETRAHEDRON) |
479 | data.nclasses = 4; |
480 | else if (params->solid == OCTAHEDRON) |
481 | data.nclasses = 2; |
482 | else |
483 | data.nclasses = 1; |
484 | data.gridptrs[0] = snewn(data.nclasses * area, int); |
485 | for (i = 0; i < data.nclasses; i++) { |
486 | data.gridptrs[i] = data.gridptrs[0] + i * area; |
487 | data.nsquares[i] = 0; |
488 | } |
489 | data.squareindex = 0; |
490 | enum_grid_squares(params, classify_grid_square_callback, &data); |
491 | |
492 | facesperclass = solids[params->solid]->nfaces / data.nclasses; |
493 | |
494 | for (i = 0; i < data.nclasses; i++) |
495 | assert(data.nsquares[i] >= facesperclass); |
496 | assert(data.squareindex == area); |
497 | |
498 | /* |
499 | * So now we know how many faces to allocate in each class. Get |
500 | * on with it. |
501 | */ |
502 | flags = snewn(area, int); |
503 | for (i = 0; i < area; i++) |
504 | flags[i] = FALSE; |
505 | |
506 | for (i = 0; i < data.nclasses; i++) { |
507 | for (j = 0; j < facesperclass; j++) { |
508 | unsigned long divisor = RAND_MAX / data.nsquares[i]; |
509 | unsigned long max = divisor * data.nsquares[i]; |
510 | int n; |
511 | |
512 | do { |
513 | n = rand(); |
514 | } while (n >= max); |
515 | |
516 | n /= divisor; |
517 | |
518 | assert(!flags[data.gridptrs[i][n]]); |
519 | flags[data.gridptrs[i][n]] = TRUE; |
520 | |
521 | /* |
522 | * Move everything else up the array. I ought to use a |
523 | * better data structure for this, but for such small |
524 | * numbers it hardly seems worth the effort. |
525 | */ |
526 | while (n < data.nsquares[i]-1) { |
527 | data.gridptrs[i][n] = data.gridptrs[i][n+1]; |
528 | n++; |
529 | } |
530 | data.nsquares[i]--; |
531 | } |
532 | } |
533 | |
534 | /* |
535 | * Now we know precisely which squares are blue. Encode this |
536 | * information in hex. While we're looping over this, collect |
537 | * the non-blue squares into a list in the now-unused gridptrs |
538 | * array. |
539 | */ |
540 | seed = snewn(area / 4 + 40, char); |
541 | p = seed; |
542 | j = 0; |
543 | k = 8; |
544 | m = 0; |
545 | for (i = 0; i < area; i++) { |
546 | if (flags[i]) { |
547 | j |= k; |
548 | } else { |
549 | data.gridptrs[0][m++] = i; |
550 | } |
551 | k >>= 1; |
552 | if (!k) { |
553 | *p++ = "0123456789ABCDEF"[j]; |
554 | k = 8; |
555 | j = 0; |
556 | } |
557 | } |
558 | if (k != 8) |
559 | *p++ = "0123456789ABCDEF"[j]; |
560 | |
561 | /* |
562 | * Choose a non-blue square for the polyhedron. |
563 | */ |
564 | { |
565 | unsigned long divisor = RAND_MAX / m; |
566 | unsigned long max = divisor * m; |
567 | int n; |
568 | |
569 | do { |
570 | n = rand(); |
571 | } while (n >= max); |
572 | |
573 | n /= divisor; |
574 | |
575 | sprintf(p, ":%d", data.gridptrs[0][n]); |
576 | } |
577 | |
578 | sfree(data.gridptrs[0]); |
579 | sfree(flags); |
580 | |
581 | return seed; |
582 | } |
583 | |
584 | static void add_grid_square_callback(void *ctx, struct grid_square *sq) |
585 | { |
586 | game_state *state = (game_state *)ctx; |
587 | |
588 | state->squares[state->nsquares] = *sq; /* structure copy */ |
589 | state->squares[state->nsquares].blue = FALSE; |
590 | state->nsquares++; |
591 | } |
592 | |
593 | static int lowest_face(const struct solid *solid) |
594 | { |
595 | int i, j, best; |
596 | float zmin; |
597 | |
598 | best = 0; |
599 | zmin = 0.0; |
600 | for (i = 0; i < solid->nfaces; i++) { |
601 | float z = 0; |
602 | |
603 | for (j = 0; j < solid->order; j++) { |
604 | int f = solid->faces[i*solid->order + j]; |
605 | z += solid->vertices[f*3+2]; |
606 | } |
607 | |
608 | if (i == 0 || zmin > z) { |
609 | zmin = z; |
610 | best = i; |
611 | } |
612 | } |
613 | |
614 | return best; |
615 | } |
616 | |
617 | static int align_poly(const struct solid *solid, struct grid_square *sq, |
618 | int *pkey) |
619 | { |
620 | float zmin; |
621 | int i, j; |
622 | int flip = (sq->flip ? -1 : +1); |
623 | |
624 | /* |
625 | * First, find the lowest z-coordinate present in the solid. |
626 | */ |
627 | zmin = 0.0; |
628 | for (i = 0; i < solid->nvertices; i++) |
629 | if (zmin > solid->vertices[i*3+2]) |
630 | zmin = solid->vertices[i*3+2]; |
631 | |
632 | /* |
633 | * Now go round the grid square. For each point in the grid |
634 | * square, we're looking for a point of the polyhedron with the |
635 | * same x- and y-coordinates (relative to the square's centre), |
636 | * and z-coordinate equal to zmin (near enough). |
637 | */ |
638 | for (j = 0; j < sq->npoints; j++) { |
639 | int matches, index; |
640 | |
641 | matches = 0; |
642 | index = -1; |
643 | |
644 | for (i = 0; i < solid->nvertices; i++) { |
645 | float dist = 0; |
646 | |
647 | dist += SQ(solid->vertices[i*3+0] * flip - sq->points[j*2+0] + sq->x); |
648 | dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y); |
649 | dist += SQ(solid->vertices[i*3+2] - zmin); |
650 | |
651 | if (dist < 0.1) { |
652 | matches++; |
653 | index = i; |
654 | } |
655 | } |
656 | |
657 | if (matches != 1 || index < 0) |
658 | return FALSE; |
659 | pkey[j] = index; |
660 | } |
661 | |
662 | return TRUE; |
663 | } |
664 | |
665 | static void flip_poly(struct solid *solid, int flip) |
666 | { |
667 | int i; |
668 | |
669 | if (flip) { |
670 | for (i = 0; i < solid->nvertices; i++) { |
671 | solid->vertices[i*3+0] *= -1; |
672 | solid->vertices[i*3+1] *= -1; |
673 | } |
674 | for (i = 0; i < solid->nfaces; i++) { |
675 | solid->normals[i*3+0] *= -1; |
676 | solid->normals[i*3+1] *= -1; |
677 | } |
678 | } |
679 | } |
680 | |
681 | static struct solid *transform_poly(const struct solid *solid, int flip, |
682 | int key0, int key1, float angle) |
683 | { |
684 | struct solid *ret = snew(struct solid); |
685 | float vx, vy, ax, ay; |
686 | float vmatrix[9], amatrix[9], vmatrix2[9]; |
687 | int i; |
688 | |
689 | *ret = *solid; /* structure copy */ |
690 | |
691 | flip_poly(ret, flip); |
692 | |
693 | /* |
694 | * Now rotate the polyhedron through the given angle. We must |
695 | * rotate about the Z-axis to bring the two vertices key0 and |
696 | * key1 into horizontal alignment, then rotate about the |
697 | * X-axis, then rotate back again. |
698 | */ |
699 | vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0]; |
700 | vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1]; |
701 | assert(APPROXEQ(vx*vx + vy*vy, 1.0)); |
702 | |
703 | vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0; |
704 | vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0; |
705 | vmatrix[2] = 0; vmatrix[5] = 0; vmatrix[8] = 1; |
706 | |
707 | ax = cos(angle); |
708 | ay = sin(angle); |
709 | |
710 | amatrix[0] = 1; amatrix[3] = 0; amatrix[6] = 0; |
711 | amatrix[1] = 0; amatrix[4] = ax; amatrix[7] = ay; |
712 | amatrix[2] = 0; amatrix[5] = -ay; amatrix[8] = ax; |
713 | |
714 | memcpy(vmatrix2, vmatrix, sizeof(vmatrix)); |
715 | vmatrix2[1] = vy; |
716 | vmatrix2[3] = -vy; |
717 | |
718 | for (i = 0; i < ret->nvertices; i++) { |
719 | MATMUL(ret->vertices + 3*i, vmatrix, ret->vertices + 3*i); |
720 | MATMUL(ret->vertices + 3*i, amatrix, ret->vertices + 3*i); |
721 | MATMUL(ret->vertices + 3*i, vmatrix2, ret->vertices + 3*i); |
722 | } |
723 | for (i = 0; i < ret->nfaces; i++) { |
724 | MATMUL(ret->normals + 3*i, vmatrix, ret->normals + 3*i); |
725 | MATMUL(ret->normals + 3*i, amatrix, ret->normals + 3*i); |
726 | MATMUL(ret->normals + 3*i, vmatrix2, ret->normals + 3*i); |
727 | } |
728 | |
729 | return ret; |
730 | } |
731 | |
732 | game_state *new_game(game_params *params, char *seed) |
733 | { |
734 | game_state *state = snew(game_state); |
735 | int area; |
736 | |
737 | state->params = *params; /* structure copy */ |
738 | state->solid = solids[params->solid]; |
739 | |
740 | area = grid_area(params->d1, params->d2, state->solid->order); |
741 | state->squares = snewn(area, struct grid_square); |
742 | state->nsquares = 0; |
743 | enum_grid_squares(params, add_grid_square_callback, state); |
744 | assert(state->nsquares == area); |
745 | |
746 | state->facecolours = snewn(state->solid->nfaces, int); |
747 | memset(state->facecolours, 0, state->solid->nfaces * sizeof(int)); |
748 | |
749 | /* |
750 | * Set up the blue squares and polyhedron position according to |
751 | * the game seed. |
752 | */ |
753 | { |
754 | char *p = seed; |
755 | int i, j, v; |
756 | |
757 | j = 8; |
758 | v = 0; |
759 | for (i = 0; i < state->nsquares; i++) { |
760 | if (j == 8) { |
761 | v = *p++; |
762 | if (v >= '0' && v <= '9') |
763 | v -= '0'; |
764 | else if (v >= 'A' && v <= 'F') |
765 | v -= 'A' - 10; |
766 | else if (v >= 'a' && v <= 'f') |
767 | v -= 'a' - 10; |
768 | else |
769 | break; |
770 | } |
771 | if (v & j) |
772 | state->squares[i].blue = TRUE; |
773 | j >>= 1; |
774 | if (j == 0) |
775 | j = 8; |
776 | } |
777 | |
778 | if (*p == ':') |
779 | p++; |
780 | |
781 | state->current = atoi(p); |
782 | if (state->current < 0 || state->current >= state->nsquares) |
783 | state->current = 0; /* got to do _something_ */ |
784 | } |
785 | |
786 | /* |
787 | * Align the polyhedron with its grid square and determine |
788 | * initial key points. |
789 | */ |
790 | { |
791 | int pkey[4]; |
792 | int ret; |
793 | |
794 | ret = align_poly(state->solid, &state->squares[state->current], pkey); |
795 | assert(ret); |
796 | |
797 | state->dpkey[0] = state->spkey[0] = pkey[0]; |
798 | state->dpkey[1] = state->spkey[0] = pkey[1]; |
799 | state->dgkey[0] = state->sgkey[0] = 0; |
800 | state->dgkey[1] = state->sgkey[0] = 1; |
801 | } |
802 | |
803 | state->previous = state->current; |
804 | state->angle = 0.0; |
805 | state->completed = FALSE; |
806 | state->movecount = 0; |
807 | |
808 | return state; |
809 | } |
810 | |
811 | game_state *dup_game(game_state *state) |
812 | { |
813 | game_state *ret = snew(game_state); |
814 | |
815 | ret->params = state->params; /* structure copy */ |
816 | ret->solid = state->solid; |
817 | ret->facecolours = snewn(ret->solid->nfaces, int); |
818 | memcpy(ret->facecolours, state->facecolours, |
819 | ret->solid->nfaces * sizeof(int)); |
820 | ret->nsquares = state->nsquares; |
821 | ret->squares = snewn(ret->nsquares, struct grid_square); |
822 | memcpy(ret->squares, state->squares, |
823 | ret->nsquares * sizeof(struct grid_square)); |
824 | ret->dpkey[0] = state->dpkey[0]; |
825 | ret->dpkey[1] = state->dpkey[1]; |
826 | ret->dgkey[0] = state->dgkey[0]; |
827 | ret->dgkey[1] = state->dgkey[1]; |
828 | ret->spkey[0] = state->spkey[0]; |
829 | ret->spkey[1] = state->spkey[1]; |
830 | ret->sgkey[0] = state->sgkey[0]; |
831 | ret->sgkey[1] = state->sgkey[1]; |
832 | ret->previous = state->previous; |
833 | ret->angle = state->angle; |
834 | ret->completed = state->completed; |
835 | ret->movecount = state->movecount; |
836 | |
837 | return ret; |
838 | } |
839 | |
840 | void free_game(game_state *state) |
841 | { |
842 | sfree(state); |
843 | } |
844 | |
845 | game_state *make_move(game_state *from, int x, int y, int button) |
846 | { |
847 | int direction; |
848 | int pkey[2], skey[2], dkey[2]; |
849 | float points[4]; |
850 | game_state *ret; |
851 | float angle; |
852 | int i, j, dest, mask; |
853 | struct solid *poly; |
854 | |
855 | /* |
856 | * All moves are made with the cursor keys. |
857 | */ |
858 | if (button == CURSOR_UP) |
859 | direction = UP; |
860 | else if (button == CURSOR_DOWN) |
861 | direction = DOWN; |
862 | else if (button == CURSOR_LEFT) |
863 | direction = LEFT; |
864 | else if (button == CURSOR_RIGHT) |
865 | direction = RIGHT; |
c71454c0 |
866 | else if (button == CURSOR_UP_LEFT) |
867 | direction = UP_LEFT; |
868 | else if (button == CURSOR_DOWN_LEFT) |
869 | direction = DOWN_LEFT; |
870 | else if (button == CURSOR_UP_RIGHT) |
871 | direction = UP_RIGHT; |
872 | else if (button == CURSOR_DOWN_RIGHT) |
873 | direction = DOWN_RIGHT; |
1482ee76 |
874 | else |
875 | return NULL; |
876 | |
877 | /* |
878 | * Find the two points in the current grid square which |
879 | * correspond to this move. |
880 | */ |
881 | mask = from->squares[from->current].directions[direction]; |
882 | if (mask == 0) |
883 | return NULL; |
884 | for (i = j = 0; i < from->squares[from->current].npoints; i++) |
885 | if (mask & (1 << i)) { |
886 | points[j*2] = from->squares[from->current].points[i*2]; |
887 | points[j*2+1] = from->squares[from->current].points[i*2+1]; |
888 | skey[j] = i; |
889 | j++; |
890 | } |
891 | assert(j == 2); |
892 | |
893 | /* |
894 | * Now find the other grid square which shares those points. |
895 | * This is our move destination. |
896 | */ |
897 | dest = -1; |
898 | for (i = 0; i < from->nsquares; i++) |
899 | if (i != from->current) { |
900 | int match = 0; |
901 | float dist; |
902 | |
903 | for (j = 0; j < from->squares[i].npoints; j++) { |
904 | dist = (SQ(from->squares[i].points[j*2] - points[0]) + |
905 | SQ(from->squares[i].points[j*2+1] - points[1])); |
906 | if (dist < 0.1) |
907 | dkey[match++] = j; |
908 | dist = (SQ(from->squares[i].points[j*2] - points[2]) + |
909 | SQ(from->squares[i].points[j*2+1] - points[3])); |
910 | if (dist < 0.1) |
911 | dkey[match++] = j; |
912 | } |
913 | |
914 | if (match == 2) { |
915 | dest = i; |
916 | break; |
917 | } |
918 | } |
919 | |
920 | if (dest < 0) |
921 | return NULL; |
922 | |
923 | ret = dup_game(from); |
924 | ret->current = i; |
925 | |
926 | /* |
927 | * So we know what grid square we're aiming for, and we also |
928 | * know the two key points (as indices in both the source and |
929 | * destination grid squares) which are invariant between source |
930 | * and destination. |
931 | * |
932 | * Next we must roll the polyhedron on to that square. So we |
933 | * find the indices of the key points within the polyhedron's |
934 | * vertex array, then use those in a call to transform_poly, |
935 | * and align the result on the new grid square. |
936 | */ |
937 | { |
938 | int all_pkey[4]; |
939 | align_poly(from->solid, &from->squares[from->current], all_pkey); |
940 | pkey[0] = all_pkey[skey[0]]; |
941 | pkey[1] = all_pkey[skey[1]]; |
942 | /* |
943 | * Now pkey[0] corresponds to skey[0] and dkey[0], and |
944 | * likewise [1]. |
945 | */ |
946 | } |
947 | |
948 | /* |
949 | * Now find the angle through which to rotate the polyhedron. |
950 | * Do this by finding the two faces that share the two vertices |
951 | * we've found, and taking the dot product of their normals. |
952 | */ |
953 | { |
954 | int f[2], nf = 0; |
955 | float dp; |
956 | |
957 | for (i = 0; i < from->solid->nfaces; i++) { |
958 | int match = 0; |
959 | for (j = 0; j < from->solid->order; j++) |
960 | if (from->solid->faces[i*from->solid->order + j] == pkey[0] || |
961 | from->solid->faces[i*from->solid->order + j] == pkey[1]) |
962 | match++; |
963 | if (match == 2) { |
964 | assert(nf < 2); |
965 | f[nf++] = i; |
966 | } |
967 | } |
968 | |
969 | assert(nf == 2); |
970 | |
971 | dp = 0; |
972 | for (i = 0; i < 3; i++) |
973 | dp += (from->solid->normals[f[0]*3+i] * |
974 | from->solid->normals[f[1]*3+i]); |
975 | angle = acos(dp); |
976 | } |
977 | |
978 | /* |
979 | * Now transform the polyhedron. We aren't entirely sure |
980 | * whether we need to rotate through angle or -angle, and the |
981 | * simplest way round this is to try both and see which one |
982 | * aligns successfully! |
983 | * |
984 | * Unfortunately, _both_ will align successfully if this is a |
985 | * cube, which won't tell us anything much. So for that |
986 | * particular case, I resort to gross hackery: I simply negate |
987 | * the angle before trying the alignment, depending on the |
988 | * direction. Which directions work which way is determined by |
989 | * pure trial and error. I said it was gross :-/ |
990 | */ |
991 | { |
992 | int all_pkey[4]; |
993 | int success; |
994 | |
995 | if (from->solid->order == 4 && direction == UP) |
996 | angle = -angle; /* HACK */ |
997 | |
998 | poly = transform_poly(from->solid, |
999 | from->squares[from->current].flip, |
1000 | pkey[0], pkey[1], angle); |
1001 | flip_poly(poly, from->squares[ret->current].flip); |
1002 | success = align_poly(poly, &from->squares[ret->current], all_pkey); |
1003 | |
1004 | if (!success) { |
1005 | angle = -angle; |
1006 | poly = transform_poly(from->solid, |
1007 | from->squares[from->current].flip, |
1008 | pkey[0], pkey[1], angle); |
1009 | flip_poly(poly, from->squares[ret->current].flip); |
1010 | success = align_poly(poly, &from->squares[ret->current], all_pkey); |
1011 | } |
1012 | |
1013 | assert(success); |
1014 | } |
1015 | |
1016 | /* |
1017 | * Now we have our rotated polyhedron, which we expect to be |
1018 | * exactly congruent to the one we started with - but with the |
1019 | * faces permuted. So we map that congruence and thereby figure |
1020 | * out how to permute the faces as a result of the polyhedron |
1021 | * having rolled. |
1022 | */ |
1023 | { |
1024 | int *newcolours = snewn(from->solid->nfaces, int); |
1025 | |
1026 | for (i = 0; i < from->solid->nfaces; i++) |
1027 | newcolours[i] = -1; |
1028 | |
1029 | for (i = 0; i < from->solid->nfaces; i++) { |
1030 | int nmatch = 0; |
1031 | |
1032 | /* |
1033 | * Now go through the transformed polyhedron's faces |
1034 | * and figure out which one's normal is approximately |
1035 | * equal to this one. |
1036 | */ |
1037 | for (j = 0; j < poly->nfaces; j++) { |
1038 | float dist; |
1039 | int k; |
1040 | |
1041 | dist = 0; |
1042 | |
1043 | for (k = 0; k < 3; k++) |
1044 | dist += SQ(poly->normals[j*3+k] - |
1045 | from->solid->normals[i*3+k]); |
1046 | |
1047 | if (APPROXEQ(dist, 0)) { |
1048 | nmatch++; |
1049 | newcolours[i] = ret->facecolours[j]; |
1050 | } |
1051 | } |
1052 | |
1053 | assert(nmatch == 1); |
1054 | } |
1055 | |
1056 | for (i = 0; i < from->solid->nfaces; i++) |
1057 | assert(newcolours[i] != -1); |
1058 | |
1059 | sfree(ret->facecolours); |
1060 | ret->facecolours = newcolours; |
1061 | } |
1062 | |
1063 | /* |
1064 | * And finally, swap the colour between the bottom face of the |
1065 | * polyhedron and the face we've just landed on. |
1066 | * |
1067 | * We don't do this if the game is already complete, since we |
1068 | * allow the user to roll the fully blue polyhedron around the |
1069 | * grid as a feeble reward. |
1070 | */ |
1071 | if (!ret->completed) { |
1072 | i = lowest_face(from->solid); |
1073 | j = ret->facecolours[i]; |
1074 | ret->facecolours[i] = ret->squares[ret->current].blue; |
1075 | ret->squares[ret->current].blue = j; |
1076 | |
1077 | /* |
1078 | * Detect game completion. |
1079 | */ |
1080 | j = 0; |
1081 | for (i = 0; i < ret->solid->nfaces; i++) |
1082 | if (ret->facecolours[i]) |
1083 | j++; |
1084 | if (j == ret->solid->nfaces) |
1085 | ret->completed = TRUE; |
1086 | } |
1087 | |
1088 | sfree(poly); |
1089 | |
1090 | /* |
1091 | * Align the normal polyhedron with its grid square, to get key |
1092 | * points for non-animated display. |
1093 | */ |
1094 | { |
1095 | int pkey[4]; |
1096 | int success; |
1097 | |
1098 | success = align_poly(ret->solid, &ret->squares[ret->current], pkey); |
1099 | assert(success); |
1100 | |
1101 | ret->dpkey[0] = pkey[0]; |
1102 | ret->dpkey[1] = pkey[1]; |
1103 | ret->dgkey[0] = 0; |
1104 | ret->dgkey[1] = 1; |
1105 | } |
1106 | |
1107 | |
1108 | ret->spkey[0] = pkey[0]; |
1109 | ret->spkey[1] = pkey[1]; |
1110 | ret->sgkey[0] = skey[0]; |
1111 | ret->sgkey[1] = skey[1]; |
1112 | ret->previous = from->current; |
1113 | ret->angle = angle; |
1114 | ret->movecount++; |
1115 | |
1116 | return ret; |
1117 | } |
1118 | |
1119 | /* ---------------------------------------------------------------------- |
1120 | * Drawing routines. |
1121 | */ |
1122 | |
1123 | struct bbox { |
1124 | float l, r, u, d; |
1125 | }; |
1126 | |
1127 | struct game_drawstate { |
1128 | int ox, oy; /* pixel position of float origin */ |
1129 | }; |
1130 | |
1131 | static void find_bbox_callback(void *ctx, struct grid_square *sq) |
1132 | { |
1133 | struct bbox *bb = (struct bbox *)ctx; |
1134 | int i; |
1135 | |
1136 | for (i = 0; i < sq->npoints; i++) { |
1137 | if (bb->l > sq->points[i*2]) bb->l = sq->points[i*2]; |
1138 | if (bb->r < sq->points[i*2]) bb->r = sq->points[i*2]; |
1139 | if (bb->u > sq->points[i*2+1]) bb->u = sq->points[i*2+1]; |
1140 | if (bb->d < sq->points[i*2+1]) bb->d = sq->points[i*2+1]; |
1141 | } |
1142 | } |
1143 | |
1144 | static struct bbox find_bbox(game_params *params) |
1145 | { |
1146 | struct bbox bb; |
1147 | |
1148 | /* |
1149 | * These should be hugely more than the real bounding box will |
1150 | * be. |
1151 | */ |
1152 | bb.l = 2 * (params->d1 + params->d2); |
1153 | bb.r = -2 * (params->d1 + params->d2); |
1154 | bb.u = 2 * (params->d1 + params->d2); |
1155 | bb.d = -2 * (params->d1 + params->d2); |
1156 | enum_grid_squares(params, find_bbox_callback, &bb); |
1157 | |
1158 | return bb; |
1159 | } |
1160 | |
1161 | void game_size(game_params *params, int *x, int *y) |
1162 | { |
1163 | struct bbox bb = find_bbox(params); |
eb2ad6f1 |
1164 | *x = (bb.r - bb.l + 2*solids[params->solid]->border) * GRID_SCALE; |
1165 | *y = (bb.d - bb.u + 2*solids[params->solid]->border) * GRID_SCALE; |
1482ee76 |
1166 | } |
1167 | |
1168 | float *game_colours(frontend *fe, game_state *state, int *ncolours) |
1169 | { |
1170 | float *ret = snewn(3 * NCOLOURS, float); |
1171 | |
1172 | frontend_default_colour(fe, &ret[COL_BACKGROUND * 3]); |
1173 | |
1174 | ret[COL_BORDER * 3 + 0] = 0.0; |
1175 | ret[COL_BORDER * 3 + 1] = 0.0; |
1176 | ret[COL_BORDER * 3 + 2] = 0.0; |
1177 | |
1178 | ret[COL_BLUE * 3 + 0] = 0.0; |
1179 | ret[COL_BLUE * 3 + 1] = 0.0; |
1180 | ret[COL_BLUE * 3 + 2] = 1.0; |
1181 | |
1182 | *ncolours = NCOLOURS; |
1183 | return ret; |
1184 | } |
1185 | |
1186 | game_drawstate *game_new_drawstate(game_state *state) |
1187 | { |
1188 | struct game_drawstate *ds = snew(struct game_drawstate); |
1189 | struct bbox bb = find_bbox(&state->params); |
1190 | |
eb2ad6f1 |
1191 | ds->ox = -(bb.l - state->solid->border) * GRID_SCALE; |
1192 | ds->oy = -(bb.u - state->solid->border) * GRID_SCALE; |
1482ee76 |
1193 | |
1194 | return ds; |
1195 | } |
1196 | |
1197 | void game_free_drawstate(game_drawstate *ds) |
1198 | { |
1199 | sfree(ds); |
1200 | } |
1201 | |
1202 | void game_redraw(frontend *fe, game_drawstate *ds, game_state *oldstate, |
1203 | game_state *state, float animtime) |
1204 | { |
1205 | int i, j; |
1206 | struct bbox bb = find_bbox(&state->params); |
1207 | struct solid *poly; |
1208 | int *pkey, *gkey; |
1209 | float t[3]; |
1210 | float angle; |
1211 | game_state *newstate; |
1212 | int square; |
1213 | |
1214 | draw_rect(fe, 0, 0, (bb.r-bb.l+2) * GRID_SCALE, |
1215 | (bb.d-bb.u+2) * GRID_SCALE, COL_BACKGROUND); |
1216 | |
1217 | if (oldstate && oldstate->movecount > state->movecount) { |
1218 | game_state *t; |
1219 | |
1220 | /* |
1221 | * This is an Undo. So reverse the order of the states, and |
1222 | * run the roll timer backwards. |
1223 | */ |
1224 | t = oldstate; |
1225 | oldstate = state; |
1226 | state = t; |
1227 | |
1228 | animtime = ROLLTIME - animtime; |
1229 | } |
1230 | |
1231 | if (!oldstate) { |
1232 | oldstate = state; |
1233 | angle = 0.0; |
1234 | square = state->current; |
1235 | pkey = state->dpkey; |
1236 | gkey = state->dgkey; |
1237 | } else { |
1238 | angle = state->angle * animtime / ROLLTIME; |
1239 | square = state->previous; |
1240 | pkey = state->spkey; |
1241 | gkey = state->sgkey; |
1242 | } |
1243 | newstate = state; |
1244 | state = oldstate; |
1245 | |
1246 | for (i = 0; i < state->nsquares; i++) { |
1247 | int coords[8]; |
1248 | |
1249 | for (j = 0; j < state->squares[i].npoints; j++) { |
1250 | coords[2*j] = state->squares[i].points[2*j] |
1251 | * GRID_SCALE + ds->ox; |
1252 | coords[2*j+1] = state->squares[i].points[2*j+1] |
1253 | * GRID_SCALE + ds->oy; |
1254 | } |
1255 | |
1256 | draw_polygon(fe, coords, state->squares[i].npoints, TRUE, |
1257 | state->squares[i].blue ? COL_BLUE : COL_BACKGROUND); |
1258 | draw_polygon(fe, coords, state->squares[i].npoints, FALSE, COL_BORDER); |
1259 | } |
1260 | |
1261 | /* |
1262 | * Now compute and draw the polyhedron. |
1263 | */ |
1264 | poly = transform_poly(state->solid, state->squares[square].flip, |
1265 | pkey[0], pkey[1], angle); |
1266 | |
1267 | /* |
1268 | * Compute the translation required to align the two key points |
1269 | * on the polyhedron with the same key points on the current |
1270 | * face. |
1271 | */ |
1272 | for (i = 0; i < 3; i++) { |
1273 | float tc = 0.0; |
1274 | |
1275 | for (j = 0; j < 2; j++) { |
1276 | float grid_coord; |
1277 | |
1278 | if (i < 2) { |
1279 | grid_coord = |
1280 | state->squares[square].points[gkey[j]*2+i]; |
1281 | } else { |
1282 | grid_coord = 0.0; |
1283 | } |
1284 | |
1285 | tc += (grid_coord - poly->vertices[pkey[j]*3+i]); |
1286 | } |
1287 | |
1288 | t[i] = tc / 2; |
1289 | } |
1290 | for (i = 0; i < poly->nvertices; i++) |
1291 | for (j = 0; j < 3; j++) |
1292 | poly->vertices[i*3+j] += t[j]; |
1293 | |
1294 | /* |
1295 | * Now actually draw each face. |
1296 | */ |
1297 | for (i = 0; i < poly->nfaces; i++) { |
1298 | float points[8]; |
1299 | int coords[8]; |
1300 | |
1301 | for (j = 0; j < poly->order; j++) { |
1302 | int f = poly->faces[i*poly->order + j]; |
1303 | points[j*2] = (poly->vertices[f*3+0] - |
1304 | poly->vertices[f*3+2] * poly->shear); |
1305 | points[j*2+1] = (poly->vertices[f*3+1] - |
1306 | poly->vertices[f*3+2] * poly->shear); |
1307 | } |
1308 | |
1309 | for (j = 0; j < poly->order; j++) { |
1310 | coords[j*2] = points[j*2] * GRID_SCALE + ds->ox; |
1311 | coords[j*2+1] = points[j*2+1] * GRID_SCALE + ds->oy; |
1312 | } |
1313 | |
1314 | /* |
1315 | * Find out whether these points are in a clockwise or |
1316 | * anticlockwise arrangement. If the latter, discard the |
1317 | * face because it's facing away from the viewer. |
1318 | * |
1319 | * This would involve fiddly winding-number stuff for a |
1320 | * general polygon, but for the simple parallelograms we'll |
1321 | * be seeing here, all we have to do is check whether the |
1322 | * corners turn right or left. So we'll take the vector |
1323 | * from point 0 to point 1, turn it right 90 degrees, |
1324 | * and check the sign of the dot product with that and the |
1325 | * next vector (point 1 to point 2). |
1326 | */ |
1327 | { |
1328 | float v1x = points[2]-points[0]; |
1329 | float v1y = points[3]-points[1]; |
1330 | float v2x = points[4]-points[2]; |
1331 | float v2y = points[5]-points[3]; |
1332 | float dp = v1x * v2y - v1y * v2x; |
1333 | |
1334 | if (dp <= 0) |
1335 | continue; |
1336 | } |
1337 | |
1338 | draw_polygon(fe, coords, poly->order, TRUE, |
1339 | state->facecolours[i] ? COL_BLUE : COL_BACKGROUND); |
1340 | draw_polygon(fe, coords, poly->order, FALSE, COL_BORDER); |
1341 | } |
1342 | sfree(poly); |
1343 | |
1344 | draw_update(fe, 0, 0, (bb.r-bb.l+2) * GRID_SCALE, |
1345 | (bb.d-bb.u+2) * GRID_SCALE); |
1346 | } |
1347 | |
1348 | float game_anim_length(game_state *oldstate, game_state *newstate) |
1349 | { |
1350 | return ROLLTIME; |
1351 | } |