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
|
/*
* Standalone test program for spectre.c.
*/
#include <assert.h>
#ifdef NO_TGMATH_H
# include <math.h>
#else
# include <tgmath.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "puzzles.h"
#include "spectre-internal.h"
#include "spectre-tables-manual.h"
#include "spectre-tables-auto.h"
#include "spectre-help.h"
static void step_tests(void)
{
SpectreContext ctx[1];
random_state *rs;
SpectreCoords *sc;
unsigned outedge;
rs = random_new("12345", 5);
spectrectx_init_random(ctx, rs);
/* Simplest possible transition: between the two Spectres making
* up a G hex. */
sc = spectre_coords_new();
spectre_coords_make_space(sc, 1);
sc->index = 0;
sc->nc = 1;
sc->c[0].type = HEX_G;
sc->c[0].index = -1;
spectrectx_step(ctx, sc, 12, &outedge);
assert(outedge == 5);
assert(sc->index == 1);
assert(sc->nc == 1);
assert(sc->c[0].type == HEX_G);
assert(sc->c[0].index == -1);
spectre_coords_free(sc);
/* Test the double Spectre transition. Here, within a F superhex,
* we attempt to step from the G subhex to the S one, in such a
* way that the place where we enter the Spectre corresponding to
* the S hex is on its spur of detached edge, causing us to
* immediately transition back out of the other side of that spur
* and end up in the D subhex instead. */
sc = spectre_coords_new();
spectre_coords_make_space(sc, 2);
sc->index = 1;
sc->nc = 2;
sc->c[0].type = HEX_G;
sc->c[0].index = 2;
sc->c[1].type = HEX_F;
sc->c[1].index = -1;
spectrectx_step(ctx, sc, 1, &outedge);
assert(outedge == 6);
assert(sc->index == 0);
assert(sc->nc == 2);
assert(sc->c[0].type == HEX_D);
assert(sc->c[0].index == 5);
assert(sc->c[1].type == HEX_F);
assert(sc->c[1].index == -1);
spectre_coords_free(sc);
/* However, _this_ transition leaves the same G subhex by the same
* edge of the hexagon, but further along it, so that we land in
* the S Spectre and stay there, without needing a double
* transition. */
sc = spectre_coords_new();
spectre_coords_make_space(sc, 2);
sc->index = 1;
sc->nc = 2;
sc->c[0].type = HEX_G;
sc->c[0].index = 2;
sc->c[1].type = HEX_F;
sc->c[1].index = -1;
spectrectx_step(ctx, sc, 13, &outedge);
assert(outedge == 4);
assert(sc->index == 0);
assert(sc->nc == 2);
assert(sc->c[0].type == HEX_S);
assert(sc->c[0].index == 3);
assert(sc->c[1].type == HEX_F);
assert(sc->c[1].index == -1);
spectre_coords_free(sc);
/* A couple of randomly generated transition tests that go a long
* way up the stack. */
sc = spectre_coords_new();
spectre_coords_make_space(sc, 7);
sc->index = 0;
sc->nc = 7;
sc->c[0].type = HEX_S;
sc->c[0].index = 3;
sc->c[1].type = HEX_Y;
sc->c[1].index = 7;
sc->c[2].type = HEX_Y;
sc->c[2].index = 4;
sc->c[3].type = HEX_Y;
sc->c[3].index = 4;
sc->c[4].type = HEX_F;
sc->c[4].index = 0;
sc->c[5].type = HEX_X;
sc->c[5].index = 1;
sc->c[6].type = HEX_G;
sc->c[6].index = -1;
spectrectx_step(ctx, sc, 13, &outedge);
assert(outedge == 12);
assert(sc->index == 0);
assert(sc->nc == 7);
assert(sc->c[0].type == HEX_Y);
assert(sc->c[0].index == 1);
assert(sc->c[1].type == HEX_P);
assert(sc->c[1].index == 1);
assert(sc->c[2].type == HEX_D);
assert(sc->c[2].index == 5);
assert(sc->c[3].type == HEX_Y);
assert(sc->c[3].index == 4);
assert(sc->c[4].type == HEX_X);
assert(sc->c[4].index == 7);
assert(sc->c[5].type == HEX_S);
assert(sc->c[5].index == 3);
assert(sc->c[6].type == HEX_G);
assert(sc->c[6].index == -1);
spectre_coords_free(sc);
sc = spectre_coords_new();
spectre_coords_make_space(sc, 7);
sc->index = 0;
sc->nc = 7;
sc->c[0].type = HEX_Y;
sc->c[0].index = 7;
sc->c[1].type = HEX_F;
sc->c[1].index = 6;
sc->c[2].type = HEX_Y;
sc->c[2].index = 4;
sc->c[3].type = HEX_X;
sc->c[3].index = 7;
sc->c[4].type = HEX_L;
sc->c[4].index = 0;
sc->c[5].type = HEX_S;
sc->c[5].index = 3;
sc->c[6].type = HEX_F;
sc->c[6].index = -1;
spectrectx_step(ctx, sc, 0, &outedge);
assert(outedge == 1);
assert(sc->index == 0);
assert(sc->nc == 7);
assert(sc->c[0].type == HEX_P);
assert(sc->c[0].index == 1);
assert(sc->c[1].type == HEX_F);
assert(sc->c[1].index == 0);
assert(sc->c[2].type == HEX_Y);
assert(sc->c[2].index == 7);
assert(sc->c[3].type == HEX_F);
assert(sc->c[3].index == 0);
assert(sc->c[4].type == HEX_G);
assert(sc->c[4].index == 2);
assert(sc->c[5].type == HEX_D);
assert(sc->c[5].index == 5);
assert(sc->c[6].type == HEX_F);
assert(sc->c[6].index == -1);
spectre_coords_free(sc);
spectrectx_cleanup(ctx);
random_free(rs);
}
struct genctx {
Graphics *gr;
FILE *fp; /* for non-graphical output modes */
random_state *rs;
Coord xmin, xmax, ymin, ymax;
};
static void gctx_set_size(
struct genctx *gctx, int width, int height, double scale, bool centre,
int *xmin, int *xmax, int *ymin, int *ymax)
{
if (centre) {
*xmax = ceil(width/(2*scale));
*xmin = -*xmax;
*ymax = ceil(height/(2*scale));
*ymin = -*ymax;
} else {
*xmin = *ymin = 0;
*xmax = ceil(width/scale);
*ymax = ceil(height/scale);
}
/* point_x() and point_y() double their output to avoid having
* to use fractions, so double the bounds we'll compare their
* results against */
gctx->xmin.c1 = *xmin * 2; gctx->xmin.cr3 = 0;
gctx->xmax.c1 = *xmax * 2; gctx->xmax.cr3 = 0;
gctx->ymin.c1 = *ymin * 2; gctx->ymin.cr3 = 0;
gctx->ymax.c1 = *ymax * 2; gctx->ymax.cr3 = 0;
}
static bool callback(void *vctx, const Spectre *spec)
{
struct genctx *gctx = (struct genctx *)vctx;
size_t i;
for (i = 0; i < 14; i++) {
Point p = spec->vertices[i];
Coord x = point_x(p), y = point_y(p);
if (coord_cmp(x, gctx->xmin) >= 0 && coord_cmp(x, gctx->xmax) <= 0 &&
coord_cmp(y, gctx->ymin) >= 0 && coord_cmp(y, gctx->ymax) <= 0)
goto ok;
}
return false;
ok:
gr_draw_spectre_from_coords(gctx->gr, spec->sc, spec->vertices);
if (gctx->fp) {
/*
* Emit calls to a made-up Python 'spectre()' function which
* takes the following parameters:
*
* - lowest-level hexagon type (one-character string)
* - index of Spectre within hexagon (0 or rarely 1)
* - array of 14 point coordinates. Each is a 2-tuple
* containing x and y. Each of those in turn is a 2-tuple
* containing coordinates of 1 and sqrt(3).
*/
fprintf(gctx->fp, "spectre('%s', %d, [",
hex_names[spec->sc->c[0].type], spec->sc->index);
for (i = 0; i < 14; i++) {
Point p = spec->vertices[i];
Coord x = point_x(p), y = point_y(p);
fprintf(gctx->fp, "%s((%d,%d),(%d,%d))", i ? ", " : "",
x.c1, x.cr3, y.c1, y.cr3);
}
fprintf(gctx->fp, "])\n");
}
return true;
}
static void spectrectx_init_random_with_four_colouring(
SpectreContext *ctx, random_state *rs)
{
spectrectx_init_random(ctx, rs);
ctx->prototype->hex_colour = random_upto(rs, 3);
ctx->prototype->prev_hex_colour = (ctx->prototype->hex_colour + 1 +
random_upto(rs, 2)) % 3;
ctx->prototype->incoming_hex_edge = random_upto(rs, 2);
}
static void generate_bfs(struct genctx *gctx)
{
SpectreContext ctx[1];
spectrectx_init_random_with_four_colouring(ctx, gctx->rs);
spectrectx_generate(ctx, callback, gctx);
spectrectx_cleanup(ctx);
}
static inline Point reflected(Point p)
{
/*
* This reflection operation is used as a conjugation by
* periodic_cheat(). For that purpose, it doesn't matter _what_
* reflection it is, only that it reverses sense.
*
* generate_raster() also uses it to conjugate between the 'find
* edges intersecting a horizontal line' and 'ditto vertical'
* operations, so for that purpose, it wants to be the specific
* reflection about the 45-degree line that swaps the positive x-
* and y-axes.
*/
Point r;
size_t i;
for (i = 0; i < 4; i++)
r.coeffs[i] = p.coeffs[3-i];
return r;
}
static void reflect_spectre(Spectre *spec)
{
size_t i;
for (i = 0; i < 14; i++)
spec->vertices[i] = reflected(spec->vertices[i]);
}
static void periodic_cheat(struct genctx *gctx)
{
Spectre start, sh, sv;
size_t i;
start.sc = NULL;
{
Point u = {{ 0, 0, 0, 0 }};
Point v = {{ 1, 0, 0, 1 }};
v = point_mul(v, point_rot(1));
spectre_place(&start, u, v, 0);
}
sh = start;
while (callback(gctx, &sh)) {
sv = sh;
i = 0;
do {
if (i) {
spectre_place(&sv, sv.vertices[6], sv.vertices[7], 0);
} else {
spectre_place(&sv, reflected(sv.vertices[6]),
reflected(sv.vertices[7]), 0);
reflect_spectre(&sv);
}
i ^= 1;
} while (callback(gctx, &sv));
sv = sh;
i = 0;
do {
if (i) {
spectre_place(&sv, sv.vertices[0], sv.vertices[1], 6);
} else {
spectre_place(&sv, reflected(sv.vertices[0]),
reflected(sv.vertices[1]), 6);
reflect_spectre(&sv);
}
i ^= 1;
} while (callback(gctx, &sv));
spectre_place(&sh, sh.vertices[12], sh.vertices[11], 4);
}
sh = start;
do {
spectre_place(&sh, sh.vertices[5], sh.vertices[4], 11);
sv = sh;
i = 0;
do {
if (i) {
spectre_place(&sv, sv.vertices[6], sv.vertices[7], 0);
} else {
spectre_place(&sv, reflected(sv.vertices[6]),
reflected(sv.vertices[7]), 0);
reflect_spectre(&sv);
}
i ^= 1;
} while (callback(gctx, &sv));
sv = sh;
i = 0;
do {
if (i) {
spectre_place(&sv, sv.vertices[0], sv.vertices[1], 6);
} else {
spectre_place(&sv, reflected(sv.vertices[0]),
reflected(sv.vertices[1]), 6);
reflect_spectre(&sv);
}
i ^= 1;
} while (callback(gctx, &sv));
} while (callback(gctx, &sh));
}
static Spectre *spectre_copy(const Spectre *orig)
{
Spectre *copy = snew(Spectre);
memcpy(copy->vertices, orig->vertices, sizeof(copy->vertices));
copy->sc = spectre_coords_copy(orig->sc);
copy->next = NULL; /* not used in this tool */
return copy;
}
static size_t find_crossings(struct genctx *gctx, const Spectre *spec, Coord y,
size_t direction, unsigned *edges_out)
{
/*
* Find edges of this Spectre which cross the horizontal line
* specified by the coordinate y.
*
* For tie-breaking purposes, we're treating the line as actually
* being at y + epsilon, so that a line with one endpoint _on_
* that coordinate is counted as crossing it if it goes upwards,
* and not downwards. Put another way, we seek edges one of whose
* vertices is < y and the other >= y.
*
* Also, we're only interested in crossings in a particular
* direction, specified by 'direction' being 0 or 1.
*/
size_t i, j;
struct Edge {
unsigned edge;
/* Location of the crossing point, as the ratio of two Coord */
Coord n, d;
} edges[14];
size_t nedges = 0;
for (i = 0; i < 14; i++) {
Coord yc[2], d[2];
yc[0] = point_y(spec->vertices[i]);
yc[1] = point_y(spec->vertices[(i+1) % 14]);
for (j = 0; j < 2; j++)
d[j] = coord_sub(yc[j], y);
if (coord_sign(d[1-direction]) >= 0 && coord_sign(d[direction]) < 0) {
Coord a0 = coord_abs(d[0]), a1 = coord_abs(d[1]);
Coord x0 = point_x(spec->vertices[i]);
Coord x1 = point_x(spec->vertices[(i+1) % 14]);
edges[nedges].d = coord_add(a0, a1);
edges[nedges].n = coord_add(coord_mul(a1, x0), coord_mul(a0, x1));
edges[nedges].edge = i;
nedges++;
/*
* Insertion sort: swap this edge backwards in the array
* until it's in the right order.
*/
{
size_t j = nedges - 1;
while (j > 0 && coord_cmp(
coord_mul(edges[j-1].n, edges[j].d),
coord_mul(edges[j].n, edges[j-1].d)) > 0) {
struct Edge tmp = edges[j-1];
edges[j-1] = edges[j];
edges[j] = tmp;
j--;
}
}
}
}
for (i = 0; i < nedges; i++)
edges_out[i] = edges[i].edge;
return nedges;
}
static void raster_emit(struct genctx *gctx, const Spectre *spec,
Coord y, unsigned edge)
{
unsigned edges[14];
size_t nedges;
Coord yprev = coord_sub(y, coord_construct(2, 4));
if (find_crossings(gctx, spec, yprev, true, edges))
return; /* we've seen this on a previous raster_x pass */
if (edge != (unsigned)-1) {
nedges = find_crossings(gctx, spec, y, false, edges);
assert(nedges > 0);
if (edge != edges[0])
return; /* we've seen this before within the same raster_x pass */
}
callback(gctx, spec);
}
static void raster_x(struct genctx *gctx, SpectreContext *ctx,
const Spectre *start, Coord *yptr, Coord xlimit)
{
Spectre *curr, *new;
Coord y;
size_t i;
unsigned incoming_edge;
/*
* Find out if this Spectre intersects our current
* y-coordinate.
*/
for (i = 0; i < 14; i++)
if (coord_cmp(point_y(start->vertices[i]), *yptr) > 0)
break;
if (i == 14) {
/*
* No, this Spectre is still below the start line.
*/
return;
}
/*
* It does! Start an x iteration here, and increment y by 2 + 4
* sqrt(3), which is the smallest possible y-extent of any
* rotation of our starting Spectre.
*/
y = *yptr;
*yptr = coord_add(*yptr, coord_construct(2, 4));
curr = spectre_copy(start);
incoming_edge = -1;
while (true) {
unsigned edges[14];
size_t nedges;
raster_emit(gctx, curr, y, incoming_edge);
nedges = find_crossings(gctx, curr, y, true, edges);
assert(nedges > 0);
for (i = 0; i+1 < nedges; i++) {
new = spectre_adjacent(ctx, curr, edges[i], &incoming_edge);
raster_emit(gctx, new, y, incoming_edge);
spectre_free(new);
}
new = spectre_adjacent(ctx, curr, edges[nedges-1], &incoming_edge);
spectre_free(curr);
curr = new;
/*
* Find out whether this Spectre is entirely beyond the
* x-limit.
*/
for (i = 0; i < 14; i++)
if (coord_cmp(point_x(curr->vertices[i]), xlimit) < 0)
break;
if (i == 14) /* no vertex broke that loop */
break;
}
spectre_free(curr);
}
static void raster_y(struct genctx *gctx, SpectreContext *ctx,
const Spectre *start, Coord x, Coord ylimit,
Coord *yptr, Coord xlimit)
{
Spectre *curr, *new;
curr = spectre_copy(start);
while (true) {
unsigned edges[14];
size_t i, nedges;
raster_x(gctx, ctx, curr, yptr, xlimit);
reflect_spectre(curr);
nedges = find_crossings(gctx, curr, x, false, edges);
reflect_spectre(curr);
assert(nedges > 0);
for (i = 0; i+1 < nedges; i++) {
new = spectre_adjacent(ctx, curr, edges[i], NULL);
raster_x(gctx, ctx, new, yptr, xlimit);
spectre_free(new);
}
new = spectre_adjacent(ctx, curr, edges[nedges-1], NULL);
spectre_free(curr);
curr = new;
/*
* Find out whether this Spectre is entirely beyond the
* y-limit.
*/
for (i = 0; i < 14; i++)
if (coord_cmp(point_y(curr->vertices[i]), ylimit) < 0)
break;
if (i == 14) /* no vertex broke that loop */
break;
}
spectre_free(curr);
}
static void generate_raster(struct genctx *gctx)
{
SpectreContext ctx[1];
Spectre *start;
Coord y = coord_integer(-10);
spectrectx_init_random_with_four_colouring(ctx, gctx->rs);
start = spectre_initial(ctx);
/*
* Move the starting Spectre down and left a bit, so that edge
* effects causing a few Spectres to be missed on the initial
* passes won't affect the overall result.
*/
{
Point offset = {{ -5, 0, 0, -5 }};
size_t i;
for (i = 0; i < 14; i++)
start->vertices[i] = point_add(start->vertices[i], offset);
}
raster_y(gctx, ctx, start, coord_integer(-10), gctx->ymax, &y, gctx->xmax);
spectre_free(start);
spectrectx_cleanup(ctx);
}
static void generate_hexes(struct genctx *gctx)
{
SpectreContext ctx[1];
spectrectx_init_random(ctx, gctx->rs);
SpectreCoords *sc;
unsigned orient, outedge, inedge;
bool printed_any = false;
size_t r = 1, ri = 0, rj = 0;
Point centre = {{ 0, 0, 0, 0 }};
const Point six = {{ 6, 0, 0, 0 }};
sc = spectre_coords_copy(ctx->prototype);
orient = random_upto(gctx->rs, 6);
while (true) {
Point top = {{ -2, 0, 4, 0 }};
Point vertices[6];
bool print_this = false;
size_t i;
for (i = 0; i < 6; i++) {
vertices[i] = point_add(centre, point_mul(
top, point_rot(2 * (orient + i))));
Coord x = point_x(vertices[i]), y = point_y(vertices[i]);
if (coord_cmp(x, gctx->xmin) >= 0 &&
coord_cmp(x, gctx->xmax) <= 0 &&
coord_cmp(y, gctx->ymin) >= 0 &&
coord_cmp(y, gctx->ymax) <= 0)
print_this = true;
}
if (print_this) {
printed_any = true;
gr_draw_hex(gctx->gr, -1, sc->c[0].type, vertices);
}
/*
* Decide which way to step next. We spiral outwards from a
* central hexagon.
*/
outedge = (ri == 0 && rj == 0) ? 5 : ri;
if (++rj >= r) {
rj = 0;
if (++ri >= 6) {
ri = 0;
if (!printed_any)
break;
printed_any = false;
++r;
}
}
spectrectx_step_hex(ctx, sc, 0, (outedge + 6 - orient) % 6, &inedge);
orient = (outedge + 9 - inedge) % 6;
centre = point_add(centre, point_mul(six, point_rot(4 + 2 * outedge)));
}
spectre_coords_free(sc);
spectrectx_cleanup(ctx);
}
int main(int argc, char **argv)
{
const char *random_seed = "12345";
const char *outfile = "-";
bool four_colour = false;
enum {
TESTS, TILING_BFS, TILING_RASTER, CHEAT, HEXES
} mode = TILING_RASTER;
enum { SVG, PYTHON } outmode = SVG;
double scale = 10, linewidth = 1.5;
int width = 1024, height = 768;
bool arcs = false;
while (--argc > 0) {
const char *arg = *++argv;
if (!strcmp(arg, "--help")) {
printf(" usage: spectre-test [FIXME]\n"
" also: spectre-test --test\n");
return 0;
} else if (!strcmp(arg, "--test")) {
mode = TESTS;
} else if (!strcmp(arg, "--hex")) {
mode = HEXES;
} else if (!strcmp(arg, "--bfs")) {
mode = TILING_BFS;
} else if (!strcmp(arg, "--cheat")) {
mode = CHEAT;
} else if (!strcmp(arg, "--python")) {
outmode = PYTHON;
} else if (!strcmp(arg, "--arcs")) {
arcs = true;
} else if (!strncmp(arg, "--seed=", 7)) {
random_seed = arg+7;
} else if (!strcmp(arg, "--fourcolour")) {
four_colour = true;
} else if (!strncmp(arg, "--scale=", 8)) {
scale = atof(arg+8);
} else if (!strncmp(arg, "--width=", 8)) {
width = atof(arg+8);
} else if (!strncmp(arg, "--height=", 9)) {
height = atof(arg+9);
} else if (!strncmp(arg, "--linewidth=", 12)) {
linewidth = atof(arg+12);
} else if (!strcmp(arg, "-o")) {
if (--argc <= 0) {
fprintf(stderr, "expected argument to '%s'\n", arg);
return 1;
}
outfile = *++argv;
} else {
fprintf(stderr, "unexpected extra argument '%s'\n", arg);
return 1;
}
}
switch (mode) {
case TESTS: {
step_tests();
break;
}
case TILING_BFS:
case TILING_RASTER:
case CHEAT: {
struct genctx gctx[1];
bool close_output = false;
int xmin, xmax, ymin, ymax;
gctx_set_size(gctx, width, height, scale, (mode != TILING_RASTER),
&xmin, &xmax, &ymin, &ymax);
switch (outmode) {
case SVG:
gctx->gr = gr_new(outfile, xmin, xmax, ymin, ymax, scale);
gctx->gr->number_cells = false;
gctx->gr->four_colour = four_colour;
gctx->gr->linewidth = linewidth;
gctx->gr->arcs = arcs;
gctx->fp = NULL;
break;
case PYTHON:
gctx->gr = NULL;
if (!strcmp(outfile, "-")) {
gctx->fp = stdout;
} else {
gctx->fp = fopen(outfile, "w");
close_output = true;
}
break;
}
gctx->rs = random_new(random_seed, strlen(random_seed));
switch (mode) {
case TILING_RASTER:
generate_raster(gctx);
break;
case TILING_BFS:
generate_bfs(gctx);
break;
case CHEAT:
periodic_cheat(gctx);
break;
default: /* shouldn't happen */
break;
}
random_free(gctx->rs);
gr_free(gctx->gr);
if (close_output)
fclose(gctx->fp);
break;
}
case HEXES: {
struct genctx gctx[1];
int xmin, xmax, ymin, ymax;
gctx_set_size(gctx, width, height, scale, true,
&xmin, &xmax, &ymin, &ymax);
gctx->gr = gr_new(outfile, xmin, xmax, ymin, ymax, scale);
gctx->gr->jigsaw_mode = true;
gctx->gr->number_edges = false;
gctx->gr->linewidth = linewidth;
gctx->rs = random_new(random_seed, strlen(random_seed));
generate_hexes(gctx); /* FIXME: bounds */
random_free(gctx->rs);
gr_free(gctx->gr);
break;
}
}
}
|