forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intra.cpp
1202 lines (1073 loc) · 35.4 KB
/
intra.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
#include "hyper.h"
namespace hr {
EX namespace intra {
EX bool in;
#if CAP_PORTALS
#if HDR
/** information per every space connected with intra-portals */
struct intra_data {
gamedata gd;
geometryinfo gi;
int wallindex;
};
#endif
EX vector<intra_data> data;
/** tells gamedata store that we are just storing one world */
EX bool switching;
/** index of the space we are currently in */
EX int current;
/** portal debugging flags */
EX int debug_portal;
/** map cells to their intra spaces */
EX map<cell*, int> intra_id;
#if HDR
/** information about portal (one side) */
struct portal_data {
int kind;
hyperpoint v0;
ld d;
transmatrix T;
transmatrix iT;
hyperpoint co0;
hyperpoint co1;
ld scale;
/* convert h to portal coordinates ('poco') to usual coordinates */
hyperpoint to_poco(hyperpoint h) const;
/* convert h from portal coordinates ('poco') to usual coordinates */
hyperpoint from_poco(hyperpoint h) const;
};
#endif
hyperpoint portal_data::to_poco(hyperpoint h) const {
if(mproduct && kind == 1) {
auto dec = product_decompose(h);
h = dec.second;
if(bt::in()) {
h = PIU( deparabolic13(h) );
h[2] = dec.first - d;
h[3] = 1;
}
else {
h = T * h;
h[0] /= h[2];
h[1] /= h[2];
h[2] = dec.first - d;
h[3] = 1;
}
if(d<0) h[2] = -h[2], h[0] = -h[0];
return h;
}
else if(mproduct && kind == 0) {
h = T * h;
ld z = product_decompose(h).first;
h /= exp(z);
auto h1 = h;
h[2] = asin_auto_clamp(h1[0]);
h[1] = z;
h[0] = asin_auto_clamp(h1[1] / cos_auto(h[2]));
h[3] = 1;
return h;
}
#if CAP_BT
else if(hyperbolic && bt::in()) {
h = deparabolic13(h);
h[3] = 1;
tie(h[0], h[1], h[2]) = make_tuple(h[1], h[2], h[0]);
h = T * h;
h[2] *= exp(h[1]);
return h;
}
#endif
else if(sol) {
h = T * h;
h[2] *= exp(-h[1]);
return h;
}
else {
h = T * h;
h /= h[3];
if(sphere)
h[2] /= sqrt(1+h[0]*h[0]+h[1]*h[1]);
if(hyperbolic)
h[2] /= sqrt(1-h[0]*h[0]-h[1]*h[1]);
return h;
}
}
hyperpoint portal_data::from_poco(hyperpoint h) const {
if(mproduct && kind == 1) {
ld xd = h[2];
if(d<0) xd = -xd, h[0] = -h[0];
#if CAP_BT
if(bt::in()) {
h[2] = 0;
return PIU( parabolic13(h) ) * exp(d+xd);
}
#endif
h[2] = 1;
auto z = product_decompose(h).first;
return iT * h * exp(d+xd-z);
}
else if(mproduct && kind == 0) {
auto h0 = h;
h[0] = sin_auto(h0[2]);
h[1] = sin_auto(h0[0]) * cos_auto(h0[2]);
h[2] = cos_auto(h0[0]) * cos_auto(h0[2]);
h[3] = 1;
return iT * h * exp(h0[1]);
}
#if CAP_BT
else if(hyperbolic && bt::in()) {
h[2] *= exp(-h[1]);
h = iT * h;
return hr::parabolic13(h[0], h[1]) * xpush0(h[2]);
}
else if(sol) {
h[2] *= exp(h[1]);
return iT * h;
}
#endif
else {
h[3] = 1;
if(sphere)
h[2] *= sqrt(1+h[0]*h[0]+h[1]*h[1]);
if(hyperbolic)
h[2] *= sqrt(1-h[0]*h[0]-h[1]*h[1]);
return normalize(iT * h);
}
}
EX portal_data make_portal(cellwalker cw, int spin) {
if(debug_portal & 289)
println(hlog, "working in: ", full_geometry_name(), " wall no ", cw.spin, "/", cw.at->type);
auto& ss = currentmap->get_cellshape(cw.at);
auto fac = ss.faces_local[cw.spin];
portal_data id;
id.scale = 1;
id.T = Id;
auto gg = geometry;
if(mproduct && cw.spin >= cw.at->type - 2) {
id.kind = 1;
id.d = product_decompose(fac[0]).first;
id.v0 = C0 * exp(id.d);
#if CAP_BT
if(bt::in()) {
for(auto h: fac)
println(hlog, PIU(deparabolic13(cgi.emb->normalize_flat(h))));
if(cw.spin == cw.at->type - 2)
fac.pop_back();
else
fac.erase(fac.begin() + 1);
id.scale = log(2)/2;
}
#else
if(false) {}
#endif
else {
hyperpoint ctr = Hypc;
for(auto p: fac) ctr += product_decompose(p).second;
ctr = cgi.emb->normalize_flat(ctr);
id.T = gpushxto0(ctr);
}
}
else if(mproduct) {
id.kind = 0;
id.v0 = Hypc;
id.scale = cgi.plevel;
for(auto p: fac) id.v0 += p;
id.v0 = cgi.emb->normalize_flat(id.v0);
hyperpoint h = cgi.emb->normalize_flat(fac[0]);
id.T = cspin90(1, 0) * spintox(gpushxto0(id.v0) * h) * gpushxto0(id.v0);
if((id.T * C0)[0] > 0) id.T = spin180() * id.T;
for(int i=0; i<3; i++) id.T[3][i] = id.T[i][3] = i==3;
if(debug_portal & 128)
for(int a=0; a<4; a++) {
hyperpoint h = fac[a];
println(hlog, kz(h), " -> ", kz(spintox(id.v0)*h), " -> ", kz(cpush(0, -hdist0(id.v0))) * kz(spintox(id.v0) * h), " -> ", kz(id.to_poco(h)));
}
}
#if CAP_BT
else if(bt::in()) {
hyperpoint removed = Hypc;
auto facmod = fac;
if(hyperbolic) for(auto& h: facmod) h = deparabolic13(h);
for(int i=0; i<isize(facmod); i++) {
int i1 = i+1; if(i1 >= isize(facmod)) i1 = 0;
int i2 = i1+1; if(i2 >= isize(facmod)) i2 = 0;
if(hypot_d(3, 2*facmod[i1] - facmod[i] - facmod[i2]) < 1e-3) {
removed = fac[i1];
facmod.erase(facmod.begin()+i1);
fac.erase(fac.begin()+i1);
}
}
id.kind = 0;
id.v0 = Hypc;
id.T = Id;
auto fac1 = fac;
auto to_coords = [] (hyperpoint& p) {
if(hyperbolic) {
p = deparabolic13(p);
p = hyperpoint(p[1], p[2], p[0], 1);
}
};
for(auto& p: fac1)
to_coords(p);
to_coords(removed);
for(auto p: fac1) id.v0 += p;
id.v0 /= isize(fac);
dynamicval<eGeometry> g(geometry, gCubeTiling);
id.T = gpushxto0(id.v0);
for(auto p: fac1) {
if(abs((id.T * p)[2]) > 1e-3 && abs((id.T * p)[0]) < 1e-3)
id.T = cspin90(2, 0) * id.T;
if(abs((id.T * p)[2]) > 1e-3 && abs((id.T * p)[1]) < 1e-3)
id.T = cspin90(2, 1) * id.T;
}
if((id.T * C03)[2] > 0) id.T = cspin180(2, 0) * id.T;
if(abs((id.T * removed)[0]) > 1e-2) id.T = cspin90(0, 1) * id.T;
if((id.T * removed)[1] < -1e-2) id.T = cspin180(0, 1) * id.T;
vector<hyperpoint> v;
geometry = gg;
for(auto f: fac) v.push_back(id.to_poco(f));
geometry = gCubeTiling;
ld sca = 1;
for(int i=0; i<isize(v); i++)
sca *= sqhypot_d(3, v[i] - v[(1+i) % isize(v)]);
sca = pow(sca, .5 / isize(v));
id.scale = sca / 2;
}
#endif
else {
id.kind = 0;
id.v0 = project_on_triangle(fac[0], fac[1], fac[2]);
id.T = cpush(2, -hdist0(id.v0)) * cspin90(2, 0) * spintox(id.v0);
hyperpoint ctr = Hypc;
for(auto p: fac) ctr += id.T*p;
ctr = normalize(ctr);
id.T = gpushxto0(ctr) * id.T;
}
if(MDIM == 3) for(int i=0; i<4; i++) id.T[3][i] = id.T[i][3] = i==3;
id.iT = inverse(id.T);
if(MDIM == 3) for(int i=0; i<4; i++) id.iT[3][i] = id.iT[i][3] = i==3;
int first = spin;
int second = spin + 1;
first = gmod(first, isize(fac));
second = gmod(second, isize(fac));
id.co0 = id.to_poco(fac[first]);
id.co1 = id.to_poco(fac[second]);
if(debug_portal & 32) {
for(int i=0; i<isize(fac); i++)
println(hlog, "edge ", i, " length is ", hdist(fac[i], fac[(i+1)%isize(fac)]));
println(hlog, "chosen edge is ", first, "--", second);
}
if(debug_portal & 256) {
println(hlog, "portal scale = ", id.scale);
auto res = [&] (ld x, ld y, ld z) {
hyperpoint h = hyperpoint(x, y, z, 1);
return id.from_poco(h);
};
for(int x=0; x<5; x++) {
println(hlog, "horizontal ", x, " = ", hdist(res(x*.1,0,0), res(x*.1+.001,0,0)));
println(hlog, "vertical ", x, " = ", hdist(res(x*.1,0,0), res(x*.1,0.001,0)));
println(hlog, "deep ", x, " = ", hdist(res(x*.1,0,0), res(x*.1,0,0.001)));
}
hyperpoint a = hyperpoint(.4, .2, .1, 1);
println(hlog, "a = ", kz(a));
println(hlog, "b = ", kz(id.from_poco(a)));
println(hlog, "c = ", kz(id.to_poco(id.from_poco(a))));
}
if(debug_portal & 1) {
for(auto p: fac) {
auto p2 = id.to_poco(p);
auto p3 = id.from_poco(p2);
println(hlog, kz(p), " > ", kz(p2), " > ", kz(p3));
}
println(hlog, kz(C0), " > ", kz(id.to_poco(C0)), " > ", kz(id.from_poco(id.to_poco(C0))));
}
return id;
}
#if HDR
/** information about connection (portal-to-portal) */
struct connection_data {
int source_world;
int target_world;
cellwalker scw, tcw;
portal_data id1;
portal_data id2;
transmatrix T;
int spin_value;
bool mirrored; /* not implemented */
};
#endif
EX map<cellwalker, connection_data> connections;
EX connection_data* find_connection(int a, int b) {
for(auto& p: connections)
if(intra_id.at(p.first.at) == a && p.second.target_world == b)
return &p.second;
return nullptr;
}
EX void switch_to(int id) {
if(current == id) return;
dynamicval<bool> is(switching, true);
data[current].gd.storegame();
current = id;
ginf[gProduct] = data[current].gi;
data[current].gd.restoregame();
}
void connect_portal_1(cellwalker cw1, cellwalker cw2, int spin) {
auto& p = connections[cw1];
p.source_world = intra_id.at(cw1.at);
p.target_world = intra_id.at(cw2.at);
p.scw = cw1;
p.tcw = cw2;
switch_to(intra_id.at(cw1.at));
int pspin = 0, nspin = 0;
if(spin > 0) pspin = spin; else nspin = -spin;
p.id1 = make_portal(cw1, nspin);
switch_to(intra_id.at(cw2.at));
p.id2 = make_portal(cw2, pspin);
p.spin_value = spin;
if(1) {
dynamicval<eGeometry> g(geometry, gCubeTiling);
transmatrix T1;
set_column(T1, 0, p.id1.co0);
set_column(T1, 1, p.id1.co1);
set_column(T1, 2, hyperpoint(0,0,p.id1.scale,0));
set_column(T1, 3, C03);
transmatrix T2;
set_column(T2, 0, p.id2.co0);
set_column(T2, 1, p.id2.co1);
set_column(T2, 2, hyperpoint(0,0,-p.id2.scale,0));
set_column(T2, 3, C03);
if(debug_portal & 2) for(int i=0; i<4; i++)
println(hlog, "mapping [", p.source_world, "]", get_column(T1, i), " to [", p.target_world, "] ", get_column(T2, i),
" dists = ", hypot_d(2, get_column(T1,i)), ",", hypot_d(2, get_column(T2,i)));
p.T = T2 * inverse(T1);
if(debug_portal & 2)
println(hlog, "det = ", det(p.T));
if(det(p.T) < 0) {
set_column(T2, 0, p.id2.co1);
set_column(T2, 1, p.id2.co0);
p.T = T2 * inverse(T1);
}
}
if(debug_portal & 2)
println(hlog, "got scale = ", tie(p.id1.scale, p.id2.scale));
if(debug_portal & 4) for(int i=0; i<5; i++) {
hyperpoint h = C03;
if(i == 1) h[0] += .2;
if(i == 2) h[0] -= .2;
if(i == 3) h[1] += .2;
if(i == 4) h[1] -= .2;
array<hyperpoint, 4> hl;
ld eps = 1e-5; ld ss = pow(eps, -2);
hl[0] = h;
hl[1] = h + point3(eps, 0, 0);
hl[2] = h + point3(0, eps, 0);
hl[3] = h + point3(0, 0, eps);
auto hl1 = hl;
may_switch_to(cw1.at);
println(hlog, "checking ", h, " -> L ", kz(p.id1.from_poco(h)), " g: ", full_geometry_name());
for(auto& hx: hl1) hx = p.id1.from_poco(hx);
print(hlog, "L side: "); analyze_orthonormal(hl1, ss);
may_switch_to(cw2.at);
println(hlog, "checking ", h, " -> R ", kz(p.id2.from_poco(p.T * h)), " g: ", full_geometry_name());
auto hl2 = hl;
for(auto& hx: hl2) hx = p.id2.from_poco(p.T * hx);
print(hlog, "R side: "); analyze_orthonormal(hl2, ss);
}
}
EX vector<pair<int, cell*>> full_sample_list;
EX void connect_portal(cellwalker cw1, cellwalker cw2, int spin) {
connect_portal_1(cw1, cw2, spin);
connect_portal_1(cw2, cw1, -spin);
}
EX void generate_sample_list_for_current() {
auto v = hybrid::gen_sample_list();
int gi = 0;
if(full_sample_list.size()) {
gi = full_sample_list.back().first;
full_sample_list.pop_back();
}
data[current].wallindex = gi;
for(auto x: v)
full_sample_list.emplace_back(x.first + gi, x.second);
println(hlog, "added ", isize(v)-1, " samples, wallindex = ", data[current].wallindex);
}
EX void regenerate_full_sample_list() {
resetter ir;
full_sample_list.clear();
for(int i=0; i<isize(data); i++) {
switch_to(i);
generate_sample_list_for_current();
}
println(hlog, isize(full_sample_list), " samples known");
}
/** make currentmap into one of the spaces in intra */
EX void become() {
dynamicval<bool> is(switching, true);
if(intra::in) {
/* let them add more spaces in this case */
data[current].gd.storegame();
intra::in = false;
return;
}
check_cgi();
cgi.require_shapes();
auto& ac = currentmap->allcells();
current = isize(data);
for(cell *c: ac)
intra_id[c] = current;
for(cell *c: ac)
currentmap->wall_offset(c);
for(cell *c: ac) c->item = itNone;
data.emplace_back();
data.back().gd.storegame();
data.back().gi = ginf[gProduct];
generate_sample_list_for_current();
sightranges[geometry] = 10;
}
/** after called become() on some spaces, actually start intra */
EX void start(int id IS(0)) {
in = true;
current = id;
dynamicval<bool> is(switching, true);
data[current].gd.restoregame();
ginf[gProduct] = data[current].gi;
again:
int missing = 0;
for(auto p: intra_id)
for(int i=0; i<p.first->type; i++) {
cell *c1 = p.first->move(i);
if(!c1) continue;
if(intra_id.count(c1) == 0) {
intra_id[c1] = p.second;
missing++;
}
}
if(debug_portal & 64) println(hlog, "missing = ", missing);
if(missing) goto again;
}
#if HDR
/** a convenience struct to switch back after a temporary switch_to */
struct resetter {
int ic;
resetter() { ic = current; }
~resetter() { if(in) switch_to(ic); }
};
#endif
EX void may_switch_to(cell *c) {
if(in) switch_to(intra_id.at(c));
}
EX int full_wall_offset(cell *c) {
int wo = currentmap->wall_offset(c);
if(in) wo += data[intra_id.at(c)].wallindex;
return wo;
}
ld dsdet(array<hyperpoint, 4> ds) {
transmatrix T;
set_column(T, 0, ds[1]-ds[0]);
set_column(T, 1, ds[2]-ds[0]);
set_column(T, 2, ds[3]-ds[0]);
return det3(T);
}
EX void analyze_orthonormal(array<hyperpoint, 4> ds, ld sca) {
transmatrix T = gpushxto0(ds[0]);
vector<ld> orths;
for(int i: {1,2,3}) {
ds[i] = T * ds[i];
if(mproduct) ds[i][2]--;
}
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
orths.push_back(dot_d(3, ds[i+1], ds[j+1]) * sca);
println(hlog, "orths = ", kz(orths));
}
EX void shift_view_portal(hyperpoint H) {
shift_view(H);
if(!through_portal()) return;
shift_view(-H);
ld minv = 0, maxv = 1;
for(int i=0; i<30; i++) {
ld t = (minv + maxv) / 2;
shift_view(H * t);
bool b = through_portal();
if(b) maxv = t; else minv = t;
shift_view(H * -t);
}
println(hlog, "maxv = ", maxv);
shift_view(H * maxv);
check_portal_movement();
shift_view_portal(H * (1 - maxv));
}
EX const connection_data* through_portal() {
transmatrix iView = view_inverse(View);
int nei = through_wall(centerover, iView * C0);
if(nei == -1) return nullptr;
auto cw1 = cellwalker(centerover, nei);
return at_or_null(connections, cw1);
}
EX void check_portal_movement() {
auto p = through_portal();
if(p) {
ld eps = 1e-5;
ld ss = pow(eps, -2);
array<hyperpoint, 4> ds; /* camera, forward, upward */
ds[0] = inverse(View) * C0;
ds[1] = inverse(get_shift_view_of(ctangent(2, -eps), View)) * C0;
ds[2] = inverse(get_shift_view_of(ctangent(1, +eps), View)) * C0;
ds[3] = inverse(get_shift_view_of(ctangent(0, +eps), View)) * C0;
if(debug_portal & 8) {
#if CAP_BT
println(hlog, "at = ", ds[0], " det = ", dsdet(ds), " bt = ", bt::minkowski_to_bt(ds[0]));
#endif
analyze_orthonormal(ds, ss);
}
for(auto& h: ds) h = p->id1.to_poco(h);
if(debug_portal & 8) {
println(hlog, "poco: at = ", ds[0], " det = ", dsdet(ds));
if(debug_portal & 16) {
dynamicval<eGeometry> g(geometry, gCubeTiling);
analyze_orthonormal(ds, ss);
}
}
/* reset the original */
View = Id; NLP = Id;
switch_to(p->target_world);
centerover = p->tcw.at;
if(1) {
dynamicval<eGeometry> g(geometry, gCubeTiling);
for(auto& h: ds) h = p->T * h;
}
if(debug_portal & 8) {
println(hlog, "poco2: at = ", ds[0], " det = ", dsdet(ds));
if(debug_portal & 16) {
dynamicval<eGeometry> g(geometry, gCubeTiling);
analyze_orthonormal(ds, ss);
}
}
for(auto& h: ds) h = p->id2.from_poco(h);
if(debug_portal & 8) {
println(hlog, "goal: at = ", ds[0], " det = ", dsdet(ds));
analyze_orthonormal(ds, ss);
}
set_view(ds[0], ds[1], ds[2]);
if(debug_portal & 8) {
array<hyperpoint, 4> xds; /* camera, forward, upward */
xds[0] = inverse(View) * C0;
xds[1] = inverse(get_shift_view_of(ctangent(2, -eps), View)) * C0;
xds[2] = inverse(get_shift_view_of(ctangent(1, +eps), View)) * C0;
xds[3] = inverse(get_shift_view_of(ctangent(0, +eps), View)) * C0;
#if CAP_BT
println(hlog, "goal: at = ", xds[0], " det = ", dsdet(xds), " bt = ", bt::minkowski_to_bt(xds[0]));
#endif
}
ld scale = p->id2.scale / p->id1.scale;
camera_speed *= scale;
anims::cycle_length *= scale;
#if CAP_VR
vrhr::absolute_unit_in_meters *= scale;
#endif
if(walking::eye_level != -1) walking::eye_level *= scale;
walking::floor_dir = -1;
walking::on_floor_of = nullptr;
}
}
vector<cellwalker> unconnected;
void erase_unconnected(cellwalker cw) {
for(int i=0; i<isize(unconnected); i++)
if(unconnected[i] == cw)
unconnected.erase(unconnected.begin() + i);
}
EX string portal_help =
"In portal maps, you can create portals on walls. Such portals can be entered to end up in another world. "
"The portals are 'immersive', that is, you can see through the portals. In the current implementation, "
"the immersive view works only in the raycasting mode, for rendering walls -- so any other objects will not "
"be rendered. (You can turn raycasting off to see the world without any portals.) As a consequence, "
"there is no HyperRogue game in a portal map -- you can only move the camera around the scene. Pick "
"'set recommended settings' in the manage portals menu to optimize settings for this.\n\n"
"To create portals between different geometries, you need to create a map in the other geometry and save it "
"(using the map editor), then in your portal map such a world can be loaded to be added to the scene "
"(in the manage portals menu). Such portals work correctly when their intrinsic shapes are equal (including the "
"curvature), their extrinsic curvatures match, and are actually implemented; see the video 'Portals to "
"Non-Euclidean Geometries' for some portals that work.\n\n"
;
EX void become_menu() {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen();
dialog::init(XLAT("Become a portal map"));
dialog::addHelp(XLAT(portal_help));
dialog::addItem(XLAT("yes, that's what I want"), 'y');
dialog::add_action([] {
intra::become();
intra::start();
game_keys_scroll = true;
mapeditor::drawplayer = false;
popScreen();
pushScreen(show_portals);
});
dialog::addBack();
dialog::display();
}
int edit_spin;
EX void world_list() {
cmode = sm::SIDE | sm::MAYDARK;
gamescreen();
dialog::init(XLAT("world list"));
dialog::start_list(900, 900, '1');
int c = current;
for(int i=0; i<isize(data); i++) {
switch_to(i);
dialog::addBoolItem(full_geometry_name(), i == c, dialog::list_fake_key++);
dialog::add_action([i] {
int ic = i;
switch_to(ic);
});
}
switch_to(c);
dialog::end_list();
dialog::addBreak(100);
#if CAP_EDIT
dialog::addItem("add a saved world to the scene", 'a');
dialog::add_action([] {
dialog::openFileDialog(levelfile, XLAT("level to load:"), ".lev", [] () {
intra::become();
if(mapstream::loadMap(levelfile.c_str())) {
addMessage(XLAT("Map loaded from %1", levelfile));
intra::become();
intra::start();
mapeditor::map_version++;
ray::reset_raycaster();
return true;
}
else {
addMessage(XLAT("Failed to load map from %1", levelfile));
intra::start();
return false;
}
});
});
#endif
dialog::addBack();
dialog::display();
}
EX void show_portals() {
cmode = sm::SIDE | sm::MAYDARK | sm::PANNING;
if(mapeditor::building_mode) cmode |= sm::EDIT_INSIDE_WALLS;
gamescreen();
dialog::init(XLAT("manage portals"));
cellwalker cw(mouseover2, point_direction);
bool valid = point_direction >= 0 && point_direction < mouseover2->type;
if(valid) {
initquickqueue();
for (const shiftmatrix& V : hr::span_at(current_display->all_drawn_copies, mouseover2)) {
dynamicval<color_t> p(poly_outline, 0xFFFFFF);
int ofs = currentmap->wall_offset(mouseover2);
queuepolyat(V, cgi.shWireframe3D[ofs + point_direction], 0, PPR::SUPERLINE);
}
quickqueue();
}
dialog::addItem(XLAT("view another world"), 'm');
dialog::add_action_push(world_list);
if(debug_portal) {
dialog::addItem(XLAT("debug"), 'd');
dialog::add_action([] {
ld eps = 1e-5;
array<hyperpoint, 4> ds; /* camera, forward, upward */
ds[0] = inverse(View) * C0;
ds[1] = inverse(get_shift_view_of(ctangent(2, -eps), View)) * C0;
ds[2] = inverse(get_shift_view_of(ctangent(1, +eps), View)) * C0;
ds[3] = inverse(get_shift_view_of(ctangent(0, +eps), View)) * C0;
set_view(ds[0], ds[1], ds[2]);
});
}
bool in_list = false; for(cellwalker x: unconnected) if(x == cw) in_list = true;
if(!valid) ;
else if(connections.count(cw)) {
dialog::addItem(XLAT("disconnect this portal"), 'd');
dialog::add_action([cw] {
auto tcw = connections[cw].tcw;
unconnected.push_back(tcw);
connections.erase(cw);
connections.erase(tcw);
mapeditor::map_version++;
});
}
else if(in_list) {
dialog::addItem(XLAT("remove %1 from the list", lalign(0, cw)), 'r');
dialog::add_action([cw] {
erase_unconnected(cw);
});
}
else {
dialog::addItem(XLAT("add to list"), 'a');
dialog::add_action([cw] { unconnected.push_back(cw); });
dialog::start_list(500, 500, '1');
for(auto p: unconnected) {
dialog::addItem(XLAT("connect " + lalign(0, p)), dialog::list_fake_key++);
dialog::add_action([p, cw] {
connect_portal(cw, p, edit_spin);
mapeditor::map_version++;
erase_unconnected(p);
});
}
dialog::end_list();
dialog::addSelItem(XLAT("portal orientation"), its(edit_spin), 'o');
dialog::add_action([] { edit_spin = edit_spin + 1; });
if(debug_portal) {
dialog::addItem(XLAT("mirror connection"), 'm');
dialog::add_action([cw] { connect_portal(cw, cw, edit_spin); });
}
if(debug_portal) {
dialog::addItem(XLAT("test portal here"), 't');
dialog::add_action([cw] { make_portal(cw, 0); });
}
}
if(!game_keys_scroll || mapeditor::drawplayer || !ray::fixed_map || vid.cells_drawn_limit > 100) {
dialog::addItem(XLAT("set recommended settings"), 'A');
dialog::add_action([] {
game_keys_scroll = true;
mapeditor::drawplayer = false;
ray::fixed_map = true;
vid.cells_drawn_limit = 100;
});
}
else {
dialog::addItem(XLAT("configure raycasting"), 'A');
dialog::add_action_push(ray::configure);
}
if(mproduct && point_direction < mouseover2->type - 2) {
ld r = get_ratio_edge(mouseover2, point_direction);
dialog::addSelItem(XLAT("height-to-width ratio"), fts(r), 'r');
dialog::add_action([] {
be_ratio_edge(mouseover2, point_direction);
mapeditor::map_version++;
ray::reset_raycaster();
});
}
else dialog::addBreak(100);
walking::add_options();
dialog::addHelp();
dialog::add_action([] { gotoHelp(portal_help); });
dialog::display();
}
#if HDR
struct portal_to_save {
cellwalker cw1;
cellwalker cw2;
int spin;
bool mirrored;
};
#endif
EX vector<portal_to_save> portals_to_save;
EX void prepare_to_save() {
portals_to_save.clear();
for(auto c: connections) if(c.second.scw < c.second.tcw) {
portals_to_save.emplace_back(portal_to_save{c.second.scw, c.second.tcw, c.second.spin_value, false});
}
}
EX void load_saved_portals() {
for(const auto& p: portals_to_save) connect_portal(p.cw1, p.cw2, p.spin);
}
EX void be_ratio(ld v IS(1)) {
check_cgi();
cgi.require_basics();
PIU( vid.plevel_factor = v * cgi.edgelen / cgi.scalefactor );
check_cgi();
cgi.require_basics();
}
EX ld get_edge_length(cell *c, int i) {
auto c1 = hybrid::get_where(c).first;
return PIU( hdist(currentmap->get_corner(c1, i), currentmap->get_corner(c1, (i+c1->type-1)%c1->type)) );
}
EX ld get_ratio_edge(cell *c, int i) {
ld len = get_edge_length(c, i);
return vid.plevel_factor * cgi.scalefactor / len;
}
EX void be_ratio_edge(cell *c, int i, ld v IS(1)) {
ld len = get_edge_length(c, i);
vid.plevel_factor = v * len / cgi.scalefactor;
check_cgi();
cgi.require_basics();
}
EX void be_ratio_edge(int i, ld v IS(1)) {
start_game();
be_ratio_edge(cwt.at, i, v);
}
/** Remove the space with the given id. Turns off intra */
EX void kill(int id) {
if(in) become();
data.erase(data.begin()+id);
vector<cellwalker> to_remove;
for(auto& p: connections) if(intra_id[p.second.scw.at] == id || intra_id[p.second.tcw.at] == id)
to_remove.push_back(p.first);
else {
if(p.second.source_world >= id) p.second.source_world--;
if(p.second.target_world >= id) p.second.target_world--;
}
for(auto r: to_remove) connections.erase(r);
vector<cell*> to_erase_cell;
for(auto& p: intra_id)
if(p.second == id)
to_erase_cell.push_back(p.first);
else if(p.second > id)
p.second--;
for(auto c: to_erase_cell)
intra_id.erase(c);
println(hlog, isize(to_remove), " connections and ", isize(to_erase_cell), " cells erased");
}
EX void erase_all_maps() {
println(hlog, "erase_all_maps called");
dynamicval<bool> is(switching, true);
data[current].gd.storegame();
in = false;
for(int i=0; i<isize(data); i++) {
current = i;
ginf[gProduct] = data[i].gi;
data[i].gd.restoregame();
clearCellMemory();
}
intra_id.clear();
connections.clear();
data.clear();
full_sample_list.clear();
}
EX set<cell*> need_to_save;
EX void prepare_need_to_save() {
need_to_save.clear();
map<cell*, cell*> parent;
vector<cell*> q;
cell *s = mapstream::save_start();
parent[s] = s;
q = {s};
for(int i=0; i<isize(q); i++) {
cell *c = q[i];
forCellEx(c2, c)
if(!parent.count(c2)) { parent[c2] = c; q.push_back(c2); }
}
for(int i=isize(q)-1; i>=0; i--) {
cell *c = q[i];
if(c == cwt.at) need_to_save.insert(c);
for(auto& p: connections) if(p.first.at == c) need_to_save.insert(c);
if(need_to_save.count(c)) need_to_save.insert(parent[c]);
}
println(hlog, "need to save ", isize(need_to_save), " out of ", isize(q), " cells");
}
auto hooks1 =
addHook(hooks_o_key, 90, [] (o_funcs& v) {
if(intra::in) v.push_back(named_dialog(XLAT("manage portals"), show_portals));
})
+ arg::add3("-intra-add", [] { start_game(); become(); })
+ arg::add3("-intra-start", [] { start_game(); become(); start(0); })
+ arg::add3("-intra-kill", [] { arg::shift(); kill(arg::argi()); start(0); regenerate_full_sample_list(); })
+ arg::add3("-be-square", [] { be_ratio(); })
+ arg::add3("-be-square-edge", [] {
arg::shift(); int i = arg::argi(); be_ratio_edge(i);
})
+ arg::add3("-debug-portal", [] { arg::shift(); debug_portal = arg::argi(); });
#endif
EX }
EX namespace walking {
EX bool on;
EX bool auto_eyelevel;
EX int floor_dir = -1;
EX cell *on_floor_of = nullptr;
EX ld eye_level = 0.2174492;
EX ld eye_angle = 0;
EX ld eye_angle_scale = 1;
EX int ticks_end, ticks_last;
EX set<color_t> colors_of_floors;
EX bool isFloor(cell *c) {
if(!isWall(c)) return false;
if(colors_of_floors.empty()) return true;
if(c->wall != waWaxWall) return false;
return colors_of_floors.count(c->landparam);
}
EX void handle() {
if(playermoved || !on) return;
make_actual_view();
if(floor_dir == -1 || on_floor_of != centerover) {
vector<int> choices;
for(int i=0; i<centerover->type; i++)
if(isFloor(centerover->cmove(i)))
choices.push_back(i);
if(sol && isize(choices) == 2) choices.pop_back();
if(isize(choices) == 1) {