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