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