-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
cell.cpp
1770 lines (1553 loc) · 50.9 KB
/
cell.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 -- cells
// Copyright (C) 2011-2019 Zeno Rogue, see 'hyper.cpp' for details
/** \file cell.cpp
* \brief General cells and maps
*
* Start with locations.cpp
*/
#include "hyper.h"
namespace hr {
#if HDR
extern int default_levs();
constexpr int PATTERN_INVALID = -1;
struct hrmap {
virtual heptagon *getOrigin() { return NULL; }
virtual cell *gamestart() { return getOrigin()->c7; }
virtual ~hrmap() { }
virtual vector<cell*>& allcells();
virtual void verify() { }
virtual void on_dim_change() { }
virtual bool link_alt(heptagon *h, heptagon *alt, hstate firststate, int dir);
virtual void extend_altmap(heptagon *h, int levs = default_levs(), bool link_cdata = true);
heptagon *may_create_step(heptagon *h, int direction) {
if(h->move(direction)) return h->move(direction);
return create_step(h, direction);
}
virtual heptagon *create_step(heptagon *h, int direction);
protected:
virtual transmatrix relative_matrixh(heptagon *h2, heptagon *h1, const hyperpoint& hint);
virtual transmatrix relative_matrixc(cell *c2, cell *c1, const hyperpoint& hint);
public:
transmatrix relative_matrix(heptagon *h2, heptagon *h1, const hyperpoint& hint) { return relative_matrixh(h2, h1, hint); }
transmatrix relative_matrix(cell *h2, cell *h1, const hyperpoint& hint) { return relative_matrixc(h2, h1, hint); }
virtual transmatrix adj(cell *c, int i) { return adj(c->master, i); }
virtual transmatrix adj(heptagon *h, int i);
transmatrix iadj(cell *c, int i) { cell *c1 = c->cmove(i); return adj(c1, c->c.spin(i)); }
transmatrix iadj(heptagon *h, int d) {
heptagon *h1 = h->cmove(d); return adj(h1, h->c.spin(d));
}
virtual void draw_all();
virtual void draw_at(cell *at, const shiftmatrix& where);
virtual void virtualRebase(heptagon*& base, transmatrix& at);
static constexpr ld SPIN_NOT_AVAILABLE = 1e5;
virtual ld spin_angle(cell *c, int d) { return SPIN_NOT_AVAILABLE; }
virtual transmatrix spin_to(cell *c, int d, ld bonus=0);
virtual transmatrix spin_from(cell *c, int d, ld bonus=0);
virtual double spacedist(cell *c, int i);
virtual bool strict_tree_rules() { return false; }
virtual void find_cell_connection(cell *c, int d);
virtual int shvid(cell *c) { return 0; }
virtual int full_shvid(cell *c) { return shvid(c); }
virtual hyperpoint get_corner(cell *c, int cid, ld cf=3) { return C0; }
virtual transmatrix master_relative(cell *c, bool get_inverse = false) { return Id; }
virtual int wall_offset(cell *c);
virtual transmatrix ray_iadj(cell *c, int i);
virtual subcellshape& get_cellshape(cell *c);
/** \brief in 3D honeycombs, returns a cellwalker res at cw->move(j) such that the face pointed at by cw and res share an edge */
virtual cellwalker strafe(cellwalker cw, int j);
/** \brief in 3D honeycombs, returns a vector<bool> v, where v[j] iff faces i and j are adjacent */
const vector<char>& dirdist(cellwalker cw) { return get_cellshape(cw.at).dirdist[cw.spin]; }
/** \brief the sequence of heptagon movement direction to get from c->master to c->move(i)->master; implemented only for reg3 */
virtual const vector<int>& get_move_seq(cell *c, int i);
/** generate a new map that is disconnected from what we already have, disconnected from the map we have so far */
virtual cell* gen_extra_origin(int fv) { throw hr_exception("gen_extra_origin not supported on this map"); }
transmatrix adjmod(cell *c, int i) { return adj(c, gmod(i, c->type)); }
transmatrix adjmod(heptagon *h, int i) { return adj(h, gmod(i, h->type)); }
transmatrix iadjmod(cell *c, int i) { return iadj(c, gmod(i, c->type)); }
transmatrix iadjmod(heptagon *h, int i) { return iadj(h, gmod(i, h->type)); }
/** this takes a large closed manifold M that is a quotient of this, and returns a unique identifier of the cell corresponding to M
* returns PATTERN_INVALID if not implemented or impossible */
virtual int pattern_value(cell *c) { return PATTERN_INVALID; }
};
/** hrmaps which are based on regular non-Euclidean 2D tilings, possibly quotient
* Operators can be applied to these maps.
* Liskov substitution warning: maps which produce both tiling like above and 3D tilings
* (e.g. Euclidean and Crystal) also inherit from hrmap_standard
**/
struct hrmap_standard : hrmap {
void draw_at(cell *at, const shiftmatrix& where) override;
transmatrix relative_matrixh(heptagon *h2, heptagon *h1, const hyperpoint& hint) override;
transmatrix relative_matrixc(cell *c2, cell *c1, const hyperpoint& hint) override;
heptagon *create_step(heptagon *h, int direction) override;
transmatrix adj(cell *c, int d) override;
transmatrix adj(heptagon *h, int d) override;
ld spin_angle(cell *c, int d) override;
double spacedist(cell *c, int i) override;
void find_cell_connection(cell *c, int d) override;
int shvid(cell *c) override;
hyperpoint get_corner(cell *c, int cid, ld cf) override;
transmatrix master_relative(cell *c, bool get_inverse) override;
bool link_alt(heptagon *h, heptagon *alt, hstate firststate, int dir) override;
void on_dim_change() override;
int pattern_value(cell *c) override;
};
void clearfrom(heptagon*);
void verifycells(heptagon*);
struct hrmap_hyperbolic : hrmap_standard {
heptagon *origin;
hrmap_hyperbolic();
hrmap_hyperbolic(heptagon *origin);
heptagon *getOrigin() override { return origin; }
~hrmap_hyperbolic() {
// verifycells(origin);
// printf("Deleting hyperbolic map: %p\n", hr::voidp(this));
clearfrom(origin);
}
void verify() override { verifycells(origin); }
void virtualRebase(heptagon*& base, transmatrix& at) override;
};
#endif
int hrmap_standard::pattern_value(cell *c) {
if(&currfp == &fieldpattern::fp_invalid) return 0;
if(ctof(c) || NONSTDVAR) return c->master->fieldval/S7;
int z = 0;
for(int u=0; u<S6; u+=2)
z = max(z, fieldpattern::btspin(createMov(c, u)->master->fieldval, c->c.spin(u)));
return -1-z;
}
void hrmap_standard::on_dim_change() {
for(auto& p: gp::gp_swapped) swapmatrix(gp::gp_adj[p]);
gp::gp_swapped.clear();
}
double hrmap::spacedist(cell *c, int i) { return hdist(tile_center(), adj(c, i) * tile_center()); }
heptagon *hrmap::create_step(heptagon *h, int direction) {
throw hr_exception("create_step called unexpectedly");
return NULL;
}
transmatrix hrmap::relative_matrixh(heptagon *h2, heptagon *h1, const hyperpoint& hint) {
println(hlog, "relative_matrixh called unexpectedly\n");
return Id;
}
transmatrix hrmap::relative_matrixc(cell *c2, cell *c1, const hyperpoint& hint) {
return relative_matrixh(c2->master, c1->master, hint);
}
bool hrmap::link_alt(heptagon *h, heptagon *alt, hstate firststate, int dir) {
return true;
}
bool hrmap_standard::link_alt(heptagon *h, heptagon *alt, hstate firststate, int dir) {
altmap::relspin(alt) = 3;
return true;
}
void hrmap::virtualRebase(heptagon*& base, transmatrix& at) {
printf("virtualRebase called unexpectedly\n");
return;
}
transmatrix hrmap::ray_iadj(cell *c, int i) {
if(WDIM == 2) {
return to_other_side(get_corner(c, i), get_corner(c, (i+1)));
}
return currentmap->iadj(c, i);
}
subcellshape& hrmap::get_cellshape(cell *c) {
if(cgi.heptshape) return *cgi.heptshape;
throw hr_exception("get_cellshape called unexpectedly");
}
cellwalker hrmap::strafe(cellwalker cw, int j) {
throw hr_exception("strafe called unexpectedly");
}
const vector<int>& hrmap::get_move_seq(cell *c, int i) {
throw hr_exception("get_move_seq not implemented for this map class");
}
transmatrix hrmap::spin_to(cell *c, int d, ld bonus) {
ld sa = spin_angle(c, d);
if(sa != SPIN_NOT_AVAILABLE) { return spin(bonus + sa); }
transmatrix T = lrspintox(tC0(adj(c, d)));
if(WDIM == 3) return T * cspin(2, 0, bonus);
return T * spin(bonus);
}
transmatrix hrmap::spin_from(cell *c, int d, ld bonus) {
ld sa = spin_angle(c, d);
if(sa != SPIN_NOT_AVAILABLE) { return spin(bonus - sa); }
transmatrix T = lspintox(tC0(adj(c, d)));
if(WDIM == 3) return T * cspin(2, 0, bonus);
return T * spin(bonus);
}
transmatrix hrmap::adj(heptagon *h, int i) { return relative_matrix(h->cmove(i), h, C0); }
vector<cell*>& hrmap::allcells() {
static vector<cell*> default_allcells;
if(disksize) return all_disk_cells;
if(closed_manifold && !(cgflags & qHUGE_BOUNDED) && !(mhybrid && hybrid::csteps == 0)) {
celllister cl(gamestart(), 1000000, 1000000, NULL);
default_allcells = cl.lst;
return default_allcells;
}
if(isize(dcal) <= 1) {
extern cellwalker cwt;
celllister cl(cwt.at, 1, 1000, NULL);
default_allcells = cl.lst;
return default_allcells;
}
return dcal;
}
EX int dirdiff(int dd, int t) {
dd %= t;
if(dd<0) dd += t;
if(t-dd < dd) dd = t-dd;
return dd;
}
EX int cellcount = 0;
EX void destroy_cell(cell *c) {
tailored_delete(c);
cellcount--;
}
EX cell *newCell(int type, heptagon *master) {
cell *c = tailored_alloc<cell> (type);
c->type = type;
c->master = master;
initcell(c);
hybrid::will_link(c);
cellcount++;
return c;
}
// -- hrmap ---
EX hrmap *currentmap;
EX vector<hrmap*> allmaps;
EX hrmap *newAltMap(heptagon *o) {
#if MAXMDIM >= 4
if(reg3::in_hrmap_rule_or_subrule())
return reg3::new_alt_map(o);
#endif
if(currentmap->strict_tree_rules())
return rulegen::new_hrmap_rulegen_alt(o);
return new hrmap_hyperbolic(o);
}
// --- hyperbolic geometry ---
EX heptagon* hyperbolic_origin() {
int odegree = geometry == gBinaryTiling ? 6 : S7;
heptagon *origin = init_heptagon(odegree);
heptagon& h = *origin;
h.s = hsOrigin;
h.emeraldval = a46 ? 0 : 98;
h.zebraval = 40;
#if CAP_IRR
if(IRREGULAR) irr::link_start(origin);
else
#endif
h.c7 = newCell(odegree, origin);
return origin;
}
hrmap_hyperbolic::hrmap_hyperbolic(heptagon *o) { origin = o; }
hrmap_hyperbolic::hrmap_hyperbolic() { origin = hyperbolic_origin(); }
void hrmap::find_cell_connection(cell *c, int d) {
heptagon *h2 = createStep(c->master, d);
c->c.connect(d, h2->c7,c->master->c.spin(d), c->master->c.mirror(d));
hybrid::link();
}
void hrmap_standard::find_cell_connection(cell *c, int d) {
#if CAP_IRR
if(IRREGULAR) {
irr::link_cell(c, d);
}
#else
if(0) {}
#endif
#if CAP_GP
else if(GOLDBERG) {
gp::extend_map(c, d);
if(!c->move(d)) {
println(hlog, "extend failed to create for ", cellwalker(c, d));
exit(1);
}
hybrid::link();
}
#endif
else if(PURE) {
hrmap::find_cell_connection(c, d);
}
else if(c == c->master->c7) {
cell *n = newCell(S6, c->master);
heptspin hs(c->master, d, false);
int alt3 = c->type/2;
int alt4 = alt3+1;
for(int u=0; u<S6; u+=2) {
if(hs.mirrored && (S7%2 == 0)) hs++;
hs.at->c7->c.connect(hs.spin, n, u, hs.mirrored);
if(hs.mirrored && (S7%2 == 0)) hs--;
hs = hs + alt3 + wstep - alt4;
}
hybrid::link();
extern void verifycell(cell *c);
verifycell(n);
}
else {
cellwalker cw(c, d, false);
cellwalker cw2 = cw - 1 + wstep - 1 + wstep - 1;
c->c.connect(d, cw2);
hybrid::link();
}
}
/** very similar to createMove in heptagon.cpp */
EX cell *createMov(cell *c, int d) {
if(d<0 || d>= c->type)
throw hr_exception("ERROR createmov\n");
if(c->move(d)) return c->move(d);
currentmap->find_cell_connection(c, d);
return c->move(d);
}
EX void eumerge(cell* c1, int s1, cell *c2, int s2, bool mirror) {
if(!c2) return;
c1->move(s1) = c2; c1->c.setspin(s1, s2, mirror);
c2->move(s2) = c1; c2->c.setspin(s2, s1, mirror);
}
// map<pair<eucoord, eucoord>, cell*> euclidean;
EX hookset<hrmap*()> hooks_newmap;
#if HDR
enum eDiskShape { dshTiles, dshVertices, dshGeometric };
#endif
/** requested disk size */
EX int req_disksize;
/** currently used disk size */
EX int disksize;
/** all the cells in the disk */
EX vector<cell*> all_disk_cells;
/** for quick test of membership */
EX vector<cell*> all_disk_cells_sorted;
/** the disk shape to use */
EX eDiskShape diskshape;
EX void init_disk_cells() {
disksize = req_disksize;
all_disk_cells.clear();
all_disk_cells_sorted.clear();
if(!disksize) return;
if(diskshape == dshTiles) {
celllister cl(currentmap->gamestart(), 1000000, disksize, NULL);
all_disk_cells = cl.lst;
}
else {
struct tileinfo {
ld dist;
cell *c;
transmatrix T;
bool operator < (const tileinfo& t2) const { return -dist < -t2.dist - 1e-6; }
};
set<cell*> seen;
std::priority_queue<tileinfo> tiles;
tiles.push(tileinfo{0, currentmap->gamestart(), Id});
ld last_dist = 0;
dynamicval<int> dmar(mine_adjacency_rule, 1);
while(isize(tiles)) {
auto ti = tiles.top();
tiles.pop();
println(hlog, "dist=", ti.dist, " for c=", ti.c);
if(seen.count(ti.c)) continue;
seen.insert(ti.c);
if(ti.dist > last_dist + 1e-6 && isize(all_disk_cells) >= disksize) break;
last_dist = ti.dist;
all_disk_cells.push_back(ti.c);
for(auto p: adj_minefield_cells_full(ti.c)) {
tileinfo next;
next.c = p.c;
next.T = ti.T * p.T;
if(diskshape == dshVertices) next.dist = ti.dist + 1;
else next.dist = hdist0(tC0(next.T));
println(hlog, ti.c, " -> ", p.c, " at ", next.dist);
tiles.push(next);
}
}
}
all_disk_cells_sorted = all_disk_cells;
sort(all_disk_cells_sorted.begin(), all_disk_cells_sorted.end());
}
EX bool is_in_disk(cell *c) {
auto it = lower_bound(all_disk_cells_sorted.begin(), all_disk_cells_sorted.end(), c);
if(it == all_disk_cells_sorted.end()) return false;
return *it == c;
}
bool sierpinski3(gp::loc g) {
int x = g.first;
int y = g.second;
set<pair<int, int>> visited;
while(true) {
if(visited.count({x,y})) return false;
visited.insert({x,y});
// if(x == -1 && y == -2) return false;
// if(x == -2 && y == -3) return false;
if(x == 0 && y == 0) return true;
if((x&1) == 1 && (y&1) == 1) return false;
x >>= 1;
y >>= 1;
// x--; y++;
tie(x, y) = make_pair(-x-y, x);
}
}
bool sierpinski46(gp::loc g) {
int x = g.first;
int y = g.second;
set<pair<int, int>> visited;
if(S7 == 6)
x += 2785684, y += 289080;
else
x += 75239892, y += 7913772;
while(true) {
if(visited.count({x,y})) return false;
visited.insert({x,y});
if(x == 0 && y == 0) return true;
int dx = gmod(x, 3);
int dy = gmod(y, 3);
if(dx == 1 && dy == 1) return false;
if(S7 == 6 && dx == dy) return false;
x = (x-dx) / 3;
y = (y-dy) / 3;
}
}
bool menger_sponge(euc::coord c) {
c[0] += 528120*9; c[1] += 438924*9; c[2] += 306712*9;
set<euc::coord> visited;
while(true) {
if(visited.count(c)) return false;
visited.insert(c);
if(c[0] == 0 && c[1] == 0 && c[2] == 0) return true;
int ones = 0;
for(int i=0; i<3; i++) {
int d = gmod(c[i], 3);
c[i] = (c[i] - d) / 3;
if(d == 1) ones++;
}
if(ones >= 2) return false;
}
}
bool sierpinski_tet(euc::coord c) {
set<euc::coord> visited;
c[0] += 16 * (1+8+64+512);
c[1] += 16 * (1+8+64+512);
c[1] += 32 * (1+8+64+512);
c[2] += 32 * (1+8+64+512);
c[0] += 64 * (1+8+64+512);
c[1] += 64 * (1+8+64+512);
while(true) {
if(visited.count(c)) return false;
visited.insert(c);
if(c[0] == 0 && c[1] == 0 && c[2] == 0) return true;
int ones = 0;
for(int i=0; i<3; i++) {
int d = gmod(c[i], 2);
c[i] = (c[i] - d) / 2;
if(d == 1) ones++;
}
if(ones & 1) return false;
}
}
EX bool is_in_fractal(cell *c) {
if(fake::in()) return FPIU(is_in_fractal(c));
if(mhybrid) { c = hybrid::get_where(c).first; return PIU(is_in_fractal(c)); }
switch(geometry) {
case gSierpinski3:
return sierpinski3(euc::full_coords2(c));
case gSierpinski4:
case gSixFlake:
return sierpinski46(euc::full_coords2(c));
case gMengerSponge:
return menger_sponge(euc::get_ispacemap()[c->master]);
case gSierpinskiTet: {
return sierpinski_tet(euc::get_ispacemap()[c->master]);
}
default:
return true;
}
}
EX cell *fractal_rep(cell *c) {
switch(geometry) {
case gSierpinski3: {
auto co = euc::full_coords2(c);
co.first += 4;
co.first &= ~15;
co.first -= 4;
co.second += 2;
co.second &= ~15;
co.second -= 2;
if(co.first == -4 && co.second == -2) co.first = 0, co.second = 0;
return euc::get_at(euc::to_coord(co))->c7;
}
case gSierpinski4:
case gSixFlake: {
auto co = euc::full_coords2(c);
if(S7 == 6) co.first += 4;
co.first -= gmod(co.first, 9);
co.second -= gmod(co.second, 9);
return euc::get_at(euc::to_coord(co))->c7;
}
case gSierpinskiTet: {
auto co = euc::get_ispacemap()[c->master];
co[0] &=~7;
co[1] &=~7;
co[2] &=~7;
return euc::get_at(co)->c7;
}
case gMengerSponge: {
auto co = euc::get_ispacemap()[c->master];
for(int i=0; i<3; i++) co[i] = co[i] - gmod(co[i], 9);
return euc::get_at(co)->c7;
}
default:
throw hr_exception("unknown fractal");
}
}
/** create a map in the current geometry */
EX void initcells() {
DEBB(DF_INIT, ("initcells"));
if(embedded_plane) {
geom3::swap_direction = -1;
check_cgi();
for(auto& m: cgi.heptmove) swapmatrix(m);
IPF(initcells());
check_cgi();
geom3::swap_direction = +1;
for(auto& m: cgi.heptmove) swapmatrix(m);
currentmap->on_dim_change();
return;
}
hrmap* res = callhandlers((hrmap*)nullptr, hooks_newmap);
if(res) currentmap = res;
#if CAP_SOLV
else if(asonov::in()) currentmap = asonov::new_map();
#endif
else if(nonisotropic || mhybrid) currentmap = nisot::new_map();
else if(INVERSE) currentmap = gp::new_inverse();
else if(fake::in()) currentmap = fake::new_map();
#if CAP_CRYSTAL
else if(cryst) currentmap = crystal::new_map();
#endif
else if(arb::in() && rulegen::known()) currentmap = rulegen::new_hrmap_rulegen();
else if(arb::in()) currentmap = arb::new_map();
#if CAP_ARCM
else if(arcm::in()) currentmap = arcm::new_map();
#endif
else if(euc::in()) currentmap = euc::new_map();
else if(hat::in()) currentmap = hat::new_map();
#if CAP_BT
else if(kite::in()) currentmap = kite::new_map();
#endif
#if MAXMDIM >= 4
else if(WDIM == 3 && !bt::in()) currentmap = reg3::new_map();
#endif
else if(sphere) currentmap = new_spherical_map();
else if(quotient) currentmap = quotientspace::new_map();
#if CAP_BT
else if(bt::in()) currentmap = bt::new_map();
#endif
else if(S3 >= OINF) currentmap = inforder::new_map();
else currentmap = new hrmap_hyperbolic;
allmaps.push_back(currentmap);
#if CAP_FIELD
windmap::create();
#endif
// origin->emeraldval =
}
EX void clearcell(cell *c) {
if(!c) return;
DEBB(DF_MEMORY, (hr::format("c%d %p\n", c->type, hr::voidp(c))));
for(int t=0; t<c->type; t++) if(c->move(t)) {
DEBB(DF_MEMORY, (hr::format("mov %p [%p] S%d\n", hr::voidp(c->move(t)), hr::voidp(c->move(t)->move(c->c.spin(t))), c->c.spin(t))));
if(c->move(t)->move(c->c.spin(t)) != NULL &&
c->move(t)->move(c->c.spin(t)) != c) {
DEBB(DF_MEMORY | DF_ERROR, (hr::format("cell error: type = %d %d -> %d\n", c->type, t, c->c.spin(t))));
if(worst_precision_error < 1e-3) exit(1);
}
c->move(t)->move(c->c.spin(t)) = NULL;
}
DEBB(DF_MEMORY, (hr::format("DEL %p\n", hr::voidp(c))));
gp::delete_mapped(c);
destroy_cell(c);
}
EX heptagon deletion_marker;
template<class T> void subcell(cell *c, const T& t) {
if(GOLDBERG) {
forCellEx(c2, c) if(c2->move(0) == c && c2 != c2->master->c7) {
subcell(c2, t);
}
}
else if(BITRUNCATED && !arcm::in() && !bt::in())
forCellEx(c2, c) t(c2);
t(c);
}
EX void clearHexes(heptagon *at) {
if(at->c7 && at->cdata) {
delete at->cdata;
at->cdata = NULL;
}
if(0);
#if CAP_IRR
else if(IRREGULAR) irr::clear_links(at);
#endif
else if(at->c7) subcell(at->c7, clearcell);
}
void unlink_cdata(heptagon *h) {
if(h->alt && h->c7) {
if(h->alt->cdata == (cdata*) h)
h->alt->cdata = NULL;
}
}
EX void clear_heptagon(heptagon *at) {
clearHexes(at);
tailored_delete(at);
}
EX void clearfrom(heptagon *at) {
if(!at) return;
queue<heptagon*> q;
unlink_cdata(at);
q.push(at);
at->alt = &deletion_marker;
//int maxq = 0;
while(!q.empty()) {
at = q.front();
// if(q.size() > maxq) maxq = q.size();
q.pop();
DEBB(DF_MEMORY, ("from %p", at));
if(!at->c7 && !ls::voronoi_structure()) {
heptagon *h = dynamic_cast<heptagon*> ((cdata_or_heptagon*) at->cdata);
if(h) {
if(h->alt != at) { DEBB(DF_MEMORY | DF_ERROR, ("alt error :: h->alt = ", h->alt, " expected ", at)); }
cell *c = h->c7;
subcell(c, destroycellcontents);
h->alt = NULL;
at->cdata = NULL;
}
}
int edges = at->degree();
if(bt::in() && WDIM == 2) edges = at->c7->type;
for(int i=0; i<edges; i++) if(at->move(i) && at->move(i) != at) {
if(at->move(i)->alt != &deletion_marker)
q.push(at->move(i));
unlink_cdata(at->move(i));
at->move(i)->alt = &deletion_marker;
DEBB(DF_MEMORY, ("!mov ", at->move(i), " [", at->move(i)->move(at->c.spin(i)), "]"));
if(at->move(i)->move(at->c.spin(i)) != NULL &&
at->move(i)->move(at->c.spin(i)) != at) {
DEBB(DF_MEMORY | DF_ERROR, ("hept error"));
exit(1);
}
at->move(i)->move(at->c.spin(i)) = NULL;
at->move(i) = NULL;
}
clearHexes(at);
tailored_delete(at);
}
//printf("maxq = %d\n", maxq);
}
EX void verifycell(cell *c) {
int t = c->type;
for(int i=0; i<t; i++) {
cell *c2 = c->move(i);
if(c2) {
if(BITRUNCATED && c == c->master->c7) verifycell(c2);
if(c2->move(c->c.spin(i)) && c2->move(c->c.spin(i)) != c) {
printf("cell error %p:%d [%d] %p:%d [%d]\n", hr::voidp(c), i, c->type, hr::voidp(c2), c->c.spin(i), c2->type);
exit(1);
}
}
}
}
EX void verifycells(heptagon *at) {
if(GOLDBERG || IRREGULAR || arcm::in()) return;
for(int i=0; i<at->type; i++) if(at->move(i) && at->move(i)->move(at->c.spin(i)) && at->move(i)->move(at->c.spin(i)) != at) {
printf("hexmix error %p [%d s=%d] %p %p\n", hr::voidp(at), i, at->c.spin(i), hr::voidp(at->move(i)), hr::voidp(at->move(i)->move(at->c.spin(i))));
}
if(!sphere && !quotient)
for(int i=0; i<S7; i++) if(at->move(i) && at->c.spin(i) == 0 && at->s != hsOrigin)
verifycells(at->move(i));
verifycell(at->c7);
}
EX int compdist(int dx[]) {
int mi = dx[0];
for(int u=0; u<S3; u++) mi = min(mi, dx[u]);
for(int u=0; u<S3; u++)
if(dx[u] > mi+2)
return -1; // { printf("cycle error!\n"); exit(1); }
for(int u=0; u<S3; u++)
if(dx[u] == mi+2)
return mi+1;
int cnt = 0;
for(int u=0; u<S3; u++)
if(dx[u] == mi) cnt++;
if(cnt < 2)
return mi+1;
return mi;
}
EX int celldist(cell *c) {
if(experimental) return 0;
if(mhybrid)
return hybrid::celldistance(c, currentmap->gamestart());
if(nil && !quotient) return DISTANCE_UNKNOWN;
if(hat::in()) return clueless_celldistance(currentmap->gamestart(), c);
if(euc::in()) return celldistance(currentmap->gamestart(), c);
if(sphere || bt::in() || WDIM == 3 || cryst || sn::in() || aperiodic || closed_manifold) return celldistance(currentmap->gamestart(), c);
#if CAP_IRR
if(IRREGULAR) return irr::celldist(c, false);
#endif
if(arcm::in() || ctof(c) || arb::in()) return c->master->distance;
#if CAP_GP
if(INVERSE) {
cell *c1 = gp::get_mapped(c);
return UIU(gp::compute_dist(c1, celldist)) / 2;
/* TODO */
}
if(GOLDBERG) return gp::compute_dist(c, celldist);
#endif
int dx[MAX_S3];
for(int u=0; u<S3; u++)
dx[u] = createMov(c, u+u)->master->distance;
return compdist(dx);
}
#if HDR
static constexpr int ALTDIST_BOUNDARY = 99999;
static constexpr int ALTDIST_UNKNOWN = 99998;
static constexpr int ALTDIST_ERROR = 90000;
#endif
EX int celldistAlt(cell *c) {
if(experimental) return 0;
if(mhybrid) {
if(in_s2xe()) return hybrid::get_where(c).second;
auto w = hybrid::get_where(c);
int d = c->master->alt && c->master->alt->alt ? hybrid::altmap_heights[c->master->alt->alt] : 0;
d = sl2 ? 0 : abs(w.second - d);
PIU ( d += celldistAlt(w.first) );
return d;
}
#if CAP_BT
if(bt::in() || sn::in()) return c->master->distance + (specialland == laCamelot && !ls::single() ? 30 : 0);
#endif
if(nil) return c->master->zebraval + abs(c->master->emeraldval) + (specialland == laCamelot && !ls::single() ? 30 : 0);;
#if CAP_CRYSTAL
if(cryst)
return crystal::dist_alt(c);
#endif
if(sphere || quotient) {
return celldist(c) - 3;
}
#if MAXMDIM >= 4
if(euc::in()) return euc::dist_alt(c);
if(hyperbolic && WDIM == 3 && !reg3::in_hrmap_rule_or_subrule())
return reg3::altdist(c->master);
#endif
if(!c->master->alt) return 0;
#if CAP_IRR
if(IRREGULAR) return irr::celldist(c, true);
#endif
if(ctof(c)) return c->master->alt->distance;
if(reg3::in()) return c->master->alt->distance;
#if CAP_GP
if(GOLDBERG) return gp::compute_dist(c, celldistAlt);
if(INVERSE) {
cell *c1 = gp::get_mapped(c);
return UIU(gp::compute_dist(c1, celldistAlt)) / 2;
/* TODO */
}
#endif
int dx[MAX_S3]; dx[0] = 0;
for(int u=0; u<S3; u++) if(createMov(c, u+u)->master->alt == NULL)
return ALTDIST_UNKNOWN;
for(int u=0; u<S3; u++)
dx[u] = createMov(c, u+u)->master->alt->distance;
// return compdist(dx); -> not OK because of boundary conditions
int mi = dx[0];
for(int i=1; i<S3; i++) mi = min(mi, dx[i]);
for(int i=0; i<S3; i++) if(dx[i] > mi+2)
return ALTDIST_BOUNDARY; // { printf("cycle error!\n"); exit(1); }
for(int i=0; i<S3; i++) if(dx[i] == mi+2)
return mi+1;
return mi;
}
/** direction upwards in the tree */
EX int updir(heptagon *h) {
#if CAP_BT
if(bt::in()) return bt::updir();
#endif
#if MAXMDIM >= 4
if(WDIM == 3 && reg3::in_hrmap_rule_or_subrule()) {
for(int i=0; i<h->type; i++) if(h->move(i) && h->move(i)->distance < h->distance)
return i;
return -1;
}
#endif
if(h->s == hsOrigin) return -1;
return 0;
}
/** direction upwards in the alt-tree */
EX int updir_alt(heptagon *h) {
if(euclid || !h->alt) return -1;
#if MAXMDIM >= 4
if(WDIM == 3 && reg3::in_hrmap_rule_or_subrule()) {
for(int i=0; i<S7; i++) if(h->move(i) && h->move(i)->alt && h->move(i)->alt->distance < h->alt->distance)
return i;
return -1;
}
#endif
return gmod(updir(h->alt) + altmap::relspin(h->alt), h->type);
}
#if HDR
static constexpr int RPV_MODULO = 5;
static constexpr int RPV_RAND = 0;
static constexpr int RPV_ZEBRA = 1;
static constexpr int RPV_EMERALD = 2;
static constexpr int RPV_PALACE = 3;
static constexpr int RPV_CYCLE = 4;
#endif
// x mod 5 = pattern type
// x mod (powers of 2) = pattern type specific
// (x/5) mod 15 = picture for drawing floors
// x mod 7 = chance of pattern-specific pic
// whole = randomization
EX bool randpattern(cell *c, int rval) {
int i, sw=0;
switch(rval%5) {
case 0:
if(rval&1) {
return hrandpos() < rval;
}
else {
int cd = getCdata(c, 0);
return !((cd/(((rval/2)&15)+1))&1);
}
case 1:
i = zebra40(c);
if(i&1) { if(rval&4) sw^=1; i &= ~1; }
if(i&2) { if(rval&8) sw^=1; i &= ~2; }
i >>= 2;
i--; i /= 3;
if(rval & (16<<i)) sw^=1;
return sw;
case 2:
i = emeraldval(c);
if(i&1) { if(rval&4) sw^=1; i &= ~1; }
if(i&2) { if(rval&8) sw^=1; i &= ~2; }
i >>= 2; i--;
if(rval & (16<<i)) sw^=1;
return sw;
case 3:
if(polara50(c)) { if(rval&4) sw^=1; }
if(polarb50(c)) { if(rval&8) sw^=1; }
i = fiftyval049(c); i += 6; i /= 7;
if(rval & (16<<i)) sw^=1;
return sw;
case 4:
i = (rval&3);
if(i == 1 && (celldist(c)&1)) sw ^= 1;
if(i == 2 && (celldist(c)&2)) sw ^= 1;
if(i == 3 && ((celldist(c)/3)&1)) sw ^= 1;
if(rval & (4<<towerval(c, celldist))) sw ^= 1;
return sw;
}
return 0;
}
EX string describeRPM(eLand l) {
int rval = randompattern[l];
switch(rval%5) {
case 0:
if(rval&1)
return "R:"+its(rval/(HRANDMAX/100))+"%";
else
return "Landscape/"+its(((rval/2)&15)+1);
case 1:
return "Z/"+its((rval>>2)&3)+"/"+its((rval>>4)&15);
case 2:
return "E/"+its((rval>>2)&3)+"/"+its((rval>>4)&2047);
case 3:
return "P/"+its((rval>>2)&3)+"/"+its((rval>>4)&255);
case 4:
return "C/"+its(rval&3)+"/"+its((rval>>2)&65535);
}
return "?";
}
EX int randpatternCode(cell *c, int rval) {
switch(rval % RPV_MODULO) {
case 1:
return zebra40(c);
case 2:
return emeraldval(c);
case 3:
return fiftyval049(c) + (polara50(c)?50:0) + (polarb50(c)?1000:0);
case 4:
return towerval(c, celldist) * 6 + celldist(c) % 6;
}
return 0;
}
#if HDR
#define RANDITER 31
#endif
char rpm_memoize[3][256][RANDITER+1];
EX void clearMemoRPM() {
for(int a=0; a<3; a++) for(int b=0; b<256; b++) for(int i=0; i<RANDITER+1; i++)
rpm_memoize[a][b][i] = 2;
}
EX bool randpatternMajority(cell *c, int ival, int iterations) {
int rval = 0;
if(ival == 0) rval = randompattern[laCaves];
if(ival == 1) rval = randompattern[laLivefjord];
if(ival == 2) rval = randompattern[laEmerald];
if(rval%RPV_MODULO == RPV_RAND) return randpattern(c, rval);
int code = randpatternCode(c, rval);
char& memo(rpm_memoize[ival][code][iterations]);
if(memo < 2) return memo;
int z = 0;
if(iterations) for(int i=0; i<c->type; i++) {
if(randpatternMajority(createMov(c,i), ival, iterations-1))
z++;
else
z--;
}
if(z!=0) memo = (z>0);