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