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