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