-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
crystal.cpp
1821 lines (1529 loc) · 52.3 KB
/
crystal.cpp
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
// Hyperbolic Rogue -- Crystal geometries
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file crystal.cpp
* \brief Multi-dimensional (aka crystal) geometries.
*/
#include "hyper.h"
namespace hr {
EX namespace crystal {
#if HDR
static constexpr int MAXDIM = 7;
static constexpr int MAX_EDGE_CRYSTAL = 2 * MAXDIM;
struct coord : public array<int, MAXDIM> {
coord operator + (coord b) { for(int i=0; i<MAXDIM; i++) b[i] += self[i]; return b; }
coord operator - (coord b) { for(int i=0; i<MAXDIM; i++) b[i] = self[i] - b[i]; return b; }
coord operator * (int x) { coord res; for(int i=0; i<MAXDIM; i++) res[i] = x * self[i]; return res; }
};
static constexpr coord c0 = {};
struct ldcoord : public array<ld, MAXDIM> {
friend ldcoord operator + (ldcoord a, ldcoord b) { ldcoord r; for(int i=0; i<MAXDIM; i++) r[i] = a[i] + b[i]; return r; }
friend ldcoord operator - (ldcoord a, ldcoord b) { ldcoord r; for(int i=0; i<MAXDIM; i++) r[i] = a[i] - b[i]; return r; }
friend ldcoord operator * (ldcoord a, ld v) { ldcoord r; for(int i=0; i<MAXDIM; i++) r[i] = a[i] * v; return r; }
friend ldcoord operator / (ldcoord a, ld v) { ldcoord r; for(int i=0; i<MAXDIM; i++) r[i] = a[i] / v; return r; }
friend ld operator | (ldcoord a, ldcoord b) { ld r=0; for(int i=0; i<MAXDIM; i++) r += a[i] * b[i]; return r; }
};
static constexpr ldcoord ldc0 = {};
#endif
#if CAP_CRYSTAL
/** Crystal can be bitruncated either by changing variation to bitruncated.
* In case of the 4D Crystal, the standard HyperRogue bitruncation becomes
* confused by having both the original and new vertices of degree 8.
* Hence Crystal implements its own bitruncation, which is selected/checked
* by setting ginf[gCrystal].vertex to 3. Additionally, this lets us double
* bitruncate.
* Function pure() checks for both kinds of bitruncation (or any other variations).
*/
EX bool pure() {
return PURE && ginf[gCrystal].vertex == 4;
}
EX bool view_coordinates = false;
bool view_east = false;
EX bool used_compass_inside;
EX ldcoord told(coord c) { ldcoord a; for(int i=0; i<MAXDIM; i++) a[i] = c[i]; return a; }
// strange number to prevent weird acting in case of precision errors
coord roundcoord(ldcoord c) { coord a; for(int i=0; i<MAXDIM; i++) a[i] = floor(c[i] + .5136); return a; }
EX ld compass_probability = 1;
int tocode(int cname) { return (1 << (cname >> 1)); }
void resize2(vector<vector<int>>& v, int a, int b, int z) {
v.clear();
v.resize(a);
for(auto& w: v) w.resize(b, z);
}
/** in the "pure" form, the adjacent vertices are internaly spaced by 2 */
const int FULLSTEP = 2;
/** to make space for the additional vertices which are added in the bitruncated version */
const int HALFSTEP = 1;
/** with variations, the connections of the vertex at coordinate v+FULLSTEP mirror the connections
* of the vertex at coordinate v. Therefore, the period of our construction is actually 2*FULLSTEP. */
const int PERIOD = 2 * FULLSTEP;
struct crystal_structure {
int dir;
int dim;
vector<vector<int>> cmap;
vector<vector<int>> next;
vector<vector<int>> prev;
vector<vector<int>> order;
void coord_to_next() {
resize2(next, 1<<dim, 2*dim, -1);
for(int a=0; a<(1<<dim); a++)
for(int b=0; b<dir; b++)
next[a][cmap[a][b]] = cmap[a][(b+1)%dir];
println(hlog, next);
}
void next_to_coord() {
resize2(cmap, 1<<dim, dir, -1);
for(int a=0; a<(1<<dim); a++) {
int at = 0;
for(int b=0; b<dir; b++) {
cmap[a][b] = at;
at = next[a][at];
}
}
println(hlog, "coordinate map is:\n", cmap);
}
void next_to_prev() {
resize2(prev, 1<<dim, 2*dim, -1);
for(int a=0; a<(1<<dim); a++)
for(int b=0; b<dir; b++) {
if(next[a][b] != -1)
prev[a][next[a][b]] = b;
}
}
void coord_to_order() {
println(hlog, dir, dim);
resize2(order, 1<<dim, 2*dim, -1);
for(int a=0; a<(1<<dim); a++)
for(int b=0; b<dir; b++)
order[a][cmap[a][b]] = b;
println(hlog, order);
}
int count_bugs() {
int bugcount = 0;
for(int a=0; a<(1<<dim); a++)
for(int b=0; b<2*dim; b++) {
if(next[a][b] == -1) continue;
int qa = a, qb = b;
for(int i=0; i<4; i++) {
if(i == 2 && (qb != (b^1))) bugcount++;
qa ^= tocode(qb);
qb ^= 1;
qb = next[qa][qb];
}
if(a != qa || b != qb) bugcount++;
}
return bugcount;
}
void next_insert(int a, int at, int val) {
int pd = next[a].size();
next[a].resize(pd + 2);
next[a][val] = next[a][at];
next[a][at] = val;
next[a][val^1] = next[a][at^1];
next[a][at^1] = val^1;
prev[a].resize(pd + 2);
prev[a][val] = at;
prev[a][next[a][val]] = val;
prev[a][val^1] = at^1;
prev[a][next[a][val^1]] = val^1;
}
void prev_insert(int a, int at, int val) {
next_insert(a, prev[a][at], val);
}
int errors;
crystal_structure() { errors = 0; }
bool may_next_insert(int a, int at, int val) {
if(isize(next[a]) != dir) {
next_insert(a, at, val);
return true;
}
else if(next[a][at] != val) errors++;
return false;
}
bool may_prev_insert(int a, int at, int val) {
if(isize(prev[a]) != dir) {
prev_insert(a, at, val);
return true;
}
else if(prev[a][at] != val) errors++;
return false;
}
void add_dimension_to(crystal_structure& poor) {
dir = poor.dir + 2;
dim = poor.dim + 1;
printf("Building dimension %d\n", dim);
next.resize(1<<dim);
prev.resize(1<<dim);
int mask = (1<<poor.dim) - 1;
int mm = tocode(poor.dir);
for(int i=0; i<(1<<dim); i++) {
if(i < mm)
next[i] = poor.next[i&mask], prev[i] = poor.prev[i&mask];
else
next[i] = poor.prev[i&mask], prev[i] = poor.next[i&mask];
}
next_insert(0, 0, poor.dir);
for(int s=2; s<1<<(dim-2); s+=2) {
if(next[s][0] < 4)
prev_insert(s, 0, poor.dir);
else
next_insert(s, 0, poor.dir);
}
// printf("next[%d][%d] = %d\n", 4, 2, next[4][2]);
for(int s=0; s<8; s++) for(int a=0; a<(1<<dim); a++) if(isize(next[a]) > poor.dir) {
int which = next[a][poor.dir];
int a1 = a ^ tocode(which);
may_next_insert(a1, which^1, poor.dir);
may_next_insert(a ^ mm, which, poor.dir^1);
which = prev[a][poor.dir];
a1 = a ^ tocode(which);
may_prev_insert(a1, which^1, poor.dir);
}
// println(hlog, next);
if(errors) { printf("errors: %d\n", errors); exit(1);; }
int unf = 0;
for(int a=0; a<(1<<dim); a++) if(isize(next[a]) == poor.dir) {
if(!unf) printf("unf: ");
printf("%d ", a);
unf ++;
}
if(unf) { printf("\n"); exit(2); }
for(int a=0; a<(1<<dim); a++) for(int b=0; b<dir; b++)
if(prev[a][next[a][b]] != b) {
println(hlog, next[a], prev[a]);
printf("next/prev %d\n", a);
exit(3);
}
if(count_bugs()) {
printf("bugs reported: %d\n", count_bugs());
exit(4);
}
}
void remove_half_dimension() {
dir--;
for(int i=0; i<(1<<dim); i++) {
int take_what = dir-1;
if(i >= (1<<(dim-1))) take_what = dir;
next[i][prev[i][take_what]] = next[i][take_what],
prev[i][next[i][take_what]] = prev[i][take_what],
next[i].resize(dir),
prev[i].resize(dir);
}
}
void build() {
dir = 4;
dim = 2;
next.resize(4, {2,3,1,0});
next_to_prev();
while(dir < S7) {
crystal_structure csx = std::move(*this);
add_dimension_to(csx);
}
if(dir > S7) remove_half_dimension();
next_to_coord();
coord_to_order();
coord_to_next();
if(count_bugs()) {
printf("bugs found\n");
}
if(dir > MAX_EDGE_CRYSTAL || dim > MAXDIM) {
printf("Dimension or directions exceeded -- I have generated it, but won't play");
exit(0);
}
}
};
struct lwalker {
crystal_structure& cs;
int id;
int spin;
lwalker(crystal_structure& cs) : cs(cs) {}
void operator = (const lwalker& x) { id = x.id; spin = x.spin; }
constexpr lwalker(const lwalker& l) : cs(l.cs), id(l.id), spin(l.spin) {}
};
lwalker operator +(lwalker a, int v) { a.spin = gmod(a.spin + v, a.cs.dir); return a; }
lwalker operator +(lwalker a, wstep_t) {
a.spin = a.cs.cmap[a.id][a.spin];
a.id ^= tocode(a.spin);
a.spin = a.cs.order[a.id][a.spin^1];
return a;
}
coord add(coord c, lwalker a, int val) {
int code = a.cs.cmap[a.id][a.spin];
c[code>>1] += ((code&1) ? val : -val);
return c;
}
coord add(coord c, int cname, int val) {
int dim = (cname>>1);
c[dim] = (c[dim] + (cname&1?val:-val));
return c;
}
ld sqhypot2(crystal_structure& cs, ldcoord co1, ldcoord co2) {
int result = 0;
for(int a=0; a<cs.dim; a++) result += (co1[a] - co2[a]) * (co1[a] - co2[a]);
return result;
}
static constexpr int Modval = 64;
struct east_structure {
map<coord, int> data;
int Xmod, cycle;
int zeroshift;
int coordid;
};
int fiftyrule(coord c) {
int res[256] = {
1,-1,32,-1,-1, 2,-1,35,32,-1, 1,-1,-1,35,-1, 2,
-1,-1,-1,-1, 4,-1,36,-1,-1,-1,-1,-1,36,-1, 4,-1,
32,-1, 1,-1,-1,34,-1, 3, 1,-1,32,-1,-1, 3,-1,34,
-1,-1,-1,-1,36,-1, 4,-1,-1,-1,-1,-1, 4,-1,36,-1,
-1, 4,-1,36,-1,-1,-1,-1,-1,36,-1, 4,-1,-1,-1,-1,
3,-1,35,-1,-1,-1,-1,-1,35,-1, 3,-1,-1,-1,-1,-1,
-1,36,-1, 4,-1,-1,-1,-1,-1, 4,-1,36,-1,-1,-1,-1,
34,-1, 2,-1,-1,-1,-1,-1, 2,-1,34,-1,-1,-1,-1,-1,
32,-1, 1,-1,-1,34,-1, 3, 1,-1,32,-1,-1, 3,-1,34,
-1,-1,-1,-1,36,-1, 4,-1,-1,-1,-1,-1, 4,-1,36,-1,
1,-1,32,-1,-1, 2,-1,35,32,-1, 1,-1,-1,35,-1, 2,
-1,-1,-1,-1, 4,-1,36,-1,-1,-1,-1,-1,36,-1, 4,-1,
-1,36,-1, 4,-1,-1,-1,-1,-1, 4,-1,36,-1,-1,-1,-1,
34,-1, 2,-1,-1,-1,-1,-1, 2,-1,34,-1,-1,-1,-1,-1,
-1, 4,-1,36,-1,-1,-1,-1,-1,36,-1, 4,-1,-1,-1,-1,
3,-1,35,-1,-1,-1,-1,-1,35,-1, 3,-1,-1,-1,-1,-1,
};
int index = 0;
for(int i=0; i<4; i++) index += (c[i] & 3) << (2 * i);
if(res[index] == -1) exit(1);
return res[index];
}
bool is_bi(crystal_structure& cs, coord co);
#if MAXMDIM >= 4
typedef array<coord, 12> shifttable;
int ctable[64][6] = {
{0, 1, 2, 3, 4, 5, },
{6, 1, 5, 4, 3, 2, },
{0, 7, 5, 4, 3, 2, },
{6, 7, 2, 3, 4, 5, },
{0, 1, 5, 4, 3, 8, },
{6, 1, 8, 3, 4, 5, },
{0, 7, 8, 3, 4, 5, },
{6, 7, 5, 4, 3, 8, },
{0, 1, 5, 4, 9, 2, },
{6, 1, 2, 9, 4, 5, },
{0, 7, 2, 9, 4, 5, },
{6, 7, 5, 4, 9, 2, },
{0, 1, 8, 9, 4, 5, },
{6, 1, 5, 4, 9, 8, },
{0, 7, 5, 4, 9, 8, },
{6, 7, 8, 9, 4, 5, },
{0, 1, 5, 10, 3, 2, },
{6, 1, 2, 3, 10, 5, },
{0, 7, 2, 3, 10, 5, },
{6, 7, 5, 10, 3, 2, },
{0, 1, 8, 3, 10, 5, },
{6, 1, 5, 10, 3, 8, },
{0, 7, 5, 10, 3, 8, },
{6, 7, 8, 3, 10, 5, },
{0, 1, 2, 9, 10, 5, },
{6, 1, 5, 10, 9, 2, },
{0, 7, 5, 10, 9, 2, },
{6, 7, 2, 9, 10, 5, },
{0, 1, 5, 10, 9, 8, },
{6, 1, 8, 9, 10, 5, },
{0, 7, 8, 9, 10, 5, },
{6, 7, 5, 10, 9, 8, },
{0, 1, 11, 4, 3, 2, },
{6, 1, 2, 3, 4, 11, },
{0, 7, 2, 3, 4, 11, },
{6, 7, 11, 4, 3, 2, },
{0, 1, 8, 3, 4, 11, },
{6, 1, 11, 4, 3, 8, },
{0, 7, 11, 4, 3, 8, },
{6, 7, 8, 3, 4, 11, },
{0, 1, 2, 9, 4, 11, },
{6, 1, 11, 4, 9, 2, },
{0, 7, 11, 4, 9, 2, },
{6, 7, 2, 9, 4, 11, },
{0, 1, 11, 4, 9, 8, },
{6, 1, 8, 9, 4, 11, },
{0, 7, 8, 9, 4, 11, },
{6, 7, 11, 4, 9, 8, },
{0, 1, 2, 3, 10, 11, },
{6, 1, 11, 10, 3, 2, },
{0, 7, 11, 10, 3, 2, },
{6, 7, 2, 3, 10, 11, },
{0, 1, 11, 10, 3, 8, },
{6, 1, 8, 3, 10, 11, },
{0, 7, 8, 3, 10, 11, },
{6, 7, 11, 10, 3, 8, },
{0, 1, 11, 10, 9, 2, },
{6, 1, 2, 9, 10, 11, },
{0, 7, 2, 9, 10, 11, },
{6, 7, 11, 10, 9, 2, },
{0, 1, 8, 9, 10, 11, },
{6, 1, 11, 10, 9, 8, },
{0, 7, 11, 10, 9, 8, },
{6, 7, 8, 9, 10, 11, },
};
shifttable get_canonical(coord co) {
shifttable res;
if(S7 == 12) {
int eid = 0;
for(int a=0; a<6; a++) if(co[a] & 2) eid += (1<<a);
for(int i=0; i<12; i++) res[i] = c0;
for(int i=0; i<6; i++) {
int c = ctable[eid][i];
res[i][c % 6] = (c>=6) ? -2 : 2;
res[6+i][c % 6] = (c>=6) ? 2 : -2;
}
}
else {
for(int i=0; i<4; i++) {
res[i] = c0;
res[i][i] = 2;
res[i+4] = c0;
res[i+4][i] = -2;
}
for(int a=0; a<4; a++) if(co[a] & 2) swap(res[a], res[a+4]);
int bts = 0;
for(int a=0; a<4; a++) if(co[a] & 2) bts++;
if(bts & 1) swap(res[2], res[3]), swap(res[6], res[7]);
}
return res;
}
#endif
EX int crystal_period = 0;
struct hrmap_crystal : hrmap_standard {
heptagon *getOrigin() override { return get_heptagon_at(c0, S7); }
map<heptagon*, coord> hcoords;
map<coord, heptagon*> heptagon_at;
map<int, eLand> landmemo;
map<coord, eLand> landmemo4;
map<cell*, map<cell*, int>> distmemo;
map<cell*, ldcoord> sgc;
cell *camelot_center;
ldcoord camelot_coord;
ld camelot_mul;
crystal_structure cs;
east_structure east;
lwalker makewalker(coord c, int d) {
lwalker a(cs);
a.id = 0;
for(int i=0; i<cs.dim; i++) if(c[i] & FULLSTEP) a.id += (1<<i);
a.spin = d;
return a;
}
bool crystal3() { return WDIM == 3; }
hrmap_crystal() {
#if MAXMDIM >= 4
if(crystal3()) reg3::generate(), cs.dim = S7 / 2; else
#endif
cs.build();
camelot_center = NULL;
}
~hrmap_crystal() {
clearfrom(getOrigin());
}
heptagon *get_heptagon_at(coord c, int deg) {
if(heptagon_at.count(c)) return heptagon_at[c];
heptagon*& h = heptagon_at[c];
h = init_heptagon(deg);
h->c7 = newCell(deg, h);
/* in {6,4} we need emeraldval for some patterns, including (bitruncated) football and (bitruncated) three-color */
h->emeraldval = (c[0] ^ c[1] ^ c[2]) & 2;
h->emeraldval ^= (c[1] & 4);
h->emeraldval ^= (c[0] & 4);
h->emeraldval ^= (c[2] & 4);
h->emeraldval ^= ((c[2] & 2) << 1);
if(c[0] & 2) h->emeraldval ^= 1;
if(ginf[gCrystal].vertex == 3)
h->fiftyval = fiftyrule(c);
for(int i=0; i<cs.dim; i++) h->distance += abs(c[i]);
h->distance /= 2;
hcoords[h] = c;
// for(int i=0; i<6; i++) crystalstep(h, i);
return h;
}
ldcoord get_coord(cell *c) {
// in C++14?
// auto b = sgc.emplace(c, ldc0);
// ldcoord& res = b.first->second;
if(sgc.count(c)) return sgc[c];
ldcoord& res = (sgc[c] = ldc0);
{ // if(b.second) {
if(BITRUNCATED && c->master->c7 != c) {
for(int i=0; i<c->type; i+=2)
res = res + told(hcoords[c->cmove(i)->master]);
res = res * 2 / c->type;
}
else if(GOLDBERG && c->master->c7 != c) {
auto m = gp::get_masters(c);
auto H = gp::get_master_coordinates(c);
for(int i=0; i<cs.dim; i++)
res = res + told(hcoords[m[i]]) * H[i];
}
else
res = told(hcoords[c->master]);
}
return res;
}
coord long_representant(cell *c);
int get_east(cell *c);
void build_east(int cid);
void verify() override { }
void prepare_east();
void apply_period(coord& co) {
for(int a=0; a<cs.dim; a++)
co[a] = szgmod(co[a], 2*crystal_period);
}
heptagon *create_step(heptagon *h, int d) override {
if(!hcoords.count(h)) {
printf("not found\n");
return NULL;
}
auto co = hcoords[h];
#if MAXMDIM >= 4
if(crystal3()) {
auto st = get_canonical(co);
auto co1 = co + st[d];
apply_period(co1);
auto h1 = get_heptagon_at(co1, S7);
auto st1 = get_canonical(co1);
for(int d1=0; d1<S7; d1++) if(st1[d1] == st[d])
h->c.connect(d, h1, (d1+S7/2) % S7, false);
return h1;
}
#endif
if(is_bi(cs, co)) {
heptspin hs(h, d);
(hs + 1 + wstep + 1).cpeek();
return h->move(d);
}
auto lw = makewalker(co, d);
if(ginf[gCrystal].vertex == 4) {
auto c1 = add(co, lw, FULLSTEP);
auto lw1 = lw+wstep;
apply_period(c1);
h->c.connect(d, heptspin(get_heptagon_at(c1, S7), lw1.spin));
}
else {
auto coc = add(add(co, lw, HALFSTEP), lw+1, HALFSTEP);
auto hc = get_heptagon_at(coc, 8);
apply_period(coc);
for(int a=0; a<8; a+=2) {
hc->c.connect(a, heptspin(h, lw.spin));
if(h->modmove(lw.spin-1)) {
hc->c.connect(a+1, heptspin(h, lw.spin) - 1 + wstep - 1);
}
co = add(co, lw, FULLSTEP);
apply_period(co);
lw = lw + wstep + (-1);
h = get_heptagon_at(co, S7);
}
}
return h->move(d);
}
#if MAXMDIM >= 4
map<int, transmatrix> adjs;
transmatrix adj(heptagon *h, int d) override {
if(!crystal3()) return hrmap_standard::adj(h, d);
auto co = hcoords[h];
int id = 0;
for(int a=0; a<S7/2; a++) id = (2*id) + ((co[a]>>1) & 1);
id = S7*id + d;
if(adjs.count(id)) return adjs[id];
transmatrix T = cgi.adjmoves[d];
reg3::generate_cellrotations();
auto st = get_canonical(co);
auto co1 = co + st[d];
auto st1 = get_canonical(co1);
int qty = 0;
transmatrix res;
ld gdist = S7 == 12 ? hdist0(tC0(cgi.adjmoves[0])) : cgi.strafedist;
h->cmove(d);
for(auto& cr: cgi.cellrotations) {
transmatrix U = T * cr.M;
ld go = hdist0(U * tC0(cgi.adjmoves[h->c.spin(d)]));
if(go > 1e-2) continue;
for(int s=0; s<S7; s++)
if(cgi.heptshape->dirdist[d][s] == 1)
for(int t=0; t<S7; t++)
if(st1[t] == st[s]) {
if(hdist(U * tC0(cgi.adjmoves[t]), tC0(cgi.adjmoves[s])) > gdist + .1)
goto wrong;
}
res = U;
qty++;
wrong: ;
}
adjs[id] = res;
if(qty == 1) return res;
println(hlog, "qty = ", qty);
exit(1);
}
transmatrix adj(cell *c, int d) override {
if(crystal3()) return adj(c->master, d);
return hrmap_standard::adj(c, d);
}
void draw_at(cell *at, const shiftmatrix& where) override {
if(!crystal3()) { hrmap_standard::draw_at(at, where); return; }
else hrmap::draw_at(at, where);
}
transmatrix relative_matrixc(cell *h2, cell *h1, const hyperpoint& hint) override {
if(!crystal3()) return hrmap_standard::relative_matrixc(h2, h1, hint);
if(h2 == h1) return Id;
for(int i=0; i<S7; i++) if(h2 == h1->move(i)) return adj(h1->master, i);
if(gmatrix0.count(h2) && gmatrix0.count(h1))
return inverse_shift(gmatrix0[h1], gmatrix0[h2]);
println(hlog, "unknown relmatrix, distance = ", celldistance(h1, h2));
return xpush(999);
}
transmatrix relative_matrixh(heptagon *h2, heptagon *h1, const hyperpoint& hint) override {
if(!crystal3()) return hrmap_standard::relative_matrixh(h2, h1, hint);
return relative_matrixc(h2->c7, h1->c7, hint);
}
#endif
};
hrmap_crystal *crystal_map() {
return (hrmap_crystal*) currentmap;
}
EX heptagon *get_heptagon_at(coord c) { return crystal_map()->get_heptagon_at(c, S7); }
EX coord get_coord(heptagon *h) { return crystal_map()->hcoords[h]; }
EX ldcoord get_ldcoord(cell *c) { return crystal_map()->get_coord(c); }
EX int get_dim() { return crystal_map()->cs.dim; }
#if MAXMDIM >= 4
EX transmatrix get_adj(heptagon *h, int d) { return crystal_map()->adj(h, d); }
#endif
bool is_bi(crystal_structure& cs, coord co) {
for(int i=0; i<cs.dim; i++) if(co[i] & HALFSTEP) return true;
return false;
}
array<array<int,2>, MAX_EDGE_CRYSTAL> distlimit_table = {{
{{SEE_ALL,SEE_ALL}}, {{SEE_ALL,SEE_ALL}}, {{SEE_ALL,SEE_ALL}}, {{SEE_ALL,SEE_ALL}}, {{15, 10}},
{{6, 4}}, {{5, 3}}, {{4, 3}}, {{4, 3}}, {{3, 2}}, {{3, 2}}, {{3, 2}}, {{3, 2}}, {{3, 2}}
}};
EX color_t colorize(cell *c, char whichCanvas) {
auto m = crystal_map();
ldcoord co = ldc0;
int dim = 3;
if(cryst) co = m->get_coord(c), dim = m->cs.dim;
#if MAXMDIM >= 4
else if(geometry == gSpace344) {
co = told(reg3::decode_coord(reg3::minimize_quotient_maps ? 1 : 2, c->master->fieldval)), dim = 4;
for(int a=0; a<4; a++) if(co[a] > 4) co[a] -= 8;
}
else if(geometry == gSeifertCover) {
int i = c->master->fieldval;
for(int a=0; a<3; a++) co[a] = i%5, i /= 5;
}
#endif
else if(euc::in()) {
auto tab = euc::get_ispacemap()[c->master];
for(int a=0; a<3; a++) co[a] = tab[a];
if(PURE) for(int a=0; a<3; a++) co[a] *= 2;
dim = 3;
}
color_t res = 0;
coord ico = roundcoord(co);
int ones = 0;
for(int i=0; i<dim; i++) if((ico[i] & 2) == 2) ones++;
switch(whichCanvas) {
case 'K':
for(int i=0; i<3; i++)
res |= ((int)(((i == 2 && S7 == 5) ? (128 + co[i] * 50) : (255&int(128 + co[i] * 25))))) << (8*i);
return res;
case '@': {
if(ico[dim-1] == 2 && (ones & 1)) return 0x1C0FFC0;
if(ico[dim-1] == 2 && !(ones & 1)) return 0x180FF80;
if(ico[dim-1] == -4 && (ones & 1)) return 0x180C0FF;
if(ico[dim-1] == -4 && !(ones & 1)) return 0x14080FF;
return 0x101010;
}
case '=':
if(ico[dim-1] == 2 && (ones & 1)) return 0x1C0FFC0;
if(ico[dim-1] == 2 && !(ones & 1)) return 0x180FF80;
if(ico[dim-1] == -2 && (ones & 1)) return 0x180C0FF;
if(ico[dim-1] == -2 && !(ones & 1)) return 0x14080FF;
return 0x101010;
case '#': {
bool grid = false;
ico[dim-1] -= 2;
for(int d=dim; d<MAXDIM; d++) ico[d] = 0;
for(int i=0; i<dim; i++) if((ico[i] & 6) == 4) grid = true;
for(int i=0; i<3; i++) part(res, i) = 0xFF + 0x18 * (ico[i]/2-2);
if(grid) res |= 0x1000000;
else if(GDIM == 2) res = (res & 0xFEFEFE) >> 1;
if(ico == c0) res = 0x1FFD500;
return res;
}
case 'O': {
for(int i=0; i<3; i++) part(res, i) = 0xFF + 0x18 * (ico[i]/2-2);
c->landparam = res;
if(ones == dim-1) res |= 0x1000000;
else if(GDIM == 2) res = (res & 0xFEFEFE) >> 1;
return res;
}
case '/': {
int s = 0;
for(int a=0; a<dim; a++) s += ico[a];
if(s > 0) return 0x1FF20FF;
else if (s < -2) return 0x1C0C0C0;
}
}
return res;
}
EX colortable coordcolors = {0xD04040, 0x40D040, 0x4040D0, 0xFFD500, 0xF000F0, 0x00F0F0, 0xF0F0F0 };
EX ld compass_angle() {
bool bitr = ginf[gCrystal].vertex == 3;
return (bitr ? 22.5_deg : 0) - master_to_c7_angle();
}
EX bool crystal_cell(cell *c, shiftmatrix V) {
if(!cryst) return false;
if(view_east && cheater) {
int d = dist_alt(c);
queuestr(V, 0.3, its(d), 0xFFFFFF, 1);
}
if(view_coordinates && WDIM == 2 && (cheater || tour::on)) {
auto m = crystal_map();
if(c->master->c7 == c && !is_bi(m->cs, m->hcoords[c->master])) {
ld dist = cellgfxdist(c, 0);
for(int i=0; i<S7; i++) {
shiftmatrix T = V * spin(compass_angle() - TAU * i / S7) * xpush(dist*.3);
auto co = m->hcoords[c->master];
auto lw = m->makewalker(co, i);
int cx = m->cs.cmap[lw.id][i];
queuestr(T, 0.3, its(co[cx>>1] / FULLSTEP), coordcolors[cx>>1], 1);
}
}
}
return false;
}
EX vector<cell*> build_shortest_path(cell *c1, cell *c2) {
auto m = crystal_map();
ldcoord co1 = m->get_coord(c1);
ldcoord co2 = m->get_coord(c2) - co1;
// draw a cylinder from co1 to co2, and find the solution by going through that cylinder
ldcoord mul = co2 / sqrt(co2|co2);
ld mmax = (co2|mul);
vector<cell*> p;
vector<int> parent_id;
manual_celllister cl;
cl.add(c2);
parent_id.push_back(-1);
int steps = 0;
int nextsteps = 1;
for(int i=0; i<isize(cl.lst); i++) {
if(i == nextsteps) steps++, nextsteps = isize(cl.lst);
cell *c = cl.lst[i];
forCellCM(c3, c) if(!cl.listed(c3)) {
if(c3 == c1) {
p.push_back(c1);
while(c3 != c2) {
while(i) {
p.push_back(c3);
i = parent_id[i];
c3 = cl.lst[i];
}
}
p.push_back(c3);
return p;
}
auto h = m->get_coord(c3) - co1;
ld dot = (h|mul);
if(dot > mmax + PERIOD/2 + .1) continue;
for(int k=0; k<m->cs.dim; k++) if(abs(h[k] - dot * mul[k]) > PERIOD + .1) goto next3;
cl.add(c3);
parent_id.push_back(i);
next3: ;
}
}
println(hlog, "Error: path not found, steps = ", steps);
return p;
}
EX int precise_distance(cell *c1, cell *c2) {
if(c1 == c2) return 0;
auto m = crystal_map();
if(pure()) {
coord co1 = m->hcoords[c1->master];
coord co2 = m->hcoords[c2->master];
int result = 0;
for(int a=0; a<m->cs.dim; a++) result += abs(co1[a] - co2[a]);
return result / FULLSTEP;
}
auto& distmemo = m->distmemo;
if(c2 == currentmap->gamestart()) swap(c1, c2);
else if(isize(distmemo[c2]) > isize(distmemo[c1])) swap(c1, c2);
if(distmemo[c1].count(c2)) return distmemo[c1][c2];
int zmin = 999999, zmax = -99;
forCellEx(c3, c2) if(distmemo[c1].count(c3)) {
int d = distmemo[c1][c3];
if(d < zmin) zmin = d;
if(d > zmax) zmax = d;
}
if(zmin+1 < zmax-1) println(hlog, "zmin < zmax");
if(zmin+1 == zmax-1) return distmemo[c1][c2] = zmin+1;
ldcoord co1 = m->get_coord(c1);
ldcoord co2 = m->get_coord(c2) - co1;
// draw a cylinder from co1 to co2, and find the solution by going through that cylinder
ldcoord mul = co2 / sqrt(co2|co2);
ld mmax = (co2|mul);
manual_celllister cl;
cl.add(c2);
int steps = 0;
int nextsteps = 1;
for(int i=0; i<isize(cl.lst); i++) {
if(i == nextsteps) steps++, nextsteps = isize(cl.lst);
cell *c = cl.lst[i];
forCellCM(c3, c) if(!cl.listed(c3)) {
if(c3 == c1) {
return distmemo[c1][c2] = distmemo[c2][c1] = 1 + steps;
}
auto h = m->get_coord(c3) - co1;
ld dot = (h|mul);
if(dot > mmax + PERIOD/2 + .1) continue;
for(int k=0; k<m->cs.dim; k++) if(abs(h[k] - dot * mul[k]) > PERIOD + .1) goto next3;
cl.add(c3);
next3: ;
}
}
println(hlog, "Error: distance not found");
return 999999;
}
EX ld space_distance(cell *c1, cell *c2) {
auto m = crystal_map();
ldcoord co1 = m->get_coord(c1);
ldcoord co2 = m->get_coord(c2);
return sqrt(sqhypot2(m->cs, co1, co2));
}
EX ld space_distance_camelot(cell *c) {
auto m = crystal_map();
return m->camelot_mul * sqrt(sqhypot2(m->cs, m->get_coord(c), m->camelot_coord));
}
EX int dist_relative(cell *c) {
auto m = crystal_map();
auto& cc = m->camelot_center;
int r = roundTableRadius(NULL);
cell *start = m->gamestart();
if(!cc) {
println(hlog, "Finding Camelot center...");
cc = start;
while(precise_distance(cc, start) < r + 5)
cc = cc->cmove(hrand(cc->type));
m->camelot_coord = m->get_coord(m->camelot_center);
if(m->cs.dir % 2)
m->camelot_coord[m->cs.dim-1] = 1;
m->camelot_mul = 1;
m->camelot_mul *= (r+5) / space_distance_camelot(start);
}
if(pure())
return precise_distance(c, cc) - r;
ld dis = space_distance_camelot(c);
if(dis < r)
return int(dis) - r;
else {
forCellCM(c1, c) if(space_distance_camelot(c1) < r)
return 0;
return int(dis) + 1 - r;
}
}
coord hrmap_crystal::long_representant(cell *c) {
auto& coordid = east.coordid;