forked from zenorogue/hyperrogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.cpp
4282 lines (3711 loc) · 146 KB
/
config.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 -- configuration
// Copyright (C) 2017-2018 Zeno Rogue, see 'hyper.cpp' for details
/** \file config.cpp
* \brief Configuration -- initial settings, saving/loading ini files, menus, etc.
*/
#include "hyper.h"
namespace hr {
#if HDR
enum eCentering { face, edge, vertex };
#endif
EX eCentering centering;
EX function<bool()> auto_restrict;
EX void add_to_changed(struct setting *f);
EX bool return_false() { return false; }
#if HDR
struct supersaver {
string name;
virtual string save() = 0;
virtual void load(const string& s) = 0;
virtual bool dosave() = 0;
virtual void reset() = 0;
virtual bool affects(void* v) { return false; }
virtual void set_default() = 0;
virtual ~supersaver() = default;
virtual void swap_with(supersaver*) = 0;
virtual void clone(struct local_parameter_set& lps, void *value) = 0;
};
typedef vector<shared_ptr<supersaver>> saverlist;
extern saverlist savers;
struct setting {
function<bool()> restrict;
string parameter_name;
string config_name;
string menu_item_name;
string help_text;
reaction_t reaction;
char default_key;
bool is_editable;
bool needs_confirm;
supersaver *saver;
virtual bool available() { if(restrict) return restrict(); return true; }
virtual bool affects(void *v) { return false; }
virtual supersaver *make_saver() { return nullptr; }
virtual void register_saver() { saver = make_saver(); }
void show_edit_option() { show_edit_option(default_key); }
virtual void show_edit_option(int key) {
println(hlog, "default called!"); }
virtual string search_key() {
return parameter_name + "|" + config_name + "|" + menu_item_name + "|" + help_text;
}
explicit setting() { restrict = auto_restrict; is_editable = false; needs_confirm = false; }
virtual void check_change() { }
reaction_t sets;
setting *set_sets(const reaction_t& s) { sets = s; return this; }
setting *set_extra(const reaction_t& r);
setting *set_reaction(const reaction_t& r);
virtual ~setting() = default;
virtual void load_from(const string& s) {
if(saver) { saver->load(s); return; }
println(hlog, "cannot load parameter: ", parameter_name, " from: ", s);
throw hr_exception("parameter cannot be loaded");
}
virtual bool load_from_animation(const string& s) {
load_from(s); return false;
}
virtual void load_as_animation(const string& s) {
load_from(s);
}
virtual bool anim_unchanged() { return true; }
virtual void anim_restore() { }
virtual cld get_cld() { throw hr_exception("parameter has no complex value"); }
virtual void set_cld_raw(cld x) { throw hr_exception("parameter has no complex value"); }
virtual void set_cld(cld value) {
auto bak = get_cld();
set_cld_raw(value);
if(value != bak && reaction) reaction();
}
};
#endif
setting *setting::set_extra(const reaction_t& r) {
auto s = sets; set_sets([s, r] { if(s) s(); dialog::get_di().extra_options = r; }); return this;
}
setting *setting::set_reaction(const reaction_t& r) {
reaction = r; return this;
}
EX map<string, std::unique_ptr<setting>> params;
EX void show_edit_option_enum(char* value, const string& name, const vector<pair<string, string>>& options, char key, setting *s);
#if HDR
struct list_setting : setting {
virtual int get_value() = 0;
virtual void set_value(int i) = 0;
vector<pair<string, string> > options;
list_setting* editable(const vector<pair<string, string> >& o, string menu_item_name, char key) {
is_editable = true;
options = o;
this->menu_item_name = menu_item_name;
default_key = key;
return this;
}
void show_edit_option(int key) override;
};
namespace anims {
extern void animate_setting(setting*, string);
}
template<class T> struct enum_setting : list_setting {
T *value, last_value, dft, anim_value;
int get_value() override { return (int) *value; }
hr::function<void(T)> set_value_to;
void set_value(int i) override { set_value_to((T)i); }
bool affects(void* v) override { return v == value; }
supersaver *make_saver() override;
virtual void load_from_raw(const string& s) {
int N = isize(options);
for(int i=0; i<N; i++) if(appears(options[i].first, s)) {
set_value_to((T)i);
return;
}
*value = (T) parseint(s);
}
void check_change() override {
if(*value != last_value) {
last_value = *value;
add_to_changed(this);
}
}
void load_from(const string& s) override {
auto bak = *value;
load_from_raw(s);
if(*value != bak && reaction) reaction();
}
bool load_from_animation(const string& s) override {
if(anim_value != *value) return false;
load_from(s);
anim_value = *value;
return true;
}
void load_as_animation(const string& s) override {
load_from(s);
anim_value = *value;
anims::animate_setting(this, s);
}
enum_setting<T>* editable(const vector<pair<string, string> >& o, string menu_item_name, char key) {
list_setting::editable(o, menu_item_name, key);
return this;
}
enum_setting<T>* set_need_confirm() {
needs_confirm = true;
return this;
}
};
/** transmatrix with equality, so we can construct val_setting<matrix_eq> */
struct matrix_eq : transmatrix {
bool operator == (const transmatrix& t) const {
for(int i=0; i<MAXMDIM; i++) for(int j=0; j<MAXMDIM; j++) if(self[i][j] != t[i][j]) return false;
return true;
}
bool operator != (const transmatrix& t) const {
return ! (self == t);
}
};
template<class T> struct val_setting : public setting {
T *value, last_value, anim_value, dft;
bool affects(void *v) override { return v == value; }
void check_change() override {
if(*value != last_value) {
last_value = *value;
add_to_changed(this);
}
}
bool anim_unchanged() override { return *value == anim_value; }
void anim_restore() override { *value = anim_value; if(reaction) reaction(); }
virtual void load_from_raw(const string& s) { throw hr_exception("load_from_raw not defined"); }
void load_from(const string& s) override {
auto bak = *value;
load_from_raw(s);
if(*value != bak && reaction) reaction();
}
bool load_from_animation(const string& s) override {
if(anim_value != *value) return false;
load_from(s);
anim_value = *value;
return true;
}
void load_as_animation(const string& s) override {
load_from(s);
anim_value = *value;
anims::animate_setting(this, s);
}
};
struct float_setting : public val_setting<ld> {
ld min_value, max_value, step;
string unit;
float_setting *editable(ld min_value, ld max_value, ld step, string menu_item_name, string help_text, char key) {
is_editable = true;
this->min_value = min_value;
this->max_value = max_value;
this->menu_item_name = menu_item_name;
this->help_text = help_text;
this->step = step;
default_key = key;
return this;
}
function<void(float_setting*)> modify_me;
float_setting *modif(const function<void(float_setting*)>& r) { modify_me = r; return this; }
supersaver *make_saver() override;
void show_edit_option(int key) override;
void load_from_raw(const string& s) override { *value = parseld(s); }
cld get_cld() override { return *value; }
void set_cld_raw(cld x) override { *value = real(x); }
};
struct float_setting_dft : public float_setting {
void show_edit_option(int key) override;
function<ld()> get_hint;
float_setting_dft* set_hint(const function<ld()>& f) { get_hint = f; return this; }
};
struct int_setting : public val_setting<int> {
int min_value, max_value;
ld step;
supersaver *make_saver() override;
function<void(int_setting*)> modify_me;
int_setting *modif(const function<void(int_setting*)>& r) { modify_me = r; return this; }
void show_edit_option(int key) override;
int_setting *editable(int min_value, int max_value, ld step, string menu_item_name, string help_text, char key) {
this->is_editable = true;
this->min_value = min_value;
this->max_value = max_value;
this->menu_item_name = menu_item_name;
this->help_text = help_text;
this->step = step;
default_key = key;
return this;
}
cld get_cld() override { return *value; }
void load_from_raw(const string& s) override { *value = parseint(s); }
void set_cld_raw(cld x) override { *value = (int)(real(x) + .5); }
void check_change() override {
if(*value != last_value) {
last_value = *value;
add_to_changed(this);
}
}
};
struct color_setting : public val_setting<color_t> {
bool has_alpha;
supersaver *make_saver() override;
void show_edit_option(int key) override;
color_setting *editable(string menu_item_name, string help_text, char key) {
this->is_editable = true;
this->menu_item_name = menu_item_name;
this->help_text = help_text;
default_key = key;
return this;
}
void load_from_raw(const string& s) override { sscanf(s.c_str(), "%x", value); }
};
struct matrix_setting : public val_setting<matrix_eq> {
int dim;
supersaver *make_saver() override;
void show_edit_option(int key) override;
matrix_setting *editable(string menu_item_name, string help_text, char key) {
this->is_editable = true;
this->menu_item_name = menu_item_name;
this->help_text = help_text;
default_key = key;
return this;
}
void load_from_raw(const string& s) override { (transmatrix&)*value = parsematrix(s); }
};
struct char_setting : public val_setting<char> {
supersaver *make_saver() override;
void show_edit_option(int key) override;
void load_from_raw(const string& s) override {
if(s == "\\0") *value = 0;
else sscanf(s.c_str(), "%c", value);
}
};
struct bool_setting : public val_setting<bool> {
supersaver *make_saver() override;
reaction_t switcher;
bool_setting* editable(string cap, char key ) {
is_editable = true;
menu_item_name = cap; default_key = key; return this;
}
void show_edit_option(int key) override;
void load_from_raw(const string& s) override { *value = parseint(s); }
cld get_cld() override { return *value; }
};
struct custom_setting : public setting {
cld last_value;
function<void(char)> custom_viewer;
function<cld()> custom_value;
function<bool(void*)> custom_affect;
void show_edit_option(int key) override { custom_viewer(key); }
supersaver *make_saver() override { throw hr_exception("make_saver for custom_setting"); }
bool affects(void *v) override { return custom_affect(v); }
void check_change() override {
if(custom_value() != last_value) {
last_value = custom_value();
add_to_changed(this);
}
}
};
struct local_parameter_set {
string label;
local_parameter_set* extends;
vector<pair<supersaver*, supersaver*>> swaps;
void pswitch();
local_parameter_set(string l, local_parameter_set *ext = nullptr) : label(l), extends(ext) {}
};
#if CAP_CONFIG
template<class T> struct dsaver : supersaver {
T& val;
T dft;
bool dosave() override { return val != dft; }
void reset() override { val = dft; }
explicit dsaver(T& val) : val(val) { }
bool affects(void* v) override { return v == &val; }
void set_default() override { dft = val; }
};
template<class T> struct saver : dsaver<T> {};
template<class T, class U, class V> supersaver* addsaver(T& i, U name, V dft) {
auto s = make_shared<saver<T>> (i);
s->dft = dft;
s->name = name;
savers.push_back(s);
return &*s;
}
template<class T> supersaver* addsaver(T& i, string name) {
return addsaver(i, name, i);
}
template<class T> void removesaver(T& val) {
for(int i=0; i<isize(savers); i++)
if(savers[i]->affects(&val))
savers.erase(savers.begin() + i);
}
template<class T> void set_saver_default(T& val) {
for(auto sav: savers)
if(sav->affects(&val))
sav->set_default();
}
template<class T, class U> supersaver *addsaverenum(T& i, U name);
template<class T> struct saverenum : supersaver {
T& val;
T dft;
explicit saverenum(T& v) : val(v) { }
bool dosave() override { return val != dft; }
void reset() override { val = dft; }
string save() override { return its(int(val)); }
void load(const string& s) override { val = (T) atoi(s.c_str()); }
bool affects(void* v) override { return v == &val; }
void set_default() override { dft = val; }
void clone(struct local_parameter_set& lps, void *value) override { addsaverenum(*(T*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saverenum<T>*)s)->val); }
};
template<class T, class U> supersaver *addsaverenum(T& i, U name, T dft) {
auto s = make_shared<saverenum<T>> (i);
s->dft = dft;
s->name = name;
savers.push_back(s);
return &*s;
}
template<class T, class U> supersaver *addsaverenum(T& i, U name) {
return addsaverenum(i, name, i);
}
template<> struct saver<int> : dsaver<int> {
explicit saver(int& val) : dsaver<int>(val) { }
string save() override { return its(val); }
void load(const string& s) override { val = atoi(s.c_str()); }
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(int*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<int>*)s)->val); }
};
template<> struct saver<char> : dsaver<char> {
explicit saver(char& val) : dsaver<char>(val) { }
string save() override { return its(val); }
void load(const string& s) override { val = atoi(s.c_str()); }
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(char*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<char>*)s)->val); }
};
template<> struct saver<bool> : dsaver<bool> {
explicit saver(bool& val) : dsaver<bool>(val) { }
string save() override { return val ? "yes" : "no"; }
void load(const string& s) override { val = isize(s) && s[0] == 'y'; }
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(bool*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<bool>*)s)->val); }
};
template<> struct saver<unsigned> : dsaver<unsigned> {
explicit saver(unsigned& val) : dsaver<unsigned>(val) { }
string save() override { return itsh(val); }
void load(const string& s) override { val = (unsigned) strtoll(s.c_str(), NULL, 16); }
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(unsigned*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<unsigned>*)s)->val); }
};
template<> struct saver<string> : dsaver<string> {
explicit saver(string& val) : dsaver<string>(val) { }
string save() override { return val; }
void load(const string& s) override { val = s; }
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(string*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<string>*)s)->val); }
};
template<> struct saver<matrix_eq> : supersaver {
matrix_eq& val;
matrix_eq dft;
explicit saver(matrix_eq& _val) : val(_val) { }
void reset() override { val = dft; }
bool affects(void* v) override { return v == &val; }
void set_default() override { dft = val; }
bool dosave() override { return !eqmatrix(val, dft); }
string save() override {
shstream ss;
for(int a=0; a<4; a++) for(int b=0; b<4; b++) print(ss, val[a][b], " ");
return ss.s;
}
void load(const string& s) override {
shstream ss;
ss.s = s;
for(int a=0; a<4; a++) for(int b=0; b<4; b++) scan(ss, val[a][b]);
}
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(matrix_eq*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<matrix_eq>*)s)->val); }
};
template<> struct saver<ld> : dsaver<ld> {
explicit saver(ld& val) : dsaver<ld>(val) { }
string save() override { return fts(val, 10); }
void load(const string& s) override {
if(s == "0.0000000000e+000") ; // ignore!
else val = atof(s.c_str());
}
void clone(struct local_parameter_set& lps, void *value) override { addsaver(*(ld*) value, lps.label + name); }
void swap_with(supersaver *s) override { swap(val, ((saver<ld>*)s)->val); }
};
#endif
#endif
supersaver *float_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
supersaver *int_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
supersaver* color_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
supersaver* matrix_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
supersaver *char_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
supersaver *bool_setting::make_saver() {
#if CAP_CONFIG
return addsaver(*value, config_name, dft);
#else
return nullptr;
#endif
}
void non_editable() {
dialog::addHelp("Warning: editing this value through this menu may not work correctly");
}
void float_setting::show_edit_option(int key) {
if(modify_me) modify_me(this);
dialog::addSelItem(XLAT(menu_item_name), fts(*value) + unit, key);
if(*value == use_the_default_value) dialog::lastItem().value = XLAT("default");
dialog::add_action([this] () {
add_to_changed(this);
dialog::editNumber(*value, min_value, max_value, step, dft, XLAT(menu_item_name), help_text);
if(sets) sets();
if(reaction) dialog::get_di().reaction = reaction;
if(!is_editable) dialog::get_di().extra_options = non_editable;
});
}
void float_setting_dft::show_edit_option(int key) {
if(modify_me) modify_me(this);
dialog::addSelItem(XLAT(menu_item_name), fts(*value) + unit, key);
if(*value == use_the_default_value) dialog::lastItem().value = XLAT("default: ") + fts(get_hint());
dialog::add_action([this] () {
add_to_changed(this);
if(*value == use_the_default_value) *value = get_hint();
dialog::editNumber(*value, min_value, max_value, step, dft, XLAT(menu_item_name), help_text);
if(sets) sets();
if(reaction) dialog::get_di().reaction = reaction;
if(!is_editable) dialog::get_di().extra_options = non_editable;
auto eo = dialog::get_di().extra_options;
dialog::get_di().extra_options = [eo, this] {
dialog::addSelItem(XLAT("use the default value"), "", 'D');
dialog::add_action([this] { *value = use_the_default_value; });
if(eo) eo();
};
});
}
void int_setting::show_edit_option(int key) {
if(modify_me) modify_me(this);
dialog::addSelItem(XLAT(menu_item_name), its(*value), key);
dialog::add_action([this] () {
add_to_changed(this);
dialog::editNumber(*value, min_value, max_value, step, dft, XLAT(menu_item_name), help_text);
if(sets) sets();
if(reaction) dialog::get_di().reaction = reaction;
if(!is_editable) dialog::get_di().extra_options = non_editable;
});
}
void bool_setting::show_edit_option(int key) {
dialog::addBoolItem(XLAT(menu_item_name), *value, key);
dialog::add_action([this] () {
add_to_changed(this);
switcher(); if(sets) sets();
if(reaction) reaction();
});
}
void color_setting::show_edit_option(int key) {
dialog::addColorItem(XLAT(menu_item_name), has_alpha ? *value : addalpha(*value), key);
dialog::add_action([this] () {
dialog::openColorDialog(*value);
dialog::colorAlpha = has_alpha;
dialog::get_di().dialogflags |= sm::SIDE;
});
}
void matrix_setting::show_edit_option(int key) {
dialog::addMatrixItem(XLAT(menu_item_name), *value, key, dim);
dialog::add_action([this] () {
dialog::editMatrix(*value, XLAT(menu_item_name), help_text, dim);
if(sets) sets();
if(reaction) dialog::get_di().reaction = reaction;
});
}
void char_setting::show_edit_option(int key) {
string s = s0; s += value;
dialog::addSelItem(XLAT(menu_item_name), s, key);
}
EX float_setting *param_f(ld& val, const string p, const string s, ld dft) {
unique_ptr<float_setting> u ( new float_setting );
u->parameter_name = p;
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = dft;
if(dft == 0) {
u->min_value = -100;
u->max_value = +100;
}
else {
u->min_value = 0;
u->max_value = 2 * dft;
}
u->step = dft / 10;
u->dft = dft;
val = dft;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX float_setting_dft *param_fd(ld& val, const string s, ld dft IS(use_the_default_value) ) {
unique_ptr<float_setting_dft> u ( new float_setting_dft );
u->parameter_name = s;
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = dft;
u->min_value = -100;
u->max_value = +100;
u->step = 1;
u->dft = dft;
val = dft;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX string param_esc(string s) {
string out;
for(char c: s)
if(c == ' ' || c == '-' || c == ':')
out += '_';
else
out += c;
return out;
}
EX int_setting *param_i(int& val, const string s, int dft) {
unique_ptr<int_setting> u ( new int_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = dft;
u->dft = dft;
val = dft;
if(dft == 0) {
u->min_value = -100;
u->max_value = +100;
}
else {
u->min_value = 0;
u->max_value = 2 * dft;
}
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX int_setting *param_i(int& val, const string s) { return param_i(val, s, val); }
EX bool_setting *param_b(bool& val, const string s, bool dft) {
unique_ptr<bool_setting> u ( new bool_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = dft;
u->dft = dft;
u->switcher = [&val] { val = !val; };
val = dft;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX color_setting *param_color(color_t& val, const string s, bool has_alpha, color_t dft) {
unique_ptr<color_setting> u ( new color_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = dft;
u->dft = dft;
u->has_alpha = has_alpha;
val = dft;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX matrix_setting *param_matrix(transmatrix& val0, const string s, int dim) {
matrix_eq& val = (matrix_eq&) val0;
unique_ptr<matrix_setting> u ( new matrix_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = val;
u->dft = val;
u->dim = dim;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX char_setting *param_char(char& val, const string s, char dft) {
unique_ptr<char_setting> u ( new char_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->last_value = dft;
u->dft = dft;
val = dft;
u->register_saver();
auto f = &*u;
params[u->parameter_name] = std::move(u);
return f;
}
EX color_setting *param_color(color_t& val, const string s, bool has_alpha) { return param_color(val, s, has_alpha, val); }
EX bool_setting *param_b(bool& val, const string s) { return param_b(val, s, val); }
#if HDR
template<class T> supersaver* enum_setting<T>::make_saver() {
#if CAP_CONFIG
return addsaverenum(*value, config_name, dft);
#endif
return nullptr;
}
template<class T> enum_setting<T> *param_enum(T& val, const string p, const string s, T dft) {
unique_ptr<enum_setting<T>> u ( new enum_setting<T> );
u->parameter_name = p;
u->config_name = s;
u->menu_item_name = s;
u->value = &val;
u->dft = dft;
val = dft;
u->last_value = dft;
u->register_saver();
auto f = &*u;
u->set_value_to = [f] (T val) { *f->value = val; };
params[p] = std::move(u);
return f;
}
#endif
EX float_setting* param_f(ld& val, const string s) {
return param_f(val, param_esc(s), s, val);
}
EX float_setting* param_f(ld& val, const string p, const string s) {
return param_f(val, p, s, val);
}
EX float_setting* param_f(ld& val, const string s, ld dft) {
return param_f(val, param_esc(s), s, dft);
}
#if HDR
template<class T>
custom_setting* param_custom(T& val, const string& s, function<void(char)> menuitem, char key) {
unique_ptr<custom_setting> u ( new custom_setting );
u->parameter_name = param_esc(s);
u->config_name = s;
u->menu_item_name = s;
u->last_value = (int) val;
u->custom_viewer = menuitem;
u->custom_value = [&val] () { return (int) val; };
u->custom_affect = [&val] (void *v) { return &val == v; };
u->default_key = key;
u->is_editable = true;
auto f = &*u;
params[s] = std::move(u);
return f;
}
#endif
EX ld bounded_mine_percentage = 0.1;
EX int bounded_mine_quantity, bounded_mine_max;
EX const char *conffile = "hyperrogue.ini";
/* extra space if more geometries are added */
EX array<ld, gGUARD+64> sightranges;
EX bool logfog;
EX videopar vid;
#define DEFAULT_WALLMODE (ISMOBILE ? 3 : 5)
#define DEFAULT_MONMODE (ISMOBILE ? 2 : 4)
EX void android_settings_changed() {
#if ISANDROID
settingsChanged = true;
#endif
}
extern color_t floorcolors[landtypes];
EX charstyle& getcs(int id IS(multi::cpid)) {
if(multi::players>1 && id >= 0 && id < multi::players)
return multi::scs[id];
else
return vid.cs;
}
struct charstyle_old {
int charid;
color_t skincolor, haircolor, dresscolor, swordcolor, dresscolor2, uicolor;
bool lefthanded;
};
EX void hread(hstream& hs, charstyle& cs) {
// before 0xA61A there was no eyecolor
if(hs.get_vernum() < 0xA61A) {
charstyle_old cso;
hread_raw(hs, cso);
cs.charid = cso.charid;
cs.skincolor = cso.skincolor;
cs.haircolor = cso.haircolor;
cs.dresscolor = cso.dresscolor;
cs.swordcolor = cs.eyecolor = cs.bowcolor = cs.bowcolor2 = cso.swordcolor;
if(cs.charid < 4) cs.eyecolor = 0;
cs.dresscolor2 = cso.dresscolor2;
cs.uicolor = cso.uicolor;
cs.lefthanded = cso.lefthanded;
}
else hread_raw(hs, cs);
}
EX void hwrite(hstream& hs, const charstyle& cs) {
hwrite_raw(hs, cs);
}
// void hread(hstream& hs, charstyle& cs) { hread_raw(hs, cs); }
// void hwrite(hstream& hs, const charstyle& cs) { hwrite_raw(hs, cs); }
EX string csnameid(int id) {
if(id == 0) return XLAT("male");
if(id == 1) return XLAT("female");
if(id == 2) return XLAT("Prince");
if(id == 3) return XLAT("Princess");
if(id == 4 || id == 5) return XLAT("cat");
if(id == 6 || id == 7) return XLAT("dog");
if(id == 8 || id == 9) return XLATN("Familiar");
return XLAT("none");
}
EX string csname(charstyle& cs) {
return csnameid(cs.charid);
}
EX int playergender() {
return (getcs().charid >= 0 && (getcs().charid&1)) ? GEN_F : GEN_M;
}
EX int princessgender() {
int g = playergender();
if(vid.samegender) return g;
return g == GEN_M ? GEN_F : GEN_M;
}
EX int default_language;
EX int lang() {
if(vid.language >= 0)
return vid.language;
return default_language;
}
EX bool autojoy = true;
#if CAP_CONFIG
saverlist savers;
#endif
#if !CAP_CONFIG
#if HDR
template<class T, class U, class V> void addsaver(T& i, U name, V dft) {
i = dft;
}
template<class T, class U> void addsaver(T& i, U name) {}
template<class T, class U> void addsaverenum(T& i, U name) {}
template<class T, class U> void addsaverenum(T& i, U name, T dft) { i = dft; }
#endif
#endif
EX void addsaver(charstyle& cs, string s) {
addsaver(cs.charid, s + ".charid");
addsaver(cs.skincolor, s + ".skincolor");
addsaver(cs.eyecolor, s + ".eyecolor");
addsaver(cs.bowcolor, s + ".bowcolor");
addsaver(cs.bowcolor2, s + ".bowcolor2");
addsaver(cs.haircolor, s + ".haircolor");
addsaver(cs.dresscolor, s + ".dresscolor");
addsaver(cs.swordcolor, s + ".swordcolor");
addsaver(cs.dresscolor2, s + ".dresscolor2");
addsaver(cs.uicolor, s + ".uicolor");
addsaver(cs.lefthanded, s + ".lefthanded");
}
// R:239, G:208, B:207
unsigned int skincolors[] = { 7, 0xD0D0D0FF, 0xEFD0C9FF, 0xC77A58FF, 0xA58869FF, 0x602010FF, 0xFFDCB1FF, 0xEDE4C8FF };
unsigned int haircolors[] = { 8, 0x686868FF, 0x8C684AFF, 0xF2E1AEFF, 0xB55239FF, 0xFFFFFFFF, 0x804000FF, 0x502810FF, 0x301800FF };
unsigned int dresscolors[] = { 6, 0xC00000FF, 0x00C000FF, 0x0000C0FF, 0xC0C000FF, 0xC0C0C0FF, 0x202020FF };
unsigned int dresscolors2[] = { 7, 0x8080FFC0, 0x80FF80C0, 0xFF8080C0, 0xFFFF80C0, 0xFF80FFC0, 0x80FFFFC0, 0xFFFFFF80 };
unsigned int swordcolors[] = { 6, 0xC0C0C0FF, 0xFFFFFFFF, 0xFFC0C0FF, 0xC0C0FFFF, 0x808080FF, 0x202020FF };
unsigned int eyecolors[] = { 4, 0x00C000FF, 0x0000C0FF, 0xC00000FF, 0xC0C000FF, 0x804010FF, 0x00C000FF };
EX void initcs(charstyle &cs) {
cs.charid = 0;
cs.skincolor = 0xD0D0D0FF;
cs.haircolor = 0x686868FF;
cs.dresscolor = 0xC00000FF;
cs.swordcolor = 0xD0D0D0FF;
cs.dresscolor2= 0x8080FFC0;
cs.uicolor = 0xFF0000FF;
cs.eyecolor = 0x603000FF;
cs.bowcolor = 0x603000FF;
cs.bowcolor2 = 0xFFD500FF;
cs.lefthanded = false;
}
EX void savecolortable(colortable& ct, string name) {
for(int i=0; i<isize(ct); i++)
addsaver(ct[i], "color:" + name + ":" + its(i));
}
EX purehookset hooks_configfile;
EX void initConfig() {
// basic config
param_i(vid.flashtime, "flashtime", 8);
param_enum(vid.msgleft, "message_style", "message style", 2)
-> editable({{"centered", ""}, {"left-aligned", ""}, {"line-broken", ""}}, "message style", 'a');
param_i(vid.msglimit, "message limit", 5);
param_i(vid.timeformat, "message log time format", 0);
param_b(resizable, "resizable", true)
-> editable("resizable window", 'r');
param_b(no_find_player, "no_find_player");
param_b(game_keys_scroll, "game_keys_scroll")
-> editable("pure exploration (game keys scroll)", 'P');
param_b(reg3::cubes_reg3, "cubes_reg3");
param_f(linepatterns::tree_starter, "tree_starter")