-
Notifications
You must be signed in to change notification settings - Fork 0
/
cube.c
1773 lines (1516 loc) · 49.6 KB
/
cube.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* cube.c: Cube game.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include "puzzles.h"
#define MAXVERTICES 20
#define MAXFACES 20
#define MAXORDER 4
struct solid {
int nvertices;
float vertices[MAXVERTICES * 3]; /* 3*npoints coordinates */
int order;
int nfaces;
int faces[MAXFACES * MAXORDER]; /* order*nfaces point indices */
float normals[MAXFACES * 3]; /* 3*npoints vector components */
float shear; /* isometric shear for nice drawing */
float border; /* border required around arena */
};
static const struct solid s_tetrahedron = {
4,
{
0.0F, -0.57735026919F, -0.20412414523F,
-0.5F, 0.28867513459F, -0.20412414523F,
0.0F, -0.0F, 0.6123724357F,
0.5F, 0.28867513459F, -0.20412414523F,
},
3, 4,
{
0,2,1, 3,1,2, 2,0,3, 1,3,0
},
{
-0.816496580928F, -0.471404520791F, 0.333333333334F,
0.0F, 0.942809041583F, 0.333333333333F,
0.816496580928F, -0.471404520791F, 0.333333333334F,
0.0F, 0.0F, -1.0F,
},
0.0F, 0.3F
};
static const struct solid s_cube = {
8,
{
-0.5F,-0.5F,-0.5F, -0.5F,-0.5F,+0.5F,
-0.5F,+0.5F,-0.5F, -0.5F,+0.5F,+0.5F,
+0.5F,-0.5F,-0.5F, +0.5F,-0.5F,+0.5F,
+0.5F,+0.5F,-0.5F, +0.5F,+0.5F,+0.5F,
},
4, 6,
{
0,1,3,2, 1,5,7,3, 5,4,6,7, 4,0,2,6, 0,4,5,1, 3,7,6,2
},
{
-1.0F,0.0F,0.0F, 0.0F,0.0F,+1.0F,
+1.0F,0.0F,0.0F, 0.0F,0.0F,-1.0F,
0.0F,-1.0F,0.0F, 0.0F,+1.0F,0.0F
},
0.3F, 0.5F
};
static const struct solid s_octahedron = {
6,
{
-0.5F, -0.28867513459472505F, 0.4082482904638664F,
0.5F, 0.28867513459472505F, -0.4082482904638664F,
-0.5F, 0.28867513459472505F, -0.4082482904638664F,
0.5F, -0.28867513459472505F, 0.4082482904638664F,
0.0F, -0.57735026918945009F, -0.4082482904638664F,
0.0F, 0.57735026918945009F, 0.4082482904638664F,
},
3, 8,
{
4,0,2, 0,5,2, 0,4,3, 5,0,3, 1,4,2, 5,1,2, 4,1,3, 1,5,3
},
{
-0.816496580928F, -0.471404520791F, -0.333333333334F,
-0.816496580928F, 0.471404520791F, 0.333333333334F,
0.0F, -0.942809041583F, 0.333333333333F,
0.0F, 0.0F, 1.0F,
0.0F, 0.0F, -1.0F,
0.0F, 0.942809041583F, -0.333333333333F,
0.816496580928F, -0.471404520791F, -0.333333333334F,
0.816496580928F, 0.471404520791F, 0.333333333334F,
},
0.0F, 0.5F
};
static const struct solid s_icosahedron = {
12,
{
0.0F, 0.57735026919F, 0.75576131408F,
0.0F, -0.93417235896F, 0.17841104489F,
0.0F, 0.93417235896F, -0.17841104489F,
0.0F, -0.57735026919F, -0.75576131408F,
-0.5F, -0.28867513459F, 0.75576131408F,
-0.5F, 0.28867513459F, -0.75576131408F,
0.5F, -0.28867513459F, 0.75576131408F,
0.5F, 0.28867513459F, -0.75576131408F,
-0.80901699437F, 0.46708617948F, 0.17841104489F,
0.80901699437F, 0.46708617948F, 0.17841104489F,
-0.80901699437F, -0.46708617948F, -0.17841104489F,
0.80901699437F, -0.46708617948F, -0.17841104489F,
},
3, 20,
{
8,0,2, 0,9,2, 1,10,3, 11,1,3, 0,4,6,
4,1,6, 5,2,7, 3,5,7, 4,8,10, 8,5,10,
9,6,11, 7,9,11, 0,8,4, 9,0,6, 10,1,4,
1,11,6, 8,2,5, 2,9,7, 3,10,5, 11,3,7,
},
{
-0.356822089773F, 0.87267799625F, 0.333333333333F,
0.356822089773F, 0.87267799625F, 0.333333333333F,
-0.356822089773F, -0.87267799625F, -0.333333333333F,
0.356822089773F, -0.87267799625F, -0.333333333333F,
-0.0F, 0.0F, 1.0F,
0.0F, -0.666666666667F, 0.745355992501F,
0.0F, 0.666666666667F, -0.745355992501F,
0.0F, 0.0F, -1.0F,
-0.934172358963F, -0.12732200375F, 0.333333333333F,
-0.934172358963F, 0.12732200375F, -0.333333333333F,
0.934172358963F, -0.12732200375F, 0.333333333333F,
0.934172358963F, 0.12732200375F, -0.333333333333F,
-0.57735026919F, 0.333333333334F, 0.745355992501F,
0.57735026919F, 0.333333333334F, 0.745355992501F,
-0.57735026919F, -0.745355992501F, 0.333333333334F,
0.57735026919F, -0.745355992501F, 0.333333333334F,
-0.57735026919F, 0.745355992501F, -0.333333333334F,
0.57735026919F, 0.745355992501F, -0.333333333334F,
-0.57735026919F, -0.333333333334F, -0.745355992501F,
0.57735026919F, -0.333333333334F, -0.745355992501F,
},
0.0F, 0.8F
};
enum {
TETRAHEDRON, CUBE, OCTAHEDRON, ICOSAHEDRON
};
static const struct solid *solids[] = {
&s_tetrahedron, &s_cube, &s_octahedron, &s_icosahedron
};
enum {
COL_BACKGROUND,
COL_BORDER,
COL_BLUE,
NCOLOURS
};
enum { LEFT, RIGHT, UP, DOWN, UP_LEFT, UP_RIGHT, DOWN_LEFT, DOWN_RIGHT };
#define PREFERRED_GRID_SCALE 48
#define GRID_SCALE (ds->gridscale)
#define ROLLTIME 0.13F
#define SQ(x) ( (x) * (x) )
#define MATMUL(ra,m,a) do { \
float rx, ry, rz, xx = (a)[0], yy = (a)[1], zz = (a)[2], *mat = (m); \
rx = mat[0] * xx + mat[3] * yy + mat[6] * zz; \
ry = mat[1] * xx + mat[4] * yy + mat[7] * zz; \
rz = mat[2] * xx + mat[5] * yy + mat[8] * zz; \
(ra)[0] = rx; (ra)[1] = ry; (ra)[2] = rz; \
} while (0)
#define APPROXEQ(x,y) ( SQ(x-y) < 0.1 )
struct grid_square {
float x, y;
int npoints;
float points[8]; /* maximum */
int directions[8]; /* bit masks showing point pairs */
int flip;
int tetra_class;
};
struct game_params {
int solid;
/*
* Grid dimensions. For a square grid these are width and
* height respectively; otherwise the grid is a hexagon, with
* the top side and the two lower diagonals having length d1
* and the remaining three sides having length d2 (so that
* d1==d2 gives a regular hexagon, and d2==0 gives a triangle).
*/
int d1, d2;
};
typedef struct game_grid game_grid;
struct game_grid {
int refcount;
struct grid_square *squares;
int nsquares;
};
#define SET_SQUARE(state, i, val) \
((state)->bluemask[(i)/32] &= ~(1 << ((i)%32)), \
(state)->bluemask[(i)/32] |= ((!!val) << ((i)%32)))
#define GET_SQUARE(state, i) \
(((state)->bluemask[(i)/32] >> ((i)%32)) & 1)
struct game_state {
struct game_params params;
const struct solid *solid;
int *facecolours;
game_grid *grid;
unsigned long *bluemask;
int current; /* index of current grid square */
int sgkey[2]; /* key-point indices into grid sq */
int dgkey[2]; /* key-point indices into grid sq */
int spkey[2]; /* key-point indices into polyhedron */
int dpkey[2]; /* key-point indices into polyhedron */
int previous;
float angle;
int completed;
int movecount;
};
static game_params *default_params(void)
{
game_params *ret = snew(game_params);
ret->solid = CUBE;
ret->d1 = 4;
ret->d2 = 4;
return ret;
}
static int game_fetch_preset(int i, char **name, game_params **params)
{
game_params *ret = snew(game_params);
char *str;
switch (i) {
case 0:
str = "Cube";
ret->solid = CUBE;
ret->d1 = 4;
ret->d2 = 4;
break;
case 1:
str = "Tetrahedron";
ret->solid = TETRAHEDRON;
ret->d1 = 1;
ret->d2 = 2;
break;
case 2:
str = "Octahedron";
ret->solid = OCTAHEDRON;
ret->d1 = 2;
ret->d2 = 2;
break;
case 3:
str = "Icosahedron";
ret->solid = ICOSAHEDRON;
ret->d1 = 3;
ret->d2 = 3;
break;
default:
sfree(ret);
return FALSE;
}
*name = dupstr(str);
*params = ret;
return TRUE;
}
static void free_params(game_params *params)
{
sfree(params);
}
static game_params *dup_params(const game_params *params)
{
game_params *ret = snew(game_params);
*ret = *params; /* structure copy */
return ret;
}
static void decode_params(game_params *ret, char const *string)
{
switch (*string) {
case 't': ret->solid = TETRAHEDRON; string++; break;
case 'c': ret->solid = CUBE; string++; break;
case 'o': ret->solid = OCTAHEDRON; string++; break;
case 'i': ret->solid = ICOSAHEDRON; string++; break;
default: break;
}
ret->d1 = ret->d2 = atoi(string);
while (*string && isdigit((unsigned char)*string)) string++;
if (*string == 'x') {
string++;
ret->d2 = atoi(string);
}
}
static char *encode_params(const game_params *params, int full)
{
char data[256];
assert(params->solid >= 0 && params->solid < 4);
sprintf(data, "%c%dx%d", "tcoi"[params->solid], params->d1, params->d2);
return dupstr(data);
}
typedef void (*egc_callback)(void *, struct grid_square *);
static void enum_grid_squares(const game_params *params, egc_callback callback,
void *ctx)
{
const struct solid *solid = solids[params->solid];
if (solid->order == 4) {
int x, y;
for (y = 0; y < params->d2; y++)
for (x = 0; x < params->d1; x++) {
struct grid_square sq;
sq.x = (float)x;
sq.y = (float)y;
sq.points[0] = x - 0.5F;
sq.points[1] = y - 0.5F;
sq.points[2] = x - 0.5F;
sq.points[3] = y + 0.5F;
sq.points[4] = x + 0.5F;
sq.points[5] = y + 0.5F;
sq.points[6] = x + 0.5F;
sq.points[7] = y - 0.5F;
sq.npoints = 4;
sq.directions[LEFT] = 0x03; /* 0,1 */
sq.directions[RIGHT] = 0x0C; /* 2,3 */
sq.directions[UP] = 0x09; /* 0,3 */
sq.directions[DOWN] = 0x06; /* 1,2 */
sq.directions[UP_LEFT] = 0; /* no diagonals in a square */
sq.directions[UP_RIGHT] = 0; /* no diagonals in a square */
sq.directions[DOWN_LEFT] = 0; /* no diagonals in a square */
sq.directions[DOWN_RIGHT] = 0; /* no diagonals in a square */
sq.flip = FALSE;
/*
* This is supremely irrelevant, but just to avoid
* having any uninitialised structure members...
*/
sq.tetra_class = 0;
callback(ctx, &sq);
}
} else {
int row, rowlen, other, i, firstix = -1;
float theight = (float)(sqrt(3) / 2.0);
for (row = 0; row < params->d1 + params->d2; row++) {
if (row < params->d2) {
other = +1;
rowlen = row + params->d1;
} else {
other = -1;
rowlen = 2*params->d2 + params->d1 - row;
}
/*
* There are `rowlen' down-pointing triangles.
*/
for (i = 0; i < rowlen; i++) {
struct grid_square sq;
int ix;
float x, y;
ix = (2 * i - (rowlen-1));
x = ix * 0.5F;
y = theight * row;
sq.x = x;
sq.y = y + theight / 3;
sq.points[0] = x - 0.5F;
sq.points[1] = y;
sq.points[2] = x;
sq.points[3] = y + theight;
sq.points[4] = x + 0.5F;
sq.points[5] = y;
sq.npoints = 3;
sq.directions[LEFT] = 0x03; /* 0,1 */
sq.directions[RIGHT] = 0x06; /* 1,2 */
sq.directions[UP] = 0x05; /* 0,2 */
sq.directions[DOWN] = 0; /* invalid move */
/*
* Down-pointing triangle: both the up diagonals go
* up, and the down ones go left and right.
*/
sq.directions[UP_LEFT] = sq.directions[UP_RIGHT] =
sq.directions[UP];
sq.directions[DOWN_LEFT] = sq.directions[LEFT];
sq.directions[DOWN_RIGHT] = sq.directions[RIGHT];
sq.flip = TRUE;
if (firstix < 0)
firstix = ix & 3;
ix -= firstix;
sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
callback(ctx, &sq);
}
/*
* There are `rowlen+other' up-pointing triangles.
*/
for (i = 0; i < rowlen+other; i++) {
struct grid_square sq;
int ix;
float x, y;
ix = (2 * i - (rowlen+other-1));
x = ix * 0.5F;
y = theight * row;
sq.x = x;
sq.y = y + 2*theight / 3;
sq.points[0] = x + 0.5F;
sq.points[1] = y + theight;
sq.points[2] = x;
sq.points[3] = y;
sq.points[4] = x - 0.5F;
sq.points[5] = y + theight;
sq.npoints = 3;
sq.directions[LEFT] = 0x06; /* 1,2 */
sq.directions[RIGHT] = 0x03; /* 0,1 */
sq.directions[DOWN] = 0x05; /* 0,2 */
sq.directions[UP] = 0; /* invalid move */
/*
* Up-pointing triangle: both the down diagonals go
* down, and the up ones go left and right.
*/
sq.directions[DOWN_LEFT] = sq.directions[DOWN_RIGHT] =
sq.directions[DOWN];
sq.directions[UP_LEFT] = sq.directions[LEFT];
sq.directions[UP_RIGHT] = sq.directions[RIGHT];
sq.flip = FALSE;
if (firstix < 0)
firstix = (ix - 1) & 3;
ix -= firstix;
sq.tetra_class = ((row+(ix&1)) & 2) ^ (ix & 3);
callback(ctx, &sq);
}
}
}
}
static int grid_area(int d1, int d2, int order)
{
/*
* An NxM grid of squares has NM squares in it.
*
* A grid of triangles with dimensions A and B has a total of
* A^2 + B^2 + 4AB triangles in it. (You can divide it up into
* a side-A triangle containing A^2 subtriangles, a side-B
* triangle containing B^2, and two congruent parallelograms,
* each with side lengths A and B, each therefore containing AB
* two-triangle rhombuses.)
*/
if (order == 4)
return d1 * d2;
else
return d1*d1 + d2*d2 + 4*d1*d2;
}
static config_item *game_configure(const game_params *params)
{
config_item *ret = snewn(4, config_item);
char buf[80];
ret[0].name = "Type of solid";
ret[0].type = C_CHOICES;
ret[0].sval = ":Tetrahedron:Cube:Octahedron:Icosahedron";
ret[0].ival = params->solid;
ret[1].name = "Width / top";
ret[1].type = C_STRING;
sprintf(buf, "%d", params->d1);
ret[1].sval = dupstr(buf);
ret[1].ival = 0;
ret[2].name = "Height / bottom";
ret[2].type = C_STRING;
sprintf(buf, "%d", params->d2);
ret[2].sval = dupstr(buf);
ret[2].ival = 0;
ret[3].name = NULL;
ret[3].type = C_END;
ret[3].sval = NULL;
ret[3].ival = 0;
return ret;
}
static game_params *custom_params(const config_item *cfg)
{
game_params *ret = snew(game_params);
ret->solid = cfg[0].ival;
ret->d1 = atoi(cfg[1].sval);
ret->d2 = atoi(cfg[2].sval);
return ret;
}
static void count_grid_square_callback(void *ctx, struct grid_square *sq)
{
int *classes = (int *)ctx;
int thisclass;
if (classes[4] == 4)
thisclass = sq->tetra_class;
else if (classes[4] == 2)
thisclass = sq->flip;
else
thisclass = 0;
classes[thisclass]++;
}
static char *validate_params(const game_params *params, int full)
{
int classes[5];
int i;
if (params->solid < 0 || params->solid >= lenof(solids))
return "Unrecognised solid type";
if (solids[params->solid]->order == 4) {
if (params->d1 <= 0 || params->d2 <= 0)
return "Both grid dimensions must be greater than zero";
} else {
if (params->d1 <= 0 && params->d2 <= 0)
return "At least one grid dimension must be greater than zero";
}
for (i = 0; i < 4; i++)
classes[i] = 0;
if (params->solid == TETRAHEDRON)
classes[4] = 4;
else if (params->solid == OCTAHEDRON)
classes[4] = 2;
else
classes[4] = 1;
enum_grid_squares(params, count_grid_square_callback, classes);
for (i = 0; i < classes[4]; i++)
if (classes[i] < solids[params->solid]->nfaces / classes[4])
return "Not enough grid space to place all blue faces";
if (grid_area(params->d1, params->d2, solids[params->solid]->order) <
solids[params->solid]->nfaces + 1)
return "Not enough space to place the solid on an empty square";
return NULL;
}
struct grid_data {
int *gridptrs[4];
int nsquares[4];
int nclasses;
int squareindex;
};
static void classify_grid_square_callback(void *ctx, struct grid_square *sq)
{
struct grid_data *data = (struct grid_data *)ctx;
int thisclass;
if (data->nclasses == 4)
thisclass = sq->tetra_class;
else if (data->nclasses == 2)
thisclass = sq->flip;
else
thisclass = 0;
data->gridptrs[thisclass][data->nsquares[thisclass]++] =
data->squareindex++;
}
static char *new_game_desc(const game_params *params, random_state *rs,
char **aux, int interactive)
{
struct grid_data data;
int i, j, k, m, area, facesperclass;
int *flags;
char *desc, *p;
/*
* Enumerate the grid squares, dividing them into equivalence
* classes as appropriate. (For the tetrahedron, there is one
* equivalence class for each face; for the octahedron there
* are two classes; for the other two solids there's only one.)
*/
area = grid_area(params->d1, params->d2, solids[params->solid]->order);
if (params->solid == TETRAHEDRON)
data.nclasses = 4;
else if (params->solid == OCTAHEDRON)
data.nclasses = 2;
else
data.nclasses = 1;
data.gridptrs[0] = snewn(data.nclasses * area, int);
for (i = 0; i < data.nclasses; i++) {
data.gridptrs[i] = data.gridptrs[0] + i * area;
data.nsquares[i] = 0;
}
data.squareindex = 0;
enum_grid_squares(params, classify_grid_square_callback, &data);
facesperclass = solids[params->solid]->nfaces / data.nclasses;
for (i = 0; i < data.nclasses; i++)
assert(data.nsquares[i] >= facesperclass);
assert(data.squareindex == area);
/*
* So now we know how many faces to allocate in each class. Get
* on with it.
*/
flags = snewn(area, int);
for (i = 0; i < area; i++)
flags[i] = FALSE;
for (i = 0; i < data.nclasses; i++) {
for (j = 0; j < facesperclass; j++) {
int n = random_upto(rs, data.nsquares[i]);
assert(!flags[data.gridptrs[i][n]]);
flags[data.gridptrs[i][n]] = TRUE;
/*
* Move everything else up the array. I ought to use a
* better data structure for this, but for such small
* numbers it hardly seems worth the effort.
*/
while (n < data.nsquares[i]-1) {
data.gridptrs[i][n] = data.gridptrs[i][n+1];
n++;
}
data.nsquares[i]--;
}
}
/*
* Now we know precisely which squares are blue. Encode this
* information in hex. While we're looping over this, collect
* the non-blue squares into a list in the now-unused gridptrs
* array.
*/
desc = snewn(area / 4 + 40, char);
p = desc;
j = 0;
k = 8;
m = 0;
for (i = 0; i < area; i++) {
if (flags[i]) {
j |= k;
} else {
data.gridptrs[0][m++] = i;
}
k >>= 1;
if (!k) {
*p++ = "0123456789ABCDEF"[j];
k = 8;
j = 0;
}
}
if (k != 8)
*p++ = "0123456789ABCDEF"[j];
/*
* Choose a non-blue square for the polyhedron.
*/
sprintf(p, ",%d", data.gridptrs[0][random_upto(rs, m)]);
sfree(data.gridptrs[0]);
sfree(flags);
return desc;
}
static void add_grid_square_callback(void *ctx, struct grid_square *sq)
{
game_grid *grid = (game_grid *)ctx;
grid->squares[grid->nsquares++] = *sq; /* structure copy */
}
static int lowest_face(const struct solid *solid)
{
int i, j, best;
float zmin;
best = 0;
zmin = 0.0;
for (i = 0; i < solid->nfaces; i++) {
float z = 0;
for (j = 0; j < solid->order; j++) {
int f = solid->faces[i*solid->order + j];
z += solid->vertices[f*3+2];
}
if (i == 0 || zmin > z) {
zmin = z;
best = i;
}
}
return best;
}
static int align_poly(const struct solid *solid, struct grid_square *sq,
int *pkey)
{
float zmin;
int i, j;
int flip = (sq->flip ? -1 : +1);
/*
* First, find the lowest z-coordinate present in the solid.
*/
zmin = 0.0;
for (i = 0; i < solid->nvertices; i++)
if (zmin > solid->vertices[i*3+2])
zmin = solid->vertices[i*3+2];
/*
* Now go round the grid square. For each point in the grid
* square, we're looking for a point of the polyhedron with the
* same x- and y-coordinates (relative to the square's centre),
* and z-coordinate equal to zmin (near enough).
*/
for (j = 0; j < sq->npoints; j++) {
int matches, index;
matches = 0;
index = -1;
for (i = 0; i < solid->nvertices; i++) {
float dist = 0;
dist += SQ(solid->vertices[i*3+0] * flip - sq->points[j*2+0] + sq->x);
dist += SQ(solid->vertices[i*3+1] * flip - sq->points[j*2+1] + sq->y);
dist += SQ(solid->vertices[i*3+2] - zmin);
if (dist < 0.1) {
matches++;
index = i;
}
}
if (matches != 1 || index < 0)
return FALSE;
pkey[j] = index;
}
return TRUE;
}
static void flip_poly(struct solid *solid, int flip)
{
int i;
if (flip) {
for (i = 0; i < solid->nvertices; i++) {
solid->vertices[i*3+0] *= -1;
solid->vertices[i*3+1] *= -1;
}
for (i = 0; i < solid->nfaces; i++) {
solid->normals[i*3+0] *= -1;
solid->normals[i*3+1] *= -1;
}
}
}
static struct solid *transform_poly(const struct solid *solid, int flip,
int key0, int key1, float angle)
{
struct solid *ret = snew(struct solid);
float vx, vy, ax, ay;
float vmatrix[9], amatrix[9], vmatrix2[9];
int i;
*ret = *solid; /* structure copy */
flip_poly(ret, flip);
/*
* Now rotate the polyhedron through the given angle. We must
* rotate about the Z-axis to bring the two vertices key0 and
* key1 into horizontal alignment, then rotate about the
* X-axis, then rotate back again.
*/
vx = ret->vertices[key1*3+0] - ret->vertices[key0*3+0];
vy = ret->vertices[key1*3+1] - ret->vertices[key0*3+1];
assert(APPROXEQ(vx*vx + vy*vy, 1.0));
vmatrix[0] = vx; vmatrix[3] = vy; vmatrix[6] = 0;
vmatrix[1] = -vy; vmatrix[4] = vx; vmatrix[7] = 0;
vmatrix[2] = 0; vmatrix[5] = 0; vmatrix[8] = 1;
ax = (float)cos(angle);
ay = (float)sin(angle);
amatrix[0] = 1; amatrix[3] = 0; amatrix[6] = 0;
amatrix[1] = 0; amatrix[4] = ax; amatrix[7] = ay;
amatrix[2] = 0; amatrix[5] = -ay; amatrix[8] = ax;
memcpy(vmatrix2, vmatrix, sizeof(vmatrix));
vmatrix2[1] = vy;
vmatrix2[3] = -vy;
for (i = 0; i < ret->nvertices; i++) {
MATMUL(ret->vertices + 3*i, vmatrix, ret->vertices + 3*i);
MATMUL(ret->vertices + 3*i, amatrix, ret->vertices + 3*i);
MATMUL(ret->vertices + 3*i, vmatrix2, ret->vertices + 3*i);
}
for (i = 0; i < ret->nfaces; i++) {
MATMUL(ret->normals + 3*i, vmatrix, ret->normals + 3*i);
MATMUL(ret->normals + 3*i, amatrix, ret->normals + 3*i);
MATMUL(ret->normals + 3*i, vmatrix2, ret->normals + 3*i);
}
return ret;
}
static char *validate_desc(const game_params *params, const char *desc)
{
int area = grid_area(params->d1, params->d2, solids[params->solid]->order);
int i, j;
i = (area + 3) / 4;
for (j = 0; j < i; j++) {
int c = desc[j];
if (c >= '0' && c <= '9') continue;
if (c >= 'A' && c <= 'F') continue;
if (c >= 'a' && c <= 'f') continue;
return "Not enough hex digits at start of string";
/* NB if desc[j]=='\0' that will also be caught here, so we're safe */
}
if (desc[i] != ',')
return "Expected ',' after hex digits";
i++;
do {
if (desc[i] < '0' || desc[i] > '9')
return "Expected decimal integer after ','";
i++;
} while (desc[i]);
return NULL;
}
static game_state *new_game(midend *me, const game_params *params,
const char *desc)
{
game_grid *grid = snew(game_grid);
game_state *state = snew(game_state);
int area;
state->params = *params; /* structure copy */
state->solid = solids[params->solid];
area = grid_area(params->d1, params->d2, state->solid->order);
grid->squares = snewn(area, struct grid_square);
grid->nsquares = 0;
enum_grid_squares(params, add_grid_square_callback, grid);
assert(grid->nsquares == area);
state->grid = grid;
grid->refcount = 1;
state->facecolours = snewn(state->solid->nfaces, int);
memset(state->facecolours, 0, state->solid->nfaces * sizeof(int));
state->bluemask = snewn((state->grid->nsquares + 31) / 32, unsigned long);
memset(state->bluemask, 0, (state->grid->nsquares + 31) / 32 *
sizeof(unsigned long));
/*
* Set up the blue squares and polyhedron position according to
* the game description.
*/
{
const char *p = desc;
int i, j, v;
j = 8;
v = 0;
for (i = 0; i < state->grid->nsquares; i++) {
if (j == 8) {
v = *p++;
if (v >= '0' && v <= '9')
v -= '0';
else if (v >= 'A' && v <= 'F')
v -= 'A' - 10;
else if (v >= 'a' && v <= 'f')
v -= 'a' - 10;
else
break;
}
if (v & j)
SET_SQUARE(state, i, TRUE);
j >>= 1;
if (j == 0)
j = 8;
}
if (*p == ',')
p++;
state->current = atoi(p);
if (state->current < 0 || state->current >= state->grid->nsquares)
state->current = 0; /* got to do _something_ */
}
/*
* Align the polyhedron with its grid square and determine
* initial key points.
*/
{
int pkey[4];
int ret;
ret = align_poly(state->solid, &state->grid->squares[state->current], pkey);
assert(ret);
state->dpkey[0] = state->spkey[0] = pkey[0];
state->dpkey[1] = state->spkey[0] = pkey[1];
state->dgkey[0] = state->sgkey[0] = 0;
state->dgkey[1] = state->sgkey[0] = 1;
}
state->previous = state->current;
state->angle = 0.0;
state->completed = 0;
state->movecount = 0;
return state;
}
static game_state *dup_game(const game_state *state)
{
game_state *ret = snew(game_state);
ret->params = state->params; /* structure copy */
ret->solid = state->solid;
ret->facecolours = snewn(ret->solid->nfaces, int);
memcpy(ret->facecolours, state->facecolours,
ret->solid->nfaces * sizeof(int));
ret->current = state->current;
ret->grid = state->grid;
ret->grid->refcount++;
ret->bluemask = snewn((ret->grid->nsquares + 31) / 32, unsigned long);
memcpy(ret->bluemask, state->bluemask, (ret->grid->nsquares + 31) / 32 *
sizeof(unsigned long));
ret->dpkey[0] = state->dpkey[0];
ret->dpkey[1] = state->dpkey[1];
ret->dgkey[0] = state->dgkey[0];
ret->dgkey[1] = state->dgkey[1];
ret->spkey[0] = state->spkey[0];
ret->spkey[1] = state->spkey[1];
ret->sgkey[0] = state->sgkey[0];
ret->sgkey[1] = state->sgkey[1];
ret->previous = state->previous;
ret->angle = state->angle;
ret->completed = state->completed;
ret->movecount = state->movecount;
return ret;
}
static void free_game(game_state *state)
{
if (--state->grid->refcount <= 0) {
sfree(state->grid->squares);
sfree(state->grid);
}